ripemd160.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* MIT (BSD) license - see LICENSE file for details */
  2. /* RIPEMD core code translated from the Bitcoin project's C++:
  3. *
  4. * src/crypto/ripemd160.cpp commit f914f1a746d7f91951c1da262a4a749dd3ebfa71
  5. * Copyright (c) 2014 The Bitcoin Core developers
  6. * Distributed under the MIT software license, see the accompanying
  7. * file COPYING or http://www.opensource.org/licenses/mit-license.php.
  8. */
  9. #include <ccan/crypto/ripemd160/ripemd160.h>
  10. #include <ccan/endian/endian.h>
  11. #include <ccan/compiler/compiler.h>
  12. #include <stdbool.h>
  13. #include <assert.h>
  14. #include <string.h>
  15. static void invalidate_ripemd160(struct ripemd160_ctx *ctx)
  16. {
  17. #ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
  18. ctx->c.num = -1U;
  19. #else
  20. ctx->bytes = -1ULL;
  21. #endif
  22. }
  23. static void check_ripemd160(struct ripemd160_ctx *ctx UNUSED)
  24. {
  25. #ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
  26. assert(ctx->c.num != -1U);
  27. #else
  28. assert(ctx->bytes != -1ULL);
  29. #endif
  30. }
  31. #ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
  32. void ripemd160_init(struct ripemd160_ctx *ctx)
  33. {
  34. RIPEMD160_Init(&ctx->c);
  35. }
  36. void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size)
  37. {
  38. check_ripemd160(ctx);
  39. RIPEMD160_Update(&ctx->c, p, size);
  40. }
  41. void ripemd160_done(struct ripemd160_ctx *ctx, struct ripemd160 *res)
  42. {
  43. RIPEMD160_Final(res->u.u8, &ctx->c);
  44. invalidate_ripemd160(ctx);
  45. }
  46. #else
  47. static uint32_t inline f1(uint32_t x, uint32_t y, uint32_t z) { return x ^ y ^ z; }
  48. static uint32_t inline f2(uint32_t x, uint32_t y, uint32_t z) { return (x & y) | (~x & z); }
  49. static uint32_t inline f3(uint32_t x, uint32_t y, uint32_t z) { return (x | ~y) ^ z; }
  50. static uint32_t inline f4(uint32_t x, uint32_t y, uint32_t z) { return (x & z) | (y & ~z); }
  51. static uint32_t inline f5(uint32_t x, uint32_t y, uint32_t z) { return x ^ (y | ~z); }
  52. static uint32_t inline rol(uint32_t x, int i) { return (x << i) | (x >> (32 - i)); }
  53. static void inline Round(uint32_t *a, uint32_t b UNUSED, uint32_t *c, uint32_t d UNUSED, uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r)
  54. {
  55. *a = rol(*a + f + x + k, r) + e;
  56. *c = rol(*c, 10);
  57. }
  58. static 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); }
  59. static 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); }
  60. static 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); }
  61. static 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); }
  62. static 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); }
  63. static 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); }
  64. static 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); }
  65. static 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); }
  66. static 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); }
  67. static 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); }
  68. /** Perform a RIPEMD-160 transformation, processing a 64-byte chunk. */
  69. static void Transform(uint32_t *s, const uint32_t *chunk)
  70. {
  71. uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4];
  72. uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1;
  73. uint32_t w0 = le32_to_cpu(chunk[0]), w1 = le32_to_cpu(chunk[1]), w2 = le32_to_cpu(chunk[2]), w3 = le32_to_cpu(chunk[3]);
  74. uint32_t w4 = le32_to_cpu(chunk[4]), w5 = le32_to_cpu(chunk[5]), w6 = le32_to_cpu(chunk[6]), w7 = le32_to_cpu(chunk[7]);
  75. uint32_t w8 = le32_to_cpu(chunk[8]), w9 = le32_to_cpu(chunk[9]), w10 = le32_to_cpu(chunk[10]), w11 = le32_to_cpu(chunk[11]);
  76. uint32_t w12 = le32_to_cpu(chunk[12]), w13 = le32_to_cpu(chunk[13]), w14 = le32_to_cpu(chunk[14]), w15 = le32_to_cpu(chunk[15]);
  77. uint32_t t;
  78. R11(&a1, b1, &c1, d1, e1, w0, 11);
  79. R12(&a2, b2, &c2, d2, e2, w5, 8);
  80. R11(&e1, a1, &b1, c1, d1, w1, 14);
  81. R12(&e2, a2, &b2, c2, d2, w14, 9);
  82. R11(&d1, e1, &a1, b1, c1, w2, 15);
  83. R12(&d2, e2, &a2, b2, c2, w7, 9);
  84. R11(&c1, d1, &e1, a1, b1, w3, 12);
  85. R12(&c2, d2, &e2, a2, b2, w0, 11);
  86. R11(&b1, c1, &d1, e1, a1, w4, 5);
  87. R12(&b2, c2, &d2, e2, a2, w9, 13);
  88. R11(&a1, b1, &c1, d1, e1, w5, 8);
  89. R12(&a2, b2, &c2, d2, e2, w2, 15);
  90. R11(&e1, a1, &b1, c1, d1, w6, 7);
  91. R12(&e2, a2, &b2, c2, d2, w11, 15);
  92. R11(&d1, e1, &a1, b1, c1, w7, 9);
  93. R12(&d2, e2, &a2, b2, c2, w4, 5);
  94. R11(&c1, d1, &e1, a1, b1, w8, 11);
  95. R12(&c2, d2, &e2, a2, b2, w13, 7);
  96. R11(&b1, c1, &d1, e1, a1, w9, 13);
  97. R12(&b2, c2, &d2, e2, a2, w6, 7);
  98. R11(&a1, b1, &c1, d1, e1, w10, 14);
  99. R12(&a2, b2, &c2, d2, e2, w15, 8);
  100. R11(&e1, a1, &b1, c1, d1, w11, 15);
  101. R12(&e2, a2, &b2, c2, d2, w8, 11);
  102. R11(&d1, e1, &a1, b1, c1, w12, 6);
  103. R12(&d2, e2, &a2, b2, c2, w1, 14);
  104. R11(&c1, d1, &e1, a1, b1, w13, 7);
  105. R12(&c2, d2, &e2, a2, b2, w10, 14);
  106. R11(&b1, c1, &d1, e1, a1, w14, 9);
  107. R12(&b2, c2, &d2, e2, a2, w3, 12);
  108. R11(&a1, b1, &c1, d1, e1, w15, 8);
  109. R12(&a2, b2, &c2, d2, e2, w12, 6);
  110. R21(&e1, a1, &b1, c1, d1, w7, 7);
  111. R22(&e2, a2, &b2, c2, d2, w6, 9);
  112. R21(&d1, e1, &a1, b1, c1, w4, 6);
  113. R22(&d2, e2, &a2, b2, c2, w11, 13);
  114. R21(&c1, d1, &e1, a1, b1, w13, 8);
  115. R22(&c2, d2, &e2, a2, b2, w3, 15);
  116. R21(&b1, c1, &d1, e1, a1, w1, 13);
  117. R22(&b2, c2, &d2, e2, a2, w7, 7);
  118. R21(&a1, b1, &c1, d1, e1, w10, 11);
  119. R22(&a2, b2, &c2, d2, e2, w0, 12);
  120. R21(&e1, a1, &b1, c1, d1, w6, 9);
  121. R22(&e2, a2, &b2, c2, d2, w13, 8);
  122. R21(&d1, e1, &a1, b1, c1, w15, 7);
  123. R22(&d2, e2, &a2, b2, c2, w5, 9);
  124. R21(&c1, d1, &e1, a1, b1, w3, 15);
  125. R22(&c2, d2, &e2, a2, b2, w10, 11);
  126. R21(&b1, c1, &d1, e1, a1, w12, 7);
  127. R22(&b2, c2, &d2, e2, a2, w14, 7);
  128. R21(&a1, b1, &c1, d1, e1, w0, 12);
  129. R22(&a2, b2, &c2, d2, e2, w15, 7);
  130. R21(&e1, a1, &b1, c1, d1, w9, 15);
  131. R22(&e2, a2, &b2, c2, d2, w8, 12);
  132. R21(&d1, e1, &a1, b1, c1, w5, 9);
  133. R22(&d2, e2, &a2, b2, c2, w12, 7);
  134. R21(&c1, d1, &e1, a1, b1, w2, 11);
  135. R22(&c2, d2, &e2, a2, b2, w4, 6);
  136. R21(&b1, c1, &d1, e1, a1, w14, 7);
  137. R22(&b2, c2, &d2, e2, a2, w9, 15);
  138. R21(&a1, b1, &c1, d1, e1, w11, 13);
  139. R22(&a2, b2, &c2, d2, e2, w1, 13);
  140. R21(&e1, a1, &b1, c1, d1, w8, 12);
  141. R22(&e2, a2, &b2, c2, d2, w2, 11);
  142. R31(&d1, e1, &a1, b1, c1, w3, 11);
  143. R32(&d2, e2, &a2, b2, c2, w15, 9);
  144. R31(&c1, d1, &e1, a1, b1, w10, 13);
  145. R32(&c2, d2, &e2, a2, b2, w5, 7);
  146. R31(&b1, c1, &d1, e1, a1, w14, 6);
  147. R32(&b2, c2, &d2, e2, a2, w1, 15);
  148. R31(&a1, b1, &c1, d1, e1, w4, 7);
  149. R32(&a2, b2, &c2, d2, e2, w3, 11);
  150. R31(&e1, a1, &b1, c1, d1, w9, 14);
  151. R32(&e2, a2, &b2, c2, d2, w7, 8);
  152. R31(&d1, e1, &a1, b1, c1, w15, 9);
  153. R32(&d2, e2, &a2, b2, c2, w14, 6);
  154. R31(&c1, d1, &e1, a1, b1, w8, 13);
  155. R32(&c2, d2, &e2, a2, b2, w6, 6);
  156. R31(&b1, c1, &d1, e1, a1, w1, 15);
  157. R32(&b2, c2, &d2, e2, a2, w9, 14);
  158. R31(&a1, b1, &c1, d1, e1, w2, 14);
  159. R32(&a2, b2, &c2, d2, e2, w11, 12);
  160. R31(&e1, a1, &b1, c1, d1, w7, 8);
  161. R32(&e2, a2, &b2, c2, d2, w8, 13);
  162. R31(&d1, e1, &a1, b1, c1, w0, 13);
  163. R32(&d2, e2, &a2, b2, c2, w12, 5);
  164. R31(&c1, d1, &e1, a1, b1, w6, 6);
  165. R32(&c2, d2, &e2, a2, b2, w2, 14);
  166. R31(&b1, c1, &d1, e1, a1, w13, 5);
  167. R32(&b2, c2, &d2, e2, a2, w10, 13);
  168. R31(&a1, b1, &c1, d1, e1, w11, 12);
  169. R32(&a2, b2, &c2, d2, e2, w0, 13);
  170. R31(&e1, a1, &b1, c1, d1, w5, 7);
  171. R32(&e2, a2, &b2, c2, d2, w4, 7);
  172. R31(&d1, e1, &a1, b1, c1, w12, 5);
  173. R32(&d2, e2, &a2, b2, c2, w13, 5);
  174. R41(&c1, d1, &e1, a1, b1, w1, 11);
  175. R42(&c2, d2, &e2, a2, b2, w8, 15);
  176. R41(&b1, c1, &d1, e1, a1, w9, 12);
  177. R42(&b2, c2, &d2, e2, a2, w6, 5);
  178. R41(&a1, b1, &c1, d1, e1, w11, 14);
  179. R42(&a2, b2, &c2, d2, e2, w4, 8);
  180. R41(&e1, a1, &b1, c1, d1, w10, 15);
  181. R42(&e2, a2, &b2, c2, d2, w1, 11);
  182. R41(&d1, e1, &a1, b1, c1, w0, 14);
  183. R42(&d2, e2, &a2, b2, c2, w3, 14);
  184. R41(&c1, d1, &e1, a1, b1, w8, 15);
  185. R42(&c2, d2, &e2, a2, b2, w11, 14);
  186. R41(&b1, c1, &d1, e1, a1, w12, 9);
  187. R42(&b2, c2, &d2, e2, a2, w15, 6);
  188. R41(&a1, b1, &c1, d1, e1, w4, 8);
  189. R42(&a2, b2, &c2, d2, e2, w0, 14);
  190. R41(&e1, a1, &b1, c1, d1, w13, 9);
  191. R42(&e2, a2, &b2, c2, d2, w5, 6);
  192. R41(&d1, e1, &a1, b1, c1, w3, 14);
  193. R42(&d2, e2, &a2, b2, c2, w12, 9);
  194. R41(&c1, d1, &e1, a1, b1, w7, 5);
  195. R42(&c2, d2, &e2, a2, b2, w2, 12);
  196. R41(&b1, c1, &d1, e1, a1, w15, 6);
  197. R42(&b2, c2, &d2, e2, a2, w13, 9);
  198. R41(&a1, b1, &c1, d1, e1, w14, 8);
  199. R42(&a2, b2, &c2, d2, e2, w9, 12);
  200. R41(&e1, a1, &b1, c1, d1, w5, 6);
  201. R42(&e2, a2, &b2, c2, d2, w7, 5);
  202. R41(&d1, e1, &a1, b1, c1, w6, 5);
  203. R42(&d2, e2, &a2, b2, c2, w10, 15);
  204. R41(&c1, d1, &e1, a1, b1, w2, 12);
  205. R42(&c2, d2, &e2, a2, b2, w14, 8);
  206. R51(&b1, c1, &d1, e1, a1, w4, 9);
  207. R52(&b2, c2, &d2, e2, a2, w12, 8);
  208. R51(&a1, b1, &c1, d1, e1, w0, 15);
  209. R52(&a2, b2, &c2, d2, e2, w15, 5);
  210. R51(&e1, a1, &b1, c1, d1, w5, 5);
  211. R52(&e2, a2, &b2, c2, d2, w10, 12);
  212. R51(&d1, e1, &a1, b1, c1, w9, 11);
  213. R52(&d2, e2, &a2, b2, c2, w4, 9);
  214. R51(&c1, d1, &e1, a1, b1, w7, 6);
  215. R52(&c2, d2, &e2, a2, b2, w1, 12);
  216. R51(&b1, c1, &d1, e1, a1, w12, 8);
  217. R52(&b2, c2, &d2, e2, a2, w5, 5);
  218. R51(&a1, b1, &c1, d1, e1, w2, 13);
  219. R52(&a2, b2, &c2, d2, e2, w8, 14);
  220. R51(&e1, a1, &b1, c1, d1, w10, 12);
  221. R52(&e2, a2, &b2, c2, d2, w7, 6);
  222. R51(&d1, e1, &a1, b1, c1, w14, 5);
  223. R52(&d2, e2, &a2, b2, c2, w6, 8);
  224. R51(&c1, d1, &e1, a1, b1, w1, 12);
  225. R52(&c2, d2, &e2, a2, b2, w2, 13);
  226. R51(&b1, c1, &d1, e1, a1, w3, 13);
  227. R52(&b2, c2, &d2, e2, a2, w13, 6);
  228. R51(&a1, b1, &c1, d1, e1, w8, 14);
  229. R52(&a2, b2, &c2, d2, e2, w14, 5);
  230. R51(&e1, a1, &b1, c1, d1, w11, 11);
  231. R52(&e2, a2, &b2, c2, d2, w0, 15);
  232. R51(&d1, e1, &a1, b1, c1, w6, 8);
  233. R52(&d2, e2, &a2, b2, c2, w3, 13);
  234. R51(&c1, d1, &e1, a1, b1, w15, 5);
  235. R52(&c2, d2, &e2, a2, b2, w9, 11);
  236. R51(&b1, c1, &d1, e1, a1, w13, 6);
  237. R52(&b2, c2, &d2, e2, a2, w11, 11);
  238. t = s[0];
  239. s[0] = s[1] + c1 + d2;
  240. s[1] = s[2] + d1 + e2;
  241. s[2] = s[3] + e1 + a2;
  242. s[3] = s[4] + a1 + b2;
  243. s[4] = t + b1 + c2;
  244. }
  245. static bool alignment_ok(const void *p UNUSED, size_t n UNUSED)
  246. {
  247. #if HAVE_UNALIGNED_ACCESS
  248. return true;
  249. #else
  250. return ((size_t)p % n == 0);
  251. #endif
  252. }
  253. static void add(struct ripemd160_ctx *ctx, const void *p, size_t len)
  254. {
  255. const unsigned char *data = p;
  256. size_t bufsize = ctx->bytes % 64;
  257. if (bufsize + len >= 64) {
  258. // Fill the buffer, and process it.
  259. memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize);
  260. ctx->bytes += 64 - bufsize;
  261. data += 64 - bufsize;
  262. len -= 64 - bufsize;
  263. Transform(ctx->s, ctx->buf.u32);
  264. bufsize = 0;
  265. }
  266. while (len >= 64) {
  267. // Process full chunks directly from the source.
  268. if (alignment_ok(data, sizeof(uint32_t)))
  269. Transform(ctx->s, (const uint32_t *)data);
  270. else {
  271. memcpy(ctx->buf.u8, data, sizeof(ctx->buf));
  272. Transform(ctx->s, ctx->buf.u32);
  273. }
  274. ctx->bytes += 64;
  275. data += 64;
  276. len -= 64;
  277. }
  278. if (len) {
  279. // Fill the buffer with what remains.
  280. memcpy(ctx->buf.u8 + bufsize, data, len);
  281. ctx->bytes += len;
  282. }
  283. }
  284. void ripemd160_init(struct ripemd160_ctx *ctx)
  285. {
  286. struct ripemd160_ctx init = RIPEMD160_INIT;
  287. *ctx = init;
  288. }
  289. void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size)
  290. {
  291. check_ripemd160(ctx);
  292. add(ctx, p, size);
  293. }
  294. void ripemd160_done(struct ripemd160_ctx *ctx, struct ripemd160 *res)
  295. {
  296. static const unsigned char pad[64] = {0x80};
  297. uint64_t sizedesc;
  298. size_t i;
  299. sizedesc = cpu_to_le64(ctx->bytes << 3);
  300. /* Add '1' bit to terminate, then all 0 bits, up to next block - 8. */
  301. add(ctx, pad, 1 + ((119 - (ctx->bytes % 64)) % 64));
  302. /* Add number of bits of data (big endian) */
  303. add(ctx, &sizedesc, 8);
  304. for (i = 0; i < sizeof(ctx->s) / sizeof(ctx->s[0]); i++)
  305. res->u.u32[i] = cpu_to_le32(ctx->s[i]);
  306. invalidate_ripemd160(ctx);
  307. }
  308. #endif
  309. void ripemd160(struct ripemd160 *ripemd, const void *p, size_t size)
  310. {
  311. struct ripemd160_ctx ctx;
  312. ripemd160_init(&ctx);
  313. ripemd160_update(&ctx, p, size);
  314. ripemd160_done(&ctx, ripemd);
  315. }
  316. void ripemd160_u8(struct ripemd160_ctx *ctx, uint8_t v)
  317. {
  318. ripemd160_update(ctx, &v, sizeof(v));
  319. }
  320. void ripemd160_u16(struct ripemd160_ctx *ctx, uint16_t v)
  321. {
  322. ripemd160_update(ctx, &v, sizeof(v));
  323. }
  324. void ripemd160_u32(struct ripemd160_ctx *ctx, uint32_t v)
  325. {
  326. ripemd160_update(ctx, &v, sizeof(v));
  327. }
  328. void ripemd160_u64(struct ripemd160_ctx *ctx, uint64_t v)
  329. {
  330. ripemd160_update(ctx, &v, sizeof(v));
  331. }
  332. /* Add as little-endian */
  333. void ripemd160_le16(struct ripemd160_ctx *ctx, uint16_t v)
  334. {
  335. leint16_t lev = cpu_to_le16(v);
  336. ripemd160_update(ctx, &lev, sizeof(lev));
  337. }
  338. void ripemd160_le32(struct ripemd160_ctx *ctx, uint32_t v)
  339. {
  340. leint32_t lev = cpu_to_le32(v);
  341. ripemd160_update(ctx, &lev, sizeof(lev));
  342. }
  343. void ripemd160_le64(struct ripemd160_ctx *ctx, uint64_t v)
  344. {
  345. leint64_t lev = cpu_to_le64(v);
  346. ripemd160_update(ctx, &lev, sizeof(lev));
  347. }
  348. /* Add as big-endian */
  349. void ripemd160_be16(struct ripemd160_ctx *ctx, uint16_t v)
  350. {
  351. beint16_t bev = cpu_to_be16(v);
  352. ripemd160_update(ctx, &bev, sizeof(bev));
  353. }
  354. void ripemd160_be32(struct ripemd160_ctx *ctx, uint32_t v)
  355. {
  356. beint32_t bev = cpu_to_be32(v);
  357. ripemd160_update(ctx, &bev, sizeof(bev));
  358. }
  359. void ripemd160_be64(struct ripemd160_ctx *ctx, uint64_t v)
  360. {
  361. beint64_t bev = cpu_to_be64(v);
  362. ripemd160_update(ctx, &bev, sizeof(bev));
  363. }