rmd160.c revision 1.1 1 /* $NetBSD: rmd160.c,v 1.1 2006/10/27 21:23:16 christos Exp $ */
2 /* $KAME: rmd160.c,v 1.2 2003/07/25 09:37:55 itojun Exp $ */
3 /* $OpenBSD: rmd160.c,v 1.3 2001/09/26 21:40:13 markus Exp $ */
4 /*
5 * Copyright (c) 2001 Markus Friedl. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*
28 * Preneel, Bosselaers, Dobbertin, "The Cryptographic Hash Function RIPEMD-160",
29 * RSA Laboratories, CryptoBytes, Volume 3, Number 2, Autumn 1997,
30 * ftp://ftp.rsasecurity.com/pub/cryptobytes/crypto3n2.pdf
31 */
32
33 #include <sys/cdefs.h>
34
35 #if defined(_KERNEL) || defined(_STANDALONE)
36 __KERNEL_RCSID(0, "$NetBSD: rmd160.c,v 1.1 2006/10/27 21:23:16 christos Exp $");
37
38 #include <lib/libkern/libkern.h>
39
40 #else
41
42 #if defined(LIBC_SCCS) && !defined(lint)
43 __RCSID("$NetBSD: rmd160.c,v 1.1 2006/10/27 21:23:16 christos Exp $");
44 #endif /* LIBC_SCCS and not lint */
45
46 #include "namespace.h"
47 #include <assert.h>
48 #include <string.h>
49
50 #endif
51
52 #include <sys/types.h>
53 #include <sys/rmd160.h>
54
55
56 #define PUT_64BIT_LE(cp, value) do { \
57 (cp)[7] = (u_char)((value) >> 56); \
58 (cp)[6] = (u_char)((value) >> 48); \
59 (cp)[5] = (u_char)((value) >> 40); \
60 (cp)[4] = (u_char)((value) >> 32); \
61 (cp)[3] = (u_char)((value) >> 24); \
62 (cp)[2] = (u_char)((value) >> 16); \
63 (cp)[1] = (u_char)((value) >> 8); \
64 (cp)[0] = (u_char)((value)); } while (/*CONSTCOND*/0)
65
66 #define PUT_32BIT_LE(cp, value) do { \
67 (cp)[3] = (value) >> 24; \
68 (cp)[2] = (value) >> 16; \
69 (cp)[1] = (value) >> 8; \
70 (cp)[0] = (value); } while (/*CONSTCOND*/0)
71
72 #define H0 0x67452301U
73 #define H1 0xEFCDAB89U
74 #define H2 0x98BADCFEU
75 #define H3 0x10325476U
76 #define H4 0xC3D2E1F0U
77
78 #define K0 0x00000000U
79 #define K1 0x5A827999U
80 #define K2 0x6ED9EBA1U
81 #define K3 0x8F1BBCDCU
82 #define K4 0xA953FD4EU
83
84 #define KK0 0x50A28BE6U
85 #define KK1 0x5C4DD124U
86 #define KK2 0x6D703EF3U
87 #define KK3 0x7A6D76E9U
88 #define KK4 0x00000000U
89
90 /* rotate x left n bits. */
91 #define ROL(n, x) (((x) << (n)) | ((x) >> (32-(n))))
92
93 #define F0(x, y, z) ((x) ^ (y) ^ (z))
94 #define F1(x, y, z) (((x) & (y)) | ((~x) & (z)))
95 #define F2(x, y, z) (((x) | (~y)) ^ (z))
96 #define F3(x, y, z) (((x) & (z)) | ((y) & (~z)))
97 #define F4(x, y, z) ((x) ^ ((y) | (~z)))
98
99 #define R(a, b, c, d, e, Fj, Kj, sj, rj) \
100 do { \
101 a = ROL(sj, a + Fj(b,c,d) + X(rj) + Kj) + e; \
102 c = ROL(10, c); \
103 } while(/*CONSTCOND*/0)
104
105 #define X(i) x[i]
106
107 static const u_char PADDING[64] = {
108 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
111 };
112
113 #if !defined(_KERNEL) && defined(__weak_alias)
114 __weak_alias(RMD160Init,_RMD160Init)
115 __weak_alias(RMD160Update,_RMD160Update)
116 __weak_alias(RMD160Final,_RMD160Final)
117 __weak_alias(RMD160Transform,_RMD160Transform)
118 #endif
119
120 void
121 RMD160Init(RMD160_CTX *ctx)
122 {
123 ctx->count = 0;
124 ctx->state[0] = H0;
125 ctx->state[1] = H1;
126 ctx->state[2] = H2;
127 ctx->state[3] = H3;
128 ctx->state[4] = H4;
129 }
130
131 void
132 RMD160Update(RMD160_CTX *ctx, const u_char *input, u_int32_t len)
133 {
134 u_int32_t have, off, need;
135
136 have = (u_int32_t)((ctx->count/8) % 64);
137 need = 64 - have;
138 ctx->count += 8 * len;
139 off = 0;
140
141 if (len >= need) {
142 if (have) {
143 memcpy(ctx->buffer + have, input, (size_t)need);
144 RMD160Transform(ctx->state, ctx->buffer);
145 off = need;
146 have = 0;
147 }
148 /* now the buffer is empty */
149 while (off + 64 <= len) {
150 RMD160Transform(ctx->state, input+off);
151 off += 64;
152 }
153 }
154 if (off < len)
155 memcpy(ctx->buffer + have, input+off, (size_t)len-off);
156 }
157
158 void
159 RMD160Final(u_char digest[20], RMD160_CTX *ctx)
160 {
161 int i;
162 u_char size[8];
163 u_int32_t padlen;
164
165 PUT_64BIT_LE(size, ctx->count);
166
167 /*
168 * pad to 64 byte blocks, at least one byte from PADDING plus 8 bytes
169 * for the size
170 */
171 padlen = (u_int32_t)(64 - ((ctx->count/8) % 64));
172 if (padlen < 1 + 8)
173 padlen += 64;
174 RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
175 RMD160Update(ctx, size, 8);
176
177 if (digest != NULL)
178 for (i = 0; i < 5; i++)
179 PUT_32BIT_LE(digest + i*4, ctx->state[i]);
180
181 memset(ctx, 0, sizeof (*ctx));
182 }
183
184 void
185 RMD160Transform(u_int32_t state[5], const u_char block[64])
186 {
187 u_int32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16];
188
189 #if BYTE_ORDER == LITTLE_ENDIAN
190 memcpy(x, block, (size_t)64);
191 #else
192 int i;
193
194 for (i = 0; i < 16; i++)
195 x[i] = le32toh(*(const u_int32_t*)(block+i*4));
196 #endif
197
198 a = state[0];
199 b = state[1];
200 c = state[2];
201 d = state[3];
202 e = state[4];
203
204 /* Round 1 */
205 R(a, b, c, d, e, F0, K0, 11, 0);
206 R(e, a, b, c, d, F0, K0, 14, 1);
207 R(d, e, a, b, c, F0, K0, 15, 2);
208 R(c, d, e, a, b, F0, K0, 12, 3);
209 R(b, c, d, e, a, F0, K0, 5, 4);
210 R(a, b, c, d, e, F0, K0, 8, 5);
211 R(e, a, b, c, d, F0, K0, 7, 6);
212 R(d, e, a, b, c, F0, K0, 9, 7);
213 R(c, d, e, a, b, F0, K0, 11, 8);
214 R(b, c, d, e, a, F0, K0, 13, 9);
215 R(a, b, c, d, e, F0, K0, 14, 10);
216 R(e, a, b, c, d, F0, K0, 15, 11);
217 R(d, e, a, b, c, F0, K0, 6, 12);
218 R(c, d, e, a, b, F0, K0, 7, 13);
219 R(b, c, d, e, a, F0, K0, 9, 14);
220 R(a, b, c, d, e, F0, K0, 8, 15); /* #15 */
221 /* Round 2 */
222 R(e, a, b, c, d, F1, K1, 7, 7);
223 R(d, e, a, b, c, F1, K1, 6, 4);
224 R(c, d, e, a, b, F1, K1, 8, 13);
225 R(b, c, d, e, a, F1, K1, 13, 1);
226 R(a, b, c, d, e, F1, K1, 11, 10);
227 R(e, a, b, c, d, F1, K1, 9, 6);
228 R(d, e, a, b, c, F1, K1, 7, 15);
229 R(c, d, e, a, b, F1, K1, 15, 3);
230 R(b, c, d, e, a, F1, K1, 7, 12);
231 R(a, b, c, d, e, F1, K1, 12, 0);
232 R(e, a, b, c, d, F1, K1, 15, 9);
233 R(d, e, a, b, c, F1, K1, 9, 5);
234 R(c, d, e, a, b, F1, K1, 11, 2);
235 R(b, c, d, e, a, F1, K1, 7, 14);
236 R(a, b, c, d, e, F1, K1, 13, 11);
237 R(e, a, b, c, d, F1, K1, 12, 8); /* #31 */
238 /* Round 3 */
239 R(d, e, a, b, c, F2, K2, 11, 3);
240 R(c, d, e, a, b, F2, K2, 13, 10);
241 R(b, c, d, e, a, F2, K2, 6, 14);
242 R(a, b, c, d, e, F2, K2, 7, 4);
243 R(e, a, b, c, d, F2, K2, 14, 9);
244 R(d, e, a, b, c, F2, K2, 9, 15);
245 R(c, d, e, a, b, F2, K2, 13, 8);
246 R(b, c, d, e, a, F2, K2, 15, 1);
247 R(a, b, c, d, e, F2, K2, 14, 2);
248 R(e, a, b, c, d, F2, K2, 8, 7);
249 R(d, e, a, b, c, F2, K2, 13, 0);
250 R(c, d, e, a, b, F2, K2, 6, 6);
251 R(b, c, d, e, a, F2, K2, 5, 13);
252 R(a, b, c, d, e, F2, K2, 12, 11);
253 R(e, a, b, c, d, F2, K2, 7, 5);
254 R(d, e, a, b, c, F2, K2, 5, 12); /* #47 */
255 /* Round 4 */
256 R(c, d, e, a, b, F3, K3, 11, 1);
257 R(b, c, d, e, a, F3, K3, 12, 9);
258 R(a, b, c, d, e, F3, K3, 14, 11);
259 R(e, a, b, c, d, F3, K3, 15, 10);
260 R(d, e, a, b, c, F3, K3, 14, 0);
261 R(c, d, e, a, b, F3, K3, 15, 8);
262 R(b, c, d, e, a, F3, K3, 9, 12);
263 R(a, b, c, d, e, F3, K3, 8, 4);
264 R(e, a, b, c, d, F3, K3, 9, 13);
265 R(d, e, a, b, c, F3, K3, 14, 3);
266 R(c, d, e, a, b, F3, K3, 5, 7);
267 R(b, c, d, e, a, F3, K3, 6, 15);
268 R(a, b, c, d, e, F3, K3, 8, 14);
269 R(e, a, b, c, d, F3, K3, 6, 5);
270 R(d, e, a, b, c, F3, K3, 5, 6);
271 R(c, d, e, a, b, F3, K3, 12, 2); /* #63 */
272 /* Round 5 */
273 R(b, c, d, e, a, F4, K4, 9, 4);
274 R(a, b, c, d, e, F4, K4, 15, 0);
275 R(e, a, b, c, d, F4, K4, 5, 5);
276 R(d, e, a, b, c, F4, K4, 11, 9);
277 R(c, d, e, a, b, F4, K4, 6, 7);
278 R(b, c, d, e, a, F4, K4, 8, 12);
279 R(a, b, c, d, e, F4, K4, 13, 2);
280 R(e, a, b, c, d, F4, K4, 12, 10);
281 R(d, e, a, b, c, F4, K4, 5, 14);
282 R(c, d, e, a, b, F4, K4, 12, 1);
283 R(b, c, d, e, a, F4, K4, 13, 3);
284 R(a, b, c, d, e, F4, K4, 14, 8);
285 R(e, a, b, c, d, F4, K4, 11, 11);
286 R(d, e, a, b, c, F4, K4, 8, 6);
287 R(c, d, e, a, b, F4, K4, 5, 15);
288 R(b, c, d, e, a, F4, K4, 6, 13); /* #79 */
289
290 aa = a ; bb = b; cc = c; dd = d; ee = e;
291
292 a = state[0];
293 b = state[1];
294 c = state[2];
295 d = state[3];
296 e = state[4];
297
298 /* Parallel round 1 */
299 R(a, b, c, d, e, F4, KK0, 8, 5);
300 R(e, a, b, c, d, F4, KK0, 9, 14);
301 R(d, e, a, b, c, F4, KK0, 9, 7);
302 R(c, d, e, a, b, F4, KK0, 11, 0);
303 R(b, c, d, e, a, F4, KK0, 13, 9);
304 R(a, b, c, d, e, F4, KK0, 15, 2);
305 R(e, a, b, c, d, F4, KK0, 15, 11);
306 R(d, e, a, b, c, F4, KK0, 5, 4);
307 R(c, d, e, a, b, F4, KK0, 7, 13);
308 R(b, c, d, e, a, F4, KK0, 7, 6);
309 R(a, b, c, d, e, F4, KK0, 8, 15);
310 R(e, a, b, c, d, F4, KK0, 11, 8);
311 R(d, e, a, b, c, F4, KK0, 14, 1);
312 R(c, d, e, a, b, F4, KK0, 14, 10);
313 R(b, c, d, e, a, F4, KK0, 12, 3);
314 R(a, b, c, d, e, F4, KK0, 6, 12); /* #15 */
315 /* Parallel round 2 */
316 R(e, a, b, c, d, F3, KK1, 9, 6);
317 R(d, e, a, b, c, F3, KK1, 13, 11);
318 R(c, d, e, a, b, F3, KK1, 15, 3);
319 R(b, c, d, e, a, F3, KK1, 7, 7);
320 R(a, b, c, d, e, F3, KK1, 12, 0);
321 R(e, a, b, c, d, F3, KK1, 8, 13);
322 R(d, e, a, b, c, F3, KK1, 9, 5);
323 R(c, d, e, a, b, F3, KK1, 11, 10);
324 R(b, c, d, e, a, F3, KK1, 7, 14);
325 R(a, b, c, d, e, F3, KK1, 7, 15);
326 R(e, a, b, c, d, F3, KK1, 12, 8);
327 R(d, e, a, b, c, F3, KK1, 7, 12);
328 R(c, d, e, a, b, F3, KK1, 6, 4);
329 R(b, c, d, e, a, F3, KK1, 15, 9);
330 R(a, b, c, d, e, F3, KK1, 13, 1);
331 R(e, a, b, c, d, F3, KK1, 11, 2); /* #31 */
332 /* Parallel round 3 */
333 R(d, e, a, b, c, F2, KK2, 9, 15);
334 R(c, d, e, a, b, F2, KK2, 7, 5);
335 R(b, c, d, e, a, F2, KK2, 15, 1);
336 R(a, b, c, d, e, F2, KK2, 11, 3);
337 R(e, a, b, c, d, F2, KK2, 8, 7);
338 R(d, e, a, b, c, F2, KK2, 6, 14);
339 R(c, d, e, a, b, F2, KK2, 6, 6);
340 R(b, c, d, e, a, F2, KK2, 14, 9);
341 R(a, b, c, d, e, F2, KK2, 12, 11);
342 R(e, a, b, c, d, F2, KK2, 13, 8);
343 R(d, e, a, b, c, F2, KK2, 5, 12);
344 R(c, d, e, a, b, F2, KK2, 14, 2);
345 R(b, c, d, e, a, F2, KK2, 13, 10);
346 R(a, b, c, d, e, F2, KK2, 13, 0);
347 R(e, a, b, c, d, F2, KK2, 7, 4);
348 R(d, e, a, b, c, F2, KK2, 5, 13); /* #47 */
349 /* Parallel round 4 */
350 R(c, d, e, a, b, F1, KK3, 15, 8);
351 R(b, c, d, e, a, F1, KK3, 5, 6);
352 R(a, b, c, d, e, F1, KK3, 8, 4);
353 R(e, a, b, c, d, F1, KK3, 11, 1);
354 R(d, e, a, b, c, F1, KK3, 14, 3);
355 R(c, d, e, a, b, F1, KK3, 14, 11);
356 R(b, c, d, e, a, F1, KK3, 6, 15);
357 R(a, b, c, d, e, F1, KK3, 14, 0);
358 R(e, a, b, c, d, F1, KK3, 6, 5);
359 R(d, e, a, b, c, F1, KK3, 9, 12);
360 R(c, d, e, a, b, F1, KK3, 12, 2);
361 R(b, c, d, e, a, F1, KK3, 9, 13);
362 R(a, b, c, d, e, F1, KK3, 12, 9);
363 R(e, a, b, c, d, F1, KK3, 5, 7);
364 R(d, e, a, b, c, F1, KK3, 15, 10);
365 R(c, d, e, a, b, F1, KK3, 8, 14); /* #63 */
366 /* Parallel round 5 */
367 R(b, c, d, e, a, F0, KK4, 8, 12);
368 R(a, b, c, d, e, F0, KK4, 5, 15);
369 R(e, a, b, c, d, F0, KK4, 12, 10);
370 R(d, e, a, b, c, F0, KK4, 9, 4);
371 R(c, d, e, a, b, F0, KK4, 12, 1);
372 R(b, c, d, e, a, F0, KK4, 5, 5);
373 R(a, b, c, d, e, F0, KK4, 14, 8);
374 R(e, a, b, c, d, F0, KK4, 6, 7);
375 R(d, e, a, b, c, F0, KK4, 8, 6);
376 R(c, d, e, a, b, F0, KK4, 13, 2);
377 R(b, c, d, e, a, F0, KK4, 6, 13);
378 R(a, b, c, d, e, F0, KK4, 5, 14);
379 R(e, a, b, c, d, F0, KK4, 15, 0);
380 R(d, e, a, b, c, F0, KK4, 13, 3);
381 R(c, d, e, a, b, F0, KK4, 11, 9);
382 R(b, c, d, e, a, F0, KK4, 11, 11); /* #79 */
383
384 t = state[1] + cc + d;
385 state[1] = state[2] + dd + e;
386 state[2] = state[3] + ee + a;
387 state[3] = state[4] + aa + b;
388 state[4] = state[0] + bb + c;
389 state[0] = t;
390 }
391