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