diff options
| author | Ondřej Surý <ondrej@sury.org> | 2016-08-30 10:08:07 +0200 |
|---|---|---|
| committer | Ondřej Surý <ondrej@sury.org> | 2016-08-30 10:08:07 +0200 |
| commit | 6de837ba4e208260ac6043d521b0a1d79ffd58a7 (patch) | |
| tree | 6370c790fadc971c6fc3ef7d16eff1d5ece5e747 /src | |
| parent | 40951862e12fe8d9c2fd0ffd4f16e9fe4d951f33 (diff) | |
| download | luaossl-6de837ba4e208260ac6043d521b0a1d79ffd58a7.tar.gz luaossl-6de837ba4e208260ac6043d521b0a1d79ffd58a7.tar.bz2 luaossl-6de837ba4e208260ac6043d521b0a1d79ffd58a7.zip | |
Use arc4random()/getrandom() to get random bytes instead of sysctl() interface
Diffstat (limited to 'src')
| -rw-r--r-- | src/openssl.c | 63 |
1 files 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 @@ | |||
| 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__) | ||
| 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 <linux/random.h> | ||
| 63 | #endif | ||
| 51 | #endif | 64 | #endif |
| 52 | 65 | ||
| 53 | #include <openssl/opensslconf.h> | 66 | #include <openssl/opensslconf.h> |
| @@ -7811,44 +7824,16 @@ static struct randL_state *randL_getstate(lua_State *L) { | |||
| 7811 | return lua_touserdata(L, lua_upvalueindex(1)); | 7824 | return lua_touserdata(L, lua_upvalueindex(1)); |
| 7812 | } /* randL_getstate() */ | 7825 | } /* randL_getstate() */ |
| 7813 | 7826 | ||
| 7814 | #ifndef HAVE_SYS_SYSCTL_H | ||
| 7815 | #define HAVE_SYS_SYSCTL_H (BSD || __GLIBC__) | ||
| 7816 | #endif | ||
| 7817 | |||
| 7818 | #if HAVE_SYS_SYSCTL_H | ||
| 7819 | #include <sys/sysctl.h> /* CTL_KERN KERN_RANDOM RANDOM_UUID KERN_URND KERN_ARND sysctl(2) */ | ||
| 7820 | #endif | ||
| 7821 | |||
| 7822 | #ifndef HAVE_RANDOM_UUID | ||
| 7823 | #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 | ||
| 7833 | 7827 | ||
| 7834 | static int randL_stir(struct randL_state *st, unsigned rqstd) { | 7828 | static int randL_stir(struct randL_state *st, unsigned rqstd) { |
| 7835 | unsigned count = 0; | 7829 | unsigned count = 0; |
| 7836 | int error; | 7830 | int error; |
| 7837 | unsigned char data[256]; | 7831 | unsigned char data[256]; |
| 7838 | #if HAVE_RANDOM_UUID || HAVE_KERN_URND || HAVE_KERN_ARND | 7832 | #if HAVE_ARC4RANDOM |
| 7839 | #if HAVE_RANDOM_UUID | 7833 | while (count < rqst) { |
| 7840 | 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 | |||
| 7847 | while (count < rqstd) { | ||
| 7848 | size_t n = MIN(rqstd - count, sizeof data); | 7834 | size_t n = MIN(rqstd - count, sizeof data); |
| 7849 | 7835 | ||
| 7850 | if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) | 7836 | arc4random(data, n); |
| 7851 | break; | ||
| 7852 | 7837 | ||
| 7853 | RAND_add(data, n, n); | 7838 | RAND_add(data, n, n); |
| 7854 | 7839 | ||
| @@ -7856,6 +7841,22 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { | |||
| 7856 | } | 7841 | } |
| 7857 | #endif | 7842 | #endif |
| 7858 | 7843 | ||
| 7844 | #if HAVE_GETRANDOM | ||
| 7845 | while (count < rqst) { | ||
| 7846 | size_t n = MIN(rqstd - count, sizeof data); | ||
| 7847 | |||
| 7848 | n = getrandom(data, n, 0); | ||
| 7849 | |||
| 7850 | if (n == -1) { | ||
| 7851 | break; | ||
| 7852 | } | ||
| 7853 | |||
| 7854 | RAND_add(data, n, n); | ||
| 7855 | |||
| 7856 | count += n; | ||
| 7857 | } | ||
| 7858 | #endif | ||
| 7859 | |||
| 7859 | if (count < rqstd) { | 7860 | if (count < rqstd) { |
| 7860 | #if defined O_CLOEXEC && (!defined _AIX /* O_CLOEXEC overflows int */) | 7861 | #if defined O_CLOEXEC && (!defined _AIX /* O_CLOEXEC overflows int */) |
| 7861 | int fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC); | 7862 | int fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC); |
