xref: /openbsd/sys/dev/rnd.c (revision cca36db2)
1 /*	$OpenBSD: rnd.c,v 1.140 2011/07/06 14:49:30 nicm Exp $	*/
2 
3 /*
4  * Copyright (c) 2011 Theo de Raadt.
5  * Copyright (c) 2008 Damien Miller.
6  * Copyright (c) 1996, 1997, 2000-2002 Michael Shalayeff.
7  * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, and the entire permission notice in its entirety,
15  *    including the disclaimer of warranties.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote
20  *    products derived from this software without specific prior
21  *    written permission.
22  *
23  * ALTERNATIVELY, this product may be distributed under the terms of
24  * the GNU Public License, in which case the provisions of the GPL are
25  * required INSTEAD OF the above restrictions.  (This clause is
26  * necessary due to a potential bad interaction between the GPL and
27  * the restrictions contained in a BSD-style copyright.)
28  *
29  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
30  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 /*
43  * Computers are very predictable devices.  Hence it is extremely hard
44  * to produce truly random numbers on a computer --- as opposed to
45  * pseudo-random numbers, which can be easily generated by using an
46  * algorithm.  Unfortunately, it is very easy for attackers to guess
47  * the sequence of pseudo-random number generators, and for some
48  * applications this is not acceptable.  Instead, we must try to
49  * gather "environmental noise" from the computer's environment, which
50  * must be hard for outside attackers to observe and use to
51  * generate random numbers.  In a Unix environment, this is best done
52  * from inside the kernel.
53  *
54  * Sources of randomness from the environment include inter-keyboard
55  * timings, inter-interrupt timings from some interrupts, and other
56  * events which are both (a) non-deterministic and (b) hard for an
57  * outside observer to measure.  Randomness from these sources is
58  * added to the "rnd states" queue; this is used as much of the
59  * source material which is mixed on occasion using a CRC-like function
60  * into the "entropy pool".  This is not cryptographically strong, but
61  * it is adequate assuming the randomness is not chosen maliciously,
62  * and it very fast because the interrupt-time event is only to add
63  * a small random token to the "rnd states" queue.
64  *
65  * When random bytes are desired, they are obtained by pulling from
66  * the entropy pool and running a MD5 hash. The MD5 hash avoids
67  * exposing the internal state of the entropy pool.  Even if it is
68  * possible to analyze MD5 in some clever way, as long as the amount
69  * of data returned from the generator is less than the inherent
70  * entropy in the pool, the output data is totally unpredictable.  For
71  * this reason, the routine decreases its internal estimate of how many
72  * bits of "true randomness" are contained in the entropy pool as it
73  * outputs random numbers.
74  *
75  * If this estimate goes to zero, the MD5 hash will continue to generate
76  * output since there is no true risk because the MD5 output is not
77  * exported outside this subsystem.  It is next used as input to seed a
78  * RC4 stream cipher.  Attempts are made to follow best practice
79  * regarding this stream cipher - the first chunk of output is discarded
80  * and the cipher is re-seeded from time to time.  This design provides
81  * very high amounts of output data from a potentially small entropy
82  * base, at high enough speeds to encourage use of random numbers in
83  * nearly any situation.
84  *
85  * The output of this single RC4 engine is then shared amongst many
86  * consumers in the kernel and userland via a few interfaces:
87  * arc4random_buf(), arc4random(), arc4random_uniform(), randomread()
88  * for the set of /dev/random nodes, and the sysctl kern.arandom.
89  *
90  * Acknowledgements:
91  * =================
92  *
93  * Ideas for constructing this random number generator were derived
94  * from Pretty Good Privacy's random number generator, and from private
95  * discussions with Phil Karn.  Colin Plumb provided a faster random
96  * number generator, which speeds up the mixing function of the entropy
97  * pool, taken from PGPfone.  Dale Worley has also contributed many
98  * useful ideas and suggestions to improve this driver.
99  *
100  * Any flaws in the design are solely my responsibility, and should
101  * not be attributed to the Phil, Colin, or any of the authors of PGP.
102  *
103  * Further background information on this topic may be obtained from
104  * RFC 1750, "Randomness Recommendations for Security", by Donald
105  * Eastlake, Steve Crocker, and Jeff Schiller.
106  *
107  * Using a RC4 stream cipher as 2nd stage after the MD5 output
108  * is the result of work by David Mazieres.
109  */
110 
111 #include <sys/param.h>
112 #include <sys/systm.h>
113 #include <sys/conf.h>
114 #include <sys/disk.h>
115 #include <sys/event.h>
116 #include <sys/limits.h>
117 #include <sys/time.h>
118 #include <sys/ioctl.h>
119 #include <sys/malloc.h>
120 #include <sys/fcntl.h>
121 #include <sys/timeout.h>
122 #include <sys/mutex.h>
123 #include <sys/workq.h>
124 #include <sys/msgbuf.h>
125 
126 #include <crypto/md5.h>
127 #include <crypto/arc4.h>
128 
129 #include <dev/rndvar.h>
130 
131 /*
132  * For the purposes of better mixing, we use the CRC-32 polynomial as
133  * well to make a twisted Generalized Feedback Shift Register
134  *
135  * (See M. Matsumoto & Y. Kurita, 1992.  Twisted GFSR generators.  ACM
136  * Transactions on Modeling and Computer Simulation 2(3):179-194.
137  * Also see M. Matsumoto & Y. Kurita, 1994.  Twisted GFSR generators
138  * II.  ACM Transactions on Mdeling and Computer Simulation 4:254-266)
139  *
140  * Thanks to Colin Plumb for suggesting this.
141  *
142  * We have not analyzed the resultant polynomial to prove it primitive;
143  * in fact it almost certainly isn't.  Nonetheless, the irreducible factors
144  * of a random large-degree polynomial over GF(2) are more than large enough
145  * that periodicity is not a concern.
146  *
147  * The input hash is much less sensitive than the output hash.  All
148  * we want from it is to be a good non-cryptographic hash -
149  * i.e. to not produce collisions when fed "random" data of the sort
150  * we expect to see.  As long as the pool state differs for different
151  * inputs, we have preserved the input entropy and done a good job.
152  * The fact that an intelligent attacker can construct inputs that
153  * will produce controlled alterations to the pool's state is not
154  * important because we don't consider such inputs to contribute any
155  * randomness.  The only property we need with respect to them is that
156  * the attacker can't increase his/her knowledge of the pool's state.
157  * Since all additions are reversible (knowing the final state and the
158  * input, you can reconstruct the initial state), if an attacker has
159  * any uncertainty about the initial state, he/she can only shuffle
160  * that uncertainty about, but never cause any collisions (which would
161  * decrease the uncertainty).
162  *
163  * The chosen system lets the state of the pool be (essentially) the input
164  * modulo the generator polynomial.  Now, for random primitive polynomials,
165  * this is a universal class of hash functions, meaning that the chance
166  * of a collision is limited by the attacker's knowledge of the generator
167  * polynomial, so if it is chosen at random, an attacker can never force
168  * a collision.  Here, we use a fixed polynomial, but we *can* assume that
169  * ###--> it is unknown to the processes generating the input entropy. <-###
170  * Because of this important property, this is a good, collision-resistant
171  * hash; hash collisions will occur no more often than chance.
172  */
173 
174 /*
175  * Stirring polynomials over GF(2) for various pool sizes. Used in
176  * add_entropy_words() below.
177  *
178  * The polynomial terms are chosen to be evenly spaced (minimum RMS
179  * distance from evenly spaced; except for the last tap, which is 1 to
180  * get the twisting happening as fast as possible.
181  *
182  * The reultant polynomial is:
183  *   2^POOLWORDS + 2^POOL_TAP1 + 2^POOL_TAP2 + 2^POOL_TAP3 + 2^POOL_TAP4 + 1
184  */
185 #define POOLWORDS	2048
186 #define POOLBYTES	(POOLWORDS*4)
187 #define POOLMASK	(POOLWORDS - 1)
188 #define	POOL_TAP1	1638
189 #define	POOL_TAP2	1231
190 #define	POOL_TAP3	819
191 #define	POOL_TAP4	411
192 
193 struct mutex entropylock = MUTEX_INITIALIZER(IPL_HIGH);
194 
195 /*
196  * Raw entropy collection from device drivers; at interrupt context or not.
197  * add_*_randomness() provide data which is put into the entropy queue.
198  * Almost completely under the entropylock.
199  */
200 struct timer_rand_state {	/* There is one of these per entropy source */
201 	u_int	last_time;
202 	u_int	last_delta;
203 	u_int	last_delta2;
204 	u_int	dont_count_entropy : 1;
205 	u_int	max_entropy : 1;
206 } rnd_states[RND_SRC_NUM];
207 
208 #define QEVLEN (1024 / sizeof(struct rand_event))
209 #define QEVSLOW (QEVLEN * 3 / 4) /* yet another 0.75 for 60-minutes hour /-; */
210 #define QEVSBITS 10
211 
212 struct rand_event {
213 	struct timer_rand_state *re_state;
214 	u_int re_nbits;
215 	u_int re_time;
216 	u_int re_val;
217 } rnd_event_space[QEVLEN];
218 struct rand_event *rnd_event_head = rnd_event_space;
219 struct rand_event *rnd_event_tail = rnd_event_space;
220 
221 struct timeout rnd_timeout;
222 struct rndstats rndstats;
223 
224 u_int32_t entropy_pool[POOLWORDS];
225 u_int	entropy_add_ptr;
226 u_char	entropy_input_rotate;
227 
228 void	dequeue_randomness(void *);
229 void	add_entropy_words(const u_int32_t *, u_int);
230 void	extract_entropy(u_int8_t *, int);
231 
232 int	filt_randomread(struct knote *, long);
233 void	filt_randomdetach(struct knote *);
234 int	filt_randomwrite(struct knote *, long);
235 
236 struct filterops randomread_filtops =
237 	{ 1, NULL, filt_randomdetach, filt_randomread };
238 struct filterops randomwrite_filtops =
239 	{ 1, NULL, filt_randomdetach, filt_randomwrite };
240 
241 static __inline struct rand_event *
242 rnd_get(void)
243 {
244 	struct rand_event *p = rnd_event_tail;
245 
246 	if (p == rnd_event_head)
247 		return NULL;
248 
249 	if (p + 1 >= &rnd_event_space[QEVLEN])
250 		rnd_event_tail = rnd_event_space;
251 	else
252 		rnd_event_tail++;
253 
254 	return p;
255 }
256 
257 static __inline struct rand_event *
258 rnd_put(void)
259 {
260 	struct rand_event *p = rnd_event_head + 1;
261 
262 	if (p >= &rnd_event_space[QEVLEN])
263 		p = rnd_event_space;
264 
265 	if (p == rnd_event_tail)
266 		return NULL;
267 
268 	return rnd_event_head = p;
269 }
270 
271 static __inline int
272 rnd_qlen(void)
273 {
274 	int len = rnd_event_head - rnd_event_tail;
275 	return (len < 0)? -len : len;
276 }
277 
278 /*
279  * This function adds entropy to the entropy pool by using timing
280  * delays.  It uses the timer_rand_state structure to make an estimate
281  * of how many bits of entropy this call has added to the pool.
282  *
283  * The number "val" is also added to the pool - it should somehow describe
284  * the type of event which just happened.  Currently the values of 0-255
285  * are for keyboard scan codes, 256 and upwards - for interrupts.
286  * On the i386, this is assumed to be at most 16 bits, and the high bits
287  * are used for a high-resolution timer.
288  */
289 void
290 enqueue_randomness(int state, int val)
291 {
292 	int	delta, delta2, delta3;
293 	struct timer_rand_state *p;
294 	struct rand_event *rep;
295 	struct timespec	ts;
296 	u_int	time, nbits;
297 
298 #ifdef DIAGNOSTIC
299 	if (state < 0 || state >= RND_SRC_NUM)
300 		return;
301 #endif
302 
303 	if (timeout_initialized(&rnd_timeout))
304 		nanotime(&ts);
305 
306 	p = &rnd_states[state];
307 	val += state << 13;
308 
309 	time = (ts.tv_nsec >> 10) + (ts.tv_sec << 20);
310 	nbits = 0;
311 
312 	/*
313 	 * Calculate the number of bits of randomness that we probably
314 	 * added.  We take into account the first and second order
315 	 * deltas in order to make our estimate.
316 	 */
317 	if (!p->dont_count_entropy) {
318 		delta  = time   - p->last_time;
319 		delta2 = delta  - p->last_delta;
320 		delta3 = delta2 - p->last_delta2;
321 
322 		if (delta < 0) delta = -delta;
323 		if (delta2 < 0) delta2 = -delta2;
324 		if (delta3 < 0) delta3 = -delta3;
325 		if (delta > delta2) delta = delta2;
326 		if (delta > delta3) delta = delta3;
327 		delta3 = delta >>= 1;
328 		/*
329 		 * delta &= 0xfff;
330 		 * we don't do it since our time sheet is different from linux
331 		 */
332 
333 		if (delta & 0xffff0000) {
334 			nbits = 16;
335 			delta >>= 16;
336 		}
337 		if (delta & 0xff00) {
338 			nbits += 8;
339 			delta >>= 8;
340 		}
341 		if (delta & 0xf0) {
342 			nbits += 4;
343 			delta >>= 4;
344 		}
345 		if (delta & 0xc) {
346 			nbits += 2;
347 			delta >>= 2;
348 		}
349 		if (delta & 2) {
350 			nbits += 1;
351 			delta >>= 1;
352 		}
353 		if (delta & 1)
354 			nbits++;
355 	} else if (p->max_entropy)
356 		nbits = 8 * sizeof(val) - 1;
357 
358 	/* given the multi-order delta logic above, this should never happen */
359 	if (nbits >= 32)
360 		return;
361 
362 	mtx_enter(&entropylock);
363 	if (!p->dont_count_entropy) {
364 		/*
365 		 * the logic is to drop low-entropy entries,
366 		 * in hope for dequeuing to be more randomfull
367 		 */
368 		if (rnd_qlen() > QEVSLOW && nbits < QEVSBITS) {
369 			rndstats.rnd_drople++;
370 			goto done;
371 		}
372 		p->last_time = time;
373 		p->last_delta  = delta3;
374 		p->last_delta2 = delta2;
375 	}
376 
377 	if ((rep = rnd_put()) == NULL) {
378 		rndstats.rnd_drops++;
379 		goto done;
380 	}
381 
382 	rep->re_state = p;
383 	rep->re_nbits = nbits;
384 	rep->re_time = ts.tv_nsec ^ (ts.tv_sec << 20);
385 	rep->re_val = val;
386 
387 	rndstats.rnd_enqs++;
388 	rndstats.rnd_ed[nbits]++;
389 	rndstats.rnd_sc[state]++;
390 	rndstats.rnd_sb[state] += nbits;
391 
392 	if (rnd_qlen() > QEVSLOW/2 && timeout_initialized(&rnd_timeout) &&
393 	    timeout_pending(&rnd_timeout))
394 		timeout_add(&rnd_timeout, 1);
395 done:
396 	mtx_leave(&entropylock);
397 }
398 
399 /*
400  * This function adds a byte into the entropy pool.  It does not
401  * update the entropy estimate.  The caller must do this if appropriate.
402  *
403  * The pool is stirred with a polynomial of degree POOLWORDS over GF(2);
404  * see POOL_TAP[1-4] above
405  *
406  * Rotate the input word by a changing number of bits, to help assure
407  * that all bits in the entropy get toggled.  Otherwise, if the pool
408  * is consistently feed small numbers (such as keyboard scan codes)
409  * then the upper bits of the entropy pool will frequently remain
410  * untouched.
411  */
412 void
413 add_entropy_words(const u_int32_t *buf, u_int n)
414 {
415 	/* derived from IEEE 802.3 CRC-32 */
416 	static const u_int32_t twist_table[8] = {
417 		0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
418 		0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278
419 	};
420 
421 	for (; n--; buf++) {
422 		u_int32_t w = (*buf << entropy_input_rotate) |
423 		    (*buf >> (32 - entropy_input_rotate));
424 		u_int i = entropy_add_ptr =
425 		    (entropy_add_ptr - 1) & POOLMASK;
426 		/*
427 		 * Normally, we add 7 bits of rotation to the pool.
428 		 * At the beginning of the pool, add an extra 7 bits
429 		 * rotation, so that successive passes spread the
430 		 * input bits across the pool evenly.
431 		 */
432 		entropy_input_rotate =
433 		    (entropy_input_rotate + (i ? 7 : 14)) & 31;
434 
435 		/* XOR pool contents corresponding to polynomial terms */
436 		w ^= entropy_pool[(i + POOL_TAP1) & POOLMASK] ^
437 		     entropy_pool[(i + POOL_TAP2) & POOLMASK] ^
438 		     entropy_pool[(i + POOL_TAP3) & POOLMASK] ^
439 		     entropy_pool[(i + POOL_TAP4) & POOLMASK] ^
440 		     entropy_pool[(i + 1) & POOLMASK] ^
441 		     entropy_pool[i]; /* + 2^POOLWORDS */
442 
443 		entropy_pool[i] = (w >> 3) ^ twist_table[w & 7];
444 	}
445 }
446 
447 /*
448  * Pulls entropy out of the queue and throws merges it into the pool
449  * with the CRC.
450  */
451 /* ARGSUSED */
452 void
453 dequeue_randomness(void *v)
454 {
455 	struct rand_event *rep;
456 	u_int32_t buf[2];
457 	u_int nbits;
458 
459 	mtx_enter(&entropylock);
460 
461 	if (timeout_initialized(&rnd_timeout))
462 		timeout_del(&rnd_timeout);
463 
464 	rndstats.rnd_deqs++;
465 	while ((rep = rnd_get())) {
466 		buf[0] = rep->re_time;
467 		buf[1] = rep->re_val;
468 		nbits = rep->re_nbits;
469 		mtx_leave(&entropylock);
470 
471 		add_entropy_words(buf, 2);
472 
473 		mtx_enter(&entropylock);
474 		rndstats.rnd_total += nbits;
475 	}
476 	mtx_leave(&entropylock);
477 }
478 
479 /*
480  * Grabs a chunk from the entropy_pool[] and slams it through MD5 when
481  * requested.
482  */
483 void
484 extract_entropy(u_int8_t *buf, int nbytes)
485 {
486 	static u_int32_t extract_pool[POOLWORDS];
487 	u_char buffer[16];
488 	MD5_CTX tmp;
489 	u_int i;
490 
491 	add_timer_randomness(nbytes);
492 
493 	while (nbytes) {
494 		i = MIN(nbytes, sizeof(buffer));
495 
496 		/*
497 		 * INTENTIONALLY not protected by entropylock.  Races
498 		 * during bcopy() result in acceptable input data; races
499 		 * during MD5Update() would create nasty data dependencies.
500 		 */
501 		bcopy(entropy_pool, extract_pool,
502 		    sizeof(extract_pool));
503 
504 		/* Hash the pool to get the output */
505 		MD5Init(&tmp);
506 		MD5Update(&tmp, (u_int8_t *)extract_pool, sizeof(extract_pool));
507 		MD5Final(buffer, &tmp);
508 
509 		/* Copy data to destination buffer */
510 		bcopy(buffer, buf, i);
511 		nbytes -= i;
512 		buf += i;
513 
514 		/* Modify pool so next hash will produce different results */
515 		add_timer_randomness(nbytes);
516 		dequeue_randomness(NULL);
517 	}
518 
519 	/* Wipe data from memory */
520 	explicit_bzero(extract_pool, sizeof(extract_pool));
521 	explicit_bzero(&tmp, sizeof(tmp));
522 	explicit_bzero(buffer, sizeof(buffer));
523 }
524 
525 /*
526  * Bytes of key material for each rc4 instance.
527  */
528 #define	ARC4_KEY_BYTES		64
529 
530 /*
531  * Throw away a multiple of the first N words of output, as suggested
532  * in the paper "Weaknesses in the Key Scheduling Algorithm of RC4"
533  * by Fluher, Mantin, and Shamir.  (N = 256 in our case.)  If the start
534  * of a new RC stream is an event that a consumer could spot, we drop
535  * the strictly recommended amount (ceil(n/log e) = 6).  If consumers
536  * only see random sub-streams, we cheat and do less computation.
537  */
538 #define	ARC4_STATE		256
539 #define	ARC4_DISCARD_SAFE	6
540 #define	ARC4_DISCARD_CHEAP	4
541 
542 /*
543  * Start with an unstable state so that rc4_getbytes() can
544  * operate (poorly) before rc4_keysetup().
545  */
546 struct rc4_ctx arc4random_state = { 0, 0, { 1, 2, 3, 4, 5, 6 } };
547 
548 struct mutex rndlock = MUTEX_INITIALIZER(IPL_HIGH);
549 struct timeout arc4_timeout;
550 
551 void arc4_reinit(void *v);		/* timeout to start reinit */
552 void arc4_init(void *, void *);		/* actually do the reinit */
553 
554 /* Return one word of randomness from an RC4 generator */
555 u_int32_t
556 arc4random(void)
557 {
558 	u_int32_t ret;
559 
560 	mtx_enter(&rndlock);
561 	rc4_getbytes(&arc4random_state, (u_char *)&ret, sizeof(ret));
562 	rndstats.arc4_reads += sizeof(ret);
563 	mtx_leave(&rndlock);
564 	return ret;
565 }
566 
567 /*
568  * Fill a buffer of arbitrary length with RC4-derived randomness.
569  */
570 void
571 arc4random_buf(void *buf, size_t n)
572 {
573 	mtx_enter(&rndlock);
574 	rc4_getbytes(&arc4random_state, (u_char *)buf, n);
575 	rndstats.arc4_reads += n;
576 	mtx_leave(&rndlock);
577 }
578 
579 /*
580  * Calculate a uniformly distributed random number less than upper_bound
581  * avoiding "modulo bias".
582  *
583  * Uniformity is achieved by generating new random numbers until the one
584  * returned is outside the range [0, 2**32 % upper_bound).  This
585  * guarantees the selected random number will be inside
586  * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
587  * after reduction modulo upper_bound.
588  */
589 u_int32_t
590 arc4random_uniform(u_int32_t upper_bound)
591 {
592 	u_int32_t r, min;
593 
594 	if (upper_bound < 2)
595 		return 0;
596 
597 #if (ULONG_MAX > 0xffffffffUL)
598 	min = 0x100000000UL % upper_bound;
599 #else
600 	/* Calculate (2**32 % upper_bound) avoiding 64-bit math */
601 	if (upper_bound > 0x80000000)
602 		min = 1 + ~upper_bound;		/* 2**32 - upper_bound */
603 	else {
604 		/* (2**32 - x) % x == 2**32 % x when x <= 2**31 */
605 		min = ((0xffffffff - upper_bound) + 1) % upper_bound;
606 	}
607 #endif
608 
609 	/*
610 	 * This could theoretically loop forever but each retry has
611 	 * p > 0.5 (worst case, usually far better) of selecting a
612 	 * number inside the range we need, so it should rarely need
613 	 * to re-roll.
614 	 */
615 	for (;;) {
616 		r = arc4random();
617 		if (r >= min)
618 			break;
619 	}
620 
621 	return r % upper_bound;
622 }
623 
624 /* ARGSUSED */
625 void
626 arc4_init(void *v, void *w)
627 {
628 	struct rc4_ctx new_ctx;
629 	struct timespec ts;
630 	u_int8_t buf[ARC4_KEY_BYTES], *p;
631 	int i;
632 
633 	/*
634 	 * Use MD5 PRNG data and a system timespec; early in the boot
635 	 * process this is the best we can do -- some architectures do
636 	 * not collect entropy very well during this time, but may have
637 	 * clock information which is better than nothing.
638 	 */
639 	extract_entropy((u_int8_t *)buf, sizeof buf);
640 	if (timeout_initialized(&rnd_timeout))
641 		nanotime(&ts);
642 	for (p = (u_int8_t *)&ts, i = 0; i < sizeof(ts); i++)
643 		buf[i] ^= p[i];
644 
645 	/* Carry over some state from the previous PRNG instance */
646 	mtx_enter(&rndlock);
647 	if (rndstats.arc4_nstirs > 0)
648 		rc4_crypt(&arc4random_state, buf, buf, sizeof(buf));
649 	mtx_leave(&rndlock);
650 
651 	rc4_keysetup(&new_ctx, buf, sizeof(buf));
652 	rc4_skip(&new_ctx, ARC4_STATE * ARC4_DISCARD_CHEAP);
653 
654 	mtx_enter(&rndlock);
655 	bcopy(&new_ctx, &arc4random_state, sizeof(new_ctx));
656 	rndstats.rnd_used += sizeof(buf) * 8;
657 	rndstats.arc4_nstirs++;
658 	mtx_leave(&rndlock);
659 
660 	explicit_bzero(buf, sizeof(buf));
661 	explicit_bzero(&new_ctx, sizeof(new_ctx));
662 }
663 
664 /*
665  * Called by timeout to mark arc4 for stirring,
666  */
667 void
668 arc4_reinit(void *v)
669 {
670 	workq_add_task(NULL, 0, arc4_init, NULL, NULL);
671 	/* 10 minutes, per dm@'s suggestion */
672 	timeout_add_sec(&arc4_timeout, 10 * 60);
673 }
674 
675 void
676 random_init(void)
677 {
678 	rnd_states[RND_SRC_TIMER].dont_count_entropy = 1;
679 	rnd_states[RND_SRC_TRUE].dont_count_entropy = 1;
680 	rnd_states[RND_SRC_TRUE].max_entropy = 1;
681 
682 	/*
683 	 * Load some code as input data until we are more alive.
684 	 * NOTE: We assume there are at 8192 bytes mapped after version,
685 	 * because we want to pull some "code" in as well.
686 	 */
687 	rc4_keysetup(&arc4random_state, (u_int8_t *)&version, 8192);
688 }
689 
690 void
691 random_start(void)
692 {
693 	if (msgbufp && msgbufp->msg_magic == MSG_MAGIC)
694 		add_entropy_words((u_int32_t *)msgbufp->msg_bufc,
695 		    msgbufp->msg_bufs / sizeof(u_int32_t));
696 
697 	dequeue_randomness(NULL);
698 	arc4_init(NULL, NULL);
699 	timeout_set(&arc4_timeout, arc4_reinit, NULL);
700 	arc4_reinit(NULL);
701 	timeout_set(&rnd_timeout, dequeue_randomness, NULL);
702 }
703 
704 int
705 randomopen(dev_t dev, int flag, int mode, struct proc *p)
706 {
707 	return 0;
708 }
709 
710 int
711 randomclose(dev_t dev, int flag, int mode, struct proc *p)
712 {
713 	return 0;
714 }
715 
716 /*
717  * Maximum number of bytes to serve directly from the main arc4random
718  * pool. Larger requests are served from a discrete arc4 instance keyed
719  * from the main pool.
720  */
721 #define ARC4_MAIN_MAX_BYTES	2048
722 
723 int
724 randomread(dev_t dev, struct uio *uio, int ioflag)
725 {
726 	u_char lbuf[ARC4_KEY_BYTES];
727 	struct rc4_ctx lctx;
728 	size_t		total = uio->uio_resid;
729 	u_char		*buf;
730 	int		myctx = 0, ret = 0;
731 
732 	if (uio->uio_resid == 0)
733 		return 0;
734 
735 	buf = malloc(POOLBYTES, M_TEMP, M_WAITOK);
736 	if (total > ARC4_MAIN_MAX_BYTES) {
737 		arc4random_buf(lbuf, sizeof(lbuf));
738 		rc4_keysetup(&lctx, lbuf, sizeof(lbuf));
739 		rc4_skip(&lctx, ARC4_STATE * ARC4_DISCARD_SAFE);
740 		explicit_bzero(lbuf, sizeof(lbuf));
741 		myctx = 1;
742 	}
743 
744 	while (ret == 0 && uio->uio_resid > 0) {
745 		int	n = min(POOLBYTES, uio->uio_resid);
746 
747 		if (myctx)
748 			rc4_getbytes(&lctx, buf, n);
749 		else
750 			arc4random_buf(buf, n);
751 		ret = uiomove((caddr_t)buf, n, uio);
752 		if (ret == 0 && uio->uio_resid > 0)
753 			yield();
754 	}
755 	if (myctx)
756 		explicit_bzero(&lctx, sizeof(lctx));
757 	explicit_bzero(buf, POOLBYTES);
758 	free(buf, M_TEMP);
759 	return ret;
760 }
761 
762 int
763 randomwrite(dev_t dev, struct uio *uio, int flags)
764 {
765 	int		ret = 0, newdata = 0;
766 	u_int32_t	*buf;
767 
768 	if (uio->uio_resid == 0)
769 		return 0;
770 
771 	buf = malloc(POOLBYTES, M_TEMP, M_WAITOK);
772 
773 	while (!ret && uio->uio_resid > 0) {
774 		int	n = min(POOLBYTES, uio->uio_resid);
775 
776 		ret = uiomove(buf, n, uio);
777 		if (ret)
778 			break;
779 		while (n % sizeof(u_int32_t))
780 			((u_int8_t *)buf)[n++] = 0;
781 		add_entropy_words(buf, n / 4);
782 		if (ret == 0 && uio->uio_resid > 0)
783 			yield();
784 		newdata = 1;
785 	}
786 
787 	if (newdata)
788 		arc4_init(NULL, NULL);
789 
790 	explicit_bzero(buf, POOLBYTES);
791 	free(buf, M_TEMP);
792 	return ret;
793 }
794 
795 int
796 randomkqfilter(dev_t dev, struct knote *kn)
797 {
798 	switch (kn->kn_filter) {
799 	case EVFILT_READ:
800 		kn->kn_fop = &randomread_filtops;
801 		break;
802 	case EVFILT_WRITE:
803 		kn->kn_fop = &randomwrite_filtops;
804 		break;
805 	default:
806 		return (EINVAL);
807 	}
808 
809 	return (0);
810 }
811 
812 void
813 filt_randomdetach(struct knote *kn)
814 {
815 }
816 
817 int
818 filt_randomread(struct knote *kn, long hint)
819 {
820 	kn->kn_data = ARC4_MAIN_MAX_BYTES;
821 	return (1);
822 }
823 
824 int
825 filt_randomwrite(struct knote *kn, long hint)
826 {
827 	kn->kn_data = POOLBYTES;
828 	return (1);
829 }
830 
831 int
832 randomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
833 {
834 	switch (cmd) {
835 	case FIOASYNC:
836 		/* No async flag in softc so this is a no-op. */
837 		break;
838 	case FIONBIO:
839 		/* Handled in the upper FS layer. */
840 		break;
841 	default:
842 		return ENOTTY;
843 	}
844 	return 0;
845 }
846