randomdev.c revision 62765
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: head/sys/dev/random/randomdev.c 62765 2000-07-07 09:03:59Z markm $ 27 */ 28 29#include <sys/param.h> 30#include <sys/queue.h> 31#include <sys/systm.h> 32#include <sys/conf.h> 33#include <sys/fcntl.h> 34#include <sys/uio.h> 35#include <sys/kernel.h> 36#include <sys/malloc.h> 37#include <sys/module.h> 38#include <sys/bus.h> 39#include <sys/random.h> 40#include <machine/bus.h> 41#include <machine/resource.h> 42#include <sys/rman.h> 43#include <sys/signalvar.h> 44#include <sys/sysctl.h> 45#include <crypto/blowfish/blowfish.h> 46 47#include <dev/randomdev/yarrow.h> 48 49static d_read_t random_read; 50static d_write_t random_write; 51 52#define CDEV_MAJOR 2 53#define RANDOM_MINOR 3 54#define URANDOM_MINOR 4 55 56static struct cdevsw random_cdevsw = { 57 /* open */ (d_open_t *)nullop, 58 /* close */ (d_close_t *)nullop, 59 /* read */ random_read, 60 /* write */ random_write, 61 /* ioctl */ noioctl, 62 /* poll */ nopoll, 63 /* mmap */ nommap, 64 /* strategy */ nostrategy, 65 /* name */ "random", 66 /* maj */ CDEV_MAJOR, 67 /* dump */ nodump, 68 /* psize */ nopsize, 69 /* flags */ 0, 70 /* bmaj */ -1 71}; 72 73/* For use with make_dev(9)/destroy_dev(9). */ 74static dev_t random_dev; 75static dev_t urandom_dev; 76 77/* Buffer used by uiomove(9) */ 78static void *random_buf; 79 80SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, "Random Number Generator"); 81SYSCTL_NODE(_kern_random, OID_AUTO, yarrow, CTLFLAG_RW, 0, "Yarrow Parameters"); 82SYSCTL_INT(_kern_random_yarrow, OID_AUTO, gengateinterval, CTLFLAG_RW, &random_state.gengateinterval, 10, "Generator Gate Interval"); 83SYSCTL_INT(_kern_random_yarrow, OID_AUTO, bins, CTLFLAG_RW, &random_state.bins, 10, "Execution time tuner"); 84SYSCTL_INT(_kern_random_yarrow, OID_AUTO, fastthresh, CTLFLAG_RW, &random_state.pool[0].thresh, 100, "Fast pool reseed threshhold"); 85SYSCTL_INT(_kern_random_yarrow, OID_AUTO, slowthresh, CTLFLAG_RW, &random_state.pool[1].thresh, 100, "Slow pool reseed threshhold"); 86SYSCTL_INT(_kern_random_yarrow, OID_AUTO, slowoverthresh, CTLFLAG_RW, &random_state.slowoverthresh, 2, "Slow pool over-threshhold reseed count"); 87 88static int 89random_read(dev_t dev, struct uio *uio, int flag) 90{ 91 u_int c, ret; 92 int error = 0; 93 94 c = min(uio->uio_resid, PAGE_SIZE); 95 random_buf = (void *)malloc(c, M_TEMP, M_WAITOK); 96 while (uio->uio_resid > 0 && error == 0) { 97 ret = read_random(random_buf, c); 98 error = uiomove(random_buf, ret, uio); 99 } 100 free(random_buf, M_TEMP); 101 return error; 102} 103 104static int 105random_write(dev_t dev, struct uio *uio, int flag) 106{ 107 u_int c; 108 int error = 0; 109 110 random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK); 111 while (uio->uio_resid > 0) { 112 c = min(uio->uio_resid, PAGE_SIZE); 113 error = uiomove(random_buf, c, uio); 114 if (error) 115 break; 116 /* write_random(random_buf, c); */ 117 } 118 free(random_buf, M_TEMP); 119 return error; 120} 121 122static int 123random_modevent(module_t mod, int type, void *data) 124{ 125 switch(type) { 126 case MOD_LOAD: 127 if (bootverbose) 128 printf("random: <entropy source>\n"); 129 random_dev = make_dev(&random_cdevsw, RANDOM_MINOR, UID_ROOT, 130 GID_WHEEL, 0666, "random"); 131 urandom_dev = make_dev(&random_cdevsw, URANDOM_MINOR, UID_ROOT, 132 GID_WHEEL, 0666, "urandom"); 133 random_init(); 134 return 0; 135 136 case MOD_UNLOAD: 137 random_deinit(); 138 destroy_dev(random_dev); 139 destroy_dev(urandom_dev); 140 return 0; 141 142 case MOD_SHUTDOWN: 143 return 0; 144 145 default: 146 return EOPNOTSUPP; 147 } 148} 149 150DEV_MODULE(random, random_modevent, NULL); 151