Use RtlGenRandom() directly on the Windows platform.

This commit is contained in:
LoRd_MuldeR 2020-10-21 19:29:37 +02:00
parent 39d9aab439
commit 24574712d1
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
2 changed files with 43 additions and 26 deletions

View File

@ -512,7 +512,7 @@ static int run_test_case(const char *const message)
goto clean_up; goto clean_up;
} }
if (strncmp(message, text_temp, length) != 0) if (memcmp(message, text_temp, length * sizeof(char)) != 0)
{ {
FPUTS(T("\n\nWhoops: Decrypted message does *not* match the original message!\n\n"), stderr); FPUTS(T("\n\nWhoops: Decrypted message does *not* match the original message!\n\n"), stderr);
goto clean_up; goto clean_up;

View File

@ -3,58 +3,75 @@
/* This work has been released under the CC0 1.0 Universal license! */ /* This work has been released under the CC0 1.0 Universal license! */
/******************************************************************************/ /******************************************************************************/
#ifdef _WIN32
#define _WIN32_WINNT 0x0600
#define _CRT_RAND_S 1
#define WIN32_LEAN_AND_MEAN 1
#endif
#include <slunkcrypt.h> #include <slunkcrypt.h>
#ifdef _WIN32 #ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <Windows.h> #include <Windows.h>
typedef BOOLEAN(WINAPI *genrandom_t)(void*, ULONG);
static volatile LONG g_random_init = 0L;
static HMODULE g_advapi32 = NULL;
static genrandom_t g_genrandom = NULL;
#else #else
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
#if (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) #if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12))
#include <sys/random.h> #include <sys/random.h>
#else
static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL };
#endif #endif
#endif #endif
#ifdef _WIN32
static genrandom_t init_genrandom()
{
LONG state;
while ((state = InterlockedCompareExchange(&g_random_init, -1L, 0L)) != 0L)
{
if (state > 0L)
{
return g_genrandom;
}
Sleep(0U);
}
if (g_advapi32 || (g_advapi32 = LoadLibrary(L"advapi32.dll")))
{
if ((g_genrandom = (genrandom_t)GetProcAddress(g_advapi32, "SystemFunction036")))
{
InterlockedExchange(&g_random_init, 1L);
return g_genrandom;
}
}
InterlockedExchange(&g_random_init, 0L);
return NULL;
}
#endif
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 #ifdef _WIN32
size_t pos = 0U; if ((length <= ((size_t)ULONG_MAX)))
while (pos < length)
{ {
const size_t bytes_left = length - pos; const genrandom_t genrandom = init_genrandom();
const size_t bytes_copy = (bytes_left < sizeof(uint32_t)) ? bytes_left : sizeof(uint32_t); if (genrandom)
uint32_t temp;
if (rand_s(&temp) != 0)
{ {
return -1; return genrandom(buffer, (ULONG)length) ? 0 : (-1);
}
for (size_t i = 0; i < bytes_copy; ++i)
{
buffer[pos++] = (uint8_t)(temp & 0xFF);
temp >>= 8;
} }
} }
return 0; return -1;
#else #else
#if (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) #if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12))
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 PATH[] = { "/dev/urandom", "/dev/arandom", "/dev/random" };
int result = -1; int result = -1;
for (size_t i = 0; (i < 3U) && (result < 0); ++i) for (size_t i = 0U; DEV_RANDOM[i] && (result != 0); ++i)
{ {
const int fd = open(PATH[i], O_RDONLY); const int fd = open(DEV_RANDOM[i], O_RDONLY);
if (fd >= 0) if (fd >= 0)
{ {
if (read(fd, buffer, length) >= length) if (read(fd, buffer, length) >= length)
@ -76,7 +93,7 @@ void slunkcrypt_bzero(void* const ptr, const size_t length)
#if defined(_WIN32) && defined(SecureZeroMemory) #if defined(_WIN32) && defined(SecureZeroMemory)
SecureZeroMemory(ptr, length); SecureZeroMemory(ptr, length);
#else #else
#if (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11)) #if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11))
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;