1 /* $OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb Exp $ */
4 * Copyright (c) 1996, David Mazieres <dm@uun.org>
5 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6 * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7 * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * ChaCha based random number generator for OpenBSD.
26 /* OPENBSD ORIGINAL: lib/libc/crypt/arc4random.c */
30 #include <sys/types.h>
41 #include <sys/types.h>
44 #ifndef HAVE_ARC4RANDOM
47 * If we're not using a native getentropy, use the one from bsd-getentropy.c
48 * under a different name, so that if in future these binaries are run on
49 * a system that has a native getentropy OpenSSL cannot call the wrong one.
51 #ifndef HAVE_GETENTROPY
52 # define getentropy(x, y) (_ssh_compat_getentropy((x), (y)))
57 #define KEYSTREAM_ONLY
58 #include "chacha_private.h"
60 #define minimum(a, b) ((a) < (b) ? (a) : (b))
62 #if defined(__GNUC__) || defined(_MSC_VER)
63 #define inline __inline
64 #else /* __GNUC__ || _MSC_VER */
66 #endif /* !__GNUC__ && !_MSC_VER */
71 #define RSBUFSZ (16*BLOCKSZ)
73 #define REKEY_BASE (1024*1024) /* NB. should be a power of 2 */
75 /* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
77 size_t rs_have; /* valid bytes at end of rs_buf */
78 size_t rs_count; /* bytes till reseed */
81 /* Maybe be preserved in fork children, if _rs_allocate() decides. */
83 chacha_ctx rs_chacha; /* chacha context for random keystream */
84 u_char rs_buf[RSBUFSZ]; /* keystream blocks */
87 static inline int _rs_allocate(struct _rs **, struct _rsx **);
88 static inline void _rs_forkdetect(void);
89 #include "arc4random.h"
91 static inline void _rs_rekey(u_char *dat, size_t datlen);
94 _rs_init(u_char *buf, size_t n)
100 if (_rs_allocate(&rs, &rsx) == -1)
104 chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
105 chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
111 u_char rnd[KEYSZ + IVSZ];
112 uint32_t rekey_fuzz = 0;
114 if (getentropy(rnd, sizeof rnd) == -1)
118 _rs_init(rnd, sizeof(rnd));
120 _rs_rekey(rnd, sizeof(rnd));
121 explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
123 /* invalidate rs_buf */
125 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
127 /* rekey interval should not be predictable */
128 chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
129 (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
130 rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
134 _rs_stir_if_needed(size_t len)
137 if (!rs || rs->rs_count <= len)
139 if (rs->rs_count <= len)
146 _rs_rekey(u_char *dat, size_t datlen)
148 #ifndef KEYSTREAM_ONLY
149 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
151 /* fill rs_buf with the keystream */
152 chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
153 rsx->rs_buf, sizeof(rsx->rs_buf));
154 /* mix in optional user provided data */
158 m = minimum(datlen, KEYSZ + IVSZ);
159 for (i = 0; i < m; i++)
160 rsx->rs_buf[i] ^= dat[i];
162 /* immediately reinit for backtracking resistance */
163 _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
164 memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
165 rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
169 _rs_random_buf(void *_buf, size_t n)
171 u_char *buf = (u_char *)_buf;
175 _rs_stir_if_needed(n);
177 if (rs->rs_have > 0) {
178 m = minimum(n, rs->rs_have);
179 keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
181 memcpy(buf, keystream, m);
182 memset(keystream, 0, m);
187 if (rs->rs_have == 0)
193 _rs_random_u32(uint32_t *val)
197 _rs_stir_if_needed(sizeof(*val));
198 if (rs->rs_have < sizeof(*val))
200 keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
201 memcpy(val, keystream, sizeof(*val));
202 memset(keystream, 0, sizeof(*val));
203 rs->rs_have -= sizeof(*val);
212 _rs_random_u32(&val);
216 DEF_WEAK(arc4random);
219 * If we are providing arc4random, then we can provide a more efficient
222 # ifndef HAVE_ARC4RANDOM_BUF
224 arc4random_buf(void *buf, size_t n)
227 _rs_random_buf(buf, n);
230 DEF_WEAK(arc4random_buf);
231 # endif /* !HAVE_ARC4RANDOM_BUF */
232 #endif /* !HAVE_ARC4RANDOM */
234 /* arc4random_buf() that uses platform arc4random() */
235 #if !defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_ARC4RANDOM)
237 arc4random_buf(void *_buf, size_t n)
241 char *buf = (char *)_buf;
243 for (i = 0; i < n; i++) {
249 explicit_bzero(&r, sizeof(r));
251 #endif /* !defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_ARC4RANDOM) */