2 * Copyright (c) 2000-2013 Mark R V Murray
3 * Copyright (c) 2013 Arthur Mesh
4 * Copyright (c) 2004 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_random.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/linker.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/random.h>
45 #include <sys/selinfo.h>
46 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
49 #include <machine/cpu.h>
50 #include <machine/vmparam.h>
52 #include <dev/random/randomdev.h>
53 #include <dev/random/randomdev_soft.h>
54 #include <dev/random/random_adaptors.h>
55 #include <dev/random/random_harvestq.h>
56 #include <dev/random/live_entropy_sources.h>
57 #include <dev/random/rwfile.h>
59 #define RANDOM_FIFO_MAX 1024 /* How many events to queue up */
62 * The harvest mutex protects the consistency of the entropy fifos and
63 * empty fifo and other associated structures.
65 struct mtx harvest_mtx;
67 /* Lockable FIFO queue holding entropy buffers */
70 STAILQ_HEAD(harvestlist, harvest) head;
73 /* Empty entropy buffers */
74 static struct entropyfifo emptyfifo;
76 /* Harvested entropy */
77 static struct entropyfifo harvestfifo;
79 /* <0 to end the kthread, 0 to let it run, 1 to flush the harvest queues */
80 int random_kthread_control = 0;
82 static struct proc *random_kthread_proc;
85 static const char *entropy_files[] = {
91 /* Deal with entropy cached externally if this is present.
92 * Lots of policy may eventually arrive in this function.
93 * Called after / is mounted.
96 random_harvestq_cache(void *arg __unused)
98 uint8_t *keyfile, *data;
101 const char **entropy_file;
106 /* Get stuff that may have been preloaded by loader(8) */
107 keyfile = preload_search_by_type("/boot/entropy");
108 if (keyfile != NULL) {
109 data = preload_fetch_addr(keyfile);
110 size = preload_fetch_size(keyfile);
111 if (data != NULL && size != 0) {
112 for (i = 0; i < size; i += 16)
113 random_harvestq_internal(get_cyclecount(), data + i, 16, 16, RANDOM_CACHED);
114 printf("random: read %zu bytes from preloaded cache\n", size);
118 printf("random: no preloaded entropy cache available\n");
122 /* Read and attempt to overwrite the entropy cache files.
123 * If the file exists, can be read and then overwritten,
124 * then use it. Ignore it otherwise, but print out what is
127 data = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK);
128 zbuf = __DECONST(void *, zero_region);
129 for (entropy_file = entropy_files; *entropy_file; entropy_file++) {
130 error = randomdev_read_file(*entropy_file, data, PAGE_SIZE);
132 printf("random: entropy cache '%s' provides %ld bytes\n", *entropy_file, (long)PAGE_SIZE);
133 error = randomdev_write_file(*entropy_file, zbuf, PAGE_SIZE);
135 printf("random: entropy cache '%s' contents used and successfully overwritten\n", *entropy_file);
136 for (i = 0; i < PAGE_SIZE; i += 16)
137 random_harvestq_internal(get_cyclecount(), data + i, 16, 16, RANDOM_CACHED);
140 printf("random: entropy cache '%s' not overwritten and therefore not used; error = %d\n", *entropy_file, error);
143 printf("random: entropy cache '%s' not present or unreadable; error = %d\n", *entropy_file, error);
145 bzero(data, PAGE_SIZE);
146 free(data, M_ENTROPY);
149 EVENTHANDLER_DEFINE(mountroot, random_harvestq_cache, NULL, 0);
152 random_kthread(void *arg)
154 STAILQ_HEAD(, harvest) local_queue;
155 struct harvest *event = NULL;
157 event_proc_f entropy_processor = arg;
159 STAILQ_INIT(&local_queue);
162 /* Process until told to stop */
163 mtx_lock_spin(&harvest_mtx);
164 for (; random_kthread_control >= 0;) {
167 * Grab all the entropy events.
168 * Drain entropy source records into a thread-local
169 * queue for processing while not holding the mutex.
171 STAILQ_CONCAT(&local_queue, &harvestfifo.head);
172 local_count += harvestfifo.count;
173 harvestfifo.count = 0;
176 * Deal with events, if any.
177 * Then transfer the used events back into the empty fifo.
179 if (!STAILQ_EMPTY(&local_queue)) {
180 mtx_unlock_spin(&harvest_mtx);
181 STAILQ_FOREACH(event, &local_queue, next)
182 entropy_processor(event);
183 mtx_lock_spin(&harvest_mtx);
184 STAILQ_CONCAT(&emptyfifo.head, &local_queue);
185 emptyfifo.count += local_count;
189 KASSERT(local_count == 0, ("random_kthread: local_count %d",
193 * Do only one round of the hardware sources for now.
194 * Later we'll need to make it rate-adaptive.
196 mtx_unlock_spin(&harvest_mtx);
197 live_entropy_sources_feed(1, entropy_processor);
198 mtx_lock_spin(&harvest_mtx);
201 * If a queue flush was commanded, it has now happened,
202 * and we can mark this by resetting the command.
205 if (random_kthread_control == 1)
206 random_kthread_control = 0;
208 /* Work done, so don't belabour the issue */
209 msleep_spin_sbt(&random_kthread_control, &harvest_mtx,
210 "-", SBT_1S/10, 0, C_PREL(1));
213 mtx_unlock_spin(&harvest_mtx);
215 random_set_wakeup_exit(&random_kthread_control);
220 random_harvestq_init(event_proc_f cb)
225 /* Initialise the harvest fifos */
227 /* Contains the currently unused event structs. */
228 STAILQ_INIT(&emptyfifo.head);
229 for (i = 0; i < RANDOM_FIFO_MAX; i++) {
230 np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK);
231 STAILQ_INSERT_TAIL(&emptyfifo.head, np, next);
233 emptyfifo.count = RANDOM_FIFO_MAX;
235 /* Will contain the queued-up events. */
236 STAILQ_INIT(&harvestfifo.head);
237 harvestfifo.count = 0;
239 mtx_init(&harvest_mtx, "entropy harvest mutex", NULL, MTX_SPIN);
241 /* Start the hash/reseed thread */
242 error = kproc_create(random_kthread, cb,
243 &random_kthread_proc, RFHIGHPID, 0, "rand_harvestq"); /* RANDOM_CSPRNG_NAME */
246 panic("Cannot create entropy maintenance thread.");
250 random_harvestq_deinit(void)
254 /* Destroy the harvest fifos */
255 while (!STAILQ_EMPTY(&emptyfifo.head)) {
256 np = STAILQ_FIRST(&emptyfifo.head);
257 STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
261 while (!STAILQ_EMPTY(&harvestfifo.head)) {
262 np = STAILQ_FIRST(&harvestfifo.head);
263 STAILQ_REMOVE_HEAD(&harvestfifo.head, next);
266 harvestfifo.count = 0;
268 mtx_destroy(&harvest_mtx);
272 * Entropy harvesting routine.
273 * This is supposed to be fast; do not do anything slow in here!
275 * It is also illegal (and morally reprehensible) to insert any
276 * high-rate data here. "High-rate" is define as a data source
277 * that will usually cause lots of failures of the "Lockless read"
278 * check a few lines below. This includes the "always-on" sources
279 * like the Intel "rdrand" or the VIA Nehamiah "xstore" sources.
282 random_harvestq_internal(u_int64_t somecounter, const void *entropy,
283 u_int count, u_int bits, enum esource origin)
285 struct harvest *event;
287 KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE,
288 ("random_harvest_internal: origin %d invalid\n", origin));
290 /* Lockless read to avoid lock operations if fifo is full. */
291 if (harvestfifo.count >= RANDOM_FIFO_MAX)
294 mtx_lock_spin(&harvest_mtx);
297 * On't overfill the harvest queue; this could steal all
300 if (harvestfifo.count < RANDOM_FIFO_MAX) {
301 event = STAILQ_FIRST(&emptyfifo.head);
303 /* Add the harvested data to the fifo */
304 STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
306 event->somecounter = somecounter;
309 event->source = origin;
311 /* XXXX Come back and make this dynamic! */
312 count = MIN(count, HARVESTSIZE);
313 memcpy(event->entropy, entropy, count);
315 STAILQ_INSERT_TAIL(&harvestfifo.head,
321 mtx_unlock_spin(&harvest_mtx);