1 /*
2 * Copyright (c) 2015-2018, Andrew Romanenko <melanhit@gmail.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32
33 #include <config.h>
34
35 #include "../akmos.h"
36 #include "../bits.h"
37 #include "../digest.h"
38
39 #include "ripemd.h"
40
41 #define H0 0x67452301
42 #define H1 0xEFCDAB89
43 #define H2 0x98BADCFE
44 #define H3 0x10325476
45 #define H4 0xC3D2E1F0
46 #define H5 0x76543210
47 #define H6 0xFEDCBA98
48 #define H7 0x89ABCDEF
49 #define H8 0x01234567
50 #define H9 0x3C2D1E0F
51
52 #define K0 0x00000000
53 #define K1 0x5A827999
54 #define K2 0x6ED9EBA1
55 #define K3 0x8F1BBCDC
56 #define K4 0xA953FD4E
57
58 #define KK0 0x50A28BE6
59 #define KK1 0x5C4DD124
60 #define KK2 0x6D703EF3
61 #define KK3 0x7A6D76E9
62 #define KK4 0x00000000
63
64 #define F0(x, y, z) ((x) ^ (y) ^ (z))
65 #define F1(x, y, z) (((x) & (y)) | ((~x) & (z)))
66 #define F2(x, y, z) (((x) | (~y)) ^ (z))
67 #define F3(x, y, z) (((x) & (z)) | ((y) & (~z)))
68 #define F4(x, y, z) ((x) ^ ((y) | (~z)))
69
70 #define R0(a, b, c, d, Fj, Kj, sj, rj) \
71 { \
72 a = ROTL32(a + Fj(b,c,d) + X(rj) + Kj, sj); \
73 }
74
75 #define R1(a, b, c, d, e, Fj, Kj, sj, rj) \
76 { \
77 a = ROTL32(a + Fj(b,c,d) + X(rj) + Kj, sj) + e; \
78 c = ROTL32(c, 10); \
79 }
80
81 #define X(i) x[i]
82
ripemd_160_transform(uint32_t * h,const uint8_t * block,size_t nb)83 static void ripemd_160_transform(uint32_t *h, const uint8_t *block, size_t nb)
84 {
85 uint32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, *x;
86 size_t i;
87
88 x = h + 5;
89
90 for(i = 0; i < nb; i++, block += AKMOS_RIPEMD_BLKLEN) {
91 memcpy(x, block, AKMOS_RIPEMD_BLKLEN);
92
93 a = h[0];
94 b = h[1];
95 c = h[2];
96 d = h[3];
97 e = h[4];
98
99 /* Round 1 */
100 R1(a, b, c, d, e, F0, K0, 11, 0);
101 R1(e, a, b, c, d, F0, K0, 14, 1);
102 R1(d, e, a, b, c, F0, K0, 15, 2);
103 R1(c, d, e, a, b, F0, K0, 12, 3);
104 R1(b, c, d, e, a, F0, K0, 5, 4);
105 R1(a, b, c, d, e, F0, K0, 8, 5);
106 R1(e, a, b, c, d, F0, K0, 7, 6);
107 R1(d, e, a, b, c, F0, K0, 9, 7);
108 R1(c, d, e, a, b, F0, K0, 11, 8);
109 R1(b, c, d, e, a, F0, K0, 13, 9);
110 R1(a, b, c, d, e, F0, K0, 14, 10);
111 R1(e, a, b, c, d, F0, K0, 15, 11);
112 R1(d, e, a, b, c, F0, K0, 6, 12);
113 R1(c, d, e, a, b, F0, K0, 7, 13);
114 R1(b, c, d, e, a, F0, K0, 9, 14);
115 R1(a, b, c, d, e, F0, K0, 8, 15); /* #15 */
116 /* Round 2 */
117 R1(e, a, b, c, d, F1, K1, 7, 7);
118 R1(d, e, a, b, c, F1, K1, 6, 4);
119 R1(c, d, e, a, b, F1, K1, 8, 13);
120 R1(b, c, d, e, a, F1, K1, 13, 1);
121 R1(a, b, c, d, e, F1, K1, 11, 10);
122 R1(e, a, b, c, d, F1, K1, 9, 6);
123 R1(d, e, a, b, c, F1, K1, 7, 15);
124 R1(c, d, e, a, b, F1, K1, 15, 3);
125 R1(b, c, d, e, a, F1, K1, 7, 12);
126 R1(a, b, c, d, e, F1, K1, 12, 0);
127 R1(e, a, b, c, d, F1, K1, 15, 9);
128 R1(d, e, a, b, c, F1, K1, 9, 5);
129 R1(c, d, e, a, b, F1, K1, 11, 2);
130 R1(b, c, d, e, a, F1, K1, 7, 14);
131 R1(a, b, c, d, e, F1, K1, 13, 11);
132 R1(e, a, b, c, d, F1, K1, 12, 8); /* #31 */
133 /* Round 3 */
134 R1(d, e, a, b, c, F2, K2, 11, 3);
135 R1(c, d, e, a, b, F2, K2, 13, 10);
136 R1(b, c, d, e, a, F2, K2, 6, 14);
137 R1(a, b, c, d, e, F2, K2, 7, 4);
138 R1(e, a, b, c, d, F2, K2, 14, 9);
139 R1(d, e, a, b, c, F2, K2, 9, 15);
140 R1(c, d, e, a, b, F2, K2, 13, 8);
141 R1(b, c, d, e, a, F2, K2, 15, 1);
142 R1(a, b, c, d, e, F2, K2, 14, 2);
143 R1(e, a, b, c, d, F2, K2, 8, 7);
144 R1(d, e, a, b, c, F2, K2, 13, 0);
145 R1(c, d, e, a, b, F2, K2, 6, 6);
146 R1(b, c, d, e, a, F2, K2, 5, 13);
147 R1(a, b, c, d, e, F2, K2, 12, 11);
148 R1(e, a, b, c, d, F2, K2, 7, 5);
149 R1(d, e, a, b, c, F2, K2, 5, 12); /* #47 */
150 /* Round 4 */
151 R1(c, d, e, a, b, F3, K3, 11, 1);
152 R1(b, c, d, e, a, F3, K3, 12, 9);
153 R1(a, b, c, d, e, F3, K3, 14, 11);
154 R1(e, a, b, c, d, F3, K3, 15, 10);
155 R1(d, e, a, b, c, F3, K3, 14, 0);
156 R1(c, d, e, a, b, F3, K3, 15, 8);
157 R1(b, c, d, e, a, F3, K3, 9, 12);
158 R1(a, b, c, d, e, F3, K3, 8, 4);
159 R1(e, a, b, c, d, F3, K3, 9, 13);
160 R1(d, e, a, b, c, F3, K3, 14, 3);
161 R1(c, d, e, a, b, F3, K3, 5, 7);
162 R1(b, c, d, e, a, F3, K3, 6, 15);
163 R1(a, b, c, d, e, F3, K3, 8, 14);
164 R1(e, a, b, c, d, F3, K3, 6, 5);
165 R1(d, e, a, b, c, F3, K3, 5, 6);
166 R1(c, d, e, a, b, F3, K3, 12, 2); /* #63 */
167 /* Round 5 */
168 R1(b, c, d, e, a, F4, K4, 9, 4);
169 R1(a, b, c, d, e, F4, K4, 15, 0);
170 R1(e, a, b, c, d, F4, K4, 5, 5);
171 R1(d, e, a, b, c, F4, K4, 11, 9);
172 R1(c, d, e, a, b, F4, K4, 6, 7);
173 R1(b, c, d, e, a, F4, K4, 8, 12);
174 R1(a, b, c, d, e, F4, K4, 13, 2);
175 R1(e, a, b, c, d, F4, K4, 12, 10);
176 R1(d, e, a, b, c, F4, K4, 5, 14);
177 R1(c, d, e, a, b, F4, K4, 12, 1);
178 R1(b, c, d, e, a, F4, K4, 13, 3);
179 R1(a, b, c, d, e, F4, K4, 14, 8);
180 R1(e, a, b, c, d, F4, K4, 11, 11);
181 R1(d, e, a, b, c, F4, K4, 8, 6);
182 R1(c, d, e, a, b, F4, K4, 5, 15);
183 R1(b, c, d, e, a, F4, K4, 6, 13); /* #79 */
184
185 aa = a ; bb = b; cc = c; dd = d; ee = e;
186
187 a = h[0]; b = h[1]; c = h[2]; d = h[3]; e = h[4];
188
189 /* Parallel round 1 */
190 R1(a, b, c, d, e, F4, KK0, 8, 5);
191 R1(e, a, b, c, d, F4, KK0, 9, 14);
192 R1(d, e, a, b, c, F4, KK0, 9, 7);
193 R1(c, d, e, a, b, F4, KK0, 11, 0);
194 R1(b, c, d, e, a, F4, KK0, 13, 9);
195 R1(a, b, c, d, e, F4, KK0, 15, 2);
196 R1(e, a, b, c, d, F4, KK0, 15, 11);
197 R1(d, e, a, b, c, F4, KK0, 5, 4);
198 R1(c, d, e, a, b, F4, KK0, 7, 13);
199 R1(b, c, d, e, a, F4, KK0, 7, 6);
200 R1(a, b, c, d, e, F4, KK0, 8, 15);
201 R1(e, a, b, c, d, F4, KK0, 11, 8);
202 R1(d, e, a, b, c, F4, KK0, 14, 1);
203 R1(c, d, e, a, b, F4, KK0, 14, 10);
204 R1(b, c, d, e, a, F4, KK0, 12, 3);
205 R1(a, b, c, d, e, F4, KK0, 6, 12); /* #15 */
206 /* Parallel round 2 */
207 R1(e, a, b, c, d, F3, KK1, 9, 6);
208 R1(d, e, a, b, c, F3, KK1, 13, 11);
209 R1(c, d, e, a, b, F3, KK1, 15, 3);
210 R1(b, c, d, e, a, F3, KK1, 7, 7);
211 R1(a, b, c, d, e, F3, KK1, 12, 0);
212 R1(e, a, b, c, d, F3, KK1, 8, 13);
213 R1(d, e, a, b, c, F3, KK1, 9, 5);
214 R1(c, d, e, a, b, F3, KK1, 11, 10);
215 R1(b, c, d, e, a, F3, KK1, 7, 14);
216 R1(a, b, c, d, e, F3, KK1, 7, 15);
217 R1(e, a, b, c, d, F3, KK1, 12, 8);
218 R1(d, e, a, b, c, F3, KK1, 7, 12);
219 R1(c, d, e, a, b, F3, KK1, 6, 4);
220 R1(b, c, d, e, a, F3, KK1, 15, 9);
221 R1(a, b, c, d, e, F3, KK1, 13, 1);
222 R1(e, a, b, c, d, F3, KK1, 11, 2); /* #31 */
223 /* Parallel round 3 */
224 R1(d, e, a, b, c, F2, KK2, 9, 15);
225 R1(c, d, e, a, b, F2, KK2, 7, 5);
226 R1(b, c, d, e, a, F2, KK2, 15, 1);
227 R1(a, b, c, d, e, F2, KK2, 11, 3);
228 R1(e, a, b, c, d, F2, KK2, 8, 7);
229 R1(d, e, a, b, c, F2, KK2, 6, 14);
230 R1(c, d, e, a, b, F2, KK2, 6, 6);
231 R1(b, c, d, e, a, F2, KK2, 14, 9);
232 R1(a, b, c, d, e, F2, KK2, 12, 11);
233 R1(e, a, b, c, d, F2, KK2, 13, 8);
234 R1(d, e, a, b, c, F2, KK2, 5, 12);
235 R1(c, d, e, a, b, F2, KK2, 14, 2);
236 R1(b, c, d, e, a, F2, KK2, 13, 10);
237 R1(a, b, c, d, e, F2, KK2, 13, 0);
238 R1(e, a, b, c, d, F2, KK2, 7, 4);
239 R1(d, e, a, b, c, F2, KK2, 5, 13); /* #47 */
240 /* Parallel round 4 */
241 R1(c, d, e, a, b, F1, KK3, 15, 8);
242 R1(b, c, d, e, a, F1, KK3, 5, 6);
243 R1(a, b, c, d, e, F1, KK3, 8, 4);
244 R1(e, a, b, c, d, F1, KK3, 11, 1);
245 R1(d, e, a, b, c, F1, KK3, 14, 3);
246 R1(c, d, e, a, b, F1, KK3, 14, 11);
247 R1(b, c, d, e, a, F1, KK3, 6, 15);
248 R1(a, b, c, d, e, F1, KK3, 14, 0);
249 R1(e, a, b, c, d, F1, KK3, 6, 5);
250 R1(d, e, a, b, c, F1, KK3, 9, 12);
251 R1(c, d, e, a, b, F1, KK3, 12, 2);
252 R1(b, c, d, e, a, F1, KK3, 9, 13);
253 R1(a, b, c, d, e, F1, KK3, 12, 9);
254 R1(e, a, b, c, d, F1, KK3, 5, 7);
255 R1(d, e, a, b, c, F1, KK3, 15, 10);
256 R1(c, d, e, a, b, F1, KK3, 8, 14); /* #63 */
257 /* Parallel round 5 */
258 R1(b, c, d, e, a, F0, KK4, 8, 12);
259 R1(a, b, c, d, e, F0, KK4, 5, 15);
260 R1(e, a, b, c, d, F0, KK4, 12, 10);
261 R1(d, e, a, b, c, F0, KK4, 9, 4);
262 R1(c, d, e, a, b, F0, KK4, 12, 1);
263 R1(b, c, d, e, a, F0, KK4, 5, 5);
264 R1(a, b, c, d, e, F0, KK4, 14, 8);
265 R1(e, a, b, c, d, F0, KK4, 6, 7);
266 R1(d, e, a, b, c, F0, KK4, 8, 6);
267 R1(c, d, e, a, b, F0, KK4, 13, 2);
268 R1(b, c, d, e, a, F0, KK4, 6, 13);
269 R1(a, b, c, d, e, F0, KK4, 5, 14);
270 R1(e, a, b, c, d, F0, KK4, 15, 0);
271 R1(d, e, a, b, c, F0, KK4, 13, 3);
272 R1(c, d, e, a, b, F0, KK4, 11, 9);
273 R1(b, c, d, e, a, F0, KK4, 11, 11); /* #79 */
274
275 t = h[1] + cc + d;
276 h[1] = h[2] + dd + e;
277 h[2] = h[3] + ee + a;
278 h[3] = h[4] + aa + b;
279 h[4] = h[0] + bb + c;
280 h[0] = t;
281 }
282 }
283
ripemd_256_transform(uint32_t * h,const uint8_t * block,size_t nb)284 static void ripemd_256_transform(uint32_t *h, const uint8_t *block, size_t nb)
285 {
286 uint32_t a, b, c, d, aa, bb, cc, dd, t, *x;
287 size_t i;
288
289 x = h + 8;
290
291 for(i = 0; i < nb; i++, block += AKMOS_RIPEMD_BLKLEN) {
292 memcpy(x, block, AKMOS_RIPEMD_BLKLEN);
293
294 a = h[0]; b = h[1]; c = h[2]; d = h[3];
295 aa = h[4]; bb = h[5]; cc = h[6]; dd = h[7];
296
297 /* Round 1 */
298 R0(a, b, c, d, F0, K0, 11, 0);
299 R0(d, a, b, c, F0, K0, 14, 1);
300 R0(c, d, a, b, F0, K0, 15, 2);
301 R0(b, c, d, a, F0, K0, 12, 3);
302 R0(a, b, c, d, F0, K0, 5, 4);
303 R0(d, a, b, c, F0, K0, 8, 5);
304 R0(c, d, a, b, F0, K0, 7, 6);
305 R0(b, c, d, a, F0, K0, 9, 7);
306 R0(a, b, c, d, F0, K0, 11, 8);
307 R0(d, a, b, c, F0, K0, 13, 9);
308 R0(c, d, a, b, F0, K0, 14, 10);
309 R0(b, c, d, a, F0, K0, 15, 11);
310 R0(a, b, c, d, F0, K0, 6, 12);
311 R0(d, a, b, c, F0, K0, 7, 13);
312 R0(c, d, a, b, F0, K0, 9, 14);
313 R0(b, c, d, a, F0, K0, 8, 15);
314
315 R0(aa, bb, cc, dd, F3, KK0, 8, 5);
316 R0(dd, aa, bb, cc, F3, KK0, 9, 14);
317 R0(cc, dd, aa, bb, F3, KK0, 9, 7);
318 R0(bb, cc, dd, aa, F3, KK0, 11, 0);
319 R0(aa, bb, cc, dd, F3, KK0, 13, 9);
320 R0(dd, aa, bb, cc, F3, KK0, 15, 2);
321 R0(cc, dd, aa, bb, F3, KK0, 15, 11);
322 R0(bb, cc, dd, aa, F3, KK0, 5, 4);
323 R0(aa, bb, cc, dd, F3, KK0, 7, 13);
324 R0(dd, aa, bb, cc, F3, KK0, 7, 6);
325 R0(cc, dd, aa, bb, F3, KK0, 8, 15);
326 R0(bb, cc, dd, aa, F3, KK0, 11, 8);
327 R0(aa, bb, cc, dd, F3, KK0, 14, 1);
328 R0(dd, aa, bb, cc, F3, KK0, 14, 10);
329 R0(cc, dd, aa, bb, F3, KK0, 12, 3);
330 R0(bb, cc, dd, aa, F3, KK0, 6, 12); /* #15 */
331
332 t = a; a = aa; aa = t;
333
334 /* Round 2 */
335 R0(a, b, c, d, F1, K1, 7, 7);
336 R0(d, a, b, c, F1, K1, 6, 4);
337 R0(c, d, a, b, F1, K1, 8, 13);
338 R0(b, c, d, a, F1, K1, 13, 1);
339 R0(a, b, c, d, F1, K1, 11, 10);
340 R0(d, a, b, c, F1, K1, 9, 6);
341 R0(c, d, a, b, F1, K1, 7, 15);
342 R0(b, c, d, a, F1, K1, 15, 3);
343 R0(a, b, c, d, F1, K1, 7, 12);
344 R0(d, a, b, c, F1, K1, 12, 0);
345 R0(c, d, a, b, F1, K1, 15, 9);
346 R0(b, c, d, a, F1, K1, 9, 5);
347 R0(a, b, c, d, F1, K1, 11, 2);
348 R0(d, a, b, c, F1, K1, 7, 14);
349 R0(c, d, a, b, F1, K1, 13, 11);
350 R0(b, c, d, a, F1, K1, 12, 8);
351
352 R0(aa, bb, cc, dd, F2, KK1, 9, 6);
353 R0(dd, aa, bb, cc, F2, KK1, 13, 11);
354 R0(cc, dd, aa, bb, F2, KK1, 15, 3);
355 R0(bb, cc, dd, aa, F2, KK1, 7, 7);
356 R0(aa, bb, cc, dd, F2, KK1, 12, 0);
357 R0(dd, aa, bb, cc, F2, KK1, 8, 13);
358 R0(cc, dd, aa, bb, F2, KK1, 9, 5);
359 R0(bb, cc, dd, aa, F2, KK1, 11, 10);
360 R0(aa, bb, cc, dd, F2, KK1, 7, 14);
361 R0(dd, aa, bb, cc, F2, KK1, 7, 15);
362 R0(cc, dd, aa, bb, F2, KK1, 12, 8);
363 R0(bb, cc, dd, aa, F2, KK1, 7, 12);
364 R0(aa, bb, cc, dd, F2, KK1, 6, 4);
365 R0(dd, aa, bb, cc, F2, KK1, 15, 9);
366 R0(cc, dd, aa, bb, F2, KK1, 13, 1);
367 R0(bb, cc, dd, aa, F2, KK1, 11, 2); /* #31 */
368
369 t = b; b = bb; bb = t;
370
371 /* Round 3 */
372 R0(a, b, c, d, F2, K2, 11, 3);
373 R0(d, a, b, c, F2, K2, 13, 10);
374 R0(c, d, a, b, F2, K2, 6, 14);
375 R0(b, c, d, a, F2, K2, 7, 4);
376 R0(a, b, c, d, F2, K2, 14, 9);
377 R0(d, a, b, c, F2, K2, 9, 15);
378 R0(c, d, a, b, F2, K2, 13, 8);
379 R0(b, c, d, a, F2, K2, 15, 1);
380 R0(a, b, c, d, F2, K2, 14, 2);
381 R0(d, a, b, c, F2, K2, 8, 7);
382 R0(c, d, a, b, F2, K2, 13, 0);
383 R0(b, c, d, a, F2, K2, 6, 6);
384 R0(a, b, c, d, F2, K2, 5, 13);
385 R0(d, a, b, c, F2, K2, 12, 11);
386 R0(c, d, a, b, F2, K2, 7, 5);
387 R0(b, c, d, a, F2, K2, 5, 12);
388
389 R0(aa, bb, cc, dd, F1, KK2, 9, 15);
390 R0(dd, aa, bb, cc, F1, KK2, 7, 5);
391 R0(cc, dd, aa, bb, F1, KK2, 15, 1);
392 R0(bb, cc, dd, aa, F1, KK2, 11, 3);
393 R0(aa, bb, cc, dd, F1, KK2, 8, 7);
394 R0(dd, aa, bb, cc, F1, KK2, 6, 14);
395 R0(cc, dd, aa, bb, F1, KK2, 6, 6);
396 R0(bb, cc, dd, aa, F1, KK2, 14, 9);
397 R0(aa, bb, cc, dd, F1, KK2, 12, 11);
398 R0(dd, aa, bb, cc, F1, KK2, 13, 8);
399 R0(cc, dd, aa, bb, F1, KK2, 5, 12);
400 R0(bb, cc, dd, aa, F1, KK2, 14, 2);
401 R0(aa, bb, cc, dd, F1, KK2, 13, 10);
402 R0(dd, aa, bb, cc, F1, KK2, 13, 0);
403 R0(cc, dd, aa, bb, F1, KK2, 7, 4);
404 R0(bb, cc, dd, aa, F1, KK2, 5, 13); /* #47 */
405
406 t = c; c = cc; cc = t;
407
408 /* Round 4 */
409 R0(a, b, c, d, F3, K3, 11, 1);
410 R0(d, a, b, c, F3, K3, 12, 9);
411 R0(c, d, a, b, F3, K3, 14, 11);
412 R0(b, c, d, a, F3, K3, 15, 10);
413 R0(a, b, c, d, F3, K3, 14, 0);
414 R0(d, a, b, c, F3, K3, 15, 8);
415 R0(c, d, a, b, F3, K3, 9, 12);
416 R0(b, c, d, a, F3, K3, 8, 4);
417 R0(a, b, c, d, F3, K3, 9, 13);
418 R0(d, a, b, c, F3, K3, 14, 3);
419 R0(c, d, a, b, F3, K3, 5, 7);
420 R0(b, c, d, a, F3, K3, 6, 15);
421 R0(a, b, c, d, F3, K3, 8, 14);
422 R0(d, a, b, c, F3, K3, 6, 5);
423 R0(c, d, a, b, F3, K3, 5, 6);
424 R0(b, c, d, a, F3, K3, 12, 2);
425
426 R0(aa, bb, cc, dd, F0, KK4, 15, 8);
427 R0(dd, aa, bb, cc, F0, KK4, 5, 6);
428 R0(cc, dd, aa, bb, F0, KK4, 8, 4);
429 R0(bb, cc, dd, aa, F0, KK4, 11, 1);
430 R0(aa, bb, cc, dd, F0, KK4, 14, 3);
431 R0(dd, aa, bb, cc, F0, KK4, 14, 11);
432 R0(cc, dd, aa, bb, F0, KK4, 6, 15);
433 R0(bb, cc, dd, aa, F0, KK4, 14, 0);
434 R0(aa, bb, cc, dd, F0, KK4, 6, 5);
435 R0(dd, aa, bb, cc, F0, KK4, 9, 12);
436 R0(cc, dd, aa, bb, F0, KK4, 12, 2);
437 R0(bb, cc, dd, aa, F0, KK4, 9, 13);
438 R0(aa, bb, cc, dd, F0, KK4, 12, 9);
439 R0(dd, aa, bb, cc, F0, KK4, 5, 7);
440 R0(cc, dd, aa, bb, F0, KK4, 15, 10);
441 R0(bb, cc, dd, aa, F0, KK4, 8, 14); /* #63 */
442
443 t = d; d = dd; dd = t;
444
445 h[0] += a; h[1] += b; h[2] += c; h[3] += d;
446 h[4] += aa; h[5] += bb; h[6] += cc; h[7] += dd;
447 }
448 }
449
ripemd_320_transform(uint32_t * h,const uint8_t * block,size_t nb)450 static void ripemd_320_transform(uint32_t *h, const uint8_t *block, size_t nb)
451 {
452 uint32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, *x;
453 size_t i;
454
455 x = h + 10;
456
457 for(i = 0; i < nb; i++, block += AKMOS_RIPEMD_BLKLEN) {
458 memcpy(x, block, AKMOS_RIPEMD_BLKLEN);
459
460 a = h[0]; b = h[1]; c = h[2]; d = h[3]; e = h[4];
461 aa = h[5]; bb = h[6]; cc = h[7]; dd = h[8]; ee = h[9];
462
463 /* Round 1 */
464 R1(a, b, c, d, e, F0, K0, 11, 0);
465 R1(e, a, b, c, d, F0, K0, 14, 1);
466 R1(d, e, a, b, c, F0, K0, 15, 2);
467 R1(c, d, e, a, b, F0, K0, 12, 3);
468 R1(b, c, d, e, a, F0, K0, 5, 4);
469 R1(a, b, c, d, e, F0, K0, 8, 5);
470 R1(e, a, b, c, d, F0, K0, 7, 6);
471 R1(d, e, a, b, c, F0, K0, 9, 7);
472 R1(c, d, e, a, b, F0, K0, 11, 8);
473 R1(b, c, d, e, a, F0, K0, 13, 9);
474 R1(a, b, c, d, e, F0, K0, 14, 10);
475 R1(e, a, b, c, d, F0, K0, 15, 11);
476 R1(d, e, a, b, c, F0, K0, 6, 12);
477 R1(c, d, e, a, b, F0, K0, 7, 13);
478 R1(b, c, d, e, a, F0, K0, 9, 14);
479 R1(a, b, c, d, e, F0, K0, 8, 15);
480
481 R1(aa, bb, cc, dd, ee, F4, KK0, 8, 5);
482 R1(ee, aa, bb, cc, dd, F4, KK0, 9, 14);
483 R1(dd, ee, aa, bb, cc, F4, KK0, 9, 7);
484 R1(cc, dd, ee, aa, bb, F4, KK0, 11, 0);
485 R1(bb, cc, dd, ee, aa, F4, KK0, 13, 9);
486 R1(aa, bb, cc, dd, ee, F4, KK0, 15, 2);
487 R1(ee, aa, bb, cc, dd, F4, KK0, 15, 11);
488 R1(dd, ee, aa, bb, cc, F4, KK0, 5, 4);
489 R1(cc, dd, ee, aa, bb, F4, KK0, 7, 13);
490 R1(bb, cc, dd, ee, aa, F4, KK0, 7, 6);
491 R1(aa, bb, cc, dd, ee, F4, KK0, 8, 15);
492 R1(ee, aa, bb, cc, dd, F4, KK0, 11, 8);
493 R1(dd, ee, aa, bb, cc, F4, KK0, 14, 1);
494 R1(cc, dd, ee, aa, bb, F4, KK0, 14, 10);
495 R1(bb, cc, dd, ee, aa, F4, KK0, 12, 3);
496 R1(aa, bb, cc, dd, ee, F4, KK0, 6, 12); /* #15 */
497
498 t = a; a = aa; aa = t;
499
500 /* Round 2 */
501 R1(e, a, b, c, d, F1, K1, 7, 7);
502 R1(d, e, a, b, c, F1, K1, 6, 4);
503 R1(c, d, e, a, b, F1, K1, 8, 13);
504 R1(b, c, d, e, a, F1, K1, 13, 1);
505 R1(a, b, c, d, e, F1, K1, 11, 10);
506 R1(e, a, b, c, d, F1, K1, 9, 6);
507 R1(d, e, a, b, c, F1, K1, 7, 15);
508 R1(c, d, e, a, b, F1, K1, 15, 3);
509 R1(b, c, d, e, a, F1, K1, 7, 12);
510 R1(a, b, c, d, e, F1, K1, 12, 0);
511 R1(e, a, b, c, d, F1, K1, 15, 9);
512 R1(d, e, a, b, c, F1, K1, 9, 5);
513 R1(c, d, e, a, b, F1, K1, 11, 2);
514 R1(b, c, d, e, a, F1, K1, 7, 14);
515 R1(a, b, c, d, e, F1, K1, 13, 11);
516 R1(e, a, b, c, d, F1, K1, 12, 8);
517
518 R1(ee, aa, bb, cc, dd, F3, KK1, 9, 6);
519 R1(dd, ee, aa, bb, cc, F3, KK1, 13, 11);
520 R1(cc, dd, ee, aa, bb, F3, KK1, 15, 3);
521 R1(bb, cc, dd, ee, aa, F3, KK1, 7, 7);
522 R1(aa, bb, cc, dd, ee, F3, KK1, 12, 0);
523 R1(ee, aa, bb, cc, dd, F3, KK1, 8, 13);
524 R1(dd, ee, aa, bb, cc, F3, KK1, 9, 5);
525 R1(cc, dd, ee, aa, bb, F3, KK1, 11, 10);
526 R1(bb, cc, dd, ee, aa, F3, KK1, 7, 14);
527 R1(aa, bb, cc, dd, ee, F3, KK1, 7, 15);
528 R1(ee, aa, bb, cc, dd, F3, KK1, 12, 8);
529 R1(dd, ee, aa, bb, cc, F3, KK1, 7, 12);
530 R1(cc, dd, ee, aa, bb, F3, KK1, 6, 4);
531 R1(bb, cc, dd, ee, aa, F3, KK1, 15, 9);
532 R1(aa, bb, cc, dd, ee, F3, KK1, 13, 1);
533 R1(ee, aa, bb, cc, dd, F3, KK1, 11, 2); /* #31 */
534
535 t = b; b = bb; bb = t;
536
537 /* Round 3 */
538 R1(d, e, a, b, c, F2, K2, 11, 3);
539 R1(c, d, e, a, b, F2, K2, 13, 10);
540 R1(b, c, d, e, a, F2, K2, 6, 14);
541 R1(a, b, c, d, e, F2, K2, 7, 4);
542 R1(e, a, b, c, d, F2, K2, 14, 9);
543 R1(d, e, a, b, c, F2, K2, 9, 15);
544 R1(c, d, e, a, b, F2, K2, 13, 8);
545 R1(b, c, d, e, a, F2, K2, 15, 1);
546 R1(a, b, c, d, e, F2, K2, 14, 2);
547 R1(e, a, b, c, d, F2, K2, 8, 7);
548 R1(d, e, a, b, c, F2, K2, 13, 0);
549 R1(c, d, e, a, b, F2, K2, 6, 6);
550 R1(b, c, d, e, a, F2, K2, 5, 13);
551 R1(a, b, c, d, e, F2, K2, 12, 11);
552 R1(e, a, b, c, d, F2, K2, 7, 5);
553 R1(d, e, a, b, c, F2, K2, 5, 12);
554
555 R1(dd, ee, aa, bb, cc, F2, KK2, 9, 15);
556 R1(cc, dd, ee, aa, bb, F2, KK2, 7, 5);
557 R1(bb, cc, dd, ee, aa, F2, KK2, 15, 1);
558 R1(aa, bb, cc, dd, ee, F2, KK2, 11, 3);
559 R1(ee, aa, bb, cc, dd, F2, KK2, 8, 7);
560 R1(dd, ee, aa, bb, cc, F2, KK2, 6, 14);
561 R1(cc, dd, ee, aa, bb, F2, KK2, 6, 6);
562 R1(bb, cc, dd, ee, aa, F2, KK2, 14, 9);
563 R1(aa, bb, cc, dd, ee, F2, KK2, 12, 11);
564 R1(ee, aa, bb, cc, dd, F2, KK2, 13, 8);
565 R1(dd, ee, aa, bb, cc, F2, KK2, 5, 12);
566 R1(cc, dd, ee, aa, bb, F2, KK2, 14, 2);
567 R1(bb, cc, dd, ee, aa, F2, KK2, 13, 10);
568 R1(aa, bb, cc, dd, ee, F2, KK2, 13, 0);
569 R1(ee, aa, bb, cc, dd, F2, KK2, 7, 4);
570 R1(dd, ee, aa, bb, cc, F2, KK2, 5, 13); /* #47 */
571
572 t = c; c = cc; cc = t;
573
574 /* Round 4 */
575 R1(c, d, e, a, b, F3, K3, 11, 1);
576 R1(b, c, d, e, a, F3, K3, 12, 9);
577 R1(a, b, c, d, e, F3, K3, 14, 11);
578 R1(e, a, b, c, d, F3, K3, 15, 10);
579 R1(d, e, a, b, c, F3, K3, 14, 0);
580 R1(c, d, e, a, b, F3, K3, 15, 8);
581 R1(b, c, d, e, a, F3, K3, 9, 12);
582 R1(a, b, c, d, e, F3, K3, 8, 4);
583 R1(e, a, b, c, d, F3, K3, 9, 13);
584 R1(d, e, a, b, c, F3, K3, 14, 3);
585 R1(c, d, e, a, b, F3, K3, 5, 7);
586 R1(b, c, d, e, a, F3, K3, 6, 15);
587 R1(a, b, c, d, e, F3, K3, 8, 14);
588 R1(e, a, b, c, d, F3, K3, 6, 5);
589 R1(d, e, a, b, c, F3, K3, 5, 6);
590 R1(c, d, e, a, b, F3, K3, 12, 2);
591
592 R1(cc, dd, ee, aa, bb, F1, KK3, 15, 8);
593 R1(bb, cc, dd, ee, aa, F1, KK3, 5, 6);
594 R1(aa, bb, cc, dd, ee, F1, KK3, 8, 4);
595 R1(ee, aa, bb, cc, dd, F1, KK3, 11, 1);
596 R1(dd, ee, aa, bb, cc, F1, KK3, 14, 3);
597 R1(cc, dd, ee, aa, bb, F1, KK3, 14, 11);
598 R1(bb, cc, dd, ee, aa, F1, KK3, 6, 15);
599 R1(aa, bb, cc, dd, ee, F1, KK3, 14, 0);
600 R1(ee, aa, bb, cc, dd, F1, KK3, 6, 5);
601 R1(dd, ee, aa, bb, cc, F1, KK3, 9, 12);
602 R1(cc, dd, ee, aa, bb, F1, KK3, 12, 2);
603 R1(bb, cc, dd, ee, aa, F1, KK3, 9, 13);
604 R1(aa, bb, cc, dd, ee, F1, KK3, 12, 9);
605 R1(ee, aa, bb, cc, dd, F1, KK3, 5, 7);
606 R1(dd, ee, aa, bb, cc, F1, KK3, 15, 10);
607 R1(cc, dd, ee, aa, bb, F1, KK3, 8, 14); /* #63 */
608
609 t = d; d = dd; dd = t;
610
611 /* Round 5 */
612 R1(b, c, d, e, a, F4, K4, 9, 4);
613 R1(a, b, c, d, e, F4, K4, 15, 0);
614 R1(e, a, b, c, d, F4, K4, 5, 5);
615 R1(d, e, a, b, c, F4, K4, 11, 9);
616 R1(c, d, e, a, b, F4, K4, 6, 7);
617 R1(b, c, d, e, a, F4, K4, 8, 12);
618 R1(a, b, c, d, e, F4, K4, 13, 2);
619 R1(e, a, b, c, d, F4, K4, 12, 10);
620 R1(d, e, a, b, c, F4, K4, 5, 14);
621 R1(c, d, e, a, b, F4, K4, 12, 1);
622 R1(b, c, d, e, a, F4, K4, 13, 3);
623 R1(a, b, c, d, e, F4, K4, 14, 8);
624 R1(e, a, b, c, d, F4, K4, 11, 11);
625 R1(d, e, a, b, c, F4, K4, 8, 6);
626 R1(c, d, e, a, b, F4, K4, 5, 15);
627 R1(b, c, d, e, a, F4, K4, 6, 13);
628
629 R1(bb, cc, dd, ee, aa, F0, KK4, 8, 12);
630 R1(aa, bb, cc, dd, ee, F0, KK4, 5, 15);
631 R1(ee, aa, bb, cc, dd, F0, KK4, 12, 10);
632 R1(dd, ee, aa, bb, cc, F0, KK4, 9, 4);
633 R1(cc, dd, ee, aa, bb, F0, KK4, 12, 1);
634 R1(bb, cc, dd, ee, aa, F0, KK4, 5, 5);
635 R1(aa, bb, cc, dd, ee, F0, KK4, 14, 8);
636 R1(ee, aa, bb, cc, dd, F0, KK4, 6, 7);
637 R1(dd, ee, aa, bb, cc, F0, KK4, 8, 6);
638 R1(cc, dd, ee, aa, bb, F0, KK4, 13, 2);
639 R1(bb, cc, dd, ee, aa, F0, KK4, 6, 13);
640 R1(aa, bb, cc, dd, ee, F0, KK4, 5, 14);
641 R1(ee, aa, bb, cc, dd, F0, KK4, 15, 0);
642 R1(dd, ee, aa, bb, cc, F0, KK4, 13, 3);
643 R1(cc, dd, ee, aa, bb, F0, KK4, 11, 9);
644 R1(bb, cc, dd, ee, aa, F0, KK4, 11, 11); /* #79 */
645
646 t = e; e = ee; ee = t;
647
648 h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e;
649 h[5] += aa; h[6] += bb; h[7] += cc; h[8] += dd; h[9] += ee;
650 }
651 }
652
akmos_ripemd_160_init(akmos_digest_algo_t * uctx)653 void akmos_ripemd_160_init(akmos_digest_algo_t *uctx)
654 {
655 akmos_ripemd_t *ctx;
656
657 ctx = &uctx->ripemd;
658
659 ctx->h[0] = H0;
660 ctx->h[1] = H1;
661 ctx->h[2] = H2;
662 ctx->h[3] = H3;
663 ctx->h[4] = H4;
664
665 ctx->total = ctx->len = 0;
666 ctx->diglen = AKMOS_RIPEMD_160_DIGLEN;
667
668 ctx->transform = ripemd_160_transform;
669 }
670
akmos_ripemd_256_init(akmos_digest_algo_t * uctx)671 void akmos_ripemd_256_init(akmos_digest_algo_t *uctx)
672 {
673 akmos_ripemd_t *ctx;
674
675 ctx = &uctx->ripemd;
676
677 ctx->h[0] = H0;
678 ctx->h[1] = H1;
679 ctx->h[2] = H2;
680 ctx->h[3] = H3;
681 ctx->h[4] = H5;
682 ctx->h[5] = H6;
683 ctx->h[6] = H7;
684 ctx->h[7] = H8;
685
686 ctx->total = ctx->len = 0;
687 ctx->diglen = AKMOS_RIPEMD_256_DIGLEN;
688
689 ctx->transform = ripemd_256_transform;
690 }
691
akmos_ripemd_320_init(akmos_digest_algo_t * uctx)692 void akmos_ripemd_320_init(akmos_digest_algo_t *uctx)
693 {
694 akmos_ripemd_t *ctx;
695
696 ctx = &uctx->ripemd;
697
698 ctx->h[0] = H0;
699 ctx->h[1] = H1;
700 ctx->h[2] = H2;
701 ctx->h[3] = H3;
702 ctx->h[4] = H4;
703 ctx->h[5] = H5;
704 ctx->h[6] = H6;
705 ctx->h[7] = H7;
706 ctx->h[8] = H8;
707 ctx->h[9] = H9;
708
709 ctx->total = ctx->len = 0;
710 ctx->diglen = AKMOS_RIPEMD_320_DIGLEN;
711
712 ctx->transform = ripemd_320_transform;
713 }
714
akmos_ripemd_update(akmos_digest_algo_t * uctx,const uint8_t * input,size_t len)715 void akmos_ripemd_update(akmos_digest_algo_t *uctx, const uint8_t *input, size_t len)
716 {
717 akmos_ripemd_t *ctx;
718 size_t nb, tmp_len;
719
720 ctx = &uctx->ripemd;
721
722 tmp_len = len + ctx->len;
723
724 if(tmp_len < AKMOS_RIPEMD_BLKLEN) {
725 memcpy(ctx->block + ctx->len, input, len);
726 ctx->len += len;
727 return;
728 }
729
730 if(ctx->len) {
731 tmp_len = AKMOS_RIPEMD_BLKLEN - ctx->len;
732 memcpy(ctx->block + ctx->len, input, tmp_len);
733
734 ctx->transform(ctx->h, ctx->block, 1 & SIZE_T_MAX);
735
736 ctx->len = 0;
737 ctx->total++;
738
739 len -= tmp_len;
740 input += tmp_len;
741 }
742
743 nb = len / AKMOS_RIPEMD_BLKLEN;
744 if(nb)
745 ctx->transform(ctx->h, input, nb);
746
747 tmp_len = len % AKMOS_RIPEMD_BLKLEN;
748 if(tmp_len) {
749 memcpy(ctx->block, input + (len - tmp_len), tmp_len);
750 ctx->len = tmp_len;
751 }
752
753 ctx->total += nb;
754 }
755
akmos_ripemd_done(akmos_digest_algo_t * uctx,uint8_t * digest)756 void akmos_ripemd_done(akmos_digest_algo_t *uctx, uint8_t *digest)
757 {
758 akmos_ripemd_t *ctx;
759 uint64_t len_b;
760 size_t i;
761
762 ctx = &uctx->ripemd;
763
764 len_b = ((ctx->total * AKMOS_RIPEMD_BLKLEN) + ctx->len) * 8;
765 ctx->block[ctx->len] = 0x80;
766 ctx->len++;
767
768 if(ctx->len > (AKMOS_RIPEMD_BLKLEN - sizeof(uint64_t))) {
769 memset(ctx->block + ctx->len, 0, AKMOS_RIPEMD_BLKLEN - ctx->len);
770 ctx->transform(ctx->h, ctx->block, 1);
771 ctx->len = 0;
772 }
773
774 memset(ctx->block + ctx->len, 0, AKMOS_RIPEMD_BLKLEN - ctx->len);
775 UNPACK64BE(ctx->block + (AKMOS_RIPEMD_BLKLEN - sizeof(uint64_t)), len_b);
776 ctx->transform(ctx->h, ctx->block, 1);
777
778 for(i = 0; i < (ctx->diglen / 4); i++, digest += sizeof(uint32_t))
779 UNPACK32BE(digest, ctx->h[i]);
780 }
781