Skip to content

Commit a380194

Browse files
authored
Add x86 intrinsics support for sha1 and sha2 (#167)
1 parent 570a200 commit a380194

19 files changed

+709
-774
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sha1/Cargo.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ name = "sha1"
1818
digest = "0.9"
1919
block-buffer = "0.9"
2020
opaque-debug = "0.3"
21+
cfg-if = "0.1"
2122
sha1-asm = { version = "0.4", optional = true }
23+
24+
[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dependencies]
25+
cpuid-bool = "0.1"
26+
27+
[target.'cfg(all(target_arch = "aarch64", target_os = "linux"))'.dependencies]
2228
libc = { version = "0.2.68", optional = true }
2329

2430
[dev-dependencies]
@@ -28,8 +34,7 @@ hex-literal = "0.2"
2834
[features]
2935
default = ["std"]
3036
std = ["digest/std"]
31-
asm = ["sha1-asm"]
37+
asm = ["sha1-asm", "libc"]
3238

33-
# TODO: Remove this feature once is_aarch64_feature_detected!() is stabilised.
34-
# Only used on AArch64 Linux systems, when built without the crypto target_feature.
35-
asm-aarch64 = ["asm", "libc"]
39+
# DEPRECATED: use `asm` instead
40+
asm-aarch64 = ["asm"]

sha1/src/aarch64.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

sha1/src/compress.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use digest::consts::U64;
2+
use digest::generic_array::GenericArray;
3+
4+
cfg_if::cfg_if! {
5+
if #[cfg(all(feature = "asm", target_arch = "aarch64", target_os = "linux"))] {
6+
mod soft;
7+
mod aarch64;
8+
use aarch64::compress as compress_inner;
9+
} else if #[cfg(all(feature = "asm", any(target_arch = "x86", target_arch = "x86_64")))] {
10+
// TODO: replace after sha1-asm rework
11+
fn compress_inner(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
12+
for block in blocks {
13+
sha1_asm::compress(state, block);
14+
}
15+
}
16+
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
17+
mod soft;
18+
mod x86;
19+
use x86::compress as compress_inner;
20+
} else {
21+
mod soft;
22+
use soft::compress as compress_inner;
23+
}
24+
}
25+
26+
pub fn compress(state: &mut [u32; 5], blocks: &[GenericArray<u8, U64>]) {
27+
// SAFETY: GenericArray<u8, U64> and [u8; 64] have
28+
// exactly the same memory layout
29+
#[allow(unsafe_code)]
30+
let blocks: &[[u8; 64]] = unsafe { &*(blocks as *const _ as *const [[u8; 64]]) };
31+
compress_inner(state, blocks);
32+
}

sha1/src/compress/aarch64.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![cfg(feature = "asm-aarch64")]
2+
use libc::{getauxval, AT_HWCAP, HWCAP_SHA1};
3+
4+
fn sha1_supported() -> bool {
5+
#[allow(unsafe_code)]
6+
let hwcaps: u64 = unsafe { getauxval(AT_HWCAP) };
7+
(hwcaps & HWCAP_SHA1) != 0
8+
}
9+
10+
pub fn compress(state: &mut [u32; 5], blocks: &[u8; 64]) {
11+
// TODO: Replace this platform-specific call with is_aarch64_feature_detected!("sha1") once
12+
// that macro is stabilised and https://github.com/rust-lang/rfcs/pull/2725 is implemented
13+
// to let us use it on no_std.
14+
if sha1_supported() {
15+
for block in blocks {
16+
sha1_asm::compress(state, block);
17+
}
18+
} else {
19+
super::soft::compress(state, blocks);
20+
}
21+
}

0 commit comments

Comments
 (0)