aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdrian-Ken Rueegsegger <rueegsegger@swiss-it.ch>2008-05-07 10:14:10 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2008-07-10 08:35:09 -0400
commitc6580eb8b17d64f0d6ad25c86a034adbda5ab4e1 (patch)
tree8b947271d9e80bbc18f7c988372fb6e2d041cceb
parent93aa7f8a12d1b229bcee12a1100a6df4945f5432 (diff)
[CRYPTO] ripemd: Add support for RIPEMD hash algorithms
This patch adds support for RIPEMD-128 and RIPEMD-160 hash algorithms. Signed-off-by: Adrian-Ken Rueegsegger <rueegsegger@swiss-it.ch> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--crypto/Makefile2
-rw-r--r--crypto/ripemd.h26
-rw-r--r--crypto/rmd128.c344
-rw-r--r--crypto/rmd160.c388
4 files changed, 760 insertions, 0 deletions
diff --git a/crypto/Makefile b/crypto/Makefile
index ca024418f4fb..1efb55635664 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -27,6 +27,8 @@ obj-$(CONFIG_CRYPTO_XCBC) += xcbc.o
27obj-$(CONFIG_CRYPTO_NULL) += crypto_null.o 27obj-$(CONFIG_CRYPTO_NULL) += crypto_null.o
28obj-$(CONFIG_CRYPTO_MD4) += md4.o 28obj-$(CONFIG_CRYPTO_MD4) += md4.o
29obj-$(CONFIG_CRYPTO_MD5) += md5.o 29obj-$(CONFIG_CRYPTO_MD5) += md5.o
30obj-$(CONFIG_CRYPTO_RMD128) += rmd128.o
31obj-$(CONFIG_CRYPTO_RMD160) += rmd160.o
30obj-$(CONFIG_CRYPTO_SHA1) += sha1_generic.o 32obj-$(CONFIG_CRYPTO_SHA1) += sha1_generic.o
31obj-$(CONFIG_CRYPTO_SHA256) += sha256_generic.o 33obj-$(CONFIG_CRYPTO_SHA256) += sha256_generic.o
32obj-$(CONFIG_CRYPTO_SHA512) += sha512_generic.o 34obj-$(CONFIG_CRYPTO_SHA512) += sha512_generic.o
diff --git a/crypto/ripemd.h b/crypto/ripemd.h
new file mode 100644
index 000000000000..2858e2266bd6
--- /dev/null
+++ b/crypto/ripemd.h
@@ -0,0 +1,26 @@
1/*
2 * Common values for RIPEMD algorithms
3 */
4
5#ifndef _CRYPTO_RMD_H
6#define _CRYPTO_RMD_H
7
8#define RMD128_DIGEST_SIZE 16
9#define RMD128_BLOCK_SIZE 64
10
11#define RMD160_DIGEST_SIZE 20
12#define RMD160_BLOCK_SIZE 64
13
14#define RMD256_DIGEST_SIZE 32
15#define RMD256_BLOCK_SIZE 64
16
17#define RMD320_DIGEST_SIZE 40
18#define RMD320_BLOCK_SIZE 64
19
20#define RMD_H0 0x67452301UL
21#define RMD_H1 0xefcdab89UL
22#define RMD_H2 0x98badcfeUL
23#define RMD_H3 0x10325476UL
24#define RMD_H4 0xc3d2e1f0UL
25
26#endif
diff --git a/crypto/rmd128.c b/crypto/rmd128.c
new file mode 100644
index 000000000000..22cc13be4033
--- /dev/null
+++ b/crypto/rmd128.c
@@ -0,0 +1,344 @@
1/*
2 * Cryptographic API.
3 *
4 * RIPEMD-128 - 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 rmd128_ctx {
27 u64 byte_count;
28 u32 state[4];
29 u32 buffer[16];
30};
31
32#define K1 0x00000000UL
33#define K2 0x5a827999UL
34#define K3 0x6ed9eba1UL
35#define K4 0x8f1bbcdcUL
36#define KK1 0x50a28be6UL
37#define KK2 0x5c4dd124UL
38#define KK3 0x6d703ef3UL
39#define KK4 0x00000000UL
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 rmd128_transform(u32 *state, u32 const *in)
52{
53 u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
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[0];
63 bbb = state[1];
64 ccc = state[2];
65 ddd = state[3];
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 2: left lane */
86 ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
87 ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
88 ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
89 ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
90 ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
91 ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
92 ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
93 ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
94 ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
95 ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
96 ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
97 ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
98 ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
99 ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
100 ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
101 ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
102
103 /* round 3: left lane */
104 ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
105 ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
106 ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
107 ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
108 ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
109 ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
110 ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
111 ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
112 ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
113 ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
114 ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
115 ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
116 ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
117 ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
118 ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
119 ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
120
121 /* round 4: left lane */
122 ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
123 ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
124 ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
125 ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
126 ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
127 ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
128 ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
129 ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
130 ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
131 ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
132 ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
133 ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
134 ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
135 ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
136 ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
137 ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
138
139 /* round 1: right lane */
140 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
141 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
142 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
143 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
144 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
145 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
146 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
147 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
148 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
149 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
150 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
151 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
152 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
153 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
154 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
155 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
156
157 /* round 2: right lane */
158 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
159 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
160 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
161 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
162 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
163 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
164 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
165 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
166 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
167 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
168 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
169 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
170 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
171 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
172 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
173 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
174
175 /* round 3: right lane */
176 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
177 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
178 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
179 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
180 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
181 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
182 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
183 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
184 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
185 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
186 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
187 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
188 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
189 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
190 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
191 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
192
193 /* round 4: right lane */
194 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
195 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
196 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
197 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
198 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
199 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
200 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
201 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
202 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
203 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
204 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
205 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
206 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
207 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
208 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
209 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
210
211 /* combine results */
212 ddd += cc + state[1]; /* final result for state[0] */
213 state[1] = state[2] + dd + aaa;
214 state[2] = state[3] + aa + bbb;
215 state[3] = state[0] + bb + ccc;
216 state[0] = ddd;
217
218 return;
219}
220
221static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
222{
223 while (words--) {
224 le32_to_cpus(buf);
225 buf++;
226 }
227}
228
229static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
230{
231 while (words--) {
232 cpu_to_le32s(buf);
233 buf++;
234 }
235}
236
237static inline void rmd128_transform_helper(struct rmd128_ctx *ctx)
238{
239 le32_to_cpu_array(ctx->buffer, sizeof(ctx->buffer) / sizeof(u32));
240 rmd128_transform(ctx->state, ctx->buffer);
241}
242
243static void rmd128_init(struct crypto_tfm *tfm)
244{
245 struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
246
247 rctx->byte_count = 0;
248
249 rctx->state[0] = RMD_H0;
250 rctx->state[1] = RMD_H1;
251 rctx->state[2] = RMD_H2;
252 rctx->state[3] = RMD_H3;
253
254 memset(rctx->buffer, 0, sizeof(rctx->buffer));
255}
256
257static void rmd128_update(struct crypto_tfm *tfm, const u8 *data,
258 unsigned int len)
259{
260 struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
261 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
262
263 rctx->byte_count += len;
264
265 /* Enough space in buffer? If so copy and we're done */
266 if (avail > len) {
267 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
268 data, len);
269 return;
270 }
271
272 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
273 data, avail);
274
275 rmd128_transform_helper(rctx);
276 data += avail;
277 len -= avail;
278
279 while (len >= sizeof(rctx->buffer)) {
280 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
281 rmd128_transform_helper(rctx);
282 data += sizeof(rctx->buffer);
283 len -= sizeof(rctx->buffer);
284 }
285
286 memcpy(rctx->buffer, data, len);
287}
288
289/* Add padding and return the message digest. */
290static void rmd128_final(struct crypto_tfm *tfm, u8 *out)
291{
292 struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
293 u32 index, padlen;
294 u64 bits;
295 static const u8 padding[64] = { 0x80, };
296 bits = rctx->byte_count << 3;
297
298 /* Pad out to 56 mod 64 */
299 index = rctx->byte_count & 0x3f;
300 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
301 rmd128_update(tfm, padding, padlen);
302
303 /* Append length */
304 rmd128_update(tfm, (const u8 *)&bits, sizeof(bits));
305
306 /* Store state in digest */
307 memcpy(out, rctx->state, sizeof(rctx->state));
308
309 /* Wipe context */
310 memset(rctx, 0, sizeof(*rctx));
311}
312
313static struct crypto_alg alg = {
314 .cra_name = "rmd128",
315 .cra_driver_name = "rmd128",
316 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
317 .cra_blocksize = RMD128_BLOCK_SIZE,
318 .cra_ctxsize = sizeof(struct rmd128_ctx),
319 .cra_module = THIS_MODULE,
320 .cra_list = LIST_HEAD_INIT(alg.cra_list),
321 .cra_u = { .digest = {
322 .dia_digestsize = RMD128_DIGEST_SIZE,
323 .dia_init = rmd128_init,
324 .dia_update = rmd128_update,
325 .dia_final = rmd128_final } }
326};
327
328static int __init rmd128_mod_init(void)
329{
330 return crypto_register_alg(&alg);
331}
332
333static void __exit rmd128_mod_fini(void)
334{
335 crypto_unregister_alg(&alg);
336}
337
338module_init(rmd128_mod_init);
339module_exit(rmd128_mod_fini);
340
341MODULE_LICENSE("GPL");
342MODULE_DESCRIPTION("RIPEMD-128 Message Digest");
343
344MODULE_ALIAS("rmd128");
diff --git a/crypto/rmd160.c b/crypto/rmd160.c
new file mode 100644
index 000000000000..a8a9d3d7046b
--- /dev/null
+++ b/crypto/rmd160.c
@@ -0,0 +1,388 @@
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 <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 rmd160_ctx {
27 u64 byte_count;
28 u32 state[5];
29 u32 buffer[16];
30};
31
32#define K1 0x00000000UL
33#define K2 0x5a827999UL
34#define K3 0x6ed9eba1UL
35#define K4 0x8f1bbcdcUL
36#define K5 0xa953fd4eUL
37#define KK1 0x50a28be6UL
38#define KK2 0x5c4dd124UL
39#define KK3 0x6d703ef3UL
40#define KK4 0x7a6d76e9UL
41#define KK5 0x00000000UL
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 rmd160_transform(u32 *state, u32 const *in)
56{
57 u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
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[0];
68 bbb = state[1];
69 ccc = state[2];
70 ddd = state[3];
71 eee = state[4];
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 2: left lane" */
92 ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
93 ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
94 ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
95 ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
96 ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
97 ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
98 ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
99 ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
100 ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
101 ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
102 ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
103 ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
104 ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
105 ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
106 ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
107 ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
108
109 /* round 3: left lane" */
110 ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
111 ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
112 ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
113 ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
114 ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
115 ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
116 ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
117 ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
118 ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
119 ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
120 ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
121 ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
122 ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
123 ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
124 ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
125 ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
126
127 /* round 4: left lane" */
128 ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
129 ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
130 ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
131 ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
132 ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
133 ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
134 ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
135 ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
136 ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
137 ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
138 ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
139 ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
140 ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
141 ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
142 ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
143 ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
144
145 /* round 5: left lane" */
146 ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
147 ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
148 ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
149 ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
150 ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
151 ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
152 ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
153 ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
154 ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
155 ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
156 ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
157 ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
158 ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
159 ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
160 ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
161 ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
162
163 /* round 1: right lane */
164 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
165 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
166 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
167 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
168 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
169 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
170 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
171 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
172 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
173 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
174 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
175 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
176 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
177 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
178 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
179 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
180
181 /* round 2: right lane */
182 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
183 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
184 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
185 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
186 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
187 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
188 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
189 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
190 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
191 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
192 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
193 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
194 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
195 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
196 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
197 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
198
199 /* round 3: right lane */
200 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
201 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
202 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
203 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
204 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
205 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
206 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
207 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
208 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
209 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
210 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
211 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
212 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
213 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
214 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
215 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
216
217 /* round 4: right lane */
218 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
219 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
220 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
221 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
222 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
223 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
224 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
225 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
226 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
227 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
228 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
229 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
230 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
231 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
232 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
233 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
234
235 /* round 5: right lane */
236 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
237 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
238 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
239 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
240 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
241 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
242 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
243 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
244 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
245 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
246 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
247 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
248 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
249 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
250 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
251 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
252
253 /* combine results */
254 ddd += cc + state[1]; /* final result for state[0] */
255 state[1] = state[2] + dd + eee;
256 state[2] = state[3] + ee + aaa;
257 state[3] = state[4] + aa + bbb;
258 state[4] = state[0] + bb + ccc;
259 state[0] = ddd;
260
261 return;
262}
263
264static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
265{
266 while (words--) {
267 le32_to_cpus(buf);
268 buf++;
269 }
270}
271
272static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
273{
274 while (words--) {
275 cpu_to_le32s(buf);
276 buf++;
277 }
278}
279
280static inline void rmd160_transform_helper(struct rmd160_ctx *ctx)
281{
282 le32_to_cpu_array(ctx->buffer, sizeof(ctx->buffer) / sizeof(u32));
283 rmd160_transform(ctx->state, ctx->buffer);
284}
285
286static void rmd160_init(struct crypto_tfm *tfm)
287{
288 struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
289
290 rctx->byte_count = 0;
291
292 rctx->state[0] = RMD_H0;
293 rctx->state[1] = RMD_H1;
294 rctx->state[2] = RMD_H2;
295 rctx->state[3] = RMD_H3;
296 rctx->state[4] = RMD_H4;
297
298 memset(rctx->buffer, 0, sizeof(rctx->buffer));
299}
300
301static void rmd160_update(struct crypto_tfm *tfm, const u8 *data,
302 unsigned int len)
303{
304 struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
305 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
306
307 rctx->byte_count += len;
308
309 /* Enough space in buffer? If so copy and we're done */
310 if (avail > len) {
311 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
312 data, len);
313 return;
314 }
315
316 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
317 data, avail);
318
319 rmd160_transform_helper(rctx);
320 data += avail;
321 len -= avail;
322
323 while (len >= sizeof(rctx->buffer)) {
324 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
325 rmd160_transform_helper(rctx);
326 data += sizeof(rctx->buffer);
327 len -= sizeof(rctx->buffer);
328 }
329
330 memcpy(rctx->buffer, data, len);
331}
332
333/* Add padding and return the message digest. */
334static void rmd160_final(struct crypto_tfm *tfm, u8 *out)
335{
336 struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
337 u32 index, padlen;
338 u64 bits;
339 static const u8 padding[64] = { 0x80, };
340 bits = rctx->byte_count << 3;
341
342 /* Pad out to 56 mod 64 */
343 index = rctx->byte_count & 0x3f;
344 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
345 rmd160_update(tfm, padding, padlen);
346
347 /* Append length */
348 rmd160_update(tfm, (const u8 *)&bits, sizeof(bits));
349
350 /* Store state in digest */
351 memcpy(out, rctx->state, sizeof(rctx->state));
352
353 /* Wipe context */
354 memset(rctx, 0, sizeof(*rctx));
355}
356
357static struct crypto_alg alg = {
358 .cra_name = "rmd160",
359 .cra_driver_name = "rmd160",
360 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
361 .cra_blocksize = RMD160_BLOCK_SIZE,
362 .cra_ctxsize = sizeof(struct rmd160_ctx),
363 .cra_module = THIS_MODULE,
364 .cra_list = LIST_HEAD_INIT(alg.cra_list),
365 .cra_u = { .digest = {
366 .dia_digestsize = RMD160_DIGEST_SIZE,
367 .dia_init = rmd160_init,
368 .dia_update = rmd160_update,
369 .dia_final = rmd160_final } }
370};
371
372static int __init rmd160_mod_init(void)
373{
374 return crypto_register_alg(&alg);
375}
376
377static void __exit rmd160_mod_fini(void)
378{
379 crypto_unregister_alg(&alg);
380}
381
382module_init(rmd160_mod_init);
383module_exit(rmd160_mod_fini);
384
385MODULE_LICENSE("GPL");
386MODULE_DESCRIPTION("RIPEMD-160 Message Digest");
387
388MODULE_ALIAS("rmd160");