Skip to main content

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 = "riscv-zknh")] {
6        mod riscv_zknh;
7
8        #[cfg(not(target_feature = "zknh"))]
9        compile_error!("riscv-zknh backend requires `zknh` target feature");
10
11        fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
12            // SAFETY: we checked above that the required target features are enabled
13            unsafe { riscv_zknh::compress(state, blocks) }
14        }
15    } else if #[cfg(sha2_256_backend = "soft")] {
16        mod soft;
17        use soft::compress;
18    } else if #[cfg(sha2_256_backend = "riscv-zknh")] {
19        mod riscv_zknh;
20
21        #[cfg(not(target_feature = "zknh"))]
22        compile_error!("riscv-zknh backend requires `zknh` target feature");
23
24        fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
25            // SAFETY: we checked above that the required target features are enabled
26            unsafe { riscv_zknh::compress(state, blocks) }
27        }
28    } else if #[cfg(sha2_256_backend = "x86-sha")] {
29        mod x86_sha;
30
31        #[cfg(not(all(
32            target_feature = "sha",
33            target_feature = "sse4.1",
34        )))]
35        compile_error!("x86-sha backend requires `sha` and `sse4.1` target features");
36
37        fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
38            // SAFETY: we checked above that the required target features are enabled
39            unsafe { x86_sha::compress(state, blocks) }
40        }
41    } else if #[cfg(sha2_256_backend = "aarch64-sha2")] {
42        mod aarch64_sha2;
43
44        #[cfg(not(target_feature = "sha2"))]
45        compile_error!("aarch64-sha2 backend requires `sha2` target feature");
46
47        fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
48            // SAFETY: we checked above that the required target features are enabled
49            unsafe { aarch64_sha2::compress(state, blocks) }
50        }
51    } else if #[cfg(target_arch = "loongarch64")] {
52        mod loongarch64_asm;
53        use loongarch64_asm::compress;
54    } else if #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))] {
55        mod wasm32_simd128;
56        use wasm32_simd128::compress;
57    } else {
58        mod soft;
59
60        cfg_if::cfg_if! {
61            if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
62                mod x86_sha;
63                cpufeatures::new!(shani_cpuid, "sha", "sse4.1");
64            } else if #[cfg(target_arch = "aarch64")] {
65                mod aarch64_sha2;
66                cpufeatures::new!(sha2_hwcap, "sha2");
67            }
68        }
69
70        fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
71            cfg_if::cfg_if! {
72                if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
73                    if shani_cpuid::get() {
74                        // SAFETY: we checked that required target features are available
75                        return unsafe { x86_sha::compress(state, blocks) };
76                    }
77                } else if #[cfg(target_arch = "aarch64")] {
78                    if sha2_hwcap::get() {
79                        // SAFETY: we checked that `sha2` target feature is available
80                        return unsafe { aarch64_sha2::compress(state, blocks) };
81                    }
82                }
83            }
84
85            soft::compress(state, blocks);
86        }
87    }
88}
89
90/// Raw SHA-256 compression function.
91///
92/// This is a low-level "hazmat" API which provides direct access to the core
93/// functionality of SHA-256.
94pub fn compress256(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
95    compress(state, blocks)
96}