diff --git a/src/aead/aes_gcm.rs b/src/aead/aes_gcm.rs index 898cd94353..781ec3d4af 100644 --- a/src/aead/aes_gcm.rs +++ b/src/aead/aes_gcm.rs @@ -127,14 +127,18 @@ fn aes_gcm_seal( if !aes_key.is_aes_hw(cpu_features) || !auth.is_clmul() { in_out } else { + use crate::bits::BitLength; + let whole_block_bits = auth.in_out_whole_block_bits(); - if whole_block_bits.as_bits() > 0 { - use crate::{bits::BitLength, c}; + let whole_block_bits_u64: BitLength = whole_block_bits.into(); + if let Ok(whole_block_bits) = whole_block_bits_u64.try_into() { + use core::num::NonZeroU64; + let (htable, xi) = auth.inner(); prefixed_extern! { fn aes_gcm_enc_kernel( input: *const u8, - in_bits: BitLength, + in_bits: BitLength, output: *mut u8, Xi: &mut gcm::Xi, ivec: &mut Counter, @@ -243,14 +247,18 @@ fn aes_gcm_open( if !aes_key.is_aes_hw(cpu_features) || !auth.is_clmul() { in_out } else { + use crate::bits::BitLength; + let whole_block_bits = auth.in_out_whole_block_bits(); - if whole_block_bits.as_bits() > 0 { - use crate::{bits::BitLength, c}; + let whole_block_bits_u64: BitLength = whole_block_bits.into(); + if let Ok(whole_block_bits) = whole_block_bits_u64.try_into() { + use core::num::NonZeroU64; + let (htable, xi) = auth.inner(); prefixed_extern! { fn aes_gcm_dec_kernel( input: *const u8, - in_bits: BitLength, + in_bits: BitLength, output: *mut u8, Xi: &mut gcm::Xi, ivec: &mut Counter, diff --git a/src/bits.rs b/src/bits.rs index 9dacad0b81..68e2722e37 100644 --- a/src/bits.rs +++ b/src/bits.rs @@ -107,3 +107,18 @@ impl BitLength { self.0.to_be_bytes() } } + +#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] +impl From> for BitLength { + fn from(BitLength(value): BitLength) -> Self { + BitLength(polyfill::u64_from_usize(value)) + } +} + +impl TryFrom> for BitLength { + type Error = >::Error; + + fn try_from(BitLength(value): BitLength) -> Result { + value.try_into().map(BitLength) + } +}