Skip to main content
This is unreleased documentation for the main (development) branch of crypto-glue.

sha2/
sha256.rs

1cfg_if::cfg_if! {
2    if #[cfg(sha2_backend = "soft")] {
3        mod soft;
4        use soft::compress;
5    } else if #[cfg(sha2_backend = "soft-compact")] {
6        mod soft_compact;
7        use soft_compact::compress;
8    } else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
9        mod soft;
10        mod x86_shani;
11        use x86_shani::compress;
12    } else if #[cfg(all(
13        any(target_arch = "riscv32", target_arch = "riscv64"),
14        sha2_backend = "riscv-zknh"
15    ))] {
16        mod riscv_zknh;
17        mod riscv_zknh_utils;
18        use riscv_zknh::compress;
19    } else if #[cfg(all(
20        any(target_arch = "riscv32", target_arch = "riscv64"),
21        sha2_backend = "riscv-zknh-compact"
22    ))] {
23        mod riscv_zknh_compact;
24        mod riscv_zknh_utils;
25        use riscv_zknh_compact::compress;
26    } else if #[cfg(target_arch = "aarch64")] {
27        mod soft;
28        mod aarch64_sha2;
29        use aarch64_sha2::compress;
30    } else if #[cfg(target_arch = "loongarch64")] {
31        mod loongarch64_asm;
32        use loongarch64_asm::compress;
33    } else if #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))] {
34        mod wasm32_simd128;
35        use wasm32_simd128::compress;
36    } else {
37        mod soft;
38        use soft::compress;
39    }
40}
41
42#[inline(always)]
43#[allow(dead_code)]
44fn to_u32s(block: &[u8; 64]) -> [u32; 16] {
45    core::array::from_fn(|i| {
46        let chunk = block[4 * i..][..4].try_into().unwrap();
47        u32::from_be_bytes(chunk)
48    })
49}
50
51/// Raw SHA-256 compression function.
52///
53/// This is a low-level "hazmat" API which provides direct access to the core
54/// functionality of SHA-256.
55pub fn compress256(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
56    compress(state, blocks)
57}