xref: /freebsd/contrib/unbound/compat/arc4random.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1 /*      $OpenBSD: arc4random.c,v 1.41 2014/07/12 13:24:54 deraadt Exp $ */
2 
3 /*
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  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 #include "config.h"
21 
22 /*
23  * ChaCha based random number generator for OpenBSD.
24  */
25 
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <signal.h>
29 #ifdef HAVE_STDINT_H
30 #include <stdint.h>
31 #endif
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/time.h>
38 #ifndef UB_ON_WINDOWS
39 #include <sys/mman.h>
40 #endif
41 
42 #define KEYSTREAM_ONLY
43 #include "chacha_private.h"
44 
45 #define arc4_min(a, b) ((a) < (b) ? (a) : (b))
46 #ifdef __GNUC__
47 #define inline __inline
48 #else				/* !__GNUC__ */
49 #define inline
50 #endif				/* !__GNUC__ */
51 #ifndef MAP_ANON
52 #define MAP_ANON MAP_ANONYMOUS
53 #endif
54 
55 #define KEYSZ	32
56 #define IVSZ	8
57 #define BLOCKSZ	64
58 #define RSBUFSZ	(16*BLOCKSZ)
59 
60 #define REKEY_BASE	(1024*1024) /* NB. should be a power of 2 */
61 
62 /* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
63 static struct {
64 	size_t		rs_have;	/* valid bytes at end of rs_buf */
65 	size_t		rs_count;	/* bytes till reseed */
66 } *rs;
67 
68 /* Preserved in fork children. */
69 static struct {
70 	chacha_ctx	rs_chacha;	/* chacha context for random keystream */
71 	u_char		rs_buf[RSBUFSZ];	/* keystream blocks */
72 } *rsx;
73 
74 static inline void _rs_rekey(u_char *dat, size_t datlen);
75 
76 /*
77  * Basic sanity checking; wish we could do better.
78  */
79 static int
80 fallback_gotdata(char *buf, size_t len)
81 {
82 	char	any_set = 0;
83 	size_t	i;
84 
85 	for (i = 0; i < len; ++i)
86 		any_set |= buf[i];
87 	if (any_set == 0)
88 		return -1;
89 	return 0;
90 }
91 
92 /* fallback for getentropy in case libc returns failure */
93 static int
94 fallback_getentropy_urandom(void *buf, size_t len)
95 {
96 	size_t i;
97 	int fd, flags;
98 	int save_errno = errno;
99 
100 start:
101 
102 	flags = O_RDONLY;
103 #ifdef O_NOFOLLOW
104 	flags |= O_NOFOLLOW;
105 #endif
106 #ifdef O_CLOEXEC
107 	flags |= O_CLOEXEC;
108 #endif
109 	fd = open("/dev/urandom", flags, 0);
110 	if (fd == -1) {
111 		if (errno == EINTR)
112 			goto start;
113 		goto nodevrandom;
114 	}
115 #ifndef O_CLOEXEC
116 #  ifdef HAVE_FCNTL
117 	fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
118 #  endif
119 #endif
120 	for (i = 0; i < len; ) {
121 		size_t wanted = len - i;
122 		ssize_t ret = read(fd, (char*)buf + i, wanted);
123 
124 		if (ret == -1) {
125 			if (errno == EAGAIN || errno == EINTR)
126 				continue;
127 			close(fd);
128 			goto nodevrandom;
129 		}
130 		i += ret;
131 	}
132 	close(fd);
133 	if (fallback_gotdata(buf, len) == 0) {
134 		errno = save_errno;
135 		return 0;		/* satisfied */
136 	}
137 nodevrandom:
138 	errno = EIO;
139 	return -1;
140 }
141 
142 static inline void
143 _rs_init(u_char *buf, size_t n)
144 {
145 	assert(buf);
146 	if (n < KEYSZ + IVSZ)
147 		return;
148 
149 	if (rs == NULL) {
150 #ifndef UB_ON_WINDOWS
151 		if ((rs = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE,
152 		    MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
153 			abort();
154 #ifdef MAP_INHERIT_ZERO
155 		if (minherit(rs, sizeof(*rs), MAP_INHERIT_ZERO) == -1)
156 			abort();
157 #endif
158 #else /* WINDOWS */
159 		rs = malloc(sizeof(*rs));
160 		if(!rs)
161 			abort();
162 #endif
163 	}
164 	if (rsx == NULL) {
165 #ifndef UB_ON_WINDOWS
166 		if ((rsx = mmap(NULL, sizeof(*rsx), PROT_READ|PROT_WRITE,
167 		    MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
168 			abort();
169 #else /* WINDOWS */
170 		rsx = malloc(sizeof(*rsx));
171 		if(!rsx)
172 			abort();
173 #endif
174 	}
175 
176 	chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8, 0);
177 	chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
178 }
179 
180 static void
181 _rs_stir(void)
182 {
183 	u_char rnd[KEYSZ + IVSZ];
184 	uint32_t rekey_fuzz = 0;
185 
186 	if (getentropy(rnd, sizeof rnd) == -1) {
187 		if(errno != ENOSYS ||
188 			fallback_getentropy_urandom(rnd, sizeof rnd) == -1) {
189 #ifdef SIGKILL
190 			raise(SIGKILL);
191 #else
192 			exit(9); /* windows */
193 #endif
194 		}
195 	}
196 
197 	if (!rs)
198 		_rs_init(rnd, sizeof(rnd));
199 	else
200 		_rs_rekey(rnd, sizeof(rnd));
201 	explicit_bzero(rnd, sizeof(rnd));	/* discard source seed */
202 
203 	/* invalidate rs_buf */
204 	rs->rs_have = 0;
205 	memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
206 
207 	/* rekey interval should not be predictable */
208 	chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
209 	    (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
210 	rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
211 }
212 
213 static inline void
214 _rs_stir_if_needed(size_t len)
215 {
216 #ifndef MAP_INHERIT_ZERO
217 	static pid_t _rs_pid = 0;
218 	pid_t pid = getpid();
219 
220 	/* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */
221 	if (_rs_pid == 0 || _rs_pid != pid) {
222 		_rs_pid = pid;
223 		if (rs)
224 			rs->rs_count = 0;
225 	}
226 #endif
227 	if (!rs || rs->rs_count <= len)
228 		_rs_stir();
229 	if (rs->rs_count <= len)
230 		rs->rs_count = 0;
231 	else
232 		rs->rs_count -= len;
233 }
234 
235 static inline void
236 _rs_rekey(u_char *dat, size_t datlen)
237 {
238 #ifndef KEYSTREAM_ONLY
239 	memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
240 #endif
241 	/* fill rs_buf with the keystream */
242 	chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
243 	    rsx->rs_buf, sizeof(rsx->rs_buf));
244 	/* mix in optional user provided data */
245 	if (dat) {
246 		size_t i, m;
247 
248 		m = arc4_min(datlen, KEYSZ + IVSZ);
249 		for (i = 0; i < m; i++)
250 			rsx->rs_buf[i] ^= dat[i];
251 	}
252 	/* immediately reinit for backtracking resistance */
253 	_rs_init(rsx->rs_buf, KEYSZ + IVSZ);
254 	memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
255 	rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
256 }
257 
258 static inline void
259 _rs_random_buf(void *_buf, size_t n)
260 {
261 	u_char *buf = (u_char *)_buf;
262 	u_char *keystream;
263 	size_t m;
264 
265 	_rs_stir_if_needed(n);
266 	while (n > 0) {
267 		if (rs->rs_have > 0) {
268 			m = arc4_min(n, rs->rs_have);
269 			keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
270 			    - rs->rs_have;
271 			memcpy(buf, keystream, m);
272 			memset(keystream, 0, m);
273 			buf += m;
274 			n -= m;
275 			rs->rs_have -= m;
276 		}
277 		if (rs->rs_have == 0)
278 			_rs_rekey(NULL, 0);
279 	}
280 }
281 
282 static inline void
283 _rs_random_u32(uint32_t *val)
284 {
285 	u_char *keystream;
286 	_rs_stir_if_needed(sizeof(*val));
287 	if (rs->rs_have < sizeof(*val))
288 		_rs_rekey(NULL, 0);
289 	keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
290 	memcpy(val, keystream, sizeof(*val));
291 	memset(keystream, 0, sizeof(*val));
292 	rs->rs_have -= sizeof(*val);
293 }
294 
295 uint32_t
296 arc4random(void)
297 {
298 	uint32_t val;
299 
300 	_ARC4_LOCK();
301 	_rs_random_u32(&val);
302 	_ARC4_UNLOCK();
303 	return val;
304 }
305 
306 void
307 arc4random_buf(void *buf, size_t n)
308 {
309 	_ARC4_LOCK();
310 	_rs_random_buf(buf, n);
311 	_ARC4_UNLOCK();
312 }
313