1 /*- 2 * Copyright (c) 2017 The FreeBSD Foundation 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/types.h> 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/libkern.h> 35 #include <sys/linker.h> 36 #include <sys/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/mutex.h> 39 #include <sys/random.h> 40 #include <sys/smp.h> 41 #include <sys/time.h> 42 43 #include <crypto/chacha20/chacha.h> 44 45 #define CHACHA20_RESEED_BYTES 65536 46 #define CHACHA20_RESEED_SECONDS 300 47 #define CHACHA20_KEYBYTES 32 48 #define CHACHA20_BUFFER_SIZE 64 49 50 CTASSERT(CHACHA20_KEYBYTES*8 >= CHACHA_MINKEYLEN); 51 52 int arc4rand_iniseed_state = ARC4_ENTR_NONE; 53 54 MALLOC_DEFINE(M_CHACHA20RANDOM, "chacha20random", "chacha20random structures"); 55 56 struct chacha20_s { 57 struct mtx mtx; 58 int numbytes; 59 time_t t_reseed; 60 u_int8_t m_buffer[CHACHA20_BUFFER_SIZE]; 61 struct chacha_ctx ctx; 62 } __aligned(CACHE_LINE_SIZE); 63 64 static struct chacha20_s *chacha20inst = NULL; 65 66 #define CHACHA20_FOREACH(_chacha20) \ 67 for (_chacha20 = &chacha20inst[0]; \ 68 _chacha20 <= &chacha20inst[mp_maxid]; \ 69 _chacha20++) 70 71 /* 72 * Mix up the current context. 73 */ 74 static void 75 chacha20_randomstir(struct chacha20_s *chacha20) 76 { 77 struct timeval tv_now; 78 u_int8_t key[CHACHA20_KEYBYTES]; 79 80 /* 81 * If the loader(8) did not have an entropy stash from the previous 82 * shutdown to load, then we will block. The answer is to make sure 83 * there is an entropy stash at shutdown time. 84 */ 85 read_random(key, CHACHA20_KEYBYTES); 86 getmicrouptime(&tv_now); 87 mtx_lock(&chacha20->mtx); 88 chacha_keysetup(&chacha20->ctx, key, CHACHA20_KEYBYTES*8); 89 chacha_ivsetup(&chacha20->ctx, (u_char *)&tv_now.tv_sec, (u_char *)&tv_now.tv_usec); 90 /* Reset for next reseed cycle. */ 91 chacha20->t_reseed = tv_now.tv_sec + CHACHA20_RESEED_SECONDS; 92 chacha20->numbytes = 0; 93 mtx_unlock(&chacha20->mtx); 94 } 95 96 /* 97 * Initialize the contexts. 98 */ 99 static void 100 chacha20_init(void) 101 { 102 struct chacha20_s *chacha20; 103 104 chacha20inst = malloc((mp_maxid + 1) * sizeof(struct chacha20_s), 105 M_CHACHA20RANDOM, M_NOWAIT | M_ZERO); 106 KASSERT(chacha20inst != NULL, ("chacha20_init: memory allocation error")); 107 108 CHACHA20_FOREACH(chacha20) { 109 mtx_init(&chacha20->mtx, "chacha20_mtx", NULL, MTX_DEF); 110 chacha20->t_reseed = -1; 111 chacha20->numbytes = 0; 112 explicit_bzero(chacha20->m_buffer, CHACHA20_BUFFER_SIZE); 113 explicit_bzero(&chacha20->ctx, sizeof(chacha20->ctx)); 114 } 115 } 116 SYSINIT(chacha20, SI_SUB_LOCK, SI_ORDER_ANY, chacha20_init, NULL); 117 118 119 static void 120 chacha20_uninit(void) 121 { 122 struct chacha20_s *chacha20; 123 124 CHACHA20_FOREACH(chacha20) 125 mtx_destroy(&chacha20->mtx); 126 free(chacha20inst, M_CHACHA20RANDOM); 127 } 128 SYSUNINIT(chacha20, SI_SUB_LOCK, SI_ORDER_ANY, chacha20_uninit, NULL); 129 130 131 /* 132 * MPSAFE 133 */ 134 void 135 arc4rand(void *ptr, u_int len, int reseed) 136 { 137 struct chacha20_s *chacha20; 138 struct timeval tv; 139 u_int length; 140 u_int8_t *p; 141 142 if (reseed || atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_HAVE, ARC4_ENTR_SEED)) 143 CHACHA20_FOREACH(chacha20) 144 chacha20_randomstir(chacha20); 145 146 chacha20 = &chacha20inst[curcpu]; 147 getmicrouptime(&tv); 148 /* We may get unlucky and be migrated off this CPU, but that is expected to be infrequent */ 149 if ((chacha20->numbytes > CHACHA20_RESEED_BYTES) || (tv.tv_sec > chacha20->t_reseed)) 150 chacha20_randomstir(chacha20); 151 152 mtx_lock(&chacha20->mtx); 153 p = ptr; 154 while (len) { 155 length = MIN(CHACHA20_BUFFER_SIZE, len); 156 chacha_encrypt_bytes(&chacha20->ctx, chacha20->m_buffer, p, length); 157 p += length; 158 len -= length; 159 chacha20->numbytes += length; 160 if (chacha20->numbytes > CHACHA20_RESEED_BYTES) { 161 mtx_unlock(&chacha20->mtx); 162 chacha20_randomstir(chacha20); 163 mtx_lock(&chacha20->mtx); 164 } 165 } 166 mtx_unlock(&chacha20->mtx); 167 } 168 169 uint32_t 170 arc4random(void) 171 { 172 uint32_t ret; 173 174 arc4rand(&ret, sizeof(ret), 0); 175 return ret; 176 } 177 178 void 179 arc4random_buf(void *ptr, size_t len) 180 { 181 182 arc4rand(ptr, len, 0); 183 } 184