1 /*- 2 * Copyright (c) 2015 Mark R V Murray 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/malloc.h> 35 #include <sys/random.h> 36 #include <sys/sysctl.h> 37 38 #if defined(RANDOM_LOADABLE) 39 #include <sys/lock.h> 40 #include <sys/sx.h> 41 #endif 42 43 #include <dev/random/randomdev.h> 44 45 /* Set up the sysctl root node for the entropy device */ 46 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, "Cryptographically Secure Random Number Generator"); 47 48 MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers and data structures"); 49 50 struct sources_head source_list = LIST_HEAD_INITIALIZER(source_list); 51 52 #if defined(RANDOM_LOADABLE) 53 struct random_algorithm *p_random_alg_context = NULL; 54 #else /* !defined(RANDOM_LOADABLE) */ 55 struct random_algorithm *p_random_alg_context = &random_alg_context; 56 #endif /* defined(RANDOM_LOADABLE) */ 57 58 #if defined(RANDOM_LOADABLE) 59 60 static void 61 null_read_random(void *dummy __unused, u_int dummy2 __unused) 62 { 63 panic("%s: no random module is loaded", __func__); 64 } 65 66 static bool 67 null_is_random_seeded(void) 68 { 69 return (false); 70 } 71 72 struct random_readers { 73 int (*read_random_uio)(struct uio *, bool); 74 void (*read_random)(void *, u_int); 75 bool (*is_random_seeded)(void); 76 } random_reader_context = { 77 (int (*)(struct uio *, bool))nullop, 78 null_read_random, 79 null_is_random_seeded, 80 }; 81 82 struct sx randomdev_config_lock; 83 84 static void 85 random_infra_sysinit(void *dummy __unused) 86 { 87 88 RANDOM_CONFIG_INIT_LOCK(); 89 } 90 SYSINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_FIRST, random_infra_sysinit, NULL); 91 92 void 93 random_infra_init(int (*p_random_read_uio)(struct uio *, bool), 94 void (*p_random_read)(void *, u_int), 95 bool (*p_is_random_seeded)(void)) 96 { 97 98 RANDOM_CONFIG_X_LOCK(); 99 random_reader_context.read_random_uio = p_random_read_uio; 100 random_reader_context.read_random = p_random_read; 101 random_reader_context.is_random_seeded = p_is_random_seeded; 102 RANDOM_CONFIG_X_UNLOCK(); 103 } 104 105 void 106 random_infra_uninit(void) 107 { 108 109 RANDOM_CONFIG_X_LOCK(); 110 random_reader_context.read_random_uio = (int (*)(struct uio *, bool))nullop; 111 random_reader_context.read_random = null_read_random; 112 random_reader_context.is_random_seeded = null_is_random_seeded; 113 RANDOM_CONFIG_X_UNLOCK(); 114 } 115 116 static void 117 random_infra_sysuninit(void *dummy __unused) 118 { 119 120 RANDOM_CONFIG_DEINIT_LOCK(); 121 } 122 SYSUNINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_FIRST, random_infra_sysuninit, NULL); 123 124 int 125 read_random_uio(struct uio *uio, bool nonblock) 126 { 127 int retval; 128 129 RANDOM_CONFIG_S_LOCK(); 130 retval = random_reader_context.read_random_uio(uio, nonblock); 131 RANDOM_CONFIG_S_UNLOCK(); 132 return (retval); 133 } 134 135 void 136 read_random(void *buf, u_int len) 137 { 138 139 RANDOM_CONFIG_S_LOCK(); 140 random_reader_context.read_random(buf, len); 141 RANDOM_CONFIG_S_UNLOCK(); 142 } 143 144 bool 145 is_random_seeded(void) 146 { 147 RANDOM_CONFIG_S_LOCK(); 148 random_reader_context.is_random_seeded(); 149 RANDOM_CONFIG_S_UNLOCK(); 150 } 151 152 153 #endif /* defined(RANDOM_LOADABLE) */ 154