1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * RIPEMD-320 - RACE Integrity Primitives Evaluation Message Digest.
7 * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
9 * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
11 #include <crypto/internal/hash.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <asm/byteorder.h>
37 #define F1(x, y, z) (x ^ y ^ z) /* XOR */
38 #define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
39 #define F3(x, y, z) ((x | ~y) ^ z)
40 #define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
41 #define F5(x, y, z) (x ^ (y | ~z))
43 #define ROUND(a, b, c, d, e, f, k, x, s) { \
44 (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
45 (a) = rol32((a), (s)) + (e); \
46 (c) = rol32((c), 10); \
49 static void rmd320_transform(u32
*state
, const __le32
*in
)
51 u32 aa
, bb
, cc
, dd
, ee
, aaa
, bbb
, ccc
, ddd
, eee
;
53 /* Initialize left lane */
60 /* Initialize right lane */
67 /* round 1: left lane */
68 ROUND(aa
, bb
, cc
, dd
, ee
, F1
, K1
, in
[0], 11);
69 ROUND(ee
, aa
, bb
, cc
, dd
, F1
, K1
, in
[1], 14);
70 ROUND(dd
, ee
, aa
, bb
, cc
, F1
, K1
, in
[2], 15);
71 ROUND(cc
, dd
, ee
, aa
, bb
, F1
, K1
, in
[3], 12);
72 ROUND(bb
, cc
, dd
, ee
, aa
, F1
, K1
, in
[4], 5);
73 ROUND(aa
, bb
, cc
, dd
, ee
, F1
, K1
, in
[5], 8);
74 ROUND(ee
, aa
, bb
, cc
, dd
, F1
, K1
, in
[6], 7);
75 ROUND(dd
, ee
, aa
, bb
, cc
, F1
, K1
, in
[7], 9);
76 ROUND(cc
, dd
, ee
, aa
, bb
, F1
, K1
, in
[8], 11);
77 ROUND(bb
, cc
, dd
, ee
, aa
, F1
, K1
, in
[9], 13);
78 ROUND(aa
, bb
, cc
, dd
, ee
, F1
, K1
, in
[10], 14);
79 ROUND(ee
, aa
, bb
, cc
, dd
, F1
, K1
, in
[11], 15);
80 ROUND(dd
, ee
, aa
, bb
, cc
, F1
, K1
, in
[12], 6);
81 ROUND(cc
, dd
, ee
, aa
, bb
, F1
, K1
, in
[13], 7);
82 ROUND(bb
, cc
, dd
, ee
, aa
, F1
, K1
, in
[14], 9);
83 ROUND(aa
, bb
, cc
, dd
, ee
, F1
, K1
, in
[15], 8);
85 /* round 1: right lane */
86 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[5], 8);
87 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F5
, KK1
, in
[14], 9);
88 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F5
, KK1
, in
[7], 9);
89 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F5
, KK1
, in
[0], 11);
90 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F5
, KK1
, in
[9], 13);
91 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[2], 15);
92 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F5
, KK1
, in
[11], 15);
93 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F5
, KK1
, in
[4], 5);
94 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F5
, KK1
, in
[13], 7);
95 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F5
, KK1
, in
[6], 7);
96 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[15], 8);
97 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F5
, KK1
, in
[8], 11);
98 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F5
, KK1
, in
[1], 14);
99 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F5
, KK1
, in
[10], 14);
100 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F5
, KK1
, in
[3], 12);
101 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[12], 6);
103 /* Swap contents of "a" registers */
106 /* round 2: left lane" */
107 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[7], 7);
108 ROUND(dd
, ee
, aa
, bb
, cc
, F2
, K2
, in
[4], 6);
109 ROUND(cc
, dd
, ee
, aa
, bb
, F2
, K2
, in
[13], 8);
110 ROUND(bb
, cc
, dd
, ee
, aa
, F2
, K2
, in
[1], 13);
111 ROUND(aa
, bb
, cc
, dd
, ee
, F2
, K2
, in
[10], 11);
112 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[6], 9);
113 ROUND(dd
, ee
, aa
, bb
, cc
, F2
, K2
, in
[15], 7);
114 ROUND(cc
, dd
, ee
, aa
, bb
, F2
, K2
, in
[3], 15);
115 ROUND(bb
, cc
, dd
, ee
, aa
, F2
, K2
, in
[12], 7);
116 ROUND(aa
, bb
, cc
, dd
, ee
, F2
, K2
, in
[0], 12);
117 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[9], 15);
118 ROUND(dd
, ee
, aa
, bb
, cc
, F2
, K2
, in
[5], 9);
119 ROUND(cc
, dd
, ee
, aa
, bb
, F2
, K2
, in
[2], 11);
120 ROUND(bb
, cc
, dd
, ee
, aa
, F2
, K2
, in
[14], 7);
121 ROUND(aa
, bb
, cc
, dd
, ee
, F2
, K2
, in
[11], 13);
122 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[8], 12);
124 /* round 2: right lane */
125 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[6], 9);
126 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F4
, KK2
, in
[11], 13);
127 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F4
, KK2
, in
[3], 15);
128 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F4
, KK2
, in
[7], 7);
129 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F4
, KK2
, in
[0], 12);
130 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[13], 8);
131 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F4
, KK2
, in
[5], 9);
132 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F4
, KK2
, in
[10], 11);
133 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F4
, KK2
, in
[14], 7);
134 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F4
, KK2
, in
[15], 7);
135 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[8], 12);
136 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F4
, KK2
, in
[12], 7);
137 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F4
, KK2
, in
[4], 6);
138 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F4
, KK2
, in
[9], 15);
139 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F4
, KK2
, in
[1], 13);
140 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[2], 11);
142 /* Swap contents of "b" registers */
145 /* round 3: left lane" */
146 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[3], 11);
147 ROUND(cc
, dd
, ee
, aa
, bb
, F3
, K3
, in
[10], 13);
148 ROUND(bb
, cc
, dd
, ee
, aa
, F3
, K3
, in
[14], 6);
149 ROUND(aa
, bb
, cc
, dd
, ee
, F3
, K3
, in
[4], 7);
150 ROUND(ee
, aa
, bb
, cc
, dd
, F3
, K3
, in
[9], 14);
151 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[15], 9);
152 ROUND(cc
, dd
, ee
, aa
, bb
, F3
, K3
, in
[8], 13);
153 ROUND(bb
, cc
, dd
, ee
, aa
, F3
, K3
, in
[1], 15);
154 ROUND(aa
, bb
, cc
, dd
, ee
, F3
, K3
, in
[2], 14);
155 ROUND(ee
, aa
, bb
, cc
, dd
, F3
, K3
, in
[7], 8);
156 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[0], 13);
157 ROUND(cc
, dd
, ee
, aa
, bb
, F3
, K3
, in
[6], 6);
158 ROUND(bb
, cc
, dd
, ee
, aa
, F3
, K3
, in
[13], 5);
159 ROUND(aa
, bb
, cc
, dd
, ee
, F3
, K3
, in
[11], 12);
160 ROUND(ee
, aa
, bb
, cc
, dd
, F3
, K3
, in
[5], 7);
161 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[12], 5);
163 /* round 3: right lane */
164 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[15], 9);
165 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F3
, KK3
, in
[5], 7);
166 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F3
, KK3
, in
[1], 15);
167 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F3
, KK3
, in
[3], 11);
168 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F3
, KK3
, in
[7], 8);
169 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[14], 6);
170 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F3
, KK3
, in
[6], 6);
171 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F3
, KK3
, in
[9], 14);
172 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F3
, KK3
, in
[11], 12);
173 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F3
, KK3
, in
[8], 13);
174 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[12], 5);
175 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F3
, KK3
, in
[2], 14);
176 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F3
, KK3
, in
[10], 13);
177 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F3
, KK3
, in
[0], 13);
178 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F3
, KK3
, in
[4], 7);
179 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[13], 5);
181 /* Swap contents of "c" registers */
184 /* round 4: left lane" */
185 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[1], 11);
186 ROUND(bb
, cc
, dd
, ee
, aa
, F4
, K4
, in
[9], 12);
187 ROUND(aa
, bb
, cc
, dd
, ee
, F4
, K4
, in
[11], 14);
188 ROUND(ee
, aa
, bb
, cc
, dd
, F4
, K4
, in
[10], 15);
189 ROUND(dd
, ee
, aa
, bb
, cc
, F4
, K4
, in
[0], 14);
190 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[8], 15);
191 ROUND(bb
, cc
, dd
, ee
, aa
, F4
, K4
, in
[12], 9);
192 ROUND(aa
, bb
, cc
, dd
, ee
, F4
, K4
, in
[4], 8);
193 ROUND(ee
, aa
, bb
, cc
, dd
, F4
, K4
, in
[13], 9);
194 ROUND(dd
, ee
, aa
, bb
, cc
, F4
, K4
, in
[3], 14);
195 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[7], 5);
196 ROUND(bb
, cc
, dd
, ee
, aa
, F4
, K4
, in
[15], 6);
197 ROUND(aa
, bb
, cc
, dd
, ee
, F4
, K4
, in
[14], 8);
198 ROUND(ee
, aa
, bb
, cc
, dd
, F4
, K4
, in
[5], 6);
199 ROUND(dd
, ee
, aa
, bb
, cc
, F4
, K4
, in
[6], 5);
200 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[2], 12);
202 /* round 4: right lane */
203 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[8], 15);
204 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F2
, KK4
, in
[6], 5);
205 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F2
, KK4
, in
[4], 8);
206 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F2
, KK4
, in
[1], 11);
207 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F2
, KK4
, in
[3], 14);
208 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[11], 14);
209 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F2
, KK4
, in
[15], 6);
210 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F2
, KK4
, in
[0], 14);
211 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F2
, KK4
, in
[5], 6);
212 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F2
, KK4
, in
[12], 9);
213 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[2], 12);
214 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F2
, KK4
, in
[13], 9);
215 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F2
, KK4
, in
[9], 12);
216 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F2
, KK4
, in
[7], 5);
217 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F2
, KK4
, in
[10], 15);
218 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[14], 8);
220 /* Swap contents of "d" registers */
223 /* round 5: left lane" */
224 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[4], 9);
225 ROUND(aa
, bb
, cc
, dd
, ee
, F5
, K5
, in
[0], 15);
226 ROUND(ee
, aa
, bb
, cc
, dd
, F5
, K5
, in
[5], 5);
227 ROUND(dd
, ee
, aa
, bb
, cc
, F5
, K5
, in
[9], 11);
228 ROUND(cc
, dd
, ee
, aa
, bb
, F5
, K5
, in
[7], 6);
229 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[12], 8);
230 ROUND(aa
, bb
, cc
, dd
, ee
, F5
, K5
, in
[2], 13);
231 ROUND(ee
, aa
, bb
, cc
, dd
, F5
, K5
, in
[10], 12);
232 ROUND(dd
, ee
, aa
, bb
, cc
, F5
, K5
, in
[14], 5);
233 ROUND(cc
, dd
, ee
, aa
, bb
, F5
, K5
, in
[1], 12);
234 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[3], 13);
235 ROUND(aa
, bb
, cc
, dd
, ee
, F5
, K5
, in
[8], 14);
236 ROUND(ee
, aa
, bb
, cc
, dd
, F5
, K5
, in
[11], 11);
237 ROUND(dd
, ee
, aa
, bb
, cc
, F5
, K5
, in
[6], 8);
238 ROUND(cc
, dd
, ee
, aa
, bb
, F5
, K5
, in
[15], 5);
239 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[13], 6);
241 /* round 5: right lane */
242 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[12], 8);
243 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F1
, KK5
, in
[15], 5);
244 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F1
, KK5
, in
[10], 12);
245 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F1
, KK5
, in
[4], 9);
246 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F1
, KK5
, in
[1], 12);
247 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[5], 5);
248 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F1
, KK5
, in
[8], 14);
249 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F1
, KK5
, in
[7], 6);
250 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F1
, KK5
, in
[6], 8);
251 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F1
, KK5
, in
[2], 13);
252 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[13], 6);
253 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F1
, KK5
, in
[14], 5);
254 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F1
, KK5
, in
[0], 15);
255 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F1
, KK5
, in
[3], 13);
256 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F1
, KK5
, in
[9], 11);
257 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[11], 11);
259 /* Swap contents of "e" registers */
262 /* combine results */
275 static int rmd320_init(struct shash_desc
*desc
)
277 struct rmd320_ctx
*rctx
= shash_desc_ctx(desc
);
279 rctx
->byte_count
= 0;
281 rctx
->state
[0] = RMD_H0
;
282 rctx
->state
[1] = RMD_H1
;
283 rctx
->state
[2] = RMD_H2
;
284 rctx
->state
[3] = RMD_H3
;
285 rctx
->state
[4] = RMD_H4
;
286 rctx
->state
[5] = RMD_H5
;
287 rctx
->state
[6] = RMD_H6
;
288 rctx
->state
[7] = RMD_H7
;
289 rctx
->state
[8] = RMD_H8
;
290 rctx
->state
[9] = RMD_H9
;
292 memset(rctx
->buffer
, 0, sizeof(rctx
->buffer
));
297 static int rmd320_update(struct shash_desc
*desc
, const u8
*data
,
300 struct rmd320_ctx
*rctx
= shash_desc_ctx(desc
);
301 const u32 avail
= sizeof(rctx
->buffer
) - (rctx
->byte_count
& 0x3f);
303 rctx
->byte_count
+= len
;
305 /* Enough space in buffer? If so copy and we're done */
307 memcpy((char *)rctx
->buffer
+ (sizeof(rctx
->buffer
) - avail
),
312 memcpy((char *)rctx
->buffer
+ (sizeof(rctx
->buffer
) - avail
),
315 rmd320_transform(rctx
->state
, rctx
->buffer
);
319 while (len
>= sizeof(rctx
->buffer
)) {
320 memcpy(rctx
->buffer
, data
, sizeof(rctx
->buffer
));
321 rmd320_transform(rctx
->state
, rctx
->buffer
);
322 data
+= sizeof(rctx
->buffer
);
323 len
-= sizeof(rctx
->buffer
);
326 memcpy(rctx
->buffer
, data
, len
);
332 /* Add padding and return the message digest. */
333 static int rmd320_final(struct shash_desc
*desc
, u8
*out
)
335 struct rmd320_ctx
*rctx
= shash_desc_ctx(desc
);
336 u32 i
, index
, padlen
;
338 __le32
*dst
= (__le32
*)out
;
339 static const u8 padding
[64] = { 0x80, };
341 bits
= cpu_to_le64(rctx
->byte_count
<< 3);
343 /* Pad out to 56 mod 64 */
344 index
= rctx
->byte_count
& 0x3f;
345 padlen
= (index
< 56) ? (56 - index
) : ((64+56) - index
);
346 rmd320_update(desc
, padding
, padlen
);
349 rmd320_update(desc
, (const u8
*)&bits
, sizeof(bits
));
351 /* Store state in digest */
352 for (i
= 0; i
< 10; i
++)
353 dst
[i
] = cpu_to_le32p(&rctx
->state
[i
]);
356 memset(rctx
, 0, sizeof(*rctx
));
361 static struct shash_alg alg
= {
362 .digestsize
= RMD320_DIGEST_SIZE
,
364 .update
= rmd320_update
,
365 .final
= rmd320_final
,
366 .descsize
= sizeof(struct rmd320_ctx
),
368 .cra_name
= "rmd320",
369 .cra_driver_name
= "rmd320-generic",
370 .cra_blocksize
= RMD320_BLOCK_SIZE
,
371 .cra_module
= THIS_MODULE
,
375 static int __init
rmd320_mod_init(void)
377 return crypto_register_shash(&alg
);
380 static void __exit
rmd320_mod_fini(void)
382 crypto_unregister_shash(&alg
);
385 subsys_initcall(rmd320_mod_init
);
386 module_exit(rmd320_mod_fini
);
388 MODULE_LICENSE("GPL");
389 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
390 MODULE_DESCRIPTION("RIPEMD-320 Message Digest");
391 MODULE_ALIAS_CRYPTO("rmd320");