SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DR_SHA1.cpp
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 /*
10 * Modified work : Copyright(c) 2016-2017, SLikeSoft UG(haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications in this file are put under the public domain.
13 * Alternatively you are permitted to license the modifications under the MIT license, if you so desire. The
14 * license can be found in the license.txt file in the root directory of this source tree.
15 */
16
17 // If compiling with MFC, you might want to add #include "StdAfx.h"
18
19 #define _CRT_SECURE_NO_WARNINGS
20 #include "slikenet/DR_SHA1.h"
21 #include <stdlib.h>
22
23 #define SHA1_MAX_FILE_BUFFER (32 * 20 * 820)
24
25 // Rotate p_val32 by p_nBits bits to the left
26 #ifndef ROL32
27 #ifdef _MSC_VER
28 #define ROL32(p_val32,p_nBits) _rotl(p_val32,p_nBits)
29 #else
30 #define ROL32(p_val32,p_nBits) (((p_val32)<<(p_nBits))|((p_val32)>>(32-(p_nBits))))
31 #endif
32 #endif
33
34 #ifdef SHA1_LITTLE_ENDIAN
35 #define SHABLK0(i) (m_block->l[i] = \
36 (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
37 #else
38 #define SHABLK0(i) (m_block->l[i])
39 #endif
40
41 #define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ \
42 m_block->l[(i+8)&15] ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
43
44 // SHA-1 rounds
45 #define S_R0(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
46 #define S_R1(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
47 #define S_R2(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5);w=ROL32(w,30);}
48 #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);}
49 #define S_R4(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5);w=ROL32(w,30);}
50
51 CSHA1::CSHA1()
52 {
53 m_block = (SHA1_WORKSPACE_BLOCK*)m_workspace;
54
55 Reset();
56 }
57
58 #ifdef SHA1_WIPE_VARIABLES
59 CSHA1::~CSHA1()
60 {
61 Reset();
62 }
63 #endif
64
65 void CSHA1::Reset()
66 {
67 // SHA1 initialization constants
68 m_state[0] = 0x67452301;
69 m_state[1] = 0xEFCDAB89;
70 m_state[2] = 0x98BADCFE;
71 m_state[3] = 0x10325476;
72 m_state[4] = 0xC3D2E1F0;
73
74 m_count[0] = 0;
75 m_count[1] = 0;
76 }
77
78 void CSHA1::Transform(UINT_32* pState, const UINT_8* pBuffer)
79 {
80 UINT_32 a = pState[0], b = pState[1], c = pState[2], d = pState[3], e = pState[4];
81
82 memcpy(m_block, pBuffer, 64);
83
84 // 4 rounds of 20 operations each, loop unrolled
85 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);
86 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);
87 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);
88 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);
89 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);
90 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);
91 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);
92 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);
93 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);
94 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);
95 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);
96 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);
97 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);
98 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);
99 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);
100 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);
101 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);
102 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);
103 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);
104 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);
105
106 // Add the working vars back into state
107 pState[0] += a;
108 pState[1] += b;
109 pState[2] += c;
110 pState[3] += d;
111 pState[4] += e;
112
113 // Wipe variables
114 #ifdef SHA1_WIPE_VARIABLES
115 a = b = c = d = e = 0;
116 #endif
117 }
118
119 void CSHA1::Update(const UINT_8* pbData, UINT_32 uLen)
120 {
121 UINT_32 j = ((m_count[0] >> 3) & 0x3F);
122
123 if((m_count[0] += (uLen << 3)) < (uLen << 3))
124 ++m_count[1]; // Overflow
125
126 m_count[1] += (uLen >> 29);
127
128 UINT_32 i;
129 if((j + uLen) > 63)
130 {
131 i = 64 - j;
132 memcpy(&m_buffer[j], pbData, i);
133 Transform(m_state, m_buffer);
134
135 for( ; (i + 63) < uLen; i += 64)
136 Transform(m_state, &pbData[i]);
137
138 j = 0;
139 }
140 else i = 0;
141
142 if((uLen - i) != 0)
143 memcpy(&m_buffer[j], &pbData[i], uLen - i);
144 }
145
146 #ifdef SHA1_UTILITY_FUNCTIONS
147 bool CSHA1::HashFile(const TCHAR* tszFileName)
148 {
149 if(tszFileName == NULL) return false;
150
151 FILE* fpIn = _tfopen(tszFileName, _T("rb"));
152 if(fpIn == NULL) return false;
153
154 UINT_8* pbData = new UINT_8[SHA1_MAX_FILE_BUFFER];
155 if(pbData == NULL) { fclose(fpIn); return false; }
156
157 bool bSuccess = true;
158 for(;;)
159 {
160 const size_t uRead = fread(pbData, 1, SHA1_MAX_FILE_BUFFER, fpIn);
161
162 if(uRead > 0)
163 Update(pbData, static_cast<UINT_32>(uRead));
164
165 if(uRead < SHA1_MAX_FILE_BUFFER)
166 {
167 if(feof(fpIn) == 0) bSuccess = false;
168 break;
169 }
170 }
171
172 fclose(fpIn);
173 delete[] pbData;
174 return bSuccess;
175 }
176 #endif
177
178 void CSHA1::Final()
179 {
180 UINT_32 i;
181
182 UINT_8 pbFinalCount[8];
183 for(i = 0; i < 8; ++i)
184 pbFinalCount[i] = static_cast<UINT_8>((m_count[((i >= 4) ? 0 : 1)] >>
185 ((3 - (i & 3)) * 8) ) & 0xFF); // Endian independent
186
187 Update((UINT_8*)"\200", 1);
188
189 while((m_count[0] & 504) != 448)
190 Update((UINT_8*)"\0", 1);
191
192 Update(pbFinalCount, 8); // Cause a Transform()
193
194 for(i = 0; i < 20; ++i)
195 m_digest[i] = static_cast<UINT_8>((m_state[i >> 2] >> ((3 -
196 (i & 3)) * 8)) & 0xFF);
197
198 // Wipe variables for security reasons
199 #ifdef SHA1_WIPE_VARIABLES
200 memset(m_buffer, 0, 64);
201 memset(m_state, 0, 20);
202 memset(m_count, 0, 8);
203 memset(pbFinalCount, 0, 8);
204 Transform(m_state, m_buffer);
205 #endif
206 }
207
208 #ifdef SHA1_UTILITY_FUNCTIONS
209 bool CSHA1::ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType) const
210 {
211 if(tszReport == NULL) return false;
212
213 TCHAR tszTemp[16];
214
215 if((rtReportType == REPORT_HEX) || (rtReportType == REPORT_HEX_SHORT))
216 {
217 _sntprintf(tszTemp, 15, _T("%02X"), m_digest[0]);
218 _tcscpy(tszReport, tszTemp);
219
220 const TCHAR* lpFmt = ((rtReportType == REPORT_HEX) ? _T(" %02X") : _T("%02X"));
221 for(size_t i = 1; i < 20; ++i)
222 {
223 _sntprintf(tszTemp, 15, lpFmt, m_digest[i]);
224 _tcscat(tszReport, tszTemp);
225 }
226 }
227 else if(rtReportType == REPORT_DIGIT)
228 {
229 _sntprintf(tszTemp, 15, _T("%u"), m_digest[0]);
230 _tcscpy(tszReport, tszTemp);
231
232 for(size_t i = 1; i < 20; ++i)
233 {
234 _sntprintf(tszTemp, 15, _T(" %u"), m_digest[i]);
235 _tcscat(tszReport, tszTemp);
236 }
237 }
238 else return false;
239
240 return true;
241 }
242 #endif
243
244 #ifdef SHA1_STL_FUNCTIONS
245 bool CSHA1::ReportHashStl(std::basic_string<TCHAR>& strOut, REPORT_TYPE rtReportType) const
246 {
247 TCHAR tszOut[84];
248 const bool bResult = ReportHash(tszOut, rtReportType);
249 if(bResult) strOut = tszOut;
250 return bResult;
251 }
252 #endif
253
254 bool CSHA1::GetHash(UINT_8* pbDest20) const
255 {
256 if(pbDest20 == NULL) return false;
257 memcpy(pbDest20, m_digest, 20);
258 return true;
259 }
260
261 // Get the raw message digest
262 // Added by Kevin to be quicker
263 unsigned char * CSHA1::GetHash( void ) const
264 {
265 return ( unsigned char * ) m_digest;
266 }
267
268 // http://cseweb.ucsd.edu/~mihir/papers/hmac-cb.pdf
269 // Sample code: http://www.opensource.apple.com/source/freeradius/freeradius-11/freeradius/src/lib/hmac.c
270 void CSHA1::HMAC(unsigned char *sharedKey, int sharedKeyLength, unsigned char *data, int dataLength, unsigned char output[SHA1_LENGTH])
271 {
272 // 1. Append zeros to the end of K to create a 64 byte string
273 static const int sha1BlockLength=64;
274
275 if (sharedKeyLength > sha1BlockLength)
276 sharedKeyLength = sha1BlockLength;
277
278 // ipad = the byte 0x36 repeated 64 times
279 // opad = the byte 0x5C repeated 64 times
280 unsigned char keyWithIpad[sha1BlockLength];
281 unsigned char keyWithOpad[sha1BlockLength];
282
283 memset( keyWithIpad, 0, sizeof(keyWithIpad));
284 memset( keyWithOpad, 0, sizeof(keyWithOpad));
285 memcpy( keyWithIpad, sharedKey, sharedKeyLength);
286 memcpy( keyWithOpad, sharedKey, sharedKeyLength);
287
288 for (int i = 0; i < sha1BlockLength; i++) {
289 keyWithIpad[i] ^= 0x36;
290 keyWithOpad[i] ^= 0x5c;
291 }
292
293 // 3. Append the data stream Text to the 64 byte string resulting from step (2)
294 // 4. Apply H to the stream generated in step (3)
295 CSHA1 firstHash;
296 firstHash.Reset();
297 firstHash.Update( keyWithIpad, sha1BlockLength );
298 firstHash.Update( data, dataLength );
299 firstHash.Final();
300
301 // 6. Append the H (hash) result from step (4) to the 64 byte string resulting from step (5)
302 // 7. Apply H to the stream generated in step (6) and output the result
303 CSHA1 secondHash;
304 secondHash.Reset();
305 secondHash.Update( keyWithOpad, sha1BlockLength );
306 secondHash.Update( firstHash.GetHash(), SHA1_LENGTH );
307 secondHash.Final();
308
309 memcpy(output, secondHash.GetHash(), SHA1_LENGTH);
310
311 // char report[128];
312 // memset(report,0,128);
313 // secondHash.ReportHash( report, 0 );
314 }