From d4ee08ab8a1c674203796161af6d013283b33d69 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 7 Mar 2022 16:38:37 +0100 Subject: [PATCH] Add memory fences where supported --- src/libsodium/crypto_hash/sha256/cp/hash_sha256_cp.c | 2 ++ src/libsodium/crypto_hash/sha512/cp/hash_sha512_cp.c | 2 ++ .../scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c | 1 + src/libsodium/crypto_sign/ed25519/ref10/open.c | 2 ++ src/libsodium/include/sodium/private/common.h | 6 ++++++ src/libsodium/sodium/codecs.c | 2 ++ 6 files changed, 15 insertions(+) diff --git a/src/libsodium/crypto_hash/sha256/cp/hash_sha256_cp.c b/src/libsodium/crypto_hash/sha256/cp/hash_sha256_cp.c index 264054f9..394c3914 100644 --- a/src/libsodium/crypto_hash/sha256/cp/hash_sha256_cp.c +++ b/src/libsodium/crypto_hash/sha256/cp/hash_sha256_cp.c @@ -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; diff --git a/src/libsodium/crypto_hash/sha512/cp/hash_sha512_cp.c b/src/libsodium/crypto_hash/sha512/cp/hash_sha512_cp.c index 8e0f36fb..a36841b9 100644 --- a/src/libsodium/crypto_hash/sha512/cp/hash_sha512_cp.c +++ b/src/libsodium/crypto_hash/sha512/cp/hash_sha512_cp.c @@ -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; diff --git a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c index 4378b22d..fdf0836b 100644 --- a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c +++ b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c @@ -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++; } diff --git a/src/libsodium/crypto_sign/ed25519/ref10/open.c b/src/libsodium/crypto_sign/ed25519/ref10/open.c index 3631edaf..0ee7547c 100644 --- a/src/libsodium/crypto_sign/ed25519/ref10/open.c +++ b/src/libsodium/crypto_sign/ed25519/ref10/open.c @@ -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; diff --git a/src/libsodium/include/sodium/private/common.h b/src/libsodium/include/sodium/private/common.h index 2ea7a1b2..7d86b96a 100644 --- a/src/libsodium/include/sodium/private/common.h +++ b/src/libsodium/include/sodium/private/common.h @@ -260,4 +260,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 diff --git a/src/libsodium/sodium/codecs.c b/src/libsodium/sodium/codecs.c index 77fa464c..36508089 100644 --- a/src/libsodium/sodium/codecs.c +++ b/src/libsodium/sodium/codecs.c @@ -7,6 +7,7 @@ #include #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--;