blob: f50c025cc9623a65abdbce4d494db5ddb20f02aa [file] [log] [blame]
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +08001/*
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 *
Adrian-Ken Rueegsegger3181c222011-01-04 15:35:51 +11008 * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +08009 *
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 */
Herbert Xud8a5e2e2008-11-08 09:58:10 +080016#include <crypto/internal/hash.h>
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080017#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/mm.h>
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080020#include <linux/types.h>
21#include <asm/byteorder.h>
22
23#include "ripemd.h"
24
25struct rmd256_ctx {
26 u64 byte_count;
27 u32 state[8];
Harvey Harrisoncaee1682008-07-04 19:48:58 +080028 __le32 buffer[16];
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080029};
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) { \
Herbert Xu5cdcc222008-06-02 21:30:38 +100046 (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080047 (a) = rol32((a), (s)); \
48}
49
Harvey Harrisoncaee1682008-07-04 19:48:58 +080050static void rmd256_transform(u32 *state, const __le32 *in)
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080051{
52 u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd, tmp;
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 tmp = aa; aa = aaa; aaa = tmp;
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 tmp = bb; bb = bbb; bbb = tmp;
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 tmp = cc; cc = ccc; ccc = tmp;
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 tmp = dd; dd = ddd; ddd = tmp;
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;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800231}
232
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800233static int rmd256_init(struct shash_desc *desc)
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800234{
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800235 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800236
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));
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800249
250 return 0;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800251}
252
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800253static int rmd256_update(struct shash_desc *desc, const u8 *data,
254 unsigned int len)
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800255{
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800256 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800257 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);
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800265 goto out;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800266 }
267
268 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
269 data, avail);
270
Adrian-Ken Rueegsegger2952ff12008-05-26 20:33:44 +1000271 rmd256_transform(rctx->state, rctx->buffer);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800272 data += avail;
273 len -= avail;
274
275 while (len >= sizeof(rctx->buffer)) {
276 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
Adrian-Ken Rueegsegger2952ff12008-05-26 20:33:44 +1000277 rmd256_transform(rctx->state, rctx->buffer);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800278 data += sizeof(rctx->buffer);
279 len -= sizeof(rctx->buffer);
280 }
281
282 memcpy(rctx->buffer, data, len);
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800283
284out:
285 return 0;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800286}
287
288/* Add padding and return the message digest. */
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800289static int rmd256_final(struct shash_desc *desc, u8 *out)
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800290{
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800291 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegsegger2952ff12008-05-26 20:33:44 +1000292 u32 i, index, padlen;
Harvey Harrisoncaee1682008-07-04 19:48:58 +0800293 __le64 bits;
294 __le32 *dst = (__le32 *)out;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800295 static const u8 padding[64] = { 0x80, };
Adrian-Ken Rueegsegger2952ff12008-05-26 20:33:44 +1000296
297 bits = cpu_to_le64(rctx->byte_count << 3);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800298
299 /* Pad out to 56 mod 64 */
300 index = rctx->byte_count & 0x3f;
301 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800302 rmd256_update(desc, padding, padlen);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800303
304 /* Append length */
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800305 rmd256_update(desc, (const u8 *)&bits, sizeof(bits));
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800306
307 /* Store state in digest */
Adrian-Ken Rueegsegger2952ff12008-05-26 20:33:44 +1000308 for (i = 0; i < 8; i++)
Herbert Xu5cdcc222008-06-02 21:30:38 +1000309 dst[i] = cpu_to_le32p(&rctx->state[i]);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800310
311 /* Wipe context */
312 memset(rctx, 0, sizeof(*rctx));
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800313
314 return 0;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800315}
316
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800317static 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_flags = CRYPTO_ALG_TYPE_SHASH,
326 .cra_blocksize = RMD256_BLOCK_SIZE,
327 .cra_module = THIS_MODULE,
328 }
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800329};
330
331static int __init rmd256_mod_init(void)
332{
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800333 return crypto_register_shash(&alg);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800334}
335
336static void __exit rmd256_mod_fini(void)
337{
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800338 crypto_unregister_shash(&alg);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800339}
340
341module_init(rmd256_mod_init);
342module_exit(rmd256_mod_fini);
343
344MODULE_LICENSE("GPL");
Adrian-Ken Rueegsegger3181c222011-01-04 15:35:51 +1100345MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800346MODULE_DESCRIPTION("RIPEMD-256 Message Digest");
Kees Cook5d26a102014-11-20 17:05:53 -0800347MODULE_ALIAS_CRYPTO("rmd256");