Merge branch 'battery' into release
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / crypto / rmd128.c
blob5de6fa2a76fbe489f4868341ce595971733e3a91
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 <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>
24 #include "ripemd.h"
26 struct rmd128_ctx {
27 u64 byte_count;
28 u32 state[4];
29 __le32 buffer[16];
32 #define K1 RMD_K1
33 #define K2 RMD_K2
34 #define K3 RMD_K3
35 #define K4 RMD_K4
36 #define KK1 RMD_K6
37 #define KK2 RMD_K7
38 #define KK3 RMD_K8
39 #define KK4 RMD_K1
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 */
46 #define ROUND(a, b, c, d, f, k, x, s) { \
47 (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
48 (a) = rol32((a), (s)); \
51 static void rmd128_transform(u32 *state, const __le32 *in)
53 u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
55 /* Initialize left lane */
56 aa = state[0];
57 bb = state[1];
58 cc = state[2];
59 dd = state[3];
61 /* Initialize right lane */
62 aaa = state[0];
63 bbb = state[1];
64 ccc = state[2];
65 ddd = state[3];
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);
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);
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);
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);
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);
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);
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);
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);
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;
218 return;
221 static void rmd128_init(struct crypto_tfm *tfm)
223 struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
225 rctx->byte_count = 0;
227 rctx->state[0] = RMD_H0;
228 rctx->state[1] = RMD_H1;
229 rctx->state[2] = RMD_H2;
230 rctx->state[3] = RMD_H3;
232 memset(rctx->buffer, 0, sizeof(rctx->buffer));
235 static void rmd128_update(struct crypto_tfm *tfm, const u8 *data,
236 unsigned int len)
238 struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
239 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
241 rctx->byte_count += len;
243 /* Enough space in buffer? If so copy and we're done */
244 if (avail > len) {
245 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
246 data, len);
247 return;
250 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
251 data, avail);
253 rmd128_transform(rctx->state, rctx->buffer);
254 data += avail;
255 len -= avail;
257 while (len >= sizeof(rctx->buffer)) {
258 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
259 rmd128_transform(rctx->state, rctx->buffer);
260 data += sizeof(rctx->buffer);
261 len -= sizeof(rctx->buffer);
264 memcpy(rctx->buffer, data, len);
267 /* Add padding and return the message digest. */
268 static void rmd128_final(struct crypto_tfm *tfm, u8 *out)
270 struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
271 u32 i, index, padlen;
272 __le64 bits;
273 __le32 *dst = (__le32 *)out;
274 static const u8 padding[64] = { 0x80, };
276 bits = cpu_to_le64(rctx->byte_count << 3);
278 /* Pad out to 56 mod 64 */
279 index = rctx->byte_count & 0x3f;
280 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
281 rmd128_update(tfm, padding, padlen);
283 /* Append length */
284 rmd128_update(tfm, (const u8 *)&bits, sizeof(bits));
286 /* Store state in digest */
287 for (i = 0; i < 4; i++)
288 dst[i] = cpu_to_le32p(&rctx->state[i]);
290 /* Wipe context */
291 memset(rctx, 0, sizeof(*rctx));
294 static struct crypto_alg alg = {
295 .cra_name = "rmd128",
296 .cra_driver_name = "rmd128",
297 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
298 .cra_blocksize = RMD128_BLOCK_SIZE,
299 .cra_ctxsize = sizeof(struct rmd128_ctx),
300 .cra_module = THIS_MODULE,
301 .cra_list = LIST_HEAD_INIT(alg.cra_list),
302 .cra_u = { .digest = {
303 .dia_digestsize = RMD128_DIGEST_SIZE,
304 .dia_init = rmd128_init,
305 .dia_update = rmd128_update,
306 .dia_final = rmd128_final } }
309 static int __init rmd128_mod_init(void)
311 return crypto_register_alg(&alg);
314 static void __exit rmd128_mod_fini(void)
316 crypto_unregister_alg(&alg);
319 module_init(rmd128_mod_init);
320 module_exit(rmd128_mod_fini);
322 MODULE_LICENSE("GPL");
323 MODULE_DESCRIPTION("RIPEMD-128 Message Digest");
325 MODULE_ALIAS("rmd128");