randomdev_soft.c revision 157815
1/*- 2 * Copyright (c) 2000-2004 Mark R V Murray 3 * Copyright (c) 2004 Robert N. M. Watson 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/sys/dev/random/randomdev_soft.c 157815 2006-04-17 18:20:38Z jhb $"); 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/bus.h> 35#include <sys/conf.h> 36#include <sys/fcntl.h> 37#include <sys/kernel.h> 38#include <sys/kthread.h> 39#include <sys/lock.h> 40#include <sys/malloc.h> 41#include <sys/mutex.h> 42#include <sys/poll.h> 43#include <sys/proc.h> 44#include <sys/random.h> 45#include <sys/selinfo.h> 46#include <sys/sysctl.h> 47#include <sys/uio.h> 48#include <sys/unistd.h> 49 50#include <machine/bus.h> 51#include <machine/cpu.h> 52 53#include <dev/random/randomdev.h> 54#include <dev/random/randomdev_soft.h> 55 56#define RANDOM_FIFO_MAX 256 /* How many events to queue up */ 57 58static void random_kthread(void *); 59static void 60random_harvest_internal(u_int64_t, const void *, u_int, 61 u_int, u_int, enum esource); 62static int random_yarrow_poll(int event,struct thread *td); 63static int random_yarrow_block(int flag); 64 65struct random_systat random_yarrow = { 66 .ident = "Software, Yarrow", 67 .init = random_yarrow_init, 68 .deinit = random_yarrow_deinit, 69 .block = random_yarrow_block, 70 .read = random_yarrow_read, 71 .write = random_yarrow_write, 72 .poll = random_yarrow_poll, 73 .reseed = random_yarrow_reseed, 74 .seeded = 1, 75}; 76 77MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers"); 78 79/* 80 * The harvest mutex protects the consistency of the entropy fifos and 81 * empty fifo. 82 */ 83struct mtx harvest_mtx; 84 85/* Lockable FIFO queue holding entropy buffers */ 86struct entropyfifo { 87 int count; 88 STAILQ_HEAD(harvestlist, harvest) head; 89}; 90 91/* Empty entropy buffers */ 92static struct entropyfifo emptyfifo; 93 94#define EMPTYBUFFERS 1024 95 96/* Harvested entropy */ 97static struct entropyfifo harvestfifo[ENTROPYSOURCE]; 98 99/* <0 to end the kthread, 0 to let it run */ 100static int random_kthread_control = 0; 101 102static struct proc *random_kthread_proc; 103 104/* List for the dynamic sysctls */ 105struct sysctl_ctx_list random_clist; 106 107/* ARGSUSED */ 108static int 109random_check_boolean(SYSCTL_HANDLER_ARGS) 110{ 111 if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0) 112 *(u_int *)(oidp->oid_arg1) = 1; 113 return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 114} 115 116/* ARGSUSED */ 117void 118random_yarrow_init(void) 119{ 120 int error, i; 121 struct harvest *np; 122 struct sysctl_oid *o, *random_o, *random_sys_o, *random_sys_harvest_o; 123 enum esource e; 124 125 o = SYSCTL_ADD_NODE(&random_clist, 126 SYSCTL_STATIC_CHILDREN(_kern), 127 OID_AUTO, "random", CTLFLAG_RW, 0, 128 "Software Random Number Generator"); 129 130 random_o = o; 131 132 random_yarrow_init_alg(&random_clist, random_o); 133 134 o = SYSCTL_ADD_NODE(&random_clist, 135 SYSCTL_CHILDREN(random_o), 136 OID_AUTO, "sys", CTLFLAG_RW, 0, 137 "Entropy Device Parameters"); 138 139 random_sys_o = o; 140 141 o = SYSCTL_ADD_PROC(&random_clist, 142 SYSCTL_CHILDREN(random_sys_o), 143 OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW, 144 &random_systat.seeded, 1, random_check_boolean, "I", 145 "Seeded State"); 146 147 o = SYSCTL_ADD_NODE(&random_clist, 148 SYSCTL_CHILDREN(random_sys_o), 149 OID_AUTO, "harvest", CTLFLAG_RW, 0, 150 "Entropy Sources"); 151 152 random_sys_harvest_o = o; 153 154 o = SYSCTL_ADD_PROC(&random_clist, 155 SYSCTL_CHILDREN(random_sys_harvest_o), 156 OID_AUTO, "ethernet", CTLTYPE_INT | CTLFLAG_RW, 157 &harvest.ethernet, 1, random_check_boolean, "I", 158 "Harvest NIC entropy"); 159 o = SYSCTL_ADD_PROC(&random_clist, 160 SYSCTL_CHILDREN(random_sys_harvest_o), 161 OID_AUTO, "point_to_point", CTLTYPE_INT | CTLFLAG_RW, 162 &harvest.point_to_point, 1, random_check_boolean, "I", 163 "Harvest serial net entropy"); 164 o = SYSCTL_ADD_PROC(&random_clist, 165 SYSCTL_CHILDREN(random_sys_harvest_o), 166 OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW, 167 &harvest.interrupt, 1, random_check_boolean, "I", 168 "Harvest IRQ entropy"); 169 o = SYSCTL_ADD_PROC(&random_clist, 170 SYSCTL_CHILDREN(random_sys_harvest_o), 171 OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW, 172 &harvest.swi, 0, random_check_boolean, "I", 173 "Harvest SWI entropy"); 174 175 /* Initialise the harvest fifos */ 176 STAILQ_INIT(&emptyfifo.head); 177 emptyfifo.count = 0; 178 for (i = 0; i < EMPTYBUFFERS; i++) { 179 np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK); 180 STAILQ_INSERT_TAIL(&emptyfifo.head, np, next); 181 } 182 for (e = RANDOM_START; e < ENTROPYSOURCE; e++) { 183 STAILQ_INIT(&harvestfifo[e].head); 184 harvestfifo[e].count = 0; 185 } 186 187 mtx_init(&harvest_mtx, "entropy harvest mutex", NULL, MTX_SPIN); 188 189 /* Start the hash/reseed thread */ 190 error = kthread_create(random_kthread, NULL, 191 &random_kthread_proc, RFHIGHPID, 0, "yarrow"); 192 if (error != 0) 193 panic("Cannot create entropy maintenance thread."); 194 195 /* Register the randomness harvesting routine */ 196 random_yarrow_init_harvester(random_harvest_internal, 197 random_yarrow_read); 198} 199 200/* ARGSUSED */ 201void 202random_yarrow_deinit(void) 203{ 204 struct harvest *np; 205 enum esource e; 206 207 /* Deregister the randomness harvesting routine */ 208 random_yarrow_deinit_harvester(); 209 210 /* 211 * Command the hash/reseed thread to end and wait for it to finish 212 */ 213 random_kthread_control = -1; 214 tsleep((void *)&random_kthread_control, 0, "term", 0); 215 216 /* Destroy the harvest fifos */ 217 while (!STAILQ_EMPTY(&emptyfifo.head)) { 218 np = STAILQ_FIRST(&emptyfifo.head); 219 STAILQ_REMOVE_HEAD(&emptyfifo.head, next); 220 free(np, M_ENTROPY); 221 } 222 for (e = RANDOM_START; e < ENTROPYSOURCE; e++) { 223 while (!STAILQ_EMPTY(&harvestfifo[e].head)) { 224 np = STAILQ_FIRST(&harvestfifo[e].head); 225 STAILQ_REMOVE_HEAD(&harvestfifo[e].head, next); 226 free(np, M_ENTROPY); 227 } 228 } 229 230 random_yarrow_deinit_alg(); 231 232 mtx_destroy(&harvest_mtx); 233 234 sysctl_ctx_free(&random_clist); 235} 236 237/* ARGSUSED */ 238static void 239random_kthread(void *arg __unused) 240{ 241 STAILQ_HEAD(, harvest) local_queue; 242 struct harvest *event = NULL; 243 int active, local_count; 244 enum esource source; 245 246 STAILQ_INIT(&local_queue); 247 local_count = 0; 248 249 /* Process until told to stop */ 250 for (; random_kthread_control == 0;) { 251 252 active = 0; 253 254 /* Cycle through all the entropy sources */ 255 mtx_lock_spin(&harvest_mtx); 256 for (source = RANDOM_START; source < ENTROPYSOURCE; source++) { 257 /* 258 * Drain entropy source records into a thread-local 259 * queue for processing while not holding the mutex. 260 */ 261 STAILQ_CONCAT(&local_queue, &harvestfifo[source].head); 262 local_count += harvestfifo[source].count; 263 harvestfifo[source].count = 0; 264 } 265 266 /* 267 * Deal with events, if any, dropping the mutex as we process 268 * each event. Then push the events back into the empty 269 * fifo. 270 */ 271 if (!STAILQ_EMPTY(&local_queue)) { 272 mtx_unlock_spin(&harvest_mtx); 273 STAILQ_FOREACH(event, &local_queue, next) 274 random_process_event(event); 275 mtx_lock_spin(&harvest_mtx); 276 STAILQ_CONCAT(&emptyfifo.head, &local_queue); 277 emptyfifo.count += local_count; 278 local_count = 0; 279 } 280 mtx_unlock_spin(&harvest_mtx); 281 282 KASSERT(local_count == 0, ("random_kthread: local_count %d", 283 local_count)); 284 285 /* Found nothing, so don't belabour the issue */ 286 if (!active) 287 tsleep(&harvestfifo, 0, "-", hz / 10); 288 289 } 290 291 random_set_wakeup_exit(&random_kthread_control); 292 /* NOTREACHED */ 293} 294 295/* Entropy harvesting routine. This is supposed to be fast; do 296 * not do anything slow in here! 297 */ 298static void 299random_harvest_internal(u_int64_t somecounter, const void *entropy, 300 u_int count, u_int bits, u_int frac, enum esource origin) 301{ 302 struct harvest *event; 303 304 KASSERT(origin == RANDOM_START || origin == RANDOM_WRITE || 305 origin == RANDOM_KEYBOARD || origin == RANDOM_MOUSE || 306 origin == RANDOM_NET || origin == RANDOM_INTERRUPT || 307 origin == RANDOM_PURE, 308 ("random_harvest_internal: origin %d invalid\n", origin)); 309 310 /* Lockless read to avoid lock operations if fifo is full. */ 311 if (harvestfifo[origin].count >= RANDOM_FIFO_MAX) 312 return; 313 314 mtx_lock_spin(&harvest_mtx); 315 316 /* 317 * Don't make the harvest queues too big - help to prevent low-grade 318 * entropy swamping 319 */ 320 if (harvestfifo[origin].count < RANDOM_FIFO_MAX) { 321 event = STAILQ_FIRST(&emptyfifo.head); 322 if (event != NULL) { 323 /* Add the harvested data to the fifo */ 324 STAILQ_REMOVE_HEAD(&emptyfifo.head, next); 325 harvestfifo[origin].count++; 326 event->somecounter = somecounter; 327 event->size = count; 328 event->bits = bits; 329 event->frac = frac; 330 event->source = origin; 331 332 /* XXXX Come back and make this dynamic! */ 333 count = MIN(count, HARVESTSIZE); 334 memcpy(event->entropy, entropy, count); 335 336 STAILQ_INSERT_TAIL(&harvestfifo[origin].head, 337 event, next); 338 } 339 } 340 mtx_unlock_spin(&harvest_mtx); 341} 342 343void 344random_yarrow_write(void *buf, int count) 345{ 346 int i; 347 u_int chunk; 348 349 /* 350 * Break the input up into HARVESTSIZE chunks. The writer has too 351 * much control here, so "estimate" the the entropy as zero. 352 */ 353 for (i = 0; i < count; i += HARVESTSIZE) { 354 chunk = HARVESTSIZE; 355 if (i + chunk >= count) 356 chunk = (u_int)(count - i); 357 random_harvest_internal(get_cyclecount(), (char *)buf + i, 358 chunk, 0, 0, RANDOM_WRITE); 359 } 360} 361 362void 363random_yarrow_unblock(void) 364{ 365 if (!random_systat.seeded) { 366 random_systat.seeded = 1; 367 selwakeuppri(&random_systat.rsel, PUSER); 368 wakeup(&random_systat); 369 } 370} 371 372static int 373random_yarrow_poll(int events, struct thread *td) 374{ 375 int revents = 0; 376 mtx_lock(&random_reseed_mtx); 377 378 if (random_systat.seeded) 379 revents = events & (POLLIN | POLLRDNORM); 380 else 381 selrecord(td, &random_systat.rsel); 382 383 mtx_unlock(&random_reseed_mtx); 384 return revents; 385} 386 387static int 388random_yarrow_block(int flag) 389{ 390 int error = 0; 391 392 mtx_lock(&random_reseed_mtx); 393 394 /* Blocking logic */ 395 while (random_systat.seeded && !error) { 396 if (flag & O_NONBLOCK) 397 error = EWOULDBLOCK; 398 else { 399 printf("Entropy device is blocking.\n"); 400 error = msleep(&random_systat, 401 &random_reseed_mtx, 402 PUSER | PCATCH, "block", 0); 403 } 404 } 405 mtx_unlock(&random_reseed_mtx); 406 407 return error; 408} 409