ripemd.c 17 KB

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