Improved slunkcrypt_random_bytes() on Unix-systems without getrandom() support.

This commit is contained in:
LoRd_MuldeR 2020-10-22 16:52:34 +02:00
parent 183b40f97d
commit 180945bd1b
Signed by: mulder
GPG Key ID: 2B5913365F57E03F

View File

@ -8,18 +8,44 @@
#ifdef _WIN32 #ifdef _WIN32
# define WIN32_LEAN_AND_MEAN 1 # define WIN32_LEAN_AND_MEAN 1
# include <Windows.h> # include <Windows.h>
# if defined(SecureZeroMemory)
# define HAVE_SECURE_ZERO_MEMORY 1
# else
# define HAVE_SECURE_ZERO_MEMORY 0
# endif
# define HAVE_GETRANDOM 0
# define HAVE_EXPLICIT_BZERO 0
#else #else
# include <unistd.h> # include <unistd.h>
# include <fcntl.h> # include <fcntl.h>
# include <string.h> # include <string.h>
# if (defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) # if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
# define HAVE_GETRANDOM 1
# define HAVE_EXPLICIT_BZERO 1
# elif defined(__FreeBSD__) && (__FreeBSD__ >= 12)
# define HAVE_GETRANDOM 1
# define HAVE_EXPLICIT_BZERO 1
# elif defined(__FreeBSD__) && (__FreeBSD__ >= 11)
# define HAVE_GETRANDOM 0
# define HAVE_EXPLICIT_BZERO 1
# else
# define HAVE_GETRANDOM 0
# define HAVE_EXPLICIT_BZERO 0
# endif
# if HAVE_GETRANDOM
# include <sys/random.h> # include <sys/random.h>
# else
# include <pthread.h>
# endif # endif
#endif #endif
#ifdef _WIN32 // ==========================================================================
// Initialization
// ==========================================================================
#if defined(_WIN32)
typedef BOOLEAN(WINAPI *genrandom_t)(void*, ULONG); typedef BOOLEAN(WINAPI *genrandom_t)(void*, ULONG);
static genrandom_t win32_init_genrandom() static genrandom_t win32_init_random(void)
{ {
static volatile LONG s_random_init = 0L; static volatile LONG s_random_init = 0L;
static HMODULE s_advapi32 = NULL; static HMODULE s_advapi32 = NULL;
@ -44,42 +70,63 @@ static genrandom_t win32_init_genrandom()
InterlockedExchange(&s_random_init, 0L); InterlockedExchange(&s_random_init, 0L);
return NULL; return NULL;
} }
#elif !HAVE_GETRANDOM
static int unix_init_random(void)
{
static pthread_mutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER;
static int s_random_fd = -1;
static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL };
if (pthread_mutex_lock(&s_mutex) != 0)
{
return -1;
}
if (s_random_fd < 0)
{
for (size_t i = 0U; DEV_RANDOM[i]; ++i)
{
if ((s_random_fd = open(DEV_RANDOM[i], O_RDONLY)) >= 0)
{
break;
}
}
}
pthread_mutex_unlock(&s_mutex);
return s_random_fd;
}
#endif #endif
// ==========================================================================
// Public functions
// ==========================================================================
int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length) int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
{ {
#ifdef _WIN32 #if defined(_WIN32)
if ((length <= ((size_t)ULONG_MAX))) if ((length <= ((size_t)ULONG_MAX)))
{ {
const genrandom_t genrandom = win32_init_genrandom(); const genrandom_t genrandom = win32_init_random();
if (genrandom) if (genrandom)
{ {
return genrandom(buffer, (ULONG)length) ? 0 : (-1); return genrandom(buffer, (ULONG)length) ? 0 : (-1);
} }
} }
return -1; return -1;
#elif (defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) #elif HAVE_GETRANDOM
if (getrandom(buffer, length, 0U) >= length) if (getrandom(buffer, length, 0U) >= length)
{ {
return 0; return 0;
} }
return -1; return -1;
#else #else
static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL }; const int fd = unix_init_random();
int result = -1; if (fd >= 0)
for (size_t i = 0U; DEV_RANDOM[i] && (result != 0); ++i)
{ {
const int fd = open(DEV_RANDOM[i], O_RDONLY); if (read(fd, buffer, length) >= length)
if (fd >= 0)
{ {
if (read(fd, buffer, length) >= length) return 0;
{
result = 0;
}
close(fd);
} }
} }
return result; return -1;
#endif #endif
} }
@ -87,9 +134,9 @@ void slunkcrypt_bzero(void* const ptr, const size_t length)
{ {
if ((ptr) && (length > 0U)) if ((ptr) && (length > 0U))
{ {
#if defined(_WIN32) && defined(SecureZeroMemory) #if HAVE_SECURE_ZERO_MEMORY
SecureZeroMemory(ptr, length); SecureZeroMemory(ptr, length);
#elif (defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11)) #elif HAVE_EXPLICIT_BZERO
explicit_bzero(ptr, length); explicit_bzero(ptr, length);
#else #else
volatile uint8_t *buffer = (volatile uint8_t*)ptr; volatile uint8_t *buffer = (volatile uint8_t*)ptr;