From 6de837ba4e208260ac6043d521b0a1d79ffd58a7 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Tue, 30 Aug 2016 10:08:07 +0200 Subject: Use arc4random()/getrandom() to get random bytes instead of sysctl() interface --- src/openssl.c | 63 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index d8eebb5..6addcaa 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -48,6 +48,19 @@ #if __APPLE__ #include /* mach_absolute_time() */ +#define HAVE_ARC4RANDOM +#endif + +#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) +#define HAVE_ARC4RANDOM +#endif + +#if defined(__linux__) +#include +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) +#define HAVE_GETRANDOM +#include +#endif #endif #include @@ -7811,44 +7824,16 @@ static struct randL_state *randL_getstate(lua_State *L) { return lua_touserdata(L, lua_upvalueindex(1)); } /* randL_getstate() */ -#ifndef HAVE_SYS_SYSCTL_H -#define HAVE_SYS_SYSCTL_H (BSD || __GLIBC__) -#endif - -#if HAVE_SYS_SYSCTL_H -#include /* CTL_KERN KERN_RANDOM RANDOM_UUID KERN_URND KERN_ARND sysctl(2) */ -#endif - -#ifndef HAVE_RANDOM_UUID -#define HAVE_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux) /* RANDOM_UUID is an enum, not macro */ -#endif - -#ifndef HAVE_KERN_URND -#define HAVE_KERN_URND (defined KERN_URND) -#endif - -#ifndef HAVE_KERN_ARND -#define HAVE_KERN_ARND (defined KERN_ARND) -#endif static int randL_stir(struct randL_state *st, unsigned rqstd) { unsigned count = 0; int error; unsigned char data[256]; -#if HAVE_RANDOM_UUID || HAVE_KERN_URND || HAVE_KERN_ARND -#if HAVE_RANDOM_UUID - int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; -#elif HAVE_KERN_URND - int mib[] = { CTL_KERN, KERN_URND }; -#else - int mib[] = { CTL_KERN, KERN_ARND }; -#endif - - while (count < rqstd) { +#if HAVE_ARC4RANDOM + while (count < rqst) { size_t n = MIN(rqstd - count, sizeof data); - if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) - break; + arc4random(data, n); RAND_add(data, n, n); @@ -7856,6 +7841,22 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { } #endif +#if HAVE_GETRANDOM + while (count < rqst) { + size_t n = MIN(rqstd - count, sizeof data); + + n = getrandom(data, n, 0); + + if (n == -1) { + break; + } + + RAND_add(data, n, n); + + count += n; + } +#endif + if (count < rqstd) { #if defined O_CLOEXEC && (!defined _AIX /* O_CLOEXEC overflows int */) int fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC); -- cgit v1.2.3-59-g8ed1b From 7d6202bfa85e8f0bf90590a9381757f15e296a33 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Tue, 30 Aug 2016 10:25:36 +0200 Subject: Reinstate sysctl call for older Linux kernels --- src/openssl.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 6addcaa..7c28c84 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -60,6 +60,8 @@ #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) #define HAVE_GETRANDOM #include +#else +#define HAVE_SYS_SYSCTL_H #endif #endif @@ -7824,13 +7826,20 @@ static struct randL_state *randL_getstate(lua_State *L) { return lua_touserdata(L, lua_upvalueindex(1)); } /* randL_getstate() */ +#if HAVE_SYS_SYSCTL_H +#include /* CTL_KERN KERN_RANDOM RANDOM_UUID KERN_URND KERN_ARND sysctl(2) */ +#endif + +#ifndef HAVE_RANDOM_UUID +#define HAVE_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux__) /* RANDOM_UUID is an enum, not macro */ +#endif static int randL_stir(struct randL_state *st, unsigned rqstd) { unsigned count = 0; int error; unsigned char data[256]; -#if HAVE_ARC4RANDOM - while (count < rqst) { +#if defined(HAVE_ARC4RANDOM) + while (count < rqstd) { size_t n = MIN(rqstd - count, sizeof data); arc4random(data, n); @@ -7839,22 +7848,34 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { count += n; } -#endif - -#if HAVE_GETRANDOM - while (count < rqst) { +#elif defined(HAVE_GETRANDOM) + while (count < rqstd) { size_t n = MIN(rqstd - count, sizeof data); n = getrandom(data, n, 0); if (n == -1) { - break; + break; } - RAND_add(data, n, n); + RAND_add(data, n, n); + + count += n; + } +#elif HAVE_RANDOM_UUID + int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; + + while (count < rqstd) { + size_t n = MIN(rqstd - count, sizeof data); + + if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) + break; + + RAND_add(data, n, n); count += n; } + #endif if (count < rqstd) { -- cgit v1.2.3-59-g8ed1b From 45dfa8916f911e55b60cb03c1cd0f6cea1141b63 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Tue, 30 Aug 2016 10:29:57 +0200 Subject: Use RAND_seed() instead of RAND_add() where appropriate --- src/openssl.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 7c28c84..9fc4595 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -7844,7 +7844,7 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { arc4random(data, n); - RAND_add(data, n, n); + RAND_seed(data, n); count += n; } @@ -7858,7 +7858,7 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { break; } - RAND_add(data, n, n); + RAND_seed(data, n); count += n; } @@ -7871,7 +7871,7 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) break; - RAND_add(data, n, n); + RAND_seed(data, n); count += n; } @@ -7906,7 +7906,7 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { goto error; default: - RAND_add(data, n, n); + RAND_seed(data, n); count += n; } -- cgit v1.2.3-59-g8ed1b From 6a6a85b0ecfbe1a68e0b2d86ba1f75a17f8c1598 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Tue, 30 Aug 2016 10:41:12 +0200 Subject: Add __DragonFly__ and generic BSD detection for ARC4RANDOM --- src/openssl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openssl.c b/src/openssl.c index 9fc4595..136110f 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -51,7 +51,7 @@ #define HAVE_ARC4RANDOM #endif -#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) +#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(BSD) #define HAVE_ARC4RANDOM #endif -- cgit v1.2.3-59-g8ed1b From 705053409dade9bc9f691a25f9754a129fa37406 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Tue, 30 Aug 2016 11:18:13 +0200 Subject: Use syscall() to call getrandom() --- src/openssl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/openssl.c b/src/openssl.c index 136110f..c25651b 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -59,6 +59,7 @@ #include #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) #define HAVE_GETRANDOM +#include #include #else #define HAVE_SYS_SYSCTL_H @@ -7852,7 +7853,7 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { while (count < rqstd) { size_t n = MIN(rqstd - count, sizeof data); - n = getrandom(data, n, 0); + n = syscall(SYS_getrandom, data, n, 0); if (n == -1) { break; -- cgit v1.2.3-59-g8ed1b