]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/sha1.cpp
test
[freerainbowtables] / Client Applications / rcracki_mt / sha1.cpp
1 /*
2  * rcracki_mt is a multithreaded implementation and fork of the original 
3  * RainbowCrack
4  *
5  * Copyright Martin Westergaard Jørgensen <martinwj2005@gmail.com>
6  * Copyright Wei Dai <weidai@eskimo.com>
7  * Copyright 2009, 2010 Daniël Niggebrugge <niggebrugge@fox-it.com>
8  * Copyright 2009, 2010 James Nobis <frt@quelrod.net>
9  *
10  * This file is part of rcracki_mt.
11  *
12  * rcracki_mt is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * rcracki_mt is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with rcracki_mt.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26 //#include <stdio.h>
27 #if defined(_WIN32)
28         #include <windows.h>
29 #endif
30
31 #include <string.h>
32
33 #include "sha1.h"
34
35 #define SHA1CircularShift(bits,word) (((word) << (bits)) | ((word) >> (32-(bits))))
36
37 // this rotate isn't faster with me
38 #if defined(_WIN32)
39         #define ROTATE(a,n)     _lrotl(a,n)
40 #else
41         #define ROTATE(a,n)     (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
42 #endif
43
44 /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
45 #if defined(_WIN32)
46 /* 5 instructions with rotate instruction, else 9 */
47 #define Endian_Reverse32(a) \
48         { \
49         unsigned long l=(a); \
50         (a)=((ROTATE(l,8)&0x00FF00FF)|(ROTATE(l,24)&0xFF00FF00)); \
51         }
52 #else
53 /* 6 instructions with rotate instruction, else 8 */
54 #define Endian_Reverse32(a) \
55         { \
56         unsigned long l=(a); \
57         l=(((l&0xFF00FF00)>>8L)|((l&0x00FF00FF)<<8L)); \
58         (a)=ROTATE(l,16L); \
59         }
60 #endif
61
62 #define F_00_19(b,c,d)  ((((c) ^ (d)) & (b)) ^ (d)) 
63 #define F_20_39(b,c,d)  ((b) ^ (c) ^ (d))
64 #define F_40_59(b,c,d)  (((b) & (c)) | (((b)|(c)) & (d))) 
65 #define F_60_79(b,c,d)  F_20_39(b,c,d)
66
67 #define K0 0x5A827999
68 #define K1 0x6ED9EBA1
69 #define K2 0x8F1BBCDC
70 #define K3 0xCA62C1D6
71
72 #define H0 0x67452301
73 #define H1 0xEFCDAB89
74 #define H2 0x98BADCFE
75 #define H3 0x10325476
76 #define H4 0xC3D2E1F0
77
78 #define SHA1HashSize 20
79
80 void SHA1_NEW( unsigned char * pData, int length, unsigned char * pDigest)
81 {
82         if (length > 16)
83                 return;
84
85         UINT4 Message_Block_Index    = 0;
86
87         union
88         {
89                 unsigned char Message_Block[64];
90                 UINT4 Message_Block_W[16];
91         };
92
93         Message_Block_W[0] = 0x00000000;
94         Message_Block_W[1] = 0x00000000;
95         Message_Block_W[2] = 0x00000000;
96         Message_Block_W[3] = 0x00000000;
97         Message_Block_W[4] = 0x00000000;
98
99         UINT4 Intermediate_Hash[5] = { H0, H1, H2, H3, H4 };
100         
101         memcpy(Message_Block, pData, length);
102         Message_Block_Index += length;
103
104         //padMessage
105         Message_Block[length] = 0x80;
106         
107         UINT4 W_15 = length << 3;
108
109         int           t;                 /* Loop counter                */
110         UINT4      temp;              /* Temporary word value        */
111         UINT4      W[80];             /* Word sequence               */
112         UINT4      A, B, C, D, E;     /* Word buffers                */
113
114     /*
115      *  Initialize the first 16 words in the array W
116      */
117
118         #define INIT(x) W[x] = Message_Block_W[x];
119         
120         #define INIT_NULL(x) W[x] = 0;
121
122         
123         Endian_Reverse32(Message_Block_W[0]);
124         INIT(0);
125
126         #define INIT_NULL_1_14 \
127                 INIT_NULL(1);  INIT_NULL_2_14;
128
129         #define INIT_NULL_2_14 \
130                 INIT_NULL(2);  INIT_NULL_3_14;
131
132         #define INIT_NULL_3_14 \
133                 INIT_NULL(3);  INIT_NULL_4_14;
134
135         #define INIT_NULL_4_14 \
136                 INIT_NULL(4); INIT_NULL_5_14;
137
138         #define INIT_NULL_5_14 \
139                 INIT_NULL(5);  INIT_NULL(6);  INIT_NULL(7); \
140                 INIT_NULL(8);  INIT_NULL(9);  INIT_NULL(10); INIT_NULL(11); \
141                 INIT_NULL(12); INIT_NULL(13); INIT_NULL(14);
142
143         #define ROTATE1_NULL_5_14 \
144                 ROTATE1_NULL; ROTATE1_NULL; ROTATE1_NULL; \
145                 ROTATE1_NULL; ROTATE1_NULL; ROTATE1_NULL; ROTATE1_NULL; \
146                 ROTATE1_NULL; ROTATE1_NULL; ROTATE1_NULL;
147
148
149         #define EXPAND(t) \
150                 W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]); \
151
152         #define EXPAND_3(t) W[t] = SHA1CircularShift(1,W[t-3]);
153         #define EXPAND_16(t) W[t] = SHA1CircularShift(1,W[t-16]);
154         #define EXPAND_3_8(t) W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8]);
155
156         if (length < 4) {
157                 INIT_NULL_1_14;
158                 W[15] = W_15;
159                 EXPAND_16(16);
160                 W[17] = 0;
161                 W[18] = W_15<<1;
162         }
163         else if (length < 8) {
164                 Endian_Reverse32(Message_Block_W[1]);
165                 INIT(1);
166                 INIT_NULL_2_14;
167                 W[15] = W_15;
168                 EXPAND_16(16);
169                 EXPAND_16(17);
170                 W[18] = W_15<<1;
171         }
172         else {
173                 Endian_Reverse32(Message_Block_W[1]);
174                 Endian_Reverse32(Message_Block_W[2]);
175                 Endian_Reverse32(Message_Block_W[3]);
176                 Endian_Reverse32(Message_Block_W[4]);
177                 INIT(1); INIT(2); INIT(3); INIT(4); 
178                 INIT_NULL_5_14;
179                 W[15] = W_15;
180                 EXPAND(16);
181                 EXPAND(17);
182                 EXPAND(18);
183         }
184
185         if (length < 12) {
186                 EXPAND_3(19);
187         }
188         else {
189                 EXPAND(19);
190         }
191
192         if (length < 16) {
193                 EXPAND_3(20);
194         }
195         else {
196                 EXPAND(20);
197         }
198         EXPAND_3(21); EXPAND_3(22);
199         EXPAND(23);
200
201         EXPAND(24); EXPAND(25); EXPAND_3_8(26); EXPAND_3_8(27);
202         EXPAND(28);     EXPAND(29);     EXPAND(30);     EXPAND(31);
203         EXPAND(32);     EXPAND(33);     EXPAND(34);     EXPAND(35);
204         EXPAND(36);     EXPAND(37);     EXPAND(38);     EXPAND(39);
205         EXPAND(40);     EXPAND(41);     EXPAND(42);     EXPAND(43);
206         EXPAND(44);     EXPAND(45);     EXPAND(46);     EXPAND(47);
207         EXPAND(48);     EXPAND(49);     EXPAND(50);     EXPAND(51);
208         EXPAND(52);     EXPAND(53);     EXPAND(54);     EXPAND(55);
209         EXPAND(56);     EXPAND(57);     EXPAND(58);     EXPAND(59);
210         EXPAND(60);     EXPAND(61);     EXPAND(62);     EXPAND(63);
211         EXPAND(64);     EXPAND(65);     EXPAND(66);     EXPAND(67);
212         EXPAND(68);     EXPAND(69);     EXPAND(70);     EXPAND(71);
213         EXPAND(72);     EXPAND(73);     EXPAND(74);     EXPAND(75);
214         EXPAND(76);     EXPAND(77);     EXPAND(78);     EXPAND(79);
215
216
217         #define ROTATE1_NEW(a, b, c, d, e, x) \
218                         e += SHA1CircularShift(5,a) + F_00_19(b,c,d) + x + K0; \
219                         b = SHA1CircularShift(30,b);
220
221         #define ROTATE1_NULL \
222                         temp = SHA1CircularShift(5,A) + F_00_19(B,C,D) + E + K0; \
223                         E = D; D = C; \
224                         C = SHA1CircularShift(30,B); \
225                         B = A; A = temp; \
226                         
227         #define ROTATE2_NEW(a, b, c, d, e, x) \
228                         e += SHA1CircularShift(5,a) + F_20_39(b,c,d) + x + K1; \
229                         b = SHA1CircularShift(30,b);
230         
231         #define ROTATE2(t) \
232                 temp = SHA1CircularShift(5,A) + F_20_39(B,C,D) + E + W[t] + K1; \
233                 E = D; D = C; \
234                 C = SHA1CircularShift(30,B); \
235                 B = A; A = temp;
236
237         #define ROTATE2_W(w) \
238                 temp = SHA1CircularShift(5,A) + F_20_39(B,C,D) + E + w + K1; \
239                 E = D; D = C; \
240                 C = SHA1CircularShift(30,B); \
241                 B = A; A = temp;
242
243         #define ROTATE3(t) \
244                 temp = SHA1CircularShift(5,A) + F_40_59(B,C,D) + E + W[t] + K2; \
245                 E = D; D = C; \
246                 C = SHA1CircularShift(30,B); \
247                 B = A; A = temp;
248
249         #define ROTATE4(t) \
250                 temp = SHA1CircularShift(5,A) + F_60_79(B,C,D) + E + W[t] + K3; \
251                 E = D; D = C; \
252                 C = SHA1CircularShift(30,B); \
253                 B = A; A = temp;
254
255         A = H0;
256         B = H1;
257         C = H2;
258         D = H3;
259         E = H4;
260
261
262         E = H2;
263         //D = 2079550178;
264         //C = 1506887872;
265         B = 2679412915u + W[0];
266         if (length < 4) {
267                 A = SHA1CircularShift(5,B) + 1722862861;
268         }
269         else {
270                 A = SHA1CircularShift(5,B) + 1722862861 + W[1];
271         }
272
273         if (length < 8) {
274                 temp = SHA1CircularShift(5,A) + ((((1506887872) ^ (2079550178)) & (B)) ^ (2079550178)) + H2 + K0;
275         }
276         else {
277                 temp = SHA1CircularShift(5,A) + (((572662306) & (B)) ^ (2079550178)) + H2 + K0 + W[2];
278         }
279         C = SHA1CircularShift(30,B);  //SHA1CircularShift(30,(2679412915 + W[0]));
280         B = A;
281         A = temp;
282         
283         if (length < 12) {
284                 temp = SHA1CircularShift(5,A) + ((((C) ^ (1506887872)) & (B)) ^ (1506887872)) + 2079550178 + K0;
285         }
286         else {
287                 temp = SHA1CircularShift(5,A) + ((((C) ^ (1506887872)) & (B)) ^ (1506887872)) + 2079550178 + K0 + W[3];
288         }
289         E = 1506887872;
290         D = C;
291         C = SHA1CircularShift(30,B);
292         B = A;
293         A = temp;
294
295         if (length < 16) {
296                 temp = SHA1CircularShift(5,A) + F_00_19(B,C,D) + 1506887872 + K0;
297         }
298         else {
299                 temp = SHA1CircularShift(5,A) + F_00_19(B,C,D) + 1506887872 + K0 + W[4];
300         }
301         E = D;
302         D = C;
303         C = SHA1CircularShift(30,B);
304         B = A;
305         A = temp;
306
307         ROTATE1_NULL_5_14;
308
309         ROTATE1_NEW( A, B, C, D, E, W_15 );
310         ROTATE1_NEW( E, A, B, C, D, W[16] );
311         ROTATE1_NEW( D, E, A, B, C, W[17] );
312         ROTATE1_NEW( C, D, E, A, B, W[18] );
313         ROTATE1_NEW( B, C, D, E, A, W[19] );
314                 
315         for(t = 20; t < 40; t++)
316         {
317                 if (t == 21 && length < 8) {
318                         ROTATE2_W((length<<5)); // *32
319                 }
320                 else {
321                         ROTATE2(t);
322                 }
323         }
324
325         for(t = 40; t < 60; t++)
326         {
327                 ROTATE3(t);
328         }
329         
330         for(t = 60; t < 80; t++)
331         {
332                 ROTATE4(t);
333         }
334         
335         Intermediate_Hash[0] += A;
336         Intermediate_Hash[1] += B;
337         Intermediate_Hash[2] += C;
338         Intermediate_Hash[3] += D;
339         Intermediate_Hash[4] += E;
340
341         Endian_Reverse32(Intermediate_Hash[0]);
342         Endian_Reverse32(Intermediate_Hash[1]);
343         Endian_Reverse32(Intermediate_Hash[2]);
344         Endian_Reverse32(Intermediate_Hash[3]);
345         Endian_Reverse32(Intermediate_Hash[4]);
346
347         memcpy(pDigest, Intermediate_Hash, 20);
348 }