sha2_asm/
lib.rs

1//! Assembly implementation of the [SHA-2] compression functions.
2//!
3//! This crate is not intended for direct use, most users should
4//! prefer the [`sha2`] crate with enabled `asm` feature instead.
5//!
6//! Only x86, x86-64, and (partially) AArch64 architectures are
7//! currently supported.
8//!
9//! [SHA-2]: https://en.wikipedia.org/wiki/SHA-2
10//! [`sha2`]: https://crates.io/crates/sha2
11
12#![no_std]
13#[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")))]
14compile_error!("crate can only be used on x86, x86-64 and aarch64 architectures");
15
16#[cfg(target_os = "windows")]
17compile_error!("crate does not support Windows targets");
18
19#[link(name = "sha256", kind = "static")]
20extern "C" {
21    fn sha256_compress(state: &mut [u32; 8], block: &[u8; 64]);
22}
23
24/// Safe wrapper around assembly implementation of SHA256 compression function
25#[inline]
26pub fn compress256(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
27    for block in blocks {
28        unsafe { sha256_compress(state, block) }
29    }
30}
31
32#[cfg(not(target_arch = "aarch64"))]
33#[link(name = "sha512", kind = "static")]
34extern "C" {
35    fn sha512_compress(state: &mut [u64; 8], block: &[u8; 128]);
36}
37
38/// Safe wrapper around assembly implementation of SHA512 compression function
39///
40/// This function is available only on x86 and x86-64 targets.
41#[cfg(not(target_arch = "aarch64"))]
42#[inline]
43pub fn compress512(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
44    for block in blocks {
45        unsafe { sha512_compress(state, block) }
46    }
47}