Debian Codegraph @

debian/rust-vendor/sha2/src/sha256/aarch64_sha2.rs

raw
1 //! SHA-256 `aarch64` backend.
2 //!
3 //! Implementation adapted from mbedtls.
4 #![allow(unsafe_op_in_unsafe_fn)]
5
6 #[cfg(not(target_arch = "aarch64"))]
7 compile_error!("aarch64-sha2 backend can be used only aarch64 target arches");
8
9 use crate::consts::K32;
10 use core::arch::aarch64::*;
11
12 #[target_feature(enable = "sha2")]
13 pub(super) unsafe fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
14 // SAFETY: Requires the sha2 feature.
15
16 // Load state into vectors.
17 let mut abcd = vld1q_u32(state[0..4].as_ptr());
18 let mut efgh = vld1q_u32(state[4..8].as_ptr());
19
20 // Iterate through the message blocks.
21 for block in blocks {
22 // Keep original state values.
23 let abcd_orig = abcd;
24 let efgh_orig = efgh;
25
26 // Load the message block into vectors, assuming little endianness.
27 let mut s0 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[0..16].as_ptr())));
28 let mut s1 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[16..32].as_ptr())));
29 let mut s2 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[32..48].as_ptr())));
30 let mut s3 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[48..64].as_ptr())));
31
32 // Rounds 0 to 3
33 let mut tmp = vaddq_u32(s0, vld1q_u32(&K32[0]));
34 let mut abcd_prev = abcd;
35 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
36 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
37
38 // Rounds 4 to 7
39 tmp = vaddq_u32(s1, vld1q_u32(&K32[4]));
40 abcd_prev = abcd;
41 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
42 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
43
44 // Rounds 8 to 11
45 tmp = vaddq_u32(s2, vld1q_u32(&K32[8]));
46 abcd_prev = abcd;
47 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
48 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
49
50 // Rounds 12 to 15
51 tmp = vaddq_u32(s3, vld1q_u32(&K32[12]));
52 abcd_prev = abcd;
53 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
54 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
55
56 for t in (16..64).step_by(16) {
57 // Rounds t to t + 3
58 s0 = vsha256su1q_u32(vsha256su0q_u32(s0, s1), s2, s3);
59 tmp = vaddq_u32(s0, vld1q_u32(&K32[t]));
60 abcd_prev = abcd;
61 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
62 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
63
64 // Rounds t + 4 to t + 7
65 s1 = vsha256su1q_u32(vsha256su0q_u32(s1, s2), s3, s0);
66 tmp = vaddq_u32(s1, vld1q_u32(&K32[t + 4]));
67 abcd_prev = abcd;
68 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
69 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
70
71 // Rounds t + 8 to t + 11
72 s2 = vsha256su1q_u32(vsha256su0q_u32(s2, s3), s0, s1);
73 tmp = vaddq_u32(s2, vld1q_u32(&K32[t + 8]));
74 abcd_prev = abcd;
75 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
76 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
77
78 // Rounds t + 12 to t + 15
79 s3 = vsha256su1q_u32(vsha256su0q_u32(s3, s0), s1, s2);
80 tmp = vaddq_u32(s3, vld1q_u32(&K32[t + 12]));
81 abcd_prev = abcd;
82 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
83 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
84 }
85
86 // Add the block-specific state to the original state.
87 abcd = vaddq_u32(abcd, abcd_orig);
88 efgh = vaddq_u32(efgh, efgh_orig);
89 }
90
91 // Store vectors into state.
92 vst1q_u32(state[0..4].as_mut_ptr(), abcd);
93 vst1q_u32(state[4..8].as_mut_ptr(), efgh);
94 }