138a52bd3SEd Maste /* $OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb Exp $ */
2f7167e0eSDag-Erling Smørgrav
3f7167e0eSDag-Erling Smørgrav /*
4f7167e0eSDag-Erling Smørgrav * Copyright (c) 1996, David Mazieres <dm@uun.org>
5f7167e0eSDag-Erling Smørgrav * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6f7167e0eSDag-Erling Smørgrav * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
738a52bd3SEd Maste * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
8f7167e0eSDag-Erling Smørgrav *
9f7167e0eSDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any
10f7167e0eSDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above
11f7167e0eSDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies.
12f7167e0eSDag-Erling Smørgrav *
13f7167e0eSDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14f7167e0eSDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15f7167e0eSDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16f7167e0eSDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17f7167e0eSDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18f7167e0eSDag-Erling Smørgrav * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19f7167e0eSDag-Erling Smørgrav * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20f7167e0eSDag-Erling Smørgrav */
21f7167e0eSDag-Erling Smørgrav
22f7167e0eSDag-Erling Smørgrav /*
23f7167e0eSDag-Erling Smørgrav * ChaCha based random number generator for OpenBSD.
24f7167e0eSDag-Erling Smørgrav */
25f7167e0eSDag-Erling Smørgrav
2638a52bd3SEd Maste /* OPENBSD ORIGINAL: lib/libc/crypt/arc4random.c */
2738a52bd3SEd Maste
28f7167e0eSDag-Erling Smørgrav #include "includes.h"
29f7167e0eSDag-Erling Smørgrav
30bc5531deSDag-Erling Smørgrav #include <sys/types.h>
31bc5531deSDag-Erling Smørgrav
32bc5531deSDag-Erling Smørgrav #include <fcntl.h>
3338a52bd3SEd Maste #include <limits.h>
3438a52bd3SEd Maste #include <signal.h>
3538a52bd3SEd Maste #ifdef HAVE_STDINT_H
3638a52bd3SEd Maste #include <stdint.h>
3738a52bd3SEd Maste #endif
38f7167e0eSDag-Erling Smørgrav #include <stdlib.h>
39f7167e0eSDag-Erling Smørgrav #include <string.h>
40f7167e0eSDag-Erling Smørgrav #include <unistd.h>
4138a52bd3SEd Maste #include <sys/types.h>
4238a52bd3SEd Maste #include <sys/time.h>
43190cef3dSDag-Erling Smørgrav
44f7167e0eSDag-Erling Smørgrav #ifndef HAVE_ARC4RANDOM
45f7167e0eSDag-Erling Smørgrav
4638a52bd3SEd Maste /*
47*f374ba41SEd Maste * Always use the getentropy implementation from bsd-getentropy.c, which
48*f374ba41SEd Maste * will call a native getentropy if available then fall back as required.
49*f374ba41SEd Maste * We use a different name so that OpenSSL cannot call the wrong getentropy.
5038a52bd3SEd Maste */
51*f374ba41SEd Maste int _ssh_compat_getentropy(void *, size_t);
52*f374ba41SEd Maste #ifdef getentropy
53*f374ba41SEd Maste # undef getentropy
54bc5531deSDag-Erling Smørgrav #endif
55*f374ba41SEd Maste #define getentropy(x, y) (_ssh_compat_getentropy((x), (y)))
56f7167e0eSDag-Erling Smørgrav
57f7167e0eSDag-Erling Smørgrav #include "log.h"
58f7167e0eSDag-Erling Smørgrav
59f7167e0eSDag-Erling Smørgrav #define KEYSTREAM_ONLY
60f7167e0eSDag-Erling Smørgrav #include "chacha_private.h"
61f7167e0eSDag-Erling Smørgrav
6238a52bd3SEd Maste #define minimum(a, b) ((a) < (b) ? (a) : (b))
63f7167e0eSDag-Erling Smørgrav
6438a52bd3SEd Maste #if defined(__GNUC__) || defined(_MSC_VER)
6538a52bd3SEd Maste #define inline __inline
6638a52bd3SEd Maste #else /* __GNUC__ || _MSC_VER */
6738a52bd3SEd Maste #define inline
6838a52bd3SEd Maste #endif /* !__GNUC__ && !_MSC_VER */
69f7167e0eSDag-Erling Smørgrav
70f7167e0eSDag-Erling Smørgrav #define KEYSZ 32
71f7167e0eSDag-Erling Smørgrav #define IVSZ 8
72f7167e0eSDag-Erling Smørgrav #define BLOCKSZ 64
73f7167e0eSDag-Erling Smørgrav #define RSBUFSZ (16*BLOCKSZ)
7438a52bd3SEd Maste
7538a52bd3SEd Maste #define REKEY_BASE (1024*1024) /* NB. should be a power of 2 */
7638a52bd3SEd Maste
7738a52bd3SEd Maste /* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
7838a52bd3SEd Maste static struct _rs {
7938a52bd3SEd Maste size_t rs_have; /* valid bytes at end of rs_buf */
8038a52bd3SEd Maste size_t rs_count; /* bytes till reseed */
8138a52bd3SEd Maste } *rs;
8238a52bd3SEd Maste
8338a52bd3SEd Maste /* Maybe be preserved in fork children, if _rs_allocate() decides. */
8438a52bd3SEd Maste static struct _rsx {
8538a52bd3SEd Maste chacha_ctx rs_chacha; /* chacha context for random keystream */
8638a52bd3SEd Maste u_char rs_buf[RSBUFSZ]; /* keystream blocks */
8738a52bd3SEd Maste } *rsx;
8838a52bd3SEd Maste
8938a52bd3SEd Maste static inline int _rs_allocate(struct _rs **, struct _rsx **);
9038a52bd3SEd Maste static inline void _rs_forkdetect(void);
9138a52bd3SEd Maste #include "arc4random.h"
92f7167e0eSDag-Erling Smørgrav
93f7167e0eSDag-Erling Smørgrav static inline void _rs_rekey(u_char *dat, size_t datlen);
94f7167e0eSDag-Erling Smørgrav
95f7167e0eSDag-Erling Smørgrav static inline void
_rs_init(u_char * buf,size_t n)96f7167e0eSDag-Erling Smørgrav _rs_init(u_char *buf, size_t n)
97f7167e0eSDag-Erling Smørgrav {
98f7167e0eSDag-Erling Smørgrav if (n < KEYSZ + IVSZ)
99f7167e0eSDag-Erling Smørgrav return;
10038a52bd3SEd Maste
10138a52bd3SEd Maste if (rs == NULL) {
10238a52bd3SEd Maste if (_rs_allocate(&rs, &rsx) == -1)
10338a52bd3SEd Maste _exit(1);
104f7167e0eSDag-Erling Smørgrav }
105f7167e0eSDag-Erling Smørgrav
10638a52bd3SEd Maste chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
10738a52bd3SEd Maste chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
10819261079SEd Maste }
109bc5531deSDag-Erling Smørgrav
110f7167e0eSDag-Erling Smørgrav static void
_rs_stir(void)111f7167e0eSDag-Erling Smørgrav _rs_stir(void)
112f7167e0eSDag-Erling Smørgrav {
113f7167e0eSDag-Erling Smørgrav u_char rnd[KEYSZ + IVSZ];
11438a52bd3SEd Maste uint32_t rekey_fuzz = 0;
115f7167e0eSDag-Erling Smørgrav
11638a52bd3SEd Maste if (getentropy(rnd, sizeof rnd) == -1)
11738a52bd3SEd Maste _getentropy_fail();
118f7167e0eSDag-Erling Smørgrav
11938a52bd3SEd Maste if (!rs)
120f7167e0eSDag-Erling Smørgrav _rs_init(rnd, sizeof(rnd));
12138a52bd3SEd Maste else
122f7167e0eSDag-Erling Smørgrav _rs_rekey(rnd, sizeof(rnd));
12338a52bd3SEd Maste explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
124f7167e0eSDag-Erling Smørgrav
125f7167e0eSDag-Erling Smørgrav /* invalidate rs_buf */
12638a52bd3SEd Maste rs->rs_have = 0;
12738a52bd3SEd Maste memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
128f7167e0eSDag-Erling Smørgrav
12938a52bd3SEd Maste /* rekey interval should not be predictable */
13038a52bd3SEd Maste chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
13138a52bd3SEd Maste (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
13238a52bd3SEd Maste rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
133f7167e0eSDag-Erling Smørgrav }
134f7167e0eSDag-Erling Smørgrav
135f7167e0eSDag-Erling Smørgrav static inline void
_rs_stir_if_needed(size_t len)136f7167e0eSDag-Erling Smørgrav _rs_stir_if_needed(size_t len)
137f7167e0eSDag-Erling Smørgrav {
13838a52bd3SEd Maste _rs_forkdetect();
13938a52bd3SEd Maste if (!rs || rs->rs_count <= len)
140f7167e0eSDag-Erling Smørgrav _rs_stir();
14138a52bd3SEd Maste if (rs->rs_count <= len)
14238a52bd3SEd Maste rs->rs_count = 0;
14338a52bd3SEd Maste else
14438a52bd3SEd Maste rs->rs_count -= len;
145f7167e0eSDag-Erling Smørgrav }
146f7167e0eSDag-Erling Smørgrav
147f7167e0eSDag-Erling Smørgrav static inline void
_rs_rekey(u_char * dat,size_t datlen)148f7167e0eSDag-Erling Smørgrav _rs_rekey(u_char *dat, size_t datlen)
149f7167e0eSDag-Erling Smørgrav {
150f7167e0eSDag-Erling Smørgrav #ifndef KEYSTREAM_ONLY
15138a52bd3SEd Maste memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
152f7167e0eSDag-Erling Smørgrav #endif
153f7167e0eSDag-Erling Smørgrav /* fill rs_buf with the keystream */
15438a52bd3SEd Maste chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
15538a52bd3SEd Maste rsx->rs_buf, sizeof(rsx->rs_buf));
156f7167e0eSDag-Erling Smørgrav /* mix in optional user provided data */
157f7167e0eSDag-Erling Smørgrav if (dat) {
158f7167e0eSDag-Erling Smørgrav size_t i, m;
159f7167e0eSDag-Erling Smørgrav
16038a52bd3SEd Maste m = minimum(datlen, KEYSZ + IVSZ);
161f7167e0eSDag-Erling Smørgrav for (i = 0; i < m; i++)
16238a52bd3SEd Maste rsx->rs_buf[i] ^= dat[i];
163f7167e0eSDag-Erling Smørgrav }
164f7167e0eSDag-Erling Smørgrav /* immediately reinit for backtracking resistance */
16538a52bd3SEd Maste _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
16638a52bd3SEd Maste memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
16738a52bd3SEd Maste rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
168f7167e0eSDag-Erling Smørgrav }
169f7167e0eSDag-Erling Smørgrav
170f7167e0eSDag-Erling Smørgrav static inline void
_rs_random_buf(void * _buf,size_t n)171f7167e0eSDag-Erling Smørgrav _rs_random_buf(void *_buf, size_t n)
172f7167e0eSDag-Erling Smørgrav {
173f7167e0eSDag-Erling Smørgrav u_char *buf = (u_char *)_buf;
17438a52bd3SEd Maste u_char *keystream;
175f7167e0eSDag-Erling Smørgrav size_t m;
176f7167e0eSDag-Erling Smørgrav
177f7167e0eSDag-Erling Smørgrav _rs_stir_if_needed(n);
178f7167e0eSDag-Erling Smørgrav while (n > 0) {
17938a52bd3SEd Maste if (rs->rs_have > 0) {
18038a52bd3SEd Maste m = minimum(n, rs->rs_have);
18138a52bd3SEd Maste keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
18238a52bd3SEd Maste - rs->rs_have;
18338a52bd3SEd Maste memcpy(buf, keystream, m);
18438a52bd3SEd Maste memset(keystream, 0, m);
185f7167e0eSDag-Erling Smørgrav buf += m;
186f7167e0eSDag-Erling Smørgrav n -= m;
18738a52bd3SEd Maste rs->rs_have -= m;
188f7167e0eSDag-Erling Smørgrav }
18938a52bd3SEd Maste if (rs->rs_have == 0)
190f7167e0eSDag-Erling Smørgrav _rs_rekey(NULL, 0);
191f7167e0eSDag-Erling Smørgrav }
192f7167e0eSDag-Erling Smørgrav }
193f7167e0eSDag-Erling Smørgrav
194f7167e0eSDag-Erling Smørgrav static inline void
_rs_random_u32(uint32_t * val)19538a52bd3SEd Maste _rs_random_u32(uint32_t *val)
196f7167e0eSDag-Erling Smørgrav {
19738a52bd3SEd Maste u_char *keystream;
19838a52bd3SEd Maste
199f7167e0eSDag-Erling Smørgrav _rs_stir_if_needed(sizeof(*val));
20038a52bd3SEd Maste if (rs->rs_have < sizeof(*val))
201f7167e0eSDag-Erling Smørgrav _rs_rekey(NULL, 0);
20238a52bd3SEd Maste keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
20338a52bd3SEd Maste memcpy(val, keystream, sizeof(*val));
20438a52bd3SEd Maste memset(keystream, 0, sizeof(*val));
20538a52bd3SEd Maste rs->rs_have -= sizeof(*val);
206f7167e0eSDag-Erling Smørgrav }
207f7167e0eSDag-Erling Smørgrav
20838a52bd3SEd Maste uint32_t
arc4random(void)209f7167e0eSDag-Erling Smørgrav arc4random(void)
210f7167e0eSDag-Erling Smørgrav {
21138a52bd3SEd Maste uint32_t val;
212f7167e0eSDag-Erling Smørgrav
213f7167e0eSDag-Erling Smørgrav _ARC4_LOCK();
214f7167e0eSDag-Erling Smørgrav _rs_random_u32(&val);
215f7167e0eSDag-Erling Smørgrav _ARC4_UNLOCK();
216f7167e0eSDag-Erling Smørgrav return val;
217f7167e0eSDag-Erling Smørgrav }
21838a52bd3SEd Maste DEF_WEAK(arc4random);
219f7167e0eSDag-Erling Smørgrav
220f7167e0eSDag-Erling Smørgrav /*
221f7167e0eSDag-Erling Smørgrav * If we are providing arc4random, then we can provide a more efficient
222f7167e0eSDag-Erling Smørgrav * arc4random_buf().
223f7167e0eSDag-Erling Smørgrav */
224f7167e0eSDag-Erling Smørgrav # ifndef HAVE_ARC4RANDOM_BUF
225f7167e0eSDag-Erling Smørgrav void
arc4random_buf(void * buf,size_t n)226f7167e0eSDag-Erling Smørgrav arc4random_buf(void *buf, size_t n)
227f7167e0eSDag-Erling Smørgrav {
228f7167e0eSDag-Erling Smørgrav _ARC4_LOCK();
229f7167e0eSDag-Erling Smørgrav _rs_random_buf(buf, n);
230f7167e0eSDag-Erling Smørgrav _ARC4_UNLOCK();
231f7167e0eSDag-Erling Smørgrav }
23238a52bd3SEd Maste DEF_WEAK(arc4random_buf);
233f7167e0eSDag-Erling Smørgrav # endif /* !HAVE_ARC4RANDOM_BUF */
234f7167e0eSDag-Erling Smørgrav #endif /* !HAVE_ARC4RANDOM */
235f7167e0eSDag-Erling Smørgrav
236f7167e0eSDag-Erling Smørgrav /* arc4random_buf() that uses platform arc4random() */
237f7167e0eSDag-Erling Smørgrav #if !defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_ARC4RANDOM)
238f7167e0eSDag-Erling Smørgrav void
arc4random_buf(void * _buf,size_t n)239f7167e0eSDag-Erling Smørgrav arc4random_buf(void *_buf, size_t n)
240f7167e0eSDag-Erling Smørgrav {
241f7167e0eSDag-Erling Smørgrav size_t i;
242f7167e0eSDag-Erling Smørgrav u_int32_t r = 0;
243f7167e0eSDag-Erling Smørgrav char *buf = (char *)_buf;
244f7167e0eSDag-Erling Smørgrav
245f7167e0eSDag-Erling Smørgrav for (i = 0; i < n; i++) {
246f7167e0eSDag-Erling Smørgrav if (i % 4 == 0)
247f7167e0eSDag-Erling Smørgrav r = arc4random();
248f7167e0eSDag-Erling Smørgrav buf[i] = r & 0xff;
249f7167e0eSDag-Erling Smørgrav r >>= 8;
250f7167e0eSDag-Erling Smørgrav }
251a0ee8cc6SDag-Erling Smørgrav explicit_bzero(&r, sizeof(r));
252f7167e0eSDag-Erling Smørgrav }
253f7167e0eSDag-Erling Smørgrav #endif /* !defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_ARC4RANDOM) */
254f7167e0eSDag-Erling Smørgrav
255