1 /* $OpenBSD: arc4random.h,v 1.4 2015/01/15 06:57:18 deraadt Exp $ */
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>
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.
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.
25 * Stub functions for portability.
28 #include <sys/endian.h>
30 #if ARC4RANDOM_FXRNG != 0
31 #include <sys/time.h> /* for sys/vdso.h only. */
33 #include <machine/atomic.h>
42 #if ARC4RANDOM_FXRNG != 0
44 * The kernel root seed version is a 64-bit counter, but we truncate it to a
45 * 32-bit value in userspace for the convenience of 32-bit platforms. 32-bit
46 * rollover is not possible with the current reseed interval (1 hour at limit)
47 * without dynamic addition of new random devices (which also force a reseed in
48 * the FXRNG design). We don't have any dynamic device mechanism at this
49 * time, and anyway something else is very wrong if billions of new devices are
52 * As is, it takes roughly 456,000 years of runtime to overflow the 32-bit
55 #define fxrng_load_acq_generation(x) atomic_load_acq_32(x)
56 static struct vdso_fxrng_generation_1 *vdso_fxrngp;
59 static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
60 #define _ARC4_LOCK() \
63 _pthread_mutex_lock(&arc4random_mtx); \
66 #define _ARC4_UNLOCK() \
69 _pthread_mutex_unlock(&arc4random_mtx); \
73 _getentropy_fail(void)
79 _rs_initialize_fxrng(void)
81 #if ARC4RANDOM_FXRNG != 0
82 struct vdso_fxrng_generation_1 *fxrngp;
85 error = _elf_aux_info(AT_FXRNG, &fxrngp, sizeof(fxrngp));
88 * New userspace on an old or !RANDOM_FENESTRASX kernel; or an
89 * arch that does not have a VDSO page.
94 /* Old userspace on newer kernel. */
95 if (fxrngp->fx_vdso_version != VDSO_FXRNG_VER_1)
103 _rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
110 if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE,
111 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
113 /* Allow bootstrapping arc4random.c on Linux/macOS */
115 if (minherit(p, sizeof(*p), INHERIT_ZERO) == -1) {
116 munmap(p, sizeof(*p));
121 _rs_initialize_fxrng();
129 * This isn't only detecting fork. We're also using the existing callback from
130 * _rs_stir_if_needed() to force arc4random(3) to reseed if the fenestrasX root
131 * seed version has changed. (That is, the root random(4) has reseeded from
137 /* Detect fork (minherit(2) INHERIT_ZERO). */
138 if (__predict_false(rs == NULL || rsx == NULL))
140 #if ARC4RANDOM_FXRNG != 0
141 /* If present, detect kernel FenestrasX seed version change. */
142 if (vdso_fxrngp == NULL)
144 if (__predict_true(rsx->rs_seed_generation ==
145 fxrng_load_acq_generation(&vdso_fxrngp->fx_generation32)))
148 /* Invalidate rs_buf to force "stir" (reseed). */
149 memset(rs, 0, sizeof(*rs));