randomdev_soft.c revision 170067
1128059Smarkm/*- 2128059Smarkm * Copyright (c) 2000-2004 Mark R V Murray 3136338Srwatson * Copyright (c) 2004 Robert N. M. Watson 4128059Smarkm * All rights reserved. 5128059Smarkm * 6128059Smarkm * Redistribution and use in source and binary forms, with or without 7128059Smarkm * modification, are permitted provided that the following conditions 8128059Smarkm * are met: 9128059Smarkm * 1. Redistributions of source code must retain the above copyright 10128059Smarkm * notice, this list of conditions and the following disclaimer 11128059Smarkm * in this position and unchanged. 12128059Smarkm * 2. Redistributions in binary form must reproduce the above copyright 13128059Smarkm * notice, this list of conditions and the following disclaimer in the 14128059Smarkm * documentation and/or other materials provided with the distribution. 15128059Smarkm * 16128059Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17128059Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18128059Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19128059Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20128059Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21128059Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22128059Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23128059Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24128059Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25128059Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26128059Smarkm * 27128059Smarkm */ 28128059Smarkm 29128059Smarkm#include <sys/cdefs.h> 30128059Smarkm__FBSDID("$FreeBSD: head/sys/dev/random/randomdev_soft.c 170067 2007-05-28 18:20:15Z rwatson $"); 31128059Smarkm 32128059Smarkm#include <sys/param.h> 33128059Smarkm#include <sys/systm.h> 34128059Smarkm#include <sys/bus.h> 35128059Smarkm#include <sys/conf.h> 36128059Smarkm#include <sys/fcntl.h> 37128059Smarkm#include <sys/kernel.h> 38128059Smarkm#include <sys/kthread.h> 39128059Smarkm#include <sys/lock.h> 40128059Smarkm#include <sys/malloc.h> 41128059Smarkm#include <sys/mutex.h> 42128059Smarkm#include <sys/poll.h> 43128059Smarkm#include <sys/proc.h> 44128059Smarkm#include <sys/random.h> 45128059Smarkm#include <sys/selinfo.h> 46128059Smarkm#include <sys/sysctl.h> 47128059Smarkm#include <sys/uio.h> 48128059Smarkm#include <sys/unistd.h> 49128059Smarkm 50128059Smarkm#include <machine/bus.h> 51128059Smarkm#include <machine/cpu.h> 52128059Smarkm 53128059Smarkm#include <dev/random/randomdev.h> 54128059Smarkm#include <dev/random/randomdev_soft.h> 55128059Smarkm 56128059Smarkm#define RANDOM_FIFO_MAX 256 /* How many events to queue up */ 57128059Smarkm 58128059Smarkmstatic void random_kthread(void *); 59128059Smarkmstatic void 60128059Smarkmrandom_harvest_internal(u_int64_t, const void *, u_int, 61128059Smarkm u_int, u_int, enum esource); 62153575Spsstatic int random_yarrow_poll(int event,struct thread *td); 63153575Spsstatic int random_yarrow_block(int flag); 64128059Smarkm 65128059Smarkmstruct random_systat random_yarrow = { 66128059Smarkm .ident = "Software, Yarrow", 67128059Smarkm .init = random_yarrow_init, 68128059Smarkm .deinit = random_yarrow_deinit, 69153575Sps .block = random_yarrow_block, 70128059Smarkm .read = random_yarrow_read, 71128059Smarkm .write = random_yarrow_write, 72153575Sps .poll = random_yarrow_poll, 73128059Smarkm .reseed = random_yarrow_reseed, 74132346Smarkm .seeded = 1, 75128059Smarkm}; 76128059Smarkm 77128059SmarkmMALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers"); 78128059Smarkm 79136338Srwatson/* 80136338Srwatson * The harvest mutex protects the consistency of the entropy fifos and 81136338Srwatson * empty fifo. 82136338Srwatson */ 83136338Srwatsonstruct mtx harvest_mtx; 84136338Srwatson 85128059Smarkm/* Lockable FIFO queue holding entropy buffers */ 86128059Smarkmstruct entropyfifo { 87128059Smarkm int count; 88128059Smarkm STAILQ_HEAD(harvestlist, harvest) head; 89128059Smarkm}; 90128059Smarkm 91128059Smarkm/* Empty entropy buffers */ 92128059Smarkmstatic struct entropyfifo emptyfifo; 93128059Smarkm 94128059Smarkm#define EMPTYBUFFERS 1024 95128059Smarkm 96128059Smarkm/* Harvested entropy */ 97144291Smarkmstatic struct entropyfifo harvestfifo[ENTROPYSOURCE]; 98128059Smarkm 99128059Smarkm/* <0 to end the kthread, 0 to let it run */ 100128059Smarkmstatic int random_kthread_control = 0; 101128059Smarkm 102128059Smarkmstatic struct proc *random_kthread_proc; 103128059Smarkm 104128059Smarkm/* List for the dynamic sysctls */ 105128059Smarkmstruct sysctl_ctx_list random_clist; 106128059Smarkm 107128059Smarkm/* ARGSUSED */ 108128059Smarkmstatic int 109128059Smarkmrandom_check_boolean(SYSCTL_HANDLER_ARGS) 110128059Smarkm{ 111128059Smarkm if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0) 112128059Smarkm *(u_int *)(oidp->oid_arg1) = 1; 113128059Smarkm return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 114128059Smarkm} 115128059Smarkm 116128059Smarkm/* ARGSUSED */ 117128059Smarkmvoid 118128059Smarkmrandom_yarrow_init(void) 119128059Smarkm{ 120128059Smarkm int error, i; 121128059Smarkm struct harvest *np; 122170067Srwatson struct sysctl_oid *random_o, *random_sys_o, *random_sys_harvest_o; 123128059Smarkm enum esource e; 124128059Smarkm 125170067Srwatson random_o = SYSCTL_ADD_NODE(&random_clist, 126128059Smarkm SYSCTL_STATIC_CHILDREN(_kern), 127128059Smarkm OID_AUTO, "random", CTLFLAG_RW, 0, 128128059Smarkm "Software Random Number Generator"); 129128059Smarkm 130128059Smarkm random_yarrow_init_alg(&random_clist, random_o); 131128059Smarkm 132170067Srwatson random_sys_o = SYSCTL_ADD_NODE(&random_clist, 133128059Smarkm SYSCTL_CHILDREN(random_o), 134128059Smarkm OID_AUTO, "sys", CTLFLAG_RW, 0, 135128059Smarkm "Entropy Device Parameters"); 136128059Smarkm 137170067Srwatson SYSCTL_ADD_PROC(&random_clist, 138128059Smarkm SYSCTL_CHILDREN(random_sys_o), 139128059Smarkm OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW, 140132346Smarkm &random_systat.seeded, 1, random_check_boolean, "I", 141128059Smarkm "Seeded State"); 142128059Smarkm 143170067Srwatson random_sys_harvest_o = SYSCTL_ADD_NODE(&random_clist, 144128059Smarkm SYSCTL_CHILDREN(random_sys_o), 145128059Smarkm OID_AUTO, "harvest", CTLFLAG_RW, 0, 146128059Smarkm "Entropy Sources"); 147128059Smarkm 148170067Srwatson SYSCTL_ADD_PROC(&random_clist, 149128059Smarkm SYSCTL_CHILDREN(random_sys_harvest_o), 150128059Smarkm OID_AUTO, "ethernet", CTLTYPE_INT | CTLFLAG_RW, 151128320Smarkm &harvest.ethernet, 1, random_check_boolean, "I", 152128059Smarkm "Harvest NIC entropy"); 153170067Srwatson SYSCTL_ADD_PROC(&random_clist, 154128059Smarkm SYSCTL_CHILDREN(random_sys_harvest_o), 155128059Smarkm OID_AUTO, "point_to_point", CTLTYPE_INT | CTLFLAG_RW, 156128320Smarkm &harvest.point_to_point, 1, random_check_boolean, "I", 157128059Smarkm "Harvest serial net entropy"); 158170067Srwatson SYSCTL_ADD_PROC(&random_clist, 159128059Smarkm SYSCTL_CHILDREN(random_sys_harvest_o), 160128059Smarkm OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW, 161128320Smarkm &harvest.interrupt, 1, random_check_boolean, "I", 162128059Smarkm "Harvest IRQ entropy"); 163170067Srwatson SYSCTL_ADD_PROC(&random_clist, 164128059Smarkm SYSCTL_CHILDREN(random_sys_harvest_o), 165128059Smarkm OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW, 166128059Smarkm &harvest.swi, 0, random_check_boolean, "I", 167128059Smarkm "Harvest SWI entropy"); 168128059Smarkm 169128059Smarkm /* Initialise the harvest fifos */ 170128059Smarkm STAILQ_INIT(&emptyfifo.head); 171128059Smarkm emptyfifo.count = 0; 172128059Smarkm for (i = 0; i < EMPTYBUFFERS; i++) { 173128059Smarkm np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK); 174128059Smarkm STAILQ_INSERT_TAIL(&emptyfifo.head, np, next); 175128059Smarkm } 176128059Smarkm for (e = RANDOM_START; e < ENTROPYSOURCE; e++) { 177128059Smarkm STAILQ_INIT(&harvestfifo[e].head); 178128059Smarkm harvestfifo[e].count = 0; 179128059Smarkm } 180128059Smarkm 181136338Srwatson mtx_init(&harvest_mtx, "entropy harvest mutex", NULL, MTX_SPIN); 182136338Srwatson 183128059Smarkm /* Start the hash/reseed thread */ 184128059Smarkm error = kthread_create(random_kthread, NULL, 185128059Smarkm &random_kthread_proc, RFHIGHPID, 0, "yarrow"); 186128059Smarkm if (error != 0) 187128059Smarkm panic("Cannot create entropy maintenance thread."); 188128059Smarkm 189128059Smarkm /* Register the randomness harvesting routine */ 190128059Smarkm random_yarrow_init_harvester(random_harvest_internal, 191128059Smarkm random_yarrow_read); 192128059Smarkm} 193128059Smarkm 194128059Smarkm/* ARGSUSED */ 195128059Smarkmvoid 196128059Smarkmrandom_yarrow_deinit(void) 197128059Smarkm{ 198128059Smarkm struct harvest *np; 199128059Smarkm enum esource e; 200128059Smarkm 201128059Smarkm /* Deregister the randomness harvesting routine */ 202128059Smarkm random_yarrow_deinit_harvester(); 203128059Smarkm 204128059Smarkm /* 205128059Smarkm * Command the hash/reseed thread to end and wait for it to finish 206128059Smarkm */ 207128059Smarkm random_kthread_control = -1; 208157815Sjhb tsleep((void *)&random_kthread_control, 0, "term", 0); 209128059Smarkm 210128059Smarkm /* Destroy the harvest fifos */ 211128059Smarkm while (!STAILQ_EMPTY(&emptyfifo.head)) { 212128059Smarkm np = STAILQ_FIRST(&emptyfifo.head); 213128059Smarkm STAILQ_REMOVE_HEAD(&emptyfifo.head, next); 214128059Smarkm free(np, M_ENTROPY); 215128059Smarkm } 216128059Smarkm for (e = RANDOM_START; e < ENTROPYSOURCE; e++) { 217128059Smarkm while (!STAILQ_EMPTY(&harvestfifo[e].head)) { 218128059Smarkm np = STAILQ_FIRST(&harvestfifo[e].head); 219128059Smarkm STAILQ_REMOVE_HEAD(&harvestfifo[e].head, next); 220128059Smarkm free(np, M_ENTROPY); 221128059Smarkm } 222128059Smarkm } 223128059Smarkm 224128059Smarkm random_yarrow_deinit_alg(); 225128059Smarkm 226136338Srwatson mtx_destroy(&harvest_mtx); 227136338Srwatson 228128059Smarkm sysctl_ctx_free(&random_clist); 229128059Smarkm} 230128059Smarkm 231128059Smarkm/* ARGSUSED */ 232128059Smarkmstatic void 233128059Smarkmrandom_kthread(void *arg __unused) 234128059Smarkm{ 235136338Srwatson STAILQ_HEAD(, harvest) local_queue; 236128059Smarkm struct harvest *event = NULL; 237137152Srwatson int active, local_count; 238128059Smarkm enum esource source; 239128059Smarkm 240136338Srwatson STAILQ_INIT(&local_queue); 241137152Srwatson local_count = 0; 242136338Srwatson 243128059Smarkm /* Process until told to stop */ 244128059Smarkm for (; random_kthread_control == 0;) { 245128059Smarkm 246128059Smarkm active = 0; 247128059Smarkm 248128059Smarkm /* Cycle through all the entropy sources */ 249136338Srwatson mtx_lock_spin(&harvest_mtx); 250128059Smarkm for (source = RANDOM_START; source < ENTROPYSOURCE; source++) { 251136338Srwatson /* 252136338Srwatson * Drain entropy source records into a thread-local 253136338Srwatson * queue for processing while not holding the mutex. 254136338Srwatson */ 255137152Srwatson STAILQ_CONCAT(&local_queue, &harvestfifo[source].head); 256137152Srwatson local_count += harvestfifo[source].count; 257137152Srwatson harvestfifo[source].count = 0; 258136338Srwatson } 259128059Smarkm 260136338Srwatson /* 261136338Srwatson * Deal with events, if any, dropping the mutex as we process 262136338Srwatson * each event. Then push the events back into the empty 263136338Srwatson * fifo. 264136338Srwatson */ 265136338Srwatson if (!STAILQ_EMPTY(&local_queue)) { 266136338Srwatson mtx_unlock_spin(&harvest_mtx); 267136338Srwatson STAILQ_FOREACH(event, &local_queue, next) 268128059Smarkm random_process_event(event); 269136338Srwatson mtx_lock_spin(&harvest_mtx); 270137152Srwatson STAILQ_CONCAT(&emptyfifo.head, &local_queue); 271137152Srwatson emptyfifo.count += local_count; 272137152Srwatson local_count = 0; 273128059Smarkm } 274136338Srwatson mtx_unlock_spin(&harvest_mtx); 275128059Smarkm 276137152Srwatson KASSERT(local_count == 0, ("random_kthread: local_count %d", 277137152Srwatson local_count)); 278137152Srwatson 279128059Smarkm /* Found nothing, so don't belabour the issue */ 280128059Smarkm if (!active) 281167086Sjhb pause("-", hz / 10); 282128059Smarkm 283128059Smarkm } 284128059Smarkm 285128059Smarkm random_set_wakeup_exit(&random_kthread_control); 286128059Smarkm /* NOTREACHED */ 287128059Smarkm} 288128059Smarkm 289128059Smarkm/* Entropy harvesting routine. This is supposed to be fast; do 290128059Smarkm * not do anything slow in here! 291128059Smarkm */ 292128059Smarkmstatic void 293128059Smarkmrandom_harvest_internal(u_int64_t somecounter, const void *entropy, 294128059Smarkm u_int count, u_int bits, u_int frac, enum esource origin) 295128059Smarkm{ 296128059Smarkm struct harvest *event; 297128059Smarkm 298136434Srwatson KASSERT(origin == RANDOM_START || origin == RANDOM_WRITE || 299136434Srwatson origin == RANDOM_KEYBOARD || origin == RANDOM_MOUSE || 300136434Srwatson origin == RANDOM_NET || origin == RANDOM_INTERRUPT || 301144291Smarkm origin == RANDOM_PURE, 302136434Srwatson ("random_harvest_internal: origin %d invalid\n", origin)); 303136434Srwatson 304133465Srwatson /* Lockless read to avoid lock operations if fifo is full. */ 305133465Srwatson if (harvestfifo[origin].count >= RANDOM_FIFO_MAX) 306133465Srwatson return; 307133465Srwatson 308136338Srwatson mtx_lock_spin(&harvest_mtx); 309128059Smarkm 310128059Smarkm /* 311128059Smarkm * Don't make the harvest queues too big - help to prevent low-grade 312128059Smarkm * entropy swamping 313128059Smarkm */ 314128059Smarkm if (harvestfifo[origin].count < RANDOM_FIFO_MAX) { 315136338Srwatson event = STAILQ_FIRST(&emptyfifo.head); 316136338Srwatson if (event != NULL) { 317136338Srwatson /* Add the harvested data to the fifo */ 318128059Smarkm STAILQ_REMOVE_HEAD(&emptyfifo.head, next); 319128059Smarkm harvestfifo[origin].count++; 320128059Smarkm event->somecounter = somecounter; 321128059Smarkm event->size = count; 322128059Smarkm event->bits = bits; 323128059Smarkm event->frac = frac; 324128059Smarkm event->source = origin; 325128059Smarkm 326128059Smarkm /* XXXX Come back and make this dynamic! */ 327128059Smarkm count = MIN(count, HARVESTSIZE); 328128059Smarkm memcpy(event->entropy, entropy, count); 329128059Smarkm 330128059Smarkm STAILQ_INSERT_TAIL(&harvestfifo[origin].head, 331128059Smarkm event, next); 332128059Smarkm } 333128059Smarkm } 334136338Srwatson mtx_unlock_spin(&harvest_mtx); 335128059Smarkm} 336128059Smarkm 337128059Smarkmvoid 338128059Smarkmrandom_yarrow_write(void *buf, int count) 339128059Smarkm{ 340128059Smarkm int i; 341128059Smarkm u_int chunk; 342128059Smarkm 343128059Smarkm /* 344128059Smarkm * Break the input up into HARVESTSIZE chunks. The writer has too 345128059Smarkm * much control here, so "estimate" the the entropy as zero. 346128059Smarkm */ 347128059Smarkm for (i = 0; i < count; i += HARVESTSIZE) { 348128059Smarkm chunk = HARVESTSIZE; 349128059Smarkm if (i + chunk >= count) 350128059Smarkm chunk = (u_int)(count - i); 351128059Smarkm random_harvest_internal(get_cyclecount(), (char *)buf + i, 352128059Smarkm chunk, 0, 0, RANDOM_WRITE); 353128059Smarkm } 354128059Smarkm} 355128059Smarkm 356128059Smarkmvoid 357128059Smarkmrandom_yarrow_unblock(void) 358128059Smarkm{ 359128059Smarkm if (!random_systat.seeded) { 360128059Smarkm random_systat.seeded = 1; 361128059Smarkm selwakeuppri(&random_systat.rsel, PUSER); 362128059Smarkm wakeup(&random_systat); 363128059Smarkm } 364128059Smarkm} 365153575Sps 366153575Spsstatic int 367153575Spsrandom_yarrow_poll(int events, struct thread *td) 368153575Sps{ 369153575Sps int revents = 0; 370153575Sps mtx_lock(&random_reseed_mtx); 371153575Sps 372153575Sps if (random_systat.seeded) 373153575Sps revents = events & (POLLIN | POLLRDNORM); 374153575Sps else 375153575Sps selrecord(td, &random_systat.rsel); 376153575Sps 377153575Sps mtx_unlock(&random_reseed_mtx); 378153575Sps return revents; 379153575Sps} 380153575Sps 381153575Spsstatic int 382153575Spsrandom_yarrow_block(int flag) 383153575Sps{ 384153575Sps int error = 0; 385153575Sps 386153575Sps mtx_lock(&random_reseed_mtx); 387153575Sps 388153575Sps /* Blocking logic */ 389153575Sps while (random_systat.seeded && !error) { 390153575Sps if (flag & O_NONBLOCK) 391153575Sps error = EWOULDBLOCK; 392153575Sps else { 393153575Sps printf("Entropy device is blocking.\n"); 394153575Sps error = msleep(&random_systat, 395153575Sps &random_reseed_mtx, 396153575Sps PUSER | PCATCH, "block", 0); 397153575Sps } 398153575Sps } 399153575Sps mtx_unlock(&random_reseed_mtx); 400153575Sps 401153575Sps return error; 402153575Sps} 403