Counter Strike : Global Offensive Source Code
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.

325 lines
11 KiB

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