ripemd.c 17 KB

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