randomdev_soft.c revision 256381
1/*- 2 * Copyright (c) 2000-2013 Mark R V Murray 3 * Copyright (c) 2004 Robert N. M. Watson 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 15 * 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. 26 * 27 */ 28 29#include "opt_random.h" 30 31#if !defined(RANDOM_YARROW) && !defined(RANDOM_FORTUNA) 32#define RANDOM_YARROW 33#elif defined(RANDOM_YARROW) && defined(RANDOM_FORTUNA) 34#error "Must define either RANDOM_YARROW or RANDOM_FORTUNA" 35#endif 36#if defined(RANDOM_FORTUNA) 37#error "Fortuna is not yet implemented" 38#endif 39 40#include <sys/cdefs.h> 41__FBSDID("$FreeBSD: stable/10/sys/dev/random/randomdev_soft.c 256381 2013-10-12 15:31:36Z markm $"); 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/fcntl.h> 46#include <sys/kernel.h> 47#include <sys/lock.h> 48#include <sys/malloc.h> 49#include <sys/module.h> 50#include <sys/mutex.h> 51#include <sys/poll.h> 52#include <sys/random.h> 53#include <sys/selinfo.h> 54#include <sys/sysctl.h> 55#include <sys/uio.h> 56#include <sys/unistd.h> 57 58#include <machine/bus.h> 59#include <machine/cpu.h> 60 61#include <dev/random/randomdev.h> 62#include <dev/random/randomdev_soft.h> 63#include <dev/random/random_harvestq.h> 64#include <dev/random/random_adaptors.h> 65#if defined(RANDOM_YARROW) 66#include <dev/random/yarrow.h> 67#endif 68#if defined(RANDOM_FORTUNA) 69#include <dev/random/fortuna.h> 70#endif 71 72 73static int randomdev_poll(int event, struct thread *td); 74static int randomdev_block(int flag); 75static void randomdev_flush_reseed(void); 76 77#if defined(RANDOM_YARROW) 78static struct random_adaptor random_context = { 79 .ident = "Software, Yarrow", 80 .init = randomdev_init, 81 .deinit = randomdev_deinit, 82 .block = randomdev_block, 83 .read = random_yarrow_read, 84 .poll = randomdev_poll, 85 .reseed = randomdev_flush_reseed, 86 .seeded = 0, /* This will be seeded during entropy processing */ 87}; 88#define RANDOM_MODULE_NAME yarrow 89#define RANDOM_CSPRNG_NAME "yarrow" 90#endif 91 92#if defined(RANDOM_FORTUNA) 93static struct random_adaptor random_context = { 94 .ident = "Software, Fortuna", 95 .init = randomdev_init, 96 .deinit = randomdev_deinit, 97 .block = randomdev_block, 98 .read = random_fortuna_read, 99 .poll = randomdev_poll, 100 .reseed = randomdev_flush_reseed, 101 .seeded = 0, /* This will be excplicitly seeded at startup when secured */ 102}; 103#define RANDOM_MODULE_NAME fortuna 104#define RANDOM_CSPRNG_NAME "fortuna" 105#endif 106 107TUNABLE_INT("kern.random.sys.seeded", &random_context.seeded); 108 109/* List for the dynamic sysctls */ 110static struct sysctl_ctx_list random_clist; 111 112/* ARGSUSED */ 113static int 114random_check_boolean(SYSCTL_HANDLER_ARGS) 115{ 116 if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0) 117 *(u_int *)(oidp->oid_arg1) = 1; 118 return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req)); 119} 120 121void 122randomdev_init(void) 123{ 124 struct sysctl_oid *random_sys_o, *random_sys_harvest_o; 125 126#if defined(RANDOM_YARROW) 127 random_yarrow_init_alg(&random_clist); 128#endif 129#if defined(RANDOM_FORTUNA) 130 random_fortuna_init_alg(&random_clist); 131#endif 132 133 random_sys_o = SYSCTL_ADD_NODE(&random_clist, 134 SYSCTL_STATIC_CHILDREN(_kern_random), 135 OID_AUTO, "sys", CTLFLAG_RW, 0, 136 "Entropy Device Parameters"); 137 138 SYSCTL_ADD_PROC(&random_clist, 139 SYSCTL_CHILDREN(random_sys_o), 140 OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW, 141 &random_context.seeded, 0, random_check_boolean, "I", 142 "Seeded State"); 143 144 random_sys_harvest_o = SYSCTL_ADD_NODE(&random_clist, 145 SYSCTL_CHILDREN(random_sys_o), 146 OID_AUTO, "harvest", CTLFLAG_RW, 0, 147 "Entropy Sources"); 148 149 SYSCTL_ADD_PROC(&random_clist, 150 SYSCTL_CHILDREN(random_sys_harvest_o), 151 OID_AUTO, "ethernet", CTLTYPE_INT | CTLFLAG_RW, 152 &harvest.ethernet, 1, random_check_boolean, "I", 153 "Harvest NIC entropy"); 154 SYSCTL_ADD_PROC(&random_clist, 155 SYSCTL_CHILDREN(random_sys_harvest_o), 156 OID_AUTO, "point_to_point", CTLTYPE_INT | CTLFLAG_RW, 157 &harvest.point_to_point, 1, random_check_boolean, "I", 158 "Harvest serial net entropy"); 159 SYSCTL_ADD_PROC(&random_clist, 160 SYSCTL_CHILDREN(random_sys_harvest_o), 161 OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW, 162 &harvest.interrupt, 1, random_check_boolean, "I", 163 "Harvest IRQ entropy"); 164 SYSCTL_ADD_PROC(&random_clist, 165 SYSCTL_CHILDREN(random_sys_harvest_o), 166 OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW, 167 &harvest.swi, 1, random_check_boolean, "I", 168 "Harvest SWI entropy"); 169 170 random_harvestq_init(random_process_event); 171 172 /* Register the randomness harvesting routine */ 173 randomdev_init_harvester(random_harvestq_internal, 174 random_context.read); 175} 176 177void 178randomdev_deinit(void) 179{ 180 /* Deregister the randomness harvesting routine */ 181 randomdev_deinit_harvester(); 182 183 /* 184 * Command the hash/reseed thread to end and wait for it to finish 185 */ 186 random_kthread_control = -1; 187 tsleep((void *)&random_kthread_control, 0, "term", 0); 188 189#if defined(RANDOM_YARROW) 190 random_yarrow_deinit_alg(); 191#endif 192#if defined(RANDOM_FORTUNA) 193 random_fortuna_deinit_alg(); 194#endif 195 196 sysctl_ctx_free(&random_clist); 197} 198 199void 200randomdev_unblock(void) 201{ 202 if (!random_context.seeded) { 203 selwakeuppri(&random_context.rsel, PUSER); 204 wakeup(&random_context); 205 printf("random: unblocking device.\n"); 206 random_context.seeded = 1; 207 } 208 /* Do arc4random(9) a favour while we are about it. */ 209 (void)atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_NONE, 210 ARC4_ENTR_HAVE); 211} 212 213static int 214randomdev_poll(int events, struct thread *td) 215{ 216 int revents = 0; 217 218 mtx_lock(&random_reseed_mtx); 219 220 if (random_context.seeded) 221 revents = events & (POLLIN | POLLRDNORM); 222 else 223 selrecord(td, &random_context.rsel); 224 225 mtx_unlock(&random_reseed_mtx); 226 return (revents); 227} 228 229static int 230randomdev_block(int flag) 231{ 232 int error = 0; 233 234 mtx_lock(&random_reseed_mtx); 235 236 /* Blocking logic */ 237 while (!random_context.seeded && !error) { 238 if (flag & O_NONBLOCK) 239 error = EWOULDBLOCK; 240 else { 241 printf("random: blocking on read.\n"); 242 error = msleep(&random_context, 243 &random_reseed_mtx, 244 PUSER | PCATCH, "block", 0); 245 } 246 } 247 mtx_unlock(&random_reseed_mtx); 248 249 return (error); 250} 251 252/* Helper routine to perform explicit reseeds */ 253static void 254randomdev_flush_reseed(void) 255{ 256 /* Command a entropy queue flush and wait for it to finish */ 257 random_kthread_control = 1; 258 while (random_kthread_control) 259 pause("-", hz / 10); 260 261#if defined(RANDOM_YARROW) 262 /* This ultimately calls randomdev_unblock() */ 263 random_yarrow_reseed(); 264#endif 265#if defined(RANDOM_FORTUNA) 266 /* This ultimately calls randomdev_unblock() */ 267 random_fortuna_reseed(); 268#endif 269} 270 271static int 272randomdev_modevent(module_t mod __unused, int type, void *unused __unused) 273{ 274 275 switch (type) { 276 case MOD_LOAD: 277 random_adaptor_register(RANDOM_CSPRNG_NAME, &random_context); 278 /* 279 * For statically built kernels that contain both device 280 * random and options PADLOCK_RNG/RDRAND_RNG/etc.., 281 * this event handler will do nothing, since the random 282 * driver-specific handlers are loaded after these HW 283 * consumers, and hence hasn't yet registered for this event. 284 * 285 * In case where both the random driver and RNG's are built 286 * as seperate modules, random.ko is loaded prior to *_rng.ko's 287 * (by dependency). This event handler is there to delay 288 * creation of /dev/{u,}random and attachment of this *_rng.ko. 289 */ 290 EVENTHANDLER_INVOKE(random_adaptor_attach, &random_context); 291 return (0); 292 } 293 294 return (EINVAL); 295} 296 297RANDOM_ADAPTOR_MODULE(RANDOM_MODULE_NAME, randomdev_modevent, 1); 298