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