esphome-api-client 0.5.0
Modern C++17 client for the ESPHome native API (plaintext + Noise)
Loading...
Searching...
No Matches
primitives.hpp
Go to the documentation of this file.
1#pragma once
2
8
10
11#include <array>
12#include <cstdint>
13#include <string>
14
15namespace esphome::api::noise {
16
17inline constexpr std::size_t hash_len = 32;
18inline constexpr std::size_t key_len = 32;
19
20using Hash = std::array<std::uint8_t, hash_len>;
21using SymmetricKey = std::array<std::uint8_t, key_len>;
22using PublicKey = std::array<std::uint8_t, key_len>;
23using PrivateKey = std::array<std::uint8_t, key_len>;
24
27
30
33void hkdf(const Hash& chaining_key,
34 ByteView input_key_material,
35 int num_outputs,
36 Hash& o1,
37 Hash& o2,
38 Hash& o3);
39
41PublicKey x25519_base(const PrivateKey& private_key);
42
44SymmetricKey x25519(const PrivateKey& private_key, const PublicKey& peer_public);
45
47void generate_keypair(PrivateKey& private_key, PublicKey& public_key);
48
53aead_encrypt(const SymmetricKey& key, std::uint64_t nonce, ByteView ad, ByteView plaintext);
54
56bool aead_decrypt(const SymmetricKey& key,
57 std::uint64_t nonce,
58 ByteView ad,
59 ByteView ciphertext,
60 ByteBuffer& out_plaintext);
61
63bool base64_decode(const std::string& text, ByteBuffer& out);
64
65} // namespace esphome::api::noise
Byte buffer / view aliases and protobuf base-128 varint codec.
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
Definition cipher_state.hpp:11
constexpr std::size_t key_len
Symmetric / DH key size.
Definition primitives.hpp:18
PublicKey x25519_base(const PrivateKey &private_key)
X25519 public key for a private scalar.
bool aead_decrypt(const SymmetricKey &key, std::uint64_t nonce, ByteView ad, ByteView ciphertext, ByteBuffer &out_plaintext)
AEAD ChaCha20-Poly1305 (IETF) decryption. Returns false on auth failure.
std::array< std::uint8_t, key_len > PublicKey
Definition primitives.hpp:22
constexpr std::size_t hash_len
SHA-256 digest / key size.
Definition primitives.hpp:17
std::array< std::uint8_t, key_len > SymmetricKey
Definition primitives.hpp:21
void generate_keypair(PrivateKey &private_key, PublicKey &public_key)
Generate a fresh X25519 keypair from the system CSPRNG.
SymmetricKey x25519(const PrivateKey &private_key, const PublicKey &peer_public)
X25519 shared secret. Throws EncryptionError on an all-zero (degenerate) result.
Hash sha256(ByteView data)
SHA-256 of data.
Hash hmac_sha256(ByteView key, ByteView data)
HMAC-SHA-256 with key over data.
bool base64_decode(const std::string &text, ByteBuffer &out)
Decode standard base64. Returns false on malformed input.
ByteBuffer aead_encrypt(const SymmetricKey &key, std::uint64_t nonce, ByteView ad, ByteView plaintext)
AEAD ChaCha20-Poly1305 (IETF) encryption.
void hkdf(const Hash &chaining_key, ByteView input_key_material, int num_outputs, Hash &o1, Hash &o2, Hash &o3)
Noise HKDF: derive num_outputs (1..3) 32-byte values from chaining_key and input_key_material.
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