1 f9ab77a8 2023-08-23 op /* $OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb Exp $ */
4 f9ab77a8 2023-08-23 op * Copyright (c) 1996, David Mazieres <dm@uun.org>
5 f9ab77a8 2023-08-23 op * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6 f9ab77a8 2023-08-23 op * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7 f9ab77a8 2023-08-23 op * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
9 f9ab77a8 2023-08-23 op * Permission to use, copy, modify, and distribute this software for any
10 f9ab77a8 2023-08-23 op * purpose with or without fee is hereby granted, provided that the above
11 f9ab77a8 2023-08-23 op * copyright notice and this permission notice appear in all copies.
13 f9ab77a8 2023-08-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 f9ab77a8 2023-08-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 f9ab77a8 2023-08-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 f9ab77a8 2023-08-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 f9ab77a8 2023-08-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 f9ab77a8 2023-08-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 f9ab77a8 2023-08-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 f9ab77a8 2023-08-23 op * ChaCha based random number generator for OpenBSD.
26 f9ab77a8 2023-08-23 op /* OPENBSD ORIGINAL: lib/libc/crypt/arc4random.c */
28 f9ab77a8 2023-08-23 op #include "../config.h"
30 f9ab77a8 2023-08-23 op #include <sys/types.h>
32 f9ab77a8 2023-08-23 op #include <fcntl.h>
33 f9ab77a8 2023-08-23 op #include <limits.h>
34 f9ab77a8 2023-08-23 op #include <signal.h>
35 f9ab77a8 2023-08-23 op #ifdef HAVE_STDINT_H
36 f9ab77a8 2023-08-23 op #include <stdint.h>
38 f9ab77a8 2023-08-23 op #include <stdlib.h>
39 f9ab77a8 2023-08-23 op #include <string.h>
40 f9ab77a8 2023-08-23 op #include <unistd.h>
41 f9ab77a8 2023-08-23 op #include <sys/types.h>
42 f9ab77a8 2023-08-23 op #include <sys/time.h>
44 f9ab77a8 2023-08-23 op #ifndef HAVE_ARC4RANDOM
47 f9ab77a8 2023-08-23 op * Always use the getentropy implementation from bsd-getentropy.c, which
48 f9ab77a8 2023-08-23 op * will call a native getentropy if available then fall back as required.
49 f9ab77a8 2023-08-23 op * We use a different name so that OpenSSL cannot call the wrong getentropy.
51 f9ab77a8 2023-08-23 op int _ssh_compat_getentropy(void *, size_t);
52 f9ab77a8 2023-08-23 op #ifdef getentropy
53 f9ab77a8 2023-08-23 op # undef getentropy
55 f9ab77a8 2023-08-23 op #define getentropy(x, y) (_ssh_compat_getentropy((x), (y)))
57 f9ab77a8 2023-08-23 op #include "log.h"
59 f9ab77a8 2023-08-23 op #define KEYSTREAM_ONLY
60 f9ab77a8 2023-08-23 op #include "chacha_private.h"
62 f9ab77a8 2023-08-23 op #define minimum(a, b) ((a) < (b) ? (a) : (b))
64 f9ab77a8 2023-08-23 op #if defined(__GNUC__) || defined(_MSC_VER)
65 f9ab77a8 2023-08-23 op #define inline __inline
66 f9ab77a8 2023-08-23 op #else /* __GNUC__ || _MSC_VER */
67 f9ab77a8 2023-08-23 op #define inline
68 f9ab77a8 2023-08-23 op #endif /* !__GNUC__ && !_MSC_VER */
70 f9ab77a8 2023-08-23 op #define KEYSZ 32
71 f9ab77a8 2023-08-23 op #define IVSZ 8
72 f9ab77a8 2023-08-23 op #define BLOCKSZ 64
73 f9ab77a8 2023-08-23 op #define RSBUFSZ (16*BLOCKSZ)
75 f9ab77a8 2023-08-23 op #define REKEY_BASE (1024*1024) /* NB. should be a power of 2 */
77 f9ab77a8 2023-08-23 op /* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
78 f9ab77a8 2023-08-23 op static struct _rs {
79 f9ab77a8 2023-08-23 op size_t rs_have; /* valid bytes at end of rs_buf */
80 f9ab77a8 2023-08-23 op size_t rs_count; /* bytes till reseed */
83 f9ab77a8 2023-08-23 op /* Maybe be preserved in fork children, if _rs_allocate() decides. */
84 f9ab77a8 2023-08-23 op static struct _rsx {
85 f9ab77a8 2023-08-23 op chacha_ctx rs_chacha; /* chacha context for random keystream */
86 f9ab77a8 2023-08-23 op u_char rs_buf[RSBUFSZ]; /* keystream blocks */
89 f9ab77a8 2023-08-23 op static inline int _rs_allocate(struct _rs **, struct _rsx **);
90 f9ab77a8 2023-08-23 op static inline void _rs_forkdetect(void);
91 f9ab77a8 2023-08-23 op #include "arc4random.h"
93 f9ab77a8 2023-08-23 op static inline void _rs_rekey(u_char *dat, size_t datlen);
95 f9ab77a8 2023-08-23 op static inline void
96 f9ab77a8 2023-08-23 op _rs_init(u_char *buf, size_t n)
98 f9ab77a8 2023-08-23 op if (n < KEYSZ + IVSZ)
101 f9ab77a8 2023-08-23 op if (rs == NULL) {
102 f9ab77a8 2023-08-23 op if (_rs_allocate(&rs, &rsx) == -1)
106 f9ab77a8 2023-08-23 op chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
107 f9ab77a8 2023-08-23 op chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
111 f9ab77a8 2023-08-23 op _rs_stir(void)
113 f9ab77a8 2023-08-23 op u_char rnd[KEYSZ + IVSZ];
114 f9ab77a8 2023-08-23 op uint32_t rekey_fuzz = 0;
116 f9ab77a8 2023-08-23 op if (getentropy(rnd, sizeof rnd) == -1)
117 f9ab77a8 2023-08-23 op _getentropy_fail();
120 f9ab77a8 2023-08-23 op _rs_init(rnd, sizeof(rnd));
122 f9ab77a8 2023-08-23 op _rs_rekey(rnd, sizeof(rnd));
123 f9ab77a8 2023-08-23 op explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
125 f9ab77a8 2023-08-23 op /* invalidate rs_buf */
126 f9ab77a8 2023-08-23 op rs->rs_have = 0;
127 f9ab77a8 2023-08-23 op memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
129 f9ab77a8 2023-08-23 op /* rekey interval should not be predictable */
130 f9ab77a8 2023-08-23 op chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
131 f9ab77a8 2023-08-23 op (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
132 f9ab77a8 2023-08-23 op rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
135 f9ab77a8 2023-08-23 op static inline void
136 f9ab77a8 2023-08-23 op _rs_stir_if_needed(size_t len)
138 f9ab77a8 2023-08-23 op _rs_forkdetect();
139 f9ab77a8 2023-08-23 op if (!rs || rs->rs_count <= len)
141 f9ab77a8 2023-08-23 op if (rs->rs_count <= len)
142 f9ab77a8 2023-08-23 op rs->rs_count = 0;
144 f9ab77a8 2023-08-23 op rs->rs_count -= len;
147 f9ab77a8 2023-08-23 op static inline void
148 f9ab77a8 2023-08-23 op _rs_rekey(u_char *dat, size_t datlen)
150 f9ab77a8 2023-08-23 op #ifndef KEYSTREAM_ONLY
151 f9ab77a8 2023-08-23 op memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
153 f9ab77a8 2023-08-23 op /* fill rs_buf with the keystream */
154 f9ab77a8 2023-08-23 op chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
155 f9ab77a8 2023-08-23 op rsx->rs_buf, sizeof(rsx->rs_buf));
156 f9ab77a8 2023-08-23 op /* mix in optional user provided data */
160 f9ab77a8 2023-08-23 op m = minimum(datlen, KEYSZ + IVSZ);
161 f9ab77a8 2023-08-23 op for (i = 0; i < m; i++)
162 f9ab77a8 2023-08-23 op rsx->rs_buf[i] ^= dat[i];
164 f9ab77a8 2023-08-23 op /* immediately reinit for backtracking resistance */
165 f9ab77a8 2023-08-23 op _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
166 f9ab77a8 2023-08-23 op memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
167 f9ab77a8 2023-08-23 op rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
170 f9ab77a8 2023-08-23 op static inline void
171 f9ab77a8 2023-08-23 op _rs_random_buf(void *_buf, size_t n)
173 f9ab77a8 2023-08-23 op u_char *buf = (u_char *)_buf;
174 f9ab77a8 2023-08-23 op u_char *keystream;
177 f9ab77a8 2023-08-23 op _rs_stir_if_needed(n);
178 f9ab77a8 2023-08-23 op while (n > 0) {
179 f9ab77a8 2023-08-23 op if (rs->rs_have > 0) {
180 f9ab77a8 2023-08-23 op m = minimum(n, rs->rs_have);
181 f9ab77a8 2023-08-23 op keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
182 f9ab77a8 2023-08-23 op - rs->rs_have;
183 f9ab77a8 2023-08-23 op memcpy(buf, keystream, m);
184 f9ab77a8 2023-08-23 op memset(keystream, 0, m);
187 f9ab77a8 2023-08-23 op rs->rs_have -= m;
189 f9ab77a8 2023-08-23 op if (rs->rs_have == 0)
190 f9ab77a8 2023-08-23 op _rs_rekey(NULL, 0);
194 f9ab77a8 2023-08-23 op static inline void
195 f9ab77a8 2023-08-23 op _rs_random_u32(uint32_t *val)
197 f9ab77a8 2023-08-23 op u_char *keystream;
199 f9ab77a8 2023-08-23 op _rs_stir_if_needed(sizeof(*val));
200 f9ab77a8 2023-08-23 op if (rs->rs_have < sizeof(*val))
201 f9ab77a8 2023-08-23 op _rs_rekey(NULL, 0);
202 f9ab77a8 2023-08-23 op keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
203 f9ab77a8 2023-08-23 op memcpy(val, keystream, sizeof(*val));
204 f9ab77a8 2023-08-23 op memset(keystream, 0, sizeof(*val));
205 f9ab77a8 2023-08-23 op rs->rs_have -= sizeof(*val);
209 f9ab77a8 2023-08-23 op arc4random(void)
211 f9ab77a8 2023-08-23 op uint32_t val;
213 f9ab77a8 2023-08-23 op _ARC4_LOCK();
214 f9ab77a8 2023-08-23 op _rs_random_u32(&val);
215 f9ab77a8 2023-08-23 op _ARC4_UNLOCK();
220 f9ab77a8 2023-08-23 op * If we are providing arc4random, then we can provide a more efficient
221 f9ab77a8 2023-08-23 op * arc4random_buf().
223 f9ab77a8 2023-08-23 op # ifndef HAVE_ARC4RANDOM_BUF
225 f9ab77a8 2023-08-23 op arc4random_buf(void *buf, size_t n)
227 f9ab77a8 2023-08-23 op _ARC4_LOCK();
228 f9ab77a8 2023-08-23 op _rs_random_buf(buf, n);
229 f9ab77a8 2023-08-23 op _ARC4_UNLOCK();
231 f9ab77a8 2023-08-23 op # endif /* !HAVE_ARC4RANDOM_BUF */
232 f9ab77a8 2023-08-23 op #endif /* !HAVE_ARC4RANDOM */
234 f9ab77a8 2023-08-23 op /* arc4random_buf() that uses platform arc4random() */
235 f9ab77a8 2023-08-23 op #if !defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_ARC4RANDOM)
237 f9ab77a8 2023-08-23 op arc4random_buf(void *_buf, size_t n)
240 f9ab77a8 2023-08-23 op u_int32_t r = 0;
241 f9ab77a8 2023-08-23 op char *buf = (char *)_buf;
243 f9ab77a8 2023-08-23 op for (i = 0; i < n; i++) {
244 f9ab77a8 2023-08-23 op if (i % 4 == 0)
245 f9ab77a8 2023-08-23 op r = arc4random();
246 f9ab77a8 2023-08-23 op buf[i] = r & 0xff;
249 f9ab77a8 2023-08-23 op explicit_bzero(&r, sizeof(r));
251 f9ab77a8 2023-08-23 op #endif /* !defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_ARC4RANDOM) */