Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using VAES instructions for NI parallel operations. #396

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 71 additions & 2 deletions .github/workflows/aes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defaults:
env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-Dwarnings"
SDE_FULL_VERSION: "9.33.0-2024-01-07"

jobs:
# Builds for no_std platforms
Expand Down Expand Up @@ -59,7 +60,7 @@ jobs:
minimal-versions:
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
with:
working-directory: ${{ github.workflow }}
working-directory: ${{ github.workflow }}

# Tests for the AES-NI backend
aesni:
Expand Down Expand Up @@ -96,6 +97,75 @@ jobs:
- run: cargo test --target ${{ matrix.target }} --features hazmat
- run: cargo test --target ${{ matrix.target }} --all-features

# Tests for the VAES AVX backend
vaes256:
runs-on: ubuntu-latest
env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-Dwarnings --cfg disable_avx512"
strategy:
matrix:
include:
- target: x86_64-unknown-linux-gnu
rust: nightly-2024-02-07
steps:
- uses: actions/checkout@v4
- uses: silvanshade/rustcrypto-actions/intel-sde-install@master
with:
sde-full-version: ${{ env.SDE_FULL_VERSION }}
- uses: RustCrypto/actions/cargo-cache@master
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
# NOTE: Write a `.cargo/config.toml` to configure the target for VAES
# NOTE: We use intel-sde as the runner since not all GitHub CI hosts support AVX512
- name: write .cargo/config.toml
shell: bash
run: |
cd ../aes/..
mkdir -p .cargo
echo '[target.${{ matrix.target }}]' > .cargo/config.toml
echo 'runner = "sde64 -future --"' >> .cargo/config.toml
- run: ${{ matrix.deps }}
- run: cargo test --target ${{ matrix.target }}
- run: cargo test --target ${{ matrix.target }} --features hazmat
- run: cargo test --target ${{ matrix.target }} --all-features

# Tests for the VAES AVX512 backend
vaes512:
runs-on: ubuntu-latest
env:
CARGO_INCREMENTAL: 0
strategy:
matrix:
include:
- target: x86_64-unknown-linux-gnu
rust: nightly-2024-02-07
steps:
- uses: actions/checkout@v4
- uses: silvanshade/rustcrypto-actions/intel-sde-install@master
with:
sde-full-version: ${{ env.SDE_FULL_VERSION }}
- uses: RustCrypto/actions/cargo-cache@master
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
# NOTE: Write a `.cargo/config.toml` to configure the target for VAES
# NOTE: We use intel-sde as the runner since not all GitHub CI hosts support AVX512
- name: write .cargo/config.toml
shell: bash
run: |
cd ../aes/..
mkdir -p .cargo
echo '[target.${{ matrix.target }}]' > .cargo/config.toml
echo 'runner = "sde64 -future --"' >> .cargo/config.toml
- run: ${{ matrix.deps }}
- run: cargo test --target ${{ matrix.target }}
- run: cargo test --target ${{ matrix.target }} --features hazmat
- run: cargo test --target ${{ matrix.target }} --all-features

# Tests for CPU feature autodetection with fallback to portable software implementation
autodetect:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -159,7 +229,6 @@ jobs:
- run: cargo test --target ${{ matrix.target }}
- run: cargo test --target ${{ matrix.target }} --all-features


# Cross-compiled tests
cross:
strategy:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
target/
**/Cargo.lock
**/target/
**/pin.log
**/pin-log.txt
**/pin-tool-log.txt
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion aes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cipher = "=0.5.0-pre.2"
zeroize = { version = "1.5.6", optional = true, default_features = false, features = ["aarch64"] }

[target.'cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86"))'.dependencies]
cpufeatures = "0.2"
cpufeatures = "0.2.12"

[dev-dependencies]
cipher = { version = "=0.5.0-pre.2", features = ["dev"] }
Expand Down
37 changes: 9 additions & 28 deletions aes/src/armv8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ use cipher::{
use core::arch::aarch64::*;
use core::fmt;

pub(crate) mod features {
cpufeatures::new!(features_aes, "aes");
pub(crate) mod aes {
pub use super::features_aes::*;
}
}

macro_rules! define_aes_impl {
(
$name:ident,
Expand All @@ -50,18 +57,6 @@ macro_rules! define_aes_impl {
decrypt: $name_dec,
}

impl $name {
#[inline(always)]
pub(crate) fn get_enc_backend(&self) -> $name_back_enc<'_> {
self.encrypt.get_enc_backend()
}

#[inline(always)]
pub(crate) fn get_dec_backend(&self) -> $name_back_dec<'_> {
self.decrypt.get_dec_backend()
}
}

impl BlockCipher for $name {}

impl KeySizeUser for $name {
Expand Down Expand Up @@ -132,13 +127,6 @@ macro_rules! define_aes_impl {
round_keys: [uint8x16_t; $rounds],
}

impl $name_enc {
#[inline(always)]
pub(crate) fn get_enc_backend(&self) -> $name_back_enc<'_> {
$name_back_enc(self)
}
}

impl BlockCipher for $name_enc {}

impl KeySizeUser for $name_enc {
Expand All @@ -160,7 +148,7 @@ macro_rules! define_aes_impl {

impl BlockCipherEncrypt for $name_enc {
fn encrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
f.call(&mut self.get_enc_backend())
f.call(&mut $name_back_enc(self))
}
}

Expand Down Expand Up @@ -194,13 +182,6 @@ macro_rules! define_aes_impl {
round_keys: [uint8x16_t; $rounds],
}

impl $name_dec {
#[inline(always)]
pub(crate) fn get_dec_backend(&self) -> $name_back_dec<'_> {
$name_back_dec(self)
}
}

impl BlockCipher for $name_dec {}

impl KeySizeUser for $name_dec {
Expand Down Expand Up @@ -235,7 +216,7 @@ macro_rules! define_aes_impl {

impl BlockCipherDecrypt for $name_dec {
fn decrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
f.call(&mut self.get_dec_backend());
f.call(&mut $name_back_dec(self));
}
}

Expand Down
Loading
Loading