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