Skip to main content

sha2/
sha256.rs

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