2 * kern_random.c -- A strong random number generator
4 * $FreeBSD: src/sys/kern/kern_random.c,v 1.36.2.4 2002/09/17 17:11:57 sam Exp $
5 * $DragonFly: src/sys/kern/Attic/kern_random.c,v 1.5 2003/07/29 21:30:02 hmp Exp $
7 * Version 0.95, last modified 18-Oct-95
9 * Copyright Theodore Ts'o, 1994, 1995. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, and the entire permission notice in its entirety,
16 * including the disclaimer of warranties.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote
21 * products derived from this software without specific prior
24 * ALTERNATIVELY, this product may be distributed under the terms of
25 * the GNU Public License, in which case the provisions of the GPL are
26 * required INSTEAD OF the above restrictions. (This clause is
27 * necessary due to a potential bad interaction between the GPL and
28 * the restrictions contained in a BSD-style copyright.)
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
31 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40 * OF THE POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/param.h>
44 #include <sys/kernel.h>
47 #include <sys/random.h>
48 #include <sys/select.h>
49 #include <sys/systm.h>
52 #include <i386/isa/icu.h>
56 XXX the below should be used. However there is too much "16"
57 hardcodeing in kern_random.c right now. -- obrien
58 #include <machine/ipl.h>
60 #define ICU_LEN (NHWI)
62 #define ICU_LEN (NSWI)
71 * The pool is stirred with a primitive polynomial of degree 128
72 * over GF(2), namely x^128 + x^99 + x^59 + x^31 + x^9 + x^7 + 1.
73 * For a pool of size 64, try x^64+x^62+x^38+x^10+x^6+x+1.
75 #define POOLWORDS 128 /* Power of 2 - note that this is 32-bit words */
76 #define POOLBITS (POOLWORDS*32)
79 #define TAP1 99 /* The polynomial taps */
85 #define TAP1 62 /* The polynomial taps */
91 #error No primitive polynomial available for chosen POOLWORDS
94 #define WRITEBUFFER 512 /* size in bytes */
96 /* There is actually only one of these, globally. */
97 struct random_bucket {
105 /* There is one of these per entropy source */
106 struct timer_rand_state {
112 static struct random_bucket random_state;
113 static u_int32_t random_pool[POOLWORDS];
114 static struct timer_rand_state keyboard_timer_state;
115 static struct timer_rand_state extract_timer_state;
116 static struct timer_rand_state irq_timer_state[MAX_INTS];
118 static struct timer_rand_state blkdev_timer_state[MAX_BLKDEV];
120 static struct wait_queue *random_wait;
123 rand_initialize(void)
125 random_state.add_ptr = 0;
126 random_state.entropy_count = 0;
127 random_state.pool = random_pool;
129 random_state.rsel.si_flags = 0;
130 random_state.rsel.si_pid = 0;
134 * This function adds an int into the entropy "pool". It does not
135 * update the entropy estimate. The caller must do this if appropriate.
137 * The pool is stirred with a primitive polynomial of degree 128
138 * over GF(2), namely x^128 + x^99 + x^59 + x^31 + x^9 + x^7 + 1.
139 * For a pool of size 64, try x^64+x^62+x^38+x^10+x^6+x+1.
141 * We rotate the input word by a changing number of bits, to help
142 * assure that all bits in the entropy get toggled. Otherwise, if we
143 * consistently feed the entropy pool small numbers (like ticks and
144 * scancodes, for example), the upper bits of the entropy pool don't
145 * get affected. --- TYT, 10/11/95
148 add_entropy_word(struct random_bucket *r, const u_int32_t input)
153 w = (input << r->input_rotate) | (input >> (32 - r->input_rotate));
154 i = r->add_ptr = (r->add_ptr - 1) & (POOLWORDS-1);
156 r->input_rotate = (r->input_rotate + 7) & 31;
159 * At the beginning of the pool, add an extra 7 bits
160 * rotation, so that successive passes spread the
161 * input bits across the pool evenly.
163 r->input_rotate = (r->input_rotate + 14) & 31;
165 /* XOR in the various taps */
166 w ^= r->pool[(i+TAP1)&(POOLWORDS-1)];
167 w ^= r->pool[(i+TAP2)&(POOLWORDS-1)];
168 w ^= r->pool[(i+TAP3)&(POOLWORDS-1)];
169 w ^= r->pool[(i+TAP4)&(POOLWORDS-1)];
170 w ^= r->pool[(i+TAP5)&(POOLWORDS-1)];
172 /* Rotate w left 1 bit (stolen from SHA) and store */
173 r->pool[i] = (w << 1) | (w >> 31);
177 * This function adds entropy to the entropy "pool" by using timing
178 * delays. It uses the timer_rand_state structure to make an estimate
179 * of how any bits of entropy this call has added to the pool.
181 * The number "num" is also added to the pool - it should somehow describe
182 * the type of event which just happened. This is currently 0-255 for
183 * keyboard scan codes, and 256 upwards for interrupts.
184 * On the i386, this is assumed to be at most 16 bits, and the high bits
185 * are used for a high-resolution timer.
188 add_timer_randomness(struct random_bucket *r, struct timer_rand_state *state,
194 struct timecounter *tc;
197 num ^= tc->tc_get_timecount(tc) << 16;
198 r->entropy_count += 2;
202 add_entropy_word(r, (u_int32_t) num);
203 add_entropy_word(r, time);
206 * Calculate number of bits of randomness we probably
207 * added. We take into account the first and second order
208 * deltas in order to make our estimate.
210 delta = time - state->last_time;
211 state->last_time = time;
213 delta2 = delta - state->last_delta;
214 state->last_delta = delta;
216 if (delta < 0) delta = -delta;
217 if (delta2 < 0) delta2 = -delta2;
218 delta = MIN(delta, delta2) >> 1;
219 for (nbits = 0; delta; nbits++)
222 r->entropy_count += nbits;
224 /* Prevent overflow */
225 if (r->entropy_count > POOLBITS)
226 r->entropy_count = POOLBITS;
228 if (r->entropy_count >= 8)
229 selwakeup(&random_state.rsel);
233 add_keyboard_randomness(u_char scancode)
235 add_timer_randomness(&random_state, &keyboard_timer_state, scancode);
239 add_interrupt_randomness(int intr)
241 add_timer_randomness(&random_state, &irq_timer_state[intr], intr);
246 add_blkdev_randomness(int major)
248 if (major >= MAX_BLKDEV)
251 add_timer_randomness(&random_state, &blkdev_timer_state[major],
257 #error extract_entropy() assumes that POOLWORDS is a multiple of 16 words.
260 * This function extracts randomness from the "entropy pool", and
261 * returns it in a buffer. This function computes how many remaining
262 * bits of entropy are left in the pool, but it does not restrict the
263 * number of bytes that are actually obtained.
266 extract_entropy(struct random_bucket *r, char *buf, int nbytes)
271 add_timer_randomness(r, &extract_timer_state, nbytes);
273 /* Redundant, but just in case... */
274 if (r->entropy_count > POOLBITS)
275 r->entropy_count = POOLBITS;
276 /* Why is this here? Left in from Ted Ts'o. Perhaps to limit time. */
281 if (r->entropy_count / 8 >= nbytes)
282 r->entropy_count -= nbytes*8;
284 r->entropy_count = 0;
287 /* Hash the pool to get the output */
292 for (i = 0; i < POOLWORDS; i += 16)
293 MD5Transform(tmp, (char *)(r->pool+i));
294 /* Modify pool so next hash will produce different results */
295 add_entropy_word(r, tmp[0]);
296 add_entropy_word(r, tmp[1]);
297 add_entropy_word(r, tmp[2]);
298 add_entropy_word(r, tmp[3]);
300 * Run the MD5 Transform one more time, since we want
301 * to add at least minimal obscuring of the inputs to
302 * add_entropy_word(). --- TYT
304 MD5Transform(tmp, (char *)(r->pool));
306 /* Copy data to destination buffer */
313 /* Wipe data from memory */
314 bzero(tmp, sizeof(tmp));
319 #ifdef notused /* XXX NOT the exported kernel interface */
321 * This function is the exported kernel interface. It returns some
322 * number of good random numbers, suitable for seeding TCP sequence
326 get_random_bytes(void *buf, u_int nbytes)
328 extract_entropy(&random_state, (char *) buf, nbytes);
333 read_random(void *buf, u_int nbytes)
335 if ((nbytes * 8) > random_state.entropy_count)
336 nbytes = random_state.entropy_count / 8;
338 return extract_entropy(&random_state, (char *)buf, nbytes);
342 read_random_unlimited(void *buf, u_int nbytes)
344 return extract_entropy(&random_state, (char *)buf, nbytes);
349 write_random(const char *buf, u_int nbytes)
354 for (i = nbytes, p = (u_int32_t *)buf;
355 i >= sizeof(u_int32_t);
356 i-= sizeof(u_int32_t), p++)
357 add_entropy_word(&random_state, *p);
361 add_entropy_word(&random_state, word);
368 add_true_randomness(int val)
370 add_entropy_word(&random_state, val);
371 random_state.entropy_count += 8*sizeof (val);
372 if (random_state.entropy_count > POOLBITS)
373 random_state.entropy_count = POOLBITS;
374 selwakeup(&random_state.rsel);
378 random_poll(dev_t dev, int events, struct thread *td)
384 if (events & (POLLIN | POLLRDNORM)) {
385 if (random_state.entropy_count >= 8)
386 revents |= events & (POLLIN | POLLRDNORM);
388 selrecord(td, &random_state.rsel);
391 if (events & (POLLOUT | POLLWRNORM))
392 revents |= events & (POLLOUT | POLLWRNORM); /* heh */