Skip to content

Commit

Permalink
aes-gcm: Clarify slice-to-pointer conversion.
Browse files Browse the repository at this point in the history
Extend the work done in cb6d5de to
replace pointer arithmetic via slicing with `InOut`'s pointer
conversion for integrated AES-GCM opening operations.

Note that sealing doesn't have any arithmetic as it doesn't support
a `src` offset.
  • Loading branch information
briansmith committed Dec 27, 2024
1 parent fb1b00e commit 4f09fa5
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/aead/aes_gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,11 @@ pub(super) fn open(
Key(key): &Key,
nonce: Nonce,
aad: Aad<&[u8]>,
in_out: &mut [u8],
in_out_slice: &mut [u8],
src: RangeFrom<usize>,
) -> Result<Tag, error::Unspecified> {
// Check that `src` is in bounds.
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
let input = in_out.get(src.clone()).ok_or(error::Unspecified)?;
let in_out = InOut::overlapping(in_out_slice, src.clone())?;

let mut ctr = Counter::one(nonce);
let tag_iv = ctr.increment();
Expand All @@ -299,20 +298,21 @@ pub(super) fn open(
Xi: &mut gcm::Xi) -> c::size_t;
}

let mut auth = gcm::Context::new(gcm_key, aad, input.len())?;
let (input, output, len) = in_out.into_input_output_len();
let mut auth = gcm::Context::new(gcm_key, aad, len)?;
let (htable, xi) = auth.inner();
let processed = unsafe {
aesni_gcm_decrypt(
in_out[src.clone()].as_ptr(),
in_out.as_mut_ptr(),
in_out.len() - src.start,
input,
output,
len,
aes_key.inner_less_safe(),
&mut ctr,
htable,
xi,
)
};
let in_out = match in_out.get_mut(processed..) {
let in_out = match in_out_slice.get_mut(processed..) {
Some(remaining) => remaining,
None => {
// This can't happen. If it did, then the assembly already
Expand Down Expand Up @@ -345,7 +345,8 @@ pub(super) fn open(
DynKey::AesHwClMul(Combo { aes_key, gcm_key }) => {
use crate::bits::BitLength;

let input_len = input.len();
let (input, output, input_len) = in_out.into_input_output_len();

let mut auth = gcm::Context::new(gcm_key, aad, input_len)?;

let remainder_len = input_len % BLOCK_LEN;
Expand All @@ -370,32 +371,32 @@ pub(super) fn open(

unsafe {
aes_gcm_dec_kernel(
in_out[src.clone()].as_ptr(),
input,
whole_block_bits,
in_out.as_mut_ptr(),
output,
xi,
&mut ctr,
aes_key.inner_less_safe(),
htable,
)
}
}
let remainder = &mut in_out[whole_len..];
let remainder = &mut in_out_slice[whole_len..];
open_finish(aes_key, auth, remainder, src, ctr, tag_iv)
}

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
DynKey::AesHwClMul(c) => open_strided(c, aad, in_out, src, ctr, tag_iv),
DynKey::AesHwClMul(c) => open_strided(c, aad, in_out_slice, src, ctr, tag_iv),

#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86_64",
target_arch = "x86"
))]
DynKey::Simd(c) => open_strided(c, aad, in_out, src, ctr, tag_iv),
DynKey::Simd(c) => open_strided(c, aad, in_out_slice, src, ctr, tag_iv),

DynKey::Fallback(c) => open_strided(c, aad, in_out, src, ctr, tag_iv),
DynKey::Fallback(c) => open_strided(c, aad, in_out_slice, src, ctr, tag_iv),
}
}

Expand Down

0 comments on commit 4f09fa5

Please sign in to comment.