Skip to main content

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