diff --git a/aes/src/armv8.rs b/aes/src/armv8.rs index 7f418a83..0bd92877 100644 --- a/aes/src/armv8.rs +++ b/aes/src/armv8.rs @@ -223,7 +223,7 @@ macro_rules! define_aes_impl { impl From<&$name_enc> for $name_dec { fn from(enc: &$name_enc) -> $name_dec { let mut round_keys = enc.round_keys; - inv_expanded_keys(&mut round_keys); + unsafe { inv_expanded_keys(&mut round_keys) }; Self { round_keys } } } diff --git a/aes/src/armv8/expand.rs b/aes/src/armv8/expand.rs index 35e7f73c..0d0deba8 100644 --- a/aes/src/armv8/expand.rs +++ b/aes/src/armv8/expand.rs @@ -41,9 +41,9 @@ pub(super) fn expand_key(key: &[u8; L]) -> [uint let mut word = ek_words[i - 1]; if i % nk == 0 { - word = sub_word(word).rotate_right(8) ^ ROUND_CONSTS[i / nk - 1]; + word = unsafe { sub_word(word) }.rotate_right(8) ^ ROUND_CONSTS[i / nk - 1]; } else if nk > 6 && i % nk == 4 { - word = sub_word(word) + word = unsafe { sub_word(word) }; } ek_words[i] = ek_words[i - nk] ^ word; @@ -56,26 +56,26 @@ pub(super) fn expand_key(key: &[u8; L]) -> [uint /// /// This is the reverse of the encryption keys, with the Inverse Mix Columns /// operation applied to all but the first and last expanded key. -#[inline] -pub(super) fn inv_expanded_keys(expanded_keys: &mut [uint8x16_t; N]) { +#[target_feature(enable = "aes")] +#[target_feature(enable = "neon")] +pub(super) unsafe fn inv_expanded_keys(expanded_keys: &mut [uint8x16_t; N]) { assert!(N == 11 || N == 13 || N == 15); for ek in expanded_keys.iter_mut().take(N - 1).skip(1) { - unsafe { *ek = vaesimcq_u8(*ek) } + *ek = vaesimcq_u8(*ek); } expanded_keys.reverse(); } /// Sub bytes for a single AES word: used for key expansion. -#[inline(always)] -fn sub_word(input: u32) -> u32 { - unsafe { - let input = vreinterpretq_u8_u32(vdupq_n_u32(input)); +#[target_feature(enable = "aes")] +#[target_feature(enable = "neon")] +unsafe fn sub_word(input: u32) -> u32 { + let input = vreinterpretq_u8_u32(vdupq_n_u32(input)); - // AES single round encryption (with a "round" key of all zeros) - let sub_input = vaeseq_u8(input, vdupq_n_u8(0)); + // AES single round encryption (with a "round" key of all zeros) + let sub_input = vaeseq_u8(input, vdupq_n_u8(0)); - vgetq_lane_u32(vreinterpretq_u32_u8(sub_input), 0) - } + vgetq_lane_u32(vreinterpretq_u32_u8(sub_input), 0) } diff --git a/aes/src/armv8/test_expand.rs b/aes/src/armv8/test_expand.rs index c52bda74..a707a55c 100644 --- a/aes/src/armv8/test_expand.rs +++ b/aes/src/armv8/test_expand.rs @@ -113,7 +113,7 @@ fn aes128_key_expansion() { #[test] fn aes128_key_expansion_inv() { let mut ek = load_expanded_keys(AES128_EXP_KEYS); - inv_expanded_keys(&mut ek); + unsafe { inv_expanded_keys(&mut ek) }; assert_eq!(store_expanded_keys(ek), AES128_EXP_INVKEYS); }