[api] Enable HAVE_WEAK_SYMBOLS and HAVE_INLINE_ASM for libsodium

Without these flags, libsodium's sodium_memzero() falls through to the
slowest fallback: a volatile byte-by-byte zeroing loop. With them, it
uses memset() guarded by a weak-symbol call and inline asm memory
clobber, which is significantly faster.

This also improves sodium_memcmp() and sodium_compare() codegen by
allowing non-volatile pointer access with weak barriers, and enables
a timing-safe comparison optimization in crypto_verify.

Both flags are safe for all ESPHome targets: __attribute__((weak)) and
__asm__ are core GCC features supported by all toolchains (Xtensa,
RISC-V, ARM, x86_64).
This commit is contained in:
J. Nick Koston
2026-03-20 16:58:34 -10:00
parent 95dea59382
commit cc58cffb7b
+3
View File
@@ -455,6 +455,9 @@ async def to_code(config: ConfigType) -> None:
cg.add_define("USE_API_PLAINTEXT")
cg.add_define("USE_API_NOISE")
cg.add_library("esphome/noise-c", "0.1.11")
# Enable optimized memzero/memcmp in libsodium instead of volatile byte loops
cg.add_build_flag("-DHAVE_WEAK_SYMBOLS=1")
cg.add_build_flag("-DHAVE_INLINE_ASM=1")
else:
cg.add_define("USE_API_PLAINTEXT")