Skip to content

Commit 13ab628

Browse files
tankyleoflow
authored andcommitted
Use a 12-byte nonce as an input to Chacha20-Poly1305
Previously, we were using the Chacha20-Poly1305 implementation at `rust-lightning/lightning/src/crypto/chacha20poly1305rfc.rs`. That implementation required us to use an 8-byte nonce. Since we made the switch to the `rust-bitcoin/chacha20_poly1305` implementation, we can now use a full 12-byte nonce as specified in the RFC.
1 parent 5067abc commit 13ab628

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/util/key_obfuscator.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ impl KeyObfuscator {
136136
fn generate_synthetic_nonce(&self, initial_nonce_material: &[u8]) -> [u8; NONCE_LENGTH] {
137137
let hmac = Self::hkdf(&self.hashing_key, initial_nonce_material);
138138
let mut nonce = [0u8; NONCE_LENGTH];
139+
// TODO: While the RFC specifies a 12-byte nonce, we use an 8-byte nonce for
140+
// backwards compatibility with the rust-lightning implementation of
141+
// Chacha20Poly1305. We now use the rust-bitcoin implementation, which allows
142+
// for 12-byte nonces, so we should figure out an upgrade path for this.
139143
nonce[4..].copy_from_slice(&hmac[..8]);
140144
nonce
141145
}

src/util/storable_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<T: EntropySource> StorableBuilder<T> {
4747
&self, input: Vec<u8>, version: i64, data_encryption_key: &[u8; 32], aad: &[u8],
4848
) -> Storable {
4949
let mut nonce = [0u8; NONCE_LENGTH];
50-
self.entropy_source.fill_bytes(&mut nonce[4..]);
50+
self.entropy_source.fill_bytes(&mut nonce);
5151

5252
let mut data_blob = PlaintextBlob { value: input, version }.encode_to_vec();
5353

0 commit comments

Comments
 (0)