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.
6 * This should be fast and callable from timers/interrupts
8 * Written by David McCullough <david_mccullough@mcafee.com>
9 * Copyright (C) 2006-2010 David McCullough
10 * Copyright (C) 2004-2005 Intel Corporation.
14 * The free distribution and use of this software in both source and binary
15 * form is allowed (with or without changes) provided that:
17 * 1. distributions of this source code include the above copyright
18 * notice, this list of conditions and the following disclaimer;
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;
24 * 3. the copyright holder's name is not used to endorse products
25 * built using this software without specific written permission.
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.
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.
38 #include <linux/version.h>
39 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
40 #include <linux/config.h>
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/list.h>
45 #include <linux/slab.h>
46 #include <linux/wait.h>
47 #include <linux/sched.h>
48 #include <linux/spinlock.h>
49 #include <linux/unistd.h>
50 #include <linux/poll.h>
51 #include <linux/random.h>
52 #include <cryptodev.h>
54 #ifdef CONFIG_OCF_FIPS
58 #ifndef HAS_RANDOM_INPUT_WAIT
59 #error "Please do not enable OCF_RANDOMHARVEST unless you have applied patches"
63 * a hack to access the debug levels from the crypto driver
65 extern int crypto_debug;
66 #define debug crypto_debug
69 * a list of all registered random providers
71 static LIST_HEAD(random_ops);
72 static int started = 0;
73 static int initted = 0;
76 struct list_head random_list;
78 int (*read_random)(void *arg, u_int32_t *buf, int len);
82 static int random_proc(void *arg);
84 static pid_t randomproc = (pid_t) -1;
85 static spinlock_t random_lock;
88 * just init the spin locks
91 crypto_random_init(void)
93 spin_lock_init(&random_lock);
99 * Add the given random reader to our list (if not present)
100 * and start the thread (if not already started)
102 * we have to assume that driver id is ok for now
107 int (*read_random)(void *arg, u_int32_t *buf, int len),
112 struct random_op *rops, *tmp;
114 dprintk("%s,%d: %s(0x%x, %p, %p)\n", __FILE__, __LINE__,
115 __FUNCTION__, driverid, read_random, arg);
118 crypto_random_init();
121 struct cryptocap *cap;
123 cap = crypto_checkdriver(driverid);
128 list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
129 if (rops->driverid == driverid && rops->read_random == read_random)
133 rops = (struct random_op *) kmalloc(sizeof(*rops), GFP_KERNEL);
137 rops->driverid = driverid;
138 rops->read_random = read_random;
141 spin_lock_irqsave(&random_lock, flags);
142 list_add_tail(&rops->random_list, &random_ops);
144 randomproc = kernel_thread(random_proc, NULL, CLONE_FS|CLONE_FILES);
145 if (randomproc < 0) {
147 printk("crypto: crypto_rregister cannot start random thread; "
152 spin_unlock_irqrestore(&random_lock, flags);
156 EXPORT_SYMBOL(crypto_rregister);
159 crypto_runregister_all(u_int32_t driverid)
161 struct random_op *rops, *tmp;
164 dprintk("%s,%d: %s(0x%x)\n", __FILE__, __LINE__, __FUNCTION__, driverid);
166 list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
167 if (rops->driverid == driverid) {
168 list_del(&rops->random_list);
173 spin_lock_irqsave(&random_lock, flags);
174 if (list_empty(&random_ops) && started)
175 kill_proc(randomproc, SIGKILL, 1);
176 spin_unlock_irqrestore(&random_lock, flags);
179 EXPORT_SYMBOL(crypto_runregister_all);
182 * while we can add entropy to random.c continue to read random data from
183 * the drivers and push it to random.
186 random_proc(void *arg)
194 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
196 spin_lock_irq(¤t->sigmask_lock);
197 sigemptyset(¤t->blocked);
198 recalc_sigpending(current);
199 spin_unlock_irq(¤t->sigmask_lock);
200 sprintf(current->comm, "ocf-random");
201 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
203 sprintf(current->comm, "ocf-random");
205 daemonize("ocf-random");
206 allow_signal(SIGKILL);
212 #ifdef CONFIG_OCF_FIPS
213 #define NUM_INT (RNDTEST_NBYTES/sizeof(int))
219 * some devices can transferr their RNG data direct into memory,
220 * so make sure it is device friendly
222 buf = kmalloc(NUM_INT * sizeof(int), GFP_DMA);
224 printk("crypto: RNG could not allocate memory\n");
229 wantcnt = NUM_INT; /* start by adding some entropy */
232 * its possible due to errors or driver removal that we no longer
233 * have anything to do, if so exit or we will consume all the CPU
236 while (!list_empty(&random_ops)) {
237 struct random_op *rops, *tmp;
239 #ifdef CONFIG_OCF_FIPS
241 wantcnt = NUM_INT; /* FIPs mode can do 20000 bits or none */
244 /* see if we can get enough entropy to make the world
247 while (bufcnt < wantcnt && bufcnt < NUM_INT) {
248 list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
250 n = (*rops->read_random)(rops->arg, &buf[bufcnt],
253 /* on failure remove the random number generator */
255 list_del(&rops->random_list);
256 printk("crypto: RNG (driverid=0x%x) failed, disabling\n",
262 /* give up CPU for a bit, just in case as this is a loop */
267 #ifdef CONFIG_OCF_FIPS
268 if (bufcnt > 0 && rndtest_buf((unsigned char *) &buf[0])) {
269 dprintk("crypto: buffer had fips errors, discarding\n");
275 * if we have a certified buffer, we can send some data
276 * to /dev/random and move along
279 /* add what we have */
280 random_input_words(buf, bufcnt, bufcnt*sizeof(int)*8);
284 /* give up CPU for a bit so we don't hog while filling */
287 /* wait for needing more */
288 wantcnt = random_input_wait();
291 wantcnt = 0; /* try to get some info again */
293 /* round up to one word or we can loop forever */
294 wantcnt = (wantcnt + (sizeof(int)*8)) / (sizeof(int)*8);
295 if (wantcnt > NUM_INT) {
299 if (signal_pending(current)) {
300 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
301 spin_lock_irq(¤t->sigmask_lock);
303 flush_signals(current);
304 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
305 spin_unlock_irq(¤t->sigmask_lock);
313 spin_lock_irq(&random_lock);
314 randomproc = (pid_t) -1;
316 spin_unlock_irq(&random_lock);