| 1 |
//! Low-level "hazmat" AES functions: ARMv8 Cryptography Extensions support. |
| 2 |
//! |
| 3 |
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256` |
| 4 |
//! implementations in this crate, but instead provides raw AES-NI accelerated |
| 5 |
//! access to the AES round function gated under the `hazmat` crate feature. |
| 6 |
|
| 7 |
use crate::{Block, Block8}; |
| 8 |
use core::arch::aarch64::*; |
| 9 |
|
| 10 |
// Stable "polyfills" for unstable core::arch::aarch64 intrinsics |
| 11 |
use super::intrinsics::{vaesdq_u8, vaeseq_u8, vaesimcq_u8, vaesmcq_u8}; |
| 12 |
|
| 13 |
/// AES cipher (encrypt) round function. |
| 14 |
#[allow(clippy::cast_ptr_alignment)] |
| 15 |
#[target_feature(enable = "aes")] |
| 16 |
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) { |
| 17 |
let b = vld1q_u8(block.as_ptr()); |
| 18 |
let k = vld1q_u8(round_key.as_ptr()); |
| 19 |
|
| 20 |
// AES single round encryption (all-zero round key, deferred until the end) |
| 21 |
let mut state = vaeseq_u8(b, vdupq_n_u8(0)); |
| 22 |
|
| 23 |
// AES mix columns (the `vaeseq_u8` instruction otherwise omits this step) |
| 24 |
state = vaesmcq_u8(state); |
| 25 |
|
| 26 |
// AES add round key (bitwise XOR) |
| 27 |
state = veorq_u8(state, k); |
| 28 |
|
| 29 |
vst1q_u8(block.as_mut_ptr(), state); |
| 30 |
} |
| 31 |
|
| 32 |
/// AES cipher (encrypt) round function: parallel version. |
| 33 |
#[allow(clippy::cast_ptr_alignment)] |
| 34 |
#[target_feature(enable = "aes")] |
| 35 |
pub(crate) unsafe fn cipher_round_par(blocks: &mut Block8, round_keys: &Block8) { |
| 36 |
for i in 0..8 { |
| 37 |
let mut state = vld1q_u8(blocks[i].as_ptr()); |
| 38 |
|
| 39 |
// AES single round encryption |
| 40 |
state = vaeseq_u8(state, vdupq_n_u8(0)); |
| 41 |
|
| 42 |
// AES mix columns |
| 43 |
state = vaesmcq_u8(state); |
| 44 |
|
| 45 |
// AES add round key (bitwise XOR) |
| 46 |
state = veorq_u8(state, vld1q_u8(round_keys[i].as_ptr())); |
| 47 |
|
| 48 |
vst1q_u8(blocks[i].as_mut_ptr(), state); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/// AES equivalent inverse cipher (decrypt) round function. |
| 53 |
#[allow(clippy::cast_ptr_alignment)] |
| 54 |
#[target_feature(enable = "aes")] |
| 55 |
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) { |
| 56 |
let b = vld1q_u8(block.as_ptr()); |
| 57 |
let k = vld1q_u8(round_key.as_ptr()); |
| 58 |
|
| 59 |
// AES single round decryption (all-zero round key, deferred until the end) |
| 60 |
let mut state = vaesdq_u8(b, vdupq_n_u8(0)); |
| 61 |
|
| 62 |
// AES inverse mix columns (the `vaesdq_u8` instruction otherwise omits this step) |
| 63 |
state = vaesimcq_u8(state); |
| 64 |
|
| 65 |
// AES add round key (bitwise XOR) |
| 66 |
state = veorq_u8(state, k); |
| 67 |
|
| 68 |
vst1q_u8(block.as_mut_ptr(), state); |
| 69 |
} |
| 70 |
|
| 71 |
/// AES equivalent inverse cipher (decrypt) round function: parallel version. |
| 72 |
#[allow(clippy::cast_ptr_alignment)] |
| 73 |
#[target_feature(enable = "aes")] |
| 74 |
pub(crate) unsafe fn equiv_inv_cipher_round_par(blocks: &mut Block8, round_keys: &Block8) { |
| 75 |
for i in 0..8 { |
| 76 |
let mut state = vld1q_u8(blocks[i].as_ptr()); |
| 77 |
|
| 78 |
// AES single round decryption (all-zero round key, deferred until the end) |
| 79 |
state = vaesdq_u8(state, vdupq_n_u8(0)); |
| 80 |
|
| 81 |
// AES inverse mix columns (the `vaesdq_u8` instruction otherwise omits this step) |
| 82 |
state = vaesimcq_u8(state); |
| 83 |
|
| 84 |
// AES add round key (bitwise XOR) |
| 85 |
state = veorq_u8(state, vld1q_u8(round_keys[i].as_ptr())); |
| 86 |
|
| 87 |
vst1q_u8(blocks[i].as_mut_ptr(), state); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/// AES mix columns function. |
| 92 |
#[allow(clippy::cast_ptr_alignment)] |
| 93 |
#[target_feature(enable = "aes")] |
| 94 |
pub(crate) unsafe fn mix_columns(block: &mut Block) { |
| 95 |
let b = vld1q_u8(block.as_ptr()); |
| 96 |
let out = vaesmcq_u8(b); |
| 97 |
vst1q_u8(block.as_mut_ptr(), out); |
| 98 |
} |
| 99 |
|
| 100 |
/// AES inverse mix columns function. |
| 101 |
#[allow(clippy::cast_ptr_alignment)] |
| 102 |
#[target_feature(enable = "aes")] |
| 103 |
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) { |
| 104 |
let b = vld1q_u8(block.as_ptr()); |
| 105 |
let out = vaesimcq_u8(b); |
| 106 |
vst1q_u8(block.as_mut_ptr(), out); |
| 107 |
} |