random.c 7.7 KB

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