md5.cpp 10 KB

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