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 */
28 #include "../config.h"
30 #include <sys/types.h>
41 #include <sys/types.h>
44 #ifndef HAVE_ARC4RANDOM
47 * Always use the getentropy implementation from bsd-getentropy.c, which
48 * will call a native getentropy if available then fall back as required.
49 * We use a different name so that OpenSSL cannot call the wrong getentropy.
51 int _ssh_compat_getentropy(void *, size_t);
55 #define getentropy(x, y) (_ssh_compat_getentropy((x), (y)))
59 #define KEYSTREAM_ONLY
60 #include "chacha_private.h"
62 #define minimum(a, b) ((a) < (b) ? (a) : (b))
64 #if defined(__GNUC__) || defined(_MSC_VER)
65 #define inline __inline
66 #else /* __GNUC__ || _MSC_VER */
68 #endif /* !__GNUC__ && !_MSC_VER */
73 #define RSBUFSZ (16*BLOCKSZ)
75 #define REKEY_BASE (1024*1024) /* NB. should be a power of 2 */
77 /* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
79 size_t rs_have; /* valid bytes at end of rs_buf */
80 size_t rs_count; /* bytes till reseed */
83 /* Maybe be preserved in fork children, if _rs_allocate() decides. */
85 chacha_ctx rs_chacha; /* chacha context for random keystream */
86 u_char rs_buf[RSBUFSZ]; /* keystream blocks */
89 static inline int _rs_allocate(struct _rs **, struct _rsx **);
90 static inline void _rs_forkdetect(void);
91 #include "arc4random.h"
93 static inline void _rs_rekey(u_char *dat, size_t datlen);
96 _rs_init(u_char *buf, size_t n)
102 if (_rs_allocate(&rs, &rsx) == -1)
106 chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
107 chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
113 u_char rnd[KEYSZ + IVSZ];
114 uint32_t rekey_fuzz = 0;
116 if (getentropy(rnd, sizeof rnd) == -1)
120 _rs_init(rnd, sizeof(rnd));
122 _rs_rekey(rnd, sizeof(rnd));
123 explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
125 /* invalidate rs_buf */
127 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
129 /* rekey interval should not be predictable */
130 chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
131 (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
132 rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
136 _rs_stir_if_needed(size_t len)
139 if (!rs || rs->rs_count <= len)
141 if (rs->rs_count <= len)
148 _rs_rekey(u_char *dat, size_t datlen)
150 #ifndef KEYSTREAM_ONLY
151 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
153 /* fill rs_buf with the keystream */
154 chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
155 rsx->rs_buf, sizeof(rsx->rs_buf));
156 /* mix in optional user provided data */
160 m = minimum(datlen, KEYSZ + IVSZ);
161 for (i = 0; i < m; i++)
162 rsx->rs_buf[i] ^= dat[i];
164 /* immediately reinit for backtracking resistance */
165 _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
166 memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
167 rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
171 _rs_random_buf(void *_buf, size_t n)
173 u_char *buf = (u_char *)_buf;
177 _rs_stir_if_needed(n);
179 if (rs->rs_have > 0) {
180 m = minimum(n, rs->rs_have);
181 keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
183 memcpy(buf, keystream, m);
184 memset(keystream, 0, m);
189 if (rs->rs_have == 0)
195 _rs_random_u32(uint32_t *val)
199 _rs_stir_if_needed(sizeof(*val));
200 if (rs->rs_have < sizeof(*val))
202 keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
203 memcpy(val, keystream, sizeof(*val));
204 memset(keystream, 0, sizeof(*val));
205 rs->rs_have -= sizeof(*val);
214 _rs_random_u32(&val);
220 * If we are providing arc4random, then we can provide a more efficient
223 # ifndef HAVE_ARC4RANDOM_BUF
225 arc4random_buf(void *buf, size_t n)
228 _rs_random_buf(buf, n);
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) */