1
0

md5.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * RFC 1321 compliant MD5 implementation
  3. *
  4. * Copyright (C) 2006-2007 Christophe Devine
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License, version 2.1 as published by the Free Software Foundation.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. * MA 02110-1301 USA
  19. */
  20. /*
  21. * The MD5 algorithm was designed by Ron Rivest in 1991.
  22. *
  23. * http://www.ietf.org/rfc/rfc1321.txt
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include "md5.h"
  31. /*
  32. * 32-bit integer manipulation macros (little endian)
  33. */
  34. #ifndef GET_UINT32_LE
  35. #define GET_UINT32_LE(n,b,i) \
  36. { \
  37. (n) = ( (unsigned long) (b)[(i) ] ) \
  38. | ( (unsigned long) (b)[(i) + 1] << 8 ) \
  39. | ( (unsigned long) (b)[(i) + 2] << 16 ) \
  40. | ( (unsigned long) (b)[(i) + 3] << 24 ); \
  41. }
  42. #endif
  43. #ifndef PUT_UINT32_LE
  44. #define PUT_UINT32_LE(n,b,i) \
  45. { \
  46. (b)[(i) ] = (unsigned char) ( (n) ); \
  47. (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \
  48. (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \
  49. (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \
  50. }
  51. #endif
  52. /*
  53. * MD5 context setup
  54. */
  55. void md5_starts( md5_context *ctx )
  56. {
  57. ctx->total[0] = 0;
  58. ctx->total[1] = 0;
  59. ctx->state[0] = 0x67452301;
  60. ctx->state[1] = 0xEFCDAB89;
  61. ctx->state[2] = 0x98BADCFE;
  62. ctx->state[3] = 0x10325476;
  63. }
  64. static void md5_process( md5_context *ctx, unsigned char data[64] )
  65. {
  66. unsigned long X[16], A, B, C, D;
  67. GET_UINT32_LE( X[ 0], data, 0 );
  68. GET_UINT32_LE( X[ 1], data, 4 );
  69. GET_UINT32_LE( X[ 2], data, 8 );
  70. GET_UINT32_LE( X[ 3], data, 12 );
  71. GET_UINT32_LE( X[ 4], data, 16 );
  72. GET_UINT32_LE( X[ 5], data, 20 );
  73. GET_UINT32_LE( X[ 6], data, 24 );
  74. GET_UINT32_LE( X[ 7], data, 28 );
  75. GET_UINT32_LE( X[ 8], data, 32 );
  76. GET_UINT32_LE( X[ 9], data, 36 );
  77. GET_UINT32_LE( X[10], data, 40 );
  78. GET_UINT32_LE( X[11], data, 44 );
  79. GET_UINT32_LE( X[12], data, 48 );
  80. GET_UINT32_LE( X[13], data, 52 );
  81. GET_UINT32_LE( X[14], data, 56 );
  82. GET_UINT32_LE( X[15], data, 60 );
  83. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  84. #define P(a,b,c,d,k,s,t) \
  85. { \
  86. a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
  87. }
  88. A = ctx->state[0];
  89. B = ctx->state[1];
  90. C = ctx->state[2];
  91. D = ctx->state[3];
  92. #define F(x,y,z) (z ^ (x & (y ^ z)))
  93. P( A, B, C, D, 0, 7, 0xD76AA478 );
  94. P( D, A, B, C, 1, 12, 0xE8C7B756 );
  95. P( C, D, A, B, 2, 17, 0x242070DB );
  96. P( B, C, D, A, 3, 22, 0xC1BDCEEE );
  97. P( A, B, C, D, 4, 7, 0xF57C0FAF );
  98. P( D, A, B, C, 5, 12, 0x4787C62A );
  99. P( C, D, A, B, 6, 17, 0xA8304613 );
  100. P( B, C, D, A, 7, 22, 0xFD469501 );
  101. P( A, B, C, D, 8, 7, 0x698098D8 );
  102. P( D, A, B, C, 9, 12, 0x8B44F7AF );
  103. P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
  104. P( B, C, D, A, 11, 22, 0x895CD7BE );
  105. P( A, B, C, D, 12, 7, 0x6B901122 );
  106. P( D, A, B, C, 13, 12, 0xFD987193 );
  107. P( C, D, A, B, 14, 17, 0xA679438E );
  108. P( B, C, D, A, 15, 22, 0x49B40821 );
  109. #undef F
  110. #define F(x,y,z) (y ^ (z & (x ^ y)))
  111. P( A, B, C, D, 1, 5, 0xF61E2562 );
  112. P( D, A, B, C, 6, 9, 0xC040B340 );
  113. P( C, D, A, B, 11, 14, 0x265E5A51 );
  114. P( B, C, D, A, 0, 20, 0xE9B6C7AA );
  115. P( A, B, C, D, 5, 5, 0xD62F105D );
  116. P( D, A, B, C, 10, 9, 0x02441453 );
  117. P( C, D, A, B, 15, 14, 0xD8A1E681 );
  118. P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
  119. P( A, B, C, D, 9, 5, 0x21E1CDE6 );
  120. P( D, A, B, C, 14, 9, 0xC33707D6 );
  121. P( C, D, A, B, 3, 14, 0xF4D50D87 );
  122. P( B, C, D, A, 8, 20, 0x455A14ED );
  123. P( A, B, C, D, 13, 5, 0xA9E3E905 );
  124. P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
  125. P( C, D, A, B, 7, 14, 0x676F02D9 );
  126. P( B, C, D, A, 12, 20, 0x8D2A4C8A );
  127. #undef F
  128. #define F(x,y,z) (x ^ y ^ z)
  129. P( A, B, C, D, 5, 4, 0xFFFA3942 );
  130. P( D, A, B, C, 8, 11, 0x8771F681 );
  131. P( C, D, A, B, 11, 16, 0x6D9D6122 );
  132. P( B, C, D, A, 14, 23, 0xFDE5380C );
  133. P( A, B, C, D, 1, 4, 0xA4BEEA44 );
  134. P( D, A, B, C, 4, 11, 0x4BDECFA9 );
  135. P( C, D, A, B, 7, 16, 0xF6BB4B60 );
  136. P( B, C, D, A, 10, 23, 0xBEBFBC70 );
  137. P( A, B, C, D, 13, 4, 0x289B7EC6 );
  138. P( D, A, B, C, 0, 11, 0xEAA127FA );
  139. P( C, D, A, B, 3, 16, 0xD4EF3085 );
  140. P( B, C, D, A, 6, 23, 0x04881D05 );
  141. P( A, B, C, D, 9, 4, 0xD9D4D039 );
  142. P( D, A, B, C, 12, 11, 0xE6DB99E5 );
  143. P( C, D, A, B, 15, 16, 0x1FA27CF8 );
  144. P( B, C, D, A, 2, 23, 0xC4AC5665 );
  145. #undef F
  146. #define F(x,y,z) (y ^ (x | ~z))
  147. P( A, B, C, D, 0, 6, 0xF4292244 );
  148. P( D, A, B, C, 7, 10, 0x432AFF97 );
  149. P( C, D, A, B, 14, 15, 0xAB9423A7 );
  150. P( B, C, D, A, 5, 21, 0xFC93A039 );
  151. P( A, B, C, D, 12, 6, 0x655B59C3 );
  152. P( D, A, B, C, 3, 10, 0x8F0CCC92 );
  153. P( C, D, A, B, 10, 15, 0xFFEFF47D );
  154. P( B, C, D, A, 1, 21, 0x85845DD1 );
  155. P( A, B, C, D, 8, 6, 0x6FA87E4F );
  156. P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
  157. P( C, D, A, B, 6, 15, 0xA3014314 );
  158. P( B, C, D, A, 13, 21, 0x4E0811A1 );
  159. P( A, B, C, D, 4, 6, 0xF7537E82 );
  160. P( D, A, B, C, 11, 10, 0xBD3AF235 );
  161. P( C, D, A, B, 2, 15, 0x2AD7D2BB );
  162. P( B, C, D, A, 9, 21, 0xEB86D391 );
  163. #undef F
  164. ctx->state[0] += A;
  165. ctx->state[1] += B;
  166. ctx->state[2] += C;
  167. ctx->state[3] += D;
  168. }
  169. /*
  170. * MD5 process buffer
  171. */
  172. void md5_update( md5_context *ctx, unsigned char *input, int ilen )
  173. {
  174. int fill;
  175. unsigned long left;
  176. if( ilen <= 0 )
  177. return;
  178. left = ctx->total[0] & 0x3F;
  179. fill = 64 - left;
  180. ctx->total[0] += ilen;
  181. ctx->total[0] &= 0xFFFFFFFF;
  182. if( ctx->total[0] < (unsigned long) ilen )
  183. ctx->total[1]++;
  184. if( left && ilen >= fill )
  185. {
  186. memcpy( (void *) (ctx->buffer + left),
  187. (void *) input, fill );
  188. md5_process( ctx, ctx->buffer );
  189. input += fill;
  190. ilen -= fill;
  191. left = 0;
  192. }
  193. while( ilen >= 64 )
  194. {
  195. md5_process( ctx, input );
  196. input += 64;
  197. ilen -= 64;
  198. }
  199. if( ilen > 0 )
  200. {
  201. memcpy( (void *) (ctx->buffer + left),
  202. (void *) input, ilen );
  203. }
  204. }
  205. static const unsigned char md5_padding[64] =
  206. {
  207. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  208. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  209. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  210. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  211. };
  212. /*
  213. * MD5 final digest
  214. */
  215. void md5_finish( md5_context *ctx, unsigned char *output )
  216. {
  217. unsigned long last, padn;
  218. unsigned long high, low;
  219. unsigned char msglen[8];
  220. high = ( ctx->total[0] >> 29 )
  221. | ( ctx->total[1] << 3 );
  222. low = ( ctx->total[0] << 3 );
  223. PUT_UINT32_LE( low, msglen, 0 );
  224. PUT_UINT32_LE( high, msglen, 4 );
  225. last = ctx->total[0] & 0x3F;
  226. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  227. md5_update( ctx, (unsigned char *) md5_padding, padn );
  228. md5_update( ctx, msglen, 8 );
  229. PUT_UINT32_LE( ctx->state[0], output, 0 );
  230. PUT_UINT32_LE( ctx->state[1], output, 4 );
  231. PUT_UINT32_LE( ctx->state[2], output, 8 );
  232. PUT_UINT32_LE( ctx->state[3], output, 12 );
  233. }
  234. /*
  235. * Output = MD5( input buffer )
  236. */
  237. void md5( unsigned char *input, int ilen,
  238. unsigned char *output )
  239. {
  240. md5_context ctx;
  241. md5_starts( &ctx );
  242. md5_update( &ctx, input, ilen );
  243. md5_finish( &ctx, output );
  244. memset( &ctx, 0, sizeof( md5_context ) );
  245. }
  246. /*
  247. * Output = MD5( file contents )
  248. */
  249. int md5_file( char *path, unsigned char *output )
  250. {
  251. FILE *f;
  252. size_t n;
  253. md5_context ctx;
  254. unsigned char buf[1024];
  255. if( ( f = fopen( path, "rb" ) ) == NULL )
  256. return( 1 );
  257. md5_starts( &ctx );
  258. while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
  259. md5_update( &ctx, buf, (int) n );
  260. md5_finish( &ctx, output );
  261. memset( &ctx, 0, sizeof( md5_context ) );
  262. if( ferror( f ) != 0 )
  263. {
  264. fclose( f );
  265. return( 2 );
  266. }
  267. fclose( f );
  268. return( 0 );
  269. }
  270. /*
  271. * MD5 HMAC context setup
  272. */
  273. void md5_hmac_starts( md5_context *ctx,
  274. unsigned char *key, int keylen )
  275. {
  276. int i;
  277. memset( ctx->ipad, 0x36, 64 );
  278. memset( ctx->opad, 0x5C, 64 );
  279. for( i = 0; i < keylen; i++ )
  280. {
  281. if( i >= 64 ) break;
  282. ctx->ipad[i] ^= key[i];
  283. ctx->opad[i] ^= key[i];
  284. }
  285. md5_starts( ctx );
  286. md5_update( ctx, ctx->ipad, 64 );
  287. }
  288. /*
  289. * MD5 HMAC process buffer
  290. */
  291. void md5_hmac_update( md5_context *ctx,
  292. unsigned char *input, int ilen )
  293. {
  294. md5_update( ctx, input, ilen );
  295. }
  296. /*
  297. * MD5 HMAC final digest
  298. */
  299. void md5_hmac_finish( md5_context *ctx, unsigned char *output )
  300. {
  301. unsigned char tmpbuf[16];
  302. md5_finish( ctx, tmpbuf );
  303. md5_starts( ctx );
  304. md5_update( ctx, ctx->opad, 64 );
  305. md5_update( ctx, tmpbuf, 16 );
  306. md5_finish( ctx, output );
  307. memset( tmpbuf, 0, sizeof( tmpbuf ) );
  308. }
  309. /*
  310. * Output = HMAC-MD5( hmac key, input buffer )
  311. */
  312. void md5_hmac( unsigned char *key, int keylen,
  313. unsigned char *input, int ilen,
  314. unsigned char *output )
  315. {
  316. md5_context ctx;
  317. md5_hmac_starts( &ctx, key, keylen );
  318. md5_hmac_update( &ctx, input, ilen );
  319. md5_hmac_finish( &ctx, output );
  320. memset( &ctx, 0, sizeof( md5_context ) );
  321. }
  322. static const char _md5_src[] = "_md5_src";
  323. #if defined(SELF_TEST)
  324. /*
  325. * RFC 1321 test vectors
  326. */
  327. static const char md5_test_str[7][81] =
  328. {
  329. { "" },
  330. { "a" },
  331. { "abc" },
  332. { "message digest" },
  333. { "abcdefghijklmnopqrstuvwxyz" },
  334. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  335. { "12345678901234567890123456789012345678901234567890123456789012" \
  336. "345678901234567890" }
  337. };
  338. static const unsigned char md5_test_sum[7][16] =
  339. {
  340. { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
  341. 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
  342. { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
  343. 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
  344. { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
  345. 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
  346. { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
  347. 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
  348. { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
  349. 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
  350. { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
  351. 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
  352. { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
  353. 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
  354. };
  355. /*
  356. * Checkup routine
  357. */
  358. int md5_self_test( int verbose )
  359. {
  360. int i;
  361. unsigned char md5sum[16];
  362. for( i = 0; i < 7; i++ )
  363. {
  364. if( verbose != 0 )
  365. printf( " MD5 test #%d: ", i + 1 );
  366. md5( (unsigned char *) md5_test_str[i],
  367. strlen( md5_test_str[i] ), md5sum );
  368. if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 )
  369. {
  370. if( verbose != 0 )
  371. printf( "failed\n" );
  372. return( 1 );
  373. }
  374. if( verbose != 0 )
  375. printf( "passed\n" );
  376. }
  377. if( verbose != 0 )
  378. printf( "\n" );
  379. return( 0 );
  380. }
  381. #else
  382. int md5_self_test( int verbose )
  383. {
  384. return( 0 );
  385. }
  386. #endif