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