git clone https://garage44.eu/jeroen/pinnacle.git.. /crypto/src/hw.rs
//! The processor's own crypto instructions, where it has them: ARMv8's
//! SHA-256 and AES instructions (ID_AA64ISAR0_EL1 says which a core has;
//! the Cortex-A53 and -A72 of the boards Pinnacle aims at, and QEMU's, do),
//! and on x86-64 SHA-NI and AES-NI (CPUID says).
//! What they compute is what the software computes, and the tests compare
//! the two.
//!
//! Who runs the code says what the core has ([`enable`]): the kernel at
//! boot, from the register; a program at its start, from the kernel (EL0
//! cannot read the register). Until then the software runs.
//!
//! The instructions use the SIMD registers. The routine saves every one it
//! touches and puts it back, so it leaves a program's registers as they
//! were, and the kernel, built not to use them, can call it too; in the
//! kernel interrupts are masked meanwhile, so no switch to a program that
//! loads its own registers lands in the middle.
use core::sync::atomic::{AtomicU8, Ordering};
static FEATURES: AtomicU8 = AtomicU8::new(0);
const SHA256: u8 = 1;
const AES: u8 = 2;
/// Running at EL1: interrupts are masked around the instructions.
const KERNEL: u8 = 0x80;
/// Records what the core has, from ID_AA64ISAR0_EL1's value, and whether
/// this is the kernel.
pub fn enable(isar0: u64, kernel: bool) {
enable_features((isar0 >> 12) & 0xf >= 1, (isar0 >> 4) & 0xf >= 1, kernel);
}
/// Records what the core has, and whether this is the kernel: on x86-64
/// from CPUID (SHA-NI with SSSE3 and SSE4.1; AES-NI).
pub fn enable_features(sha256: bool, aes: bool, kernel: bool) {
let mut features = 0;
if sha256 {
features |= SHA256;
}
if aes {
features |= AES;
}
if kernel {
features |= KERNEL;
}
FEATURES.store(features, Ordering::Relaxed);
}
/// What the core has, as recorded. A program on Pinnacle's std (Cartopolis)
/// has no start that records it, as rt's does: it asks the kernel at the
/// first use. Without it TLS ran AES in software, two thirds of all the
/// time Cartopolis spent on its cores.
fn features() -> u8 {
#[cfg(target_os = "pinnacle")]
{
static ASKED: std::sync::Once = std::sync::Once::new();
ASKED.call_once(|| {
use std::os::pinnacle::{abi::call::CPU_FEATURES, syscall};
enable(syscall(CPU_FEATURES, [0; 7]).0, false);
});
}
FEATURES.load(Ordering::Relaxed)
}
/// Whether SHA-256 runs on the core's own instructions.
pub fn sha256() -> bool {
cfg!(any(target_arch = "aarch64", target_arch = "x86_64")) && features() & SHA256 != 0
}
/// Whether AES runs on the core's own instructions.
pub fn aes() -> bool {
cfg!(any(target_arch = "aarch64", target_arch = "x86_64")) && features() & AES != 0
}
/// Runs SHA-256's compression over `blocks` (whole 64-byte blocks) on the
/// core's instructions. Call only when [`sha256`] says so.
#[cfg(target_arch = "aarch64")]
pub fn sha256_blocks(state: &mut [u32; 8], blocks: &[u8]) {
assert!(blocks.len().is_multiple_of(64));
let count = blocks.len() / 64;
if count == 0 {
return;
}
let mut saved = [0u128; 32];
let masked = mask_interrupts();
// SAFETY: reads `count` blocks and the round constants, reads and
// writes `state`; v0–v9 and v16–v31 are saved to `saved` first and put
// back last, so no register the caller had changes.
unsafe {
core::arch::asm!(
".arch_extension fp",
".arch_extension simd",
".arch_extension sha2",
"stp q0, q1, [{saved}, #0]",
"stp q2, q3, [{saved}, #32]",
"stp q4, q5, [{saved}, #64]",
"stp q6, q7, [{saved}, #96]",
"stp q8, q9, [{saved}, #128]",
"stp q16, q17, [{saved}, #256]",
"stp q18, q19, [{saved}, #288]",
"stp q20, q21, [{saved}, #320]",
"stp q22, q23, [{saved}, #352]",
"stp q24, q25, [{saved}, #384]",
"stp q26, q27, [{saved}, #416]",
"stp q28, q29, [{saved}, #448]",
"stp q30, q31, [{saved}, #480]",
"ld1 {{v0.4s, v1.4s}}, [{state}]",
"ld1 {{v16.4s, v17.4s, v18.4s, v19.4s}}, [{k}], #64",
"ld1 {{v20.4s, v21.4s, v22.4s, v23.4s}}, [{k}], #64",
"ld1 {{v24.4s, v25.4s, v26.4s, v27.4s}}, [{k}], #64",
"ld1 {{v28.4s, v29.4s, v30.4s, v31.4s}}, [{k}]",
"2:",
"ld1 {{v4.16b, v5.16b, v6.16b, v7.16b}}, [{data}], #64",
"rev32 v4.16b, v4.16b",
"rev32 v5.16b, v5.16b",
"rev32 v6.16b, v6.16b",
"rev32 v7.16b, v7.16b",
"mov v2.16b, v0.16b",
"mov v3.16b, v1.16b",
"add v8.4s, v4.4s, v16.4s",
"sha256su0 v4.4s, v5.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v4.4s, v6.4s, v7.4s",
"add v8.4s, v5.4s, v17.4s",
"sha256su0 v5.4s, v6.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v5.4s, v7.4s, v4.4s",
"add v8.4s, v6.4s, v18.4s",
"sha256su0 v6.4s, v7.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v6.4s, v4.4s, v5.4s",
"add v8.4s, v7.4s, v19.4s",
"sha256su0 v7.4s, v4.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v7.4s, v5.4s, v6.4s",
"add v8.4s, v4.4s, v20.4s",
"sha256su0 v4.4s, v5.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v4.4s, v6.4s, v7.4s",
"add v8.4s, v5.4s, v21.4s",
"sha256su0 v5.4s, v6.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v5.4s, v7.4s, v4.4s",
"add v8.4s, v6.4s, v22.4s",
"sha256su0 v6.4s, v7.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v6.4s, v4.4s, v5.4s",
"add v8.4s, v7.4s, v23.4s",
"sha256su0 v7.4s, v4.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v7.4s, v5.4s, v6.4s",
"add v8.4s, v4.4s, v24.4s",
"sha256su0 v4.4s, v5.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v4.4s, v6.4s, v7.4s",
"add v8.4s, v5.4s, v25.4s",
"sha256su0 v5.4s, v6.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v5.4s, v7.4s, v4.4s",
"add v8.4s, v6.4s, v26.4s",
"sha256su0 v6.4s, v7.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v6.4s, v4.4s, v5.4s",
"add v8.4s, v7.4s, v27.4s",
"sha256su0 v7.4s, v4.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"sha256su1 v7.4s, v5.4s, v6.4s",
"add v8.4s, v4.4s, v28.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"add v8.4s, v5.4s, v29.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"add v8.4s, v6.4s, v30.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"add v8.4s, v7.4s, v31.4s",
"mov v9.16b, v0.16b",
"sha256h q0, q1, v8.4s",
"sha256h2 q1, q9, v8.4s",
"add v0.4s, v0.4s, v2.4s",
"add v1.4s, v1.4s, v3.4s",
"subs {count}, {count}, #1",
"b.ne 2b",
"st1 {{v0.4s, v1.4s}}, [{state}]",
"ldp q0, q1, [{saved}, #0]",
"ldp q2, q3, [{saved}, #32]",
"ldp q4, q5, [{saved}, #64]",
"ldp q6, q7, [{saved}, #96]",
"ldp q8, q9, [{saved}, #128]",
"ldp q16, q17, [{saved}, #256]",
"ldp q18, q19, [{saved}, #288]",
"ldp q20, q21, [{saved}, #320]",
"ldp q22, q23, [{saved}, #352]",
"ldp q24, q25, [{saved}, #384]",
"ldp q26, q27, [{saved}, #416]",
"ldp q28, q29, [{saved}, #448]",
"ldp q30, q31, [{saved}, #480]",
saved = in(reg) saved.as_mut_ptr(),
state = in(reg) state.as_mut_ptr(),
k = inout(reg) crate::sha2::K256.as_ptr() => _,
data = inout(reg) blocks.as_ptr() => _,
count = inout(reg) count => _,
options(nostack),
);
}
unmask_interrupts(masked);
}
#[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]
pub fn sha256_blocks(_state: &mut [u32; 8], _blocks: &[u8]) {
unreachable!("no SHA-256 instructions on this architecture");
}
/// In the kernel, masks interrupts on this core until [`unmask_interrupts`]:
/// no switch to a program that loads its own SIMD registers may land in the
/// middle of the instructions. Returns the mask found.
#[cfg(target_arch = "aarch64")]
fn mask_interrupts() -> Option<u64> {
if features() & KERNEL == 0 {
return None;
}
let daif: u64;
// SAFETY: masks interrupts on this core.
unsafe { core::arch::asm!("mrs {}, daif", "msr daifset, #0xf", out(reg) daif, options(nostack)) };
Some(daif)
}
#[cfg(target_arch = "aarch64")]
fn unmask_interrupts(masked: Option<u64>) {
if let Some(daif) = masked {
// SAFETY: puts back the mask found.
unsafe { core::arch::asm!("msr daif, {}", in(reg) daif, options(nostack)) };
}
}
/// AES over whole 16-byte blocks, in place, on the core's instructions:
/// AES-128 with 11 round keys, AES-256 with 15. Call only when [`aes`]
/// says so.
#[cfg(target_arch = "aarch64")]
pub fn aes_blocks(keys: &[[u8; 16]], blocks: &mut [u8]) {
match keys.len() {
11 => aes10_blocks(keys, blocks),
15 => aes14_blocks(keys, blocks),
n => unreachable!("AES has no {n} round keys"),
}
}
#[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]
pub fn aes_blocks(_keys: &[[u8; 16]], _blocks: &mut [u8]) {
unreachable!("no AES instructions on this architecture");
}
/// AES with 10 rounds over whole 16-byte blocks, in place, on the core's
/// instructions; `keys` are its 11 round keys.
#[cfg(target_arch = "aarch64")]
fn aes10_blocks(keys: &[[u8; 16]], blocks: &mut [u8]) {
assert!(keys.len() == 11 && blocks.len().is_multiple_of(16));
let count = blocks.len() / 16;
if count == 0 {
return;
}
let mut saved = [0u128; 32];
let masked = mask_interrupts();
// SAFETY: reads the round keys, reads and writes `count` blocks; v0
// and v16-v30 are saved first and put back last.
unsafe {
core::arch::asm!(
".arch_extension fp",
".arch_extension simd",
".arch_extension aes",
"str q0, [{saved}, #0]",
"stp q16, q17, [{saved}, #256]",
"stp q18, q19, [{saved}, #288]",
"stp q20, q21, [{saved}, #320]",
"stp q22, q23, [{saved}, #352]",
"stp q24, q25, [{saved}, #384]",
"stp q26, q27, [{saved}, #416]",
"stp q28, q29, [{saved}, #448]",
"str q30, [{saved}, #480]",
"ld1 {{v16.16b, v17.16b, v18.16b, v19.16b}}, [{k}], #64",
"ld1 {{v20.16b, v21.16b, v22.16b, v23.16b}}, [{k}], #64",
"ld1 {{v24.16b, v25.16b, v26.16b}}, [{k}]",
"2:",
"ld1 {{v0.16b}}, [{data}]",
"aese v0.16b, v16.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v17.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v18.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v19.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v20.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v21.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v22.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v23.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v24.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v25.16b",
"eor v0.16b, v0.16b, v26.16b",
"st1 {{v0.16b}}, [{data}], #16",
"subs {count}, {count}, #1",
"b.ne 2b",
"ldr q0, [{saved}, #0]",
"ldp q16, q17, [{saved}, #256]",
"ldp q18, q19, [{saved}, #288]",
"ldp q20, q21, [{saved}, #320]",
"ldp q22, q23, [{saved}, #352]",
"ldp q24, q25, [{saved}, #384]",
"ldp q26, q27, [{saved}, #416]",
"ldp q28, q29, [{saved}, #448]",
"ldr q30, [{saved}, #480]",
saved = in(reg) saved.as_mut_ptr(),
k = inout(reg) keys.as_ptr() => _,
data = inout(reg) blocks.as_mut_ptr() => _,
count = inout(reg) count => _,
options(nostack),
);
}
unmask_interrupts(masked);
}
/// AES with 14 rounds over whole 16-byte blocks, in place, on the core's
/// instructions; `keys` are its 15 round keys.
#[cfg(target_arch = "aarch64")]
fn aes14_blocks(keys: &[[u8; 16]], blocks: &mut [u8]) {
assert!(keys.len() == 15 && blocks.len().is_multiple_of(16));
let count = blocks.len() / 16;
if count == 0 {
return;
}
let mut saved = [0u128; 32];
let masked = mask_interrupts();
// SAFETY: reads the round keys, reads and writes `count` blocks; v0
// and v16-v30 are saved first and put back last.
unsafe {
core::arch::asm!(
".arch_extension fp",
".arch_extension simd",
".arch_extension aes",
"str q0, [{saved}, #0]",
"stp q16, q17, [{saved}, #256]",
"stp q18, q19, [{saved}, #288]",
"stp q20, q21, [{saved}, #320]",
"stp q22, q23, [{saved}, #352]",
"stp q24, q25, [{saved}, #384]",
"stp q26, q27, [{saved}, #416]",
"stp q28, q29, [{saved}, #448]",
"str q30, [{saved}, #480]",
"ld1 {{v16.16b, v17.16b, v18.16b, v19.16b}}, [{k}], #64",
"ld1 {{v20.16b, v21.16b, v22.16b, v23.16b}}, [{k}], #64",
"ld1 {{v24.16b, v25.16b, v26.16b, v27.16b}}, [{k}], #64",
"ld1 {{v28.16b, v29.16b, v30.16b}}, [{k}]",
"2:",
"ld1 {{v0.16b}}, [{data}]",
"aese v0.16b, v16.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v17.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v18.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v19.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v20.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v21.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v22.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v23.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v24.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v25.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v26.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v27.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v28.16b",
"aesmc v0.16b, v0.16b",
"aese v0.16b, v29.16b",
"eor v0.16b, v0.16b, v30.16b",
"st1 {{v0.16b}}, [{data}], #16",
"subs {count}, {count}, #1",
"b.ne 2b",
"ldr q0, [{saved}, #0]",
"ldp q16, q17, [{saved}, #256]",
"ldp q18, q19, [{saved}, #288]",
"ldp q20, q21, [{saved}, #320]",
"ldp q22, q23, [{saved}, #352]",
"ldp q24, q25, [{saved}, #384]",
"ldp q26, q27, [{saved}, #416]",
"ldp q28, q29, [{saved}, #448]",
"ldr q30, [{saved}, #480]",
saved = in(reg) saved.as_mut_ptr(),
k = inout(reg) keys.as_ptr() => _,
data = inout(reg) blocks.as_mut_ptr() => _,
count = inout(reg) count => _,
options(nostack),
);
}
unmask_interrupts(masked);
}
// x86-64: SHA-NI and AES-NI, with SSSE3 and SSE4.1 for the shuffles. The
// routines are written in assembly whole, xmm registers named in the text,
// because the kernel is built without SSE: it may not name them to the
// compiler. Each saves what it touches and puts it back.
/// SHA-256's compression over whole 64-byte blocks with SHA-NI, the state
/// kept as ABEF and CDGH the way its rounds want it. Call only when
/// [`sha256`] says so.
#[cfg(target_arch = "x86_64")]
pub fn sha256_blocks(state: &mut [u32; 8], blocks: &[u8]) {
/// Byte order within each word: the message is big-endian.
static SWAP: [u8; 16] = [3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12];
assert!(blocks.len().is_multiple_of(64));
let count = blocks.len() / 64;
if count == 0 {
return;
}
let mut saved = [0u128; 11];
let masked = mask_interrupts();
// SAFETY: reads `count` blocks, the round constants and the mask,
// reads and writes `state`; xmm0–xmm10 are saved first and put back
// last, so no register the caller had changes.
unsafe {
core::arch::asm!(
"movdqu [{saved} + 0], xmm0",
"movdqu [{saved} + 16], xmm1",
"movdqu [{saved} + 32], xmm2",
"movdqu [{saved} + 48], xmm3",
"movdqu [{saved} + 64], xmm4",
"movdqu [{saved} + 80], xmm5",
"movdqu [{saved} + 96], xmm6",
"movdqu [{saved} + 112], xmm7",
"movdqu [{saved} + 128], xmm8",
"movdqu [{saved} + 144], xmm9",
"movdqu [{saved} + 160], xmm10",
"movdqu xmm1, [{state}]",
"movdqu xmm2, [{state} + 16]",
"pshufd xmm1, xmm1, 0xb1",
"pshufd xmm2, xmm2, 0x1b",
"movdqa xmm7, xmm1",
"palignr xmm1, xmm2, 8",
"pblendw xmm2, xmm7, 0xf0",
"movdqu xmm8, [{swap}]",
"2:",
"movdqa xmm9, xmm1",
"movdqa xmm10, xmm2",
"movdqu xmm0, [{data} + 0]",
"pshufb xmm0, xmm8",
"movdqa xmm3, xmm0",
"movdqu xmm7, [{k} + 0]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"movdqu xmm0, [{data} + 16]",
"pshufb xmm0, xmm8",
"movdqa xmm4, xmm0",
"movdqu xmm7, [{k} + 16]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm3, xmm4",
"movdqu xmm0, [{data} + 32]",
"pshufb xmm0, xmm8",
"movdqa xmm5, xmm0",
"movdqu xmm7, [{k} + 32]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm4, xmm5",
"movdqu xmm0, [{data} + 48]",
"pshufb xmm0, xmm8",
"movdqa xmm6, xmm0",
"movdqu xmm7, [{k} + 48]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm6",
"palignr xmm7, xmm5, 4",
"paddd xmm3, xmm7",
"sha256msg2 xmm3, xmm6",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm5, xmm6",
"movdqa xmm0, xmm3",
"movdqu xmm7, [{k} + 64]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm3",
"palignr xmm7, xmm6, 4",
"paddd xmm4, xmm7",
"sha256msg2 xmm4, xmm3",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm6, xmm3",
"movdqa xmm0, xmm4",
"movdqu xmm7, [{k} + 80]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm4",
"palignr xmm7, xmm3, 4",
"paddd xmm5, xmm7",
"sha256msg2 xmm5, xmm4",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm3, xmm4",
"movdqa xmm0, xmm5",
"movdqu xmm7, [{k} + 96]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm5",
"palignr xmm7, xmm4, 4",
"paddd xmm6, xmm7",
"sha256msg2 xmm6, xmm5",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm4, xmm5",
"movdqa xmm0, xmm6",
"movdqu xmm7, [{k} + 112]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm6",
"palignr xmm7, xmm5, 4",
"paddd xmm3, xmm7",
"sha256msg2 xmm3, xmm6",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm5, xmm6",
"movdqa xmm0, xmm3",
"movdqu xmm7, [{k} + 128]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm3",
"palignr xmm7, xmm6, 4",
"paddd xmm4, xmm7",
"sha256msg2 xmm4, xmm3",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm6, xmm3",
"movdqa xmm0, xmm4",
"movdqu xmm7, [{k} + 144]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm4",
"palignr xmm7, xmm3, 4",
"paddd xmm5, xmm7",
"sha256msg2 xmm5, xmm4",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm3, xmm4",
"movdqa xmm0, xmm5",
"movdqu xmm7, [{k} + 160]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm5",
"palignr xmm7, xmm4, 4",
"paddd xmm6, xmm7",
"sha256msg2 xmm6, xmm5",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm4, xmm5",
"movdqa xmm0, xmm6",
"movdqu xmm7, [{k} + 176]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm6",
"palignr xmm7, xmm5, 4",
"paddd xmm3, xmm7",
"sha256msg2 xmm3, xmm6",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm5, xmm6",
"movdqa xmm0, xmm3",
"movdqu xmm7, [{k} + 192]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm3",
"palignr xmm7, xmm6, 4",
"paddd xmm4, xmm7",
"sha256msg2 xmm4, xmm3",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"sha256msg1 xmm6, xmm3",
"movdqa xmm0, xmm4",
"movdqu xmm7, [{k} + 208]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm4",
"palignr xmm7, xmm3, 4",
"paddd xmm5, xmm7",
"sha256msg2 xmm5, xmm4",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"movdqa xmm0, xmm5",
"movdqu xmm7, [{k} + 224]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"movdqa xmm7, xmm5",
"palignr xmm7, xmm4, 4",
"paddd xmm6, xmm7",
"sha256msg2 xmm6, xmm5",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"movdqa xmm0, xmm6",
"movdqu xmm7, [{k} + 240]",
"paddd xmm0, xmm7",
"sha256rnds2 xmm2, xmm1",
"pshufd xmm0, xmm0, 0x0e",
"sha256rnds2 xmm1, xmm2",
"paddd xmm1, xmm9",
"paddd xmm2, xmm10",
"add {data}, 64",
"dec {count}",
"jnz 2b",
"pshufd xmm1, xmm1, 0x1b",
"pshufd xmm2, xmm2, 0xb1",
"movdqa xmm7, xmm1",
"pblendw xmm1, xmm2, 0xf0",
"palignr xmm2, xmm7, 8",
"movdqu [{state}], xmm1",
"movdqu [{state} + 16], xmm2",
"movdqu xmm0, [{saved} + 0]",
"movdqu xmm1, [{saved} + 16]",
"movdqu xmm2, [{saved} + 32]",
"movdqu xmm3, [{saved} + 48]",
"movdqu xmm4, [{saved} + 64]",
"movdqu xmm5, [{saved} + 80]",
"movdqu xmm6, [{saved} + 96]",
"movdqu xmm7, [{saved} + 112]",
"movdqu xmm8, [{saved} + 128]",
"movdqu xmm9, [{saved} + 144]",
"movdqu xmm10, [{saved} + 160]",
saved = in(reg) saved.as_mut_ptr(),
state = in(reg) state.as_mut_ptr(),
swap = in(reg) SWAP.as_ptr(),
k = in(reg) crate::sha2::K256.as_ptr(),
data = inout(reg) blocks.as_ptr() => _,
count = inout(reg) count => _,
options(nostack),
);
}
unmask_interrupts(masked);
}
/// AES over whole 16-byte blocks, in place, with AES-NI: AES-128 with 11
/// round keys, AES-256 with 15, all held in registers. Call only when
/// [`aes`] says so.
#[cfg(target_arch = "x86_64")]
pub fn aes_blocks(keys: &[[u8; 16]], blocks: &mut [u8]) {
assert!(blocks.len().is_multiple_of(16));
let count = blocks.len() / 16;
if count == 0 {
return;
}
let mut saved = [0u128; 16];
let masked = mask_interrupts();
match keys.len() {
// SAFETY: reads the round keys, reads and writes `count` blocks;
// xmm0–xmm15 are saved first and put back last.
11 => unsafe {
core::arch::asm!(
"movdqu [{saved} + 0], xmm0",
"movdqu [{saved} + 16], xmm1",
"movdqu [{saved} + 32], xmm2",
"movdqu [{saved} + 48], xmm3",
"movdqu [{saved} + 64], xmm4",
"movdqu [{saved} + 80], xmm5",
"movdqu [{saved} + 96], xmm6",
"movdqu [{saved} + 112], xmm7",
"movdqu [{saved} + 128], xmm8",
"movdqu [{saved} + 144], xmm9",
"movdqu [{saved} + 160], xmm10",
"movdqu [{saved} + 176], xmm11",
"movdqu [{saved} + 192], xmm12",
"movdqu [{saved} + 208], xmm13",
"movdqu [{saved} + 224], xmm14",
"movdqu [{saved} + 240], xmm15",
"movdqu xmm1, [{k} + 0]",
"movdqu xmm2, [{k} + 16]",
"movdqu xmm3, [{k} + 32]",
"movdqu xmm4, [{k} + 48]",
"movdqu xmm5, [{k} + 64]",
"movdqu xmm6, [{k} + 80]",
"movdqu xmm7, [{k} + 96]",
"movdqu xmm8, [{k} + 112]",
"movdqu xmm9, [{k} + 128]",
"movdqu xmm10, [{k} + 144]",
"movdqu xmm11, [{k} + 160]",
"2:",
"movdqu xmm0, [{data}]",
"pxor xmm0, xmm1",
"aesenc xmm0, xmm2",
"aesenc xmm0, xmm3",
"aesenc xmm0, xmm4",
"aesenc xmm0, xmm5",
"aesenc xmm0, xmm6",
"aesenc xmm0, xmm7",
"aesenc xmm0, xmm8",
"aesenc xmm0, xmm9",
"aesenc xmm0, xmm10",
"aesenclast xmm0, xmm11",
"movdqu [{data}], xmm0",
"add {data}, 16",
"dec {count}",
"jnz 2b",
"movdqu xmm0, [{saved} + 0]",
"movdqu xmm1, [{saved} + 16]",
"movdqu xmm2, [{saved} + 32]",
"movdqu xmm3, [{saved} + 48]",
"movdqu xmm4, [{saved} + 64]",
"movdqu xmm5, [{saved} + 80]",
"movdqu xmm6, [{saved} + 96]",
"movdqu xmm7, [{saved} + 112]",
"movdqu xmm8, [{saved} + 128]",
"movdqu xmm9, [{saved} + 144]",
"movdqu xmm10, [{saved} + 160]",
"movdqu xmm11, [{saved} + 176]",
"movdqu xmm12, [{saved} + 192]",
"movdqu xmm13, [{saved} + 208]",
"movdqu xmm14, [{saved} + 224]",
"movdqu xmm15, [{saved} + 240]",
saved = in(reg) saved.as_mut_ptr(),
k = in(reg) keys.as_ptr(),
data = inout(reg) blocks.as_mut_ptr() => _,
count = inout(reg) count => _,
options(nostack),
);
},
// SAFETY: as above, with 15 round keys.
15 => unsafe {
core::arch::asm!(
"movdqu [{saved} + 0], xmm0",
"movdqu [{saved} + 16], xmm1",
"movdqu [{saved} + 32], xmm2",
"movdqu [{saved} + 48], xmm3",
"movdqu [{saved} + 64], xmm4",
"movdqu [{saved} + 80], xmm5",
"movdqu [{saved} + 96], xmm6",
"movdqu [{saved} + 112], xmm7",
"movdqu [{saved} + 128], xmm8",
"movdqu [{saved} + 144], xmm9",
"movdqu [{saved} + 160], xmm10",
"movdqu [{saved} + 176], xmm11",
"movdqu [{saved} + 192], xmm12",
"movdqu [{saved} + 208], xmm13",
"movdqu [{saved} + 224], xmm14",
"movdqu [{saved} + 240], xmm15",
"movdqu xmm1, [{k} + 0]",
"movdqu xmm2, [{k} + 16]",
"movdqu xmm3, [{k} + 32]",
"movdqu xmm4, [{k} + 48]",
"movdqu xmm5, [{k} + 64]",
"movdqu xmm6, [{k} + 80]",
"movdqu xmm7, [{k} + 96]",
"movdqu xmm8, [{k} + 112]",
"movdqu xmm9, [{k} + 128]",
"movdqu xmm10, [{k} + 144]",
"movdqu xmm11, [{k} + 160]",
"movdqu xmm12, [{k} + 176]",
"movdqu xmm13, [{k} + 192]",
"movdqu xmm14, [{k} + 208]",
"movdqu xmm15, [{k} + 224]",
"2:",
"movdqu xmm0, [{data}]",
"pxor xmm0, xmm1",
"aesenc xmm0, xmm2",
"aesenc xmm0, xmm3",
"aesenc xmm0, xmm4",
"aesenc xmm0, xmm5",
"aesenc xmm0, xmm6",
"aesenc xmm0, xmm7",
"aesenc xmm0, xmm8",
"aesenc xmm0, xmm9",
"aesenc xmm0, xmm10",
"aesenc xmm0, xmm11",
"aesenc xmm0, xmm12",
"aesenc xmm0, xmm13",
"aesenc xmm0, xmm14",
"aesenclast xmm0, xmm15",
"movdqu [{data}], xmm0",
"add {data}, 16",
"dec {count}",
"jnz 2b",
"movdqu xmm0, [{saved} + 0]",
"movdqu xmm1, [{saved} + 16]",
"movdqu xmm2, [{saved} + 32]",
"movdqu xmm3, [{saved} + 48]",
"movdqu xmm4, [{saved} + 64]",
"movdqu xmm5, [{saved} + 80]",
"movdqu xmm6, [{saved} + 96]",
"movdqu xmm7, [{saved} + 112]",
"movdqu xmm8, [{saved} + 128]",
"movdqu xmm9, [{saved} + 144]",
"movdqu xmm10, [{saved} + 160]",
"movdqu xmm11, [{saved} + 176]",
"movdqu xmm12, [{saved} + 192]",
"movdqu xmm13, [{saved} + 208]",
"movdqu xmm14, [{saved} + 224]",
"movdqu xmm15, [{saved} + 240]",
saved = in(reg) saved.as_mut_ptr(),
k = in(reg) keys.as_ptr(),
data = inout(reg) blocks.as_mut_ptr() => _,
count = inout(reg) count => _,
options(nostack),
);
},
n => unreachable!("AES has no {n} round keys"),
}
unmask_interrupts(masked);
}
/// In the kernel, masks interrupts on this core until [`unmask_interrupts`],
/// as on ARM. Returns the flags found.
#[cfg(target_arch = "x86_64")]
fn mask_interrupts() -> Option<u64> {
if features() & KERNEL == 0 {
return None;
}
let flags: u64;
// SAFETY: saves RFLAGS and clears IF on this core.
unsafe { core::arch::asm!("pushfq", "pop {}", "cli", out(reg) flags) };
Some(flags)
}
#[cfg(target_arch = "x86_64")]
fn unmask_interrupts(masked: Option<u64>) {
if let Some(flags) = masked {
// SAFETY: puts back the flags found, IF among them.
unsafe { core::arch::asm!("push {}", "popfq", in(reg) flags) };
}
}