summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/openssl.c67
1 files changed, 45 insertions, 22 deletions
diff --git a/src/openssl.c b/src/openssl.c
index d8eebb5..c25651b 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -48,6 +48,22 @@
48 48
49#if __APPLE__ 49#if __APPLE__
50#include <mach/mach_time.h> /* mach_absolute_time() */ 50#include <mach/mach_time.h> /* mach_absolute_time() */
51#define HAVE_ARC4RANDOM
52#endif
53
54#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(BSD)
55#define HAVE_ARC4RANDOM
56#endif
57
58#if defined(__linux__)
59#include <linux/version.h>
60#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)
61#define HAVE_GETRANDOM
62#include <sys/syscall.h>
63#include <linux/random.h>
64#else
65#define HAVE_SYS_SYSCTL_H
66#endif
51#endif 67#endif
52 68
53#include <openssl/opensslconf.h> 69#include <openssl/opensslconf.h>
@@ -7811,38 +7827,44 @@ static struct randL_state *randL_getstate(lua_State *L) {
7811 return lua_touserdata(L, lua_upvalueindex(1)); 7827 return lua_touserdata(L, lua_upvalueindex(1));
7812} /* randL_getstate() */ 7828} /* randL_getstate() */
7813 7829
7814#ifndef HAVE_SYS_SYSCTL_H
7815#define HAVE_SYS_SYSCTL_H (BSD || __GLIBC__)
7816#endif
7817
7818#if HAVE_SYS_SYSCTL_H 7830#if HAVE_SYS_SYSCTL_H
7819#include <sys/sysctl.h> /* CTL_KERN KERN_RANDOM RANDOM_UUID KERN_URND KERN_ARND sysctl(2) */ 7831#include <sys/sysctl.h> /* CTL_KERN KERN_RANDOM RANDOM_UUID KERN_URND KERN_ARND sysctl(2) */
7820#endif 7832#endif
7821 7833
7822#ifndef HAVE_RANDOM_UUID 7834#ifndef HAVE_RANDOM_UUID
7823#define HAVE_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux) /* RANDOM_UUID is an enum, not macro */ 7835#define HAVE_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux__) /* RANDOM_UUID is an enum, not macro */
7824#endif
7825
7826#ifndef HAVE_KERN_URND
7827#define HAVE_KERN_URND (defined KERN_URND)
7828#endif
7829
7830#ifndef HAVE_KERN_ARND
7831#define HAVE_KERN_ARND (defined KERN_ARND)
7832#endif 7836#endif
7833 7837
7834static int randL_stir(struct randL_state *st, unsigned rqstd) { 7838static int randL_stir(struct randL_state *st, unsigned rqstd) {
7835 unsigned count = 0; 7839 unsigned count = 0;
7836 int error; 7840 int error;
7837 unsigned char data[256]; 7841 unsigned char data[256];
7838#if HAVE_RANDOM_UUID || HAVE_KERN_URND || HAVE_KERN_ARND 7842#if defined(HAVE_ARC4RANDOM)
7839#if HAVE_RANDOM_UUID 7843 while (count < rqstd) {
7844 size_t n = MIN(rqstd - count, sizeof data);
7845
7846 arc4random(data, n);
7847
7848 RAND_seed(data, n);
7849
7850 count += n;
7851 }
7852#elif defined(HAVE_GETRANDOM)
7853 while (count < rqstd) {
7854 size_t n = MIN(rqstd - count, sizeof data);
7855
7856 n = syscall(SYS_getrandom, data, n, 0);
7857
7858 if (n == -1) {
7859 break;
7860 }
7861
7862 RAND_seed(data, n);
7863
7864 count += n;
7865 }
7866#elif HAVE_RANDOM_UUID
7840 int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; 7867 int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID };
7841#elif HAVE_KERN_URND
7842 int mib[] = { CTL_KERN, KERN_URND };
7843#else
7844 int mib[] = { CTL_KERN, KERN_ARND };
7845#endif
7846 7868
7847 while (count < rqstd) { 7869 while (count < rqstd) {
7848 size_t n = MIN(rqstd - count, sizeof data); 7870 size_t n = MIN(rqstd - count, sizeof data);
@@ -7850,10 +7872,11 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) {
7850 if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) 7872 if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0))
7851 break; 7873 break;
7852 7874
7853 RAND_add(data, n, n); 7875 RAND_seed(data, n);
7854 7876
7855 count += n; 7877 count += n;
7856 } 7878 }
7879
7857#endif 7880#endif
7858 7881
7859 if (count < rqstd) { 7882 if (count < rqstd) {
@@ -7884,7 +7907,7 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) {
7884 7907
7885 goto error; 7908 goto error;
7886 default: 7909 default:
7887 RAND_add(data, n, n); 7910 RAND_seed(data, n);
7888 7911
7889 count += n; 7912 count += n;
7890 } 7913 }