random.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * A system independant way of adding entropy to the kernels pool
  3. * this way the drivers can focus on the real work and we can take
  4. * care of pushing it to the appropriate place in the kernel.
  5. *
  6. * This should be fast and callable from timers/interrupts
  7. *
  8. * Written by David McCullough <[email protected]>
  9. * Copyright (C) 2006-2010 David McCullough
  10. * Copyright (C) 2004-2005 Intel Corporation.
  11. *
  12. * LICENSE TERMS
  13. *
  14. * The free distribution and use of this software in both source and binary
  15. * form is allowed (with or without changes) provided that:
  16. *
  17. * 1. distributions of this source code include the above copyright
  18. * notice, this list of conditions and the following disclaimer;
  19. *
  20. * 2. distributions in binary form include the above copyright
  21. * notice, this list of conditions and the following disclaimer
  22. * in the documentation and/or other associated materials;
  23. *
  24. * 3. the copyright holder's name is not used to endorse products
  25. * built using this software without specific written permission.
  26. *
  27. * ALTERNATIVELY, provided that this notice is retained in full, this product
  28. * may be distributed under the terms of the GNU General Public License (GPL),
  29. * in which case the provisions of the GPL apply INSTEAD OF those given above.
  30. *
  31. * DISCLAIMER
  32. *
  33. * This software is provided 'as is' with no explicit or implied warranties
  34. * in respect of its properties, including, but not limited to, correctness
  35. * and/or fitness for purpose.
  36. */
  37. #include <linux/version.h>
  38. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33))
  39. #include <generated/autoconf.h>
  40. #else
  41. #include <linux/autoconf.h>
  42. #endif
  43. #include <linux/module.h>
  44. #include <linux/init.h>
  45. #include <linux/list.h>
  46. #include <linux/slab.h>
  47. #include <linux/wait.h>
  48. #include <linux/sched.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/version.h>
  51. #include <linux/unistd.h>
  52. #include <linux/poll.h>
  53. #include <linux/random.h>
  54. #include <cryptodev.h>
  55. #ifdef CONFIG_OCF_FIPS
  56. #include "rndtest.h"
  57. #endif
  58. #ifndef HAS_RANDOM_INPUT_WAIT
  59. #error "Please do not enable OCF_RANDOMHARVEST unless you have applied patches"
  60. #endif
  61. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
  62. #include <linux/sched.h>
  63. #define kill_proc(p,s,v) send_sig(s,find_task_by_vpid(p),0)
  64. #endif
  65. /*
  66. * a hack to access the debug levels from the crypto driver
  67. */
  68. extern int crypto_debug;
  69. #define debug crypto_debug
  70. /*
  71. * a list of all registered random providers
  72. */
  73. static LIST_HEAD(random_ops);
  74. static int started = 0;
  75. static int initted = 0;
  76. struct random_op {
  77. struct list_head random_list;
  78. u_int32_t driverid;
  79. int (*read_random)(void *arg, u_int32_t *buf, int len);
  80. void *arg;
  81. };
  82. static int random_proc(void *arg);
  83. static pid_t randomproc = (pid_t) -1;
  84. static spinlock_t random_lock;
  85. /*
  86. * just init the spin locks
  87. */
  88. static int
  89. crypto_random_init(void)
  90. {
  91. spin_lock_init(&random_lock);
  92. initted = 1;
  93. return(0);
  94. }
  95. /*
  96. * Add the given random reader to our list (if not present)
  97. * and start the thread (if not already started)
  98. *
  99. * we have to assume that driver id is ok for now
  100. */
  101. int
  102. crypto_rregister(
  103. u_int32_t driverid,
  104. int (*read_random)(void *arg, u_int32_t *buf, int len),
  105. void *arg)
  106. {
  107. unsigned long flags;
  108. int ret = 0;
  109. struct random_op *rops, *tmp;
  110. dprintk("%s,%d: %s(0x%x, %p, %p)\n", __FILE__, __LINE__,
  111. __FUNCTION__, driverid, read_random, arg);
  112. if (!initted)
  113. crypto_random_init();
  114. #if 0
  115. struct cryptocap *cap;
  116. cap = crypto_checkdriver(driverid);
  117. if (!cap)
  118. return EINVAL;
  119. #endif
  120. list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
  121. if (rops->driverid == driverid && rops->read_random == read_random)
  122. return EEXIST;
  123. }
  124. rops = (struct random_op *) kmalloc(sizeof(*rops), GFP_KERNEL);
  125. if (!rops)
  126. return ENOMEM;
  127. rops->driverid = driverid;
  128. rops->read_random = read_random;
  129. rops->arg = arg;
  130. spin_lock_irqsave(&random_lock, flags);
  131. list_add_tail(&rops->random_list, &random_ops);
  132. if (!started) {
  133. randomproc = kernel_thread(random_proc, NULL, CLONE_FS|CLONE_FILES);
  134. if (randomproc < 0) {
  135. ret = randomproc;
  136. printk("crypto: crypto_rregister cannot start random thread; "
  137. "error %d", ret);
  138. } else
  139. started = 1;
  140. }
  141. spin_unlock_irqrestore(&random_lock, flags);
  142. return ret;
  143. }
  144. EXPORT_SYMBOL(crypto_rregister);
  145. int
  146. crypto_runregister_all(u_int32_t driverid)
  147. {
  148. struct random_op *rops, *tmp;
  149. unsigned long flags;
  150. dprintk("%s,%d: %s(0x%x)\n", __FILE__, __LINE__, __FUNCTION__, driverid);
  151. list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
  152. if (rops->driverid == driverid) {
  153. list_del(&rops->random_list);
  154. kfree(rops);
  155. }
  156. }
  157. spin_lock_irqsave(&random_lock, flags);
  158. if (list_empty(&random_ops) && started)
  159. kill_proc(randomproc, SIGKILL, 1);
  160. spin_unlock_irqrestore(&random_lock, flags);
  161. return(0);
  162. }
  163. EXPORT_SYMBOL(crypto_runregister_all);
  164. /*
  165. * while we can add entropy to random.c continue to read random data from
  166. * the drivers and push it to random.
  167. */
  168. static int
  169. random_proc(void *arg)
  170. {
  171. int n;
  172. int wantcnt;
  173. int bufcnt = 0;
  174. int retval = 0;
  175. int *buf = NULL;
  176. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  177. daemonize();
  178. spin_lock_irq(&current->sigmask_lock);
  179. sigemptyset(&current->blocked);
  180. recalc_sigpending(current);
  181. spin_unlock_irq(&current->sigmask_lock);
  182. sprintf(current->comm, "ocf-random");
  183. #else
  184. daemonize("ocf-random");
  185. allow_signal(SIGKILL);
  186. #endif
  187. (void) get_fs();
  188. set_fs(get_ds());
  189. #ifdef CONFIG_OCF_FIPS
  190. #define NUM_INT (RNDTEST_NBYTES/sizeof(int))
  191. #else
  192. #define NUM_INT 32
  193. #endif
  194. /*
  195. * some devices can transferr their RNG data direct into memory,
  196. * so make sure it is device friendly
  197. */
  198. buf = kmalloc(NUM_INT * sizeof(int), GFP_DMA);
  199. if (NULL == buf) {
  200. printk("crypto: RNG could not allocate memory\n");
  201. retval = -ENOMEM;
  202. goto bad_alloc;
  203. }
  204. wantcnt = NUM_INT; /* start by adding some entropy */
  205. /*
  206. * its possible due to errors or driver removal that we no longer
  207. * have anything to do, if so exit or we will consume all the CPU
  208. * doing nothing
  209. */
  210. while (!list_empty(&random_ops)) {
  211. struct random_op *rops, *tmp;
  212. #ifdef CONFIG_OCF_FIPS
  213. if (wantcnt)
  214. wantcnt = NUM_INT; /* FIPs mode can do 20000 bits or none */
  215. #endif
  216. /* see if we can get enough entropy to make the world
  217. * a better place.
  218. */
  219. while (bufcnt < wantcnt && bufcnt < NUM_INT) {
  220. list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
  221. n = (*rops->read_random)(rops->arg, &buf[bufcnt],
  222. NUM_INT - bufcnt);
  223. /* on failure remove the random number generator */
  224. if (n == -1) {
  225. list_del(&rops->random_list);
  226. printk("crypto: RNG (driverid=0x%x) failed, disabling\n",
  227. rops->driverid);
  228. kfree(rops);
  229. } else if (n > 0)
  230. bufcnt += n;
  231. }
  232. /* give up CPU for a bit, just in case as this is a loop */
  233. schedule();
  234. }
  235. #ifdef CONFIG_OCF_FIPS
  236. if (bufcnt > 0 && rndtest_buf((unsigned char *) &buf[0])) {
  237. dprintk("crypto: buffer had fips errors, discarding\n");
  238. bufcnt = 0;
  239. }
  240. #endif
  241. /*
  242. * if we have a certified buffer, we can send some data
  243. * to /dev/random and move along
  244. */
  245. if (bufcnt > 0) {
  246. /* add what we have */
  247. random_input_words(buf, bufcnt, bufcnt*sizeof(int)*8);
  248. bufcnt = 0;
  249. }
  250. /* give up CPU for a bit so we don't hog while filling */
  251. schedule();
  252. /* wait for needing more */
  253. wantcnt = random_input_wait();
  254. if (wantcnt <= 0)
  255. wantcnt = 0; /* try to get some info again */
  256. else
  257. /* round up to one word or we can loop forever */
  258. wantcnt = (wantcnt + (sizeof(int)*8)) / (sizeof(int)*8);
  259. if (wantcnt > NUM_INT) {
  260. wantcnt = NUM_INT;
  261. }
  262. if (signal_pending(current)) {
  263. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  264. spin_lock_irq(&current->sigmask_lock);
  265. #endif
  266. flush_signals(current);
  267. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  268. spin_unlock_irq(&current->sigmask_lock);
  269. #endif
  270. }
  271. }
  272. kfree(buf);
  273. bad_alloc:
  274. spin_lock_irq(&random_lock);
  275. randomproc = (pid_t) -1;
  276. started = 0;
  277. spin_unlock_irq(&random_lock);
  278. return retval;
  279. }