1 /*- 2 * Copyright (c) 2000 Mark R V Murray 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bus.h> 32 #include <sys/conf.h> 33 #include <sys/fcntl.h> 34 #include <sys/filio.h> 35 #include <sys/kernel.h> 36 #include <sys/kthread.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/mutex.h> 41 #include <sys/poll.h> 42 #include <sys/proc.h> 43 #include <sys/queue.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 #include <sys/vnode.h> 50 51 #include <machine/bus.h> 52 #include <machine/cpu.h> 53 #include <machine/resource.h> 54 55 #include <dev/random/randomdev.h> 56 57 static d_open_t random_open; 58 static d_close_t random_close; 59 static d_read_t random_read; 60 static d_write_t random_write; 61 static d_ioctl_t random_ioctl; 62 static d_poll_t random_poll; 63 64 #define CDEV_MAJOR 2 65 #define RANDOM_MINOR 3 66 67 static struct cdevsw random_cdevsw = { 68 /* open */ random_open, 69 /* close */ random_close, 70 /* read */ random_read, 71 /* write */ random_write, 72 /* ioctl */ random_ioctl, 73 /* poll */ random_poll, 74 /* mmap */ nommap, 75 /* strategy */ nostrategy, 76 /* name */ "random", 77 /* maj */ CDEV_MAJOR, 78 /* dump */ nodump, 79 /* psize */ nopsize, 80 /* flags */ 0, 81 }; 82 83 static void random_kthread(void *); 84 static void random_harvest_internal(u_int64_t, void *, u_int, u_int, u_int, enum esource); 85 static void random_write_internal(void *, u_int); 86 87 /* Ring buffer holding harvested entropy */ 88 static struct harvestring { 89 volatile u_int head; 90 volatile u_int tail; 91 struct harvest data[HARVEST_RING_SIZE]; 92 } harvestring; 93 94 static struct random_systat { 95 u_int seeded; /* 0 causes blocking 1 allows normal output */ 96 u_int burst; /* number of events to do before sleeping */ 97 struct selinfo rsel; /* For poll(2) */ 98 } random_systat; 99 100 /* <0 to end the kthread, 0 to let it run */ 101 static int random_kthread_control = 0; 102 103 static struct proc *random_kthread_proc; 104 105 /* For use with make_dev(9)/destroy_dev(9). */ 106 static dev_t random_dev; 107 static dev_t urandom_dev; 108 109 static int 110 random_check_boolean(SYSCTL_HANDLER_ARGS) 111 { 112 if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0) 113 *(u_int *)(oidp->oid_arg1) = 1; 114 return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 115 } 116 117 RANDOM_CHECK_UINT(burst, 0, 20); 118 119 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 120 0, "Random Number Generator"); 121 SYSCTL_NODE(_kern_random, OID_AUTO, sys, CTLFLAG_RW, 122 0, "Entropy Device Parameters"); 123 SYSCTL_PROC(_kern_random_sys, OID_AUTO, seeded, 124 CTLTYPE_INT|CTLFLAG_RW, &random_systat.seeded, 1, 125 random_check_boolean, "I", "Seeded State"); 126 SYSCTL_PROC(_kern_random_sys, OID_AUTO, burst, 127 CTLTYPE_INT|CTLFLAG_RW, &random_systat.burst, 20, 128 random_check_uint_burst, "I", "Harvest Burst Size"); 129 SYSCTL_NODE(_kern_random_sys, OID_AUTO, harvest, CTLFLAG_RW, 130 0, "Entropy Sources"); 131 SYSCTL_PROC(_kern_random_sys_harvest, OID_AUTO, ethernet, 132 CTLTYPE_INT|CTLFLAG_RW, &harvest.ethernet, 0, 133 random_check_boolean, "I", "Harvest NIC entropy"); 134 SYSCTL_PROC(_kern_random_sys_harvest, OID_AUTO, point_to_point, 135 CTLTYPE_INT|CTLFLAG_RW, &harvest.point_to_point, 0, 136 random_check_boolean, "I", "Harvest serial net entropy"); 137 SYSCTL_PROC(_kern_random_sys_harvest, OID_AUTO, interrupt, 138 CTLTYPE_INT|CTLFLAG_RW, &harvest.interrupt, 0, 139 random_check_boolean, "I", "Harvest IRQ entropy"); 140 141 static int 142 random_open(dev_t dev, int flags, int fmt, struct thread *td) 143 { 144 int error; 145 146 if (flags & FWRITE) { 147 error = suser(td->td_proc); 148 if (error) 149 return (error); 150 error = securelevel_gt(td->td_proc->p_ucred, 0); 151 if (error) 152 return (error); 153 } 154 return 0; 155 } 156 157 static int 158 random_close(dev_t dev, int flags, int fmt, struct thread *td) 159 { 160 if (flags & FWRITE) { 161 if (!(suser(td->td_proc) || 162 securelevel_gt(td->td_proc->p_ucred, 0))) 163 random_reseed(); 164 } 165 return 0; 166 } 167 168 static int 169 random_read(dev_t dev, struct uio *uio, int flag) 170 { 171 u_int c, ret; 172 int error = 0; 173 void *random_buf; 174 175 while (!random_systat.seeded) { 176 if (flag & IO_NDELAY) 177 error = EWOULDBLOCK; 178 else 179 error = tsleep(&random_systat, PUSER|PCATCH, 180 "block", 0); 181 if (error != 0) 182 return error; 183 } 184 c = min(uio->uio_resid, PAGE_SIZE); 185 random_buf = (void *)malloc(c, M_TEMP, M_WAITOK); 186 while (uio->uio_resid > 0 && error == 0) { 187 ret = read_random_real(random_buf, c); 188 error = uiomove(random_buf, ret, uio); 189 } 190 free(random_buf, M_TEMP); 191 return error; 192 } 193 194 static int 195 random_write(dev_t dev, struct uio *uio, int flag) 196 { 197 u_int c; 198 int error; 199 void *random_buf; 200 201 error = 0; 202 random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK); 203 while (uio->uio_resid > 0) { 204 c = min(uio->uio_resid, PAGE_SIZE); 205 error = uiomove(random_buf, c, uio); 206 if (error) 207 break; 208 random_write_internal(random_buf, c); 209 } 210 free(random_buf, M_TEMP); 211 return error; 212 } 213 214 static int 215 random_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 216 { 217 switch (cmd) { 218 /* Really handled in upper layer */ 219 case FIOASYNC: 220 case FIONBIO: 221 return 0; 222 default: 223 return ENOTTY; 224 } 225 } 226 227 static int 228 random_poll(dev_t dev, int events, struct thread *td) 229 { 230 int revents; 231 232 revents = 0; 233 if (events & (POLLIN | POLLRDNORM)) { 234 if (random_systat.seeded) 235 revents = events & (POLLIN | POLLRDNORM); 236 else 237 selrecord(td, &random_systat.rsel); 238 } 239 return revents; 240 } 241 242 static int 243 random_modevent(module_t mod, int type, void *data) 244 { 245 int error; 246 247 switch(type) { 248 case MOD_LOAD: 249 random_init(); 250 251 /* This can be turned off by the very paranoid 252 * a reseed will turn it back on. 253 */ 254 random_systat.seeded = 1; 255 256 /* Number of envents to process off the harvest 257 * queue before giving it a break and sleeping 258 */ 259 random_systat.burst = 20; 260 261 /* Initialise the harvest ringbuffer */ 262 harvestring.head = 0; 263 harvestring.tail = 0; 264 265 if (bootverbose) 266 printf("random: <entropy source>\n"); 267 random_dev = make_dev(&random_cdevsw, RANDOM_MINOR, UID_ROOT, 268 GID_WHEEL, 0666, "random"); 269 urandom_dev = make_dev_alias(random_dev, "urandom"); 270 271 /* Start the hash/reseed thread */ 272 error = kthread_create(random_kthread, NULL, 273 &random_kthread_proc, RFHIGHPID, "random"); 274 if (error != 0) 275 return error; 276 277 /* Register the randomness harvesting routine */ 278 random_init_harvester(random_harvest_internal, 279 read_random_real); 280 281 return 0; 282 283 case MOD_UNLOAD: 284 /* Deregister the randomness harvesting routine */ 285 random_deinit_harvester(); 286 287 /* Command the hash/reseed thread to end and 288 * wait for it to finish 289 */ 290 random_kthread_control = -1; 291 tsleep((void *)&random_kthread_control, PUSER, "term", 0); 292 293 random_deinit(); 294 295 destroy_dev(random_dev); 296 destroy_dev(urandom_dev); 297 return 0; 298 299 case MOD_SHUTDOWN: 300 return 0; 301 302 default: 303 return EOPNOTSUPP; 304 } 305 } 306 307 DEV_MODULE(random, random_modevent, NULL); 308 309 static void 310 random_kthread(void *arg /* NOTUSED */) 311 { 312 struct harvest *event; 313 int newtail, burst; 314 315 /* Drain the harvest queue (in 'burst' size chunks, 316 * if 'burst' > 0. If 'burst' == 0, then completely 317 * drain the queue. 318 */ 319 for (burst = 0; ; burst++) { 320 321 if ((harvestring.tail == harvestring.head) || 322 (random_systat.burst && burst == random_systat.burst)) { 323 tsleep(&harvestring, PUSER, "sleep", hz/10); 324 burst = 0; 325 326 } 327 else { 328 329 /* Suck a harvested entropy event out of the queue and 330 * hand it to the event processor 331 */ 332 333 newtail = (harvestring.tail + 1) & HARVEST_RING_MASK; 334 event = &harvestring.data[harvestring.tail]; 335 336 /* Bump the ring counter. This action is assumed 337 * to be atomic. 338 */ 339 harvestring.tail = newtail; 340 341 random_process_event(event); 342 343 } 344 345 /* Is the thread scheduled for a shutdown? */ 346 if (random_kthread_control != 0) { 347 #ifdef DEBUG 348 mtx_lock(&Giant); 349 printf("Random kthread setting terminate\n"); 350 mtx_unlock(&Giant); 351 #endif 352 random_set_wakeup_exit(&random_kthread_control); 353 /* NOTREACHED */ 354 break; 355 } 356 357 } 358 359 } 360 361 /* Entropy harvesting routine. This is supposed to be fast; do 362 * not do anything slow in here! 363 */ 364 static void 365 random_harvest_internal(u_int64_t somecounter, void *entropy, u_int count, 366 u_int bits, u_int frac, enum esource origin) 367 { 368 struct harvest *harvest; 369 int newhead; 370 371 newhead = (harvestring.head + 1) & HARVEST_RING_MASK; 372 373 if (newhead != harvestring.tail) { 374 375 /* Add the harvested data to the ring buffer */ 376 377 harvest = &harvestring.data[harvestring.head]; 378 379 /* Stuff the harvested data into the ring */ 380 harvest->somecounter = somecounter; 381 count = count > HARVESTSIZE ? HARVESTSIZE : count; 382 memcpy(harvest->entropy, entropy, count); 383 harvest->size = count; 384 harvest->bits = bits; 385 harvest->frac = frac; 386 harvest->source = origin < ENTROPYSOURCE ? origin : 0; 387 388 /* Bump the ring counter. This action is assumed 389 * to be atomic. 390 */ 391 harvestring.head = newhead; 392 393 } 394 395 } 396 397 static void 398 random_write_internal(void *buf, u_int count) 399 { 400 u_int i; 401 402 /* Break the input up into HARVESTSIZE chunks. 403 * The writer has too much control here, so "estimate" the 404 * the entropy as zero. 405 */ 406 for (i = 0; i < count; i += HARVESTSIZE) { 407 random_harvest_internal(get_cyclecount(), (char *)buf + i, 408 HARVESTSIZE, 0, 0, RANDOM_WRITE); 409 } 410 411 /* Maybe the loop iterated at least once */ 412 if (i > count) 413 i -= HARVESTSIZE; 414 415 /* Get the last bytes even if the input length is not 416 * a multiple of HARVESTSIZE. 417 */ 418 count %= HARVESTSIZE; 419 if (count) { 420 random_harvest_internal(get_cyclecount(), (char *)buf + i, 421 count, 0, 0, RANDOM_WRITE); 422 } 423 } 424 425 void 426 random_unblock(void) 427 { 428 if (!random_systat.seeded) { 429 random_systat.seeded = 1; 430 selwakeup(&random_systat.rsel); 431 wakeup(&random_systat); 432 } 433 } 434