DEFINITIONS
This source file includes following functions.
- RMD160_Init
- RMD160_Transform
- RMD160_Update
- RMD160_Final
- RMD160_Equal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include "rmd160.h"
24
25 #ifndef lint
26
27 #endif
28
29
30
31 #ifdef HAVE_SYS_ENDIAN_H_
32 #include <sys/endian.h>
33 #endif
34
35 #ifdef HAVE_MACHINE_ENDIAN_H_
36 #include <machine/endian.h>
37 #endif
38
39
40
41 #include <assert.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #ifndef _DIAGASSERT
47 #define _DIAGASSERT(cond) assert(cond)
48 #endif
49
50
51
52
53
54
55
56 #define BYTES_TO_DWORD(strptr) \
57 (((uint32_t) *((strptr)+3) << 24) | \
58 ((uint32_t) *((strptr)+2) << 16) | \
59 ((uint32_t) *((strptr)+1) << 8) | \
60 ((uint32_t) *(strptr)))
61
62
63
64 #define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
65
66
67 #define F(x, y, z) ((x) ^ (y) ^ (z))
68 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
69 #define H(x, y, z) (((x) | ~(y)) ^ (z))
70 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
71 #define J(x, y, z) ((x) ^ ((y) | ~(z)))
72
73
74 #define FF(a, b, c, d, e, x, s) { \
75 (a) += F((b), (c), (d)) + (x); \
76 (a) = ROL((a), (s)) + (e); \
77 (c) = ROL((c), 10); \
78 }
79 #define GG(a, b, c, d, e, x, s) { \
80 (a) += G((b), (c), (d)) + (x) + 0x5a827999U; \
81 (a) = ROL((a), (s)) + (e); \
82 (c) = ROL((c), 10); \
83 }
84 #define HH(a, b, c, d, e, x, s) { \
85 (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1U; \
86 (a) = ROL((a), (s)) + (e); \
87 (c) = ROL((c), 10); \
88 }
89 #define II(a, b, c, d, e, x, s) { \
90 (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcU; \
91 (a) = ROL((a), (s)) + (e); \
92 (c) = ROL((c), 10); \
93 }
94 #define JJ(a, b, c, d, e, x, s) { \
95 (a) += J((b), (c), (d)) + (x) + 0xa953fd4eU; \
96 (a) = ROL((a), (s)) + (e); \
97 (c) = ROL((c), 10); \
98 }
99 #define FFF(a, b, c, d, e, x, s) { \
100 (a) += F((b), (c), (d)) + (x); \
101 (a) = ROL((a), (s)) + (e); \
102 (c) = ROL((c), 10); \
103 }
104 #define GGG(a, b, c, d, e, x, s) { \
105 (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9U; \
106 (a) = ROL((a), (s)) + (e); \
107 (c) = ROL((c), 10); \
108 }
109 #define HHH(a, b, c, d, e, x, s) { \
110 (a) += H((b), (c), (d)) + (x) + 0x6d703ef3U; \
111 (a) = ROL((a), (s)) + (e); \
112 (c) = ROL((c), 10); \
113 }
114 #define III(a, b, c, d, e, x, s) { \
115 (a) += I((b), (c), (d)) + (x) + 0x5c4dd124U; \
116 (a) = ROL((a), (s)) + (e); \
117 (c) = ROL((c), 10); \
118 }
119 #define JJJ(a, b, c, d, e, x, s) { \
120 (a) += J((b), (c), (d)) + (x) + 0x50a28be6U; \
121 (a) = ROL((a), (s)) + (e); \
122 (c) = ROL((c), 10); \
123 }
124
125
126
127 void
128 RMD160_Init(RMD160_CTX *context)
129 {
130
131 _DIAGASSERT(context != NULL);
132
133
134 context->state[0] = 0x67452301U;
135 context->state[1] = 0xefcdab89U;
136 context->state[2] = 0x98badcfeU;
137 context->state[3] = 0x10325476U;
138 context->state[4] = 0xc3d2e1f0U;
139 context->length[0] = context->length[1] = 0;
140 context->buflen = 0;
141 }
142
143
144
145 void
146 RMD160_Transform(uint32_t state[5], const uint32_t block[16])
147 {
148 uint32_t aa, bb, cc, dd, ee;
149 uint32_t aaa, bbb, ccc, ddd, eee;
150
151 _DIAGASSERT(state != NULL);
152 _DIAGASSERT(block != NULL);
153
154 aa = aaa = state[0];
155 bb = bbb = state[1];
156 cc = ccc = state[2];
157 dd = ddd = state[3];
158 ee = eee = state[4];
159
160
161 FF(aa, bb, cc, dd, ee, block[ 0], 11);
162 FF(ee, aa, bb, cc, dd, block[ 1], 14);
163 FF(dd, ee, aa, bb, cc, block[ 2], 15);
164 FF(cc, dd, ee, aa, bb, block[ 3], 12);
165 FF(bb, cc, dd, ee, aa, block[ 4], 5);
166 FF(aa, bb, cc, dd, ee, block[ 5], 8);
167 FF(ee, aa, bb, cc, dd, block[ 6], 7);
168 FF(dd, ee, aa, bb, cc, block[ 7], 9);
169 FF(cc, dd, ee, aa, bb, block[ 8], 11);
170 FF(bb, cc, dd, ee, aa, block[ 9], 13);
171 FF(aa, bb, cc, dd, ee, block[10], 14);
172 FF(ee, aa, bb, cc, dd, block[11], 15);
173 FF(dd, ee, aa, bb, cc, block[12], 6);
174 FF(cc, dd, ee, aa, bb, block[13], 7);
175 FF(bb, cc, dd, ee, aa, block[14], 9);
176 FF(aa, bb, cc, dd, ee, block[15], 8);
177
178
179 GG(ee, aa, bb, cc, dd, block[ 7], 7);
180 GG(dd, ee, aa, bb, cc, block[ 4], 6);
181 GG(cc, dd, ee, aa, bb, block[13], 8);
182 GG(bb, cc, dd, ee, aa, block[ 1], 13);
183 GG(aa, bb, cc, dd, ee, block[10], 11);
184 GG(ee, aa, bb, cc, dd, block[ 6], 9);
185 GG(dd, ee, aa, bb, cc, block[15], 7);
186 GG(cc, dd, ee, aa, bb, block[ 3], 15);
187 GG(bb, cc, dd, ee, aa, block[12], 7);
188 GG(aa, bb, cc, dd, ee, block[ 0], 12);
189 GG(ee, aa, bb, cc, dd, block[ 9], 15);
190 GG(dd, ee, aa, bb, cc, block[ 5], 9);
191 GG(cc, dd, ee, aa, bb, block[ 2], 11);
192 GG(bb, cc, dd, ee, aa, block[14], 7);
193 GG(aa, bb, cc, dd, ee, block[11], 13);
194 GG(ee, aa, bb, cc, dd, block[ 8], 12);
195
196
197 HH(dd, ee, aa, bb, cc, block[ 3], 11);
198 HH(cc, dd, ee, aa, bb, block[10], 13);
199 HH(bb, cc, dd, ee, aa, block[14], 6);
200 HH(aa, bb, cc, dd, ee, block[ 4], 7);
201 HH(ee, aa, bb, cc, dd, block[ 9], 14);
202 HH(dd, ee, aa, bb, cc, block[15], 9);
203 HH(cc, dd, ee, aa, bb, block[ 8], 13);
204 HH(bb, cc, dd, ee, aa, block[ 1], 15);
205 HH(aa, bb, cc, dd, ee, block[ 2], 14);
206 HH(ee, aa, bb, cc, dd, block[ 7], 8);
207 HH(dd, ee, aa, bb, cc, block[ 0], 13);
208 HH(cc, dd, ee, aa, bb, block[ 6], 6);
209 HH(bb, cc, dd, ee, aa, block[13], 5);
210 HH(aa, bb, cc, dd, ee, block[11], 12);
211 HH(ee, aa, bb, cc, dd, block[ 5], 7);
212 HH(dd, ee, aa, bb, cc, block[12], 5);
213
214
215 II(cc, dd, ee, aa, bb, block[ 1], 11);
216 II(bb, cc, dd, ee, aa, block[ 9], 12);
217 II(aa, bb, cc, dd, ee, block[11], 14);
218 II(ee, aa, bb, cc, dd, block[10], 15);
219 II(dd, ee, aa, bb, cc, block[ 0], 14);
220 II(cc, dd, ee, aa, bb, block[ 8], 15);
221 II(bb, cc, dd, ee, aa, block[12], 9);
222 II(aa, bb, cc, dd, ee, block[ 4], 8);
223 II(ee, aa, bb, cc, dd, block[13], 9);
224 II(dd, ee, aa, bb, cc, block[ 3], 14);
225 II(cc, dd, ee, aa, bb, block[ 7], 5);
226 II(bb, cc, dd, ee, aa, block[15], 6);
227 II(aa, bb, cc, dd, ee, block[14], 8);
228 II(ee, aa, bb, cc, dd, block[ 5], 6);
229 II(dd, ee, aa, bb, cc, block[ 6], 5);
230 II(cc, dd, ee, aa, bb, block[ 2], 12);
231
232
233 JJ(bb, cc, dd, ee, aa, block[ 4], 9);
234 JJ(aa, bb, cc, dd, ee, block[ 0], 15);
235 JJ(ee, aa, bb, cc, dd, block[ 5], 5);
236 JJ(dd, ee, aa, bb, cc, block[ 9], 11);
237 JJ(cc, dd, ee, aa, bb, block[ 7], 6);
238 JJ(bb, cc, dd, ee, aa, block[12], 8);
239 JJ(aa, bb, cc, dd, ee, block[ 2], 13);
240 JJ(ee, aa, bb, cc, dd, block[10], 12);
241 JJ(dd, ee, aa, bb, cc, block[14], 5);
242 JJ(cc, dd, ee, aa, bb, block[ 1], 12);
243 JJ(bb, cc, dd, ee, aa, block[ 3], 13);
244 JJ(aa, bb, cc, dd, ee, block[ 8], 14);
245 JJ(ee, aa, bb, cc, dd, block[11], 11);
246 JJ(dd, ee, aa, bb, cc, block[ 6], 8);
247 JJ(cc, dd, ee, aa, bb, block[15], 5);
248 JJ(bb, cc, dd, ee, aa, block[13], 6);
249
250
251 JJJ(aaa, bbb, ccc, ddd, eee, block[ 5], 8);
252 JJJ(eee, aaa, bbb, ccc, ddd, block[14], 9);
253 JJJ(ddd, eee, aaa, bbb, ccc, block[ 7], 9);
254 JJJ(ccc, ddd, eee, aaa, bbb, block[ 0], 11);
255 JJJ(bbb, ccc, ddd, eee, aaa, block[ 9], 13);
256 JJJ(aaa, bbb, ccc, ddd, eee, block[ 2], 15);
257 JJJ(eee, aaa, bbb, ccc, ddd, block[11], 15);
258 JJJ(ddd, eee, aaa, bbb, ccc, block[ 4], 5);
259 JJJ(ccc, ddd, eee, aaa, bbb, block[13], 7);
260 JJJ(bbb, ccc, ddd, eee, aaa, block[ 6], 7);
261 JJJ(aaa, bbb, ccc, ddd, eee, block[15], 8);
262 JJJ(eee, aaa, bbb, ccc, ddd, block[ 8], 11);
263 JJJ(ddd, eee, aaa, bbb, ccc, block[ 1], 14);
264 JJJ(ccc, ddd, eee, aaa, bbb, block[10], 14);
265 JJJ(bbb, ccc, ddd, eee, aaa, block[ 3], 12);
266 JJJ(aaa, bbb, ccc, ddd, eee, block[12], 6);
267
268
269 III(eee, aaa, bbb, ccc, ddd, block[ 6], 9);
270 III(ddd, eee, aaa, bbb, ccc, block[11], 13);
271 III(ccc, ddd, eee, aaa, bbb, block[ 3], 15);
272 III(bbb, ccc, ddd, eee, aaa, block[ 7], 7);
273 III(aaa, bbb, ccc, ddd, eee, block[ 0], 12);
274 III(eee, aaa, bbb, ccc, ddd, block[13], 8);
275 III(ddd, eee, aaa, bbb, ccc, block[ 5], 9);
276 III(ccc, ddd, eee, aaa, bbb, block[10], 11);
277 III(bbb, ccc, ddd, eee, aaa, block[14], 7);
278 III(aaa, bbb, ccc, ddd, eee, block[15], 7);
279 III(eee, aaa, bbb, ccc, ddd, block[ 8], 12);
280 III(ddd, eee, aaa, bbb, ccc, block[12], 7);
281 III(ccc, ddd, eee, aaa, bbb, block[ 4], 6);
282 III(bbb, ccc, ddd, eee, aaa, block[ 9], 15);
283 III(aaa, bbb, ccc, ddd, eee, block[ 1], 13);
284 III(eee, aaa, bbb, ccc, ddd, block[ 2], 11);
285
286
287 HHH(ddd, eee, aaa, bbb, ccc, block[15], 9);
288 HHH(ccc, ddd, eee, aaa, bbb, block[ 5], 7);
289 HHH(bbb, ccc, ddd, eee, aaa, block[ 1], 15);
290 HHH(aaa, bbb, ccc, ddd, eee, block[ 3], 11);
291 HHH(eee, aaa, bbb, ccc, ddd, block[ 7], 8);
292 HHH(ddd, eee, aaa, bbb, ccc, block[14], 6);
293 HHH(ccc, ddd, eee, aaa, bbb, block[ 6], 6);
294 HHH(bbb, ccc, ddd, eee, aaa, block[ 9], 14);
295 HHH(aaa, bbb, ccc, ddd, eee, block[11], 12);
296 HHH(eee, aaa, bbb, ccc, ddd, block[ 8], 13);
297 HHH(ddd, eee, aaa, bbb, ccc, block[12], 5);
298 HHH(ccc, ddd, eee, aaa, bbb, block[ 2], 14);
299 HHH(bbb, ccc, ddd, eee, aaa, block[10], 13);
300 HHH(aaa, bbb, ccc, ddd, eee, block[ 0], 13);
301 HHH(eee, aaa, bbb, ccc, ddd, block[ 4], 7);
302 HHH(ddd, eee, aaa, bbb, ccc, block[13], 5);
303
304
305 GGG(ccc, ddd, eee, aaa, bbb, block[ 8], 15);
306 GGG(bbb, ccc, ddd, eee, aaa, block[ 6], 5);
307 GGG(aaa, bbb, ccc, ddd, eee, block[ 4], 8);
308 GGG(eee, aaa, bbb, ccc, ddd, block[ 1], 11);
309 GGG(ddd, eee, aaa, bbb, ccc, block[ 3], 14);
310 GGG(ccc, ddd, eee, aaa, bbb, block[11], 14);
311 GGG(bbb, ccc, ddd, eee, aaa, block[15], 6);
312 GGG(aaa, bbb, ccc, ddd, eee, block[ 0], 14);
313 GGG(eee, aaa, bbb, ccc, ddd, block[ 5], 6);
314 GGG(ddd, eee, aaa, bbb, ccc, block[12], 9);
315 GGG(ccc, ddd, eee, aaa, bbb, block[ 2], 12);
316 GGG(bbb, ccc, ddd, eee, aaa, block[13], 9);
317 GGG(aaa, bbb, ccc, ddd, eee, block[ 9], 12);
318 GGG(eee, aaa, bbb, ccc, ddd, block[ 7], 5);
319 GGG(ddd, eee, aaa, bbb, ccc, block[10], 15);
320 GGG(ccc, ddd, eee, aaa, bbb, block[14], 8);
321
322
323 FFF(bbb, ccc, ddd, eee, aaa, block[12] , 8);
324 FFF(aaa, bbb, ccc, ddd, eee, block[15] , 5);
325 FFF(eee, aaa, bbb, ccc, ddd, block[10] , 12);
326 FFF(ddd, eee, aaa, bbb, ccc, block[ 4] , 9);
327 FFF(ccc, ddd, eee, aaa, bbb, block[ 1] , 12);
328 FFF(bbb, ccc, ddd, eee, aaa, block[ 5] , 5);
329 FFF(aaa, bbb, ccc, ddd, eee, block[ 8] , 14);
330 FFF(eee, aaa, bbb, ccc, ddd, block[ 7] , 6);
331 FFF(ddd, eee, aaa, bbb, ccc, block[ 6] , 8);
332 FFF(ccc, ddd, eee, aaa, bbb, block[ 2] , 13);
333 FFF(bbb, ccc, ddd, eee, aaa, block[13] , 6);
334 FFF(aaa, bbb, ccc, ddd, eee, block[14] , 5);
335 FFF(eee, aaa, bbb, ccc, ddd, block[ 0] , 15);
336 FFF(ddd, eee, aaa, bbb, ccc, block[ 3] , 13);
337 FFF(ccc, ddd, eee, aaa, bbb, block[ 9] , 11);
338 FFF(bbb, ccc, ddd, eee, aaa, block[11] , 11);
339
340
341 ddd += cc + state[1];
342 state[1] = state[2] + dd + eee;
343 state[2] = state[3] + ee + aaa;
344 state[3] = state[4] + aa + bbb;
345 state[4] = state[0] + bb + ccc;
346 state[0] = ddd;
347 }
348
349
350
351 void
352 RMD160_Update(RMD160_CTX *context, const uint8_t *data, size_t nbytes)
353 {
354 uint32_t X[16];
355 uint32_t ofs = 0;
356 uint32_t i;
357 #ifdef WORDS_BIGENDIAN
358 uint32_t j;
359 #endif
360
361 _DIAGASSERT(context != NULL);
362 _DIAGASSERT(data != NULL);
363
364
365 if (context->length[0] + nbytes < context->length[0])
366 context->length[1]++;
367 context->length[0] += nbytes;
368
369 (void)memset(X, 0, sizeof(X));
370
371 if ( context->buflen + nbytes < 64 )
372 {
373 (void)memcpy(context->bbuffer + context->buflen, data, nbytes);
374 context->buflen += nbytes;
375 }
376 else
377 {
378
379 ofs = 64 - context->buflen;
380 (void)memcpy(context->bbuffer + context->buflen, data, ofs);
381 #ifndef WORDS_BIGENDIAN
382 (void)memcpy(X, context->bbuffer, sizeof(X));
383 #else
384 for (j=0; j < 16; j++)
385 X[j] = BYTES_TO_DWORD(context->bbuffer + (4 * j));
386 #endif
387 RMD160_Transform(context->state, X);
388 nbytes -= ofs;
389
390
391 for (i = 0; i < (nbytes >> 6); i++) {
392 #ifndef WORDS_BIGENDIAN
393 (void)memcpy(X, data + (64 * i) + ofs, sizeof(X));
394 #else
395 for (j=0; j < 16; j++)
396 X[j] = BYTES_TO_DWORD(data + (64 * i) + (4 * j) + ofs);
397 #endif
398 RMD160_Transform(context->state, X);
399 }
400
401
402
403
404 context->buflen = nbytes & 63;
405 memcpy(context->bbuffer, data + (64 * i) + ofs, context->buflen);
406 }
407 }
408
409
410
411 void
412 RMD160_Final(uint8_t digest[20], RMD160_CTX *context)
413 {
414 uint32_t i;
415 uint32_t X[16];
416 #ifdef WORDS_BIGENDIAN
417 uint32_t j;
418 #endif
419
420 _DIAGASSERT(digest != NULL);
421 _DIAGASSERT(context != NULL);
422
423
424 context->bbuffer[context->buflen] = (uint8_t)'\200';
425
426 (void)memset(context->bbuffer + context->buflen + 1, 0,
427 63 - context->buflen);
428 #ifndef WORDS_BIGENDIAN
429 (void)memcpy(X, context->bbuffer, sizeof(X));
430 #else
431 for (j=0; j < 16; j++)
432 X[j] = BYTES_TO_DWORD(context->bbuffer + (4 * j));
433 #endif
434 if ((context->buflen) > 55) {
435
436 RMD160_Transform(context->state, X);
437 (void)memset(X, 0, sizeof(X));
438 }
439
440
441 X[14] = context->length[0] << 3;
442 X[15] = (context->length[0] >> 29) |
443 (context->length[1] << 3);
444 RMD160_Transform(context->state, X);
445
446 if (digest != NULL) {
447 for (i = 0; i < 20; i += 4) {
448
449 digest[i] = context->state[i>>2];
450 digest[i + 1] = (context->state[i>>2] >> 8);
451 digest[i + 2] = (context->state[i>>2] >> 16);
452 digest[i + 3] = (context->state[i>>2] >> 24);
453 }
454 }
455 }
456
457 int RMD160_Equal(RMD160_CTX* pctx1, RMD160_CTX* pctx2) {
458 return pctx1->buflen == pctx2->buflen
459 && memcmp(pctx1->length, pctx2->length, sizeof(pctx1->length)) == 0
460 && memcmp(pctx1->state, pctx2->state, sizeof(pctx1->state)) == 0
461 && memcmp(pctx1->bbuffer, pctx2->bbuffer, sizeof(pctx1->bbuffer)) == 0;
462 }
463
464