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 .poll = randomdev_poll,
80 .reseed = randomdev_flush_reseed,
81 .seeded = 0, /* This will be seeded during entropy processing */
83 #define RANDOM_MODULE_NAME yarrow
84 #define RANDOM_CSPRNG_NAME "yarrow"
87 #if defined(FORTUNA_RNG)
88 static struct random_adaptor random_context = {
89 .ident = "Software, Fortuna",
90 .init = randomdev_init,
91 .deinit = randomdev_deinit,
92 .block = randomdev_block,
93 .read = random_fortuna_read,
94 .poll = randomdev_poll,
95 .reseed = randomdev_flush_reseed,
96 .seeded = 0, /* This will be excplicitly seeded at startup when secured */
98 #define RANDOM_MODULE_NAME fortuna
99 #define RANDOM_CSPRNG_NAME "fortuna"
102 /* List for the dynamic sysctls */
103 static struct sysctl_ctx_list random_clist;
107 random_check_boolean(SYSCTL_HANDLER_ARGS)
109 if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0)
110 *(u_int *)(oidp->oid_arg1) = 1;
111 return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req));
117 struct sysctl_oid *random_sys_o, *random_sys_harvest_o;
119 #if defined(YARROW_RNG)
120 random_yarrow_init_alg(&random_clist);
122 #if defined(FORTUNA_RNG)
123 random_fortuna_init_alg(&random_clist);
126 random_sys_o = SYSCTL_ADD_NODE(&random_clist,
127 SYSCTL_STATIC_CHILDREN(_kern_random),
128 OID_AUTO, "sys", CTLFLAG_RW, 0,
129 "Entropy Device Parameters");
131 SYSCTL_ADD_PROC(&random_clist,
132 SYSCTL_CHILDREN(random_sys_o),
133 OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW,
134 &random_context.seeded, 0, random_check_boolean, "I",
137 random_sys_harvest_o = SYSCTL_ADD_NODE(&random_clist,
138 SYSCTL_CHILDREN(random_sys_o),
139 OID_AUTO, "harvest", CTLFLAG_RW, 0,
142 SYSCTL_ADD_PROC(&random_clist,
143 SYSCTL_CHILDREN(random_sys_harvest_o),
144 OID_AUTO, "ethernet", CTLTYPE_INT | CTLFLAG_RW,
145 &harvest.ethernet, 1, random_check_boolean, "I",
146 "Harvest NIC entropy");
147 SYSCTL_ADD_PROC(&random_clist,
148 SYSCTL_CHILDREN(random_sys_harvest_o),
149 OID_AUTO, "point_to_point", CTLTYPE_INT | CTLFLAG_RW,
150 &harvest.point_to_point, 1, random_check_boolean, "I",
151 "Harvest serial net entropy");
152 SYSCTL_ADD_PROC(&random_clist,
153 SYSCTL_CHILDREN(random_sys_harvest_o),
154 OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW,
155 &harvest.interrupt, 1, random_check_boolean, "I",
156 "Harvest IRQ entropy");
157 SYSCTL_ADD_PROC(&random_clist,
158 SYSCTL_CHILDREN(random_sys_harvest_o),
159 OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW,
160 &harvest.swi, 1, random_check_boolean, "I",
161 "Harvest SWI entropy");
163 random_harvestq_init(random_process_event);
165 /* Register the randomness harvesting routine */
166 randomdev_init_harvester(random_harvestq_internal,
167 random_context.read);
171 randomdev_deinit(void)
173 /* Deregister the randomness harvesting routine */
174 randomdev_deinit_harvester();
177 * Command the hash/reseed thread to end and wait for it to finish
179 random_kthread_control = -1;
180 tsleep((void *)&random_kthread_control, 0, "term", 0);
182 #if defined(YARROW_RNG)
183 random_yarrow_deinit_alg();
185 #if defined(FORTUNA_RNG)
186 random_fortuna_deinit_alg();
189 sysctl_ctx_free(&random_clist);
193 randomdev_unblock(void)
195 if (!random_context.seeded) {
196 selwakeuppri(&random_context.rsel, PUSER);
197 wakeup(&random_context);
198 printf("random: unblocking device.\n");
199 random_context.seeded = 1;
201 /* Do arc4random(9) a favour while we are about it. */
202 (void)atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_NONE,
207 randomdev_poll(int events, struct thread *td)
211 mtx_lock(&random_reseed_mtx);
213 if (random_context.seeded)
214 revents = events & (POLLIN | POLLRDNORM);
216 selrecord(td, &random_context.rsel);
218 mtx_unlock(&random_reseed_mtx);
223 randomdev_block(int flag)
227 mtx_lock(&random_reseed_mtx);
230 while (!random_context.seeded && !error) {
231 if (flag & O_NONBLOCK)
234 printf("random: blocking on read.\n");
235 error = msleep(&random_context,
237 PUSER | PCATCH, "block", 0);
240 mtx_unlock(&random_reseed_mtx);
245 /* Helper routine to perform explicit reseeds */
247 randomdev_flush_reseed(void)
249 /* Command a entropy queue flush and wait for it to finish */
250 random_kthread_control = 1;
251 while (random_kthread_control)
254 #if defined(YARROW_RNG)
255 /* This ultimately calls randomdev_unblock() */
256 random_yarrow_reseed();
258 #if defined(FORTUNA_RNG)
259 /* This ultimately calls randomdev_unblock() */
260 random_fortuna_reseed();
265 randomdev_modevent(module_t mod __unused, int type, void *unused __unused)
270 random_adaptor_register(RANDOM_CSPRNG_NAME, &random_context);
272 * For statically built kernels that contain both device
273 * random and options PADLOCK_RNG/RDRAND_RNG/etc..,
274 * this event handler will do nothing, since the random
275 * driver-specific handlers are loaded after these HW
276 * consumers, and hence hasn't yet registered for this event.
278 * In case where both the random driver and RNG's are built
279 * as seperate modules, random.ko is loaded prior to *_rng.ko's
280 * (by dependency). This event handler is there to delay
281 * creation of /dev/{u,}random and attachment of this *_rng.ko.
283 EVENTHANDLER_INVOKE(random_adaptor_attach, &random_context);
290 RANDOM_ADAPTOR_MODULE(RANDOM_MODULE_NAME, randomdev_modevent, 1);