ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
api_frame_helper_noise.h
Go to the documentation of this file.
1#pragma once
2#include "api_frame_helper.h"
3#ifdef USE_API
4#ifdef USE_API_NOISE
5#include "noise/protocol.h"
6#include "api_noise_context.h"
7
8namespace esphome::api {
9
10class APINoiseFrameHelper final : public APIFrameHelper {
11 public:
12 // Noise header structure:
13 // Pos 0: indicator (0x01)
14 // Pos 1-2: encrypted payload size (16-bit big-endian)
15 // Pos 3-6: encrypted type (16-bit) + data_len (16-bit)
16 // Pos 7+: actual payload data
17 static constexpr uint8_t HEADER_PADDING = 1 + 2 + 2 + 2; // indicator + size + type + data_len
18
19 APINoiseFrameHelper(std::unique_ptr<socket::Socket> socket, APINoiseContext &ctx)
20 : APIFrameHelper(std::move(socket)), ctx_(ctx) {
22 }
23 ~APINoiseFrameHelper() override;
24 APIError init() override;
25#ifdef USE_API_PLAINTEXT
26 // Take over a connection whose first bytes were consumed by a plaintext
27 // helper on an unprovisioned device (see APIError::PROTOCOL_SWITCH_TO_NOISE).
28 // Seeds the already-read header bytes and pumps the handshake state machine
29 // until it would block.
30 APIError init_from_handoff(const uint8_t *header, uint8_t header_len);
31#endif
32 APIError loop() override;
33 APIError read_packet(ReadPacketBuffer *buffer) override;
34 APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override;
35 APIError write_protobuf_messages(ProtoWriteBuffer buffer, std::span<const MessageInfo> messages) override;
36
37 protected:
46 APIError write_frame_(const uint8_t *data, uint16_t len);
47 APIError encrypt_noise_message_(uint8_t *buf_start, uint16_t payload_size, uint8_t message_type,
48 uint16_t &encrypted_len_out);
51 void send_explicit_handshake_reject_(const LogString *reason);
53 APIError handle_noise_error_(int err, const LogString *func_name, APIError api_err);
54
55 // Pointers first (4 bytes each)
56 NoiseHandshakeState *handshake_{nullptr};
57 NoiseCipherState *send_cipher_{nullptr};
58 NoiseCipherState *recv_cipher_{nullptr};
59
60 // Reference to noise context (4 bytes on 32-bit)
62
63 // Buffer for noise handshake prologue (released after handshake)
65
66 // NoiseProtocolId (size depends on implementation)
67 NoiseProtocolId nid_;
68
69 // Group small types together
70 // Fixed-size header buffer for noise protocol:
71 // 1 byte for indicator + 2 bytes for message size (16-bit value, not varint)
72 // Note: Maximum message size is UINT16_MAX (65535), with a limit of 128 bytes during handshake phase
73 uint8_t rx_header_buf_[3];
74 uint8_t rx_header_buf_len_ = 0;
75 // 4 bytes total, no padding
76};
77
78} // namespace esphome::api
79#endif // USE_API_NOISE
80#endif // USE_API
Byte buffer that skips zero-initialization on resize().
Definition api_buffer.h:36
APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override
APIError pump_handshake_()
Drive the handshake state machine until DATA, WOULD_BLOCK, or a fatal error.
APIError read_packet(ReadPacketBuffer *buffer) override
APIError handle_noise_error_(int err, const LogString *func_name, APIError api_err)
APIError write_protobuf_messages(ProtoWriteBuffer buffer, std::span< const MessageInfo > messages) override
APIError state_action_()
To be called from read/write methods.
APIError try_read_frame_()
Read a packet into the rx_buf_.
APIError loop() override
Run through handshake messages (if in that phase)
APIError encrypt_noise_message_(uint8_t *buf_start, uint16_t payload_size, uint8_t message_type, uint16_t &encrypted_len_out)
APINoiseFrameHelper(std::unique_ptr< socket::Socket > socket, APINoiseContext &ctx)
APIError handle_handshake_frame_error_(APIError aerr)
void send_explicit_handshake_reject_(const LogString *reason)
APIError write_frame_(const uint8_t *data, uint16_t len)
APIError init() override
Initialize the frame helper, returns OK if successful.
APIError init_handshake_()
Initiate the data structures for the handshake.
APIError init_from_handoff(const uint8_t *header, uint8_t header_len)
uint16_t type
const void size_t len
Definition hal.h:64
STL namespace.
uint32_t payload_size()