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