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