1 //! Low-level "hazmat" AES functions: AES-NI 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 super::{
8     arch::*,
9     utils::{load8, store8},
10 };
11 use crate::{Block, ParBlocks};
12 
13 /// AES cipher (encrypt) round function.
14 #[allow(clippy::cast_ptr_alignment)]
15 #[target_feature(enable = "aes")]
cipher_round(block: &mut Block, round_key: &Block)16 pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
17     // Safety: `loadu` and `storeu` support unaligned access
18     let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
19     let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
20     let out = _mm_aesenc_si128(b, k);
21     _mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
22 }
23 
24 /// AES cipher (encrypt) round function: parallel version.
25 #[allow(clippy::cast_ptr_alignment)]
26 #[target_feature(enable = "aes")]
cipher_round_par(blocks: &mut ParBlocks, round_keys: &ParBlocks)27 pub(crate) unsafe fn cipher_round_par(blocks: &mut ParBlocks, round_keys: &ParBlocks) {
28     let xmm_keys = load8(round_keys);
29     let mut xmm_blocks = load8(blocks);
30 
31     for i in 0..8 {
32         xmm_blocks[i] = _mm_aesenc_si128(xmm_blocks[i], xmm_keys[i]);
33     }
34 
35     store8(blocks, xmm_blocks);
36 }
37 
38 /// AES cipher (encrypt) round function.
39 #[allow(clippy::cast_ptr_alignment)]
40 #[target_feature(enable = "aes")]
equiv_inv_cipher_round(block: &mut Block, round_key: &Block)41 pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
42     // Safety: `loadu` and `storeu` support unaligned access
43     let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
44     let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
45     let out = _mm_aesdec_si128(b, k);
46     _mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
47 }
48 
49 /// AES cipher (encrypt) round function: parallel version.
50 #[allow(clippy::cast_ptr_alignment)]
51 #[target_feature(enable = "aes")]
equiv_inv_cipher_round_par(blocks: &mut ParBlocks, round_keys: &ParBlocks)52 pub(crate) unsafe fn equiv_inv_cipher_round_par(blocks: &mut ParBlocks, round_keys: &ParBlocks) {
53     let xmm_keys = load8(round_keys);
54     let mut xmm_blocks = load8(blocks);
55 
56     for i in 0..8 {
57         xmm_blocks[i] = _mm_aesdec_si128(xmm_blocks[i], xmm_keys[i]);
58     }
59 
60     store8(blocks, xmm_blocks);
61 }
62 
63 /// AES mix columns function.
64 #[allow(clippy::cast_ptr_alignment)]
65 #[target_feature(enable = "aes")]
mix_columns(block: &mut Block)66 pub(crate) unsafe fn mix_columns(block: &mut Block) {
67     // Safety: `loadu` and `storeu` support unaligned access
68     let mut state = _mm_loadu_si128(block.as_ptr() as *const __m128i);
69 
70     // Emulate mix columns by performing three inverse mix columns operations
71     state = _mm_aesimc_si128(state);
72     state = _mm_aesimc_si128(state);
73     state = _mm_aesimc_si128(state);
74 
75     _mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, state);
76 }
77 
78 /// AES inverse mix columns function.
79 #[allow(clippy::cast_ptr_alignment)]
80 #[target_feature(enable = "aes")]
inv_mix_columns(block: &mut Block)81 pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
82     // Safety: `loadu` and `storeu` support unaligned access
83     let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
84     let out = _mm_aesimc_si128(b);
85     _mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
86 }
87