Blob


1 /* $OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb Exp $ */
3 /*
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>
8 *
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.
12 *
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.
20 */
22 /*
23 * ChaCha based random number generator for OpenBSD.
24 */
26 /* OPENBSD ORIGINAL: lib/libc/crypt/arc4random.c */
28 #include "../config.h"
30 #include <sys/types.h>
32 #include <fcntl.h>
33 #include <limits.h>
34 #include <signal.h>
35 #ifdef HAVE_STDINT_H
36 #include <stdint.h>
37 #endif
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/time.h>
44 #ifndef HAVE_ARC4RANDOM
46 /*
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.
50 */
51 int _ssh_compat_getentropy(void *, size_t);
52 #ifdef getentropy
53 # undef getentropy
54 #endif
55 #define getentropy(x, y) (_ssh_compat_getentropy((x), (y)))
57 #include "log.h"
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 */
67 #define inline
68 #endif /* !__GNUC__ && !_MSC_VER */
70 #define KEYSZ 32
71 #define IVSZ 8
72 #define BLOCKSZ 64
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. */
78 static struct _rs {
79 size_t rs_have; /* valid bytes at end of rs_buf */
80 size_t rs_count; /* bytes till reseed */
81 } *rs;
83 /* Maybe be preserved in fork children, if _rs_allocate() decides. */
84 static struct _rsx {
85 chacha_ctx rs_chacha; /* chacha context for random keystream */
86 u_char rs_buf[RSBUFSZ]; /* keystream blocks */
87 } *rsx;
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);
95 static inline void
96 _rs_init(u_char *buf, size_t n)
97 {
98 if (n < KEYSZ + IVSZ)
99 return;
101 if (rs == NULL) {
102 if (_rs_allocate(&rs, &rsx) == -1)
103 _exit(1);
106 chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
107 chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
110 static void
111 _rs_stir(void)
113 u_char rnd[KEYSZ + IVSZ];
114 uint32_t rekey_fuzz = 0;
116 if (getentropy(rnd, sizeof rnd) == -1)
117 _getentropy_fail();
119 if (!rs)
120 _rs_init(rnd, sizeof(rnd));
121 else
122 _rs_rekey(rnd, sizeof(rnd));
123 explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
125 /* invalidate rs_buf */
126 rs->rs_have = 0;
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);
135 static inline void
136 _rs_stir_if_needed(size_t len)
138 _rs_forkdetect();
139 if (!rs || rs->rs_count <= len)
140 _rs_stir();
141 if (rs->rs_count <= len)
142 rs->rs_count = 0;
143 else
144 rs->rs_count -= len;
147 static inline void
148 _rs_rekey(u_char *dat, size_t datlen)
150 #ifndef KEYSTREAM_ONLY
151 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
152 #endif
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 */
157 if (dat) {
158 size_t i, m;
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;
170 static inline void
171 _rs_random_buf(void *_buf, size_t n)
173 u_char *buf = (u_char *)_buf;
174 u_char *keystream;
175 size_t m;
177 _rs_stir_if_needed(n);
178 while (n > 0) {
179 if (rs->rs_have > 0) {
180 m = minimum(n, rs->rs_have);
181 keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
182 - rs->rs_have;
183 memcpy(buf, keystream, m);
184 memset(keystream, 0, m);
185 buf += m;
186 n -= m;
187 rs->rs_have -= m;
189 if (rs->rs_have == 0)
190 _rs_rekey(NULL, 0);
194 static inline void
195 _rs_random_u32(uint32_t *val)
197 u_char *keystream;
199 _rs_stir_if_needed(sizeof(*val));
200 if (rs->rs_have < sizeof(*val))
201 _rs_rekey(NULL, 0);
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);
208 uint32_t
209 arc4random(void)
211 uint32_t val;
213 _ARC4_LOCK();
214 _rs_random_u32(&val);
215 _ARC4_UNLOCK();
216 return val;
219 /*
220 * If we are providing arc4random, then we can provide a more efficient
221 * arc4random_buf().
222 */
223 # ifndef HAVE_ARC4RANDOM_BUF
224 void
225 arc4random_buf(void *buf, size_t n)
227 _ARC4_LOCK();
228 _rs_random_buf(buf, n);
229 _ARC4_UNLOCK();
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)
236 void
237 arc4random_buf(void *_buf, size_t n)
239 size_t i;
240 u_int32_t r = 0;
241 char *buf = (char *)_buf;
243 for (i = 0; i < n; i++) {
244 if (i % 4 == 0)
245 r = arc4random();
246 buf[i] = r & 0xff;
247 r >>= 8;
249 explicit_bzero(&r, sizeof(r));
251 #endif /* !defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_ARC4RANDOM) */