ripemd160.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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) ] ) \
  36. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  37. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  38. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  39. }
  40. #endif
  41. #ifndef PUT_UINT32_LE
  42. #define PUT_UINT32_LE(n,b,i) \
  43. { \
  44. (b)[(i) ] = (uint8_t) ( ( (n) ) & 0xFF ); \
  45. (b)[(i) + 1] = (uint8_t) ( ( (n) >> 8 ) & 0xFF ); \
  46. (b)[(i) + 2] = (uint8_t) ( ( (n) >> 16 ) & 0xFF ); \
  47. (b)[(i) + 3] = (uint8_t) ( ( (n) >> 24 ) & 0xFF ); \
  48. }
  49. #endif
  50. /*
  51. * RIPEMD-160 context setup
  52. */
  53. void ripemd160_Init(RIPEMD160_CTX *ctx)
  54. {
  55. memzero(ctx, sizeof(RIPEMD160_CTX));
  56. ctx->total[0] = 0;
  57. ctx->total[1] = 0;
  58. ctx->state[0] = 0x67452301;
  59. ctx->state[1] = 0xEFCDAB89;
  60. ctx->state[2] = 0x98BADCFE;
  61. ctx->state[3] = 0x10325476;
  62. ctx->state[4] = 0xC3D2E1F0;
  63. }
  64. #if !defined(MBEDTLS_RIPEMD160_PROCESS_ALT)
  65. /*
  66. * Process one block
  67. */
  68. void ripemd160_process( RIPEMD160_CTX *ctx, const uint8_t data[RIPEMD160_BLOCK_LENGTH] )
  69. {
  70. uint32_t A = 0, B = 0, C = 0, D = 0, E = 0, Ap = 0, Bp = 0, Cp = 0, Dp = 0, Ep = 0, X[16] = {0};
  71. GET_UINT32_LE( X[ 0], data, 0 );
  72. GET_UINT32_LE( X[ 1], data, 4 );
  73. GET_UINT32_LE( X[ 2], data, 8 );
  74. GET_UINT32_LE( X[ 3], data, 12 );
  75. GET_UINT32_LE( X[ 4], data, 16 );
  76. GET_UINT32_LE( X[ 5], data, 20 );
  77. GET_UINT32_LE( X[ 6], data, 24 );
  78. GET_UINT32_LE( X[ 7], data, 28 );
  79. GET_UINT32_LE( X[ 8], data, 32 );
  80. GET_UINT32_LE( X[ 9], data, 36 );
  81. GET_UINT32_LE( X[10], data, 40 );
  82. GET_UINT32_LE( X[11], data, 44 );
  83. GET_UINT32_LE( X[12], data, 48 );
  84. GET_UINT32_LE( X[13], data, 52 );
  85. GET_UINT32_LE( X[14], data, 56 );
  86. GET_UINT32_LE( X[15], data, 60 );
  87. A = Ap = ctx->state[0];
  88. B = Bp = ctx->state[1];
  89. C = Cp = ctx->state[2];
  90. D = Dp = ctx->state[3];
  91. E = Ep = ctx->state[4];
  92. #define F1( x, y, z ) ( x ^ y ^ z )
  93. #define F2( x, y, z ) ( ( x & y ) | ( ~x & z ) )
  94. #define F3( x, y, z ) ( ( x | ~y ) ^ z )
  95. #define F4( x, y, z ) ( ( x & z ) | ( y & ~z ) )
  96. #define F5( x, y, z ) ( x ^ ( y | ~z ) )
  97. #define S( x, n ) ( ( x << n ) | ( x >> (32 - n) ) )
  98. #define P( a, b, c, d, e, r, s, f, k ) \
  99. a += f( b, c, d ) + X[r] + k; \
  100. a = S( a, s ) + e; \
  101. c = S( c, 10 );
  102. #define P2( a, b, c, d, e, r, s, rp, sp ) \
  103. P( a, b, c, d, e, r, s, F, K ); \
  104. P( a ## p, b ## p, c ## p, d ## p, e ## p, rp, sp, Fp, Kp );
  105. #define F F1
  106. #define K 0x00000000
  107. #define Fp F5
  108. #define Kp 0x50A28BE6
  109. P2( A, B, C, D, E, 0, 11, 5, 8 );
  110. P2( E, A, B, C, D, 1, 14, 14, 9 );
  111. P2( D, E, A, B, C, 2, 15, 7, 9 );
  112. P2( C, D, E, A, B, 3, 12, 0, 11 );
  113. P2( B, C, D, E, A, 4, 5, 9, 13 );
  114. P2( A, B, C, D, E, 5, 8, 2, 15 );
  115. P2( E, A, B, C, D, 6, 7, 11, 15 );
  116. P2( D, E, A, B, C, 7, 9, 4, 5 );
  117. P2( C, D, E, A, B, 8, 11, 13, 7 );
  118. P2( B, C, D, E, A, 9, 13, 6, 7 );
  119. P2( A, B, C, D, E, 10, 14, 15, 8 );
  120. P2( E, A, B, C, D, 11, 15, 8, 11 );
  121. P2( D, E, A, B, C, 12, 6, 1, 14 );
  122. P2( C, D, E, A, B, 13, 7, 10, 14 );
  123. P2( B, C, D, E, A, 14, 9, 3, 12 );
  124. P2( A, B, C, D, E, 15, 8, 12, 6 );
  125. #undef F
  126. #undef K
  127. #undef Fp
  128. #undef Kp
  129. #define F F2
  130. #define K 0x5A827999
  131. #define Fp F4
  132. #define Kp 0x5C4DD124
  133. P2( E, A, B, C, D, 7, 7, 6, 9 );
  134. P2( D, E, A, B, C, 4, 6, 11, 13 );
  135. P2( C, D, E, A, B, 13, 8, 3, 15 );
  136. P2( B, C, D, E, A, 1, 13, 7, 7 );
  137. P2( A, B, C, D, E, 10, 11, 0, 12 );
  138. P2( E, A, B, C, D, 6, 9, 13, 8 );
  139. P2( D, E, A, B, C, 15, 7, 5, 9 );
  140. P2( C, D, E, A, B, 3, 15, 10, 11 );
  141. P2( B, C, D, E, A, 12, 7, 14, 7 );
  142. P2( A, B, C, D, E, 0, 12, 15, 7 );
  143. P2( E, A, B, C, D, 9, 15, 8, 12 );
  144. P2( D, E, A, B, C, 5, 9, 12, 7 );
  145. P2( C, D, E, A, B, 2, 11, 4, 6 );
  146. P2( B, C, D, E, A, 14, 7, 9, 15 );
  147. P2( A, B, C, D, E, 11, 13, 1, 13 );
  148. P2( E, A, B, C, D, 8, 12, 2, 11 );
  149. #undef F
  150. #undef K
  151. #undef Fp
  152. #undef Kp
  153. #define F F3
  154. #define K 0x6ED9EBA1
  155. #define Fp F3
  156. #define Kp 0x6D703EF3
  157. P2( D, E, A, B, C, 3, 11, 15, 9 );
  158. P2( C, D, E, A, B, 10, 13, 5, 7 );
  159. P2( B, C, D, E, A, 14, 6, 1, 15 );
  160. P2( A, B, C, D, E, 4, 7, 3, 11 );
  161. P2( E, A, B, C, D, 9, 14, 7, 8 );
  162. P2( D, E, A, B, C, 15, 9, 14, 6 );
  163. P2( C, D, E, A, B, 8, 13, 6, 6 );
  164. P2( B, C, D, E, A, 1, 15, 9, 14 );
  165. P2( A, B, C, D, E, 2, 14, 11, 12 );
  166. P2( E, A, B, C, D, 7, 8, 8, 13 );
  167. P2( D, E, A, B, C, 0, 13, 12, 5 );
  168. P2( C, D, E, A, B, 6, 6, 2, 14 );
  169. P2( B, C, D, E, A, 13, 5, 10, 13 );
  170. P2( A, B, C, D, E, 11, 12, 0, 13 );
  171. P2( E, A, B, C, D, 5, 7, 4, 7 );
  172. P2( D, E, A, B, C, 12, 5, 13, 5 );
  173. #undef F
  174. #undef K
  175. #undef Fp
  176. #undef Kp
  177. #define F F4
  178. #define K 0x8F1BBCDC
  179. #define Fp F2
  180. #define Kp 0x7A6D76E9
  181. P2( C, D, E, A, B, 1, 11, 8, 15 );
  182. P2( B, C, D, E, A, 9, 12, 6, 5 );
  183. P2( A, B, C, D, E, 11, 14, 4, 8 );
  184. P2( E, A, B, C, D, 10, 15, 1, 11 );
  185. P2( D, E, A, B, C, 0, 14, 3, 14 );
  186. P2( C, D, E, A, B, 8, 15, 11, 14 );
  187. P2( B, C, D, E, A, 12, 9, 15, 6 );
  188. P2( A, B, C, D, E, 4, 8, 0, 14 );
  189. P2( E, A, B, C, D, 13, 9, 5, 6 );
  190. P2( D, E, A, B, C, 3, 14, 12, 9 );
  191. P2( C, D, E, A, B, 7, 5, 2, 12 );
  192. P2( B, C, D, E, A, 15, 6, 13, 9 );
  193. P2( A, B, C, D, E, 14, 8, 9, 12 );
  194. P2( E, A, B, C, D, 5, 6, 7, 5 );
  195. P2( D, E, A, B, C, 6, 5, 10, 15 );
  196. P2( C, D, E, A, B, 2, 12, 14, 8 );
  197. #undef F
  198. #undef K
  199. #undef Fp
  200. #undef Kp
  201. #define F F5
  202. #define K 0xA953FD4E
  203. #define Fp F1
  204. #define Kp 0x00000000
  205. P2( B, C, D, E, A, 4, 9, 12, 8 );
  206. P2( A, B, C, D, E, 0, 15, 15, 5 );
  207. P2( E, A, B, C, D, 5, 5, 10, 12 );
  208. P2( D, E, A, B, C, 9, 11, 4, 9 );
  209. P2( C, D, E, A, B, 7, 6, 1, 12 );
  210. P2( B, C, D, E, A, 12, 8, 5, 5 );
  211. P2( A, B, C, D, E, 2, 13, 8, 14 );
  212. P2( E, A, B, C, D, 10, 12, 7, 6 );
  213. P2( D, E, A, B, C, 14, 5, 6, 8 );
  214. P2( C, D, E, A, B, 1, 12, 2, 13 );
  215. P2( B, C, D, E, A, 3, 13, 13, 6 );
  216. P2( A, B, C, D, E, 8, 14, 14, 5 );
  217. P2( E, A, B, C, D, 11, 11, 0, 15 );
  218. P2( D, E, A, B, C, 6, 8, 3, 13 );
  219. P2( C, D, E, A, B, 15, 5, 9, 11 );
  220. P2( B, C, D, E, A, 13, 6, 11, 11 );
  221. #undef F
  222. #undef K
  223. #undef Fp
  224. #undef Kp
  225. C = ctx->state[1] + C + Dp;
  226. ctx->state[1] = ctx->state[2] + D + Ep;
  227. ctx->state[2] = ctx->state[3] + E + Ap;
  228. ctx->state[3] = ctx->state[4] + A + Bp;
  229. ctx->state[4] = ctx->state[0] + B + Cp;
  230. ctx->state[0] = C;
  231. }
  232. #endif /* !MBEDTLS_RIPEMD160_PROCESS_ALT */
  233. /*
  234. * RIPEMD-160 process buffer
  235. */
  236. void ripemd160_Update( RIPEMD160_CTX *ctx, const uint8_t *input, uint32_t ilen )
  237. {
  238. uint32_t fill = 0;
  239. uint32_t left = 0;
  240. if( ilen == 0 )
  241. return;
  242. left = ctx->total[0] & 0x3F;
  243. fill = RIPEMD160_BLOCK_LENGTH - left;
  244. ctx->total[0] += (uint32_t) ilen;
  245. ctx->total[0] &= 0xFFFFFFFF;
  246. if( ctx->total[0] < (uint32_t) ilen )
  247. ctx->total[1]++;
  248. if( left && ilen >= fill )
  249. {
  250. memcpy( (void *) (ctx->buffer + left), input, fill );
  251. ripemd160_process( ctx, ctx->buffer );
  252. input += fill;
  253. ilen -= fill;
  254. left = 0;
  255. }
  256. while( ilen >= RIPEMD160_BLOCK_LENGTH )
  257. {
  258. ripemd160_process( ctx, input );
  259. input += RIPEMD160_BLOCK_LENGTH;
  260. ilen -= RIPEMD160_BLOCK_LENGTH;
  261. }
  262. if( ilen > 0 )
  263. {
  264. memcpy( (void *) (ctx->buffer + left), input, ilen );
  265. }
  266. }
  267. static const uint8_t ripemd160_padding[RIPEMD160_BLOCK_LENGTH] =
  268. {
  269. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  270. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  271. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  272. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  273. };
  274. /*
  275. * RIPEMD-160 final digest
  276. */
  277. void ripemd160_Final( RIPEMD160_CTX *ctx, uint8_t output[RIPEMD160_DIGEST_LENGTH] )
  278. {
  279. uint32_t last = 0; uint32_t padn = 0;
  280. uint32_t high = 0; uint32_t low = 0;
  281. uint8_t msglen[8] = {0};
  282. high = ( ctx->total[0] >> 29 )
  283. | ( ctx->total[1] << 3 );
  284. low = ( ctx->total[0] << 3 );
  285. PUT_UINT32_LE( low, msglen, 0 );
  286. PUT_UINT32_LE( high, msglen, 4 );
  287. last = ctx->total[0] & 0x3F;
  288. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  289. ripemd160_Update( ctx, ripemd160_padding, padn );
  290. ripemd160_Update( ctx, msglen, 8 );
  291. PUT_UINT32_LE( ctx->state[0], output, 0 );
  292. PUT_UINT32_LE( ctx->state[1], output, 4 );
  293. PUT_UINT32_LE( ctx->state[2], output, 8 );
  294. PUT_UINT32_LE( ctx->state[3], output, 12 );
  295. PUT_UINT32_LE( ctx->state[4], output, 16 );
  296. memzero(ctx, sizeof(RIPEMD160_CTX));
  297. }
  298. /*
  299. * output = RIPEMD-160( input buffer )
  300. */
  301. void ripemd160(const uint8_t *msg, uint32_t msg_len, uint8_t hash[RIPEMD160_DIGEST_LENGTH])
  302. {
  303. RIPEMD160_CTX ctx = {0};
  304. ripemd160_Init( &ctx );
  305. ripemd160_Update( &ctx, msg, msg_len );
  306. ripemd160_Final( &ctx, hash );
  307. }