From cc58cffb7b8f26e783b3fbd581ed17a0e70f98f3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 20 Mar 2026 16:58:34 -1000 Subject: [PATCH] [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). --- esphome/components/api/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index 4c3cf81927..84589d540d 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -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")