From 648f46d22a31d8dbb179767bda23df06865e81d8 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 29 Jun 2016 15:28:15 +0200 Subject: [PATCH] Expose sodium_crit_enter() and sodium_crit_leave() internally --- src/libsodium/Makefile.am | 1 + src/libsodium/include/sodium/private/mutex.h | 7 +++ .../salsa20/randombytes_salsa20_random.c | 30 ++++++++----- src/libsodium/sodium/core.c | 44 +++++++++---------- 4 files changed, 49 insertions(+), 33 deletions(-) create mode 100644 src/libsodium/include/sodium/private/mutex.h diff --git a/src/libsodium/Makefile.am b/src/libsodium/Makefile.am index 4ea04d05..fef73783 100644 --- a/src/libsodium/Makefile.am +++ b/src/libsodium/Makefile.am @@ -102,6 +102,7 @@ libsodium_la_SOURCES = \ crypto_verify/64/ref/verify_64.c \ include/sodium/private/common.h \ include/sodium/private/curve25519_ref10.h \ + include/sodium/private/mutex.h \ randombytes/randombytes.c \ sodium/core.c \ sodium/runtime.c \ diff --git a/src/libsodium/include/sodium/private/mutex.h b/src/libsodium/include/sodium/private/mutex.h new file mode 100644 index 00000000..322b6742 --- /dev/null +++ b/src/libsodium/include/sodium/private/mutex.h @@ -0,0 +1,7 @@ +#ifndef mutex_H +#define mutex_H 1 + +extern int sodium_crit_enter(void); +extern int sodium_crit_leave(void); + +#endif diff --git a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c index b71ce7e1..b21925dd 100644 --- a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +++ b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c @@ -27,6 +27,7 @@ #include "randombytes.h" #include "randombytes_salsa20_random.h" #include "utils.h" +#include "private/mutex.h" #ifdef _WIN32 # include @@ -297,7 +298,7 @@ randombytes_salsa20_random_rekey(const unsigned char * const mix) } static void -randombytes_salsa20_random_stir(void) +randombytes_salsa20_random_stir_unlocked(void) { /* constant to personalize the hash function */ const unsigned char hsigma[crypto_generichash_KEYBYTES] = { @@ -355,18 +356,26 @@ randombytes_salsa20_random_stir(void) #endif } +static void +randombytes_salsa20_random_stir(void) +{ + sodium_crit_enter(); + randombytes_salsa20_random_stir_unlocked(); + sodium_crit_leave(); +} + static void randombytes_salsa20_random_stir_if_needed(void) { #ifdef HAVE_GETPID if (stream.initialized == 0) { - randombytes_salsa20_random_stir(); + randombytes_salsa20_random_stir_unlocked(); } else if (stream.pid != getpid()) { abort(); } #else if (stream.initialized == 0) { - randombytes_salsa20_random_stir(); + randombytes_salsa20_random_stir_unlocked(); } #endif } @@ -376,6 +385,7 @@ randombytes_salsa20_random_close(void) { int ret = -1; + sodium_crit_enter(); #ifndef _WIN32 if (stream.random_data_source_fd != -1 && close(stream.random_data_source_fd) == 0) { @@ -403,6 +413,8 @@ randombytes_salsa20_random_close(void) ret = 0; } #endif + sodium_crit_leave(); + return ret; } @@ -412,6 +424,7 @@ randombytes_salsa20_random_buf(void * const buf, const size_t size) size_t i; int ret; + sodium_crit_enter(); randombytes_salsa20_random_stir_if_needed(); COMPILER_ASSERT(sizeof stream.nonce == crypto_stream_salsa20_NONCEBYTES); #ifdef ULONG_LONG_MAX @@ -427,14 +440,16 @@ randombytes_salsa20_random_buf(void * const buf, const size_t size) stream.nonce++; crypto_stream_salsa20_xor(stream.key, stream.key, sizeof stream.key, (unsigned char *) &stream.nonce, stream.key); + sodium_crit_leave(); } static uint32_t -randombytes_salsa20_random_getword(void) +randombytes_salsa20_random(void) { uint32_t val; int ret; + sodium_crit_enter(); COMPILER_ASSERT(sizeof stream.rnd32 >= (sizeof stream.key) + (sizeof val)); COMPILER_ASSERT(((sizeof stream.rnd32) - (sizeof stream.key)) % sizeof val == (size_t) 0U); @@ -453,16 +468,11 @@ randombytes_salsa20_random_getword(void) stream.rnd32_outleft -= sizeof val; memcpy(&val, &stream.rnd32[stream.rnd32_outleft], sizeof val); memset(&stream.rnd32[stream.rnd32_outleft], 0, sizeof val); + sodium_crit_leave(); return val; } -static uint32_t -randombytes_salsa20_random(void) -{ - return randombytes_salsa20_random_getword(); -} - static const char * randombytes_salsa20_implementation_name(void) { diff --git a/src/libsodium/sodium/core.c b/src/libsodium/sodium/core.c index e046aa30..87fd6d14 100644 --- a/src/libsodium/sodium/core.c +++ b/src/libsodium/sodium/core.c @@ -16,6 +16,7 @@ #include "randombytes.h" #include "runtime.h" #include "utils.h" +#include "private/mutex.h" #if !defined(_MSC_VER) && 1 # warning This is unstable, untested, development code. @@ -26,19 +27,16 @@ # warning Alternatively, use the "stable" branch in the git repository. #endif -static int _sodium_crit_enter(void); -static int _sodium_crit_leave(void); - static volatile int initialized; int sodium_init(void) { - if (_sodium_crit_enter() != 0) { + if (sodium_crit_enter() != 0) { return -1; } if (initialized != 0) { - if (_sodium_crit_leave() != 0) { + if (sodium_crit_leave() != 0) { return -1; } return 1; @@ -52,7 +50,7 @@ sodium_init(void) _crypto_scalarmult_curve25519_pick_best_implementation(); _crypto_stream_chacha20_pick_best_implementation(); initialized = 1; - if (_sodium_crit_leave() != 0) { + if (sodium_crit_leave() != 0) { return -1; } return 0; @@ -62,14 +60,14 @@ sodium_init(void) static pthread_mutex_t _sodium_lock = PTHREAD_MUTEX_INITIALIZER; -static int -_sodium_crit_enter(void) +int +sodium_crit_enter(void) { return pthread_mutex_lock(&_sodium_lock); } -static int -_sodium_crit_leave(void) +int +sodium_crit_leave(void) { return pthread_mutex_unlock(&_sodium_lock); } @@ -79,7 +77,7 @@ _sodium_crit_leave(void) static CRITICAL_SECTION _sodium_lock; static volatile LONG _sodium_lock_initialized; -static int +int _sodium_crit_init(void) { LONG status = 0L; @@ -100,8 +98,8 @@ _sodium_crit_init(void) } } -static int -_sodium_crit_enter(void) +int +sodium_crit_enter(void) { if (_sodium_crit_init() != 0) { return -1; @@ -111,8 +109,8 @@ _sodium_crit_enter(void) return 0; } -static int -_sodium_crit_leave(void) +int +sodium_crit_leave(void) { LeaveCriticalSection(&_sodium_lock); @@ -123,8 +121,8 @@ _sodium_crit_leave(void) static volatile int _sodium_lock; -static int -_sodium_crit_enter(void) +int +sodium_crit_enter(void) { # ifdef HAVE_NANOSLEEP struct timespec q; @@ -140,8 +138,8 @@ _sodium_crit_enter(void) return 0; } -static int -_sodium_crit_leave(void) +int +sodium_crit_leave(void) { __sync_lock_release(&_sodium_lock); @@ -150,14 +148,14 @@ _sodium_crit_leave(void) #else -static int -_sodium_crit_enter(void) +int +sodium_crit_enter(void) { return 0; } -static int -_sodium_crit_leave(void) +int +sodium_crit_leave(void) { return 0; }