OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / crypto / rmd160.c
blob525d7bb752cf6a7317d728c57dd3e5528bf7d6bb
1 /*
2 * Cryptographic API.
4 * RIPEMD-160 - 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 rmd160_ctx {
26 u64 byte_count;
27 u32 state[5];
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 rmd160_transform(u32 *state, const __le32 *in)
56 u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
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[0];
67 bbb = state[1];
68 ccc = state[2];
69 ddd = state[3];
70 eee = state[4];
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 2: left lane" */
91 ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
92 ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
93 ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
94 ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
95 ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
96 ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
97 ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
98 ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
99 ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
100 ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
101 ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
102 ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
103 ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
104 ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
105 ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
106 ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
108 /* round 3: left lane" */
109 ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
110 ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
111 ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
112 ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
113 ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
114 ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
115 ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
116 ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
117 ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
118 ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
119 ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
120 ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
121 ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
122 ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
123 ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
124 ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
126 /* round 4: left lane" */
127 ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
128 ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
129 ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
130 ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
131 ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
132 ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
133 ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
134 ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
135 ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
136 ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
137 ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
138 ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
139 ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
140 ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
141 ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
142 ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
144 /* round 5: left lane" */
145 ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
146 ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
147 ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
148 ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
149 ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
150 ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
151 ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
152 ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
153 ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
154 ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
155 ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
156 ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
157 ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
158 ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
159 ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
160 ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
162 /* round 1: right lane */
163 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
164 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
165 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
166 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
167 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
168 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
169 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
170 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
171 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
172 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
173 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
174 ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
175 ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
176 ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
177 ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
178 ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
180 /* round 2: right lane */
181 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
182 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
183 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
184 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
185 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
186 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
187 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
188 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
189 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
190 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
191 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
192 ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
193 ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
194 ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
195 ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
196 ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
198 /* round 3: right lane */
199 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
200 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
201 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
202 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
203 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
204 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
205 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
206 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
207 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
208 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
209 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
210 ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
211 ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
212 ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
213 ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
214 ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
216 /* round 4: right lane */
217 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
218 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
219 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
220 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
221 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
222 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
223 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
224 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
225 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
226 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
227 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
228 ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
229 ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
230 ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
231 ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
232 ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
234 /* round 5: right lane */
235 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
236 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
237 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
238 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
239 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
240 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
241 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
242 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
243 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
244 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
245 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
246 ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
247 ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
248 ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
249 ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
250 ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
252 /* combine results */
253 ddd += cc + state[1]; /* final result for state[0] */
254 state[1] = state[2] + dd + eee;
255 state[2] = state[3] + ee + aaa;
256 state[3] = state[4] + aa + bbb;
257 state[4] = state[0] + bb + ccc;
258 state[0] = ddd;
260 return;
263 static int rmd160_init(struct shash_desc *desc)
265 struct rmd160_ctx *rctx = shash_desc_ctx(desc);
267 rctx->byte_count = 0;
269 rctx->state[0] = RMD_H0;
270 rctx->state[1] = RMD_H1;
271 rctx->state[2] = RMD_H2;
272 rctx->state[3] = RMD_H3;
273 rctx->state[4] = RMD_H4;
275 memset(rctx->buffer, 0, sizeof(rctx->buffer));
277 return 0;
280 static int rmd160_update(struct shash_desc *desc, const u8 *data,
281 unsigned int len)
283 struct rmd160_ctx *rctx = shash_desc_ctx(desc);
284 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
286 rctx->byte_count += len;
288 /* Enough space in buffer? If so copy and we're done */
289 if (avail > len) {
290 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
291 data, len);
292 goto out;
295 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
296 data, avail);
298 rmd160_transform(rctx->state, rctx->buffer);
299 data += avail;
300 len -= avail;
302 while (len >= sizeof(rctx->buffer)) {
303 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
304 rmd160_transform(rctx->state, rctx->buffer);
305 data += sizeof(rctx->buffer);
306 len -= sizeof(rctx->buffer);
309 memcpy(rctx->buffer, data, len);
311 out:
312 return 0;
315 /* Add padding and return the message digest. */
316 static int rmd160_final(struct shash_desc *desc, u8 *out)
318 struct rmd160_ctx *rctx = shash_desc_ctx(desc);
319 u32 i, index, padlen;
320 __le64 bits;
321 __le32 *dst = (__le32 *)out;
322 static const u8 padding[64] = { 0x80, };
324 bits = cpu_to_le64(rctx->byte_count << 3);
326 /* Pad out to 56 mod 64 */
327 index = rctx->byte_count & 0x3f;
328 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
329 rmd160_update(desc, padding, padlen);
331 /* Append length */
332 rmd160_update(desc, (const u8 *)&bits, sizeof(bits));
334 /* Store state in digest */
335 for (i = 0; i < 5; i++)
336 dst[i] = cpu_to_le32p(&rctx->state[i]);
338 /* Wipe context */
339 memset(rctx, 0, sizeof(*rctx));
341 return 0;
344 static struct shash_alg alg = {
345 .digestsize = RMD160_DIGEST_SIZE,
346 .init = rmd160_init,
347 .update = rmd160_update,
348 .final = rmd160_final,
349 .descsize = sizeof(struct rmd160_ctx),
350 .base = {
351 .cra_name = "rmd160",
352 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
353 .cra_blocksize = RMD160_BLOCK_SIZE,
354 .cra_module = THIS_MODULE,
358 static int __init rmd160_mod_init(void)
360 return crypto_register_shash(&alg);
363 static void __exit rmd160_mod_fini(void)
365 crypto_unregister_shash(&alg);
368 module_init(rmd160_mod_init);
369 module_exit(rmd160_mod_fini);
371 MODULE_LICENSE("GPL");
372 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
373 MODULE_DESCRIPTION("RIPEMD-160 Message Digest");