diff options
Diffstat (limited to 'src/hash.c')
-rw-r--r-- | src/hash.c | 83 |
1 files changed, 13 insertions, 70 deletions
@@ -1,104 +1,47 @@ -#include "config.h" +#include <s.h> #include <assert.h> -#include <pthread.h> #include <stdbool.h> -#include <stddef.h> -#include <stdint.h> #include <stdio.h> -#include <string.h> #include <siphash.h> -#include "hash.h" #include "logerr.h" #include "random.h" +#include "hash.h" -static volatile bool -INITIALIZED = false; -static uint8_t -HASH_KEY[SIPHASHBS_KEY_LENGTH]; +static bool +SEED_INITIALIZED = false; -static pthread_mutex_t -SEED_MUTEX = PTHREAD_MUTEX_INITIALIZER; +static u8 +HASH_KEY[SIPHASH_KEY_LENGTH]; -static int -unsafe_init(void) { +int +hash_setup(void) { int rc = -1; - if (urandom_bytes(SIPHASHBS_KEY_LENGTH, &HASH_KEY)) { + if (random_bytes(SIPHASH_KEY_LENGTH, &HASH_KEY)) { logerr("urandom_bytes()"); goto out; } + SEED_INITIALIZED = true; rc = 0; out: return rc; } -static int -safe_init(void) { - int rc = -1; - - bool lock_acquired = false; - - if (INITIALIZED == true) { - rc = 0; - goto out; - } - - const int ret1 = pthread_mutex_lock(&SEED_MUTEX); - if (ret1) { - logerr("pthread_mutex_lock(): %s", strerror(ret1)); - goto out; - } - lock_acquired = true; - - if (INITIALIZED == true) { - rc = 0; - goto out; - } - - if (unsafe_init()) { - logerr("unsafe_init()"); - goto out; - } - - INITIALIZED = true; - - rc = 0; -out: - if (lock_acquired) { - const int ret2 = pthread_mutex_unlock(&SEED_MUTEX); - if (ret2) { - logerr("pthread_mutex_unlock(): %s", strerror(ret2)); - rc = -1; - } - } - return rc; -} - -static void -ensure_initialized(void) { - assert((safe_init() == 0) && "Failed to initialized the hash seed"); -} - -int -hash_init(void) { - return safe_init(); -} - void hash( const size_t inlen, const void *const restrict in, - uint8_t out[HASH_OUTPUT_LENGTH] + u8 out[HASH_OUTPUT_LENGTH] ) { - ensure_initialized(); - siphashbs(HASH_KEY, inlen, in, out); + assert(SEED_INITIALIZED == true); + siphash(HASH_KEY, inlen, in, out); } |