mirror of
https://github.com/esphome/esphome.git
synced 2025-09-22 05:02:23 +01:00
Merge branch 'sha256_ota' into integration
This commit is contained in:
@@ -39,32 +39,6 @@ void MD5Digest::add(const uint8_t *data, size_t len) { br_md5_update(&this->ctx_
|
|||||||
void MD5Digest::calculate() { br_md5_out(&this->ctx_, this->digest_); }
|
void MD5Digest::calculate() { br_md5_out(&this->ctx_, this->digest_); }
|
||||||
#endif // USE_RP2040
|
#endif // USE_RP2040
|
||||||
|
|
||||||
void MD5Digest::get_bytes(uint8_t *output) { memcpy(output, this->digest_, 16); }
|
|
||||||
|
|
||||||
void MD5Digest::get_hex(char *output) {
|
|
||||||
for (size_t i = 0; i < 16; i++) {
|
|
||||||
uint8_t byte = this->digest_[i];
|
|
||||||
output[i * 2] = format_hex_char(byte >> 4);
|
|
||||||
output[i * 2 + 1] = format_hex_char(byte & 0x0F);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MD5Digest::equals_bytes(const uint8_t *expected) {
|
|
||||||
for (size_t i = 0; i < 16; i++) {
|
|
||||||
if (expected[i] != this->digest_[i]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MD5Digest::equals_hex(const char *expected) {
|
|
||||||
uint8_t parsed[16];
|
|
||||||
if (!parse_hex(expected, parsed, 16))
|
|
||||||
return false;
|
|
||||||
return equals_bytes(parsed);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace md5
|
} // namespace md5
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
#endif
|
#endif
|
||||||
|
@@ -42,26 +42,11 @@ class MD5Digest : public HashBase {
|
|||||||
/// Compute the digest, based on the provided data.
|
/// Compute the digest, based on the provided data.
|
||||||
void calculate() override;
|
void calculate() override;
|
||||||
|
|
||||||
/// Retrieve the MD5 digest as bytes.
|
|
||||||
/// The output must be able to hold 16 bytes or more.
|
|
||||||
void get_bytes(uint8_t *output);
|
|
||||||
|
|
||||||
/// Retrieve the MD5 digest as hex characters.
|
|
||||||
/// The output must be able to hold 32 bytes or more.
|
|
||||||
void get_hex(char *output) override;
|
|
||||||
|
|
||||||
/// Get the size of the hex output (32 for MD5)
|
/// Get the size of the hex output (32 for MD5)
|
||||||
size_t get_hex_size() const override { return 32; }
|
size_t get_hex_size() const override { return 32; }
|
||||||
|
|
||||||
/// Compare the digest against a provided byte-encoded digest (16 bytes).
|
|
||||||
bool equals_bytes(const uint8_t *expected);
|
|
||||||
|
|
||||||
/// Compare the digest against a provided hex-encoded digest (32 bytes).
|
|
||||||
bool equals_hex(const char *expected);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
MD5_CTX_TYPE ctx_{};
|
MD5_CTX_TYPE ctx_{};
|
||||||
uint8_t digest_[16];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace md5
|
} // namespace md5
|
||||||
|
@@ -19,7 +19,7 @@ void SHA256::init() {
|
|||||||
|
|
||||||
void SHA256::add(const uint8_t *data, size_t len) { mbedtls_sha256_update(&this->ctx_, data, len); }
|
void SHA256::add(const uint8_t *data, size_t len) { mbedtls_sha256_update(&this->ctx_, data, len); }
|
||||||
|
|
||||||
void SHA256::calculate() { mbedtls_sha256_finish(&this->ctx_, this->hash_); }
|
void SHA256::calculate() { mbedtls_sha256_finish(&this->ctx_, this->digest_); }
|
||||||
|
|
||||||
#elif defined(USE_ESP8266) || defined(USE_RP2040)
|
#elif defined(USE_ESP8266) || defined(USE_RP2040)
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ void SHA256::add(const uint8_t *data, size_t len) { br_sha256_update(&this->ctx_
|
|||||||
|
|
||||||
void SHA256::calculate() {
|
void SHA256::calculate() {
|
||||||
if (!this->calculated_) {
|
if (!this->calculated_) {
|
||||||
br_sha256_out(&this->ctx_, this->hash_);
|
br_sha256_out(&this->ctx_, this->digest_);
|
||||||
this->calculated_ = true;
|
this->calculated_ = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ void SHA256::calculate() {
|
|||||||
}
|
}
|
||||||
if (!this->calculated_) {
|
if (!this->calculated_) {
|
||||||
unsigned int len = 32;
|
unsigned int len = 32;
|
||||||
EVP_DigestFinal_ex(this->ctx_, this->hash_, &len);
|
EVP_DigestFinal_ex(this->ctx_, this->digest_, &len);
|
||||||
this->calculated_ = true;
|
this->calculated_ = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,32 +78,12 @@ void SHA256::calculate() {
|
|||||||
#error "SHA256 not supported on this platform"
|
#error "SHA256 not supported on this platform"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void SHA256::get_bytes(uint8_t *output) { memcpy(output, this->hash_, 32); }
|
|
||||||
|
|
||||||
void SHA256::get_hex(char *output) {
|
|
||||||
for (size_t i = 0; i < 32; i++) {
|
|
||||||
uint8_t byte = this->hash_[i];
|
|
||||||
output[i * 2] = format_hex_char(byte >> 4);
|
|
||||||
output[i * 2 + 1] = format_hex_char(byte & 0x0F);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string SHA256::get_hex_string() {
|
std::string SHA256::get_hex_string() {
|
||||||
char buf[65];
|
char buf[65];
|
||||||
this->get_hex(buf);
|
this->get_hex(buf);
|
||||||
return std::string(buf);
|
return std::string(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SHA256::equals_bytes(const uint8_t *expected) { return memcmp(this->hash_, expected, 32) == 0; }
|
|
||||||
|
|
||||||
bool SHA256::equals_hex(const char *expected) {
|
|
||||||
uint8_t parsed[32];
|
|
||||||
if (!parse_hex(expected, parsed, 32)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return this->equals_bytes(parsed);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace esphome::sha256
|
} // namespace esphome::sha256
|
||||||
|
|
||||||
#endif // Platform check
|
#endif // Platform check
|
||||||
|
@@ -33,27 +33,19 @@ class SHA256 : public esphome::HashBase {
|
|||||||
|
|
||||||
void calculate() override;
|
void calculate() override;
|
||||||
|
|
||||||
void get_bytes(uint8_t *output);
|
|
||||||
void get_hex(char *output) override;
|
|
||||||
std::string get_hex_string();
|
std::string get_hex_string();
|
||||||
|
|
||||||
/// Get the size of the hex output (64 for SHA256)
|
/// Get the size of the hex output (64 for SHA256)
|
||||||
size_t get_hex_size() const override { return 64; }
|
size_t get_hex_size() const override { return 64; }
|
||||||
|
|
||||||
bool equals_bytes(const uint8_t *expected);
|
|
||||||
bool equals_hex(const char *expected);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
|
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
|
||||||
mbedtls_sha256_context ctx_{};
|
mbedtls_sha256_context ctx_{};
|
||||||
uint8_t hash_[32];
|
|
||||||
#elif defined(USE_ESP8266) || defined(USE_RP2040)
|
#elif defined(USE_ESP8266) || defined(USE_RP2040)
|
||||||
br_sha256_context ctx_{};
|
br_sha256_context ctx_{};
|
||||||
uint8_t hash_[32];
|
|
||||||
bool calculated_{false};
|
bool calculated_{false};
|
||||||
#elif defined(USE_HOST)
|
#elif defined(USE_HOST)
|
||||||
EVP_MD_CTX *ctx_{nullptr};
|
EVP_MD_CTX *ctx_{nullptr};
|
||||||
uint8_t hash_[32];
|
|
||||||
bool calculated_{false};
|
bool calculated_{false};
|
||||||
#else
|
#else
|
||||||
#error "SHA256 not supported on this platform"
|
#error "SHA256 not supported on this platform"
|
||||||
|
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <cstring>
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|
||||||
@@ -20,11 +22,43 @@ class HashBase {
|
|||||||
/// Compute the hash based on provided data
|
/// Compute the hash based on provided data
|
||||||
virtual void calculate() = 0;
|
virtual void calculate() = 0;
|
||||||
|
|
||||||
|
/// Retrieve the hash as bytes
|
||||||
|
void get_bytes(uint8_t *output) {
|
||||||
|
const size_t hash_bytes = this->get_hex_size() / 2;
|
||||||
|
memcpy(output, this->digest_, hash_bytes);
|
||||||
|
}
|
||||||
|
|
||||||
/// Retrieve the hash as hex characters
|
/// Retrieve the hash as hex characters
|
||||||
virtual void get_hex(char *output) = 0;
|
void get_hex(char *output) {
|
||||||
|
const size_t hash_bytes = this->get_hex_size() / 2;
|
||||||
|
for (size_t i = 0; i < hash_bytes; i++) {
|
||||||
|
uint8_t byte = this->digest_[i];
|
||||||
|
output[i * 2] = format_hex_char(byte >> 4);
|
||||||
|
output[i * 2 + 1] = format_hex_char(byte & 0x0F);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compare the hash against a provided byte-encoded hash
|
||||||
|
bool equals_bytes(const uint8_t *expected) {
|
||||||
|
const size_t hash_bytes = this->get_hex_size() / 2;
|
||||||
|
return memcmp(this->digest_, expected, hash_bytes) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compare the hash against a provided hex-encoded hash
|
||||||
|
bool equals_hex(const char *expected) {
|
||||||
|
const size_t hash_bytes = this->get_hex_size() / 2;
|
||||||
|
uint8_t parsed[32]; // Max size for SHA256
|
||||||
|
if (!parse_hex(expected, parsed, hash_bytes)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return this->equals_bytes(parsed);
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the size of the hex output (32 for MD5, 64 for SHA256)
|
/// Get the size of the hex output (32 for MD5, 64 for SHA256)
|
||||||
virtual size_t get_hex_size() const = 0;
|
virtual size_t get_hex_size() const = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
uint8_t digest_[32]; // Common digest storage, sized for largest hash (SHA256)
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
Reference in New Issue
Block a user