]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/dev/random/harvest.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / dev / random / harvest.c
1 /*-
2  * Copyright (c) 2000-2013 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  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/kthread.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/poll.h>
37 #include <sys/queue.h>
38 #include <sys/random.h>
39 #include <sys/selinfo.h>
40 #include <sys/syslog.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
43
44 #include <machine/cpu.h>
45
46 #include <dev/random/randomdev_soft.h>
47
48 static int read_random_phony(void *, int);
49
50 /* Structure holding the desired entropy sources */
51 struct harvest_select harvest = { 1, 1, 1, 1 };
52 static int warned = 0;
53
54 /* hold the address of the routine which is actually called if
55  * the randomdev is loaded
56  */
57 static void (*reap_func)(u_int64_t, const void *, u_int, u_int,
58     enum esource) = NULL;
59 static int (*read_func)(void *, int) = read_random_phony;
60
61 /* Initialise the harvester at load time */
62 void
63 randomdev_init_harvester(void (*reaper)(u_int64_t, const void *, u_int,
64     u_int, enum esource), int (*reader)(void *, int))
65 {
66         reap_func = reaper;
67         read_func = reader;
68 }
69
70 /* Deinitialise the harvester at unload time */
71 void
72 randomdev_deinit_harvester(void)
73 {
74         reap_func = NULL;
75         read_func = read_random_phony;
76         warned = 0;
77 }
78
79 /* Entropy harvesting routine. This is supposed to be fast; do
80  * not do anything slow in here!
81  * Implemented as in indirect call to allow non-inclusion of
82  * the entropy device.
83  *
84  * XXXRW: get_cyclecount() is cheap on most modern hardware, where cycle
85  * counters are built in, but on older hardware it will do a real time clock
86  * read which can be quite expensive.
87  */
88 void
89 random_harvest(void *entropy, u_int count, u_int bits, enum esource origin)
90 {
91         if (reap_func)
92                 (*reap_func)(get_cyclecount(), entropy, count, bits, origin);
93 }
94
95 /* Userland-visible version of read_random */
96 int
97 read_random(void *buf, int count)
98 {
99         return ((*read_func)(buf, count));
100 }
101
102 /* If the entropy device is not loaded, make a token effort to
103  * provide _some_ kind of randomness. This should only be used
104  * inside other RNG's, like arc4random(9).
105  */
106 static int
107 read_random_phony(void *buf, int count)
108 {
109         u_long randval;
110         int size, i;
111
112         if (!warned) {
113                 log(LOG_WARNING, "random device not loaded; using insecure entropy\n");
114                 warned = 1;
115         }
116
117         /* srandom() is called in kern/init_main.c:proc0_post() */
118
119         /* Fill buf[] with random(9) output */
120         for (i = 0; i < count; i+= (int)sizeof(u_long)) {
121                 randval = random();
122                 size = MIN(count - i, sizeof(u_long));
123                 memcpy(&((char *)buf)[i], &randval, (size_t)size);
124         }
125
126         return (count);
127 }
128
129 /* Helper routine to enable kproc_exit() to work while the module is
130  * being (or has been) unloaded.
131  * This routine is in this file because it is always linked into the kernel,
132  * and will thus never be unloaded. This is critical for unloadable modules
133  * that have threads.
134  */
135 void
136 random_set_wakeup_exit(void *control)
137 {
138         wakeup(control);
139         kproc_exit(0);
140         /* NOTREACHED */
141 }