2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * Preneel, Bosselaers, Dobbertin, "The Cryptographic Hash Function RIPEMD-160",
26 * RSA Laboratories, CryptoBytes, Volume 3, Number 2, Autumn 1997,
27 * ftp://ftp.rsasecurity.com/pub/cryptobytes/crypto3n2.pdf
34 #include <sys/types.h>
41 #define PUT_64BIT_LE(cp, value) do { \
42 (cp)[7] = (value) >> 56; \
43 (cp)[6] = (value) >> 48; \
44 (cp)[5] = (value) >> 40; \
45 (cp)[4] = (value) >> 32; \
46 (cp)[3] = (value) >> 24; \
47 (cp)[2] = (value) >> 16; \
48 (cp)[1] = (value) >> 8; \
49 (cp)[0] = (value); } while (0)
51 #define PUT_32BIT_LE(cp, value) do { \
52 (cp)[3] = (value) >> 24; \
53 (cp)[2] = (value) >> 16; \
54 (cp)[1] = (value) >> 8; \
55 (cp)[0] = (value); } while (0)
57 #define H0 0x67452301U
58 #define H1 0xEFCDAB89U
59 #define H2 0x98BADCFEU
60 #define H3 0x10325476U
61 #define H4 0xC3D2E1F0U
63 #define K0 0x00000000U
64 #define K1 0x5A827999U
65 #define K2 0x6ED9EBA1U
66 #define K3 0x8F1BBCDCU
67 #define K4 0xA953FD4EU
69 #define KK0 0x50A28BE6U
70 #define KK1 0x5C4DD124U
71 #define KK2 0x6D703EF3U
72 #define KK3 0x7A6D76E9U
73 #define KK4 0x00000000U
75 /* rotate x left n bits. */
76 #define ROL(n, x) (((x) << (n)) | ((x) >> (32-(n))))
78 #define F0(x, y, z) ((x) ^ (y) ^ (z))
79 #define F1(x, y, z) (((x) & (y)) | ((~x) & (z)))
80 #define F2(x, y, z) (((x) | (~y)) ^ (z))
81 #define F3(x, y, z) (((x) & (z)) | ((y) & (~z)))
82 #define F4(x, y, z) ((x) ^ ((y) | (~z)))
84 #define R(a, b, c, d, e, Fj, Kj, sj, rj) \
86 a = ROL(sj, a + Fj(b,c,d) + X(rj) + Kj) + e; \
92 static u_int8_t PADDING[RMD160_BLOCK_LENGTH] = {
93 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
99 RMD160Init(RMD160_CTX *ctx)
110 RMD160Update(RMD160_CTX *ctx, const u_int8_t *input, size_t len)
112 size_t have, off, need;
114 have = (ctx->count / 8) % RMD160_BLOCK_LENGTH;
115 need = RMD160_BLOCK_LENGTH - have;
116 ctx->count += 8 * len;
121 memcpy(ctx->buffer + have, input, need);
122 RMD160Transform(ctx->state, ctx->buffer);
126 /* now the buffer is empty */
127 while (off + RMD160_BLOCK_LENGTH <= len) {
128 RMD160Transform(ctx->state, input+off);
129 off += RMD160_BLOCK_LENGTH;
133 memcpy(ctx->buffer + have, input+off, len-off);
137 RMD160Pad(RMD160_CTX *ctx)
142 PUT_64BIT_LE(size, ctx->count);
145 * pad to RMD160_BLOCK_LENGTH byte blocks, at least one byte from
146 * PADDING plus 8 bytes for the size
148 padlen = RMD160_BLOCK_LENGTH - ((ctx->count / 8) % RMD160_BLOCK_LENGTH);
150 padlen += RMD160_BLOCK_LENGTH;
151 RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
152 RMD160Update(ctx, size, 8);
156 RMD160Final(u_int8_t digest[RMD160_DIGEST_LENGTH], RMD160_CTX *ctx)
161 for (i = 0; i < 5; i++)
162 PUT_32BIT_LE(digest + i*4, ctx->state[i]);
163 memset(ctx, 0, sizeof (*ctx));
167 RMD160Transform(u_int32_t state[5], const u_int8_t block[RMD160_BLOCK_LENGTH])
169 u_int32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16];
171 #if BYTE_ORDER == LITTLE_ENDIAN
172 memcpy(x, block, RMD160_BLOCK_LENGTH);
176 for (i = 0; i < 16; i++)
178 (u_int32_t)(block[i*4 + 0]) |
179 (u_int32_t)(block[i*4 + 1]) << 8 |
180 (u_int32_t)(block[i*4 + 2]) << 16 |
181 (u_int32_t)(block[i*4 + 3]) << 24);
191 R(a, b, c, d, e, F0, K0, 11, 0);
192 R(e, a, b, c, d, F0, K0, 14, 1);
193 R(d, e, a, b, c, F0, K0, 15, 2);
194 R(c, d, e, a, b, F0, K0, 12, 3);
195 R(b, c, d, e, a, F0, K0, 5, 4);
196 R(a, b, c, d, e, F0, K0, 8, 5);
197 R(e, a, b, c, d, F0, K0, 7, 6);
198 R(d, e, a, b, c, F0, K0, 9, 7);
199 R(c, d, e, a, b, F0, K0, 11, 8);
200 R(b, c, d, e, a, F0, K0, 13, 9);
201 R(a, b, c, d, e, F0, K0, 14, 10);
202 R(e, a, b, c, d, F0, K0, 15, 11);
203 R(d, e, a, b, c, F0, K0, 6, 12);
204 R(c, d, e, a, b, F0, K0, 7, 13);
205 R(b, c, d, e, a, F0, K0, 9, 14);
206 R(a, b, c, d, e, F0, K0, 8, 15); /* #15 */
208 R(e, a, b, c, d, F1, K1, 7, 7);
209 R(d, e, a, b, c, F1, K1, 6, 4);
210 R(c, d, e, a, b, F1, K1, 8, 13);
211 R(b, c, d, e, a, F1, K1, 13, 1);
212 R(a, b, c, d, e, F1, K1, 11, 10);
213 R(e, a, b, c, d, F1, K1, 9, 6);
214 R(d, e, a, b, c, F1, K1, 7, 15);
215 R(c, d, e, a, b, F1, K1, 15, 3);
216 R(b, c, d, e, a, F1, K1, 7, 12);
217 R(a, b, c, d, e, F1, K1, 12, 0);
218 R(e, a, b, c, d, F1, K1, 15, 9);
219 R(d, e, a, b, c, F1, K1, 9, 5);
220 R(c, d, e, a, b, F1, K1, 11, 2);
221 R(b, c, d, e, a, F1, K1, 7, 14);
222 R(a, b, c, d, e, F1, K1, 13, 11);
223 R(e, a, b, c, d, F1, K1, 12, 8); /* #31 */
225 R(d, e, a, b, c, F2, K2, 11, 3);
226 R(c, d, e, a, b, F2, K2, 13, 10);
227 R(b, c, d, e, a, F2, K2, 6, 14);
228 R(a, b, c, d, e, F2, K2, 7, 4);
229 R(e, a, b, c, d, F2, K2, 14, 9);
230 R(d, e, a, b, c, F2, K2, 9, 15);
231 R(c, d, e, a, b, F2, K2, 13, 8);
232 R(b, c, d, e, a, F2, K2, 15, 1);
233 R(a, b, c, d, e, F2, K2, 14, 2);
234 R(e, a, b, c, d, F2, K2, 8, 7);
235 R(d, e, a, b, c, F2, K2, 13, 0);
236 R(c, d, e, a, b, F2, K2, 6, 6);
237 R(b, c, d, e, a, F2, K2, 5, 13);
238 R(a, b, c, d, e, F2, K2, 12, 11);
239 R(e, a, b, c, d, F2, K2, 7, 5);
240 R(d, e, a, b, c, F2, K2, 5, 12); /* #47 */
242 R(c, d, e, a, b, F3, K3, 11, 1);
243 R(b, c, d, e, a, F3, K3, 12, 9);
244 R(a, b, c, d, e, F3, K3, 14, 11);
245 R(e, a, b, c, d, F3, K3, 15, 10);
246 R(d, e, a, b, c, F3, K3, 14, 0);
247 R(c, d, e, a, b, F3, K3, 15, 8);
248 R(b, c, d, e, a, F3, K3, 9, 12);
249 R(a, b, c, d, e, F3, K3, 8, 4);
250 R(e, a, b, c, d, F3, K3, 9, 13);
251 R(d, e, a, b, c, F3, K3, 14, 3);
252 R(c, d, e, a, b, F3, K3, 5, 7);
253 R(b, c, d, e, a, F3, K3, 6, 15);
254 R(a, b, c, d, e, F3, K3, 8, 14);
255 R(e, a, b, c, d, F3, K3, 6, 5);
256 R(d, e, a, b, c, F3, K3, 5, 6);
257 R(c, d, e, a, b, F3, K3, 12, 2); /* #63 */
259 R(b, c, d, e, a, F4, K4, 9, 4);
260 R(a, b, c, d, e, F4, K4, 15, 0);
261 R(e, a, b, c, d, F4, K4, 5, 5);
262 R(d, e, a, b, c, F4, K4, 11, 9);
263 R(c, d, e, a, b, F4, K4, 6, 7);
264 R(b, c, d, e, a, F4, K4, 8, 12);
265 R(a, b, c, d, e, F4, K4, 13, 2);
266 R(e, a, b, c, d, F4, K4, 12, 10);
267 R(d, e, a, b, c, F4, K4, 5, 14);
268 R(c, d, e, a, b, F4, K4, 12, 1);
269 R(b, c, d, e, a, F4, K4, 13, 3);
270 R(a, b, c, d, e, F4, K4, 14, 8);
271 R(e, a, b, c, d, F4, K4, 11, 11);
272 R(d, e, a, b, c, F4, K4, 8, 6);
273 R(c, d, e, a, b, F4, K4, 5, 15);
274 R(b, c, d, e, a, F4, K4, 6, 13); /* #79 */
276 aa = a ; bb = b; cc = c; dd = d; ee = e;
284 /* Parallel round 1 */
285 R(a, b, c, d, e, F4, KK0, 8, 5);
286 R(e, a, b, c, d, F4, KK0, 9, 14);
287 R(d, e, a, b, c, F4, KK0, 9, 7);
288 R(c, d, e, a, b, F4, KK0, 11, 0);
289 R(b, c, d, e, a, F4, KK0, 13, 9);
290 R(a, b, c, d, e, F4, KK0, 15, 2);
291 R(e, a, b, c, d, F4, KK0, 15, 11);
292 R(d, e, a, b, c, F4, KK0, 5, 4);
293 R(c, d, e, a, b, F4, KK0, 7, 13);
294 R(b, c, d, e, a, F4, KK0, 7, 6);
295 R(a, b, c, d, e, F4, KK0, 8, 15);
296 R(e, a, b, c, d, F4, KK0, 11, 8);
297 R(d, e, a, b, c, F4, KK0, 14, 1);
298 R(c, d, e, a, b, F4, KK0, 14, 10);
299 R(b, c, d, e, a, F4, KK0, 12, 3);
300 R(a, b, c, d, e, F4, KK0, 6, 12); /* #15 */
301 /* Parallel round 2 */
302 R(e, a, b, c, d, F3, KK1, 9, 6);
303 R(d, e, a, b, c, F3, KK1, 13, 11);
304 R(c, d, e, a, b, F3, KK1, 15, 3);
305 R(b, c, d, e, a, F3, KK1, 7, 7);
306 R(a, b, c, d, e, F3, KK1, 12, 0);
307 R(e, a, b, c, d, F3, KK1, 8, 13);
308 R(d, e, a, b, c, F3, KK1, 9, 5);
309 R(c, d, e, a, b, F3, KK1, 11, 10);
310 R(b, c, d, e, a, F3, KK1, 7, 14);
311 R(a, b, c, d, e, F3, KK1, 7, 15);
312 R(e, a, b, c, d, F3, KK1, 12, 8);
313 R(d, e, a, b, c, F3, KK1, 7, 12);
314 R(c, d, e, a, b, F3, KK1, 6, 4);
315 R(b, c, d, e, a, F3, KK1, 15, 9);
316 R(a, b, c, d, e, F3, KK1, 13, 1);
317 R(e, a, b, c, d, F3, KK1, 11, 2); /* #31 */
318 /* Parallel round 3 */
319 R(d, e, a, b, c, F2, KK2, 9, 15);
320 R(c, d, e, a, b, F2, KK2, 7, 5);
321 R(b, c, d, e, a, F2, KK2, 15, 1);
322 R(a, b, c, d, e, F2, KK2, 11, 3);
323 R(e, a, b, c, d, F2, KK2, 8, 7);
324 R(d, e, a, b, c, F2, KK2, 6, 14);
325 R(c, d, e, a, b, F2, KK2, 6, 6);
326 R(b, c, d, e, a, F2, KK2, 14, 9);
327 R(a, b, c, d, e, F2, KK2, 12, 11);
328 R(e, a, b, c, d, F2, KK2, 13, 8);
329 R(d, e, a, b, c, F2, KK2, 5, 12);
330 R(c, d, e, a, b, F2, KK2, 14, 2);
331 R(b, c, d, e, a, F2, KK2, 13, 10);
332 R(a, b, c, d, e, F2, KK2, 13, 0);
333 R(e, a, b, c, d, F2, KK2, 7, 4);
334 R(d, e, a, b, c, F2, KK2, 5, 13); /* #47 */
335 /* Parallel round 4 */
336 R(c, d, e, a, b, F1, KK3, 15, 8);
337 R(b, c, d, e, a, F1, KK3, 5, 6);
338 R(a, b, c, d, e, F1, KK3, 8, 4);
339 R(e, a, b, c, d, F1, KK3, 11, 1);
340 R(d, e, a, b, c, F1, KK3, 14, 3);
341 R(c, d, e, a, b, F1, KK3, 14, 11);
342 R(b, c, d, e, a, F1, KK3, 6, 15);
343 R(a, b, c, d, e, F1, KK3, 14, 0);
344 R(e, a, b, c, d, F1, KK3, 6, 5);
345 R(d, e, a, b, c, F1, KK3, 9, 12);
346 R(c, d, e, a, b, F1, KK3, 12, 2);
347 R(b, c, d, e, a, F1, KK3, 9, 13);
348 R(a, b, c, d, e, F1, KK3, 12, 9);
349 R(e, a, b, c, d, F1, KK3, 5, 7);
350 R(d, e, a, b, c, F1, KK3, 15, 10);
351 R(c, d, e, a, b, F1, KK3, 8, 14); /* #63 */
352 /* Parallel round 5 */
353 R(b, c, d, e, a, F0, KK4, 8, 12);
354 R(a, b, c, d, e, F0, KK4, 5, 15);
355 R(e, a, b, c, d, F0, KK4, 12, 10);
356 R(d, e, a, b, c, F0, KK4, 9, 4);
357 R(c, d, e, a, b, F0, KK4, 12, 1);
358 R(b, c, d, e, a, F0, KK4, 5, 5);
359 R(a, b, c, d, e, F0, KK4, 14, 8);
360 R(e, a, b, c, d, F0, KK4, 6, 7);
361 R(d, e, a, b, c, F0, KK4, 8, 6);
362 R(c, d, e, a, b, F0, KK4, 13, 2);
363 R(b, c, d, e, a, F0, KK4, 6, 13);
364 R(a, b, c, d, e, F0, KK4, 5, 14);
365 R(e, a, b, c, d, F0, KK4, 15, 0);
366 R(d, e, a, b, c, F0, KK4, 13, 3);
367 R(c, d, e, a, b, F0, KK4, 11, 9);
368 R(b, c, d, e, a, F0, KK4, 11, 11); /* #79 */
370 t = state[1] + cc + d;
371 state[1] = state[2] + dd + e;
372 state[2] = state[3] + ee + a;
373 state[3] = state[4] + aa + b;
374 state[4] = state[0] + bb + c;
378 #endif /* !WITH_OPENSSL */