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