Some code clean-up.

This commit is contained in:
LoRd_MuldeR 2020-10-28 21:58:24 +01:00
parent 9c08cb9dd4
commit 6c26203c30
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
2 changed files with 41 additions and 27 deletions

View File

@ -105,7 +105,7 @@ SLUNKCRYPT_API int slunkcrypt_decrypt_inplace(const slunkcrypt_t context, uint8_
* Auxiliary functions
*/
SLUNKCRYPT_API size_t slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length);
SLUNKCRYPT_API void slunkcrypt_bzero(void* const ptr, const size_t length);
SLUNKCRYPT_API void slunkcrypt_bzero(void* const buffer, const size_t length);
#ifdef __cplusplus
}

View File

@ -22,29 +22,27 @@
/* Compiler compatibility */
#if defined(__GNUC__) || defined(__clang__)
# define AT_EXIT(X) ((void)0)
# define DESTRUCTOR __attribute__((destructor))
# define HAVE_DESTRUCTOR 1
#else
# define AT_EXIT(X) atexit((X))
# define DESTRUCTOR
# define HAVE_DESTRUCTOR 0
#endif
// ==========================================================================
// One-time init
// Call once support
// ==========================================================================
#ifdef _WIN32
# define ONCE_TYPE volatile LONG
# define ONCE_INIT 0L
# define ONCE_FUNC win32_run_once
# define CALL_ONCE win32_call_once
# define CALL_ONCE_TYPE volatile LONG
# define CALL_ONCE_INIT 0L
#else
# define ONCE_TYPE pthread_once_t
# define ONCE_INIT PTHREAD_ONCE_INIT
# define ONCE_FUNC pthread_once
# define CALL_ONCE pthread_once
# define CALL_ONCE_TYPE pthread_once_t
# define CALL_ONCE_INIT PTHREAD_ONCE_INIT
#endif
#ifdef _WIN32
static void win32_run_once(ONCE_TYPE *const control, void (*init_routine)(void))
static void win32_call_once(CALL_ONCE_TYPE *const control, void (*init_routine)(void))
{
LONG status;
while ((status = InterlockedCompareExchange(control, -1L, 0L)) != 0L)
@ -80,7 +78,7 @@ static void win32_run_once(ONCE_TYPE *const control, void (*init_routine)(void))
#endif
/* Global state */
static ONCE_TYPE s_random_is_initialized = ONCE_INIT;
static CALL_ONCE_TYPE s_random_is_initialized = CALL_ONCE_INIT;
#if defined(_WIN32)
typedef BOOLEAN(WINAPI *rtl_genrandom_t)(void *buffer, ULONG buff_size);
static HMODULE s_dll_advapi32 = NULL;
@ -90,8 +88,8 @@ static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/
static int s_random_fd = -1;
#endif
/* Close down CSRNG */
static DESTRUCTOR void exit_random_source(void)
/* De-initialize CSRNG */
static void exit_random_source(void)
{
#if defined(_WIN32)
s_rtl_genrandom = NULL;
@ -126,13 +124,15 @@ static void init_random_source(void)
}
}
#endif
AT_EXIT(exit_random_source);
#if !HAVE_DESTRUCTOR
atexit(exit_random_source);
#endif
}
/* 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);
CALL_ONCE(&s_random_is_initialized, init_random_source);
#if defined(_WIN32)
if (s_rtl_genrandom)
{
@ -157,7 +157,12 @@ size_t slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
// ==========================================================================
#ifdef _WIN32
# ifdef SecureZeroMemory
# define HAVE_EXPLICIT_BZERO 1
# define explicit_bzero SecureZeroMemory
# else
# define HAVE_EXPLICIT_BZERO 0
# endif
#else
# if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
# define HAVE_EXPLICIT_BZERO 1
@ -168,20 +173,29 @@ size_t slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
# endif
#endif
void slunkcrypt_bzero(void* const ptr, const size_t length)
void slunkcrypt_bzero(void* const buffer, const size_t length)
{
if ((ptr) && (length > 0U))
if ((buffer) && (length > 0U))
{
#if defined(_WIN32) && defined(SecureZeroMemory)
SecureZeroMemory(ptr, length);
#elif HAVE_EXPLICIT_BZERO
explicit_bzero(ptr, length);
#if HAVE_EXPLICIT_BZERO
explicit_bzero(buffer, length);
#else
volatile uint8_t *buffer = (volatile uint8_t*)ptr;
volatile uint8_t* ptr = (volatile uint8_t*) buffer;
for (size_t i = 0U; i < length; ++i)
{
buffer[i] = 0U;
ptr[i] = 0U;
}
#endif
}
}
// ==========================================================================
// Destructor
// ==========================================================================
#if HAVE_DESTRUCTOR
__attribute__((destructor)) void slunkcrypt_destructor()
{
exit_random_source();
}
#endif