162765Smarkm/*- 2255362Smarkm * Copyright (c) 2000-2013 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> 40230230Sdas#include <sys/syslog.h> 41128059Smarkm#include <sys/systm.h> 4274741Smarkm#include <sys/sysctl.h> 4369168Smarkm 4469168Smarkm#include <machine/cpu.h> 4569168Smarkm 46128059Smarkm#include <dev/random/randomdev_soft.h> 4762765Smarkm 4891600Smarkmstatic int read_random_phony(void *, int); 4967112Smarkm 5072667Smarkm/* Structure holding the desired entropy sources */ 51256381Smarkmstruct harvest_select harvest = { 1, 1, 1, 1 }; 52230230Sdasstatic int warned = 0; 5372667Smarkm 5463771Smarkm/* hold the address of the routine which is actually called if 5569168Smarkm * the randomdev is loaded 5663771Smarkm */ 57256381Smarkmstatic void (*reap_func)(u_int64_t, const void *, u_int, u_int, 58128059Smarkm enum esource) = NULL; 5991600Smarkmstatic int (*read_func)(void *, int) = read_random_phony; 6062765Smarkm 6162765Smarkm/* Initialise the harvester at load time */ 6262765Smarkmvoid 63255362Smarkmrandomdev_init_harvester(void (*reaper)(u_int64_t, const void *, u_int, 64256381Smarkm u_int, enum esource), int (*reader)(void *, int)) 6562765Smarkm{ 6667112Smarkm reap_func = reaper; 6767112Smarkm read_func = reader; 6862765Smarkm} 6962765Smarkm 7062765Smarkm/* Deinitialise the harvester at unload time */ 7162765Smarkmvoid 72255362Smarkmrandomdev_deinit_harvester(void) 7362765Smarkm{ 7467112Smarkm reap_func = NULL; 7567112Smarkm read_func = read_random_phony; 76230230Sdas warned = 0; 7762765Smarkm} 7862765Smarkm 7963771Smarkm/* Entropy harvesting routine. This is supposed to be fast; do 8063771Smarkm * not do anything slow in here! 8163771Smarkm * Implemented as in indirect call to allow non-inclusion of 8263771Smarkm * the entropy device. 83136672Srwatson * 84136672Srwatson * XXXRW: get_cyclecount() is cheap on most modern hardware, where cycle 85136672Srwatson * counters are built in, but on older hardware it will do a real time clock 86136672Srwatson * read which can be quite expensive. 8763771Smarkm */ 8862765Smarkmvoid 89256381Smarkmrandom_harvest(void *entropy, u_int count, u_int bits, enum esource origin) 9062765Smarkm{ 9169168Smarkm if (reap_func) 92256381Smarkm (*reap_func)(get_cyclecount(), entropy, count, bits, origin); 9362765Smarkm} 9465686Smarkm 9567112Smarkm/* Userland-visible version of read_random */ 9691600Smarkmint 9791600Smarkmread_random(void *buf, int count) 9867112Smarkm{ 99128059Smarkm return ((*read_func)(buf, count)); 10067112Smarkm} 10167112Smarkm 10267112Smarkm/* If the entropy device is not loaded, make a token effort to 10367112Smarkm * provide _some_ kind of randomness. This should only be used 10467112Smarkm * inside other RNG's, like arc4random(9). 10565686Smarkm */ 10691600Smarkmstatic int 10791600Smarkmread_random_phony(void *buf, int count) 10865686Smarkm{ 10967112Smarkm u_long randval; 11067112Smarkm int size, i; 11167112Smarkm 112230230Sdas if (!warned) { 113230230Sdas log(LOG_WARNING, "random device not loaded; using insecure entropy\n"); 114230230Sdas warned = 1; 115230230Sdas } 116230230Sdas 117110404Sache /* srandom() is called in kern/init_main.c:proc0_post() */ 11867112Smarkm 11967112Smarkm /* Fill buf[] with random(9) output */ 12095197Smarkm for (i = 0; i < count; i+= (int)sizeof(u_long)) { 12167112Smarkm randval = random(); 122128059Smarkm size = MIN(count - i, sizeof(u_long)); 12391600Smarkm memcpy(&((char *)buf)[i], &randval, (size_t)size); 12467112Smarkm } 12567112Smarkm 126128059Smarkm return (count); 12765686Smarkm} 12865686Smarkm 129172836Sjulian/* Helper routine to enable kproc_exit() to work while the module is 13067112Smarkm * being (or has been) unloaded. 13167112Smarkm * This routine is in this file because it is always linked into the kernel, 13267112Smarkm * and will thus never be unloaded. This is critical for unloadable modules 13367112Smarkm * that have threads. 13467112Smarkm */ 13565686Smarkmvoid 13667112Smarkmrandom_set_wakeup_exit(void *control) 13765686Smarkm{ 13867112Smarkm wakeup(control); 139172836Sjulian kproc_exit(0); 14067112Smarkm /* NOTREACHED */ 14165686Smarkm} 142