2 * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com>
3 * Copyright (c) 2013 Mark R V Murray
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/param.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/kernel.h>
32 #include <sys/libkern.h>
34 #include <sys/malloc.h>
35 #include <sys/queue.h>
36 #include <sys/random.h>
37 #include <sys/selinfo.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
41 #include <sys/unistd.h>
43 #include <machine/cpu.h>
45 #include <dev/random/randomdev.h>
46 #include <dev/random/randomdev_soft.h>
47 #include <dev/random/random_adaptors.h>
48 #include <dev/random/random_harvestq.h>
50 #include "live_entropy_sources.h"
52 LIST_HEAD(les_head, live_entropy_sources);
53 static struct les_head sources = LIST_HEAD_INITIALIZER(sources);
56 * The live_lock protects the consistency of the "struct les_head sources"
58 static struct sx les_lock; /* need a sleepable lock */
61 live_entropy_source_register(struct random_hardware_source *rsource)
63 struct live_entropy_sources *les;
65 KASSERT(rsource != NULL, ("invalid input to %s", __func__));
67 les = malloc(sizeof(struct live_entropy_sources), M_ENTROPY, M_WAITOK);
68 les->rsource = rsource;
71 LIST_INSERT_HEAD(&sources, les, entries);
72 sx_xunlock(&les_lock);
76 live_entropy_source_deregister(struct random_hardware_source *rsource)
78 struct live_entropy_sources *les = NULL;
80 KASSERT(rsource != NULL, ("invalid input to %s", __func__));
83 LIST_FOREACH(les, &sources, entries)
84 if (les->rsource == rsource) {
85 LIST_REMOVE(les, entries);
88 sx_xunlock(&les_lock);
94 live_entropy_source_handler(SYSCTL_HANDLER_ARGS)
96 struct live_entropy_sources *les;
103 if (LIST_EMPTY(&sources))
104 error = SYSCTL_OUT(req, "", 0);
106 LIST_FOREACH(les, &sources, entries) {
108 error = SYSCTL_OUT(req, ",", count++ ? 1 : 0);
112 error = SYSCTL_OUT(req, les->rsource->ident, strlen(les->rsource->ident));
118 sx_sunlock(&les_lock);
124 live_entropy_sources_init(void *unused)
127 SYSCTL_PROC(_kern_random, OID_AUTO, live_entropy_sources,
128 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
129 NULL, 0, live_entropy_source_handler, "",
130 "List of Active Live Entropy Sources");
132 sx_init(&les_lock, "live_entropy_sources");
136 * Run through all "live" sources reading entropy for the given
137 * number of rounds, which should be a multiple of the number
138 * of entropy accumulation pools in use; 2 for Yarrow and 32
142 * This function runs inside the RNG thread! Don't do anything silly!
143 * Remember that we are NOT holding harvest_mtx on entry!
146 live_entropy_sources_feed(int rounds, event_proc_f entropy_processor)
148 static struct harvest event;
149 static uint8_t buf[HARVESTSIZE];
150 struct live_entropy_sources *les;
156 * Walk over all of live entropy sources, and feed their output
157 * to the system-wide RNG.
159 LIST_FOREACH(les, &sources, entries) {
161 for (i = 0; i < rounds; i++) {
163 * This should be quick, since it's a live entropy
166 /* FIXME: Whine loudly if this didn't work. */
167 n = les->rsource->read(buf, sizeof(buf));
168 n = MIN(n, HARVESTSIZE);
170 event.somecounter = get_cyclecount();
172 event.bits = (n*8)/2;
173 event.source = les->rsource->source;
174 memcpy(event.entropy, buf, n);
176 /* Do the actual entropy insertion */
177 entropy_processor(&event);
182 sx_sunlock(&les_lock);
186 live_entropy_sources_deinit(void *unused)
189 sx_destroy(&les_lock);
192 SYSINIT(random_adaptors, SI_SUB_RANDOM, SI_ORDER_FIRST,
193 live_entropy_sources_init, NULL);
194 SYSUNINIT(random_adaptors, SI_SUB_RANDOM, SI_ORDER_FIRST,
195 live_entropy_sources_deinit, NULL);