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