1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-22 05:02:23 +01:00

fix libretiny

This commit is contained in:
J. Nick Koston
2025-09-21 10:56:08 -06:00
parent ada1b00cad
commit 3aa7da60e6
3 changed files with 16 additions and 40 deletions

View File

@@ -8,7 +8,7 @@
namespace esphome::sha256 { namespace esphome::sha256 {
#ifdef USE_ESP32 #if defined(USE_ESP32) || defined(USE_LIBRETINY)
SHA256::~SHA256() { SHA256::~SHA256() {
if (this->ctx_) { 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<SHA256Context>();
}
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 #else
#error "SHA256 not supported on this platform" #error "SHA256 not supported on this platform"
#endif #endif

View File

@@ -9,14 +9,14 @@
#include <string> #include <string>
#include <memory> #include <memory>
#ifdef USE_ESP32 #if defined(USE_ESP32) || defined(USE_LIBRETINY)
#include "mbedtls/sha256.h" #include "mbedtls/sha256.h"
#elif defined(USE_ESP8266) || defined(USE_RP2040) #elif defined(USE_ESP8266) || defined(USE_RP2040)
#include <bearssl/bearssl_hash.h> #include <bearssl/bearssl_hash.h>
#elif defined(USE_HOST) #elif defined(USE_HOST)
#include <openssl/evp.h> #include <openssl/evp.h>
#elif defined(USE_ARDUINO) #else
#include <SHA256.h> #error "SHA256 not supported on this platform"
#endif #endif
namespace esphome::sha256 { namespace esphome::sha256 {
@@ -41,7 +41,7 @@ class SHA256 {
bool equals_hex(const char *expected); bool equals_hex(const char *expected);
protected: protected:
#ifdef USE_ESP32 #if defined(USE_ESP32) || defined(USE_LIBRETINY)
struct SHA256Context { struct SHA256Context {
mbedtls_sha256_context ctx; mbedtls_sha256_context ctx;
uint8_t hash[32]; uint8_t hash[32];
@@ -58,12 +58,6 @@ class SHA256 {
uint8_t hash[32]; uint8_t hash[32];
bool calculated{false}; bool calculated{false};
}; };
#elif defined(USE_ARDUINO)
struct SHA256Context {
::SHA256 sha;
uint8_t hash[32];
bool calculated{false};
};
#else #else
#error "SHA256 not supported on this platform" #error "SHA256 not supported on this platform"
#endif #endif

View File

@@ -82,6 +82,16 @@ template<typename T> constexpr T byteswap(T n) {
return m; return m;
} }
template<> constexpr uint8_t byteswap(uint8_t n) { return n; } 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 uint16_t byteswap(uint16_t n) { return __builtin_bswap16(n); }
template<> constexpr uint32_t byteswap(uint32_t n) { return __builtin_bswap32(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); } 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 int16_t byteswap(int16_t n) { return __builtin_bswap16(n); }
template<> constexpr int32_t byteswap(int32_t n) { return __builtin_bswap32(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); } template<> constexpr int64_t byteswap(int64_t n) { return __builtin_bswap64(n); }
#endif
///@} ///@}