md5.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * This file is adapted from PolarSSL 1.3.19 (GPL)
  3. */
  4. /*
  5. * RFC 1321 compliant MD5 implementation
  6. *
  7. * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
  8. *
  9. * This file is part of mbed TLS (https://tls.mbed.org)
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * The MD5 algorithm was designed by Ron Rivest in 1991.
  27. *
  28. * http://www.ietf.org/rfc/rfc1321.txt
  29. */
  30. #include <string.h>
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. typedef struct
  34. {
  35. uint32_t total[2]; /*!< number of bytes processed */
  36. uint32_t state[4]; /*!< intermediate digest state */
  37. unsigned char buffer[64]; /*!< data block being processed */
  38. }
  39. md5_context;
  40. /* Implementation that should never be optimized out by the compiler */
  41. static void polarssl_zeroize( void *v, size_t n ) {
  42. volatile unsigned char *p = (unsigned char *) v; while( n-- ) *p++ = 0;
  43. }
  44. /*
  45. * 32-bit integer manipulation macros (little endian)
  46. */
  47. #ifndef GET_UINT32_LE
  48. #define GET_UINT32_LE(n,b,i) \
  49. { \
  50. (n) = ( (uint32_t) (b)[(i) ] ) \
  51. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  52. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  53. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  54. }
  55. #endif
  56. #ifndef PUT_UINT32_LE
  57. #define PUT_UINT32_LE(n,b,i) \
  58. { \
  59. (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
  60. (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
  61. (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
  62. (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
  63. }
  64. #endif
  65. void md5_init( md5_context *ctx )
  66. {
  67. memset( ctx, 0, sizeof( md5_context ) );
  68. }
  69. void md5_free( md5_context *ctx )
  70. {
  71. if( ctx == NULL )
  72. return;
  73. polarssl_zeroize( ctx, sizeof( md5_context ) );
  74. }
  75. /*
  76. * MD5 context setup
  77. */
  78. void md5_starts( md5_context *ctx )
  79. {
  80. ctx->total[0] = 0;
  81. ctx->total[1] = 0;
  82. ctx->state[0] = 0x67452301;
  83. ctx->state[1] = 0xEFCDAB89;
  84. ctx->state[2] = 0x98BADCFE;
  85. ctx->state[3] = 0x10325476;
  86. }
  87. void md5_process( md5_context *ctx, const unsigned char data[64] )
  88. {
  89. uint32_t X[16], A, B, C, D;
  90. GET_UINT32_LE( X[ 0], data, 0 );
  91. GET_UINT32_LE( X[ 1], data, 4 );
  92. GET_UINT32_LE( X[ 2], data, 8 );
  93. GET_UINT32_LE( X[ 3], data, 12 );
  94. GET_UINT32_LE( X[ 4], data, 16 );
  95. GET_UINT32_LE( X[ 5], data, 20 );
  96. GET_UINT32_LE( X[ 6], data, 24 );
  97. GET_UINT32_LE( X[ 7], data, 28 );
  98. GET_UINT32_LE( X[ 8], data, 32 );
  99. GET_UINT32_LE( X[ 9], data, 36 );
  100. GET_UINT32_LE( X[10], data, 40 );
  101. GET_UINT32_LE( X[11], data, 44 );
  102. GET_UINT32_LE( X[12], data, 48 );
  103. GET_UINT32_LE( X[13], data, 52 );
  104. GET_UINT32_LE( X[14], data, 56 );
  105. GET_UINT32_LE( X[15], data, 60 );
  106. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  107. #define P(a,b,c,d,k,s,t) \
  108. { \
  109. a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
  110. }
  111. A = ctx->state[0];
  112. B = ctx->state[1];
  113. C = ctx->state[2];
  114. D = ctx->state[3];
  115. #define F(x,y,z) (z ^ (x & (y ^ z)))
  116. P( A, B, C, D, 0, 7, 0xD76AA478 );
  117. P( D, A, B, C, 1, 12, 0xE8C7B756 );
  118. P( C, D, A, B, 2, 17, 0x242070DB );
  119. P( B, C, D, A, 3, 22, 0xC1BDCEEE );
  120. P( A, B, C, D, 4, 7, 0xF57C0FAF );
  121. P( D, A, B, C, 5, 12, 0x4787C62A );
  122. P( C, D, A, B, 6, 17, 0xA8304613 );
  123. P( B, C, D, A, 7, 22, 0xFD469501 );
  124. P( A, B, C, D, 8, 7, 0x698098D8 );
  125. P( D, A, B, C, 9, 12, 0x8B44F7AF );
  126. P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
  127. P( B, C, D, A, 11, 22, 0x895CD7BE );
  128. P( A, B, C, D, 12, 7, 0x6B901122 );
  129. P( D, A, B, C, 13, 12, 0xFD987193 );
  130. P( C, D, A, B, 14, 17, 0xA679438E );
  131. P( B, C, D, A, 15, 22, 0x49B40821 );
  132. #undef F
  133. #define F(x,y,z) (y ^ (z & (x ^ y)))
  134. P( A, B, C, D, 1, 5, 0xF61E2562 );
  135. P( D, A, B, C, 6, 9, 0xC040B340 );
  136. P( C, D, A, B, 11, 14, 0x265E5A51 );
  137. P( B, C, D, A, 0, 20, 0xE9B6C7AA );
  138. P( A, B, C, D, 5, 5, 0xD62F105D );
  139. P( D, A, B, C, 10, 9, 0x02441453 );
  140. P( C, D, A, B, 15, 14, 0xD8A1E681 );
  141. P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
  142. P( A, B, C, D, 9, 5, 0x21E1CDE6 );
  143. P( D, A, B, C, 14, 9, 0xC33707D6 );
  144. P( C, D, A, B, 3, 14, 0xF4D50D87 );
  145. P( B, C, D, A, 8, 20, 0x455A14ED );
  146. P( A, B, C, D, 13, 5, 0xA9E3E905 );
  147. P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
  148. P( C, D, A, B, 7, 14, 0x676F02D9 );
  149. P( B, C, D, A, 12, 20, 0x8D2A4C8A );
  150. #undef F
  151. #define F(x,y,z) (x ^ y ^ z)
  152. P( A, B, C, D, 5, 4, 0xFFFA3942 );
  153. P( D, A, B, C, 8, 11, 0x8771F681 );
  154. P( C, D, A, B, 11, 16, 0x6D9D6122 );
  155. P( B, C, D, A, 14, 23, 0xFDE5380C );
  156. P( A, B, C, D, 1, 4, 0xA4BEEA44 );
  157. P( D, A, B, C, 4, 11, 0x4BDECFA9 );
  158. P( C, D, A, B, 7, 16, 0xF6BB4B60 );
  159. P( B, C, D, A, 10, 23, 0xBEBFBC70 );
  160. P( A, B, C, D, 13, 4, 0x289B7EC6 );
  161. P( D, A, B, C, 0, 11, 0xEAA127FA );
  162. P( C, D, A, B, 3, 16, 0xD4EF3085 );
  163. P( B, C, D, A, 6, 23, 0x04881D05 );
  164. P( A, B, C, D, 9, 4, 0xD9D4D039 );
  165. P( D, A, B, C, 12, 11, 0xE6DB99E5 );
  166. P( C, D, A, B, 15, 16, 0x1FA27CF8 );
  167. P( B, C, D, A, 2, 23, 0xC4AC5665 );
  168. #undef F
  169. #define F(x,y,z) (y ^ (x | ~z))
  170. P( A, B, C, D, 0, 6, 0xF4292244 );
  171. P( D, A, B, C, 7, 10, 0x432AFF97 );
  172. P( C, D, A, B, 14, 15, 0xAB9423A7 );
  173. P( B, C, D, A, 5, 21, 0xFC93A039 );
  174. P( A, B, C, D, 12, 6, 0x655B59C3 );
  175. P( D, A, B, C, 3, 10, 0x8F0CCC92 );
  176. P( C, D, A, B, 10, 15, 0xFFEFF47D );
  177. P( B, C, D, A, 1, 21, 0x85845DD1 );
  178. P( A, B, C, D, 8, 6, 0x6FA87E4F );
  179. P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
  180. P( C, D, A, B, 6, 15, 0xA3014314 );
  181. P( B, C, D, A, 13, 21, 0x4E0811A1 );
  182. P( A, B, C, D, 4, 6, 0xF7537E82 );
  183. P( D, A, B, C, 11, 10, 0xBD3AF235 );
  184. P( C, D, A, B, 2, 15, 0x2AD7D2BB );
  185. P( B, C, D, A, 9, 21, 0xEB86D391 );
  186. #undef F
  187. ctx->state[0] += A;
  188. ctx->state[1] += B;
  189. ctx->state[2] += C;
  190. ctx->state[3] += D;
  191. }
  192. /*
  193. * MD5 process buffer
  194. */
  195. void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen )
  196. {
  197. size_t fill;
  198. uint32_t left;
  199. if( ilen == 0 )
  200. return;
  201. left = ctx->total[0] & 0x3F;
  202. fill = 64 - left;
  203. ctx->total[0] += (uint32_t) ilen;
  204. ctx->total[0] &= 0xFFFFFFFF;
  205. if( ctx->total[0] < (uint32_t) ilen )
  206. ctx->total[1]++;
  207. if( left && ilen >= fill )
  208. {
  209. memcpy( (void *) (ctx->buffer + left), input, fill );
  210. md5_process( ctx, ctx->buffer );
  211. input += fill;
  212. ilen -= fill;
  213. left = 0;
  214. }
  215. while( ilen >= 64 )
  216. {
  217. md5_process( ctx, input );
  218. input += 64;
  219. ilen -= 64;
  220. }
  221. if( ilen > 0 )
  222. {
  223. memcpy( (void *) (ctx->buffer + left), input, ilen );
  224. }
  225. }
  226. static const unsigned char md5_padding[64] =
  227. {
  228. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  229. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  230. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  231. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  232. };
  233. /*
  234. * MD5 final digest
  235. */
  236. void md5_finish( md5_context *ctx, unsigned char output[16] )
  237. {
  238. uint32_t last, padn;
  239. uint32_t high, low;
  240. unsigned char msglen[8];
  241. high = ( ctx->total[0] >> 29 )
  242. | ( ctx->total[1] << 3 );
  243. low = ( ctx->total[0] << 3 );
  244. PUT_UINT32_LE( low, msglen, 0 );
  245. PUT_UINT32_LE( high, msglen, 4 );
  246. last = ctx->total[0] & 0x3F;
  247. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  248. md5_update( ctx, md5_padding, padn );
  249. md5_update( ctx, msglen, 8 );
  250. PUT_UINT32_LE( ctx->state[0], output, 0 );
  251. PUT_UINT32_LE( ctx->state[1], output, 4 );
  252. PUT_UINT32_LE( ctx->state[2], output, 8 );
  253. PUT_UINT32_LE( ctx->state[3], output, 12 );
  254. }
  255. /*
  256. * output = MD5( input buffer )
  257. */
  258. void md5( const unsigned char *input, size_t ilen, unsigned char output[16] )
  259. {
  260. md5_context ctx;
  261. md5_init( &ctx );
  262. md5_starts( &ctx );
  263. md5_update( &ctx, input, ilen );
  264. md5_finish( &ctx, output );
  265. md5_free( &ctx );
  266. }