yarrow.c revision 174073
162053Smarkm/*-
2128059Smarkm * Copyright (c) 2000-2004 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 */
2762053Smarkm
28119418Sobrien#include <sys/cdefs.h>
29119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/random/yarrow.c 174073 2007-11-29 16:06:12Z simon $");
30119418Sobrien
3162053Smarkm#include <sys/param.h>
3265686Smarkm#include <sys/kernel.h>
3374914Sjhb#include <sys/lock.h>
34122871Smarkm#include <sys/malloc.h>
3567365Sjhb#include <sys/mutex.h>
3662053Smarkm#include <sys/random.h>
3774072Smarkm#include <sys/sysctl.h>
38122871Smarkm#include <sys/systm.h>
3969168Smarkm
40143418Sume#include <crypto/rijndael/rijndael-api-fst.h>
41100082Smarkm#include <crypto/sha2/sha2.h>
4269168Smarkm
4367112Smarkm#include <dev/random/hash.h>
44128059Smarkm#include <dev/random/randomdev_soft.h>
4567112Smarkm#include <dev/random/yarrow.h>
4662053Smarkm
4774072SmarkmRANDOM_CHECK_UINT(gengateinterval, 4, 64);
4874072SmarkmRANDOM_CHECK_UINT(bins, 2, 16);
4974072SmarkmRANDOM_CHECK_UINT(fastthresh, BLOCKSIZE/4, BLOCKSIZE);
5074072SmarkmRANDOM_CHECK_UINT(slowthresh, BLOCKSIZE/4, BLOCKSIZE);
5174072SmarkmRANDOM_CHECK_UINT(slowoverthresh, 1, 5);
5274072Smarkm
5389170Smsmith/* Structure holding the entropy state */
5489170Smsmithstatic struct random_state random_state;
5589170Smsmith
5662765Smarkmstatic void generator_gate(void);
5774072Smarkmstatic void reseed(u_int);
5862053Smarkm
5965686Smarkm/* The reseed thread mutex */
60153575Spsstruct mtx random_reseed_mtx;
6162765Smarkm
6274072Smarkm/* Process a single stochastic event off the harvest queue */
6374072Smarkmvoid
6474072Smarkmrandom_process_event(struct harvest *event)
6562765Smarkm{
6691600Smarkm	u_int pl, overthreshhold[2];
6765686Smarkm	struct source *source;
6891600Smarkm	enum esource src;
6965686Smarkm
7074072Smarkm	/* Unpack the event into the appropriate source accumulator */
7174072Smarkm	pl = random_state.which;
7274072Smarkm	source = &random_state.pool[pl].source[event->source];
7374072Smarkm	yarrow_hash_iterate(&random_state.pool[pl].hash, event->entropy,
7474072Smarkm		sizeof(event->entropy));
7574072Smarkm	yarrow_hash_iterate(&random_state.pool[pl].hash, &event->somecounter,
7674072Smarkm		sizeof(event->somecounter));
7774072Smarkm	source->frac += event->frac;
7874072Smarkm	source->bits += event->bits + source->frac/1024;
7974072Smarkm	source->frac %= 1024;
8065686Smarkm
8174072Smarkm	/* Count the over-threshold sources in each pool */
8274072Smarkm	for (pl = 0; pl < 2; pl++) {
8374072Smarkm		overthreshhold[pl] = 0;
8491600Smarkm		for (src = RANDOM_START; src < ENTROPYSOURCE; src++) {
8574072Smarkm			if (random_state.pool[pl].source[src].bits
8674072Smarkm				> random_state.pool[pl].thresh)
8774072Smarkm				overthreshhold[pl]++;
8865686Smarkm		}
8974072Smarkm	}
9065686Smarkm
9174072Smarkm	/* if any fast source over threshhold, reseed */
9274072Smarkm	if (overthreshhold[FAST])
9374072Smarkm		reseed(FAST);
9465686Smarkm
9574072Smarkm	/* if enough slow sources are over threshhold, reseed */
9674072Smarkm	if (overthreshhold[SLOW] >= random_state.slowoverthresh)
9774072Smarkm		reseed(SLOW);
9865686Smarkm
9974072Smarkm	/* Invert the fast/slow pool selector bit */
10074072Smarkm	random_state.which = !random_state.which;
10162765Smarkm}
10262765Smarkm
10374072Smarkmvoid
104128059Smarkmrandom_yarrow_init_alg(struct sysctl_ctx_list *clist, struct sysctl_oid *in_o)
10562053Smarkm{
10674072Smarkm	int i;
107170025Srwatson	struct sysctl_oid *random_yarrow_o;
10865686Smarkm
10972364Smarkm	/* Yarrow parameters. Do not adjust these unless you have
11072364Smarkm	 * have a very good clue about what they do!
11172364Smarkm	 */
112170025Srwatson	random_yarrow_o = SYSCTL_ADD_NODE(clist,
113128059Smarkm		SYSCTL_CHILDREN(in_o),
114128059Smarkm		OID_AUTO, "yarrow", CTLFLAG_RW, 0,
115128059Smarkm		"Yarrow Parameters");
116128059Smarkm
117170025Srwatson	SYSCTL_ADD_PROC(clist,
118128059Smarkm		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
119128059Smarkm		"gengateinterval", CTLTYPE_INT|CTLFLAG_RW,
120128059Smarkm		&random_state.gengateinterval, 10,
121128059Smarkm		random_check_uint_gengateinterval, "I",
122128059Smarkm		"Generation gate interval");
123128059Smarkm
124170025Srwatson	SYSCTL_ADD_PROC(clist,
125128059Smarkm		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
126128059Smarkm		"bins", CTLTYPE_INT|CTLFLAG_RW,
127128059Smarkm		&random_state.bins, 10,
128128059Smarkm		random_check_uint_bins, "I",
129128059Smarkm		"Execution time tuner");
130128059Smarkm
131170025Srwatson	SYSCTL_ADD_PROC(clist,
132128059Smarkm		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
133128059Smarkm		"fastthresh", CTLTYPE_INT|CTLFLAG_RW,
134128059Smarkm		&random_state.pool[0].thresh, (3*BLOCKSIZE)/4,
135128059Smarkm		random_check_uint_fastthresh, "I",
136128059Smarkm		"Fast reseed threshold");
137128059Smarkm
138170025Srwatson	SYSCTL_ADD_PROC(clist,
139128059Smarkm		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
140128059Smarkm		"slowthresh", CTLTYPE_INT|CTLFLAG_RW,
141128059Smarkm		&random_state.pool[1].thresh, BLOCKSIZE,
142128059Smarkm		random_check_uint_slowthresh, "I",
143128059Smarkm		"Slow reseed threshold");
144128059Smarkm
145170025Srwatson	SYSCTL_ADD_PROC(clist,
146128059Smarkm		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
147128059Smarkm		"slowoverthresh", CTLTYPE_INT|CTLFLAG_RW,
148128059Smarkm		&random_state.slowoverthresh, 2,
149128059Smarkm		random_check_uint_slowoverthresh, "I",
150128059Smarkm		"Slow over-threshold reseed");
151128059Smarkm
15262765Smarkm	random_state.gengateinterval = 10;
15362765Smarkm	random_state.bins = 10;
15474072Smarkm	random_state.pool[0].thresh = (3*BLOCKSIZE)/4;
15574072Smarkm	random_state.pool[1].thresh = BLOCKSIZE;
15662765Smarkm	random_state.slowoverthresh = 2;
15762765Smarkm	random_state.which = FAST;
15865686Smarkm
15974072Smarkm	/* Initialise the fast and slow entropy pools */
16074072Smarkm	for (i = 0; i < 2; i++)
16174072Smarkm		yarrow_hash_init(&random_state.pool[i].hash);
16265686Smarkm
16374072Smarkm	/* Clear the counter */
16474072Smarkm	for (i = 0; i < 4; i++)
16574072Smarkm		random_state.counter[i] = 0;
16669526Smarkm
16774072Smarkm	/* Set up a lock for the reseed process */
16893818Sjhb	mtx_init(&random_reseed_mtx, "random reseed", NULL, MTX_DEF);
16962053Smarkm}
17062053Smarkm
17162053Smarkmvoid
172128059Smarkmrandom_yarrow_deinit_alg(void)
17362053Smarkm{
17465686Smarkm	mtx_destroy(&random_reseed_mtx);
17562765Smarkm}
17662765Smarkm
17762765Smarkmstatic void
17874072Smarkmreseed(u_int fastslow)
17962765Smarkm{
18065686Smarkm	/* Interrupt-context stack is a limited resource; make large
18165686Smarkm	 * structures static.
18263771Smarkm	 */
18365686Smarkm	static u_char v[TIMEBIN][KEYSIZE];	/* v[i] */
18465686Smarkm	static struct yarrowhash context;
18565686Smarkm	u_char hash[KEYSIZE];			/* h' */
18665686Smarkm	u_char temp[KEYSIZE];
18791600Smarkm	u_int i;
18891600Smarkm	enum esource j;
18962053Smarkm
19065686Smarkm	/* The reseed task must not be jumped on */
19172200Sbmilekic	mtx_lock(&random_reseed_mtx);
19265686Smarkm
19362053Smarkm	/* 1. Hash the accumulated entropy into v[0] */
19462053Smarkm
19574072Smarkm	yarrow_hash_init(&context);
19665686Smarkm	/* Feed the slow pool hash in if slow */
19765686Smarkm	if (fastslow == SLOW)
19865686Smarkm		yarrow_hash_iterate(&context,
19974072Smarkm			&random_state.pool[SLOW].hash,
20074072Smarkm			sizeof(struct yarrowhash));
20165686Smarkm	yarrow_hash_iterate(&context,
20265686Smarkm		&random_state.pool[FAST].hash, sizeof(struct yarrowhash));
20374072Smarkm	yarrow_hash_finish(&context, v[0]);
20462765Smarkm
20563771Smarkm	/* 2. Compute hash values for all v. _Supposed_ to be computationally
20663771Smarkm	 *    intensive.
20763771Smarkm	 */
20862053Smarkm
20962765Smarkm	if (random_state.bins > TIMEBIN)
21062765Smarkm		random_state.bins = TIMEBIN;
21162765Smarkm	for (i = 1; i < random_state.bins; i++) {
21274072Smarkm		yarrow_hash_init(&context);
21374072Smarkm		/* v[i] #= h(v[i - 1]) */
21465686Smarkm		yarrow_hash_iterate(&context, v[i - 1], KEYSIZE);
21562765Smarkm		/* v[i] #= h(v[0]) */
21665686Smarkm		yarrow_hash_iterate(&context, v[0], KEYSIZE);
21762765Smarkm		/* v[i] #= h(i) */
21874072Smarkm		yarrow_hash_iterate(&context, &i, sizeof(u_int));
21965686Smarkm		/* Return the hashval */
22065686Smarkm		yarrow_hash_finish(&context, v[i]);
22162053Smarkm	}
22262053Smarkm
22365686Smarkm	/* 3. Compute a new key; h' is the identity function here;
22465686Smarkm	 *    it is not being ignored!
22565686Smarkm	 */
22662053Smarkm
22774072Smarkm	yarrow_hash_init(&context);
22865686Smarkm	yarrow_hash_iterate(&context, &random_state.key, KEYSIZE);
22965686Smarkm	for (i = 1; i < random_state.bins; i++)
23065686Smarkm		yarrow_hash_iterate(&context, &v[i], KEYSIZE);
23165686Smarkm	yarrow_hash_finish(&context, temp);
23274072Smarkm	yarrow_encrypt_init(&random_state.key, temp);
23362053Smarkm
23462053Smarkm	/* 4. Recompute the counter */
23562053Smarkm
23674072Smarkm	for (i = 0; i < 4; i++)
23774072Smarkm		random_state.counter[i] = 0;
23874072Smarkm	yarrow_encrypt(&random_state.key, random_state.counter, temp);
23974072Smarkm	memcpy(random_state.counter, temp, sizeof(random_state.counter));
24062053Smarkm
24162765Smarkm	/* 5. Reset entropy estimate accumulators to zero */
24262053Smarkm
24362765Smarkm	for (i = 0; i <= fastslow; i++) {
24491600Smarkm		for (j = RANDOM_START; j < ENTROPYSOURCE; j++) {
24574072Smarkm			random_state.pool[i].source[j].bits = 0;
24674072Smarkm			random_state.pool[i].source[j].frac = 0;
24762765Smarkm		}
24862765Smarkm	}
24962053Smarkm
25062053Smarkm	/* 6. Wipe memory of intermediate values */
25162053Smarkm
25265686Smarkm	memset((void *)v, 0, sizeof(v));
25365686Smarkm	memset((void *)temp, 0, sizeof(temp));
25465686Smarkm	memset((void *)hash, 0, sizeof(hash));
25562053Smarkm
25665686Smarkm	/* 7. Dump to seed file */
25765686Smarkm	/* XXX Not done here yet */
25862053Smarkm
259153575Sps	/* Unblock the device if it was blocked due to being unseeded */
260153575Sps	random_yarrow_unblock();
261153575Sps
26265686Smarkm	/* Release the reseed mutex */
26372200Sbmilekic	mtx_unlock(&random_reseed_mtx);
26462053Smarkm}
26562053Smarkm
266100082Smarkm/* Internal function to return processed entropy from the PRNG */
26791600Smarkmint
268128059Smarkmrandom_yarrow_read(void *buf, int count)
26962053Smarkm{
27062053Smarkm	static int cur = 0;
27162053Smarkm	static int gate = 1;
27274908Smarkm	static u_char genval[KEYSIZE];
273107789Smarkm	size_t tomove;
27491600Smarkm	int i;
27591600Smarkm	int retval;
27662053Smarkm
27762875Smarkm	/* The reseed task must not be jumped on */
27872200Sbmilekic	mtx_lock(&random_reseed_mtx);
27962875Smarkm
28062053Smarkm	if (gate) {
28162053Smarkm		generator_gate();
28262765Smarkm		random_state.outputblocks = 0;
28362053Smarkm		gate = 0;
28462053Smarkm	}
28591600Smarkm	if (count > 0 && (size_t)count >= sizeof(random_state.counter)) {
28662053Smarkm		retval = 0;
28791600Smarkm		for (i = 0; i < count; i += (int)sizeof(random_state.counter)) {
28874072Smarkm			random_state.counter[0]++;
28974072Smarkm			yarrow_encrypt(&random_state.key, random_state.counter,
29074908Smarkm				genval);
291107789Smarkm			tomove = min(count - i, sizeof(random_state.counter));
292107789Smarkm			memcpy((char *)buf + i, genval, tomove);
29374072Smarkm			if (++random_state.outputblocks >=
29474072Smarkm				random_state.gengateinterval) {
29562053Smarkm				generator_gate();
29662765Smarkm				random_state.outputblocks = 0;
29762053Smarkm			}
298107789Smarkm			retval += (int)tomove;
299174073Ssimon			cur = 0;
30062053Smarkm		}
30162053Smarkm	}
30262053Smarkm	else {
30362053Smarkm		if (!cur) {
30474072Smarkm			random_state.counter[0]++;
30574072Smarkm			yarrow_encrypt(&random_state.key, random_state.counter,
30674908Smarkm				genval);
30791600Smarkm			memcpy(buf, genval, (size_t)count);
30891600Smarkm			cur = (int)sizeof(random_state.counter) - count;
30974072Smarkm			if (++random_state.outputblocks >=
31074072Smarkm				random_state.gengateinterval) {
31162053Smarkm				generator_gate();
31262765Smarkm				random_state.outputblocks = 0;
31362053Smarkm			}
31462053Smarkm			retval = count;
31562053Smarkm		}
31662053Smarkm		else {
317128059Smarkm			retval = MIN(cur, count);
31891600Smarkm			memcpy(buf,
31991600Smarkm			    &genval[(int)sizeof(random_state.counter) - cur],
32091600Smarkm			    (size_t)retval);
32162053Smarkm			cur -= retval;
32262053Smarkm		}
32362053Smarkm	}
32472200Sbmilekic	mtx_unlock(&random_reseed_mtx);
32562053Smarkm	return retval;
32662053Smarkm}
32762053Smarkm
32862765Smarkmstatic void
32962053Smarkmgenerator_gate(void)
33062053Smarkm{
33174072Smarkm	u_int i;
33265686Smarkm	u_char temp[KEYSIZE];
33362053Smarkm
33462765Smarkm	for (i = 0; i < KEYSIZE; i += sizeof(random_state.counter)) {
33574072Smarkm		random_state.counter[0]++;
33674072Smarkm		yarrow_encrypt(&random_state.key, random_state.counter,
33774072Smarkm			&(temp[i]));
33862053Smarkm	}
33962053Smarkm
34074072Smarkm	yarrow_encrypt_init(&random_state.key, temp);
34165686Smarkm	memset((void *)temp, 0, KEYSIZE);
34262875Smarkm
34362053Smarkm}
34462765Smarkm
34569172Smarkm/* Helper routine to perform explicit reseeds */
34669172Smarkmvoid
347128059Smarkmrandom_yarrow_reseed(void)
34869172Smarkm{
349100082Smarkm	reseed(SLOW);
35069172Smarkm}
351