Only use getrandom on recent glibc, when TLS not in use.

This commit is contained in:
Roger A. Light 2019-02-27 20:52:17 +00:00
parent 4564ff1232
commit 1a3eaeabce

View File

@ -28,8 +28,10 @@ Contributors:
# include <sys/stat.h> # include <sys/stat.h>
#endif #endif
#ifdef __linux__ #if !defined(WITH_TLS) && defined(__linux__)
# include <sys/random.h> # if defined(__GLIBC__) && __GLIBC_PREREQ(2, 25)
# include <sys/random.h>
# endif
#endif #endif
#ifdef WITH_TLS #ifdef WITH_TLS
@ -310,12 +312,11 @@ int util__random_bytes(void *bytes, int count)
if(RAND_bytes(bytes, count) == 1){ if(RAND_bytes(bytes, count) == 1){
rc = MOSQ_ERR_SUCCESS; rc = MOSQ_ERR_SUCCESS;
} }
#else #elif defined(__GLIBC__) && __GLIBC_PREREQ(2, 25)
# ifdef __GLIBC__
if(getrandom(bytes, count, 0) == 0){ if(getrandom(bytes, count, 0) == 0){
rc = MOSQ_ERR_SUCCESS; rc = MOSQ_ERR_SUCCESS;
} }
# elif defined(WIN32) #elif defined(WIN32)
HRYPTPROV provider; HRYPTPROV provider;
if(!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)){ if(!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)){
@ -327,14 +328,13 @@ int util__random_bytes(void *bytes, int count)
} }
CryptReleaseContext(provider, 0); CryptReleaseContext(provider, 0);
# else #else
int i; int i;
for(i=0; i<count; i++){ for(i=0; i<count; i++){
((uint8_t *)bytes)[i] = (uint8_t )(random()&0xFF); ((uint8_t *)bytes)[i] = (uint8_t )(random()&0xFF);
} }
rc = MOSQ_ERR_SUCCESS; rc = MOSQ_ERR_SUCCESS;
# endif
#endif #endif
return rc; return rc;
} }