Skip to content

Commit 8ee02eb

Browse files
committed
Correct ChaCha[Dual]PolyReadAdapter extra stream contents logic
`ChaCha[Dual]PolyReadAdapter` currently read the encrypted object using `Readable` through the ChaCha stream (including the Poly1305 HMAC), but then consume any remaining bytes directly. This results in any extra bytes not consumed by the desired type's `Readable` being ignored and not included in the HMAC check. This is likely not the desired behavior - if we get some data which has extra slack at the end we ignore, it should still be authenticated as the sender likely thinks that data has meaning and included it in their HMAC check. Luckily, I believe this is currently dead code - `ChaCha[Dual]PolyReadAdapter` are only used for TLV stream reads which consume the full underlying stream. However, if either is used for non-TLV-streams in the future, this may be important. Here we simply push any extra bytes read through the ChaCha20Poly1305 reader, ensuring extra data is included in the HMAC check.
1 parent 3129ac3 commit 8ee02eb

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lightning/src/crypto/streams.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ impl<T: Readable> LengthReadableArgs<([u8; 32], [u8; 32])> for ChaChaDualPolyRea
128128
ChaChaDualPolyReader { chacha: &mut chacha, poly: &mut mac, read_len: 0, read: s };
129129

130130
let readable: T = Readable::read(&mut chacha_stream)?;
131-
chacha_stream.read.eat_remaining()?;
131+
while chacha_stream.read.bytes_remain() {
132+
let mut buf = [0; 256];
133+
chacha_stream.read(&mut buf)?;
134+
}
132135

133136
let read_len = chacha_stream.read_len;
134137

@@ -203,7 +206,10 @@ impl<T: Readable> LengthReadableArgs<[u8; 32]> for ChaChaPolyReadAdapter<T> {
203206
let s = FixedLengthReader::new(r, decrypted_len);
204207
let mut chacha_stream = ChaChaPolyReader { chacha: &mut chacha, read: s };
205208
let readable: T = Readable::read(&mut chacha_stream)?;
206-
chacha_stream.read.eat_remaining()?;
209+
while chacha_stream.read.bytes_remain() {
210+
let mut buf = [0; 256];
211+
chacha_stream.read(&mut buf)?;
212+
}
207213

208214
let mut tag = [0 as u8; 16];
209215
r.read_exact(&mut tag)?;

0 commit comments

Comments
 (0)