Debian Codegraph @

third_party/rust/windows-core/src/imp/sha1.rs

raw
1 #![allow(clippy::many_single_char_names)]
2
3 pub const fn sha1(data: &ConstBuffer) -> Digest {
4 let state: [u32; 5] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
5 let len: u64 = 0;
6 let blocks = Blocks {
7 len: 0,
8 data: [0; 64],
9 };
10 let (blocks, len, state) = process_blocks(blocks, data, len, state);
11 digest(state, len, blocks)
12 }
13
14 const BUFFER_SIZE: usize = 1024;
15
16 pub struct ConstBuffer {
17 data: [u8; BUFFER_SIZE],
18 head: usize,
19 }
20
21 impl ConstBuffer {
22 pub const fn for_class<C: crate::RuntimeName, I: crate::RuntimeType>() -> Self {
23 Self::new()
24 .push_slice(b"rc(")
25 .push_slice(C::NAME.as_bytes())
26 .push(b';')
27 .push_other(I::SIGNATURE)
28 .push(b')')
29 }
30
31 pub const fn for_interface<T: crate::Interface>() -> Self {
32 Self::new().push_guid(&T::IID)
33 }
34
35 pub const fn from_slice(slice: &[u8]) -> Self {
36 let s = Self::new();
37 s.push_slice(slice)
38 }
39
40 pub const fn new() -> Self {
41 Self {
42 data: [0; BUFFER_SIZE],
43 head: 0,
44 }
45 }
46
47 pub const fn push_slice(self, slice: &[u8]) -> Self {
48 self.push_amount(slice, slice.len())
49 }
50
51 const fn get(&self, index: usize) -> u8 {
52 self.data[index]
53 }
54
55 const fn len(&self) -> usize {
56 self.head
57 }
58
59 pub fn as_slice(&self) -> &[u8] {
60 &self.data[..self.head]
61 }
62
63 pub const fn push_other(self, other: Self) -> Self {
64 self.push_amount(&other.data, other.len())
65 }
66
67 const fn push(mut self, value: u8) -> Self {
68 self.data[self.head] = value;
69 self.head += 1;
70 self
71 }
72
73 const fn push_hex_u8(self, value: u8) -> Self {
74 const fn digit(mut value: u8) -> u8 {
75 value &= 0xF;
76
77 if value < 10 {
78 b'0' + value
79 } else {
80 b'a' + (value - 10)
81 }
82 }
83
84 self.push(digit(value >> 4)).push(digit(value))
85 }
86
87 const fn push_hex_u16(self, value: u16) -> Self {
88 self.push_hex_u8((value >> 8) as u8)
89 .push_hex_u8((value & 0xFF) as u8)
90 }
91
92 const fn push_hex_u32(self, value: u32) -> Self {
93 self.push_hex_u16((value >> 16) as u16)
94 .push_hex_u16((value & 0xFFFF) as u16)
95 }
96
97 const fn push_amount(mut self, slice: &[u8], amount: usize) -> Self {
98 let mut i = 0;
99 while i < amount {
100 self.data[self.head + i] = slice[i];
101 i += 1;
102 }
103 self.head += i;
104 self
105 }
106
107 const fn push_guid(self, guid: &crate::GUID) -> Self {
108 self.push(b'{')
109 .push_hex_u32(guid.data1)
110 .push(b'-')
111 .push_hex_u16(guid.data2)
112 .push(b'-')
113 .push_hex_u16(guid.data3)
114 .push(b'-')
115 .push_hex_u16((guid.data4[0] as u16) << 8 | guid.data4[1] as u16)
116 .push(b'-')
117 .push_hex_u16((guid.data4[2] as u16) << 8 | guid.data4[3] as u16)
118 .push_hex_u16((guid.data4[4] as u16) << 8 | guid.data4[5] as u16)
119 .push_hex_u16((guid.data4[6] as u16) << 8 | guid.data4[7] as u16)
120 .push(b'}')
121 }
122 }
123
124 struct Blocks {
125 len: u32,
126 data: [u8; 64],
127 }
128
129 const fn process_blocks(
130 mut blocks: Blocks,
131 data: &ConstBuffer,
132 mut len: u64,
133 mut state: [u32; 5],
134 ) -> (Blocks, u64, [u32; 5]) {
135 const fn as_block(input: &ConstBuffer, offset: usize) -> [u32; 16] {
136 let mut result = [0u32; 16];
137
138 let mut i = 0;
139 while i != 16 {
140 let off = offset + (i * 4);
141 result[i] = (input.get(off + 3) as u32)
142 | ((input.get(off + 2) as u32) << 8)
143 | ((input.get(off + 1) as u32) << 16)
144 | ((input.get(off) as u32) << 24);
145 i += 1;
146 }
147 result
148 }
149
150 const fn clone_from_slice_64(
151 mut data: [u8; 64],
152 slice: &[u8],
153 offset: usize,
154 num_elems: usize,
155 ) -> [u8; 64] {
156 let mut i = 0;
157 while i < num_elems {
158 data[i] = slice[offset + i];
159 i += 1;
160 }
161 data
162 }
163
164 let mut i = 0;
165 while i < data.len() {
166 if data.len() - i >= 64 {
167 let chunk_block = as_block(data, i);
168 len += 64;
169 state = process_state(state, chunk_block);
170 i += 64;
171 } else {
172 let num_elems = data.len() - i;
173 blocks.data = clone_from_slice_64(blocks.data, &data.data, i, num_elems);
174 blocks.len = num_elems as u32;
175 break;
176 }
177 }
178 (blocks, len, state)
179 }
180
181 const fn process_state(mut state: [u32; 5], block: [u32; 16]) -> [u32; 5] {
182 let a = state[0];
183 let b = state[1];
184 let c = state[2];
185 let d = state[3];
186 let e = state[4];
187 let (block, b, e) = r0(block, a, b, c, d, e, 0);
188 let (block, a, d) = r0(block, e, a, b, c, d, 1);
189 let (block, e, c) = r0(block, d, e, a, b, c, 2);
190 let (block, d, b) = r0(block, c, d, e, a, b, 3);
191 let (block, c, a) = r0(block, b, c, d, e, a, 4);
192 let (block, b, e) = r0(block, a, b, c, d, e, 5);
193 let (block, a, d) = r0(block, e, a, b, c, d, 6);
194 let (block, e, c) = r0(block, d, e, a, b, c, 7);
195 let (block, d, b) = r0(block, c, d, e, a, b, 8);
196 let (block, c, a) = r0(block, b, c, d, e, a, 9);
197 let (block, b, e) = r0(block, a, b, c, d, e, 10);
198 let (block, a, d) = r0(block, e, a, b, c, d, 11);
199 let (block, e, c) = r0(block, d, e, a, b, c, 12);
200 let (block, d, b) = r0(block, c, d, e, a, b, 13);
201 let (block, c, a) = r0(block, b, c, d, e, a, 14);
202 let (block, b, e) = r0(block, a, b, c, d, e, 15);
203 let (block, a, d) = r1(block, e, a, b, c, d, 0);
204 let (block, e, c) = r1(block, d, e, a, b, c, 1);
205 let (block, d, b) = r1(block, c, d, e, a, b, 2);
206 let (block, c, a) = r1(block, b, c, d, e, a, 3);
207 let (block, b, e) = r2(block, a, b, c, d, e, 4);
208 let (block, a, d) = r2(block, e, a, b, c, d, 5);
209 let (block, e, c) = r2(block, d, e, a, b, c, 6);
210 let (block, d, b) = r2(block, c, d, e, a, b, 7);
211 let (block, c, a) = r2(block, b, c, d, e, a, 8);
212 let (block, b, e) = r2(block, a, b, c, d, e, 9);
213 let (block, a, d) = r2(block, e, a, b, c, d, 10);
214 let (block, e, c) = r2(block, d, e, a, b, c, 11);
215 let (block, d, b) = r2(block, c, d, e, a, b, 12);
216 let (block, c, a) = r2(block, b, c, d, e, a, 13);
217 let (block, b, e) = r2(block, a, b, c, d, e, 14);
218 let (block, a, d) = r2(block, e, a, b, c, d, 15);
219 let (block, e, c) = r2(block, d, e, a, b, c, 0);
220 let (block, d, b) = r2(block, c, d, e, a, b, 1);
221 let (block, c, a) = r2(block, b, c, d, e, a, 2);
222 let (block, b, e) = r2(block, a, b, c, d, e, 3);
223 let (block, a, d) = r2(block, e, a, b, c, d, 4);
224 let (block, e, c) = r2(block, d, e, a, b, c, 5);
225 let (block, d, b) = r2(block, c, d, e, a, b, 6);
226 let (block, c, a) = r2(block, b, c, d, e, a, 7);
227 let (block, b, e) = r3(block, a, b, c, d, e, 8);
228 let (block, a, d) = r3(block, e, a, b, c, d, 9);
229 let (block, e, c) = r3(block, d, e, a, b, c, 10);
230 let (block, d, b) = r3(block, c, d, e, a, b, 11);
231 let (block, c, a) = r3(block, b, c, d, e, a, 12);
232 let (block, b, e) = r3(block, a, b, c, d, e, 13);
233 let (block, a, d) = r3(block, e, a, b, c, d, 14);
234 let (block, e, c) = r3(block, d, e, a, b, c, 15);
235 let (block, d, b) = r3(block, c, d, e, a, b, 0);
236 let (block, c, a) = r3(block, b, c, d, e, a, 1);
237 let (block, b, e) = r3(block, a, b, c, d, e, 2);
238 let (block, a, d) = r3(block, e, a, b, c, d, 3);
239 let (block, e, c) = r3(block, d, e, a, b, c, 4);
240 let (block, d, b) = r3(block, c, d, e, a, b, 5);
241 let (block, c, a) = r3(block, b, c, d, e, a, 6);
242 let (block, b, e) = r3(block, a, b, c, d, e, 7);
243 let (block, a, d) = r3(block, e, a, b, c, d, 8);
244 let (block, e, c) = r3(block, d, e, a, b, c, 9);
245 let (block, d, b) = r3(block, c, d, e, a, b, 10);
246 let (block, c, a) = r3(block, b, c, d, e, a, 11);
247 let (block, b, e) = r4(block, a, b, c, d, e, 12);
248 let (block, a, d) = r4(block, e, a, b, c, d, 13);
249 let (block, e, c) = r4(block, d, e, a, b, c, 14);
250 let (block, d, b) = r4(block, c, d, e, a, b, 15);
251 let (block, c, a) = r4(block, b, c, d, e, a, 0);
252 let (block, b, e) = r4(block, a, b, c, d, e, 1);
253 let (block, a, d) = r4(block, e, a, b, c, d, 2);
254 let (block, e, c) = r4(block, d, e, a, b, c, 3);
255 let (block, d, b) = r4(block, c, d, e, a, b, 4);
256 let (block, c, a) = r4(block, b, c, d, e, a, 5);
257 let (block, b, e) = r4(block, a, b, c, d, e, 6);
258 let (block, a, d) = r4(block, e, a, b, c, d, 7);
259 let (block, e, c) = r4(block, d, e, a, b, c, 8);
260 let (block, d, b) = r4(block, c, d, e, a, b, 9);
261 let (block, c, a) = r4(block, b, c, d, e, a, 10);
262 let (block, b, e) = r4(block, a, b, c, d, e, 11);
263 let (block, a, d) = r4(block, e, a, b, c, d, 12);
264 let (block, e, c) = r4(block, d, e, a, b, c, 13);
265 let (block, d, b) = r4(block, c, d, e, a, b, 14);
266 let (_, c, a) = r4(block, b, c, d, e, a, 15);
267
268 state[0] = state[0].wrapping_add(a);
269 state[1] = state[1].wrapping_add(b);
270 state[2] = state[2].wrapping_add(c);
271 state[3] = state[3].wrapping_add(d);
272 state[4] = state[4].wrapping_add(e);
273 state
274 }
275
276 const fn digest(mut state: [u32; 5], len: u64, blocks: Blocks) -> Digest {
277 const fn clone_from_slice_128(
278 mut data: [u8; 128],
279 slice: &[u8],
280 offset: usize,
281 num_elems: usize,
282 ) -> [u8; 128] {
283 let mut i = 0;
284 while i < num_elems {
285 data[i] = slice[offset + i];
286 i += 1;
287 }
288 data
289 }
290
291 const fn clone_slice_128(mut data: [u8; 128], slice: &[u8], _offset: usize) -> [u8; 128] {
292 let mut i = 0;
293 while i < slice.len() {
294 data[_offset + i] = slice[i];
295 i += 1;
296 }
297 data
298 }
299
300 const fn as_block(input: &[u8], offset: usize) -> [u32; 16] {
301 let mut result = [0u32; 16];
302
303 let mut i = 0;
304 while i != 16 {
305 let off = offset + (i * 4);
306 result[i] = (input[off + 3] as u32)
307 | ((input[off + 2] as u32) << 8)
308 | ((input[off + 1] as u32) << 16)
309 | ((input[off] as u32) << 24);
310 i += 1;
311 }
312 result
313 }
314
315 let bits = (len + (blocks.len as u64)) * 8;
316 let extra = [
317 (bits >> 56) as u8,
318 (bits >> 48) as u8,
319 (bits >> 40) as u8,
320 (bits >> 32) as u8,
321 (bits >> 24) as u8,
322 (bits >> 16) as u8,
323 (bits >> 8) as u8,
324 bits as u8,
325 ];
326 let mut last = [0; 128];
327 let blocklen = blocks.len as usize;
328 last = clone_from_slice_128(last, &blocks.data, 0, blocklen);
329 last[blocklen] = 0x80;
330
331 if blocklen < 56 {
332 last = clone_slice_128(last, &extra, 56);
333 state = process_state(state, as_block(&last, 0));
334 } else {
335 last = clone_slice_128(last, &extra, 120);
336 state = process_state(state, as_block(&last, 0));
337 state = process_state(state, as_block(&last, 64));
338 }
339 Digest { data: state }
340 }
341
342 const fn rol(value: u32, bits: usize) -> u32 {
343 (value << bits) | (value >> (32 - bits))
344 }
345
346 const fn blk(block: &[u32], i: usize) -> u32 {
347 let value = block[(i + 13) & 15] ^ block[(i + 8) & 15] ^ block[(i + 2) & 15] ^ block[i];
348 rol(value, 1)
349 }
350
351 const fn r0(
352 block: [u32; 16],
353 v: u32,
354 mut w: u32,
355 x: u32,
356 y: u32,
357 mut z: u32,
358 i: usize,
359 ) -> ([u32; 16], u32, u32) {
360 let n = ((w & (x ^ y)) ^ y)
361 .wrapping_add(block[i])
362 .wrapping_add(0x5a82_7999)
363 .wrapping_add(rol(v, 5));
364 z = z.wrapping_add(n);
365 w = rol(w, 30);
366 (block, w, z)
367 }
368
369 const fn r1(
370 mut block: [u32; 16],
371 v: u32,
372 mut w: u32,
373 x: u32,
374 y: u32,
375 mut z: u32,
376 i: usize,
377 ) -> ([u32; 16], u32, u32) {
378 block[i] = blk(&block, i);
379 let n = ((w & (x ^ y)) ^ y)
380 .wrapping_add(block[i])
381 .wrapping_add(0x5a82_7999)
382 .wrapping_add(rol(v, 5));
383 z = z.wrapping_add(n);
384 w = rol(w, 30);
385 (block, w, z)
386 }
387
388 const fn r2(
389 mut block: [u32; 16],
390 v: u32,
391 mut w: u32,
392 x: u32,
393 y: u32,
394 mut z: u32,
395 i: usize,
396 ) -> ([u32; 16], u32, u32) {
397 block[i] = blk(&block, i);
398 let n = (w ^ x ^ y)
399 .wrapping_add(block[i])
400 .wrapping_add(0x6ed9_eba1)
401 .wrapping_add(rol(v, 5));
402 z = z.wrapping_add(n);
403 w = rol(w, 30);
404 (block, w, z)
405 }
406
407 const fn r3(
408 mut block: [u32; 16],
409 v: u32,
410 mut w: u32,
411 x: u32,
412 y: u32,
413 mut z: u32,
414 i: usize,
415 ) -> ([u32; 16], u32, u32) {
416 block[i] = blk(&block, i);
417 let n = (((w | x) & y) | (w & x))
418 .wrapping_add(block[i])
419 .wrapping_add(0x8f1b_bcdc)
420 .wrapping_add(rol(v, 5));
421 z = z.wrapping_add(n);
422 w = rol(w, 30);
423 (block, w, z)
424 }
425
426 const fn r4(
427 mut block: [u32; 16],
428 v: u32,
429 mut w: u32,
430 x: u32,
431 y: u32,
432 mut z: u32,
433 i: usize,
434 ) -> ([u32; 16], u32, u32) {
435 block[i] = blk(&block, i);
436 let n = (w ^ x ^ y)
437 .wrapping_add(block[i])
438 .wrapping_add(0xca62_c1d6)
439 .wrapping_add(rol(v, 5));
440 z = z.wrapping_add(n);
441 w = rol(w, 30);
442 (block, w, z)
443 }
444
445 pub struct Digest {
446 data: [u32; 5],
447 }
448
449 impl Digest {
450 pub const fn bytes(&self) -> [u8; 20] {
451 [
452 (self.data[0] >> 24) as u8,
453 (self.data[0] >> 16) as u8,
454 (self.data[0] >> 8) as u8,
455 self.data[0] as u8,
456 (self.data[1] >> 24) as u8,
457 (self.data[1] >> 16) as u8,
458 (self.data[1] >> 8) as u8,
459 self.data[1] as u8,
460 (self.data[2] >> 24) as u8,
461 (self.data[2] >> 16) as u8,
462 (self.data[2] >> 8) as u8,
463 self.data[2] as u8,
464 (self.data[3] >> 24) as u8,
465 (self.data[3] >> 16) as u8,
466 (self.data[3] >> 8) as u8,
467 self.data[3] as u8,
468 (self.data[4] >> 24) as u8,
469 (self.data[4] >> 16) as u8,
470 (self.data[4] >> 8) as u8,
471 self.data[4] as u8,
472 ]
473 }
474 }
475
476 impl core::fmt::Display for Digest {
477 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
478 for i in self.data.iter() {
479 write!(f, "{:08x}", i)?;
480 }
481 Ok(())
482 }
483 }