2020-10-13 15:04:59 +02:00
|
|
|
/******************************************************************************/
|
2020-10-19 21:56:12 +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! */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-10-19 21:56:12 +02:00
|
|
|
#include <slunkcrypt.h>
|
2020-10-13 15:04:59 +02:00
|
|
|
|
2020-10-13 19:33:01 +02:00
|
|
|
#ifdef _WIN32
|
2020-10-21 21:58:46 +02:00
|
|
|
# define WIN32_LEAN_AND_MEAN 1
|
|
|
|
# include <Windows.h>
|
2020-10-22 16:52:34 +02:00
|
|
|
# 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
|
2020-10-21 19:29:37 +02:00
|
|
|
#else
|
2020-10-21 21:58:46 +02:00
|
|
|
# include <unistd.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# include <string.h>
|
2020-10-22 16:52:34 +02:00
|
|
|
# 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
|
2020-10-21 21:58:46 +02:00
|
|
|
# include <sys/random.h>
|
2020-10-22 16:52:34 +02:00
|
|
|
# else
|
|
|
|
# include <pthread.h>
|
2020-10-21 21:58:46 +02:00
|
|
|
# endif
|
2020-10-13 15:37:40 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-22 16:52:34 +02:00
|
|
|
// ==========================================================================
|
|
|
|
// Initialization
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2020-10-21 21:58:46 +02:00
|
|
|
typedef BOOLEAN(WINAPI *genrandom_t)(void*, ULONG);
|
2020-10-22 16:52:34 +02:00
|
|
|
static genrandom_t win32_init_random(void)
|
2020-10-21 19:29:37 +02:00
|
|
|
{
|
2020-10-21 21:58:46 +02:00
|
|
|
static volatile LONG s_random_init = 0L;
|
|
|
|
static HMODULE s_advapi32 = NULL;
|
|
|
|
static genrandom_t s_genrandom = NULL;
|
2020-10-21 19:29:37 +02:00
|
|
|
LONG state;
|
2020-10-21 21:58:46 +02:00
|
|
|
while ((state = InterlockedCompareExchange(&s_random_init, -1L, 0L)) != 0L)
|
2020-10-21 19:29:37 +02:00
|
|
|
{
|
|
|
|
if (state > 0L)
|
|
|
|
{
|
2020-10-21 21:58:46 +02:00
|
|
|
return s_genrandom;
|
2020-10-21 19:29:37 +02:00
|
|
|
}
|
|
|
|
Sleep(0U);
|
|
|
|
}
|
2020-10-21 21:58:46 +02:00
|
|
|
if (s_advapi32 || (s_advapi32 = LoadLibraryW(L"advapi32.dll")))
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
2020-10-21 21:58:46 +02:00
|
|
|
if ((s_genrandom = (genrandom_t)GetProcAddress(s_advapi32, "SystemFunction036")))
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
2020-10-21 21:58:46 +02:00
|
|
|
InterlockedExchange(&s_random_init, 1L);
|
|
|
|
return s_genrandom;
|
2020-10-13 15:04:59 +02:00
|
|
|
}
|
2020-10-21 19:29:37 +02:00
|
|
|
}
|
2020-10-21 21:58:46 +02:00
|
|
|
InterlockedExchange(&s_random_init, 0L);
|
2020-10-21 19:29:37 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-22 16:52:34 +02:00
|
|
|
#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;
|
|
|
|
}
|
2020-10-21 19:29:37 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-22 16:52:34 +02:00
|
|
|
// ==========================================================================
|
|
|
|
// Public functions
|
|
|
|
// ==========================================================================
|
|
|
|
|
2020-10-21 19:29:37 +02:00
|
|
|
int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
|
|
|
|
{
|
2020-10-22 16:52:34 +02:00
|
|
|
#if defined(_WIN32)
|
2020-10-21 19:29:37 +02:00
|
|
|
if ((length <= ((size_t)ULONG_MAX)))
|
|
|
|
{
|
2020-10-22 16:52:34 +02:00
|
|
|
const genrandom_t genrandom = win32_init_random();
|
2020-10-21 19:29:37 +02:00
|
|
|
if (genrandom)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
2020-10-21 19:29:37 +02:00
|
|
|
return genrandom(buffer, (ULONG)length) ? 0 : (-1);
|
2020-10-13 15:04:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 19:29:37 +02:00
|
|
|
return -1;
|
2020-10-22 16:52:34 +02:00
|
|
|
#elif HAVE_GETRANDOM
|
2020-10-13 17:42:22 +02:00
|
|
|
if (getrandom(buffer, length, 0U) >= length)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
#else
|
2020-10-22 16:52:34 +02:00
|
|
|
const int fd = unix_init_random();
|
|
|
|
if (fd >= 0)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
2020-10-22 16:52:34 +02:00
|
|
|
if (read(fd, buffer, length) >= length)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
2020-10-22 16:52:34 +02:00
|
|
|
return 0;
|
2020-10-13 15:04:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-22 16:52:34 +02:00
|
|
|
return -1;
|
2020-10-13 17:42:22 +02:00
|
|
|
#endif
|
2020-10-13 15:04:59 +02:00
|
|
|
}
|
|
|
|
|
2020-10-19 21:56:12 +02:00
|
|
|
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))
|
|
|
|
{
|
2020-10-22 16:52:34 +02:00
|
|
|
#if HAVE_SECURE_ZERO_MEMORY
|
2020-10-14 13:14:47 +02:00
|
|
|
SecureZeroMemory(ptr, length);
|
2020-10-22 16:52:34 +02:00
|
|
|
#elif HAVE_EXPLICIT_BZERO
|
2020-10-14 13:14:47 +02:00
|
|
|
explicit_bzero(ptr, length);
|
2020-10-13 19:33:01 +02:00
|
|
|
#else
|
2020-10-21 17:07:03 +02:00
|
|
|
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
|
2020-10-14 13:14:47 +02:00
|
|
|
}
|
2020-10-13 15:04:59 +02:00
|
|
|
}
|