diff --git a/Makefile b/Makefile index 20ee405..91ae927 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,9 @@ # Options # --------------------------------------------------------------------------- -DEBUG ?= 0 +DEBUG ?= 0 +STATIC ?= 0 + MARCH ?= native MTUNE ?= native @@ -26,7 +28,7 @@ ifeq ($(DEBUG),1) else CONFIG := CFLAGS += -O3 -DNDEBUG - LDFLGS += -static -s + LDFLGS := -s endif MACHINE := $(shell $(CC) -dumpmachine) @@ -37,6 +39,10 @@ else SUFFIX := endif +ifeq ($(STATIC),1) + LDFLGS += -static +endif + ifneq ($(filter %-w64-mingw32,$(MACHINE)),) LDFLGS += -mconsole -municode endif diff --git a/frontend/src/platform.h b/frontend/src/platform.h index 3a63fe3..09908b1 100644 --- a/frontend/src/platform.h +++ b/frontend/src/platform.h @@ -10,92 +10,84 @@ #include #include #ifdef _WIN32 -#include -#ifndef _SH_SECURE -#define _SH_SECURE 0x80 -#endif +# include +# ifndef _SH_SECURE +# define _SH_SECURE 0x80 +# endif #endif -#ifdef _WIN32 -#define OS_TYPE "Win" +#if defined(_WIN32) +# define OS_TYPE "Windows" +#elif defined(__CYGWIN__) +# define OS_TYPE "Cygwin" +#elif defined(__linux__) +# define OS_TYPE "Linux" +#elif defined(__FreeBSD__) +# define OS_TYPE "FreeBSD" +#elif defined(__NetBSD__) +# define OS_TYPE "NetBSD" +#elif defined(__OpenBSD__) +# define OS_TYPE "OpenBSD" +#elif defined(__sun) +# define OS_TYPE "Solaris" +#elif defined(__unix__) +# define OS_TYPE "Unix" #else -#ifdef __CYGWIN__ -#define OS_TYPE "Cygwin" -#else -#ifdef __linux__ -#define OS_TYPE "Linux" -#else -#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) -#define OS_TYPE "BSD" -#else -#if defined(__APPLE__) && defined(__MACH__) -#define OS_TYPE "macOS" -#else -#error Unknown operating system! -#endif -#endif -#endif -#endif +# error Unknown operating system! #endif #if defined(__x86_64__) || defined(_M_X64) -#define CPU_ARCH "x64" +# define CPU_ARCH "x64" +#elif defined(__i386__) || defined(_M_IX86) +# define CPU_ARCH "x86" +#elif defined(__aarch64__) || defined(_M_ARM64) +# define CPU_ARCH "arm64" +#elif defined(__arm__) || defined(_M_ARM) +# define CPU_ARCH "arm" #else -#if defined(__i386__) || defined(_M_IX86) -#define CPU_ARCH "x86" -#else -#if defined(__aarch64__) || defined(_M_ARM64) -#define CPU_ARCH "arm64" -#else -#if defined(__arm__) || defined(_M_ARM) -#define CPU_ARCH "arm" -#else -#error Unknown CPU architecture! -#endif -#endif -#endif +# error Unknown CPU architecture! #endif #ifdef _WIN32 -#define MAIN wmain -#define CHR wchar_t -#define _T(X) L##X -#define GETENV(X) _wgetenv((X)) -#define STRLEN(X) wcslen((X)) -#define STRICMP(X,Y) _wcsicmp((X),(Y)) -#define STRRCHR(X,Y) wcsrchr((X),(Y)) -#define FPUTS(X,Y) fputws((X),(Y)) -#define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__) -#define FOPEN(X,Y) _wfsopen((X),(Y),_SH_SECURE) -#ifdef __USE_MINGW_ANSI_STDIO -#define PRISTR "ls" -#define PRIstr "hs" -#define PRIwcs "ls" +# define MAIN wmain +# define CHR wchar_t +# define _T(X) L##X +# define GETENV(X) _wgetenv((X)) +# define STRLEN(X) wcslen((X)) +# define STRICMP(X,Y) _wcsicmp((X),(Y)) +# define STRRCHR(X,Y) wcsrchr((X),(Y)) +# define FPUTS(X,Y) fputws((X),(Y)) +# define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__) +# define FOPEN(X,Y) _wfsopen((X),(Y),_SH_SECURE) +# ifdef __USE_MINGW_ANSI_STDIO +# define PRISTR "ls" +# define PRIstr "hs" +# define PRIwcs "ls" +# else +# define PRISTR "s" +# define PRIstr "S" +# define PRIwcs "s" +# endif #else -#define PRISTR "s" -#define PRIstr "S" -#define PRIwcs "s" -#endif -#else -#define MAIN main -#define CHR char -#define _T(X) X -#define GETENV(X) getenv((X)) -#define STRLEN(X) strlen((X)) -#define STRICMP(X,Y) strcasecmp((X),(Y)) -#define STRRCHR(X,Y) strrchr((X),(Y)) -#define FPUTS(X,Y) fputs((X),(Y)) -#define FPRINTF(X,Y,...) fprintf((X),(Y),__VA_ARGS__) -#define FOPEN(X,Y) fopen((X),(Y)) -#define PRISTR "s" -#define PRIstr "s" -#define PRIwcs "ls" +# define MAIN main +# define CHR char +# define _T(X) X +# define GETENV(X) getenv((X)) +# define STRLEN(X) strlen((X)) +# define STRICMP(X,Y) strcasecmp((X),(Y)) +# define STRRCHR(X,Y) strrchr((X),(Y)) +# define FPUTS(X,Y) fputs((X),(Y)) +# define FPRINTF(X,Y,...) fprintf((X),(Y),__VA_ARGS__) +# define FOPEN(X,Y) fopen((X),(Y)) +# define PRISTR "s" +# define PRIstr "s" +# define PRIwcs "ls" #endif #define T(X) _T(X) #ifdef _MSC_VER -#define strdup(X) _strdup((X)) +# define strdup(X) _strdup((X)) #endif #endif diff --git a/libslunkcrypt/src/internal.c b/libslunkcrypt/src/internal.c index acc949c..d967632 100644 --- a/libslunkcrypt/src/internal.c +++ b/libslunkcrypt/src/internal.c @@ -6,44 +6,42 @@ #include #ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN 1 -#include -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; +# define WIN32_LEAN_AND_MEAN 1 +# include #else -#include -#include -#include -#if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) -#include -#else -static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL }; -#endif +# include +# include +# include +# if (defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) +# include +# endif #endif #ifdef _WIN32 -static genrandom_t init_genrandom() +typedef BOOLEAN(WINAPI *genrandom_t)(void*, ULONG); +static genrandom_t win32_init_genrandom() { + static volatile LONG s_random_init = 0L; + static HMODULE s_advapi32 = NULL; + static genrandom_t s_genrandom = NULL; LONG state; - while ((state = InterlockedCompareExchange(&g_random_init, -1L, 0L)) != 0L) + while ((state = InterlockedCompareExchange(&s_random_init, -1L, 0L)) != 0L) { if (state > 0L) { - return g_genrandom; + return s_genrandom; } Sleep(0U); } - if (g_advapi32 || (g_advapi32 = LoadLibrary(L"advapi32.dll"))) + if (s_advapi32 || (s_advapi32 = LoadLibraryW(L"advapi32.dll"))) { - if ((g_genrandom = (genrandom_t)GetProcAddress(g_advapi32, "SystemFunction036"))) + if ((s_genrandom = (genrandom_t)GetProcAddress(s_advapi32, "SystemFunction036"))) { - InterlockedExchange(&g_random_init, 1L); - return g_genrandom; + InterlockedExchange(&s_random_init, 1L); + return s_genrandom; } } - InterlockedExchange(&g_random_init, 0L); + InterlockedExchange(&s_random_init, 0L); return NULL; } #endif @@ -53,21 +51,21 @@ 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(); + const genrandom_t genrandom = win32_init_genrandom(); if (genrandom) { return genrandom(buffer, (ULONG)length) ? 0 : (-1); } } return -1; -#else -#if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) +#elif (defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) if (getrandom(buffer, length, 0U) >= length) { return 0; } return -1; #else + static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL }; int result = -1; for (size_t i = 0U; DEV_RANDOM[i] && (result != 0); ++i) { @@ -83,7 +81,6 @@ int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length) } return result; #endif -#endif } void slunkcrypt_bzero(void* const ptr, const size_t length) @@ -92,8 +89,7 @@ void slunkcrypt_bzero(void* const ptr, const size_t length) { #if defined(_WIN32) && defined(SecureZeroMemory) SecureZeroMemory(ptr, length); -#else -#if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11)) +#elif (defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11)) explicit_bzero(ptr, length); #else volatile uint8_t *buffer = (volatile uint8_t*)ptr; @@ -101,7 +97,6 @@ void slunkcrypt_bzero(void* const ptr, const size_t length) { buffer[i] = 0U; } -#endif #endif } }