math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / symm / rmd320.c
1 /* -*-c-*-
2 *
3 * The RIPEMD-320 message digest function
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <mLib/bits.h>
31
32 #include "ghash.h"
33 #include "ghash-def.h"
34 #include "hash.h"
35 #include "rmd320.h"
36
37 /*----- Main code ---------------------------------------------------------*/
38
39 /* --- @rmd320_compress@ --- *
40 *
41 * Arguments: @rmd320_ctx *ctx@ = pointer to context block
42 * @const void *sbuf@ = pointer to buffer of appropriate size
43 *
44 * Returns: ---
45 *
46 * Use: RIPEMD-320 compression function.
47 */
48
49 void rmd320_compress(rmd320_ctx *ctx, const void *sbuf)
50 {
51 uint32 a, b, c, d, e;
52 uint32 A, B, C, D, E;
53 uint32 buf[16];
54
55 /* --- Fetch the chaining variables --- */
56
57 a = ctx->a;
58 b = ctx->b;
59 c = ctx->c;
60 d = ctx->d;
61 e = ctx->e;
62
63 A = ctx->A;
64 B = ctx->B;
65 C = ctx->C;
66 D = ctx->D;
67 E = ctx->E;
68
69 /* --- Fetch the buffer contents --- */
70
71 {
72 int i;
73 const octet *p;
74
75 for (i = 0, p = sbuf; i < 16; i++, p += 4)
76 buf[i] = LOAD32_L(p);
77 }
78
79 /* --- Definitions for round functions --- */
80
81 #define F(x, y, z) ((x) ^ (y) ^ (z))
82 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
83 #define H(x, y, z) (((x) | ~(y)) ^ (z))
84 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
85 #define J(x, y, z) ((x) ^ ((y) | ~(z)))
86
87 #define T(v, w, x, y, z, i, r, f, k) do { \
88 uint32 _t = v + f(w, x, y) + buf[i] + k; \
89 v = ROL32(_t, r) + z; x = ROL32(x, 10); \
90 } while (0)
91
92 #define F1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, F, 0x00000000)
93 #define G1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, G, 0x5a827999)
94 #define H1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, H, 0x6ed9eba1)
95 #define I1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, I, 0x8f1bbcdc)
96 #define J1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, J, 0xa953fd4e)
97
98 #define F2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, J, 0x50a28be6)
99 #define G2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, I, 0x5c4dd124)
100 #define H2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, H, 0x6d703ef3)
101 #define I2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, G, 0x7a6d76e9)
102 #define J2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, F, 0x00000000)
103
104 /* --- We must do both lines together --- */
105
106 F1(a, b, c, d, e, 0, 11);
107 F1(e, a, b, c, d, 1, 14);
108 F1(d, e, a, b, c, 2, 15);
109 F1(c, d, e, a, b, 3, 12);
110 F1(b, c, d, e, a, 4, 5);
111 F1(a, b, c, d, e, 5, 8);
112 F1(e, a, b, c, d, 6, 7);
113 F1(d, e, a, b, c, 7, 9);
114 F1(c, d, e, a, b, 8, 11);
115 F1(b, c, d, e, a, 9, 13);
116 F1(a, b, c, d, e, 10, 14);
117 F1(e, a, b, c, d, 11, 15);
118 F1(d, e, a, b, c, 12, 6);
119 F1(c, d, e, a, b, 13, 7);
120 F1(b, c, d, e, a, 14, 9);
121 F1(a, b, c, d, e, 15, 8);
122
123 F2(A, B, C, D, E, 5, 8);
124 F2(E, A, B, C, D, 14, 9);
125 F2(D, E, A, B, C, 7, 9);
126 F2(C, D, E, A, B, 0, 11);
127 F2(B, C, D, E, A, 9, 13);
128 F2(A, B, C, D, E, 2, 15);
129 F2(E, A, B, C, D, 11, 15);
130 F2(D, E, A, B, C, 4, 5);
131 F2(C, D, E, A, B, 13, 7);
132 F2(B, C, D, E, A, 6, 7);
133 F2(A, B, C, D, E, 15, 8);
134 F2(E, A, B, C, D, 8, 11);
135 F2(D, E, A, B, C, 1, 14);
136 F2(C, D, E, A, B, 10, 14);
137 F2(B, C, D, E, A, 3, 12);
138 F2(A, B, C, D, E, 12, 6);
139
140 G1(e, A, b, c, d, 7, 7);
141 G1(d, e, A, b, c, 4, 6);
142 G1(c, d, e, A, b, 13, 8);
143 G1(b, c, d, e, A, 1, 13);
144 G1(A, b, c, d, e, 10, 11);
145 G1(e, A, b, c, d, 6, 9);
146 G1(d, e, A, b, c, 15, 7);
147 G1(c, d, e, A, b, 3, 15);
148 G1(b, c, d, e, A, 12, 7);
149 G1(A, b, c, d, e, 0, 12);
150 G1(e, A, b, c, d, 9, 15);
151 G1(d, e, A, b, c, 5, 9);
152 G1(c, d, e, A, b, 2, 11);
153 G1(b, c, d, e, A, 14, 7);
154 G1(A, b, c, d, e, 11, 13);
155 G1(e, A, b, c, d, 8, 12);
156
157 G2(E, a, B, C, D, 6, 9);
158 G2(D, E, a, B, C, 11, 13);
159 G2(C, D, E, a, B, 3, 15);
160 G2(B, C, D, E, a, 7, 7);
161 G2(a, B, C, D, E, 0, 12);
162 G2(E, a, B, C, D, 13, 8);
163 G2(D, E, a, B, C, 5, 9);
164 G2(C, D, E, a, B, 10, 11);
165 G2(B, C, D, E, a, 14, 7);
166 G2(a, B, C, D, E, 15, 7);
167 G2(E, a, B, C, D, 8, 12);
168 G2(D, E, a, B, C, 12, 7);
169 G2(C, D, E, a, B, 4, 6);
170 G2(B, C, D, E, a, 9, 15);
171 G2(a, B, C, D, E, 1, 13);
172 G2(E, a, B, C, D, 2, 11);
173
174 H1(d, e, A, B, c, 3, 11);
175 H1(c, d, e, A, B, 10, 13);
176 H1(B, c, d, e, A, 14, 6);
177 H1(A, B, c, d, e, 4, 7);
178 H1(e, A, B, c, d, 9, 14);
179 H1(d, e, A, B, c, 15, 9);
180 H1(c, d, e, A, B, 8, 13);
181 H1(B, c, d, e, A, 1, 15);
182 H1(A, B, c, d, e, 2, 14);
183 H1(e, A, B, c, d, 7, 8);
184 H1(d, e, A, B, c, 0, 13);
185 H1(c, d, e, A, B, 6, 6);
186 H1(B, c, d, e, A, 13, 5);
187 H1(A, B, c, d, e, 11, 12);
188 H1(e, A, B, c, d, 5, 7);
189 H1(d, e, A, B, c, 12, 5);
190
191 H2(D, E, a, b, C, 15, 9);
192 H2(C, D, E, a, b, 5, 7);
193 H2(b, C, D, E, a, 1, 15);
194 H2(a, b, C, D, E, 3, 11);
195 H2(E, a, b, C, D, 7, 8);
196 H2(D, E, a, b, C, 14, 6);
197 H2(C, D, E, a, b, 6, 6);
198 H2(b, C, D, E, a, 9, 14);
199 H2(a, b, C, D, E, 11, 12);
200 H2(E, a, b, C, D, 8, 13);
201 H2(D, E, a, b, C, 12, 5);
202 H2(C, D, E, a, b, 2, 14);
203 H2(b, C, D, E, a, 10, 13);
204 H2(a, b, C, D, E, 0, 13);
205 H2(E, a, b, C, D, 4, 7);
206 H2(D, E, a, b, C, 13, 5);
207
208 I1(C, d, e, A, B, 1, 11);
209 I1(B, C, d, e, A, 9, 12);
210 I1(A, B, C, d, e, 11, 14);
211 I1(e, A, B, C, d, 10, 15);
212 I1(d, e, A, B, C, 0, 14);
213 I1(C, d, e, A, B, 8, 15);
214 I1(B, C, d, e, A, 12, 9);
215 I1(A, B, C, d, e, 4, 8);
216 I1(e, A, B, C, d, 13, 9);
217 I1(d, e, A, B, C, 3, 14);
218 I1(C, d, e, A, B, 7, 5);
219 I1(B, C, d, e, A, 15, 6);
220 I1(A, B, C, d, e, 14, 8);
221 I1(e, A, B, C, d, 5, 6);
222 I1(d, e, A, B, C, 6, 5);
223 I1(C, d, e, A, B, 2, 12);
224
225 I2(c, D, E, a, b, 8, 15);
226 I2(b, c, D, E, a, 6, 5);
227 I2(a, b, c, D, E, 4, 8);
228 I2(E, a, b, c, D, 1, 11);
229 I2(D, E, a, b, c, 3, 14);
230 I2(c, D, E, a, b, 11, 14);
231 I2(b, c, D, E, a, 15, 6);
232 I2(a, b, c, D, E, 0, 14);
233 I2(E, a, b, c, D, 5, 6);
234 I2(D, E, a, b, c, 12, 9);
235 I2(c, D, E, a, b, 2, 12);
236 I2(b, c, D, E, a, 13, 9);
237 I2(a, b, c, D, E, 9, 12);
238 I2(E, a, b, c, D, 7, 5);
239 I2(D, E, a, b, c, 10, 15);
240 I2(c, D, E, a, b, 14, 8);
241
242 J1(B, C, D, e, A, 4, 9);
243 J1(A, B, C, D, e, 0, 15);
244 J1(e, A, B, C, D, 5, 5);
245 J1(D, e, A, B, C, 9, 11);
246 J1(C, D, e, A, B, 7, 6);
247 J1(B, C, D, e, A, 12, 8);
248 J1(A, B, C, D, e, 2, 13);
249 J1(e, A, B, C, D, 10, 12);
250 J1(D, e, A, B, C, 14, 5);
251 J1(C, D, e, A, B, 1, 12);
252 J1(B, C, D, e, A, 3, 13);
253 J1(A, B, C, D, e, 8, 14);
254 J1(e, A, B, C, D, 11, 11);
255 J1(D, e, A, B, C, 6, 8);
256 J1(C, D, e, A, B, 15, 5);
257 J1(B, C, D, e, A, 13, 6);
258
259 J2(b, c, d, E, a, 12, 8);
260 J2(a, b, c, d, E, 15, 5);
261 J2(E, a, b, c, d, 10, 12);
262 J2(d, E, a, b, c, 4, 9);
263 J2(c, d, E, a, b, 1, 12);
264 J2(b, c, d, E, a, 5, 5);
265 J2(a, b, c, d, E, 8, 14);
266 J2(E, a, b, c, d, 7, 6);
267 J2(d, E, a, b, c, 6, 8);
268 J2(c, d, E, a, b, 2, 13);
269 J2(b, c, d, E, a, 13, 6);
270 J2(a, b, c, d, E, 14, 5);
271 J2(E, a, b, c, d, 0, 15);
272 J2(d, E, a, b, c, 3, 13);
273 J2(c, d, E, a, b, 9, 11);
274 J2(b, c, d, E, a, 11, 11);
275
276 /* --- Write out the result --- */
277
278 ctx->a += A;
279 ctx->b += B;
280 ctx->c += C;
281 ctx->d += D;
282 ctx->e += E;
283 ctx->A += a;
284 ctx->B += b;
285 ctx->C += c;
286 ctx->D += d;
287 ctx->E += e;
288 }
289
290 /* --- @rmd320_init@ --- *
291 *
292 * Arguments: @rmd320_ctx *ctx@ = pointer to context block to initialize
293 *
294 * Returns: ---
295 *
296 * Use: Initializes a context block ready for hashing.
297 */
298
299 void rmd320_init(rmd320_ctx *ctx)
300 {
301 ctx->a = 0x67452301;
302 ctx->b = 0xefcdab89;
303 ctx->c = 0x98badcfe;
304 ctx->d = 0x10325476;
305 ctx->e = 0xc3d2e1f0;
306 ctx->A = 0x76543210;
307 ctx->B = 0xfedcba98;
308 ctx->C = 0x89abcdef;
309 ctx->D = 0x01234567;
310 ctx->E = 0x3c2d1e0f;
311 ctx->off = 0;
312 ctx->nl = ctx->nh = 0;
313 }
314
315 /* --- @rmd320_set@ --- *
316 *
317 * Arguments: @rmd320_ctx *ctx@ = pointer to context block
318 * @const void *buf@ = pointer to state buffer
319 * @unsigned long count@ = current count of bytes processed
320 *
321 * Returns: ---
322 *
323 * Use: Initializes a context block from a given state. This is
324 * useful in cases where the initial hash state is meant to be
325 * secret, e.g., for NMAC and HMAC support.
326 */
327
328 void rmd320_set(rmd320_ctx *ctx, const void *buf, unsigned long count)
329 {
330 const octet *p = buf;
331 ctx->a = LOAD32_L(p + 0);
332 ctx->b = LOAD32_L(p + 4);
333 ctx->c = LOAD32_L(p + 8);
334 ctx->d = LOAD32_L(p + 12);
335 ctx->e = LOAD32_L(p + 16);
336 ctx->A = LOAD32_L(p + 20);
337 ctx->B = LOAD32_L(p + 24);
338 ctx->C = LOAD32_L(p + 28);
339 ctx->D = LOAD32_L(p + 32);
340 ctx->E = LOAD32_L(p + 36);
341 ctx->off = 0;
342 ctx->nl = U32(count);
343 ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
344 }
345
346 /* --- @rmd320_hash@ --- *
347 *
348 * Arguments: @rmd320_ctx *ctx@ = pointer to context block
349 * @const void *buf@ = buffer of data to hash
350 * @size_t sz@ = size of buffer to hash
351 *
352 * Returns: ---
353 *
354 * Use: Hashes a buffer of data. The buffer may be of any size and
355 * alignment.
356 */
357
358 void rmd320_hash(rmd320_ctx *ctx, const void *buf, size_t sz)
359 {
360 HASH_BUFFER(RMD320, rmd320, ctx, buf, sz);
361 }
362
363 /* --- @rmd320_done@ --- *
364 *
365 * Arguments: @rmd320_ctx *ctx@ = pointer to context block
366 * @void *hash@ = pointer to output buffer
367 *
368 * Returns: ---
369 *
370 * Use: Returns the hash of the data read so far.
371 */
372
373 void rmd320_done(rmd320_ctx *ctx, void *hash)
374 {
375 octet *p = hash;
376 HASH_MD5STRENGTH(RMD320, rmd320, ctx);
377 STORE32_L(p + 0, ctx->a);
378 STORE32_L(p + 4, ctx->b);
379 STORE32_L(p + 8, ctx->c);
380 STORE32_L(p + 12, ctx->d);
381 STORE32_L(p + 16, ctx->e);
382 STORE32_L(p + 20, ctx->A);
383 STORE32_L(p + 24, ctx->B);
384 STORE32_L(p + 28, ctx->C);
385 STORE32_L(p + 32, ctx->D);
386 STORE32_L(p + 36, ctx->E);
387 }
388
389 /* --- @rmd320_state@ --- *
390 *
391 * Arguments: @rmd320_ctx *ctx@ = pointer to context
392 * @void *state@ = pointer to buffer for current state
393 *
394 * Returns: Number of bytes written to the hash function so far.
395 *
396 * Use: Returns the current state of the hash function such that
397 * it can be passed to @rmd320_set@.
398 */
399
400 unsigned long rmd320_state(rmd320_ctx *ctx, void *state)
401 {
402 octet *p = state;
403 STORE32_L(p + 0, ctx->a);
404 STORE32_L(p + 4, ctx->b);
405 STORE32_L(p + 8, ctx->c);
406 STORE32_L(p + 12, ctx->d);
407 STORE32_L(p + 16, ctx->e);
408 STORE32_L(p + 20, ctx->A);
409 STORE32_L(p + 24, ctx->B);
410 STORE32_L(p + 28, ctx->C);
411 STORE32_L(p + 32, ctx->D);
412 STORE32_L(p + 36, ctx->E);
413 return (ctx->nl | ((ctx->nh << 16) << 16));
414 }
415
416 /* --- Generic interface --- */
417
418 GHASH_DEF(RMD320, rmd320)
419
420 /* --- Test code --- */
421
422 HASH_TEST(RMD320, rmd320)
423
424 /*----- That's all, folks -------------------------------------------------*/