]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/random/harvest.c
This commit was generated by cvs2svn to compensate for changes in r107609,
[FreeBSD/FreeBSD.git] / sys / dev / random / harvest.c
1 /*-
2  * Copyright (c) 2000 Mark R V Murray
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/types.h>
32 #include <sys/queue.h>
33 #include <sys/kthread.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/poll.h>
37 #include <sys/selinfo.h>
38 #include <sys/sysctl.h>
39 #include <sys/random.h>
40
41 #include <machine/cpu.h>
42
43 #include <dev/random/randomdev.h>
44
45 static int read_random_phony(void *, int);
46
47 /* Structure holding the desired entropy sources */
48 struct harvest_select harvest = { 0, 0, 0 };
49
50 /* hold the address of the routine which is actually called if
51  * the randomdev is loaded
52  */
53 static void (*reap_func)(u_int64_t, void *, u_int, u_int, u_int, enum esource)
54     = NULL;
55 static int (*read_func)(void *, int) = read_random_phony;
56
57 /* Initialise the harvester at load time */
58 void
59 random_init_harvester(void (*reaper)(u_int64_t, void *, u_int, u_int, u_int,
60     enum esource), int (*reader)(void *, int))
61 {
62         reap_func = reaper;
63         read_func = reader;
64 }
65
66 /* Deinitialise the harvester at unload time */
67 void
68 random_deinit_harvester(void)
69 {
70         reap_func = NULL;
71         read_func = read_random_phony;
72 }
73
74 /* Entropy harvesting routine. This is supposed to be fast; do
75  * not do anything slow in here!
76  * Implemented as in indirect call to allow non-inclusion of
77  * the entropy device.
78  */
79 void
80 random_harvest(void *entropy, u_int count, u_int bits, u_int frac,
81     enum esource origin)
82 {
83         if (reap_func)
84                 (*reap_func)(get_cyclecount(), entropy, count, bits, frac,
85                     origin);
86 }
87
88 /* Userland-visible version of read_random */
89 int
90 read_random(void *buf, int count)
91 {
92         return (*read_func)(buf, count);
93 }
94
95 /* If the entropy device is not loaded, make a token effort to
96  * provide _some_ kind of randomness. This should only be used
97  * inside other RNG's, like arc4random(9).
98  */
99 static int
100 read_random_phony(void *buf, int count)
101 {
102         u_long randval;
103         int size, i;
104         static int initialised = 0;
105
106         /* Try to give random(9) a half decent initialisation
107          * DO NOT make the mistake of thinking this is secure!!
108          */
109         if (!initialised)
110                 srandom((u_long)get_cyclecount());
111
112         /* Fill buf[] with random(9) output */
113         for (i = 0; i < count; i+= (int)sizeof(u_long)) {
114                 randval = random();
115                 size = (count - i) < (int)sizeof(u_long)
116                     ? (count - i)
117                     : sizeof(u_long);
118                 memcpy(&((char *)buf)[i], &randval, (size_t)size);
119         }
120
121         return count;
122 }
123
124 /* Helper routine to enable kthread_exit() to work while the module is
125  * being (or has been) unloaded.
126  * This routine is in this file because it is always linked into the kernel,
127  * and will thus never be unloaded. This is critical for unloadable modules
128  * that have threads.
129  */
130 void
131 random_set_wakeup_exit(void *control)
132 {
133         wakeup(control);
134         mtx_lock(&Giant);
135         kthread_exit(0);
136         /* NOTREACHED */
137 }