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