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