diff --git a/esphome/components/sha256/sha256.cpp b/esphome/components/sha256/sha256.cpp index 933e3ff8031..23995e6534b 100644 --- a/esphome/components/sha256/sha256.cpp +++ b/esphome/components/sha256/sha256.cpp @@ -10,11 +10,11 @@ namespace esphome::sha256 { #if defined(USE_ESP32) || defined(USE_LIBRETINY) -// CRITICAL ESP32-S2/S3 HARDWARE SHA ACCELERATION REQUIREMENTS (IDF 5.5.x): +// CRITICAL ESP32 HARDWARE SHA ACCELERATION REQUIREMENTS (IDF 5.5.x): // -// The ESP32-S2/S3 uses hardware DMA for SHA acceleration. The DMA engine requires proper -// alignment of the digest output buffer. This is handled automatically via HashBase::digest_ -// which has alignas(32). This imposes two critical constraints: +// ESP32 variants (except original ESP32) use DMA-based hardware SHA acceleration that requires +// 32-byte aligned digest buffers. This is handled automatically via HashBase::digest_ which has +// alignas(32) on these platforms. Two additional constraints apply: // // 1. NO VARIABLE LENGTH ARRAYS (VLAs): VLAs corrupt the stack layout, causing the DMA engine to // write to incorrect memory locations. This results in null pointer dereferences and crashes. @@ -35,7 +35,7 @@ namespace esphome::sha256 { // // hasher destroyed when function returns // } // -// INCORRECT USAGE (WILL FAIL ON ESP32-S2/S3): +// INCORRECT USAGE (WILL FAIL): // void my_function() { // sha256::SHA256 hasher; // helper(&hasher); // WRONG: Passed to different stack frame diff --git a/esphome/components/sha256/sha256.h b/esphome/components/sha256/sha256.h index 5fab9bde613..bafb359485c 100644 --- a/esphome/components/sha256/sha256.h +++ b/esphome/components/sha256/sha256.h @@ -24,7 +24,7 @@ namespace esphome::sha256 { /// SHA256 hash implementation. /// -/// CRITICAL for ESP32-S2/S3 with IDF 5.5.x hardware SHA acceleration: +/// CRITICAL for ESP32 variants (except original) with IDF 5.5.x hardware SHA acceleration: /// 1. The object MUST stay in the same stack frame (no passing to other functions) /// 2. NO Variable Length Arrays (VLAs) in the same function /// diff --git a/esphome/core/hash_base.h b/esphome/core/hash_base.h index 5b62fd0d17e..f48093b63e3 100644 --- a/esphome/core/hash_base.h +++ b/esphome/core/hash_base.h @@ -44,9 +44,13 @@ class HashBase { virtual size_t get_size() const = 0; protected: - // 32-byte alignment required for ESP32-S2/S3 hardware SHA DMA operations. - // This also sets the class alignment to 32, ensuring derived objects are properly aligned. - alignas(32) uint8_t digest_[32]; // Storage sized for max(MD5=16, SHA256=32) bytes +// ESP32 variants with DMA-based hardware SHA (all except original ESP32) require 32-byte aligned buffers. +// Original ESP32 uses a different hardware SHA implementation without DMA alignment requirements. +// Other platforms (ESP8266, RP2040, LibreTiny) use software SHA and don't need alignment. +#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32) + alignas(32) +#endif + uint8_t digest_[32]; // Storage sized for max(MD5=16, SHA256=32) bytes }; } // namespace esphome