Remove unneeded .Pp before .Rs in various manual pages.
[dragonfly.git] / sys / opencrypto / rmd160.c
blob892f5742c4f5c41baa08e044e1fbf2d1be1184ab
1 /* $FreeBSD: src/sys/opencrypto/rmd160.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $ */
2 /* $DragonFly: src/sys/opencrypto/rmd160.c,v 1.2 2003/06/17 04:28:54 dillon 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.
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.
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.
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
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/endian.h>
35 #include <opencrypto/rmd160.h>
37 #define PUT_64BIT_LE(cp, value) do { \
38 (cp)[7] = (value) >> 56; \
39 (cp)[6] = (value) >> 48; \
40 (cp)[5] = (value) >> 40; \
41 (cp)[4] = (value) >> 32; \
42 (cp)[3] = (value) >> 24; \
43 (cp)[2] = (value) >> 16; \
44 (cp)[1] = (value) >> 8; \
45 (cp)[0] = (value); } while (0)
47 #define PUT_32BIT_LE(cp, value) do { \
48 (cp)[3] = (value) >> 24; \
49 (cp)[2] = (value) >> 16; \
50 (cp)[1] = (value) >> 8; \
51 (cp)[0] = (value); } while (0)
53 #define H0 0x67452301U
54 #define H1 0xEFCDAB89U
55 #define H2 0x98BADCFEU
56 #define H3 0x10325476U
57 #define H4 0xC3D2E1F0U
59 #define K0 0x00000000U
60 #define K1 0x5A827999U
61 #define K2 0x6ED9EBA1U
62 #define K3 0x8F1BBCDCU
63 #define K4 0xA953FD4EU
65 #define KK0 0x50A28BE6U
66 #define KK1 0x5C4DD124U
67 #define KK2 0x6D703EF3U
68 #define KK3 0x7A6D76E9U
69 #define KK4 0x00000000U
71 /* rotate x left n bits. */
72 #define ROL(n, x) (((x) << (n)) | ((x) >> (32-(n))))
74 #define F0(x, y, z) ((x) ^ (y) ^ (z))
75 #define F1(x, y, z) (((x) & (y)) | ((~x) & (z)))
76 #define F2(x, y, z) (((x) | (~y)) ^ (z))
77 #define F3(x, y, z) (((x) & (z)) | ((y) & (~z)))
78 #define F4(x, y, z) ((x) ^ ((y) | (~z)))
80 #define R(a, b, c, d, e, Fj, Kj, sj, rj) \
81 do { \
82 a = ROL(sj, a + Fj(b,c,d) + X(rj) + Kj) + e; \
83 c = ROL(10, c); \
84 } while(0)
86 #define X(i) x[i]
88 static u_char PADDING[64] = {
89 0x80, 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, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
94 void
95 RMD160Init(RMD160_CTX *ctx)
97 ctx->count = 0;
98 ctx->state[0] = H0;
99 ctx->state[1] = H1;
100 ctx->state[2] = H2;
101 ctx->state[3] = H3;
102 ctx->state[4] = H4;
105 void
106 RMD160Update(RMD160_CTX *ctx, const u_char *input, u_int32_t len)
108 u_int32_t have, off, need;
110 have = (ctx->count/8) % 64;
111 need = 64 - have;
112 ctx->count += 8 * len;
113 off = 0;
115 if (len >= need) {
116 if (have) {
117 memcpy(ctx->buffer + have, input, need);
118 RMD160Transform(ctx->state, ctx->buffer);
119 off = need;
120 have = 0;
122 /* now the buffer is empty */
123 while (off + 64 <= len) {
124 RMD160Transform(ctx->state, input+off);
125 off += 64;
128 if (off < len)
129 memcpy(ctx->buffer + have, input+off, len-off);
132 void
133 RMD160Final(u_char digest[20], RMD160_CTX *ctx)
135 int i;
136 u_char size[8];
137 u_int32_t padlen;
139 PUT_64BIT_LE(size, ctx->count);
142 * pad to 64 byte blocks, at least one byte from PADDING plus 8 bytes
143 * for the size
145 padlen = 64 - ((ctx->count/8) % 64);
146 if (padlen < 1 + 8)
147 padlen += 64;
148 RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
149 RMD160Update(ctx, size, 8);
151 if (digest != NULL)
152 for (i = 0; i < 5; i++)
153 PUT_32BIT_LE(digest + i*4, ctx->state[i]);
155 memset(ctx, 0, sizeof (*ctx));
158 void
159 RMD160Transform(u_int32_t state[5], const u_char block[64])
161 u_int32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16];
163 #if BYTE_ORDER == LITTLE_ENDIAN
164 memcpy(x, block, 64);
165 #else
166 int i;
168 for (i = 0; i < 16; i++)
169 x[i] = bswap32(*(const u_int32_t*)(block+i*4));
170 #endif
172 a = state[0];
173 b = state[1];
174 c = state[2];
175 d = state[3];
176 e = state[4];
178 /* Round 1 */
179 R(a, b, c, d, e, F0, K0, 11, 0);
180 R(e, a, b, c, d, F0, K0, 14, 1);
181 R(d, e, a, b, c, F0, K0, 15, 2);
182 R(c, d, e, a, b, F0, K0, 12, 3);
183 R(b, c, d, e, a, F0, K0, 5, 4);
184 R(a, b, c, d, e, F0, K0, 8, 5);
185 R(e, a, b, c, d, F0, K0, 7, 6);
186 R(d, e, a, b, c, F0, K0, 9, 7);
187 R(c, d, e, a, b, F0, K0, 11, 8);
188 R(b, c, d, e, a, F0, K0, 13, 9);
189 R(a, b, c, d, e, F0, K0, 14, 10);
190 R(e, a, b, c, d, F0, K0, 15, 11);
191 R(d, e, a, b, c, F0, K0, 6, 12);
192 R(c, d, e, a, b, F0, K0, 7, 13);
193 R(b, c, d, e, a, F0, K0, 9, 14);
194 R(a, b, c, d, e, F0, K0, 8, 15); /* #15 */
195 /* Round 2 */
196 R(e, a, b, c, d, F1, K1, 7, 7);
197 R(d, e, a, b, c, F1, K1, 6, 4);
198 R(c, d, e, a, b, F1, K1, 8, 13);
199 R(b, c, d, e, a, F1, K1, 13, 1);
200 R(a, b, c, d, e, F1, K1, 11, 10);
201 R(e, a, b, c, d, F1, K1, 9, 6);
202 R(d, e, a, b, c, F1, K1, 7, 15);
203 R(c, d, e, a, b, F1, K1, 15, 3);
204 R(b, c, d, e, a, F1, K1, 7, 12);
205 R(a, b, c, d, e, F1, K1, 12, 0);
206 R(e, a, b, c, d, F1, K1, 15, 9);
207 R(d, e, a, b, c, F1, K1, 9, 5);
208 R(c, d, e, a, b, F1, K1, 11, 2);
209 R(b, c, d, e, a, F1, K1, 7, 14);
210 R(a, b, c, d, e, F1, K1, 13, 11);
211 R(e, a, b, c, d, F1, K1, 12, 8); /* #31 */
212 /* Round 3 */
213 R(d, e, a, b, c, F2, K2, 11, 3);
214 R(c, d, e, a, b, F2, K2, 13, 10);
215 R(b, c, d, e, a, F2, K2, 6, 14);
216 R(a, b, c, d, e, F2, K2, 7, 4);
217 R(e, a, b, c, d, F2, K2, 14, 9);
218 R(d, e, a, b, c, F2, K2, 9, 15);
219 R(c, d, e, a, b, F2, K2, 13, 8);
220 R(b, c, d, e, a, F2, K2, 15, 1);
221 R(a, b, c, d, e, F2, K2, 14, 2);
222 R(e, a, b, c, d, F2, K2, 8, 7);
223 R(d, e, a, b, c, F2, K2, 13, 0);
224 R(c, d, e, a, b, F2, K2, 6, 6);
225 R(b, c, d, e, a, F2, K2, 5, 13);
226 R(a, b, c, d, e, F2, K2, 12, 11);
227 R(e, a, b, c, d, F2, K2, 7, 5);
228 R(d, e, a, b, c, F2, K2, 5, 12); /* #47 */
229 /* Round 4 */
230 R(c, d, e, a, b, F3, K3, 11, 1);
231 R(b, c, d, e, a, F3, K3, 12, 9);
232 R(a, b, c, d, e, F3, K3, 14, 11);
233 R(e, a, b, c, d, F3, K3, 15, 10);
234 R(d, e, a, b, c, F3, K3, 14, 0);
235 R(c, d, e, a, b, F3, K3, 15, 8);
236 R(b, c, d, e, a, F3, K3, 9, 12);
237 R(a, b, c, d, e, F3, K3, 8, 4);
238 R(e, a, b, c, d, F3, K3, 9, 13);
239 R(d, e, a, b, c, F3, K3, 14, 3);
240 R(c, d, e, a, b, F3, K3, 5, 7);
241 R(b, c, d, e, a, F3, K3, 6, 15);
242 R(a, b, c, d, e, F3, K3, 8, 14);
243 R(e, a, b, c, d, F3, K3, 6, 5);
244 R(d, e, a, b, c, F3, K3, 5, 6);
245 R(c, d, e, a, b, F3, K3, 12, 2); /* #63 */
246 /* Round 5 */
247 R(b, c, d, e, a, F4, K4, 9, 4);
248 R(a, b, c, d, e, F4, K4, 15, 0);
249 R(e, a, b, c, d, F4, K4, 5, 5);
250 R(d, e, a, b, c, F4, K4, 11, 9);
251 R(c, d, e, a, b, F4, K4, 6, 7);
252 R(b, c, d, e, a, F4, K4, 8, 12);
253 R(a, b, c, d, e, F4, K4, 13, 2);
254 R(e, a, b, c, d, F4, K4, 12, 10);
255 R(d, e, a, b, c, F4, K4, 5, 14);
256 R(c, d, e, a, b, F4, K4, 12, 1);
257 R(b, c, d, e, a, F4, K4, 13, 3);
258 R(a, b, c, d, e, F4, K4, 14, 8);
259 R(e, a, b, c, d, F4, K4, 11, 11);
260 R(d, e, a, b, c, F4, K4, 8, 6);
261 R(c, d, e, a, b, F4, K4, 5, 15);
262 R(b, c, d, e, a, F4, K4, 6, 13); /* #79 */
264 aa = a ; bb = b; cc = c; dd = d; ee = e;
266 a = state[0];
267 b = state[1];
268 c = state[2];
269 d = state[3];
270 e = state[4];
272 /* Parallel round 1 */
273 R(a, b, c, d, e, F4, KK0, 8, 5);
274 R(e, a, b, c, d, F4, KK0, 9, 14);
275 R(d, e, a, b, c, F4, KK0, 9, 7);
276 R(c, d, e, a, b, F4, KK0, 11, 0);
277 R(b, c, d, e, a, F4, KK0, 13, 9);
278 R(a, b, c, d, e, F4, KK0, 15, 2);
279 R(e, a, b, c, d, F4, KK0, 15, 11);
280 R(d, e, a, b, c, F4, KK0, 5, 4);
281 R(c, d, e, a, b, F4, KK0, 7, 13);
282 R(b, c, d, e, a, F4, KK0, 7, 6);
283 R(a, b, c, d, e, F4, KK0, 8, 15);
284 R(e, a, b, c, d, F4, KK0, 11, 8);
285 R(d, e, a, b, c, F4, KK0, 14, 1);
286 R(c, d, e, a, b, F4, KK0, 14, 10);
287 R(b, c, d, e, a, F4, KK0, 12, 3);
288 R(a, b, c, d, e, F4, KK0, 6, 12); /* #15 */
289 /* Parallel round 2 */
290 R(e, a, b, c, d, F3, KK1, 9, 6);
291 R(d, e, a, b, c, F3, KK1, 13, 11);
292 R(c, d, e, a, b, F3, KK1, 15, 3);
293 R(b, c, d, e, a, F3, KK1, 7, 7);
294 R(a, b, c, d, e, F3, KK1, 12, 0);
295 R(e, a, b, c, d, F3, KK1, 8, 13);
296 R(d, e, a, b, c, F3, KK1, 9, 5);
297 R(c, d, e, a, b, F3, KK1, 11, 10);
298 R(b, c, d, e, a, F3, KK1, 7, 14);
299 R(a, b, c, d, e, F3, KK1, 7, 15);
300 R(e, a, b, c, d, F3, KK1, 12, 8);
301 R(d, e, a, b, c, F3, KK1, 7, 12);
302 R(c, d, e, a, b, F3, KK1, 6, 4);
303 R(b, c, d, e, a, F3, KK1, 15, 9);
304 R(a, b, c, d, e, F3, KK1, 13, 1);
305 R(e, a, b, c, d, F3, KK1, 11, 2); /* #31 */
306 /* Parallel round 3 */
307 R(d, e, a, b, c, F2, KK2, 9, 15);
308 R(c, d, e, a, b, F2, KK2, 7, 5);
309 R(b, c, d, e, a, F2, KK2, 15, 1);
310 R(a, b, c, d, e, F2, KK2, 11, 3);
311 R(e, a, b, c, d, F2, KK2, 8, 7);
312 R(d, e, a, b, c, F2, KK2, 6, 14);
313 R(c, d, e, a, b, F2, KK2, 6, 6);
314 R(b, c, d, e, a, F2, KK2, 14, 9);
315 R(a, b, c, d, e, F2, KK2, 12, 11);
316 R(e, a, b, c, d, F2, KK2, 13, 8);
317 R(d, e, a, b, c, F2, KK2, 5, 12);
318 R(c, d, e, a, b, F2, KK2, 14, 2);
319 R(b, c, d, e, a, F2, KK2, 13, 10);
320 R(a, b, c, d, e, F2, KK2, 13, 0);
321 R(e, a, b, c, d, F2, KK2, 7, 4);
322 R(d, e, a, b, c, F2, KK2, 5, 13); /* #47 */
323 /* Parallel round 4 */
324 R(c, d, e, a, b, F1, KK3, 15, 8);
325 R(b, c, d, e, a, F1, KK3, 5, 6);
326 R(a, b, c, d, e, F1, KK3, 8, 4);
327 R(e, a, b, c, d, F1, KK3, 11, 1);
328 R(d, e, a, b, c, F1, KK3, 14, 3);
329 R(c, d, e, a, b, F1, KK3, 14, 11);
330 R(b, c, d, e, a, F1, KK3, 6, 15);
331 R(a, b, c, d, e, F1, KK3, 14, 0);
332 R(e, a, b, c, d, F1, KK3, 6, 5);
333 R(d, e, a, b, c, F1, KK3, 9, 12);
334 R(c, d, e, a, b, F1, KK3, 12, 2);
335 R(b, c, d, e, a, F1, KK3, 9, 13);
336 R(a, b, c, d, e, F1, KK3, 12, 9);
337 R(e, a, b, c, d, F1, KK3, 5, 7);
338 R(d, e, a, b, c, F1, KK3, 15, 10);
339 R(c, d, e, a, b, F1, KK3, 8, 14); /* #63 */
340 /* Parallel round 5 */
341 R(b, c, d, e, a, F0, KK4, 8, 12);
342 R(a, b, c, d, e, F0, KK4, 5, 15);
343 R(e, a, b, c, d, F0, KK4, 12, 10);
344 R(d, e, a, b, c, F0, KK4, 9, 4);
345 R(c, d, e, a, b, F0, KK4, 12, 1);
346 R(b, c, d, e, a, F0, KK4, 5, 5);
347 R(a, b, c, d, e, F0, KK4, 14, 8);
348 R(e, a, b, c, d, F0, KK4, 6, 7);
349 R(d, e, a, b, c, F0, KK4, 8, 6);
350 R(c, d, e, a, b, F0, KK4, 13, 2);
351 R(b, c, d, e, a, F0, KK4, 6, 13);
352 R(a, b, c, d, e, F0, KK4, 5, 14);
353 R(e, a, b, c, d, F0, KK4, 15, 0);
354 R(d, e, a, b, c, F0, KK4, 13, 3);
355 R(c, d, e, a, b, F0, KK4, 11, 9);
356 R(b, c, d, e, a, F0, KK4, 11, 11); /* #79 */
358 t = state[1] + cc + d;
359 state[1] = state[2] + dd + e;
360 state[2] = state[3] + ee + a;
361 state[3] = state[4] + aa + b;
362 state[4] = state[0] + bb + c;
363 state[0] = t;