yarrow.c revision 254147
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 254147 2013-08-09 15:31:50Z obrien $");
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>
44254147Sobrien#include <dev/random/random_adaptors.h>
45128059Smarkm#include <dev/random/randomdev_soft.h>
4667112Smarkm#include <dev/random/yarrow.h>
4762053Smarkm
4874072SmarkmRANDOM_CHECK_UINT(gengateinterval, 4, 64);
4974072SmarkmRANDOM_CHECK_UINT(bins, 2, 16);
5074072SmarkmRANDOM_CHECK_UINT(fastthresh, BLOCKSIZE/4, BLOCKSIZE);
5174072SmarkmRANDOM_CHECK_UINT(slowthresh, BLOCKSIZE/4, BLOCKSIZE);
5274072SmarkmRANDOM_CHECK_UINT(slowoverthresh, 1, 5);
5374072Smarkm
5489170Smsmith/* Structure holding the entropy state */
5589170Smsmithstatic struct random_state random_state;
5689170Smsmith
5762765Smarkmstatic void generator_gate(void);
5874072Smarkmstatic void reseed(u_int);
5962053Smarkm
6065686Smarkm/* The reseed thread mutex */
61153575Spsstruct mtx random_reseed_mtx;
6262765Smarkm
6374072Smarkm/* Process a single stochastic event off the harvest queue */
6474072Smarkmvoid
6574072Smarkmrandom_process_event(struct harvest *event)
6662765Smarkm{
6791600Smarkm	u_int pl, overthreshhold[2];
6865686Smarkm	struct source *source;
6991600Smarkm	enum esource src;
7065686Smarkm
7174072Smarkm	/* Unpack the event into the appropriate source accumulator */
7274072Smarkm	pl = random_state.which;
7374072Smarkm	source = &random_state.pool[pl].source[event->source];
7474072Smarkm	yarrow_hash_iterate(&random_state.pool[pl].hash, event->entropy,
7574072Smarkm		sizeof(event->entropy));
7674072Smarkm	yarrow_hash_iterate(&random_state.pool[pl].hash, &event->somecounter,
7774072Smarkm		sizeof(event->somecounter));
7874072Smarkm	source->frac += event->frac;
7974072Smarkm	source->bits += event->bits + source->frac/1024;
8074072Smarkm	source->frac %= 1024;
8165686Smarkm
8274072Smarkm	/* Count the over-threshold sources in each pool */
8374072Smarkm	for (pl = 0; pl < 2; pl++) {
8474072Smarkm		overthreshhold[pl] = 0;
8591600Smarkm		for (src = RANDOM_START; src < ENTROPYSOURCE; src++) {
8674072Smarkm			if (random_state.pool[pl].source[src].bits
8774072Smarkm				> random_state.pool[pl].thresh)
8874072Smarkm				overthreshhold[pl]++;
8965686Smarkm		}
9074072Smarkm	}
9165686Smarkm
9274072Smarkm	/* if any fast source over threshhold, reseed */
9374072Smarkm	if (overthreshhold[FAST])
9474072Smarkm		reseed(FAST);
9565686Smarkm
9674072Smarkm	/* if enough slow sources are over threshhold, reseed */
9774072Smarkm	if (overthreshhold[SLOW] >= random_state.slowoverthresh)
9874072Smarkm		reseed(SLOW);
9965686Smarkm
10074072Smarkm	/* Invert the fast/slow pool selector bit */
10174072Smarkm	random_state.which = !random_state.which;
10262765Smarkm}
10362765Smarkm
10474072Smarkmvoid
105254147Sobrienrandom_yarrow_init_alg(struct sysctl_ctx_list *clist)
10662053Smarkm{
10774072Smarkm	int i;
108170025Srwatson	struct sysctl_oid *random_yarrow_o;
10965686Smarkm
11072364Smarkm	/* Yarrow parameters. Do not adjust these unless you have
11172364Smarkm	 * have a very good clue about what they do!
11272364Smarkm	 */
113170025Srwatson	random_yarrow_o = SYSCTL_ADD_NODE(clist,
114254147Sobrien		SYSCTL_STATIC_CHILDREN(_kern_random),
115128059Smarkm		OID_AUTO, "yarrow", CTLFLAG_RW, 0,
116128059Smarkm		"Yarrow Parameters");
117128059Smarkm
118170025Srwatson	SYSCTL_ADD_PROC(clist,
119128059Smarkm		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
120128059Smarkm		"gengateinterval", CTLTYPE_INT|CTLFLAG_RW,
121128059Smarkm		&random_state.gengateinterval, 10,
122128059Smarkm		random_check_uint_gengateinterval, "I",
123128059Smarkm		"Generation gate interval");
124128059Smarkm
125170025Srwatson	SYSCTL_ADD_PROC(clist,
126128059Smarkm		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
127128059Smarkm		"bins", CTLTYPE_INT|CTLFLAG_RW,
128128059Smarkm		&random_state.bins, 10,
129128059Smarkm		random_check_uint_bins, "I",
130128059Smarkm		"Execution time tuner");
131128059Smarkm
132170025Srwatson	SYSCTL_ADD_PROC(clist,
133128059Smarkm		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
134128059Smarkm		"fastthresh", CTLTYPE_INT|CTLFLAG_RW,
135128059Smarkm		&random_state.pool[0].thresh, (3*BLOCKSIZE)/4,
136128059Smarkm		random_check_uint_fastthresh, "I",
137128059Smarkm		"Fast reseed threshold");
138128059Smarkm
139170025Srwatson	SYSCTL_ADD_PROC(clist,
140128059Smarkm		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
141128059Smarkm		"slowthresh", CTLTYPE_INT|CTLFLAG_RW,
142128059Smarkm		&random_state.pool[1].thresh, BLOCKSIZE,
143128059Smarkm		random_check_uint_slowthresh, "I",
144128059Smarkm		"Slow reseed threshold");
145128059Smarkm
146170025Srwatson	SYSCTL_ADD_PROC(clist,
147128059Smarkm		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
148128059Smarkm		"slowoverthresh", CTLTYPE_INT|CTLFLAG_RW,
149128059Smarkm		&random_state.slowoverthresh, 2,
150128059Smarkm		random_check_uint_slowoverthresh, "I",
151128059Smarkm		"Slow over-threshold reseed");
152128059Smarkm
15362765Smarkm	random_state.gengateinterval = 10;
15462765Smarkm	random_state.bins = 10;
15574072Smarkm	random_state.pool[0].thresh = (3*BLOCKSIZE)/4;
15674072Smarkm	random_state.pool[1].thresh = BLOCKSIZE;
15762765Smarkm	random_state.slowoverthresh = 2;
15862765Smarkm	random_state.which = FAST;
15965686Smarkm
16074072Smarkm	/* Initialise the fast and slow entropy pools */
16174072Smarkm	for (i = 0; i < 2; i++)
16274072Smarkm		yarrow_hash_init(&random_state.pool[i].hash);
16365686Smarkm
16474072Smarkm	/* Clear the counter */
16574072Smarkm	for (i = 0; i < 4; i++)
16674072Smarkm		random_state.counter[i] = 0;
16769526Smarkm
16874072Smarkm	/* Set up a lock for the reseed process */
16993818Sjhb	mtx_init(&random_reseed_mtx, "random reseed", NULL, MTX_DEF);
17062053Smarkm}
17162053Smarkm
17262053Smarkmvoid
173128059Smarkmrandom_yarrow_deinit_alg(void)
17462053Smarkm{
17565686Smarkm	mtx_destroy(&random_reseed_mtx);
17662765Smarkm}
17762765Smarkm
17862765Smarkmstatic void
17974072Smarkmreseed(u_int fastslow)
18062765Smarkm{
18165686Smarkm	/* Interrupt-context stack is a limited resource; make large
18265686Smarkm	 * structures static.
18363771Smarkm	 */
18465686Smarkm	static u_char v[TIMEBIN][KEYSIZE];	/* v[i] */
18565686Smarkm	static struct yarrowhash context;
18665686Smarkm	u_char hash[KEYSIZE];			/* h' */
18765686Smarkm	u_char temp[KEYSIZE];
18891600Smarkm	u_int i;
18991600Smarkm	enum esource j;
19062053Smarkm
19165686Smarkm	/* The reseed task must not be jumped on */
19272200Sbmilekic	mtx_lock(&random_reseed_mtx);
19365686Smarkm
19462053Smarkm	/* 1. Hash the accumulated entropy into v[0] */
19562053Smarkm
19674072Smarkm	yarrow_hash_init(&context);
19765686Smarkm	/* Feed the slow pool hash in if slow */
19865686Smarkm	if (fastslow == SLOW)
19965686Smarkm		yarrow_hash_iterate(&context,
20074072Smarkm			&random_state.pool[SLOW].hash,
20174072Smarkm			sizeof(struct yarrowhash));
20265686Smarkm	yarrow_hash_iterate(&context,
20365686Smarkm		&random_state.pool[FAST].hash, sizeof(struct yarrowhash));
20474072Smarkm	yarrow_hash_finish(&context, v[0]);
20562765Smarkm
20663771Smarkm	/* 2. Compute hash values for all v. _Supposed_ to be computationally
20763771Smarkm	 *    intensive.
20863771Smarkm	 */
20962053Smarkm
21062765Smarkm	if (random_state.bins > TIMEBIN)
21162765Smarkm		random_state.bins = TIMEBIN;
21262765Smarkm	for (i = 1; i < random_state.bins; i++) {
21374072Smarkm		yarrow_hash_init(&context);
21474072Smarkm		/* v[i] #= h(v[i - 1]) */
21565686Smarkm		yarrow_hash_iterate(&context, v[i - 1], KEYSIZE);
21662765Smarkm		/* v[i] #= h(v[0]) */
21765686Smarkm		yarrow_hash_iterate(&context, v[0], KEYSIZE);
21862765Smarkm		/* v[i] #= h(i) */
21974072Smarkm		yarrow_hash_iterate(&context, &i, sizeof(u_int));
22065686Smarkm		/* Return the hashval */
22165686Smarkm		yarrow_hash_finish(&context, v[i]);
22262053Smarkm	}
22362053Smarkm
22465686Smarkm	/* 3. Compute a new key; h' is the identity function here;
22565686Smarkm	 *    it is not being ignored!
22665686Smarkm	 */
22762053Smarkm
22874072Smarkm	yarrow_hash_init(&context);
22965686Smarkm	yarrow_hash_iterate(&context, &random_state.key, KEYSIZE);
23065686Smarkm	for (i = 1; i < random_state.bins; i++)
23165686Smarkm		yarrow_hash_iterate(&context, &v[i], KEYSIZE);
23265686Smarkm	yarrow_hash_finish(&context, temp);
23374072Smarkm	yarrow_encrypt_init(&random_state.key, temp);
23462053Smarkm
23562053Smarkm	/* 4. Recompute the counter */
23662053Smarkm
23774072Smarkm	for (i = 0; i < 4; i++)
23874072Smarkm		random_state.counter[i] = 0;
23974072Smarkm	yarrow_encrypt(&random_state.key, random_state.counter, temp);
24074072Smarkm	memcpy(random_state.counter, temp, sizeof(random_state.counter));
24162053Smarkm
24262765Smarkm	/* 5. Reset entropy estimate accumulators to zero */
24362053Smarkm
24462765Smarkm	for (i = 0; i <= fastslow; i++) {
24591600Smarkm		for (j = RANDOM_START; j < ENTROPYSOURCE; j++) {
24674072Smarkm			random_state.pool[i].source[j].bits = 0;
24774072Smarkm			random_state.pool[i].source[j].frac = 0;
24862765Smarkm		}
24962765Smarkm	}
25062053Smarkm
25162053Smarkm	/* 6. Wipe memory of intermediate values */
25262053Smarkm
25365686Smarkm	memset((void *)v, 0, sizeof(v));
25465686Smarkm	memset((void *)temp, 0, sizeof(temp));
25565686Smarkm	memset((void *)hash, 0, sizeof(hash));
25662053Smarkm
25765686Smarkm	/* 7. Dump to seed file */
25865686Smarkm	/* XXX Not done here yet */
25962053Smarkm
260153575Sps	/* Unblock the device if it was blocked due to being unseeded */
261153575Sps	random_yarrow_unblock();
262153575Sps
26365686Smarkm	/* Release the reseed mutex */
26472200Sbmilekic	mtx_unlock(&random_reseed_mtx);
26562053Smarkm}
26662053Smarkm
267100082Smarkm/* Internal function to return processed entropy from the PRNG */
26891600Smarkmint
269128059Smarkmrandom_yarrow_read(void *buf, int count)
27062053Smarkm{
27162053Smarkm	static int cur = 0;
27262053Smarkm	static int gate = 1;
27374908Smarkm	static u_char genval[KEYSIZE];
274107789Smarkm	size_t tomove;
27591600Smarkm	int i;
27691600Smarkm	int retval;
27762053Smarkm
27862875Smarkm	/* The reseed task must not be jumped on */
27972200Sbmilekic	mtx_lock(&random_reseed_mtx);
28062875Smarkm
28162053Smarkm	if (gate) {
28262053Smarkm		generator_gate();
28362765Smarkm		random_state.outputblocks = 0;
28462053Smarkm		gate = 0;
28562053Smarkm	}
28691600Smarkm	if (count > 0 && (size_t)count >= sizeof(random_state.counter)) {
28762053Smarkm		retval = 0;
28891600Smarkm		for (i = 0; i < count; i += (int)sizeof(random_state.counter)) {
28974072Smarkm			random_state.counter[0]++;
29074072Smarkm			yarrow_encrypt(&random_state.key, random_state.counter,
29174908Smarkm				genval);
292107789Smarkm			tomove = min(count - i, sizeof(random_state.counter));
293107789Smarkm			memcpy((char *)buf + i, genval, tomove);
29474072Smarkm			if (++random_state.outputblocks >=
29574072Smarkm				random_state.gengateinterval) {
29662053Smarkm				generator_gate();
29762765Smarkm				random_state.outputblocks = 0;
29862053Smarkm			}
299107789Smarkm			retval += (int)tomove;
300174073Ssimon			cur = 0;
30162053Smarkm		}
30262053Smarkm	}
30362053Smarkm	else {
30462053Smarkm		if (!cur) {
30574072Smarkm			random_state.counter[0]++;
30674072Smarkm			yarrow_encrypt(&random_state.key, random_state.counter,
30774908Smarkm				genval);
30891600Smarkm			memcpy(buf, genval, (size_t)count);
30991600Smarkm			cur = (int)sizeof(random_state.counter) - count;
31074072Smarkm			if (++random_state.outputblocks >=
31174072Smarkm				random_state.gengateinterval) {
31262053Smarkm				generator_gate();
31362765Smarkm				random_state.outputblocks = 0;
31462053Smarkm			}
31562053Smarkm			retval = count;
31662053Smarkm		}
31762053Smarkm		else {
318128059Smarkm			retval = MIN(cur, count);
31991600Smarkm			memcpy(buf,
32091600Smarkm			    &genval[(int)sizeof(random_state.counter) - cur],
32191600Smarkm			    (size_t)retval);
32262053Smarkm			cur -= retval;
32362053Smarkm		}
32462053Smarkm	}
32572200Sbmilekic	mtx_unlock(&random_reseed_mtx);
32662053Smarkm	return retval;
32762053Smarkm}
32862053Smarkm
32962765Smarkmstatic void
33062053Smarkmgenerator_gate(void)
33162053Smarkm{
33274072Smarkm	u_int i;
33365686Smarkm	u_char temp[KEYSIZE];
33462053Smarkm
33562765Smarkm	for (i = 0; i < KEYSIZE; i += sizeof(random_state.counter)) {
33674072Smarkm		random_state.counter[0]++;
33774072Smarkm		yarrow_encrypt(&random_state.key, random_state.counter,
33874072Smarkm			&(temp[i]));
33962053Smarkm	}
34062053Smarkm
34174072Smarkm	yarrow_encrypt_init(&random_state.key, temp);
34265686Smarkm	memset((void *)temp, 0, KEYSIZE);
34362875Smarkm
34462053Smarkm}
34562765Smarkm
34669172Smarkm/* Helper routine to perform explicit reseeds */
34769172Smarkmvoid
348128059Smarkmrandom_yarrow_reseed(void)
34969172Smarkm{
350100082Smarkm	reseed(SLOW);
35169172Smarkm}
352