diff --git a/src/aead/chacha20_poly1305_openssh.rs b/src/aead/chacha20_poly1305_openssh.rs index 529fd0653..3037be487 100644 --- a/src/aead/chacha20_poly1305_openssh.rs +++ b/src/aead/chacha20_poly1305_openssh.rs @@ -126,7 +126,9 @@ impl OpeningKey { ciphertext_in_plaintext_out: &'a mut [u8], tag: &[u8; TAG_LEN], ) -> Result<&'a [u8], error::Unspecified> { - let mut counter = make_counter(sequence_number); + if ciphertext_in_plaintext_out.len() < PACKET_LENGTH_LEN { + return Err(error::Unspecified); + } // We must verify the tag before decrypting so that // `ciphertext_in_plaintext_out` is unmodified if verification fails. @@ -134,7 +136,9 @@ impl OpeningKey { let poly_key = derive_poly1305_key(&self.key.k_2, counter.increment()); verify(poly_key, ciphertext_in_plaintext_out, tag)?; + // Won't panic because the length was checked above. let plaintext_in_ciphertext_out = &mut ciphertext_in_plaintext_out[PACKET_LENGTH_LEN..]; + self.key .k_2 .encrypt_in_place(counter, plaintext_in_ciphertext_out);