2 * Copyright (c) 2000-2013 Mark R V Murray
3 * Copyright (c) 2004 Robert N. M. Watson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #if !defined(YARROW_RNG) && !defined(FORTUNA_RNG)
31 #elif defined(YARROW_RNG) && defined(FORTUNA_RNG)
32 #error "Must define either YARROW_RNG or FORTUNA_RNG"
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
47 #include <sys/random.h>
48 #include <sys/selinfo.h>
49 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
53 #include <machine/bus.h>
54 #include <machine/cpu.h>
56 #include <dev/random/random_adaptors.h>
57 #include <dev/random/randomdev.h>
58 #include <dev/random/randomdev_soft.h>
59 #if defined(YARROW_RNG)
60 #include <dev/random/yarrow.h>
62 #if defined(FORTUNA_RNG)
63 #include <dev/random/fortuna.h>
66 #include "random_harvestq.h"
68 static int randomdev_poll(int event, struct thread *td);
69 static int randomdev_block(int flag);
70 static void randomdev_flush_reseed(void);
72 #if defined(YARROW_RNG)
73 static struct random_adaptor random_context = {
74 .ident = "Software, Yarrow",
75 .init = randomdev_init,
76 .deinit = randomdev_deinit,
77 .block = randomdev_block,
78 .read = random_yarrow_read,
79 .write = randomdev_write,
80 .poll = randomdev_poll,
81 .reseed = randomdev_flush_reseed,
84 #define RANDOM_MODULE_NAME yarrow
85 #define RANDOM_CSPRNG_NAME "yarrow"
88 #if defined(FORTUNA_RNG)
89 static struct random_adaptor random_context = {
90 .ident = "Software, Fortuna",
91 .init = randomdev_init,
92 .deinit = randomdev_deinit,
93 .block = randomdev_block,
94 .read = random_fortuna_read,
95 .write = randomdev_write,
96 .poll = randomdev_poll,
97 .reseed = randomdev_flush_reseed,
100 #define RANDOM_MODULE_NAME fortuna
101 #define RANDOM_CSPRNG_NAME "fortuna"
105 /* List for the dynamic sysctls */
106 static struct sysctl_ctx_list random_clist;
110 random_check_boolean(SYSCTL_HANDLER_ARGS)
112 if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0)
113 *(u_int *)(oidp->oid_arg1) = 1;
114 return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
120 struct sysctl_oid *random_sys_o, *random_sys_harvest_o;
122 #if defined(YARROW_RNG)
123 random_yarrow_init_alg(&random_clist);
125 #if defined(FORTUNA_RNG)
126 random_fortuna_init_alg(&random_clist);
129 random_sys_o = SYSCTL_ADD_NODE(&random_clist,
130 SYSCTL_STATIC_CHILDREN(_kern_random),
131 OID_AUTO, "sys", CTLFLAG_RW, 0,
132 "Entropy Device Parameters");
134 SYSCTL_ADD_PROC(&random_clist,
135 SYSCTL_CHILDREN(random_sys_o),
136 OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW,
137 &random_context.seeded, 1, random_check_boolean, "I",
140 random_sys_harvest_o = SYSCTL_ADD_NODE(&random_clist,
141 SYSCTL_CHILDREN(random_sys_o),
142 OID_AUTO, "harvest", CTLFLAG_RW, 0,
145 SYSCTL_ADD_PROC(&random_clist,
146 SYSCTL_CHILDREN(random_sys_harvest_o),
147 OID_AUTO, "ethernet", CTLTYPE_INT | CTLFLAG_RW,
148 &harvest.ethernet, 1, random_check_boolean, "I",
149 "Harvest NIC entropy");
150 SYSCTL_ADD_PROC(&random_clist,
151 SYSCTL_CHILDREN(random_sys_harvest_o),
152 OID_AUTO, "point_to_point", CTLTYPE_INT | CTLFLAG_RW,
153 &harvest.point_to_point, 1, random_check_boolean, "I",
154 "Harvest serial net entropy");
155 SYSCTL_ADD_PROC(&random_clist,
156 SYSCTL_CHILDREN(random_sys_harvest_o),
157 OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW,
158 &harvest.interrupt, 0, random_check_boolean, "I",
159 "Harvest IRQ entropy");
160 SYSCTL_ADD_PROC(&random_clist,
161 SYSCTL_CHILDREN(random_sys_harvest_o),
162 OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW,
163 &harvest.swi, 0, random_check_boolean, "I",
164 "Harvest SWI entropy");
166 random_harvestq_init(random_process_event);
168 /* Register the randomness harvesting routine */
169 randomdev_init_harvester(random_harvestq_internal,
170 random_context.read);
174 randomdev_deinit(void)
176 /* Deregister the randomness harvesting routine */
177 randomdev_deinit_harvester();
180 * Command the hash/reseed thread to end and wait for it to finish
182 random_kthread_control = -1;
183 tsleep((void *)&random_kthread_control, 0, "term", 0);
185 #if defined(YARROW_RNG)
186 random_yarrow_deinit_alg();
188 #if defined(FORTUNA_RNG)
189 random_fortuna_deinit_alg();
192 sysctl_ctx_free(&random_clist);
196 randomdev_write(void *buf, int count)
202 * Break the input up into HARVESTSIZE chunks. The writer has too
203 * much control here, so "estimate" the entropy as zero.
205 for (i = 0; i < count; i += HARVESTSIZE) {
207 if (i + chunk >= count)
208 chunk = (u_int)(count - i);
209 random_harvestq_internal(get_cyclecount(), (char *)buf + i,
210 chunk, 0, 0, RANDOM_WRITE);
215 randomdev_unblock(void)
217 if (!random_context.seeded) {
218 random_context.seeded = 1;
219 selwakeuppri(&random_context.rsel, PUSER);
220 wakeup(&random_context);
222 (void)atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_NONE,
227 randomdev_poll(int events, struct thread *td)
230 mtx_lock(&random_reseed_mtx);
232 if (random_context.seeded)
233 revents = events & (POLLIN | POLLRDNORM);
235 selrecord(td, &random_context.rsel);
237 mtx_unlock(&random_reseed_mtx);
242 randomdev_block(int flag)
246 mtx_lock(&random_reseed_mtx);
249 while (!random_context.seeded && !error) {
250 if (flag & O_NONBLOCK)
253 printf("Entropy device is blocking.\n");
254 error = msleep(&random_context,
256 PUSER | PCATCH, "block", 0);
259 mtx_unlock(&random_reseed_mtx);
264 /* Helper routine to perform explicit reseeds */
266 randomdev_flush_reseed(void)
268 /* Command a entropy queue flush and wait for it to finish */
269 random_kthread_control = 1;
270 while (random_kthread_control)
273 #if defined(YARROW_RNG)
274 random_yarrow_reseed();
276 #if defined(FORTUNA_RNG)
277 random_fortuna_reseed();
282 randomdev_modevent(module_t mod, int type, void *unused)
287 random_adaptor_register(RANDOM_CSPRNG_NAME, &random_context);
289 * For statically built kernels that contain both device
290 * random and options PADLOCK_RNG/RDRAND_RNG/etc..,
291 * this event handler will do nothing, since the random
292 * driver-specific handlers are loaded after these HW
293 * consumers, and hence hasn't yet registered for this event.
295 * In case where both the random driver and RNG's are built
296 * as seperate modules, random.ko is loaded prior to *_rng.ko's
297 * (by dependency). This event handler is there to delay
298 * creation of /dev/{u,}random and attachment of this *_rng.ko.
300 EVENTHANDLER_INVOKE(random_adaptor_attach, &random_context);
307 RANDOM_ADAPTOR_MODULE(RANDOM_MODULE_NAME, randomdev_modevent, 1);