math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / symm / rmd256.c
CommitLineData
1c64c8e2 1/* -*-c-*-
2 *
1c64c8e2 3 * The RIPEMD-256 message digest function
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
1c64c8e2 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.
45c0fd36 16 *
1c64c8e2 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.
45c0fd36 21 *
1c64c8e2 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
1c64c8e2 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 "rmd256.h"
36
37/*----- Main code ---------------------------------------------------------*/
38
39/* --- @rmd256_compress@ --- *
40 *
41 * Arguments: @rmd256_ctx *ctx@ = pointer to context block
42 * @const void *sbuf@ = pointer to buffer of appropriate size
43 *
44 * Returns: ---
45 *
46 * Use: RIPEMD-256 compression function.
47 */
48
49void rmd256_compress(rmd256_ctx *ctx, const void *sbuf)
50{
51 uint32 a, b, c, d;
52 uint32 A, B, C, D;
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 A = ctx->A;
62 B = ctx->B;
63 C = ctx->C;
64 D = ctx->D;
65
66 /* --- Fetch the buffer contents --- */
67
68 {
69 int i;
70 const octet *p;
71
72 for (i = 0, p = sbuf; i < 16; i++, p += 4)
73 buf[i] = LOAD32_L(p);
74 }
75
76 /* --- Definitions for round functions --- */
77
78#define F(x, y, z) ((x) ^ (y) ^ (z))
79#define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
80#define H(x, y, z) (((x) | ~(y)) ^ (z))
81#define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
82
83#define T(w, x, y, z, i, r, f, k) do { \
84 uint32 _t = w + f(x, y, z) + buf[i] + k; \
85 w = ROL32(_t, r); \
86} while (0)
87
88#define F1(w, x, y, z, i, r) T(w, x, y, z, i, r, F, 0x00000000)
89#define G1(w, x, y, z, i, r) T(w, x, y, z, i, r, G, 0x5a827999)
90#define H1(w, x, y, z, i, r) T(w, x, y, z, i, r, H, 0x6ed9eba1)
91#define I1(w, x, y, z, i, r) T(w, x, y, z, i, r, I, 0x8f1bbcdc)
45c0fd36 92
1c64c8e2 93#define F2(w, x, y, z, i, r) T(w, x, y, z, i, r, I, 0x50a28be6)
94#define G2(w, x, y, z, i, r) T(w, x, y, z, i, r, H, 0x5c4dd124)
95#define H2(w, x, y, z, i, r) T(w, x, y, z, i, r, G, 0x6d703ef3)
96#define I2(w, x, y, z, i, r) T(w, x, y, z, i, r, F, 0x00000000)
97
b755a9e4 98 /* --- We must do both sides together --- */
1c64c8e2 99
100 F1(a, b, c, d, 0, 11);
101 F1(d, a, b, c, 1, 14);
102 F1(c, d, a, b, 2, 15);
103 F1(b, c, d, a, 3, 12);
104 F1(a, b, c, d, 4, 5);
105 F1(d, a, b, c, 5, 8);
106 F1(c, d, a, b, 6, 7);
107 F1(b, c, d, a, 7, 9);
108 F1(a, b, c, d, 8, 11);
109 F1(d, a, b, c, 9, 13);
110 F1(c, d, a, b, 10, 14);
111 F1(b, c, d, a, 11, 15);
112 F1(a, b, c, d, 12, 6);
113 F1(d, a, b, c, 13, 7);
114 F1(c, d, a, b, 14, 9);
115 F1(b, c, d, a, 15, 8);
116
117 F2(A, B, C, D, 5, 8);
118 F2(D, A, B, C, 14, 9);
119 F2(C, D, A, B, 7, 9);
120 F2(B, C, D, A, 0, 11);
121 F2(A, B, C, D, 9, 13);
122 F2(D, A, B, C, 2, 15);
123 F2(C, D, A, B, 11, 15);
124 F2(B, C, D, A, 4, 5);
125 F2(A, B, C, D, 13, 7);
126 F2(D, A, B, C, 6, 7);
127 F2(C, D, A, B, 15, 8);
128 F2(B, C, D, A, 8, 11);
129 F2(A, B, C, D, 1, 14);
130 F2(D, A, B, C, 10, 14);
131 F2(C, D, A, B, 3, 12);
132 F2(B, C, D, A, 12, 6);
133
134 G1(A, b, c, d, 7, 7);
135 G1(d, A, b, c, 4, 6);
136 G1(c, d, A, b, 13, 8);
137 G1(b, c, d, A, 1, 13);
138 G1(A, b, c, d, 10, 11);
139 G1(d, A, b, c, 6, 9);
140 G1(c, d, A, b, 15, 7);
141 G1(b, c, d, A, 3, 15);
142 G1(A, b, c, d, 12, 7);
143 G1(d, A, b, c, 0, 12);
144 G1(c, d, A, b, 9, 15);
145 G1(b, c, d, A, 5, 9);
146 G1(A, b, c, d, 2, 11);
147 G1(d, A, b, c, 14, 7);
148 G1(c, d, A, b, 11, 13);
149 G1(b, c, d, A, 8, 12);
150
151 G2(a, B, C, D, 6, 9);
152 G2(D, a, B, C, 11, 13);
153 G2(C, D, a, B, 3, 15);
154 G2(B, C, D, a, 7, 7);
155 G2(a, B, C, D, 0, 12);
156 G2(D, a, B, C, 13, 8);
157 G2(C, D, a, B, 5, 9);
158 G2(B, C, D, a, 10, 11);
159 G2(a, B, C, D, 14, 7);
160 G2(D, a, B, C, 15, 7);
161 G2(C, D, a, B, 8, 12);
162 G2(B, C, D, a, 12, 7);
163 G2(a, B, C, D, 4, 6);
164 G2(D, a, B, C, 9, 15);
165 G2(C, D, a, B, 1, 13);
166 G2(B, C, D, a, 2, 11);
167
168 H1(A, B, c, d, 3, 11);
169 H1(d, A, B, c, 10, 13);
170 H1(c, d, A, B, 14, 6);
171 H1(B, c, d, A, 4, 7);
172 H1(A, B, c, d, 9, 14);
173 H1(d, A, B, c, 15, 9);
174 H1(c, d, A, B, 8, 13);
175 H1(B, c, d, A, 1, 15);
176 H1(A, B, c, d, 2, 14);
177 H1(d, A, B, c, 7, 8);
178 H1(c, d, A, B, 0, 13);
179 H1(B, c, d, A, 6, 6);
180 H1(A, B, c, d, 13, 5);
181 H1(d, A, B, c, 11, 12);
182 H1(c, d, A, B, 5, 7);
183 H1(B, c, d, A, 12, 5);
184
185 H2(a, b, C, D, 15, 9);
186 H2(D, a, b, C, 5, 7);
187 H2(C, D, a, b, 1, 15);
188 H2(b, C, D, a, 3, 11);
189 H2(a, b, C, D, 7, 8);
190 H2(D, a, b, C, 14, 6);
191 H2(C, D, a, b, 6, 6);
192 H2(b, C, D, a, 9, 14);
193 H2(a, b, C, D, 11, 12);
194 H2(D, a, b, C, 8, 13);
195 H2(C, D, a, b, 12, 5);
196 H2(b, C, D, a, 2, 14);
197 H2(a, b, C, D, 10, 13);
198 H2(D, a, b, C, 0, 13);
199 H2(C, D, a, b, 4, 7);
200 H2(b, C, D, a, 13, 5);
201
202 I1(A, B, C, d, 1, 11);
203 I1(d, A, B, C, 9, 12);
204 I1(C, d, A, B, 11, 14);
205 I1(B, C, d, A, 10, 15);
206 I1(A, B, C, d, 0, 14);
207 I1(d, A, B, C, 8, 15);
208 I1(C, d, A, B, 12, 9);
209 I1(B, C, d, A, 4, 8);
210 I1(A, B, C, d, 13, 9);
211 I1(d, A, B, C, 3, 14);
212 I1(C, d, A, B, 7, 5);
213 I1(B, C, d, A, 15, 6);
214 I1(A, B, C, d, 14, 8);
215 I1(d, A, B, C, 5, 6);
216 I1(C, d, A, B, 6, 5);
217 I1(B, C, d, A, 2, 12);
45c0fd36 218
1c64c8e2 219 I2(a, b, c, D, 8, 15);
220 I2(D, a, b, c, 6, 5);
221 I2(c, D, a, b, 4, 8);
222 I2(b, c, D, a, 1, 11);
223 I2(a, b, c, D, 3, 14);
224 I2(D, a, b, c, 11, 14);
225 I2(c, D, a, b, 15, 6);
226 I2(b, c, D, a, 0, 14);
227 I2(a, b, c, D, 5, 6);
228 I2(D, a, b, c, 12, 9);
229 I2(c, D, a, b, 2, 12);
230 I2(b, c, D, a, 13, 9);
231 I2(a, b, c, D, 9, 12);
232 I2(D, a, b, c, 7, 5);
233 I2(c, D, a, b, 10, 15);
234 I2(b, c, D, a, 14, 8);
45c0fd36 235
1c64c8e2 236 /* --- Recombine the two halves --- */
237
238 ctx->a += A;
239 ctx->b += B;
240 ctx->c += C;
241 ctx->d += D;
242 ctx->A += a;
243 ctx->B += b;
244 ctx->C += c;
245 ctx->D += d;
246}
247
248/* --- @rmd256_init@ --- *
249 *
250 * Arguments: @rmd256_ctx *ctx@ = pointer to context block to initialize
251 *
252 * Returns: ---
253 *
254 * Use: Initializes a context block ready for hashing.
255 */
256
257void rmd256_init(rmd256_ctx *ctx)
258{
259 ctx->a = 0x67452301;
260 ctx->b = 0xefcdab89;
261 ctx->c = 0x98badcfe;
262 ctx->d = 0x10325476;
263 ctx->A = 0x76543210;
264 ctx->B = 0xfedcba98;
265 ctx->C = 0x89abcdef;
266 ctx->D = 0x01234567;
267 ctx->off = 0;
268 ctx->nl = ctx->nh = 0;
269}
270
271/* --- @rmd256_set@ --- *
272 *
273 * Arguments: @rmd256_ctx *ctx@ = pointer to context block
274 * @const void *buf@ = pointer to state buffer
275 * @unsigned long count@ = current count of bytes processed
276 *
277 * Returns: ---
278 *
279 * Use: Initializes a context block from a given state. This is
280 * useful in cases where the initial hash state is meant to be
281 * secret, e.g., for NMAC and HMAC support.
282 */
283
284void rmd256_set(rmd256_ctx *ctx, const void *buf, unsigned long count)
285{
286 const octet *p = buf;
45c0fd36
MW
287 ctx->a = LOAD32_L(p + 0);
288 ctx->b = LOAD32_L(p + 4);
289 ctx->c = LOAD32_L(p + 8);
1c64c8e2 290 ctx->d = LOAD32_L(p + 12);
291 ctx->A = LOAD32_L(p + 16);
292 ctx->B = LOAD32_L(p + 20);
293 ctx->C = LOAD32_L(p + 24);
294 ctx->D = LOAD32_L(p + 28);
295 ctx->off = 0;
296 ctx->nl = U32(count);
297 ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
298}
299
300/* --- @rmd256_hash@ --- *
301 *
302 * Arguments: @rmd256_ctx *ctx@ = pointer to context block
303 * @const void *buf@ = buffer of data to hash
304 * @size_t sz@ = size of buffer to hash
305 *
306 * Returns: ---
307 *
308 * Use: Hashes a buffer of data. The buffer may be of any size and
309 * alignment.
310 */
311
312void rmd256_hash(rmd256_ctx *ctx, const void *buf, size_t sz)
313{
314 HASH_BUFFER(RMD256, rmd256, ctx, buf, sz);
315}
316
317/* --- @rmd256_done@ --- *
318 *
319 * Arguments: @rmd256_ctx *ctx@ = pointer to context block
320 * @void *hash@ = pointer to output buffer
321 *
322 * Returns: ---
323 *
324 * Use: Returns the hash of the data read so far.
325 */
326
327void rmd256_done(rmd256_ctx *ctx, void *hash)
328{
329 octet *p = hash;
330 HASH_MD5STRENGTH(RMD256, rmd256, ctx);
45c0fd36
MW
331 STORE32_L(p + 0, ctx->a);
332 STORE32_L(p + 4, ctx->b);
333 STORE32_L(p + 8, ctx->c);
1c64c8e2 334 STORE32_L(p + 12, ctx->d);
335 STORE32_L(p + 16, ctx->A);
336 STORE32_L(p + 20, ctx->B);
337 STORE32_L(p + 24, ctx->C);
338 STORE32_L(p + 28, ctx->D);
339}
340
341/* --- @rmd256_state@ --- *
342 *
343 * Arguments: @rmd256_ctx *ctx@ = pointer to context
344 * @void *state@ = pointer to buffer for current state
345 *
346 * Returns: Number of bytes written to the hash function so far.
347 *
348 * Use: Returns the current state of the hash function such that
349 * it can be passed to @rmd256_set@.
350 */
351
352unsigned long rmd256_state(rmd256_ctx *ctx, void *state)
353{
354 octet *p = state;
45c0fd36
MW
355 STORE32_L(p + 0, ctx->a);
356 STORE32_L(p + 4, ctx->b);
357 STORE32_L(p + 8, ctx->c);
1c64c8e2 358 STORE32_L(p + 12, ctx->d);
359 STORE32_L(p + 16, ctx->A);
360 STORE32_L(p + 20, ctx->B);
361 STORE32_L(p + 24, ctx->C);
362 STORE32_L(p + 28, ctx->D);
363 return (ctx->nl | ((ctx->nh << 16) << 16));
364}
365
366/* --- Generic interface --- */
367
368GHASH_DEF(RMD256, rmd256)
369
370/* --- Test code --- */
371
372HASH_TEST(RMD256, rmd256)
373
374/*----- That's all, folks -------------------------------------------------*/