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