xref: /freebsd/lib/libc/gen/getentropy.c (revision e9ac27430c0c639c1f89b1f048a44f6cea0ec265)
1*e9ac2743SConrad Meyer /*-
2*e9ac2743SConrad Meyer  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*e9ac2743SConrad Meyer  *
4*e9ac2743SConrad Meyer  * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org>
5*e9ac2743SConrad Meyer  * All rights reserved.
6*e9ac2743SConrad Meyer  *
7*e9ac2743SConrad Meyer  * Redistribution and use in source and binary forms, with or without
8*e9ac2743SConrad Meyer  * modification, are permitted provided that the following conditions
9*e9ac2743SConrad Meyer  * are met:
10*e9ac2743SConrad Meyer  * 1. Redistributions of source code must retain the above copyright
11*e9ac2743SConrad Meyer  *    notice, this list of conditions and the following disclaimer.
12*e9ac2743SConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
13*e9ac2743SConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
14*e9ac2743SConrad Meyer  *    documentation and/or other materials provided with the distribution.
15*e9ac2743SConrad Meyer  *
16*e9ac2743SConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*e9ac2743SConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*e9ac2743SConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*e9ac2743SConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*e9ac2743SConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*e9ac2743SConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*e9ac2743SConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*e9ac2743SConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*e9ac2743SConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*e9ac2743SConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*e9ac2743SConrad Meyer  * SUCH DAMAGE.
27*e9ac2743SConrad Meyer  */
28*e9ac2743SConrad Meyer 
29*e9ac2743SConrad Meyer #include <sys/cdefs.h>
30*e9ac2743SConrad Meyer __FBSDID("$FreeBSD$");
31*e9ac2743SConrad Meyer 
32*e9ac2743SConrad Meyer #include <sys/param.h>
33*e9ac2743SConrad Meyer #include <sys/random.h>
34*e9ac2743SConrad Meyer 
35*e9ac2743SConrad Meyer #include <errno.h>
36*e9ac2743SConrad Meyer #include <stdlib.h>
37*e9ac2743SConrad Meyer 
38*e9ac2743SConrad Meyer #include "libc_private.h"
39*e9ac2743SConrad Meyer 
40*e9ac2743SConrad Meyer int
41*e9ac2743SConrad Meyer getentropy(void *buf, size_t buflen)
42*e9ac2743SConrad Meyer {
43*e9ac2743SConrad Meyer 	ssize_t rd;
44*e9ac2743SConrad Meyer 
45*e9ac2743SConrad Meyer 	if (buflen > 256) {
46*e9ac2743SConrad Meyer 		errno = EIO;
47*e9ac2743SConrad Meyer 		return (-1);
48*e9ac2743SConrad Meyer 	}
49*e9ac2743SConrad Meyer 
50*e9ac2743SConrad Meyer 	while (buflen > 0) {
51*e9ac2743SConrad Meyer 		rd = getrandom(buf, buflen, 0);
52*e9ac2743SConrad Meyer 		if (rd == -1) {
53*e9ac2743SConrad Meyer 			if (errno == EINTR)
54*e9ac2743SConrad Meyer 				continue;
55*e9ac2743SConrad Meyer 			else if (errno == ENOSYS)
56*e9ac2743SConrad Meyer 				abort();
57*e9ac2743SConrad Meyer 			else
58*e9ac2743SConrad Meyer 				return (-1);
59*e9ac2743SConrad Meyer 		}
60*e9ac2743SConrad Meyer 
61*e9ac2743SConrad Meyer 		/* This cannot happen. */
62*e9ac2743SConrad Meyer 		if (rd == 0)
63*e9ac2743SConrad Meyer 			abort();
64*e9ac2743SConrad Meyer 
65*e9ac2743SConrad Meyer 		buf = (char *)buf + rd;
66*e9ac2743SConrad Meyer 		buflen -= rd;
67*e9ac2743SConrad Meyer 	}
68*e9ac2743SConrad Meyer 
69*e9ac2743SConrad Meyer 	return (0);
70*e9ac2743SConrad Meyer }
71