ripemd160.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * RIPE MD-160 implementation
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /*
  22. * The RIPEMD-160 algorithm was designed by RIPE in 1996
  23. * http://homes.esat.kuleuven.be/~bosselae/ripemd160.html
  24. * http://ehash.iaik.tugraz.at/wiki/RIPEMD-160
  25. */
  26. #include <string.h>
  27. #include "ripemd160.h"
  28. #include "memzero.h"
  29. /*
  30. * 32-bit integer manipulation macros (little endian)
  31. */
  32. #ifndef GET_UINT32_LE
  33. #define GET_UINT32_LE(n, b, i) \
  34. { \
  35. (n) = ((uint32_t)(b)[(i)]) | ((uint32_t)(b)[(i) + 1] << 8) | \
  36. ((uint32_t)(b)[(i) + 2] << 16) | ((uint32_t)(b)[(i) + 3] << 24); \
  37. }
  38. #endif
  39. #ifndef PUT_UINT32_LE
  40. #define PUT_UINT32_LE(n, b, i) \
  41. { \
  42. (b)[(i)] = (uint8_t)(((n)) & 0xFF); \
  43. (b)[(i) + 1] = (uint8_t)(((n) >> 8) & 0xFF); \
  44. (b)[(i) + 2] = (uint8_t)(((n) >> 16) & 0xFF); \
  45. (b)[(i) + 3] = (uint8_t)(((n) >> 24) & 0xFF); \
  46. }
  47. #endif
  48. /*
  49. * RIPEMD-160 context setup
  50. */
  51. void ripemd160_Init(RIPEMD160_CTX* ctx) {
  52. memzero(ctx, sizeof(RIPEMD160_CTX));
  53. ctx->total[0] = 0;
  54. ctx->total[1] = 0;
  55. ctx->state[0] = 0x67452301;
  56. ctx->state[1] = 0xEFCDAB89;
  57. ctx->state[2] = 0x98BADCFE;
  58. ctx->state[3] = 0x10325476;
  59. ctx->state[4] = 0xC3D2E1F0;
  60. }
  61. #if !defined(MBEDTLS_RIPEMD160_PROCESS_ALT)
  62. /*
  63. * Process one block
  64. */
  65. void ripemd160_process(RIPEMD160_CTX* ctx, const uint8_t data[RIPEMD160_BLOCK_LENGTH]) {
  66. uint32_t A = 0, B = 0, C = 0, D = 0, E = 0, Ap = 0, Bp = 0, Cp = 0, Dp = 0, Ep = 0,
  67. X[16] = {0};
  68. GET_UINT32_LE(X[0], data, 0);
  69. GET_UINT32_LE(X[1], data, 4);
  70. GET_UINT32_LE(X[2], data, 8);
  71. GET_UINT32_LE(X[3], data, 12);
  72. GET_UINT32_LE(X[4], data, 16);
  73. GET_UINT32_LE(X[5], data, 20);
  74. GET_UINT32_LE(X[6], data, 24);
  75. GET_UINT32_LE(X[7], data, 28);
  76. GET_UINT32_LE(X[8], data, 32);
  77. GET_UINT32_LE(X[9], data, 36);
  78. GET_UINT32_LE(X[10], data, 40);
  79. GET_UINT32_LE(X[11], data, 44);
  80. GET_UINT32_LE(X[12], data, 48);
  81. GET_UINT32_LE(X[13], data, 52);
  82. GET_UINT32_LE(X[14], data, 56);
  83. GET_UINT32_LE(X[15], data, 60);
  84. A = Ap = ctx->state[0];
  85. B = Bp = ctx->state[1];
  86. C = Cp = ctx->state[2];
  87. D = Dp = ctx->state[3];
  88. E = Ep = ctx->state[4];
  89. #define F1(x, y, z) (x ^ y ^ z)
  90. #define F2(x, y, z) ((x & y) | (~x & z))
  91. #define F3(x, y, z) ((x | ~y) ^ z)
  92. #define F4(x, y, z) ((x & z) | (y & ~z))
  93. #define F5(x, y, z) (x ^ (y | ~z))
  94. #define S(x, n) ((x << n) | (x >> (32 - n)))
  95. #define P(a, b, c, d, e, r, s, f, k) \
  96. a += f(b, c, d) + X[r] + k; \
  97. a = S(a, s) + e; \
  98. c = S(c, 10);
  99. #define P2(a, b, c, d, e, r, s, rp, sp) \
  100. P(a, b, c, d, e, r, s, F, K); \
  101. P(a##p, b##p, c##p, d##p, e##p, rp, sp, Fp, Kp);
  102. #define F F1
  103. #define K 0x00000000
  104. #define Fp F5
  105. #define Kp 0x50A28BE6
  106. P2(A, B, C, D, E, 0, 11, 5, 8);
  107. P2(E, A, B, C, D, 1, 14, 14, 9);
  108. P2(D, E, A, B, C, 2, 15, 7, 9);
  109. P2(C, D, E, A, B, 3, 12, 0, 11);
  110. P2(B, C, D, E, A, 4, 5, 9, 13);
  111. P2(A, B, C, D, E, 5, 8, 2, 15);
  112. P2(E, A, B, C, D, 6, 7, 11, 15);
  113. P2(D, E, A, B, C, 7, 9, 4, 5);
  114. P2(C, D, E, A, B, 8, 11, 13, 7);
  115. P2(B, C, D, E, A, 9, 13, 6, 7);
  116. P2(A, B, C, D, E, 10, 14, 15, 8);
  117. P2(E, A, B, C, D, 11, 15, 8, 11);
  118. P2(D, E, A, B, C, 12, 6, 1, 14);
  119. P2(C, D, E, A, B, 13, 7, 10, 14);
  120. P2(B, C, D, E, A, 14, 9, 3, 12);
  121. P2(A, B, C, D, E, 15, 8, 12, 6);
  122. #undef F
  123. #undef K
  124. #undef Fp
  125. #undef Kp
  126. #define F F2
  127. #define K 0x5A827999
  128. #define Fp F4
  129. #define Kp 0x5C4DD124
  130. P2(E, A, B, C, D, 7, 7, 6, 9);
  131. P2(D, E, A, B, C, 4, 6, 11, 13);
  132. P2(C, D, E, A, B, 13, 8, 3, 15);
  133. P2(B, C, D, E, A, 1, 13, 7, 7);
  134. P2(A, B, C, D, E, 10, 11, 0, 12);
  135. P2(E, A, B, C, D, 6, 9, 13, 8);
  136. P2(D, E, A, B, C, 15, 7, 5, 9);
  137. P2(C, D, E, A, B, 3, 15, 10, 11);
  138. P2(B, C, D, E, A, 12, 7, 14, 7);
  139. P2(A, B, C, D, E, 0, 12, 15, 7);
  140. P2(E, A, B, C, D, 9, 15, 8, 12);
  141. P2(D, E, A, B, C, 5, 9, 12, 7);
  142. P2(C, D, E, A, B, 2, 11, 4, 6);
  143. P2(B, C, D, E, A, 14, 7, 9, 15);
  144. P2(A, B, C, D, E, 11, 13, 1, 13);
  145. P2(E, A, B, C, D, 8, 12, 2, 11);
  146. #undef F
  147. #undef K
  148. #undef Fp
  149. #undef Kp
  150. #define F F3
  151. #define K 0x6ED9EBA1
  152. #define Fp F3
  153. #define Kp 0x6D703EF3
  154. P2(D, E, A, B, C, 3, 11, 15, 9);
  155. P2(C, D, E, A, B, 10, 13, 5, 7);
  156. P2(B, C, D, E, A, 14, 6, 1, 15);
  157. P2(A, B, C, D, E, 4, 7, 3, 11);
  158. P2(E, A, B, C, D, 9, 14, 7, 8);
  159. P2(D, E, A, B, C, 15, 9, 14, 6);
  160. P2(C, D, E, A, B, 8, 13, 6, 6);
  161. P2(B, C, D, E, A, 1, 15, 9, 14);
  162. P2(A, B, C, D, E, 2, 14, 11, 12);
  163. P2(E, A, B, C, D, 7, 8, 8, 13);
  164. P2(D, E, A, B, C, 0, 13, 12, 5);
  165. P2(C, D, E, A, B, 6, 6, 2, 14);
  166. P2(B, C, D, E, A, 13, 5, 10, 13);
  167. P2(A, B, C, D, E, 11, 12, 0, 13);
  168. P2(E, A, B, C, D, 5, 7, 4, 7);
  169. P2(D, E, A, B, C, 12, 5, 13, 5);
  170. #undef F
  171. #undef K
  172. #undef Fp
  173. #undef Kp
  174. #define F F4
  175. #define K 0x8F1BBCDC
  176. #define Fp F2
  177. #define Kp 0x7A6D76E9
  178. P2(C, D, E, A, B, 1, 11, 8, 15);
  179. P2(B, C, D, E, A, 9, 12, 6, 5);
  180. P2(A, B, C, D, E, 11, 14, 4, 8);
  181. P2(E, A, B, C, D, 10, 15, 1, 11);
  182. P2(D, E, A, B, C, 0, 14, 3, 14);
  183. P2(C, D, E, A, B, 8, 15, 11, 14);
  184. P2(B, C, D, E, A, 12, 9, 15, 6);
  185. P2(A, B, C, D, E, 4, 8, 0, 14);
  186. P2(E, A, B, C, D, 13, 9, 5, 6);
  187. P2(D, E, A, B, C, 3, 14, 12, 9);
  188. P2(C, D, E, A, B, 7, 5, 2, 12);
  189. P2(B, C, D, E, A, 15, 6, 13, 9);
  190. P2(A, B, C, D, E, 14, 8, 9, 12);
  191. P2(E, A, B, C, D, 5, 6, 7, 5);
  192. P2(D, E, A, B, C, 6, 5, 10, 15);
  193. P2(C, D, E, A, B, 2, 12, 14, 8);
  194. #undef F
  195. #undef K
  196. #undef Fp
  197. #undef Kp
  198. #define F F5
  199. #define K 0xA953FD4E
  200. #define Fp F1
  201. #define Kp 0x00000000
  202. P2(B, C, D, E, A, 4, 9, 12, 8);
  203. P2(A, B, C, D, E, 0, 15, 15, 5);
  204. P2(E, A, B, C, D, 5, 5, 10, 12);
  205. P2(D, E, A, B, C, 9, 11, 4, 9);
  206. P2(C, D, E, A, B, 7, 6, 1, 12);
  207. P2(B, C, D, E, A, 12, 8, 5, 5);
  208. P2(A, B, C, D, E, 2, 13, 8, 14);
  209. P2(E, A, B, C, D, 10, 12, 7, 6);
  210. P2(D, E, A, B, C, 14, 5, 6, 8);
  211. P2(C, D, E, A, B, 1, 12, 2, 13);
  212. P2(B, C, D, E, A, 3, 13, 13, 6);
  213. P2(A, B, C, D, E, 8, 14, 14, 5);
  214. P2(E, A, B, C, D, 11, 11, 0, 15);
  215. P2(D, E, A, B, C, 6, 8, 3, 13);
  216. P2(C, D, E, A, B, 15, 5, 9, 11);
  217. P2(B, C, D, E, A, 13, 6, 11, 11);
  218. #undef F
  219. #undef K
  220. #undef Fp
  221. #undef Kp
  222. C = ctx->state[1] + C + Dp;
  223. ctx->state[1] = ctx->state[2] + D + Ep;
  224. ctx->state[2] = ctx->state[3] + E + Ap;
  225. ctx->state[3] = ctx->state[4] + A + Bp;
  226. ctx->state[4] = ctx->state[0] + B + Cp;
  227. ctx->state[0] = C;
  228. }
  229. #endif /* !MBEDTLS_RIPEMD160_PROCESS_ALT */
  230. /*
  231. * RIPEMD-160 process buffer
  232. */
  233. void ripemd160_Update(RIPEMD160_CTX* ctx, const uint8_t* input, uint32_t ilen) {
  234. uint32_t fill = 0;
  235. uint32_t left = 0;
  236. if(ilen == 0) return;
  237. left = ctx->total[0] & 0x3F;
  238. fill = RIPEMD160_BLOCK_LENGTH - left;
  239. ctx->total[0] += (uint32_t)ilen;
  240. ctx->total[0] &= 0xFFFFFFFF;
  241. if(ctx->total[0] < (uint32_t)ilen) ctx->total[1]++;
  242. if(left && ilen >= fill) {
  243. memcpy((void*)(ctx->buffer + left), input, fill);
  244. ripemd160_process(ctx, ctx->buffer);
  245. input += fill;
  246. ilen -= fill;
  247. left = 0;
  248. }
  249. while(ilen >= RIPEMD160_BLOCK_LENGTH) {
  250. ripemd160_process(ctx, input);
  251. input += RIPEMD160_BLOCK_LENGTH;
  252. ilen -= RIPEMD160_BLOCK_LENGTH;
  253. }
  254. if(ilen > 0) {
  255. memcpy((void*)(ctx->buffer + left), input, ilen);
  256. }
  257. }
  258. static const uint8_t ripemd160_padding[RIPEMD160_BLOCK_LENGTH] = {
  259. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  260. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  261. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  262. /*
  263. * RIPEMD-160 final digest
  264. */
  265. void ripemd160_Final(RIPEMD160_CTX* ctx, uint8_t output[RIPEMD160_DIGEST_LENGTH]) {
  266. uint32_t last = 0;
  267. uint32_t padn = 0;
  268. uint32_t high = 0;
  269. uint32_t low = 0;
  270. uint8_t msglen[8] = {0};
  271. high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
  272. low = (ctx->total[0] << 3);
  273. PUT_UINT32_LE(low, msglen, 0);
  274. PUT_UINT32_LE(high, msglen, 4);
  275. last = ctx->total[0] & 0x3F;
  276. padn = (last < 56) ? (56 - last) : (120 - last);
  277. ripemd160_Update(ctx, ripemd160_padding, padn);
  278. ripemd160_Update(ctx, msglen, 8);
  279. PUT_UINT32_LE(ctx->state[0], output, 0);
  280. PUT_UINT32_LE(ctx->state[1], output, 4);
  281. PUT_UINT32_LE(ctx->state[2], output, 8);
  282. PUT_UINT32_LE(ctx->state[3], output, 12);
  283. PUT_UINT32_LE(ctx->state[4], output, 16);
  284. memzero(ctx, sizeof(RIPEMD160_CTX));
  285. }
  286. /*
  287. * output = RIPEMD-160( input buffer )
  288. */
  289. void ripemd160(const uint8_t* msg, uint32_t msg_len, uint8_t hash[RIPEMD160_DIGEST_LENGTH]) {
  290. RIPEMD160_CTX ctx = {0};
  291. ripemd160_Init(&ctx);
  292. ripemd160_Update(&ctx, msg, msg_len);
  293. ripemd160_Final(&ctx, hash);
  294. }