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