Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "basetypes.h"
  7. #include "commonmacros.h"
  8. #include "checksum_md5.h"
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include "tier1/strtools.h"
  12. #include "tier0/dbg.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. // The four core functions - F1 is optimized somewhat
  16. // #define F1(x, y, z) (x & y | ~x & z)
  17. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  18. #define F2(x, y, z) F1(z, x, y)
  19. #define F3(x, y, z) (x ^ y ^ z)
  20. #define F4(x, y, z) (y ^ (x | ~z))
  21. // This is the central step in the MD5 algorithm.
  22. #define MD5STEP(f, w, x, y, z, data, s) \
  23. ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
  24. //-----------------------------------------------------------------------------
  25. // Purpose: The core of the MD5 algorithm, this alters an existing MD5 hash to
  26. // reflect the addition of 16 longwords of new data. MD5Update blocks
  27. // the data and converts bytes into longwords for this routine.
  28. // Input : buf[4] -
  29. // in[16] -
  30. // Output : static void
  31. //-----------------------------------------------------------------------------
  32. static void MD5Transform(unsigned int buf[4], unsigned int const in[16])
  33. {
  34. register unsigned int a, b, c, d;
  35. a = buf[0];
  36. b = buf[1];
  37. c = buf[2];
  38. d = buf[3];
  39. MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  40. MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  41. MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  42. MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  43. MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  44. MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  45. MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  46. MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  47. MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  48. MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  49. MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  50. MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  51. MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  52. MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  53. MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  54. MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  55. MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  56. MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  57. MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  58. MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  59. MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  60. MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  61. MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  62. MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  63. MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  64. MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  65. MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  66. MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  67. MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  68. MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  69. MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  70. MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  71. MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  72. MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  73. MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  74. MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  75. MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  76. MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  77. MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  78. MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  79. MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  80. MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  81. MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  82. MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  83. MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  84. MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  85. MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  86. MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  87. MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  88. MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  89. MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  90. MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  91. MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  92. MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  93. MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  94. MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  95. MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  96. MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  97. MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  98. MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  99. MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  100. MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  101. MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  102. MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  103. buf[0] += a;
  104. buf[1] += b;
  105. buf[2] += c;
  106. buf[3] += d;
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Purpose: Start MD5 accumulation. Set bit count to 0 and buffer to mysterious initialization constants.
  110. // Input : *ctx -
  111. //-----------------------------------------------------------------------------
  112. void MD5Init(MD5Context_t *ctx)
  113. {
  114. ctx->buf[0] = 0x67452301;
  115. ctx->buf[1] = 0xefcdab89;
  116. ctx->buf[2] = 0x98badcfe;
  117. ctx->buf[3] = 0x10325476;
  118. ctx->bits[0] = 0;
  119. ctx->bits[1] = 0;
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Purpose: Update context to reflect the concatenation of another buffer full of bytes.
  123. // Input : *ctx -
  124. // *buf -
  125. // len -
  126. //-----------------------------------------------------------------------------
  127. void MD5Update(MD5Context_t *ctx, unsigned char const *buf, unsigned int len)
  128. {
  129. unsigned int t;
  130. /* Update bitcount */
  131. t = ctx->bits[0];
  132. if ((ctx->bits[0] = t + ((unsigned int) len << 3)) < t)
  133. ctx->bits[1]++; /* Carry from low to high */
  134. ctx->bits[1] += len >> 29;
  135. t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
  136. /* Handle any leading odd-sized chunks */
  137. if (t)
  138. {
  139. unsigned char *p = (unsigned char *) ctx->in + t;
  140. t = 64 - t;
  141. if (len < t)
  142. {
  143. memcpy(p, buf, len);
  144. return;
  145. }
  146. memcpy(p, buf, t);
  147. //byteReverse(ctx->in, 16);
  148. MD5Transform(ctx->buf, (unsigned int *) ctx->in);
  149. buf += t;
  150. len -= t;
  151. }
  152. /* Process data in 64-byte chunks */
  153. while (len >= 64)
  154. {
  155. memcpy(ctx->in, buf, 64);
  156. //byteReverse(ctx->in, 16);
  157. MD5Transform(ctx->buf, (unsigned int *) ctx->in);
  158. buf += 64;
  159. len -= 64;
  160. }
  161. /* Handle any remaining bytes of data. */
  162. memcpy(ctx->in, buf, len);
  163. }
  164. //-----------------------------------------------------------------------------
  165. // Purpose: Final wrapup - pad to 64-byte boundary with the bit pattern
  166. // 1 0* (64-bit count of bits processed, MSB-first)
  167. // Input : digest[MD5_DIGEST_LENGTH] -
  168. // *ctx -
  169. //-----------------------------------------------------------------------------
  170. void MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5Context_t *ctx)
  171. {
  172. unsigned count;
  173. unsigned char *p;
  174. /* Compute number of bytes mod 64 */
  175. count = (ctx->bits[0] >> 3) & 0x3F;
  176. /* Set the first char of padding to 0x80. This is safe since there is
  177. always at least one byte free */
  178. p = ctx->in + count;
  179. *p++ = 0x80;
  180. /* Bytes of padding needed to make 64 bytes */
  181. count = 64 - 1 - count;
  182. /* Pad out to 56 mod 64 */
  183. if (count < 8)
  184. {
  185. /* Two lots of padding: Pad the first block to 64 bytes */
  186. memset(p, 0, count);
  187. //byteReverse(ctx->in, 16);
  188. MD5Transform(ctx->buf, (unsigned int *) ctx->in);
  189. /* Now fill the next block with 56 bytes */
  190. memset(ctx->in, 0, 56);
  191. }
  192. else
  193. {
  194. /* Pad block to 56 bytes */
  195. memset(p, 0, count - 8);
  196. }
  197. //byteReverse(ctx->in, 14);
  198. /* Append length in bits and transform */
  199. ((unsigned int *) ctx->in)[14] = ctx->bits[0];
  200. ((unsigned int *) ctx->in)[15] = ctx->bits[1];
  201. MD5Transform(ctx->buf, (unsigned int *) ctx->in);
  202. //byteReverse((unsigned char *) ctx->buf, 4);
  203. memcpy(digest, ctx->buf, MD5_DIGEST_LENGTH);
  204. memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
  205. }
  206. //-----------------------------------------------------------------------------
  207. // Purpose:
  208. // Input : *hash -
  209. // hashlen -
  210. // Output : char
  211. //-----------------------------------------------------------------------------
  212. char *MD5_Print( unsigned char *hash, int hashlen )
  213. {
  214. static char szReturn[64];
  215. Assert( hashlen <= 32 );
  216. Q_binarytohex( hash, hashlen, szReturn, sizeof( szReturn ) );
  217. return szReturn;
  218. }
  219. //-----------------------------------------------------------------------------
  220. // Purpose: generate pseudo random number from a seed number
  221. // Input : seed number
  222. // Output : pseudo random number
  223. //-----------------------------------------------------------------------------
  224. unsigned int MD5_PseudoRandom(unsigned int nSeed)
  225. {
  226. MD5Context_t ctx;
  227. unsigned char digest[MD5_DIGEST_LENGTH]; // The MD5 Hash
  228. memset( &ctx, 0, sizeof( ctx ) );
  229. MD5Init(&ctx);
  230. MD5Update(&ctx, (unsigned char*)&nSeed, sizeof(nSeed) );
  231. MD5Final(digest, &ctx);
  232. return *(unsigned int*)(digest+6); // use 4 middle bytes for random value
  233. }
  234. //-----------------------------------------------------------------------------
  235. bool MD5_Compare( const MD5Value_t &data, const MD5Value_t &compare )
  236. {
  237. return V_memcmp( data.bits, compare.bits, MD5_DIGEST_LENGTH ) == 0;
  238. }
  239. //-----------------------------------------------------------------------------
  240. void MD5Value_t::Zero()
  241. {
  242. V_memset( bits, 0, sizeof( bits ) );
  243. }
  244. //-----------------------------------------------------------------------------
  245. bool MD5Value_t::IsZero() const
  246. {
  247. for ( int i = 0 ; i < Q_ARRAYSIZE( bits ) ; ++i )
  248. {
  249. if ( bits[i] != 0 )
  250. return false;
  251. }
  252. return true;
  253. }
  254. //-----------------------------------------------------------------------------
  255. void MD5_ProcessSingleBuffer( const void *p, int len, MD5Value_t &md5Result )
  256. {
  257. Assert( len >= 0 );
  258. MD5Context_t ctx;
  259. MD5Init( &ctx );
  260. MD5Update( &ctx, (unsigned char const *)p, len );
  261. MD5Final( md5Result.bits, &ctx );
  262. }