xref: /titanic_50/usr/src/lib/libc/port/gen/getentropy.c (revision c536b1f93b31eba539c635a91be8ee2ab0fcb15a)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2015, Joyent, Inc.
14  */
15 
16 /*
17  * getentropy(3C) is an OpenBSD compatible wrapper around getrandom(2).
18  */
19 
20 #include <errno.h>
21 #include <sys/random.h>
22 
23 int
24 getentropy(void *buf, size_t buflen)
25 {
26 	int ret;
27 
28 	if (buflen > 256) {
29 		errno = EIO;
30 		return (-1);
31 	}
32 
33 	ret = getrandom(buf, buflen, 0);
34 	if (ret == -1 || ret != buflen) {
35 		if (errno != EFAULT)
36 			errno = EIO;
37 		return (-1);
38 	}
39 
40 	return (0);
41 }
42