esphome-api-client 0.5.0
Modern C++17 client for the ESPHome native API (plaintext + Noise)
Loading...
Searching...
No Matches
noise_frame_helper.hpp
Go to the documentation of this file.
1#pragma once
2
5
11
12#include <cstddef>
13#include <cstdint>
14#include <string>
15
16namespace esphome::api {
17
21 std::string psk_base64;
23 std::string expected_name;
24};
25
30class NoiseFrameHelper final : public FrameHelper {
31public:
33 static constexpr std::uint8_t indicator = 0x01;
34
37 explicit NoiseFrameHelper(const NoiseConfig& config);
38
39 void encode(std::uint32_t msg_type, ByteView payload, ByteBuffer& out) override;
40 void feed(ByteView data) override;
41 FrameStatus next(std::uint32_t& out_type, ByteView& out_payload) override;
42
43 [[nodiscard]] bool needs_handshake() const override {
44 return true;
45 }
47 void set_handshake_handlers(HandshakeReady on_ready, HandshakeError on_error) override;
48
49 [[nodiscard]] const std::string& server_name() const noexcept {
50 return server_name_;
51 }
52
53private:
54 enum class State { ExpectServerHello, ExpectHandshake, Ready, Failed };
55
56 FrameStatus next_raw(ByteView& out_payload);
57 void compact();
58 void handle_server_hello(ByteView payload);
59 void handle_server_handshake(ByteView payload);
60 void report_error(const std::string& what);
61
63 std::string expected_name_;
64 std::string server_name_;
65
66 noise::NoiseHandshake handshake_;
67 noise::CipherState encrypt_;
68 noise::CipherState decrypt_;
69 State state_ = State::ExpectServerHello;
70
71 HandshakeReady on_ready_;
72 HandshakeError on_error_;
73
74 ByteBuffer buffer_;
75 std::size_t consumed_ = 0;
76 ByteBuffer plaintext_;
77};
78
79} // namespace esphome::api
Byte buffer / view aliases and protobuf base-128 varint codec.
Noise CipherState: a keyed AEAD with an incrementing nonce.
Non-owning view over a contiguous run of bytes (a C++17 stand-in for std::span<const std::uint8_t>).
Definition bytes.hpp:17
Stateful codec sitting between the transport and the connection.
Definition frame_helper.hpp:30
std::function< void()> HandshakeReady
Definition frame_helper.hpp:58
std::function< void(const std::string &what)> HandshakeError
Definition frame_helper.hpp:59
Encrypted frame codec.
Definition noise_frame_helper.hpp:30
NoiseFrameHelper(const NoiseConfig &config)
Construct from config.
bool needs_handshake() const override
True if the transport requires a handshake before application messages (and before the connection sho...
Definition noise_frame_helper.hpp:43
const std::string & server_name() const noexcept
Definition noise_frame_helper.hpp:49
void feed(ByteView data) override
Append freshly received bytes to the internal reassembly buffer.
void set_handshake_handlers(HandshakeReady on_ready, HandshakeError on_error) override
Register callbacks fired when the handshake completes or fails.
ByteBuffer connect_bytes() override
Bytes to transmit immediately after the TCP connection is established (the client's first handshake f...
FrameStatus next(std::uint32_t &out_type, ByteView &out_payload) override
Try to extract the next complete frame.
void encode(std::uint32_t msg_type, ByteView payload, ByteBuffer &out) override
Serialize a message (numeric type id + protobuf payload) to wire bytes, appending them to out.
static constexpr std::uint8_t indicator
Indicator byte prefixing every Noise frame.
Definition noise_frame_helper.hpp:33
Abstract frame codec: turns a byte stream into ESPHome message frames and back, independent of the un...
std::array< std::uint8_t, key_len > SymmetricKey
Definition primitives.hpp:21
Definition bytes.hpp:10
@ Failed
Terminated by an error.
FrameStatus
Result of attempting to pull a frame from the reassembly buffer.
Definition frame_helper.hpp:17
std::vector< std::uint8_t > ByteBuffer
Owning, growable byte buffer.
Definition bytes.hpp:13
Noise_NNpsk0_25519_ChaChaPoly_SHA256 handshake (initiator + responder).
Noise cryptographic primitives (SHA-256, HMAC, HKDF, X25519, ChaCha20- Poly1305, base64).
Configuration for an encrypted connection.
Definition noise_frame_helper.hpp:19
std::string expected_name
Optional expected device name, verified against the server hello.
Definition noise_frame_helper.hpp:23
std::string psk_base64
Base64-encoded 32-byte pre-shared key (the device's api.encryption.key).
Definition noise_frame_helper.hpp:21