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