xref: /dflybsd-src/lib/libc/gen/getentropy.c (revision fef748e3062060853a5d9859a1ea7507fbe0217b)
1*fef748e3SMatthew Dillon /*
2*fef748e3SMatthew Dillon  * Copyright (c) 2021 The DragonFly Project.  All rights reserved.
3*fef748e3SMatthew Dillon  *
4*fef748e3SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5*fef748e3SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6*fef748e3SMatthew Dillon  *
7*fef748e3SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8*fef748e3SMatthew Dillon  * modification, are permitted provided that the following conditions
9*fef748e3SMatthew Dillon  * are met:
10*fef748e3SMatthew Dillon  *
11*fef748e3SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12*fef748e3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13*fef748e3SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14*fef748e3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15*fef748e3SMatthew Dillon  *    the documentation and/or other materials provided with the
16*fef748e3SMatthew Dillon  *    distribution.
17*fef748e3SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18*fef748e3SMatthew Dillon  *    contributors may be used to endorse or promote products derived
19*fef748e3SMatthew Dillon  *    from this software without specific, prior written permission.
20*fef748e3SMatthew Dillon  *
21*fef748e3SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*fef748e3SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*fef748e3SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*fef748e3SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25*fef748e3SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*fef748e3SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*fef748e3SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28*fef748e3SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29*fef748e3SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30*fef748e3SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31*fef748e3SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*fef748e3SMatthew Dillon  * SUCH DAMAGE.
33*fef748e3SMatthew Dillon  */
34*fef748e3SMatthew Dillon #include <sys/types.h>
35*fef748e3SMatthew Dillon #include <sys/errno.h>
36*fef748e3SMatthew Dillon #include <sys/random.h>
37*fef748e3SMatthew Dillon #include <unistd.h>
38*fef748e3SMatthew Dillon 
39*fef748e3SMatthew Dillon int
getentropy(void * buf,size_t len)40*fef748e3SMatthew Dillon getentropy(void *buf, size_t len)
41*fef748e3SMatthew Dillon {
42*fef748e3SMatthew Dillon 	ssize_t res;
43*fef748e3SMatthew Dillon 
44*fef748e3SMatthew Dillon 	if (len > 256) {
45*fef748e3SMatthew Dillon 		errno = EIO;
46*fef748e3SMatthew Dillon 		return -1;
47*fef748e3SMatthew Dillon 	}
48*fef748e3SMatthew Dillon 	res = getrandom(buf, len, 0);
49*fef748e3SMatthew Dillon 	if (res == len)
50*fef748e3SMatthew Dillon 		return 0;
51*fef748e3SMatthew Dillon 	if (res >= 0)
52*fef748e3SMatthew Dillon 		errno = EIO;
53*fef748e3SMatthew Dillon 	return -1;
54*fef748e3SMatthew Dillon }
55