randomdev_soft.c revision 259065
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: releng/10.0/sys/dev/random/randomdev_soft.c 256414 2013-10-13 00:13:57Z 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 .priority = 90, /* High priority, so top of the list. Fortuna may still win. */ 88}; 89#define RANDOM_MODULE_NAME yarrow 90#define RANDOM_CSPRNG_NAME "yarrow" 91#endif 92 93#if defined(RANDOM_FORTUNA) 94static struct random_adaptor random_context = { 95 .ident = "Software, Fortuna", 96 .init = randomdev_init, 97 .deinit = randomdev_deinit, 98 .block = randomdev_block, 99 .read = random_fortuna_read, 100 .poll = randomdev_poll, 101 .reseed = randomdev_flush_reseed, 102 .seeded = 0, /* This will be excplicitly seeded at startup when secured */ 103 .priority = 100, /* High priority, so top of the list. Beat Yarrow. */ 104}; 105#define RANDOM_MODULE_NAME fortuna 106#define RANDOM_CSPRNG_NAME "fortuna" 107#endif 108 109TUNABLE_INT("kern.random.sys.seeded", &random_context.seeded); 110 111/* List for the dynamic sysctls */ 112static struct sysctl_ctx_list random_clist; 113 114/* ARGSUSED */ 115static int 116random_check_boolean(SYSCTL_HANDLER_ARGS) 117{ 118 if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0) 119 *(u_int *)(oidp->oid_arg1) = 1; 120 return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req)); 121} 122 123void 124randomdev_init(void) 125{ 126 struct sysctl_oid *random_sys_o, *random_sys_harvest_o; 127 128#if defined(RANDOM_YARROW) 129 random_yarrow_init_alg(&random_clist); 130#endif 131#if defined(RANDOM_FORTUNA) 132 random_fortuna_init_alg(&random_clist); 133#endif 134 135 random_sys_o = SYSCTL_ADD_NODE(&random_clist, 136 SYSCTL_STATIC_CHILDREN(_kern_random), 137 OID_AUTO, "sys", CTLFLAG_RW, 0, 138 "Entropy Device Parameters"); 139 140 SYSCTL_ADD_PROC(&random_clist, 141 SYSCTL_CHILDREN(random_sys_o), 142 OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW, 143 &random_context.seeded, 0, random_check_boolean, "I", 144 "Seeded State"); 145 146 random_sys_harvest_o = SYSCTL_ADD_NODE(&random_clist, 147 SYSCTL_CHILDREN(random_sys_o), 148 OID_AUTO, "harvest", CTLFLAG_RW, 0, 149 "Entropy Sources"); 150 151 SYSCTL_ADD_PROC(&random_clist, 152 SYSCTL_CHILDREN(random_sys_harvest_o), 153 OID_AUTO, "ethernet", CTLTYPE_INT | CTLFLAG_RW, 154 &harvest.ethernet, 1, random_check_boolean, "I", 155 "Harvest NIC entropy"); 156 SYSCTL_ADD_PROC(&random_clist, 157 SYSCTL_CHILDREN(random_sys_harvest_o), 158 OID_AUTO, "point_to_point", CTLTYPE_INT | CTLFLAG_RW, 159 &harvest.point_to_point, 1, random_check_boolean, "I", 160 "Harvest serial net entropy"); 161 SYSCTL_ADD_PROC(&random_clist, 162 SYSCTL_CHILDREN(random_sys_harvest_o), 163 OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW, 164 &harvest.interrupt, 1, random_check_boolean, "I", 165 "Harvest IRQ entropy"); 166 SYSCTL_ADD_PROC(&random_clist, 167 SYSCTL_CHILDREN(random_sys_harvest_o), 168 OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW, 169 &harvest.swi, 1, random_check_boolean, "I", 170 "Harvest SWI entropy"); 171 172 random_harvestq_init(random_process_event); 173 174 /* Register the randomness harvesting routine */ 175 randomdev_init_harvester(random_harvestq_internal, 176 random_context.read); 177} 178 179void 180randomdev_deinit(void) 181{ 182 /* Deregister the randomness harvesting routine */ 183 randomdev_deinit_harvester(); 184 185 /* 186 * Command the hash/reseed thread to end and wait for it to finish 187 */ 188 random_kthread_control = -1; 189 tsleep((void *)&random_kthread_control, 0, "term", 0); 190 191#if defined(RANDOM_YARROW) 192 random_yarrow_deinit_alg(); 193#endif 194#if defined(RANDOM_FORTUNA) 195 random_fortuna_deinit_alg(); 196#endif 197 198 sysctl_ctx_free(&random_clist); 199} 200 201void 202randomdev_unblock(void) 203{ 204 if (!random_context.seeded) { 205 selwakeuppri(&random_context.rsel, PUSER); 206 wakeup(&random_context); 207 printf("random: unblocking device.\n"); 208 random_context.seeded = 1; 209 } 210 /* Do arc4random(9) a favour while we are about it. */ 211 (void)atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_NONE, 212 ARC4_ENTR_HAVE); 213} 214 215static int 216randomdev_poll(int events, struct thread *td) 217{ 218 int revents = 0; 219 220 mtx_lock(&random_reseed_mtx); 221 222 if (random_context.seeded) 223 revents = events & (POLLIN | POLLRDNORM); 224 else 225 selrecord(td, &random_context.rsel); 226 227 mtx_unlock(&random_reseed_mtx); 228 return (revents); 229} 230 231static int 232randomdev_block(int flag) 233{ 234 int error = 0; 235 236 mtx_lock(&random_reseed_mtx); 237 238 /* Blocking logic */ 239 while (!random_context.seeded && !error) { 240 if (flag & O_NONBLOCK) 241 error = EWOULDBLOCK; 242 else { 243 printf("random: blocking on read.\n"); 244 error = msleep(&random_context, 245 &random_reseed_mtx, 246 PUSER | PCATCH, "block", 0); 247 } 248 } 249 mtx_unlock(&random_reseed_mtx); 250 251 return (error); 252} 253 254/* Helper routine to perform explicit reseeds */ 255static void 256randomdev_flush_reseed(void) 257{ 258 /* Command a entropy queue flush and wait for it to finish */ 259 random_kthread_control = 1; 260 while (random_kthread_control) 261 pause("-", hz / 10); 262 263#if defined(RANDOM_YARROW) 264 /* This ultimately calls randomdev_unblock() */ 265 random_yarrow_reseed(); 266#endif 267#if defined(RANDOM_FORTUNA) 268 /* This ultimately calls randomdev_unblock() */ 269 random_fortuna_reseed(); 270#endif 271} 272 273static int 274randomdev_modevent(module_t mod __unused, int type, void *unused __unused) 275{ 276 277 switch (type) { 278 case MOD_LOAD: 279 random_adaptor_register(RANDOM_CSPRNG_NAME, &random_context); 280 /* 281 * For statically built kernels that contain both device 282 * random and options PADLOCK_RNG/RDRAND_RNG/etc.., 283 * this event handler will do nothing, since the random 284 * driver-specific handlers are loaded after these HW 285 * consumers, and hence hasn't yet registered for this event. 286 * 287 * In case where both the random driver and RNG's are built 288 * as seperate modules, random.ko is loaded prior to *_rng.ko's 289 * (by dependency). This event handler is there to delay 290 * creation of /dev/{u,}random and attachment of this *_rng.ko. 291 */ 292 EVENTHANDLER_INVOKE(random_adaptor_attach, &random_context); 293 return (0); 294 } 295 296 return (EINVAL); 297} 298 299RANDOM_ADAPTOR_MODULE(RANDOM_MODULE_NAME, randomdev_modevent, 1); 300