random.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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-2007 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. #ifndef AUTOCONF_INCLUDED
  38. #include <linux/config.h>
  39. #endif
  40. #include <linux/module.h>
  41. #include <linux/init.h>
  42. #include <linux/list.h>
  43. #include <linux/slab.h>
  44. #include <linux/wait.h>
  45. #include <linux/sched.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/version.h>
  48. #include <linux/unistd.h>
  49. #include <linux/poll.h>
  50. #include <linux/random.h>
  51. #include <cryptodev.h>
  52. #ifdef CONFIG_OCF_FIPS
  53. #include "rndtest.h"
  54. #endif
  55. #ifndef HAS_RANDOM_INPUT_WAIT
  56. #error "Please do not enable OCF_RANDOMHARVEST unless you have applied patches"
  57. #endif
  58. /*
  59. * a hack to access the debug levels from the crypto driver
  60. */
  61. extern int crypto_debug;
  62. #define debug crypto_debug
  63. /*
  64. * a list of all registered random providers
  65. */
  66. static LIST_HEAD(random_ops);
  67. static int started = 0;
  68. static int initted = 0;
  69. struct random_op {
  70. struct list_head random_list;
  71. u_int32_t driverid;
  72. int (*read_random)(void *arg, u_int32_t *buf, int len);
  73. void *arg;
  74. };
  75. static int random_proc(void *arg);
  76. static pid_t randomproc = (pid_t) -1;
  77. static spinlock_t random_lock;
  78. /*
  79. * just init the spin locks
  80. */
  81. static int
  82. crypto_random_init(void)
  83. {
  84. spin_lock_init(&random_lock);
  85. initted = 1;
  86. return(0);
  87. }
  88. /*
  89. * Add the given random reader to our list (if not present)
  90. * and start the thread (if not already started)
  91. *
  92. * we have to assume that driver id is ok for now
  93. */
  94. int
  95. crypto_rregister(
  96. u_int32_t driverid,
  97. int (*read_random)(void *arg, u_int32_t *buf, int len),
  98. void *arg)
  99. {
  100. unsigned long flags;
  101. int ret = 0;
  102. struct random_op *rops, *tmp;
  103. dprintk("%s,%d: %s(0x%x, %p, %p)\n", __FILE__, __LINE__,
  104. __FUNCTION__, driverid, read_random, arg);
  105. if (!initted)
  106. crypto_random_init();
  107. #if 0
  108. struct cryptocap *cap;
  109. cap = crypto_checkdriver(driverid);
  110. if (!cap)
  111. return EINVAL;
  112. #endif
  113. list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
  114. if (rops->driverid == driverid && rops->read_random == read_random)
  115. return EEXIST;
  116. }
  117. rops = (struct random_op *) kmalloc(sizeof(*rops), GFP_KERNEL);
  118. if (!rops)
  119. return ENOMEM;
  120. rops->driverid = driverid;
  121. rops->read_random = read_random;
  122. rops->arg = arg;
  123. spin_lock_irqsave(&random_lock, flags);
  124. list_add_tail(&rops->random_list, &random_ops);
  125. if (!started) {
  126. randomproc = kernel_thread(random_proc, NULL, CLONE_FS|CLONE_FILES);
  127. if (randomproc < 0) {
  128. ret = randomproc;
  129. printk("crypto: crypto_rregister cannot start random thread; "
  130. "error %d", ret);
  131. } else
  132. started = 1;
  133. }
  134. spin_unlock_irqrestore(&random_lock, flags);
  135. return ret;
  136. }
  137. EXPORT_SYMBOL(crypto_rregister);
  138. int
  139. crypto_runregister_all(u_int32_t driverid)
  140. {
  141. struct random_op *rops, *tmp;
  142. unsigned long flags;
  143. dprintk("%s,%d: %s(0x%x)\n", __FILE__, __LINE__, __FUNCTION__, driverid);
  144. list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
  145. if (rops->driverid == driverid) {
  146. list_del(&rops->random_list);
  147. kfree(rops);
  148. }
  149. }
  150. spin_lock_irqsave(&random_lock, flags);
  151. if (list_empty(&random_ops) && started)
  152. kill_proc(randomproc, SIGKILL, 1);
  153. spin_unlock_irqrestore(&random_lock, flags);
  154. return(0);
  155. }
  156. EXPORT_SYMBOL(crypto_runregister_all);
  157. /*
  158. * while we can add entropy to random.c continue to read random data from
  159. * the drivers and push it to random.
  160. */
  161. static int
  162. random_proc(void *arg)
  163. {
  164. int n;
  165. int wantcnt;
  166. int bufcnt = 0;
  167. int retval = 0;
  168. int *buf = NULL;
  169. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  170. daemonize();
  171. spin_lock_irq(&current->sigmask_lock);
  172. sigemptyset(&current->blocked);
  173. recalc_sigpending(current);
  174. spin_unlock_irq(&current->sigmask_lock);
  175. sprintf(current->comm, "ocf-random");
  176. #else
  177. daemonize("ocf-random");
  178. allow_signal(SIGKILL);
  179. #endif
  180. (void) get_fs();
  181. set_fs(get_ds());
  182. #ifdef CONFIG_OCF_FIPS
  183. #define NUM_INT (RNDTEST_NBYTES/sizeof(int))
  184. #else
  185. #define NUM_INT 32
  186. #endif
  187. /*
  188. * some devices can transferr their RNG data direct into memory,
  189. * so make sure it is device friendly
  190. */
  191. buf = kmalloc(NUM_INT * sizeof(int), GFP_DMA);
  192. if (NULL == buf) {
  193. printk("crypto: RNG could not allocate memory\n");
  194. retval = -ENOMEM;
  195. goto bad_alloc;
  196. }
  197. wantcnt = NUM_INT; /* start by adding some entropy */
  198. /*
  199. * its possible due to errors or driver removal that we no longer
  200. * have anything to do, if so exit or we will consume all the CPU
  201. * doing nothing
  202. */
  203. while (!list_empty(&random_ops)) {
  204. struct random_op *rops, *tmp;
  205. #ifdef CONFIG_OCF_FIPS
  206. if (wantcnt)
  207. wantcnt = NUM_INT; /* FIPs mode can do 20000 bits or none */
  208. #endif
  209. /* see if we can get enough entropy to make the world
  210. * a better place.
  211. */
  212. while (bufcnt < wantcnt && bufcnt < NUM_INT) {
  213. list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
  214. n = (*rops->read_random)(rops->arg, &buf[bufcnt],
  215. NUM_INT - bufcnt);
  216. /* on failure remove the random number generator */
  217. if (n == -1) {
  218. list_del(&rops->random_list);
  219. printk("crypto: RNG (driverid=0x%x) failed, disabling\n",
  220. rops->driverid);
  221. kfree(rops);
  222. } else if (n > 0)
  223. bufcnt += n;
  224. }
  225. /* give up CPU for a bit, just in case as this is a loop */
  226. schedule();
  227. }
  228. #ifdef CONFIG_OCF_FIPS
  229. if (bufcnt > 0 && rndtest_buf((unsigned char *) &buf[0])) {
  230. dprintk("crypto: buffer had fips errors, discarding\n");
  231. bufcnt = 0;
  232. }
  233. #endif
  234. /*
  235. * if we have a certified buffer, we can send some data
  236. * to /dev/random and move along
  237. */
  238. if (bufcnt > 0) {
  239. /* add what we have */
  240. random_input_words(buf, bufcnt, bufcnt*sizeof(int)*8);
  241. bufcnt = 0;
  242. }
  243. /* give up CPU for a bit so we don't hog while filling */
  244. schedule();
  245. /* wait for needing more */
  246. wantcnt = random_input_wait();
  247. if (wantcnt <= 0)
  248. wantcnt = 0; /* try to get some info again */
  249. else
  250. /* round up to one word or we can loop forever */
  251. wantcnt = (wantcnt + (sizeof(int)*8)) / (sizeof(int)*8);
  252. if (wantcnt > NUM_INT) {
  253. wantcnt = NUM_INT;
  254. }
  255. if (signal_pending(current)) {
  256. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  257. spin_lock_irq(&current->sigmask_lock);
  258. #endif
  259. flush_signals(current);
  260. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  261. spin_unlock_irq(&current->sigmask_lock);
  262. #endif
  263. }
  264. }
  265. kfree(buf);
  266. bad_alloc:
  267. spin_lock_irq(&random_lock);
  268. randomproc = (pid_t) -1;
  269. started = 0;
  270. spin_unlock_irq(&random_lock);
  271. return retval;
  272. }