From: Kirill Isakov Date: Sun, 1 May 2022 17:09:06 +0000 (+0600) Subject: Replace getrandom() with getentropy() X-Git-Url: https://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=e6111686d731af1e621ea9f38babef50610f7717;p=tinc Replace getrandom() with getentropy() Unlike getrandom(), this function is supported by most BSDs (with the exception of NetBSD), and by various Illumos distributions. --- diff --git a/src/linux/meson.build b/src/linux/meson.build index 0213d060..8c0f1377 100644 --- a/src/linux/meson.build +++ b/src/linux/meson.build @@ -2,13 +2,9 @@ check_headers += [ 'linux/if_tun.h', 'netpacket/packet.h', 'sys/epoll.h', - 'sys/random.h', ] -check_functions += [ - 'recvmmsg', - 'getrandom', -] +check_functions += 'recvmmsg' src_tincd += files('device.c') diff --git a/src/meson.build b/src/meson.build index 351124e7..02a42833 100644 --- a/src/meson.build +++ b/src/meson.build @@ -62,6 +62,7 @@ check_headers = [ 'sys/ioctl.h', 'sys/mman.h', 'sys/param.h', + 'sys/random.h', 'sys/resource.h', 'sys/socket.h', 'sys/stat.h', @@ -85,6 +86,7 @@ check_functions = [ 'explicit_bzero', 'explicit_memset', 'fchmod', + 'getentropy', 'gettimeofday', 'memset_s', 'mlockall', diff --git a/src/random.c b/src/random.c index fad7173f..f2e898f7 100644 --- a/src/random.c +++ b/src/random.c @@ -2,12 +2,12 @@ #include "random.h" -#ifndef HAVE_GETRANDOM +#ifndef HAVE_GETENTROPY static int random_fd = -1; #endif void random_init(void) { -#ifndef HAVE_GETRANDOM +#ifndef HAVE_GETENTROPY random_fd = open("/dev/urandom", O_RDONLY); if(random_fd < 0) { @@ -23,7 +23,7 @@ void random_init(void) { } void random_exit(void) { -#ifndef HAVE_GETRANDOM +#ifndef HAVE_GETENTROPY close(random_fd); #endif } @@ -32,8 +32,9 @@ void randomize(void *vout, size_t outlen) { uint8_t *out = vout; while(outlen) { -#ifdef HAVE_GETRANDOM - ssize_t len = getrandom(out, outlen, 0); +#ifdef HAVE_GETENTROPY + int reqlen = (int) MIN(256, outlen); + int len = !getentropy(out, reqlen) ? reqlen : -1; #else ssize_t len = read(random_fd, out, outlen); #endif diff --git a/test/unit/test_random_noinit.c b/test/unit/test_random_noinit.c index aeedda95..fa6087f1 100644 --- a/test/unit/test_random_noinit.c +++ b/test/unit/test_random_noinit.c @@ -2,7 +2,7 @@ #include "unittest.h" -#ifdef HAVE_GETRANDOM +#ifdef HAVE_GETENTROPY int main(void) { return 1; } @@ -14,4 +14,4 @@ int main(void) { randomize(buf, sizeof(buf)); return 0; } -#endif // HAVE_GETRANDOM +#endif // HAVE_GETENTROPY