OverSim
SHA1.cc
Go to the documentation of this file.
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 
6  See header file for version history and test vectors.
7 */
8 
9 // If compiling with MFC, you might want to add #include "StdAfx.h"
10 
11 #define _CRT_SECURE_NO_WARNINGS
12 #include "SHA1.h"
13 
14 #define SHA1_MAX_FILE_BUFFER (32 * 20 * 820)
15 
16 // Rotate p_val32 by p_nBits bits to the left
17 #ifndef ROL32
18 #ifdef _MSC_VER
19 #define ROL32(p_val32,p_nBits) _rotl(p_val32,p_nBits)
20 #else
21 #define ROL32(p_val32,p_nBits) (((p_val32)<<(p_nBits))|((p_val32)>>(32-(p_nBits))))
22 #endif
23 #endif
24 
25 #ifdef SHA1_LITTLE_ENDIAN
26 #define SHABLK0(i) (m_block->l[i] = \
27  (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
28 #else
29 #define SHABLK0(i) (m_block->l[i])
30 #endif
31 
32 #define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ \
33  m_block->l[(i+8)&15] ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
34 
35 // SHA-1 rounds
36 #define S_R0(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
37 #define S_R1(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
38 #define S_R2(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5);w=ROL32(w,30);}
39 #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);}
40 #define S_R4(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5);w=ROL32(w,30);}
41 
42 #pragma warning(push)
43 // Disable compiler warning 'Conditional expression is constant'
44 #pragma warning(disable: 4127)
45 
47 {
49 
50  Reset();
51 }
52 
53 #ifdef SHA1_WIPE_VARIABLES
55 {
56  Reset();
57 }
58 #endif
59 
61 {
62  // SHA1 initialization constants
63  m_state[0] = 0x67452301;
64  m_state[1] = 0xEFCDAB89;
65  m_state[2] = 0x98BADCFE;
66  m_state[3] = 0x10325476;
67  m_state[4] = 0xC3D2E1F0;
68 
69  m_count[0] = 0;
70  m_count[1] = 0;
71 }
72 
73 void CSHA1::Transform(UINT_32* pState, const UINT_8* pBuffer)
74 {
75  UINT_32 a = pState[0], b = pState[1], c = pState[2], d = pState[3], e = pState[4];
76 
77  memcpy(m_block, pBuffer, 64);
78 
79  // 4 rounds of 20 operations each, loop unrolled
80  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);
81  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);
82  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);
83  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);
84  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);
85  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);
86  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);
87  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);
88  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);
89  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);
90  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);
91  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);
92  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);
93  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);
94  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);
95  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);
96  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);
97  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);
98  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);
99  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);
100 
101  // Add the working vars back into state
102  pState[0] += a;
103  pState[1] += b;
104  pState[2] += c;
105  pState[3] += d;
106  pState[4] += e;
107 
108  // Wipe variables
109 #ifdef SHA1_WIPE_VARIABLES
110  a = b = c = d = e = 0;
111 #endif
112 }
113 
114 void CSHA1::Update(const UINT_8* pbData, UINT_32 uLen)
115 {
116  UINT_32 j = ((m_count[0] >> 3) & 0x3F);
117 
118  if((m_count[0] += (uLen << 3)) < (uLen << 3))
119  ++m_count[1]; // Overflow
120 
121  m_count[1] += (uLen >> 29);
122 
123  UINT_32 i;
124  if((j + uLen) > 63)
125  {
126  i = 64 - j;
127  memcpy(&m_buffer[j], pbData, i);
129 
130  for( ; (i + 63) < uLen; i += 64)
131  Transform(m_state, &pbData[i]);
132 
133  j = 0;
134  }
135  else i = 0;
136 
137  if((uLen - i) != 0)
138  memcpy(&m_buffer[j], &pbData[i], uLen - i);
139 }
140 
141 #ifdef SHA1_UTILITY_FUNCTIONS
142 bool CSHA1::HashFile(const TCHAR* tszFileName)
143 {
144  if(tszFileName == NULL) return false;
145 
146  FILE* fpIn = _tfopen(tszFileName, _T("rb"));
147  if(fpIn == NULL) return false;
148 
149  UINT_8* pbData = new UINT_8[SHA1_MAX_FILE_BUFFER];
150  if(pbData == NULL) { fclose(fpIn); return false; }
151 
152  bool bSuccess = true;
153  while(true)
154  {
155  const size_t uRead = fread(pbData, 1, SHA1_MAX_FILE_BUFFER, fpIn);
156 
157  if(uRead > 0)
158  Update(pbData, static_cast<UINT_32>(uRead));
159 
160  if(uRead < SHA1_MAX_FILE_BUFFER)
161  {
162  if(feof(fpIn) == 0) bSuccess = false;
163  break;
164  }
165  }
166 
167  fclose(fpIn);
168  delete[] pbData;
169  return bSuccess;
170 }
171 #endif
172 
174 {
175  UINT_32 i;
176 
177  UINT_8 pbFinalCount[8];
178  for(i = 0; i < 8; ++i)
179  pbFinalCount[i] = static_cast<UINT_8>((m_count[((i >= 4) ? 0 : 1)] >>
180  ((3 - (i & 3)) * 8) ) & 0xFF); // Endian independent
181 
182  Update((UINT_8*)"\200", 1);
183 
184  while((m_count[0] & 504) != 448)
185  Update((UINT_8*)"\0", 1);
186 
187  Update(pbFinalCount, 8); // Cause a Transform()
188 
189  for(i = 0; i < 20; ++i)
190  m_digest[i] = static_cast<UINT_8>((m_state[i >> 2] >> ((3 -
191  (i & 3)) * 8)) & 0xFF);
192 
193  // Wipe variables for security reasons
194 #ifdef SHA1_WIPE_VARIABLES
195  memset(m_buffer, 0, 64);
196  memset(m_state, 0, 20);
197  memset(m_count, 0, 8);
198  memset(pbFinalCount, 0, 8);
200 #endif
201 }
202 
203 #ifdef SHA1_UTILITY_FUNCTIONS
204 bool CSHA1::ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType) const
205 {
206  if(tszReport == NULL) return false;
207 
208  TCHAR tszTemp[16];
209 
210  if((rtReportType == REPORT_HEX) || (rtReportType == REPORT_HEX_SHORT))
211  {
212  _sntprintf(tszTemp, 15, _T("%02X"), m_digest[0]);
213  _tcscpy(tszReport, tszTemp);
214 
215  const TCHAR* lpFmt = ((rtReportType == REPORT_HEX) ? _T(" %02X") : _T("%02X"));
216  for(size_t i = 1; i < 20; ++i)
217  {
218  _sntprintf(tszTemp, 15, lpFmt, m_digest[i]);
219  _tcscat(tszReport, tszTemp);
220  }
221  }
222  else if(rtReportType == REPORT_DIGIT)
223  {
224  _sntprintf(tszTemp, 15, _T("%u"), m_digest[0]);
225  _tcscpy(tszReport, tszTemp);
226 
227  for(size_t i = 1; i < 20; ++i)
228  {
229  _sntprintf(tszTemp, 15, _T(" %u"), m_digest[i]);
230  _tcscat(tszReport, tszTemp);
231  }
232  }
233  else return false;
234 
235  return true;
236 }
237 #endif
238 
239 #ifdef SHA1_STL_FUNCTIONS
240 bool CSHA1::ReportHashStl(std::basic_string<TCHAR>& strOut, REPORT_TYPE rtReportType) const
241 {
242  TCHAR tszOut[84];
243  const bool bResult = ReportHash(tszOut, rtReportType);
244  if(bResult) strOut = tszOut;
245  return bResult;
246 }
247 #endif
248 
249 bool CSHA1::GetHash(UINT_8* pbDest20) const
250 {
251  if(pbDest20 == NULL) return false;
252  memcpy(pbDest20, m_digest, 20);
253  return true;
254 }
255 
256 #pragma warning(pop)