nilfs2: fix potential hang in nilfs_error on errors=remount-ro
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / crypto / rmd320.c
blob86becaba2f05bbd714121242110b28338c5da3f2
1 /*
2 * Cryptographic API.
4 * RIPEMD-320 - RACE Integrity Primitives Evaluation Message Digest.
6 * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
8 * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.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 rmd320_ctx {
26 u64 byte_count;
27 u32 state[10];
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 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
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))
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); \
54 static void rmd320_transform(u32 *state, const __le32 *in)
56 u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee, tmp;
58 /* Initialize left lane */
59 aa = state[0];
60 bb = state[1];
61 cc = state[2];
62 dd = state[3];
63 ee = state[4];
65 /* Initialize right lane */
66 aaa = state[5];
67 bbb = state[6];
68 ccc = state[7];
69 ddd = state[8];
70 eee = state[9];
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);
90 /* round 1: right lane */
91 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
92 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
93 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
94 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
95 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
96 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
97 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
98 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
99 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
100 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
101 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
102 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
103 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
104 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
105 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
106 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
108 /* Swap contents of "a" registers */
109 tmp = aa; aa = aaa; aaa = tmp;
111 /* round 2: left lane" */
112 ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
113 ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
114 ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
115 ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
116 ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
117 ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
118 ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
119 ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
120 ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
121 ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
122 ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
123 ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
124 ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
125 ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
126 ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
127 ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
129 /* round 2: right lane */
130 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
131 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
132 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
133 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
134 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
135 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
136 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
137 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
138 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
139 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
140 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
141 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
142 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
143 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
144 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
145 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
147 /* Swap contents of "b" registers */
148 tmp = bb; bb = bbb; bbb = tmp;
150 /* round 3: left lane" */
151 ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
152 ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
153 ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
154 ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
155 ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
156 ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
157 ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
158 ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
159 ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
160 ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
161 ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
162 ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
163 ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
164 ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
165 ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
166 ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
168 /* round 3: right lane */
169 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
170 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
171 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
172 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
173 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
174 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
175 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
176 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
177 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
178 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
179 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
180 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
181 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
182 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
183 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
184 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
186 /* Swap contents of "c" registers */
187 tmp = cc; cc = ccc; ccc = tmp;
189 /* round 4: left lane" */
190 ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
191 ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
192 ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
193 ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
194 ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
195 ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
196 ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
197 ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
198 ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
199 ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
200 ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
201 ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
202 ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
203 ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
204 ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
205 ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
207 /* round 4: right lane */
208 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
209 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
210 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
211 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
212 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
213 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
214 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
215 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
216 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
217 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
218 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
219 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
220 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
221 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
222 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
223 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
225 /* Swap contents of "d" registers */
226 tmp = dd; dd = ddd; ddd = tmp;
228 /* round 5: left lane" */
229 ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
230 ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
231 ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
232 ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
233 ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
234 ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
235 ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
236 ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
237 ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
238 ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
239 ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
240 ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
241 ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
242 ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
243 ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
244 ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
246 /* round 5: right lane */
247 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
248 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
249 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
250 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
251 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
252 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
253 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
254 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
255 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
256 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
257 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
258 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
259 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
260 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
261 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
262 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
264 /* Swap contents of "e" registers */
265 tmp = ee; ee = eee; eee = tmp;
267 /* combine results */
268 state[0] += aa;
269 state[1] += bb;
270 state[2] += cc;
271 state[3] += dd;
272 state[4] += ee;
273 state[5] += aaa;
274 state[6] += bbb;
275 state[7] += ccc;
276 state[8] += ddd;
277 state[9] += eee;
279 return;
282 static int rmd320_init(struct shash_desc *desc)
284 struct rmd320_ctx *rctx = shash_desc_ctx(desc);
286 rctx->byte_count = 0;
288 rctx->state[0] = RMD_H0;
289 rctx->state[1] = RMD_H1;
290 rctx->state[2] = RMD_H2;
291 rctx->state[3] = RMD_H3;
292 rctx->state[4] = RMD_H4;
293 rctx->state[5] = RMD_H5;
294 rctx->state[6] = RMD_H6;
295 rctx->state[7] = RMD_H7;
296 rctx->state[8] = RMD_H8;
297 rctx->state[9] = RMD_H9;
299 memset(rctx->buffer, 0, sizeof(rctx->buffer));
301 return 0;
304 static int rmd320_update(struct shash_desc *desc, const u8 *data,
305 unsigned int len)
307 struct rmd320_ctx *rctx = shash_desc_ctx(desc);
308 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
310 rctx->byte_count += len;
312 /* Enough space in buffer? If so copy and we're done */
313 if (avail > len) {
314 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
315 data, len);
316 goto out;
319 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
320 data, avail);
322 rmd320_transform(rctx->state, rctx->buffer);
323 data += avail;
324 len -= avail;
326 while (len >= sizeof(rctx->buffer)) {
327 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
328 rmd320_transform(rctx->state, rctx->buffer);
329 data += sizeof(rctx->buffer);
330 len -= sizeof(rctx->buffer);
333 memcpy(rctx->buffer, data, len);
335 out:
336 return 0;
339 /* Add padding and return the message digest. */
340 static int rmd320_final(struct shash_desc *desc, u8 *out)
342 struct rmd320_ctx *rctx = shash_desc_ctx(desc);
343 u32 i, index, padlen;
344 __le64 bits;
345 __le32 *dst = (__le32 *)out;
346 static const u8 padding[64] = { 0x80, };
348 bits = cpu_to_le64(rctx->byte_count << 3);
350 /* Pad out to 56 mod 64 */
351 index = rctx->byte_count & 0x3f;
352 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
353 rmd320_update(desc, padding, padlen);
355 /* Append length */
356 rmd320_update(desc, (const u8 *)&bits, sizeof(bits));
358 /* Store state in digest */
359 for (i = 0; i < 10; i++)
360 dst[i] = cpu_to_le32p(&rctx->state[i]);
362 /* Wipe context */
363 memset(rctx, 0, sizeof(*rctx));
365 return 0;
368 static struct shash_alg alg = {
369 .digestsize = RMD320_DIGEST_SIZE,
370 .init = rmd320_init,
371 .update = rmd320_update,
372 .final = rmd320_final,
373 .descsize = sizeof(struct rmd320_ctx),
374 .base = {
375 .cra_name = "rmd320",
376 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
377 .cra_blocksize = RMD320_BLOCK_SIZE,
378 .cra_module = THIS_MODULE,
382 static int __init rmd320_mod_init(void)
384 return crypto_register_shash(&alg);
387 static void __exit rmd320_mod_fini(void)
389 crypto_unregister_shash(&alg);
392 module_init(rmd320_mod_init);
393 module_exit(rmd320_mod_fini);
395 MODULE_LICENSE("GPL");
396 MODULE_DESCRIPTION("RIPEMD-320 Message Digest");