1 /* $OpenBSD: arc4random.c,v 1.55 2019/03/24 17:56:54 deraadt 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 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include "namespace.h"
30 #if defined(__FreeBSD__)
41 #include <sys/types.h>
44 #include "libc_private.h"
45 #include "un-namespace.h"
48 #define KEYSTREAM_ONLY
49 #if defined(__FreeBSD__)
50 #define ARC4RANDOM_FXRNG 1
52 #define ARC4RANDOM_FXRNG 0
56 #define minimum(a, b) ((a) < (b) ? (a) : (b))
58 #if defined(__GNUC__) || defined(_MSC_VER)
59 #define inline __inline
60 #else /* __GNUC__ || _MSC_VER */
62 #endif /* !__GNUC__ && !_MSC_VER */
67 #define RSBUFSZ (16*BLOCKSZ)
69 /* Marked INHERIT_ZERO, so zero'd out in fork children. */
71 size_t rs_have; /* valid bytes at end of rs_buf */
72 size_t rs_count; /* bytes till reseed */
75 /* Maybe be preserved in fork children, if _rs_allocate() decides. */
77 chacha_ctx rs_chacha; /* chacha context for random keystream */
78 u_char rs_buf[RSBUFSZ]; /* keystream blocks */
80 uint32_t rs_seed_generation; /* 32-bit userspace RNG version */
84 static inline int _rs_allocate(struct _rs **, struct _rsx **);
85 static inline void _rs_forkdetect(void);
86 #include "arc4random.h"
88 static inline void _rs_rekey(u_char *dat, size_t datlen);
91 _rs_init(u_char *buf, size_t n)
97 if (_rs_allocate(&rs, &rsx) == -1)
101 chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
102 chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ, NULL);
108 u_char rnd[KEYSZ + IVSZ];
110 #if defined(__FreeBSD__)
114 * De-couple allocation (which locates the vdso_fxrngp pointer in
115 * auxinfo) from initialization. This allows us to read the root seed
116 * version before we fetch system entropy, maintaining the invariant
117 * that the PRF was seeded with entropy from rs_seed_generation or a
118 * later generation. But never seeded from an earlier generation.
119 * This invariant prevents us from missing a root reseed event.
123 if (_rs_allocate(&rs, &rsx) == -1)
128 * Transition period: new userspace on old kernel. This should become
129 * a hard error at some point, if the scheme is adopted.
131 if (vdso_fxrngp != NULL)
132 rsx->rs_seed_generation =
133 fxrng_load_acq_generation(&vdso_fxrngp->fx_generation32);
136 if (getentropy(rnd, sizeof rnd) == -1)
139 #if !defined(__FreeBSD__)
141 _rs_init(rnd, sizeof(rnd));
142 #else /* __FreeBSD__ */
145 _rs_init(rnd, sizeof(rnd));
148 _rs_rekey(rnd, sizeof(rnd));
149 explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
151 /* invalidate rs_buf */
153 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
155 rs->rs_count = 1600000;
159 _rs_stir_if_needed(size_t len)
162 if (!rs || rs->rs_count <= len)
164 if (rs->rs_count <= len)
171 _rs_rekey(u_char *dat, size_t datlen)
173 #ifndef KEYSTREAM_ONLY
174 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
176 /* fill rs_buf with the keystream */
177 chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
178 rsx->rs_buf, sizeof(rsx->rs_buf));
179 /* mix in optional user provided data */
183 m = minimum(datlen, KEYSZ + IVSZ);
184 for (i = 0; i < m; i++)
185 rsx->rs_buf[i] ^= dat[i];
187 /* immediately reinit for backtracking resistance */
188 _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
189 memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
190 rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
194 _rs_random_buf(void *_buf, size_t n)
196 u_char *buf = (u_char *)_buf;
200 _rs_stir_if_needed(n);
202 if (rs->rs_have > 0) {
203 m = minimum(n, rs->rs_have);
204 keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
206 memcpy(buf, keystream, m);
207 memset(keystream, 0, m);
212 if (rs->rs_have == 0)
218 _rs_random_u32(uint32_t *val)
222 _rs_stir_if_needed(sizeof(*val));
223 if (rs->rs_have < sizeof(*val))
225 keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
226 memcpy(val, keystream, sizeof(*val));
227 memset(keystream, 0, sizeof(*val));
228 rs->rs_have -= sizeof(*val);
237 _rs_random_u32(&val);
243 arc4random_buf(void *buf, size_t n)
246 _rs_random_buf(buf, n);