xref: /dflybsd-src/lib/libc/gen/arc4random.c (revision 22cd51fe15e7d8e951102064ba0d7e07839d6394)
1*22cd51feSMatthew Dillon /*	$OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb Exp $	*/
2*22cd51feSMatthew Dillon 
3984263bcSMatthew Dillon /*
4afca6579SPeter Avalos  * Copyright (c) 1996, David Mazieres <dm@uun.org>
5afca6579SPeter Avalos  * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6*22cd51feSMatthew Dillon  * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7*22cd51feSMatthew Dillon  * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
8984263bcSMatthew Dillon  *
9afca6579SPeter Avalos  * Permission to use, copy, modify, and distribute this software for any
10afca6579SPeter Avalos  * purpose with or without fee is hereby granted, provided that the above
11afca6579SPeter Avalos  * copyright notice and this permission notice appear in all copies.
12afca6579SPeter Avalos  *
13afca6579SPeter Avalos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14afca6579SPeter Avalos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15afca6579SPeter Avalos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16afca6579SPeter Avalos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17afca6579SPeter Avalos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18afca6579SPeter Avalos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19afca6579SPeter Avalos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20984263bcSMatthew Dillon  */
21984263bcSMatthew Dillon 
22984263bcSMatthew Dillon /*
23*22cd51feSMatthew Dillon  * ChaCha based random number generator for OpenBSD.
24984263bcSMatthew Dillon  */
25984263bcSMatthew Dillon 
26*22cd51feSMatthew Dillon #include <sys/cdefs.h>
2717ea2221SMatthew Dillon #include "namespace.h"
28*22cd51feSMatthew Dillon #if defined(__FreeBSD__)
29*22cd51feSMatthew Dillon #include <assert.h>
30*22cd51feSMatthew Dillon #endif
31*22cd51feSMatthew Dillon #include <fcntl.h>
32*22cd51feSMatthew Dillon #include <limits.h>
33*22cd51feSMatthew Dillon #include <pthread.h>
34*22cd51feSMatthew Dillon #include <signal.h>
35*22cd51feSMatthew Dillon #include <stdint.h>
36*22cd51feSMatthew Dillon #include <stdlib.h>
37*22cd51feSMatthew Dillon #include <string.h>
38*22cd51feSMatthew Dillon #include <unistd.h>
39afca6579SPeter Avalos #include <sys/types.h>
40afca6579SPeter Avalos #include <sys/time.h>
41afca6579SPeter Avalos 
42afca6579SPeter Avalos #include "libc_private.h"
4317ea2221SMatthew Dillon #include "un-namespace.h"
44984263bcSMatthew Dillon 
45*22cd51feSMatthew Dillon #define CHACHA_EMBED
46*22cd51feSMatthew Dillon #define KEYSTREAM_ONLY
47*22cd51feSMatthew Dillon #if defined(__FreeBSD__)
48*22cd51feSMatthew Dillon #define ARC4RANDOM_FXRNG 1
49*22cd51feSMatthew Dillon #else
50*22cd51feSMatthew Dillon #define ARC4RANDOM_FXRNG 0
51*22cd51feSMatthew Dillon #endif
52*22cd51feSMatthew Dillon #include "chacha.c"
53afca6579SPeter Avalos 
54*22cd51feSMatthew Dillon #define minimum(a, b) ((a) < (b) ? (a) : (b))
55afca6579SPeter Avalos 
56*22cd51feSMatthew Dillon #if defined(__GNUC__) || defined(_MSC_VER)
57*22cd51feSMatthew Dillon #define inline __inline
58*22cd51feSMatthew Dillon #else				/* __GNUC__ || _MSC_VER */
59*22cd51feSMatthew Dillon #define inline
60*22cd51feSMatthew Dillon #endif				/* !__GNUC__ && !_MSC_VER */
61ebbb4b97SMatthew Dillon 
62*22cd51feSMatthew Dillon #define KEYSZ	32
63*22cd51feSMatthew Dillon #define IVSZ	8
64*22cd51feSMatthew Dillon #define BLOCKSZ	64
65*22cd51feSMatthew Dillon #define RSBUFSZ	(16*BLOCKSZ)
66ebbb4b97SMatthew Dillon 
67*22cd51feSMatthew Dillon #define REKEY_BASE	(1024*1024) /* NB. should be a power of 2 */
68984263bcSMatthew Dillon 
69*22cd51feSMatthew Dillon /* Marked INHERIT_ZERO, so zero'd out in fork children. */
70*22cd51feSMatthew Dillon static struct _rs {
71*22cd51feSMatthew Dillon 	size_t		rs_have;	/* valid bytes at end of rs_buf */
72*22cd51feSMatthew Dillon 	size_t		rs_count;	/* bytes till reseed */
73*22cd51feSMatthew Dillon } *rs;
74*22cd51feSMatthew Dillon 
75*22cd51feSMatthew Dillon /* Maybe be preserved in fork children, if _rs_allocate() decides. */
76*22cd51feSMatthew Dillon static struct _rsx {
77*22cd51feSMatthew Dillon 	chacha_ctx	rs_chacha;	/* chacha context for random keystream */
78*22cd51feSMatthew Dillon 	u_char		rs_buf[RSBUFSZ];	/* keystream blocks */
79*22cd51feSMatthew Dillon #ifdef __FreeBSD__
80*22cd51feSMatthew Dillon 	uint32_t	rs_seed_generation;	/* 32-bit userspace RNG version */
81*22cd51feSMatthew Dillon #endif
82*22cd51feSMatthew Dillon } *rsx;
83*22cd51feSMatthew Dillon 
84*22cd51feSMatthew Dillon static inline int _rs_allocate(struct _rs **, struct _rsx **);
85*22cd51feSMatthew Dillon static inline void _rs_forkdetect(void);
86*22cd51feSMatthew Dillon #include "arc4random.h"
87*22cd51feSMatthew Dillon 
88*22cd51feSMatthew Dillon static inline void _rs_rekey(u_char *dat, size_t datlen);
8947afb358SJoerg Sonnenberger 
90afca6579SPeter Avalos static inline void
_rs_init(u_char * buf,size_t n)91*22cd51feSMatthew Dillon _rs_init(u_char *buf, size_t n)
92984263bcSMatthew Dillon {
93*22cd51feSMatthew Dillon 	if (n < KEYSZ + IVSZ)
94*22cd51feSMatthew Dillon 		return;
95984263bcSMatthew Dillon 
96*22cd51feSMatthew Dillon 	if (rs == NULL) {
97*22cd51feSMatthew Dillon 		if (_rs_allocate(&rs, &rsx) == -1)
98*22cd51feSMatthew Dillon 			_exit(1);
99984263bcSMatthew Dillon 	}
100984263bcSMatthew Dillon 
101*22cd51feSMatthew Dillon 	chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
102*22cd51feSMatthew Dillon 	chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ, NULL);
103afca6579SPeter Avalos }
104afca6579SPeter Avalos 
105afca6579SPeter Avalos static void
_rs_stir(void)106*22cd51feSMatthew Dillon _rs_stir(void)
107afca6579SPeter Avalos {
108*22cd51feSMatthew Dillon 	u_char rnd[KEYSZ + IVSZ];
109*22cd51feSMatthew Dillon 	uint32_t rekey_fuzz = 0;
110*22cd51feSMatthew Dillon 
111*22cd51feSMatthew Dillon #if defined(__FreeBSD__)
112*22cd51feSMatthew Dillon 	bool need_init;
113*22cd51feSMatthew Dillon 
114*22cd51feSMatthew Dillon 	/*
115*22cd51feSMatthew Dillon 	 * De-couple allocation (which locates the vdso_fxrngp pointer in
116*22cd51feSMatthew Dillon 	 * auxinfo) from initialization.  This allows us to read the root seed
117*22cd51feSMatthew Dillon 	 * version before we fetch system entropy, maintaining the invariant
118*22cd51feSMatthew Dillon 	 * that the PRF was seeded with entropy from rs_seed_generation or a
119*22cd51feSMatthew Dillon 	 * later generation.  But never seeded from an earlier generation.
120*22cd51feSMatthew Dillon 	 * This invariant prevents us from missing a root reseed event.
121*22cd51feSMatthew Dillon 	 */
122*22cd51feSMatthew Dillon 	need_init = false;
123*22cd51feSMatthew Dillon 	if (rs == NULL) {
124*22cd51feSMatthew Dillon 		if (_rs_allocate(&rs, &rsx) == -1)
125*22cd51feSMatthew Dillon 			abort();
126*22cd51feSMatthew Dillon 		need_init = true;
127afca6579SPeter Avalos 	}
128*22cd51feSMatthew Dillon 	/*
129*22cd51feSMatthew Dillon 	 * Transition period: new userspace on old kernel.  This should become
130*22cd51feSMatthew Dillon 	 * a hard error at some point, if the scheme is adopted.
131*22cd51feSMatthew Dillon 	 */
132*22cd51feSMatthew Dillon 	if (vdso_fxrngp != NULL)
133*22cd51feSMatthew Dillon 		rsx->rs_seed_generation =
134*22cd51feSMatthew Dillon 		    fxrng_load_acq_generation(&vdso_fxrngp->fx_generation32);
135*22cd51feSMatthew Dillon #endif
136*22cd51feSMatthew Dillon 
137*22cd51feSMatthew Dillon 	if (getentropy(rnd, sizeof rnd) == -1)
138*22cd51feSMatthew Dillon 		_getentropy_fail();
139*22cd51feSMatthew Dillon 
140*22cd51feSMatthew Dillon #if !defined(__FreeBSD__)
141*22cd51feSMatthew Dillon 	if (!rs)
142*22cd51feSMatthew Dillon 		_rs_init(rnd, sizeof(rnd));
143*22cd51feSMatthew Dillon #else /* __FreeBSD__ */
144*22cd51feSMatthew Dillon 	assert(rs != NULL);
145*22cd51feSMatthew Dillon 	if (need_init)
146*22cd51feSMatthew Dillon 		_rs_init(rnd, sizeof(rnd));
147*22cd51feSMatthew Dillon #endif
148*22cd51feSMatthew Dillon 	else
149*22cd51feSMatthew Dillon 		_rs_rekey(rnd, sizeof(rnd));
150*22cd51feSMatthew Dillon 	explicit_bzero(rnd, sizeof(rnd));	/* discard source seed */
151*22cd51feSMatthew Dillon 
152*22cd51feSMatthew Dillon 	/* invalidate rs_buf */
153*22cd51feSMatthew Dillon 	rs->rs_have = 0;
154*22cd51feSMatthew Dillon 	memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
155*22cd51feSMatthew Dillon 
156*22cd51feSMatthew Dillon 	/* rekey interval should not be predictable */
157*22cd51feSMatthew Dillon 	chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
158*22cd51feSMatthew Dillon 	    (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
159*22cd51feSMatthew Dillon 	rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
160afca6579SPeter Avalos }
161afca6579SPeter Avalos 
162afca6579SPeter Avalos static inline void
_rs_stir_if_needed(size_t len)163*22cd51feSMatthew Dillon _rs_stir_if_needed(size_t len)
164afca6579SPeter Avalos {
165*22cd51feSMatthew Dillon 	_rs_forkdetect();
166*22cd51feSMatthew Dillon 	if (!rs || rs->rs_count <= len)
167*22cd51feSMatthew Dillon 		_rs_stir();
168*22cd51feSMatthew Dillon 	if (rs->rs_count <= len)
169*22cd51feSMatthew Dillon 		rs->rs_count = 0;
170*22cd51feSMatthew Dillon 	else
171*22cd51feSMatthew Dillon 		rs->rs_count -= len;
172984263bcSMatthew Dillon }
173984263bcSMatthew Dillon 
174*22cd51feSMatthew Dillon static inline void
_rs_rekey(u_char * dat,size_t datlen)175*22cd51feSMatthew Dillon _rs_rekey(u_char *dat, size_t datlen)
176984263bcSMatthew Dillon {
177*22cd51feSMatthew Dillon #ifndef KEYSTREAM_ONLY
178*22cd51feSMatthew Dillon 	memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
179*22cd51feSMatthew Dillon #endif
180*22cd51feSMatthew Dillon 	/* fill rs_buf with the keystream */
181*22cd51feSMatthew Dillon 	chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
182*22cd51feSMatthew Dillon 	    rsx->rs_buf, sizeof(rsx->rs_buf));
183*22cd51feSMatthew Dillon 	/* mix in optional user provided data */
184*22cd51feSMatthew Dillon 	if (dat) {
185*22cd51feSMatthew Dillon 		size_t i, m;
186*22cd51feSMatthew Dillon 
187*22cd51feSMatthew Dillon 		m = minimum(datlen, KEYSZ + IVSZ);
188*22cd51feSMatthew Dillon 		for (i = 0; i < m; i++)
189*22cd51feSMatthew Dillon 			rsx->rs_buf[i] ^= dat[i];
190*22cd51feSMatthew Dillon 	}
191*22cd51feSMatthew Dillon 	/* immediately reinit for backtracking resistance */
192*22cd51feSMatthew Dillon 	_rs_init(rsx->rs_buf, KEYSZ + IVSZ);
193*22cd51feSMatthew Dillon 	memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
194*22cd51feSMatthew Dillon 	rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
195984263bcSMatthew Dillon }
196984263bcSMatthew Dillon 
197*22cd51feSMatthew Dillon static inline void
_rs_random_buf(void * _buf,size_t n)198*22cd51feSMatthew Dillon _rs_random_buf(void *_buf, size_t n)
199afca6579SPeter Avalos {
200afca6579SPeter Avalos 	u_char *buf = (u_char *)_buf;
201*22cd51feSMatthew Dillon 	u_char *keystream;
202*22cd51feSMatthew Dillon 	size_t m;
203*22cd51feSMatthew Dillon 
204*22cd51feSMatthew Dillon 	_rs_stir_if_needed(n);
205*22cd51feSMatthew Dillon 	while (n > 0) {
206*22cd51feSMatthew Dillon 		if (rs->rs_have > 0) {
207*22cd51feSMatthew Dillon 			m = minimum(n, rs->rs_have);
208*22cd51feSMatthew Dillon 			keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
209*22cd51feSMatthew Dillon 			    - rs->rs_have;
210*22cd51feSMatthew Dillon 			memcpy(buf, keystream, m);
211*22cd51feSMatthew Dillon 			memset(keystream, 0, m);
212*22cd51feSMatthew Dillon 			buf += m;
213*22cd51feSMatthew Dillon 			n -= m;
214*22cd51feSMatthew Dillon 			rs->rs_have -= m;
215*22cd51feSMatthew Dillon 		}
216*22cd51feSMatthew Dillon 		if (rs->rs_have == 0)
217*22cd51feSMatthew Dillon 			_rs_rekey(NULL, 0);
218*22cd51feSMatthew Dillon 	}
219*22cd51feSMatthew Dillon }
220*22cd51feSMatthew Dillon 
221*22cd51feSMatthew Dillon static inline void
_rs_random_u32(uint32_t * val)222*22cd51feSMatthew Dillon _rs_random_u32(uint32_t *val)
223*22cd51feSMatthew Dillon {
224*22cd51feSMatthew Dillon 	u_char *keystream;
225*22cd51feSMatthew Dillon 
226*22cd51feSMatthew Dillon 	_rs_stir_if_needed(sizeof(*val));
227*22cd51feSMatthew Dillon 	if (rs->rs_have < sizeof(*val))
228*22cd51feSMatthew Dillon 		_rs_rekey(NULL, 0);
229*22cd51feSMatthew Dillon 	keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
230*22cd51feSMatthew Dillon 	memcpy(val, keystream, sizeof(*val));
231*22cd51feSMatthew Dillon 	memset(keystream, 0, sizeof(*val));
232*22cd51feSMatthew Dillon 	rs->rs_have -= sizeof(*val);
233*22cd51feSMatthew Dillon }
234*22cd51feSMatthew Dillon 
235*22cd51feSMatthew Dillon uint32_t
arc4random(void)236*22cd51feSMatthew Dillon arc4random(void)
237*22cd51feSMatthew Dillon {
238*22cd51feSMatthew Dillon 	uint32_t val;
239afca6579SPeter Avalos 
240d0ce80dcSzrj 	_ARC4_LOCK();
241*22cd51feSMatthew Dillon 	_rs_random_u32(&val);
242*22cd51feSMatthew Dillon 	_ARC4_UNLOCK();
243*22cd51feSMatthew Dillon 	return val;
244afca6579SPeter Avalos }
245*22cd51feSMatthew Dillon 
246*22cd51feSMatthew Dillon void
arc4random_buf(void * buf,size_t n)247*22cd51feSMatthew Dillon arc4random_buf(void *buf, size_t n)
248*22cd51feSMatthew Dillon {
249*22cd51feSMatthew Dillon 	_ARC4_LOCK();
250*22cd51feSMatthew Dillon 	_rs_random_buf(buf, n);
251d0ce80dcSzrj 	_ARC4_UNLOCK();
252afca6579SPeter Avalos }
253