xref: /freebsd-src/crypto/openssh/openbsd-compat/arc4random.h (revision 38a52bd3b5cac3da6f7f6eef3dd050e6aa08ebb3)
1*38a52bd3SEd Maste /*	$OpenBSD: arc4random_linux.h,v 1.12 2019/07/11 10:37:28 inoguchi Exp $	*/
2*38a52bd3SEd Maste 
3*38a52bd3SEd Maste /*
4*38a52bd3SEd Maste  * Copyright (c) 1996, David Mazieres <dm@uun.org>
5*38a52bd3SEd Maste  * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6*38a52bd3SEd Maste  * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7*38a52bd3SEd Maste  * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
8*38a52bd3SEd Maste  *
9*38a52bd3SEd Maste  * Permission to use, copy, modify, and distribute this software for any
10*38a52bd3SEd Maste  * purpose with or without fee is hereby granted, provided that the above
11*38a52bd3SEd Maste  * copyright notice and this permission notice appear in all copies.
12*38a52bd3SEd Maste  *
13*38a52bd3SEd Maste  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14*38a52bd3SEd Maste  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15*38a52bd3SEd Maste  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16*38a52bd3SEd Maste  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17*38a52bd3SEd Maste  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18*38a52bd3SEd Maste  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19*38a52bd3SEd Maste  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20*38a52bd3SEd Maste  */
21*38a52bd3SEd Maste 
22*38a52bd3SEd Maste /*
23*38a52bd3SEd Maste  * Stub functions for portability.  From LibreSSL with some adaptations.
24*38a52bd3SEd Maste  */
25*38a52bd3SEd Maste 
26*38a52bd3SEd Maste #include <sys/mman.h>
27*38a52bd3SEd Maste 
28*38a52bd3SEd Maste #include <signal.h>
29*38a52bd3SEd Maste 
30*38a52bd3SEd Maste /* OpenSSH isn't multithreaded */
31*38a52bd3SEd Maste #define _ARC4_LOCK()
32*38a52bd3SEd Maste #define _ARC4_UNLOCK()
33*38a52bd3SEd Maste #define _ARC4_ATFORK(f)
34*38a52bd3SEd Maste 
35*38a52bd3SEd Maste static inline void
36*38a52bd3SEd Maste _getentropy_fail(void)
37*38a52bd3SEd Maste {
38*38a52bd3SEd Maste 	fatal("getentropy failed");
39*38a52bd3SEd Maste }
40*38a52bd3SEd Maste 
41*38a52bd3SEd Maste static volatile sig_atomic_t _rs_forked;
42*38a52bd3SEd Maste 
43*38a52bd3SEd Maste static inline void
44*38a52bd3SEd Maste _rs_forkhandler(void)
45*38a52bd3SEd Maste {
46*38a52bd3SEd Maste 	_rs_forked = 1;
47*38a52bd3SEd Maste }
48*38a52bd3SEd Maste 
49*38a52bd3SEd Maste static inline void
50*38a52bd3SEd Maste _rs_forkdetect(void)
51*38a52bd3SEd Maste {
52*38a52bd3SEd Maste 	static pid_t _rs_pid = 0;
53*38a52bd3SEd Maste 	pid_t pid = getpid();
54*38a52bd3SEd Maste 
55*38a52bd3SEd Maste 	if (_rs_pid == 0 || _rs_pid == 1 || _rs_pid != pid || _rs_forked) {
56*38a52bd3SEd Maste 		_rs_pid = pid;
57*38a52bd3SEd Maste 		_rs_forked = 0;
58*38a52bd3SEd Maste 		if (rs)
59*38a52bd3SEd Maste 			memset(rs, 0, sizeof(*rs));
60*38a52bd3SEd Maste 	}
61*38a52bd3SEd Maste }
62*38a52bd3SEd Maste 
63*38a52bd3SEd Maste static inline int
64*38a52bd3SEd Maste _rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
65*38a52bd3SEd Maste {
66*38a52bd3SEd Maste 	if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
67*38a52bd3SEd Maste 	    MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
68*38a52bd3SEd Maste 		return (-1);
69*38a52bd3SEd Maste 
70*38a52bd3SEd Maste 	if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE,
71*38a52bd3SEd Maste 	    MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
72*38a52bd3SEd Maste 		munmap(*rsp, sizeof(**rsp));
73*38a52bd3SEd Maste 		*rsp = NULL;
74*38a52bd3SEd Maste 		return (-1);
75*38a52bd3SEd Maste 	}
76*38a52bd3SEd Maste 
77*38a52bd3SEd Maste 	_ARC4_ATFORK(_rs_forkhandler);
78*38a52bd3SEd Maste 	return (0);
79*38a52bd3SEd Maste }
80