20static const char *
const TAG =
"api.noise";
22static const char PROLOGUE_INIT[] PROGMEM =
"NoiseAPIInit";
24static const char *
const PROLOGUE_INIT =
"NoiseAPIInit";
26static constexpr size_t PROLOGUE_INIT_LEN = 12;
29static constexpr size_t API_MAX_LOG_BYTES = 168;
31#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
32#define HELPER_LOG(msg, ...) \
34 char peername_buf[socket::SOCKADDR_STR_LEN]; \
35 this->get_peername_to(peername_buf); \
36 ESP_LOGVV(TAG, "%s (%s): " msg, this->client_name_, peername_buf, ##__VA_ARGS__); \
39#define HELPER_LOG(msg, ...) ((void) 0)
42#ifdef HELPER_LOG_PACKETS
43#define LOG_PACKET_RECEIVED(buffer) \
45 char hex_buf_[format_hex_pretty_size(API_MAX_LOG_BYTES)]; \
46 ESP_LOGVV(TAG, "Received frame: %s", \
47 format_hex_pretty_to(hex_buf_, (buffer).data(), \
48 (buffer).size() < API_MAX_LOG_BYTES ? (buffer).size() : API_MAX_LOG_BYTES)); \
50#define LOG_PACKET_SENDING(data, len) \
52 char hex_buf_[format_hex_pretty_size(API_MAX_LOG_BYTES)]; \
53 ESP_LOGVV(TAG, "Sending raw: %s", \
54 format_hex_pretty_to(hex_buf_, data, (len) < API_MAX_LOG_BYTES ? (len) : API_MAX_LOG_BYTES)); \
57#define LOG_PACKET_RECEIVED(buffer) ((void) 0)
58#define LOG_PACKET_SENDING(data, len) ((void) 0)
63 if (err == NOISE_ERROR_NO_MEMORY)
64 return LOG_STR(
"NO_MEMORY");
65 if (err == NOISE_ERROR_UNKNOWN_ID)
66 return LOG_STR(
"UNKNOWN_ID");
67 if (err == NOISE_ERROR_UNKNOWN_NAME)
68 return LOG_STR(
"UNKNOWN_NAME");
69 if (err == NOISE_ERROR_MAC_FAILURE)
70 return LOG_STR(
"MAC_FAILURE");
71 if (err == NOISE_ERROR_NOT_APPLICABLE)
72 return LOG_STR(
"NOT_APPLICABLE");
73 if (err == NOISE_ERROR_SYSTEM)
74 return LOG_STR(
"SYSTEM");
75 if (err == NOISE_ERROR_REMOTE_KEY_REQUIRED)
76 return LOG_STR(
"REMOTE_KEY_REQUIRED");
77 if (err == NOISE_ERROR_LOCAL_KEY_REQUIRED)
78 return LOG_STR(
"LOCAL_KEY_REQUIRED");
79 if (err == NOISE_ERROR_PSK_REQUIRED)
80 return LOG_STR(
"PSK_REQUIRED");
81 if (err == NOISE_ERROR_INVALID_LENGTH)
82 return LOG_STR(
"INVALID_LENGTH");
83 if (err == NOISE_ERROR_INVALID_PARAM)
84 return LOG_STR(
"INVALID_PARAM");
85 if (err == NOISE_ERROR_INVALID_STATE)
86 return LOG_STR(
"INVALID_STATE");
87 if (err == NOISE_ERROR_INVALID_NONCE)
88 return LOG_STR(
"INVALID_NONCE");
89 if (err == NOISE_ERROR_INVALID_PRIVATE_KEY)
90 return LOG_STR(
"INVALID_PRIVATE_KEY");
91 if (err == NOISE_ERROR_INVALID_PUBLIC_KEY)
92 return LOG_STR(
"INVALID_PUBLIC_KEY");
93 if (err == NOISE_ERROR_INVALID_FORMAT)
94 return LOG_STR(
"INVALID_FORMAT");
95 if (err == NOISE_ERROR_INVALID_SIGNATURE)
96 return LOG_STR(
"INVALID_SIGNATURE");
97 return LOG_STR(
"UNKNOWN");
109 prologue_.resize(old_size + PROLOGUE_INIT_LEN);
111 memcpy_P(
prologue_.data() + old_size, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
113 std::memcpy(
prologue_.data() + old_size, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
133 HELPER_LOG(
"%s failed: %s", LOG_STR_ARG(func_name), LOG_STR_ARG(
noise_err_to_logstr(err)));
145 bool socket_ready = this->
socket_->ready();
180 if (
static_cast<uint8_t
>(received) != to_read) {
197 uint16_t limit = (
state_ ==
State::DATA) ? MAX_MESSAGE_SIZE : MAX_HANDSHAKE_SIZE;
198 if (msg_size > limit) {
200 HELPER_LOG(
"Bad packet: message size %u exceeds maximum %u", msg_size, limit);
205 if (this->
rx_buf_.size() != msg_size) {
206 this->
rx_buf_.resize(msg_size);
218 if (
static_cast<uint16_t
>(received) != to_read) {
224 LOG_PACKET_RECEIVED(this->
rx_buf_);
246 HELPER_LOG(
"Bad state for method: %d", (
int)
state_);
257 size_t old_size = this->
prologue_.size();
261 std::memcpy(this->
prologue_.data() + old_size + 2, this->rx_buf_.data(), this->rx_buf_.size());
268 char mac[MAC_ADDRESS_BUFFER_SIZE];
272 size_t name_len = name.size() + 1;
273 size_t name_offset = 1;
274 size_t mac_offset = name_offset + name_len;
275 size_t total_size = 1 + name_len + MAC_ADDRESS_BUFFER_SIZE;
279 constexpr size_t max_msg_size = 1 + ESPHOME_DEVICE_NAME_MAX_LEN + 1 + MAC_ADDRESS_BUFFER_SIZE;
280 uint8_t msg[max_msg_size];
286 std::memcpy(msg + name_offset, name.c_str(), name_len);
288 std::memcpy(msg + mac_offset, mac, MAC_ADDRESS_BUFFER_SIZE);
302 int action = noise_handshakestate_get_action(
handshake_);
303 if (action == NOISE_ACTION_READ_MESSAGE) {
313 }
else if (this->
rx_buf_[0] != 0x00) {
314 HELPER_LOG(
"Bad handshake error byte: %u", this->
rx_buf_[0]);
320 noise_buffer_init(mbuf);
321 noise_buffer_set_input(mbuf, this->
rx_buf_.data() + 1, this->rx_buf_.size() - 1);
322 err = noise_handshakestate_read_message(
handshake_, &mbuf,
nullptr);
326 : LOG_STR(
"Handshake error"));
334 }
else if (action == NOISE_ACTION_WRITE_MESSAGE) {
337 noise_buffer_init(mbuf);
338 noise_buffer_set_output(mbuf, buffer + 1,
sizeof(buffer) - 1);
340 err = noise_handshakestate_write_message(
handshake_, &mbuf,
nullptr);
356 HELPER_LOG(
"Bad action for handshake: %d", action);
370#ifdef USE_STORE_LOG_STR_IN_FLASH
372 size_t reason_len = strlen_P(
reinterpret_cast<PGM_P
>(reason));
373 if (reason_len > 0) {
374 memcpy_P(data + 1,
reinterpret_cast<PGM_P
>(reason), reason_len);
378 const char *reason_str = LOG_STR_ARG(reason);
379 size_t reason_len = strlen(reason_str);
380 if (reason_len > 0) {
382 std::memcpy(data + 1, reason_str, reason_len);
386 size_t data_size = reason_len + 1;
409 noise_buffer_init(mbuf);
410 noise_buffer_set_inout(mbuf, this->
rx_buf_.data(), this->rx_buf_.size(), this->rx_buf_.size());
411 int err = noise_cipherstate_decrypt(this->
recv_cipher_, &mbuf);
418 uint16_t msg_size = mbuf.size;
419 uint8_t *msg_data = this->
rx_buf_.data();
422 HELPER_LOG(
"Bad data packet: size %d too short", msg_size);
426 uint16_t
type = (((uint16_t) msg_data[0]) << 8) | msg_data[1];
427 uint16_t data_len = (((uint16_t) msg_data[2]) << 8) | msg_data[3];
428 if (data_len > msg_size - 4) {
430 HELPER_LOG(
"Bad data packet: data_len %u greater than msg_size %u", data_len, msg_size);
434 buffer->
data = msg_data + 4;
457 if (messages.empty()) {
461 uint8_t *buffer_data = buffer.
get_buffer()->data();
465 uint16_t total_write_len = 0;
468 for (
const auto &msg : messages) {
470 uint8_t *buf_start = buffer_data + msg.offset;
477 const uint8_t msg_offset = 3;
478 buf_start[msg_offset] =
static_cast<uint8_t
>(msg.message_type >> 8);
479 buf_start[msg_offset + 1] =
static_cast<uint8_t
>(msg.message_type);
480 buf_start[msg_offset + 2] =
static_cast<uint8_t
>(msg.payload_size >> 8);
481 buf_start[msg_offset + 3] =
static_cast<uint8_t
>(msg.payload_size);
489 noise_buffer_init(mbuf);
490 noise_buffer_set_inout(mbuf, buf_start + msg_offset, 4 + msg.payload_size,
493 int err = noise_cipherstate_encrypt(
send_cipher_, &mbuf);
500 buf_start[1] =
static_cast<uint8_t
>(mbuf.size >> 8);
501 buf_start[2] =
static_cast<uint8_t
>(mbuf.size);
504 size_t msg_len =
static_cast<size_t>(3 + mbuf.size);
506 total_write_len += msg_len;
516 header[1] = (uint8_t) (
len >> 8);
517 header[2] = (uint8_t)
len;
525 iov[1].
iov_base =
const_cast<uint8_t *
>(data);
540 nid_.pattern_id = NOISE_PATTERN_NN;
541 nid_.cipher_id = NOISE_CIPHER_CHACHAPOLY;
542 nid_.dh_id = NOISE_DH_CURVE25519;
543 nid_.prefix_id = NOISE_PREFIX_STANDARD;
544 nid_.hybrid_id = NOISE_DH_NONE;
545 nid_.hash_id = NOISE_HASH_SHA256;
546 nid_.modifier_ids[0] = NOISE_MODIFIER_PSK0;
548 err = noise_handshakestate_new_by_id(&
handshake_, &
nid_, NOISE_ROLE_RESPONDER);
555 err = noise_handshakestate_set_pre_shared_key(
handshake_, psk.data(), psk.size());
579 int action = noise_handshakestate_get_action(
handshake_);
580 if (action == NOISE_ACTION_READ_MESSAGE || action == NOISE_ACTION_WRITE_MESSAGE)
582 if (action != NOISE_ACTION_SPLIT) {
584 HELPER_LOG(
"Bad action for handshake: %d", action);
595 HELPER_LOG(
"Handshake complete!");
621 ESP_LOGE(TAG,
"Acquiring random bytes failed; rebooting");
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Minimal static vector - saves memory by avoiding std::vector overhead.
void push_back(const T &value)
APIError handle_socket_read_result_(ssize_t received)
std::vector< uint8_t > rx_buf_
uint8_t frame_header_padding_
std::unique_ptr< socket::Socket > socket_
uint8_t frame_footer_size_
APIError write_raw_(const struct iovec *iov, int iovcnt, uint16_t total_write_len)
const psk_t & get_psk() const
APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override
uint8_t rx_header_buf_len_
NoiseCipherState * send_cipher_
APIError read_packet(ReadPacketBuffer *buffer) override
NoiseCipherState * recv_cipher_
~APINoiseFrameHelper() override
std::vector< uint8_t > prologue_
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)
NoiseHandshakeState * handshake_
uint8_t rx_header_buf_[3]
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 check_handshake_finished_()
APIError init_handshake_()
Initiate the data structures for the handshake.
std::vector< uint8_t > * get_buffer() const
void noise_rand_bytes(void *output, size_t len)
@ HANDSHAKESTATE_READ_FAILED
@ HANDSHAKESTATE_BAD_STATE
@ HANDSHAKESTATE_SPLIT_FAILED
@ BAD_HANDSHAKE_PACKET_LEN
@ BAD_HANDSHAKE_ERROR_BYTE
@ HANDSHAKESTATE_SETUP_FAILED
@ CIPHERSTATE_ENCRYPT_FAILED
@ CIPHERSTATE_DECRYPT_FAILED
@ HANDSHAKESTATE_WRITE_FAILED
const LogString * noise_err_to_logstr(int err)
Convert a noise error code to a readable error.
bool random_bytes(uint8_t *data, size_t len)
Generate len number of random bytes.
void get_mac_address_into_buffer(std::span< char, MAC_ADDRESS_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in lowercase hex notation.
Application App
Global storage of Application pointer - only one Application can exist.