esphome-api-client 0.5.0
Modern C++17 client for the ESPHome native API (plaintext + Noise)
Loading...
Searching...
No Matches
noise_handshake.hpp
Go to the documentation of this file.
1#pragma once
2
5
10
11namespace esphome::api::noise {
12
14inline constexpr auto protocol_name = "Noise_NNpsk0_25519_ChaChaPoly_SHA256";
15
22public:
23 enum class Role { Initiator, Responder };
24
26 void initialize(Role role, const SymmetricKey& psk, ByteView prologue);
27
30
33 bool read_message(ByteView message, ByteBuffer& out_payload);
34
35 [[nodiscard]] bool is_complete() const noexcept {
36 return complete_;
37 }
38
41 void split(CipherState& send, CipherState& recv);
42
43 [[nodiscard]] const Hash& handshake_hash() const noexcept {
44 return symmetric_.handshake_hash();
45 }
46
48 void set_ephemeral_for_testing(const PrivateKey& private_key);
49
50private:
51 void ensure_ephemeral();
52
53 Role role_ = Role::Initiator;
54 SymmetricKey psk_{};
55 SymmetricState symmetric_;
56 PrivateKey ephemeral_private_{};
57 PublicKey ephemeral_public_{};
58 PublicKey remote_ephemeral_{};
59 bool have_ephemeral_ = false;
60 bool complete_ = false;
61};
62
63} // namespace esphome::api::noise
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
Wraps a symmetric key plus a monotonically increasing nonce, per the Noise CipherState object.
Definition cipher_state.hpp:15
Drives the two-message NNpsk0 handshake.
Definition noise_handshake.hpp:21
bool is_complete() const noexcept
Definition noise_handshake.hpp:35
bool read_message(ByteView message, ByteBuffer &out_payload)
Read the next handshake message into out_payload.
const Hash & handshake_hash() const noexcept
Definition noise_handshake.hpp:43
ByteBuffer write_message(ByteView payload=ByteView{})
Write the next handshake message (Initiator: message 1; Responder: 2).
void split(CipherState &send, CipherState &recv)
After completion, derive the transport cipher states for this role (send is what this side encrypts w...
Role
Definition noise_handshake.hpp:23
void initialize(Role role, const SymmetricKey &psk, ByteView prologue)
Set up with the role, the 32-byte PSK and the prologue.
void set_ephemeral_for_testing(const PrivateKey &private_key)
Test hook: force a fixed ephemeral private key (for known-answer vectors).
Implements the Noise SymmetricState object: it accumulates the handshake transcript hash h,...
Definition symmetric_state.hpp:18
const Hash & handshake_hash() const noexcept
Definition symmetric_state.hpp:42
Definition cipher_state.hpp:11
constexpr auto protocol_name
Noise protocol name for the ESPHome native API.
Definition noise_handshake.hpp:14
std::array< std::uint8_t, key_len > PublicKey
Definition primitives.hpp:22
std::array< std::uint8_t, key_len > SymmetricKey
Definition primitives.hpp:21
std::array< std::uint8_t, key_len > PrivateKey
Definition primitives.hpp:23
std::array< std::uint8_t, hash_len > Hash
Definition primitives.hpp:20
std::vector< std::uint8_t > ByteBuffer
Owning, growable byte buffer.
Definition bytes.hpp:13
Noise cryptographic primitives (SHA-256, HMAC, HKDF, X25519, ChaCha20- Poly1305, base64).
Noise SymmetricState: chaining key + transcript hash + CipherState.