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