diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-06-08 03:18:28 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-06-08 03:24:46 +0200 |
commit | 0632c8af68fa566dfd461c758968d2daa9b04f98 (patch) | |
tree | 222e3afb24a9460fc61ee936a7ede97d5e7e65b8 /src | |
parent | wg: encoding: add missing static array constraints (diff) | |
download | wireguard-tools-0632c8af68fa566dfd461c758968d2daa9b04f98.tar.xz wireguard-tools-0632c8af68fa566dfd461c758968d2daa9b04f98.zip |
wg: support getentropy(3)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/genkey.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/genkey.c b/src/genkey.c index 5971528..04de2ba 100644 --- a/src/genkey.c +++ b/src/genkey.c @@ -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; |