Small improvement to Makefile + fixed compilation with µClibc, which does *not* provide getentropy() or explicit_bzero() + enable these functions on Solaris/Illumos.

This commit is contained in:
LoRd_MuldeR 2022-05-06 00:33:45 +02:00
parent 69e549aafd
commit 207039f4f7
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
2 changed files with 32 additions and 27 deletions

View File

@ -10,8 +10,6 @@ FLTO ?= 0
FPGO ?= 0 FPGO ?= 0
STRIP ?= 0 STRIP ?= 0
CPU ?= 0 CPU ?= 0
MARCH ?= 0
MTUNE ?= 0
THREAD ?= 1 THREAD ?= 1
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@ -29,18 +27,18 @@ CONFIG =
LDFLGS = -lpthread LDFLGS = -lpthread
CFLAGS = -I$(SUBDIR_LIB)/include -std=gnu99 -Wall CFLAGS = -I$(SUBDIR_LIB)/include -std=gnu99 -Wall
ifneq ($(CPU),0) ifneq (,$(firstword $(filter 32 64,$(CPU))))
CFLAGS += -m$(firstword $(CPU)) CFLAGS += -m$(firstword $(CPU))
endif endif
ifneq ($(TARGET),) ifneq (,$(firstword $(TARGET)))
CFLAGS += --target=$(firstword $(TARGET)) CFLAGS += --target=$(firstword $(TARGET))
LDFLGS += --target=$(firstword $(TARGET)) LDFLGS += --target=$(firstword $(TARGET))
endif endif
ifneq ($(MARCH),0) ifneq (,$(firstword $(MARCH)))
CFLAGS += -march=$(firstword $(MARCH)) CFLAGS += -march=$(firstword $(MARCH))
endif endif
ifneq ($(MTUNE),0) ifneq (,$(firstword $(MTUNE)))
CFLAGS += -mtune=$(firstword $(MTUNE)) CFLAGS += -mtune=$(firstword $(MTUNE))
endif endif
@ -65,7 +63,7 @@ endif
MACHINE := $(shell $(CC) -dumpmachine || echo unknown) MACHINE := $(shell $(CC) -dumpmachine || echo unknown)
ifeq ($(MACHINE),$(filter %mingw32 %-windows-gnu %-cygwin %-cygnus,$(MACHINE))) ifneq (,$(firstword $(filter %mingw32 %-windows-gnu %-cygwin %-cygnus,$(MACHINE))))
SUFFIX := .exe SUFFIX := .exe
else else
SUFFIX := SUFFIX :=
@ -83,7 +81,7 @@ ifeq ($(STATIC),1)
LDFLGS += -static LDFLGS += -static
endif endif
ifeq ($(MACHINE),$(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE))) ifneq (,$(firstword $(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE))))
LDFLGS += -mconsole -municode LDFLGS += -mconsole -municode
endif endif

View File

@ -40,24 +40,31 @@ static INLINE size_t MIN_SIZE(const size_t a, const size_t b) { return (a > b) ?
# include <pthread.h> # include <pthread.h>
#endif #endif
/* detect destructor support */ /* detect compiler destructor support */
#undef ATTRIB_DESTRUCTOR #undef HAVE_ATTRIB_DESTRUCTOR
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
# define ATTRIB_DESTRUCTOR __attribute__((destructor)) # define HAVE_ATTRIB_DESTRUCTOR 1
#endif #endif
/* detect getentropy() support */ /* detect getentropy() or RtlGenRandom() support */
#undef GETENTROPY #undef HAVE_GETENTROPY
#if (defined(__linux__) && (__linux__ >= 1)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) || (defined(__OpenBSD__) && (__OpenBSD__ >= 1)) #undef HAVE_WIN32RTLGENRANDOM
# define GETENTROPY getentropy #if defined(_WIN32)
# define HAVE_WIN32RTLGENRANDOM 1
#elif (defined(__linux__) && !defined(__UCLIBC__)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) || defined(__OpenBSD__) || (defined(__sun) && defined(__SVR4))
# define HAVE_GETENTROPY 1
#else
# pragma message("Function getentropy() is *not* available -> using fallback!")
#endif #endif
/* detect explicit_bzero() support */ /* detect explicit_bzero() or SecureZeroMemory() support */
#undef EXPLICIT_BZERO #undef EXPLICIT_BZERO
#if defined(_WIN32) && (_WIN32 >= 1) && defined(SecureZeroMemory) #if defined(_WIN32) && defined(SecureZeroMemory)
# define EXPLICIT_BZERO SecureZeroMemory # define EXPLICIT_BZERO SecureZeroMemory
#elif (defined(__linux__) && (__linux__ >= 1)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11)) || (defined(__OpenBSD__) && (__OpenBSD__ >= 1)) #elif (defined(__linux__) && !defined(__UCLIBC__)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11)) || defined(__OpenBSD__) || (defined(__sun) && defined(__SVR4))
# define EXPLICIT_BZERO explicit_bzero # define EXPLICIT_BZERO explicit_bzero
#else
# pragma message("Function explicit_bzero() is *not* available -> using fallback!")
#endif #endif
// ========================================================================== // ==========================================================================
@ -102,15 +109,15 @@ static void exit_random_source(void)
/* Initialize CSRNG */ /* Initialize CSRNG */
static void init_random_source(void) static void init_random_source(void)
{ {
#ifdef _WIN32 #ifdef HAVE_WIN32RTLGENRANDOM
if ((s_advapi32 = LoadLibraryW(L"advapi32.dll"))) if ((s_advapi32 = LoadLibraryW(L"advapi32.dll")))
{ {
s_genrandom = (ptr_genrandom_t) GetProcAddress(s_advapi32, "SystemFunction036"); s_genrandom = (ptr_genrandom_t) GetProcAddress(s_advapi32, "SystemFunction036");
} }
#else #else
#if defined(GETENTROPY) #if defined(HAVE_GETENTROPY)
uint8_t temp; uint8_t temp;
if (GETENTROPY(&temp, sizeof(uint8_t)) >= 0) if (getentropy(&temp, sizeof(uint8_t)) >= 0)
{ {
goto init_completed; goto init_completed;
} }
@ -124,7 +131,7 @@ static void init_random_source(void)
} }
init_completed: ; init_completed: ;
#endif #endif
#if !defined(ATTRIB_DESTRUCTOR) #if !defined(HAVE_ATTRIB_DESTRUCTOR)
atexit(exit_random_source); atexit(exit_random_source);
#endif #endif
} }
@ -134,7 +141,7 @@ size_t slunkcrypt_random_bytes(uint8_t *const buffer, const size_t length)
{ {
size_t offset; size_t offset;
pthread_once(&s_random_is_initialized, init_random_source); pthread_once(&s_random_is_initialized, init_random_source);
#ifdef _WIN32 #ifdef HAVE_WIN32RTLGENRANDOM
if (s_genrandom) if (s_genrandom)
{ {
ULONG count; ULONG count;
@ -162,14 +169,14 @@ size_t slunkcrypt_random_bytes(uint8_t *const buffer, const size_t length)
} }
return offset; return offset;
} }
#if defined(GETENTROPY) #if defined(HAVE_GETENTROPY)
else else
{ {
size_t count; size_t count;
for (offset = 0U; offset < length; offset += count) for (offset = 0U; offset < length; offset += count)
{ {
count = MIN_SIZE(length - offset, 256U); /*the maximum permitted value is 256*/ count = MIN_SIZE(length - offset, 256U); /*the maximum permitted value is 256*/
if (GETENTROPY(buffer + offset, count) < 0) if (getentropy(buffer + offset, count) < 0)
{ {
break; /*failed*/ break; /*failed*/
} }
@ -206,8 +213,8 @@ void slunkcrypt_bzero(void* const buffer, const size_t length)
// Destructor // Destructor
// ========================================================================== // ==========================================================================
#if defined(ATTRIB_DESTRUCTOR) #if defined(HAVE_ATTRIB_DESTRUCTOR)
ATTRIB_DESTRUCTOR void slunkcrypt_destructor() __attribute__((destructor)) void slunkcrypt_destructor()
{ {
exit_random_source(); exit_random_source();
} }