1*e9a2e4d1SXin LI /* $OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb Exp $ */ 2c0b48470SDavid Schultz 383a03b38SAndrey A. Chernov /* 4860c4e58SAndrey A. Chernov * Copyright (c) 1996, David Mazieres <dm@uun.org> 5860c4e58SAndrey A. Chernov * Copyright (c) 2008, Damien Miller <djm@openbsd.org> 6c1e80940SXin LI * Copyright (c) 2013, Markus Friedl <markus@openbsd.org> 7c1e80940SXin LI * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org> 883a03b38SAndrey A. Chernov * 9860c4e58SAndrey A. Chernov * Permission to use, copy, modify, and distribute this software for any 10860c4e58SAndrey A. Chernov * purpose with or without fee is hereby granted, provided that the above 11860c4e58SAndrey A. Chernov * copyright notice and this permission notice appear in all copies. 12860c4e58SAndrey A. Chernov * 13860c4e58SAndrey A. Chernov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14860c4e58SAndrey A. Chernov * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15860c4e58SAndrey A. Chernov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16860c4e58SAndrey A. Chernov * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17860c4e58SAndrey A. Chernov * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18860c4e58SAndrey A. Chernov * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19860c4e58SAndrey A. Chernov * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 2083a03b38SAndrey A. Chernov */ 2183a03b38SAndrey A. Chernov 2283a03b38SAndrey A. Chernov /* 23c1e80940SXin LI * ChaCha based random number generator for OpenBSD. 2483a03b38SAndrey A. Chernov */ 2583a03b38SAndrey A. Chernov 26333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 27333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 28333fc21eSDavid E. O'Brien 29d201fe46SDaniel Eischen #include "namespace.h" 30f8e8a06dSConrad Meyer #if defined(__FreeBSD__) 31f8e8a06dSConrad Meyer #include <assert.h> 32f8e8a06dSConrad Meyer #endif 3383a03b38SAndrey A. Chernov #include <fcntl.h> 347a0789b4SDavid Schultz #include <limits.h> 355295209eSBrian Feldman #include <pthread.h> 36c1e80940SXin LI #include <signal.h> 37c1e80940SXin LI #include <stdint.h> 38c1e80940SXin LI #include <stdlib.h> 39c1e80940SXin LI #include <string.h> 40c1e80940SXin LI #include <unistd.h> 41c1e80940SXin LI #include <sys/types.h> 42c1e80940SXin LI #include <sys/time.h> 435295209eSBrian Feldman 445295209eSBrian Feldman #include "libc_private.h" 45d201fe46SDaniel Eischen #include "un-namespace.h" 4683a03b38SAndrey A. Chernov 47243e0943SConrad Meyer #define CHACHA_EMBED 48c1e80940SXin LI #define KEYSTREAM_ONLY 49c2ca0667SAlex Richardson #if defined(__FreeBSD__) 50c2ca0667SAlex Richardson #define ARC4RANDOM_FXRNG 1 51c2ca0667SAlex Richardson #else 52c2ca0667SAlex Richardson #define ARC4RANDOM_FXRNG 0 53c2ca0667SAlex Richardson #endif 54c1e80940SXin LI #include "chacha.c" 55c1e80940SXin LI 56c1e80940SXin LI #define minimum(a, b) ((a) < (b) ? (a) : (b)) 57c1e80940SXin LI 58c1e80940SXin LI #if defined(__GNUC__) || defined(_MSC_VER) 59c0b48470SDavid Schultz #define inline __inline 60c1e80940SXin LI #else /* __GNUC__ || _MSC_VER */ 61c0b48470SDavid Schultz #define inline 62c1e80940SXin LI #endif /* !__GNUC__ && !_MSC_VER */ 63c0b48470SDavid Schultz 64c1e80940SXin LI #define KEYSZ 32 65c1e80940SXin LI #define IVSZ 8 66c1e80940SXin LI #define BLOCKSZ 64 67c1e80940SXin LI #define RSBUFSZ (16*BLOCKSZ) 6883a03b38SAndrey A. Chernov 69*e9a2e4d1SXin LI #define REKEY_BASE (1024*1024) /* NB. should be a power of 2 */ 70*e9a2e4d1SXin LI 71c1e80940SXin LI /* Marked INHERIT_ZERO, so zero'd out in fork children. */ 72c1e80940SXin LI static struct _rs { 73c1e80940SXin LI size_t rs_have; /* valid bytes at end of rs_buf */ 74c1e80940SXin LI size_t rs_count; /* bytes till reseed */ 75c1e80940SXin LI } *rs; 765295209eSBrian Feldman 77c1e80940SXin LI /* Maybe be preserved in fork children, if _rs_allocate() decides. */ 78c1e80940SXin LI static struct _rsx { 79c1e80940SXin LI chacha_ctx rs_chacha; /* chacha context for random keystream */ 80c1e80940SXin LI u_char rs_buf[RSBUFSZ]; /* keystream blocks */ 81f8e8a06dSConrad Meyer #ifdef __FreeBSD__ 82f8e8a06dSConrad Meyer uint32_t rs_seed_generation; /* 32-bit userspace RNG version */ 83f8e8a06dSConrad Meyer #endif 84c1e80940SXin LI } *rsx; 855295209eSBrian Feldman 86c1e80940SXin LI static inline int _rs_allocate(struct _rs **, struct _rsx **); 87c1e80940SXin LI static inline void _rs_forkdetect(void); 88c1e80940SXin LI #include "arc4random.h" 895295209eSBrian Feldman 90c1e80940SXin LI static inline void _rs_rekey(u_char *dat, size_t datlen); 9160ce8b0eSDavid Schultz 9283a03b38SAndrey A. Chernov static inline void 93c1e80940SXin LI _rs_init(u_char *buf, size_t n) 9483a03b38SAndrey A. Chernov { 95c1e80940SXin LI if (n < KEYSZ + IVSZ) 96c1e80940SXin LI return; 9783a03b38SAndrey A. Chernov 98c1e80940SXin LI if (rs == NULL) { 99c1e80940SXin LI if (_rs_allocate(&rs, &rsx) == -1) 100d25a1430SXin LI _exit(1); 10149a6e1baSEd Maste } 10283a03b38SAndrey A. Chernov 103c1e80940SXin LI chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8); 104c1e80940SXin LI chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ, NULL); 10583a03b38SAndrey A. Chernov } 10683a03b38SAndrey A. Chernov 1077a0789b4SDavid Schultz static void 108c1e80940SXin LI _rs_stir(void) 1097a0789b4SDavid Schultz { 110c1e80940SXin LI u_char rnd[KEYSZ + IVSZ]; 111*e9a2e4d1SXin LI uint32_t rekey_fuzz = 0; 1127a0789b4SDavid Schultz 113f8e8a06dSConrad Meyer #if defined(__FreeBSD__) 114f8e8a06dSConrad Meyer bool need_init; 115f8e8a06dSConrad Meyer 116f8e8a06dSConrad Meyer /* 117f8e8a06dSConrad Meyer * De-couple allocation (which locates the vdso_fxrngp pointer in 118f8e8a06dSConrad Meyer * auxinfo) from initialization. This allows us to read the root seed 119f8e8a06dSConrad Meyer * version before we fetch system entropy, maintaining the invariant 120f8e8a06dSConrad Meyer * that the PRF was seeded with entropy from rs_seed_generation or a 121f8e8a06dSConrad Meyer * later generation. But never seeded from an earlier generation. 122f8e8a06dSConrad Meyer * This invariant prevents us from missing a root reseed event. 123f8e8a06dSConrad Meyer */ 124f8e8a06dSConrad Meyer need_init = false; 125f8e8a06dSConrad Meyer if (rs == NULL) { 126f8e8a06dSConrad Meyer if (_rs_allocate(&rs, &rsx) == -1) 127f8e8a06dSConrad Meyer abort(); 128f8e8a06dSConrad Meyer need_init = true; 129f8e8a06dSConrad Meyer } 130f8e8a06dSConrad Meyer /* 131f8e8a06dSConrad Meyer * Transition period: new userspace on old kernel. This should become 132f8e8a06dSConrad Meyer * a hard error at some point, if the scheme is adopted. 133f8e8a06dSConrad Meyer */ 134f8e8a06dSConrad Meyer if (vdso_fxrngp != NULL) 135f8e8a06dSConrad Meyer rsx->rs_seed_generation = 136f8e8a06dSConrad Meyer fxrng_load_acq_generation(&vdso_fxrngp->fx_generation32); 137f8e8a06dSConrad Meyer #endif 138f8e8a06dSConrad Meyer 139c1e80940SXin LI if (getentropy(rnd, sizeof rnd) == -1) 140c1e80940SXin LI _getentropy_fail(); 141c1e80940SXin LI 142f8e8a06dSConrad Meyer #if !defined(__FreeBSD__) 143c1e80940SXin LI if (!rs) 144c1e80940SXin LI _rs_init(rnd, sizeof(rnd)); 145f8e8a06dSConrad Meyer #else /* __FreeBSD__ */ 146f8e8a06dSConrad Meyer assert(rs != NULL); 147f8e8a06dSConrad Meyer if (need_init) 148f8e8a06dSConrad Meyer _rs_init(rnd, sizeof(rnd)); 149f8e8a06dSConrad Meyer #endif 150c1e80940SXin LI else 151c1e80940SXin LI _rs_rekey(rnd, sizeof(rnd)); 152c1e80940SXin LI explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */ 153c1e80940SXin LI 154c1e80940SXin LI /* invalidate rs_buf */ 155c1e80940SXin LI rs->rs_have = 0; 156c1e80940SXin LI memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf)); 157c1e80940SXin LI 158*e9a2e4d1SXin LI /* rekey interval should not be predictable */ 159*e9a2e4d1SXin LI chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz, 160*e9a2e4d1SXin LI (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz)); 161*e9a2e4d1SXin LI rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE); 1627a0789b4SDavid Schultz } 1637a0789b4SDavid Schultz 164c1e80940SXin LI static inline void 165c1e80940SXin LI _rs_stir_if_needed(size_t len) 16683a03b38SAndrey A. Chernov { 167c1e80940SXin LI _rs_forkdetect(); 168c1e80940SXin LI if (!rs || rs->rs_count <= len) 169c1e80940SXin LI _rs_stir(); 170c1e80940SXin LI if (rs->rs_count <= len) 171c1e80940SXin LI rs->rs_count = 0; 172c1e80940SXin LI else 173c1e80940SXin LI rs->rs_count -= len; 17483a03b38SAndrey A. Chernov } 17583a03b38SAndrey A. Chernov 176c1e80940SXin LI static inline void 177c1e80940SXin LI _rs_rekey(u_char *dat, size_t datlen) 17883a03b38SAndrey A. Chernov { 179c1e80940SXin LI #ifndef KEYSTREAM_ONLY 180c1e80940SXin LI memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf)); 181c1e80940SXin LI #endif 182c1e80940SXin LI /* fill rs_buf with the keystream */ 183c1e80940SXin LI chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf, 184c1e80940SXin LI rsx->rs_buf, sizeof(rsx->rs_buf)); 185c1e80940SXin LI /* mix in optional user provided data */ 186c1e80940SXin LI if (dat) { 187c1e80940SXin LI size_t i, m; 188c1e80940SXin LI 189c1e80940SXin LI m = minimum(datlen, KEYSZ + IVSZ); 190c1e80940SXin LI for (i = 0; i < m; i++) 191c1e80940SXin LI rsx->rs_buf[i] ^= dat[i]; 192c1e80940SXin LI } 193c1e80940SXin LI /* immediately reinit for backtracking resistance */ 194c1e80940SXin LI _rs_init(rsx->rs_buf, KEYSZ + IVSZ); 195c1e80940SXin LI memset(rsx->rs_buf, 0, KEYSZ + IVSZ); 196c1e80940SXin LI rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ; 19783a03b38SAndrey A. Chernov } 19883a03b38SAndrey A. Chernov 199c1e80940SXin LI static inline void 200c1e80940SXin LI _rs_random_buf(void *_buf, size_t n) 201bc6847e2SAndrey A. Chernov { 202bc6847e2SAndrey A. Chernov u_char *buf = (u_char *)_buf; 203c1e80940SXin LI u_char *keystream; 204c1e80940SXin LI size_t m; 205c1e80940SXin LI 206c1e80940SXin LI _rs_stir_if_needed(n); 207c1e80940SXin LI while (n > 0) { 208c1e80940SXin LI if (rs->rs_have > 0) { 209c1e80940SXin LI m = minimum(n, rs->rs_have); 210c1e80940SXin LI keystream = rsx->rs_buf + sizeof(rsx->rs_buf) 211c1e80940SXin LI - rs->rs_have; 212c1e80940SXin LI memcpy(buf, keystream, m); 213c1e80940SXin LI memset(keystream, 0, m); 214c1e80940SXin LI buf += m; 215c1e80940SXin LI n -= m; 216c1e80940SXin LI rs->rs_have -= m; 217bc6847e2SAndrey A. Chernov } 218c1e80940SXin LI if (rs->rs_have == 0) 219c1e80940SXin LI _rs_rekey(NULL, 0); 220c1e80940SXin LI } 221c1e80940SXin LI } 222c1e80940SXin LI 223c1e80940SXin LI static inline void 224c1e80940SXin LI _rs_random_u32(uint32_t *val) 225c1e80940SXin LI { 226c1e80940SXin LI u_char *keystream; 227c1e80940SXin LI 228c1e80940SXin LI _rs_stir_if_needed(sizeof(*val)); 229c1e80940SXin LI if (rs->rs_have < sizeof(*val)) 230c1e80940SXin LI _rs_rekey(NULL, 0); 231c1e80940SXin LI keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have; 232c1e80940SXin LI memcpy(val, keystream, sizeof(*val)); 233c1e80940SXin LI memset(keystream, 0, sizeof(*val)); 234c1e80940SXin LI rs->rs_have -= sizeof(*val); 235c1e80940SXin LI } 236c1e80940SXin LI 237c1e80940SXin LI uint32_t 238c1e80940SXin LI arc4random(void) 239c1e80940SXin LI { 240c1e80940SXin LI uint32_t val; 241c1e80940SXin LI 242c1e80940SXin LI _ARC4_LOCK(); 243c1e80940SXin LI _rs_random_u32(&val); 244c1e80940SXin LI _ARC4_UNLOCK(); 245c1e80940SXin LI return val; 246c1e80940SXin LI } 247c1e80940SXin LI 248c1e80940SXin LI void 249c1e80940SXin LI arc4random_buf(void *buf, size_t n) 250c1e80940SXin LI { 251c1e80940SXin LI _ARC4_LOCK(); 252c1e80940SXin LI _rs_random_buf(buf, n); 253c0b48470SDavid Schultz _ARC4_UNLOCK(); 254bc6847e2SAndrey A. Chernov } 255