forked from RustCrypto/asm-hashes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
83 lines (77 loc) · 2.7 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Assembly implementation of the [SHA-2] compression functions.
//!
//! This crate is not intended for direct use, most users should
//! prefer the [`sha2`] crate with enabled `asm` feature instead.
//!
//! Only x86, x86-64, and (partially) AArch64 architectures are
//! currently supported.
//!
//! [SHA-2]: https://en.wikipedia.org/wiki/SHA-2
//! [`sha2`]: https://crates.io/crates/sha2
#![no_std]
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")))]
compile_error!("crate can only be used on x86, x86-64 and aarch64 architectures");
#[link(name = "sha256", kind = "static")]
#[allow(dead_code)]
extern "C" {
fn sha256_compress(state: &mut [u32; 8], block: &[u8; 64]);
#[cfg(target_feature = "avx2")]
fn sha256_transform_rorx(state: &mut [u32; 8], block: *const [u8; 64], num_blocks: usize);
#[cfg(target_feature = "aes")]
fn sha256_ni_transform(digest: &mut [u32; 8], data: *const [u8; 64], nblk: u64);
}
cfg_if::cfg_if! {
if #[cfg(target_feature = "aes")]
{
#[inline]
pub fn compress256(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
if !blocks.is_empty() {
unsafe { sha256_ni_transform(state, blocks.as_ptr(), blocks.len() as u64) }
}
}
}
else if #[cfg(target_feature = "avx2")]
{
#[inline]
pub fn compress256(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
if !blocks.is_empty() {
unsafe { sha256_transform_rorx(state, blocks.as_ptr(), blocks.len()) }
}
}
}
else{
#[inline]
pub fn compress256(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
for block in blocks {
unsafe { sha256_compress(state, block) }
}
}
}
}
#[cfg(not(target_arch = "aarch64"))]
#[link(name = "sha512", kind = "static")]
extern "C" {
#[cfg(not(target_feature = "avx2"))]
fn sha512_compress(state: &mut [u64; 8], block: &[u8; 128]);
#[cfg(target_feature = "avx2")]
fn sha512_transform_rorx(state: &mut [u64; 8], block: *const [u8; 128], num_blocks: usize);
}
/// Safe wrapper around assembly implementation of SHA512 compression function
///
/// This function is available only on x86 and x86-64 targets.
#[inline]
#[cfg(not(target_arch = "aarch64"))]
#[cfg(target_feature = "avx2")]
pub fn compress512(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
if !blocks.is_empty() {
unsafe { sha512_transform_rorx(state, blocks.as_ptr(), blocks.len()) }
}
}
#[inline]
#[cfg(not(target_arch = "aarch64"))]
#[cfg(not(target_feature = "avx2"))]
pub fn compress512(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
for block in blocks {
unsafe { sha512_compress(state, block) }
}
}