]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/random/randomdev_soft.c
MFC- tracking commit.
[FreeBSD/FreeBSD.git] / sys / dev / random / randomdev_soft.c
1 /*-
2  * Copyright (c) 2000-2013 Mark R V Murray
3  * Copyright (c) 2004 Robert N. M. Watson
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
15  *
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.
26  *
27  */
28
29 #if !defined(YARROW_RNG) && !defined(FORTUNA_RNG)
30 #define YARROW_RNG
31 #elif defined(YARROW_RNG) && defined(FORTUNA_RNG)
32 #error "Must define either YARROW_RNG or FORTUNA_RNG"
33 #endif
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/poll.h>
47 #include <sys/random.h>
48 #include <sys/selinfo.h>
49 #include <sys/sysctl.h>
50 #include <sys/uio.h>
51 #include <sys/unistd.h>
52
53 #include <machine/bus.h>
54 #include <machine/cpu.h>
55
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>
61 #endif
62 #if defined(FORTUNA_RNG)
63 #include <dev/random/fortuna.h>
64 #endif
65
66 #include "random_harvestq.h"
67
68 static int randomdev_poll(int event, struct thread *td);
69 static int randomdev_block(int flag);
70 static void randomdev_flush_reseed(void);
71
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 */
82 };
83 #define RANDOM_MODULE_NAME      yarrow
84 #define RANDOM_CSPRNG_NAME      "yarrow"
85 #endif
86
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 */
97 };
98 #define RANDOM_MODULE_NAME      fortuna
99 #define RANDOM_CSPRNG_NAME      "fortuna"
100 #endif
101
102 /* List for the dynamic sysctls */
103 static struct sysctl_ctx_list random_clist;
104
105 /* ARGSUSED */
106 static int
107 random_check_boolean(SYSCTL_HANDLER_ARGS)
108 {
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));
112 }
113
114 void
115 randomdev_init(void)
116 {
117         struct sysctl_oid *random_sys_o, *random_sys_harvest_o;
118
119 #if defined(YARROW_RNG)
120         random_yarrow_init_alg(&random_clist);
121 #endif
122 #if defined(FORTUNA_RNG)
123         random_fortuna_init_alg(&random_clist);
124 #endif
125
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");
130
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",
135             "Seeded State");
136
137         random_sys_harvest_o = SYSCTL_ADD_NODE(&random_clist,
138             SYSCTL_CHILDREN(random_sys_o),
139             OID_AUTO, "harvest", CTLFLAG_RW, 0,
140             "Entropy Sources");
141
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");
162
163         random_harvestq_init(random_process_event);
164
165         /* Register the randomness harvesting routine */
166         randomdev_init_harvester(random_harvestq_internal,
167             random_context.read);
168 }
169
170 void
171 randomdev_deinit(void)
172 {
173         /* Deregister the randomness harvesting routine */
174         randomdev_deinit_harvester();
175
176         /*
177          * Command the hash/reseed thread to end and wait for it to finish
178          */
179         random_kthread_control = -1;
180         tsleep((void *)&random_kthread_control, 0, "term", 0);
181
182 #if defined(YARROW_RNG)
183         random_yarrow_deinit_alg();
184 #endif
185 #if defined(FORTUNA_RNG)
186         random_fortuna_deinit_alg();
187 #endif
188
189         sysctl_ctx_free(&random_clist);
190 }
191
192 void
193 randomdev_unblock(void)
194 {
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;
200         }
201         /* Do arc4random(9) a favour while we are about it. */
202         (void)atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_NONE,
203             ARC4_ENTR_HAVE);
204 }
205
206 static int
207 randomdev_poll(int events, struct thread *td)
208 {
209         int revents = 0;
210
211         mtx_lock(&random_reseed_mtx);
212
213         if (random_context.seeded)
214                 revents = events & (POLLIN | POLLRDNORM);
215         else
216                 selrecord(td, &random_context.rsel);
217
218         mtx_unlock(&random_reseed_mtx);
219         return (revents);
220 }
221
222 static int
223 randomdev_block(int flag)
224 {
225         int error = 0;
226
227         mtx_lock(&random_reseed_mtx);
228
229         /* Blocking logic */
230         while (!random_context.seeded && !error) {
231                 if (flag & O_NONBLOCK)
232                         error = EWOULDBLOCK;
233                 else {
234                         printf("random: blocking on read.\n");
235                         error = msleep(&random_context,
236                             &random_reseed_mtx,
237                             PUSER | PCATCH, "block", 0);
238                 }
239         }
240         mtx_unlock(&random_reseed_mtx);
241
242         return (error);
243 }
244
245 /* Helper routine to perform explicit reseeds */
246 static void
247 randomdev_flush_reseed(void)
248 {
249         /* Command a entropy queue flush and wait for it to finish */
250         random_kthread_control = 1;
251         while (random_kthread_control)
252                 pause("-", hz / 10);
253
254 #if defined(YARROW_RNG)
255         /* This ultimately calls randomdev_unblock() */
256         random_yarrow_reseed();
257 #endif
258 #if defined(FORTUNA_RNG)
259         /* This ultimately calls randomdev_unblock() */
260         random_fortuna_reseed();
261 #endif
262 }
263
264 static int
265 randomdev_modevent(module_t mod __unused, int type, void *unused __unused)
266 {
267
268         switch (type) {
269         case MOD_LOAD:
270                 random_adaptor_register(RANDOM_CSPRNG_NAME, &random_context);
271                 /*
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.
277                  *
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.
282                  */
283                 EVENTHANDLER_INVOKE(random_adaptor_attach, &random_context);
284                 return (0);
285         }
286
287         return (EINVAL);
288 }
289
290 RANDOM_ADAPTOR_MODULE(RANDOM_MODULE_NAME, randomdev_modevent, 1);