randomdev.c revision 63855
19111Stwisti/*-
29111Stwisti * Copyright (c) 2000 Mark R V Murray
39111Stwisti * All rights reserved.
49111Stwisti *
59111Stwisti * Redistribution and use in source and binary forms, with or without
69111Stwisti * modification, are permitted provided that the following conditions
79111Stwisti * are met:
89111Stwisti * 1. Redistributions of source code must retain the above copyright
99111Stwisti *    notice, this list of conditions and the following disclaimer
109111Stwisti *    in this position and unchanged.
119111Stwisti * 2. Redistributions in binary form must reproduce the above copyright
129111Stwisti *    notice, this list of conditions and the following disclaimer in the
139111Stwisti *    documentation and/or other materials provided with the distribution.
149111Stwisti *
159111Stwisti * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
169111Stwisti * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
179111Stwisti * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
189111Stwisti * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199111Stwisti * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
209111Stwisti * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
219111Stwisti * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
229111Stwisti * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
239111Stwisti * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
249111Stwisti * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
259111Stwisti *
269111Stwisti * $FreeBSD: head/sys/dev/random/randomdev.c 63855 2000-07-25 21:18:47Z markm $
279111Stwisti */
289111Stwisti
299111Stwisti#include <sys/param.h>
309111Stwisti#include <sys/queue.h>
319111Stwisti#include <sys/systm.h>
329111Stwisti#include <sys/conf.h>
339111Stwisti#include <sys/fcntl.h>
349111Stwisti#include <sys/uio.h>
359111Stwisti#include <sys/kernel.h>
369111Stwisti#include <sys/malloc.h>
379111Stwisti#include <sys/module.h>
389111Stwisti#include <sys/bus.h>
399111Stwisti#include <sys/random.h>
409111Stwisti#include <machine/bus.h>
419111Stwisti#include <machine/resource.h>
429111Stwisti#include <sys/rman.h>
439111Stwisti#include <sys/signalvar.h>
449111Stwisti#include <sys/sysctl.h>
45#include <crypto/blowfish/blowfish.h>
46
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;
52
53#define CDEV_MAJOR	2
54#define RANDOM_MINOR	3
55#define URANDOM_MINOR	4
56
57static struct cdevsw random_cdevsw = {
58	/* open */	random_open,
59	/* close */	(d_close_t *)nullop,
60	/* read */	random_read,
61	/* write */	random_write,
62	/* ioctl */	noioctl,
63	/* poll */	nopoll,
64	/* mmap */	nommap,
65	/* strategy */	nostrategy,
66	/* name */	"random",
67	/* maj */	CDEV_MAJOR,
68	/* dump */	nodump,
69	/* psize */	nopsize,
70	/* flags */	0,
71	/* bmaj */	-1
72};
73
74/* For use with make_dev(9)/destroy_dev(9). */
75static dev_t random_dev;
76static dev_t urandom_dev;
77
78SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, "Random Number Generator");
79SYSCTL_NODE(_kern_random, OID_AUTO, yarrow, CTLFLAG_RW, 0, "Yarrow Parameters");
80SYSCTL_INT(_kern_random_yarrow, OID_AUTO, gengateinterval, CTLFLAG_RW,
81	&random_state.gengateinterval, 10, "Generator Gate Interval");
82SYSCTL_INT(_kern_random_yarrow, OID_AUTO, bins, CTLFLAG_RW,
83	&random_state.bins, 10, "Execution time tuner");
84SYSCTL_INT(_kern_random_yarrow, OID_AUTO, fastthresh, CTLFLAG_RW,
85	&random_state.pool[0].thresh, 100, "Fast pool reseed threshhold");
86SYSCTL_INT(_kern_random_yarrow, OID_AUTO, slowthresh, CTLFLAG_RW,
87	&random_state.pool[1].thresh, 160, "Slow pool reseed threshhold");
88SYSCTL_INT(_kern_random_yarrow, OID_AUTO, slowoverthresh, CTLFLAG_RW,
89	&random_state.slowoverthresh, 2, "Slow pool over-threshhold reseed");
90
91static int
92random_open(dev_t dev, int flags, int fmt, struct proc *p)
93{
94	if ((flags & FWRITE) && (securelevel > 0 || suser(p)))
95		return EPERM;
96	else
97		return 0;
98}
99
100static int
101random_read(dev_t dev, struct uio *uio, int flag)
102{
103	u_int c, ret;
104	int error = 0;
105	void *random_buf;
106
107	c = min(uio->uio_resid, PAGE_SIZE);
108	random_buf = (void *)malloc(c, M_TEMP, M_WAITOK);
109	while (uio->uio_resid > 0 && error == 0) {
110		ret = read_random(random_buf, c);
111		error = uiomove(random_buf, ret, uio);
112	}
113	free(random_buf, M_TEMP);
114	return error;
115}
116
117static int
118random_write(dev_t dev, struct uio *uio, int flag)
119{
120	u_int c;
121	int error = 0;
122	void *random_buf;
123
124	random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
125	while (uio->uio_resid > 0) {
126		c = min(uio->uio_resid, PAGE_SIZE);
127		error = uiomove(random_buf, c, uio);
128		if (error)
129			break;
130		write_random(random_buf, c);
131	}
132	free(random_buf, M_TEMP);
133	return error;
134}
135
136static int
137random_modevent(module_t mod, int type, void *data)
138{
139	switch(type) {
140	case MOD_LOAD:
141		if (bootverbose)
142			printf("random: <entropy source>\n");
143		random_dev = make_dev(&random_cdevsw, RANDOM_MINOR, UID_ROOT,
144			GID_WHEEL, 0666, "random");
145		urandom_dev = make_dev(&random_cdevsw, URANDOM_MINOR, UID_ROOT,
146			GID_WHEEL, 0666, "urandom");
147		random_init();
148		return 0;
149
150	case MOD_UNLOAD:
151		random_deinit();
152		destroy_dev(random_dev);
153		destroy_dev(urandom_dev);
154		return 0;
155
156	case MOD_SHUTDOWN:
157		return 0;
158
159	default:
160		return EOPNOTSUPP;
161	}
162}
163
164DEV_MODULE(random, random_modevent, NULL);
165