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