Expose sodium_crit_enter() and sodium_crit_leave() internally

This commit is contained in:
Frank Denis 2016-06-29 15:28:15 +02:00
parent 5a3ff833fd
commit 648f46d22a
4 changed files with 49 additions and 33 deletions

View File

@ -102,6 +102,7 @@ libsodium_la_SOURCES = \
crypto_verify/64/ref/verify_64.c \ crypto_verify/64/ref/verify_64.c \
include/sodium/private/common.h \ include/sodium/private/common.h \
include/sodium/private/curve25519_ref10.h \ include/sodium/private/curve25519_ref10.h \
include/sodium/private/mutex.h \
randombytes/randombytes.c \ randombytes/randombytes.c \
sodium/core.c \ sodium/core.c \
sodium/runtime.c \ sodium/runtime.c \

View File

@ -0,0 +1,7 @@
#ifndef mutex_H
#define mutex_H 1
extern int sodium_crit_enter(void);
extern int sodium_crit_leave(void);
#endif

View File

@ -27,6 +27,7 @@
#include "randombytes.h" #include "randombytes.h"
#include "randombytes_salsa20_random.h" #include "randombytes_salsa20_random.h"
#include "utils.h" #include "utils.h"
#include "private/mutex.h"
#ifdef _WIN32 #ifdef _WIN32
# include <windows.h> # include <windows.h>
@ -297,7 +298,7 @@ randombytes_salsa20_random_rekey(const unsigned char * const mix)
} }
static void static void
randombytes_salsa20_random_stir(void) randombytes_salsa20_random_stir_unlocked(void)
{ {
/* constant to personalize the hash function */ /* constant to personalize the hash function */
const unsigned char hsigma[crypto_generichash_KEYBYTES] = { const unsigned char hsigma[crypto_generichash_KEYBYTES] = {
@ -355,18 +356,26 @@ randombytes_salsa20_random_stir(void)
#endif #endif
} }
static void
randombytes_salsa20_random_stir(void)
{
sodium_crit_enter();
randombytes_salsa20_random_stir_unlocked();
sodium_crit_leave();
}
static void static void
randombytes_salsa20_random_stir_if_needed(void) randombytes_salsa20_random_stir_if_needed(void)
{ {
#ifdef HAVE_GETPID #ifdef HAVE_GETPID
if (stream.initialized == 0) { if (stream.initialized == 0) {
randombytes_salsa20_random_stir(); randombytes_salsa20_random_stir_unlocked();
} else if (stream.pid != getpid()) { } else if (stream.pid != getpid()) {
abort(); abort();
} }
#else #else
if (stream.initialized == 0) { if (stream.initialized == 0) {
randombytes_salsa20_random_stir(); randombytes_salsa20_random_stir_unlocked();
} }
#endif #endif
} }
@ -376,6 +385,7 @@ randombytes_salsa20_random_close(void)
{ {
int ret = -1; int ret = -1;
sodium_crit_enter();
#ifndef _WIN32 #ifndef _WIN32
if (stream.random_data_source_fd != -1 && if (stream.random_data_source_fd != -1 &&
close(stream.random_data_source_fd) == 0) { close(stream.random_data_source_fd) == 0) {
@ -403,6 +413,8 @@ randombytes_salsa20_random_close(void)
ret = 0; ret = 0;
} }
#endif #endif
sodium_crit_leave();
return ret; return ret;
} }
@ -412,6 +424,7 @@ randombytes_salsa20_random_buf(void * const buf, const size_t size)
size_t i; size_t i;
int ret; int ret;
sodium_crit_enter();
randombytes_salsa20_random_stir_if_needed(); randombytes_salsa20_random_stir_if_needed();
COMPILER_ASSERT(sizeof stream.nonce == crypto_stream_salsa20_NONCEBYTES); COMPILER_ASSERT(sizeof stream.nonce == crypto_stream_salsa20_NONCEBYTES);
#ifdef ULONG_LONG_MAX #ifdef ULONG_LONG_MAX
@ -427,14 +440,16 @@ randombytes_salsa20_random_buf(void * const buf, const size_t size)
stream.nonce++; stream.nonce++;
crypto_stream_salsa20_xor(stream.key, stream.key, sizeof stream.key, crypto_stream_salsa20_xor(stream.key, stream.key, sizeof stream.key,
(unsigned char *) &stream.nonce, stream.key); (unsigned char *) &stream.nonce, stream.key);
sodium_crit_leave();
} }
static uint32_t static uint32_t
randombytes_salsa20_random_getword(void) randombytes_salsa20_random(void)
{ {
uint32_t val; uint32_t val;
int ret; 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));
COMPILER_ASSERT(((sizeof stream.rnd32) - (sizeof stream.key)) COMPILER_ASSERT(((sizeof stream.rnd32) - (sizeof stream.key))
% sizeof val == (size_t) 0U); % sizeof val == (size_t) 0U);
@ -453,16 +468,11 @@ randombytes_salsa20_random_getword(void)
stream.rnd32_outleft -= sizeof val; stream.rnd32_outleft -= sizeof val;
memcpy(&val, &stream.rnd32[stream.rnd32_outleft], sizeof val); memcpy(&val, &stream.rnd32[stream.rnd32_outleft], sizeof val);
memset(&stream.rnd32[stream.rnd32_outleft], 0, sizeof val); memset(&stream.rnd32[stream.rnd32_outleft], 0, sizeof val);
sodium_crit_leave();
return val; return val;
} }
static uint32_t
randombytes_salsa20_random(void)
{
return randombytes_salsa20_random_getword();
}
static const char * static const char *
randombytes_salsa20_implementation_name(void) randombytes_salsa20_implementation_name(void)
{ {

View File

@ -16,6 +16,7 @@
#include "randombytes.h" #include "randombytes.h"
#include "runtime.h" #include "runtime.h"
#include "utils.h" #include "utils.h"
#include "private/mutex.h"
#if !defined(_MSC_VER) && 1 #if !defined(_MSC_VER) && 1
# warning This is unstable, untested, development code. # warning This is unstable, untested, development code.
@ -26,19 +27,16 @@
# warning Alternatively, use the "stable" branch in the git repository. # warning Alternatively, use the "stable" branch in the git repository.
#endif #endif
static int _sodium_crit_enter(void);
static int _sodium_crit_leave(void);
static volatile int initialized; static volatile int initialized;
int int
sodium_init(void) sodium_init(void)
{ {
if (_sodium_crit_enter() != 0) { if (sodium_crit_enter() != 0) {
return -1; return -1;
} }
if (initialized != 0) { if (initialized != 0) {
if (_sodium_crit_leave() != 0) { if (sodium_crit_leave() != 0) {
return -1; return -1;
} }
return 1; return 1;
@ -52,7 +50,7 @@ sodium_init(void)
_crypto_scalarmult_curve25519_pick_best_implementation(); _crypto_scalarmult_curve25519_pick_best_implementation();
_crypto_stream_chacha20_pick_best_implementation(); _crypto_stream_chacha20_pick_best_implementation();
initialized = 1; initialized = 1;
if (_sodium_crit_leave() != 0) { if (sodium_crit_leave() != 0) {
return -1; return -1;
} }
return 0; return 0;
@ -62,14 +60,14 @@ sodium_init(void)
static pthread_mutex_t _sodium_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t _sodium_lock = PTHREAD_MUTEX_INITIALIZER;
static int int
_sodium_crit_enter(void) sodium_crit_enter(void)
{ {
return pthread_mutex_lock(&_sodium_lock); return pthread_mutex_lock(&_sodium_lock);
} }
static int int
_sodium_crit_leave(void) sodium_crit_leave(void)
{ {
return pthread_mutex_unlock(&_sodium_lock); return pthread_mutex_unlock(&_sodium_lock);
} }
@ -79,7 +77,7 @@ _sodium_crit_leave(void)
static CRITICAL_SECTION _sodium_lock; static CRITICAL_SECTION _sodium_lock;
static volatile LONG _sodium_lock_initialized; static volatile LONG _sodium_lock_initialized;
static int int
_sodium_crit_init(void) _sodium_crit_init(void)
{ {
LONG status = 0L; LONG status = 0L;
@ -100,8 +98,8 @@ _sodium_crit_init(void)
} }
} }
static int int
_sodium_crit_enter(void) sodium_crit_enter(void)
{ {
if (_sodium_crit_init() != 0) { if (_sodium_crit_init() != 0) {
return -1; return -1;
@ -111,8 +109,8 @@ _sodium_crit_enter(void)
return 0; return 0;
} }
static int int
_sodium_crit_leave(void) sodium_crit_leave(void)
{ {
LeaveCriticalSection(&_sodium_lock); LeaveCriticalSection(&_sodium_lock);
@ -123,8 +121,8 @@ _sodium_crit_leave(void)
static volatile int _sodium_lock; static volatile int _sodium_lock;
static int int
_sodium_crit_enter(void) sodium_crit_enter(void)
{ {
# ifdef HAVE_NANOSLEEP # ifdef HAVE_NANOSLEEP
struct timespec q; struct timespec q;
@ -140,8 +138,8 @@ _sodium_crit_enter(void)
return 0; return 0;
} }
static int int
_sodium_crit_leave(void) sodium_crit_leave(void)
{ {
__sync_lock_release(&_sodium_lock); __sync_lock_release(&_sodium_lock);
@ -150,14 +148,14 @@ _sodium_crit_leave(void)
#else #else
static int int
_sodium_crit_enter(void) sodium_crit_enter(void)
{ {
return 0; return 0;
} }
static int int
_sodium_crit_leave(void) sodium_crit_leave(void)
{ {
return 0; return 0;
} }