md5/compress/
soft.rs

1#![allow(clippy::many_single_char_names, clippy::unreadable_literal)]
2use crate::consts::RC;
3use core::convert::TryInto;
4
5#[inline(always)]
6fn op_f(w: u32, x: u32, y: u32, z: u32, m: u32, c: u32, s: u32) -> u32 {
7    ((x & y) | (!x & z))
8        .wrapping_add(w)
9        .wrapping_add(m)
10        .wrapping_add(c)
11        .rotate_left(s)
12        .wrapping_add(x)
13}
14#[inline(always)]
15fn op_g(w: u32, x: u32, y: u32, z: u32, m: u32, c: u32, s: u32) -> u32 {
16    ((x & z) | (y & !z))
17        .wrapping_add(w)
18        .wrapping_add(m)
19        .wrapping_add(c)
20        .rotate_left(s)
21        .wrapping_add(x)
22}
23
24#[inline(always)]
25fn op_h(w: u32, x: u32, y: u32, z: u32, m: u32, c: u32, s: u32) -> u32 {
26    (x ^ y ^ z)
27        .wrapping_add(w)
28        .wrapping_add(m)
29        .wrapping_add(c)
30        .rotate_left(s)
31        .wrapping_add(x)
32}
33
34#[inline(always)]
35fn op_i(w: u32, x: u32, y: u32, z: u32, m: u32, c: u32, s: u32) -> u32 {
36    (y ^ (x | !z))
37        .wrapping_add(w)
38        .wrapping_add(m)
39        .wrapping_add(c)
40        .rotate_left(s)
41        .wrapping_add(x)
42}
43
44#[inline]
45pub fn compress_block(state: &mut [u32; 4], input: &[u8; 64]) {
46    let mut a = state[0];
47    let mut b = state[1];
48    let mut c = state[2];
49    let mut d = state[3];
50
51    let mut data = [0u32; 16];
52    for (o, chunk) in data.iter_mut().zip(input.chunks_exact(4)) {
53        *o = u32::from_le_bytes(chunk.try_into().unwrap());
54    }
55
56    // round 1
57    a = op_f(a, b, c, d, data[0], RC[0], 7);
58    d = op_f(d, a, b, c, data[1], RC[1], 12);
59    c = op_f(c, d, a, b, data[2], RC[2], 17);
60    b = op_f(b, c, d, a, data[3], RC[3], 22);
61
62    a = op_f(a, b, c, d, data[4], RC[4], 7);
63    d = op_f(d, a, b, c, data[5], RC[5], 12);
64    c = op_f(c, d, a, b, data[6], RC[6], 17);
65    b = op_f(b, c, d, a, data[7], RC[7], 22);
66
67    a = op_f(a, b, c, d, data[8], RC[8], 7);
68    d = op_f(d, a, b, c, data[9], RC[9], 12);
69    c = op_f(c, d, a, b, data[10], RC[10], 17);
70    b = op_f(b, c, d, a, data[11], RC[11], 22);
71
72    a = op_f(a, b, c, d, data[12], RC[12], 7);
73    d = op_f(d, a, b, c, data[13], RC[13], 12);
74    c = op_f(c, d, a, b, data[14], RC[14], 17);
75    b = op_f(b, c, d, a, data[15], RC[15], 22);
76
77    // round 2
78    a = op_g(a, b, c, d, data[1], RC[16], 5);
79    d = op_g(d, a, b, c, data[6], RC[17], 9);
80    c = op_g(c, d, a, b, data[11], RC[18], 14);
81    b = op_g(b, c, d, a, data[0], RC[19], 20);
82
83    a = op_g(a, b, c, d, data[5], RC[20], 5);
84    d = op_g(d, a, b, c, data[10], RC[21], 9);
85    c = op_g(c, d, a, b, data[15], RC[22], 14);
86    b = op_g(b, c, d, a, data[4], RC[23], 20);
87
88    a = op_g(a, b, c, d, data[9], RC[24], 5);
89    d = op_g(d, a, b, c, data[14], RC[25], 9);
90    c = op_g(c, d, a, b, data[3], RC[26], 14);
91    b = op_g(b, c, d, a, data[8], RC[27], 20);
92
93    a = op_g(a, b, c, d, data[13], RC[28], 5);
94    d = op_g(d, a, b, c, data[2], RC[29], 9);
95    c = op_g(c, d, a, b, data[7], RC[30], 14);
96    b = op_g(b, c, d, a, data[12], RC[31], 20);
97
98    // round 3
99    a = op_h(a, b, c, d, data[5], RC[32], 4);
100    d = op_h(d, a, b, c, data[8], RC[33], 11);
101    c = op_h(c, d, a, b, data[11], RC[34], 16);
102    b = op_h(b, c, d, a, data[14], RC[35], 23);
103
104    a = op_h(a, b, c, d, data[1], RC[36], 4);
105    d = op_h(d, a, b, c, data[4], RC[37], 11);
106    c = op_h(c, d, a, b, data[7], RC[38], 16);
107    b = op_h(b, c, d, a, data[10], RC[39], 23);
108
109    a = op_h(a, b, c, d, data[13], RC[40], 4);
110    d = op_h(d, a, b, c, data[0], RC[41], 11);
111    c = op_h(c, d, a, b, data[3], RC[42], 16);
112    b = op_h(b, c, d, a, data[6], RC[43], 23);
113
114    a = op_h(a, b, c, d, data[9], RC[44], 4);
115    d = op_h(d, a, b, c, data[12], RC[45], 11);
116    c = op_h(c, d, a, b, data[15], RC[46], 16);
117    b = op_h(b, c, d, a, data[2], RC[47], 23);
118
119    // round 4
120    a = op_i(a, b, c, d, data[0], RC[48], 6);
121    d = op_i(d, a, b, c, data[7], RC[49], 10);
122    c = op_i(c, d, a, b, data[14], RC[50], 15);
123    b = op_i(b, c, d, a, data[5], RC[51], 21);
124
125    a = op_i(a, b, c, d, data[12], RC[52], 6);
126    d = op_i(d, a, b, c, data[3], RC[53], 10);
127    c = op_i(c, d, a, b, data[10], RC[54], 15);
128    b = op_i(b, c, d, a, data[1], RC[55], 21);
129
130    a = op_i(a, b, c, d, data[8], RC[56], 6);
131    d = op_i(d, a, b, c, data[15], RC[57], 10);
132    c = op_i(c, d, a, b, data[6], RC[58], 15);
133    b = op_i(b, c, d, a, data[13], RC[59], 21);
134
135    a = op_i(a, b, c, d, data[4], RC[60], 6);
136    d = op_i(d, a, b, c, data[11], RC[61], 10);
137    c = op_i(c, d, a, b, data[2], RC[62], 15);
138    b = op_i(b, c, d, a, data[9], RC[63], 21);
139
140    state[0] = state[0].wrapping_add(a);
141    state[1] = state[1].wrapping_add(b);
142    state[2] = state[2].wrapping_add(c);
143    state[3] = state[3].wrapping_add(d);
144}
145
146#[inline]
147pub fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) {
148    for block in blocks {
149        compress_block(state, block)
150    }
151}