This source file includes following definitions.
- rmd320_transform
- rmd320_init
- rmd320_update
- rmd320_final
- rmd320_mod_init
- rmd320_mod_fini
1
2
3
4
5
6
7
8
9
10
11 #include <crypto/internal/hash.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/mm.h>
15 #include <linux/types.h>
16 #include <asm/byteorder.h>
17
18 #include "ripemd.h"
19
20 struct rmd320_ctx {
21 u64 byte_count;
22 u32 state[10];
23 __le32 buffer[16];
24 };
25
26 #define K1 RMD_K1
27 #define K2 RMD_K2
28 #define K3 RMD_K3
29 #define K4 RMD_K4
30 #define K5 RMD_K5
31 #define KK1 RMD_K6
32 #define KK2 RMD_K7
33 #define KK3 RMD_K8
34 #define KK4 RMD_K9
35 #define KK5 RMD_K1
36
37 #define F1(x, y, z) (x ^ y ^ z)
38 #define F2(x, y, z) (z ^ (x & (y ^ z)))
39 #define F3(x, y, z) ((x | ~y) ^ z)
40 #define F4(x, y, z) (y ^ (z & (x ^ y)))
41 #define F5(x, y, z) (x ^ (y | ~z))
42
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); \
47 }
48
49 static void rmd320_transform(u32 *state, const __le32 *in)
50 {
51 u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
52
53
54 aa = state[0];
55 bb = state[1];
56 cc = state[2];
57 dd = state[3];
58 ee = state[4];
59
60
61 aaa = state[5];
62 bbb = state[6];
63 ccc = state[7];
64 ddd = state[8];
65 eee = state[9];
66
67
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);
84
85
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);
102
103
104 swap(aa, aaa);
105
106
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);
123
124
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);
141
142
143 swap(bb, bbb);
144
145
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);
162
163
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);
180
181
182 swap(cc, ccc);
183
184
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);
201
202
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);
219
220
221 swap(dd, ddd);
222
223
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);
240
241
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);
258
259
260 swap(ee, eee);
261
262
263 state[0] += aa;
264 state[1] += bb;
265 state[2] += cc;
266 state[3] += dd;
267 state[4] += ee;
268 state[5] += aaa;
269 state[6] += bbb;
270 state[7] += ccc;
271 state[8] += ddd;
272 state[9] += eee;
273 }
274
275 static int rmd320_init(struct shash_desc *desc)
276 {
277 struct rmd320_ctx *rctx = shash_desc_ctx(desc);
278
279 rctx->byte_count = 0;
280
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;
291
292 memset(rctx->buffer, 0, sizeof(rctx->buffer));
293
294 return 0;
295 }
296
297 static int rmd320_update(struct shash_desc *desc, const u8 *data,
298 unsigned int len)
299 {
300 struct rmd320_ctx *rctx = shash_desc_ctx(desc);
301 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
302
303 rctx->byte_count += len;
304
305
306 if (avail > len) {
307 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
308 data, len);
309 goto out;
310 }
311
312 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
313 data, avail);
314
315 rmd320_transform(rctx->state, rctx->buffer);
316 data += avail;
317 len -= avail;
318
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);
324 }
325
326 memcpy(rctx->buffer, data, len);
327
328 out:
329 return 0;
330 }
331
332
333 static int rmd320_final(struct shash_desc *desc, u8 *out)
334 {
335 struct rmd320_ctx *rctx = shash_desc_ctx(desc);
336 u32 i, index, padlen;
337 __le64 bits;
338 __le32 *dst = (__le32 *)out;
339 static const u8 padding[64] = { 0x80, };
340
341 bits = cpu_to_le64(rctx->byte_count << 3);
342
343
344 index = rctx->byte_count & 0x3f;
345 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
346 rmd320_update(desc, padding, padlen);
347
348
349 rmd320_update(desc, (const u8 *)&bits, sizeof(bits));
350
351
352 for (i = 0; i < 10; i++)
353 dst[i] = cpu_to_le32p(&rctx->state[i]);
354
355
356 memset(rctx, 0, sizeof(*rctx));
357
358 return 0;
359 }
360
361 static struct shash_alg alg = {
362 .digestsize = RMD320_DIGEST_SIZE,
363 .init = rmd320_init,
364 .update = rmd320_update,
365 .final = rmd320_final,
366 .descsize = sizeof(struct rmd320_ctx),
367 .base = {
368 .cra_name = "rmd320",
369 .cra_driver_name = "rmd320-generic",
370 .cra_blocksize = RMD320_BLOCK_SIZE,
371 .cra_module = THIS_MODULE,
372 }
373 };
374
375 static int __init rmd320_mod_init(void)
376 {
377 return crypto_register_shash(&alg);
378 }
379
380 static void __exit rmd320_mod_fini(void)
381 {
382 crypto_unregister_shash(&alg);
383 }
384
385 subsys_initcall(rmd320_mod_init);
386 module_exit(rmd320_mod_fini);
387
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");