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