]> git.ipfire.org Git - thirdparty/wireguard-tools.git/commitdiff
wg: support getentropy(3)
authorJason A. Donenfeld <Jason@zx2c4.com>
Fri, 8 Jun 2018 01:18:28 +0000 (03:18 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Fri, 8 Jun 2018 01:24:46 +0000 (03:24 +0200)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
src/genkey.c

index 597152854973e2c45c1f20757b6f6a6ace6e5fb0..04de2baa140ad9862d7c865fc063e722164412e2 100644 (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;