randomdev_soft.c revision 185254
1128059Smarkm/*-
2128059Smarkm * Copyright (c) 2000-2004 Mark R V Murray
3136338Srwatson * Copyright (c) 2004 Robert N. M. Watson
4128059Smarkm * All rights reserved.
5128059Smarkm *
6128059Smarkm * Redistribution and use in source and binary forms, with or without
7128059Smarkm * modification, are permitted provided that the following conditions
8128059Smarkm * are met:
9128059Smarkm * 1. Redistributions of source code must retain the above copyright
10128059Smarkm *    notice, this list of conditions and the following disclaimer
11128059Smarkm *    in this position and unchanged.
12128059Smarkm * 2. Redistributions in binary form must reproduce the above copyright
13128059Smarkm *    notice, this list of conditions and the following disclaimer in the
14128059Smarkm *    documentation and/or other materials provided with the distribution.
15128059Smarkm *
16128059Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17128059Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18128059Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19128059Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20128059Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21128059Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22128059Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23128059Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24128059Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25128059Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26128059Smarkm *
27128059Smarkm */
28128059Smarkm
29128059Smarkm#include <sys/cdefs.h>
30128059Smarkm__FBSDID("$FreeBSD: head/sys/dev/random/randomdev_soft.c 185254 2008-11-24 17:39:39Z cperciva $");
31128059Smarkm
32128059Smarkm#include <sys/param.h>
33128059Smarkm#include <sys/systm.h>
34128059Smarkm#include <sys/bus.h>
35128059Smarkm#include <sys/conf.h>
36128059Smarkm#include <sys/fcntl.h>
37128059Smarkm#include <sys/kernel.h>
38128059Smarkm#include <sys/kthread.h>
39128059Smarkm#include <sys/lock.h>
40128059Smarkm#include <sys/malloc.h>
41128059Smarkm#include <sys/mutex.h>
42128059Smarkm#include <sys/poll.h>
43128059Smarkm#include <sys/proc.h>
44128059Smarkm#include <sys/random.h>
45128059Smarkm#include <sys/selinfo.h>
46128059Smarkm#include <sys/sysctl.h>
47128059Smarkm#include <sys/uio.h>
48128059Smarkm#include <sys/unistd.h>
49128059Smarkm
50128059Smarkm#include <machine/bus.h>
51128059Smarkm#include <machine/cpu.h>
52128059Smarkm
53128059Smarkm#include <dev/random/randomdev.h>
54128059Smarkm#include <dev/random/randomdev_soft.h>
55128059Smarkm
56128059Smarkm#define RANDOM_FIFO_MAX	256	/* How many events to queue up */
57128059Smarkm
58128059Smarkmstatic void random_kthread(void *);
59128059Smarkmstatic void
60128059Smarkmrandom_harvest_internal(u_int64_t, const void *, u_int,
61128059Smarkm    u_int, u_int, enum esource);
62153575Spsstatic int random_yarrow_poll(int event,struct thread *td);
63153575Spsstatic int random_yarrow_block(int flag);
64185254Scpercivastatic void random_yarrow_flush_reseed(void);
65128059Smarkm
66128059Smarkmstruct random_systat random_yarrow = {
67128059Smarkm	.ident = "Software, Yarrow",
68128059Smarkm	.init = random_yarrow_init,
69128059Smarkm	.deinit = random_yarrow_deinit,
70153575Sps	.block = random_yarrow_block,
71128059Smarkm	.read = random_yarrow_read,
72128059Smarkm	.write = random_yarrow_write,
73153575Sps	.poll = random_yarrow_poll,
74185254Scperciva	.reseed = random_yarrow_flush_reseed,
75132346Smarkm	.seeded = 1,
76128059Smarkm};
77128059Smarkm
78128059SmarkmMALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers");
79128059Smarkm
80136338Srwatson/*
81136338Srwatson * The harvest mutex protects the consistency of the entropy fifos and
82136338Srwatson * empty fifo.
83136338Srwatson */
84136338Srwatsonstruct mtx	harvest_mtx;
85136338Srwatson
86128059Smarkm/* Lockable FIFO queue holding entropy buffers */
87128059Smarkmstruct entropyfifo {
88128059Smarkm	int count;
89128059Smarkm	STAILQ_HEAD(harvestlist, harvest) head;
90128059Smarkm};
91128059Smarkm
92128059Smarkm/* Empty entropy buffers */
93128059Smarkmstatic struct entropyfifo emptyfifo;
94128059Smarkm
95128059Smarkm#define EMPTYBUFFERS	1024
96128059Smarkm
97128059Smarkm/* Harvested entropy */
98144291Smarkmstatic struct entropyfifo harvestfifo[ENTROPYSOURCE];
99128059Smarkm
100185254Scperciva/* <0 to end the kthread, 0 to let it run, 1 to flush the harvest queues */
101128059Smarkmstatic int random_kthread_control = 0;
102128059Smarkm
103128059Smarkmstatic struct proc *random_kthread_proc;
104128059Smarkm
105128059Smarkm/* List for the dynamic sysctls */
106128059Smarkmstruct sysctl_ctx_list random_clist;
107128059Smarkm
108128059Smarkm/* ARGSUSED */
109128059Smarkmstatic int
110128059Smarkmrandom_check_boolean(SYSCTL_HANDLER_ARGS)
111128059Smarkm{
112128059Smarkm	if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0)
113128059Smarkm		*(u_int *)(oidp->oid_arg1) = 1;
114128059Smarkm	return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
115128059Smarkm}
116128059Smarkm
117128059Smarkm/* ARGSUSED */
118128059Smarkmvoid
119128059Smarkmrandom_yarrow_init(void)
120128059Smarkm{
121128059Smarkm	int error, i;
122128059Smarkm	struct harvest *np;
123170067Srwatson	struct sysctl_oid *random_o, *random_sys_o, *random_sys_harvest_o;
124128059Smarkm	enum esource e;
125128059Smarkm
126170067Srwatson	random_o = SYSCTL_ADD_NODE(&random_clist,
127128059Smarkm	    SYSCTL_STATIC_CHILDREN(_kern),
128128059Smarkm	    OID_AUTO, "random", CTLFLAG_RW, 0,
129128059Smarkm	    "Software Random Number Generator");
130128059Smarkm
131128059Smarkm	random_yarrow_init_alg(&random_clist, random_o);
132128059Smarkm
133170067Srwatson	random_sys_o = SYSCTL_ADD_NODE(&random_clist,
134128059Smarkm	    SYSCTL_CHILDREN(random_o),
135128059Smarkm	    OID_AUTO, "sys", CTLFLAG_RW, 0,
136128059Smarkm	    "Entropy Device Parameters");
137128059Smarkm
138170067Srwatson	SYSCTL_ADD_PROC(&random_clist,
139128059Smarkm	    SYSCTL_CHILDREN(random_sys_o),
140128059Smarkm	    OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW,
141132346Smarkm	    &random_systat.seeded, 1, random_check_boolean, "I",
142128059Smarkm	    "Seeded State");
143128059Smarkm
144170067Srwatson	random_sys_harvest_o = SYSCTL_ADD_NODE(&random_clist,
145128059Smarkm	    SYSCTL_CHILDREN(random_sys_o),
146128059Smarkm	    OID_AUTO, "harvest", CTLFLAG_RW, 0,
147128059Smarkm	    "Entropy Sources");
148128059Smarkm
149170067Srwatson	SYSCTL_ADD_PROC(&random_clist,
150128059Smarkm	    SYSCTL_CHILDREN(random_sys_harvest_o),
151128059Smarkm	    OID_AUTO, "ethernet", CTLTYPE_INT | CTLFLAG_RW,
152128320Smarkm	    &harvest.ethernet, 1, random_check_boolean, "I",
153128059Smarkm	    "Harvest NIC entropy");
154170067Srwatson	SYSCTL_ADD_PROC(&random_clist,
155128059Smarkm	    SYSCTL_CHILDREN(random_sys_harvest_o),
156128059Smarkm	    OID_AUTO, "point_to_point", CTLTYPE_INT | CTLFLAG_RW,
157128320Smarkm	    &harvest.point_to_point, 1, random_check_boolean, "I",
158128059Smarkm	    "Harvest serial net entropy");
159170067Srwatson	SYSCTL_ADD_PROC(&random_clist,
160128059Smarkm	    SYSCTL_CHILDREN(random_sys_harvest_o),
161128059Smarkm	    OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW,
162128320Smarkm	    &harvest.interrupt, 1, random_check_boolean, "I",
163128059Smarkm	    "Harvest IRQ entropy");
164170067Srwatson	SYSCTL_ADD_PROC(&random_clist,
165128059Smarkm	    SYSCTL_CHILDREN(random_sys_harvest_o),
166128059Smarkm	    OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW,
167128059Smarkm	    &harvest.swi, 0, random_check_boolean, "I",
168128059Smarkm	    "Harvest SWI entropy");
169128059Smarkm
170128059Smarkm	/* Initialise the harvest fifos */
171128059Smarkm	STAILQ_INIT(&emptyfifo.head);
172128059Smarkm	emptyfifo.count = 0;
173128059Smarkm	for (i = 0; i < EMPTYBUFFERS; i++) {
174128059Smarkm		np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK);
175128059Smarkm		STAILQ_INSERT_TAIL(&emptyfifo.head, np, next);
176128059Smarkm	}
177128059Smarkm	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
178128059Smarkm		STAILQ_INIT(&harvestfifo[e].head);
179128059Smarkm		harvestfifo[e].count = 0;
180128059Smarkm	}
181128059Smarkm
182136338Srwatson	mtx_init(&harvest_mtx, "entropy harvest mutex", NULL, MTX_SPIN);
183136338Srwatson
184128059Smarkm	/* Start the hash/reseed thread */
185172836Sjulian	error = kproc_create(random_kthread, NULL,
186128059Smarkm	    &random_kthread_proc, RFHIGHPID, 0, "yarrow");
187128059Smarkm	if (error != 0)
188128059Smarkm		panic("Cannot create entropy maintenance thread.");
189128059Smarkm
190128059Smarkm	/* Register the randomness harvesting routine */
191128059Smarkm	random_yarrow_init_harvester(random_harvest_internal,
192128059Smarkm	    random_yarrow_read);
193128059Smarkm}
194128059Smarkm
195128059Smarkm/* ARGSUSED */
196128059Smarkmvoid
197128059Smarkmrandom_yarrow_deinit(void)
198128059Smarkm{
199128059Smarkm	struct harvest *np;
200128059Smarkm	enum esource e;
201128059Smarkm
202128059Smarkm	/* Deregister the randomness harvesting routine */
203128059Smarkm	random_yarrow_deinit_harvester();
204128059Smarkm
205128059Smarkm	/*
206128059Smarkm	 * Command the hash/reseed thread to end and wait for it to finish
207128059Smarkm	 */
208128059Smarkm	random_kthread_control = -1;
209157815Sjhb	tsleep((void *)&random_kthread_control, 0, "term", 0);
210128059Smarkm
211128059Smarkm	/* Destroy the harvest fifos */
212128059Smarkm	while (!STAILQ_EMPTY(&emptyfifo.head)) {
213128059Smarkm		np = STAILQ_FIRST(&emptyfifo.head);
214128059Smarkm		STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
215128059Smarkm		free(np, M_ENTROPY);
216128059Smarkm	}
217128059Smarkm	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
218128059Smarkm		while (!STAILQ_EMPTY(&harvestfifo[e].head)) {
219128059Smarkm			np = STAILQ_FIRST(&harvestfifo[e].head);
220128059Smarkm			STAILQ_REMOVE_HEAD(&harvestfifo[e].head, next);
221128059Smarkm			free(np, M_ENTROPY);
222128059Smarkm		}
223128059Smarkm	}
224128059Smarkm
225128059Smarkm	random_yarrow_deinit_alg();
226128059Smarkm
227136338Srwatson	mtx_destroy(&harvest_mtx);
228136338Srwatson
229128059Smarkm	sysctl_ctx_free(&random_clist);
230128059Smarkm}
231128059Smarkm
232128059Smarkm/* ARGSUSED */
233128059Smarkmstatic void
234128059Smarkmrandom_kthread(void *arg __unused)
235128059Smarkm{
236136338Srwatson	STAILQ_HEAD(, harvest) local_queue;
237128059Smarkm	struct harvest *event = NULL;
238137152Srwatson	int active, local_count;
239128059Smarkm	enum esource source;
240128059Smarkm
241136338Srwatson	STAILQ_INIT(&local_queue);
242137152Srwatson	local_count = 0;
243136338Srwatson
244128059Smarkm	/* Process until told to stop */
245185254Scperciva	for (; random_kthread_control >= 0;) {
246128059Smarkm
247128059Smarkm		active = 0;
248128059Smarkm
249128059Smarkm		/* Cycle through all the entropy sources */
250136338Srwatson		mtx_lock_spin(&harvest_mtx);
251128059Smarkm		for (source = RANDOM_START; source < ENTROPYSOURCE; source++) {
252136338Srwatson			/*
253136338Srwatson			 * Drain entropy source records into a thread-local
254136338Srwatson			 * queue for processing while not holding the mutex.
255136338Srwatson			 */
256137152Srwatson			STAILQ_CONCAT(&local_queue, &harvestfifo[source].head);
257137152Srwatson			local_count += harvestfifo[source].count;
258137152Srwatson			harvestfifo[source].count = 0;
259136338Srwatson		}
260128059Smarkm
261136338Srwatson		/*
262136338Srwatson		 * Deal with events, if any, dropping the mutex as we process
263136338Srwatson		 * each event.  Then push the events back into the empty
264136338Srwatson		 * fifo.
265136338Srwatson		 */
266136338Srwatson		if (!STAILQ_EMPTY(&local_queue)) {
267136338Srwatson			mtx_unlock_spin(&harvest_mtx);
268136338Srwatson			STAILQ_FOREACH(event, &local_queue, next)
269128059Smarkm				random_process_event(event);
270136338Srwatson			mtx_lock_spin(&harvest_mtx);
271137152Srwatson			STAILQ_CONCAT(&emptyfifo.head, &local_queue);
272137152Srwatson			emptyfifo.count += local_count;
273137152Srwatson			local_count = 0;
274128059Smarkm		}
275136338Srwatson		mtx_unlock_spin(&harvest_mtx);
276128059Smarkm
277137152Srwatson		KASSERT(local_count == 0, ("random_kthread: local_count %d",
278137152Srwatson		    local_count));
279137152Srwatson
280185254Scperciva		/*
281185254Scperciva		 * If a queue flush was commanded, it has now happened,
282185254Scperciva		 * and we can mark this by resetting the command.
283185254Scperciva		 */
284185254Scperciva		if (random_kthread_control == 1)
285185254Scperciva			random_kthread_control = 0;
286185254Scperciva
287128059Smarkm		/* Found nothing, so don't belabour the issue */
288128059Smarkm		if (!active)
289167086Sjhb			pause("-", hz / 10);
290128059Smarkm
291128059Smarkm	}
292128059Smarkm
293128059Smarkm	random_set_wakeup_exit(&random_kthread_control);
294128059Smarkm	/* NOTREACHED */
295128059Smarkm}
296128059Smarkm
297128059Smarkm/* Entropy harvesting routine. This is supposed to be fast; do
298128059Smarkm * not do anything slow in here!
299128059Smarkm */
300128059Smarkmstatic void
301128059Smarkmrandom_harvest_internal(u_int64_t somecounter, const void *entropy,
302128059Smarkm    u_int count, u_int bits, u_int frac, enum esource origin)
303128059Smarkm{
304128059Smarkm	struct harvest *event;
305128059Smarkm
306136434Srwatson	KASSERT(origin == RANDOM_START || origin == RANDOM_WRITE ||
307136434Srwatson            origin == RANDOM_KEYBOARD || origin == RANDOM_MOUSE ||
308136434Srwatson            origin == RANDOM_NET || origin == RANDOM_INTERRUPT ||
309144291Smarkm            origin == RANDOM_PURE,
310136434Srwatson	    ("random_harvest_internal: origin %d invalid\n", origin));
311136434Srwatson
312133465Srwatson	/* Lockless read to avoid lock operations if fifo is full. */
313133465Srwatson	if (harvestfifo[origin].count >= RANDOM_FIFO_MAX)
314133465Srwatson		return;
315133465Srwatson
316136338Srwatson	mtx_lock_spin(&harvest_mtx);
317128059Smarkm
318128059Smarkm	/*
319128059Smarkm	 * Don't make the harvest queues too big - help to prevent low-grade
320128059Smarkm	 * entropy swamping
321128059Smarkm	 */
322128059Smarkm	if (harvestfifo[origin].count < RANDOM_FIFO_MAX) {
323136338Srwatson		event = STAILQ_FIRST(&emptyfifo.head);
324136338Srwatson		if (event != NULL) {
325136338Srwatson			/* Add the harvested data to the fifo */
326128059Smarkm			STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
327128059Smarkm			harvestfifo[origin].count++;
328128059Smarkm			event->somecounter = somecounter;
329128059Smarkm			event->size = count;
330128059Smarkm			event->bits = bits;
331128059Smarkm			event->frac = frac;
332128059Smarkm			event->source = origin;
333128059Smarkm
334128059Smarkm			/* XXXX Come back and make this dynamic! */
335128059Smarkm			count = MIN(count, HARVESTSIZE);
336128059Smarkm			memcpy(event->entropy, entropy, count);
337128059Smarkm
338128059Smarkm			STAILQ_INSERT_TAIL(&harvestfifo[origin].head,
339128059Smarkm			    event, next);
340128059Smarkm		}
341128059Smarkm	}
342136338Srwatson	mtx_unlock_spin(&harvest_mtx);
343128059Smarkm}
344128059Smarkm
345128059Smarkmvoid
346128059Smarkmrandom_yarrow_write(void *buf, int count)
347128059Smarkm{
348128059Smarkm	int i;
349128059Smarkm	u_int chunk;
350128059Smarkm
351128059Smarkm	/*
352128059Smarkm	 * Break the input up into HARVESTSIZE chunks. The writer has too
353128059Smarkm	 * much control here, so "estimate" the the entropy as zero.
354128059Smarkm	 */
355128059Smarkm	for (i = 0; i < count; i += HARVESTSIZE) {
356128059Smarkm		chunk = HARVESTSIZE;
357128059Smarkm		if (i + chunk >= count)
358128059Smarkm			chunk = (u_int)(count - i);
359128059Smarkm		random_harvest_internal(get_cyclecount(), (char *)buf + i,
360128059Smarkm		    chunk, 0, 0, RANDOM_WRITE);
361128059Smarkm	}
362128059Smarkm}
363128059Smarkm
364128059Smarkmvoid
365128059Smarkmrandom_yarrow_unblock(void)
366128059Smarkm{
367128059Smarkm	if (!random_systat.seeded) {
368128059Smarkm		random_systat.seeded = 1;
369128059Smarkm		selwakeuppri(&random_systat.rsel, PUSER);
370128059Smarkm		wakeup(&random_systat);
371128059Smarkm	}
372128059Smarkm}
373153575Sps
374153575Spsstatic int
375153575Spsrandom_yarrow_poll(int events, struct thread *td)
376153575Sps{
377153575Sps	int revents = 0;
378153575Sps	mtx_lock(&random_reseed_mtx);
379153575Sps
380153575Sps	if (random_systat.seeded)
381153575Sps		revents = events & (POLLIN | POLLRDNORM);
382153575Sps	else
383153575Sps		selrecord(td, &random_systat.rsel);
384153575Sps
385153575Sps	mtx_unlock(&random_reseed_mtx);
386153575Sps	return revents;
387153575Sps}
388153575Sps
389153575Spsstatic int
390153575Spsrandom_yarrow_block(int flag)
391153575Sps{
392153575Sps	int error = 0;
393153575Sps
394153575Sps	mtx_lock(&random_reseed_mtx);
395153575Sps
396153575Sps	/* Blocking logic */
397153575Sps	while (random_systat.seeded && !error) {
398153575Sps		if (flag & O_NONBLOCK)
399153575Sps			error = EWOULDBLOCK;
400153575Sps		else {
401153575Sps			printf("Entropy device is blocking.\n");
402153575Sps			error = msleep(&random_systat,
403153575Sps			    &random_reseed_mtx,
404153575Sps			    PUSER | PCATCH, "block", 0);
405153575Sps		}
406153575Sps	}
407153575Sps	mtx_unlock(&random_reseed_mtx);
408153575Sps
409153575Sps	return error;
410153575Sps}
411185254Scperciva
412185254Scperciva/* Helper routine to perform explicit reseeds */
413185254Scpercivastatic void
414185254Scpercivarandom_yarrow_flush_reseed(void)
415185254Scperciva{
416185254Scperciva	/* Command a entropy queue flush and wait for it to finish */
417185254Scperciva	random_kthread_control = 1;
418185254Scperciva	while (random_kthread_control)
419185254Scperciva		pause("-", hz / 10);
420185254Scperciva
421185254Scperciva	random_yarrow_reseed();
422185254Scperciva}
423