1 /*
2 * Cryptographic API.
3 *
4 * RIPEMD-256 - RACE Integrity Primitives Evaluation Message Digest.
5 *
6 * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
7 *
8 * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
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
25 struct rmd256_ctx {
26 u64 byte_count;
27 u32 state[8];
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 KK1 RMD_K6
36 #define KK2 RMD_K7
37 #define KK3 RMD_K8
38 #define KK4 RMD_K1
39
40 #define F1(x, y, z) (x ^ y ^ z) /* XOR */
41 #define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
42 #define F3(x, y, z) ((x | ~y) ^ z)
43 #define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
44
45 #define ROUND(a, b, c, d, f, k, x, s) { \
46 (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
47 (a) = rol32((a), (s)); \
48 }
49
rmd256_transform(u32 * state,const __le32 * in)50 static void rmd256_transform(u32 *state, const __le32 *in)
51 {
52 u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
53
54 /* Initialize left lane */
55 aa = state[0];
56 bb = state[1];
57 cc = state[2];
58 dd = state[3];
59
60 /* Initialize right lane */
61 aaa = state[4];
62 bbb = state[5];
63 ccc = state[6];
64 ddd = state[7];
65
66 /* round 1: left lane */
67 ROUND(aa, bb, cc, dd, F1, K1, in[0], 11);
68 ROUND(dd, aa, bb, cc, F1, K1, in[1], 14);
69 ROUND(cc, dd, aa, bb, F1, K1, in[2], 15);
70 ROUND(bb, cc, dd, aa, F1, K1, in[3], 12);
71 ROUND(aa, bb, cc, dd, F1, K1, in[4], 5);
72 ROUND(dd, aa, bb, cc, F1, K1, in[5], 8);
73 ROUND(cc, dd, aa, bb, F1, K1, in[6], 7);
74 ROUND(bb, cc, dd, aa, F1, K1, in[7], 9);
75 ROUND(aa, bb, cc, dd, F1, K1, in[8], 11);
76 ROUND(dd, aa, bb, cc, F1, K1, in[9], 13);
77 ROUND(cc, dd, aa, bb, F1, K1, in[10], 14);
78 ROUND(bb, cc, dd, aa, F1, K1, in[11], 15);
79 ROUND(aa, bb, cc, dd, F1, K1, in[12], 6);
80 ROUND(dd, aa, bb, cc, F1, K1, in[13], 7);
81 ROUND(cc, dd, aa, bb, F1, K1, in[14], 9);
82 ROUND(bb, cc, dd, aa, F1, K1, in[15], 8);
83
84 /* round 1: right lane */
85 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
86 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
87 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
88 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
89 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
90 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
91 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
92 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
93 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
94 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
95 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
96 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
97 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
98 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
99 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
100 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
101
102 /* Swap contents of "a" registers */
103 swap(aa, aaa);
104
105 /* round 2: left lane */
106 ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
107 ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
108 ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
109 ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
110 ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
111 ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
112 ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
113 ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
114 ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
115 ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
116 ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
117 ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
118 ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
119 ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
120 ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
121 ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
122
123 /* round 2: right lane */
124 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
125 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
126 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
127 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
128 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
129 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
130 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
131 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
132 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
133 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
134 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
135 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
136 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
137 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
138 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
139 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
140
141 /* Swap contents of "b" registers */
142 swap(bb, bbb);
143
144 /* round 3: left lane */
145 ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
146 ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
147 ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
148 ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
149 ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
150 ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
151 ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
152 ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
153 ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
154 ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
155 ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
156 ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
157 ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
158 ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
159 ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
160 ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
161
162 /* round 3: right lane */
163 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
164 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
165 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
166 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
167 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
168 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
169 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
170 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
171 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
172 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
173 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
174 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
175 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
176 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
177 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
178 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
179
180 /* Swap contents of "c" registers */
181 swap(cc, ccc);
182
183 /* round 4: left lane */
184 ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
185 ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
186 ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
187 ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
188 ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
189 ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
190 ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
191 ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
192 ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
193 ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
194 ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
195 ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
196 ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
197 ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
198 ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
199 ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
200
201 /* round 4: right lane */
202 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
203 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
204 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
205 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
206 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
207 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
208 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
209 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
210 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
211 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
212 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
213 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
214 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
215 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
216 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
217 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
218
219 /* Swap contents of "d" registers */
220 swap(dd, ddd);
221
222 /* combine results */
223 state[0] += aa;
224 state[1] += bb;
225 state[2] += cc;
226 state[3] += dd;
227 state[4] += aaa;
228 state[5] += bbb;
229 state[6] += ccc;
230 state[7] += ddd;
231 }
232
rmd256_init(struct shash_desc * desc)233 static int rmd256_init(struct shash_desc *desc)
234 {
235 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
236
237 rctx->byte_count = 0;
238
239 rctx->state[0] = RMD_H0;
240 rctx->state[1] = RMD_H1;
241 rctx->state[2] = RMD_H2;
242 rctx->state[3] = RMD_H3;
243 rctx->state[4] = RMD_H5;
244 rctx->state[5] = RMD_H6;
245 rctx->state[6] = RMD_H7;
246 rctx->state[7] = RMD_H8;
247
248 memset(rctx->buffer, 0, sizeof(rctx->buffer));
249
250 return 0;
251 }
252
rmd256_update(struct shash_desc * desc,const u8 * data,unsigned int len)253 static int rmd256_update(struct shash_desc *desc, const u8 *data,
254 unsigned int len)
255 {
256 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
257 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
258
259 rctx->byte_count += len;
260
261 /* Enough space in buffer? If so copy and we're done */
262 if (avail > len) {
263 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
264 data, len);
265 goto out;
266 }
267
268 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
269 data, avail);
270
271 rmd256_transform(rctx->state, rctx->buffer);
272 data += avail;
273 len -= avail;
274
275 while (len >= sizeof(rctx->buffer)) {
276 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
277 rmd256_transform(rctx->state, rctx->buffer);
278 data += sizeof(rctx->buffer);
279 len -= sizeof(rctx->buffer);
280 }
281
282 memcpy(rctx->buffer, data, len);
283
284 out:
285 return 0;
286 }
287
288 /* Add padding and return the message digest. */
rmd256_final(struct shash_desc * desc,u8 * out)289 static int rmd256_final(struct shash_desc *desc, u8 *out)
290 {
291 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
292 u32 i, index, padlen;
293 __le64 bits;
294 __le32 *dst = (__le32 *)out;
295 static const u8 padding[64] = { 0x80, };
296
297 bits = cpu_to_le64(rctx->byte_count << 3);
298
299 /* Pad out to 56 mod 64 */
300 index = rctx->byte_count & 0x3f;
301 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
302 rmd256_update(desc, padding, padlen);
303
304 /* Append length */
305 rmd256_update(desc, (const u8 *)&bits, sizeof(bits));
306
307 /* Store state in digest */
308 for (i = 0; i < 8; i++)
309 dst[i] = cpu_to_le32p(&rctx->state[i]);
310
311 /* Wipe context */
312 memset(rctx, 0, sizeof(*rctx));
313
314 return 0;
315 }
316
317 static struct shash_alg alg = {
318 .digestsize = RMD256_DIGEST_SIZE,
319 .init = rmd256_init,
320 .update = rmd256_update,
321 .final = rmd256_final,
322 .descsize = sizeof(struct rmd256_ctx),
323 .base = {
324 .cra_name = "rmd256",
325 .cra_blocksize = RMD256_BLOCK_SIZE,
326 .cra_module = THIS_MODULE,
327 }
328 };
329
rmd256_mod_init(void)330 static int __init rmd256_mod_init(void)
331 {
332 return crypto_register_shash(&alg);
333 }
334
rmd256_mod_fini(void)335 static void __exit rmd256_mod_fini(void)
336 {
337 crypto_unregister_shash(&alg);
338 }
339
340 module_init(rmd256_mod_init);
341 module_exit(rmd256_mod_fini);
342
343 MODULE_LICENSE("GPL");
344 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
345 MODULE_DESCRIPTION("RIPEMD-256 Message Digest");
346 MODULE_ALIAS_CRYPTO("rmd256");
347