修訂 | 207039f4f7b200cc0a91a8d378748eb9ad897293 (tree) |
---|---|
時間 | 2022-05-08 00:55:03 |
作者 | LoRd_MuldeR <mulder2@gmx....> |
Commiter | LoRd_MuldeR |
Small improvement to Makefile + fixed compilation with µClibc, which does *not* provide getentropy() or explicit_bzero() + enable these functions on Solaris/Illumos.
@@ -10,8 +10,6 @@ FLTO ?= 0 | ||
10 | 10 | FPGO ?= 0 |
11 | 11 | STRIP ?= 0 |
12 | 12 | CPU ?= 0 |
13 | -MARCH ?= 0 | |
14 | -MTUNE ?= 0 | |
15 | 13 | THREAD ?= 1 |
16 | 14 | |
17 | 15 | # --------------------------------------------------------------------------- |
@@ -29,18 +27,18 @@ CONFIG = | ||
29 | 27 | LDFLGS = -lpthread |
30 | 28 | CFLAGS = -I$(SUBDIR_LIB)/include -std=gnu99 -Wall |
31 | 29 | |
32 | -ifneq ($(CPU),0) | |
30 | +ifneq (,$(firstword $(filter 32 64,$(CPU)))) | |
33 | 31 | CFLAGS += -m$(firstword $(CPU)) |
34 | 32 | endif |
35 | -ifneq ($(TARGET),) | |
33 | +ifneq (,$(firstword $(TARGET))) | |
36 | 34 | CFLAGS += --target=$(firstword $(TARGET)) |
37 | 35 | LDFLGS += --target=$(firstword $(TARGET)) |
38 | 36 | endif |
39 | 37 | |
40 | -ifneq ($(MARCH),0) | |
38 | +ifneq (,$(firstword $(MARCH))) | |
41 | 39 | CFLAGS += -march=$(firstword $(MARCH)) |
42 | 40 | endif |
43 | -ifneq ($(MTUNE),0) | |
41 | +ifneq (,$(firstword $(MTUNE))) | |
44 | 42 | CFLAGS += -mtune=$(firstword $(MTUNE)) |
45 | 43 | endif |
46 | 44 |
@@ -65,7 +63,7 @@ endif | ||
65 | 63 | |
66 | 64 | MACHINE := $(shell $(CC) -dumpmachine || echo unknown) |
67 | 65 | |
68 | -ifeq ($(MACHINE),$(filter %mingw32 %-windows-gnu %-cygwin %-cygnus,$(MACHINE))) | |
66 | +ifneq (,$(firstword $(filter %mingw32 %-windows-gnu %-cygwin %-cygnus,$(MACHINE)))) | |
69 | 67 | SUFFIX := .exe |
70 | 68 | else |
71 | 69 | SUFFIX := |
@@ -83,7 +81,7 @@ ifeq ($(STATIC),1) | ||
83 | 81 | LDFLGS += -static |
84 | 82 | endif |
85 | 83 | |
86 | -ifeq ($(MACHINE),$(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE))) | |
84 | +ifneq (,$(firstword $(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE)))) | |
87 | 85 | LDFLGS += -mconsole -municode |
88 | 86 | endif |
89 | 87 |
@@ -40,24 +40,31 @@ static INLINE size_t MIN_SIZE(const size_t a, const size_t b) { return (a > b) ? | ||
40 | 40 | # include <pthread.h> |
41 | 41 | #endif |
42 | 42 | |
43 | -/* detect destructor support */ | |
44 | -#undef ATTRIB_DESTRUCTOR | |
43 | +/* detect compiler destructor support */ | |
44 | +#undef HAVE_ATTRIB_DESTRUCTOR | |
45 | 45 | #if defined(__GNUC__) || defined(__clang__) |
46 | -# define ATTRIB_DESTRUCTOR __attribute__((destructor)) | |
46 | +# define HAVE_ATTRIB_DESTRUCTOR 1 | |
47 | 47 | #endif |
48 | 48 | |
49 | -/* detect getentropy() support */ | |
50 | -#undef GETENTROPY | |
51 | -#if (defined(__linux__) && (__linux__ >= 1)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) || (defined(__OpenBSD__) && (__OpenBSD__ >= 1)) | |
52 | -# define GETENTROPY getentropy | |
49 | +/* detect getentropy() or RtlGenRandom() support */ | |
50 | +#undef HAVE_GETENTROPY | |
51 | +#undef HAVE_WIN32RTLGENRANDOM | |
52 | +#if defined(_WIN32) | |
53 | +# define HAVE_WIN32RTLGENRANDOM 1 | |
54 | +#elif (defined(__linux__) && !defined(__UCLIBC__)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) || defined(__OpenBSD__) || (defined(__sun) && defined(__SVR4)) | |
55 | +# define HAVE_GETENTROPY 1 | |
56 | +#else | |
57 | +# pragma message("Function getentropy() is *not* available -> using fallback!") | |
53 | 58 | #endif |
54 | 59 | |
55 | -/* detect explicit_bzero() support */ | |
60 | +/* detect explicit_bzero() or SecureZeroMemory() support */ | |
56 | 61 | #undef EXPLICIT_BZERO |
57 | -#if defined(_WIN32) && (_WIN32 >= 1) && defined(SecureZeroMemory) | |
62 | +#if defined(_WIN32) && defined(SecureZeroMemory) | |
58 | 63 | # define EXPLICIT_BZERO SecureZeroMemory |
59 | -#elif (defined(__linux__) && (__linux__ >= 1)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11)) || (defined(__OpenBSD__) && (__OpenBSD__ >= 1)) | |
64 | +#elif (defined(__linux__) && !defined(__UCLIBC__)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11)) || defined(__OpenBSD__) || (defined(__sun) && defined(__SVR4)) | |
60 | 65 | # define EXPLICIT_BZERO explicit_bzero |
66 | +#else | |
67 | +# pragma message("Function explicit_bzero() is *not* available -> using fallback!") | |
61 | 68 | #endif |
62 | 69 | |
63 | 70 | // ========================================================================== |
@@ -102,15 +109,15 @@ static void exit_random_source(void) | ||
102 | 109 | /* Initialize CSRNG */ |
103 | 110 | static void init_random_source(void) |
104 | 111 | { |
105 | -#ifdef _WIN32 | |
112 | +#ifdef HAVE_WIN32RTLGENRANDOM | |
106 | 113 | if ((s_advapi32 = LoadLibraryW(L"advapi32.dll"))) |
107 | 114 | { |
108 | 115 | s_genrandom = (ptr_genrandom_t) GetProcAddress(s_advapi32, "SystemFunction036"); |
109 | 116 | } |
110 | 117 | #else |
111 | -#if defined(GETENTROPY) | |
118 | +#if defined(HAVE_GETENTROPY) | |
112 | 119 | uint8_t temp; |
113 | - if (GETENTROPY(&temp, sizeof(uint8_t)) >= 0) | |
120 | + if (getentropy(&temp, sizeof(uint8_t)) >= 0) | |
114 | 121 | { |
115 | 122 | goto init_completed; |
116 | 123 | } |
@@ -124,7 +131,7 @@ static void init_random_source(void) | ||
124 | 131 | } |
125 | 132 | init_completed: ; |
126 | 133 | #endif |
127 | -#if !defined(ATTRIB_DESTRUCTOR) | |
134 | +#if !defined(HAVE_ATTRIB_DESTRUCTOR) | |
128 | 135 | atexit(exit_random_source); |
129 | 136 | #endif |
130 | 137 | } |
@@ -134,7 +141,7 @@ size_t slunkcrypt_random_bytes(uint8_t *const buffer, const size_t length) | ||
134 | 141 | { |
135 | 142 | size_t offset; |
136 | 143 | pthread_once(&s_random_is_initialized, init_random_source); |
137 | -#ifdef _WIN32 | |
144 | +#ifdef HAVE_WIN32RTLGENRANDOM | |
138 | 145 | if (s_genrandom) |
139 | 146 | { |
140 | 147 | ULONG count; |
@@ -162,14 +169,14 @@ size_t slunkcrypt_random_bytes(uint8_t *const buffer, const size_t length) | ||
162 | 169 | } |
163 | 170 | return offset; |
164 | 171 | } |
165 | -#if defined(GETENTROPY) | |
172 | +#if defined(HAVE_GETENTROPY) | |
166 | 173 | else |
167 | 174 | { |
168 | 175 | size_t count; |
169 | 176 | for (offset = 0U; offset < length; offset += count) |
170 | 177 | { |
171 | 178 | count = MIN_SIZE(length - offset, 256U); /*the maximum permitted value is 256*/ |
172 | - if (GETENTROPY(buffer + offset, count) < 0) | |
179 | + if (getentropy(buffer + offset, count) < 0) | |
173 | 180 | { |
174 | 181 | break; /*failed*/ |
175 | 182 | } |
@@ -206,8 +213,8 @@ void slunkcrypt_bzero(void* const buffer, const size_t length) | ||
206 | 213 | // Destructor |
207 | 214 | // ========================================================================== |
208 | 215 | |
209 | -#if defined(ATTRIB_DESTRUCTOR) | |
210 | -ATTRIB_DESTRUCTOR void slunkcrypt_destructor() | |
216 | +#if defined(HAVE_ATTRIB_DESTRUCTOR) | |
217 | +__attribute__((destructor)) void slunkcrypt_destructor() | |
211 | 218 | { |
212 | 219 | exit_random_source(); |
213 | 220 | } |