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