1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-24 12:36:01 -07:00

Add memory fences where supported

This commit is contained in:
Frank Denis 2022-03-07 16:38:37 +01:00
parent 3a6a6025cc
commit e95d437f84
6 changed files with 15 additions and 0 deletions

View File

@ -156,6 +156,7 @@ SHA256_Pad(crypto_hash_sha256_state *state, uint32_t tmp32[64 + 8])
unsigned int r;
unsigned int i;
ACQUIRE_FENCE;
r = (unsigned int) ((state->count >> 3) & 0x3f);
if (r < 56) {
for (i = 0; i < 56 - r; i++) {
@ -197,6 +198,7 @@ crypto_hash_sha256_update(crypto_hash_sha256_state *state,
if (inlen <= 0U) {
return 0;
}
ACQUIRE_FENCE;
r = (unsigned long long) ((state->count >> 3) & 0x3f);
state->count += ((uint64_t) inlen) << 3;

View File

@ -175,6 +175,7 @@ SHA512_Pad(crypto_hash_sha512_state *state, uint64_t tmp64[80 + 8])
unsigned int r;
unsigned int i;
ACQUIRE_FENCE;
r = (unsigned int) ((state->count[1] >> 3) & 0x7f);
if (r < 112) {
for (i = 0; i < 112 - r; i++) {
@ -218,6 +219,7 @@ crypto_hash_sha512_update(crypto_hash_sha512_state *state,
if (inlen <= 0U) {
return 0;
}
ACQUIRE_FENCE;
r = (unsigned long long) ((state->count[1] >> 3) & 0x7f);
bitlen[1] = ((uint64_t) inlen) << 3;

View File

@ -57,6 +57,7 @@ sodium_strnlen(const char *str, size_t maxlen)
{
size_t i = 0U;
ACQUIRE_FENCE;
while (i < maxlen && str[i] != 0) {
i++;
}

View File

@ -7,6 +7,7 @@
#include "crypto_sign_ed25519.h"
#include "crypto_verify_32.h"
#include "sign_ed25519_ref10.h"
#include "private/common.h"
#include "private/ed25519_ref10.h"
#include "utils.h"
@ -23,6 +24,7 @@ _crypto_sign_ed25519_verify_detached(const unsigned char *sig,
ge25519_p3 A;
ge25519_p2 R;
ACQUIRE_FENCE;
#ifdef ED25519_COMPAT
if (sig[63] & 224) {
return -1;

View File

@ -258,4 +258,10 @@ extern void ct_unpoison(const void *, size_t);
# define UNPOISON(X, L) (void) 0
#endif
#ifdef __ATOMIC_ACQUIRE
# define ACQUIRE_FENCE __atomic_thread_fence(__ATOMIC_ACQUIRE)
#else
# define ACQUIRE_FENCE (void) 0
#endif
#endif

View File

@ -7,6 +7,7 @@
#include <string.h>
#include "core.h"
#include "private/common.h"
#include "utils.h"
/* Derived from original code by CodesInChaos */
@ -250,6 +251,7 @@ _sodium_base642bin_skip_padding(const char * const b64, const size_t b64_len,
errno = ERANGE;
return -1;
}
ACQUIRE_FENCE;
c = b64[*b64_pos_p];
if (c == '=') {
padding_len--;