Debian Codegraph @

debian/rust-vendor/sha2/src/sha512/aarch64_sha3.rs

raw
1 // Implementation adapted from mbedtls.
2 #![allow(unsafe_op_in_unsafe_fn)]
3
4 #[cfg(not(target_arch = "aarch64"))]
5 compile_error!("aarch64-sha3 backend can be used only aarch64 target arches");
6
7 use crate::consts::K64;
8 use core::arch::aarch64::*;
9
10 #[target_feature(enable = "sha3")]
11 pub(super) unsafe fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
12 // SAFETY: Requires the sha3 feature.
13
14 // Load state into vectors.
15 let mut ab = vld1q_u64(state[0..2].as_ptr());
16 let mut cd = vld1q_u64(state[2..4].as_ptr());
17 let mut ef = vld1q_u64(state[4..6].as_ptr());
18 let mut gh = vld1q_u64(state[6..8].as_ptr());
19
20 // Iterate through the message blocks.
21 for block in blocks {
22 // Keep original state values.
23 let ab_orig = ab;
24 let cd_orig = cd;
25 let ef_orig = ef;
26 let gh_orig = gh;
27
28 // Load the message block into vectors, assuming little endianness.
29 let mut s0 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[0..16].as_ptr())));
30 let mut s1 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[16..32].as_ptr())));
31 let mut s2 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[32..48].as_ptr())));
32 let mut s3 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[48..64].as_ptr())));
33 let mut s4 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[64..80].as_ptr())));
34 let mut s5 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[80..96].as_ptr())));
35 let mut s6 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[96..112].as_ptr())));
36 let mut s7 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[112..128].as_ptr())));
37
38 // Rounds 0 and 1
39 let mut initial_sum = vaddq_u64(s0, vld1q_u64(&K64[0]));
40 let mut sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
41 let mut intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
42 gh = vsha512h2q_u64(intermed, cd, ab);
43 cd = vaddq_u64(cd, intermed);
44
45 // Rounds 2 and 3
46 initial_sum = vaddq_u64(s1, vld1q_u64(&K64[2]));
47 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
48 intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
49 ef = vsha512h2q_u64(intermed, ab, gh);
50 ab = vaddq_u64(ab, intermed);
51
52 // Rounds 4 and 5
53 initial_sum = vaddq_u64(s2, vld1q_u64(&K64[4]));
54 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
55 intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
56 cd = vsha512h2q_u64(intermed, gh, ef);
57 gh = vaddq_u64(gh, intermed);
58
59 // Rounds 6 and 7
60 initial_sum = vaddq_u64(s3, vld1q_u64(&K64[6]));
61 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
62 intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
63 ab = vsha512h2q_u64(intermed, ef, cd);
64 ef = vaddq_u64(ef, intermed);
65
66 // Rounds 8 and 9
67 initial_sum = vaddq_u64(s4, vld1q_u64(&K64[8]));
68 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
69 intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
70 gh = vsha512h2q_u64(intermed, cd, ab);
71 cd = vaddq_u64(cd, intermed);
72
73 // Rounds 10 and 11
74 initial_sum = vaddq_u64(s5, vld1q_u64(&K64[10]));
75 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
76 intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
77 ef = vsha512h2q_u64(intermed, ab, gh);
78 ab = vaddq_u64(ab, intermed);
79
80 // Rounds 12 and 13
81 initial_sum = vaddq_u64(s6, vld1q_u64(&K64[12]));
82 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
83 intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
84 cd = vsha512h2q_u64(intermed, gh, ef);
85 gh = vaddq_u64(gh, intermed);
86
87 // Rounds 14 and 15
88 initial_sum = vaddq_u64(s7, vld1q_u64(&K64[14]));
89 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
90 intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
91 ab = vsha512h2q_u64(intermed, ef, cd);
92 ef = vaddq_u64(ef, intermed);
93
94 for t in (16..80).step_by(16) {
95 // Rounds t and t + 1
96 s0 = vsha512su1q_u64(vsha512su0q_u64(s0, s1), s7, vextq_u64(s4, s5, 1));
97 initial_sum = vaddq_u64(s0, vld1q_u64(&K64[t]));
98 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
99 intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
100 gh = vsha512h2q_u64(intermed, cd, ab);
101 cd = vaddq_u64(cd, intermed);
102
103 // Rounds t + 2 and t + 3
104 s1 = vsha512su1q_u64(vsha512su0q_u64(s1, s2), s0, vextq_u64(s5, s6, 1));
105 initial_sum = vaddq_u64(s1, vld1q_u64(&K64[t + 2]));
106 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
107 intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
108 ef = vsha512h2q_u64(intermed, ab, gh);
109 ab = vaddq_u64(ab, intermed);
110
111 // Rounds t + 4 and t + 5
112 s2 = vsha512su1q_u64(vsha512su0q_u64(s2, s3), s1, vextq_u64(s6, s7, 1));
113 initial_sum = vaddq_u64(s2, vld1q_u64(&K64[t + 4]));
114 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
115 intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
116 cd = vsha512h2q_u64(intermed, gh, ef);
117 gh = vaddq_u64(gh, intermed);
118
119 // Rounds t + 6 and t + 7
120 s3 = vsha512su1q_u64(vsha512su0q_u64(s3, s4), s2, vextq_u64(s7, s0, 1));
121 initial_sum = vaddq_u64(s3, vld1q_u64(&K64[t + 6]));
122 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
123 intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
124 ab = vsha512h2q_u64(intermed, ef, cd);
125 ef = vaddq_u64(ef, intermed);
126
127 // Rounds t + 8 and t + 9
128 s4 = vsha512su1q_u64(vsha512su0q_u64(s4, s5), s3, vextq_u64(s0, s1, 1));
129 initial_sum = vaddq_u64(s4, vld1q_u64(&K64[t + 8]));
130 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
131 intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
132 gh = vsha512h2q_u64(intermed, cd, ab);
133 cd = vaddq_u64(cd, intermed);
134
135 // Rounds t + 10 and t + 11
136 s5 = vsha512su1q_u64(vsha512su0q_u64(s5, s6), s4, vextq_u64(s1, s2, 1));
137 initial_sum = vaddq_u64(s5, vld1q_u64(&K64[t + 10]));
138 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
139 intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
140 ef = vsha512h2q_u64(intermed, ab, gh);
141 ab = vaddq_u64(ab, intermed);
142
143 // Rounds t + 12 and t + 13
144 s6 = vsha512su1q_u64(vsha512su0q_u64(s6, s7), s5, vextq_u64(s2, s3, 1));
145 initial_sum = vaddq_u64(s6, vld1q_u64(&K64[t + 12]));
146 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
147 intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
148 cd = vsha512h2q_u64(intermed, gh, ef);
149 gh = vaddq_u64(gh, intermed);
150
151 // Rounds t + 14 and t + 15
152 s7 = vsha512su1q_u64(vsha512su0q_u64(s7, s0), s6, vextq_u64(s3, s4, 1));
153 initial_sum = vaddq_u64(s7, vld1q_u64(&K64[t + 14]));
154 sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
155 intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
156 ab = vsha512h2q_u64(intermed, ef, cd);
157 ef = vaddq_u64(ef, intermed);
158 }
159
160 // Add the block-specific state to the original state.
161 ab = vaddq_u64(ab, ab_orig);
162 cd = vaddq_u64(cd, cd_orig);
163 ef = vaddq_u64(ef, ef_orig);
164 gh = vaddq_u64(gh, gh_orig);
165 }
166
167 // Store vectors into state.
168 vst1q_u64(state[0..2].as_mut_ptr(), ab);
169 vst1q_u64(state[2..4].as_mut_ptr(), cd);
170 vst1q_u64(state[4..6].as_mut_ptr(), ef);
171 vst1q_u64(state[6..8].as_mut_ptr(), gh);
172 }