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