Skip to content

Commit f3849b6

Browse files
committed
Fix AEAD-4 payload size check and nonce wrap-around
1 parent 376e0bd commit f3849b6

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

src/Mesh.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,9 @@ Packet* Mesh::createPathReturn(const uint8_t* dest_hash, const uint8_t* secret,
505505

506506
Packet* Mesh::createDatagram(uint8_t type, const Identity& dest, const uint8_t* secret, const uint8_t* data, size_t data_len, uint16_t aead_nonce) {
507507
if (type == PAYLOAD_TYPE_TXT_MSG || type == PAYLOAD_TYPE_REQ || type == PAYLOAD_TYPE_RESPONSE) {
508+
size_t hash_prefix = PATH_HASH_SIZE * 2; // dest_hash + src_hash
508509
size_t max_overhead = aead_nonce ? (AEAD_NONCE_SIZE + AEAD_TAG_SIZE) : (CIPHER_MAC_SIZE + CIPHER_BLOCK_SIZE-1);
509-
if (data_len + max_overhead > MAX_PACKET_PAYLOAD) return NULL;
510+
if (data_len + hash_prefix + max_overhead > MAX_PACKET_PAYLOAD) return NULL;
510511
} else {
511512
return NULL; // invalid type
512513
}

src/helpers/ContactInfo.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ struct ContactInfo {
2222
// Returns next AEAD nonce (post-increment) if peer supports AEAD, 0 otherwise.
2323
// When 0, callers use ECB encryption.
2424
uint16_t nextAeadNonce() const {
25-
if (flags & CONTACT_FLAG_AEAD) return ++aead_nonce;
25+
if (flags & CONTACT_FLAG_AEAD) {
26+
if (++aead_nonce == 0) ++aead_nonce; // skip 0 (sentinel for ECB)
27+
return aead_nonce;
28+
}
2629
return 0;
2730
}
2831

0 commit comments

Comments
 (0)