Merge PR 'Use the full 96-bit ChaCha20-Poly1305 nonce width for peer storage' (#5247)
What changed, and why it matters
This change updates how an encryption nonce is generated for stored peer data. Previously only 8 bytes of randomness were used inside a 12-byte nonce, with the first 4 bytes forced to zero. Now all 12 bytes are filled with randomness, making the nonce the full size intended by the ChaCha20-Poly1305 algorithm. Using a too-small random space can, in some designs, raise the risk of nonce reuse, which is dangerous for this cipher. The patch is a hardening improvement rather than a clear-cut fix for an active vulnerability, because the surrounding code also appears to use random peer-storage keys that limit exposure.
Treat as a low-to-moderate cryptographic hardening fix. Review whether any persisted peer-storage blobs generated with the old 8-byte nonce format need migration or re-encryption, and confirm that the peer-storage key derivation remains random per peer and per restart so that the prior reduced nonce space did not create a practical collision window.
Security signals we found
Cryptographic nonce construction changed from 8-byte random to 12-byte random
ChaCha20-Poly1305 nonce field now uses full 96-bit width as specified by the algorithm
Hard-coded zero prefix removed from nonce derivation
Hardening of encrypted peer-storage format
Evidence from the diff
In lightning/src/ln/our_peer_storage.rs, derive_nonce() previously produced a 12-byte ChaCha20-Poly1305 nonce by zeroing nonce[0..4] and copying only 8 bytes from an HMAC-SHA256 output into nonce[4..12]. The commit now copies 12 bytes into the entire nonce. ChaCha20-Poly1305 security depends on nonce uniqueness under a given key. Truncating the nonce to 8 random bytes reduces the nonce space from 2^96 to 2^64, increasing the birthday-collision probability. However, the peer-storage key is itself derived from random material per peer and per restart, so practical nonce-reuse risk appears limited. The change is a cryptographic hardening/correctness fix.
Changed components
lightning/src/ln/our_peer_storage.rsPeer storage encryption nonce derivationChaCha20-Poly1305 AEAD usage for persisted peer dataInspect captured patch +1 / −2
### lightning/src/ln/our_peer_storage.rs
@@ -130,8 +130,7 @@ fn derive_nonce(key: &PeerStorageKey, random_bytes: &[u8]) -> [u8; 12] {
let mut hmac = HmacEngine::<Sha256>::new(key_hash.as_byte_array());
hmac.input(&random_bytes);
let mut nonce = [0u8; 12];
- // First 4 bytes of the nonce should be 0.
- nonce[4..].copy_from_slice(&Hmac::from_engine(hmac).to_byte_array()[0..8]);
+ nonce.copy_from_slice(&Hmac::from_engine(hmac).to_byte_array()[0..12]);
nonce
}Why this scored 46/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.