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

View file

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