Skip to main content

sha2/
sha512.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 [u64; 8], blocks: &[[u8; 128]]) {
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_512_backend = "x86-avx2")] {
19        mod x86_avx2;
20
21        #[cfg(not(target_feature = "avx2"))]
22        compile_error!("x86-avx2 backend requires avx2 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 { x86_avx2::compress(state, blocks) }
27        }
28    } else if #[cfg(sha2_512_backend = "aarch64-sha3")]  {
29        mod aarch64_sha3;
30
31        #[cfg(not(target_feature = "sha3"))]
32        compile_error!("aarch64-sha3 backend requires sha3 target feature");
33
34        fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
35            // SAFETY: we checked above that the required target features are enabled
36            unsafe { aarch64_sha3::compress(state, blocks) }
37        }
38    } else if #[cfg(target_arch = "loongarch64")] {
39        mod loongarch64_asm;
40        use loongarch64_asm::compress;
41    } else if #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))] {
42        mod wasm32_simd128;
43        use wasm32_simd128::compress;
44    } else {
45        mod soft;
46
47        cfg_if::cfg_if! {
48            if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
49                mod x86_avx2;
50                cpufeatures::new!(avx2_cpuid, "avx2");
51            } else if #[cfg(target_arch = "aarch64")] {
52                mod aarch64_sha3;
53                cpufeatures::new!(sha3_hwcap, "sha3");
54            }
55        }
56
57        fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
58            cfg_if::cfg_if! {
59                if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
60                    if avx2_cpuid::get() {
61                        // SAFETY: we checked that required target features are available
62                        return unsafe { x86_avx2::compress(state, blocks) };
63                    }
64                } else if #[cfg(target_arch = "aarch64")] {
65                    if sha3_hwcap::get() {
66                        // SAFETY: we checked that `sha3` target feature is available
67                        return unsafe { aarch64_sha3::compress(state, blocks) };
68                    }
69                }
70            }
71
72            soft::compress(state, blocks);
73        }
74    }
75}
76
77/// Raw SHA-512 compression function.
78///
79/// This is a low-level "hazmat" API which provides direct access to the core
80/// functionality of SHA-512.
81pub fn compress512(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
82    compress(state, blocks)
83}