Faster sodium_memcmp and sodium_is_ezro

This commit is contained in:
Frank Denis
2026-09-20 12:33:37 +02:00
parent f6bd914086
commit 57d44b9549
+14 -4
View File
@@ -196,7 +196,8 @@ sodium_memcmp(const void *const b1_, const void *const b2_, size_t len)
(const volatile unsigned char *volatile) b2_;
#endif
size_t i;
volatile unsigned char d = 0U;
unsigned char d = 0U;
volatile unsigned char e;
#if HAVE_WEAK_SYMBOLS
_sodium_dummy_symbol_to_prevent_memcmp_lto(b1, b2, len);
@@ -204,7 +205,11 @@ sodium_memcmp(const void *const b1_, const void *const b2_, size_t len)
for (i = 0U; i < len; i++) {
d |= b1[i] ^ b2[i];
}
return (1 & ((d - 1) >> 8)) - 1;
e = d;
#if defined(__GNUC__) || defined(__clang__)
__asm__("" : "+r"(e) :);
#endif
return (1 & ((e - 1) >> 8)) - 1;
}
#ifdef HAVE_WEAK_SYMBOLS
@@ -258,12 +263,17 @@ int
sodium_is_zero(const unsigned char *n, const size_t nlen)
{
size_t i;
volatile unsigned char d = 0U;
unsigned char d = 0U;
volatile unsigned char e;
for (i = 0U; i < nlen; i++) {
d |= n[i];
}
return 1 & ((d - 1) >> 8);
e = d;
#if defined(__GNUC__) || defined(__clang__)
__asm__("" : "+r"(e) :);
#endif
return 1 & ((e - 1) >> 8);
}
void