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