1*38a52bd3SEd Maste /* $OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb Exp $ */ 2f7167e0eSDag-Erling Smørgrav 3f7167e0eSDag-Erling Smørgrav /* 4f7167e0eSDag-Erling Smørgrav * Copyright (c) 1996, David Mazieres <dm@uun.org> 5f7167e0eSDag-Erling Smørgrav * Copyright (c) 2008, Damien Miller <djm@openbsd.org> 6f7167e0eSDag-Erling Smørgrav * Copyright (c) 2013, Markus Friedl <markus@openbsd.org> 7*38a52bd3SEd Maste * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org> 8f7167e0eSDag-Erling Smørgrav * 9f7167e0eSDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any 10f7167e0eSDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above 11f7167e0eSDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies. 12f7167e0eSDag-Erling Smørgrav * 13f7167e0eSDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14f7167e0eSDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15f7167e0eSDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16f7167e0eSDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17f7167e0eSDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18f7167e0eSDag-Erling Smørgrav * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19f7167e0eSDag-Erling Smørgrav * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20f7167e0eSDag-Erling Smørgrav */ 21f7167e0eSDag-Erling Smørgrav 22f7167e0eSDag-Erling Smørgrav /* 23f7167e0eSDag-Erling Smørgrav * ChaCha based random number generator for OpenBSD. 24f7167e0eSDag-Erling Smørgrav */ 25f7167e0eSDag-Erling Smørgrav 26*38a52bd3SEd Maste /* OPENBSD ORIGINAL: lib/libc/crypt/arc4random.c */ 27*38a52bd3SEd Maste 28f7167e0eSDag-Erling Smørgrav #include "includes.h" 29f7167e0eSDag-Erling Smørgrav 30bc5531deSDag-Erling Smørgrav #include <sys/types.h> 31bc5531deSDag-Erling Smørgrav 32bc5531deSDag-Erling Smørgrav #include <fcntl.h> 33*38a52bd3SEd Maste #include <limits.h> 34*38a52bd3SEd Maste #include <signal.h> 35*38a52bd3SEd Maste #ifdef HAVE_STDINT_H 36*38a52bd3SEd Maste #include <stdint.h> 37*38a52bd3SEd Maste #endif 38f7167e0eSDag-Erling Smørgrav #include <stdlib.h> 39f7167e0eSDag-Erling Smørgrav #include <string.h> 40f7167e0eSDag-Erling Smørgrav #include <unistd.h> 41*38a52bd3SEd Maste #include <sys/types.h> 42*38a52bd3SEd Maste #include <sys/time.h> 43190cef3dSDag-Erling Smørgrav 44f7167e0eSDag-Erling Smørgrav #ifndef HAVE_ARC4RANDOM 45f7167e0eSDag-Erling Smørgrav 46*38a52bd3SEd Maste /* 47*38a52bd3SEd Maste * If we're not using a native getentropy, use the one from bsd-getentropy.c 48*38a52bd3SEd Maste * under a different name, so that if in future these binaries are run on 49*38a52bd3SEd Maste * a system that has a native getentropy OpenSSL cannot call the wrong one. 50*38a52bd3SEd Maste */ 51*38a52bd3SEd Maste #ifndef HAVE_GETENTROPY 52*38a52bd3SEd Maste # define getentropy(x, y) (_ssh_compat_getentropy((x), (y))) 53bc5531deSDag-Erling Smørgrav #endif 54f7167e0eSDag-Erling Smørgrav 55f7167e0eSDag-Erling Smørgrav #include "log.h" 56f7167e0eSDag-Erling Smørgrav 57f7167e0eSDag-Erling Smørgrav #define KEYSTREAM_ONLY 58f7167e0eSDag-Erling Smørgrav #include "chacha_private.h" 59f7167e0eSDag-Erling Smørgrav 60*38a52bd3SEd Maste #define minimum(a, b) ((a) < (b) ? (a) : (b)) 61f7167e0eSDag-Erling Smørgrav 62*38a52bd3SEd Maste #if defined(__GNUC__) || defined(_MSC_VER) 63*38a52bd3SEd Maste #define inline __inline 64*38a52bd3SEd Maste #else /* __GNUC__ || _MSC_VER */ 65*38a52bd3SEd Maste #define inline 66*38a52bd3SEd Maste #endif /* !__GNUC__ && !_MSC_VER */ 67f7167e0eSDag-Erling Smørgrav 68f7167e0eSDag-Erling Smørgrav #define KEYSZ 32 69f7167e0eSDag-Erling Smørgrav #define IVSZ 8 70f7167e0eSDag-Erling Smørgrav #define BLOCKSZ 64 71f7167e0eSDag-Erling Smørgrav #define RSBUFSZ (16*BLOCKSZ) 72*38a52bd3SEd Maste 73*38a52bd3SEd Maste #define REKEY_BASE (1024*1024) /* NB. should be a power of 2 */ 74*38a52bd3SEd Maste 75*38a52bd3SEd Maste /* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */ 76*38a52bd3SEd Maste static struct _rs { 77*38a52bd3SEd Maste size_t rs_have; /* valid bytes at end of rs_buf */ 78*38a52bd3SEd Maste size_t rs_count; /* bytes till reseed */ 79*38a52bd3SEd Maste } *rs; 80*38a52bd3SEd Maste 81*38a52bd3SEd Maste /* Maybe be preserved in fork children, if _rs_allocate() decides. */ 82*38a52bd3SEd Maste static struct _rsx { 83*38a52bd3SEd Maste chacha_ctx rs_chacha; /* chacha context for random keystream */ 84*38a52bd3SEd Maste u_char rs_buf[RSBUFSZ]; /* keystream blocks */ 85*38a52bd3SEd Maste } *rsx; 86*38a52bd3SEd Maste 87*38a52bd3SEd Maste static inline int _rs_allocate(struct _rs **, struct _rsx **); 88*38a52bd3SEd Maste static inline void _rs_forkdetect(void); 89*38a52bd3SEd Maste #include "arc4random.h" 90f7167e0eSDag-Erling Smørgrav 91f7167e0eSDag-Erling Smørgrav static inline void _rs_rekey(u_char *dat, size_t datlen); 92f7167e0eSDag-Erling Smørgrav 93f7167e0eSDag-Erling Smørgrav static inline void 94f7167e0eSDag-Erling Smørgrav _rs_init(u_char *buf, size_t n) 95f7167e0eSDag-Erling Smørgrav { 96f7167e0eSDag-Erling Smørgrav if (n < KEYSZ + IVSZ) 97f7167e0eSDag-Erling Smørgrav return; 98*38a52bd3SEd Maste 99*38a52bd3SEd Maste if (rs == NULL) { 100*38a52bd3SEd Maste if (_rs_allocate(&rs, &rsx) == -1) 101*38a52bd3SEd Maste _exit(1); 102f7167e0eSDag-Erling Smørgrav } 103f7167e0eSDag-Erling Smørgrav 104*38a52bd3SEd Maste chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8); 105*38a52bd3SEd Maste chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ); 10619261079SEd Maste } 107bc5531deSDag-Erling Smørgrav 108f7167e0eSDag-Erling Smørgrav static void 109f7167e0eSDag-Erling Smørgrav _rs_stir(void) 110f7167e0eSDag-Erling Smørgrav { 111f7167e0eSDag-Erling Smørgrav u_char rnd[KEYSZ + IVSZ]; 112*38a52bd3SEd Maste uint32_t rekey_fuzz = 0; 113f7167e0eSDag-Erling Smørgrav 114*38a52bd3SEd Maste if (getentropy(rnd, sizeof rnd) == -1) 115*38a52bd3SEd Maste _getentropy_fail(); 116f7167e0eSDag-Erling Smørgrav 117*38a52bd3SEd Maste if (!rs) 118f7167e0eSDag-Erling Smørgrav _rs_init(rnd, sizeof(rnd)); 119*38a52bd3SEd Maste else 120f7167e0eSDag-Erling Smørgrav _rs_rekey(rnd, sizeof(rnd)); 121*38a52bd3SEd Maste explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */ 122f7167e0eSDag-Erling Smørgrav 123f7167e0eSDag-Erling Smørgrav /* invalidate rs_buf */ 124*38a52bd3SEd Maste rs->rs_have = 0; 125*38a52bd3SEd Maste memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf)); 126f7167e0eSDag-Erling Smørgrav 127*38a52bd3SEd Maste /* rekey interval should not be predictable */ 128*38a52bd3SEd Maste chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz, 129*38a52bd3SEd Maste (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz)); 130*38a52bd3SEd Maste rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE); 131f7167e0eSDag-Erling Smørgrav } 132f7167e0eSDag-Erling Smørgrav 133f7167e0eSDag-Erling Smørgrav static inline void 134f7167e0eSDag-Erling Smørgrav _rs_stir_if_needed(size_t len) 135f7167e0eSDag-Erling Smørgrav { 136*38a52bd3SEd Maste _rs_forkdetect(); 137*38a52bd3SEd Maste if (!rs || rs->rs_count <= len) 138f7167e0eSDag-Erling Smørgrav _rs_stir(); 139*38a52bd3SEd Maste if (rs->rs_count <= len) 140*38a52bd3SEd Maste rs->rs_count = 0; 141*38a52bd3SEd Maste else 142*38a52bd3SEd Maste rs->rs_count -= len; 143f7167e0eSDag-Erling Smørgrav } 144f7167e0eSDag-Erling Smørgrav 145f7167e0eSDag-Erling Smørgrav static inline void 146f7167e0eSDag-Erling Smørgrav _rs_rekey(u_char *dat, size_t datlen) 147f7167e0eSDag-Erling Smørgrav { 148f7167e0eSDag-Erling Smørgrav #ifndef KEYSTREAM_ONLY 149*38a52bd3SEd Maste memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf)); 150f7167e0eSDag-Erling Smørgrav #endif 151f7167e0eSDag-Erling Smørgrav /* fill rs_buf with the keystream */ 152*38a52bd3SEd Maste chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf, 153*38a52bd3SEd Maste rsx->rs_buf, sizeof(rsx->rs_buf)); 154f7167e0eSDag-Erling Smørgrav /* mix in optional user provided data */ 155f7167e0eSDag-Erling Smørgrav if (dat) { 156f7167e0eSDag-Erling Smørgrav size_t i, m; 157f7167e0eSDag-Erling Smørgrav 158*38a52bd3SEd Maste m = minimum(datlen, KEYSZ + IVSZ); 159f7167e0eSDag-Erling Smørgrav for (i = 0; i < m; i++) 160*38a52bd3SEd Maste rsx->rs_buf[i] ^= dat[i]; 161f7167e0eSDag-Erling Smørgrav } 162f7167e0eSDag-Erling Smørgrav /* immediately reinit for backtracking resistance */ 163*38a52bd3SEd Maste _rs_init(rsx->rs_buf, KEYSZ + IVSZ); 164*38a52bd3SEd Maste memset(rsx->rs_buf, 0, KEYSZ + IVSZ); 165*38a52bd3SEd Maste rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ; 166f7167e0eSDag-Erling Smørgrav } 167f7167e0eSDag-Erling Smørgrav 168f7167e0eSDag-Erling Smørgrav static inline void 169f7167e0eSDag-Erling Smørgrav _rs_random_buf(void *_buf, size_t n) 170f7167e0eSDag-Erling Smørgrav { 171f7167e0eSDag-Erling Smørgrav u_char *buf = (u_char *)_buf; 172*38a52bd3SEd Maste u_char *keystream; 173f7167e0eSDag-Erling Smørgrav size_t m; 174f7167e0eSDag-Erling Smørgrav 175f7167e0eSDag-Erling Smørgrav _rs_stir_if_needed(n); 176f7167e0eSDag-Erling Smørgrav while (n > 0) { 177*38a52bd3SEd Maste if (rs->rs_have > 0) { 178*38a52bd3SEd Maste m = minimum(n, rs->rs_have); 179*38a52bd3SEd Maste keystream = rsx->rs_buf + sizeof(rsx->rs_buf) 180*38a52bd3SEd Maste - rs->rs_have; 181*38a52bd3SEd Maste memcpy(buf, keystream, m); 182*38a52bd3SEd Maste memset(keystream, 0, m); 183f7167e0eSDag-Erling Smørgrav buf += m; 184f7167e0eSDag-Erling Smørgrav n -= m; 185*38a52bd3SEd Maste rs->rs_have -= m; 186f7167e0eSDag-Erling Smørgrav } 187*38a52bd3SEd Maste if (rs->rs_have == 0) 188f7167e0eSDag-Erling Smørgrav _rs_rekey(NULL, 0); 189f7167e0eSDag-Erling Smørgrav } 190f7167e0eSDag-Erling Smørgrav } 191f7167e0eSDag-Erling Smørgrav 192f7167e0eSDag-Erling Smørgrav static inline void 193*38a52bd3SEd Maste _rs_random_u32(uint32_t *val) 194f7167e0eSDag-Erling Smørgrav { 195*38a52bd3SEd Maste u_char *keystream; 196*38a52bd3SEd Maste 197f7167e0eSDag-Erling Smørgrav _rs_stir_if_needed(sizeof(*val)); 198*38a52bd3SEd Maste if (rs->rs_have < sizeof(*val)) 199f7167e0eSDag-Erling Smørgrav _rs_rekey(NULL, 0); 200*38a52bd3SEd Maste keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have; 201*38a52bd3SEd Maste memcpy(val, keystream, sizeof(*val)); 202*38a52bd3SEd Maste memset(keystream, 0, sizeof(*val)); 203*38a52bd3SEd Maste rs->rs_have -= sizeof(*val); 204f7167e0eSDag-Erling Smørgrav } 205f7167e0eSDag-Erling Smørgrav 206*38a52bd3SEd Maste uint32_t 207f7167e0eSDag-Erling Smørgrav arc4random(void) 208f7167e0eSDag-Erling Smørgrav { 209*38a52bd3SEd Maste uint32_t val; 210f7167e0eSDag-Erling Smørgrav 211f7167e0eSDag-Erling Smørgrav _ARC4_LOCK(); 212f7167e0eSDag-Erling Smørgrav _rs_random_u32(&val); 213f7167e0eSDag-Erling Smørgrav _ARC4_UNLOCK(); 214f7167e0eSDag-Erling Smørgrav return val; 215f7167e0eSDag-Erling Smørgrav } 216*38a52bd3SEd Maste DEF_WEAK(arc4random); 217f7167e0eSDag-Erling Smørgrav 218f7167e0eSDag-Erling Smørgrav /* 219f7167e0eSDag-Erling Smørgrav * If we are providing arc4random, then we can provide a more efficient 220f7167e0eSDag-Erling Smørgrav * arc4random_buf(). 221f7167e0eSDag-Erling Smørgrav */ 222f7167e0eSDag-Erling Smørgrav # ifndef HAVE_ARC4RANDOM_BUF 223f7167e0eSDag-Erling Smørgrav void 224f7167e0eSDag-Erling Smørgrav arc4random_buf(void *buf, size_t n) 225f7167e0eSDag-Erling Smørgrav { 226f7167e0eSDag-Erling Smørgrav _ARC4_LOCK(); 227f7167e0eSDag-Erling Smørgrav _rs_random_buf(buf, n); 228f7167e0eSDag-Erling Smørgrav _ARC4_UNLOCK(); 229f7167e0eSDag-Erling Smørgrav } 230*38a52bd3SEd Maste DEF_WEAK(arc4random_buf); 231f7167e0eSDag-Erling Smørgrav # endif /* !HAVE_ARC4RANDOM_BUF */ 232f7167e0eSDag-Erling Smørgrav #endif /* !HAVE_ARC4RANDOM */ 233f7167e0eSDag-Erling Smørgrav 234f7167e0eSDag-Erling Smørgrav /* arc4random_buf() that uses platform arc4random() */ 235f7167e0eSDag-Erling Smørgrav #if !defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_ARC4RANDOM) 236f7167e0eSDag-Erling Smørgrav void 237f7167e0eSDag-Erling Smørgrav arc4random_buf(void *_buf, size_t n) 238f7167e0eSDag-Erling Smørgrav { 239f7167e0eSDag-Erling Smørgrav size_t i; 240f7167e0eSDag-Erling Smørgrav u_int32_t r = 0; 241f7167e0eSDag-Erling Smørgrav char *buf = (char *)_buf; 242f7167e0eSDag-Erling Smørgrav 243f7167e0eSDag-Erling Smørgrav for (i = 0; i < n; i++) { 244f7167e0eSDag-Erling Smørgrav if (i % 4 == 0) 245f7167e0eSDag-Erling Smørgrav r = arc4random(); 246f7167e0eSDag-Erling Smørgrav buf[i] = r & 0xff; 247f7167e0eSDag-Erling Smørgrav r >>= 8; 248f7167e0eSDag-Erling Smørgrav } 249a0ee8cc6SDag-Erling Smørgrav explicit_bzero(&r, sizeof(r)); 250f7167e0eSDag-Erling Smørgrav } 251f7167e0eSDag-Erling Smørgrav #endif /* !defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_ARC4RANDOM) */ 252f7167e0eSDag-Erling Smørgrav 253