Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chacha20_poly1305_openssh: Minimize and document panicking. #2228

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/aead/chacha20_poly1305_openssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl SealingKey {
/// `padding_length||payload||random padding`. It will be overwritten by
/// `encrypted_packet_length||ciphertext`, where `encrypted_packet_length`
/// is encrypted with `K_1` and `ciphertext` is encrypted by `K_2`.
///
/// # Panics
///
/// Panics if `plaintext_in_ciphertext_out.len() < PACKET_LENGTH_LEN`.
pub fn seal_in_place(
&self,
sequence_number: u32,
Expand Down Expand Up @@ -126,6 +130,10 @@ impl OpeningKey {
ciphertext_in_plaintext_out: &'a mut [u8],
tag: &[u8; TAG_LEN],
) -> Result<&'a [u8], error::Unspecified> {
if ciphertext_in_plaintext_out.len() < PACKET_LENGTH_LEN {
return Err(error::Unspecified);
}

let mut counter = make_counter(sequence_number);

// We must verify the tag before decrypting so that
Expand All @@ -134,7 +142,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);
Expand Down