xref: /dragonfly/lib/libc/gen/arc4random.h (revision 22cd51fe)
1*22cd51feSMatthew Dillon /*	$OpenBSD: arc4random.h,v 1.4 2015/01/15 06:57:18 deraadt Exp $	*/
2*22cd51feSMatthew Dillon 
3*22cd51feSMatthew Dillon /*
4*22cd51feSMatthew Dillon  * Copyright (c) 1996, David Mazieres <dm@uun.org>
5*22cd51feSMatthew Dillon  * 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>
8*22cd51feSMatthew Dillon  *
9*22cd51feSMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
10*22cd51feSMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
11*22cd51feSMatthew Dillon  * copyright notice and this permission notice appear in all copies.
12*22cd51feSMatthew Dillon  *
13*22cd51feSMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14*22cd51feSMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15*22cd51feSMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16*22cd51feSMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17*22cd51feSMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18*22cd51feSMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19*22cd51feSMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20*22cd51feSMatthew Dillon  *
21*22cd51feSMatthew Dillon  * $FreeBSD$
22*22cd51feSMatthew Dillon  */
23*22cd51feSMatthew Dillon 
24*22cd51feSMatthew Dillon /*
25*22cd51feSMatthew Dillon  * Stub functions for portability.
26*22cd51feSMatthew Dillon  */
27*22cd51feSMatthew Dillon #include <sys/endian.h>
28*22cd51feSMatthew Dillon #include <sys/mman.h>
29*22cd51feSMatthew Dillon #if ARC4RANDOM_FXRNG != 0
30*22cd51feSMatthew Dillon #include <sys/time.h>	/* for sys/vdso.h only. */
31*22cd51feSMatthew Dillon #include <sys/vdso.h>
32*22cd51feSMatthew Dillon #include <machine/atomic.h>
33*22cd51feSMatthew Dillon #endif
34*22cd51feSMatthew Dillon 
35*22cd51feSMatthew Dillon #include <err.h>
36*22cd51feSMatthew Dillon #include <errno.h>
37*22cd51feSMatthew Dillon #include <signal.h>
38*22cd51feSMatthew Dillon #include <stdbool.h>
39*22cd51feSMatthew Dillon #include <stdint.h>
40*22cd51feSMatthew Dillon 
41*22cd51feSMatthew Dillon #if ARC4RANDOM_FXRNG != 0
42*22cd51feSMatthew Dillon /*
43*22cd51feSMatthew Dillon  * The kernel root seed version is a 64-bit counter, but we truncate it to a
44*22cd51feSMatthew Dillon  * 32-bit value in userspace for the convenience of 32-bit platforms.  32-bit
45*22cd51feSMatthew Dillon  * rollover is not possible with the current reseed interval (1 hour at limit)
46*22cd51feSMatthew Dillon  * without dynamic addition of new random devices (which also force a reseed in
47*22cd51feSMatthew Dillon  * the FXRNG design).  We don't have any dynamic device mechanism at this
48*22cd51feSMatthew Dillon  * time, and anyway something else is very wrong if billions of new devices are
49*22cd51feSMatthew Dillon  * being added.
50*22cd51feSMatthew Dillon  *
51*22cd51feSMatthew Dillon  * As is, it takes roughly 456,000 years of runtime to overflow the 32-bit
52*22cd51feSMatthew Dillon  * version.
53*22cd51feSMatthew Dillon  */
54*22cd51feSMatthew Dillon #define	fxrng_load_acq_generation(x)	atomic_load_acq_32(x)
55*22cd51feSMatthew Dillon static struct vdso_fxrng_generation_1 *vdso_fxrngp;
56*22cd51feSMatthew Dillon #endif
57*22cd51feSMatthew Dillon 
58*22cd51feSMatthew Dillon static pthread_mutex_t	arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
59*22cd51feSMatthew Dillon #define	_ARC4_LOCK()						\
60*22cd51feSMatthew Dillon 	do {							\
61*22cd51feSMatthew Dillon 		if (__isthreaded)				\
62*22cd51feSMatthew Dillon 			_pthread_mutex_lock(&arc4random_mtx);	\
63*22cd51feSMatthew Dillon 	} while (0)
64*22cd51feSMatthew Dillon 
65*22cd51feSMatthew Dillon #define	_ARC4_UNLOCK()						\
66*22cd51feSMatthew Dillon 	do {							\
67*22cd51feSMatthew Dillon 		if (__isthreaded)				\
68*22cd51feSMatthew Dillon 			_pthread_mutex_unlock(&arc4random_mtx);	\
69*22cd51feSMatthew Dillon 	} while (0)
70*22cd51feSMatthew Dillon 
71*22cd51feSMatthew Dillon static inline void
_getentropy_fail(void)72*22cd51feSMatthew Dillon _getentropy_fail(void)
73*22cd51feSMatthew Dillon {
74*22cd51feSMatthew Dillon 	raise(SIGKILL);
75*22cd51feSMatthew Dillon }
76*22cd51feSMatthew Dillon 
77*22cd51feSMatthew Dillon static inline void
_rs_initialize_fxrng(void)78*22cd51feSMatthew Dillon _rs_initialize_fxrng(void)
79*22cd51feSMatthew Dillon {
80*22cd51feSMatthew Dillon #if ARC4RANDOM_FXRNG != 0
81*22cd51feSMatthew Dillon 	struct vdso_fxrng_generation_1 *fxrngp;
82*22cd51feSMatthew Dillon 	int error;
83*22cd51feSMatthew Dillon 
84*22cd51feSMatthew Dillon 	error = _elf_aux_info(AT_FXRNG, &fxrngp, sizeof(fxrngp));
85*22cd51feSMatthew Dillon 	if (error != 0) {
86*22cd51feSMatthew Dillon 		/*
87*22cd51feSMatthew Dillon 		 * New userspace on an old or !RANDOM_FENESTRASX kernel; or an
88*22cd51feSMatthew Dillon 		 * arch that does not have a VDSO page.
89*22cd51feSMatthew Dillon 		 */
90*22cd51feSMatthew Dillon 		return;
91*22cd51feSMatthew Dillon 	}
92*22cd51feSMatthew Dillon 
93*22cd51feSMatthew Dillon 	/* Old userspace on newer kernel. */
94*22cd51feSMatthew Dillon 	if (fxrngp->fx_vdso_version != VDSO_FXRNG_VER_1)
95*22cd51feSMatthew Dillon 		return;
96*22cd51feSMatthew Dillon 
97*22cd51feSMatthew Dillon 	vdso_fxrngp = fxrngp;
98*22cd51feSMatthew Dillon #endif
99*22cd51feSMatthew Dillon }
100*22cd51feSMatthew Dillon 
101*22cd51feSMatthew Dillon static inline int
_rs_allocate(struct _rs ** rsp,struct _rsx ** rsxp)102*22cd51feSMatthew Dillon _rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
103*22cd51feSMatthew Dillon {
104*22cd51feSMatthew Dillon 	struct {
105*22cd51feSMatthew Dillon 		struct _rs rs;
106*22cd51feSMatthew Dillon 		struct _rsx rsx;
107*22cd51feSMatthew Dillon 	} *p;
108*22cd51feSMatthew Dillon 
109*22cd51feSMatthew Dillon 	if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE,
110*22cd51feSMatthew Dillon 	    MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
111*22cd51feSMatthew Dillon 		return (-1);
112*22cd51feSMatthew Dillon 	/* Allow bootstrapping arc4random.c on Linux/macOS */
113*22cd51feSMatthew Dillon #ifdef INHERIT_ZERO
114*22cd51feSMatthew Dillon 	if (minherit(p, sizeof(*p), INHERIT_ZERO) == -1) {
115*22cd51feSMatthew Dillon 		munmap(p, sizeof(*p));
116*22cd51feSMatthew Dillon 		return (-1);
117*22cd51feSMatthew Dillon 	}
118*22cd51feSMatthew Dillon #endif
119*22cd51feSMatthew Dillon 
120*22cd51feSMatthew Dillon 	_rs_initialize_fxrng();
121*22cd51feSMatthew Dillon 
122*22cd51feSMatthew Dillon 	*rsp = &p->rs;
123*22cd51feSMatthew Dillon 	*rsxp = &p->rsx;
124*22cd51feSMatthew Dillon 	return (0);
125*22cd51feSMatthew Dillon }
126*22cd51feSMatthew Dillon 
127*22cd51feSMatthew Dillon /*
128*22cd51feSMatthew Dillon  * This isn't only detecting fork.  We're also using the existing callback from
129*22cd51feSMatthew Dillon  * _rs_stir_if_needed() to force arc4random(3) to reseed if the fenestrasX root
130*22cd51feSMatthew Dillon  * seed version has changed.  (That is, the root random(4) has reseeded from
131*22cd51feSMatthew Dillon  * pooled entropy.)
132*22cd51feSMatthew Dillon  */
133*22cd51feSMatthew Dillon static inline void
_rs_forkdetect(void)134*22cd51feSMatthew Dillon _rs_forkdetect(void)
135*22cd51feSMatthew Dillon {
136*22cd51feSMatthew Dillon 	/* Detect fork (minherit(2) INHERIT_ZERO). */
137*22cd51feSMatthew Dillon 	if (__predict_false(rs == NULL || rsx == NULL))
138*22cd51feSMatthew Dillon 		return;
139*22cd51feSMatthew Dillon #if ARC4RANDOM_FXRNG != 0
140*22cd51feSMatthew Dillon 	/* If present, detect kernel FenestrasX seed version change. */
141*22cd51feSMatthew Dillon 	if (vdso_fxrngp == NULL)
142*22cd51feSMatthew Dillon 		return;
143*22cd51feSMatthew Dillon 	if (__predict_true(rsx->rs_seed_generation ==
144*22cd51feSMatthew Dillon 	    fxrng_load_acq_generation(&vdso_fxrngp->fx_generation32)))
145*22cd51feSMatthew Dillon 		return;
146*22cd51feSMatthew Dillon #endif
147*22cd51feSMatthew Dillon 	/* Invalidate rs_buf to force "stir" (reseed). */
148*22cd51feSMatthew Dillon 	memset(rs, 0, sizeof(*rs));
149*22cd51feSMatthew Dillon }
150