xref: /linux/crypto/rmd160.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 /*
2  * Cryptographic API.
3  *
4  * RIPEMD-160 - 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 rmd160_ctx {
26 	u64 byte_count;
27 	u32 state[5];
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)		/* XOR */
43 #define F2(x, y, z) (z ^ (x & (y ^ z)))	/* x ? y : z */
44 #define F3(x, y, z) ((x | ~y) ^ z)
45 #define F4(x, y, z) (y ^ (z & (x ^ 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 
54 static void rmd160_transform(u32 *state, const __le32 *in)
55 {
56 	u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
57 
58 	/* Initialize left lane */
59 	aa = state[0];
60 	bb = state[1];
61 	cc = state[2];
62 	dd = state[3];
63 	ee = state[4];
64 
65 	/* Initialize right lane */
66 	aaa = state[0];
67 	bbb = state[1];
68 	ccc = state[2];
69 	ddd = state[3];
70 	eee = state[4];
71 
72 	/* round 1: left lane */
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 	/* round 2: left lane" */
91 	ROUND(ee, aa, bb, cc, dd, F2, K2, in[7],   7);
92 	ROUND(dd, ee, aa, bb, cc, F2, K2, in[4],   6);
93 	ROUND(cc, dd, ee, aa, bb, F2, K2, in[13],  8);
94 	ROUND(bb, cc, dd, ee, aa, F2, K2, in[1],  13);
95 	ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
96 	ROUND(ee, aa, bb, cc, dd, F2, K2, in[6],   9);
97 	ROUND(dd, ee, aa, bb, cc, F2, K2, in[15],  7);
98 	ROUND(cc, dd, ee, aa, bb, F2, K2, in[3],  15);
99 	ROUND(bb, cc, dd, ee, aa, F2, K2, in[12],  7);
100 	ROUND(aa, bb, cc, dd, ee, F2, K2, in[0],  12);
101 	ROUND(ee, aa, bb, cc, dd, F2, K2, in[9],  15);
102 	ROUND(dd, ee, aa, bb, cc, F2, K2, in[5],   9);
103 	ROUND(cc, dd, ee, aa, bb, F2, K2, in[2],  11);
104 	ROUND(bb, cc, dd, ee, aa, F2, K2, in[14],  7);
105 	ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
106 	ROUND(ee, aa, bb, cc, dd, F2, K2, in[8],  12);
107 
108 	/* round 3: left lane" */
109 	ROUND(dd, ee, aa, bb, cc, F3, K3, in[3],  11);
110 	ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
111 	ROUND(bb, cc, dd, ee, aa, F3, K3, in[14],  6);
112 	ROUND(aa, bb, cc, dd, ee, F3, K3, in[4],   7);
113 	ROUND(ee, aa, bb, cc, dd, F3, K3, in[9],  14);
114 	ROUND(dd, ee, aa, bb, cc, F3, K3, in[15],  9);
115 	ROUND(cc, dd, ee, aa, bb, F3, K3, in[8],  13);
116 	ROUND(bb, cc, dd, ee, aa, F3, K3, in[1],  15);
117 	ROUND(aa, bb, cc, dd, ee, F3, K3, in[2],  14);
118 	ROUND(ee, aa, bb, cc, dd, F3, K3, in[7],   8);
119 	ROUND(dd, ee, aa, bb, cc, F3, K3, in[0],  13);
120 	ROUND(cc, dd, ee, aa, bb, F3, K3, in[6],   6);
121 	ROUND(bb, cc, dd, ee, aa, F3, K3, in[13],  5);
122 	ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
123 	ROUND(ee, aa, bb, cc, dd, F3, K3, in[5],   7);
124 	ROUND(dd, ee, aa, bb, cc, F3, K3, in[12],  5);
125 
126 	/* round 4: left lane" */
127 	ROUND(cc, dd, ee, aa, bb, F4, K4, in[1],  11);
128 	ROUND(bb, cc, dd, ee, aa, F4, K4, in[9],  12);
129 	ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
130 	ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
131 	ROUND(dd, ee, aa, bb, cc, F4, K4, in[0],  14);
132 	ROUND(cc, dd, ee, aa, bb, F4, K4, in[8],  15);
133 	ROUND(bb, cc, dd, ee, aa, F4, K4, in[12],  9);
134 	ROUND(aa, bb, cc, dd, ee, F4, K4, in[4],   8);
135 	ROUND(ee, aa, bb, cc, dd, F4, K4, in[13],  9);
136 	ROUND(dd, ee, aa, bb, cc, F4, K4, in[3],  14);
137 	ROUND(cc, dd, ee, aa, bb, F4, K4, in[7],   5);
138 	ROUND(bb, cc, dd, ee, aa, F4, K4, in[15],  6);
139 	ROUND(aa, bb, cc, dd, ee, F4, K4, in[14],  8);
140 	ROUND(ee, aa, bb, cc, dd, F4, K4, in[5],   6);
141 	ROUND(dd, ee, aa, bb, cc, F4, K4, in[6],   5);
142 	ROUND(cc, dd, ee, aa, bb, F4, K4, in[2],  12);
143 
144 	/* round 5: left lane" */
145 	ROUND(bb, cc, dd, ee, aa, F5, K5, in[4],   9);
146 	ROUND(aa, bb, cc, dd, ee, F5, K5, in[0],  15);
147 	ROUND(ee, aa, bb, cc, dd, F5, K5, in[5],   5);
148 	ROUND(dd, ee, aa, bb, cc, F5, K5, in[9],  11);
149 	ROUND(cc, dd, ee, aa, bb, F5, K5, in[7],   6);
150 	ROUND(bb, cc, dd, ee, aa, F5, K5, in[12],  8);
151 	ROUND(aa, bb, cc, dd, ee, F5, K5, in[2],  13);
152 	ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
153 	ROUND(dd, ee, aa, bb, cc, F5, K5, in[14],  5);
154 	ROUND(cc, dd, ee, aa, bb, F5, K5, in[1],  12);
155 	ROUND(bb, cc, dd, ee, aa, F5, K5, in[3],  13);
156 	ROUND(aa, bb, cc, dd, ee, F5, K5, in[8],  14);
157 	ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
158 	ROUND(dd, ee, aa, bb, cc, F5, K5, in[6],   8);
159 	ROUND(cc, dd, ee, aa, bb, F5, K5, in[15],  5);
160 	ROUND(bb, cc, dd, ee, aa, F5, K5, in[13],  6);
161 
162 	/* round 1: right lane */
163 	ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5],   8);
164 	ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14],  9);
165 	ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7],   9);
166 	ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0],  11);
167 	ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9],  13);
168 	ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2],  15);
169 	ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
170 	ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4],   5);
171 	ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13],  7);
172 	ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6],   7);
173 	ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15],  8);
174 	ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8],  11);
175 	ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1],  14);
176 	ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
177 	ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3],  12);
178 	ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12],  6);
179 
180 	/* round 2: right lane */
181 	ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6],   9);
182 	ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
183 	ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3],  15);
184 	ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7],   7);
185 	ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0],  12);
186 	ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13],  8);
187 	ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5],   9);
188 	ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
189 	ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14],  7);
190 	ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15],  7);
191 	ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8],  12);
192 	ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12],  7);
193 	ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4],   6);
194 	ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9],  15);
195 	ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1],  13);
196 	ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2],  11);
197 
198 	/* round 3: right lane */
199 	ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15],  9);
200 	ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5],   7);
201 	ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1],  15);
202 	ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3],  11);
203 	ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7],   8);
204 	ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14],  6);
205 	ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6],   6);
206 	ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9],  14);
207 	ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
208 	ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8],  13);
209 	ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12],  5);
210 	ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2],  14);
211 	ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
212 	ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0],  13);
213 	ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4],   7);
214 	ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13],  5);
215 
216 	/* round 4: right lane */
217 	ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8],  15);
218 	ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6],   5);
219 	ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4],   8);
220 	ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1],  11);
221 	ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3],  14);
222 	ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
223 	ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15],  6);
224 	ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0],  14);
225 	ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5],   6);
226 	ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12],  9);
227 	ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2],  12);
228 	ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13],  9);
229 	ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9],  12);
230 	ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7],   5);
231 	ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
232 	ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14],  8);
233 
234 	/* round 5: right lane */
235 	ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12],  8);
236 	ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15],  5);
237 	ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
238 	ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4],   9);
239 	ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1],  12);
240 	ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5],   5);
241 	ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8],  14);
242 	ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7],   6);
243 	ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6],   8);
244 	ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2],  13);
245 	ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13],  6);
246 	ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14],  5);
247 	ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0],  15);
248 	ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3],  13);
249 	ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9],  11);
250 	ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
251 
252 	/* combine results */
253 	ddd += cc + state[1];		/* final result for state[0] */
254 	state[1] = state[2] + dd + eee;
255 	state[2] = state[3] + ee + aaa;
256 	state[3] = state[4] + aa + bbb;
257 	state[4] = state[0] + bb + ccc;
258 	state[0] = ddd;
259 }
260 
261 static int rmd160_init(struct shash_desc *desc)
262 {
263 	struct rmd160_ctx *rctx = shash_desc_ctx(desc);
264 
265 	rctx->byte_count = 0;
266 
267 	rctx->state[0] = RMD_H0;
268 	rctx->state[1] = RMD_H1;
269 	rctx->state[2] = RMD_H2;
270 	rctx->state[3] = RMD_H3;
271 	rctx->state[4] = RMD_H4;
272 
273 	memset(rctx->buffer, 0, sizeof(rctx->buffer));
274 
275 	return 0;
276 }
277 
278 static int rmd160_update(struct shash_desc *desc, const u8 *data,
279 			 unsigned int len)
280 {
281 	struct rmd160_ctx *rctx = shash_desc_ctx(desc);
282 	const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
283 
284 	rctx->byte_count += len;
285 
286 	/* Enough space in buffer? If so copy and we're done */
287 	if (avail > len) {
288 		memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
289 		       data, len);
290 		goto out;
291 	}
292 
293 	memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
294 	       data, avail);
295 
296 	rmd160_transform(rctx->state, rctx->buffer);
297 	data += avail;
298 	len -= avail;
299 
300 	while (len >= sizeof(rctx->buffer)) {
301 		memcpy(rctx->buffer, data, sizeof(rctx->buffer));
302 		rmd160_transform(rctx->state, rctx->buffer);
303 		data += sizeof(rctx->buffer);
304 		len -= sizeof(rctx->buffer);
305 	}
306 
307 	memcpy(rctx->buffer, data, len);
308 
309 out:
310 	return 0;
311 }
312 
313 /* Add padding and return the message digest. */
314 static int rmd160_final(struct shash_desc *desc, u8 *out)
315 {
316 	struct rmd160_ctx *rctx = shash_desc_ctx(desc);
317 	u32 i, index, padlen;
318 	__le64 bits;
319 	__le32 *dst = (__le32 *)out;
320 	static const u8 padding[64] = { 0x80, };
321 
322 	bits = cpu_to_le64(rctx->byte_count << 3);
323 
324 	/* Pad out to 56 mod 64 */
325 	index = rctx->byte_count & 0x3f;
326 	padlen = (index < 56) ? (56 - index) : ((64+56) - index);
327 	rmd160_update(desc, padding, padlen);
328 
329 	/* Append length */
330 	rmd160_update(desc, (const u8 *)&bits, sizeof(bits));
331 
332 	/* Store state in digest */
333 	for (i = 0; i < 5; i++)
334 		dst[i] = cpu_to_le32p(&rctx->state[i]);
335 
336 	/* Wipe context */
337 	memset(rctx, 0, sizeof(*rctx));
338 
339 	return 0;
340 }
341 
342 static struct shash_alg alg = {
343 	.digestsize	=	RMD160_DIGEST_SIZE,
344 	.init		=	rmd160_init,
345 	.update		=	rmd160_update,
346 	.final		=	rmd160_final,
347 	.descsize	=	sizeof(struct rmd160_ctx),
348 	.base		=	{
349 		.cra_name	 =	"rmd160",
350 		.cra_blocksize	 =	RMD160_BLOCK_SIZE,
351 		.cra_module	 =	THIS_MODULE,
352 	}
353 };
354 
355 static int __init rmd160_mod_init(void)
356 {
357 	return crypto_register_shash(&alg);
358 }
359 
360 static void __exit rmd160_mod_fini(void)
361 {
362 	crypto_unregister_shash(&alg);
363 }
364 
365 module_init(rmd160_mod_init);
366 module_exit(rmd160_mod_fini);
367 
368 MODULE_LICENSE("GPL");
369 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
370 MODULE_DESCRIPTION("RIPEMD-160 Message Digest");
371 MODULE_ALIAS_CRYPTO("rmd160");
372