2000-06-25 08:38:58 +00:00
|
|
|
/*-
|
2000-07-07 09:03:59 +00:00
|
|
|
* Copyright (c) 2000 Mark R V Murray
|
2000-06-25 08:38:58 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2000-07-07 09:03:59 +00:00
|
|
|
#include <sys/queue.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/bus.h>
|
2000-10-14 10:59:56 +00:00
|
|
|
#include <sys/poll.h>
|
2001-01-09 04:33:49 +00:00
|
|
|
#include <sys/selinfo.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
#include <sys/random.h>
|
2001-01-14 17:50:15 +00:00
|
|
|
#include <sys/sysctl.h>
|
2000-10-14 10:59:56 +00:00
|
|
|
#include <sys/vnode.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/resource.h>
|
|
|
|
#include <crypto/blowfish/blowfish.h>
|
|
|
|
|
2000-10-14 10:59:56 +00:00
|
|
|
#include <dev/random/hash.h>
|
|
|
|
#include <dev/random/yarrow.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-07-25 21:18:47 +00:00
|
|
|
static d_open_t random_open;
|
2000-11-25 19:13:29 +00:00
|
|
|
static d_close_t random_close;
|
2000-07-07 09:03:59 +00:00
|
|
|
static d_read_t random_read;
|
|
|
|
static d_write_t random_write;
|
2000-09-10 13:52:19 +00:00
|
|
|
static d_ioctl_t random_ioctl;
|
2000-10-14 10:59:56 +00:00
|
|
|
static d_poll_t random_poll;
|
2000-06-25 08:38:58 +00:00
|
|
|
|
|
|
|
#define CDEV_MAJOR 2
|
|
|
|
#define RANDOM_MINOR 3
|
2000-06-27 09:38:40 +00:00
|
|
|
#define URANDOM_MINOR 4
|
2000-06-25 08:38:58 +00:00
|
|
|
|
|
|
|
static struct cdevsw random_cdevsw = {
|
2000-07-25 21:18:47 +00:00
|
|
|
/* open */ random_open,
|
2000-11-25 19:13:29 +00:00
|
|
|
/* close */ random_close,
|
2000-07-07 09:03:59 +00:00
|
|
|
/* read */ random_read,
|
|
|
|
/* write */ random_write,
|
2000-09-10 13:52:19 +00:00
|
|
|
/* ioctl */ random_ioctl,
|
2000-10-14 10:59:56 +00:00
|
|
|
/* poll */ random_poll,
|
2000-06-25 08:38:58 +00:00
|
|
|
/* mmap */ nommap,
|
|
|
|
/* strategy */ nostrategy,
|
|
|
|
/* name */ "random",
|
|
|
|
/* maj */ CDEV_MAJOR,
|
|
|
|
/* dump */ nodump,
|
|
|
|
/* psize */ nopsize,
|
|
|
|
/* flags */ 0,
|
|
|
|
/* bmaj */ -1
|
|
|
|
};
|
|
|
|
|
|
|
|
/* For use with make_dev(9)/destroy_dev(9). */
|
2000-07-07 09:03:59 +00:00
|
|
|
static dev_t random_dev;
|
2000-09-10 13:52:19 +00:00
|
|
|
static dev_t urandom_dev; /* XXX Temporary */
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2001-01-14 17:50:15 +00:00
|
|
|
/* To stash the sysctl's until they are removed */
|
|
|
|
static struct sysctl_oid *random_sysctl[10]; /* magic # is sysctl count */
|
|
|
|
static int sysctlcount = 0;
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-07-25 21:18:47 +00:00
|
|
|
static int
|
|
|
|
random_open(dev_t dev, int flags, int fmt, struct proc *p)
|
|
|
|
{
|
|
|
|
if ((flags & FWRITE) && (securelevel > 0 || suser(p)))
|
|
|
|
return EPERM;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-11-25 19:13:29 +00:00
|
|
|
static int
|
|
|
|
random_close(dev_t dev, int flags, int fmt, struct proc *p)
|
|
|
|
{
|
2000-11-25 20:08:07 +00:00
|
|
|
if ((flags & FWRITE) && !(securelevel > 0 || suser(p)))
|
2000-11-25 19:13:29 +00:00
|
|
|
random_reseed();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-06-25 08:38:58 +00:00
|
|
|
static int
|
2000-07-07 09:03:59 +00:00
|
|
|
random_read(dev_t dev, struct uio *uio, int flag)
|
2000-06-25 08:38:58 +00:00
|
|
|
{
|
|
|
|
u_int c, ret;
|
|
|
|
int error = 0;
|
2000-07-09 11:49:07 +00:00
|
|
|
void *random_buf;
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-10-18 10:39:18 +00:00
|
|
|
while (!random_state.seeded) {
|
|
|
|
if (flag & IO_NDELAY)
|
|
|
|
error = EWOULDBLOCK;
|
2000-10-14 10:59:56 +00:00
|
|
|
else
|
2000-10-18 10:39:18 +00:00
|
|
|
error = tsleep(&random_state, PUSER|PCATCH, "rndblk", 0);
|
|
|
|
if (error != 0)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
c = min(uio->uio_resid, PAGE_SIZE);
|
|
|
|
random_buf = (void *)malloc(c, M_TEMP, M_WAITOK);
|
|
|
|
while (uio->uio_resid > 0 && error == 0) {
|
|
|
|
ret = read_random_real(random_buf, c);
|
|
|
|
error = uiomove(random_buf, ret, uio);
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
2000-10-18 10:39:18 +00:00
|
|
|
free(random_buf, M_TEMP);
|
2000-06-25 08:38:58 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-07 09:03:59 +00:00
|
|
|
random_write(dev_t dev, struct uio *uio, int flag)
|
2000-06-25 08:38:58 +00:00
|
|
|
{
|
|
|
|
u_int c;
|
|
|
|
int error = 0;
|
2000-07-09 11:49:07 +00:00
|
|
|
void *random_buf;
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-07-07 09:03:59 +00:00
|
|
|
random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
|
2000-06-25 08:38:58 +00:00
|
|
|
while (uio->uio_resid > 0) {
|
|
|
|
c = min(uio->uio_resid, PAGE_SIZE);
|
2000-07-07 09:03:59 +00:00
|
|
|
error = uiomove(random_buf, c, uio);
|
2000-06-25 08:38:58 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
2000-07-17 12:23:04 +00:00
|
|
|
write_random(random_buf, c);
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
2000-07-07 09:03:59 +00:00
|
|
|
free(random_buf, M_TEMP);
|
2000-06-25 08:38:58 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2000-09-10 13:52:19 +00:00
|
|
|
static int
|
|
|
|
random_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
|
|
|
|
{
|
|
|
|
return ENOTTY;
|
|
|
|
}
|
|
|
|
|
2000-10-14 10:59:56 +00:00
|
|
|
static int
|
|
|
|
random_poll(dev_t dev, int events, struct proc *p)
|
|
|
|
{
|
|
|
|
int revents;
|
|
|
|
|
|
|
|
revents = 0;
|
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
|
|
|
if (random_state.seeded)
|
|
|
|
revents = events & (POLLIN | POLLRDNORM);
|
|
|
|
else
|
|
|
|
selrecord(p, &random_state.rsel);
|
|
|
|
}
|
|
|
|
return revents;
|
|
|
|
}
|
|
|
|
|
2000-06-25 08:38:58 +00:00
|
|
|
static int
|
|
|
|
random_modevent(module_t mod, int type, void *data)
|
|
|
|
{
|
2001-01-14 17:50:15 +00:00
|
|
|
struct sysctl_oid *node_base, *node1, *node2;
|
|
|
|
int error, i;
|
2000-09-10 13:52:19 +00:00
|
|
|
|
2000-06-25 08:38:58 +00:00
|
|
|
switch(type) {
|
|
|
|
case MOD_LOAD:
|
2000-09-10 13:52:19 +00:00
|
|
|
error = random_init();
|
|
|
|
if (error != 0)
|
|
|
|
return error;
|
2001-01-14 17:50:15 +00:00
|
|
|
|
|
|
|
random_sysctl[sysctlcount++] = node_base =
|
|
|
|
SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_kern),
|
|
|
|
OID_AUTO, "random", CTLFLAG_RW, 0,
|
|
|
|
"Random Number Generator");
|
|
|
|
random_sysctl[sysctlcount++] = node1 =
|
|
|
|
SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(node_base),
|
|
|
|
OID_AUTO, "sys", CTLFLAG_RW, 0,
|
|
|
|
"Entropy Device Parameters");
|
|
|
|
random_sysctl[sysctlcount++] =
|
|
|
|
SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node1),
|
|
|
|
OID_AUTO, "seeded", CTLFLAG_RW,
|
|
|
|
&random_state.seeded, 0, "Seeded State");
|
|
|
|
random_sysctl[sysctlcount++] = node2 =
|
|
|
|
SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(node_base),
|
|
|
|
OID_AUTO, "yarrow", CTLFLAG_RW, 0,
|
|
|
|
"Yarrow Parameters");
|
|
|
|
random_sysctl[sysctlcount++] =
|
|
|
|
SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node2),
|
|
|
|
OID_AUTO, "gengateinterval", CTLFLAG_RW,
|
|
|
|
&random_state.gengateinterval, 0,
|
|
|
|
"Generator Gate Interval");
|
|
|
|
random_sysctl[sysctlcount++] =
|
|
|
|
SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node2),
|
|
|
|
OID_AUTO, "bins", CTLFLAG_RW,
|
|
|
|
&random_state.bins, 0,
|
|
|
|
"Execution time tuner");
|
|
|
|
random_sysctl[sysctlcount++] =
|
|
|
|
SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node2),
|
|
|
|
OID_AUTO, "fastthresh", CTLFLAG_RW,
|
|
|
|
&random_state.pool[0].thresh, 0,
|
|
|
|
"Fast pool reseed threshhold");
|
|
|
|
random_sysctl[sysctlcount++] =
|
|
|
|
SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node2),
|
|
|
|
OID_AUTO, "slowthresh", CTLFLAG_RW,
|
|
|
|
&random_state.pool[1].thresh, 0,
|
|
|
|
"Slow pool reseed threshhold");
|
|
|
|
random_sysctl[sysctlcount++] =
|
|
|
|
SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node2),
|
|
|
|
OID_AUTO, "slowoverthresh", CTLFLAG_RW,
|
|
|
|
&random_state.slowoverthresh, 0,
|
|
|
|
"Slow pool over-threshhold reseed");
|
|
|
|
|
2000-06-25 08:38:58 +00:00
|
|
|
if (bootverbose)
|
|
|
|
printf("random: <entropy source>\n");
|
2000-07-07 09:03:59 +00:00
|
|
|
random_dev = make_dev(&random_cdevsw, RANDOM_MINOR, UID_ROOT,
|
2000-06-27 09:38:40 +00:00
|
|
|
GID_WHEEL, 0666, "random");
|
2000-07-07 09:03:59 +00:00
|
|
|
urandom_dev = make_dev(&random_cdevsw, URANDOM_MINOR, UID_ROOT,
|
2000-09-10 13:52:19 +00:00
|
|
|
GID_WHEEL, 0666, "urandom"); /* XXX Temporary */
|
2000-06-25 08:38:58 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case MOD_UNLOAD:
|
2000-07-07 09:03:59 +00:00
|
|
|
random_deinit();
|
|
|
|
destroy_dev(random_dev);
|
2000-09-10 13:52:19 +00:00
|
|
|
destroy_dev(urandom_dev); /* XXX Temporary */
|
2001-01-14 17:50:15 +00:00
|
|
|
for (i = sysctlcount - 1; i >= 0; i--)
|
|
|
|
if (sysctl_remove_oid(random_sysctl[i], 1, 0) == EINVAL)
|
|
|
|
panic("random: removing sysctl");
|
2000-06-25 08:38:58 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case MOD_SHUTDOWN:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEV_MODULE(random, random_modevent, NULL);
|