wg: support getentropy(3)

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-06-08 03:18:28 +02:00
parent d90e49599b
commit 0632c8af68
1 changed files with 11 additions and 0 deletions

View File

@ -13,6 +13,9 @@
#ifdef __linux__
#include <sys/syscall.h>
#endif
#ifdef __APPLE__
#include <sys/random.h>
#endif
#include "curve25519.h"
#include "encoding.h"
@ -22,11 +25,19 @@ static inline ssize_t get_random_bytes(uint8_t *out, size_t len)
{
ssize_t ret;
int fd;
#if defined(__OpenBSD__) || defined(__APPLE__) || (defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25)))
ret = getentropy(out, len);
if (!ret)
return len;
#endif
#if defined(__NR_getrandom) && defined(__linux__)
ret = syscall(__NR_getrandom, out, len, 0);
if (ret >= 0)
return ret;
#endif
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0)
return fd;