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