ESPHome 2026.5.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 APIError loop() override;
26 APIError read_packet(ReadPacketBuffer *buffer) override;
27 APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override;
28 APIError write_protobuf_messages(ProtoWriteBuffer buffer, std::span<const MessageInfo> messages) override;
29
30 protected:
38 APIError write_frame_(const uint8_t *data, uint16_t len);
39 APIError encrypt_noise_message_(uint8_t *buf_start, uint16_t payload_size, uint8_t message_type,
40 uint16_t &encrypted_len_out);
43 void send_explicit_handshake_reject_(const LogString *reason);
45 APIError handle_noise_error_(int err, const LogString *func_name, APIError api_err);
46
47 // Pointers first (4 bytes each)
48 NoiseHandshakeState *handshake_{nullptr};
49 NoiseCipherState *send_cipher_{nullptr};
50 NoiseCipherState *recv_cipher_{nullptr};
51
52 // Reference to noise context (4 bytes on 32-bit)
54
55 // Buffer for noise handshake prologue (released after handshake)
57
58 // NoiseProtocolId (size depends on implementation)
59 NoiseProtocolId nid_;
60
61 // Group small types together
62 // Fixed-size header buffer for noise protocol:
63 // 1 byte for indicator + 2 bytes for message size (16-bit value, not varint)
64 // Note: Maximum message size is UINT16_MAX (65535), with a limit of 128 bytes during handshake phase
65 uint8_t rx_header_buf_[3];
66 uint8_t rx_header_buf_len_ = 0;
67 // 4 bytes total, no padding
68};
69
70} // namespace esphome::api
71#endif // USE_API_NOISE
72#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 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.
uint16_t type
std::string size_t len
Definition helpers.h:1045
uint32_t payload_size()