xref: /freebsd/contrib/unbound/compat/getentropy_freebsd.c (revision 0eefd3079a04edf4cf315403beb0344724567f42)
1*0eefd307SCy Schubert /*	$OpenBSD: getentropy_freebsd.c,v 1.3 2016/08/07 03:27:21 tb Exp $	*/
2*0eefd307SCy Schubert 
3*0eefd307SCy Schubert /*
4*0eefd307SCy Schubert  * Copyright (c) 2014 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5*0eefd307SCy Schubert  * Copyright (c) 2014 Brent Cook <bcook@openbsd.org>
6*0eefd307SCy Schubert  *
7*0eefd307SCy Schubert  * Permission to use, copy, modify, and distribute this software for any
8*0eefd307SCy Schubert  * purpose with or without fee is hereby granted, provided that the above
9*0eefd307SCy Schubert  * copyright notice and this permission notice appear in all copies.
10*0eefd307SCy Schubert  *
11*0eefd307SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12*0eefd307SCy Schubert  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13*0eefd307SCy Schubert  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14*0eefd307SCy Schubert  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15*0eefd307SCy Schubert  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16*0eefd307SCy Schubert  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17*0eefd307SCy Schubert  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*0eefd307SCy Schubert  *
19*0eefd307SCy Schubert  * Emulation of getentropy(2) as documented at:
20*0eefd307SCy Schubert  * http://man.openbsd.org/getentropy.2
21*0eefd307SCy Schubert  */
22*0eefd307SCy Schubert 
23*0eefd307SCy Schubert #include <sys/types.h>
24*0eefd307SCy Schubert #include <sys/sysctl.h>
25*0eefd307SCy Schubert 
26*0eefd307SCy Schubert #include <errno.h>
27*0eefd307SCy Schubert #include <stddef.h>
28*0eefd307SCy Schubert 
29*0eefd307SCy Schubert /*
30*0eefd307SCy Schubert  * Derived from lib/libc/gen/arc4random.c from FreeBSD.
31*0eefd307SCy Schubert  */
32*0eefd307SCy Schubert static size_t
getentropy_sysctl(u_char * buf,size_t size)33*0eefd307SCy Schubert getentropy_sysctl(u_char *buf, size_t size)
34*0eefd307SCy Schubert {
35*0eefd307SCy Schubert 	int mib[2];
36*0eefd307SCy Schubert 	size_t len, done;
37*0eefd307SCy Schubert 
38*0eefd307SCy Schubert 	mib[0] = CTL_KERN;
39*0eefd307SCy Schubert 	mib[1] = KERN_ARND;
40*0eefd307SCy Schubert 	done = 0;
41*0eefd307SCy Schubert 
42*0eefd307SCy Schubert 	do {
43*0eefd307SCy Schubert 		len = size;
44*0eefd307SCy Schubert 		if (sysctl(mib, 2, buf, &len, NULL, 0) == -1)
45*0eefd307SCy Schubert 			return (done);
46*0eefd307SCy Schubert 		done += len;
47*0eefd307SCy Schubert 		buf += len;
48*0eefd307SCy Schubert 		size -= len;
49*0eefd307SCy Schubert 	} while (size > 0);
50*0eefd307SCy Schubert 
51*0eefd307SCy Schubert 	return (done);
52*0eefd307SCy Schubert }
53*0eefd307SCy Schubert 
54*0eefd307SCy Schubert int
getentropy(void * buf,size_t len)55*0eefd307SCy Schubert getentropy(void *buf, size_t len)
56*0eefd307SCy Schubert {
57*0eefd307SCy Schubert 	if (len <= 256 && getentropy_sysctl(buf, len) == len)
58*0eefd307SCy Schubert 		return (0);
59*0eefd307SCy Schubert 
60*0eefd307SCy Schubert 	errno = EIO;
61*0eefd307SCy Schubert 	return (-1);
62*0eefd307SCy Schubert }
63