1 /*- 2 * THE BEER-WARE LICENSE 3 * 4 * <dan@FreeBSD.ORG> wrote this file. As long as you retain this notice you 5 * can do whatever you want with this stuff. If we meet some day, and you 6 * think this stuff is worth it, you can buy me a beer in return. 7 * 8 * Dan Moschuk 9 */ 10 11 #include <sys/cdefs.h> 12 __FBSDID("$FreeBSD$"); 13 14 #include <sys/types.h> 15 #include <sys/param.h> 16 #include <sys/kernel.h> 17 #include <sys/random.h> 18 #include <sys/libkern.h> 19 #include <sys/lock.h> 20 #include <sys/mutex.h> 21 #include <sys/time.h> 22 23 #define ARC4_RESEED_BYTES 65536 24 #define ARC4_RESEED_SECONDS 300 25 #define ARC4_KEYBYTES (256 / 8) 26 27 int arc4rand_iniseed_state = ARC4_ENTR_NONE; 28 29 static u_int8_t arc4_i, arc4_j; 30 static int arc4_numruns = 0; 31 static u_int8_t arc4_sbox[256]; 32 static time_t arc4_t_reseed; 33 static struct mtx arc4_mtx; 34 35 static u_int8_t arc4_randbyte(void); 36 37 static __inline void 38 arc4_swap(u_int8_t *a, u_int8_t *b) 39 { 40 u_int8_t c; 41 42 c = *a; 43 *a = *b; 44 *b = c; 45 } 46 47 /* 48 * Stir our S-box. 49 */ 50 static void 51 arc4_randomstir (void) 52 { 53 u_int8_t key[256]; 54 int r, n; 55 struct timeval tv_now; 56 57 /* 58 * XXX read_random() returns unsafe numbers if the entropy 59 * device is not loaded -- MarkM. 60 */ 61 r = read_random(key, ARC4_KEYBYTES); 62 getmicrouptime(&tv_now); 63 mtx_lock(&arc4_mtx); 64 /* If r == 0 || -1, just use what was on the stack. */ 65 if (r > 0) { 66 for (n = r; n < sizeof(key); n++) 67 key[n] = key[n % r]; 68 } 69 70 for (n = 0; n < 256; n++) { 71 arc4_j = (arc4_j + arc4_sbox[n] + key[n]) % 256; 72 arc4_swap(&arc4_sbox[n], &arc4_sbox[arc4_j]); 73 } 74 arc4_i = arc4_j = 0; 75 76 /* Reset for next reseed cycle. */ 77 arc4_t_reseed = tv_now.tv_sec + ARC4_RESEED_SECONDS; 78 arc4_numruns = 0; 79 80 /* 81 * Throw away the first N words of output, as suggested in the 82 * paper "Weaknesses in the Key Scheduling Algorithm of RC4" 83 * by Fluher, Mantin, and Shamir. (N = 256 in our case.) 84 */ 85 for (n = 0; n < 256*4; n++) 86 arc4_randbyte(); 87 mtx_unlock(&arc4_mtx); 88 } 89 90 /* 91 * Initialize our S-box to its beginning defaults. 92 */ 93 static void 94 arc4_init(void) 95 { 96 int n; 97 98 mtx_init(&arc4_mtx, "arc4_mtx", NULL, MTX_DEF); 99 arc4_i = arc4_j = 0; 100 for (n = 0; n < 256; n++) 101 arc4_sbox[n] = (u_int8_t) n; 102 103 arc4_t_reseed = 0; 104 } 105 106 SYSINIT(arc4_init, SI_SUB_LOCK, SI_ORDER_ANY, arc4_init, NULL); 107 108 /* 109 * Generate a random byte. 110 */ 111 static u_int8_t 112 arc4_randbyte(void) 113 { 114 u_int8_t arc4_t; 115 116 arc4_i = (arc4_i + 1) % 256; 117 arc4_j = (arc4_j + arc4_sbox[arc4_i]) % 256; 118 119 arc4_swap(&arc4_sbox[arc4_i], &arc4_sbox[arc4_j]); 120 121 arc4_t = (arc4_sbox[arc4_i] + arc4_sbox[arc4_j]) % 256; 122 return arc4_sbox[arc4_t]; 123 } 124 125 /* 126 * MPSAFE 127 */ 128 void 129 arc4rand(void *ptr, u_int len, int reseed) 130 { 131 u_char *p; 132 struct timeval tv; 133 134 getmicrouptime(&tv); 135 if (atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_HAVE, 136 ARC4_ENTR_SEED) || reseed || 137 (arc4_numruns > ARC4_RESEED_BYTES) || 138 (tv.tv_sec > arc4_t_reseed)) 139 arc4_randomstir(); 140 141 mtx_lock(&arc4_mtx); 142 arc4_numruns += len; 143 p = ptr; 144 while (len--) 145 *p++ = arc4_randbyte(); 146 mtx_unlock(&arc4_mtx); 147 } 148 149 uint32_t 150 arc4random(void) 151 { 152 uint32_t ret; 153 154 arc4rand(&ret, sizeof ret, 0); 155 return ret; 156 } 157