From be4f3cd7c2df12f219d3e6a40559602a053df4e2 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 3 Jul 2016 20:39:47 +0200 Subject: [PATCH] wg: always fallback to /dev/urandom Signed-off-by: Jason A. Donenfeld --- src/genkey.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/genkey.c b/src/genkey.c index 8e63108..a312b46 100644 --- a/src/genkey.c +++ b/src/genkey.c @@ -6,31 +6,29 @@ #include #include #include -#include #include +#include #include "curve25519.h" #include "base64.h" #include "subcommands.h" -#ifdef __NR_getrandom -static inline ssize_t get_random_bytes(uint8_t *out, size_t len) -{ - return syscall(__NR_getrandom, out, len, 0); -} -#else -#include static inline ssize_t get_random_bytes(uint8_t *out, size_t len) { ssize_t ret; - int fd = open("/dev/urandom", O_RDONLY); + int fd; +#ifdef __NR_getrandom + ret = syscall(__NR_getrandom, out, len, 0); + if (ret >= 0) + return ret; +#endif + fd = open("/dev/urandom", O_RDONLY); if (fd < 0) return fd; ret = read(fd, out, len); close(fd); return ret; } -#endif int genkey_main(int argc, char *argv[]) {