vfio/pci: Pull BAR mapping setup from read-write path
[linux/fpc-iii.git] / crypto / rmd256.c
blobf50c025cc9623a65abdbce4d494db5ddb20f02aa
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;
233 static int rmd256_init(struct shash_desc *desc)
235 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
237 rctx->byte_count = 0;
239 rctx->state[0] = RMD_H0;
240 rctx->state[1] = RMD_H1;
241 rctx->state[2] = RMD_H2;
242 rctx->state[3] = RMD_H3;
243 rctx->state[4] = RMD_H5;
244 rctx->state[5] = RMD_H6;
245 rctx->state[6] = RMD_H7;
246 rctx->state[7] = RMD_H8;
248 memset(rctx->buffer, 0, sizeof(rctx->buffer));
250 return 0;
253 static int rmd256_update(struct shash_desc *desc, const u8 *data,
254 unsigned int len)
256 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
257 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
259 rctx->byte_count += len;
261 /* Enough space in buffer? If so copy and we're done */
262 if (avail > len) {
263 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
264 data, len);
265 goto out;
268 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
269 data, avail);
271 rmd256_transform(rctx->state, rctx->buffer);
272 data += avail;
273 len -= avail;
275 while (len >= sizeof(rctx->buffer)) {
276 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
277 rmd256_transform(rctx->state, rctx->buffer);
278 data += sizeof(rctx->buffer);
279 len -= sizeof(rctx->buffer);
282 memcpy(rctx->buffer, data, len);
284 out:
285 return 0;
288 /* Add padding and return the message digest. */
289 static int rmd256_final(struct shash_desc *desc, u8 *out)
291 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
292 u32 i, index, padlen;
293 __le64 bits;
294 __le32 *dst = (__le32 *)out;
295 static const u8 padding[64] = { 0x80, };
297 bits = cpu_to_le64(rctx->byte_count << 3);
299 /* Pad out to 56 mod 64 */
300 index = rctx->byte_count & 0x3f;
301 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
302 rmd256_update(desc, padding, padlen);
304 /* Append length */
305 rmd256_update(desc, (const u8 *)&bits, sizeof(bits));
307 /* Store state in digest */
308 for (i = 0; i < 8; i++)
309 dst[i] = cpu_to_le32p(&rctx->state[i]);
311 /* Wipe context */
312 memset(rctx, 0, sizeof(*rctx));
314 return 0;
317 static struct shash_alg alg = {
318 .digestsize = RMD256_DIGEST_SIZE,
319 .init = rmd256_init,
320 .update = rmd256_update,
321 .final = rmd256_final,
322 .descsize = sizeof(struct rmd256_ctx),
323 .base = {
324 .cra_name = "rmd256",
325 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
326 .cra_blocksize = RMD256_BLOCK_SIZE,
327 .cra_module = THIS_MODULE,
331 static int __init rmd256_mod_init(void)
333 return crypto_register_shash(&alg);
336 static void __exit rmd256_mod_fini(void)
338 crypto_unregister_shash(&alg);
341 module_init(rmd256_mod_init);
342 module_exit(rmd256_mod_fini);
344 MODULE_LICENSE("GPL");
345 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
346 MODULE_DESCRIPTION("RIPEMD-256 Message Digest");
347 MODULE_ALIAS_CRYPTO("rmd256");