Skip to content

Commit

Permalink
feature = force-soft to disable hardware acceleration manually (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Feb 5, 2022
1 parent 284cc6d commit f32eccf
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 43 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ default = [

std = []
openssh = []
# Disable hardware acceleration.
# NOTE: aarch64 hardware acceleration requires core::arch to stablize intrinsics functions
force-soft = []

[package.metadata.docs.rs]
all-features = true
Expand Down
4 changes: 2 additions & 2 deletions src/blockcipher/aes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cfg_if::cfg_if;

cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), not(feature = "force-soft")))] {
#[path = "./x86.rs"]
mod platform;

Expand All @@ -15,7 +15,7 @@ cfg_if! {
pub use self::dynamic::*;
}
}
} else if #[cfg(target_arch = "aarch64")] {
} else if #[cfg(all(target_arch = "aarch64", not(feature = "force-soft")))] {
#[path = "./aarch64.rs"]
mod platform;
mod generic;
Expand Down
18 changes: 11 additions & 7 deletions src/hash/blake2b/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const BLAKE2B_512_IV: [u64; 8] = [
#[cfg(all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "avx2",
not(feature = "force-soft")
))]
#[path = "./x86/mod.rs"]
mod platform;
Expand All @@ -98,13 +99,16 @@ mod platform;
// #[path = "./aarch64.rs"]
// mod platform;

#[cfg(not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "avx2",
),
all(target_arch = "aarch64", target_feature = "crypto")
)))]
#[cfg(any(
not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "avx2",
),
all(target_arch = "aarch64", target_feature = "crypto")
)),
feature = "force-soft"
))]
#[path = "./generic.rs"]
mod platform;

Expand Down
18 changes: 11 additions & 7 deletions src/hash/blake2s/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const BLAKE2S_256_IV: [u32; 8] = [
#[cfg(all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "sse2",
not(feature = "force-soft")
))]
#[path = "./x86/mod.rs"]
mod platform;
Expand All @@ -58,13 +59,16 @@ mod platform;
// #[path = "./aarch64.rs"]
// mod platform;

#[cfg(not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "sse2",
),
all(target_arch = "aarch64", target_feature = "crypto")
)))]
#[cfg(any(
not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "sse2",
),
all(target_arch = "aarch64", target_feature = "crypto")
)),
feature = "force-soft"
))]
#[path = "./generic.rs"]
mod platform;

Expand Down
4 changes: 2 additions & 2 deletions src/hash/sha2/sha256/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cfg_if::cfg_if;
use core::convert::TryFrom;

cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), not(feature = "force-soft")))] {
mod x86;

cfg_if! {
Expand All @@ -31,7 +31,7 @@ cfg_if! {
}
}
}
} else if #[cfg(target_arch = "aarch64")] {
} else if #[cfg(all(target_arch = "aarch64", not(feature = "force-soft")))] {
mod aarch64;

cfg_if! {
Expand Down
6 changes: 3 additions & 3 deletions src/hash/sha2/sha512/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::convert::TryFrom;
// NOTE:
// 1. AArch64 架构在 ARMv8.4-A 版本里面增加了对 SHA2-512 的支持。
// 2. X86/X86_64 架构的 SHA-NI 目前并不包含 SHA2-512。
#[cfg(target_arch = "aarch64")]
#[cfg(all(target_arch = "aarch64", not(feature = "force-soft")))]
mod aarch64;
mod generic;

Expand Down Expand Up @@ -119,7 +119,7 @@ const SHA384_INITIAL_STATE: [u64; 8] = [
0x47B5481DBEFA4FA4,
];

#[cfg(target_arch = "aarch64")]
#[cfg(all(target_arch = "aarch64", not(feature = "force-soft")))]
#[inline]
fn transform(state: &mut [u64; 8], block: &[u8]) {
// Waiting `std_detect` to support AArch64 v8.4-A
Expand All @@ -134,7 +134,7 @@ fn transform(state: &mut [u64; 8], block: &[u8]) {
// }
}

#[cfg(not(target_arch = "aarch64"))]
#[cfg(any(not(target_arch = "aarch64"), feature = "force-soft"))]
#[inline]
fn transform(state: &mut [u64; 8], block: &[u8]) {
generic::transform(state, block)
Expand Down
4 changes: 2 additions & 2 deletions src/mac/ghash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use cfg_if::cfg_if;

cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), not(feature = "force-soft")))] {
#[path = "./x86.rs"]
mod platform;

Expand All @@ -17,7 +17,7 @@ cfg_if! {
pub use self::dynamic::GHash;
}
}
} else if #[cfg(target_arch = "aarch64")] {
} else if #[cfg(all(target_arch = "aarch64", not(feature = "force-soft")))] {
#[path = "./aarch64.rs"]
mod platform;

Expand Down
4 changes: 2 additions & 2 deletions src/mac/polyval/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cfg_if::cfg_if;

cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), not(feature = "force-soft")))] {
#[path = "./x86.rs"]
mod platform;

Expand All @@ -15,7 +15,7 @@ cfg_if! {
pub use self::dynamic::Polyval;
}
}
} else if #[cfg(target_arch = "aarch64")] {
} else if #[cfg(all(target_arch = "aarch64", not(feature = "force-soft")))] {
#[path = "./aarch64.rs"]
mod platform;

Expand Down
19 changes: 10 additions & 9 deletions src/util/and.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#[cfg(target_arch = "aarch64")]
use core::arch::aarch64::*;
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;

use cfg_if::cfg_if;

cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), not(feature = "force-soft")))] {
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;

#[target_feature(enable = "sse2")]
unsafe fn and_si128_inplace_sse2(a: &mut [u8], b: &[u8]) {
let mut c = _mm_loadu_si128(a.as_ptr() as *const __m128i);
Expand All @@ -34,7 +32,10 @@ cfg_if! {
}
}
}
} else if #[cfg(target_arch = "aarch64")] {
} else if #[cfg(all(target_arch = "aarch64", not(feature = "force-soft")))] {
#[cfg(target_arch = "aarch64")]
use core::arch::aarch64::*;

pub fn and_si128_inplace(a: &mut [u8], b: &[u8]) {
unsafe {
let c: *mut uint8x16_t = a.as_mut_ptr() as *mut uint8x16_t;
Expand Down
19 changes: 10 additions & 9 deletions src/util/xor.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#[cfg(target_arch = "aarch64")]
use core::arch::aarch64::*;
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;

use cfg_if::cfg_if;

cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), not(feature = "force-soft")))] {
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;

pub fn xor_si128_inplace_sse2(a: &mut [u8], b: &[u8]) {
unsafe {
let mut c = _mm_loadu_si128(a.as_ptr() as *const __m128i);
Expand All @@ -33,7 +31,10 @@ cfg_if! {
xor_si128_inplace_generic(a, b)
}
}
} else if #[cfg(target_arch = "aarch64")] {
} else if #[cfg(all(target_arch = "aarch64", not(feature = "force-soft")))] {
#[cfg(target_arch = "aarch64")]
use core::arch::aarch64::*;

pub fn xor_si128_inplace(a: &mut [u8], b: &[u8]) {
unsafe {
let c: *mut uint8x16_t = a.as_mut_ptr() as *mut uint8x16_t;
Expand Down

0 comments on commit f32eccf

Please sign in to comment.