1// Copyright 2024 Brian Smith.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15use super::{BlockLen, CHAINING_WORDS};
16use crate::{cpu, polyfill::slice::AsChunks};
17use cfg_if::cfg_if;
18use core::num::Wrapping;
19
20pub(in super::super) const SHA256_BLOCK_LEN: BlockLen = BlockLen::_512;
21
22pub type State32 = [Wrapping<u32>; CHAINING_WORDS];
23
24pub(crate) fn block_data_order_32(
25 state: &mut State32,
26 data: AsChunks<u8, { SHA256_BLOCK_LEN.into() }>,
27 cpu: cpu::Features,
28) {
29 cfg_if! {
30 if #[cfg(all(target_arch = "aarch64", target_endian = "little"))] {
31 use cpu::{GetFeature as _, arm::Sha256};
32 if let Some(cpu) = cpu.get_feature() {
33 sha2_32_ffi!(unsafe { Sha256 => sha256_block_data_order_hw }, state, data, cpu)
34 } else {
35 sha2_32_ffi!(unsafe { () => sha256_block_data_order_nohw }, state, data, ())
36 }
37 } else if #[cfg(all(target_arch = "arm", target_endian = "little"))] {
38 use cpu::{GetFeature as _, arm::Neon};
39 if let Some(cpu) = cpu.get_feature() {
40 sha2_32_ffi!(unsafe { Neon => sha256_block_data_order_neon }, state, data, cpu)
41 } else {
42 sha2_32_ffi!(unsafe { () => sha256_block_data_order_nohw }, state, data, ())
43 }
44 } else if #[cfg(target_arch = "x86_64")] {
45 use cpu::{GetFeature as _, intel::{Avx, IntelCpu, Sha, Ssse3 }};
46 let cpu = cpu.values();
47 if let Some(cpu) = cpu.get_feature() {
48 sha2_32_ffi!(unsafe { (Sha, Ssse3) => sha256_block_data_order_hw }, state, data, cpu)
49 } else if let Some(cpu) = cpu.get_feature() {
50 // Pre-Zen AMD CPUs had slow SHLD/SHRD; Zen added the SHA
51 // extension; see the discussion in upstream's sha1-586.pl.
52 sha2_32_ffi!(unsafe { (Avx, IntelCpu) => sha256_block_data_order_avx }, state, data, cpu)
53 } else if let Some(cpu) = cpu.get_feature() {
54 sha2_32_ffi!(unsafe { Ssse3 => sha256_block_data_order_ssse3 }, state, data, cpu)
55 } else {
56 sha2_32_ffi!(unsafe { () => sha256_block_data_order_nohw }, state, data, ())
57 }
58 } else {
59 let _ = cpu; // Unneeded.
60 *state = super::fallback::block_data_order(*state, data)
61 }
62 }
63}
64