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

sha2: use cpufeatures to detect sha2 on aarch64 #267

Merged
merged 1 commit into from May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions Cargo.lock

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

11 changes: 6 additions & 5 deletions sha2/Cargo.toml
Expand Up @@ -21,11 +21,12 @@ opaque-debug = "0.3"
cfg-if = "1.0"
sha2-asm = { version = "0.6.1", optional = true }

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

[target.aarch64-apple-darwin.dependencies]
tarcieri marked this conversation as resolved.
Show resolved Hide resolved
cpufeatures = { version = "0.1", git = "https://github.com/rustcrypto/utils.git" }
[target.'cfg(all(target_arch = "aarch64", target_os = "linux"))'.dependencies]
tarcieri marked this conversation as resolved.
Show resolved Hide resolved
libc = { version = "0.2.93", optional = true }
cpufeatures = { version = "0.1", git = "https://github.com/rustcrypto/utils.git" }
[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dependencies]
cpufeatures = { version = "0.1", git = "https://github.com/rustcrypto/utils.git" }

[dev-dependencies]
digest = { version = "0.9", features = ["dev"] }
Expand All @@ -34,7 +35,7 @@ hex-literal = "0.2"
[features]
default = ["std"]
std = ["digest/std"]
asm = ["sha2-asm", "libc"]
asm = ["sha2-asm"]
compress = [] # Expose compress function
force-soft = [] # Force software implementation
asm-aarch64 = ["asm"] # DEPRECATED: use `asm` instead
Expand Down
18 changes: 4 additions & 14 deletions sha2/src/sha256/aarch64.rs
@@ -1,23 +1,13 @@
#[cfg(target_os = "linux")]
#[inline(always)]
pub fn sha2_supported() -> bool {
use libc::{getauxval, AT_HWCAP, HWCAP_SHA2};
//! SHA-256 `aarch64` backend.

let hwcaps: u64 = unsafe { getauxval(AT_HWCAP) };
(hwcaps & HWCAP_SHA2) != 0
}
// TODO: stdarch intrinsics: RustCrypto/hashes#257

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

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() {
if sha2_hwcap::get() {
sha2_asm::compress256(state, blocks);
} else {
super::soft::compress(state, blocks);
Expand Down
2 changes: 2 additions & 0 deletions sha2/src/sha256/x86.rs
@@ -1,3 +1,5 @@
//! SHA-256 `x86`/`x86_64` backend

#![allow(clippy::many_single_char_names)]

#[cfg(target_arch = "x86")]
Expand Down