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