Skip to content

Commit 03ca9d5

Browse files
committed
Fix AEAD-4 payload size check and nonce wrap-around
1 parent c04942a commit 03ca9d5

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
@@ -508,8 +508,9 @@ Packet* Mesh::createPathReturn(const uint8_t* dest_hash, const uint8_t* secret,
508508

509509
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) {
510510
if (type == PAYLOAD_TYPE_TXT_MSG || type == PAYLOAD_TYPE_REQ || type == PAYLOAD_TYPE_RESPONSE) {
511+
size_t hash_prefix = PATH_HASH_SIZE * 2; // dest_hash + src_hash
511512
size_t max_overhead = aead_nonce ? (AEAD_NONCE_SIZE + AEAD_TAG_SIZE) : (CIPHER_MAC_SIZE + CIPHER_BLOCK_SIZE-1);
512-
if (data_len + max_overhead > MAX_PACKET_PAYLOAD) return NULL;
513+
if (data_len + hash_prefix + max_overhead > MAX_PACKET_PAYLOAD) return NULL;
513514
} else {
514515
return NULL; // invalid type
515516
}

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)