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

Archive Download this file



interactive