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-11-04 23:17:59 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
# define WIN32_LEAN_AND_MEAN 1
|
|
|
|
# define _CRT_SECURE_NO_WARNINGS 1
|
|
|
|
#else
|
|
|
|
# define _GNU_SOURCE 1
|
|
|
|
#endif
|
|
|
|
|
2020-10-26 19:56:45 +01:00
|
|
|
/* Internal */
|
2021-04-17 15:13:13 +02:00
|
|
|
#include "slunkcrypt.h"
|
|
|
|
#include "compiler.h"
|
2020-10-13 15:04:59 +02:00
|
|
|
|
2020-10-26 19:56:45 +01:00
|
|
|
/* CRT */
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
2021-04-21 13:52:49 +02:00
|
|
|
/* Utils */
|
|
|
|
static INLINE size_t MIN_SIZE(const size_t a, const size_t b) { return (a > b) ? b : a; }
|
|
|
|
|
2021-04-17 15:13:13 +02:00
|
|
|
// ==========================================================================
|
|
|
|
// Platform compatibility
|
|
|
|
// ==========================================================================
|
|
|
|
|
2020-10-13 19:33:01 +02:00
|
|
|
#ifdef _WIN32
|
2020-10-21 21:58:46 +02:00
|
|
|
# include <Windows.h>
|
2020-10-21 19:29:37 +02:00
|
|
|
#else
|
2020-10-21 21:58:46 +02:00
|
|
|
# include <unistd.h>
|
2020-10-28 14:40:13 +01:00
|
|
|
# include <pthread.h>
|
2020-10-13 15:37:40 +02:00
|
|
|
#endif
|
|
|
|
|
2021-04-17 15:13:13 +02:00
|
|
|
/* detect destructor support */
|
|
|
|
#undef ATTRIB_DESTRUCTOR
|
2020-10-28 16:21:01 +01:00
|
|
|
#if defined(__GNUC__) || defined(__clang__)
|
2021-04-17 15:13:13 +02:00
|
|
|
# define ATTRIB_DESTRUCTOR __attribute__((destructor))
|
|
|
|
#endif
|
|
|
|
|
2021-04-20 21:01:58 +02:00
|
|
|
/* detect getentropy() support */
|
2021-04-21 13:52:49 +02:00
|
|
|
#undef GETENTROPY
|
2021-04-20 21:01:58 +02:00
|
|
|
#if defined(__linux__) && defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
|
2021-04-21 13:52:49 +02:00
|
|
|
# define GETENTROPY getentropy
|
2021-04-17 15:13:13 +02:00
|
|
|
#elif defined(__FreeBSD__) && (__FreeBSD__ >= 12)
|
2021-04-21 13:52:49 +02:00
|
|
|
# define GETENTROPY getentropy
|
2021-04-20 21:01:58 +02:00
|
|
|
#elif defined(__OpenBSD__) && (__OpenBSD__ >= 1)
|
2021-04-21 13:52:49 +02:00
|
|
|
# define GETENTROPY getentropy
|
2021-04-17 15:13:13 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* detect explicit_bzero() support */
|
|
|
|
#undef EXPLICIT_BZERO
|
2021-04-17 16:05:32 +02:00
|
|
|
#if defined(_WIN32) && defined(SecureZeroMemory)
|
2021-04-17 15:13:13 +02:00
|
|
|
# define EXPLICIT_BZERO SecureZeroMemory
|
|
|
|
#elif defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
|
|
|
|
# define EXPLICIT_BZERO explicit_bzero
|
|
|
|
#elif defined(__FreeBSD__) && (__FreeBSD__ >= 11)
|
|
|
|
# define EXPLICIT_BZERO explicit_bzero
|
2021-04-20 21:01:58 +02:00
|
|
|
#elif defined(__OpenBSD__) && (__OpenBSD__ >= 1)
|
|
|
|
# define EXPLICIT_BZERO explicit_bzero
|
2020-10-28 16:21:01 +01:00
|
|
|
#endif
|
|
|
|
|
2020-10-26 19:56:45 +01:00
|
|
|
// ==========================================================================
|
2020-10-28 21:58:24 +01:00
|
|
|
// Call once support
|
2020-10-26 19:56:45 +01:00
|
|
|
// ==========================================================================
|
|
|
|
|
2020-10-28 14:40:13 +01:00
|
|
|
#ifdef _WIN32
|
2020-10-28 21:58:24 +01:00
|
|
|
# define CALL_ONCE win32_call_once
|
|
|
|
# define CALL_ONCE_TYPE volatile LONG
|
|
|
|
# define CALL_ONCE_INIT 0L
|
2020-10-28 14:40:13 +01:00
|
|
|
#else
|
2020-10-28 21:58:24 +01:00
|
|
|
# define CALL_ONCE pthread_once
|
|
|
|
# define CALL_ONCE_TYPE pthread_once_t
|
|
|
|
# define CALL_ONCE_INIT PTHREAD_ONCE_INIT
|
2020-10-28 14:40:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2020-10-28 21:58:24 +01:00
|
|
|
static void win32_call_once(CALL_ONCE_TYPE *const control, void (*init_routine)(void))
|
2020-10-26 19:56:45 +01:00
|
|
|
{
|
2020-10-28 14:40:13 +01:00
|
|
|
LONG status;
|
|
|
|
while ((status = InterlockedCompareExchange(control, -1L, 0L)) != 0L)
|
2020-10-26 19:56:45 +01:00
|
|
|
{
|
2020-10-28 14:40:13 +01:00
|
|
|
if(status > 0L)
|
|
|
|
{
|
|
|
|
return; /*already initialized*/
|
|
|
|
}
|
|
|
|
SwitchToThread();
|
2020-10-26 19:56:45 +01:00
|
|
|
}
|
2020-10-28 14:40:13 +01:00
|
|
|
init_routine();
|
|
|
|
InterlockedExchange(control, 1L);
|
2020-10-26 19:56:45 +01:00
|
|
|
}
|
2020-10-28 14:40:13 +01:00
|
|
|
#endif
|
2020-10-26 19:56:45 +01:00
|
|
|
|
2020-10-22 16:52:34 +02:00
|
|
|
// ==========================================================================
|
2020-10-28 14:40:13 +01:00
|
|
|
// Random bytes
|
2020-10-22 16:52:34 +02:00
|
|
|
// ==========================================================================
|
|
|
|
|
2020-10-28 14:40:13 +01:00
|
|
|
/* Global state */
|
2020-10-28 21:58:24 +01:00
|
|
|
static CALL_ONCE_TYPE s_random_is_initialized = CALL_ONCE_INIT;
|
2020-10-22 16:52:34 +02:00
|
|
|
#if defined(_WIN32)
|
2021-04-21 13:52:49 +02:00
|
|
|
typedef BOOLEAN(WINAPI *rtlgenrandom_t)(void *buffer, ULONG length);
|
2021-04-17 15:13:13 +02:00
|
|
|
static HMODULE s_advapi32 = NULL;
|
|
|
|
static rtlgenrandom_t s_rtlgenrandom = NULL;
|
2021-04-17 16:05:32 +02:00
|
|
|
#else
|
2020-10-22 18:01:59 +02:00
|
|
|
static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL };
|
|
|
|
static int s_random_fd = -1;
|
|
|
|
#endif
|
|
|
|
|
2020-10-28 21:58:24 +01:00
|
|
|
/* De-initialize CSRNG */
|
|
|
|
static void exit_random_source(void)
|
2020-10-21 19:29:37 +02:00
|
|
|
{
|
2020-10-26 19:56:45 +01:00
|
|
|
#if defined(_WIN32)
|
2021-04-17 15:13:13 +02:00
|
|
|
s_rtlgenrandom = NULL;
|
|
|
|
if (s_advapi32)
|
2020-10-28 14:40:13 +01:00
|
|
|
{
|
2021-04-17 15:13:13 +02:00
|
|
|
FreeLibrary(s_advapi32);
|
|
|
|
s_advapi32 = NULL;
|
2020-10-28 14:40:13 +01:00
|
|
|
}
|
2021-04-17 16:05:32 +02:00
|
|
|
#else
|
2020-10-28 14:40:13 +01:00
|
|
|
if (s_random_fd >= 0)
|
|
|
|
{
|
|
|
|
close(s_random_fd);
|
|
|
|
s_random_fd = -1;
|
2020-10-26 19:56:45 +01:00
|
|
|
}
|
2020-10-28 14:40:13 +01:00
|
|
|
#endif
|
2020-10-21 19:29:37 +02:00
|
|
|
}
|
2020-10-22 18:01:59 +02:00
|
|
|
|
2020-10-28 14:40:13 +01:00
|
|
|
/* Initialize CSRNG */
|
|
|
|
static void init_random_source(void)
|
2020-10-22 16:52:34 +02:00
|
|
|
{
|
2020-10-26 19:56:45 +01:00
|
|
|
#if defined(_WIN32)
|
2021-04-17 15:13:13 +02:00
|
|
|
if ((s_advapi32 = LoadLibraryW(L"advapi32.dll")))
|
2020-10-28 14:40:13 +01:00
|
|
|
{
|
2021-04-17 15:13:13 +02:00
|
|
|
s_rtlgenrandom = (rtlgenrandom_t) GetProcAddress(s_advapi32, "SystemFunction036");
|
2020-10-28 14:40:13 +01:00
|
|
|
}
|
2021-04-17 16:05:32 +02:00
|
|
|
#else
|
2021-04-21 13:52:49 +02:00
|
|
|
#if defined(GETENTROPY)
|
2021-04-17 16:05:32 +02:00
|
|
|
uint8_t temp;
|
2021-04-21 13:52:49 +02:00
|
|
|
if (GETENTROPY(&temp, sizeof(uint8_t)) >= 0)
|
2021-04-17 16:05:32 +02:00
|
|
|
{
|
|
|
|
goto init_completed;
|
|
|
|
}
|
|
|
|
#endif
|
2020-10-28 14:40:13 +01:00
|
|
|
for (size_t i = 0U; DEV_RANDOM[i]; ++i)
|
|
|
|
{
|
|
|
|
if ((s_random_fd = open(DEV_RANDOM[i], O_RDONLY)) >= 0)
|
2020-10-26 19:56:45 +01:00
|
|
|
{
|
2021-04-17 16:05:32 +02:00
|
|
|
goto init_completed;
|
2020-10-26 19:56:45 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-17 16:05:32 +02:00
|
|
|
init_completed: ;
|
2020-10-28 14:40:13 +01:00
|
|
|
#endif
|
2021-04-17 15:13:13 +02:00
|
|
|
#if !defined(ATTRIB_DESTRUCTOR)
|
2020-10-28 21:58:24 +01:00
|
|
|
atexit(exit_random_source);
|
|
|
|
#endif
|
2020-10-22 18:01:59 +02:00
|
|
|
}
|
2020-10-21 19:29:37 +02:00
|
|
|
|
2020-10-28 14:40:13 +01:00
|
|
|
/* Generate random bytes */
|
|
|
|
size_t slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
|
2020-10-21 19:29:37 +02:00
|
|
|
{
|
2020-10-28 21:58:24 +01:00
|
|
|
CALL_ONCE(&s_random_is_initialized, init_random_source);
|
2020-10-22 16:52:34 +02:00
|
|
|
#if defined(_WIN32)
|
2021-04-17 15:13:13 +02:00
|
|
|
if (s_rtlgenrandom)
|
2020-10-21 19:29:37 +02:00
|
|
|
{
|
2021-04-21 13:52:49 +02:00
|
|
|
const ULONG count = (ULONG) MIN_SIZE(length, ULONG_MAX);
|
|
|
|
return s_rtlgenrandom(buffer, count) ? count : 0U;
|
2020-10-13 15:04:59 +02:00
|
|
|
}
|
2021-04-21 13:52:49 +02:00
|
|
|
return 0U;
|
2020-10-13 17:42:22 +02:00
|
|
|
#else
|
2020-10-22 18:01:59 +02:00
|
|
|
if (s_random_fd >= 0)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
2020-10-28 14:40:13 +01:00
|
|
|
const ssize_t result = read(s_random_fd, buffer, length);
|
|
|
|
return (result < 0) ? 0U : ((size_t)result);
|
2020-10-13 15:04:59 +02:00
|
|
|
}
|
2021-04-21 13:52:49 +02:00
|
|
|
#if defined(GETENTROPY)
|
2021-04-17 16:05:32 +02:00
|
|
|
else
|
|
|
|
{
|
2021-04-21 13:52:49 +02:00
|
|
|
size_t offset, count;
|
|
|
|
for (offset = 0U; offset < length; offset += count)
|
|
|
|
{
|
|
|
|
count = MIN_SIZE(length - offset, 256U);
|
|
|
|
if (GETENTROPY(buffer + offset, count) < 0)
|
|
|
|
{
|
|
|
|
break; /*failed*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return offset;
|
2021-04-17 16:05:32 +02:00
|
|
|
}
|
2021-04-21 13:52:49 +02:00
|
|
|
#else
|
|
|
|
return 0U;
|
2021-04-17 16:05:32 +02:00
|
|
|
#endif
|
2020-10-13 17:42:22 +02:00
|
|
|
#endif
|
2020-10-13 15:04:59 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 14:40:13 +01:00
|
|
|
// ==========================================================================
|
|
|
|
// Zero memory
|
|
|
|
// ==========================================================================
|
|
|
|
|
2020-10-28 21:58:24 +01:00
|
|
|
void slunkcrypt_bzero(void* const buffer, const size_t length)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
2020-10-28 21:58:24 +01:00
|
|
|
if ((buffer) && (length > 0U))
|
2020-10-14 13:14:47 +02:00
|
|
|
{
|
2021-04-17 15:13:13 +02:00
|
|
|
#if defined(EXPLICIT_BZERO)
|
|
|
|
EXPLICIT_BZERO(buffer, length);
|
2020-10-13 19:33:01 +02:00
|
|
|
#else
|
2020-10-28 21:58:24 +01:00
|
|
|
volatile uint8_t* ptr = (volatile uint8_t*) buffer;
|
2020-10-14 13:14:47 +02:00
|
|
|
for (size_t i = 0U; i < length; ++i)
|
|
|
|
{
|
2020-10-28 21:58:24 +01:00
|
|
|
ptr[i] = 0U;
|
2020-10-14 13:14:47 +02:00
|
|
|
}
|
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
|
|
|
}
|
2020-10-28 21:58:24 +01:00
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// Destructor
|
|
|
|
// ==========================================================================
|
|
|
|
|
2021-04-17 15:13:13 +02:00
|
|
|
#if defined(ATTRIB_DESTRUCTOR)
|
|
|
|
ATTRIB_DESTRUCTOR void slunkcrypt_destructor()
|
2020-10-28 21:58:24 +01:00
|
|
|
{
|
|
|
|
exit_random_source();
|
|
|
|
}
|
|
|
|
#endif
|