randomdev_soft.c revision 132346
1/*-
2 * Copyright (c) 2000-2004 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 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/dev/random/randomdev_soft.c 132346 2004-07-18 09:07:58Z markm $");
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/bus.h>
34#include <sys/conf.h>
35#include <sys/fcntl.h>
36#include <sys/kernel.h>
37#include <sys/kthread.h>
38#include <sys/lock.h>
39#include <sys/malloc.h>
40#include <sys/mutex.h>
41#include <sys/poll.h>
42#include <sys/proc.h>
43#include <sys/random.h>
44#include <sys/selinfo.h>
45#include <sys/sysctl.h>
46#include <sys/uio.h>
47#include <sys/unistd.h>
48#include <sys/vnode.h>
49
50#include <machine/bus.h>
51#include <machine/cpu.h>
52
53#include <dev/random/randomdev.h>
54#include <dev/random/randomdev_soft.h>
55
56#define RANDOM_FIFO_MAX	256	/* How many events to queue up */
57
58static void random_kthread(void *);
59static void
60random_harvest_internal(u_int64_t, const void *, u_int,
61    u_int, u_int, enum esource);
62
63struct random_systat random_yarrow = {
64	.ident = "Software, Yarrow",
65	.init = random_yarrow_init,
66	.deinit = random_yarrow_deinit,
67	.read = random_yarrow_read,
68	.write = random_yarrow_write,
69	.reseed = random_yarrow_reseed,
70	.seeded = 1,
71};
72
73MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers");
74
75/* Lockable FIFO queue holding entropy buffers */
76struct entropyfifo {
77	struct mtx lock;
78	int count;
79	STAILQ_HEAD(harvestlist, harvest) head;
80};
81
82/* Empty entropy buffers */
83static struct entropyfifo emptyfifo;
84
85#define EMPTYBUFFERS	1024
86
87/* Harvested entropy */
88static struct entropyfifo harvestfifo[ENTROPYSOURCE];
89
90/* <0 to end the kthread, 0 to let it run */
91static int random_kthread_control = 0;
92
93static struct proc *random_kthread_proc;
94
95/* List for the dynamic sysctls */
96struct sysctl_ctx_list random_clist;
97
98/* ARGSUSED */
99static int
100random_check_boolean(SYSCTL_HANDLER_ARGS)
101{
102	if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0)
103		*(u_int *)(oidp->oid_arg1) = 1;
104	return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
105}
106
107/* ARGSUSED */
108void
109random_yarrow_init(void)
110{
111	int error, i;
112	struct harvest *np;
113	struct sysctl_oid *o, *random_o, *random_sys_o, *random_sys_harvest_o;
114	enum esource e;
115
116	o = SYSCTL_ADD_NODE(&random_clist,
117	    SYSCTL_STATIC_CHILDREN(_kern),
118	    OID_AUTO, "random", CTLFLAG_RW, 0,
119	    "Software Random Number Generator");
120
121	random_o = o;
122
123	random_yarrow_init_alg(&random_clist, random_o);
124
125	o = SYSCTL_ADD_NODE(&random_clist,
126	    SYSCTL_CHILDREN(random_o),
127	    OID_AUTO, "sys", CTLFLAG_RW, 0,
128	    "Entropy Device Parameters");
129
130	random_sys_o = o;
131
132	o = SYSCTL_ADD_PROC(&random_clist,
133	    SYSCTL_CHILDREN(random_sys_o),
134	    OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW,
135	    &random_systat.seeded, 1, random_check_boolean, "I",
136	    "Seeded State");
137
138	o = SYSCTL_ADD_NODE(&random_clist,
139	    SYSCTL_CHILDREN(random_sys_o),
140	    OID_AUTO, "harvest", CTLFLAG_RW, 0,
141	    "Entropy Sources");
142
143	random_sys_harvest_o = o;
144
145	o = SYSCTL_ADD_PROC(&random_clist,
146	    SYSCTL_CHILDREN(random_sys_harvest_o),
147	    OID_AUTO, "ethernet", CTLTYPE_INT | CTLFLAG_RW,
148	    &harvest.ethernet, 1, random_check_boolean, "I",
149	    "Harvest NIC entropy");
150	o = SYSCTL_ADD_PROC(&random_clist,
151	    SYSCTL_CHILDREN(random_sys_harvest_o),
152	    OID_AUTO, "point_to_point", CTLTYPE_INT | CTLFLAG_RW,
153	    &harvest.point_to_point, 1, random_check_boolean, "I",
154	    "Harvest serial net entropy");
155	o = SYSCTL_ADD_PROC(&random_clist,
156	    SYSCTL_CHILDREN(random_sys_harvest_o),
157	    OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW,
158	    &harvest.interrupt, 1, random_check_boolean, "I",
159	    "Harvest IRQ entropy");
160	o = SYSCTL_ADD_PROC(&random_clist,
161	    SYSCTL_CHILDREN(random_sys_harvest_o),
162	    OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW,
163	    &harvest.swi, 0, random_check_boolean, "I",
164	    "Harvest SWI entropy");
165
166	/* Initialise the harvest fifos */
167	STAILQ_INIT(&emptyfifo.head);
168	emptyfifo.count = 0;
169	mtx_init(&emptyfifo.lock, "entropy harvest buffers", NULL, MTX_SPIN);
170	for (i = 0; i < EMPTYBUFFERS; i++) {
171		np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK);
172		STAILQ_INSERT_TAIL(&emptyfifo.head, np, next);
173	}
174	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
175		STAILQ_INIT(&harvestfifo[e].head);
176		harvestfifo[e].count = 0;
177		mtx_init(&harvestfifo[e].lock, "entropy harvest", NULL,
178		    MTX_SPIN);
179	}
180
181	/* Start the hash/reseed thread */
182	error = kthread_create(random_kthread, NULL,
183	    &random_kthread_proc, RFHIGHPID, 0, "yarrow");
184	if (error != 0)
185		panic("Cannot create entropy maintenance thread.");
186
187	/* Register the randomness harvesting routine */
188	random_yarrow_init_harvester(random_harvest_internal,
189	    random_yarrow_read);
190}
191
192/* ARGSUSED */
193void
194random_yarrow_deinit(void)
195{
196	struct harvest *np;
197	enum esource e;
198
199	/* Deregister the randomness harvesting routine */
200	random_yarrow_deinit_harvester();
201
202	/*
203	 * Command the hash/reseed thread to end and wait for it to finish
204	 */
205	random_kthread_control = -1;
206	tsleep((void *)&random_kthread_control, PUSER, "term", 0);
207
208	/* Destroy the harvest fifos */
209	while (!STAILQ_EMPTY(&emptyfifo.head)) {
210		np = STAILQ_FIRST(&emptyfifo.head);
211		STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
212		free(np, M_ENTROPY);
213	}
214	mtx_destroy(&emptyfifo.lock);
215	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
216		while (!STAILQ_EMPTY(&harvestfifo[e].head)) {
217			np = STAILQ_FIRST(&harvestfifo[e].head);
218			STAILQ_REMOVE_HEAD(&harvestfifo[e].head, next);
219			free(np, M_ENTROPY);
220		}
221		mtx_destroy(&harvestfifo[e].lock);
222	}
223
224	random_yarrow_deinit_alg();
225
226	sysctl_ctx_free(&random_clist);
227}
228
229/* ARGSUSED */
230static void
231random_kthread(void *arg __unused)
232{
233	struct harvest *event = NULL;
234	int found, active;
235	enum esource source;
236
237	/* Process until told to stop */
238	for (; random_kthread_control == 0;) {
239
240		active = 0;
241
242		/* Cycle through all the entropy sources */
243		for (source = RANDOM_START; source < ENTROPYSOURCE; source++) {
244
245			found = 0;
246
247			/* Lock up queue draining */
248			mtx_lock_spin(&harvestfifo[source].lock);
249
250			if (!STAILQ_EMPTY(&harvestfifo[source].head)) {
251
252				/* Get a harvested entropy event */
253				harvestfifo[source].count--;
254				event = STAILQ_FIRST(&harvestfifo[source].head);
255				STAILQ_REMOVE_HEAD(&harvestfifo[source].head,
256				    next);
257
258				active = found = 1;
259
260			}
261			/* Unlock the queue */
262			mtx_unlock_spin(&harvestfifo[source].lock);
263
264			/* Deal with the event and dispose of it */
265			if (found) {
266
267				random_process_event(event);
268
269				/* Lock the empty event buffer fifo */
270				mtx_lock_spin(&emptyfifo.lock);
271
272				STAILQ_INSERT_TAIL(&emptyfifo.head, event,
273				    next);
274
275				mtx_unlock_spin(&emptyfifo.lock);
276
277			}
278		}
279
280		/* Found nothing, so don't belabour the issue */
281		if (!active)
282			tsleep(&harvestfifo, PUSER, "-", hz / 10);
283
284	}
285
286	random_set_wakeup_exit(&random_kthread_control);
287	/* NOTREACHED */
288}
289
290/* Entropy harvesting routine. This is supposed to be fast; do
291 * not do anything slow in here!
292 */
293static void
294random_harvest_internal(u_int64_t somecounter, const void *entropy,
295    u_int count, u_int bits, u_int frac, enum esource origin)
296{
297	struct harvest *event;
298
299	/* Lock the particular fifo */
300	mtx_lock_spin(&harvestfifo[origin].lock);
301
302	/*
303	 * Don't make the harvest queues too big - help to prevent low-grade
304	 * entropy swamping
305	 */
306	if (harvestfifo[origin].count < RANDOM_FIFO_MAX) {
307
308		/* Lock the empty event buffer fifo */
309		mtx_lock_spin(&emptyfifo.lock);
310
311		if (!STAILQ_EMPTY(&emptyfifo.head)) {
312			event = STAILQ_FIRST(&emptyfifo.head);
313			STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
314		} else
315			event = NULL;
316
317		mtx_unlock_spin(&emptyfifo.lock);
318
319		/* If we didn't obtain a buffer, tough */
320		if (event) {
321
322			/* Add the harvested data to the fifo */
323			harvestfifo[origin].count++;
324			event->somecounter = somecounter;
325			event->size = count;
326			event->bits = bits;
327			event->frac = frac;
328			event->source = origin;
329
330			/* XXXX Come back and make this dynamic! */
331			count = MIN(count, HARVESTSIZE);
332			memcpy(event->entropy, entropy, count);
333
334			STAILQ_INSERT_TAIL(&harvestfifo[origin].head,
335			    event, next);
336		}
337	}
338	mtx_unlock_spin(&harvestfifo[origin].lock);
339
340}
341
342void
343random_yarrow_write(void *buf, int count)
344{
345	int i;
346	u_int chunk;
347
348	/*
349	 * Break the input up into HARVESTSIZE chunks. The writer has too
350	 * much control here, so "estimate" the the entropy as zero.
351	 */
352	for (i = 0; i < count; i += HARVESTSIZE) {
353		chunk = HARVESTSIZE;
354		if (i + chunk >= count)
355			chunk = (u_int)(count - i);
356		random_harvest_internal(get_cyclecount(), (char *)buf + i,
357		    chunk, 0, 0, RANDOM_WRITE);
358	}
359}
360
361void
362random_yarrow_unblock(void)
363{
364	if (!random_systat.seeded) {
365		random_systat.seeded = 1;
366		selwakeuppri(&random_systat.rsel, PUSER);
367		wakeup(&random_systat);
368	}
369}
370