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
|
2022-03-23 22:33:45 +01:00
|
|
|
# define WIN32_LEAN_AND_MEAN 1
|
|
|
|
# define _CRT_SECURE_NO_WARNINGS 1
|
2020-11-04 23:17:59 +01:00
|
|
|
#else
|
2022-03-23 22:33:45 +01:00
|
|
|
# define _GNU_SOURCE 1
|
2020-11-04 23:17:59 +01:00
|
|
|
#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
|
2022-03-23 22:33:45 +01:00
|
|
|
# include <Windows.h>
|
|
|
|
# include <io.h>
|
2020-10-21 19:29:37 +02:00
|
|
|
#else
|
2022-03-23 22:33:45 +01:00
|
|
|
# include <unistd.h>
|
2022-09-21 23:42:10 +02:00
|
|
|
# include <sched.h>
|
2022-09-23 00:10:07 +02:00
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
# include <sys/random.h>
|
2020-10-13 15:37:40 +02:00
|
|
|
#endif
|
|
|
|
|
2022-05-06 00:33:45 +02:00
|
|
|
/* detect compiler destructor support */
|
|
|
|
#undef HAVE_ATTRIB_DESTRUCTOR
|
2020-10-28 16:21:01 +01:00
|
|
|
#if defined(__GNUC__) || defined(__clang__)
|
2022-05-06 00:33:45 +02:00
|
|
|
# define HAVE_ATTRIB_DESTRUCTOR 1
|
2021-04-17 15:13:13 +02:00
|
|
|
#endif
|
|
|
|
|
2022-05-06 00:33:45 +02:00
|
|
|
/* detect getentropy() or RtlGenRandom() support */
|
|
|
|
#undef HAVE_GETENTROPY
|
|
|
|
#undef HAVE_WIN32RTLGENRANDOM
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# define HAVE_WIN32RTLGENRANDOM 1
|
2022-09-24 16:20:35 +02:00
|
|
|
#elif (defined(__linux__) && !defined(__UCLIBC__)) || defined(__CYGWIN__) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) || defined(__DragonFly__) || defined(__OpenBSD__) || (defined(__sun) && defined(__SVR4)) || (defined(__APPLE__) && defined(__MACH__))
|
2022-05-06 00:33:45 +02:00
|
|
|
# define HAVE_GETENTROPY 1
|
|
|
|
#else
|
|
|
|
# pragma message("Function getentropy() is *not* available -> using fallback!")
|
2021-04-17 15:13:13 +02:00
|
|
|
#endif
|
|
|
|
|
2022-05-06 00:33:45 +02:00
|
|
|
/* detect explicit_bzero() or SecureZeroMemory() support */
|
2021-04-17 15:13:13 +02:00
|
|
|
#undef EXPLICIT_BZERO
|
2022-05-06 00:33:45 +02:00
|
|
|
#if defined(_WIN32) && defined(SecureZeroMemory)
|
2022-03-23 22:33:45 +01:00
|
|
|
# define EXPLICIT_BZERO SecureZeroMemory
|
2022-09-24 16:20:35 +02:00
|
|
|
#elif (defined(__linux__) && !defined(__UCLIBC__)) || defined(__CYGWIN__) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11)) || defined(__DragonFly__) || defined(__OpenBSD__) || (defined(__sun) && defined(__SVR4)) || defined(__HAIKU__)
|
2022-03-23 22:33:45 +01:00
|
|
|
# define EXPLICIT_BZERO explicit_bzero
|
2022-05-06 00:33:45 +02:00
|
|
|
#else
|
|
|
|
# pragma message("Function explicit_bzero() is *not* available -> using fallback!")
|
2020-10-28 14:40:13 +01:00
|
|
|
#endif
|
2020-10-26 19:56:45 +01:00
|
|
|
|
2022-09-21 23:42:10 +02:00
|
|
|
/* detect sched_yield() or Sleep() support */
|
|
|
|
#ifdef _WIN32
|
2022-09-23 00:10:07 +02:00
|
|
|
# define THREAD_YIELD() Sleep(0)
|
2022-09-21 23:42:10 +02:00
|
|
|
#else
|
2022-09-23 00:10:07 +02:00
|
|
|
# define THREAD_YIELD() sched_yield()
|
2022-09-21 23:42:10 +02:00
|
|
|
#endif
|
|
|
|
|
2022-09-20 23:58:45 +02:00
|
|
|
// ==========================================================================
|
|
|
|
// One-time initialization
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
/* atomic memory access */
|
2022-09-21 23:42:10 +02:00
|
|
|
#if defined(_MSC_VER) && (!defined(__GNUC__))
|
2022-09-23 00:10:07 +02:00
|
|
|
# define ONCE_FLAG_T LONG
|
2022-09-20 23:58:45 +02:00
|
|
|
# define ATOMIC_COMPARE_EXCHANGE(X,Y,Z) InterlockedCompareExchange((X),(Y),(Z))
|
2022-09-21 23:42:10 +02:00
|
|
|
# define ATOMIC_EXCHANGE(X,Y) InterlockedExchange((X),(Y))
|
2022-09-20 23:58:45 +02:00
|
|
|
#else
|
2022-09-23 00:10:07 +02:00
|
|
|
# define ONCE_FLAG_T int
|
2022-09-20 23:58:45 +02:00
|
|
|
# define ATOMIC_COMPARE_EXCHANGE(X,Y,Z) __sync_val_compare_and_swap((X),(Z),(Y))
|
2022-09-21 23:42:10 +02:00
|
|
|
# define ATOMIC_EXCHANGE(X,Y) __sync_lock_test_and_set((X),(Y))
|
2022-09-20 23:58:45 +02:00
|
|
|
#endif
|
|
|
|
|
2022-09-23 00:10:07 +02:00
|
|
|
/* execute init routine once */
|
|
|
|
static INLINE void initialize_once(volatile ONCE_FLAG_T *const once_control, void (*const init_routine)(void))
|
2022-09-20 23:58:45 +02:00
|
|
|
{
|
2022-09-23 00:10:07 +02:00
|
|
|
ONCE_FLAG_T state;
|
|
|
|
while ((state = ATOMIC_COMPARE_EXCHANGE(once_control, -1, 0)) != 0)
|
2022-09-20 23:58:45 +02:00
|
|
|
{
|
|
|
|
if (state > 0)
|
|
|
|
{
|
|
|
|
return; /*already initialized*/
|
|
|
|
}
|
2022-09-23 00:10:07 +02:00
|
|
|
THREAD_YIELD();
|
2022-09-20 23:58:45 +02:00
|
|
|
}
|
|
|
|
init_routine();
|
2022-09-23 00:10:07 +02:00
|
|
|
ATOMIC_EXCHANGE(once_control, 1);
|
2022-09-20 23:58:45 +02: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
|
|
|
// ==========================================================================
|
|
|
|
|
2022-02-08 22:46:33 +01:00
|
|
|
#define MAX_COUNT 1048576U
|
|
|
|
|
2020-10-28 14:40:13 +01:00
|
|
|
/* Global state */
|
2022-09-23 00:10:07 +02:00
|
|
|
static volatile ONCE_FLAG_T s_random_is_initialized = 0;
|
2020-10-22 16:52:34 +02:00
|
|
|
#if defined(_WIN32)
|
2022-02-08 22:46:33 +01:00
|
|
|
typedef BOOLEAN(WINAPI *ptr_genrandom_t)(void *buffer, ULONG length);
|
2021-04-17 15:13:13 +02:00
|
|
|
static HMODULE s_advapi32 = NULL;
|
2022-02-08 22:46:33 +01:00
|
|
|
static ptr_genrandom_t s_genrandom = 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
|
|
|
{
|
2022-02-08 22:46:33 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (s_genrandom)
|
|
|
|
{
|
|
|
|
s_genrandom = NULL;
|
|
|
|
}
|
2021-04-17 15:13:13 +02:00
|
|
|
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
|
|
|
{
|
2022-05-06 00:33:45 +02:00
|
|
|
#ifdef HAVE_WIN32RTLGENRANDOM
|
2021-04-17 15:13:13 +02:00
|
|
|
if ((s_advapi32 = LoadLibraryW(L"advapi32.dll")))
|
2020-10-28 14:40:13 +01:00
|
|
|
{
|
2022-02-08 22:46:33 +01:00
|
|
|
s_genrandom = (ptr_genrandom_t) GetProcAddress(s_advapi32, "SystemFunction036");
|
2020-10-28 14:40:13 +01:00
|
|
|
}
|
2021-04-17 16:05:32 +02:00
|
|
|
#else
|
2022-05-06 00:33:45 +02:00
|
|
|
#if defined(HAVE_GETENTROPY)
|
2021-04-17 16:05:32 +02:00
|
|
|
uint8_t temp;
|
2022-05-06 00:33:45 +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
|
2022-05-06 00:33:45 +02:00
|
|
|
#if !defined(HAVE_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 */
|
2022-03-23 22:33:45 +01:00
|
|
|
size_t slunkcrypt_random_bytes(uint8_t *const buffer, const size_t length)
|
2020-10-21 19:29:37 +02:00
|
|
|
{
|
2022-02-08 22:46:33 +01:00
|
|
|
size_t offset;
|
2022-09-20 23:58:45 +02:00
|
|
|
initialize_once(&s_random_is_initialized, init_random_source);
|
2022-05-06 00:33:45 +02:00
|
|
|
#ifdef HAVE_WIN32RTLGENRANDOM
|
2022-02-08 22:46:33 +01:00
|
|
|
if (s_genrandom)
|
2020-10-21 19:29:37 +02:00
|
|
|
{
|
2022-02-08 22:46:33 +01:00
|
|
|
ULONG count;
|
|
|
|
for (offset = 0U; offset < length; offset += count)
|
|
|
|
{
|
|
|
|
count = (ULONG) MIN_SIZE(length - offset, MAX_COUNT);
|
|
|
|
if (!s_genrandom(buffer + offset, count))
|
|
|
|
{
|
|
|
|
break; /*failed*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return offset;
|
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
|
|
|
{
|
2022-02-08 22:46:33 +01:00
|
|
|
ssize_t count;
|
|
|
|
for (offset = 0; offset < length; offset += (size_t)count)
|
|
|
|
{
|
|
|
|
if (!((count = read(s_random_fd, buffer + offset, MIN_SIZE(length - offset, MAX_COUNT))) > 0))
|
|
|
|
{
|
|
|
|
break; /*failed*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return offset;
|
2020-10-13 15:04:59 +02:00
|
|
|
}
|
2022-05-06 00:33:45 +02:00
|
|
|
#if defined(HAVE_GETENTROPY)
|
2021-04-17 16:05:32 +02:00
|
|
|
else
|
|
|
|
{
|
2022-02-08 22:46:33 +01:00
|
|
|
size_t count;
|
2021-04-21 13:52:49 +02:00
|
|
|
for (offset = 0U; offset < length; offset += count)
|
|
|
|
{
|
2022-02-08 22:46:33 +01:00
|
|
|
count = MIN_SIZE(length - offset, 256U); /*the maximum permitted value is 256*/
|
2022-05-06 00:33:45 +02:00
|
|
|
if (getentropy(buffer + offset, count) < 0)
|
2021-04-21 13:52:49 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
// ==========================================================================
|
|
|
|
|
2022-05-06 00:33:45 +02:00
|
|
|
#if defined(HAVE_ATTRIB_DESTRUCTOR)
|
2022-09-21 23:42:10 +02:00
|
|
|
__attribute__((destructor)) void slunkcrypt_destructor(void)
|
2020-10-28 21:58:24 +01:00
|
|
|
{
|
|
|
|
exit_random_source();
|
|
|
|
}
|
|
|
|
#endif
|