Use progmem_memcpy for the key and prologue copies and load the saved psk through a temp

This commit is contained in:
J. Nick Koston
2026-09-05 12:05:18 +02:00
parent 4f7ba7fa29
commit 3601a1aad8
3 changed files with 8 additions and 11 deletions
+4 -1
View File
@@ -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
@@ -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 <cstring>
@@ -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));
+2 -5
View File
@@ -1,5 +1,6 @@
#include "noise.h"
#ifdef USE_NOISE
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include <algorithm>
@@ -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) {