This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
ripemd.c
299 lines (270 loc) · 9.92 KB
/
ripemd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (C) 2006-2009 Vincent Hanquez <vincent@snarc.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ripemd.h"
#include "bitfn.h"
#include <string.h>
void ripemd160_init(struct ripemd160_ctx *ctx)
{
memset(ctx, 0, sizeof(*ctx));
ctx->h[0] = 0x67452301;
ctx->h[1] = 0xefcdab89;
ctx->h[2] = 0x98badcfe;
ctx->h[3] = 0x10325476;
ctx->h[4] = 0xc3d2e1f0;
}
#define K1 0x00000000
#define K2 0x5a827999
#define K3 0x6ed9eba1
#define K4 0x8f1bbcdc
#define K5 0xa953fd4e
#define K6 0x50a28be6
#define K7 0x5c4dd124
#define K8 0x6d703ef3
#define K9 0x7a6d76e9
#define f1(x, y, z) (x ^ y ^ z)
#define f2(x, y, z) (z ^ (x & (y ^ z)))
#define f3(x, y, z) ((x | ~y) ^ z)
#define f4(x, y, z) (y ^ (z & (x ^ y)))
#define f5(x, y, z) (x ^ (y | ~z))
#define R(a, b, c, d, e, f, k, i, s) \
a += f(b, c, d) + w[i] + k; a = rol32(a, s) + e; c = rol32(c, 10)
static void ripemd160_do_chunk(struct ripemd160_ctx *ctx, uint32_t *buf)
{
uint32_t a1, b1, c1, d1, e1, a2, b2, c2, d2, e2;
#ifdef ARCH_IS_BIG_ENDIAN
uint32_t w[16];
cpu_to_le32_array(w, buf, 16);
#else
uint32_t *w = buf;
#endif
a1 = ctx->h[0]; b1 = ctx->h[1]; c1 = ctx->h[2]; d1 = ctx->h[3]; e1 = ctx->h[4];
a2 = ctx->h[0]; b2 = ctx->h[1]; c2 = ctx->h[2]; d2 = ctx->h[3]; e2 = ctx->h[4];
/* 5 passes on first state copy */
R(a1, b1, c1, d1, e1, f1, K1, 0, 11);
R(e1, a1, b1, c1, d1, f1, K1, 1, 14);
R(d1, e1, a1, b1, c1, f1, K1, 2, 15);
R(c1, d1, e1, a1, b1, f1, K1, 3, 12);
R(b1, c1, d1, e1, a1, f1, K1, 4, 5);
R(a1, b1, c1, d1, e1, f1, K1, 5, 8);
R(e1, a1, b1, c1, d1, f1, K1, 6, 7);
R(d1, e1, a1, b1, c1, f1, K1, 7, 9);
R(c1, d1, e1, a1, b1, f1, K1, 8, 11);
R(b1, c1, d1, e1, a1, f1, K1, 9, 13);
R(a1, b1, c1, d1, e1, f1, K1, 10, 14);
R(e1, a1, b1, c1, d1, f1, K1, 11, 15);
R(d1, e1, a1, b1, c1, f1, K1, 12, 6);
R(c1, d1, e1, a1, b1, f1, K1, 13, 7);
R(b1, c1, d1, e1, a1, f1, K1, 14, 9);
R(a1, b1, c1, d1, e1, f1, K1, 15, 8);
R(e1, a1, b1, c1, d1, f2, K2, 7, 7);
R(d1, e1, a1, b1, c1, f2, K2, 4, 6);
R(c1, d1, e1, a1, b1, f2, K2, 13, 8);
R(b1, c1, d1, e1, a1, f2, K2, 1, 13);
R(a1, b1, c1, d1, e1, f2, K2, 10, 11);
R(e1, a1, b1, c1, d1, f2, K2, 6, 9);
R(d1, e1, a1, b1, c1, f2, K2, 15, 7);
R(c1, d1, e1, a1, b1, f2, K2, 3, 15);
R(b1, c1, d1, e1, a1, f2, K2, 12, 7);
R(a1, b1, c1, d1, e1, f2, K2, 0, 12);
R(e1, a1, b1, c1, d1, f2, K2, 9, 15);
R(d1, e1, a1, b1, c1, f2, K2, 5, 9);
R(c1, d1, e1, a1, b1, f2, K2, 2, 11);
R(b1, c1, d1, e1, a1, f2, K2, 14, 7);
R(a1, b1, c1, d1, e1, f2, K2, 11, 13);
R(e1, a1, b1, c1, d1, f2, K2, 8, 12);
R(d1, e1, a1, b1, c1, f3, K3, 3, 11);
R(c1, d1, e1, a1, b1, f3, K3, 10, 13);
R(b1, c1, d1, e1, a1, f3, K3, 14, 6);
R(a1, b1, c1, d1, e1, f3, K3, 4, 7);
R(e1, a1, b1, c1, d1, f3, K3, 9, 14);
R(d1, e1, a1, b1, c1, f3, K3, 15, 9);
R(c1, d1, e1, a1, b1, f3, K3, 8, 13);
R(b1, c1, d1, e1, a1, f3, K3, 1, 15);
R(a1, b1, c1, d1, e1, f3, K3, 2, 14);
R(e1, a1, b1, c1, d1, f3, K3, 7, 8);
R(d1, e1, a1, b1, c1, f3, K3, 0, 13);
R(c1, d1, e1, a1, b1, f3, K3, 6, 6);
R(b1, c1, d1, e1, a1, f3, K3, 13, 5);
R(a1, b1, c1, d1, e1, f3, K3, 11, 12);
R(e1, a1, b1, c1, d1, f3, K3, 5, 7);
R(d1, e1, a1, b1, c1, f3, K3, 12, 5);
R(c1, d1, e1, a1, b1, f4, K4, 1, 11);
R(b1, c1, d1, e1, a1, f4, K4, 9, 12);
R(a1, b1, c1, d1, e1, f4, K4, 11, 14);
R(e1, a1, b1, c1, d1, f4, K4, 10, 15);
R(d1, e1, a1, b1, c1, f4, K4, 0, 14);
R(c1, d1, e1, a1, b1, f4, K4, 8, 15);
R(b1, c1, d1, e1, a1, f4, K4, 12, 9);
R(a1, b1, c1, d1, e1, f4, K4, 4, 8);
R(e1, a1, b1, c1, d1, f4, K4, 13, 9);
R(d1, e1, a1, b1, c1, f4, K4, 3, 14);
R(c1, d1, e1, a1, b1, f4, K4, 7, 5);
R(b1, c1, d1, e1, a1, f4, K4, 15, 6);
R(a1, b1, c1, d1, e1, f4, K4, 14, 8);
R(e1, a1, b1, c1, d1, f4, K4, 5, 6);
R(d1, e1, a1, b1, c1, f4, K4, 6, 5);
R(c1, d1, e1, a1, b1, f4, K4, 2, 12);
R(b1, c1, d1, e1, a1, f5, K5, 4, 9);
R(a1, b1, c1, d1, e1, f5, K5, 0, 15);
R(e1, a1, b1, c1, d1, f5, K5, 5, 5);
R(d1, e1, a1, b1, c1, f5, K5, 9, 11);
R(c1, d1, e1, a1, b1, f5, K5, 7, 6);
R(b1, c1, d1, e1, a1, f5, K5, 12, 8);
R(a1, b1, c1, d1, e1, f5, K5, 2, 13);
R(e1, a1, b1, c1, d1, f5, K5, 10, 12);
R(d1, e1, a1, b1, c1, f5, K5, 14, 5);
R(c1, d1, e1, a1, b1, f5, K5, 1, 12);
R(b1, c1, d1, e1, a1, f5, K5, 3, 13);
R(a1, b1, c1, d1, e1, f5, K5, 8, 14);
R(e1, a1, b1, c1, d1, f5, K5, 11, 11);
R(d1, e1, a1, b1, c1, f5, K5, 6, 8);
R(c1, d1, e1, a1, b1, f5, K5, 15, 5);
R(b1, c1, d1, e1, a1, f5, K5, 13, 6);
/* 5 passes on second state copy */
R(a2, b2, c2, d2, e2, f5, K6, 5, 8);
R(e2, a2, b2, c2, d2, f5, K6, 14, 9);
R(d2, e2, a2, b2, c2, f5, K6, 7, 9);
R(c2, d2, e2, a2, b2, f5, K6, 0, 11);
R(b2, c2, d2, e2, a2, f5, K6, 9, 13);
R(a2, b2, c2, d2, e2, f5, K6, 2, 15);
R(e2, a2, b2, c2, d2, f5, K6, 11, 15);
R(d2, e2, a2, b2, c2, f5, K6, 4, 5);
R(c2, d2, e2, a2, b2, f5, K6, 13, 7);
R(b2, c2, d2, e2, a2, f5, K6, 6, 7);
R(a2, b2, c2, d2, e2, f5, K6, 15, 8);
R(e2, a2, b2, c2, d2, f5, K6, 8, 11);
R(d2, e2, a2, b2, c2, f5, K6, 1, 14);
R(c2, d2, e2, a2, b2, f5, K6, 10, 14);
R(b2, c2, d2, e2, a2, f5, K6, 3, 12);
R(a2, b2, c2, d2, e2, f5, K6, 12, 6);
R(e2, a2, b2, c2, d2, f4, K7, 6, 9);
R(d2, e2, a2, b2, c2, f4, K7, 11, 13);
R(c2, d2, e2, a2, b2, f4, K7, 3, 15);
R(b2, c2, d2, e2, a2, f4, K7, 7, 7);
R(a2, b2, c2, d2, e2, f4, K7, 0, 12);
R(e2, a2, b2, c2, d2, f4, K7, 13, 8);
R(d2, e2, a2, b2, c2, f4, K7, 5, 9);
R(c2, d2, e2, a2, b2, f4, K7, 10, 11);
R(b2, c2, d2, e2, a2, f4, K7, 14, 7);
R(a2, b2, c2, d2, e2, f4, K7, 15, 7);
R(e2, a2, b2, c2, d2, f4, K7, 8, 12);
R(d2, e2, a2, b2, c2, f4, K7, 12, 7);
R(c2, d2, e2, a2, b2, f4, K7, 4, 6);
R(b2, c2, d2, e2, a2, f4, K7, 9, 15);
R(a2, b2, c2, d2, e2, f4, K7, 1, 13);
R(e2, a2, b2, c2, d2, f4, K7, 2, 11);
R(d2, e2, a2, b2, c2, f3, K8, 15, 9);
R(c2, d2, e2, a2, b2, f3, K8, 5, 7);
R(b2, c2, d2, e2, a2, f3, K8, 1, 15);
R(a2, b2, c2, d2, e2, f3, K8, 3, 11);
R(e2, a2, b2, c2, d2, f3, K8, 7, 8);
R(d2, e2, a2, b2, c2, f3, K8, 14, 6);
R(c2, d2, e2, a2, b2, f3, K8, 6, 6);
R(b2, c2, d2, e2, a2, f3, K8, 9, 14);
R(a2, b2, c2, d2, e2, f3, K8, 11, 12);
R(e2, a2, b2, c2, d2, f3, K8, 8, 13);
R(d2, e2, a2, b2, c2, f3, K8, 12, 5);
R(c2, d2, e2, a2, b2, f3, K8, 2, 14);
R(b2, c2, d2, e2, a2, f3, K8, 10, 13);
R(a2, b2, c2, d2, e2, f3, K8, 0, 13);
R(e2, a2, b2, c2, d2, f3, K8, 4, 7);
R(d2, e2, a2, b2, c2, f3, K8, 13, 5);
R(c2, d2, e2, a2, b2, f2, K9, 8, 15);
R(b2, c2, d2, e2, a2, f2, K9, 6, 5);
R(a2, b2, c2, d2, e2, f2, K9, 4, 8);
R(e2, a2, b2, c2, d2, f2, K9, 1, 11);
R(d2, e2, a2, b2, c2, f2, K9, 3, 14);
R(c2, d2, e2, a2, b2, f2, K9, 11, 14);
R(b2, c2, d2, e2, a2, f2, K9, 15, 6);
R(a2, b2, c2, d2, e2, f2, K9, 0, 14);
R(e2, a2, b2, c2, d2, f2, K9, 5, 6);
R(d2, e2, a2, b2, c2, f2, K9, 12, 9);
R(c2, d2, e2, a2, b2, f2, K9, 2, 12);
R(b2, c2, d2, e2, a2, f2, K9, 13, 9);
R(a2, b2, c2, d2, e2, f2, K9, 9, 12);
R(e2, a2, b2, c2, d2, f2, K9, 7, 5);
R(d2, e2, a2, b2, c2, f2, K9, 10, 15);
R(c2, d2, e2, a2, b2, f2, K9, 14, 8);
R(b2, c2, d2, e2, a2, f1, K1, 12, 8);
R(a2, b2, c2, d2, e2, f1, K1, 15, 5);
R(e2, a2, b2, c2, d2, f1, K1, 10, 12);
R(d2, e2, a2, b2, c2, f1, K1, 4, 9);
R(c2, d2, e2, a2, b2, f1, K1, 1, 12);
R(b2, c2, d2, e2, a2, f1, K1, 5, 5);
R(a2, b2, c2, d2, e2, f1, K1, 8, 14);
R(e2, a2, b2, c2, d2, f1, K1, 7, 6);
R(d2, e2, a2, b2, c2, f1, K1, 6, 8);
R(c2, d2, e2, a2, b2, f1, K1, 2, 13);
R(b2, c2, d2, e2, a2, f1, K1, 13, 6);
R(a2, b2, c2, d2, e2, f1, K1, 14, 5);
R(e2, a2, b2, c2, d2, f1, K1, 0, 15);
R(d2, e2, a2, b2, c2, f1, K1, 3, 13);
R(c2, d2, e2, a2, b2, f1, K1, 9, 11);
R(b2, c2, d2, e2, a2, f1, K1, 11, 11);
d2 += c1 + ctx->h[1];
ctx->h[1] = ctx->h[2] + d1 + e2;
ctx->h[2] = ctx->h[3] + e1 + a2;
ctx->h[3] = ctx->h[4] + a1 + b2;
ctx->h[4] = ctx->h[0] + b1 + c2;
ctx->h[0] = d2;
}
void ripemd160_update(struct ripemd160_ctx *ctx, uint8_t *data, uint32_t len)
{
uint32_t index, to_fill;
index = (uint32_t) (ctx->sz & 0x3f);
to_fill = 64 - index;
ctx->sz += len;
if (index && len >= to_fill) {
memcpy(ctx->buf + index, data, to_fill);
ripemd160_do_chunk(ctx, (uint32_t *) ctx->buf);
len -= to_fill;
data += to_fill;
index = 0;
}
for (; len >= 64; len -= 64, data += 64)
ripemd160_do_chunk(ctx, (uint32_t *) data);
if (len)
memcpy(ctx->buf + index, data, len);
}
void ripemd160_finalize(struct ripemd160_ctx *ctx, uint8_t *out)
{
static uint8_t padding[64] = { 0x80, };
uint64_t bits;
uint32_t index, padlen;
uint32_t *p = (uint32_t *) out;
/* add padding and update data with it */
bits = cpu_to_le64(ctx->sz << 3);
/* pad out to 56 */
index = (uint32_t) (ctx->sz & 0x3f);
padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
ripemd160_update(ctx, padding, padlen);
/* append length */
ripemd160_update(ctx, (uint8_t *) &bits, sizeof(bits));
/* output digest */
p[0] = cpu_to_le32(ctx->h[0]);
p[1] = cpu_to_le32(ctx->h[1]);
p[2] = cpu_to_le32(ctx->h[2]);
p[3] = cpu_to_le32(ctx->h[3]);
p[4] = cpu_to_le32(ctx->h[4]);
}