2 * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com>
3 * Copyright (c) 2013 David E. O'Brien <obrien@NUXI.org>
4 * Copyright (c) 2004 Mark R V Murray
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.
29 #include <sys/param.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/kthread.h>
36 #include <sys/random.h>
37 #include <sys/selinfo.h>
38 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/queue.h>
42 #include <sys/libkern.h>
43 #include <sys/unistd.h>
45 #include <dev/random/randomdev.h>
46 #include <dev/random/randomdev_soft.h>
47 #include <dev/random/random_adaptors.h>
49 LIST_HEAD(adaptors_head, random_adaptors);
50 static struct adaptors_head adaptors = LIST_HEAD_INITIALIZER(adaptors);
51 static struct sx adaptors_lock; /* need a sleepable lock */
53 /* List for the dynamic sysctls */
54 static struct sysctl_ctx_list random_clist;
56 struct random_adaptor *random_adaptor;
58 MALLOC_DEFINE(M_RANDOM_ADAPTORS, "random_adaptors", "Random adaptors buffers");
61 random_adaptor_register(const char *name, struct random_adaptor *rsp)
63 struct random_adaptors *rpp;
65 KASSERT(name != NULL && rsp != NULL, ("invalid input to %s", __func__));
67 rpp = malloc(sizeof(struct random_adaptors), M_RANDOM_ADAPTORS,
72 sx_xlock(&adaptors_lock);
73 LIST_INSERT_HEAD(&adaptors, rpp, entries);
74 sx_xunlock(&adaptors_lock);
79 struct random_adaptor *
80 random_adaptor_get(const char *name)
82 struct random_adaptors *rpp;
83 struct random_adaptor *rsp;
87 sx_slock(&adaptors_lock);
89 LIST_FOREACH(rpp, &adaptors, entries)
90 if (strcmp(rpp->name, name) == 0)
93 sx_sunlock(&adaptors_lock);
99 * In the past, the logic of the random_adaptor selection was inverted, such
100 * that hardware RNGs would be chosen unless disabled. This routine is here to
101 * preserve that functionality to avoid folks losing their hardware RNGs by
102 * upgrading to newer kernel.
105 random_adaptor_choose_legacy(struct random_adaptor **adaptor)
107 struct random_adaptor *tmp;
110 /* Then go looking for hardware */
112 TUNABLE_INT_FETCH("hw.nehemiah_rng_enable", &enable);
113 if (enable && (tmp = random_adaptor_get("nehemiah")))
117 TUNABLE_INT_FETCH("hw.ivy_rng_enable", &enable);
118 if (enable && (tmp = random_adaptor_get("rdrand")))
123 * Walk a list of registered random(4) adaptors and pick the last non-selected
126 * If none are selected, use yarrow if available.
129 random_adaptor_choose(struct random_adaptor **adaptor)
131 char rngs[128], *token, *cp;
132 struct random_adaptors *rpp;
134 KASSERT(adaptor != NULL, ("pre-conditions failed"));
138 random_adaptor_choose_legacy(adaptor);
140 if (*adaptor != NULL)
143 if (TUNABLE_STR_FETCH("rngs_want", rngs, sizeof(rngs))) {
146 while ((token = strsep(&cp, ",")) != NULL) {
147 if ((*adaptor = random_adaptor_get(token)) != NULL)
149 else if (bootverbose)
151 "%s random adaptor is not available, skipping\n",
156 if (*adaptor == NULL) {
158 * Either no RNGs are prefered via rngs_want tunable, or
159 * no prefered RNGs are registered.
160 * Fallback to Yarrow.
162 *adaptor = random_adaptor_get("yarrow");
164 if (*adaptor == NULL) {
166 * Yarrow doesn't seem to be available.
167 * Fallback to the first thing that's on the list of
170 sx_slock(&adaptors_lock);
172 rpp = LIST_FIRST(&adaptors);
176 sx_sunlock(&adaptors_lock);
179 if (bootverbose && *adaptor)
180 printf("Falling back to <%s> random adaptor\n",
186 random_adaptors_deinit(void *unused)
189 sx_destroy(&adaptors_lock);
190 sysctl_ctx_free(&random_clist);
194 random_sysctl_adaptors_handler(SYSCTL_HANDLER_ARGS)
196 struct random_adaptors *rpp;
201 sx_slock(&adaptors_lock);
203 if (LIST_EMPTY(&adaptors)) {
204 error = SYSCTL_OUT(req, "", 0);
207 LIST_FOREACH(rpp, &adaptors, entries) {
209 error = SYSCTL_OUT(req, ",", count++ ? 1 : 0);
214 error = SYSCTL_OUT(req, rpp->name, strlen(rpp->name));
221 sx_sunlock(&adaptors_lock);
227 random_sysctl_active_adaptor_handler(SYSCTL_HANDLER_ARGS)
229 struct random_adaptor *rsp;
230 struct random_adaptors *rpp;
235 rsp = random_adaptor;
238 sx_slock(&adaptors_lock);
240 LIST_FOREACH(rpp, &adaptors, entries) {
245 sx_sunlock(&adaptors_lock);
248 if (rsp == NULL || name == NULL) {
249 error = SYSCTL_OUT(req, "", 0);
251 error = SYSCTL_OUT(req, name, strlen(name));
258 random_adaptors_init(void *unused)
261 SYSCTL_PROC(_kern_random, OID_AUTO, adaptors,
262 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
263 NULL, 0, random_sysctl_adaptors_handler, "",
264 "Random Number Generator adaptors");
266 SYSCTL_PROC(_kern_random, OID_AUTO, active_adaptor,
267 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
268 NULL, 0, random_sysctl_active_adaptor_handler, "",
269 "Active Random Number Generator Adaptor");
271 sx_init(&adaptors_lock, "random_adaptors");
274 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, "Random Number Generator");
276 SYSINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST, random_adaptors_init,
278 SYSUNINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST,
279 random_adaptors_deinit, NULL);