1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <crypto/internal/hash.h>
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/mm.h>
20#include <linux/types.h>
21#include <asm/byteorder.h>
22
23#include "ripemd.h"
24
25struct rmd320_ctx {
26 u64 byte_count;
27 u32 state[10];
28 __le32 buffer[16];
29};
30
31#define K1 RMD_K1
32#define K2 RMD_K2
33#define K3 RMD_K3
34#define K4 RMD_K4
35#define K5 RMD_K5
36#define KK1 RMD_K6
37#define KK2 RMD_K7
38#define KK3 RMD_K8
39#define KK4 RMD_K9
40#define KK5 RMD_K1
41
42#define F1(x, y, z) (x ^ y ^ z)
43#define F2(x, y, z) (z ^ (x & (y ^ z)))
44#define F3(x, y, z) ((x | ~y) ^ z)
45#define F4(x, y, z) (y ^ (z & (x ^ y)))
46#define F5(x, y, z) (x ^ (y | ~z))
47
48#define ROUND(a, b, c, d, e, f, k, x, s) { \
49 (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
50 (a) = rol32((a), (s)) + (e); \
51 (c) = rol32((c), 10); \
52}
53
54static void rmd320_transform(u32 *state, const __le32 *in)
55{
56 u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
57
58
59 aa = state[0];
60 bb = state[1];
61 cc = state[2];
62 dd = state[3];
63 ee = state[4];
64
65
66 aaa = state[5];
67 bbb = state[6];
68 ccc = state[7];
69 ddd = state[8];
70 eee = state[9];
71
72
73 ROUND(aa, bb, cc, dd, ee, F1, K1, in[0], 11);
74 ROUND(ee, aa, bb, cc, dd, F1, K1, in[1], 14);
75 ROUND(dd, ee, aa, bb, cc, F1, K1, in[2], 15);
76 ROUND(cc, dd, ee, aa, bb, F1, K1, in[3], 12);
77 ROUND(bb, cc, dd, ee, aa, F1, K1, in[4], 5);
78 ROUND(aa, bb, cc, dd, ee, F1, K1, in[5], 8);
79 ROUND(ee, aa, bb, cc, dd, F1, K1, in[6], 7);
80 ROUND(dd, ee, aa, bb, cc, F1, K1, in[7], 9);
81 ROUND(cc, dd, ee, aa, bb, F1, K1, in[8], 11);
82 ROUND(bb, cc, dd, ee, aa, F1, K1, in[9], 13);
83 ROUND(aa, bb, cc, dd, ee, F1, K1, in[10], 14);
84 ROUND(ee, aa, bb, cc, dd, F1, K1, in[11], 15);
85 ROUND(dd, ee, aa, bb, cc, F1, K1, in[12], 6);
86 ROUND(cc, dd, ee, aa, bb, F1, K1, in[13], 7);
87 ROUND(bb, cc, dd, ee, aa, F1, K1, in[14], 9);
88 ROUND(aa, bb, cc, dd, ee, F1, K1, in[15], 8);
89
90
91 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
92 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
93 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
94 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
95 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
96 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
97 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
98 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
99 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
100 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
101 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
102 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
103 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
104 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
105 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
106 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
107
108
109 swap(aa, aaa);
110
111
112 ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
113 ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
114 ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
115 ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
116 ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
117 ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
118 ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
119 ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
120 ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
121 ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
122 ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
123 ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
124 ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
125 ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
126 ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
127 ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
128
129
130 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
131 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
132 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
133 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
134 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
135 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
136 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
137 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
138 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
139 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
140 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
141 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
142 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
143 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
144 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
145 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
146
147
148 swap(bb, bbb);
149
150
151 ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
152 ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
153 ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
154 ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
155 ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
156 ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
157 ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
158 ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
159 ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
160 ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
161 ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
162 ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
163 ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
164 ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
165 ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
166 ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
167
168
169 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
170 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
171 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
172 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
173 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
174 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
175 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
176 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
177 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
178 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
179 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
180 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
181 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
182 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
183 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
184 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
185
186
187 swap(cc, ccc);
188
189
190 ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
191 ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
192 ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
193 ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
194 ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
195 ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
196 ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
197 ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
198 ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
199 ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
200 ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
201 ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
202 ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
203 ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
204 ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
205 ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
206
207
208 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
209 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
210 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
211 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
212 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
213 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
214 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
215 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
216 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
217 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
218 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
219 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
220 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
221 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
222 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
223 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
224
225
226 swap(dd, ddd);
227
228
229 ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
230 ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
231 ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
232 ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
233 ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
234 ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
235 ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
236 ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
237 ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
238 ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
239 ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
240 ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
241 ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
242 ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
243 ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
244 ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
245
246
247 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
248 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
249 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
250 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
251 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
252 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
253 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
254 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
255 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
256 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
257 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
258 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
259 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
260 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
261 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
262 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
263
264
265 swap(ee, eee);
266
267
268 state[0] += aa;
269 state[1] += bb;
270 state[2] += cc;
271 state[3] += dd;
272 state[4] += ee;
273 state[5] += aaa;
274 state[6] += bbb;
275 state[7] += ccc;
276 state[8] += ddd;
277 state[9] += eee;
278}
279
280static int rmd320_init(struct shash_desc *desc)
281{
282 struct rmd320_ctx *rctx = shash_desc_ctx(desc);
283
284 rctx->byte_count = 0;
285
286 rctx->state[0] = RMD_H0;
287 rctx->state[1] = RMD_H1;
288 rctx->state[2] = RMD_H2;
289 rctx->state[3] = RMD_H3;
290 rctx->state[4] = RMD_H4;
291 rctx->state[5] = RMD_H5;
292 rctx->state[6] = RMD_H6;
293 rctx->state[7] = RMD_H7;
294 rctx->state[8] = RMD_H8;
295 rctx->state[9] = RMD_H9;
296
297 memset(rctx->buffer, 0, sizeof(rctx->buffer));
298
299 return 0;
300}
301
302static int rmd320_update(struct shash_desc *desc, const u8 *data,
303 unsigned int len)
304{
305 struct rmd320_ctx *rctx = shash_desc_ctx(desc);
306 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
307
308 rctx->byte_count += len;
309
310
311 if (avail > len) {
312 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
313 data, len);
314 goto out;
315 }
316
317 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
318 data, avail);
319
320 rmd320_transform(rctx->state, rctx->buffer);
321 data += avail;
322 len -= avail;
323
324 while (len >= sizeof(rctx->buffer)) {
325 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
326 rmd320_transform(rctx->state, rctx->buffer);
327 data += sizeof(rctx->buffer);
328 len -= sizeof(rctx->buffer);
329 }
330
331 memcpy(rctx->buffer, data, len);
332
333out:
334 return 0;
335}
336
337
338static int rmd320_final(struct shash_desc *desc, u8 *out)
339{
340 struct rmd320_ctx *rctx = shash_desc_ctx(desc);
341 u32 i, index, padlen;
342 __le64 bits;
343 __le32 *dst = (__le32 *)out;
344 static const u8 padding[64] = { 0x80, };
345
346 bits = cpu_to_le64(rctx->byte_count << 3);
347
348
349 index = rctx->byte_count & 0x3f;
350 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
351 rmd320_update(desc, padding, padlen);
352
353
354 rmd320_update(desc, (const u8 *)&bits, sizeof(bits));
355
356
357 for (i = 0; i < 10; i++)
358 dst[i] = cpu_to_le32p(&rctx->state[i]);
359
360
361 memset(rctx, 0, sizeof(*rctx));
362
363 return 0;
364}
365
366static struct shash_alg alg = {
367 .digestsize = RMD320_DIGEST_SIZE,
368 .init = rmd320_init,
369 .update = rmd320_update,
370 .final = rmd320_final,
371 .descsize = sizeof(struct rmd320_ctx),
372 .base = {
373 .cra_name = "rmd320",
374 .cra_blocksize = RMD320_BLOCK_SIZE,
375 .cra_module = THIS_MODULE,
376 }
377};
378
379static int __init rmd320_mod_init(void)
380{
381 return crypto_register_shash(&alg);
382}
383
384static void __exit rmd320_mod_fini(void)
385{
386 crypto_unregister_shash(&alg);
387}
388
389module_init(rmd320_mod_init);
390module_exit(rmd320_mod_fini);
391
392MODULE_LICENSE("GPL");
393MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
394MODULE_DESCRIPTION("RIPEMD-320 Message Digest");
395MODULE_ALIAS_CRYPTO("rmd320");
396