]> git.sesse.net Git - freerainbowtables/blob - Common/rt api/HashAlgorithm.cpp
909f79cf5ada7695c4ab2d7f83dc719134c5f58a
[freerainbowtables] / Common / rt api / HashAlgorithm.cpp
1 /*
2    RainbowCrack - a general propose implementation of Philippe Oechslin's faster time-memory trade-off technique.
3
4    Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
5 */
6
7 #include "HashAlgorithm.h"
8
9 #include "Public.h"
10 #include <string.h>
11 #include "md4.h"
12 #include "md5.h"
13 #include "des.h"
14 #define MSCACHE_HASH_SIZE 16
15 void setup_des_key(unsigned char key_56[], des_key_schedule &ks)
16 {
17         des_cblock key;
18
19         key[0] = key_56[0];
20         key[1] = (key_56[0] << 7) | (key_56[1] >> 1);
21         key[2] = (key_56[1] << 6) | (key_56[2] >> 2);
22         key[3] = (key_56[2] << 5) | (key_56[3] >> 3);
23         key[4] = (key_56[3] << 4) | (key_56[4] >> 4);
24         key[5] = (key_56[4] << 3) | (key_56[5] >> 5);
25         key[6] = (key_56[5] << 2) | (key_56[6] >> 6);
26         key[7] = (key_56[6] << 1);
27
28         //des_set_odd_parity(&key);
29         des_set_key(&key, ks);
30 }
31
32 void HashLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
33 {
34         /*
35         unsigned char data[7] = {0};
36         memcpy(data, pPlain, nPlainLen > 7 ? 7 : nPlainLen);
37         */
38
39         int i;
40         for (i = nPlainLen; i < 7; i++)
41                 pPlain[i] = 0;
42
43         static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
44         des_key_schedule ks;
45         //setup_des_key(data, ks);
46         setup_des_key(pPlain, ks);
47         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pHash, ks, DES_ENCRYPT);
48 }
49
50 void HashLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
51 {
52         unsigned char pass[14];
53         unsigned char pre_lmresp[21];
54         static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
55         static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; 
56         des_key_schedule ks;
57
58         memset (pass,0,sizeof(pass));
59         memset (pre_lmresp,0,sizeof(pre_lmresp));
60
61         memcpy (pass,pPlain, nPlainLen);
62
63         setup_des_key(pass, ks);
64         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
65
66         setup_des_key(&pass[7], ks);
67         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)&pre_lmresp[8], ks, DES_ENCRYPT);
68
69         setup_des_key(pre_lmresp, ks);
70         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
71
72         setup_des_key(&pre_lmresp[7], ks);
73         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
74
75         setup_des_key(&pre_lmresp[14], ks);
76         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
77
78
79
80 void HashHALFLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
81 {       
82         unsigned char pre_lmresp[8];
83         static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
84         static unsigned char salt[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
85
86         des_key_schedule ks;
87         unsigned char plain[8] = {0};   
88         memcpy(plain, pPlain, nPlainLen);
89         setup_des_key(plain, ks);
90         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
91
92         setup_des_key(pre_lmresp, ks);
93         des_ecb_encrypt((des_cblock*)salt, (des_cblock*)pHash, ks, DES_ENCRYPT);
94
95
96
97
98 void HashNTLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
99 {
100   unsigned char UnicodePlain[MAX_PLAIN_LEN];
101   static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; 
102   
103   int len = (nPlainLen < 127) ? nPlainLen : 127;
104   int i;
105   
106   for (i = 0; i < len; i++)
107   {
108     UnicodePlain[i * 2] = pPlain[i];
109     UnicodePlain[i * 2 + 1] = 0x00;
110   }
111
112   des_key_schedule ks;
113   unsigned char lm[21];
114
115   MD4_NEW(UnicodePlain, len * 2, lm);
116   lm[16] = lm[17] = lm[18] = lm[19] = lm[20] = 0;
117
118   setup_des_key(lm, ks);
119   des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
120
121   setup_des_key(&lm[7], ks);
122   des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
123
124   setup_des_key(&lm[14], ks);
125   des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
126 }
127
128 /*
129 void HashORACLE(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
130 {
131         char ToEncrypt[256];
132         char temp[256];
133         char username[256];
134
135         DES_cblock iv,iv2;
136         DES_key_schedule ks1,ks2;
137         unsigned char deskey_fixed[]={ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
138         int i,j;
139 #ifdef _WIN32
140         strcpy_s(username, sizeof(username), "SYS");
141 #else
142         strcpy(username, "SYS");
143 #endif
144         int userlen = 3;
145 #ifdef _WIN32
146         _strupr((char*) pPlain);
147 #else
148         strupr((char*) pPlain);
149 #endif
150         memset (ToEncrypt,0,sizeof(ToEncrypt));
151
152         for (i=1,j=0; j<userlen; i++,j++)
153         {
154                 ToEncrypt[i] = username[j];
155                 i++;
156         }
157
158         for (j=0; j<nPlainLen; i++,j++)
159         {
160                 ToEncrypt[i] = pPlain[j];
161                 i++;
162         }
163
164         i=i-1;
165         memset (iv,0,8);
166         memset (iv2,0,8);
167         DES_set_key((DES_cblock*) deskey_fixed, &ks1);
168         DES_ncbc_encrypt((unsigned char*) ToEncrypt, (unsigned char*) temp, i, &ks1, &iv, DES_ENCRYPT);
169         DES_set_key((DES_cblock*) &iv, &ks2);
170         DES_ncbc_encrypt((unsigned char*) ToEncrypt, (unsigned char*) temp, i, &ks2, &iv2, DES_ENCRYPT);
171         memcpy (pHash,iv2,8);
172 }
173 */
174 void HashNTLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
175 {
176         unsigned char UnicodePlain[MAX_PLAIN_LEN * 2];
177         int i;
178         for (i = 0; i < nPlainLen; i++)
179         {
180                 UnicodePlain[i * 2] = pPlain[i];
181                 UnicodePlain[i * 2 + 1] = 0x00;
182         }
183
184         MD4_NEW(UnicodePlain, nPlainLen * 2, pHash);
185 }
186 /*
187 void HashMD2(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
188 {
189         MD2(pPlain, nPlainLen, pHash);
190 }
191 */
192 void HashMD4(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
193 {
194         MD4_NEW(pPlain, nPlainLen, pHash);
195 }
196
197 void HashMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
198 {
199         MD5_NEW(pPlain, nPlainLen, pHash);
200 }
201 void HashDoubleMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
202 {
203         MD5_NEW(pPlain, nPlainLen, pHash);
204         unsigned char hash[16];
205         memcpy(hash, pHash, 16);
206         MD5_NEW(hash, 16, pHash);
207 }
208 /*
209 void HashSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
210 {
211         SHA1(pPlain, nPlainLen, pHash);
212 }
213
214 void HashRIPEMD160(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
215 {
216         RIPEMD160(pPlain, nPlainLen, pHash);
217 }
218
219 void HashMSCACHE(unsigned char *pPlain, int nPlainLen, unsigned char* pHash)
220 {
221         char unicode_pwd[256];
222         char unicode_user[256];
223         static unsigned char username[] = "administrator";
224         static int userlen = 13;
225         unsigned char   final1[MD4_DIGEST_LENGTH];
226         MD4_CTX ctx;
227         int i;
228
229 //      strcpy (username, "administrator");
230 //      userlen = 13;
231
232         for (i=0; i<nPlainLen; i++)
233         {
234                 unicode_pwd[i*2] = pPlain[i];
235                 unicode_pwd[i*2+1] = 0x00;
236         }
237
238         for (i=0; i<userlen; i++)
239         {
240                 unicode_user[i*2] = username[i];
241                 unicode_user[i*2+1] = 0x00;
242         }
243
244         MD4_Init(&ctx);
245         MD4_Update(&ctx,unicode_pwd,nPlainLen*2);
246         MD4_Final(final1,&ctx);
247
248         MD4_Init(&ctx);
249         MD4_Update(&ctx,final1,MD4_DIGEST_LENGTH);
250         MD4_Update(&ctx,(unsigned char*) unicode_user,userlen*2);
251         MD4_Final(pHash,&ctx);
252
253         /*
254         unsigned char unicode_pwd[256];
255         for (int i=0; i<nPlainLen; i++)
256         {
257                 unicode_pwd[i*2] = pPlain[i];
258                 unicode_pwd[i*2+1] = 0x00;
259         }*/     
260         /*
261         unsigned char *buf = (unsigned char*)calloc(MSCACHE_HASH_SIZE + nSaltLength, sizeof(unsigned char));    
262         HashNTLM(pPlain, nPlainLen, buf, NULL);
263         //MD4(unicode_pwd, nPlainLen*2, buf);
264         memcpy(buf + MSCACHE_HASH_SIZE, pSalt, nSaltLength);
265         MD4(buf, MSCACHE_HASH_SIZE + nSaltLength, pHash); 
266         free(buf);
267         */
268 //}
269
270 //*********************************************************************************
271 // Code for MySQL password hashing
272 //*********************************************************************************
273 /*
274 inline void mysql_hash_password_323(unsigned long *result, const char *password) 
275 {
276   register unsigned long nr=1345345333L, add=7, nr2=0x12345671L;
277   unsigned long tmp;
278   for (; *password ; password++) 
279   {
280     if (*password == ' ' || *password == '\t') continue;
281         tmp= (unsigned long) (unsigned char) *password;
282         nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
283         nr2+=(nr2 << 8) ^ nr;
284         add+=tmp;
285   }
286   result[0]=nr & (((unsigned long) 1L << 31) -1L); ;
287   result[1]=nr2 & (((unsigned long) 1L << 31) -1L);
288   return;
289 }
290
291 void HashMySQL323(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
292 {
293         unsigned long hash_pass[2];     
294         unsigned char* f = (unsigned char*) hash_pass;
295
296         unsigned char* pass = (unsigned char*) calloc (nPlainLen+4,sizeof(unsigned char));
297         memcpy(pass,pPlain,nPlainLen);
298
299         mysql_hash_password_323(hash_pass, (char*) pass);
300         pHash[0]=*(f+3); pHash[1]=*(f+2); pHash[2]=*(f+1); pHash[3]=*(f+0);
301         pHash[4]=*(f+7); pHash[5]=*(f+6); pHash[6]=*(f+5); pHash[7]=*(f+4);
302
303         free (pass);
304 }
305
306 void HashMySQLSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
307 {
308         unsigned char hash_stage1[SHA_DIGEST_LENGTH];
309         SHA_CTX ctx;
310
311         SHA1_Init(&ctx);
312         SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
313         SHA1_Final(hash_stage1, &ctx);
314         SHA1_Init(&ctx);
315         SHA1_Update(&ctx, hash_stage1, SHA_DIGEST_LENGTH);
316         SHA1_Final(pHash, &ctx);
317 }
318 */
319 //*********************************************************************************
320 // Code for PIX password hashing
321 //*********************************************************************************
322 static char itoa64[] =          /* 0 ... 63 => ascii - 64 */
323         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
324
325 void _crypt_to64(char *s, unsigned long v, int n)
326 {
327         while (--n >= 0) {
328                 *s++ = itoa64[v&0x3f];
329                 v >>= 6;
330         }
331 }
332 /*
333 void HashPIX(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
334 {
335         char temp[MD5_DIGEST_LENGTH+1];
336         unsigned char final[MD5_DIGEST_LENGTH];
337         char* pass = (char*) calloc (nPlainLen+MD5_DIGEST_LENGTH,sizeof(char));
338
339         memcpy (pass,pPlain,nPlainLen);
340
341         MD5_CTX ctx;
342         MD5_Init(&ctx);
343         MD5_Update(&ctx, (unsigned char *) pass, MD5_DIGEST_LENGTH);
344         MD5_Final(final, &ctx);
345
346         char* p = (char*) temp;
347         _crypt_to64(p,*(unsigned long*) (final+0),4); p += 4;
348         _crypt_to64(p,*(unsigned long*) (final+4),4); p += 4;
349         _crypt_to64(p,*(unsigned long*) (final+8),4); p += 4;
350         _crypt_to64(p,*(unsigned long*) (final+12),4); p += 4;
351         *p=0;
352
353         memcpy(pHash,temp,MD5_DIGEST_LENGTH);
354
355         free (pass);
356 }
357 */