New multiprecision integer arithmetic suite.
[u/mdw/catacomb] / rmd160.c
1 /* -*-c-*-
2 *
3 * $Id: rmd160.c,v 1.1 1999/09/03 08:41:12 mdw Exp $
4 *
5 * The RIPEMD-160 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: rmd160.c,v $
33 * Revision 1.1 1999/09/03 08:41:12 mdw
34 * Initial import.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <mLib/bits.h>
41
42 #include "hash.h"
43 #include "rmd160.h"
44
45 /*----- Main code ---------------------------------------------------------*/
46
47 /* --- @rmd160_compress@ --- *
48 *
49 * Arguments: @rmd160_ctx *ctx@ = pointer to context block
50 * @const void *sbuf@ = pointer to buffer of appropriate size
51 *
52 * Returns: ---
53 *
54 * Use: RIPEMD-160 compression function.
55 */
56
57 void rmd160_compress(rmd160_ctx *ctx, const void *sbuf)
58 {
59 uint32 a, b, c, d, e;
60 uint32 A, B, C, D, E;
61 uint32 buf[16];
62
63 /* --- Fetch the chaining variables --- */
64
65 a = A = ctx->a;
66 b = B = ctx->b;
67 c = C = ctx->c;
68 d = D = ctx->d;
69 e = E = ctx->e;
70
71 /* --- Fetch the buffer contents --- */
72
73 {
74 int i;
75 const octet *p;
76
77 for (i = 0, p = sbuf; i < 16; i++, p += 4)
78 buf[i] = LOAD32_L(p);
79 }
80
81 /* --- Definitions for round functions --- */
82
83 #define F(x, y, z) ((x) ^ (y) ^ (z))
84 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
85 #define H(x, y, z) (((x) | ~(y)) ^ (z))
86 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
87 #define J(x, y, z) ((x) ^ ((y) | ~(z)))
88
89 #define T(v, w, x, y, z, i, r, f, k) do { \
90 uint32 _t = v + f(w, x, y) + buf[i] + k; \
91 v = ROL32(_t, r) + z; x = ROL32(x, 10); \
92 } while (0)
93
94 #define F1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, F, 0x00000000)
95 #define G1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, G, 0x5a827999)
96 #define H1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, H, 0x6ed9eba1)
97 #define I1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, I, 0x8f1bbcdc)
98 #define J1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, J, 0xa953fd4e)
99
100 #define F2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, J, 0x50a28be6)
101 #define G2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, I, 0x5c4dd124)
102 #define H2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, H, 0x6d703ef3)
103 #define I2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, G, 0x7a6d76e9)
104 #define J2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, F, 0x00000000)
105
106 /* --- First the left hand side --- */
107
108 F1(a, b, c, d, e, 0, 11);
109 F1(e, a, b, c, d, 1, 14);
110 F1(d, e, a, b, c, 2, 15);
111 F1(c, d, e, a, b, 3, 12);
112 F1(b, c, d, e, a, 4, 5);
113 F1(a, b, c, d, e, 5, 8);
114 F1(e, a, b, c, d, 6, 7);
115 F1(d, e, a, b, c, 7, 9);
116 F1(c, d, e, a, b, 8, 11);
117 F1(b, c, d, e, a, 9, 13);
118 F1(a, b, c, d, e, 10, 14);
119 F1(e, a, b, c, d, 11, 15);
120 F1(d, e, a, b, c, 12, 6);
121 F1(c, d, e, a, b, 13, 7);
122 F1(b, c, d, e, a, 14, 9);
123 F1(a, b, c, d, e, 15, 8);
124
125 G1(e, a, b, c, d, 7, 7);
126 G1(d, e, a, b, c, 4, 6);
127 G1(c, d, e, a, b, 13, 8);
128 G1(b, c, d, e, a, 1, 13);
129 G1(a, b, c, d, e, 10, 11);
130 G1(e, a, b, c, d, 6, 9);
131 G1(d, e, a, b, c, 15, 7);
132 G1(c, d, e, a, b, 3, 15);
133 G1(b, c, d, e, a, 12, 7);
134 G1(a, b, c, d, e, 0, 12);
135 G1(e, a, b, c, d, 9, 15);
136 G1(d, e, a, b, c, 5, 9);
137 G1(c, d, e, a, b, 2, 11);
138 G1(b, c, d, e, a, 14, 7);
139 G1(a, b, c, d, e, 11, 13);
140 G1(e, a, b, c, d, 8, 12);
141
142 H1(d, e, a, b, c, 3, 11);
143 H1(c, d, e, a, b, 10, 13);
144 H1(b, c, d, e, a, 14, 6);
145 H1(a, b, c, d, e, 4, 7);
146 H1(e, a, b, c, d, 9, 14);
147 H1(d, e, a, b, c, 15, 9);
148 H1(c, d, e, a, b, 8, 13);
149 H1(b, c, d, e, a, 1, 15);
150 H1(a, b, c, d, e, 2, 14);
151 H1(e, a, b, c, d, 7, 8);
152 H1(d, e, a, b, c, 0, 13);
153 H1(c, d, e, a, b, 6, 6);
154 H1(b, c, d, e, a, 13, 5);
155 H1(a, b, c, d, e, 11, 12);
156 H1(e, a, b, c, d, 5, 7);
157 H1(d, e, a, b, c, 12, 5);
158
159 I1(c, d, e, a, b, 1, 11);
160 I1(b, c, d, e, a, 9, 12);
161 I1(a, b, c, d, e, 11, 14);
162 I1(e, a, b, c, d, 10, 15);
163 I1(d, e, a, b, c, 0, 14);
164 I1(c, d, e, a, b, 8, 15);
165 I1(b, c, d, e, a, 12, 9);
166 I1(a, b, c, d, e, 4, 8);
167 I1(e, a, b, c, d, 13, 9);
168 I1(d, e, a, b, c, 3, 14);
169 I1(c, d, e, a, b, 7, 5);
170 I1(b, c, d, e, a, 15, 6);
171 I1(a, b, c, d, e, 14, 8);
172 I1(e, a, b, c, d, 5, 6);
173 I1(d, e, a, b, c, 6, 5);
174 I1(c, d, e, a, b, 2, 12);
175
176 J1(b, c, d, e, a, 4, 9);
177 J1(a, b, c, d, e, 0, 15);
178 J1(e, a, b, c, d, 5, 5);
179 J1(d, e, a, b, c, 9, 11);
180 J1(c, d, e, a, b, 7, 6);
181 J1(b, c, d, e, a, 12, 8);
182 J1(a, b, c, d, e, 2, 13);
183 J1(e, a, b, c, d, 10, 12);
184 J1(d, e, a, b, c, 14, 5);
185 J1(c, d, e, a, b, 1, 12);
186 J1(b, c, d, e, a, 3, 13);
187 J1(a, b, c, d, e, 8, 14);
188 J1(e, a, b, c, d, 11, 11);
189 J1(d, e, a, b, c, 6, 8);
190 J1(c, d, e, a, b, 15, 5);
191 J1(b, c, d, e, a, 13, 6);
192
193 /* --- And then the right hand side --- */
194
195 F2(A, B, C, D, E, 5, 8);
196 F2(E, A, B, C, D, 14, 9);
197 F2(D, E, A, B, C, 7, 9);
198 F2(C, D, E, A, B, 0, 11);
199 F2(B, C, D, E, A, 9, 13);
200 F2(A, B, C, D, E, 2, 15);
201 F2(E, A, B, C, D, 11, 15);
202 F2(D, E, A, B, C, 4, 5);
203 F2(C, D, E, A, B, 13, 7);
204 F2(B, C, D, E, A, 6, 7);
205 F2(A, B, C, D, E, 15, 8);
206 F2(E, A, B, C, D, 8, 11);
207 F2(D, E, A, B, C, 1, 14);
208 F2(C, D, E, A, B, 10, 14);
209 F2(B, C, D, E, A, 3, 12);
210 F2(A, B, C, D, E, 12, 6);
211
212 G2(E, A, B, C, D, 6, 9);
213 G2(D, E, A, B, C, 11, 13);
214 G2(C, D, E, A, B, 3, 15);
215 G2(B, C, D, E, A, 7, 7);
216 G2(A, B, C, D, E, 0, 12);
217 G2(E, A, B, C, D, 13, 8);
218 G2(D, E, A, B, C, 5, 9);
219 G2(C, D, E, A, B, 10, 11);
220 G2(B, C, D, E, A, 14, 7);
221 G2(A, B, C, D, E, 15, 7);
222 G2(E, A, B, C, D, 8, 12);
223 G2(D, E, A, B, C, 12, 7);
224 G2(C, D, E, A, B, 4, 6);
225 G2(B, C, D, E, A, 9, 15);
226 G2(A, B, C, D, E, 1, 13);
227 G2(E, A, B, C, D, 2, 11);
228
229 H2(D, E, A, B, C, 15, 9);
230 H2(C, D, E, A, B, 5, 7);
231 H2(B, C, D, E, A, 1, 15);
232 H2(A, B, C, D, E, 3, 11);
233 H2(E, A, B, C, D, 7, 8);
234 H2(D, E, A, B, C, 14, 6);
235 H2(C, D, E, A, B, 6, 6);
236 H2(B, C, D, E, A, 9, 14);
237 H2(A, B, C, D, E, 11, 12);
238 H2(E, A, B, C, D, 8, 13);
239 H2(D, E, A, B, C, 12, 5);
240 H2(C, D, E, A, B, 2, 14);
241 H2(B, C, D, E, A, 10, 13);
242 H2(A, B, C, D, E, 0, 13);
243 H2(E, A, B, C, D, 4, 7);
244 H2(D, E, A, B, C, 13, 5);
245
246 I2(C, D, E, A, B, 8, 15);
247 I2(B, C, D, E, A, 6, 5);
248 I2(A, B, C, D, E, 4, 8);
249 I2(E, A, B, C, D, 1, 11);
250 I2(D, E, A, B, C, 3, 14);
251 I2(C, D, E, A, B, 11, 14);
252 I2(B, C, D, E, A, 15, 6);
253 I2(A, B, C, D, E, 0, 14);
254 I2(E, A, B, C, D, 5, 6);
255 I2(D, E, A, B, C, 12, 9);
256 I2(C, D, E, A, B, 2, 12);
257 I2(B, C, D, E, A, 13, 9);
258 I2(A, B, C, D, E, 9, 12);
259 I2(E, A, B, C, D, 7, 5);
260 I2(D, E, A, B, C, 10, 15);
261 I2(C, D, E, A, B, 14, 8);
262
263 J2(B, C, D, E, A, 12, 8);
264 J2(A, B, C, D, E, 15, 5);
265 J2(E, A, B, C, D, 10, 12);
266 J2(D, E, A, B, C, 4, 9);
267 J2(C, D, E, A, B, 1, 12);
268 J2(B, C, D, E, A, 5, 5);
269 J2(A, B, C, D, E, 8, 14);
270 J2(E, A, B, C, D, 7, 6);
271 J2(D, E, A, B, C, 6, 8);
272 J2(C, D, E, A, B, 2, 13);
273 J2(B, C, D, E, A, 13, 6);
274 J2(A, B, C, D, E, 14, 5);
275 J2(E, A, B, C, D, 0, 15);
276 J2(D, E, A, B, C, 3, 13);
277 J2(C, D, E, A, B, 9, 11);
278 J2(B, C, D, E, A, 11, 11);
279
280 /* --- Recombine the two halves --- */
281
282 {
283 uint32
284 tmp = ctx->b + c + D;
285 ctx->b = ctx->c + d + E;
286 ctx->c = ctx->d + e + A;
287 ctx->d = ctx->e + a + B;
288 ctx->e = ctx->a + b + C;
289 ctx->a = tmp;
290 }
291 }
292
293 /* --- @rmd160_init@ --- *
294 *
295 * Arguments: @rmd160_ctx *ctx@ = pointer to context block to initialize
296 *
297 * Returns: ---
298 *
299 * Use: Initializes a context block ready for hashing.
300 */
301
302 void rmd160_init(rmd160_ctx *ctx)
303 {
304 ctx->a = 0x67452301;
305 ctx->b = 0xefcdab89;
306 ctx->c = 0x98badcfe;
307 ctx->d = 0x10325476;
308 ctx->e = 0xc3d2e1f0;
309 ctx->off = 0;
310 ctx->count = 0;
311 }
312
313 /* --- @rmd160_set@ --- *
314 *
315 * Arguments: @rmd160_ctx *ctx@ = pointer to context block
316 * @const void *buf@ = pointer to state buffer
317 * @unsigned long count@ = current count of bytes processed
318 *
319 * Returns: ---
320 *
321 * Use: Initializes a context block from a given state. This is
322 * useful in cases where the initial hash state is meant to be
323 * secret, e.g., for NMAC and HMAC support.
324 */
325
326 void rmd160_set(rmd160_ctx *ctx, const void *buf, unsigned long count)
327 {
328 const octet *p = buf;
329 ctx->a = LOAD32_L(p + 0);
330 ctx->b = LOAD32_L(p + 4);
331 ctx->c = LOAD32_L(p + 8);
332 ctx->d = LOAD32_L(p + 12);
333 ctx->e = LOAD32_L(p + 16);
334 ctx->off = 0;
335 ctx->count = count;
336 }
337
338 /* --- @rmd160_hash@ --- *
339 *
340 * Arguments: @rmd160_ctx *ctx@ = pointer to context block
341 * @const void *buf@ = buffer of data to hash
342 * @size_t sz@ = size of buffer to hash
343 *
344 * Returns: ---
345 *
346 * Use: Hashes a buffer of data. The buffer may be of any size and
347 * alignment.
348 */
349
350 void rmd160_hash(rmd160_ctx *ctx, const void *buf, size_t sz)
351 {
352 HASH_BUFFER(RMD160, rmd160, ctx, buf, sz);
353 }
354
355 /* --- @rmd160_done@ --- *
356 *
357 * Arguments: @rmd160_ctx *ctx@ = pointer to context block
358 * @void *hash@ = pointer to output buffer
359 *
360 * Returns: ---
361 *
362 * Use: Returns the hash of the data read so far.
363 */
364
365 void rmd160_done(rmd160_ctx *ctx, void *hash)
366 {
367 octet *p = hash;
368 HASH_MD5STRENGTH(RMD160, rmd160, ctx);
369 STORE32_L(p + 0, ctx->a);
370 STORE32_L(p + 4, ctx->b);
371 STORE32_L(p + 8, ctx->c);
372 STORE32_L(p + 12, ctx->d);
373 STORE32_L(p + 16, ctx->e);
374 }
375
376 /* --- @rmd160_state@ --- *
377 *
378 * Arguments: @rmd160_ctx *ctx@ = pointer to context
379 * @void *state@ = pointer to buffer for current state
380 *
381 * Returns: Number of bytes written to the hash function so far.
382 *
383 * Use: Returns the current state of the hash function such that
384 * it can be passed to @rmd160_set@.
385 */
386
387 unsigned long rmd160_state(rmd160_ctx *ctx, void *state)
388 {
389 octet *p = state;
390 STORE32_L(p + 0, ctx->a);
391 STORE32_L(p + 4, ctx->b);
392 STORE32_L(p + 8, ctx->c);
393 STORE32_L(p + 12, ctx->d);
394 STORE32_L(p + 16, ctx->e);
395 return (ctx->count);
396 }
397
398 /* --- Test code --- */
399
400 HASH_TEST(RMD160, rmd160)
401
402 /*----- That's all, folks -------------------------------------------------*/