Add memory fences where supported

This commit is contained in:
Frank Denis
2023-09-09 09:01:44 +02:00
parent 3a6a6025cc
commit e95d437f84
6 changed files with 15 additions and 0 deletions
@@ -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;
@@ -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;
@@ -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++;
}
@@ -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;
@@ -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
+2
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--;