yarrow.c revision 91600
162053Smarkm/*-
262765Smarkm * Copyright (c) 2000 Mark R V Murray
362053Smarkm * All rights reserved.
462053Smarkm *
562053Smarkm * Redistribution and use in source and binary forms, with or without
662053Smarkm * modification, are permitted provided that the following conditions
762053Smarkm * are met:
862053Smarkm * 1. Redistributions of source code must retain the above copyright
962053Smarkm *    notice, this list of conditions and the following disclaimer
1062053Smarkm *    in this position and unchanged.
1162053Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1262053Smarkm *    notice, this list of conditions and the following disclaimer in the
1362053Smarkm *    documentation and/or other materials provided with the distribution.
1462053Smarkm *
1562053Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1662053Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1762053Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1862053Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1962053Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2062053Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2162053Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2262053Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2362053Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2462053Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2562053Smarkm *
2662053Smarkm * $FreeBSD: head/sys/dev/random/yarrow.c 91600 2002-03-03 19:44:22Z markm $
2762053Smarkm */
2862053Smarkm
2962053Smarkm#include <sys/param.h>
3062053Smarkm#include <sys/systm.h>
3165686Smarkm#include <sys/kernel.h>
3274914Sjhb#include <sys/lock.h>
3367365Sjhb#include <sys/mutex.h>
3462053Smarkm#include <sys/random.h>
3574072Smarkm#include <sys/sysctl.h>
3669168Smarkm
3774072Smarkm#include <crypto/rijndael/rijndael.h>
3869168Smarkm
3967112Smarkm#include <dev/random/hash.h>
4074072Smarkm#include <dev/random/randomdev.h>
4167112Smarkm#include <dev/random/yarrow.h>
4262053Smarkm
4362765Smarkm/* #define DEBUG */
4462053Smarkm
4574072SmarkmRANDOM_CHECK_UINT(gengateinterval, 4, 64);
4674072SmarkmRANDOM_CHECK_UINT(bins, 2, 16);
4774072SmarkmRANDOM_CHECK_UINT(fastthresh, BLOCKSIZE/4, BLOCKSIZE);
4874072SmarkmRANDOM_CHECK_UINT(slowthresh, BLOCKSIZE/4, BLOCKSIZE);
4974072SmarkmRANDOM_CHECK_UINT(slowoverthresh, 1, 5);
5074072Smarkm
5189170Smsmith/* Structure holding the entropy state */
5289170Smsmithstatic struct random_state random_state;
5389170Smsmith
5474072SmarkmSYSCTL_NODE(_kern_random, OID_AUTO, yarrow, CTLFLAG_RW, 0, "Yarrow Parameters");
5574072SmarkmSYSCTL_PROC(_kern_random_yarrow, OID_AUTO, gengateinterval,
5674072Smarkm	CTLTYPE_INT|CTLFLAG_RW, &random_state.gengateinterval, 10,
5774072Smarkm	random_check_uint_gengateinterval, "I", "Generator Gate Interval");
5874072SmarkmSYSCTL_PROC(_kern_random_yarrow, OID_AUTO, bins,
5974072Smarkm	CTLTYPE_INT|CTLFLAG_RW, &random_state.bins, 10,
6074072Smarkm	random_check_uint_bins, "I", "Execution time tuner");
6174072SmarkmSYSCTL_PROC(_kern_random_yarrow, OID_AUTO, fastthresh,
6274072Smarkm	CTLTYPE_INT|CTLFLAG_RW, &random_state.pool[0].thresh, (3*BLOCKSIZE)/4,
6374072Smarkm	random_check_uint_fastthresh, "I", "Fast reseed threshold");
6474072SmarkmSYSCTL_PROC(_kern_random_yarrow, OID_AUTO, slowthresh,
6574072Smarkm	CTLTYPE_INT|CTLFLAG_RW, &random_state.pool[1].thresh, BLOCKSIZE,
6674072Smarkm	random_check_uint_slowthresh, "I", "Slow reseed threshold");
6774072SmarkmSYSCTL_PROC(_kern_random_yarrow, OID_AUTO, slowoverthresh,
6874072Smarkm	CTLTYPE_INT|CTLFLAG_RW, &random_state.slowoverthresh, 2,
6974072Smarkm	random_check_uint_slowoverthresh, "I", "Slow over-threshold reseed");
7074072Smarkm
7162765Smarkmstatic void generator_gate(void);
7274072Smarkmstatic void reseed(u_int);
7362053Smarkm
7465686Smarkm/* The reseed thread mutex */
7565856Sjhbstatic struct mtx random_reseed_mtx;
7662765Smarkm
7774072Smarkm/* Process a single stochastic event off the harvest queue */
7874072Smarkmvoid
7974072Smarkmrandom_process_event(struct harvest *event)
8062765Smarkm{
8191600Smarkm	u_int pl, overthreshhold[2];
8265686Smarkm	struct source *source;
8391600Smarkm	enum esource src;
8465686Smarkm
8574072Smarkm	/* Unpack the event into the appropriate source accumulator */
8674072Smarkm	pl = random_state.which;
8774072Smarkm	source = &random_state.pool[pl].source[event->source];
8874072Smarkm	yarrow_hash_iterate(&random_state.pool[pl].hash, event->entropy,
8974072Smarkm		sizeof(event->entropy));
9074072Smarkm	yarrow_hash_iterate(&random_state.pool[pl].hash, &event->somecounter,
9174072Smarkm		sizeof(event->somecounter));
9274072Smarkm	source->frac += event->frac;
9374072Smarkm	source->bits += event->bits + source->frac/1024;
9474072Smarkm	source->frac %= 1024;
9565686Smarkm
9674072Smarkm	/* Count the over-threshold sources in each pool */
9774072Smarkm	for (pl = 0; pl < 2; pl++) {
9874072Smarkm		overthreshhold[pl] = 0;
9991600Smarkm		for (src = RANDOM_START; src < ENTROPYSOURCE; src++) {
10074072Smarkm			if (random_state.pool[pl].source[src].bits
10174072Smarkm				> random_state.pool[pl].thresh)
10274072Smarkm				overthreshhold[pl]++;
10365686Smarkm		}
10474072Smarkm	}
10565686Smarkm
10674072Smarkm	/* if any fast source over threshhold, reseed */
10774072Smarkm	if (overthreshhold[FAST])
10874072Smarkm		reseed(FAST);
10965686Smarkm
11074072Smarkm	/* if enough slow sources are over threshhold, reseed */
11174072Smarkm	if (overthreshhold[SLOW] >= random_state.slowoverthresh)
11274072Smarkm		reseed(SLOW);
11365686Smarkm
11474072Smarkm	/* Invert the fast/slow pool selector bit */
11574072Smarkm	random_state.which = !random_state.which;
11662765Smarkm}
11762765Smarkm
11874072Smarkmvoid
11962765Smarkmrandom_init(void)
12062053Smarkm{
12174072Smarkm	int i;
12265686Smarkm
12372364Smarkm	/* Yarrow parameters. Do not adjust these unless you have
12472364Smarkm	 * have a very good clue about what they do!
12572364Smarkm	 */
12662765Smarkm	random_state.gengateinterval = 10;
12762765Smarkm	random_state.bins = 10;
12874072Smarkm	random_state.pool[0].thresh = (3*BLOCKSIZE)/4;
12974072Smarkm	random_state.pool[1].thresh = BLOCKSIZE;
13062765Smarkm	random_state.slowoverthresh = 2;
13162765Smarkm	random_state.which = FAST;
13265686Smarkm
13374072Smarkm	/* Initialise the fast and slow entropy pools */
13474072Smarkm	for (i = 0; i < 2; i++)
13574072Smarkm		yarrow_hash_init(&random_state.pool[i].hash);
13665686Smarkm
13774072Smarkm	/* Clear the counter */
13874072Smarkm	for (i = 0; i < 4; i++)
13974072Smarkm		random_state.counter[i] = 0;
14069526Smarkm
14174072Smarkm	/* Set up a lock for the reseed process */
14274072Smarkm	mtx_init(&random_reseed_mtx, "random reseed", MTX_DEF);
14362053Smarkm}
14462053Smarkm
14562053Smarkmvoid
14662765Smarkmrandom_deinit(void)
14762053Smarkm{
14865686Smarkm	mtx_destroy(&random_reseed_mtx);
14962765Smarkm}
15062765Smarkm
15162765Smarkmstatic void
15274072Smarkmreseed(u_int fastslow)
15362765Smarkm{
15465686Smarkm	/* Interrupt-context stack is a limited resource; make large
15565686Smarkm	 * structures static.
15663771Smarkm	 */
15765686Smarkm	static u_char v[TIMEBIN][KEYSIZE];	/* v[i] */
15865686Smarkm	static struct yarrowhash context;
15965686Smarkm	u_char hash[KEYSIZE];			/* h' */
16065686Smarkm	u_char temp[KEYSIZE];
16191600Smarkm	u_int i;
16291600Smarkm	enum esource j;
16362053Smarkm
16462765Smarkm#ifdef DEBUG
16572200Sbmilekic	mtx_lock(&Giant);
16662765Smarkm	printf("Reseed type %d\n", fastslow);
16772200Sbmilekic	mtx_unlock(&Giant);
16862765Smarkm#endif
16962765Smarkm
17065686Smarkm	/* The reseed task must not be jumped on */
17172200Sbmilekic	mtx_lock(&random_reseed_mtx);
17265686Smarkm
17362053Smarkm	/* 1. Hash the accumulated entropy into v[0] */
17462053Smarkm
17574072Smarkm	yarrow_hash_init(&context);
17665686Smarkm	/* Feed the slow pool hash in if slow */
17765686Smarkm	if (fastslow == SLOW)
17865686Smarkm		yarrow_hash_iterate(&context,
17974072Smarkm			&random_state.pool[SLOW].hash,
18074072Smarkm			sizeof(struct yarrowhash));
18165686Smarkm	yarrow_hash_iterate(&context,
18265686Smarkm		&random_state.pool[FAST].hash, sizeof(struct yarrowhash));
18374072Smarkm	yarrow_hash_finish(&context, v[0]);
18462765Smarkm
18563771Smarkm	/* 2. Compute hash values for all v. _Supposed_ to be computationally
18663771Smarkm	 *    intensive.
18763771Smarkm	 */
18862053Smarkm
18962765Smarkm	if (random_state.bins > TIMEBIN)
19062765Smarkm		random_state.bins = TIMEBIN;
19162765Smarkm	for (i = 1; i < random_state.bins; i++) {
19274072Smarkm		yarrow_hash_init(&context);
19374072Smarkm		/* v[i] #= h(v[i - 1]) */
19465686Smarkm		yarrow_hash_iterate(&context, v[i - 1], KEYSIZE);
19562765Smarkm		/* v[i] #= h(v[0]) */
19665686Smarkm		yarrow_hash_iterate(&context, v[0], KEYSIZE);
19762765Smarkm		/* v[i] #= h(i) */
19874072Smarkm		yarrow_hash_iterate(&context, &i, sizeof(u_int));
19965686Smarkm		/* Return the hashval */
20065686Smarkm		yarrow_hash_finish(&context, v[i]);
20162053Smarkm	}
20262053Smarkm
20365686Smarkm	/* 3. Compute a new key; h' is the identity function here;
20465686Smarkm	 *    it is not being ignored!
20565686Smarkm	 */
20662053Smarkm
20774072Smarkm	yarrow_hash_init(&context);
20865686Smarkm	yarrow_hash_iterate(&context, &random_state.key, KEYSIZE);
20965686Smarkm	for (i = 1; i < random_state.bins; i++)
21065686Smarkm		yarrow_hash_iterate(&context, &v[i], KEYSIZE);
21165686Smarkm	yarrow_hash_finish(&context, temp);
21274072Smarkm	yarrow_encrypt_init(&random_state.key, temp);
21362053Smarkm
21462053Smarkm	/* 4. Recompute the counter */
21562053Smarkm
21674072Smarkm	for (i = 0; i < 4; i++)
21774072Smarkm		random_state.counter[i] = 0;
21874072Smarkm	yarrow_encrypt(&random_state.key, random_state.counter, temp);
21974072Smarkm	memcpy(random_state.counter, temp, sizeof(random_state.counter));
22062053Smarkm
22162765Smarkm	/* 5. Reset entropy estimate accumulators to zero */
22262053Smarkm
22362765Smarkm	for (i = 0; i <= fastslow; i++) {
22491600Smarkm		for (j = RANDOM_START; j < ENTROPYSOURCE; j++) {
22574072Smarkm			random_state.pool[i].source[j].bits = 0;
22674072Smarkm			random_state.pool[i].source[j].frac = 0;
22762765Smarkm		}
22862765Smarkm	}
22962053Smarkm
23062053Smarkm	/* 6. Wipe memory of intermediate values */
23162053Smarkm
23265686Smarkm	memset((void *)v, 0, sizeof(v));
23365686Smarkm	memset((void *)temp, 0, sizeof(temp));
23465686Smarkm	memset((void *)hash, 0, sizeof(hash));
23562053Smarkm
23665686Smarkm	/* 7. Dump to seed file */
23765686Smarkm	/* XXX Not done here yet */
23862053Smarkm
23965686Smarkm	/* Release the reseed mutex */
24072200Sbmilekic	mtx_unlock(&random_reseed_mtx);
24165686Smarkm
24265686Smarkm#ifdef DEBUG
24372200Sbmilekic	mtx_lock(&Giant);
24465686Smarkm	printf("Reseed finish\n");
24572200Sbmilekic	mtx_unlock(&Giant);
24665686Smarkm#endif
24765686Smarkm
24874072Smarkm	/* Unblock the device if it was blocked due to being unseeded */
24974072Smarkm	random_unblock();
25062053Smarkm}
25162053Smarkm
25274072Smarkm/* Internal function to do return processed entropy from the
25374072Smarkm * Yarrow PRNG
25474072Smarkm */
25591600Smarkmint
25691600Smarkmread_random_real(void *buf, int count)
25762053Smarkm{
25862053Smarkm	static int cur = 0;
25962053Smarkm	static int gate = 1;
26074908Smarkm	static u_char genval[KEYSIZE];
26191600Smarkm	int i;
26291600Smarkm	int retval;
26362053Smarkm
26462875Smarkm	/* The reseed task must not be jumped on */
26572200Sbmilekic	mtx_lock(&random_reseed_mtx);
26662875Smarkm
26762053Smarkm	if (gate) {
26862053Smarkm		generator_gate();
26962765Smarkm		random_state.outputblocks = 0;
27062053Smarkm		gate = 0;
27162053Smarkm	}
27291600Smarkm	if (count > 0 && (size_t)count >= sizeof(random_state.counter)) {
27362053Smarkm		retval = 0;
27491600Smarkm		for (i = 0; i < count; i += (int)sizeof(random_state.counter)) {
27574072Smarkm			random_state.counter[0]++;
27674072Smarkm			yarrow_encrypt(&random_state.key, random_state.counter,
27774908Smarkm				genval);
27874908Smarkm			memcpy((char *)buf + i, genval,
27963855Smarkm				sizeof(random_state.counter));
28074072Smarkm			if (++random_state.outputblocks >=
28174072Smarkm				random_state.gengateinterval) {
28262053Smarkm				generator_gate();
28362765Smarkm				random_state.outputblocks = 0;
28462053Smarkm			}
28591600Smarkm			retval += (int)sizeof(random_state.counter);
28662053Smarkm		}
28762053Smarkm	}
28862053Smarkm	else {
28962053Smarkm		if (!cur) {
29074072Smarkm			random_state.counter[0]++;
29174072Smarkm			yarrow_encrypt(&random_state.key, random_state.counter,
29274908Smarkm				genval);
29391600Smarkm			memcpy(buf, genval, (size_t)count);
29491600Smarkm			cur = (int)sizeof(random_state.counter) - count;
29574072Smarkm			if (++random_state.outputblocks >=
29674072Smarkm				random_state.gengateinterval) {
29762053Smarkm				generator_gate();
29862765Smarkm				random_state.outputblocks = 0;
29962053Smarkm			}
30062053Smarkm			retval = count;
30162053Smarkm		}
30262053Smarkm		else {
30362053Smarkm			retval = cur < count ? cur : count;
30491600Smarkm			memcpy(buf,
30591600Smarkm			    &genval[(int)sizeof(random_state.counter) - cur],
30691600Smarkm			    (size_t)retval);
30762053Smarkm			cur -= retval;
30862053Smarkm		}
30962053Smarkm	}
31072200Sbmilekic	mtx_unlock(&random_reseed_mtx);
31162053Smarkm	return retval;
31262053Smarkm}
31362053Smarkm
31462765Smarkmstatic void
31562053Smarkmgenerator_gate(void)
31662053Smarkm{
31774072Smarkm	u_int i;
31865686Smarkm	u_char temp[KEYSIZE];
31962053Smarkm
32062765Smarkm#ifdef DEBUG
32172200Sbmilekic	mtx_lock(&Giant);
32262875Smarkm	printf("Generator gate\n");
32372200Sbmilekic	mtx_unlock(&Giant);
32462765Smarkm#endif
32562875Smarkm
32662765Smarkm	for (i = 0; i < KEYSIZE; i += sizeof(random_state.counter)) {
32774072Smarkm		random_state.counter[0]++;
32874072Smarkm		yarrow_encrypt(&random_state.key, random_state.counter,
32974072Smarkm			&(temp[i]));
33062053Smarkm	}
33162053Smarkm
33274072Smarkm	yarrow_encrypt_init(&random_state.key, temp);
33365686Smarkm	memset((void *)temp, 0, KEYSIZE);
33462875Smarkm
33565686Smarkm#ifdef DEBUG
33672200Sbmilekic	mtx_lock(&Giant);
33765686Smarkm	printf("Generator gate finish\n");
33872200Sbmilekic	mtx_unlock(&Giant);
33965686Smarkm#endif
34062053Smarkm}
34162765Smarkm
34269172Smarkm/* Helper routine to perform explicit reseeds */
34369172Smarkmvoid
34469172Smarkmrandom_reseed(void)
34569172Smarkm{
34669172Smarkm	reseed(FAST);
34769172Smarkm}
348