From 3aa7da60e6c18515c8d78fa5c3ebc3b9e7938efe Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 21 Sep 2025 10:56:08 -0600 Subject: [PATCH] fix libretiny --- esphome/components/sha256/sha256.cpp | 31 +--------------------------- esphome/components/sha256/sha256.h | 14 ++++--------- esphome/core/helpers.h | 11 ++++++++++ 3 files changed, 16 insertions(+), 40 deletions(-) diff --git a/esphome/components/sha256/sha256.cpp b/esphome/components/sha256/sha256.cpp index f3c0625a5a..62edb5aaa2 100644 --- a/esphome/components/sha256/sha256.cpp +++ b/esphome/components/sha256/sha256.cpp @@ -8,7 +8,7 @@ namespace esphome::sha256 { -#ifdef USE_ESP32 +#if defined(USE_ESP32) || defined(USE_LIBRETINY) SHA256::~SHA256() { if (this->ctx_) { @@ -106,35 +106,6 @@ void SHA256::calculate() { } } -#elif defined(USE_ARDUINO) - -SHA256::~SHA256() = default; - -void SHA256::init() { - if (!this->ctx_) { - this->ctx_ = std::make_unique(); - } - this->ctx_->sha.reset(); - this->ctx_->calculated = false; -} - -void SHA256::add(const uint8_t *data, size_t len) { - if (!this->ctx_) { - this->init(); - } - this->ctx_->sha.update(data, len); -} - -void SHA256::calculate() { - if (!this->ctx_) { - this->init(); - } - if (!this->ctx_->calculated) { - this->ctx_->sha.finalize(this->ctx_->hash, 32); - this->ctx_->calculated = true; - } -} - #else #error "SHA256 not supported on this platform" #endif diff --git a/esphome/components/sha256/sha256.h b/esphome/components/sha256/sha256.h index 89b3218166..0cea4cdcef 100644 --- a/esphome/components/sha256/sha256.h +++ b/esphome/components/sha256/sha256.h @@ -9,14 +9,14 @@ #include #include -#ifdef USE_ESP32 +#if defined(USE_ESP32) || defined(USE_LIBRETINY) #include "mbedtls/sha256.h" #elif defined(USE_ESP8266) || defined(USE_RP2040) #include #elif defined(USE_HOST) #include -#elif defined(USE_ARDUINO) -#include +#else +#error "SHA256 not supported on this platform" #endif namespace esphome::sha256 { @@ -41,7 +41,7 @@ class SHA256 { bool equals_hex(const char *expected); protected: -#ifdef USE_ESP32 +#if defined(USE_ESP32) || defined(USE_LIBRETINY) struct SHA256Context { mbedtls_sha256_context ctx; uint8_t hash[32]; @@ -58,12 +58,6 @@ class SHA256 { uint8_t hash[32]; bool calculated{false}; }; -#elif defined(USE_ARDUINO) - struct SHA256Context { - ::SHA256 sha; - uint8_t hash[32]; - bool calculated{false}; - }; #else #error "SHA256 not supported on this platform" #endif diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 21aa159b25..a28718de5a 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -82,6 +82,16 @@ template constexpr T byteswap(T n) { return m; } template<> constexpr uint8_t byteswap(uint8_t n) { return n; } +#ifdef USE_LIBRETINY +// LibreTiny's Beken framework redefines __builtin_bswap functions as non-constexpr +template<> inline uint16_t byteswap(uint16_t n) { return __builtin_bswap16(n); } +template<> inline uint32_t byteswap(uint32_t n) { return __builtin_bswap32(n); } +template<> inline uint64_t byteswap(uint64_t n) { return __builtin_bswap64(n); } +template<> inline int8_t byteswap(int8_t n) { return n; } +template<> inline int16_t byteswap(int16_t n) { return __builtin_bswap16(n); } +template<> inline int32_t byteswap(int32_t n) { return __builtin_bswap32(n); } +template<> inline int64_t byteswap(int64_t n) { return __builtin_bswap64(n); } +#else template<> constexpr uint16_t byteswap(uint16_t n) { return __builtin_bswap16(n); } template<> constexpr uint32_t byteswap(uint32_t n) { return __builtin_bswap32(n); } template<> constexpr uint64_t byteswap(uint64_t n) { return __builtin_bswap64(n); } @@ -89,6 +99,7 @@ template<> constexpr int8_t byteswap(int8_t n) { return n; } template<> constexpr int16_t byteswap(int16_t n) { return __builtin_bswap16(n); } template<> constexpr int32_t byteswap(int32_t n) { return __builtin_bswap32(n); } template<> constexpr int64_t byteswap(int64_t n) { return __builtin_bswap64(n); } +#endif ///@}