code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / crypto / rmd128.c
blob1ceb6735aa5386f5507fbd0c9e5cc19a66feb7b5
1 /*
2 * Cryptographic API.
4 * RIPEMD-128 - 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 rmd128_ctx {
26 u64 byte_count;
27 u32 state[4];
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 rmd128_transform(u32 *state, const __le32 *in)
52 u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
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[0];
62 bbb = state[1];
63 ccc = state[2];
64 ddd = state[3];
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 2: left lane */
85 ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
86 ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
87 ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
88 ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
89 ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
90 ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
91 ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
92 ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
93 ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
94 ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
95 ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
96 ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
97 ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
98 ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
99 ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
100 ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
102 /* round 3: left lane */
103 ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
104 ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
105 ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
106 ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
107 ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
108 ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
109 ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
110 ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
111 ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
112 ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
113 ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
114 ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
115 ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
116 ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
117 ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
118 ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
120 /* round 4: left lane */
121 ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
122 ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
123 ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
124 ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
125 ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
126 ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
127 ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
128 ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
129 ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
130 ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
131 ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
132 ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
133 ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
134 ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
135 ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
136 ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
138 /* round 1: right lane */
139 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
140 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
141 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
142 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
143 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
144 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
145 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
146 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
147 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
148 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
149 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
150 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
151 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
152 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
153 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
154 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
156 /* round 2: right lane */
157 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
158 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
159 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
160 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
161 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
162 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
163 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
164 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
165 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
166 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
167 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
168 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
169 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
170 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
171 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
172 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
174 /* round 3: right lane */
175 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
176 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
177 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
178 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
179 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
180 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
181 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
182 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
183 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
184 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
185 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
186 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
187 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
188 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
189 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
190 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
192 /* round 4: right lane */
193 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
194 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
195 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
196 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
197 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
198 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
199 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
200 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
201 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
202 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
203 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
204 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
205 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
206 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
207 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
208 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
210 /* combine results */
211 ddd += cc + state[1]; /* final result for state[0] */
212 state[1] = state[2] + dd + aaa;
213 state[2] = state[3] + aa + bbb;
214 state[3] = state[0] + bb + ccc;
215 state[0] = ddd;
217 return;
220 static int rmd128_init(struct shash_desc *desc)
222 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
224 rctx->byte_count = 0;
226 rctx->state[0] = RMD_H0;
227 rctx->state[1] = RMD_H1;
228 rctx->state[2] = RMD_H2;
229 rctx->state[3] = RMD_H3;
231 memset(rctx->buffer, 0, sizeof(rctx->buffer));
233 return 0;
236 static int rmd128_update(struct shash_desc *desc, const u8 *data,
237 unsigned int len)
239 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
240 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
242 rctx->byte_count += len;
244 /* Enough space in buffer? If so copy and we're done */
245 if (avail > len) {
246 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
247 data, len);
248 goto out;
251 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
252 data, avail);
254 rmd128_transform(rctx->state, rctx->buffer);
255 data += avail;
256 len -= avail;
258 while (len >= sizeof(rctx->buffer)) {
259 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
260 rmd128_transform(rctx->state, rctx->buffer);
261 data += sizeof(rctx->buffer);
262 len -= sizeof(rctx->buffer);
265 memcpy(rctx->buffer, data, len);
267 out:
268 return 0;
271 /* Add padding and return the message digest. */
272 static int rmd128_final(struct shash_desc *desc, u8 *out)
274 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
275 u32 i, index, padlen;
276 __le64 bits;
277 __le32 *dst = (__le32 *)out;
278 static const u8 padding[64] = { 0x80, };
280 bits = cpu_to_le64(rctx->byte_count << 3);
282 /* Pad out to 56 mod 64 */
283 index = rctx->byte_count & 0x3f;
284 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
285 rmd128_update(desc, padding, padlen);
287 /* Append length */
288 rmd128_update(desc, (const u8 *)&bits, sizeof(bits));
290 /* Store state in digest */
291 for (i = 0; i < 4; i++)
292 dst[i] = cpu_to_le32p(&rctx->state[i]);
294 /* Wipe context */
295 memset(rctx, 0, sizeof(*rctx));
297 return 0;
300 static struct shash_alg alg = {
301 .digestsize = RMD128_DIGEST_SIZE,
302 .init = rmd128_init,
303 .update = rmd128_update,
304 .final = rmd128_final,
305 .descsize = sizeof(struct rmd128_ctx),
306 .base = {
307 .cra_name = "rmd128",
308 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
309 .cra_blocksize = RMD128_BLOCK_SIZE,
310 .cra_module = THIS_MODULE,
314 static int __init rmd128_mod_init(void)
316 return crypto_register_shash(&alg);
319 static void __exit rmd128_mod_fini(void)
321 crypto_unregister_shash(&alg);
324 module_init(rmd128_mod_init);
325 module_exit(rmd128_mod_fini);
327 MODULE_LICENSE("GPL");
328 MODULE_DESCRIPTION("RIPEMD-128 Message Digest");