2 * Copyright (c) 2000 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.
29 #include <sys/param.h>
30 #include <sys/systm.h>
33 #include <sys/fcntl.h>
34 #include <sys/filio.h>
35 #include <sys/kernel.h>
36 #include <sys/kthread.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
42 #include <sys/random.h>
43 #include <sys/selinfo.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/vnode.h>
49 #include <machine/bus.h>
50 #include <machine/cpu.h>
52 #include <dev/random/randomdev.h>
54 static d_open_t random_open;
55 static d_close_t random_close;
56 static d_read_t random_read;
57 static d_write_t random_write;
58 static d_ioctl_t random_ioctl;
59 static d_poll_t random_poll;
62 #define RANDOM_MINOR 3
64 static struct cdevsw random_cdevsw = {
65 /* open */ random_open,
66 /* close */ random_close,
67 /* read */ random_read,
68 /* write */ random_write,
69 /* ioctl */ random_ioctl,
70 /* poll */ random_poll,
72 /* strategy */ nostrategy,
81 static void random_kthread(void *);
82 static void random_harvest_internal(u_int64_t, void *, u_int, u_int, u_int, enum esource);
83 static void random_write_internal(void *, int);
85 /* Ring buffer holding harvested entropy */
86 static struct harvestring {
89 struct harvest data[HARVEST_RING_SIZE];
92 static struct random_systat {
93 u_int seeded; /* 0 causes blocking 1 allows normal output */
94 u_int burst; /* number of events to do before sleeping */
95 struct selinfo rsel; /* For poll(2) */
98 /* <0 to end the kthread, 0 to let it run */
99 static int random_kthread_control = 0;
101 static struct proc *random_kthread_proc;
103 /* For use with make_dev(9)/destroy_dev(9). */
104 static dev_t random_dev;
105 static dev_t urandom_dev;
109 random_check_boolean(SYSCTL_HANDLER_ARGS)
111 if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0)
112 *(u_int *)(oidp->oid_arg1) = 1;
113 return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
116 RANDOM_CHECK_UINT(burst, 0, 20);
118 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW,
119 0, "Random Number Generator");
120 SYSCTL_NODE(_kern_random, OID_AUTO, sys, CTLFLAG_RW,
121 0, "Entropy Device Parameters");
122 SYSCTL_PROC(_kern_random_sys, OID_AUTO, seeded,
123 CTLTYPE_INT|CTLFLAG_RW, &random_systat.seeded, 1,
124 random_check_boolean, "I", "Seeded State");
125 SYSCTL_PROC(_kern_random_sys, OID_AUTO, burst,
126 CTLTYPE_INT|CTLFLAG_RW, &random_systat.burst, 20,
127 random_check_uint_burst, "I", "Harvest Burst Size");
128 SYSCTL_NODE(_kern_random_sys, OID_AUTO, harvest, CTLFLAG_RW,
129 0, "Entropy Sources");
130 SYSCTL_PROC(_kern_random_sys_harvest, OID_AUTO, ethernet,
131 CTLTYPE_INT|CTLFLAG_RW, &harvest.ethernet, 0,
132 random_check_boolean, "I", "Harvest NIC entropy");
133 SYSCTL_PROC(_kern_random_sys_harvest, OID_AUTO, point_to_point,
134 CTLTYPE_INT|CTLFLAG_RW, &harvest.point_to_point, 0,
135 random_check_boolean, "I", "Harvest serial net entropy");
136 SYSCTL_PROC(_kern_random_sys_harvest, OID_AUTO, interrupt,
137 CTLTYPE_INT|CTLFLAG_RW, &harvest.interrupt, 0,
138 random_check_boolean, "I", "Harvest IRQ entropy");
139 SYSCTL_PROC(_kern_random_sys_harvest, OID_AUTO, swi,
140 CTLTYPE_INT|CTLFLAG_RW, &harvest.swi, 0,
141 random_check_boolean, "I", "Harvest SWI entropy");
145 random_open(dev_t dev __unused, int flags, int fmt __unused, struct thread *td)
149 if (flags & FWRITE) {
153 error = securelevel_gt(td->td_ucred, 0);
162 random_close(dev_t dev __unused, int flags, int fmt __unused, struct thread *td)
164 if (flags & FWRITE) {
166 securelevel_gt(td->td_ucred, 0)))
174 random_read(dev_t dev __unused, struct uio *uio, int flag)
180 while (!random_systat.seeded) {
181 if (flag & IO_NDELAY)
184 error = tsleep(&random_systat, PUSER|PCATCH,
189 c = uio->uio_resid < PAGE_SIZE ? uio->uio_resid : PAGE_SIZE;
190 random_buf = (void *)malloc((u_long)c, M_TEMP, M_WAITOK);
191 while (uio->uio_resid > 0 && error == 0) {
192 ret = read_random_real(random_buf, c);
193 error = uiomove(random_buf, ret, uio);
195 free(random_buf, M_TEMP);
201 random_write(dev_t dev __unused, struct uio *uio, int flag __unused)
208 random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
209 while (uio->uio_resid > 0) {
210 c = (int)(uio->uio_resid < PAGE_SIZE
213 error = uiomove(random_buf, c, uio);
216 random_write_internal(random_buf, c);
218 free(random_buf, M_TEMP);
224 random_ioctl(dev_t dev __unused, u_long cmd, caddr_t addr __unused,
225 int flags __unused, struct thread *td __unused)
228 /* Really handled in upper layer */
239 random_poll(dev_t dev __unused, int events, struct thread *td)
244 if (events & (POLLIN | POLLRDNORM)) {
245 if (random_systat.seeded)
246 revents = events & (POLLIN | POLLRDNORM);
248 selrecord(td, &random_systat.rsel);
255 random_modevent(module_t mod __unused, int type, void *data __unused)
263 /* This can be turned off by the very paranoid
264 * a reseed will turn it back on.
266 random_systat.seeded = 1;
268 /* Number of envents to process off the harvest
269 * queue before giving it a break and sleeping
271 random_systat.burst = 20;
273 /* Initialise the harvest ringbuffer */
274 harvestring.head = 0;
275 harvestring.tail = 0;
278 printf("random: <entropy source>\n");
279 random_dev = make_dev(&random_cdevsw, RANDOM_MINOR, UID_ROOT,
280 GID_WHEEL, 0666, "random");
281 urandom_dev = make_dev_alias(random_dev, "urandom");
283 /* Start the hash/reseed thread */
284 error = kthread_create(random_kthread, NULL,
285 &random_kthread_proc, RFHIGHPID, "random");
289 /* Register the randomness harvesting routine */
290 random_init_harvester(random_harvest_internal,
296 /* Deregister the randomness harvesting routine */
297 random_deinit_harvester();
299 /* Command the hash/reseed thread to end and
300 * wait for it to finish
302 random_kthread_control = -1;
303 tsleep((void *)&random_kthread_control, PUSER, "term", 0);
307 destroy_dev(random_dev);
308 destroy_dev(urandom_dev);
319 DEV_MODULE(random, random_modevent, NULL);
323 random_kthread(void *arg __unused)
325 struct harvest *event;
326 u_int newtail, burst;
328 /* Drain the harvest queue (in 'burst' size chunks,
329 * if 'burst' > 0. If 'burst' == 0, then completely
332 for (burst = 0; ; burst++) {
334 if ((harvestring.tail == harvestring.head) ||
335 (random_systat.burst && burst == random_systat.burst)) {
336 tsleep(&harvestring, PUSER, "sleep", hz/10);
342 /* Suck a harvested entropy event out of the queue and
343 * hand it to the event processor
346 newtail = (harvestring.tail + 1) & HARVEST_RING_MASK;
347 event = &harvestring.data[harvestring.tail];
349 /* Bump the ring counter. This action is assumed
352 harvestring.tail = newtail;
354 random_process_event(event);
358 /* Is the thread scheduled for a shutdown? */
359 if (random_kthread_control != 0) {
362 printf("Random kthread setting terminate\n");
365 random_set_wakeup_exit(&random_kthread_control);
374 /* Entropy harvesting routine. This is supposed to be fast; do
375 * not do anything slow in here!
378 random_harvest_internal(u_int64_t somecounter, void *entropy, u_int count,
379 u_int bits, u_int frac, enum esource origin)
381 struct harvest *pharvest;
384 newhead = (harvestring.head + 1) & HARVEST_RING_MASK;
386 if (newhead != harvestring.tail) {
388 /* Add the harvested data to the ring buffer */
390 pharvest = &harvestring.data[harvestring.head];
392 /* Stuff the harvested data into the ring */
393 pharvest->somecounter = somecounter;
394 count = count > HARVESTSIZE ? HARVESTSIZE : count;
395 memcpy(pharvest->entropy, entropy, count);
396 pharvest->size = count;
397 pharvest->bits = bits;
398 pharvest->frac = frac;
400 origin < ENTROPYSOURCE ? origin : RANDOM_START;
402 /* Bump the ring counter. This action is assumed
405 harvestring.head = newhead;
412 random_write_internal(void *buf, int count)
416 /* Break the input up into HARVESTSIZE chunks.
417 * The writer has too much control here, so "estimate" the
418 * the entropy as zero.
420 for (i = 0; i < count; i += HARVESTSIZE) {
421 random_harvest_internal(get_cyclecount(), (char *)buf + i,
422 HARVESTSIZE, 0, 0, RANDOM_WRITE);
425 /* Maybe the loop iterated at least once */
429 /* Get the last bytes even if the input length is not
430 * a multiple of HARVESTSIZE.
432 count %= HARVESTSIZE;
434 random_harvest_internal(get_cyclecount(), (char *)buf + i,
435 (u_int)count, 0, 0, RANDOM_WRITE);
442 if (!random_systat.seeded) {
443 random_systat.seeded = 1;
444 selwakeup(&random_systat.rsel);
445 wakeup(&random_systat);