comparison rmd160.c @ 0:d7da3b1e1540 libtomcrypt

put back the 0.95 makefile which was inadvertently merged over
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:21:40 +0000
parents
children 6362d3854bb4
comparison
equal deleted inserted replaced
-1:000000000000 0:d7da3b1e1540
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11
12 /* Implementation of RIPEMD-160 based on the source by Antoon Bosselaers, ESAT-COSIC
13 *
14 * This source has been radically overhauled to be portable and work within
15 * the LibTomCrypt API by Tom St Denis
16 */
17 #include "mycrypt.h"
18
19 #ifdef RIPEMD160
20
21 const struct _hash_descriptor rmd160_desc =
22 {
23 "rmd160",
24 9,
25 20,
26 64,
27 &rmd160_init,
28 &rmd160_process,
29 &rmd160_done,
30 &rmd160_test
31 };
32
33 /* the five basic functions F(), G() and H() */
34 #define F(x, y, z) ((x) ^ (y) ^ (z))
35 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
36 #define H(x, y, z) (((x) | ~(y)) ^ (z))
37 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
38 #define J(x, y, z) ((x) ^ ((y) | ~(z)))
39
40 /* the ten basic operations FF() through III() */
41 #define FF(a, b, c, d, e, x, s) \
42 (a) += F((b), (c), (d)) + (x);\
43 (a) = ROL((a), (s)) + (e);\
44 (c) = ROL((c), 10);
45
46 #define GG(a, b, c, d, e, x, s) \
47 (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
48 (a) = ROL((a), (s)) + (e);\
49 (c) = ROL((c), 10);
50
51 #define HH(a, b, c, d, e, x, s) \
52 (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
53 (a) = ROL((a), (s)) + (e);\
54 (c) = ROL((c), 10);
55
56 #define II(a, b, c, d, e, x, s) \
57 (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
58 (a) = ROL((a), (s)) + (e);\
59 (c) = ROL((c), 10);
60
61 #define JJ(a, b, c, d, e, x, s) \
62 (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
63 (a) = ROL((a), (s)) + (e);\
64 (c) = ROL((c), 10);
65
66 #define FFF(a, b, c, d, e, x, s) \
67 (a) += F((b), (c), (d)) + (x);\
68 (a) = ROL((a), (s)) + (e);\
69 (c) = ROL((c), 10);
70
71 #define GGG(a, b, c, d, e, x, s) \
72 (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
73 (a) = ROL((a), (s)) + (e);\
74 (c) = ROL((c), 10);
75
76 #define HHH(a, b, c, d, e, x, s) \
77 (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
78 (a) = ROL((a), (s)) + (e);\
79 (c) = ROL((c), 10);
80
81 #define III(a, b, c, d, e, x, s) \
82 (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
83 (a) = ROL((a), (s)) + (e);\
84 (c) = ROL((c), 10);
85
86 #define JJJ(a, b, c, d, e, x, s) \
87 (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
88 (a) = ROL((a), (s)) + (e);\
89 (c) = ROL((c), 10);
90
91
92 #ifdef CLEAN_STACK
93 static void _rmd160_compress(hash_state *md, unsigned char *buf)
94 #else
95 static void rmd160_compress(hash_state *md, unsigned char *buf)
96 #endif
97 {
98 ulong32 aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,X[16];
99 int i;
100
101 /* load words X */
102 for (i = 0; i < 16; i++){
103 LOAD32L(X[i], buf + (4 * i));
104 }
105
106 /* load state */
107 aa = aaa = md->rmd160.state[0];
108 bb = bbb = md->rmd160.state[1];
109 cc = ccc = md->rmd160.state[2];
110 dd = ddd = md->rmd160.state[3];
111 ee = eee = md->rmd160.state[4];
112
113 /* round 1 */
114 FF(aa, bb, cc, dd, ee, X[ 0], 11);
115 FF(ee, aa, bb, cc, dd, X[ 1], 14);
116 FF(dd, ee, aa, bb, cc, X[ 2], 15);
117 FF(cc, dd, ee, aa, bb, X[ 3], 12);
118 FF(bb, cc, dd, ee, aa, X[ 4], 5);
119 FF(aa, bb, cc, dd, ee, X[ 5], 8);
120 FF(ee, aa, bb, cc, dd, X[ 6], 7);
121 FF(dd, ee, aa, bb, cc, X[ 7], 9);
122 FF(cc, dd, ee, aa, bb, X[ 8], 11);
123 FF(bb, cc, dd, ee, aa, X[ 9], 13);
124 FF(aa, bb, cc, dd, ee, X[10], 14);
125 FF(ee, aa, bb, cc, dd, X[11], 15);
126 FF(dd, ee, aa, bb, cc, X[12], 6);
127 FF(cc, dd, ee, aa, bb, X[13], 7);
128 FF(bb, cc, dd, ee, aa, X[14], 9);
129 FF(aa, bb, cc, dd, ee, X[15], 8);
130
131 /* round 2 */
132 GG(ee, aa, bb, cc, dd, X[ 7], 7);
133 GG(dd, ee, aa, bb, cc, X[ 4], 6);
134 GG(cc, dd, ee, aa, bb, X[13], 8);
135 GG(bb, cc, dd, ee, aa, X[ 1], 13);
136 GG(aa, bb, cc, dd, ee, X[10], 11);
137 GG(ee, aa, bb, cc, dd, X[ 6], 9);
138 GG(dd, ee, aa, bb, cc, X[15], 7);
139 GG(cc, dd, ee, aa, bb, X[ 3], 15);
140 GG(bb, cc, dd, ee, aa, X[12], 7);
141 GG(aa, bb, cc, dd, ee, X[ 0], 12);
142 GG(ee, aa, bb, cc, dd, X[ 9], 15);
143 GG(dd, ee, aa, bb, cc, X[ 5], 9);
144 GG(cc, dd, ee, aa, bb, X[ 2], 11);
145 GG(bb, cc, dd, ee, aa, X[14], 7);
146 GG(aa, bb, cc, dd, ee, X[11], 13);
147 GG(ee, aa, bb, cc, dd, X[ 8], 12);
148
149 /* round 3 */
150 HH(dd, ee, aa, bb, cc, X[ 3], 11);
151 HH(cc, dd, ee, aa, bb, X[10], 13);
152 HH(bb, cc, dd, ee, aa, X[14], 6);
153 HH(aa, bb, cc, dd, ee, X[ 4], 7);
154 HH(ee, aa, bb, cc, dd, X[ 9], 14);
155 HH(dd, ee, aa, bb, cc, X[15], 9);
156 HH(cc, dd, ee, aa, bb, X[ 8], 13);
157 HH(bb, cc, dd, ee, aa, X[ 1], 15);
158 HH(aa, bb, cc, dd, ee, X[ 2], 14);
159 HH(ee, aa, bb, cc, dd, X[ 7], 8);
160 HH(dd, ee, aa, bb, cc, X[ 0], 13);
161 HH(cc, dd, ee, aa, bb, X[ 6], 6);
162 HH(bb, cc, dd, ee, aa, X[13], 5);
163 HH(aa, bb, cc, dd, ee, X[11], 12);
164 HH(ee, aa, bb, cc, dd, X[ 5], 7);
165 HH(dd, ee, aa, bb, cc, X[12], 5);
166
167 /* round 4 */
168 II(cc, dd, ee, aa, bb, X[ 1], 11);
169 II(bb, cc, dd, ee, aa, X[ 9], 12);
170 II(aa, bb, cc, dd, ee, X[11], 14);
171 II(ee, aa, bb, cc, dd, X[10], 15);
172 II(dd, ee, aa, bb, cc, X[ 0], 14);
173 II(cc, dd, ee, aa, bb, X[ 8], 15);
174 II(bb, cc, dd, ee, aa, X[12], 9);
175 II(aa, bb, cc, dd, ee, X[ 4], 8);
176 II(ee, aa, bb, cc, dd, X[13], 9);
177 II(dd, ee, aa, bb, cc, X[ 3], 14);
178 II(cc, dd, ee, aa, bb, X[ 7], 5);
179 II(bb, cc, dd, ee, aa, X[15], 6);
180 II(aa, bb, cc, dd, ee, X[14], 8);
181 II(ee, aa, bb, cc, dd, X[ 5], 6);
182 II(dd, ee, aa, bb, cc, X[ 6], 5);
183 II(cc, dd, ee, aa, bb, X[ 2], 12);
184
185 /* round 5 */
186 JJ(bb, cc, dd, ee, aa, X[ 4], 9);
187 JJ(aa, bb, cc, dd, ee, X[ 0], 15);
188 JJ(ee, aa, bb, cc, dd, X[ 5], 5);
189 JJ(dd, ee, aa, bb, cc, X[ 9], 11);
190 JJ(cc, dd, ee, aa, bb, X[ 7], 6);
191 JJ(bb, cc, dd, ee, aa, X[12], 8);
192 JJ(aa, bb, cc, dd, ee, X[ 2], 13);
193 JJ(ee, aa, bb, cc, dd, X[10], 12);
194 JJ(dd, ee, aa, bb, cc, X[14], 5);
195 JJ(cc, dd, ee, aa, bb, X[ 1], 12);
196 JJ(bb, cc, dd, ee, aa, X[ 3], 13);
197 JJ(aa, bb, cc, dd, ee, X[ 8], 14);
198 JJ(ee, aa, bb, cc, dd, X[11], 11);
199 JJ(dd, ee, aa, bb, cc, X[ 6], 8);
200 JJ(cc, dd, ee, aa, bb, X[15], 5);
201 JJ(bb, cc, dd, ee, aa, X[13], 6);
202
203 /* parallel round 1 */
204 JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8);
205 JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
206 JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9);
207 JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
208 JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
209 JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
210 JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
211 JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5);
212 JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
213 JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7);
214 JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
215 JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
216 JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
217 JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
218 JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
219 JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
220
221 /* parallel round 2 */
222 III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
223 III(ddd, eee, aaa, bbb, ccc, X[11], 13);
224 III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
225 III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
226 III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
227 III(eee, aaa, bbb, ccc, ddd, X[13], 8);
228 III(ddd, eee, aaa, bbb, ccc, X[ 5], 9);
229 III(ccc, ddd, eee, aaa, bbb, X[10], 11);
230 III(bbb, ccc, ddd, eee, aaa, X[14], 7);
231 III(aaa, bbb, ccc, ddd, eee, X[15], 7);
232 III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
233 III(ddd, eee, aaa, bbb, ccc, X[12], 7);
234 III(ccc, ddd, eee, aaa, bbb, X[ 4], 6);
235 III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
236 III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
237 III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
238
239 /* parallel round 3 */
240 HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
241 HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7);
242 HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
243 HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
244 HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8);
245 HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
246 HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6);
247 HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
248 HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
249 HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
250 HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
251 HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
252 HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
253 HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
254 HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
255 HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
256
257 /* parallel round 4 */
258 GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
259 GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
260 GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
261 GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
262 GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
263 GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
264 GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
265 GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
266 GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6);
267 GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
268 GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
269 GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
270 GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
271 GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5);
272 GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
273 GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
274
275 /* parallel round 5 */
276 FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8);
277 FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5);
278 FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
279 FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9);
280 FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
281 FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5);
282 FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
283 FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6);
284 FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8);
285 FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
286 FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6);
287 FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5);
288 FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
289 FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
290 FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
291 FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
292
293 /* combine results */
294 ddd += cc + md->rmd160.state[1]; /* final result for md->rmd160.state[0] */
295 md->rmd160.state[1] = md->rmd160.state[2] + dd + eee;
296 md->rmd160.state[2] = md->rmd160.state[3] + ee + aaa;
297 md->rmd160.state[3] = md->rmd160.state[4] + aa + bbb;
298 md->rmd160.state[4] = md->rmd160.state[0] + bb + ccc;
299 md->rmd160.state[0] = ddd;
300 }
301
302 #ifdef CLEAN_STACK
303 static void rmd160_compress(hash_state *md, unsigned char *buf)
304 {
305 _rmd160_compress(md, buf);
306 burn_stack(sizeof(ulong32) * 26 + sizeof(int));
307 }
308 #endif
309
310 void rmd160_init(hash_state * md)
311 {
312 _ARGCHK(md != NULL);
313 md->rmd160.state[0] = 0x67452301UL;
314 md->rmd160.state[1] = 0xefcdab89UL;
315 md->rmd160.state[2] = 0x98badcfeUL;
316 md->rmd160.state[3] = 0x10325476UL;
317 md->rmd160.state[4] = 0xc3d2e1f0UL;
318 md->rmd160.curlen = 0;
319 md->rmd160.length = 0;
320 }
321
322 HASH_PROCESS(rmd160_process, rmd160_compress, rmd160, 64)
323
324 int rmd160_done(hash_state * md, unsigned char *hash)
325 {
326 int i;
327
328 _ARGCHK(md != NULL);
329 _ARGCHK(hash != NULL);
330
331 if (md->rmd160.curlen >= sizeof(md->rmd160.buf)) {
332 return CRYPT_INVALID_ARG;
333 }
334
335
336 /* increase the length of the message */
337 md->rmd160.length += md->rmd160.curlen * 8;
338
339 /* append the '1' bit */
340 md->rmd160.buf[md->rmd160.curlen++] = (unsigned char)0x80;
341
342 /* if the length is currently above 56 bytes we append zeros
343 * then compress. Then we can fall back to padding zeros and length
344 * encoding like normal.
345 */
346 if (md->rmd160.curlen > 56) {
347 while (md->rmd160.curlen < 64) {
348 md->rmd160.buf[md->rmd160.curlen++] = (unsigned char)0;
349 }
350 rmd160_compress(md, md->rmd160.buf);
351 md->rmd160.curlen = 0;
352 }
353
354 /* pad upto 56 bytes of zeroes */
355 while (md->rmd160.curlen < 56) {
356 md->rmd160.buf[md->rmd160.curlen++] = (unsigned char)0;
357 }
358
359 /* store length */
360 STORE64L(md->rmd160.length, md->rmd160.buf+56);
361 rmd160_compress(md, md->rmd160.buf);
362
363 /* copy output */
364 for (i = 0; i < 5; i++) {
365 STORE32L(md->rmd160.state[i], hash+(4*i));
366 }
367 #ifdef CLEAN_STACK
368 zeromem(md, sizeof(hash_state));
369 #endif
370 return CRYPT_OK;
371 }
372
373 int rmd160_test(void)
374 {
375 #ifndef LTC_TEST
376 return CRYPT_NOP;
377 #else
378 static const struct {
379 char *msg;
380 unsigned char md[20];
381 } tests[] = {
382 { "",
383 { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28,
384 0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 }
385 },
386 { "a",
387 { 0x0b, 0xdc, 0x9d, 0x2d, 0x25, 0x6b, 0x3e, 0xe9, 0xda, 0xae,
388 0x34, 0x7b, 0xe6, 0xf4, 0xdc, 0x83, 0x5a, 0x46, 0x7f, 0xfe }
389 },
390 { "abc",
391 { 0x8e, 0xb2, 0x08, 0xf7, 0xe0, 0x5d, 0x98, 0x7a, 0x9b, 0x04,
392 0x4a, 0x8e, 0x98, 0xc6, 0xb0, 0x87, 0xf1, 0x5a, 0x0b, 0xfc }
393 },
394 { "message digest",
395 { 0x5d, 0x06, 0x89, 0xef, 0x49, 0xd2, 0xfa, 0xe5, 0x72, 0xb8,
396 0x81, 0xb1, 0x23, 0xa8, 0x5f, 0xfa, 0x21, 0x59, 0x5f, 0x36 }
397 },
398 { "abcdefghijklmnopqrstuvwxyz",
399 { 0xf7, 0x1c, 0x27, 0x10, 0x9c, 0x69, 0x2c, 0x1b, 0x56, 0xbb,
400 0xdc, 0xeb, 0x5b, 0x9d, 0x28, 0x65, 0xb3, 0x70, 0x8d, 0xbc }
401 },
402 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
403 { 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05,
404 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b }
405 }
406 };
407 int x;
408 unsigned char buf[20];
409 hash_state md;
410
411 for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
412 rmd160_init(&md);
413 rmd160_process(&md, (unsigned char *)tests[x].msg, strlen(tests[x].msg));
414 rmd160_done(&md, buf);
415 if (memcmp(buf, tests[x].md, 20) != 0) {
416 #if 0
417 printf("Failed test %d\n", x);
418 #endif
419 return CRYPT_FAIL_TESTVECTOR;
420 }
421 }
422 return CRYPT_OK;
423 #endif
424 }
425
426 #endif
427