summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2016-08-30 10:25:36 +0200
committerOndřej Surý <ondrej@sury.org>2016-08-30 10:25:36 +0200
commit7d6202bfa85e8f0bf90590a9381757f15e296a33 (patch)
tree1949eb9256ab93dbe512a37b1e75720dbdce5e8e /src
parent6de837ba4e208260ac6043d521b0a1d79ffd58a7 (diff)
downloadluaossl-7d6202bfa85e8f0bf90590a9381757f15e296a33.tar.gz
luaossl-7d6202bfa85e8f0bf90590a9381757f15e296a33.tar.bz2
luaossl-7d6202bfa85e8f0bf90590a9381757f15e296a33.zip
Reinstate sysctl call for older Linux kernels
Diffstat (limited to 'src')
-rw-r--r--src/openssl.c37
1 files 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 @@
60#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) 60#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)
61#define HAVE_GETRANDOM 61#define HAVE_GETRANDOM
62#include <linux/random.h> 62#include <linux/random.h>
63#else
64#define HAVE_SYS_SYSCTL_H
63#endif 65#endif
64#endif 66#endif
65 67
@@ -7824,13 +7826,20 @@ static struct randL_state *randL_getstate(lua_State *L) {
7824 return lua_touserdata(L, lua_upvalueindex(1)); 7826 return lua_touserdata(L, lua_upvalueindex(1));
7825} /* randL_getstate() */ 7827} /* randL_getstate() */
7826 7828
7829#if HAVE_SYS_SYSCTL_H
7830#include <sys/sysctl.h> /* CTL_KERN KERN_RANDOM RANDOM_UUID KERN_URND KERN_ARND sysctl(2) */
7831#endif
7832
7833#ifndef HAVE_RANDOM_UUID
7834#define HAVE_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux__) /* RANDOM_UUID is an enum, not macro */
7835#endif
7827 7836
7828static int randL_stir(struct randL_state *st, unsigned rqstd) { 7837static int randL_stir(struct randL_state *st, unsigned rqstd) {
7829 unsigned count = 0; 7838 unsigned count = 0;
7830 int error; 7839 int error;
7831 unsigned char data[256]; 7840 unsigned char data[256];
7832#if HAVE_ARC4RANDOM 7841#if defined(HAVE_ARC4RANDOM)
7833 while (count < rqst) { 7842 while (count < rqstd) {
7834 size_t n = MIN(rqstd - count, sizeof data); 7843 size_t n = MIN(rqstd - count, sizeof data);
7835 7844
7836 arc4random(data, n); 7845 arc4random(data, n);
@@ -7839,22 +7848,34 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) {
7839 7848
7840 count += n; 7849 count += n;
7841 } 7850 }
7842#endif 7851#elif defined(HAVE_GETRANDOM)
7843 7852 while (count < rqstd) {
7844#if HAVE_GETRANDOM
7845 while (count < rqst) {
7846 size_t n = MIN(rqstd - count, sizeof data); 7853 size_t n = MIN(rqstd - count, sizeof data);
7847 7854
7848 n = getrandom(data, n, 0); 7855 n = getrandom(data, n, 0);
7849 7856
7850 if (n == -1) { 7857 if (n == -1) {
7851 break; 7858 break;
7852 } 7859 }
7853 7860
7854 RAND_add(data, n, n); 7861 RAND_add(data, n, n);
7862
7863 count += n;
7864 }
7865#elif HAVE_RANDOM_UUID
7866 int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID };
7867
7868 while (count < rqstd) {
7869 size_t n = MIN(rqstd - count, sizeof data);
7870
7871 if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0))
7872 break;
7873
7874 RAND_add(data, n, n);
7855 7875
7856 count += n; 7876 count += n;
7857 } 7877 }
7878
7858#endif 7879#endif
7859 7880
7860 if (count < rqstd) { 7881 if (count < rqstd) {