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.

300 lines
7.7 KiB

  1. //========= Copyright � 2005, Valve Inc, All rights reserved. ==========
  2. //
  3. // Purpose: Implementation of SHA-1
  4. //
  5. //=============================================================================
  6. /*
  7. 100% free public domain implementation of the SHA-1
  8. algorithm by Dominik Reichl <dominik.reichl@t-online.de>
  9. === Test Vectors (from FIPS PUB 180-1) ===
  10. SHA1("abc") =
  11. A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  12. SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
  13. 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  14. SHA1(A million repetitions of "a") =
  15. 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  16. */
  17. #if !defined(_MINIMUM_BUILD_)
  18. #include "checksum_sha1.h"
  19. #else
  20. //
  21. // This path is build in the CEG/DRM projects where we require that no CRT references are made !
  22. //
  23. #include <intrin.h> // memcpy, memset etc... will be inlined.
  24. #include "tier1/checksum_sha1.h"
  25. #endif
  26. #define MAX_FILE_READ_BUFFER 8000
  27. // Rotate x bits to the left
  28. #ifndef ROL32
  29. #define ROL32(_val32, _nBits) (((_val32)<<(_nBits))|((_val32)>>(32-(_nBits))))
  30. #endif
  31. #ifdef SHA1_LITTLE_ENDIAN
  32. #define SHABLK0(i) (m_block->l[i] = \
  33. (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
  34. #else
  35. #define SHABLK0(i) (m_block->l[i])
  36. #endif
  37. #define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \
  38. ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
  39. // SHA-1 rounds
  40. #define _R0(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
  41. #define _R1(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
  42. #define _R2(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5); w=ROL32(w,30); }
  43. #define _R3(v,w,x,y,z,i) { z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5); w=ROL32(w,30); }
  44. #define _R4(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5); w=ROL32(w,30); }
  45. #ifdef _MINIMUM_BUILD_
  46. Minimum_CSHA1::Minimum_CSHA1()
  47. #else
  48. CSHA1::CSHA1()
  49. #endif
  50. {
  51. m_block = (SHA1_WORKSPACE_BLOCK *)m_workspace;
  52. Reset();
  53. }
  54. #ifdef _MINIMUM_BUILD_
  55. Minimum_CSHA1::~Minimum_CSHA1()
  56. #else
  57. CSHA1::~CSHA1()
  58. #endif
  59. {
  60. // Reset();
  61. }
  62. #ifdef _MINIMUM_BUILD_
  63. void Minimum_CSHA1::Reset()
  64. #else
  65. void CSHA1::Reset()
  66. #endif
  67. {
  68. // SHA1 initialization constants
  69. m_state[0] = 0x67452301;
  70. m_state[1] = 0xEFCDAB89;
  71. m_state[2] = 0x98BADCFE;
  72. m_state[3] = 0x10325476;
  73. m_state[4] = 0xC3D2E1F0;
  74. m_count[0] = 0;
  75. m_count[1] = 0;
  76. }
  77. #ifdef _MINIMUM_BUILD_
  78. void Minimum_CSHA1::Transform(uint32 state[5], const uint8 buffer[64])
  79. #else
  80. void CSHA1::Transform(uint32 state[5], const uint8 buffer[64])
  81. #endif
  82. {
  83. uint32 a = 0, b = 0, c = 0, d = 0, e = 0;
  84. memcpy(m_block, buffer, 64);
  85. // Copy state[] to working vars
  86. a = state[0];
  87. b = state[1];
  88. c = state[2];
  89. d = state[3];
  90. e = state[4];
  91. // 4 rounds of 20 operations each. Loop unrolled.
  92. _R0(a,b,c,d,e, 0); _R0(e,a,b,c,d, 1); _R0(d,e,a,b,c, 2); _R0(c,d,e,a,b, 3);
  93. _R0(b,c,d,e,a, 4); _R0(a,b,c,d,e, 5); _R0(e,a,b,c,d, 6); _R0(d,e,a,b,c, 7);
  94. _R0(c,d,e,a,b, 8); _R0(b,c,d,e,a, 9); _R0(a,b,c,d,e,10); _R0(e,a,b,c,d,11);
  95. _R0(d,e,a,b,c,12); _R0(c,d,e,a,b,13); _R0(b,c,d,e,a,14); _R0(a,b,c,d,e,15);
  96. _R1(e,a,b,c,d,16); _R1(d,e,a,b,c,17); _R1(c,d,e,a,b,18); _R1(b,c,d,e,a,19);
  97. _R2(a,b,c,d,e,20); _R2(e,a,b,c,d,21); _R2(d,e,a,b,c,22); _R2(c,d,e,a,b,23);
  98. _R2(b,c,d,e,a,24); _R2(a,b,c,d,e,25); _R2(e,a,b,c,d,26); _R2(d,e,a,b,c,27);
  99. _R2(c,d,e,a,b,28); _R2(b,c,d,e,a,29); _R2(a,b,c,d,e,30); _R2(e,a,b,c,d,31);
  100. _R2(d,e,a,b,c,32); _R2(c,d,e,a,b,33); _R2(b,c,d,e,a,34); _R2(a,b,c,d,e,35);
  101. _R2(e,a,b,c,d,36); _R2(d,e,a,b,c,37); _R2(c,d,e,a,b,38); _R2(b,c,d,e,a,39);
  102. _R3(a,b,c,d,e,40); _R3(e,a,b,c,d,41); _R3(d,e,a,b,c,42); _R3(c,d,e,a,b,43);
  103. _R3(b,c,d,e,a,44); _R3(a,b,c,d,e,45); _R3(e,a,b,c,d,46); _R3(d,e,a,b,c,47);
  104. _R3(c,d,e,a,b,48); _R3(b,c,d,e,a,49); _R3(a,b,c,d,e,50); _R3(e,a,b,c,d,51);
  105. _R3(d,e,a,b,c,52); _R3(c,d,e,a,b,53); _R3(b,c,d,e,a,54); _R3(a,b,c,d,e,55);
  106. _R3(e,a,b,c,d,56); _R3(d,e,a,b,c,57); _R3(c,d,e,a,b,58); _R3(b,c,d,e,a,59);
  107. _R4(a,b,c,d,e,60); _R4(e,a,b,c,d,61); _R4(d,e,a,b,c,62); _R4(c,d,e,a,b,63);
  108. _R4(b,c,d,e,a,64); _R4(a,b,c,d,e,65); _R4(e,a,b,c,d,66); _R4(d,e,a,b,c,67);
  109. _R4(c,d,e,a,b,68); _R4(b,c,d,e,a,69); _R4(a,b,c,d,e,70); _R4(e,a,b,c,d,71);
  110. _R4(d,e,a,b,c,72); _R4(c,d,e,a,b,73); _R4(b,c,d,e,a,74); _R4(a,b,c,d,e,75);
  111. _R4(e,a,b,c,d,76); _R4(d,e,a,b,c,77); _R4(c,d,e,a,b,78); _R4(b,c,d,e,a,79);
  112. // Add the working vars back into state[]
  113. state[0] += a;
  114. state[1] += b;
  115. state[2] += c;
  116. state[3] += d;
  117. state[4] += e;
  118. // Wipe variables
  119. a = b = c = d = e = 0;
  120. }
  121. // Use this function to hash in binary data and strings
  122. #ifdef _MINIMUM_BUILD_
  123. void Minimum_CSHA1::Update( const void *pvData, unsigned int len )
  124. #else
  125. void CSHA1::Update( const void *pvData, unsigned int len)
  126. #endif
  127. {
  128. const uint8 *data = (const uint8 *)pvData;
  129. uint32 i = 0, j;
  130. j = (m_count[0] >> 3) & 63;
  131. if((m_count[0] += len << 3) < (len << 3)) m_count[1]++;
  132. m_count[1] += (len >> 29);
  133. if((j + len) > 63)
  134. {
  135. memcpy(&m_buffer[j], data, (i = 64 - j));
  136. Transform(m_state, m_buffer);
  137. for (; i+63 < len; i += 64)
  138. Transform(m_state, &data[i]);
  139. j = 0;
  140. }
  141. else i = 0;
  142. memcpy(&m_buffer[j], &data[i], len - i);
  143. }
  144. #if !defined(_MINIMUM_BUILD_)
  145. // Hash in file contents
  146. bool CSHA1::HashFile(const char *szFileName)
  147. {
  148. uint32 ulFileSize = 0, ulRest = 0, ulBlocks = 0;
  149. uint32 i = 0;
  150. uint8 uData[MAX_FILE_READ_BUFFER];
  151. FILE *fIn = NULL;
  152. if(szFileName == NULL) return(false);
  153. if((fIn = fopen(szFileName, "rb")) == NULL) return(false);
  154. fseek(fIn, 0, SEEK_END);
  155. ulFileSize = ftell(fIn);
  156. fseek(fIn, 0, SEEK_SET);
  157. ulRest = ulFileSize % MAX_FILE_READ_BUFFER;
  158. ulBlocks = ulFileSize / MAX_FILE_READ_BUFFER;
  159. for(i = 0; i < ulBlocks; i++)
  160. {
  161. fread(uData, 1, MAX_FILE_READ_BUFFER, fIn);
  162. Update(uData, MAX_FILE_READ_BUFFER);
  163. }
  164. if(ulRest != 0)
  165. {
  166. fread(uData, 1, ulRest, fIn);
  167. Update(uData, ulRest);
  168. }
  169. fclose(fIn);
  170. fIn = NULL;
  171. return(true);
  172. }
  173. #endif
  174. #ifdef _MINIMUM_BUILD_
  175. void Minimum_CSHA1::Final()
  176. #else
  177. void CSHA1::Final()
  178. #endif
  179. {
  180. uint32 i = 0;
  181. uint8 finalcount[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  182. for (i = 0; i < 8; i++)
  183. finalcount[i] = (uint8)((m_count[(i >= 4 ? 0 : 1)]
  184. >> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
  185. Update((uint8 *)"\200", 1);
  186. while ((m_count[0] & 504) != 448)
  187. Update((uint8 *)"\0", 1);
  188. Update(finalcount, 8); // Cause a SHA1Transform()
  189. for (i = 0; i < k_cubHash; i++)
  190. {
  191. m_digest[i] = (uint8)((m_state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255);
  192. }
  193. // Wipe variables for security reasons
  194. i = 0;
  195. memset(m_buffer, 0, sizeof(m_buffer) );
  196. memset(m_state, 0, sizeof(m_state) );
  197. memset(m_count, 0, sizeof(m_count) );
  198. memset(finalcount, 0, sizeof( finalcount) );
  199. Transform(m_state, m_buffer);
  200. }
  201. #if !defined(_MINIMUM_BUILD_)
  202. // Get the final hash as a pre-formatted string
  203. void CSHA1::ReportHash(char *szReport, uint8 uReportType)
  204. {
  205. uint8 i = 0;
  206. char szTemp[12];
  207. if(szReport == NULL) return;
  208. if(uReportType == REPORT_HEX)
  209. {
  210. sprintf(szTemp, "%02X", m_digest[0]);
  211. strcat(szReport, szTemp);
  212. for(i = 1; i < k_cubHash; i++)
  213. {
  214. sprintf(szTemp, " %02X", m_digest[i]);
  215. strcat(szReport, szTemp);
  216. }
  217. }
  218. else if(uReportType == REPORT_DIGIT)
  219. {
  220. sprintf(szTemp, "%u", m_digest[0]);
  221. strcat(szReport, szTemp);
  222. for(i = 1; i < k_cubHash; i++)
  223. {
  224. sprintf(szTemp, " %u", m_digest[i]);
  225. strcat(szReport, szTemp);
  226. }
  227. }
  228. else strcpy(szReport, "Error: Unknown report type!");
  229. }
  230. #endif // _MINIMUM_BUILD_
  231. // Get the raw message digest
  232. #ifdef _MINIMUM_BUILD_
  233. void Minimum_CSHA1::GetHash(uint8 *uDest)
  234. #else
  235. void CSHA1::GetHash(uint8 *uDest)
  236. #endif
  237. {
  238. memcpy(uDest, m_digest, k_cubHash);
  239. }
  240. #ifndef _MINIMUM_BUILD_
  241. // utility hash comparison function
  242. bool HashLessFunc( SHADigest_t const &lhs, SHADigest_t const &rhs )
  243. {
  244. int iRes = memcmp( &lhs, &rhs, sizeof( SHADigest_t ) );
  245. return ( iRes < 0 );
  246. }
  247. #endif