diff --git a/esphome/components/sha256/__init__.py b/esphome/components/sha256/__init__.py index e24da86e25..f77820744b 100644 --- a/esphome/components/sha256/__init__.py +++ b/esphome/components/sha256/__init__.py @@ -1,5 +1,19 @@ import esphome.codegen as cg +from esphome.core import CORE, IS_MACOS CODEOWNERS = ["@esphome/core"] sha256_ns = cg.esphome_ns.namespace("sha256") + + +async def to_code(config): + # Add OpenSSL library for host platform + if CORE.is_host: + if IS_MACOS: + # macOS needs special handling for Homebrew OpenSSL + cg.add_build_flag("-I/opt/homebrew/opt/openssl/include") + cg.add_build_flag("-L/opt/homebrew/opt/openssl/lib") + cg.add_build_flag("-lcrypto") + else: + # Linux and other Unix systems usually have OpenSSL in standard paths + cg.add_build_flag("-lcrypto") diff --git a/esphome/components/sha256/sha256.cpp b/esphome/components/sha256/sha256.cpp index 71e4045499..6fa17bb7c0 100644 --- a/esphome/components/sha256/sha256.cpp +++ b/esphome/components/sha256/sha256.cpp @@ -1,7 +1,7 @@ #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) +#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_HOST) #include "esphome/core/helpers.h" #include @@ -67,6 +67,35 @@ void SHA256::calculate() { } } +#elif defined(USE_HOST) + +SHA256::~SHA256() = default; + +void SHA256::init() { + if (!this->ctx_) { + this->ctx_ = std::make_unique(); + } + SHA256_Init(&this->ctx_->ctx); + this->ctx_->calculated = false; +} + +void SHA256::add(const uint8_t *data, size_t len) { + if (!this->ctx_) { + this->init(); + } + SHA256_Update(&this->ctx_->ctx, data, len); +} + +void SHA256::calculate() { + if (!this->ctx_) { + this->init(); + } + if (!this->ctx_->calculated) { + SHA256_Final(this->ctx_->hash, &this->ctx_->ctx); + this->ctx_->calculated = true; + } +} + #elif defined(USE_ARDUINO) SHA256::~SHA256() = default; diff --git a/esphome/components/sha256/sha256.h b/esphome/components/sha256/sha256.h index 2a7aa72183..246a7ca891 100644 --- a/esphome/components/sha256/sha256.h +++ b/esphome/components/sha256/sha256.h @@ -3,7 +3,7 @@ #include "esphome/core/defines.h" // Only define SHA256 on platforms that support it -#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) +#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_HOST) #include #include @@ -13,6 +13,8 @@ #include "mbedtls/sha256.h" #elif defined(USE_ESP8266) || defined(USE_RP2040) #include +#elif defined(USE_HOST) +#include #elif defined(USE_ARDUINO) #include #endif @@ -50,6 +52,12 @@ class SHA256 { uint8_t hash[32]; bool calculated{false}; }; +#elif defined(USE_HOST) + struct SHA256Context { + SHA256_CTX ctx; + uint8_t hash[32]; + bool calculated{false}; + }; #elif defined(USE_ARDUINO) struct SHA256Context { ::SHA256 sha; diff --git a/tests/components/sha512/common.yaml b/tests/components/sha512/common.yaml new file mode 100644 index 0000000000..72adf30501 --- /dev/null +++ b/tests/components/sha512/common.yaml @@ -0,0 +1,5 @@ +wifi: + ssid: MySSID + password: password1 + +sha256: diff --git a/tests/components/sha512/test.bk72xx-ard.yaml b/tests/components/sha512/test.bk72xx-ard.yaml new file mode 100644 index 0000000000..25cb37a0b4 --- /dev/null +++ b/tests/components/sha512/test.bk72xx-ard.yaml @@ -0,0 +1,2 @@ +packages: + common: !include common.yaml diff --git a/tests/components/sha512/test.esp32-idf.yaml b/tests/components/sha512/test.esp32-idf.yaml new file mode 100644 index 0000000000..dade44d145 --- /dev/null +++ b/tests/components/sha512/test.esp32-idf.yaml @@ -0,0 +1 @@ +<<: !include common.yaml diff --git a/tests/components/sha512/test.esp8266-ard.yaml b/tests/components/sha512/test.esp8266-ard.yaml new file mode 100644 index 0000000000..dade44d145 --- /dev/null +++ b/tests/components/sha512/test.esp8266-ard.yaml @@ -0,0 +1 @@ +<<: !include common.yaml diff --git a/tests/components/sha512/test.host.yaml b/tests/components/sha512/test.host.yaml new file mode 100644 index 0000000000..dade44d145 --- /dev/null +++ b/tests/components/sha512/test.host.yaml @@ -0,0 +1 @@ +<<: !include common.yaml diff --git a/tests/components/sha512/test.rp2040-ard.yaml b/tests/components/sha512/test.rp2040-ard.yaml new file mode 100644 index 0000000000..dade44d145 --- /dev/null +++ b/tests/components/sha512/test.rp2040-ard.yaml @@ -0,0 +1 @@ +<<: !include common.yaml