Switch to using getentropy() function, because it is available on OpenBSD and it doesn't require an additional header.

This commit is contained in:
LoRd_MuldeR 2021-04-20 21:01:58 +02:00
parent acc52ce8d8
commit b63dfd1a44
Signed by: mulder
GPG Key ID: 2B5913365F57E03F

View File

@ -36,14 +36,14 @@
# define ATTRIB_DESTRUCTOR __attribute__((destructor)) # define ATTRIB_DESTRUCTOR __attribute__((destructor))
#endif #endif
/* detect getrandom() support */ /* detect getentropy() support */
#undef SYS_GETRANDOM #undef SYSCALL_GETENTROPY
#if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25) #if defined(__linux__) && defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
# define SYS_GETRANDOM 1 # define SYSCALL_GETENTROPY getentropy
# include <sys/random.h>
#elif defined(__FreeBSD__) && (__FreeBSD__ >= 12) #elif defined(__FreeBSD__) && (__FreeBSD__ >= 12)
# define SYS_GETRANDOM 1 # define SYSCALL_GETENTROPY getentropy
# include <sys/random.h> #elif defined(__OpenBSD__) && (__OpenBSD__ >= 1)
# define SYSCALL_GETENTROPY getentropy
#endif #endif
/* detect explicit_bzero() support */ /* detect explicit_bzero() support */
@ -54,6 +54,8 @@
# define EXPLICIT_BZERO explicit_bzero # define EXPLICIT_BZERO explicit_bzero
#elif defined(__FreeBSD__) && (__FreeBSD__ >= 11) #elif defined(__FreeBSD__) && (__FreeBSD__ >= 11)
# define EXPLICIT_BZERO explicit_bzero # define EXPLICIT_BZERO explicit_bzero
#elif defined(__OpenBSD__) && (__OpenBSD__ >= 1)
# define EXPLICIT_BZERO explicit_bzero
#endif #endif
// ========================================================================== // ==========================================================================
@ -130,9 +132,9 @@ static void init_random_source(void)
s_rtlgenrandom = (rtlgenrandom_t) GetProcAddress(s_advapi32, "SystemFunction036"); s_rtlgenrandom = (rtlgenrandom_t) GetProcAddress(s_advapi32, "SystemFunction036");
} }
#else #else
#if defined(SYS_GETRANDOM) #if defined(SYSCALL_GETENTROPY)
uint8_t temp; uint8_t temp;
if (getrandom(&temp, 0U, 0U) >= 0) if (SYSCALL_GETENTROPY(&temp, sizeof(uint8_t)) >= 0)
{ {
goto init_completed; goto init_completed;
} }
@ -158,8 +160,8 @@ size_t slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
#if defined(_WIN32) #if defined(_WIN32)
if (s_rtlgenrandom) if (s_rtlgenrandom)
{ {
const ULONG buff_size = (ULONG)length; const ULONG buff_length = (ULONG)length;
return s_rtlgenrandom(buffer, buff_size) ? buff_size : 0U; return s_rtlgenrandom(buffer, buff_length) ? buff_length : 0U;
} }
#else #else
if (s_random_fd >= 0) if (s_random_fd >= 0)
@ -167,11 +169,10 @@ size_t slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
const ssize_t result = read(s_random_fd, buffer, length); const ssize_t result = read(s_random_fd, buffer, length);
return (result < 0) ? 0U : ((size_t)result); return (result < 0) ? 0U : ((size_t)result);
} }
#if defined(SYS_GETRANDOM) #if defined(SYSCALL_GETENTROPY)
else else
{ {
const ssize_t result = getrandom(buffer, length, 0U); return (SYSCALL_GETENTROPY(buffer, length) >= 0) ? length : 0U;
return (result < 0) ? 0U : ((size_t)result);
} }
#endif #endif
#endif #endif