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

use evp interface

This commit is contained in:
J. Nick Koston
2025-09-21 10:44:09 -06:00
parent d1fb3336f0
commit ada1b00cad
2 changed files with 16 additions and 6 deletions

View File

@@ -69,13 +69,22 @@ void SHA256::calculate() {
#elif defined(USE_HOST) #elif defined(USE_HOST)
SHA256::~SHA256() = default; SHA256::~SHA256() {
if (this->ctx_ && this->ctx_->ctx) {
EVP_MD_CTX_free(this->ctx_->ctx);
this->ctx_->ctx = nullptr;
}
}
void SHA256::init() { void SHA256::init() {
if (!this->ctx_) { if (!this->ctx_) {
this->ctx_ = std::make_unique<SHA256Context>(); this->ctx_ = std::make_unique<SHA256Context>();
} }
SHA256_Init(&this->ctx_->ctx); if (this->ctx_->ctx) {
EVP_MD_CTX_free(this->ctx_->ctx);
}
this->ctx_->ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(this->ctx_->ctx, EVP_sha256(), nullptr);
this->ctx_->calculated = false; this->ctx_->calculated = false;
} }
@@ -83,7 +92,7 @@ void SHA256::add(const uint8_t *data, size_t len) {
if (!this->ctx_) { if (!this->ctx_) {
this->init(); this->init();
} }
SHA256_Update(&this->ctx_->ctx, data, len); EVP_DigestUpdate(this->ctx_->ctx, data, len);
} }
void SHA256::calculate() { void SHA256::calculate() {
@@ -91,7 +100,8 @@ void SHA256::calculate() {
this->init(); this->init();
} }
if (!this->ctx_->calculated) { if (!this->ctx_->calculated) {
SHA256_Final(this->ctx_->hash, &this->ctx_->ctx); unsigned int len = 32;
EVP_DigestFinal_ex(this->ctx_->ctx, this->ctx_->hash, &len);
this->ctx_->calculated = true; this->ctx_->calculated = true;
} }
} }

View File

@@ -14,7 +14,7 @@
#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/sha.h> #include <openssl/evp.h>
#elif defined(USE_ARDUINO) #elif defined(USE_ARDUINO)
#include <SHA256.h> #include <SHA256.h>
#endif #endif
@@ -54,7 +54,7 @@ class SHA256 {
}; };
#elif defined(USE_HOST) #elif defined(USE_HOST)
struct SHA256Context { struct SHA256Context {
SHA256_CTX ctx; EVP_MD_CTX *ctx{nullptr};
uint8_t hash[32]; uint8_t hash[32];
bool calculated{false}; bool calculated{false};
}; };