randomdev_soft.c revision 192774
1128059Smarkm/*-
2192774Smarkm * Copyright (c) 2000-2009 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 192774 2009-05-25 22:50:11Z markm $");
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 *);
59192774Smarkmstatic 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;
238192774Smarkm	int 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		/* Cycle through all the entropy sources */
248136338Srwatson		mtx_lock_spin(&harvest_mtx);
249128059Smarkm		for (source = RANDOM_START; source < ENTROPYSOURCE; source++) {
250136338Srwatson			/*
251136338Srwatson			 * Drain entropy source records into a thread-local
252136338Srwatson			 * queue for processing while not holding the mutex.
253136338Srwatson			 */
254137152Srwatson			STAILQ_CONCAT(&local_queue, &harvestfifo[source].head);
255137152Srwatson			local_count += harvestfifo[source].count;
256137152Srwatson			harvestfifo[source].count = 0;
257136338Srwatson		}
258128059Smarkm
259136338Srwatson		/*
260136338Srwatson		 * Deal with events, if any, dropping the mutex as we process
261136338Srwatson		 * each event.  Then push the events back into the empty
262136338Srwatson		 * fifo.
263136338Srwatson		 */
264136338Srwatson		if (!STAILQ_EMPTY(&local_queue)) {
265136338Srwatson			mtx_unlock_spin(&harvest_mtx);
266136338Srwatson			STAILQ_FOREACH(event, &local_queue, next)
267128059Smarkm				random_process_event(event);
268136338Srwatson			mtx_lock_spin(&harvest_mtx);
269137152Srwatson			STAILQ_CONCAT(&emptyfifo.head, &local_queue);
270137152Srwatson			emptyfifo.count += local_count;
271137152Srwatson			local_count = 0;
272128059Smarkm		}
273136338Srwatson		mtx_unlock_spin(&harvest_mtx);
274128059Smarkm
275137152Srwatson		KASSERT(local_count == 0, ("random_kthread: local_count %d",
276137152Srwatson		    local_count));
277137152Srwatson
278185254Scperciva		/*
279185254Scperciva		 * If a queue flush was commanded, it has now happened,
280185254Scperciva		 * and we can mark this by resetting the command.
281185254Scperciva		 */
282185254Scperciva		if (random_kthread_control == 1)
283185254Scperciva			random_kthread_control = 0;
284185254Scperciva
285192774Smarkm		/* Work done, so don't belabour the issue */
286192774Smarkm		pause("-", hz / 10);
287128059Smarkm
288128059Smarkm	}
289128059Smarkm
290128059Smarkm	random_set_wakeup_exit(&random_kthread_control);
291128059Smarkm	/* NOTREACHED */
292128059Smarkm}
293128059Smarkm
294128059Smarkm/* Entropy harvesting routine. This is supposed to be fast; do
295128059Smarkm * not do anything slow in here!
296128059Smarkm */
297128059Smarkmstatic void
298128059Smarkmrandom_harvest_internal(u_int64_t somecounter, const void *entropy,
299128059Smarkm    u_int count, u_int bits, u_int frac, enum esource origin)
300128059Smarkm{
301128059Smarkm	struct harvest *event;
302128059Smarkm
303136434Srwatson	KASSERT(origin == RANDOM_START || origin == RANDOM_WRITE ||
304136434Srwatson            origin == RANDOM_KEYBOARD || origin == RANDOM_MOUSE ||
305136434Srwatson            origin == RANDOM_NET || origin == RANDOM_INTERRUPT ||
306144291Smarkm            origin == RANDOM_PURE,
307136434Srwatson	    ("random_harvest_internal: origin %d invalid\n", origin));
308136434Srwatson
309133465Srwatson	/* Lockless read to avoid lock operations if fifo is full. */
310133465Srwatson	if (harvestfifo[origin].count >= RANDOM_FIFO_MAX)
311133465Srwatson		return;
312133465Srwatson
313136338Srwatson	mtx_lock_spin(&harvest_mtx);
314128059Smarkm
315128059Smarkm	/*
316128059Smarkm	 * Don't make the harvest queues too big - help to prevent low-grade
317128059Smarkm	 * entropy swamping
318128059Smarkm	 */
319128059Smarkm	if (harvestfifo[origin].count < RANDOM_FIFO_MAX) {
320136338Srwatson		event = STAILQ_FIRST(&emptyfifo.head);
321136338Srwatson		if (event != NULL) {
322136338Srwatson			/* Add the harvested data to the fifo */
323128059Smarkm			STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
324128059Smarkm			harvestfifo[origin].count++;
325128059Smarkm			event->somecounter = somecounter;
326128059Smarkm			event->size = count;
327128059Smarkm			event->bits = bits;
328128059Smarkm			event->frac = frac;
329128059Smarkm			event->source = origin;
330128059Smarkm
331128059Smarkm			/* XXXX Come back and make this dynamic! */
332128059Smarkm			count = MIN(count, HARVESTSIZE);
333128059Smarkm			memcpy(event->entropy, entropy, count);
334128059Smarkm
335128059Smarkm			STAILQ_INSERT_TAIL(&harvestfifo[origin].head,
336128059Smarkm			    event, next);
337128059Smarkm		}
338128059Smarkm	}
339136338Srwatson	mtx_unlock_spin(&harvest_mtx);
340128059Smarkm}
341128059Smarkm
342128059Smarkmvoid
343128059Smarkmrandom_yarrow_write(void *buf, int count)
344128059Smarkm{
345128059Smarkm	int i;
346128059Smarkm	u_int chunk;
347128059Smarkm
348128059Smarkm	/*
349128059Smarkm	 * Break the input up into HARVESTSIZE chunks. The writer has too
350128059Smarkm	 * much control here, so "estimate" the the entropy as zero.
351128059Smarkm	 */
352128059Smarkm	for (i = 0; i < count; i += HARVESTSIZE) {
353128059Smarkm		chunk = HARVESTSIZE;
354128059Smarkm		if (i + chunk >= count)
355128059Smarkm			chunk = (u_int)(count - i);
356128059Smarkm		random_harvest_internal(get_cyclecount(), (char *)buf + i,
357128059Smarkm		    chunk, 0, 0, RANDOM_WRITE);
358128059Smarkm	}
359128059Smarkm}
360128059Smarkm
361128059Smarkmvoid
362128059Smarkmrandom_yarrow_unblock(void)
363128059Smarkm{
364128059Smarkm	if (!random_systat.seeded) {
365128059Smarkm		random_systat.seeded = 1;
366128059Smarkm		selwakeuppri(&random_systat.rsel, PUSER);
367128059Smarkm		wakeup(&random_systat);
368128059Smarkm	}
369128059Smarkm}
370153575Sps
371153575Spsstatic int
372153575Spsrandom_yarrow_poll(int events, struct thread *td)
373153575Sps{
374153575Sps	int revents = 0;
375153575Sps	mtx_lock(&random_reseed_mtx);
376153575Sps
377153575Sps	if (random_systat.seeded)
378153575Sps		revents = events & (POLLIN | POLLRDNORM);
379153575Sps	else
380153575Sps		selrecord(td, &random_systat.rsel);
381192774Smarkm
382153575Sps	mtx_unlock(&random_reseed_mtx);
383153575Sps	return revents;
384153575Sps}
385153575Sps
386153575Spsstatic int
387153575Spsrandom_yarrow_block(int flag)
388153575Sps{
389153575Sps	int error = 0;
390153575Sps
391153575Sps	mtx_lock(&random_reseed_mtx);
392153575Sps
393153575Sps	/* Blocking logic */
394153575Sps	while (random_systat.seeded && !error) {
395153575Sps		if (flag & O_NONBLOCK)
396153575Sps			error = EWOULDBLOCK;
397153575Sps		else {
398153575Sps			printf("Entropy device is blocking.\n");
399153575Sps			error = msleep(&random_systat,
400153575Sps			    &random_reseed_mtx,
401153575Sps			    PUSER | PCATCH, "block", 0);
402153575Sps		}
403153575Sps	}
404153575Sps	mtx_unlock(&random_reseed_mtx);
405153575Sps
406153575Sps	return error;
407192774Smarkm}
408185254Scperciva
409185254Scperciva/* Helper routine to perform explicit reseeds */
410185254Scpercivastatic void
411185254Scpercivarandom_yarrow_flush_reseed(void)
412185254Scperciva{
413185254Scperciva	/* Command a entropy queue flush and wait for it to finish */
414185254Scperciva	random_kthread_control = 1;
415185254Scperciva	while (random_kthread_control)
416185254Scperciva		pause("-", hz / 10);
417185254Scperciva
418185254Scperciva	random_yarrow_reseed();
419185254Scperciva}
420