Add linux-next specific files for 20110728
[linux-2.6/next.git] / crypto / rmd256.c
blob69293d9b56e0c4dca0ee4e739386ceb81e1a9173
1 /*
2 * Cryptographic API.
4 * RIPEMD-256 - RACE Integrity Primitives Evaluation Message Digest.
6 * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
8 * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
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.
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>
23 #include "ripemd.h"
25 struct rmd256_ctx {
26 u64 byte_count;
27 u32 state[8];
28 __le32 buffer[16];
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
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 */
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)); \
50 static void rmd256_transform(u32 *state, const __le32 *in)
52 u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd, tmp;
54 /* Initialize left lane */
55 aa = state[0];
56 bb = state[1];
57 cc = state[2];
58 dd = state[3];
60 /* Initialize right lane */
61 aaa = state[4];
62 bbb = state[5];
63 ccc = state[6];
64 ddd = state[7];
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);
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);
102 /* Swap contents of "a" registers */
103 tmp = aa; aa = aaa; aaa = tmp;
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);
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);
141 /* Swap contents of "b" registers */
142 tmp = bb; bb = bbb; bbb = tmp;
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);
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);
180 /* Swap contents of "c" registers */
181 tmp = cc; cc = ccc; ccc = tmp;
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);
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);
219 /* Swap contents of "d" registers */
220 tmp = dd; dd = ddd; ddd = tmp;
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;
232 return;
235 static int rmd256_init(struct shash_desc *desc)
237 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
239 rctx->byte_count = 0;
241 rctx->state[0] = RMD_H0;
242 rctx->state[1] = RMD_H1;
243 rctx->state[2] = RMD_H2;
244 rctx->state[3] = RMD_H3;
245 rctx->state[4] = RMD_H5;
246 rctx->state[5] = RMD_H6;
247 rctx->state[6] = RMD_H7;
248 rctx->state[7] = RMD_H8;
250 memset(rctx->buffer, 0, sizeof(rctx->buffer));
252 return 0;
255 static int rmd256_update(struct shash_desc *desc, const u8 *data,
256 unsigned int len)
258 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
259 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
261 rctx->byte_count += len;
263 /* Enough space in buffer? If so copy and we're done */
264 if (avail > len) {
265 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
266 data, len);
267 goto out;
270 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
271 data, avail);
273 rmd256_transform(rctx->state, rctx->buffer);
274 data += avail;
275 len -= avail;
277 while (len >= sizeof(rctx->buffer)) {
278 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
279 rmd256_transform(rctx->state, rctx->buffer);
280 data += sizeof(rctx->buffer);
281 len -= sizeof(rctx->buffer);
284 memcpy(rctx->buffer, data, len);
286 out:
287 return 0;
290 /* Add padding and return the message digest. */
291 static int rmd256_final(struct shash_desc *desc, u8 *out)
293 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
294 u32 i, index, padlen;
295 __le64 bits;
296 __le32 *dst = (__le32 *)out;
297 static const u8 padding[64] = { 0x80, };
299 bits = cpu_to_le64(rctx->byte_count << 3);
301 /* Pad out to 56 mod 64 */
302 index = rctx->byte_count & 0x3f;
303 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
304 rmd256_update(desc, padding, padlen);
306 /* Append length */
307 rmd256_update(desc, (const u8 *)&bits, sizeof(bits));
309 /* Store state in digest */
310 for (i = 0; i < 8; i++)
311 dst[i] = cpu_to_le32p(&rctx->state[i]);
313 /* Wipe context */
314 memset(rctx, 0, sizeof(*rctx));
316 return 0;
319 static struct shash_alg alg = {
320 .digestsize = RMD256_DIGEST_SIZE,
321 .init = rmd256_init,
322 .update = rmd256_update,
323 .final = rmd256_final,
324 .descsize = sizeof(struct rmd256_ctx),
325 .base = {
326 .cra_name = "rmd256",
327 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
328 .cra_blocksize = RMD256_BLOCK_SIZE,
329 .cra_module = THIS_MODULE,
333 static int __init rmd256_mod_init(void)
335 return crypto_register_shash(&alg);
338 static void __exit rmd256_mod_fini(void)
340 crypto_unregister_shash(&alg);
343 module_init(rmd256_mod_init);
344 module_exit(rmd256_mod_fini);
346 MODULE_LICENSE("GPL");
347 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
348 MODULE_DESCRIPTION("RIPEMD-256 Message Digest");