yarrow.c revision 74914
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 74914 2001-03-28 09:17:56Z jhb $ 2762053Smarkm */ 2862053Smarkm 2962053Smarkm#include <sys/param.h> 3062053Smarkm#include <sys/systm.h> 3165686Smarkm#include <sys/kernel.h> 3262053Smarkm#include <sys/libkern.h> 3374914Sjhb#include <sys/lock.h> 3467365Sjhb#include <sys/mutex.h> 3570834Swollman#include <sys/selinfo.h> 3662053Smarkm#include <sys/random.h> 3774072Smarkm#include <sys/sysctl.h> 3862053Smarkm#include <sys/types.h> 3969168Smarkm 4074072Smarkm#include <crypto/rijndael/rijndael.h> 4169168Smarkm 4267112Smarkm#include <dev/random/hash.h> 4374072Smarkm#include <dev/random/randomdev.h> 4467112Smarkm#include <dev/random/yarrow.h> 4562053Smarkm 4662765Smarkm/* #define DEBUG */ 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 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 7462765Smarkm/* Structure holding the entropy state */ 7562765Smarkmstruct random_state random_state; 7662765Smarkm 7765686Smarkm/* The reseed thread mutex */ 7865856Sjhbstatic struct mtx random_reseed_mtx; 7962765Smarkm 8074072Smarkm/* Process a single stochastic event off the harvest queue */ 8174072Smarkmvoid 8274072Smarkmrandom_process_event(struct harvest *event) 8362765Smarkm{ 8474072Smarkm u_int pl, src, overthreshhold[2]; 8565686Smarkm struct source *source; 8665686Smarkm 8774072Smarkm /* Unpack the event into the appropriate source accumulator */ 8874072Smarkm pl = random_state.which; 8974072Smarkm source = &random_state.pool[pl].source[event->source]; 9074072Smarkm yarrow_hash_iterate(&random_state.pool[pl].hash, event->entropy, 9174072Smarkm sizeof(event->entropy)); 9274072Smarkm yarrow_hash_iterate(&random_state.pool[pl].hash, &event->somecounter, 9374072Smarkm sizeof(event->somecounter)); 9474072Smarkm source->frac += event->frac; 9574072Smarkm source->bits += event->bits + source->frac/1024; 9674072Smarkm source->frac %= 1024; 9765686Smarkm 9874072Smarkm /* Count the over-threshold sources in each pool */ 9974072Smarkm for (pl = 0; pl < 2; pl++) { 10074072Smarkm overthreshhold[pl] = 0; 10174072Smarkm for (src = 0; src < ENTROPYSOURCE; src++) { 10274072Smarkm if (random_state.pool[pl].source[src].bits 10374072Smarkm > random_state.pool[pl].thresh) 10474072Smarkm overthreshhold[pl]++; 10565686Smarkm } 10674072Smarkm } 10765686Smarkm 10874072Smarkm /* if any fast source over threshhold, reseed */ 10974072Smarkm if (overthreshhold[FAST]) 11074072Smarkm reseed(FAST); 11165686Smarkm 11274072Smarkm /* if enough slow sources are over threshhold, reseed */ 11374072Smarkm if (overthreshhold[SLOW] >= random_state.slowoverthresh) 11474072Smarkm reseed(SLOW); 11565686Smarkm 11674072Smarkm /* Invert the fast/slow pool selector bit */ 11774072Smarkm random_state.which = !random_state.which; 11862765Smarkm} 11962765Smarkm 12074072Smarkmvoid 12162765Smarkmrandom_init(void) 12262053Smarkm{ 12374072Smarkm int i; 12465686Smarkm 12572364Smarkm /* Yarrow parameters. Do not adjust these unless you have 12672364Smarkm * have a very good clue about what they do! 12772364Smarkm */ 12862765Smarkm random_state.gengateinterval = 10; 12962765Smarkm random_state.bins = 10; 13074072Smarkm random_state.pool[0].thresh = (3*BLOCKSIZE)/4; 13174072Smarkm random_state.pool[1].thresh = BLOCKSIZE; 13262765Smarkm random_state.slowoverthresh = 2; 13362765Smarkm random_state.which = FAST; 13465686Smarkm 13574072Smarkm /* Initialise the fast and slow entropy pools */ 13674072Smarkm for (i = 0; i < 2; i++) 13774072Smarkm yarrow_hash_init(&random_state.pool[i].hash); 13865686Smarkm 13974072Smarkm /* Clear the counter */ 14074072Smarkm for (i = 0; i < 4; i++) 14174072Smarkm random_state.counter[i] = 0; 14269526Smarkm 14374072Smarkm /* Set up a lock for the reseed process */ 14474072Smarkm mtx_init(&random_reseed_mtx, "random reseed", MTX_DEF); 14562053Smarkm} 14662053Smarkm 14762053Smarkmvoid 14862765Smarkmrandom_deinit(void) 14962053Smarkm{ 15065686Smarkm mtx_destroy(&random_reseed_mtx); 15162765Smarkm} 15262765Smarkm 15362765Smarkmstatic void 15474072Smarkmreseed(u_int fastslow) 15562765Smarkm{ 15665686Smarkm /* Interrupt-context stack is a limited resource; make large 15765686Smarkm * structures static. 15863771Smarkm */ 15965686Smarkm static u_char v[TIMEBIN][KEYSIZE]; /* v[i] */ 16065686Smarkm static struct yarrowhash context; 16165686Smarkm u_char hash[KEYSIZE]; /* h' */ 16265686Smarkm u_char temp[KEYSIZE]; 16374072Smarkm u_int i, j; 16462053Smarkm 16562765Smarkm#ifdef DEBUG 16672200Sbmilekic mtx_lock(&Giant); 16762765Smarkm printf("Reseed type %d\n", fastslow); 16872200Sbmilekic mtx_unlock(&Giant); 16962765Smarkm#endif 17062765Smarkm 17165686Smarkm /* The reseed task must not be jumped on */ 17272200Sbmilekic mtx_lock(&random_reseed_mtx); 17365686Smarkm 17462053Smarkm /* 1. Hash the accumulated entropy into v[0] */ 17562053Smarkm 17674072Smarkm yarrow_hash_init(&context); 17765686Smarkm /* Feed the slow pool hash in if slow */ 17865686Smarkm if (fastslow == SLOW) 17965686Smarkm yarrow_hash_iterate(&context, 18074072Smarkm &random_state.pool[SLOW].hash, 18174072Smarkm sizeof(struct yarrowhash)); 18265686Smarkm yarrow_hash_iterate(&context, 18365686Smarkm &random_state.pool[FAST].hash, sizeof(struct yarrowhash)); 18474072Smarkm yarrow_hash_finish(&context, v[0]); 18562765Smarkm 18663771Smarkm /* 2. Compute hash values for all v. _Supposed_ to be computationally 18763771Smarkm * intensive. 18863771Smarkm */ 18962053Smarkm 19062765Smarkm if (random_state.bins > TIMEBIN) 19162765Smarkm random_state.bins = TIMEBIN; 19262765Smarkm for (i = 1; i < random_state.bins; i++) { 19374072Smarkm yarrow_hash_init(&context); 19474072Smarkm /* v[i] #= h(v[i - 1]) */ 19565686Smarkm yarrow_hash_iterate(&context, v[i - 1], KEYSIZE); 19662765Smarkm /* v[i] #= h(v[0]) */ 19765686Smarkm yarrow_hash_iterate(&context, v[0], KEYSIZE); 19862765Smarkm /* v[i] #= h(i) */ 19974072Smarkm yarrow_hash_iterate(&context, &i, sizeof(u_int)); 20065686Smarkm /* Return the hashval */ 20165686Smarkm yarrow_hash_finish(&context, v[i]); 20262053Smarkm } 20362053Smarkm 20465686Smarkm /* 3. Compute a new key; h' is the identity function here; 20565686Smarkm * it is not being ignored! 20665686Smarkm */ 20762053Smarkm 20874072Smarkm yarrow_hash_init(&context); 20965686Smarkm yarrow_hash_iterate(&context, &random_state.key, KEYSIZE); 21065686Smarkm for (i = 1; i < random_state.bins; i++) 21165686Smarkm yarrow_hash_iterate(&context, &v[i], KEYSIZE); 21265686Smarkm yarrow_hash_finish(&context, temp); 21374072Smarkm yarrow_encrypt_init(&random_state.key, temp); 21462053Smarkm 21562053Smarkm /* 4. Recompute the counter */ 21662053Smarkm 21774072Smarkm for (i = 0; i < 4; i++) 21874072Smarkm random_state.counter[i] = 0; 21974072Smarkm yarrow_encrypt(&random_state.key, random_state.counter, temp); 22074072Smarkm memcpy(random_state.counter, temp, sizeof(random_state.counter)); 22162053Smarkm 22262765Smarkm /* 5. Reset entropy estimate accumulators to zero */ 22362053Smarkm 22462765Smarkm for (i = 0; i <= fastslow; i++) { 22562765Smarkm for (j = 0; j < ENTROPYSOURCE; j++) { 22674072Smarkm random_state.pool[i].source[j].bits = 0; 22774072Smarkm random_state.pool[i].source[j].frac = 0; 22862765Smarkm } 22962765Smarkm } 23062053Smarkm 23162053Smarkm /* 6. Wipe memory of intermediate values */ 23262053Smarkm 23365686Smarkm memset((void *)v, 0, sizeof(v)); 23465686Smarkm memset((void *)temp, 0, sizeof(temp)); 23565686Smarkm memset((void *)hash, 0, sizeof(hash)); 23662053Smarkm 23765686Smarkm /* 7. Dump to seed file */ 23865686Smarkm /* XXX Not done here yet */ 23962053Smarkm 24065686Smarkm /* Release the reseed mutex */ 24172200Sbmilekic mtx_unlock(&random_reseed_mtx); 24265686Smarkm 24365686Smarkm#ifdef DEBUG 24472200Sbmilekic mtx_lock(&Giant); 24565686Smarkm printf("Reseed finish\n"); 24672200Sbmilekic mtx_unlock(&Giant); 24765686Smarkm#endif 24865686Smarkm 24974072Smarkm /* Unblock the device if it was blocked due to being unseeded */ 25074072Smarkm random_unblock(); 25162053Smarkm} 25262053Smarkm 25374072Smarkm/* Internal function to do return processed entropy from the 25474072Smarkm * Yarrow PRNG 25574072Smarkm */ 25662053Smarkmu_int 25767112Smarkmread_random_real(void *buf, u_int count) 25862053Smarkm{ 25962053Smarkm static int cur = 0; 26062053Smarkm static int gate = 1; 26174908Smarkm static u_char genval[KEYSIZE]; 26262053Smarkm u_int i; 26362053Smarkm u_int retval; 26462053Smarkm 26562875Smarkm /* The reseed task must not be jumped on */ 26672200Sbmilekic mtx_lock(&random_reseed_mtx); 26762875Smarkm 26862053Smarkm if (gate) { 26962053Smarkm generator_gate(); 27062765Smarkm random_state.outputblocks = 0; 27162053Smarkm gate = 0; 27262053Smarkm } 27362765Smarkm if (count >= sizeof(random_state.counter)) { 27462053Smarkm retval = 0; 27562765Smarkm for (i = 0; i < count; i += sizeof(random_state.counter)) { 27674072Smarkm random_state.counter[0]++; 27774072Smarkm yarrow_encrypt(&random_state.key, random_state.counter, 27874908Smarkm genval); 27974908Smarkm memcpy((char *)buf + i, genval, 28063855Smarkm sizeof(random_state.counter)); 28174072Smarkm if (++random_state.outputblocks >= 28274072Smarkm random_state.gengateinterval) { 28362053Smarkm generator_gate(); 28462765Smarkm random_state.outputblocks = 0; 28562053Smarkm } 28662765Smarkm retval += sizeof(random_state.counter); 28762053Smarkm } 28862053Smarkm } 28962053Smarkm else { 29062053Smarkm if (!cur) { 29174072Smarkm random_state.counter[0]++; 29274072Smarkm yarrow_encrypt(&random_state.key, random_state.counter, 29374908Smarkm genval); 29474908Smarkm memcpy(buf, genval, count); 29562765Smarkm cur = sizeof(random_state.counter) - count; 29674072Smarkm if (++random_state.outputblocks >= 29774072Smarkm random_state.gengateinterval) { 29862053Smarkm generator_gate(); 29962765Smarkm random_state.outputblocks = 0; 30062053Smarkm } 30162053Smarkm retval = count; 30262053Smarkm } 30362053Smarkm else { 30462053Smarkm retval = cur < count ? cur : count; 30574908Smarkm memcpy(buf, &genval[sizeof(random_state.counter) - cur], 30662053Smarkm 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