/rust/registry/src/index.crates.io-6f17d22bba15001f/sha1-0.10.6/src/compress/soft.rs
Line | Count | Source (jump to first uncovered line) |
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 | 0 | fn add(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { |
9 | 0 | [ |
10 | 0 | a[0].wrapping_add(b[0]), |
11 | 0 | a[1].wrapping_add(b[1]), |
12 | 0 | a[2].wrapping_add(b[2]), |
13 | 0 | a[3].wrapping_add(b[3]), |
14 | 0 | ] |
15 | 0 | } |
16 | | |
17 | | #[inline(always)] |
18 | 0 | fn xor(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { |
19 | 0 | [a[0] ^ b[0], a[1] ^ b[1], a[2] ^ b[2], a[3] ^ b[3]] |
20 | 0 | } |
21 | | |
22 | | #[inline] |
23 | 0 | pub fn sha1_first_add(e: u32, w0: [u32; 4]) -> [u32; 4] { |
24 | 0 | let [a, b, c, d] = w0; |
25 | 0 | [e.wrapping_add(a), b, c, d] |
26 | 0 | } |
27 | | |
28 | 0 | fn sha1msg1(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { |
29 | 0 | let [_, _, w2, w3] = a; |
30 | 0 | let [w4, w5, _, _] = b; |
31 | 0 | [a[0] ^ w2, a[1] ^ w3, a[2] ^ w4, a[3] ^ w5] |
32 | 0 | } |
33 | | |
34 | 0 | fn sha1msg2(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { |
35 | 0 | let [x0, x1, x2, x3] = a; |
36 | 0 | let [_, w13, w14, w15] = b; |
37 | 0 |
|
38 | 0 | let w16 = (x0 ^ w13).rotate_left(1); |
39 | 0 | let w17 = (x1 ^ w14).rotate_left(1); |
40 | 0 | let w18 = (x2 ^ w15).rotate_left(1); |
41 | 0 | let w19 = (x3 ^ w16).rotate_left(1); |
42 | 0 |
|
43 | 0 | [w16, w17, w18, w19] |
44 | 0 | } |
45 | | |
46 | | #[inline] |
47 | 0 | fn sha1_first_half(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { |
48 | 0 | sha1_first_add(abcd[0].rotate_left(30), msg) |
49 | 0 | } |
50 | | |
51 | 0 | fn sha1_digest_round_x4(abcd: [u32; 4], work: [u32; 4], i: i8) -> [u32; 4] { |
52 | 0 | match i { |
53 | 0 | 0 => sha1rnds4c(abcd, add(work, [K[0]; 4])), |
54 | 0 | 1 => sha1rnds4p(abcd, add(work, [K[1]; 4])), |
55 | 0 | 2 => sha1rnds4m(abcd, add(work, [K[2]; 4])), |
56 | 0 | 3 => sha1rnds4p(abcd, add(work, [K[3]; 4])), |
57 | 0 | _ => unreachable!("unknown icosaround index"), |
58 | | } |
59 | 0 | } |
60 | | |
61 | 0 | fn sha1rnds4c(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { |
62 | 0 | let [mut a, mut b, mut c, mut d] = abcd; |
63 | 0 | let [t, u, v, w] = msg; |
64 | 0 | 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 | 0 | e = e |
73 | 0 | .wrapping_add(a.rotate_left(5)) |
74 | 0 | .wrapping_add(bool3ary_202!(b, c, d)) |
75 | 0 | .wrapping_add(t); |
76 | 0 | b = b.rotate_left(30); |
77 | 0 |
|
78 | 0 | d = d |
79 | 0 | .wrapping_add(e.rotate_left(5)) |
80 | 0 | .wrapping_add(bool3ary_202!(a, b, c)) |
81 | 0 | .wrapping_add(u); |
82 | 0 | a = a.rotate_left(30); |
83 | 0 |
|
84 | 0 | c = c |
85 | 0 | .wrapping_add(d.rotate_left(5)) |
86 | 0 | .wrapping_add(bool3ary_202!(e, a, b)) |
87 | 0 | .wrapping_add(v); |
88 | 0 | e = e.rotate_left(30); |
89 | 0 |
|
90 | 0 | b = b |
91 | 0 | .wrapping_add(c.rotate_left(5)) |
92 | 0 | .wrapping_add(bool3ary_202!(d, e, a)) |
93 | 0 | .wrapping_add(w); |
94 | 0 | d = d.rotate_left(30); |
95 | 0 |
|
96 | 0 | [b, c, d, e] |
97 | 0 | } |
98 | | |
99 | 0 | fn sha1rnds4p(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { |
100 | 0 | let [mut a, mut b, mut c, mut d] = abcd; |
101 | 0 | let [t, u, v, w] = msg; |
102 | 0 | 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 | 0 | e = e |
111 | 0 | .wrapping_add(a.rotate_left(5)) |
112 | 0 | .wrapping_add(bool3ary_150!(b, c, d)) |
113 | 0 | .wrapping_add(t); |
114 | 0 | b = b.rotate_left(30); |
115 | 0 |
|
116 | 0 | d = d |
117 | 0 | .wrapping_add(e.rotate_left(5)) |
118 | 0 | .wrapping_add(bool3ary_150!(a, b, c)) |
119 | 0 | .wrapping_add(u); |
120 | 0 | a = a.rotate_left(30); |
121 | 0 |
|
122 | 0 | c = c |
123 | 0 | .wrapping_add(d.rotate_left(5)) |
124 | 0 | .wrapping_add(bool3ary_150!(e, a, b)) |
125 | 0 | .wrapping_add(v); |
126 | 0 | e = e.rotate_left(30); |
127 | 0 |
|
128 | 0 | b = b |
129 | 0 | .wrapping_add(c.rotate_left(5)) |
130 | 0 | .wrapping_add(bool3ary_150!(d, e, a)) |
131 | 0 | .wrapping_add(w); |
132 | 0 | d = d.rotate_left(30); |
133 | 0 |
|
134 | 0 | [b, c, d, e] |
135 | 0 | } |
136 | | |
137 | 0 | fn sha1rnds4m(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { |
138 | 0 | let [mut a, mut b, mut c, mut d] = abcd; |
139 | 0 | let [t, u, v, w] = msg; |
140 | 0 | 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 | 0 | e = e |
149 | 0 | .wrapping_add(a.rotate_left(5)) |
150 | 0 | .wrapping_add(bool3ary_232!(b, c, d)) |
151 | 0 | .wrapping_add(t); |
152 | 0 | b = b.rotate_left(30); |
153 | 0 |
|
154 | 0 | d = d |
155 | 0 | .wrapping_add(e.rotate_left(5)) |
156 | 0 | .wrapping_add(bool3ary_232!(a, b, c)) |
157 | 0 | .wrapping_add(u); |
158 | 0 | a = a.rotate_left(30); |
159 | 0 |
|
160 | 0 | c = c |
161 | 0 | .wrapping_add(d.rotate_left(5)) |
162 | 0 | .wrapping_add(bool3ary_232!(e, a, b)) |
163 | 0 | .wrapping_add(v); |
164 | 0 | e = e.rotate_left(30); |
165 | 0 |
|
166 | 0 | b = b |
167 | 0 | .wrapping_add(c.rotate_left(5)) |
168 | 0 | .wrapping_add(bool3ary_232!(d, e, a)) |
169 | 0 | .wrapping_add(w); |
170 | 0 | d = d.rotate_left(30); |
171 | 0 |
|
172 | 0 | [b, c, d, e] |
173 | 0 | } |
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 | 0 | fn sha1_digest_block_u32(state: &mut [u32; 5], block: &[u32; 16]) { |
200 | 0 | let mut w0 = [block[0], block[1], block[2], block[3]]; |
201 | 0 | let mut w1 = [block[4], block[5], block[6], block[7]]; |
202 | 0 | let mut w2 = [block[8], block[9], block[10], block[11]]; |
203 | 0 | let mut w3 = [block[12], block[13], block[14], block[15]]; |
204 | 0 | #[allow(clippy::needless_late_init)] |
205 | 0 | let mut w4; |
206 | 0 |
|
207 | 0 | let mut h0 = [state[0], state[1], state[2], state[3]]; |
208 | 0 | let mut h1 = sha1_first_add(state[4], w0); |
209 | 0 |
|
210 | 0 | // Rounds 0..20 |
211 | 0 | h1 = sha1_digest_round_x4(h0, h1, 0); |
212 | 0 | h0 = rounds4!(h1, h0, w1, 0); |
213 | 0 | h1 = rounds4!(h0, h1, w2, 0); |
214 | 0 | h0 = rounds4!(h1, h0, w3, 0); |
215 | 0 | schedule_rounds4!(h0, h1, w0, w1, w2, w3, w4, 0); |
216 | 0 |
|
217 | 0 | // Rounds 20..40 |
218 | 0 | schedule_rounds4!(h1, h0, w1, w2, w3, w4, w0, 1); |
219 | 0 | schedule_rounds4!(h0, h1, w2, w3, w4, w0, w1, 1); |
220 | 0 | schedule_rounds4!(h1, h0, w3, w4, w0, w1, w2, 1); |
221 | 0 | schedule_rounds4!(h0, h1, w4, w0, w1, w2, w3, 1); |
222 | 0 | schedule_rounds4!(h1, h0, w0, w1, w2, w3, w4, 1); |
223 | 0 |
|
224 | 0 | // Rounds 40..60 |
225 | 0 | schedule_rounds4!(h0, h1, w1, w2, w3, w4, w0, 2); |
226 | 0 | schedule_rounds4!(h1, h0, w2, w3, w4, w0, w1, 2); |
227 | 0 | schedule_rounds4!(h0, h1, w3, w4, w0, w1, w2, 2); |
228 | 0 | schedule_rounds4!(h1, h0, w4, w0, w1, w2, w3, 2); |
229 | 0 | schedule_rounds4!(h0, h1, w0, w1, w2, w3, w4, 2); |
230 | 0 |
|
231 | 0 | // Rounds 60..80 |
232 | 0 | schedule_rounds4!(h1, h0, w1, w2, w3, w4, w0, 3); |
233 | 0 | schedule_rounds4!(h0, h1, w2, w3, w4, w0, w1, 3); |
234 | 0 | schedule_rounds4!(h1, h0, w3, w4, w0, w1, w2, 3); |
235 | 0 | schedule_rounds4!(h0, h1, w4, w0, w1, w2, w3, 3); |
236 | 0 | schedule_rounds4!(h1, h0, w0, w1, w2, w3, w4, 3); |
237 | 0 |
|
238 | 0 | let e = h1[0].rotate_left(30); |
239 | 0 | let [a, b, c, d] = h0; |
240 | 0 |
|
241 | 0 | state[0] = state[0].wrapping_add(a); |
242 | 0 | state[1] = state[1].wrapping_add(b); |
243 | 0 | state[2] = state[2].wrapping_add(c); |
244 | 0 | state[3] = state[3].wrapping_add(d); |
245 | 0 | state[4] = state[4].wrapping_add(e); |
246 | 0 | } |
247 | | |
248 | 0 | pub fn compress(state: &mut [u32; 5], blocks: &[[u8; BLOCK_SIZE]]) { |
249 | 0 | let mut block_u32 = [0u32; BLOCK_SIZE / 4]; |
250 | 0 | // since LLVM can't properly use aliasing yet it will make |
251 | 0 | // unnecessary state stores without this copy |
252 | 0 | let mut state_cpy = *state; |
253 | 0 | for block in blocks.iter() { |
254 | 0 | for (o, chunk) in block_u32.iter_mut().zip(block.chunks_exact(4)) { |
255 | 0 | *o = u32::from_be_bytes(chunk.try_into().unwrap()); |
256 | 0 | } |
257 | 0 | sha1_digest_block_u32(&mut state_cpy, &block_u32); |
258 | | } |
259 | 0 | *state = state_cpy; |
260 | 0 | } |