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