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

aes-gcm internals: Clarify in_out processing. #2205

Merged
merged 2 commits into from
Jan 1, 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
49 changes: 29 additions & 20 deletions src/aead/aes_gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,34 +316,34 @@ pub(super) fn open(
xi,
)
};
let in_out = match in_out_slice.get_mut(processed..) {
Some(remaining) => remaining,
None => {
let in_out_slice = in_out_slice.get_mut(processed..).unwrap_or_else(|| {
// This can't happen. If it did, then the assembly already
// caused a buffer overflow.
unreachable!()
});
// Authenticate any remaining whole blocks.
let in_out = Overlapping::new(in_out_slice, src.clone()).unwrap_or_else(
|SrcIndexError { .. }| {
// This can't happen. If it did, then the assembly already
// caused a buffer overflow.
// overwrote part of the remaining input.
unreachable!()
}
};
// Authenticate any remaining whole blocks.
let input = match in_out.get(src.clone()) {
Some(remaining_input) => remaining_input,
None => unreachable!(),
};
let (whole, _) = slice::as_chunks(input);
},
);
let (whole, _) = slice::as_chunks(in_out.input());
auth.update_blocks(whole);

let whole_len = slice::flatten(whole).len();

// Decrypt any remaining whole blocks.
let whole = Overlapping::new(&mut in_out[..(src.start + whole_len)], src.clone())
let whole = Overlapping::new(&mut in_out_slice[..(src.start + whole_len)], src.clone())
.map_err(error::erase::<SrcIndexError>)?;
aes_key.ctr32_encrypt_within(whole, &mut ctr);

let in_out = match in_out.get_mut(whole_len..) {
let in_out_slice = match in_out_slice.get_mut(whole_len..) {
Some(partial) => partial,
None => unreachable!(),
};
open_finish(aes_key, auth, in_out, src, ctr, tag_iv)
open_finish(aes_key, auth, in_out_slice, src, ctr, tag_iv)
}

#[cfg(target_arch = "aarch64")]
Expand Down Expand Up @@ -422,12 +422,14 @@ pub(super) fn open(
fn open_strided<A: aes::EncryptBlock + aes::EncryptCtr32, G: gcm::UpdateBlocks + gcm::Gmult>(
Combo { aes_key, gcm_key }: &Combo<A, G>,
aad: Aad<&[u8]>,
in_out: &mut [u8],
in_out_slice: &mut [u8],
src: RangeFrom<usize>,
mut ctr: Counter,
tag_iv: aes::Iv,
) -> Result<Tag, error::Unspecified> {
let input = in_out.get(src.clone()).ok_or(error::Unspecified)?;
let in_out =
Overlapping::new(in_out_slice, src.clone()).map_err(error::erase::<SrcIndexError>)?;
let input = in_out.input();
let input_len = input.len();

let mut auth = gcm::Context::new(gcm_key, aad, input_len)?;
Expand All @@ -445,7 +447,7 @@ fn open_strided<A: aes::EncryptBlock + aes::EncryptCtr32, G: gcm::UpdateBlocks +
chunk_len = whole_len - output;
}

let ciphertext = &in_out[input..][..chunk_len];
let ciphertext = &in_out_slice[input..][..chunk_len];
let (ciphertext, leftover) = slice::as_chunks(ciphertext);
debug_assert_eq!(leftover.len(), 0);
if ciphertext.is_empty() {
Expand All @@ -454,7 +456,7 @@ fn open_strided<A: aes::EncryptBlock + aes::EncryptCtr32, G: gcm::UpdateBlocks +
auth.update_blocks(ciphertext);

let chunk = Overlapping::new(
&mut in_out[output..][..(chunk_len + in_prefix_len)],
&mut in_out_slice[output..][..(chunk_len + in_prefix_len)],
in_prefix_len..,
)
.map_err(error::erase::<SrcIndexError>)?;
Expand All @@ -464,7 +466,14 @@ fn open_strided<A: aes::EncryptBlock + aes::EncryptCtr32, G: gcm::UpdateBlocks +
}
}

open_finish(aes_key, auth, &mut in_out[whole_len..], src, ctr, tag_iv)
open_finish(
aes_key,
auth,
&mut in_out_slice[whole_len..],
src,
ctr,
tag_iv,
)
}

fn open_finish<A: aes::EncryptBlock, G: gcm::Gmult>(
Expand Down