Skip to main content

sha1/
compress.rs

1cfg_if::cfg_if! {
2    if #[cfg(sha1_backend = "soft")] {
3        mod soft;
4        pub(crate) use soft::compress;
5    } else if #[cfg(sha1_backend = "aarch64-sha2")] {
6        mod aarch64_sha2;
7
8        #[cfg(not(target_feature = "sha2"))]
9        compile_error!("aarch64-sha2 backend requires sha2 target feature");
10
11        pub(crate) fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
12            // SAFETY: we checked above that the required target features are enabled
13            unsafe { aarch64_sha2::compress(state, blocks) }
14        }
15    } else if #[cfg(sha1_backend = "x86-sha")] {
16        mod x86_sha;
17
18        #[cfg(not(all(
19            target_feature = "sha",
20            target_feature = "sse2",
21            target_feature = "ssse3",
22            target_feature = "sse4.1",
23        )))]
24        compile_error!("x86-sha backend requires sha, sse2, ssse3, sse4.1 target features");
25
26        pub(crate) fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
27            // SAFETY: we checked above that the required target features are enabled
28            unsafe { x86_sha::compress(state, blocks) }
29        }
30    } else if #[cfg(target_arch = "loongarch64")] {
31        mod loongarch64_asm;
32        pub(crate) use loongarch64_asm::compress;
33    } else {
34        mod soft;
35
36        cfg_if::cfg_if! {
37            if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
38                mod x86_sha;
39                cpufeatures::new!(shani_cpuid, "sha", "sse2", "ssse3", "sse4.1");
40            } else if #[cfg(target_arch = "aarch64")] {
41                mod aarch64_sha2;
42                cpufeatures::new!(sha2_hwcap, "sha2");
43            }
44        }
45
46        pub(crate) fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
47            cfg_if::cfg_if! {
48                if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
49                    if shani_cpuid::get() {
50                        // SAFETY: we checked that required target features are available
51                        return unsafe { x86_sha::compress(state, blocks) };
52                    }
53                } else if #[cfg(target_arch = "aarch64")] {
54                    if sha2_hwcap::get() {
55                        // SAFETY: we checked that `sha2` target feature is available
56                        return unsafe { aarch64_sha2::compress(state, blocks) };
57                    }
58                }
59            }
60
61            soft::compress(state, blocks);
62        }
63    }
64}