diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index f9069ab5c0..040fe0ead2 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -586,8 +586,11 @@ bool APIServer::load_and_apply_noise_psk_() { #ifdef USE_API_NOISE_PSK_FROM_YAML return false; #else - if (!this->noise_pref_.load(&this->saved_psk_)) + // Load into a temp so a failed read cannot disturb the key in use + SavedNoisePsk loaded{}; + if (!this->noise_pref_.load(&loaded)) return false; + this->saved_psk_ = loaded; this->noise_ctx_.set_psk(this->saved_psk_.psk.data()); return true; #endif diff --git a/esphome/components/esphome/ota/ota_esphome_noise.cpp b/esphome/components/esphome/ota/ota_esphome_noise.cpp index c33f97cdff..fffe866e98 100644 --- a/esphome/components/esphome/ota/ota_esphome_noise.cpp +++ b/esphome/components/esphome/ota/ota_esphome_noise.cpp @@ -3,6 +3,7 @@ #ifdef USE_OTA_ENCRYPTION #include "esphome/components/noise/noise.h" #include "esphome/components/ota/ota_backend.h" +#include "esphome/core/hal.h" #include "esphome/core/log.h" #include @@ -49,11 +50,7 @@ bool ESPHomeOTAComponent::noise_start_session_(uint8_t server_feature_flags) { static constexpr size_t PROLOGUE_FEATURE_ACK_LEN = 2; // OTA_RESPONSE_FEATURE_FLAGS + server flags uint8_t prologue[OTA_NOISE_PROLOGUE_INIT_LEN + sizeof(MAGIC_BYTES) + PROLOGUE_ACK_LEN + PROLOGUE_CLIENT_FEATURES_LEN + PROLOGUE_FEATURE_ACK_LEN]; -#ifdef USE_ESP8266 - memcpy_P(prologue, OTA_NOISE_PROLOGUE_INIT, OTA_NOISE_PROLOGUE_INIT_LEN); -#else - std::memcpy(prologue, OTA_NOISE_PROLOGUE_INIT, OTA_NOISE_PROLOGUE_INIT_LEN); -#endif + progmem_memcpy(prologue, OTA_NOISE_PROLOGUE_INIT, OTA_NOISE_PROLOGUE_INIT_LEN); uint8_t *p = prologue + OTA_NOISE_PROLOGUE_INIT_LEN; // Magic bytes, already validated in MAGIC_READ std::memcpy(p, MAGIC_BYTES, sizeof(MAGIC_BYTES)); diff --git a/esphome/components/noise/noise.cpp b/esphome/components/noise/noise.cpp index 54c1bb315b..80a8cfc0dc 100644 --- a/esphome/components/noise/noise.cpp +++ b/esphome/components/noise/noise.cpp @@ -1,5 +1,6 @@ #include "noise.h" #ifdef USE_NOISE +#include "esphome/core/hal.h" #include "esphome/core/log.h" #include @@ -29,11 +30,7 @@ void NoiseContext::load_psk(psk_t &out) const { out.fill(0); return; } -#ifdef USE_ESP8266 - memcpy_P(out.data(), this->psk_, out.size()); -#else - std::memcpy(out.data(), this->psk_, out.size()); -#endif + progmem_memcpy(out.data(), this->psk_, out.size()); } const LogString *noise_err_to_logstr(int err) {