SlunkCrypt/libslunkcrypt/src/internal.c

108 lines
2.5 KiB
C
Raw Normal View History

2020-10-13 15:04:59 +02:00
/******************************************************************************/
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
2020-10-13 15:04:59 +02:00
/* This work has been released under the CC0 1.0 Universal license! */
/******************************************************************************/
#include <slunkcrypt.h>
2020-10-13 15:04:59 +02:00
2020-10-13 19:33:01 +02:00
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
2020-10-13 19:33:01 +02:00
#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;
2020-10-13 19:33:01 +02:00
#else
2020-10-13 15:37:40 +02:00
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12))
#include <sys/random.h>
#else
static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL };
2020-10-13 19:33:01 +02:00
#endif
2020-10-13 15:37:40 +02:00
#endif
2020-10-13 15:04:59 +02:00
#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")))
2020-10-13 15:04:59 +02:00
{
if ((g_genrandom = (genrandom_t)GetProcAddress(g_advapi32, "SystemFunction036")))
2020-10-13 15:04:59 +02:00
{
InterlockedExchange(&g_random_init, 1L);
return g_genrandom;
2020-10-13 15:04:59 +02:00
}
}
InterlockedExchange(&g_random_init, 0L);
return NULL;
}
#endif
int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
{
#ifdef _WIN32
if ((length <= ((size_t)ULONG_MAX)))
{
const genrandom_t genrandom = init_genrandom();
if (genrandom)
2020-10-13 15:04:59 +02:00
{
return genrandom(buffer, (ULONG)length) ? 0 : (-1);
2020-10-13 15:04:59 +02:00
}
}
return -1;
2020-10-13 15:04:59 +02:00
#else
#if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12))
if (getrandom(buffer, length, 0U) >= length)
{
return 0;
}
return -1;
#else
2020-10-13 15:04:59 +02:00
int result = -1;
for (size_t i = 0U; DEV_RANDOM[i] && (result != 0); ++i)
2020-10-13 15:04:59 +02:00
{
const int fd = open(DEV_RANDOM[i], O_RDONLY);
2020-10-13 15:04:59 +02:00
if (fd >= 0)
{
2020-10-13 15:37:40 +02:00
if (read(fd, buffer, length) >= length)
2020-10-13 15:04:59 +02:00
{
result = 0;
}
close(fd);
}
}
return result;
#endif
2020-10-13 15:04:59 +02:00
#endif
}
void slunkcrypt_bzero(void* const ptr, const size_t length)
2020-10-13 15:04:59 +02:00
{
2020-10-14 13:14:47 +02:00
if ((ptr) && (length > 0U))
{
#if defined(_WIN32) && defined(SecureZeroMemory)
2020-10-14 13:14:47 +02:00
SecureZeroMemory(ptr, length);
2020-10-13 19:33:01 +02:00
#else
#if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11))
2020-10-14 13:14:47 +02:00
explicit_bzero(ptr, length);
2020-10-13 19:33:01 +02:00
#else
volatile uint8_t *buffer = (volatile uint8_t*)ptr;
2020-10-14 13:14:47 +02:00
for (size_t i = 0U; i < length; ++i)
{
buffer[i] = 0U;
}
2020-10-13 19:33:01 +02:00
#endif
#endif
2020-10-14 13:14:47 +02:00
}
2020-10-13 15:04:59 +02:00
}