/rust/registry/src/index.crates.io-1949cf8c6b5b557f/sha1-0.10.7/src/compress/soft.rs
Line | Count | Source |
1 | | #![allow(clippy::many_single_char_names)] |
2 | | use super::BLOCK_SIZE; |
3 | | use core::convert::TryInto; |
4 | | |
5 | | const K: [u32; 4] = [0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6]; |
6 | | |
7 | | #[inline(always)] |
8 | 488M | fn add(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { |
9 | 488M | [ |
10 | 488M | a[0].wrapping_add(b[0]), |
11 | 488M | a[1].wrapping_add(b[1]), |
12 | 488M | a[2].wrapping_add(b[2]), |
13 | 488M | a[3].wrapping_add(b[3]), |
14 | 488M | ] |
15 | 488M | } |
16 | | |
17 | | #[inline(always)] |
18 | 390M | fn xor(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { |
19 | 390M | [a[0] ^ b[0], a[1] ^ b[1], a[2] ^ b[2], a[3] ^ b[3]] |
20 | 390M | } |
21 | | |
22 | | #[inline] |
23 | 488M | pub fn sha1_first_add(e: u32, w0: [u32; 4]) -> [u32; 4] { |
24 | 488M | let [a, b, c, d] = w0; |
25 | 488M | [e.wrapping_add(a), b, c, d] |
26 | 488M | } |
27 | | |
28 | 390M | fn sha1msg1(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { |
29 | 390M | let [_, _, w2, w3] = a; |
30 | 390M | let [w4, w5, _, _] = b; |
31 | 390M | [a[0] ^ w2, a[1] ^ w3, a[2] ^ w4, a[3] ^ w5] |
32 | 390M | } |
33 | | |
34 | 390M | fn sha1msg2(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { |
35 | 390M | let [x0, x1, x2, x3] = a; |
36 | 390M | let [_, w13, w14, w15] = b; |
37 | | |
38 | 390M | let w16 = (x0 ^ w13).rotate_left(1); |
39 | 390M | let w17 = (x1 ^ w14).rotate_left(1); |
40 | 390M | let w18 = (x2 ^ w15).rotate_left(1); |
41 | 390M | let w19 = (x3 ^ w16).rotate_left(1); |
42 | | |
43 | 390M | [w16, w17, w18, w19] |
44 | 390M | } |
45 | | |
46 | | #[inline] |
47 | 463M | fn sha1_first_half(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { |
48 | 463M | sha1_first_add(abcd[0].rotate_left(30), msg) |
49 | 463M | } |
50 | | |
51 | 488M | fn sha1_digest_round_x4(abcd: [u32; 4], work: [u32; 4], i: i8) -> [u32; 4] { |
52 | 488M | match i { |
53 | 122M | 0 => sha1rnds4c(abcd, add(work, [K[0]; 4])), |
54 | 122M | 1 => sha1rnds4p(abcd, add(work, [K[1]; 4])), |
55 | 122M | 2 => sha1rnds4m(abcd, add(work, [K[2]; 4])), |
56 | 122M | 3 => sha1rnds4p(abcd, add(work, [K[3]; 4])), |
57 | 0 | _ => unreachable!("unknown icosaround index"), |
58 | | } |
59 | 488M | } |
60 | | |
61 | 122M | fn sha1rnds4c(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { |
62 | 122M | let [mut a, mut b, mut c, mut d] = abcd; |
63 | 122M | let [t, u, v, w] = msg; |
64 | 122M | let mut e = 0u32; |
65 | | |
66 | | macro_rules! bool3ary_202 { |
67 | | ($a:expr, $b:expr, $c:expr) => { |
68 | | $c ^ ($a & ($b ^ $c)) |
69 | | }; |
70 | | } // Choose, MD5F, SHA1C |
71 | | |
72 | 122M | e = e |
73 | 122M | .wrapping_add(a.rotate_left(5)) |
74 | 122M | .wrapping_add(bool3ary_202!(b, c, d)) |
75 | 122M | .wrapping_add(t); |
76 | 122M | b = b.rotate_left(30); |
77 | | |
78 | 122M | d = d |
79 | 122M | .wrapping_add(e.rotate_left(5)) |
80 | 122M | .wrapping_add(bool3ary_202!(a, b, c)) |
81 | 122M | .wrapping_add(u); |
82 | 122M | a = a.rotate_left(30); |
83 | | |
84 | 122M | c = c |
85 | 122M | .wrapping_add(d.rotate_left(5)) |
86 | 122M | .wrapping_add(bool3ary_202!(e, a, b)) |
87 | 122M | .wrapping_add(v); |
88 | 122M | e = e.rotate_left(30); |
89 | | |
90 | 122M | b = b |
91 | 122M | .wrapping_add(c.rotate_left(5)) |
92 | 122M | .wrapping_add(bool3ary_202!(d, e, a)) |
93 | 122M | .wrapping_add(w); |
94 | 122M | d = d.rotate_left(30); |
95 | | |
96 | 122M | [b, c, d, e] |
97 | 122M | } |
98 | | |
99 | 244M | fn sha1rnds4p(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { |
100 | 244M | let [mut a, mut b, mut c, mut d] = abcd; |
101 | 244M | let [t, u, v, w] = msg; |
102 | 244M | let mut e = 0u32; |
103 | | |
104 | | macro_rules! bool3ary_150 { |
105 | | ($a:expr, $b:expr, $c:expr) => { |
106 | | $a ^ $b ^ $c |
107 | | }; |
108 | | } // Parity, XOR, MD5H, SHA1P |
109 | | |
110 | 244M | e = e |
111 | 244M | .wrapping_add(a.rotate_left(5)) |
112 | 244M | .wrapping_add(bool3ary_150!(b, c, d)) |
113 | 244M | .wrapping_add(t); |
114 | 244M | b = b.rotate_left(30); |
115 | | |
116 | 244M | d = d |
117 | 244M | .wrapping_add(e.rotate_left(5)) |
118 | 244M | .wrapping_add(bool3ary_150!(a, b, c)) |
119 | 244M | .wrapping_add(u); |
120 | 244M | a = a.rotate_left(30); |
121 | | |
122 | 244M | c = c |
123 | 244M | .wrapping_add(d.rotate_left(5)) |
124 | 244M | .wrapping_add(bool3ary_150!(e, a, b)) |
125 | 244M | .wrapping_add(v); |
126 | 244M | e = e.rotate_left(30); |
127 | | |
128 | 244M | b = b |
129 | 244M | .wrapping_add(c.rotate_left(5)) |
130 | 244M | .wrapping_add(bool3ary_150!(d, e, a)) |
131 | 244M | .wrapping_add(w); |
132 | 244M | d = d.rotate_left(30); |
133 | | |
134 | 244M | [b, c, d, e] |
135 | 244M | } |
136 | | |
137 | 122M | fn sha1rnds4m(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { |
138 | 122M | let [mut a, mut b, mut c, mut d] = abcd; |
139 | 122M | let [t, u, v, w] = msg; |
140 | 122M | let mut e = 0u32; |
141 | | |
142 | | macro_rules! bool3ary_232 { |
143 | | ($a:expr, $b:expr, $c:expr) => { |
144 | | ($a & $b) ^ ($a & $c) ^ ($b & $c) |
145 | | }; |
146 | | } // Majority, SHA1M |
147 | | |
148 | 122M | e = e |
149 | 122M | .wrapping_add(a.rotate_left(5)) |
150 | 122M | .wrapping_add(bool3ary_232!(b, c, d)) |
151 | 122M | .wrapping_add(t); |
152 | 122M | b = b.rotate_left(30); |
153 | | |
154 | 122M | d = d |
155 | 122M | .wrapping_add(e.rotate_left(5)) |
156 | 122M | .wrapping_add(bool3ary_232!(a, b, c)) |
157 | 122M | .wrapping_add(u); |
158 | 122M | a = a.rotate_left(30); |
159 | | |
160 | 122M | c = c |
161 | 122M | .wrapping_add(d.rotate_left(5)) |
162 | 122M | .wrapping_add(bool3ary_232!(e, a, b)) |
163 | 122M | .wrapping_add(v); |
164 | 122M | e = e.rotate_left(30); |
165 | | |
166 | 122M | b = b |
167 | 122M | .wrapping_add(c.rotate_left(5)) |
168 | 122M | .wrapping_add(bool3ary_232!(d, e, a)) |
169 | 122M | .wrapping_add(w); |
170 | 122M | d = d.rotate_left(30); |
171 | | |
172 | 122M | [b, c, d, e] |
173 | 122M | } |
174 | | |
175 | | macro_rules! rounds4 { |
176 | | ($h0:ident, $h1:ident, $wk:expr, $i:expr) => { |
177 | | sha1_digest_round_x4($h0, sha1_first_half($h1, $wk), $i) |
178 | | }; |
179 | | } |
180 | | |
181 | | macro_rules! schedule { |
182 | | ($v0:expr, $v1:expr, $v2:expr, $v3:expr) => { |
183 | | sha1msg2(xor(sha1msg1($v0, $v1), $v2), $v3) |
184 | | }; |
185 | | } |
186 | | |
187 | | macro_rules! schedule_rounds4 { |
188 | | ( |
189 | | $h0:ident, $h1:ident, |
190 | | $w0:expr, $w1:expr, $w2:expr, $w3:expr, $w4:expr, |
191 | | $i:expr |
192 | | ) => { |
193 | | $w4 = schedule!($w0, $w1, $w2, $w3); |
194 | | $h1 = rounds4!($h0, $h1, $w4, $i); |
195 | | }; |
196 | | } |
197 | | |
198 | | #[inline(always)] |
199 | 24.4M | fn sha1_digest_block_u32(state: &mut [u32; 5], block: &[u32; 16]) { |
200 | 24.4M | let mut w0 = [block[0], block[1], block[2], block[3]]; |
201 | 24.4M | let mut w1 = [block[4], block[5], block[6], block[7]]; |
202 | 24.4M | let mut w2 = [block[8], block[9], block[10], block[11]]; |
203 | 24.4M | let mut w3 = [block[12], block[13], block[14], block[15]]; |
204 | | #[allow(clippy::needless_late_init)] |
205 | | let mut w4; |
206 | | |
207 | 24.4M | let mut h0 = [state[0], state[1], state[2], state[3]]; |
208 | 24.4M | let mut h1 = sha1_first_add(state[4], w0); |
209 | | |
210 | | // Rounds 0..20 |
211 | 24.4M | h1 = sha1_digest_round_x4(h0, h1, 0); |
212 | 24.4M | h0 = rounds4!(h1, h0, w1, 0); |
213 | 24.4M | h1 = rounds4!(h0, h1, w2, 0); |
214 | 24.4M | h0 = rounds4!(h1, h0, w3, 0); |
215 | 24.4M | schedule_rounds4!(h0, h1, w0, w1, w2, w3, w4, 0); |
216 | | |
217 | | // Rounds 20..40 |
218 | 24.4M | schedule_rounds4!(h1, h0, w1, w2, w3, w4, w0, 1); |
219 | 24.4M | schedule_rounds4!(h0, h1, w2, w3, w4, w0, w1, 1); |
220 | 24.4M | schedule_rounds4!(h1, h0, w3, w4, w0, w1, w2, 1); |
221 | 24.4M | schedule_rounds4!(h0, h1, w4, w0, w1, w2, w3, 1); |
222 | 24.4M | schedule_rounds4!(h1, h0, w0, w1, w2, w3, w4, 1); |
223 | | |
224 | | // Rounds 40..60 |
225 | 24.4M | schedule_rounds4!(h0, h1, w1, w2, w3, w4, w0, 2); |
226 | 24.4M | schedule_rounds4!(h1, h0, w2, w3, w4, w0, w1, 2); |
227 | 24.4M | schedule_rounds4!(h0, h1, w3, w4, w0, w1, w2, 2); |
228 | 24.4M | schedule_rounds4!(h1, h0, w4, w0, w1, w2, w3, 2); |
229 | 24.4M | schedule_rounds4!(h0, h1, w0, w1, w2, w3, w4, 2); |
230 | | |
231 | | // Rounds 60..80 |
232 | 24.4M | schedule_rounds4!(h1, h0, w1, w2, w3, w4, w0, 3); |
233 | 24.4M | schedule_rounds4!(h0, h1, w2, w3, w4, w0, w1, 3); |
234 | 24.4M | schedule_rounds4!(h1, h0, w3, w4, w0, w1, w2, 3); |
235 | 24.4M | schedule_rounds4!(h0, h1, w4, w0, w1, w2, w3, 3); |
236 | 24.4M | schedule_rounds4!(h1, h0, w0, w1, w2, w3, w4, 3); |
237 | | |
238 | 24.4M | let e = h1[0].rotate_left(30); |
239 | 24.4M | let [a, b, c, d] = h0; |
240 | | |
241 | 24.4M | state[0] = state[0].wrapping_add(a); |
242 | 24.4M | state[1] = state[1].wrapping_add(b); |
243 | 24.4M | state[2] = state[2].wrapping_add(c); |
244 | 24.4M | state[3] = state[3].wrapping_add(d); |
245 | 24.4M | state[4] = state[4].wrapping_add(e); |
246 | 24.4M | } |
247 | | |
248 | 24.2M | pub fn compress(state: &mut [u32; 5], blocks: &[[u8; BLOCK_SIZE]]) { |
249 | 24.2M | let mut block_u32 = [0u32; BLOCK_SIZE / 4]; |
250 | | // since LLVM can't properly use aliasing yet it will make |
251 | | // unnecessary state stores without this copy |
252 | 24.2M | let mut state_cpy = *state; |
253 | 24.4M | for block in blocks.iter() { |
254 | 390M | for (o, chunk) in block_u32.iter_mut().zip(block.chunks_exact(4)) { |
255 | 390M | *o = u32::from_be_bytes(chunk.try_into().unwrap()); |
256 | 390M | } |
257 | 24.4M | sha1_digest_block_u32(&mut state_cpy, &block_u32); |
258 | | } |
259 | 24.2M | *state = state_cpy; |
260 | 24.2M | } |