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/sha256.h>
55 #include <dev/random/hash.h>
56 #include <dev/random/randomdev.h>
57 #include <dev/random/random_harvestq.h>
61 #if defined(RANDOM_LOADABLE)
62 #define READ_RANDOM_UIO _read_random_uio
63 #define READ_RANDOM _read_random
64 static int READ_RANDOM_UIO(struct uio *, bool);
65 static u_int READ_RANDOM(void *, u_int);
67 #define READ_RANDOM_UIO read_random_uio
68 #define READ_RANDOM read_random
71 static d_read_t randomdev_read;
72 static d_write_t randomdev_write;
73 static d_poll_t randomdev_poll;
74 static d_ioctl_t randomdev_ioctl;
76 static struct cdevsw random_cdevsw = {
78 .d_version = D_VERSION,
79 .d_read = randomdev_read,
80 .d_write = randomdev_write,
81 .d_poll = randomdev_poll,
82 .d_ioctl = randomdev_ioctl,
85 /* For use with make_dev(9)/destroy_dev(9). */
86 static struct cdev *random_dev;
89 random_alg_context_ra_init_alg(void *data)
92 p_random_alg_context = &random_alg_context;
93 p_random_alg_context->ra_init_alg(data);
94 #if defined(RANDOM_LOADABLE)
95 random_infra_init(READ_RANDOM_UIO, READ_RANDOM);
100 random_alg_context_ra_deinit_alg(void *data)
103 #if defined(RANDOM_LOADABLE)
104 random_infra_uninit();
106 p_random_alg_context->ra_deinit_alg(data);
107 p_random_alg_context = NULL;
110 SYSINIT(random_device, SI_SUB_RANDOM, SI_ORDER_THIRD, random_alg_context_ra_init_alg, NULL);
111 SYSUNINIT(random_device, SI_SUB_RANDOM, SI_ORDER_THIRD, random_alg_context_ra_deinit_alg, NULL);
113 static struct selinfo rsel;
116 * This is the read uio(9) interface for random(4).
120 randomdev_read(struct cdev *dev __unused, struct uio *uio, int flags)
123 return (READ_RANDOM_UIO(uio, (flags & O_NONBLOCK) != 0));
127 READ_RANDOM_UIO(struct uio *uio, bool nonblock)
130 int error, spamcount;
131 ssize_t read_len, total_read, c;
133 random_buf = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK);
134 p_random_alg_context->ra_pre_read();
137 /* (Un)Blocking logic */
138 while (!p_random_alg_context->ra_seeded()) {
143 /* keep tapping away at the pre-read until we seed/unblock. */
144 p_random_alg_context->ra_pre_read();
145 /* Only bother the console every 10 seconds or so */
147 printf("random: %s unblock wait\n", __func__);
148 spamcount = (spamcount + 1)%100;
149 error = tsleep(&random_alg_context, PCATCH, "randseed", hz/10);
150 if (error == ERESTART || error == EINTR)
154 read_rate_increment((uio->uio_resid + sizeof(uint32_t))/sizeof(uint32_t));
156 while (uio->uio_resid && !error) {
157 read_len = uio->uio_resid;
160 * Round up the read length to a crypto block size multiple,
161 * which is what the underlying generator is expecting.
162 * See the random_buf size requirements in the Yarrow/Fortuna code.
164 read_len = roundup(read_len, RANDOM_BLOCKSIZE);
165 /* Work in chunks page-sized or less */
166 read_len = MIN(read_len, PAGE_SIZE);
167 p_random_alg_context->ra_read(random_buf, read_len);
168 c = MIN(uio->uio_resid, read_len);
169 error = uiomove(random_buf, c, uio);
172 if (total_read != uio->uio_resid && (error == ERESTART || error == EINTR))
173 /* Return partial read, not error. */
176 free(random_buf, M_ENTROPY);
181 * Kernel API version of read_random().
182 * This is similar to random_alg_read(),
183 * except it doesn't interface with uio(9).
184 * It cannot assumed that random_buf is a multiple of
185 * RANDOM_BLOCKSIZE bytes.
188 READ_RANDOM(void *random_buf, u_int len)
191 uint8_t local_buf[len + RANDOM_BLOCKSIZE];
193 KASSERT(random_buf != NULL, ("No suitable random buffer in %s", __func__));
194 p_random_alg_context->ra_pre_read();
195 /* (Un)Blocking logic; if not seeded, return nothing. */
196 if (p_random_alg_context->ra_seeded()) {
197 read_rate_increment((len + sizeof(uint32_t))/sizeof(uint32_t));
201 * Round up the read length to a crypto block size multiple,
202 * which is what the underlying generator is expecting.
204 read_len = roundup(len, RANDOM_BLOCKSIZE);
205 p_random_alg_context->ra_read(local_buf, read_len);
206 memcpy(random_buf, local_buf, len);
214 randomdev_accumulate(uint8_t *buf, u_int count)
216 static u_int destination = 0;
217 static struct harvest_event event;
218 static struct randomdev_hash hash;
219 static uint32_t entropy_data[RANDOM_KEYSIZE_WORDS];
223 /* Extra timing here is helpful to scrape scheduler jitter entropy */
224 randomdev_hash_init(&hash);
225 timestamp = (uint32_t)get_cyclecount();
226 randomdev_hash_iterate(&hash, ×tamp, sizeof(timestamp));
227 randomdev_hash_iterate(&hash, buf, count);
228 timestamp = (uint32_t)get_cyclecount();
229 randomdev_hash_iterate(&hash, ×tamp, sizeof(timestamp));
230 randomdev_hash_finish(&hash, entropy_data);
231 explicit_bzero(&hash, sizeof(hash));
232 for (i = 0; i < RANDOM_KEYSIZE_WORDS; i += sizeof(event.he_entropy)/sizeof(event.he_entropy[0])) {
233 event.he_somecounter = (uint32_t)get_cyclecount();
234 event.he_size = sizeof(event.he_entropy);
235 event.he_bits = event.he_size/8;
236 event.he_source = RANDOM_CACHED;
237 event.he_destination = destination++; /* Harmless cheating */
238 memcpy(event.he_entropy, entropy_data + i, sizeof(event.he_entropy));
239 p_random_alg_context->ra_event_processor(&event);
241 explicit_bzero(entropy_data, sizeof(entropy_data));
246 randomdev_write(struct cdev *dev __unused, struct uio *uio, int flags __unused)
252 random_buf = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK);
253 nbytes = uio->uio_resid;
254 while (uio->uio_resid > 0 && error == 0) {
255 c = MIN(uio->uio_resid, PAGE_SIZE);
256 error = uiomove(random_buf, c, uio);
259 randomdev_accumulate(random_buf, c);
260 tsleep(&random_alg_context, 0, "randwr", hz/10);
262 if (nbytes != uio->uio_resid && (error == ERESTART || error == EINTR))
263 /* Partial write, not error. */
265 free(random_buf, M_ENTROPY);
271 randomdev_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
274 if (events & (POLLIN | POLLRDNORM)) {
275 if (p_random_alg_context->ra_seeded())
276 events &= (POLLIN | POLLRDNORM);
278 selrecord(td, &rsel);
283 /* This will be called by the entropy processor when it seeds itself and becomes secure */
285 randomdev_unblock(void)
288 selwakeuppri(&rsel, PUSER);
289 wakeup(&random_alg_context);
290 printf("random: unblocking device.\n");
291 /* Do random(9) a favour while we are about it. */
292 (void)atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_NONE, ARC4_ENTR_HAVE);
297 randomdev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr __unused,
298 int flags __unused, struct thread *td __unused)
303 /* Really handled in upper layer */
315 random_source_register(struct random_source *rsource)
317 struct random_sources *rrs;
319 KASSERT(rsource != NULL, ("invalid input to %s", __func__));
321 rrs = malloc(sizeof(*rrs), M_ENTROPY, M_WAITOK);
322 rrs->rrs_source = rsource;
324 printf("random: registering fast source %s\n", rsource->rs_ident);
325 LIST_INSERT_HEAD(&source_list, rrs, rrs_entries);
329 random_source_deregister(struct random_source *rsource)
331 struct random_sources *rrs = NULL;
333 KASSERT(rsource != NULL, ("invalid input to %s", __func__));
334 LIST_FOREACH(rrs, &source_list, rrs_entries)
335 if (rrs->rrs_source == rsource) {
336 LIST_REMOVE(rrs, rrs_entries);
340 free(rrs, M_ENTROPY);
344 random_source_handler(SYSCTL_HANDLER_ARGS)
346 struct random_sources *rrs;
350 sbuf_new_for_sysctl(&sbuf, NULL, 64, req);
352 LIST_FOREACH(rrs, &source_list, rrs_entries) {
353 sbuf_cat(&sbuf, (count++ ? ",'" : "'"));
354 sbuf_cat(&sbuf, rrs->rrs_source->rs_ident);
355 sbuf_cat(&sbuf, "'");
357 error = sbuf_finish(&sbuf);
361 SYSCTL_PROC(_kern_random, OID_AUTO, random_sources, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
362 NULL, 0, random_source_handler, "A",
363 "List of active fast entropy sources.");
367 randomdev_modevent(module_t mod __unused, int type, void *data __unused)
373 printf("random: entropy device external interface\n");
374 random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw,
375 RANDOM_UNIT, NULL, UID_ROOT, GID_WHEEL, 0644, "random");
376 make_dev_alias(random_dev, "urandom"); /* compatibility */
379 destroy_dev(random_dev);
390 static moduledata_t randomdev_mod = {
396 DECLARE_MODULE(random_device, randomdev_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
397 MODULE_VERSION(random_device, 1);
398 MODULE_DEPEND(random_device, crypto, 1, 1, 1);
399 MODULE_DEPEND(random_device, random_harvestq, 1, 1, 1);