1 /*
2 * Copyright (C) 2007 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (C) 2013 James Almer
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stddef.h>
23 #include <string.h>
24
25 #include "attributes.h"
26 #include "avutil.h"
27 #include "bswap.h"
28 #include "intreadwrite.h"
29 #include "ripemd.h"
30 #include "mem.h"
31
32 /** hash context */
33 typedef struct AVRIPEMD {
34 uint8_t digest_len; ///< digest length in 32-bit words
35 uint64_t count; ///< number of bytes in buffer
36 uint8_t buffer[64]; ///< 512-bit buffer of input values used in hash updating
37 uint32_t state[10]; ///< current hash value
38 /** function used to update hash for 512-bit input block */
39 void (*transform)(uint32_t *state, const uint8_t buffer[64]);
40 } AVRIPEMD;
41
42 const int av_ripemd_size = sizeof(AVRIPEMD);
43
av_ripemd_alloc(void)44 struct AVRIPEMD *av_ripemd_alloc(void)
45 {
46 return av_mallocz(sizeof(struct AVRIPEMD));
47 }
48
49 static const uint32_t KA[4] = {
50 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e
51 };
52
53 static const uint32_t KB[4] = {
54 0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9
55 };
56
57 static const int ROTA[80] = {
58 11, 14, 15, 12, 5, 8, 7 , 9, 11, 13, 14, 15, 6, 7, 9, 8,
59 7 , 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
60 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
61 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
62 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
63 };
64
65 static const int ROTB[80] = {
66 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
67 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
68 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
69 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
70 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
71 };
72
73 static const int WA[80] = {
74 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
75 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
76 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
77 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
78 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
79 };
80
81 static const int WB[80] = {
82 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
83 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
84 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
85 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
86 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
87 };
88
89 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
90
91 #define ROUND128_0_TO_15(a,b,c,d,e,f,g,h) \
92 a = rol(a + (( b ^ c ^ d) + block[WA[n]]), ROTA[n]); \
93 e = rol(e + ((((f ^ g) & h) ^ g) + block[WB[n]] + KB[0]), ROTB[n]); \
94 n++
95
96 #define ROUND128_16_TO_31(a,b,c,d,e,f,g,h) \
97 a = rol(a + ((((c ^ d) & b) ^ d) + block[WA[n]] + KA[0]), ROTA[n]); \
98 e = rol(e + (((~g | f) ^ h) + block[WB[n]] + KB[1]), ROTB[n]); \
99 n++
100
101 #define ROUND128_32_TO_47(a,b,c,d,e,f,g,h) \
102 a = rol(a + (((~c | b) ^ d) + block[WA[n]] + KA[1]), ROTA[n]); \
103 e = rol(e + ((((g ^ h) & f) ^ h) + block[WB[n]] + KB[2]), ROTB[n]); \
104 n++
105
106 #define ROUND128_48_TO_63(a,b,c,d,e,f,g,h) \
107 a = rol(a + ((((b ^ c) & d) ^ c) + block[WA[n]] + KA[2]), ROTA[n]); \
108 e = rol(e + (( f ^ g ^ h) + block[WB[n]]), ROTB[n]); \
109 n++
110
111 #define R128_0 \
112 ROUND128_0_TO_15(a,b,c,d,e,f,g,h); \
113 ROUND128_0_TO_15(d,a,b,c,h,e,f,g); \
114 ROUND128_0_TO_15(c,d,a,b,g,h,e,f); \
115 ROUND128_0_TO_15(b,c,d,a,f,g,h,e)
116
117 #define R128_16 \
118 ROUND128_16_TO_31(a,b,c,d,e,f,g,h); \
119 ROUND128_16_TO_31(d,a,b,c,h,e,f,g); \
120 ROUND128_16_TO_31(c,d,a,b,g,h,e,f); \
121 ROUND128_16_TO_31(b,c,d,a,f,g,h,e)
122
123 #define R128_32 \
124 ROUND128_32_TO_47(a,b,c,d,e,f,g,h); \
125 ROUND128_32_TO_47(d,a,b,c,h,e,f,g); \
126 ROUND128_32_TO_47(c,d,a,b,g,h,e,f); \
127 ROUND128_32_TO_47(b,c,d,a,f,g,h,e)
128
129 #define R128_48 \
130 ROUND128_48_TO_63(a,b,c,d,e,f,g,h); \
131 ROUND128_48_TO_63(d,a,b,c,h,e,f,g); \
132 ROUND128_48_TO_63(c,d,a,b,g,h,e,f); \
133 ROUND128_48_TO_63(b,c,d,a,f,g,h,e)
134
ripemd128_transform(uint32_t * state,const uint8_t buffer[64])135 static void ripemd128_transform(uint32_t *state, const uint8_t buffer[64])
136 {
137 uint32_t a, b, c, d, e, f, g, h, av_unused t;
138 uint32_t block[16];
139 int n;
140
141 a = e = state[0];
142 b = f = state[1];
143 c = g = state[2];
144 d = h = state[3];
145
146 for (n = 0; n < 16; n++)
147 block[n] = AV_RL32(buffer + 4 * n);
148 n = 0;
149
150 #if CONFIG_SMALL
151 for (; n < 16;) {
152 ROUND128_0_TO_15(a,b,c,d,e,f,g,h);
153 t = d; d = c; c = b; b = a; a = t;
154 t = h; h = g; g = f; f = e; e = t;
155 }
156
157 for (; n < 32;) {
158 ROUND128_16_TO_31(a,b,c,d,e,f,g,h);
159 t = d; d = c; c = b; b = a; a = t;
160 t = h; h = g; g = f; f = e; e = t;
161 }
162
163 for (; n < 48;) {
164 ROUND128_32_TO_47(a,b,c,d,e,f,g,h);
165 t = d; d = c; c = b; b = a; a = t;
166 t = h; h = g; g = f; f = e; e = t;
167 }
168
169 for (; n < 64;) {
170 ROUND128_48_TO_63(a,b,c,d,e,f,g,h);
171 t = d; d = c; c = b; b = a; a = t;
172 t = h; h = g; g = f; f = e; e = t;
173 }
174 #else
175
176 R128_0; R128_0; R128_0; R128_0;
177
178 R128_16; R128_16; R128_16; R128_16;
179
180 R128_32; R128_32; R128_32; R128_32;
181
182 R128_48; R128_48; R128_48; R128_48;
183 #endif
184
185 h += c + state[1];
186 state[1] = state[2] + d + e;
187 state[2] = state[3] + a + f;
188 state[3] = state[0] + b + g;
189 state[0] = h;
190 }
191
ripemd256_transform(uint32_t * state,const uint8_t buffer[64])192 static void ripemd256_transform(uint32_t *state, const uint8_t buffer[64])
193 {
194 uint32_t a, b, c, d, e, f, g, h, av_unused t;
195 uint32_t block[16];
196 int n;
197
198 a = state[0]; b = state[1]; c = state[2]; d = state[3];
199 e = state[4]; f = state[5]; g = state[6]; h = state[7];
200
201 for (n = 0; n < 16; n++)
202 block[n] = AV_RL32(buffer + 4 * n);
203 n = 0;
204
205 #if CONFIG_SMALL
206 for (; n < 16;) {
207 ROUND128_0_TO_15(a,b,c,d,e,f,g,h);
208 t = d; d = c; c = b; b = a; a = t;
209 t = h; h = g; g = f; f = e; e = t;
210 }
211 FFSWAP(uint32_t, a, e);
212
213 for (; n < 32;) {
214 ROUND128_16_TO_31(a,b,c,d,e,f,g,h);
215 t = d; d = c; c = b; b = a; a = t;
216 t = h; h = g; g = f; f = e; e = t;
217 }
218 FFSWAP(uint32_t, b, f);
219
220 for (; n < 48;) {
221 ROUND128_32_TO_47(a,b,c,d,e,f,g,h);
222 t = d; d = c; c = b; b = a; a = t;
223 t = h; h = g; g = f; f = e; e = t;
224 }
225 FFSWAP(uint32_t, c, g);
226
227 for (; n < 64;) {
228 ROUND128_48_TO_63(a,b,c,d,e,f,g,h);
229 t = d; d = c; c = b; b = a; a = t;
230 t = h; h = g; g = f; f = e; e = t;
231 }
232 FFSWAP(uint32_t, d, h);
233 #else
234
235 R128_0; R128_0; R128_0; R128_0;
236 FFSWAP(uint32_t, a, e);
237
238 R128_16; R128_16; R128_16; R128_16;
239 FFSWAP(uint32_t, b, f);
240
241 R128_32; R128_32; R128_32; R128_32;
242 FFSWAP(uint32_t, c, g);
243
244 R128_48; R128_48; R128_48; R128_48;
245 FFSWAP(uint32_t, d, h);
246 #endif
247
248 state[0] += a; state[1] += b; state[2] += c; state[3] += d;
249 state[4] += e; state[5] += f; state[6] += g; state[7] += h;
250 }
251
252 #define ROTATE(x,y) \
253 x = rol(x, 10); \
254 y = rol(y, 10); \
255 n++
256
257 #define ROUND160_0_TO_15(a,b,c,d,e,f,g,h,i,j) \
258 a = rol(a + (( b ^ c ^ d) + block[WA[n]]), ROTA[n]) + e; \
259 f = rol(f + (((~i | h) ^ g) + block[WB[n]] + KB[0]), ROTB[n]) + j; \
260 ROTATE(c,h)
261
262 #define ROUND160_16_TO_31(a,b,c,d,e,f,g,h,i,j) \
263 a = rol(a + ((((c ^ d) & b) ^ d) + block[WA[n]] + KA[0]), ROTA[n]) + e; \
264 f = rol(f + ((((g ^ h) & i) ^ h) + block[WB[n]] + KB[1]), ROTB[n]) + j; \
265 ROTATE(c,h)
266
267 #define ROUND160_32_TO_47(a,b,c,d,e,f,g,h,i,j) \
268 a = rol(a + (((~c | b) ^ d) + block[WA[n]] + KA[1]), ROTA[n]) + e; \
269 f = rol(f + (((~h | g) ^ i) + block[WB[n]] + KB[2]), ROTB[n]) + j; \
270 ROTATE(c,h)
271
272 #define ROUND160_48_TO_63(a,b,c,d,e,f,g,h,i,j) \
273 a = rol(a + ((((b ^ c) & d) ^ c) + block[WA[n]] + KA[2]), ROTA[n]) + e; \
274 f = rol(f + ((((h ^ i) & g) ^ i) + block[WB[n]] + KB[3]), ROTB[n]) + j; \
275 ROTATE(c,h)
276
277 #define ROUND160_64_TO_79(a,b,c,d,e,f,g,h,i,j) \
278 a = rol(a + (((~d | c) ^ b) + block[WA[n]] + KA[3]), ROTA[n]) + e; \
279 f = rol(f + (( g ^ h ^ i) + block[WB[n]]), ROTB[n]) + j; \
280 ROTATE(c,h)
281
282 #define R160_0 \
283 ROUND160_0_TO_15(a,b,c,d,e,f,g,h,i,j); \
284 ROUND160_0_TO_15(e,a,b,c,d,j,f,g,h,i); \
285 ROUND160_0_TO_15(d,e,a,b,c,i,j,f,g,h); \
286 ROUND160_0_TO_15(c,d,e,a,b,h,i,j,f,g); \
287 ROUND160_0_TO_15(b,c,d,e,a,g,h,i,j,f)
288
289 #define R160_16 \
290 ROUND160_16_TO_31(e,a,b,c,d,j,f,g,h,i); \
291 ROUND160_16_TO_31(d,e,a,b,c,i,j,f,g,h); \
292 ROUND160_16_TO_31(c,d,e,a,b,h,i,j,f,g); \
293 ROUND160_16_TO_31(b,c,d,e,a,g,h,i,j,f); \
294 ROUND160_16_TO_31(a,b,c,d,e,f,g,h,i,j)
295
296 #define R160_32 \
297 ROUND160_32_TO_47(d,e,a,b,c,i,j,f,g,h); \
298 ROUND160_32_TO_47(c,d,e,a,b,h,i,j,f,g); \
299 ROUND160_32_TO_47(b,c,d,e,a,g,h,i,j,f); \
300 ROUND160_32_TO_47(a,b,c,d,e,f,g,h,i,j); \
301 ROUND160_32_TO_47(e,a,b,c,d,j,f,g,h,i)
302
303 #define R160_48 \
304 ROUND160_48_TO_63(c,d,e,a,b,h,i,j,f,g); \
305 ROUND160_48_TO_63(b,c,d,e,a,g,h,i,j,f); \
306 ROUND160_48_TO_63(a,b,c,d,e,f,g,h,i,j); \
307 ROUND160_48_TO_63(e,a,b,c,d,j,f,g,h,i); \
308 ROUND160_48_TO_63(d,e,a,b,c,i,j,f,g,h)
309
310 #define R160_64 \
311 ROUND160_64_TO_79(b,c,d,e,a,g,h,i,j,f); \
312 ROUND160_64_TO_79(a,b,c,d,e,f,g,h,i,j); \
313 ROUND160_64_TO_79(e,a,b,c,d,j,f,g,h,i); \
314 ROUND160_64_TO_79(d,e,a,b,c,i,j,f,g,h); \
315 ROUND160_64_TO_79(c,d,e,a,b,h,i,j,f,g)
316
ripemd160_transform(uint32_t * state,const uint8_t buffer[64])317 static void ripemd160_transform(uint32_t *state, const uint8_t buffer[64])
318 {
319 uint32_t a, b, c, d, e, f, g, h, i, j, av_unused t;
320 uint32_t block[16];
321 int n;
322
323 a = f = state[0];
324 b = g = state[1];
325 c = h = state[2];
326 d = i = state[3];
327 e = j = state[4];
328
329 for (n = 0; n < 16; n++)
330 block[n] = AV_RL32(buffer + 4 * n);
331 n = 0;
332
333 #if CONFIG_SMALL
334 for (; n < 16;) {
335 ROUND160_0_TO_15(a,b,c,d,e,f,g,h,i,j);
336 t = e; e = d; d = c; c = b; b = a; a = t;
337 t = j; j = i; i = h; h = g; g = f; f = t;
338 }
339
340 for (; n < 32;) {
341 ROUND160_16_TO_31(a,b,c,d,e,f,g,h,i,j);
342 t = e; e = d; d = c; c = b; b = a; a = t;
343 t = j; j = i; i = h; h = g; g = f; f = t;
344 }
345
346 for (; n < 48;) {
347 ROUND160_32_TO_47(a,b,c,d,e,f,g,h,i,j);
348 t = e; e = d; d = c; c = b; b = a; a = t;
349 t = j; j = i; i = h; h = g; g = f; f = t;
350 }
351
352 for (; n < 64;) {
353 ROUND160_48_TO_63(a,b,c,d,e,f,g,h,i,j);
354 t = e; e = d; d = c; c = b; b = a; a = t;
355 t = j; j = i; i = h; h = g; g = f; f = t;
356 }
357
358 for (; n < 80;) {
359 ROUND160_64_TO_79(a,b,c,d,e,f,g,h,i,j);
360 t = e; e = d; d = c; c = b; b = a; a = t;
361 t = j; j = i; i = h; h = g; g = f; f = t;
362 }
363 #else
364
365 R160_0; R160_0; R160_0;
366 ROUND160_0_TO_15(a,b,c,d,e,f,g,h,i,j);
367
368 R160_16; R160_16; R160_16;
369 ROUND160_16_TO_31(e,a,b,c,d,j,f,g,h,i);
370
371 R160_32; R160_32; R160_32;
372 ROUND160_32_TO_47(d,e,a,b,c,i,j,f,g,h);
373
374 R160_48; R160_48; R160_48;
375 ROUND160_48_TO_63(c,d,e,a,b,h,i,j,f,g);
376
377 R160_64; R160_64; R160_64;
378 ROUND160_64_TO_79(b,c,d,e,a,g,h,i,j,f);
379 #endif
380
381 i += c + state[1];
382 state[1] = state[2] + d + j;
383 state[2] = state[3] + e + f;
384 state[3] = state[4] + a + g;
385 state[4] = state[0] + b + h;
386 state[0] = i;
387 }
388
ripemd320_transform(uint32_t * state,const uint8_t buffer[64])389 static void ripemd320_transform(uint32_t *state, const uint8_t buffer[64])
390 {
391 uint32_t a, b, c, d, e, f, g, h, i, j, av_unused t;
392 uint32_t block[16];
393 int n;
394
395 a = state[0]; b = state[1]; c = state[2]; d = state[3]; e = state[4];
396 f = state[5]; g = state[6]; h = state[7]; i = state[8]; j = state[9];
397
398 for (n = 0; n < 16; n++)
399 block[n] = AV_RL32(buffer + 4 * n);
400 n = 0;
401
402 #if CONFIG_SMALL
403 for (; n < 16;) {
404 ROUND160_0_TO_15(a,b,c,d,e,f,g,h,i,j);
405 t = e; e = d; d = c; c = b; b = a; a = t;
406 t = j; j = i; i = h; h = g; g = f; f = t;
407 }
408 FFSWAP(uint32_t, b, g);
409
410 for (; n < 32;) {
411 ROUND160_16_TO_31(a,b,c,d,e,f,g,h,i,j);
412 t = e; e = d; d = c; c = b; b = a; a = t;
413 t = j; j = i; i = h; h = g; g = f; f = t;
414 }
415 FFSWAP(uint32_t, d, i);
416
417 for (; n < 48;) {
418 ROUND160_32_TO_47(a,b,c,d,e,f,g,h,i,j);
419 t = e; e = d; d = c; c = b; b = a; a = t;
420 t = j; j = i; i = h; h = g; g = f; f = t;
421 }
422 FFSWAP(uint32_t, a, f);
423
424 for (; n < 64;) {
425 ROUND160_48_TO_63(a,b,c,d,e,f,g,h,i,j);
426 t = e; e = d; d = c; c = b; b = a; a = t;
427 t = j; j = i; i = h; h = g; g = f; f = t;
428 }
429 FFSWAP(uint32_t, c, h);
430
431 for (; n < 80;) {
432 ROUND160_64_TO_79(a,b,c,d,e,f,g,h,i,j);
433 t = e; e = d; d = c; c = b; b = a; a = t;
434 t = j; j = i; i = h; h = g; g = f; f = t;
435 }
436 FFSWAP(uint32_t, e, j);
437 #else
438
439 R160_0; R160_0; R160_0;
440 ROUND160_0_TO_15(a,b,c,d,e,f,g,h,i,j);
441 FFSWAP(uint32_t, a, f);
442
443 R160_16; R160_16; R160_16;
444 ROUND160_16_TO_31(e,a,b,c,d,j,f,g,h,i);
445 FFSWAP(uint32_t, b, g);
446
447 R160_32; R160_32; R160_32;
448 ROUND160_32_TO_47(d,e,a,b,c,i,j,f,g,h);
449 FFSWAP(uint32_t, c, h);
450
451 R160_48; R160_48; R160_48;
452 ROUND160_48_TO_63(c,d,e,a,b,h,i,j,f,g);
453 FFSWAP(uint32_t, d, i);
454
455 R160_64; R160_64; R160_64;
456 ROUND160_64_TO_79(b,c,d,e,a,g,h,i,j,f);
457 FFSWAP(uint32_t, e, j);
458 #endif
459
460 state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e;
461 state[5] += f; state[6] += g; state[7] += h; state[8] += i; state[9] += j;
462 }
463
av_ripemd_init(AVRIPEMD * ctx,int bits)464 av_cold int av_ripemd_init(AVRIPEMD *ctx, int bits)
465 {
466 ctx->digest_len = bits >> 5;
467 switch (bits) {
468 case 128: // RIPEMD-128
469 ctx->state[0] = 0x67452301;
470 ctx->state[1] = 0xEFCDAB89;
471 ctx->state[2] = 0x98BADCFE;
472 ctx->state[3] = 0x10325476;
473 ctx->transform = ripemd128_transform;
474 break;
475 case 160: // RIPEMD-160
476 ctx->state[0] = 0x67452301;
477 ctx->state[1] = 0xEFCDAB89;
478 ctx->state[2] = 0x98BADCFE;
479 ctx->state[3] = 0x10325476;
480 ctx->state[4] = 0xC3D2E1F0;
481 ctx->transform = ripemd160_transform;
482 break;
483 case 256: // RIPEMD-256
484 ctx->state[0] = 0x67452301;
485 ctx->state[1] = 0xEFCDAB89;
486 ctx->state[2] = 0x98BADCFE;
487 ctx->state[3] = 0x10325476;
488 ctx->state[4] = 0x76543210;
489 ctx->state[5] = 0xFEDCBA98;
490 ctx->state[6] = 0x89ABCDEF;
491 ctx->state[7] = 0x01234567;
492 ctx->transform = ripemd256_transform;
493 break;
494 case 320: // RIPEMD-320
495 ctx->state[0] = 0x67452301;
496 ctx->state[1] = 0xEFCDAB89;
497 ctx->state[2] = 0x98BADCFE;
498 ctx->state[3] = 0x10325476;
499 ctx->state[4] = 0xC3D2E1F0;
500 ctx->state[5] = 0x76543210;
501 ctx->state[6] = 0xFEDCBA98;
502 ctx->state[7] = 0x89ABCDEF;
503 ctx->state[8] = 0x01234567;
504 ctx->state[9] = 0x3C2D1E0F;
505 ctx->transform = ripemd320_transform;
506 break;
507 default:
508 return AVERROR(EINVAL);
509 }
510 ctx->count = 0;
511 return 0;
512 }
513
514 #if FF_API_CRYPTO_SIZE_T
av_ripemd_update(AVRIPEMD * ctx,const uint8_t * data,unsigned int len)515 void av_ripemd_update(AVRIPEMD* ctx, const uint8_t* data, unsigned int len)
516 #else
517 void av_ripemd_update(AVRIPEMD* ctx, const uint8_t* data, size_t len)
518 #endif
519 {
520 unsigned int i, j;
521
522 j = ctx->count & 63;
523 ctx->count += len;
524 #if CONFIG_SMALL
525 for (i = 0; i < len; i++) {
526 ctx->buffer[j++] = data[i];
527 if (64 == j) {
528 ctx->transform(ctx->state, ctx->buffer);
529 j = 0;
530 }
531 }
532 #else
533 if ((j + len) > 63) {
534 memcpy(&ctx->buffer[j], data, (i = 64 - j));
535 ctx->transform(ctx->state, ctx->buffer);
536 for (; i + 63 < len; i += 64)
537 ctx->transform(ctx->state, &data[i]);
538 j = 0;
539 } else
540 i = 0;
541 memcpy(&ctx->buffer[j], &data[i], len - i);
542 #endif
543 }
544
av_ripemd_final(AVRIPEMD * ctx,uint8_t * digest)545 void av_ripemd_final(AVRIPEMD* ctx, uint8_t *digest)
546 {
547 int i;
548 uint64_t finalcount = av_le2ne64(ctx->count << 3);
549
550 av_ripemd_update(ctx, "\200", 1);
551 while ((ctx->count & 63) != 56)
552 av_ripemd_update(ctx, "", 1);
553 av_ripemd_update(ctx, (uint8_t *)&finalcount, 8); /* Should cause a transform() */
554 for (i = 0; i < ctx->digest_len; i++)
555 AV_WL32(digest + i*4, ctx->state[i]);
556 }
557