Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright (c) 2014 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "crypto/ripemd160.h"
  5. #include "crypto/common.h"
  6. #include <string.h>
  7. // Internal implementation code.
  8. namespace
  9. {
  10. /// Internal RIPEMD-160 implementation.
  11. namespace ripemd160
  12. {
  13. uint32_t inline f1(uint32_t x, uint32_t y, uint32_t z) { return x ^ y ^ z; }
  14. uint32_t inline f2(uint32_t x, uint32_t y, uint32_t z) { return (x & y) | (~x & z); }
  15. uint32_t inline f3(uint32_t x, uint32_t y, uint32_t z) { return (x | ~y) ^ z; }
  16. uint32_t inline f4(uint32_t x, uint32_t y, uint32_t z) { return (x & z) | (y & ~z); }
  17. uint32_t inline f5(uint32_t x, uint32_t y, uint32_t z) { return x ^ (y | ~z); }
  18. /** Initialize RIPEMD-160 state. */
  19. void inline Initialize(uint32_t* s)
  20. {
  21. s[0] = 0x67452301ul;
  22. s[1] = 0xEFCDAB89ul;
  23. s[2] = 0x98BADCFEul;
  24. s[3] = 0x10325476ul;
  25. s[4] = 0xC3D2E1F0ul;
  26. }
  27. uint32_t inline rol(uint32_t x, int i) { return (x << i) | (x >> (32 - i)); }
  28. void inline Round(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r)
  29. {
  30. a = rol(a + f + x + k, r) + e;
  31. c = rol(c, 10);
  32. }
  33. void inline R11(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, c, d), x, 0, r); }
  34. void inline R21(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, c, d), x, 0x5A827999ul, r); }
  35. void inline R31(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, c, d), x, 0x6ED9EBA1ul, r); }
  36. void inline R41(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, c, d), x, 0x8F1BBCDCul, r); }
  37. void inline R51(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, c, d), x, 0xA953FD4Eul, r); }
  38. void inline R12(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, c, d), x, 0x50A28BE6ul, r); }
  39. void inline R22(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, c, d), x, 0x5C4DD124ul, r); }
  40. void inline R32(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, c, d), x, 0x6D703EF3ul, r); }
  41. void inline R42(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, c, d), x, 0x7A6D76E9ul, r); }
  42. void inline R52(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, c, d), x, 0, r); }
  43. /** Perform a RIPEMD-160 transformation, processing a 64-byte chunk. */
  44. void Transform(uint32_t* s, const unsigned char* chunk)
  45. {
  46. uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4];
  47. uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1;
  48. uint32_t w0 = ReadLE32(chunk + 0), w1 = ReadLE32(chunk + 4), w2 = ReadLE32(chunk + 8), w3 = ReadLE32(chunk + 12);
  49. uint32_t w4 = ReadLE32(chunk + 16), w5 = ReadLE32(chunk + 20), w6 = ReadLE32(chunk + 24), w7 = ReadLE32(chunk + 28);
  50. uint32_t w8 = ReadLE32(chunk + 32), w9 = ReadLE32(chunk + 36), w10 = ReadLE32(chunk + 40), w11 = ReadLE32(chunk + 44);
  51. uint32_t w12 = ReadLE32(chunk + 48), w13 = ReadLE32(chunk + 52), w14 = ReadLE32(chunk + 56), w15 = ReadLE32(chunk + 60);
  52. R11(a1, b1, c1, d1, e1, w0, 11);
  53. R12(a2, b2, c2, d2, e2, w5, 8);
  54. R11(e1, a1, b1, c1, d1, w1, 14);
  55. R12(e2, a2, b2, c2, d2, w14, 9);
  56. R11(d1, e1, a1, b1, c1, w2, 15);
  57. R12(d2, e2, a2, b2, c2, w7, 9);
  58. R11(c1, d1, e1, a1, b1, w3, 12);
  59. R12(c2, d2, e2, a2, b2, w0, 11);
  60. R11(b1, c1, d1, e1, a1, w4, 5);
  61. R12(b2, c2, d2, e2, a2, w9, 13);
  62. R11(a1, b1, c1, d1, e1, w5, 8);
  63. R12(a2, b2, c2, d2, e2, w2, 15);
  64. R11(e1, a1, b1, c1, d1, w6, 7);
  65. R12(e2, a2, b2, c2, d2, w11, 15);
  66. R11(d1, e1, a1, b1, c1, w7, 9);
  67. R12(d2, e2, a2, b2, c2, w4, 5);
  68. R11(c1, d1, e1, a1, b1, w8, 11);
  69. R12(c2, d2, e2, a2, b2, w13, 7);
  70. R11(b1, c1, d1, e1, a1, w9, 13);
  71. R12(b2, c2, d2, e2, a2, w6, 7);
  72. R11(a1, b1, c1, d1, e1, w10, 14);
  73. R12(a2, b2, c2, d2, e2, w15, 8);
  74. R11(e1, a1, b1, c1, d1, w11, 15);
  75. R12(e2, a2, b2, c2, d2, w8, 11);
  76. R11(d1, e1, a1, b1, c1, w12, 6);
  77. R12(d2, e2, a2, b2, c2, w1, 14);
  78. R11(c1, d1, e1, a1, b1, w13, 7);
  79. R12(c2, d2, e2, a2, b2, w10, 14);
  80. R11(b1, c1, d1, e1, a1, w14, 9);
  81. R12(b2, c2, d2, e2, a2, w3, 12);
  82. R11(a1, b1, c1, d1, e1, w15, 8);
  83. R12(a2, b2, c2, d2, e2, w12, 6);
  84. R21(e1, a1, b1, c1, d1, w7, 7);
  85. R22(e2, a2, b2, c2, d2, w6, 9);
  86. R21(d1, e1, a1, b1, c1, w4, 6);
  87. R22(d2, e2, a2, b2, c2, w11, 13);
  88. R21(c1, d1, e1, a1, b1, w13, 8);
  89. R22(c2, d2, e2, a2, b2, w3, 15);
  90. R21(b1, c1, d1, e1, a1, w1, 13);
  91. R22(b2, c2, d2, e2, a2, w7, 7);
  92. R21(a1, b1, c1, d1, e1, w10, 11);
  93. R22(a2, b2, c2, d2, e2, w0, 12);
  94. R21(e1, a1, b1, c1, d1, w6, 9);
  95. R22(e2, a2, b2, c2, d2, w13, 8);
  96. R21(d1, e1, a1, b1, c1, w15, 7);
  97. R22(d2, e2, a2, b2, c2, w5, 9);
  98. R21(c1, d1, e1, a1, b1, w3, 15);
  99. R22(c2, d2, e2, a2, b2, w10, 11);
  100. R21(b1, c1, d1, e1, a1, w12, 7);
  101. R22(b2, c2, d2, e2, a2, w14, 7);
  102. R21(a1, b1, c1, d1, e1, w0, 12);
  103. R22(a2, b2, c2, d2, e2, w15, 7);
  104. R21(e1, a1, b1, c1, d1, w9, 15);
  105. R22(e2, a2, b2, c2, d2, w8, 12);
  106. R21(d1, e1, a1, b1, c1, w5, 9);
  107. R22(d2, e2, a2, b2, c2, w12, 7);
  108. R21(c1, d1, e1, a1, b1, w2, 11);
  109. R22(c2, d2, e2, a2, b2, w4, 6);
  110. R21(b1, c1, d1, e1, a1, w14, 7);
  111. R22(b2, c2, d2, e2, a2, w9, 15);
  112. R21(a1, b1, c1, d1, e1, w11, 13);
  113. R22(a2, b2, c2, d2, e2, w1, 13);
  114. R21(e1, a1, b1, c1, d1, w8, 12);
  115. R22(e2, a2, b2, c2, d2, w2, 11);
  116. R31(d1, e1, a1, b1, c1, w3, 11);
  117. R32(d2, e2, a2, b2, c2, w15, 9);
  118. R31(c1, d1, e1, a1, b1, w10, 13);
  119. R32(c2, d2, e2, a2, b2, w5, 7);
  120. R31(b1, c1, d1, e1, a1, w14, 6);
  121. R32(b2, c2, d2, e2, a2, w1, 15);
  122. R31(a1, b1, c1, d1, e1, w4, 7);
  123. R32(a2, b2, c2, d2, e2, w3, 11);
  124. R31(e1, a1, b1, c1, d1, w9, 14);
  125. R32(e2, a2, b2, c2, d2, w7, 8);
  126. R31(d1, e1, a1, b1, c1, w15, 9);
  127. R32(d2, e2, a2, b2, c2, w14, 6);
  128. R31(c1, d1, e1, a1, b1, w8, 13);
  129. R32(c2, d2, e2, a2, b2, w6, 6);
  130. R31(b1, c1, d1, e1, a1, w1, 15);
  131. R32(b2, c2, d2, e2, a2, w9, 14);
  132. R31(a1, b1, c1, d1, e1, w2, 14);
  133. R32(a2, b2, c2, d2, e2, w11, 12);
  134. R31(e1, a1, b1, c1, d1, w7, 8);
  135. R32(e2, a2, b2, c2, d2, w8, 13);
  136. R31(d1, e1, a1, b1, c1, w0, 13);
  137. R32(d2, e2, a2, b2, c2, w12, 5);
  138. R31(c1, d1, e1, a1, b1, w6, 6);
  139. R32(c2, d2, e2, a2, b2, w2, 14);
  140. R31(b1, c1, d1, e1, a1, w13, 5);
  141. R32(b2, c2, d2, e2, a2, w10, 13);
  142. R31(a1, b1, c1, d1, e1, w11, 12);
  143. R32(a2, b2, c2, d2, e2, w0, 13);
  144. R31(e1, a1, b1, c1, d1, w5, 7);
  145. R32(e2, a2, b2, c2, d2, w4, 7);
  146. R31(d1, e1, a1, b1, c1, w12, 5);
  147. R32(d2, e2, a2, b2, c2, w13, 5);
  148. R41(c1, d1, e1, a1, b1, w1, 11);
  149. R42(c2, d2, e2, a2, b2, w8, 15);
  150. R41(b1, c1, d1, e1, a1, w9, 12);
  151. R42(b2, c2, d2, e2, a2, w6, 5);
  152. R41(a1, b1, c1, d1, e1, w11, 14);
  153. R42(a2, b2, c2, d2, e2, w4, 8);
  154. R41(e1, a1, b1, c1, d1, w10, 15);
  155. R42(e2, a2, b2, c2, d2, w1, 11);
  156. R41(d1, e1, a1, b1, c1, w0, 14);
  157. R42(d2, e2, a2, b2, c2, w3, 14);
  158. R41(c1, d1, e1, a1, b1, w8, 15);
  159. R42(c2, d2, e2, a2, b2, w11, 14);
  160. R41(b1, c1, d1, e1, a1, w12, 9);
  161. R42(b2, c2, d2, e2, a2, w15, 6);
  162. R41(a1, b1, c1, d1, e1, w4, 8);
  163. R42(a2, b2, c2, d2, e2, w0, 14);
  164. R41(e1, a1, b1, c1, d1, w13, 9);
  165. R42(e2, a2, b2, c2, d2, w5, 6);
  166. R41(d1, e1, a1, b1, c1, w3, 14);
  167. R42(d2, e2, a2, b2, c2, w12, 9);
  168. R41(c1, d1, e1, a1, b1, w7, 5);
  169. R42(c2, d2, e2, a2, b2, w2, 12);
  170. R41(b1, c1, d1, e1, a1, w15, 6);
  171. R42(b2, c2, d2, e2, a2, w13, 9);
  172. R41(a1, b1, c1, d1, e1, w14, 8);
  173. R42(a2, b2, c2, d2, e2, w9, 12);
  174. R41(e1, a1, b1, c1, d1, w5, 6);
  175. R42(e2, a2, b2, c2, d2, w7, 5);
  176. R41(d1, e1, a1, b1, c1, w6, 5);
  177. R42(d2, e2, a2, b2, c2, w10, 15);
  178. R41(c1, d1, e1, a1, b1, w2, 12);
  179. R42(c2, d2, e2, a2, b2, w14, 8);
  180. R51(b1, c1, d1, e1, a1, w4, 9);
  181. R52(b2, c2, d2, e2, a2, w12, 8);
  182. R51(a1, b1, c1, d1, e1, w0, 15);
  183. R52(a2, b2, c2, d2, e2, w15, 5);
  184. R51(e1, a1, b1, c1, d1, w5, 5);
  185. R52(e2, a2, b2, c2, d2, w10, 12);
  186. R51(d1, e1, a1, b1, c1, w9, 11);
  187. R52(d2, e2, a2, b2, c2, w4, 9);
  188. R51(c1, d1, e1, a1, b1, w7, 6);
  189. R52(c2, d2, e2, a2, b2, w1, 12);
  190. R51(b1, c1, d1, e1, a1, w12, 8);
  191. R52(b2, c2, d2, e2, a2, w5, 5);
  192. R51(a1, b1, c1, d1, e1, w2, 13);
  193. R52(a2, b2, c2, d2, e2, w8, 14);
  194. R51(e1, a1, b1, c1, d1, w10, 12);
  195. R52(e2, a2, b2, c2, d2, w7, 6);
  196. R51(d1, e1, a1, b1, c1, w14, 5);
  197. R52(d2, e2, a2, b2, c2, w6, 8);
  198. R51(c1, d1, e1, a1, b1, w1, 12);
  199. R52(c2, d2, e2, a2, b2, w2, 13);
  200. R51(b1, c1, d1, e1, a1, w3, 13);
  201. R52(b2, c2, d2, e2, a2, w13, 6);
  202. R51(a1, b1, c1, d1, e1, w8, 14);
  203. R52(a2, b2, c2, d2, e2, w14, 5);
  204. R51(e1, a1, b1, c1, d1, w11, 11);
  205. R52(e2, a2, b2, c2, d2, w0, 15);
  206. R51(d1, e1, a1, b1, c1, w6, 8);
  207. R52(d2, e2, a2, b2, c2, w3, 13);
  208. R51(c1, d1, e1, a1, b1, w15, 5);
  209. R52(c2, d2, e2, a2, b2, w9, 11);
  210. R51(b1, c1, d1, e1, a1, w13, 6);
  211. R52(b2, c2, d2, e2, a2, w11, 11);
  212. uint32_t t = s[0];
  213. s[0] = s[1] + c1 + d2;
  214. s[1] = s[2] + d1 + e2;
  215. s[2] = s[3] + e1 + a2;
  216. s[3] = s[4] + a1 + b2;
  217. s[4] = t + b1 + c2;
  218. }
  219. } // namespace ripemd160
  220. } // namespace
  221. ////// RIPEMD160
  222. CRIPEMD160::CRIPEMD160() : bytes(0)
  223. {
  224. ripemd160::Initialize(s);
  225. }
  226. CRIPEMD160& CRIPEMD160::Write(const unsigned char* data, size_t len)
  227. {
  228. const unsigned char* end = data + len;
  229. size_t bufsize = bytes % 64;
  230. if (bufsize && bufsize + len >= 64) {
  231. // Fill the buffer, and process it.
  232. memcpy(buf + bufsize, data, 64 - bufsize);
  233. bytes += 64 - bufsize;
  234. data += 64 - bufsize;
  235. ripemd160::Transform(s, buf);
  236. bufsize = 0;
  237. }
  238. while (end >= data + 64) {
  239. // Process full chunks directly from the source.
  240. ripemd160::Transform(s, data);
  241. bytes += 64;
  242. data += 64;
  243. }
  244. if (end > data) {
  245. // Fill the buffer with what remains.
  246. memcpy(buf + bufsize, data, end - data);
  247. bytes += end - data;
  248. }
  249. return *this;
  250. }
  251. void CRIPEMD160::Finalize(unsigned char hash[OUTPUT_SIZE])
  252. {
  253. static const unsigned char pad[64] = {0x80};
  254. unsigned char sizedesc[8];
  255. WriteLE64(sizedesc, bytes << 3);
  256. Write(pad, 1 + ((119 - (bytes % 64)) % 64));
  257. Write(sizedesc, 8);
  258. WriteLE32(hash, s[0]);
  259. WriteLE32(hash + 4, s[1]);
  260. WriteLE32(hash + 8, s[2]);
  261. WriteLE32(hash + 12, s[3]);
  262. WriteLE32(hash + 16, s[4]);
  263. }
  264. CRIPEMD160& CRIPEMD160::Reset()
  265. {
  266. bytes = 0;
  267. ripemd160::Initialize(s);
  268. return *this;
  269. }