crit_{enter,leave} can fail

This commit is contained in:
Frank Denis 2016-07-05 12:03:00 +02:00
parent 29492143ab
commit 89918e94f1

View File

@ -359,9 +359,13 @@ randombytes_salsa20_random_stir_unlocked(void)
static void static void
randombytes_salsa20_random_stir(void) randombytes_salsa20_random_stir(void)
{ {
sodium_crit_enter(); if (sodium_crit_enter() != 0) {
abort();
}
randombytes_salsa20_random_stir_unlocked(); randombytes_salsa20_random_stir_unlocked();
sodium_crit_leave(); if (sodium_crit_leave() != 0) {
abort();
}
} }
static void static void
@ -385,7 +389,9 @@ randombytes_salsa20_random_close(void)
{ {
int ret = -1; int ret = -1;
sodium_crit_enter(); if (sodium_crit_enter() != 0) {
abort();
}
#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) {
@ -413,8 +419,9 @@ randombytes_salsa20_random_close(void)
ret = 0; ret = 0;
} }
#endif #endif
sodium_crit_leave(); if (sodium_crit_leave() != 0) {
abort();
}
return ret; return ret;
} }
@ -424,7 +431,9 @@ randombytes_salsa20_random_buf(void * const buf, const size_t size)
size_t i; size_t i;
int ret; int ret;
sodium_crit_enter(); if (sodium_crit_enter() != 0) {
abort();
}
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
@ -440,7 +449,9 @@ 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(); if (sodium_crit_leave() != 0) {
abort();
}
} }
static uint32_t static uint32_t
@ -449,7 +460,9 @@ randombytes_salsa20_random(void)
uint32_t val; uint32_t val;
int ret; int ret;
sodium_crit_enter(); if (sodium_crit_enter() != 0) {
abort();
}
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);
@ -468,8 +481,9 @@ randombytes_salsa20_random(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(); if (sodium_crit_leave() != 0) {
abort();
}
return val; return val;
} }