staging: most: usb: fix usb_disconnect race condition
[linux/fpc-iii.git] / crypto / rmd128.c
blob40e053b97b697cfa4eeda4e7935b1932fde13156
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 <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 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;
218 static int rmd128_init(struct shash_desc *desc)
220 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
222 rctx->byte_count = 0;
224 rctx->state[0] = RMD_H0;
225 rctx->state[1] = RMD_H1;
226 rctx->state[2] = RMD_H2;
227 rctx->state[3] = RMD_H3;
229 memset(rctx->buffer, 0, sizeof(rctx->buffer));
231 return 0;
234 static int rmd128_update(struct shash_desc *desc, const u8 *data,
235 unsigned int len)
237 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
238 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
240 rctx->byte_count += len;
242 /* Enough space in buffer? If so copy and we're done */
243 if (avail > len) {
244 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
245 data, len);
246 goto out;
249 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
250 data, avail);
252 rmd128_transform(rctx->state, rctx->buffer);
253 data += avail;
254 len -= avail;
256 while (len >= sizeof(rctx->buffer)) {
257 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
258 rmd128_transform(rctx->state, rctx->buffer);
259 data += sizeof(rctx->buffer);
260 len -= sizeof(rctx->buffer);
263 memcpy(rctx->buffer, data, len);
265 out:
266 return 0;
269 /* Add padding and return the message digest. */
270 static int rmd128_final(struct shash_desc *desc, u8 *out)
272 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
273 u32 i, index, padlen;
274 __le64 bits;
275 __le32 *dst = (__le32 *)out;
276 static const u8 padding[64] = { 0x80, };
278 bits = cpu_to_le64(rctx->byte_count << 3);
280 /* Pad out to 56 mod 64 */
281 index = rctx->byte_count & 0x3f;
282 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
283 rmd128_update(desc, padding, padlen);
285 /* Append length */
286 rmd128_update(desc, (const u8 *)&bits, sizeof(bits));
288 /* Store state in digest */
289 for (i = 0; i < 4; i++)
290 dst[i] = cpu_to_le32p(&rctx->state[i]);
292 /* Wipe context */
293 memset(rctx, 0, sizeof(*rctx));
295 return 0;
298 static struct shash_alg alg = {
299 .digestsize = RMD128_DIGEST_SIZE,
300 .init = rmd128_init,
301 .update = rmd128_update,
302 .final = rmd128_final,
303 .descsize = sizeof(struct rmd128_ctx),
304 .base = {
305 .cra_name = "rmd128",
306 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
307 .cra_blocksize = RMD128_BLOCK_SIZE,
308 .cra_module = THIS_MODULE,
312 static int __init rmd128_mod_init(void)
314 return crypto_register_shash(&alg);
317 static void __exit rmd128_mod_fini(void)
319 crypto_unregister_shash(&alg);
322 module_init(rmd128_mod_init);
323 module_exit(rmd128_mod_fini);
325 MODULE_LICENSE("GPL");
326 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
327 MODULE_DESCRIPTION("RIPEMD-128 Message Digest");
328 MODULE_ALIAS_CRYPTO("rmd128");