14#define HANDSHAKE_STEP_LOG(func_name, err_code) \
15 ESP_LOGVV(TAG, "%s failed: %s", LOG_STR_ARG(LOG_STR(func_name)), LOG_STR_ARG(noise_err_to_logstr(err_code)))
33 const NoiseProtocolId nid = {
34 .prefix_id = NOISE_PREFIX_STANDARD,
35 .pattern_id = NOISE_PATTERN_NN,
36 .modifier_ids = {NOISE_MODIFIER_PSK0},
37 .dh_id = NOISE_DH_CURVE25519,
38 .cipher_id = NOISE_CIPHER_CHACHAPOLY,
39 .hash_id = NOISE_HASH_SHA256,
40 .hybrid_id = NOISE_DH_NONE,
44 int err = noise_handshakestate_new_by_id(&this->
handshake_, &nid, NOISE_ROLE_RESPONDER);
46 HANDSHAKE_STEP_LOG(
"noise_handshakestate_new_by_id", err);
52 err = noise_handshakestate_set_pre_shared_key(this->
handshake_, psk.data(), psk.size());
54 HANDSHAKE_STEP_LOG(
"noise_handshakestate_set_pre_shared_key", err);
57 err = noise_handshakestate_set_prologue(this->
handshake_, prologue, prologue_len);
59 HANDSHAKE_STEP_LOG(
"noise_handshakestate_set_prologue", err);
62#ifdef USE_NOISE_SPARE_EPHEMERAL
66 HANDSHAKE_STEP_LOG(
"noise_handshakestate_set_local_ephemeral", err);
69 err = noise_handshakestate_start(this->
handshake_);
71 HANDSHAKE_STEP_LOG(
"noise_handshakestate_start", err);
88 ESP_LOGVV(TAG,
"action() on uninitialized or split handshake");
93 case NOISE_ACTION_READ_MESSAGE:
95 case NOISE_ACTION_WRITE_MESSAGE:
97 case NOISE_ACTION_SPLIT:
101 ESP_LOGVV(TAG,
"Unexpected noise action %d",
raw);
109 noise_buffer_init(mbuf);
110 noise_buffer_set_input(mbuf, data,
len);
111 return noise_handshakestate_read_message(this->
handshake_, &mbuf,
nullptr);
120 noise_buffer_init(mbuf);
122 int err = noise_handshakestate_write_message(this->
handshake_, &mbuf,
nullptr);
132 send_cipher =
nullptr;
133 recv_cipher =
nullptr;
134 int err = noise_handshakestate_split(this->
handshake_, &send_cipher, &recv_cipher);
148 ESP_LOGE(TAG,
"Acquiring random bytes failed; rebooting");
Runs the CPU at 160 MHz while alive.
Leaves a stretch of the current loop pass out of the blocking warning.
void load_psk(psk_t &out) const
Copy the key out (flash-aware on ESP8266); all zeros when none is set.
int write_message(uint8_t *out, size_t capacity, size_t &out_len)
Produce the next handshake message into out; out_len receives its size and is zero on error.
~NoiseResponderHandshake()
Action action() const
ACTION_FAILED is the catch-all: returned before init(), after split() has released the state,...
int init(const NoiseContext &ctx, const uint8_t *prologue, size_t prologue_len)
Create and start the handshake with the context's PSK and the prologue.
int fail_init_(int err)
Release a half-initialized state so a failed init() leaves the object as if init() was never called.
NoiseHandshakeState * handshake_
int read_message(uint8_t *data, size_t len)
Process one received handshake message.
int split(NoiseCipherState *&send_cipher, NoiseCipherState *&recv_cipher)
Hand out the transport ciphers and free the handshake state.
std::vector< uint8_t > & out
ESPHOME_LOG_TAG(TAG, "noise")
std::array< uint8_t, 32 > psk_t
void noise_rand_bytes(void *output, size_t len)
int consume_spare_ephemeral(NoiseHandshakeState *state)
Move the key pair into a handshake that has not started and wipe the slot; 0 unless noise-c rejects i...
bool random_bytes(uint8_t *data, size_t len)
Generate len random bytes using the platform's secure RNG (hardware RNG or OS CSPRNG).
uint16_t uint16_t & capacity