xref: /freebsd/lib/libc/gen/arc4random.h (revision c1e80940f3b4030df0aaed73028053af057e476d)
1*c1e80940SXin LI /*	$OpenBSD: arc4random.h,v 1.4 2015/01/15 06:57:18 deraadt Exp $	*/
2*c1e80940SXin LI 
3*c1e80940SXin LI /*
4*c1e80940SXin LI  * Copyright (c) 1996, David Mazieres <dm@uun.org>
5*c1e80940SXin LI  * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6*c1e80940SXin LI  * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7*c1e80940SXin LI  * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
8*c1e80940SXin LI  *
9*c1e80940SXin LI  * Permission to use, copy, modify, and distribute this software for any
10*c1e80940SXin LI  * purpose with or without fee is hereby granted, provided that the above
11*c1e80940SXin LI  * copyright notice and this permission notice appear in all copies.
12*c1e80940SXin LI  *
13*c1e80940SXin LI  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14*c1e80940SXin LI  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15*c1e80940SXin LI  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16*c1e80940SXin LI  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17*c1e80940SXin LI  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18*c1e80940SXin LI  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19*c1e80940SXin LI  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20*c1e80940SXin LI  *
21*c1e80940SXin LI  * $FreeBSD$
22*c1e80940SXin LI  */
23*c1e80940SXin LI 
24*c1e80940SXin LI /*
25*c1e80940SXin LI  * Stub functions for portability.
26*c1e80940SXin LI  */
27*c1e80940SXin LI #include <sys/mman.h>
28*c1e80940SXin LI 
29*c1e80940SXin LI #include <signal.h>
30*c1e80940SXin LI 
31*c1e80940SXin LI static pthread_mutex_t	arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
32*c1e80940SXin LI #define	_ARC4_LOCK()						\
33*c1e80940SXin LI 	do {							\
34*c1e80940SXin LI 		if (__isthreaded)				\
35*c1e80940SXin LI 			_pthread_mutex_lock(&arc4random_mtx);	\
36*c1e80940SXin LI 	} while (0)
37*c1e80940SXin LI 
38*c1e80940SXin LI #define	_ARC4_UNLOCK()						\
39*c1e80940SXin LI 	do {							\
40*c1e80940SXin LI 		if (__isthreaded)				\
41*c1e80940SXin LI 			_pthread_mutex_unlock(&arc4random_mtx);	\
42*c1e80940SXin LI 	} while (0)
43*c1e80940SXin LI 
44*c1e80940SXin LI static inline void
45*c1e80940SXin LI _getentropy_fail(void)
46*c1e80940SXin LI {
47*c1e80940SXin LI 	raise(SIGKILL);
48*c1e80940SXin LI }
49*c1e80940SXin LI 
50*c1e80940SXin LI static inline int
51*c1e80940SXin LI _rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
52*c1e80940SXin LI {
53*c1e80940SXin LI 	struct {
54*c1e80940SXin LI 		struct _rs rs;
55*c1e80940SXin LI 		struct _rsx rsx;
56*c1e80940SXin LI 	} *p;
57*c1e80940SXin LI 
58*c1e80940SXin LI 	if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE,
59*c1e80940SXin LI 	    MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
60*c1e80940SXin LI 		return (-1);
61*c1e80940SXin LI 	if (minherit(p, sizeof(*p), INHERIT_ZERO) == -1) {
62*c1e80940SXin LI 		munmap(p, sizeof(*p));
63*c1e80940SXin LI 		return (-1);
64*c1e80940SXin LI 	}
65*c1e80940SXin LI 
66*c1e80940SXin LI 	*rsp = &p->rs;
67*c1e80940SXin LI 	*rsxp = &p->rsx;
68*c1e80940SXin LI 	return (0);
69*c1e80940SXin LI }
70*c1e80940SXin LI 
71*c1e80940SXin LI static inline void
72*c1e80940SXin LI _rs_forkdetect(void)
73*c1e80940SXin LI {
74*c1e80940SXin LI }
75