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 <sys/random.h> on Windows

* getentropy needs different header on old Linux versions

* Move getentropy check to different autoconf file
This commit is contained in:
oittaa 2018-10-10 03:36:26 +03:00 committed by Douglas Stebila
parent 4f32761b5c
commit 654e2e5af3
2 changed files with 17 additions and 1 deletions

View File

@ -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)
]
)

View File

@ -8,6 +8,11 @@
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#if defined(__APPLE__)
#include <sys/random.h>
#else
#include <unistd.h>
#endif
#endif
#include <fcntl.h>
@ -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;