aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-06-08 03:18:28 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-06-08 03:24:46 +0200
commit99d0eda9cab0ee07166dc856ad31cac7ef30eb7d (patch)
tree4d8bf5874e956f1711d4053604f3fd4c182d8b94
parenttools: encoding: add missing static array constraints (diff)
downloadwireguard-monolithic-historical-99d0eda9cab0ee07166dc856ad31cac7ef30eb7d.tar.xz
wireguard-monolithic-historical-99d0eda9cab0ee07166dc856ad31cac7ef30eb7d.zip
tools: support getentropy(3)
-rw-r--r--src/tools/genkey.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/tools/genkey.c b/src/tools/genkey.c
index 5971528..04de2ba 100644
--- a/src/tools/genkey.c
+++ b/src/tools/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;