aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorAdrian-Ken Rueegsegger <rueegsegger@swiss-it.ch>2008-05-09 09:27:02 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2008-07-10 08:35:13 -0400
commitc555c28d9da517579085a00fc80e725b0b5d9fce (patch)
treeee4047e939f8e866457c47bcd4fd0e180a73885d /crypto
parentba6b0398721524ec817f74ea3492b48fa79de52f (diff)
[CRYPTO] ripemd: Add support for RIPEMD-256 and RIPEMD-320
This patch adds support for the extended RIPEMD hash algorithms RIPEMD-256 and RIPEMD-320. Signed-off-by: Adrian-Ken Rueegsegger <rueegsegger@swiss-it.ch> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/Makefile2
-rw-r--r--crypto/rmd256.c363
-rw-r--r--crypto/rmd320.c412
3 files changed, 777 insertions, 0 deletions
diff --git a/crypto/Makefile b/crypto/Makefile
index 1efb55635664..807656b64e02 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -29,6 +29,8 @@ obj-$(CONFIG_CRYPTO_MD4) += md4.o
29obj-$(CONFIG_CRYPTO_MD5) += md5.o 29obj-$(CONFIG_CRYPTO_MD5) += md5.o
30obj-$(CONFIG_CRYPTO_RMD128) += rmd128.o 30obj-$(CONFIG_CRYPTO_RMD128) += rmd128.o
31obj-$(CONFIG_CRYPTO_RMD160) += rmd160.o 31obj-$(CONFIG_CRYPTO_RMD160) += rmd160.o
32obj-$(CONFIG_CRYPTO_RMD256) += rmd256.o
33obj-$(CONFIG_CRYPTO_RMD320) += rmd320.o
32obj-$(CONFIG_CRYPTO_SHA1) += sha1_generic.o 34obj-$(CONFIG_CRYPTO_SHA1) += sha1_generic.o
33obj-$(CONFIG_CRYPTO_SHA256) += sha256_generic.o 35obj-$(CONFIG_CRYPTO_SHA256) += sha256_generic.o
34obj-$(CONFIG_CRYPTO_SHA512) += sha512_generic.o 36obj-$(CONFIG_CRYPTO_SHA512) += sha512_generic.o
diff --git a/crypto/rmd256.c b/crypto/rmd256.c
new file mode 100644
index 000000000000..060ee81cb8ad
--- /dev/null
+++ b/crypto/rmd256.c
@@ -0,0 +1,363 @@
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 <rueegsegger (at) swiss-it.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 <linux/init.h>
17#include <linux/module.h>
18#include <linux/mm.h>
19#include <linux/crypto.h>
20#include <linux/cryptohash.h>
21#include <linux/types.h>
22#include <asm/byteorder.h>
23
24#include "ripemd.h"
25
26struct rmd256_ctx {
27 u64 byte_count;
28 u32 state[8];
29 u32 buffer[16];
30};
31
32#define K1 RMD_K1
33#define K2 RMD_K2
34#define K3 RMD_K3
35#define K4 RMD_K4
36#define KK1 RMD_K6
37#define KK2 RMD_K7
38#define KK3 RMD_K8
39#define KK4 RMD_K1
40
41#define F1(x, y, z) (x ^ y ^ z) /* XOR */
42#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
43#define F3(x, y, z) ((x | ~y) ^ z)
44#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
45
46#define ROUND(a, b, c, d, f, k, x, s) { \
47 (a) += f((b), (c), (d)) + (x) + (k); \
48 (a) = rol32((a), (s)); \
49}
50
51static void rmd256_transform(u32 *state, u32 const *in)
52{
53 u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd, tmp;
54
55 /* Initialize left lane */
56 aa = state[0];
57 bb = state[1];
58 cc = state[2];
59 dd = state[3];
60
61 /* Initialize right lane */
62 aaa = state[4];
63 bbb = state[5];
64 ccc = state[6];
65 ddd = state[7];
66
67 /* round 1: left lane */
68 ROUND(aa, bb, cc, dd, F1, K1, in[0], 11);
69 ROUND(dd, aa, bb, cc, F1, K1, in[1], 14);
70 ROUND(cc, dd, aa, bb, F1, K1, in[2], 15);
71 ROUND(bb, cc, dd, aa, F1, K1, in[3], 12);
72 ROUND(aa, bb, cc, dd, F1, K1, in[4], 5);
73 ROUND(dd, aa, bb, cc, F1, K1, in[5], 8);
74 ROUND(cc, dd, aa, bb, F1, K1, in[6], 7);
75 ROUND(bb, cc, dd, aa, F1, K1, in[7], 9);
76 ROUND(aa, bb, cc, dd, F1, K1, in[8], 11);
77 ROUND(dd, aa, bb, cc, F1, K1, in[9], 13);
78 ROUND(cc, dd, aa, bb, F1, K1, in[10], 14);
79 ROUND(bb, cc, dd, aa, F1, K1, in[11], 15);
80 ROUND(aa, bb, cc, dd, F1, K1, in[12], 6);
81 ROUND(dd, aa, bb, cc, F1, K1, in[13], 7);
82 ROUND(cc, dd, aa, bb, F1, K1, in[14], 9);
83 ROUND(bb, cc, dd, aa, F1, K1, in[15], 8);
84
85 /* round 1: right lane */
86 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
87 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
88 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
89 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
90 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
91 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
92 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
93 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
94 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
95 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
96 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
97 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
98 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
99 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
100 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
101 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
102
103 /* Swap contents of "a" registers */
104 tmp = aa; aa = aaa; aaa = tmp;
105
106 /* round 2: left lane */
107 ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
108 ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
109 ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
110 ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
111 ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
112 ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
113 ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
114 ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
115 ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
116 ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
117 ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
118 ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
119 ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
120 ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
121 ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
122 ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
123
124 /* round 2: right lane */
125 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
126 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
127 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
128 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
129 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
130 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
131 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
132 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
133 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
134 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
135 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
136 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
137 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
138 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
139 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
140 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
141
142 /* Swap contents of "b" registers */
143 tmp = bb; bb = bbb; bbb = tmp;
144
145 /* round 3: left lane */
146 ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
147 ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
148 ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
149 ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
150 ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
151 ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
152 ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
153 ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
154 ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
155 ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
156 ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
157 ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
158 ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
159 ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
160 ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
161 ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
162
163 /* round 3: right lane */
164 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
165 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
166 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
167 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
168 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
169 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
170 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
171 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
172 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
173 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
174 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
175 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
176 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
177 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
178 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
179 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
180
181 /* Swap contents of "c" registers */
182 tmp = cc; cc = ccc; ccc = tmp;
183
184 /* round 4: left lane */
185 ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
186 ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
187 ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
188 ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
189 ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
190 ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
191 ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
192 ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
193 ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
194 ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
195 ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
196 ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
197 ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
198 ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
199 ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
200 ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
201
202 /* round 4: right lane */
203 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
204 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
205 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
206 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
207 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
208 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
209 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
210 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
211 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
212 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
213 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
214 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
215 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
216 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
217 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
218 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
219
220 /* Swap contents of "d" registers */
221 tmp = dd; dd = ddd; ddd = tmp;
222
223 /* combine results */
224 state[0] += aa;
225 state[1] += bb;
226 state[2] += cc;
227 state[3] += dd;
228 state[4] += aaa;
229 state[5] += bbb;
230 state[6] += ccc;
231 state[7] += ddd;
232
233 return;
234}
235
236static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
237{
238 while (words--) {
239 le32_to_cpus(buf);
240 buf++;
241 }
242}
243
244static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
245{
246 while (words--) {
247 cpu_to_le32s(buf);
248 buf++;
249 }
250}
251
252static inline void rmd256_transform_helper(struct rmd256_ctx *ctx)
253{
254 le32_to_cpu_array(ctx->buffer, sizeof(ctx->buffer) / sizeof(u32));
255 rmd256_transform(ctx->state, ctx->buffer);
256}
257
258static void rmd256_init(struct crypto_tfm *tfm)
259{
260 struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
261
262 rctx->byte_count = 0;
263
264 rctx->state[0] = RMD_H0;
265 rctx->state[1] = RMD_H1;
266 rctx->state[2] = RMD_H2;
267 rctx->state[3] = RMD_H3;
268 rctx->state[4] = RMD_H5;
269 rctx->state[5] = RMD_H6;
270 rctx->state[6] = RMD_H7;
271 rctx->state[7] = RMD_H8;
272
273 memset(rctx->buffer, 0, sizeof(rctx->buffer));
274}
275
276static void rmd256_update(struct crypto_tfm *tfm, const u8 *data,
277 unsigned int len)
278{
279 struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
280 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
281
282 rctx->byte_count += len;
283
284 /* Enough space in buffer? If so copy and we're done */
285 if (avail > len) {
286 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
287 data, len);
288 return;
289 }
290
291 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
292 data, avail);
293
294 rmd256_transform_helper(rctx);
295 data += avail;
296 len -= avail;
297
298 while (len >= sizeof(rctx->buffer)) {
299 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
300 rmd256_transform_helper(rctx);
301 data += sizeof(rctx->buffer);
302 len -= sizeof(rctx->buffer);
303 }
304
305 memcpy(rctx->buffer, data, len);
306}
307
308/* Add padding and return the message digest. */
309static void rmd256_final(struct crypto_tfm *tfm, u8 *out)
310{
311 struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
312 u32 index, padlen;
313 u64 bits;
314 static const u8 padding[64] = { 0x80, };
315 bits = rctx->byte_count << 3;
316
317 /* Pad out to 56 mod 64 */
318 index = rctx->byte_count & 0x3f;
319 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
320 rmd256_update(tfm, padding, padlen);
321
322 /* Append length */
323 rmd256_update(tfm, (const u8 *)&bits, sizeof(bits));
324
325 /* Store state in digest */
326 memcpy(out, rctx->state, sizeof(rctx->state));
327
328 /* Wipe context */
329 memset(rctx, 0, sizeof(*rctx));
330}
331
332static struct crypto_alg alg = {
333 .cra_name = "rmd256",
334 .cra_driver_name = "rmd256",
335 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
336 .cra_blocksize = RMD256_BLOCK_SIZE,
337 .cra_ctxsize = sizeof(struct rmd256_ctx),
338 .cra_module = THIS_MODULE,
339 .cra_list = LIST_HEAD_INIT(alg.cra_list),
340 .cra_u = { .digest = {
341 .dia_digestsize = RMD256_DIGEST_SIZE,
342 .dia_init = rmd256_init,
343 .dia_update = rmd256_update,
344 .dia_final = rmd256_final } }
345};
346
347static int __init rmd256_mod_init(void)
348{
349 return crypto_register_alg(&alg);
350}
351
352static void __exit rmd256_mod_fini(void)
353{
354 crypto_unregister_alg(&alg);
355}
356
357module_init(rmd256_mod_init);
358module_exit(rmd256_mod_fini);
359
360MODULE_LICENSE("GPL");
361MODULE_DESCRIPTION("RIPEMD-256 Message Digest");
362
363MODULE_ALIAS("rmd256");
diff --git a/crypto/rmd320.c b/crypto/rmd320.c
new file mode 100644
index 000000000000..b39c0543a8fa
--- /dev/null
+++ b/crypto/rmd320.c
@@ -0,0 +1,412 @@
1/*
2 * Cryptographic API.
3 *
4 * RIPEMD-320 - 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 <rueegsegger (at) swiss-it.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 <linux/init.h>
17#include <linux/module.h>
18#include <linux/mm.h>
19#include <linux/crypto.h>
20#include <linux/cryptohash.h>
21#include <linux/types.h>
22#include <asm/byteorder.h>
23
24#include "ripemd.h"
25
26struct rmd320_ctx {
27 u64 byte_count;
28 u32 state[10];
29 u32 buffer[16];
30};
31
32#define K1 RMD_K1
33#define K2 RMD_K2
34#define K3 RMD_K3
35#define K4 RMD_K4
36#define K5 RMD_K5
37#define KK1 RMD_K6
38#define KK2 RMD_K7
39#define KK3 RMD_K8
40#define KK4 RMD_K9
41#define KK5 RMD_K1
42
43#define F1(x, y, z) (x ^ y ^ z) /* XOR */
44#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
45#define F3(x, y, z) ((x | ~y) ^ z)
46#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
47#define F5(x, y, z) (x ^ (y | ~z))
48
49#define ROUND(a, b, c, d, e, f, k, x, s) { \
50 (a) += f((b), (c), (d)) + (x) + (k); \
51 (a) = rol32((a), (s)) + (e); \
52 (c) = rol32((c), 10); \
53}
54
55static void rmd320_transform(u32 *state, u32 const *in)
56{
57 u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee, tmp;
58
59 /* Initialize left lane */
60 aa = state[0];
61 bb = state[1];
62 cc = state[2];
63 dd = state[3];
64 ee = state[4];
65
66 /* Initialize right lane */
67 aaa = state[5];
68 bbb = state[6];
69 ccc = state[7];
70 ddd = state[8];
71 eee = state[9];
72
73 /* round 1: left lane */
74 ROUND(aa, bb, cc, dd, ee, F1, K1, in[0], 11);
75 ROUND(ee, aa, bb, cc, dd, F1, K1, in[1], 14);
76 ROUND(dd, ee, aa, bb, cc, F1, K1, in[2], 15);
77 ROUND(cc, dd, ee, aa, bb, F1, K1, in[3], 12);
78 ROUND(bb, cc, dd, ee, aa, F1, K1, in[4], 5);
79 ROUND(aa, bb, cc, dd, ee, F1, K1, in[5], 8);
80 ROUND(ee, aa, bb, cc, dd, F1, K1, in[6], 7);
81 ROUND(dd, ee, aa, bb, cc, F1, K1, in[7], 9);
82 ROUND(cc, dd, ee, aa, bb, F1, K1, in[8], 11);
83 ROUND(bb, cc, dd, ee, aa, F1, K1, in[9], 13);
84 ROUND(aa, bb, cc, dd, ee, F1, K1, in[10], 14);
85 ROUND(ee, aa, bb, cc, dd, F1, K1, in[11], 15);
86 ROUND(dd, ee, aa, bb, cc, F1, K1, in[12], 6);
87 ROUND(cc, dd, ee, aa, bb, F1, K1, in[13], 7);
88 ROUND(bb, cc, dd, ee, aa, F1, K1, in[14], 9);
89 ROUND(aa, bb, cc, dd, ee, F1, K1, in[15], 8);
90
91 /* round 1: right lane */
92 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
93 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
94 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
95 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
96 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
97 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
98 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
99 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
100 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
101 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
102 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
103 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
104 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
105 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
106 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
107 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
108
109 /* Swap contents of "a" registers */
110 tmp = aa; aa = aaa; aaa = tmp;
111
112 /* round 2: left lane" */
113 ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
114 ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
115 ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
116 ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
117 ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
118 ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
119 ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
120 ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
121 ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
122 ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
123 ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
124 ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
125 ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
126 ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
127 ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
128 ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
129
130 /* round 2: right lane */
131 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
132 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
133 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
134 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
135 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
136 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
137 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
138 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
139 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
140 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
141 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
142 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
143 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
144 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
145 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
146 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
147
148 /* Swap contents of "b" registers */
149 tmp = bb; bb = bbb; bbb = tmp;
150
151 /* round 3: left lane" */
152 ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
153 ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
154 ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
155 ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
156 ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
157 ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
158 ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
159 ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
160 ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
161 ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
162 ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
163 ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
164 ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
165 ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
166 ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
167 ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
168
169 /* round 3: right lane */
170 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
171 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
172 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
173 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
174 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
175 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
176 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
177 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
178 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
179 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
180 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
181 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
182 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
183 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
184 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
185 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
186
187 /* Swap contents of "c" registers */
188 tmp = cc; cc = ccc; ccc = tmp;
189
190 /* round 4: left lane" */
191 ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
192 ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
193 ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
194 ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
195 ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
196 ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
197 ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
198 ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
199 ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
200 ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
201 ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
202 ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
203 ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
204 ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
205 ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
206 ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
207
208 /* round 4: right lane */
209 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
210 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
211 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
212 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
213 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
214 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
215 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
216 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
217 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
218 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
219 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
220 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
221 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
222 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
223 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
224 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
225
226 /* Swap contents of "d" registers */
227 tmp = dd; dd = ddd; ddd = tmp;
228
229 /* round 5: left lane" */
230 ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
231 ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
232 ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
233 ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
234 ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
235 ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
236 ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
237 ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
238 ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
239 ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
240 ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
241 ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
242 ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
243 ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
244 ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
245 ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
246
247 /* round 5: right lane */
248 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
249 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
250 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
251 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
252 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
253 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
254 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
255 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
256 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
257 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
258 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
259 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
260 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
261 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
262 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
263 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
264
265 /* Swap contents of "e" registers */
266 tmp = ee; ee = eee; eee = tmp;
267
268 /* combine results */
269 state[0] += aa;
270 state[1] += bb;
271 state[2] += cc;
272 state[3] += dd;
273 state[4] += ee;
274 state[5] += aaa;
275 state[6] += bbb;
276 state[7] += ccc;
277 state[8] += ddd;
278 state[9] += eee;
279
280 return;
281}
282
283static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
284{
285 while (words--) {
286 le32_to_cpus(buf);
287 buf++;
288 }
289}
290
291static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
292{
293 while (words--) {
294 cpu_to_le32s(buf);
295 buf++;
296 }
297}
298
299static inline void rmd320_transform_helper(struct rmd320_ctx *ctx)
300{
301 le32_to_cpu_array(ctx->buffer, sizeof(ctx->buffer) / sizeof(u32));
302 rmd320_transform(ctx->state, ctx->buffer);
303}
304
305static void rmd320_init(struct crypto_tfm *tfm)
306{
307 struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
308
309 rctx->byte_count = 0;
310
311 rctx->state[0] = RMD_H0;
312 rctx->state[1] = RMD_H1;
313 rctx->state[2] = RMD_H2;
314 rctx->state[3] = RMD_H3;
315 rctx->state[4] = RMD_H4;
316 rctx->state[5] = RMD_H5;
317 rctx->state[6] = RMD_H6;
318 rctx->state[7] = RMD_H7;
319 rctx->state[8] = RMD_H8;
320 rctx->state[9] = RMD_H9;
321
322 memset(rctx->buffer, 0, sizeof(rctx->buffer));
323}
324
325static void rmd320_update(struct crypto_tfm *tfm, const u8 *data,
326 unsigned int len)
327{
328 struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
329 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
330
331 rctx->byte_count += len;
332
333 /* Enough space in buffer? If so copy and we're done */
334 if (avail > len) {
335 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
336 data, len);
337 return;
338 }
339
340 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
341 data, avail);
342
343 rmd320_transform_helper(rctx);
344 data += avail;
345 len -= avail;
346
347 while (len >= sizeof(rctx->buffer)) {
348 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
349 rmd320_transform_helper(rctx);
350 data += sizeof(rctx->buffer);
351 len -= sizeof(rctx->buffer);
352 }
353
354 memcpy(rctx->buffer, data, len);
355}
356
357/* Add padding and return the message digest. */
358static void rmd320_final(struct crypto_tfm *tfm, u8 *out)
359{
360 struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
361 u32 index, padlen;
362 u64 bits;
363 static const u8 padding[64] = { 0x80, };
364 bits = rctx->byte_count << 3;
365
366 /* Pad out to 56 mod 64 */
367 index = rctx->byte_count & 0x3f;
368 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
369 rmd320_update(tfm, padding, padlen);
370
371 /* Append length */
372 rmd320_update(tfm, (const u8 *)&bits, sizeof(bits));
373
374 /* Store state in digest */
375 memcpy(out, rctx->state, sizeof(rctx->state));
376
377 /* Wipe context */
378 memset(rctx, 0, sizeof(*rctx));
379}
380
381static struct crypto_alg alg = {
382 .cra_name = "rmd320",
383 .cra_driver_name = "rmd320",
384 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
385 .cra_blocksize = RMD320_BLOCK_SIZE,
386 .cra_ctxsize = sizeof(struct rmd320_ctx),
387 .cra_module = THIS_MODULE,
388 .cra_list = LIST_HEAD_INIT(alg.cra_list),
389 .cra_u = { .digest = {
390 .dia_digestsize = RMD320_DIGEST_SIZE,
391 .dia_init = rmd320_init,
392 .dia_update = rmd320_update,
393 .dia_final = rmd320_final } }
394};
395
396static int __init rmd320_mod_init(void)
397{
398 return crypto_register_alg(&alg);
399}
400
401static void __exit rmd320_mod_fini(void)
402{
403 crypto_unregister_alg(&alg);
404}
405
406module_init(rmd320_mod_init);
407module_exit(rmd320_mod_fini);
408
409MODULE_LICENSE("GPL");
410MODULE_DESCRIPTION("RIPEMD-320 Message Digest");
411
412MODULE_ALIAS("rmd320");