Skip to content

Commit 6526793

Browse files
committed
Fix AEAD-4 payload size check and nonce wrap-around
1 parent e224e5c commit 6526793

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

513513
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) {
514514
if (type == PAYLOAD_TYPE_TXT_MSG || type == PAYLOAD_TYPE_REQ || type == PAYLOAD_TYPE_RESPONSE) {
515+
size_t hash_prefix = PATH_HASH_SIZE * 2; // dest_hash + src_hash
515516
size_t max_overhead = aead_nonce ? (AEAD_NONCE_SIZE + AEAD_TAG_SIZE) : (CIPHER_MAC_SIZE + CIPHER_BLOCK_SIZE-1);
516-
if (data_len + max_overhead > MAX_PACKET_PAYLOAD) return NULL;
517+
if (data_len + hash_prefix + max_overhead > MAX_PACKET_PAYLOAD) return NULL;
517518
} else {
518519
return NULL; // invalid type
519520
}

src/helpers/ContactInfo.h

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

0 commit comments

Comments
 (0)