2 * Copyright (c) 2000-2015 Mark R V Murray
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/filio.h>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
40 #include <sys/module.h>
41 #include <sys/malloc.h>
44 #include <sys/random.h>
46 #include <sys/selinfo.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
50 #include <sys/unistd.h>
52 #include <crypto/rijndael/rijndael-api-fst.h>
53 #include <crypto/sha2/sha2.h>
55 #include <dev/random/hash.h>
56 #include <dev/random/randomdev.h>
57 #include <dev/random/random_harvestq.h>
59 #include "opt_random.h"
61 #if defined(RANDOM_DUMMY) && defined(RANDOM_YARROW)
62 #error "Cannot define both RANDOM_DUMMY and RANDOM_YARROW"
67 static d_read_t randomdev_read;
68 static d_write_t randomdev_write;
69 static d_poll_t randomdev_poll;
70 static d_ioctl_t randomdev_ioctl;
72 static struct cdevsw random_cdevsw = {
74 .d_version = D_VERSION,
75 .d_read = randomdev_read,
76 .d_write = randomdev_write,
77 .d_poll = randomdev_poll,
78 .d_ioctl = randomdev_ioctl,
81 /* For use with make_dev(9)/destroy_dev(9). */
82 static struct cdev *random_dev;
84 /* Set up the sysctl root node for the entropy device */
85 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, "Cryptographically Secure Random Number Generator");
87 MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers and data structures");
89 #if defined(RANDOM_DUMMY)
92 * Dummy "always block" pseudo algorithm, used when there is no real
93 * random(4) driver to provide a CSPRNG.
97 dummy_random_zero(void)
108 struct random_algorithm random_alg_context = {
111 .ra_deinit_alg = NULL,
112 .ra_pre_read = dummy_random,
113 .ra_read = (random_alg_read_t *)dummy_random_zero,
114 .ra_write = (random_alg_write_t *)dummy_random_zero,
115 .ra_reseed = dummy_random,
116 .ra_seeded = (random_alg_seeded_t *)dummy_random_zero,
117 .ra_event_processor = NULL,
121 #else /* !defined(RANDOM_DUMMY) */
123 LIST_HEAD(sources_head, random_sources);
124 static struct sources_head source_list = LIST_HEAD_INITIALIZER(source_list);
125 static u_int read_rate;
128 random_alg_context_ra_init_alg(void *data)
131 random_alg_context.ra_init_alg(data);
135 random_alg_context_ra_deinit_alg(void *data)
138 random_alg_context.ra_deinit_alg(data);
141 SYSINIT(random_device, SI_SUB_RANDOM, SI_ORDER_THIRD, random_alg_context_ra_init_alg, NULL);
142 SYSUNINIT(random_device, SI_SUB_RANDOM, SI_ORDER_THIRD, random_alg_context_ra_deinit_alg, NULL);
144 #endif /* defined(RANDOM_DUMMY) */
146 static struct selinfo rsel;
149 * This is the read uio(9) interface for random(4).
153 randomdev_read(struct cdev *dev __unused, struct uio *uio, int flags)
156 return (read_random_uio(uio, (flags & O_NONBLOCK) != 0));
160 read_random_uio(struct uio *uio, bool nonblock)
164 ssize_t read_len, total_read, c;
166 random_buf = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK);
167 random_alg_context.ra_pre_read();
168 /* (Un)Blocking logic */
170 while (!random_alg_context.ra_seeded()) {
175 tsleep(&random_alg_context, 0, "randseed", hz/10);
176 /* keep tapping away at the pre-read until we seed/unblock. */
177 random_alg_context.ra_pre_read();
178 printf("random: %s unblock wait\n", __func__);
181 #if !defined(RANDOM_DUMMY)
182 /* XXX: FIX!! Next line as an atomic operation? */
183 read_rate += (uio->uio_resid + sizeof(uint32_t))/sizeof(uint32_t);
186 while (uio->uio_resid && !error) {
187 read_len = uio->uio_resid;
190 * Round up the read length to a crypto block size multiple,
191 * which is what the underlying generator is expecting.
192 * See the random_buf size requirements in the Yarrow/Fortuna code.
194 read_len += RANDOM_BLOCKSIZE;
195 read_len -= read_len % RANDOM_BLOCKSIZE;
196 read_len = MIN(read_len, PAGE_SIZE);
197 random_alg_context.ra_read(random_buf, read_len);
198 c = MIN(uio->uio_resid, read_len);
199 error = uiomove(random_buf, c, uio);
202 if (total_read != uio->uio_resid && (error == ERESTART || error == EINTR) )
203 /* Return partial read, not error. */
206 free(random_buf, M_ENTROPY);
211 * Kernel API version of read_random().
212 * This is similar to random_alg_read(),
213 * except it doesn't interface with uio(9).
214 * It cannot assumed that random_buf is a multiple of
215 * RANDOM_BLOCKSIZE bytes.
218 read_random(void *random_buf, u_int len)
220 u_int read_len, total_read, c;
221 uint8_t local_buf[len + RANDOM_BLOCKSIZE];
223 KASSERT(random_buf != NULL, ("No suitable random buffer in %s", __func__));
224 random_alg_context.ra_pre_read();
225 /* (Un)Blocking logic; if not seeded, return nothing. */
226 if (random_alg_context.ra_seeded()) {
227 #if !defined(RANDOM_DUMMY)
228 /* XXX: FIX!! Next line as an atomic operation? */
229 read_rate += (len + sizeof(uint32_t))/sizeof(uint32_t);
234 * Round up the read length to a crypto block size multiple,
235 * which is what the underlying generator is expecting.
237 read_len += RANDOM_BLOCKSIZE;
238 read_len -= read_len % RANDOM_BLOCKSIZE;
241 c = MIN(read_len, PAGE_SIZE);
242 random_alg_context.ra_read(&local_buf[total_read], c);
246 memcpy(random_buf, local_buf, len);
254 randomdev_write(struct cdev *dev __unused, struct uio *uio, int flags __unused)
260 random_buf = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK);
261 nbytes = uio->uio_resid;
262 while (uio->uio_resid > 0 && error == 0) {
263 c = MIN(uio->uio_resid, PAGE_SIZE);
264 error = uiomove(random_buf, c, uio);
267 random_alg_context.ra_write(random_buf, c);
268 tsleep(&random_alg_context, 0, "randwr", hz/10);
270 if (nbytes != uio->uio_resid && (error == ERESTART || error == EINTR))
271 /* Partial write, not error. */
273 free(random_buf, M_ENTROPY);
279 randomdev_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
282 if (events & (POLLIN | POLLRDNORM)) {
283 if (random_alg_context.ra_seeded())
284 events &= (POLLIN | POLLRDNORM);
286 selrecord(td, &rsel);
291 /* This will be called by the entropy processor when it seeds itself and becomes secure */
293 randomdev_unblock(void)
296 selwakeuppri(&rsel, PUSER);
297 wakeup(&random_alg_context);
298 printf("random: unblocking device.\n");
299 /* Do random(9) a favour while we are about it. */
300 (void)atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_NONE, ARC4_ENTR_HAVE);
305 randomdev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr __unused,
306 int flags __unused, struct thread *td __unused)
311 /* Really handled in upper layer */
323 random_source_register(struct random_source *rsource)
325 #if defined(RANDOM_DUMMY)
327 #else /* !defined(RANDOM_DUMMY) */
328 struct random_sources *rrs;
330 KASSERT(rsource != NULL, ("invalid input to %s", __func__));
332 rrs = malloc(sizeof(*rrs), M_ENTROPY, M_WAITOK);
333 rrs->rrs_source = rsource;
335 printf("random: registering fast source %s\n", rsource->rs_ident);
336 LIST_INSERT_HEAD(&source_list, rrs, rrs_entries);
337 #endif /* defined(RANDOM_DUMMY) */
341 random_source_deregister(struct random_source *rsource)
343 #if defined(RANDOM_DUMMY)
345 #else /* !defined(RANDOM_DUMMY) */
346 struct random_sources *rrs = NULL;
348 KASSERT(rsource != NULL, ("invalid input to %s", __func__));
349 LIST_FOREACH(rrs, &source_list, rrs_entries)
350 if (rrs->rrs_source == rsource) {
351 LIST_REMOVE(rrs, rrs_entries);
355 free(rrs, M_ENTROPY);
356 #endif /* defined(RANDOM_DUMMY) */
359 #if !defined(RANDOM_DUMMY)
361 * Run through all fast sources reading entropy for the given
362 * number of rounds, which should be a multiple of the number
363 * of entropy accumulation pools in use; 2 for Yarrow and 32
367 * This function runs inside the RNG thread! Don't do anything silly!
370 random_sources_feed(void)
372 uint32_t entropy[HARVESTSIZE];
373 struct random_sources *rrs;
374 u_int i, n, local_read_rate;
377 * Step over all of live entropy sources, and feed their output
378 * to the system-wide RNG.
380 /* XXX: FIX!! Next lines as an atomic operation? */
381 local_read_rate = read_rate;
382 read_rate = RANDOM_ALG_READ_RATE_MINIMUM;
383 LIST_FOREACH(rrs, &source_list, rrs_entries) {
384 for (i = 0; i < random_alg_context.ra_poolcount*local_read_rate; i++) {
385 n = rrs->rrs_source->rs_read(entropy, sizeof(entropy));
386 KASSERT((n > 0 && n <= sizeof(entropy)), ("very bad return from rs_read (= %d) in %s", n, __func__));
387 random_harvest_direct(entropy, n, (n*8)/2, rrs->rrs_source->rs_source);
390 explicit_bzero(entropy, sizeof(entropy));
394 random_source_handler(SYSCTL_HANDLER_ARGS)
396 struct random_sources *rrs;
400 sbuf_new_for_sysctl(&sbuf, NULL, 64, req);
402 LIST_FOREACH(rrs, &source_list, rrs_entries) {
403 sbuf_cat(&sbuf, (count++ ? ",'" : "'"));
404 sbuf_cat(&sbuf, rrs->rrs_source->rs_ident);
405 sbuf_cat(&sbuf, "'");
407 error = sbuf_finish(&sbuf);
411 SYSCTL_PROC(_kern_random, OID_AUTO, random_sources, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
412 NULL, 0, random_source_handler, "A",
413 "List of active fast entropy sources.");
414 #endif /* !defined(RANDOM_DUMMY) */
418 randomdev_modevent(module_t mod __unused, int type, void *data __unused)
424 printf("random: entropy device external interface\n");
425 random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw,
426 RANDOM_UNIT, NULL, UID_ROOT, GID_WHEEL, 0644, "random");
427 make_dev_alias(random_dev, "urandom"); /* compatibility */
430 destroy_dev(random_dev);
441 static moduledata_t randomdev_mod = {
447 DECLARE_MODULE(random_device, randomdev_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
448 MODULE_VERSION(random_device, 1);