Skip to content

Commit

Permalink
feat(sha2): use latest sha2-asm and enable M1 (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 5, 2021
1 parent ee361ed commit e8b3abe
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 30 deletions.
33 changes: 16 additions & 17 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -20,3 +20,6 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io]
sha2-asm = { git = "https://github.com/RustCrypto/asm-hashes" }
3 changes: 2 additions & 1 deletion sha2/Cargo.toml
Expand Up @@ -19,7 +19,7 @@ digest = "0.9"
block-buffer = "0.9"
opaque-debug = "0.3"
cfg-if = "1.0"
sha2-asm = { version = "0.5", optional = true }
sha2-asm = { version = "0.6", optional = true }

[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dependencies]
cpuid-bool = "0.2"
Expand All @@ -38,3 +38,4 @@ asm = ["sha2-asm", "libc"]
compress = [] # Expose compress function
force-soft = [] # Force software implementation
asm-aarch64 = ["asm"] # DEPRECATED: use `asm` instead

6 changes: 2 additions & 4 deletions sha2/src/sha256.rs
Expand Up @@ -148,14 +148,12 @@ cfg_if::cfg_if! {
#[cfg(feature = "asm")]
mod soft {
pub(crate) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
for block in blocks {
sha2_asm::compress256(state, block);
}
sha2_asm::compress256(state, blocks);
}
}
mod x86;
use x86::compress;
} else if #[cfg(all(feature = "asm", target_arch = "aarch64", target_os = "linux"))] {
} else if #[cfg(all(feature = "asm", target_arch = "aarch64", any(target_os = "macos", target_os = "linux")))] {
mod soft;
mod aarch64;
use aarch64::compress;
Expand Down
16 changes: 11 additions & 5 deletions sha2/src/sha256/aarch64.rs
@@ -1,18 +1,24 @@
use libc::{getauxval, AT_HWCAP, HWCAP_SHA2};

#[cfg(target_os = "linux")]
#[inline(always)]
pub fn sha2_supported() -> bool {
use libc::{getauxval, AT_HWCAP, HWCAP_SHA2};

let hwcaps: u64 = unsafe { getauxval(AT_HWCAP) };
(hwcaps & HWCAP_SHA2) != 0
}

#[cfg(target_os = "macos")]
#[inline(always)]
pub fn sha2_supported() -> bool {
// TODO: Use cpufeatures once support lands
true
}

pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
// TODO: Replace with https://github.com/rust-lang/rfcs/pull/2725
// after stabilization
if sha2_supported() {
for block in blocks {
sha2_asm::compress256(state, block);
}
sha2_asm::compress256(state, blocks);
} else {
super::soft::compress(state, blocks);
}
Expand Down
4 changes: 1 addition & 3 deletions sha2/src/sha512.rs
Expand Up @@ -233,9 +233,7 @@ cfg_if::cfg_if! {
use soft::compress;
} else if #[cfg(all(feature = "asm", any(target_arch = "x86", target_arch = "x86_64")))] {
fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
for block in blocks {
sha2_asm::compress512(state, block);
}
sha2_asm::compress512(state, blocks);
}
} else {
mod soft;
Expand Down

0 comments on commit e8b3abe

Please sign in to comment.