randomdev_soft.c revision 170067
1/*-
2 * Copyright (c) 2000-2004 Mark R V Murray
3 * Copyright (c) 2004 Robert N. M. Watson
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer
11 *    in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/dev/random/randomdev_soft.c 170067 2007-05-28 18:20:15Z rwatson $");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/bus.h>
35#include <sys/conf.h>
36#include <sys/fcntl.h>
37#include <sys/kernel.h>
38#include <sys/kthread.h>
39#include <sys/lock.h>
40#include <sys/malloc.h>
41#include <sys/mutex.h>
42#include <sys/poll.h>
43#include <sys/proc.h>
44#include <sys/random.h>
45#include <sys/selinfo.h>
46#include <sys/sysctl.h>
47#include <sys/uio.h>
48#include <sys/unistd.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);
62static int random_yarrow_poll(int event,struct thread *td);
63static int random_yarrow_block(int flag);
64
65struct random_systat random_yarrow = {
66	.ident = "Software, Yarrow",
67	.init = random_yarrow_init,
68	.deinit = random_yarrow_deinit,
69	.block = random_yarrow_block,
70	.read = random_yarrow_read,
71	.write = random_yarrow_write,
72	.poll = random_yarrow_poll,
73	.reseed = random_yarrow_reseed,
74	.seeded = 1,
75};
76
77MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers");
78
79/*
80 * The harvest mutex protects the consistency of the entropy fifos and
81 * empty fifo.
82 */
83struct mtx	harvest_mtx;
84
85/* Lockable FIFO queue holding entropy buffers */
86struct entropyfifo {
87	int count;
88	STAILQ_HEAD(harvestlist, harvest) head;
89};
90
91/* Empty entropy buffers */
92static struct entropyfifo emptyfifo;
93
94#define EMPTYBUFFERS	1024
95
96/* Harvested entropy */
97static struct entropyfifo harvestfifo[ENTROPYSOURCE];
98
99/* <0 to end the kthread, 0 to let it run */
100static int random_kthread_control = 0;
101
102static struct proc *random_kthread_proc;
103
104/* List for the dynamic sysctls */
105struct sysctl_ctx_list random_clist;
106
107/* ARGSUSED */
108static int
109random_check_boolean(SYSCTL_HANDLER_ARGS)
110{
111	if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0)
112		*(u_int *)(oidp->oid_arg1) = 1;
113	return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
114}
115
116/* ARGSUSED */
117void
118random_yarrow_init(void)
119{
120	int error, i;
121	struct harvest *np;
122	struct sysctl_oid *random_o, *random_sys_o, *random_sys_harvest_o;
123	enum esource e;
124
125	random_o = SYSCTL_ADD_NODE(&random_clist,
126	    SYSCTL_STATIC_CHILDREN(_kern),
127	    OID_AUTO, "random", CTLFLAG_RW, 0,
128	    "Software Random Number Generator");
129
130	random_yarrow_init_alg(&random_clist, random_o);
131
132	random_sys_o = SYSCTL_ADD_NODE(&random_clist,
133	    SYSCTL_CHILDREN(random_o),
134	    OID_AUTO, "sys", CTLFLAG_RW, 0,
135	    "Entropy Device Parameters");
136
137	SYSCTL_ADD_PROC(&random_clist,
138	    SYSCTL_CHILDREN(random_sys_o),
139	    OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW,
140	    &random_systat.seeded, 1, random_check_boolean, "I",
141	    "Seeded State");
142
143	random_sys_harvest_o = SYSCTL_ADD_NODE(&random_clist,
144	    SYSCTL_CHILDREN(random_sys_o),
145	    OID_AUTO, "harvest", CTLFLAG_RW, 0,
146	    "Entropy Sources");
147
148	SYSCTL_ADD_PROC(&random_clist,
149	    SYSCTL_CHILDREN(random_sys_harvest_o),
150	    OID_AUTO, "ethernet", CTLTYPE_INT | CTLFLAG_RW,
151	    &harvest.ethernet, 1, random_check_boolean, "I",
152	    "Harvest NIC entropy");
153	SYSCTL_ADD_PROC(&random_clist,
154	    SYSCTL_CHILDREN(random_sys_harvest_o),
155	    OID_AUTO, "point_to_point", CTLTYPE_INT | CTLFLAG_RW,
156	    &harvest.point_to_point, 1, random_check_boolean, "I",
157	    "Harvest serial net entropy");
158	SYSCTL_ADD_PROC(&random_clist,
159	    SYSCTL_CHILDREN(random_sys_harvest_o),
160	    OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW,
161	    &harvest.interrupt, 1, random_check_boolean, "I",
162	    "Harvest IRQ entropy");
163	SYSCTL_ADD_PROC(&random_clist,
164	    SYSCTL_CHILDREN(random_sys_harvest_o),
165	    OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW,
166	    &harvest.swi, 0, random_check_boolean, "I",
167	    "Harvest SWI entropy");
168
169	/* Initialise the harvest fifos */
170	STAILQ_INIT(&emptyfifo.head);
171	emptyfifo.count = 0;
172	for (i = 0; i < EMPTYBUFFERS; i++) {
173		np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK);
174		STAILQ_INSERT_TAIL(&emptyfifo.head, np, next);
175	}
176	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
177		STAILQ_INIT(&harvestfifo[e].head);
178		harvestfifo[e].count = 0;
179	}
180
181	mtx_init(&harvest_mtx, "entropy harvest mutex", NULL, MTX_SPIN);
182
183	/* Start the hash/reseed thread */
184	error = kthread_create(random_kthread, NULL,
185	    &random_kthread_proc, RFHIGHPID, 0, "yarrow");
186	if (error != 0)
187		panic("Cannot create entropy maintenance thread.");
188
189	/* Register the randomness harvesting routine */
190	random_yarrow_init_harvester(random_harvest_internal,
191	    random_yarrow_read);
192}
193
194/* ARGSUSED */
195void
196random_yarrow_deinit(void)
197{
198	struct harvest *np;
199	enum esource e;
200
201	/* Deregister the randomness harvesting routine */
202	random_yarrow_deinit_harvester();
203
204	/*
205	 * Command the hash/reseed thread to end and wait for it to finish
206	 */
207	random_kthread_control = -1;
208	tsleep((void *)&random_kthread_control, 0, "term", 0);
209
210	/* Destroy the harvest fifos */
211	while (!STAILQ_EMPTY(&emptyfifo.head)) {
212		np = STAILQ_FIRST(&emptyfifo.head);
213		STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
214		free(np, M_ENTROPY);
215	}
216	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
217		while (!STAILQ_EMPTY(&harvestfifo[e].head)) {
218			np = STAILQ_FIRST(&harvestfifo[e].head);
219			STAILQ_REMOVE_HEAD(&harvestfifo[e].head, next);
220			free(np, M_ENTROPY);
221		}
222	}
223
224	random_yarrow_deinit_alg();
225
226	mtx_destroy(&harvest_mtx);
227
228	sysctl_ctx_free(&random_clist);
229}
230
231/* ARGSUSED */
232static void
233random_kthread(void *arg __unused)
234{
235	STAILQ_HEAD(, harvest) local_queue;
236	struct harvest *event = NULL;
237	int active, local_count;
238	enum esource source;
239
240	STAILQ_INIT(&local_queue);
241	local_count = 0;
242
243	/* Process until told to stop */
244	for (; random_kthread_control == 0;) {
245
246		active = 0;
247
248		/* Cycle through all the entropy sources */
249		mtx_lock_spin(&harvest_mtx);
250		for (source = RANDOM_START; source < ENTROPYSOURCE; source++) {
251			/*
252			 * Drain entropy source records into a thread-local
253			 * queue for processing while not holding the mutex.
254			 */
255			STAILQ_CONCAT(&local_queue, &harvestfifo[source].head);
256			local_count += harvestfifo[source].count;
257			harvestfifo[source].count = 0;
258		}
259
260		/*
261		 * Deal with events, if any, dropping the mutex as we process
262		 * each event.  Then push the events back into the empty
263		 * fifo.
264		 */
265		if (!STAILQ_EMPTY(&local_queue)) {
266			mtx_unlock_spin(&harvest_mtx);
267			STAILQ_FOREACH(event, &local_queue, next)
268				random_process_event(event);
269			mtx_lock_spin(&harvest_mtx);
270			STAILQ_CONCAT(&emptyfifo.head, &local_queue);
271			emptyfifo.count += local_count;
272			local_count = 0;
273		}
274		mtx_unlock_spin(&harvest_mtx);
275
276		KASSERT(local_count == 0, ("random_kthread: local_count %d",
277		    local_count));
278
279		/* Found nothing, so don't belabour the issue */
280		if (!active)
281			pause("-", hz / 10);
282
283	}
284
285	random_set_wakeup_exit(&random_kthread_control);
286	/* NOTREACHED */
287}
288
289/* Entropy harvesting routine. This is supposed to be fast; do
290 * not do anything slow in here!
291 */
292static void
293random_harvest_internal(u_int64_t somecounter, const void *entropy,
294    u_int count, u_int bits, u_int frac, enum esource origin)
295{
296	struct harvest *event;
297
298	KASSERT(origin == RANDOM_START || origin == RANDOM_WRITE ||
299            origin == RANDOM_KEYBOARD || origin == RANDOM_MOUSE ||
300            origin == RANDOM_NET || origin == RANDOM_INTERRUPT ||
301            origin == RANDOM_PURE,
302	    ("random_harvest_internal: origin %d invalid\n", origin));
303
304	/* Lockless read to avoid lock operations if fifo is full. */
305	if (harvestfifo[origin].count >= RANDOM_FIFO_MAX)
306		return;
307
308	mtx_lock_spin(&harvest_mtx);
309
310	/*
311	 * Don't make the harvest queues too big - help to prevent low-grade
312	 * entropy swamping
313	 */
314	if (harvestfifo[origin].count < RANDOM_FIFO_MAX) {
315		event = STAILQ_FIRST(&emptyfifo.head);
316		if (event != NULL) {
317			/* Add the harvested data to the fifo */
318			STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
319			harvestfifo[origin].count++;
320			event->somecounter = somecounter;
321			event->size = count;
322			event->bits = bits;
323			event->frac = frac;
324			event->source = origin;
325
326			/* XXXX Come back and make this dynamic! */
327			count = MIN(count, HARVESTSIZE);
328			memcpy(event->entropy, entropy, count);
329
330			STAILQ_INSERT_TAIL(&harvestfifo[origin].head,
331			    event, next);
332		}
333	}
334	mtx_unlock_spin(&harvest_mtx);
335}
336
337void
338random_yarrow_write(void *buf, int count)
339{
340	int i;
341	u_int chunk;
342
343	/*
344	 * Break the input up into HARVESTSIZE chunks. The writer has too
345	 * much control here, so "estimate" the the entropy as zero.
346	 */
347	for (i = 0; i < count; i += HARVESTSIZE) {
348		chunk = HARVESTSIZE;
349		if (i + chunk >= count)
350			chunk = (u_int)(count - i);
351		random_harvest_internal(get_cyclecount(), (char *)buf + i,
352		    chunk, 0, 0, RANDOM_WRITE);
353	}
354}
355
356void
357random_yarrow_unblock(void)
358{
359	if (!random_systat.seeded) {
360		random_systat.seeded = 1;
361		selwakeuppri(&random_systat.rsel, PUSER);
362		wakeup(&random_systat);
363	}
364}
365
366static int
367random_yarrow_poll(int events, struct thread *td)
368{
369	int revents = 0;
370	mtx_lock(&random_reseed_mtx);
371
372	if (random_systat.seeded)
373		revents = events & (POLLIN | POLLRDNORM);
374	else
375		selrecord(td, &random_systat.rsel);
376
377	mtx_unlock(&random_reseed_mtx);
378	return revents;
379}
380
381static int
382random_yarrow_block(int flag)
383{
384	int error = 0;
385
386	mtx_lock(&random_reseed_mtx);
387
388	/* Blocking logic */
389	while (random_systat.seeded && !error) {
390		if (flag & O_NONBLOCK)
391			error = EWOULDBLOCK;
392		else {
393			printf("Entropy device is blocking.\n");
394			error = msleep(&random_systat,
395			    &random_reseed_mtx,
396			    PUSER | PCATCH, "block", 0);
397		}
398	}
399	mtx_unlock(&random_reseed_mtx);
400
401	return error;
402}
403