31 #ifdef HAVE_SYS_ENDIAN_H_ 32 #include <sys/endian.h> 35 #ifdef HAVE_MACHINE_ENDIAN_H_ 36 #include <machine/endian.h> 47 #define _DIAGASSERT(cond) assert(cond) 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))) 64 #define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n)))) 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))) 74 #define FF(a, b, c, d, e, x, s) { \ 75 (a) += F((b), (c), (d)) + (x); \ 76 (a) = ROL((a), (s)) + (e); \ 79 #define GG(a, b, c, d, e, x, s) { \ 80 (a) += G((b), (c), (d)) + (x) + 0x5a827999U; \ 81 (a) = ROL((a), (s)) + (e); \ 84 #define HH(a, b, c, d, e, x, s) { \ 85 (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1U; \ 86 (a) = ROL((a), (s)) + (e); \ 89 #define II(a, b, c, d, e, x, s) { \ 90 (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcU; \ 91 (a) = ROL((a), (s)) + (e); \ 94 #define JJ(a, b, c, d, e, x, s) { \ 95 (a) += J((b), (c), (d)) + (x) + 0xa953fd4eU; \ 96 (a) = ROL((a), (s)) + (e); \ 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); \ 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); \ 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); \ 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); \ 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); \ 134 context->
state[0] = 0x67452301
U;
135 context->
state[1] = 0xefcdab89
U;
136 context->
state[2] = 0x98badcfe
U;
137 context->
state[3] = 0x10325476
U;
138 context->
state[4] = 0xc3d2e1f0
U;
162 FF(aa, bb, cc, dd, ee, block[ 0], 11);
163 FF(ee, aa, bb, cc, dd, block[ 1], 14);
164 FF(dd, ee, aa, bb, cc, block[ 2], 15);
165 FF(cc, dd, ee, aa, bb, block[ 3], 12);
166 FF(bb, cc, dd, ee, aa, block[ 4], 5);
167 FF(aa, bb, cc, dd, ee, block[ 5], 8);
168 FF(ee, aa, bb, cc, dd, block[ 6], 7);
169 FF(dd, ee, aa, bb, cc, block[ 7], 9);
170 FF(cc, dd, ee, aa, bb, block[ 8], 11);
171 FF(bb, cc, dd, ee, aa, block[ 9], 13);
172 FF(aa, bb, cc, dd, ee, block[10], 14);
173 FF(ee, aa, bb, cc, dd, block[11], 15);
174 FF(dd, ee, aa, bb, cc, block[12], 6);
175 FF(cc, dd, ee, aa, bb, block[13], 7);
176 FF(bb, cc, dd, ee, aa, block[14], 9);
177 FF(aa, bb, cc, dd, ee, block[15], 8);
180 GG(ee, aa, bb, cc, dd, block[ 7], 7);
181 GG(dd, ee, aa, bb, cc, block[ 4], 6);
182 GG(cc, dd, ee, aa, bb, block[13], 8);
183 GG(bb, cc, dd, ee, aa, block[ 1], 13);
184 GG(aa, bb, cc, dd, ee, block[10], 11);
185 GG(ee, aa, bb, cc, dd, block[ 6], 9);
186 GG(dd, ee, aa, bb, cc, block[15], 7);
187 GG(cc, dd, ee, aa, bb, block[ 3], 15);
188 GG(bb, cc, dd, ee, aa, block[12], 7);
189 GG(aa, bb, cc, dd, ee, block[ 0], 12);
190 GG(ee, aa, bb, cc, dd, block[ 9], 15);
191 GG(dd, ee, aa, bb, cc, block[ 5], 9);
192 GG(cc, dd, ee, aa, bb, block[ 2], 11);
193 GG(bb, cc, dd, ee, aa, block[14], 7);
194 GG(aa, bb, cc, dd, ee, block[11], 13);
195 GG(ee, aa, bb, cc, dd, block[ 8], 12);
198 HH(dd, ee, aa, bb, cc, block[ 3], 11);
199 HH(cc, dd, ee, aa, bb, block[10], 13);
200 HH(bb, cc, dd, ee, aa, block[14], 6);
201 HH(aa, bb, cc, dd, ee, block[ 4], 7);
202 HH(ee, aa, bb, cc, dd, block[ 9], 14);
203 HH(dd, ee, aa, bb, cc, block[15], 9);
204 HH(cc, dd, ee, aa, bb, block[ 8], 13);
205 HH(bb, cc, dd, ee, aa, block[ 1], 15);
206 HH(aa, bb, cc, dd, ee, block[ 2], 14);
207 HH(ee, aa, bb, cc, dd, block[ 7], 8);
208 HH(dd, ee, aa, bb, cc, block[ 0], 13);
209 HH(cc, dd, ee, aa, bb, block[ 6], 6);
210 HH(bb, cc, dd, ee, aa, block[13], 5);
211 HH(aa, bb, cc, dd, ee, block[11], 12);
212 HH(ee, aa, bb, cc, dd, block[ 5], 7);
213 HH(dd, ee, aa, bb, cc, block[12], 5);
216 II(cc, dd, ee, aa, bb, block[ 1], 11);
217 II(bb, cc, dd, ee, aa, block[ 9], 12);
218 II(aa, bb, cc, dd, ee, block[11], 14);
219 II(ee, aa, bb, cc, dd, block[10], 15);
220 II(dd, ee, aa, bb, cc, block[ 0], 14);
221 II(cc, dd, ee, aa, bb, block[ 8], 15);
222 II(bb, cc, dd, ee, aa, block[12], 9);
223 II(aa, bb, cc, dd, ee, block[ 4], 8);
224 II(ee, aa, bb, cc, dd, block[13], 9);
225 II(dd, ee, aa, bb, cc, block[ 3], 14);
226 II(cc, dd, ee, aa, bb, block[ 7], 5);
227 II(bb, cc, dd, ee, aa, block[15], 6);
228 II(aa, bb, cc, dd, ee, block[14], 8);
229 II(ee, aa, bb, cc, dd, block[ 5], 6);
230 II(dd, ee, aa, bb, cc, block[ 6], 5);
231 II(cc, dd, ee, aa, bb, block[ 2], 12);
234 JJ(bb, cc, dd, ee, aa, block[ 4], 9);
235 JJ(aa, bb, cc, dd, ee, block[ 0], 15);
236 JJ(ee, aa, bb, cc, dd, block[ 5], 5);
237 JJ(dd, ee, aa, bb, cc, block[ 9], 11);
238 JJ(cc, dd, ee, aa, bb, block[ 7], 6);
239 JJ(bb, cc, dd, ee, aa, block[12], 8);
240 JJ(aa, bb, cc, dd, ee, block[ 2], 13);
241 JJ(ee, aa, bb, cc, dd, block[10], 12);
242 JJ(dd, ee, aa, bb, cc, block[14], 5);
243 JJ(cc, dd, ee, aa, bb, block[ 1], 12);
244 JJ(bb, cc, dd, ee, aa, block[ 3], 13);
245 JJ(aa, bb, cc, dd, ee, block[ 8], 14);
246 JJ(ee, aa, bb, cc, dd, block[11], 11);
247 JJ(dd, ee, aa, bb, cc, block[ 6], 8);
248 JJ(cc, dd, ee, aa, bb, block[15], 5);
249 JJ(bb, cc, dd, ee, aa, block[13], 6);
252 JJJ(aaa, bbb, ccc, ddd, eee, block[ 5], 8);
253 JJJ(eee, aaa, bbb, ccc, ddd, block[14], 9);
254 JJJ(ddd, eee, aaa, bbb, ccc, block[ 7], 9);
255 JJJ(ccc, ddd, eee, aaa, bbb, block[ 0], 11);
256 JJJ(bbb, ccc, ddd, eee, aaa, block[ 9], 13);
257 JJJ(aaa, bbb, ccc, ddd, eee, block[ 2], 15);
258 JJJ(eee, aaa, bbb, ccc, ddd, block[11], 15);
259 JJJ(ddd, eee, aaa, bbb, ccc, block[ 4], 5);
260 JJJ(ccc, ddd, eee, aaa, bbb, block[13], 7);
261 JJJ(bbb, ccc, ddd, eee, aaa, block[ 6], 7);
262 JJJ(aaa, bbb, ccc, ddd, eee, block[15], 8);
263 JJJ(eee, aaa, bbb, ccc, ddd, block[ 8], 11);
264 JJJ(ddd, eee, aaa, bbb, ccc, block[ 1], 14);
265 JJJ(ccc, ddd, eee, aaa, bbb, block[10], 14);
266 JJJ(bbb, ccc, ddd, eee, aaa, block[ 3], 12);
267 JJJ(aaa, bbb, ccc, ddd, eee, block[12], 6);
270 III(eee, aaa, bbb, ccc, ddd, block[ 6], 9);
271 III(ddd, eee, aaa, bbb, ccc, block[11], 13);
272 III(ccc, ddd, eee, aaa, bbb, block[ 3], 15);
273 III(bbb, ccc, ddd, eee, aaa, block[ 7], 7);
274 III(aaa, bbb, ccc, ddd, eee, block[ 0], 12);
275 III(eee, aaa, bbb, ccc, ddd, block[13], 8);
276 III(ddd, eee, aaa, bbb, ccc, block[ 5], 9);
277 III(ccc, ddd, eee, aaa, bbb, block[10], 11);
278 III(bbb, ccc, ddd, eee, aaa, block[14], 7);
279 III(aaa, bbb, ccc, ddd, eee, block[15], 7);
280 III(eee, aaa, bbb, ccc, ddd, block[ 8], 12);
281 III(ddd, eee, aaa, bbb, ccc, block[12], 7);
282 III(ccc, ddd, eee, aaa, bbb, block[ 4], 6);
283 III(bbb, ccc, ddd, eee, aaa, block[ 9], 15);
284 III(aaa, bbb, ccc, ddd, eee, block[ 1], 13);
285 III(eee, aaa, bbb, ccc, ddd, block[ 2], 11);
288 HHH(ddd, eee, aaa, bbb, ccc, block[15], 9);
289 HHH(ccc, ddd, eee, aaa, bbb, block[ 5], 7);
290 HHH(bbb, ccc, ddd, eee, aaa, block[ 1], 15);
291 HHH(aaa, bbb, ccc, ddd, eee, block[ 3], 11);
292 HHH(eee, aaa, bbb, ccc, ddd, block[ 7], 8);
293 HHH(ddd, eee, aaa, bbb, ccc, block[14], 6);
294 HHH(ccc, ddd, eee, aaa, bbb, block[ 6], 6);
295 HHH(bbb, ccc, ddd, eee, aaa, block[ 9], 14);
296 HHH(aaa, bbb, ccc, ddd, eee, block[11], 12);
297 HHH(eee, aaa, bbb, ccc, ddd, block[ 8], 13);
298 HHH(ddd, eee, aaa, bbb, ccc, block[12], 5);
299 HHH(ccc, ddd, eee, aaa, bbb, block[ 2], 14);
300 HHH(bbb, ccc, ddd, eee, aaa, block[10], 13);
301 HHH(aaa, bbb, ccc, ddd, eee, block[ 0], 13);
302 HHH(eee, aaa, bbb, ccc, ddd, block[ 4], 7);
303 HHH(ddd, eee, aaa, bbb, ccc, block[13], 5);
306 GGG(ccc, ddd, eee, aaa, bbb, block[ 8], 15);
307 GGG(bbb, ccc, ddd, eee, aaa, block[ 6], 5);
308 GGG(aaa, bbb, ccc, ddd, eee, block[ 4], 8);
309 GGG(eee, aaa, bbb, ccc, ddd, block[ 1], 11);
310 GGG(ddd, eee, aaa, bbb, ccc, block[ 3], 14);
311 GGG(ccc, ddd, eee, aaa, bbb, block[11], 14);
312 GGG(bbb, ccc, ddd, eee, aaa, block[15], 6);
313 GGG(aaa, bbb, ccc, ddd, eee, block[ 0], 14);
314 GGG(eee, aaa, bbb, ccc, ddd, block[ 5], 6);
315 GGG(ddd, eee, aaa, bbb, ccc, block[12], 9);
316 GGG(ccc, ddd, eee, aaa, bbb, block[ 2], 12);
317 GGG(bbb, ccc, ddd, eee, aaa, block[13], 9);
318 GGG(aaa, bbb, ccc, ddd, eee, block[ 9], 12);
319 GGG(eee, aaa, bbb, ccc, ddd, block[ 7], 5);
320 GGG(ddd, eee, aaa, bbb, ccc, block[10], 15);
321 GGG(ccc, ddd, eee, aaa, bbb, block[14], 8);
324 FFF(bbb, ccc, ddd, eee, aaa, block[12] , 8);
325 FFF(aaa, bbb, ccc, ddd, eee, block[15] , 5);
326 FFF(eee, aaa, bbb, ccc, ddd, block[10] , 12);
327 FFF(ddd, eee, aaa, bbb, ccc, block[ 4] , 9);
328 FFF(ccc, ddd, eee, aaa, bbb, block[ 1] , 12);
329 FFF(bbb, ccc, ddd, eee, aaa, block[ 5] , 5);
330 FFF(aaa, bbb, ccc, ddd, eee, block[ 8] , 14);
331 FFF(eee, aaa, bbb, ccc, ddd, block[ 7] , 6);
332 FFF(ddd, eee, aaa, bbb, ccc, block[ 6] , 8);
333 FFF(ccc, ddd, eee, aaa, bbb, block[ 2] , 13);
334 FFF(bbb, ccc, ddd, eee, aaa, block[13] , 6);
335 FFF(aaa, bbb, ccc, ddd, eee, block[14] , 5);
336 FFF(eee, aaa, bbb, ccc, ddd, block[ 0] , 15);
337 FFF(ddd, eee, aaa, bbb, ccc, block[ 3] , 13);
338 FFF(ccc, ddd, eee, aaa, bbb, block[ 9] , 11);
339 FFF(bbb, ccc, ddd, eee, aaa, block[11] , 11);
342 ddd += cc + state[1];
343 state[1] = state[2] + dd + eee;
344 state[2] = state[3] + ee + aaa;
345 state[3] = state[4] + aa + bbb;
346 state[4] = state[0] + bb + ccc;
358 #ifdef WORDS_BIGENDIAN 366 #if SIZEOF_SIZE_T * CHAR_BIT > 32 369 if (context->
length[0] + nbytes < context->length[0])
374 (void)memset(X, 0,
sizeof(X));
376 if ( context->
buflen + nbytes < 64 )
378 (void)memcpy(context->
bbuffer + context->
buflen, data, nbytes);
384 ofs = 64 - context->
buflen;
386 #ifndef WORDS_BIGENDIAN 387 (void)memcpy(X, context->
bbuffer,
sizeof(X));
389 for (j=0; j < 16; j++)
396 for (i = 0; i < (nbytes >> 6); i++) {
397 #ifndef WORDS_BIGENDIAN 398 (void)memcpy(X, data + (64 * i) + ofs,
sizeof(X));
400 for (j=0; j < 16; j++)
410 memcpy(context->
bbuffer, data + (64 * i) + ofs, context->
buflen);
421 #ifdef WORDS_BIGENDIAN 433 #ifndef WORDS_BIGENDIAN 434 (void)memcpy(X, context->
bbuffer,
sizeof(X));
436 for (j=0; j < 16; j++)
439 if ((context->
buflen) > 55) {
442 (void)memset(X, 0,
sizeof(X));
446 X[14] = context->
length[0] << 3;
447 X[15] = (context->
length[0] >> 29) |
448 (context->
length[1] << 3);
451 if (digest !=
NULL) {
452 for (i = 0; i < 20; i += 4) {
454 digest[i] = context->
state[i>>2];
455 digest[i + 1] = (context->
state[i>>2] >> 8);
456 digest[i + 2] = (context->
state[i>>2] >> 16);
457 digest[i + 3] = (context->
state[i>>2] >> 24);
#define II(a, b, c, d, e, x, s)
#define JJJ(a, b, c, d, e, x, s)
#define GGG(a, b, c, d, e, x, s)
#define GG(a, b, c, d, e, x, s)
#define III(a, b, c, d, e, x, s)
#define BYTES_TO_DWORD(strptr)
void RMD160_Transform(uint32_t state[5], const uint32_t block[16])
#define FF(a, b, c, d, e, x, s)
void RMD160_Update(RMD160_CTX *context, const uint8_t *data, size_t nbytes)
int RMD160_Finish(RMD160_CTX *context, uint8_t digest[20])
#define _DIAGASSERT(cond)
#define HH(a, b, c, d, e, x, s)
#define JJ(a, b, c, d, e, x, s)
#define FFF(a, b, c, d, e, x, s)
#define HHH(a, b, c, d, e, x, s)
int RMD160_Init(RMD160_CTX *context)