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