randomdev_soft.c revision 167086
181588Sru/*-
259892Sjasone * Copyright (c) 2000-2004 Mark R V Murray
379754Sdd * Copyright (c) 2004 Robert N. M. Watson
459892Sjasone * All rights reserved.
559892Sjasone *
659892Sjasone * Redistribution and use in source and binary forms, with or without
759892Sjasone * modification, are permitted provided that the following conditions
859892Sjasone * are met:
959892Sjasone * 1. Redistributions of source code must retain the above copyright
1059892Sjasone *    notice, this list of conditions and the following disclaimer
1159892Sjasone *    in this position and unchanged.
1259892Sjasone * 2. Redistributions in binary form must reproduce the above copyright
1359892Sjasone *    notice, this list of conditions and the following disclaimer in the
1459892Sjasone *    documentation and/or other materials provided with the distribution.
1579754Sdd *
1659892Sjasone * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1759892Sjasone * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1859892Sjasone * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1959892Sjasone * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2059892Sjasone * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2159892Sjasone * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2259892Sjasone * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2359892Sjasone * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2459892Sjasone * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2559892Sjasone * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2659892Sjasone *
2779754Sdd */
2859892Sjasone
2959892Sjasone#include <sys/cdefs.h>
3059892Sjasone__FBSDID("$FreeBSD: head/sys/dev/random/randomdev_soft.c 167086 2007-02-27 17:23:29Z jhb $");
3159892Sjasone
3259892Sjasone#include <sys/param.h>
3359892Sjasone#include <sys/systm.h>
3459892Sjasone#include <sys/bus.h>
3559892Sjasone#include <sys/conf.h>
36124535Sru#include <sys/fcntl.h>
3759892Sjasone#include <sys/kernel.h>
3884306Sru#include <sys/kthread.h>
3984306Sru#include <sys/lock.h>
4059892Sjasone#include <sys/malloc.h>
4159892Sjasone#include <sys/mutex.h>
4259892Sjasone#include <sys/poll.h>
4359892Sjasone#include <sys/proc.h>
4459892Sjasone#include <sys/random.h>
4559892Sjasone#include <sys/selinfo.h>
4659892Sjasone#include <sys/sysctl.h>
4759892Sjasone#include <sys/uio.h>
4859892Sjasone#include <sys/unistd.h>
4959892Sjasone
5059892Sjasone#include <machine/bus.h>
5159892Sjasone#include <machine/cpu.h>
5259892Sjasone
5359892Sjasone#include <dev/random/randomdev.h>
5459892Sjasone#include <dev/random/randomdev_soft.h>
5559892Sjasone
5659892Sjasone#define RANDOM_FIFO_MAX	256	/* How many events to queue up */
5759892Sjasone
58112542Scharnierstatic void random_kthread(void *);
5959892Sjasonestatic void
60112542Scharnierrandom_harvest_internal(u_int64_t, const void *, u_int,
6159892Sjasone    u_int, u_int, enum esource);
6259892Sjasonestatic int random_yarrow_poll(int event,struct thread *td);
6359892Sjasonestatic int random_yarrow_block(int flag);
6459892Sjasone
6559892Sjasonestruct random_systat random_yarrow = {
6659892Sjasone	.ident = "Software, Yarrow",
6759892Sjasone	.init = random_yarrow_init,
6859892Sjasone	.deinit = random_yarrow_deinit,
6959892Sjasone	.block = random_yarrow_block,
7059892Sjasone	.read = random_yarrow_read,
7159892Sjasone	.write = random_yarrow_write,
7259892Sjasone	.poll = random_yarrow_poll,
7359892Sjasone	.reseed = random_yarrow_reseed,
74112542Scharnier	.seeded = 1,
7559892Sjasone};
76112542Scharnier
7773092SruMALLOC_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 *o, *random_o, *random_sys_o, *random_sys_harvest_o;
123	enum esource e;
124
125	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_o = o;
131
132	random_yarrow_init_alg(&random_clist, random_o);
133
134	o = SYSCTL_ADD_NODE(&random_clist,
135	    SYSCTL_CHILDREN(random_o),
136	    OID_AUTO, "sys", CTLFLAG_RW, 0,
137	    "Entropy Device Parameters");
138
139	random_sys_o = o;
140
141	o = SYSCTL_ADD_PROC(&random_clist,
142	    SYSCTL_CHILDREN(random_sys_o),
143	    OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW,
144	    &random_systat.seeded, 1, random_check_boolean, "I",
145	    "Seeded State");
146
147	o = SYSCTL_ADD_NODE(&random_clist,
148	    SYSCTL_CHILDREN(random_sys_o),
149	    OID_AUTO, "harvest", CTLFLAG_RW, 0,
150	    "Entropy Sources");
151
152	random_sys_harvest_o = o;
153
154	o = SYSCTL_ADD_PROC(&random_clist,
155	    SYSCTL_CHILDREN(random_sys_harvest_o),
156	    OID_AUTO, "ethernet", CTLTYPE_INT | CTLFLAG_RW,
157	    &harvest.ethernet, 1, random_check_boolean, "I",
158	    "Harvest NIC entropy");
159	o = SYSCTL_ADD_PROC(&random_clist,
160	    SYSCTL_CHILDREN(random_sys_harvest_o),
161	    OID_AUTO, "point_to_point", CTLTYPE_INT | CTLFLAG_RW,
162	    &harvest.point_to_point, 1, random_check_boolean, "I",
163	    "Harvest serial net entropy");
164	o = SYSCTL_ADD_PROC(&random_clist,
165	    SYSCTL_CHILDREN(random_sys_harvest_o),
166	    OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW,
167	    &harvest.interrupt, 1, random_check_boolean, "I",
168	    "Harvest IRQ entropy");
169	o = SYSCTL_ADD_PROC(&random_clist,
170	    SYSCTL_CHILDREN(random_sys_harvest_o),
171	    OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW,
172	    &harvest.swi, 0, random_check_boolean, "I",
173	    "Harvest SWI entropy");
174
175	/* Initialise the harvest fifos */
176	STAILQ_INIT(&emptyfifo.head);
177	emptyfifo.count = 0;
178	for (i = 0; i < EMPTYBUFFERS; i++) {
179		np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK);
180		STAILQ_INSERT_TAIL(&emptyfifo.head, np, next);
181	}
182	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
183		STAILQ_INIT(&harvestfifo[e].head);
184		harvestfifo[e].count = 0;
185	}
186
187	mtx_init(&harvest_mtx, "entropy harvest mutex", NULL, MTX_SPIN);
188
189	/* Start the hash/reseed thread */
190	error = kthread_create(random_kthread, NULL,
191	    &random_kthread_proc, RFHIGHPID, 0, "yarrow");
192	if (error != 0)
193		panic("Cannot create entropy maintenance thread.");
194
195	/* Register the randomness harvesting routine */
196	random_yarrow_init_harvester(random_harvest_internal,
197	    random_yarrow_read);
198}
199
200/* ARGSUSED */
201void
202random_yarrow_deinit(void)
203{
204	struct harvest *np;
205	enum esource e;
206
207	/* Deregister the randomness harvesting routine */
208	random_yarrow_deinit_harvester();
209
210	/*
211	 * Command the hash/reseed thread to end and wait for it to finish
212	 */
213	random_kthread_control = -1;
214	tsleep((void *)&random_kthread_control, 0, "term", 0);
215
216	/* Destroy the harvest fifos */
217	while (!STAILQ_EMPTY(&emptyfifo.head)) {
218		np = STAILQ_FIRST(&emptyfifo.head);
219		STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
220		free(np, M_ENTROPY);
221	}
222	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
223		while (!STAILQ_EMPTY(&harvestfifo[e].head)) {
224			np = STAILQ_FIRST(&harvestfifo[e].head);
225			STAILQ_REMOVE_HEAD(&harvestfifo[e].head, next);
226			free(np, M_ENTROPY);
227		}
228	}
229
230	random_yarrow_deinit_alg();
231
232	mtx_destroy(&harvest_mtx);
233
234	sysctl_ctx_free(&random_clist);
235}
236
237/* ARGSUSED */
238static void
239random_kthread(void *arg __unused)
240{
241	STAILQ_HEAD(, harvest) local_queue;
242	struct harvest *event = NULL;
243	int active, local_count;
244	enum esource source;
245
246	STAILQ_INIT(&local_queue);
247	local_count = 0;
248
249	/* Process until told to stop */
250	for (; random_kthread_control == 0;) {
251
252		active = 0;
253
254		/* Cycle through all the entropy sources */
255		mtx_lock_spin(&harvest_mtx);
256		for (source = RANDOM_START; source < ENTROPYSOURCE; source++) {
257			/*
258			 * Drain entropy source records into a thread-local
259			 * queue for processing while not holding the mutex.
260			 */
261			STAILQ_CONCAT(&local_queue, &harvestfifo[source].head);
262			local_count += harvestfifo[source].count;
263			harvestfifo[source].count = 0;
264		}
265
266		/*
267		 * Deal with events, if any, dropping the mutex as we process
268		 * each event.  Then push the events back into the empty
269		 * fifo.
270		 */
271		if (!STAILQ_EMPTY(&local_queue)) {
272			mtx_unlock_spin(&harvest_mtx);
273			STAILQ_FOREACH(event, &local_queue, next)
274				random_process_event(event);
275			mtx_lock_spin(&harvest_mtx);
276			STAILQ_CONCAT(&emptyfifo.head, &local_queue);
277			emptyfifo.count += local_count;
278			local_count = 0;
279		}
280		mtx_unlock_spin(&harvest_mtx);
281
282		KASSERT(local_count == 0, ("random_kthread: local_count %d",
283		    local_count));
284
285		/* Found nothing, so don't belabour the issue */
286		if (!active)
287			pause("-", hz / 10);
288
289	}
290
291	random_set_wakeup_exit(&random_kthread_control);
292	/* NOTREACHED */
293}
294
295/* Entropy harvesting routine. This is supposed to be fast; do
296 * not do anything slow in here!
297 */
298static void
299random_harvest_internal(u_int64_t somecounter, const void *entropy,
300    u_int count, u_int bits, u_int frac, enum esource origin)
301{
302	struct harvest *event;
303
304	KASSERT(origin == RANDOM_START || origin == RANDOM_WRITE ||
305            origin == RANDOM_KEYBOARD || origin == RANDOM_MOUSE ||
306            origin == RANDOM_NET || origin == RANDOM_INTERRUPT ||
307            origin == RANDOM_PURE,
308	    ("random_harvest_internal: origin %d invalid\n", origin));
309
310	/* Lockless read to avoid lock operations if fifo is full. */
311	if (harvestfifo[origin].count >= RANDOM_FIFO_MAX)
312		return;
313
314	mtx_lock_spin(&harvest_mtx);
315
316	/*
317	 * Don't make the harvest queues too big - help to prevent low-grade
318	 * entropy swamping
319	 */
320	if (harvestfifo[origin].count < RANDOM_FIFO_MAX) {
321		event = STAILQ_FIRST(&emptyfifo.head);
322		if (event != NULL) {
323			/* Add the harvested data to the fifo */
324			STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
325			harvestfifo[origin].count++;
326			event->somecounter = somecounter;
327			event->size = count;
328			event->bits = bits;
329			event->frac = frac;
330			event->source = origin;
331
332			/* XXXX Come back and make this dynamic! */
333			count = MIN(count, HARVESTSIZE);
334			memcpy(event->entropy, entropy, count);
335
336			STAILQ_INSERT_TAIL(&harvestfifo[origin].head,
337			    event, next);
338		}
339	}
340	mtx_unlock_spin(&harvest_mtx);
341}
342
343void
344random_yarrow_write(void *buf, int count)
345{
346	int i;
347	u_int chunk;
348
349	/*
350	 * Break the input up into HARVESTSIZE chunks. The writer has too
351	 * much control here, so "estimate" the the entropy as zero.
352	 */
353	for (i = 0; i < count; i += HARVESTSIZE) {
354		chunk = HARVESTSIZE;
355		if (i + chunk >= count)
356			chunk = (u_int)(count - i);
357		random_harvest_internal(get_cyclecount(), (char *)buf + i,
358		    chunk, 0, 0, RANDOM_WRITE);
359	}
360}
361
362void
363random_yarrow_unblock(void)
364{
365	if (!random_systat.seeded) {
366		random_systat.seeded = 1;
367		selwakeuppri(&random_systat.rsel, PUSER);
368		wakeup(&random_systat);
369	}
370}
371
372static int
373random_yarrow_poll(int events, struct thread *td)
374{
375	int revents = 0;
376	mtx_lock(&random_reseed_mtx);
377
378	if (random_systat.seeded)
379		revents = events & (POLLIN | POLLRDNORM);
380	else
381		selrecord(td, &random_systat.rsel);
382
383	mtx_unlock(&random_reseed_mtx);
384	return revents;
385}
386
387static int
388random_yarrow_block(int flag)
389{
390	int error = 0;
391
392	mtx_lock(&random_reseed_mtx);
393
394	/* Blocking logic */
395	while (random_systat.seeded && !error) {
396		if (flag & O_NONBLOCK)
397			error = EWOULDBLOCK;
398		else {
399			printf("Entropy device is blocking.\n");
400			error = msleep(&random_systat,
401			    &random_reseed_mtx,
402			    PUSER | PCATCH, "block", 0);
403		}
404	}
405	mtx_unlock(&random_reseed_mtx);
406
407	return error;
408}
409