162765Smarkm/*- 2128059Smarkm * Copyright (c) 2000-2004 Mark R V Murray 362765Smarkm * All rights reserved. 462765Smarkm * 562765Smarkm * Redistribution and use in source and binary forms, with or without 662765Smarkm * modification, are permitted provided that the following conditions 762765Smarkm * are met: 862765Smarkm * 1. Redistributions of source code must retain the above copyright 962765Smarkm * notice, this list of conditions and the following disclaimer 1062765Smarkm * in this position and unchanged. 1162765Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1262765Smarkm * notice, this list of conditions and the following disclaimer in the 1362765Smarkm * documentation and/or other materials provided with the distribution. 1462765Smarkm * 1562765Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1662765Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1762765Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1862765Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1962765Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2062765Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2162765Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2262765Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2362765Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2462765Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2562765Smarkm * 2662765Smarkm */ 2762765Smarkm 28119418Sobrien#include <sys/cdefs.h> 29119418Sobrien__FBSDID("$FreeBSD$"); 30119418Sobrien 3162765Smarkm#include <sys/param.h> 3265686Smarkm#include <sys/kthread.h> 3374914Sjhb#include <sys/lock.h> 34122871Smarkm#include <sys/malloc.h> 3567365Sjhb#include <sys/mutex.h> 3667112Smarkm#include <sys/poll.h> 37122871Smarkm#include <sys/queue.h> 38122871Smarkm#include <sys/random.h> 3970834Swollman#include <sys/selinfo.h> 40128059Smarkm#include <sys/systm.h> 4174741Smarkm#include <sys/sysctl.h> 4269168Smarkm 4369168Smarkm#include <machine/cpu.h> 4469168Smarkm 45128059Smarkm#include <dev/random/randomdev_soft.h> 4662765Smarkm 4791600Smarkmstatic int read_random_phony(void *, int); 4867112Smarkm 4972667Smarkm/* Structure holding the desired entropy sources */ 50128320Smarkmstruct harvest_select harvest = { 1, 1, 1, 0 }; 5172667Smarkm 5263771Smarkm/* hold the address of the routine which is actually called if 5369168Smarkm * the randomdev is loaded 5463771Smarkm */ 55128059Smarkmstatic void (*reap_func)(u_int64_t, const void *, u_int, u_int, u_int, 56128059Smarkm enum esource) = NULL; 5791600Smarkmstatic int (*read_func)(void *, int) = read_random_phony; 5862765Smarkm 5962765Smarkm/* Initialise the harvester at load time */ 6062765Smarkmvoid 61128059Smarkmrandom_yarrow_init_harvester(void (*reaper)(u_int64_t, const void *, u_int, 62128059Smarkm u_int, u_int, enum esource), int (*reader)(void *, int)) 6362765Smarkm{ 6467112Smarkm reap_func = reaper; 6567112Smarkm read_func = reader; 6662765Smarkm} 6762765Smarkm 6862765Smarkm/* Deinitialise the harvester at unload time */ 6962765Smarkmvoid 70128059Smarkmrandom_yarrow_deinit_harvester(void) 7162765Smarkm{ 7267112Smarkm reap_func = NULL; 7367112Smarkm read_func = read_random_phony; 7462765Smarkm} 7562765Smarkm 7663771Smarkm/* Entropy harvesting routine. This is supposed to be fast; do 7763771Smarkm * not do anything slow in here! 7863771Smarkm * Implemented as in indirect call to allow non-inclusion of 7963771Smarkm * the entropy device. 80136672Srwatson * 81136672Srwatson * XXXRW: get_cyclecount() is cheap on most modern hardware, where cycle 82136672Srwatson * counters are built in, but on older hardware it will do a real time clock 83136672Srwatson * read which can be quite expensive. 8463771Smarkm */ 8562765Smarkmvoid 8680032Smarkmrandom_harvest(void *entropy, u_int count, u_int bits, u_int frac, 8780032Smarkm enum esource origin) 8862765Smarkm{ 8969168Smarkm if (reap_func) 9080032Smarkm (*reap_func)(get_cyclecount(), entropy, count, bits, frac, 9180032Smarkm origin); 9262765Smarkm} 9365686Smarkm 9467112Smarkm/* Userland-visible version of read_random */ 9591600Smarkmint 9691600Smarkmread_random(void *buf, int count) 9767112Smarkm{ 98128059Smarkm return ((*read_func)(buf, count)); 9967112Smarkm} 10067112Smarkm 10167112Smarkm/* If the entropy device is not loaded, make a token effort to 10267112Smarkm * provide _some_ kind of randomness. This should only be used 10367112Smarkm * inside other RNG's, like arc4random(9). 10465686Smarkm */ 10591600Smarkmstatic int 10691600Smarkmread_random_phony(void *buf, int count) 10765686Smarkm{ 10867112Smarkm u_long randval; 10967112Smarkm int size, i; 11067112Smarkm 111110404Sache /* srandom() is called in kern/init_main.c:proc0_post() */ 11267112Smarkm 11367112Smarkm /* Fill buf[] with random(9) output */ 11495197Smarkm for (i = 0; i < count; i+= (int)sizeof(u_long)) { 11567112Smarkm randval = random(); 116128059Smarkm size = MIN(count - i, sizeof(u_long)); 11791600Smarkm memcpy(&((char *)buf)[i], &randval, (size_t)size); 11867112Smarkm } 11967112Smarkm 120128059Smarkm return (count); 12165686Smarkm} 12265686Smarkm 123172836Sjulian/* Helper routine to enable kproc_exit() to work while the module is 12467112Smarkm * being (or has been) unloaded. 12567112Smarkm * This routine is in this file because it is always linked into the kernel, 12667112Smarkm * and will thus never be unloaded. This is critical for unloadable modules 12767112Smarkm * that have threads. 12867112Smarkm */ 12965686Smarkmvoid 13067112Smarkmrandom_set_wakeup_exit(void *control) 13165686Smarkm{ 13267112Smarkm wakeup(control); 133172836Sjulian kproc_exit(0); 13467112Smarkm /* NOTREACHED */ 13565686Smarkm} 136