Skip to content

Commit 3129ac3

Browse files
committed
Update ChaChaPolyWriteAdapter to bypass crypto when calc'ing len
`ChaChaPolyWriteAdapter` is used to wrap `Writeable` objects in an AEAD before serializing them. This is great, except that we sometimes call `<T as ChaChaPolyWriteAdapter>::encode` which first calls `Writeable::serialized_length` to calculate the length to pre-allocate in a `Vec` before doing the actual write. Because `ChaChaPolyWriteAdapter` did not override `Writeable::serialized_length`, this led to doing the full cryptographic AEAD encryption and MAC twice, once to calculate the length and once to actually write the data. Instead, here, we override `<ChaChaPolyWriteAdapter as Writeable::serialized_length` to simply calculate the length we need.
1 parent 5f14409 commit 3129ac3

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

lightning/src/crypto/streams.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ impl<'a, T: Writeable> Writeable for ChaChaPolyWriteAdapter<'a, T> {
5151

5252
Ok(())
5353
}
54+
55+
fn serialized_length(&self) -> usize {
56+
self.writeable.serialized_length() + 16
57+
}
5458
}
5559

5660
/// Encrypts the provided plaintext with the given key using ChaCha20Poly1305 in the modified

0 commit comments

Comments
 (0)