Skip to content

Commit 037c4d8

Browse files
committed
ripemd: implement RIPEMD-128
For the sake of completeness, implement the RIPEMD-128 digest algorithm. Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
1 parent cc52373 commit 037c4d8

File tree

5 files changed

+270
-2
lines changed

5 files changed

+270
-2
lines changed

ripemd/benches/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ use digest::bench_update;
55
use ripemd::{Ripemd160, Ripemd256, Ripemd320};
66
use test::Bencher;
77

8+
bench_update!(
9+
Ripemd128::default();
10+
ripemd128_10 10;
11+
ripemd128_100 100;
12+
ripemd128_1000 1000;
13+
ripemd128_10000 10000;
14+
);
15+
816
bench_update!(
917
Ripemd160::default();
1018
ripemd160_10 10;

ripemd/src/c128.rs

+247
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
use core::convert::TryInto;
2+
3+
pub const DIGEST_BUF_LEN: usize = 4;
4+
pub const WORK_BUF_LEN: usize = 16;
5+
6+
pub const H0: [u32; DIGEST_BUF_LEN] = [
7+
0x6745_2301,
8+
0xefcd_ab89,
9+
0x98ba_dcfe,
10+
0x1032_5476,
11+
];
12+
13+
macro_rules! round(
14+
($a:expr, $b:expr, $c:expr, $d:expr,
15+
$x:expr, $bits:expr, $add:expr, $round:expr) => ({
16+
$a = $a.wrapping_add($round).wrapping_add($x).wrapping_add($add);
17+
$a = $a.rotate_left($bits);
18+
});
19+
);
20+
21+
macro_rules! process_block(
22+
($h:ident, $data:expr,
23+
$( round1: h_ordering $f0:expr, $f1:expr, $f2:expr, $f3:expr;
24+
data_index $data_index1:expr; roll_shift $bits1:expr )*;
25+
$( round2: h_ordering $g0:expr, $g1:expr, $g2:expr, $g3:expr;
26+
data_index $data_index2:expr; roll_shift $bits2:expr )*;
27+
$( round3: h_ordering $h0:expr, $h1:expr, $h2:expr, $h3:expr;
28+
data_index $data_index3:expr; roll_shift $bits3:expr )*;
29+
$( round4: h_ordering $i0:expr, $i1:expr, $i2:expr, $i3:expr;
30+
data_index $data_index4:expr; roll_shift $bits4:expr )*;
31+
$( par_round1: h_ordering $pi0:expr, $pi1:expr, $pi2:expr, $pi3:expr;
32+
data_index $pdata_index1:expr; roll_shift $pbits1:expr )*;
33+
$( par_round2: h_ordering $ph0:expr, $ph1:expr, $ph2:expr, $ph3:expr;
34+
data_index $pdata_index2:expr; roll_shift $pbits2:expr )*;
35+
$( par_round3: h_ordering $pg0:expr, $pg1:expr, $pg2:expr, $pg3:expr;
36+
data_index $pdata_index3:expr; roll_shift $pbits3:expr )*;
37+
$( par_round4: h_ordering $pf0:expr, $pf1:expr, $pf2:expr, $pf3:expr;
38+
data_index $pdata_index4:expr; roll_shift $pbits4:expr )*;
39+
) => ({
40+
let mut bb = *$h;
41+
let mut bbb = *$h;
42+
43+
// Round 1
44+
$( round!(bb[$f0], bb[$f1], bb[$f2], bb[$f3],
45+
$data[$data_index1], $bits1, 0x0000_0000,
46+
bb[$f1] ^ bb[$f2] ^ bb[$f3]); )*
47+
48+
// Round 2
49+
$( round!(bb[$g0], bb[$g1], bb[$g2], bb[$g3],
50+
$data[$data_index2], $bits2, 0x5a82_7999,
51+
(bb[$g1] & bb[$g2]) | (!bb[$g1] & bb[$g3])); )*
52+
53+
// Round 3
54+
$( round!(bb[$h0], bb[$h1], bb[$h2], bb[$h3],
55+
$data[$data_index3], $bits3, 0x6ed9_eba1,
56+
(bb[$h1] | !bb[$h2]) ^ bb[$h3]); )*
57+
58+
// Round 4
59+
$( round!(bb[$i0], bb[$i1], bb[$i2], bb[$i3],
60+
$data[$data_index4], $bits4, 0x8f1b_bcdc,
61+
(bb[$i1] & bb[$i3]) | (bb[$i2] & !bb[$i3])); )*
62+
63+
// Parallel rounds: these are the same as the previous five
64+
// rounds except that the constants have changed, we work
65+
// with the other buffer, and they are applied in reverse
66+
// order.
67+
68+
// Parallel Round 1
69+
$( round!(bbb[$pi0], bbb[$pi1], bbb[$pi2], bbb[$pi3],
70+
$data[$pdata_index1], $pbits1, 0x50a2_8be6,
71+
(bbb[$pi1] & bbb[$pi3]) | (bbb[$pi2] & !bbb[$pi3])); )*
72+
73+
// Parallel Round 2
74+
$( round!(bbb[$ph0], bbb[$ph1], bbb[$ph2], bbb[$ph3],
75+
$data[$pdata_index2], $pbits2, 0x5c4d_d124,
76+
(bbb[$ph1] | !bbb[$ph2]) ^ bbb[$ph3]); )*
77+
78+
// Parallel Round 3
79+
$( round!(bbb[$pg0], bbb[$pg1], bbb[$pg2], bbb[$pg3],
80+
$data[$pdata_index3], $pbits3, 0x6d70_3ef3,
81+
(bbb[$pg1] & bbb[$pg2]) | (!bbb[$pg1] & bbb[$pg3])); )*
82+
83+
// Parallel Round 4
84+
$( round!(bbb[$pf0], bbb[$pf1], bbb[$pf2], bbb[$pf3],
85+
$data[$pdata_index4], $pbits4, 0x0000_0000,
86+
bbb[$pf1] ^ bbb[$pf2] ^ bbb[$pf3]); )*
87+
88+
// Combine results
89+
bbb[3] = bbb[3].wrapping_add($h[1]).wrapping_add(bb[2]);
90+
$h[1] = $h[2].wrapping_add(bb[3]).wrapping_add(bbb[0]);
91+
$h[2] = $h[3].wrapping_add(bb[0]).wrapping_add(bbb[1]);
92+
$h[3] = $h[0].wrapping_add(bb[1]).wrapping_add(bbb[2]);
93+
$h[0] = bbb[3];
94+
});
95+
);
96+
97+
pub fn compress(h: &mut [u32; DIGEST_BUF_LEN], data: &[u8; 64]) {
98+
let mut w = [0u32; WORK_BUF_LEN];
99+
for (o, chunk) in w.iter_mut().zip(data.chunks_exact(4)) {
100+
*o = u32::from_le_bytes(chunk.try_into().unwrap());
101+
}
102+
process_block!(h, w[..],
103+
// Round 1
104+
round1: h_ordering 0, 1, 2, 3; data_index 0; roll_shift 11
105+
round1: h_ordering 3, 0, 1, 2; data_index 1; roll_shift 14
106+
round1: h_ordering 2, 3, 0, 1; data_index 2; roll_shift 15
107+
round1: h_ordering 1, 2, 3, 0; data_index 3; roll_shift 12
108+
round1: h_ordering 0, 1, 2, 3; data_index 4; roll_shift 5
109+
round1: h_ordering 3, 0, 1, 2; data_index 5; roll_shift 8
110+
round1: h_ordering 2, 3, 0, 1; data_index 6; roll_shift 7
111+
round1: h_ordering 1, 2, 3, 0; data_index 7; roll_shift 9
112+
round1: h_ordering 0, 1, 2, 3; data_index 8; roll_shift 11
113+
round1: h_ordering 3, 0, 1, 2; data_index 9; roll_shift 13
114+
round1: h_ordering 2, 3, 0, 1; data_index 10; roll_shift 14
115+
round1: h_ordering 1, 2, 3, 0; data_index 11; roll_shift 15
116+
round1: h_ordering 0, 1, 2, 3; data_index 12; roll_shift 6
117+
round1: h_ordering 3, 0, 1, 2; data_index 13; roll_shift 7
118+
round1: h_ordering 2, 3, 0, 1; data_index 14; roll_shift 9
119+
round1: h_ordering 1, 2, 3, 0; data_index 15; roll_shift 8;
120+
121+
// Round 2
122+
round2: h_ordering 0, 1, 2, 3; data_index 7; roll_shift 7
123+
round2: h_ordering 3, 0, 1, 2; data_index 4; roll_shift 6
124+
round2: h_ordering 2, 3, 0, 1; data_index 13; roll_shift 8
125+
round2: h_ordering 1, 2, 3, 0; data_index 1; roll_shift 13
126+
round2: h_ordering 0, 1, 2, 3; data_index 10; roll_shift 11
127+
round2: h_ordering 3, 0, 1, 2; data_index 6; roll_shift 9
128+
round2: h_ordering 2, 3, 0, 1; data_index 15; roll_shift 7
129+
round2: h_ordering 1, 2, 3, 0; data_index 3; roll_shift 15
130+
round2: h_ordering 0, 1, 2, 3; data_index 12; roll_shift 7
131+
round2: h_ordering 3, 0, 1, 2; data_index 0; roll_shift 12
132+
round2: h_ordering 2, 3, 0, 1; data_index 9; roll_shift 15
133+
round2: h_ordering 1, 2, 3, 0; data_index 5; roll_shift 9
134+
round2: h_ordering 0, 1, 2, 3; data_index 2; roll_shift 11
135+
round2: h_ordering 3, 0, 1, 2; data_index 14; roll_shift 7
136+
round2: h_ordering 2, 3, 0, 1; data_index 11; roll_shift 13
137+
round2: h_ordering 1, 2, 3, 0; data_index 8; roll_shift 12;
138+
139+
// Round 3
140+
round3: h_ordering 0, 1, 2, 3; data_index 3; roll_shift 11
141+
round3: h_ordering 3, 0, 1, 2; data_index 10; roll_shift 13
142+
round3: h_ordering 2, 3, 0, 1; data_index 14; roll_shift 6
143+
round3: h_ordering 1, 2, 3, 0; data_index 4; roll_shift 7
144+
round3: h_ordering 0, 1, 2, 3; data_index 9; roll_shift 14
145+
round3: h_ordering 3, 0, 1, 2; data_index 15; roll_shift 9
146+
round3: h_ordering 2, 3, 0, 1; data_index 8; roll_shift 13
147+
round3: h_ordering 1, 2, 3, 0; data_index 1; roll_shift 15
148+
round3: h_ordering 0, 1, 2, 3; data_index 2; roll_shift 14
149+
round3: h_ordering 3, 0, 1, 2; data_index 7; roll_shift 8
150+
round3: h_ordering 2, 3, 0, 1; data_index 0; roll_shift 13
151+
round3: h_ordering 1, 2, 3, 0; data_index 6; roll_shift 6
152+
round3: h_ordering 0, 1, 2, 3; data_index 13; roll_shift 5
153+
round3: h_ordering 3, 0, 1, 2; data_index 11; roll_shift 12
154+
round3: h_ordering 2, 3, 0, 1; data_index 5; roll_shift 7
155+
round3: h_ordering 1, 2, 3, 0; data_index 12; roll_shift 5;
156+
157+
// Round 4
158+
round4: h_ordering 0, 1, 2, 3; data_index 1; roll_shift 11
159+
round4: h_ordering 3, 0, 1, 2; data_index 9; roll_shift 12
160+
round4: h_ordering 2, 3, 0, 1; data_index 11; roll_shift 14
161+
round4: h_ordering 1, 2, 3, 0; data_index 10; roll_shift 15
162+
round4: h_ordering 0, 1, 2, 3; data_index 0; roll_shift 14
163+
round4: h_ordering 3, 0, 1, 2; data_index 8; roll_shift 15
164+
round4: h_ordering 2, 3, 0, 1; data_index 12; roll_shift 9
165+
round4: h_ordering 1, 2, 3, 0; data_index 4; roll_shift 8
166+
round4: h_ordering 0, 1, 2, 3; data_index 13; roll_shift 9
167+
round4: h_ordering 3, 0, 1, 2; data_index 3; roll_shift 14
168+
round4: h_ordering 2, 3, 0, 1; data_index 7; roll_shift 5
169+
round4: h_ordering 1, 2, 3, 0; data_index 15; roll_shift 6
170+
round4: h_ordering 0, 1, 2, 3; data_index 14; roll_shift 8
171+
round4: h_ordering 3, 0, 1, 2; data_index 5; roll_shift 6
172+
round4: h_ordering 2, 3, 0, 1; data_index 6; roll_shift 5
173+
round4: h_ordering 1, 2, 3, 0; data_index 2; roll_shift 12;
174+
175+
// Parallel Round 1
176+
par_round1: h_ordering 0, 1, 2, 3; data_index 5; roll_shift 8
177+
par_round1: h_ordering 3, 0, 1, 2; data_index 14; roll_shift 9
178+
par_round1: h_ordering 2, 3, 0, 1; data_index 7; roll_shift 9
179+
par_round1: h_ordering 1, 2, 3, 0; data_index 0; roll_shift 11
180+
par_round1: h_ordering 0, 1, 2, 3; data_index 9; roll_shift 13
181+
par_round1: h_ordering 3, 0, 1, 2; data_index 2; roll_shift 15
182+
par_round1: h_ordering 2, 3, 0, 1; data_index 11; roll_shift 15
183+
par_round1: h_ordering 1, 2, 3, 0; data_index 4; roll_shift 5
184+
par_round1: h_ordering 0, 1, 2, 3; data_index 13; roll_shift 7
185+
par_round1: h_ordering 3, 0, 1, 2; data_index 6; roll_shift 7
186+
par_round1: h_ordering 2, 3, 0, 1; data_index 15; roll_shift 8
187+
par_round1: h_ordering 1, 2, 3, 0; data_index 8; roll_shift 11
188+
par_round1: h_ordering 0, 1, 2, 3; data_index 1; roll_shift 14
189+
par_round1: h_ordering 3, 0, 1, 2; data_index 10; roll_shift 14
190+
par_round1: h_ordering 2, 3, 0, 1; data_index 3; roll_shift 12
191+
par_round1: h_ordering 1, 2, 3, 0; data_index 12; roll_shift 6;
192+
193+
// Parallel Round 2
194+
par_round2: h_ordering 0, 1, 2, 3; data_index 6; roll_shift 9
195+
par_round2: h_ordering 3, 0, 1, 2; data_index 11; roll_shift 13
196+
par_round2: h_ordering 2, 3, 0, 1; data_index 3; roll_shift 15
197+
par_round2: h_ordering 1, 2, 3, 0; data_index 7; roll_shift 7
198+
par_round2: h_ordering 0, 1, 2, 3; data_index 0; roll_shift 12
199+
par_round2: h_ordering 3, 0, 1, 2; data_index 13; roll_shift 8
200+
par_round2: h_ordering 2, 3, 0, 1; data_index 5; roll_shift 9
201+
par_round2: h_ordering 1, 2, 3, 0; data_index 10; roll_shift 11
202+
par_round2: h_ordering 0, 1, 2, 3; data_index 14; roll_shift 7
203+
par_round2: h_ordering 3, 0, 1, 2; data_index 15; roll_shift 7
204+
par_round2: h_ordering 2, 3, 0, 1; data_index 8; roll_shift 12
205+
par_round2: h_ordering 1, 2, 3, 0; data_index 12; roll_shift 7
206+
par_round2: h_ordering 0, 1, 2, 3; data_index 4; roll_shift 6
207+
par_round2: h_ordering 3, 0, 1, 2; data_index 9; roll_shift 15
208+
par_round2: h_ordering 2, 3, 0, 1; data_index 1; roll_shift 13
209+
par_round2: h_ordering 1, 2, 3, 0; data_index 2; roll_shift 11;
210+
211+
// Parallel Round 3
212+
par_round3: h_ordering 0, 1, 2, 3; data_index 15; roll_shift 9
213+
par_round3: h_ordering 3, 0, 1, 2; data_index 5; roll_shift 7
214+
par_round3: h_ordering 2, 3, 0, 1; data_index 1; roll_shift 15
215+
par_round3: h_ordering 1, 2, 3, 0; data_index 3; roll_shift 11
216+
par_round3: h_ordering 0, 1, 2, 3; data_index 7; roll_shift 8
217+
par_round3: h_ordering 3, 0, 1, 2; data_index 14; roll_shift 6
218+
par_round3: h_ordering 2, 3, 0, 1; data_index 6; roll_shift 6
219+
par_round3: h_ordering 1, 2, 3, 0; data_index 9; roll_shift 14
220+
par_round3: h_ordering 0, 1, 2, 3; data_index 11; roll_shift 12
221+
par_round3: h_ordering 3, 0, 1, 2; data_index 8; roll_shift 13
222+
par_round3: h_ordering 2, 3, 0, 1; data_index 12; roll_shift 5
223+
par_round3: h_ordering 1, 2, 3, 0; data_index 2; roll_shift 14
224+
par_round3: h_ordering 0, 1, 2, 3; data_index 10; roll_shift 13
225+
par_round3: h_ordering 3, 0, 1, 2; data_index 0; roll_shift 13
226+
par_round3: h_ordering 2, 3, 0, 1; data_index 4; roll_shift 7
227+
par_round3: h_ordering 1, 2, 3, 0; data_index 13; roll_shift 5;
228+
229+
// Parallel Round 4
230+
par_round4: h_ordering 0, 1, 2, 3; data_index 8; roll_shift 15
231+
par_round4: h_ordering 3, 0, 1, 2; data_index 6; roll_shift 5
232+
par_round4: h_ordering 2, 3, 0, 1; data_index 4; roll_shift 8
233+
par_round4: h_ordering 1, 2, 3, 0; data_index 1; roll_shift 11
234+
par_round4: h_ordering 0, 1, 2, 3; data_index 3; roll_shift 14
235+
par_round4: h_ordering 3, 0, 1, 2; data_index 11; roll_shift 14
236+
par_round4: h_ordering 2, 3, 0, 1; data_index 15; roll_shift 6
237+
par_round4: h_ordering 1, 2, 3, 0; data_index 0; roll_shift 14
238+
par_round4: h_ordering 0, 1, 2, 3; data_index 5; roll_shift 6
239+
par_round4: h_ordering 3, 0, 1, 2; data_index 12; roll_shift 9
240+
par_round4: h_ordering 2, 3, 0, 1; data_index 2; roll_shift 12
241+
par_round4: h_ordering 1, 2, 3, 0; data_index 13; roll_shift 9
242+
par_round4: h_ordering 0, 1, 2, 3; data_index 9; roll_shift 12
243+
par_round4: h_ordering 3, 0, 1, 2; data_index 7; roll_shift 5
244+
par_round4: h_ordering 2, 3, 0, 1; data_index 10; roll_shift 15
245+
par_round4: h_ordering 1, 2, 3, 0; data_index 14; roll_shift 8;
246+
);
247+
}

ripemd/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ use digest::{
5555
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore,
5656
OutputSizeUser, Reset, UpdateCore,
5757
},
58-
typenum::{Unsigned, U20, U32, U40, U64},
58+
typenum::{Unsigned, U16, U20, U32, U40, U64},
5959
HashMarker, Output,
6060
};
6161

62+
mod c128;
6263
mod c160;
6364
mod c256;
6465
mod c320;
@@ -153,6 +154,7 @@ macro_rules! impl_ripemd {
153154
};
154155
}
155156

157+
impl_ripemd!(Ripemd128Core, Ripemd128, c128, "128", "RIPEMD-128", U16);
156158
impl_ripemd!(Ripemd160Core, Ripemd160, c160, "160", "RIPEMD-160", U20);
157159
impl_ripemd!(Ripemd256Core, Ripemd256, c256, "256", "RIPEMD-256", U32);
158160
impl_ripemd!(Ripemd320Core, Ripemd320, c320, "320", "RIPEMD-320", U40);

ripemd/tests/data/ripemd128.blb

236 Bytes
Binary file not shown.

ripemd/tests/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
use digest::dev::{feed_rand_16mib, fixed_reset_test};
22
use digest::new_test;
33
use hex_literal::hex;
4-
use ripemd::{Digest, Ripemd160, Ripemd256, Ripemd320};
4+
use ripemd::{Digest, Ripemd128, Ripemd160, Ripemd256, Ripemd320};
55

66
// Test vectors from FIPS 180-1 and from the [RIPEMD webpage][1].
77
//
88
// [1] https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
9+
new_test!(ripemd128_main, "ripemd128", Ripemd128, fixed_reset_test);
910
new_test!(ripemd160_main, "ripemd160", Ripemd160, fixed_reset_test);
1011
new_test!(ripemd256_main, "ripemd256", Ripemd256, fixed_reset_test);
1112
new_test!(ripemd320_main, "ripemd320", Ripemd320, fixed_reset_test);
1213

14+
#[test]
15+
fn ripemd128_rand() {
16+
let mut h = Ripemd128::new();
17+
feed_rand_16mib(&mut h);
18+
assert_eq!(
19+
h.finalize()[..],
20+
hex!("01eb52529bcec15bd0cb4040ec998632")[..]
21+
);
22+
}
23+
1324
#[test]
1425
fn ripemd160_rand() {
1526
let mut h = Ripemd160::new();

0 commit comments

Comments
 (0)