DR_SHA1.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. 100% free public domain implementation of the SHA-1 algorithm
  3. by Dominik Reichl <dominik.reichl@t-online.de>
  4. Web: http://www.dominik-reichl.de/
  5. See header file for version history and test vectors.
  6. */
  7. // If compiling with MFC, you might want to add #include "StdAfx.h"
  8. #define _CRT_SECURE_NO_WARNINGS
  9. #include "DR_SHA1.h"
  10. #include <stdlib.h>
  11. #define SHA1_MAX_FILE_BUFFER (32 * 20 * 820)
  12. // Rotate p_val32 by p_nBits bits to the left
  13. #ifndef ROL32
  14. #ifdef _MSC_VER
  15. #define ROL32(p_val32,p_nBits) _rotl(p_val32,p_nBits)
  16. #else
  17. #define ROL32(p_val32,p_nBits) (((p_val32)<<(p_nBits))|((p_val32)>>(32-(p_nBits))))
  18. #endif
  19. #endif
  20. #ifdef SHA1_LITTLE_ENDIAN
  21. #define SHABLK0(i) (m_block->l[i] = \
  22. (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
  23. #else
  24. #define SHABLK0(i) (m_block->l[i])
  25. #endif
  26. #define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ \
  27. m_block->l[(i+8)&15] ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
  28. // SHA-1 rounds
  29. #define S_R0(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
  30. #define S_R1(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
  31. #define S_R2(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5);w=ROL32(w,30);}
  32. #define S_R3(v,w,x,y,z,i) {z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5);w=ROL32(w,30);}
  33. #define S_R4(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5);w=ROL32(w,30);}
  34. #pragma warning(push)
  35. // Disable compiler warning 'Conditional expression is constant'
  36. #pragma warning(disable: 4127)
  37. CSHA1::CSHA1()
  38. {
  39. m_block = (SHA1_WORKSPACE_BLOCK*)m_workspace;
  40. Reset();
  41. }
  42. #ifdef SHA1_WIPE_VARIABLES
  43. CSHA1::~CSHA1()
  44. {
  45. Reset();
  46. }
  47. #endif
  48. void CSHA1::Reset()
  49. {
  50. // SHA1 initialization constants
  51. m_state[0] = 0x67452301;
  52. m_state[1] = 0xEFCDAB89;
  53. m_state[2] = 0x98BADCFE;
  54. m_state[3] = 0x10325476;
  55. m_state[4] = 0xC3D2E1F0;
  56. m_count[0] = 0;
  57. m_count[1] = 0;
  58. }
  59. void CSHA1::Transform(UINT_32* pState, const UINT_8* pBuffer)
  60. {
  61. UINT_32 a = pState[0], b = pState[1], c = pState[2], d = pState[3], e = pState[4];
  62. memcpy(m_block, pBuffer, 64);
  63. // 4 rounds of 20 operations each, loop unrolled
  64. S_R0(a,b,c,d,e, 0); S_R0(e,a,b,c,d, 1); S_R0(d,e,a,b,c, 2); S_R0(c,d,e,a,b, 3);
  65. S_R0(b,c,d,e,a, 4); S_R0(a,b,c,d,e, 5); S_R0(e,a,b,c,d, 6); S_R0(d,e,a,b,c, 7);
  66. S_R0(c,d,e,a,b, 8); S_R0(b,c,d,e,a, 9); S_R0(a,b,c,d,e,10); S_R0(e,a,b,c,d,11);
  67. S_R0(d,e,a,b,c,12); S_R0(c,d,e,a,b,13); S_R0(b,c,d,e,a,14); S_R0(a,b,c,d,e,15);
  68. S_R1(e,a,b,c,d,16); S_R1(d,e,a,b,c,17); S_R1(c,d,e,a,b,18); S_R1(b,c,d,e,a,19);
  69. S_R2(a,b,c,d,e,20); S_R2(e,a,b,c,d,21); S_R2(d,e,a,b,c,22); S_R2(c,d,e,a,b,23);
  70. S_R2(b,c,d,e,a,24); S_R2(a,b,c,d,e,25); S_R2(e,a,b,c,d,26); S_R2(d,e,a,b,c,27);
  71. S_R2(c,d,e,a,b,28); S_R2(b,c,d,e,a,29); S_R2(a,b,c,d,e,30); S_R2(e,a,b,c,d,31);
  72. S_R2(d,e,a,b,c,32); S_R2(c,d,e,a,b,33); S_R2(b,c,d,e,a,34); S_R2(a,b,c,d,e,35);
  73. S_R2(e,a,b,c,d,36); S_R2(d,e,a,b,c,37); S_R2(c,d,e,a,b,38); S_R2(b,c,d,e,a,39);
  74. S_R3(a,b,c,d,e,40); S_R3(e,a,b,c,d,41); S_R3(d,e,a,b,c,42); S_R3(c,d,e,a,b,43);
  75. S_R3(b,c,d,e,a,44); S_R3(a,b,c,d,e,45); S_R3(e,a,b,c,d,46); S_R3(d,e,a,b,c,47);
  76. S_R3(c,d,e,a,b,48); S_R3(b,c,d,e,a,49); S_R3(a,b,c,d,e,50); S_R3(e,a,b,c,d,51);
  77. S_R3(d,e,a,b,c,52); S_R3(c,d,e,a,b,53); S_R3(b,c,d,e,a,54); S_R3(a,b,c,d,e,55);
  78. S_R3(e,a,b,c,d,56); S_R3(d,e,a,b,c,57); S_R3(c,d,e,a,b,58); S_R3(b,c,d,e,a,59);
  79. S_R4(a,b,c,d,e,60); S_R4(e,a,b,c,d,61); S_R4(d,e,a,b,c,62); S_R4(c,d,e,a,b,63);
  80. S_R4(b,c,d,e,a,64); S_R4(a,b,c,d,e,65); S_R4(e,a,b,c,d,66); S_R4(d,e,a,b,c,67);
  81. S_R4(c,d,e,a,b,68); S_R4(b,c,d,e,a,69); S_R4(a,b,c,d,e,70); S_R4(e,a,b,c,d,71);
  82. S_R4(d,e,a,b,c,72); S_R4(c,d,e,a,b,73); S_R4(b,c,d,e,a,74); S_R4(a,b,c,d,e,75);
  83. S_R4(e,a,b,c,d,76); S_R4(d,e,a,b,c,77); S_R4(c,d,e,a,b,78); S_R4(b,c,d,e,a,79);
  84. // Add the working vars back into state
  85. pState[0] += a;
  86. pState[1] += b;
  87. pState[2] += c;
  88. pState[3] += d;
  89. pState[4] += e;
  90. // Wipe variables
  91. #ifdef SHA1_WIPE_VARIABLES
  92. a = b = c = d = e = 0;
  93. #endif
  94. }
  95. void CSHA1::Update(const UINT_8* pbData, UINT_32 uLen)
  96. {
  97. UINT_32 j = ((m_count[0] >> 3) & 0x3F);
  98. if((m_count[0] += (uLen << 3)) < (uLen << 3))
  99. ++m_count[1]; // Overflow
  100. m_count[1] += (uLen >> 29);
  101. UINT_32 i;
  102. if((j + uLen) > 63)
  103. {
  104. i = 64 - j;
  105. memcpy(&m_buffer[j], pbData, i);
  106. Transform(m_state, m_buffer);
  107. for( ; (i + 63) < uLen; i += 64)
  108. Transform(m_state, &pbData[i]);
  109. j = 0;
  110. }
  111. else i = 0;
  112. if((uLen - i) != 0)
  113. memcpy(&m_buffer[j], &pbData[i], uLen - i);
  114. }
  115. #ifdef SHA1_UTILITY_FUNCTIONS
  116. bool CSHA1::HashFile(const TCHAR* tszFileName)
  117. {
  118. if(tszFileName == NULL) return false;
  119. FILE* fpIn = _tfopen(tszFileName, _T("rb"));
  120. if(fpIn == NULL) return false;
  121. UINT_8* pbData = new UINT_8[SHA1_MAX_FILE_BUFFER];
  122. if(pbData == NULL) { fclose(fpIn); return false; }
  123. bool bSuccess = true;
  124. while(true)
  125. {
  126. const size_t uRead = fread(pbData, 1, SHA1_MAX_FILE_BUFFER, fpIn);
  127. if(uRead > 0)
  128. Update(pbData, static_cast<UINT_32>(uRead));
  129. if(uRead < SHA1_MAX_FILE_BUFFER)
  130. {
  131. if(feof(fpIn) == 0) bSuccess = false;
  132. break;
  133. }
  134. }
  135. fclose(fpIn);
  136. delete[] pbData;
  137. return bSuccess;
  138. }
  139. #endif
  140. void CSHA1::Final()
  141. {
  142. UINT_32 i;
  143. UINT_8 pbFinalCount[8];
  144. for(i = 0; i < 8; ++i)
  145. pbFinalCount[i] = static_cast<UINT_8>((m_count[((i >= 4) ? 0 : 1)] >>
  146. ((3 - (i & 3)) * 8) ) & 0xFF); // Endian independent
  147. Update((UINT_8*)"\200", 1);
  148. while((m_count[0] & 504) != 448)
  149. Update((UINT_8*)"\0", 1);
  150. Update(pbFinalCount, 8); // Cause a Transform()
  151. for(i = 0; i < 20; ++i)
  152. m_digest[i] = static_cast<UINT_8>((m_state[i >> 2] >> ((3 -
  153. (i & 3)) * 8)) & 0xFF);
  154. // Wipe variables for security reasons
  155. #ifdef SHA1_WIPE_VARIABLES
  156. memset(m_buffer, 0, 64);
  157. memset(m_state, 0, 20);
  158. memset(m_count, 0, 8);
  159. memset(pbFinalCount, 0, 8);
  160. Transform(m_state, m_buffer);
  161. #endif
  162. }
  163. #ifdef SHA1_UTILITY_FUNCTIONS
  164. bool CSHA1::ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType) const
  165. {
  166. if(tszReport == NULL) return false;
  167. TCHAR tszTemp[16];
  168. if((rtReportType == REPORT_HEX) || (rtReportType == REPORT_HEX_SHORT))
  169. {
  170. _sntprintf(tszTemp, 15, _T("%02X"), m_digest[0]);
  171. _tcscpy(tszReport, tszTemp);
  172. const TCHAR* lpFmt = ((rtReportType == REPORT_HEX) ? _T(" %02X") : _T("%02X"));
  173. for(size_t i = 1; i < 20; ++i)
  174. {
  175. _sntprintf(tszTemp, 15, lpFmt, m_digest[i]);
  176. _tcscat(tszReport, tszTemp);
  177. }
  178. }
  179. else if(rtReportType == REPORT_DIGIT)
  180. {
  181. _sntprintf(tszTemp, 15, _T("%u"), m_digest[0]);
  182. _tcscpy(tszReport, tszTemp);
  183. for(size_t i = 1; i < 20; ++i)
  184. {
  185. _sntprintf(tszTemp, 15, _T(" %u"), m_digest[i]);
  186. _tcscat(tszReport, tszTemp);
  187. }
  188. }
  189. else return false;
  190. return true;
  191. }
  192. #endif
  193. #ifdef SHA1_STL_FUNCTIONS
  194. bool CSHA1::ReportHashStl(std::basic_string<TCHAR>& strOut, REPORT_TYPE rtReportType) const
  195. {
  196. TCHAR tszOut[84];
  197. const bool bResult = ReportHash(tszOut, rtReportType);
  198. if(bResult) strOut = tszOut;
  199. return bResult;
  200. }
  201. #endif
  202. bool CSHA1::GetHash(UINT_8* pbDest20) const
  203. {
  204. if(pbDest20 == NULL) return false;
  205. memcpy(pbDest20, m_digest, 20);
  206. return true;
  207. }
  208. // Get the raw message digest
  209. // Added by Kevin to be quicker
  210. unsigned char * CSHA1::GetHash( void ) const
  211. {
  212. return ( unsigned char * ) m_digest;
  213. }
  214. // http://cseweb.ucsd.edu/~mihir/papers/hmac-cb.pdf
  215. // Sample code: http://www.opensource.apple.com/source/freeradius/freeradius-11/freeradius/src/lib/hmac.c
  216. void CSHA1::HMAC(unsigned char *sharedKey, int sharedKeyLength, unsigned char *data, int dataLength, unsigned char output[SHA1_LENGTH])
  217. {
  218. // 1. Append zeros to the end of K to create a 64 byte string
  219. static const int sha1BlockLength=64;
  220. if (sharedKeyLength > sha1BlockLength)
  221. sharedKeyLength = sha1BlockLength;
  222. // ipad = the byte 0x36 repeated 64 times
  223. // opad = the byte 0x5C repeated 64 times
  224. unsigned char keyWithIpad[sha1BlockLength];
  225. unsigned char keyWithOpad[sha1BlockLength];
  226. memset( keyWithIpad, 0, sizeof(keyWithIpad));
  227. memset( keyWithOpad, 0, sizeof(keyWithOpad));
  228. memcpy( keyWithIpad, sharedKey, sharedKeyLength);
  229. memcpy( keyWithOpad, sharedKey, sharedKeyLength);
  230. for (int i = 0; i < sha1BlockLength; i++) {
  231. keyWithIpad[i] ^= 0x36;
  232. keyWithOpad[i] ^= 0x5c;
  233. }
  234. // 3. Append the data stream Text to the 64 byte string resulting from step (2)
  235. // 4. Apply H to the stream generated in step (3)
  236. CSHA1 firstHash;
  237. firstHash.Reset();
  238. firstHash.Update( keyWithIpad, sha1BlockLength );
  239. firstHash.Update( data, dataLength );
  240. firstHash.Final();
  241. // 6. Append the H (hash) result from step (4) to the 64 byte string resulting from step (5)
  242. // 7. Apply H to the stream generated in step (6) and output the result
  243. CSHA1 secondHash;
  244. secondHash.Reset();
  245. secondHash.Update( keyWithOpad, sha1BlockLength );
  246. secondHash.Update( firstHash.GetHash(), SHA1_LENGTH );
  247. secondHash.Final();
  248. memcpy(output, secondHash.GetHash(), SHA1_LENGTH);
  249. // char report[128];
  250. // memset(report,0,128);
  251. // secondHash.ReportHash( report, 0 );
  252. }
  253. #pragma warning(pop)
粤ICP备19079148号