randomdev.c revision 66155
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 66155 2000-09-21 06:23:16Z 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/proc.h>
40#include <sys/random.h>
41#include <machine/bus.h>
42#include <machine/resource.h>
43#include <sys/sysctl.h>
44#include <crypto/blowfish/blowfish.h>
45
46#include <dev/randomdev/hash.h>
47#include <dev/randomdev/yarrow.h>
48
49static d_open_t random_open;
50static d_read_t random_read;
51static d_write_t random_write;
52static d_ioctl_t random_ioctl;
53
54#define CDEV_MAJOR	2
55#define RANDOM_MINOR	3
56#define URANDOM_MINOR	4
57
58static struct cdevsw random_cdevsw = {
59	/* open */	random_open,
60	/* close */	(d_close_t *)nullop,
61	/* read */	random_read,
62	/* write */	random_write,
63	/* ioctl */	random_ioctl,
64	/* poll */	nopoll,
65	/* mmap */	nommap,
66	/* strategy */	nostrategy,
67	/* name */	"random",
68	/* maj */	CDEV_MAJOR,
69	/* dump */	nodump,
70	/* psize */	nopsize,
71	/* flags */	0,
72	/* bmaj */	-1
73};
74
75/* For use with make_dev(9)/destroy_dev(9). */
76static dev_t random_dev;
77static dev_t urandom_dev; /* XXX Temporary */
78
79SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, "Random Number Generator");
80SYSCTL_NODE(_kern_random, OID_AUTO, yarrow, CTLFLAG_RW, 0, "Yarrow Parameters");
81SYSCTL_INT(_kern_random_yarrow, OID_AUTO, gengateinterval, CTLFLAG_RW,
82	&random_state.gengateinterval, 10, "Generator Gate Interval");
83SYSCTL_INT(_kern_random_yarrow, OID_AUTO, bins, CTLFLAG_RW,
84	&random_state.bins, 10, "Execution time tuner");
85SYSCTL_INT(_kern_random_yarrow, OID_AUTO, fastthresh, CTLFLAG_RW,
86	&random_state.pool[0].thresh, 100, "Fast pool reseed threshhold");
87SYSCTL_INT(_kern_random_yarrow, OID_AUTO, slowthresh, CTLFLAG_RW,
88	&random_state.pool[1].thresh, 160, "Slow pool reseed threshhold");
89SYSCTL_INT(_kern_random_yarrow, OID_AUTO, slowoverthresh, CTLFLAG_RW,
90	&random_state.slowoverthresh, 2, "Slow pool over-threshhold reseed");
91
92static int
93random_open(dev_t dev, int flags, int fmt, struct proc *p)
94{
95	if ((flags & FWRITE) && (securelevel > 0 || suser(p)))
96		return EPERM;
97	else
98		return 0;
99}
100
101static int
102random_read(dev_t dev, struct uio *uio, int flag)
103{
104	u_int c, ret;
105	int error = 0;
106	void *random_buf;
107
108	c = min(uio->uio_resid, PAGE_SIZE);
109	random_buf = (void *)malloc(c, M_TEMP, M_WAITOK);
110	while (uio->uio_resid > 0 && error == 0) {
111		ret = read_random(random_buf, c);
112		error = uiomove(random_buf, ret, uio);
113	}
114	free(random_buf, M_TEMP);
115	return error;
116}
117
118static int
119random_write(dev_t dev, struct uio *uio, int flag)
120{
121	u_int c;
122	int error = 0;
123	void *random_buf;
124
125	random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
126	while (uio->uio_resid > 0) {
127		c = min(uio->uio_resid, PAGE_SIZE);
128		error = uiomove(random_buf, c, uio);
129		if (error)
130			break;
131		write_random(random_buf, c);
132	}
133	free(random_buf, M_TEMP);
134	return error;
135}
136
137static int
138random_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
139{
140	return ENOTTY;
141}
142
143static int
144random_modevent(module_t mod, int type, void *data)
145{
146	int error;
147
148	switch(type) {
149	case MOD_LOAD:
150		error = random_init();
151		if (error != 0)
152			return error;
153		if (bootverbose)
154			printf("random: <entropy source>\n");
155		random_dev = make_dev(&random_cdevsw, RANDOM_MINOR, UID_ROOT,
156			GID_WHEEL, 0666, "random");
157		urandom_dev = make_dev(&random_cdevsw, URANDOM_MINOR, UID_ROOT,
158			GID_WHEEL, 0666, "urandom"); /* XXX Temporary */
159		return 0;
160
161	case MOD_UNLOAD:
162		random_deinit();
163		destroy_dev(random_dev);
164		destroy_dev(urandom_dev); /* XXX Temporary */
165		return 0;
166
167	case MOD_SHUTDOWN:
168		return 0;
169
170	default:
171		return EOPNOTSUPP;
172	}
173}
174
175DEV_MODULE(random, random_modevent, NULL);
176