prng.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright IBM Corp. 2006,2007
  3. * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
  4. * Driver for the s390 pseudo random number generator
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/miscdevice.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/random.h>
  13. #include <linux/slab.h>
  14. #include <asm/debug.h>
  15. #include <asm/uaccess.h>
  16. #include "crypt_s390.h"
  17. MODULE_LICENSE("GPL");
  18. MODULE_AUTHOR("Jan Glauber <jan.glauber@de.ibm.com>");
  19. MODULE_DESCRIPTION("s390 PRNG interface");
  20. static int prng_chunk_size = 256;
  21. module_param(prng_chunk_size, int, S_IRUSR | S_IRGRP | S_IROTH);
  22. MODULE_PARM_DESC(prng_chunk_size, "PRNG read chunk size in bytes");
  23. static int prng_entropy_limit = 4096;
  24. module_param(prng_entropy_limit, int, S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
  25. MODULE_PARM_DESC(prng_entropy_limit,
  26. "PRNG add entropy after that much bytes were produced");
  27. /*
  28. * Any one who considers arithmetical methods of producing random digits is,
  29. * of course, in a state of sin. -- John von Neumann
  30. */
  31. struct s390_prng_data {
  32. unsigned long count; /* how many bytes were produced */
  33. char *buf;
  34. };
  35. static struct s390_prng_data *p;
  36. /* copied from libica, use a non-zero initial parameter block */
  37. static unsigned char parm_block[32] = {
  38. 0x0F,0x2B,0x8E,0x63,0x8C,0x8E,0xD2,0x52,0x64,0xB7,0xA0,0x7B,0x75,0x28,0xB8,0xF4,
  39. 0x75,0x5F,0xD2,0xA6,0x8D,0x97,0x11,0xFF,0x49,0xD8,0x23,0xF3,0x7E,0x21,0xEC,0xA0,
  40. };
  41. static int prng_open(struct inode *inode, struct file *file)
  42. {
  43. return nonseekable_open(inode, file);
  44. }
  45. static void prng_add_entropy(void)
  46. {
  47. __u64 entropy[4];
  48. unsigned int i;
  49. int ret;
  50. for (i = 0; i < 16; i++) {
  51. ret = crypt_s390_kmc(KMC_PRNG, parm_block, (char *)entropy,
  52. (char *)entropy, sizeof(entropy));
  53. BUG_ON(ret < 0 || ret != sizeof(entropy));
  54. memcpy(parm_block, entropy, sizeof(entropy));
  55. }
  56. }
  57. static void prng_seed(int nbytes)
  58. {
  59. char buf[16];
  60. int i = 0;
  61. BUG_ON(nbytes > 16);
  62. get_random_bytes(buf, nbytes);
  63. /* Add the entropy */
  64. while (nbytes >= 8) {
  65. *((__u64 *)parm_block) ^= *((__u64 *)(buf+i));
  66. prng_add_entropy();
  67. i += 8;
  68. nbytes -= 8;
  69. }
  70. prng_add_entropy();
  71. }
  72. static ssize_t prng_read(struct file *file, char __user *ubuf, size_t nbytes,
  73. loff_t *ppos)
  74. {
  75. int chunk, n;
  76. int ret = 0;
  77. int tmp;
  78. /* nbytes can be arbitrary length, we split it into chunks */
  79. while (nbytes) {
  80. /* same as in extract_entropy_user in random.c */
  81. if (need_resched()) {
  82. if (signal_pending(current)) {
  83. if (ret == 0)
  84. ret = -ERESTARTSYS;
  85. break;
  86. }
  87. schedule();
  88. }
  89. /*
  90. * we lose some random bytes if an attacker issues
  91. * reads < 8 bytes, but we don't care
  92. */
  93. chunk = min_t(int, nbytes, prng_chunk_size);
  94. /* PRNG only likes multiples of 8 bytes */
  95. n = (chunk + 7) & -8;
  96. if (p->count > prng_entropy_limit)
  97. prng_seed(8);
  98. /* if the CPU supports PRNG stckf is present too */
  99. asm volatile(".insn s,0xb27c0000,%0"
  100. : "=m" (*((unsigned long long *)p->buf)) : : "cc");
  101. /*
  102. * Beside the STCKF the input for the TDES-EDE is the output
  103. * of the last operation. We differ here from X9.17 since we
  104. * only store one timestamp into the buffer. Padding the whole
  105. * buffer with timestamps does not improve security, since
  106. * successive stckf have nearly constant offsets.
  107. * If an attacker knows the first timestamp it would be
  108. * trivial to guess the additional values. One timestamp
  109. * is therefore enough and still guarantees unique input values.
  110. *
  111. * Note: you can still get strict X9.17 conformity by setting
  112. * prng_chunk_size to 8 bytes.
  113. */
  114. tmp = crypt_s390_kmc(KMC_PRNG, parm_block, p->buf, p->buf, n);
  115. BUG_ON((tmp < 0) || (tmp != n));
  116. p->count += n;
  117. if (copy_to_user(ubuf, p->buf, chunk))
  118. return -EFAULT;
  119. nbytes -= chunk;
  120. ret += chunk;
  121. ubuf += chunk;
  122. }
  123. return ret;
  124. }
  125. static const struct file_operations prng_fops = {
  126. .owner = THIS_MODULE,
  127. .open = &prng_open,
  128. .release = NULL,
  129. .read = &prng_read,
  130. .llseek = noop_llseek,
  131. };
  132. static struct miscdevice prng_dev = {
  133. .name = "prandom",
  134. .minor = MISC_DYNAMIC_MINOR,
  135. .fops = &prng_fops,
  136. };
  137. static int __init prng_init(void)
  138. {
  139. int ret;
  140. /* check if the CPU has a PRNG */
  141. if (!crypt_s390_func_available(KMC_PRNG, CRYPT_S390_MSA))
  142. return -EOPNOTSUPP;
  143. if (prng_chunk_size < 8)
  144. return -EINVAL;
  145. p = kmalloc(sizeof(struct s390_prng_data), GFP_KERNEL);
  146. if (!p)
  147. return -ENOMEM;
  148. p->count = 0;
  149. p->buf = kmalloc(prng_chunk_size, GFP_KERNEL);
  150. if (!p->buf) {
  151. ret = -ENOMEM;
  152. goto out_free;
  153. }
  154. /* initialize the PRNG, add 128 bits of entropy */
  155. prng_seed(16);
  156. ret = misc_register(&prng_dev);
  157. if (ret)
  158. goto out_buf;
  159. return 0;
  160. out_buf:
  161. kfree(p->buf);
  162. out_free:
  163. kfree(p);
  164. return ret;
  165. }
  166. static void __exit prng_exit(void)
  167. {
  168. /* wipe me */
  169. kzfree(p->buf);
  170. kfree(p);
  171. misc_deregister(&prng_dev);
  172. }
  173. module_init(prng_init);
  174. module_exit(prng_exit);