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