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
STRIP ?= 0
CPU ?= 0
MARCH ?= 0
MTUNE ?= 0
THREAD ?= 1
# ---------------------------------------------------------------------------
@ -29,18 +27,18 @@ CONFIG =
LDFLGS = -lpthread
CFLAGS = -I$(SUBDIR_LIB)/include -std=gnu99 -Wall
ifneq ($(CPU),0)
ifneq (,$(firstword $(filter 32 64,$(CPU))))
CFLAGS += -m$(firstword $(CPU))
endif
ifneq ($(TARGET),)
ifneq (,$(firstword $(TARGET)))
CFLAGS += --target=$(firstword $(TARGET))
LDFLGS += --target=$(firstword $(TARGET))
endif
ifneq ($(MARCH),0)
ifneq (,$(firstword $(MARCH)))
CFLAGS += -march=$(firstword $(MARCH))
endif
ifneq ($(MTUNE),0)
ifneq (,$(firstword $(MTUNE)))
CFLAGS += -mtune=$(firstword $(MTUNE))
endif
@ -65,7 +63,7 @@ endif
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
else
SUFFIX :=
@ -83,7 +81,7 @@ ifeq ($(STATIC),1)
LDFLGS += -static
endif
ifeq ($(MACHINE),$(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE)))
ifneq (,$(firstword $(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE))))
LDFLGS += -mconsole -municode
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>
#endif
/* detect destructor support */
#undef ATTRIB_DESTRUCTOR
/* detect compiler destructor support */
#undef HAVE_ATTRIB_DESTRUCTOR
#if defined(__GNUC__) || defined(__clang__)
# define ATTRIB_DESTRUCTOR __attribute__((destructor))
# define HAVE_ATTRIB_DESTRUCTOR 1
#endif
/* detect getentropy() support */
#undef GETENTROPY
#if (defined(__linux__) && (__linux__ >= 1)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) || (defined(__OpenBSD__) && (__OpenBSD__ >= 1))
# define GETENTROPY getentropy
/* detect getentropy() or RtlGenRandom() support */
#undef HAVE_GETENTROPY
#undef HAVE_WIN32RTLGENRANDOM
#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
/* detect explicit_bzero() support */
/* detect explicit_bzero() or SecureZeroMemory() support */
#undef EXPLICIT_BZERO
#if defined(_WIN32) && (_WIN32 >= 1) && defined(SecureZeroMemory)
#if defined(_WIN32) && defined(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
#else
# pragma message("Function explicit_bzero() is *not* available -> using fallback!")
#endif
// ==========================================================================
@ -102,15 +109,15 @@ static void exit_random_source(void)
/* Initialize CSRNG */
static void init_random_source(void)
{
#ifdef _WIN32
#ifdef HAVE_WIN32RTLGENRANDOM
if ((s_advapi32 = LoadLibraryW(L"advapi32.dll")))
{
s_genrandom = (ptr_genrandom_t) GetProcAddress(s_advapi32, "SystemFunction036");
}
#else
#if defined(GETENTROPY)
#if defined(HAVE_GETENTROPY)
uint8_t temp;
if (GETENTROPY(&temp, sizeof(uint8_t)) >= 0)
if (getentropy(&temp, sizeof(uint8_t)) >= 0)
{
goto init_completed;
}
@ -124,7 +131,7 @@ static void init_random_source(void)
}
init_completed: ;
#endif
#if !defined(ATTRIB_DESTRUCTOR)
#if !defined(HAVE_ATTRIB_DESTRUCTOR)
atexit(exit_random_source);
#endif
}
@ -134,7 +141,7 @@ size_t slunkcrypt_random_bytes(uint8_t *const buffer, const size_t length)
{
size_t offset;
pthread_once(&s_random_is_initialized, init_random_source);
#ifdef _WIN32
#ifdef HAVE_WIN32RTLGENRANDOM
if (s_genrandom)
{
ULONG count;
@ -162,14 +169,14 @@ size_t slunkcrypt_random_bytes(uint8_t *const buffer, const size_t length)
}
return offset;
}
#if defined(GETENTROPY)
#if defined(HAVE_GETENTROPY)
else
{
size_t count;
for (offset = 0U; offset < length; offset += count)
{
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*/
}
@ -206,8 +213,8 @@ void slunkcrypt_bzero(void* const buffer, const size_t length)
// Destructor
// ==========================================================================
#if defined(ATTRIB_DESTRUCTOR)
ATTRIB_DESTRUCTOR void slunkcrypt_destructor()
#if defined(HAVE_ATTRIB_DESTRUCTOR)
__attribute__((destructor)) void slunkcrypt_destructor()
{
exit_random_source();
}