SlunkCrypt/libslunkcrypt/src/junk.c

179 lines
4.3 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! */
/******************************************************************************/
2020-10-26 19:56:45 +01:00
/* Internal */
#include "../include/slunkcrypt.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>
/* Platform compatibility */
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>
#else
2020-10-21 21:58:46 +02:00
# include <unistd.h>
# include <pthread.h>
2020-10-13 15:37:40 +02:00
#endif
2020-10-26 19:56:45 +01:00
// ==========================================================================
// One-time init
2020-10-26 19:56:45 +01:00
// ==========================================================================
#ifdef _WIN32
# define ONCE_TYPE volatile LONG
# define ONCE_INIT 0L
# define ONCE_FUNC win32_run_once
#else
# define ONCE_TYPE pthread_once_t
# define ONCE_INIT PTHREAD_ONCE_INIT
# define ONCE_FUNC pthread_once
#endif
#ifdef _WIN32
static void win32_run_once(ONCE_TYPE *const control, void (*init_routine)(void))
2020-10-26 19:56:45 +01:00
{
LONG status;
while ((status = InterlockedCompareExchange(control, -1L, 0L)) != 0L)
2020-10-26 19:56:45 +01:00
{
if(status > 0L)
{
return; /*already initialized*/
}
SwitchToThread();
2020-10-26 19:56:45 +01:00
}
init_routine();
InterlockedExchange(control, 1L);
2020-10-26 19:56:45 +01:00
}
#endif
2020-10-26 19:56:45 +01:00
// ==========================================================================
// Random bytes
// ==========================================================================
#ifdef _WIN32
# define HAVE_GETRANDOM 0
#else
# if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
# define HAVE_GETRANDOM 1
# elif defined(__FreeBSD__) && (__FreeBSD__ >= 12)
# define HAVE_GETRANDOM 1
# else
# define HAVE_GETRANDOM 0
# endif
#endif
#if HAVE_GETRANDOM
# include <sys/random.h>
#endif
2020-10-26 19:56:45 +01:00
/* Global state */
static ONCE_TYPE s_random_is_initialized = ONCE_INIT;
#if defined(_WIN32)
typedef BOOLEAN(WINAPI *rtl_genrandom_t)(void *buffer, ULONG buff_size);
static HMODULE s_dll_advapi32 = NULL;
static rtl_genrandom_t s_rtl_genrandom = NULL;
#elif !HAVE_GETRANDOM
static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL };
static int s_random_fd = -1;
#endif
/* Close down CSRNG */
static void exit_random_source(void)
{
2020-10-26 19:56:45 +01:00
#if defined(_WIN32)
s_rtl_genrandom = NULL;
if (s_dll_advapi32)
{
FreeLibrary(s_dll_advapi32);
s_dll_advapi32 = NULL;
}
#elif !HAVE_GETRANDOM
if (s_random_fd >= 0)
{
close(s_random_fd);
s_random_fd = -1;
2020-10-26 19:56:45 +01:00
}
#endif
}
/* Initialize CSRNG */
static void init_random_source(void)
{
2020-10-26 19:56:45 +01:00
#if defined(_WIN32)
if ((s_dll_advapi32 = LoadLibraryW(L"advapi32.dll")))
{
s_rtl_genrandom = (rtl_genrandom_t)GetProcAddress(s_dll_advapi32, "SystemFunction036");
}
#elif !HAVE_GETRANDOM
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
{
break; /*success*/
2020-10-26 19:56:45 +01:00
}
}
#endif
atexit(exit_random_source);
}
/* Generate random bytes */
size_t slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
{
ONCE_FUNC(&s_random_is_initialized, init_random_source);
#if defined(_WIN32)
if (s_rtl_genrandom)
{
const ULONG buff_size = (ULONG)length;
return s_rtl_genrandom(buffer, buff_size) ? buff_size : 0U;
2020-10-13 15:04:59 +02:00
}
#elif HAVE_GETRANDOM
const ssize_t result = getrandom(buffer, length, 0U);
return (result < 0) ? 0U : ((size_t)result);
#else
if (s_random_fd >= 0)
2020-10-13 15:04:59 +02: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
}
#endif
return 0U;
2020-10-13 15:04:59 +02:00
}
// ==========================================================================
// Zero memory
// ==========================================================================
#ifdef _WIN32
# define HAVE_EXPLICIT_BZERO 0
#else
# if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
# define HAVE_EXPLICIT_BZERO 1
# elif defined(__FreeBSD__) && (__FreeBSD__ >= 11)
# define HAVE_EXPLICIT_BZERO 1
# else
# define HAVE_EXPLICIT_BZERO 0
# endif
#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);
#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
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
}