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