You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

557 lines
17KB

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