Skip to content

Commit

Permalink
Major changes
Browse files Browse the repository at this point in the history
Changed the feature detection and impl selection to be build-time instead of compile-time.
Exposed `pre_enc` and its friends
Fixed some typos in doc
  • Loading branch information
sayantn committed Aug 22, 2024
1 parent 6f92046 commit 02ca0bd
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 184 deletions.
65 changes: 65 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#![cfg_attr(
all(feature = "nightly", target_arch = "arm", target_feature = "v8"),
feature(stdarch_arm_feature_detection)
)]
#![cfg_attr(
all(
feature = "nightly",
any(target_arch = "riscv64", target_arch = "riscv32")
),
feature(stdarch_riscv_feature_detection)
)]
use std::arch::*;

fn select_impl() -> &'static str {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
if is_x86_feature_detected!("aes") && is_x86_feature_detected!("sse4.1") {
return "x86";
}
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))]
if is_aarch64_feature_detected!("aes") {
return "neon";
}
#[cfg(all(feature = "nightly", target_arch = "arm", target_feature = "v8"))]
if is_arm_feature_detected!("aes") {
return "arm-neon";
}
#[cfg(all(
feature = "nightly",
any(target_arch = "riscv64", target_arch = "riscv32")
))]
if is_riscv_feature_detected!("zkne") && is_riscv_feature_detected!("zknd") {
return "risc-v";
}
"software"
}

fn select_x2_impl() -> &'static str {
#[cfg(all(feature = "nightly", any(target_arch = "x86", target_arch = "x86_64")))]
if is_x86_feature_detected!("vaes") {
return "vaes";
}
"tuple"
}

fn select_x4_impl() -> &'static str {
#[cfg(all(feature = "nightly", any(target_arch = "x86", target_arch = "x86_64")))]
if is_x86_feature_detected!("avx512f") {
return "avx512f";
}
"tuple"
}

fn main() {
println!("cargo:rerun-if-changed=build.rs");

println!(
"cargo:rustc-check-cfg=cfg(aes_impl, values(\"x86\", \"neon\", \"arm-neon\", \"risc-v\", \"software\"))"
);
println!("cargo:rustc-check-cfg=cfg(aes_x2_impl, values(\"vaes\", \"tuple\"))");
println!("cargo:rustc-check-cfg=cfg(aes_x4_impl, values(\"avx512f\", \"tuple\"))");

println!("cargo:rustc-cfg=aes_impl=\"{}\"", select_impl());
println!("cargo:rustc-cfg=aes_x2_impl=\"{}\"", select_x2_impl());
println!("cargo:rustc-cfg=aes_x4_impl=\"{}\"", select_x4_impl());
}
18 changes: 11 additions & 7 deletions src/aes_arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ impl AesBlock {
}
}

/// /// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes`
#[inline(always)]
pub(crate) fn pre_enc_last(self, round_key: Self) -> Self {
pub fn pre_enc_last(self, round_key: Self) -> Self {
Self(unsafe { vaeseq_u8(self.0, round_key.0) })
}

/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes` -> `MixColumns`
#[inline(always)]
pub(crate) fn pre_enc(self, round_key: Self) -> Self {
pub fn pre_enc(self, round_key: Self) -> Self {
self.pre_enc_last(round_key).mc()
}

Expand All @@ -128,17 +130,19 @@ impl AesBlock {
self.pre_enc(Self::zero()) ^ round_key
}

/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes`
#[inline(always)]
pub(crate) fn pre_dec_last(self, round_key: Self) -> Self {
pub fn pre_dec_last(self, round_key: Self) -> Self {
Self(unsafe { vaesdq_u8(self.0, round_key.0) })
}

/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes` -> `InvMixColumns`
#[inline(always)]
pub(crate) fn pre_dec(self, round_key: Self) -> Self {
pub fn pre_dec(self, round_key: Self) -> Self {
self.pre_dec_last(round_key).imc()
}

/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
#[inline]
pub fn dec(self, round_key: Self) -> Self {
self.pre_dec(Self::zero()) ^ round_key
Expand All @@ -150,7 +154,7 @@ impl AesBlock {
self.pre_enc_last(Self::zero()) ^ round_key
}

/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
#[inline]
pub fn dec_last(self, round_key: Self) -> Self {
self.pre_dec_last(Self::zero()) ^ round_key
Expand All @@ -162,7 +166,7 @@ impl AesBlock {
Self(unsafe { vaesmcq_u8(self.0) })
}

/// Performs the `InvMixColumn`s operation
/// Performs the `InvMixColumns` operation
#[inline]
pub fn imc(self) -> Self {
Self(unsafe { vaesimcq_u8(self.0) })
Expand Down
6 changes: 3 additions & 3 deletions src/aes_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl AesBlock {
)
}

/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
#[inline]
pub fn dec(self, round_key: Self) -> Self {
Self(
Expand All @@ -160,7 +160,7 @@ impl AesBlock {
)
}

/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
#[inline]
pub fn dec_last(self, round_key: Self) -> Self {
Self(
Expand Down Expand Up @@ -210,7 +210,7 @@ impl AesBlock {
)
}

/// Performs the `InvMixColumn`s operation
/// Performs the `InvMixColumns` operation
#[inline]
pub fn imc(self) -> Self {
Self(
Expand Down
18 changes: 11 additions & 7 deletions src/aes_riscv32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ impl AesBlock {
(self.0 | self.1 | self.2 | self.3) == 0
}

/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes` -> `MixColumns`
#[inline(always)]
pub(crate) fn pre_enc(self, round_key: Self) -> Self {
pub fn pre_enc(self, round_key: Self) -> Self {
outer!(aes32esmi, self, round_key)
}

Expand All @@ -133,8 +134,9 @@ impl AesBlock {
self.pre_enc(Self::zero()) ^ round_key
}

/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes`
#[inline(always)]
pub(crate) fn pre_enc_last(self, round_key: Self) -> Self {
pub fn pre_enc_last(self, round_key: Self) -> Self {
outer!(aes32esi, self, round_key)
}

Expand All @@ -144,23 +146,25 @@ impl AesBlock {
self.pre_enc_last(Self::zero()) ^ round_key
}

/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes` -> `InvMixColumns`
#[inline(always)]
pub(crate) fn pre_dec(self, round_key: Self) -> Self {
pub fn pre_dec(self, round_key: Self) -> Self {
outer!(aes32dsmi, self, round_key)
}

/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
#[inline]
pub fn dec(self, round_key: Self) -> Self {
self.pre_dec(Self::zero()) ^ round_key
}

/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes`
#[inline(always)]
pub(crate) fn pre_dec_last(self, round_key: Self) -> Self {
pub fn pre_dec_last(self, round_key: Self) -> Self {
outer!(aes32dsi, self, round_key)
}

/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
#[inline]
pub fn dec_last(self, round_key: Self) -> Self {
self.pre_dec_last(Self::zero()) ^ round_key
Expand All @@ -172,7 +176,7 @@ impl AesBlock {
self.pre_dec_last(Self::zero()).enc(Self::zero())
}

/// Performs the `InvMixColumn`s operation
/// Performs the `InvMixColumns` operation
#[inline]
pub fn imc(self) -> Self {
self.pre_enc_last(Self::zero()).dec(Self::zero())
Expand Down
6 changes: 3 additions & 3 deletions src/aes_riscv64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl AesBlock {
}
}

/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
#[inline]
pub fn dec(self, round_key: Self) -> Self {
unsafe {
Expand All @@ -124,7 +124,7 @@ impl AesBlock {
}
}

/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
#[inline]
pub fn dec_last(self, round_key: Self) -> Self {
unsafe {
Expand All @@ -144,7 +144,7 @@ impl AesBlock {
}
}

/// Performs the `InvMixColumn`s operation
/// Performs the `InvMixColumns` operation
#[inline]
pub fn imc(self) -> Self {
unsafe { Self(aes64im(self.0), aes64im(self.1)) }
Expand Down
6 changes: 3 additions & 3 deletions src/aes_x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl AesBlock {
Self(unsafe { _mm_aesenc_si128(self.0, round_key.0) })
}

/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
#[inline]
pub fn dec(self, round_key: Self) -> Self {
Self(unsafe { _mm_aesdec_si128(self.0, round_key.0) })
Expand All @@ -103,7 +103,7 @@ impl AesBlock {
Self(unsafe { _mm_aesenclast_si128(self.0, round_key.0) })
}

/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
#[inline]
pub fn dec_last(self, round_key: Self) -> Self {
Self(unsafe { _mm_aesdeclast_si128(self.0, round_key.0) })
Expand All @@ -120,7 +120,7 @@ impl AesBlock {
})
}

/// Performs the `InvMixColumn`s operation
/// Performs the `InvMixColumns` operation
#[inline]
pub fn imc(self) -> Self {
Self(unsafe { _mm_aesimc_si128(self.0) })
Expand Down
34 changes: 32 additions & 2 deletions src/aesdefault_x2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,55 @@ impl AesBlockX2 {
self.0.is_zero() & self.1.is_zero()
}

/// Performs the operation `AddRoundKey` -> `SubBytes` -> `ShiftRows` -> `MixColumns`
#[inline]
pub fn pre_enc(self, round_key: Self) -> Self {
Self(self.0.pre_enc(round_key.0), self.1.pre_enc(round_key.1))
}

/// Performs one round of AES encryption function (`ShiftRows`->`SubBytes`->`MixColumns`->`AddRoundKey`)
#[inline]
pub fn enc(self, round_key: Self) -> Self {
Self(self.0.enc(round_key.0), self.1.enc(round_key.1))
}

/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes` -> `InvMixColumns`
#[inline]
pub fn pre_dec(self, round_key: Self) -> Self {
Self(self.0.pre_dec(round_key.0), self.1.pre_dec(round_key.1))
}

/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
#[inline]
pub fn dec(self, round_key: Self) -> Self {
Self(self.0.dec(round_key.0), self.1.dec(round_key.1))
}

/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes`
#[inline]
pub fn pre_enc_last(self, round_key: Self) -> Self {
Self(
self.0.pre_enc_last(round_key.0),
self.1.pre_enc_last(round_key.1),
)
}

/// Performs one round of AES encryption function without `MixColumns` (`ShiftRows`->`SubBytes`->`AddRoundKey`)
#[inline]
pub fn enc_last(self, round_key: Self) -> Self {
Self(self.0.enc_last(round_key.0), self.1.enc_last(round_key.1))
}

/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes`
#[inline]
pub fn pre_dec_last(self, round_key: Self) -> Self {
Self(
self.0.pre_dec_last(round_key.0),
self.1.pre_dec_last(round_key.1),
)
}

/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
#[inline]
pub fn dec_last(self, round_key: Self) -> Self {
Self(self.0.dec_last(round_key.0), self.1.dec_last(round_key.1))
Expand Down
34 changes: 32 additions & 2 deletions src/aesdefault_x4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,55 @@ impl AesBlockX4 {
self.0.is_zero() & self.1.is_zero()
}

/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes` -> `MixColumns`
#[inline]
pub fn pre_enc(self, round_key: Self) -> Self {
Self(self.0.pre_enc(round_key.0), self.1.pre_enc(round_key.1))
}

/// Performs one round of AES encryption function (`ShiftRows`->`SubBytes`->`MixColumns`->`AddRoundKey`)
#[inline]
pub fn enc(self, round_key: Self) -> Self {
Self(self.0.enc(round_key.0), self.1.enc(round_key.1))
}

/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes` -> `InvMixColumns`
#[inline]
pub fn pre_dec(self, round_key: Self) -> Self {
Self(self.0.pre_dec(round_key.0), self.1.pre_dec(round_key.1))
}

/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
#[inline]
pub fn dec(self, round_key: Self) -> Self {
Self(self.0.dec(round_key.0), self.1.dec(round_key.1))
}

/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes`
#[inline]
pub fn pre_enc_last(self, round_key: Self) -> Self {
Self(
self.0.pre_enc_last(round_key.0),
self.1.pre_enc_last(round_key.1),
)
}

/// Performs one round of AES encryption function without `MixColumns` (`ShiftRows`->`SubBytes`->`AddRoundKey`)
#[inline]
pub fn enc_last(self, round_key: Self) -> Self {
Self(self.0.enc_last(round_key.0), self.1.enc_last(round_key.1))
}

/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes`
#[inline]
pub fn pre_dec_last(self, round_key: Self) -> Self {
Self(
self.0.pre_dec_last(round_key.0),
self.1.pre_dec_last(round_key.1),
)
}

/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
#[inline]
pub fn dec_last(self, round_key: Self) -> Self {
Self(self.0.dec_last(round_key.0), self.1.dec_last(round_key.1))
Expand Down
Loading

0 comments on commit 02ca0bd

Please sign in to comment.