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 "opt_random.h"
33 #include <sys/kernel.h>
34 #include <sys/libkern.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/queue.h>
39 #include <sys/random.h>
42 #include <sys/sysctl.h>
43 #include <sys/systm.h>
44 #include <sys/unistd.h>
46 #include <machine/cpu.h>
48 #include <dev/random/randomdev.h>
49 #include <dev/random/randomdev_soft.h>
50 #include <dev/random/random_adaptors.h>
51 #include <dev/random/random_harvestq.h>
53 #include "live_entropy_sources.h"
56 * The les_lock protects the consistency of the "struct les_head les_sources"
58 static struct sx les_lock; /* Need a sleepable lock for the sbuf/sysctl stuff. */
60 LIST_HEAD(les_head, live_entropy_sources);
61 static struct les_head les_sources = LIST_HEAD_INITIALIZER(les_sources);
64 live_entropy_source_register(struct live_entropy_source *rsource)
66 struct live_entropy_sources *lles;
68 KASSERT(rsource != NULL, ("invalid input to %s", __func__));
70 lles = malloc(sizeof(*lles), M_ENTROPY, M_WAITOK);
71 lles->lles_rsource = rsource;
74 LIST_INSERT_HEAD(&les_sources, lles, lles_entries);
75 sx_xunlock(&les_lock);
79 live_entropy_source_deregister(struct live_entropy_source *rsource)
81 struct live_entropy_sources *lles = NULL;
83 KASSERT(rsource != NULL, ("invalid input to %s", __func__));
86 LIST_FOREACH(lles, &les_sources, lles_entries)
87 if (lles->lles_rsource == rsource) {
88 LIST_REMOVE(lles, lles_entries);
91 sx_xunlock(&les_lock);
93 free(lles, M_ENTROPY);
97 live_entropy_source_handler(SYSCTL_HANDLER_ARGS)
99 struct live_entropy_sources *lles;
105 sbuf_new_for_sysctl(&sbuf, NULL, 64, req);
108 LIST_FOREACH(lles, &les_sources, lles_entries) {
109 sbuf_cat(&sbuf, (count++ ? ",'" : "'"));
110 sbuf_cat(&sbuf, lles->lles_rsource->les_ident);
111 sbuf_cat(&sbuf, "'");
114 error = sbuf_finish(&sbuf);
117 sx_sunlock(&les_lock);
123 * Run through all "live" sources reading entropy for the given
124 * number of rounds, which should be a multiple of the number
125 * of entropy accumulation pools in use; 2 for Yarrow and 32
129 * This function runs inside the RNG thread! Don't do anything silly!
131 /* XXXRW: get_cyclecount() is cheap on most modern hardware, where cycle
132 * counters are built in, but on older hardware it will do a real time clock
133 * read which can be quite expensive.
136 live_entropy_sources_feed(void)
138 static struct harvest_event event;
139 struct live_entropy_sources *lles;
146 * Walk over all of live entropy sources, and feed their output
147 * to the system-wide RNG.
149 read_rate = random_adaptor_read_rate();
150 LIST_FOREACH(lles, &les_sources, lles_entries) {
152 for (i = 0; i < harvest_pool_count*read_rate; i++) {
153 /* This *must* be quick, since it's a live entropy source. */
154 n = lles->lles_rsource->les_read(event.he_entropy, HARVESTSIZE);
155 KASSERT((n > 0 && n <= HARVESTSIZE), ("very bad return from les_read (= %d) in %s", n, __func__));
156 memset(event.he_entropy + n, 0, HARVESTSIZE - n);
158 event.he_somecounter = get_cyclecount();
160 event.he_bits = (n*8)/2;
161 event.he_source = lles->lles_rsource->les_source;
162 event.he_destination = harvest_destination[event.he_source]++;
164 /* Do the actual entropy insertion */
165 harvest_process_event(&event);
170 sx_sunlock(&les_lock);
174 live_entropy_sources_init(void)
177 SYSCTL_PROC(_kern_random, OID_AUTO, live_entropy_sources,
178 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
179 NULL, 0, live_entropy_source_handler, "",
180 "List of Active Live Entropy Sources");
182 sx_init(&les_lock, "live_entropy_sources");
186 live_entropy_sources_deinit(void)
189 sx_destroy(&les_lock);