From 654e2e5af3095c20930cffc399d1e4bba25bf956 Mon Sep 17 00:00:00 2001 From: oittaa Date: Wed, 10 Oct 2018 03:36:26 +0300 Subject: [PATCH] Use getentropy() if available (#407) * Check if getentropy exists in configure.ac * Use getentropy in rand.c if available * rand.c: try to fix broken osx * Use /dev/urandom with broken operating systems * Enable getentropy on macOS * Don't include on Windows * getentropy needs different header on old Linux versions * Move getentropy check to different autoconf file --- config/ac_checks.m4 | 1 + src/common/rand.c | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/config/ac_checks.m4 b/config/ac_checks.m4 index 393f251ee..f45376d86 100644 --- a/config/ac_checks.m4 +++ b/config/ac_checks.m4 @@ -21,5 +21,6 @@ AC_TYPE_UINT8_T AC_FUNC_MALLOC AC_CHECK_FUNCS([gettimeofday memmove memset pow sqrt strdup]) AC_CHECK_SIZEOF([size_t]) +AC_CHECK_FUNCS(getentropy) ] ) diff --git a/src/common/rand.c b/src/common/rand.c index 1ce8712ff..25f6f2471 100644 --- a/src/common/rand.c +++ b/src/common/rand.c @@ -8,6 +8,11 @@ #include #include #include +#if defined(__APPLE__) +#include +#else +#include +#endif #endif #include @@ -51,12 +56,21 @@ void OQS_randombytes(uint8_t *random_array, size_t bytes_to_read) { oqs_randombytes_algorithm(random_array, bytes_to_read); } +#if !defined(_WIN32) +#if defined(HAVE_GETENTROPY) +void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { + + int rc; + do { + rc = getentropy(random_array, bytes_to_read); + } while (rc != 0); +} +#else static __inline void delay(unsigned int count) { while (count--) { } } -#if !defined(_WIN32) void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { FILE *handle; @@ -82,6 +96,7 @@ void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { } fclose(handle); } +#endif #else void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { HCRYPTPROV hCryptProv;