| 1 |
//! AES encryption support |
| 2 |
|
| 3 |
use crate::{Block, Block8}; |
| 4 |
use cipher::inout::InOut; |
| 5 |
use core::arch::aarch64::*; |
| 6 |
|
| 7 |
// Stable "polyfills" for unstable core::arch::aarch64 intrinsics |
| 8 |
// TODO(tarcieri): remove when these intrinsics have been stabilized |
| 9 |
use super::intrinsics::{ |
| 10 |
vaesdq_u8, vaesdq_u8_and_vaesimcq_u8, vaeseq_u8, vaeseq_u8_and_vaesmcq_u8, |
| 11 |
}; |
| 12 |
|
| 13 |
/// Perform AES encryption using the given expanded keys. |
| 14 |
#[target_feature(enable = "aes")] |
| 15 |
#[target_feature(enable = "neon")] |
| 16 |
pub(super) unsafe fn encrypt1<const N: usize>( |
| 17 |
expanded_keys: &[uint8x16_t; N], |
| 18 |
block: InOut<'_, '_, Block>, |
| 19 |
) { |
| 20 |
let rounds = N - 1; |
| 21 |
assert!(rounds == 10 || rounds == 12 || rounds == 14); |
| 22 |
|
| 23 |
let (in_ptr, out_ptr) = block.into_raw(); |
| 24 |
|
| 25 |
let mut state = vld1q_u8(in_ptr as *const u8); |
| 26 |
|
| 27 |
for k in expanded_keys.iter().take(rounds - 1) { |
| 28 |
// AES single round encryption and mix columns |
| 29 |
state = vaeseq_u8_and_vaesmcq_u8(state, *k); |
| 30 |
} |
| 31 |
|
| 32 |
// AES single round encryption |
| 33 |
state = vaeseq_u8(state, expanded_keys[rounds - 1]); |
| 34 |
|
| 35 |
// Final add (bitwise XOR) |
| 36 |
state = veorq_u8(state, expanded_keys[rounds]); |
| 37 |
|
| 38 |
vst1q_u8(out_ptr as *mut u8, state); |
| 39 |
} |
| 40 |
|
| 41 |
/// Perform parallel AES encryption 8-blocks-at-a-time using the given expanded keys. |
| 42 |
#[target_feature(enable = "aes")] |
| 43 |
#[target_feature(enable = "neon")] |
| 44 |
pub(super) unsafe fn encrypt8<const N: usize>( |
| 45 |
expanded_keys: &[uint8x16_t; N], |
| 46 |
blocks: InOut<'_, '_, Block8>, |
| 47 |
) { |
| 48 |
let rounds = N - 1; |
| 49 |
assert!(rounds == 10 || rounds == 12 || rounds == 14); |
| 50 |
|
| 51 |
let (in_ptr, out_ptr) = blocks.into_raw(); |
| 52 |
let in_ptr = in_ptr as *const Block; |
| 53 |
let out_ptr = out_ptr as *const Block; |
| 54 |
|
| 55 |
let mut state = [ |
| 56 |
vld1q_u8(in_ptr.add(0) as *const u8), |
| 57 |
vld1q_u8(in_ptr.add(1) as *const u8), |
| 58 |
vld1q_u8(in_ptr.add(2) as *const u8), |
| 59 |
vld1q_u8(in_ptr.add(3) as *const u8), |
| 60 |
vld1q_u8(in_ptr.add(4) as *const u8), |
| 61 |
vld1q_u8(in_ptr.add(5) as *const u8), |
| 62 |
vld1q_u8(in_ptr.add(6) as *const u8), |
| 63 |
vld1q_u8(in_ptr.add(7) as *const u8), |
| 64 |
]; |
| 65 |
|
| 66 |
for k in expanded_keys.iter().take(rounds - 1) { |
| 67 |
for i in 0..8 { |
| 68 |
// AES single round encryption and mix columns |
| 69 |
state[i] = vaeseq_u8_and_vaesmcq_u8(state[i], *k); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
for i in 0..8 { |
| 74 |
// AES single round encryption |
| 75 |
state[i] = vaeseq_u8(state[i], expanded_keys[rounds - 1]); |
| 76 |
|
| 77 |
// Final add (bitwise XOR) |
| 78 |
state[i] = veorq_u8(state[i], expanded_keys[rounds]); |
| 79 |
|
| 80 |
vst1q_u8(out_ptr.add(i) as *mut u8, state[i]); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/// Perform AES decryption using the given expanded keys. |
| 85 |
#[target_feature(enable = "aes")] |
| 86 |
#[target_feature(enable = "neon")] |
| 87 |
pub(super) unsafe fn decrypt1<const N: usize>( |
| 88 |
expanded_keys: &[uint8x16_t; N], |
| 89 |
block: InOut<'_, '_, Block>, |
| 90 |
) { |
| 91 |
let rounds = N - 1; |
| 92 |
assert!(rounds == 10 || rounds == 12 || rounds == 14); |
| 93 |
|
| 94 |
let (in_ptr, out_ptr) = block.into_raw(); |
| 95 |
let mut state = vld1q_u8(in_ptr as *const u8); |
| 96 |
|
| 97 |
for k in expanded_keys.iter().take(rounds - 1) { |
| 98 |
// AES single round decryption and inverse mix columns |
| 99 |
state = vaesdq_u8_and_vaesimcq_u8(state, *k); |
| 100 |
} |
| 101 |
|
| 102 |
// AES single round decryption |
| 103 |
state = vaesdq_u8(state, expanded_keys[rounds - 1]); |
| 104 |
|
| 105 |
// Final add (bitwise XOR) |
| 106 |
state = veorq_u8(state, expanded_keys[rounds]); |
| 107 |
|
| 108 |
vst1q_u8(out_ptr as *mut u8, state); |
| 109 |
} |
| 110 |
|
| 111 |
/// Perform parallel AES decryption 8-blocks-at-a-time using the given expanded keys. |
| 112 |
#[target_feature(enable = "aes")] |
| 113 |
#[target_feature(enable = "neon")] |
| 114 |
pub(super) unsafe fn decrypt8<const N: usize>( |
| 115 |
expanded_keys: &[uint8x16_t; N], |
| 116 |
blocks: InOut<'_, '_, Block8>, |
| 117 |
) { |
| 118 |
let rounds = N - 1; |
| 119 |
assert!(rounds == 10 || rounds == 12 || rounds == 14); |
| 120 |
|
| 121 |
let (in_ptr, out_ptr) = blocks.into_raw(); |
| 122 |
let in_ptr = in_ptr as *const Block; |
| 123 |
let out_ptr = out_ptr as *const Block; |
| 124 |
|
| 125 |
let mut state = [ |
| 126 |
vld1q_u8(in_ptr.add(0) as *const u8), |
| 127 |
vld1q_u8(in_ptr.add(1) as *const u8), |
| 128 |
vld1q_u8(in_ptr.add(2) as *const u8), |
| 129 |
vld1q_u8(in_ptr.add(3) as *const u8), |
| 130 |
vld1q_u8(in_ptr.add(4) as *const u8), |
| 131 |
vld1q_u8(in_ptr.add(5) as *const u8), |
| 132 |
vld1q_u8(in_ptr.add(6) as *const u8), |
| 133 |
vld1q_u8(in_ptr.add(7) as *const u8), |
| 134 |
]; |
| 135 |
|
| 136 |
for k in expanded_keys.iter().take(rounds - 1) { |
| 137 |
for i in 0..8 { |
| 138 |
// AES single round decryption and inverse mix columns |
| 139 |
state[i] = vaesdq_u8_and_vaesimcq_u8(state[i], *k); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
for i in 0..8 { |
| 144 |
// AES single round decryption |
| 145 |
state[i] = vaesdq_u8(state[i], expanded_keys[rounds - 1]); |
| 146 |
|
| 147 |
// Final add (bitwise XOR) |
| 148 |
state[i] = veorq_u8(state[i], expanded_keys[rounds]); |
| 149 |
|
| 150 |
vst1q_u8(out_ptr.add(i) as *mut u8, state[i]); |
| 151 |
} |
| 152 |
} |