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

feat(sha2): use latest sha2-asm and enable M1 #261

Merged
merged 5 commits into from May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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/dignifiedquire/asm-hashes", branch = "arm-m1" }
dignifiedquire marked this conversation as resolved.
Show resolved Hide resolved
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

2 changes: 1 addition & 1 deletion sha2/src/sha256.rs
Expand Up @@ -155,7 +155,7 @@ cfg_if::cfg_if! {
}
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: Verify this is true for all available chips.
Copy link
Member

@tarcieri tarcieri May 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is. See the discussion here:

RustCrypto/utils#378 (comment)

Perhaps leave a TODO to use the cpufeatures crate for this detection, once ARM 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