mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 09:01:49 +00:00 
			
		
		
		
	fix libretiny
This commit is contained in:
		@@ -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<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
 | 
			
		||||
#error "SHA256 not supported on this platform"
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -9,14 +9,14 @@
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <memory>
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
 | 
			
		||||
#include "mbedtls/sha256.h"
 | 
			
		||||
#elif defined(USE_ESP8266) || defined(USE_RP2040)
 | 
			
		||||
#include <bearssl/bearssl_hash.h>
 | 
			
		||||
#elif defined(USE_HOST)
 | 
			
		||||
#include <openssl/evp.h>
 | 
			
		||||
#elif defined(USE_ARDUINO)
 | 
			
		||||
#include <SHA256.h>
 | 
			
		||||
#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
 | 
			
		||||
 
 | 
			
		||||
@@ -82,6 +82,16 @@ template<typename T> 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
 | 
			
		||||
 | 
			
		||||
///@}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user