md5.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. unsigned char ipad[64]; /*!< HMAC: inner padding */
  39. unsigned char opad[64]; /*!< HMAC: outer padding */
  40. }
  41. md5_context;
  42. /* Implementation that should never be optimized out by the compiler */
  43. static void polarssl_zeroize( void *v, size_t n ) {
  44. volatile unsigned char *p = (unsigned char *) v; while( n-- ) *p++ = 0;
  45. }
  46. /*
  47. * 32-bit integer manipulation macros (little endian)
  48. */
  49. #ifndef GET_UINT32_LE
  50. #define GET_UINT32_LE(n,b,i) \
  51. { \
  52. (n) = ( (uint32_t) (b)[(i) ] ) \
  53. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  54. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  55. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  56. }
  57. #endif
  58. #ifndef PUT_UINT32_LE
  59. #define PUT_UINT32_LE(n,b,i) \
  60. { \
  61. (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
  62. (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
  63. (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
  64. (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
  65. }
  66. #endif
  67. void md5_init( md5_context *ctx )
  68. {
  69. memset( ctx, 0, sizeof( md5_context ) );
  70. }
  71. void md5_free( md5_context *ctx )
  72. {
  73. if( ctx == NULL )
  74. return;
  75. polarssl_zeroize( ctx, sizeof( md5_context ) );
  76. }
  77. /*
  78. * MD5 context setup
  79. */
  80. void md5_starts( md5_context *ctx )
  81. {
  82. ctx->total[0] = 0;
  83. ctx->total[1] = 0;
  84. ctx->state[0] = 0x67452301;
  85. ctx->state[1] = 0xEFCDAB89;
  86. ctx->state[2] = 0x98BADCFE;
  87. ctx->state[3] = 0x10325476;
  88. }
  89. void md5_process( md5_context *ctx, const unsigned char data[64] )
  90. {
  91. uint32_t X[16], A, B, C, D;
  92. GET_UINT32_LE( X[ 0], data, 0 );
  93. GET_UINT32_LE( X[ 1], data, 4 );
  94. GET_UINT32_LE( X[ 2], data, 8 );
  95. GET_UINT32_LE( X[ 3], data, 12 );
  96. GET_UINT32_LE( X[ 4], data, 16 );
  97. GET_UINT32_LE( X[ 5], data, 20 );
  98. GET_UINT32_LE( X[ 6], data, 24 );
  99. GET_UINT32_LE( X[ 7], data, 28 );
  100. GET_UINT32_LE( X[ 8], data, 32 );
  101. GET_UINT32_LE( X[ 9], data, 36 );
  102. GET_UINT32_LE( X[10], data, 40 );
  103. GET_UINT32_LE( X[11], data, 44 );
  104. GET_UINT32_LE( X[12], data, 48 );
  105. GET_UINT32_LE( X[13], data, 52 );
  106. GET_UINT32_LE( X[14], data, 56 );
  107. GET_UINT32_LE( X[15], data, 60 );
  108. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  109. #define P(a,b,c,d,k,s,t) \
  110. { \
  111. a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
  112. }
  113. A = ctx->state[0];
  114. B = ctx->state[1];
  115. C = ctx->state[2];
  116. D = ctx->state[3];
  117. #define F(x,y,z) (z ^ (x & (y ^ z)))
  118. P( A, B, C, D, 0, 7, 0xD76AA478 );
  119. P( D, A, B, C, 1, 12, 0xE8C7B756 );
  120. P( C, D, A, B, 2, 17, 0x242070DB );
  121. P( B, C, D, A, 3, 22, 0xC1BDCEEE );
  122. P( A, B, C, D, 4, 7, 0xF57C0FAF );
  123. P( D, A, B, C, 5, 12, 0x4787C62A );
  124. P( C, D, A, B, 6, 17, 0xA8304613 );
  125. P( B, C, D, A, 7, 22, 0xFD469501 );
  126. P( A, B, C, D, 8, 7, 0x698098D8 );
  127. P( D, A, B, C, 9, 12, 0x8B44F7AF );
  128. P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
  129. P( B, C, D, A, 11, 22, 0x895CD7BE );
  130. P( A, B, C, D, 12, 7, 0x6B901122 );
  131. P( D, A, B, C, 13, 12, 0xFD987193 );
  132. P( C, D, A, B, 14, 17, 0xA679438E );
  133. P( B, C, D, A, 15, 22, 0x49B40821 );
  134. #undef F
  135. #define F(x,y,z) (y ^ (z & (x ^ y)))
  136. P( A, B, C, D, 1, 5, 0xF61E2562 );
  137. P( D, A, B, C, 6, 9, 0xC040B340 );
  138. P( C, D, A, B, 11, 14, 0x265E5A51 );
  139. P( B, C, D, A, 0, 20, 0xE9B6C7AA );
  140. P( A, B, C, D, 5, 5, 0xD62F105D );
  141. P( D, A, B, C, 10, 9, 0x02441453 );
  142. P( C, D, A, B, 15, 14, 0xD8A1E681 );
  143. P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
  144. P( A, B, C, D, 9, 5, 0x21E1CDE6 );
  145. P( D, A, B, C, 14, 9, 0xC33707D6 );
  146. P( C, D, A, B, 3, 14, 0xF4D50D87 );
  147. P( B, C, D, A, 8, 20, 0x455A14ED );
  148. P( A, B, C, D, 13, 5, 0xA9E3E905 );
  149. P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
  150. P( C, D, A, B, 7, 14, 0x676F02D9 );
  151. P( B, C, D, A, 12, 20, 0x8D2A4C8A );
  152. #undef F
  153. #define F(x,y,z) (x ^ y ^ z)
  154. P( A, B, C, D, 5, 4, 0xFFFA3942 );
  155. P( D, A, B, C, 8, 11, 0x8771F681 );
  156. P( C, D, A, B, 11, 16, 0x6D9D6122 );
  157. P( B, C, D, A, 14, 23, 0xFDE5380C );
  158. P( A, B, C, D, 1, 4, 0xA4BEEA44 );
  159. P( D, A, B, C, 4, 11, 0x4BDECFA9 );
  160. P( C, D, A, B, 7, 16, 0xF6BB4B60 );
  161. P( B, C, D, A, 10, 23, 0xBEBFBC70 );
  162. P( A, B, C, D, 13, 4, 0x289B7EC6 );
  163. P( D, A, B, C, 0, 11, 0xEAA127FA );
  164. P( C, D, A, B, 3, 16, 0xD4EF3085 );
  165. P( B, C, D, A, 6, 23, 0x04881D05 );
  166. P( A, B, C, D, 9, 4, 0xD9D4D039 );
  167. P( D, A, B, C, 12, 11, 0xE6DB99E5 );
  168. P( C, D, A, B, 15, 16, 0x1FA27CF8 );
  169. P( B, C, D, A, 2, 23, 0xC4AC5665 );
  170. #undef F
  171. #define F(x,y,z) (y ^ (x | ~z))
  172. P( A, B, C, D, 0, 6, 0xF4292244 );
  173. P( D, A, B, C, 7, 10, 0x432AFF97 );
  174. P( C, D, A, B, 14, 15, 0xAB9423A7 );
  175. P( B, C, D, A, 5, 21, 0xFC93A039 );
  176. P( A, B, C, D, 12, 6, 0x655B59C3 );
  177. P( D, A, B, C, 3, 10, 0x8F0CCC92 );
  178. P( C, D, A, B, 10, 15, 0xFFEFF47D );
  179. P( B, C, D, A, 1, 21, 0x85845DD1 );
  180. P( A, B, C, D, 8, 6, 0x6FA87E4F );
  181. P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
  182. P( C, D, A, B, 6, 15, 0xA3014314 );
  183. P( B, C, D, A, 13, 21, 0x4E0811A1 );
  184. P( A, B, C, D, 4, 6, 0xF7537E82 );
  185. P( D, A, B, C, 11, 10, 0xBD3AF235 );
  186. P( C, D, A, B, 2, 15, 0x2AD7D2BB );
  187. P( B, C, D, A, 9, 21, 0xEB86D391 );
  188. #undef F
  189. ctx->state[0] += A;
  190. ctx->state[1] += B;
  191. ctx->state[2] += C;
  192. ctx->state[3] += D;
  193. }
  194. /*
  195. * MD5 process buffer
  196. */
  197. void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen )
  198. {
  199. size_t fill;
  200. uint32_t left;
  201. if( ilen == 0 )
  202. return;
  203. left = ctx->total[0] & 0x3F;
  204. fill = 64 - left;
  205. ctx->total[0] += (uint32_t) ilen;
  206. ctx->total[0] &= 0xFFFFFFFF;
  207. if( ctx->total[0] < (uint32_t) ilen )
  208. ctx->total[1]++;
  209. if( left && ilen >= fill )
  210. {
  211. memcpy( (void *) (ctx->buffer + left), input, fill );
  212. md5_process( ctx, ctx->buffer );
  213. input += fill;
  214. ilen -= fill;
  215. left = 0;
  216. }
  217. while( ilen >= 64 )
  218. {
  219. md5_process( ctx, input );
  220. input += 64;
  221. ilen -= 64;
  222. }
  223. if( ilen > 0 )
  224. {
  225. memcpy( (void *) (ctx->buffer + left), input, ilen );
  226. }
  227. }
  228. static const unsigned char md5_padding[64] =
  229. {
  230. 0x80, 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. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  233. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  234. };
  235. /*
  236. * MD5 final digest
  237. */
  238. void md5_finish( md5_context *ctx, unsigned char output[16] )
  239. {
  240. uint32_t last, padn;
  241. uint32_t high, low;
  242. unsigned char msglen[8];
  243. high = ( ctx->total[0] >> 29 )
  244. | ( ctx->total[1] << 3 );
  245. low = ( ctx->total[0] << 3 );
  246. PUT_UINT32_LE( low, msglen, 0 );
  247. PUT_UINT32_LE( high, msglen, 4 );
  248. last = ctx->total[0] & 0x3F;
  249. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  250. md5_update( ctx, md5_padding, padn );
  251. md5_update( ctx, msglen, 8 );
  252. PUT_UINT32_LE( ctx->state[0], output, 0 );
  253. PUT_UINT32_LE( ctx->state[1], output, 4 );
  254. PUT_UINT32_LE( ctx->state[2], output, 8 );
  255. PUT_UINT32_LE( ctx->state[3], output, 12 );
  256. }
  257. /*
  258. * output = MD5( input buffer )
  259. */
  260. void md5( const unsigned char *input, size_t ilen, unsigned char output[16] )
  261. {
  262. /*static md5_context ctx;
  263. static int done=0;
  264. if(done==0)
  265. {
  266. md5_init( &ctx );
  267. done=1;
  268. }*/
  269. md5_context ctx;
  270. md5_init( &ctx );
  271. md5_starts( &ctx );
  272. md5_update( &ctx, input, ilen );
  273. md5_finish( &ctx, output );
  274. md5_free( &ctx );
  275. }
  276. /*
  277. * MD5 HMAC context setup
  278. */
  279. void md5_hmac_starts( md5_context *ctx, const unsigned char *key,
  280. size_t keylen )
  281. {
  282. size_t i;
  283. unsigned char sum[16];
  284. if( keylen > 64 )
  285. {
  286. md5( key, keylen, sum );
  287. keylen = 16;
  288. key = sum;
  289. }
  290. memset( ctx->ipad, 0x36, 64 );
  291. memset( ctx->opad, 0x5C, 64 );
  292. for( i = 0; i < keylen; i++ )
  293. {
  294. ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
  295. ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
  296. }
  297. md5_starts( ctx );
  298. md5_update( ctx, ctx->ipad, 64 );
  299. polarssl_zeroize( sum, sizeof( sum ) );
  300. }
  301. /*
  302. * MD5 HMAC process buffer
  303. */
  304. void md5_hmac_update( md5_context *ctx, const unsigned char *input,
  305. size_t ilen )
  306. {
  307. md5_update( ctx, input, ilen );
  308. }
  309. /*
  310. * MD5 HMAC final digest
  311. */
  312. void md5_hmac_finish( md5_context *ctx, unsigned char output[16] )
  313. {
  314. unsigned char tmpbuf[16];
  315. md5_finish( ctx, tmpbuf );
  316. md5_starts( ctx );
  317. md5_update( ctx, ctx->opad, 64 );
  318. md5_update( ctx, tmpbuf, 16 );
  319. md5_finish( ctx, output );
  320. polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) );
  321. }
  322. /*
  323. * MD5 HMAC context reset
  324. */
  325. void md5_hmac_reset( md5_context *ctx )
  326. {
  327. md5_starts( ctx );
  328. md5_update( ctx, ctx->ipad, 64 );
  329. }
  330. /*
  331. * output = HMAC-MD5( hmac key, input buffer )
  332. */
  333. void md5_hmac( const unsigned char *key, size_t keylen,
  334. const unsigned char *input, size_t ilen,
  335. unsigned char output[16] )
  336. {
  337. md5_context ctx;
  338. md5_init( &ctx );
  339. md5_hmac_starts( &ctx, key, keylen );
  340. md5_hmac_update( &ctx, input, ilen );
  341. md5_hmac_finish( &ctx, output );
  342. md5_free( &ctx );
  343. }