|
1 | | -const K: [u32; 4] = [0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6]; |
2 | | - |
3 | 1 | cfg_if::cfg_if! { |
4 | | - if #[cfg(feature = "force-soft")] { |
5 | | - mod soft; |
6 | | - use soft::compress as compress_inner; |
7 | | - } else if #[cfg(all(target_arch = "aarch64"))] { |
| 2 | + if #[cfg(sha1_backend = "soft")] { |
8 | 3 | mod soft; |
9 | | - mod aarch64; |
10 | | - use aarch64::compress as compress_inner; |
| 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 | + } |
11 | 30 | } else if #[cfg(target_arch = "loongarch64")] { |
12 | 31 | mod loongarch64_asm; |
13 | | - use loongarch64_asm::compress as compress_inner; |
14 | | - } else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { |
15 | | - mod soft; |
16 | | - mod x86; |
17 | | - use x86::compress as compress_inner; |
| 32 | + pub(crate) use loongarch64_asm::compress; |
18 | 33 | } else { |
19 | 34 | mod soft; |
20 | | - use soft::compress as compress_inner; |
21 | | - } |
22 | | -} |
23 | 35 |
|
24 | | -/// SHA-1 compression function |
25 | | -pub fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) { |
26 | | - compress_inner(state, blocks); |
| 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 | + } |
27 | 64 | } |
0 commit comments