84 lines
1.9 KiB
C++
84 lines
1.9 KiB
C++
#include "sha256.h"
|
|
|
|
// Only compile SHA256 implementation on platforms that support it
|
|
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_HOST)
|
|
|
|
#include "esphome/core/helpers.h"
|
|
#include <cstring>
|
|
|
|
namespace esphome::sha256 {
|
|
|
|
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
|
|
|
|
SHA256::~SHA256() { mbedtls_sha256_free(&this->ctx_); }
|
|
|
|
void SHA256::init() {
|
|
mbedtls_sha256_init(&this->ctx_);
|
|
mbedtls_sha256_starts(&this->ctx_, 0); // 0 = SHA256, not SHA224
|
|
}
|
|
|
|
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->digest_); }
|
|
|
|
#elif defined(USE_ESP8266) || defined(USE_RP2040)
|
|
|
|
SHA256::~SHA256() = default;
|
|
|
|
void SHA256::init() {
|
|
br_sha256_init(&this->ctx_);
|
|
this->calculated_ = false;
|
|
}
|
|
|
|
void SHA256::add(const uint8_t *data, size_t len) { br_sha256_update(&this->ctx_, data, len); }
|
|
|
|
void SHA256::calculate() {
|
|
if (!this->calculated_) {
|
|
br_sha256_out(&this->ctx_, this->digest_);
|
|
this->calculated_ = true;
|
|
}
|
|
}
|
|
|
|
#elif defined(USE_HOST)
|
|
|
|
SHA256::~SHA256() {
|
|
if (this->ctx_) {
|
|
EVP_MD_CTX_free(this->ctx_);
|
|
}
|
|
}
|
|
|
|
void SHA256::init() {
|
|
if (this->ctx_) {
|
|
EVP_MD_CTX_free(this->ctx_);
|
|
}
|
|
this->ctx_ = EVP_MD_CTX_new();
|
|
EVP_DigestInit_ex(this->ctx_, EVP_sha256(), nullptr);
|
|
this->calculated_ = false;
|
|
}
|
|
|
|
void SHA256::add(const uint8_t *data, size_t len) {
|
|
if (!this->ctx_) {
|
|
this->init();
|
|
}
|
|
EVP_DigestUpdate(this->ctx_, data, len);
|
|
}
|
|
|
|
void SHA256::calculate() {
|
|
if (!this->ctx_) {
|
|
this->init();
|
|
}
|
|
if (!this->calculated_) {
|
|
unsigned int len = 32;
|
|
EVP_DigestFinal_ex(this->ctx_, this->digest_, &len);
|
|
this->calculated_ = true;
|
|
}
|
|
}
|
|
|
|
#else
|
|
#error "SHA256 not supported on this platform"
|
|
#endif
|
|
|
|
} // namespace esphome::sha256
|
|
|
|
#endif // Platform check
|