]> git.sesse.net Git - freerainbowtables/blob - Common/rt api/HashAlgorithm.cpp
64-bit fixes for distrrtgen
[freerainbowtables] / Common / rt api / HashAlgorithm.cpp
1 /*
2  * freerainbowtables is a project for generating, distributing, and using
3  * perfect rainbow tables
4  *
5  * Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
6  * Copyright Martin Westergaard Jørgensen <martinwj2005@gmail.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 freerainbowtables.
11  *
12  * freerainbowtables 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  * freerainbowtables 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 freerainbowtables.  If not, see <http://www.gnu.org/licenses/>.
24  *
25  * Changes: not using OpenSSL routines the slow way anymore, as suggested by jci.
26  */
27
28 #include "HashAlgorithm.h"
29
30 #include "Public.h"
31
32 #include <string.h>
33
34 //#include <openssl/md2.h>
35 #include "md4.h"
36 #include "md5.h"
37 #include "des.h"
38 //#include "sha1.h"
39 #if defined(_WIN32) && !defined(__GNUC__)
40         #pragma comment(lib, "libeay32.lib")
41 #endif
42
43 #ifdef __NetBSD__
44         #include <des.h>
45 #endif
46
47 #define MSCACHE_HASH_SIZE 16
48 void setup_des_key(unsigned char key_56[], des_key_schedule &ks)
49 {
50         des_cblock key;
51
52         key[0] = key_56[0];
53         key[1] = (key_56[0] << 7) | (key_56[1] >> 1);
54         key[2] = (key_56[1] << 6) | (key_56[2] >> 2);
55         key[3] = (key_56[2] << 5) | (key_56[3] >> 3);
56         key[4] = (key_56[3] << 4) | (key_56[4] >> 4);
57         key[5] = (key_56[4] << 3) | (key_56[5] >> 5);
58         key[6] = (key_56[5] << 2) | (key_56[6] >> 6);
59         key[7] = (key_56[6] << 1);
60
61         //des_set_odd_parity(&key);
62         des_set_key(&key, ks);
63 }
64
65 void HashLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
66 {
67         /*
68         unsigned char data[7] = {0};
69         memcpy(data, pPlain, nPlainLen > 7 ? 7 : nPlainLen);
70         */
71
72         int i;
73         for (i = nPlainLen; i < 7; i++)
74                 pPlain[i] = 0;
75
76         static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
77         des_key_schedule ks;
78         //setup_des_key(data, ks);
79         setup_des_key(pPlain, ks);
80         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pHash, ks, DES_ENCRYPT);
81 }
82
83 void HashLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
84 {
85         unsigned char pass[14];
86         unsigned char pre_lmresp[21];
87         static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
88         static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; 
89         des_key_schedule ks;
90
91         memset (pass,0,sizeof(pass));
92         memset (pre_lmresp,0,sizeof(pre_lmresp));
93
94         memcpy (pass,pPlain, nPlainLen);
95
96         setup_des_key(pass, ks);
97         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
98
99         setup_des_key(&pass[7], ks);
100         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)&pre_lmresp[8], ks, DES_ENCRYPT);
101
102         setup_des_key(pre_lmresp, ks);
103         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
104
105         setup_des_key(&pre_lmresp[7], ks);
106         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
107
108         setup_des_key(&pre_lmresp[14], ks);
109         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
110
111
112
113 void HashHALFLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
114 {       
115         unsigned char pre_lmresp[8];
116         static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
117         static unsigned char salt[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
118
119         des_key_schedule ks;
120         unsigned char plain[8] = {0};   
121         memcpy(plain, pPlain, nPlainLen);
122         setup_des_key(plain, ks);
123         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
124
125         setup_des_key(pre_lmresp, ks);
126         des_ecb_encrypt((des_cblock*)salt, (des_cblock*)pHash, ks, DES_ENCRYPT);
127
128
129
130
131 void HashNTLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
132 {
133         unsigned char UnicodePlain[MAX_PLAIN_LEN];
134         static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; 
135         
136         int len = (nPlainLen < 127) ? nPlainLen : 127;
137         int i;
138         
139         for (i = 0; i < len; i++)
140         {
141         UnicodePlain[i * 2] = pPlain[i];
142         UnicodePlain[i * 2 + 1] = 0x00;
143         }
144         
145         des_key_schedule ks;
146         unsigned char lm[21];
147         
148         /*MD4_CTX ctx;
149         MD4_Init(&ctx);
150         MD4_Update(&ctx, UnicodePlain, len * 2);
151         MD4_Final(lm, &ctx);  */
152         MD4_NEW(UnicodePlain, len * 2, lm);
153         
154         //MD4(UnicodePlain, len * 2, lm);
155         lm[16] = lm[17] = lm[18] = lm[19] = lm[20] = 0;
156         
157         setup_des_key(lm, ks);
158         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
159         
160         setup_des_key(&lm[7], ks);
161         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
162         
163         setup_des_key(&lm[14], ks);
164         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
165 }
166
167 /*
168 void HashORACLE(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
169 {
170         char ToEncrypt[256];
171         char temp[256];
172         char username[256];
173
174         DES_cblock iv,iv2;
175         DES_key_schedule ks1,ks2;
176         unsigned char deskey_fixed[]={ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
177         int i,j;
178 #ifdef _WIN32
179         strcpy_s(username, sizeof(username), "SYS");
180 #else
181         strcpy(username, "SYS");
182 #endif
183         int userlen = 3;
184 #ifdef _WIN32
185         _strupr((char*) pPlain);
186 #else
187         strupr((char*) pPlain);
188 #endif
189         memset (ToEncrypt,0,sizeof(ToEncrypt));
190
191         for (i=1,j=0; j<userlen; i++,j++)
192         {
193                 ToEncrypt[i] = username[j];
194                 i++;
195         }
196
197         for (j=0; j<nPlainLen; i++,j++)
198         {
199                 ToEncrypt[i] = pPlain[j];
200                 i++;
201         }
202
203         i=i-1;
204         memset (iv,0,8);
205         memset (iv2,0,8);
206         DES_set_key((DES_cblock*) deskey_fixed, &ks1);
207         DES_ncbc_encrypt((unsigned char*) ToEncrypt, (unsigned char*) temp, i, &ks1, &iv, DES_ENCRYPT);
208         DES_set_key((DES_cblock*) &iv, &ks2);
209         DES_ncbc_encrypt((unsigned char*) ToEncrypt, (unsigned char*) temp, i, &ks2, &iv2, DES_ENCRYPT);
210         memcpy (pHash,iv2,8);
211 }
212 */
213 void HashNTLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
214 {
215         unsigned char UnicodePlain[MAX_PLAIN_LEN * 2];
216         int i;
217         for (i = 0; i < nPlainLen; i++)
218         {
219                 UnicodePlain[i * 2] = pPlain[i];
220                 UnicodePlain[i * 2 + 1] = 0x00;
221         }
222
223         MD4_NEW(UnicodePlain, nPlainLen * 2, pHash);
224 }
225
226 /*
227 void HashMD2(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
228 {
229         MD2_CTX ctx;
230         MD2_Init(&ctx);
231         MD2_Update(&ctx, pPlain, nPlainLen);
232         MD2_Final(pHash, &ctx);
233
234         //MD2(pPlain, nPlainLen, pHash);
235 }
236 */
237
238 void HashMD4(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
239 {
240         MD4_NEW(pPlain, nPlainLen, pHash);
241 }
242
243 void HashMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
244 {
245         MD5_NEW(pPlain, nPlainLen, pHash);
246 }
247 void HashDoubleMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
248 {
249         MD5_NEW(pPlain, nPlainLen, pHash);
250         unsigned char hash[16];
251         memcpy(hash, pHash, 16);
252         MD5_NEW(hash, 16, pHash);
253 }
254
255 /*
256 void HashSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
257 {
258         SHA_CTX ctx;
259         SHA1_Init(&ctx);
260         SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
261         SHA1_Final(pHash, &ctx);
262 }
263
264 void HashRIPEMD160(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
265 {
266         RIPEMD160_CTX ctx;
267         RIPEMD160_Init(&ctx);
268         RIPEMD160_Update(&ctx, pPlain, nPlainLen);
269         RIPEMD160_Final(pHash, &ctx);  
270
271         //RIPEMD160(pPlain, nPlainLen, pHash);
272 }
273
274 void HashMSCACHE(unsigned char *pPlain, int nPlainLen, unsigned char* pHash)
275 {
276         char unicode_pwd[256];
277         char unicode_user[256];
278         static unsigned char username[] = "administrator";
279         static int userlen = 13;
280         unsigned char   final1[MD4_DIGEST_LENGTH];
281         MD4_CTX ctx;
282         int i;
283
284 //      strcpy (username, "administrator");
285 //      userlen = 13;
286
287         for (i=0; i<nPlainLen; i++)
288         {
289                 unicode_pwd[i*2] = pPlain[i];
290                 unicode_pwd[i*2+1] = 0x00;
291         }
292
293         for (i=0; i<userlen; i++)
294         {
295                 unicode_user[i*2] = username[i];
296                 unicode_user[i*2+1] = 0x00;
297         }
298
299         MD4_NEW( (unsigned char*)unicode_pwd, nPlainLen*2, final1 );
300
301         MD4_Init(&ctx);
302         MD4_Update(&ctx,final1,MD4_DIGEST_LENGTH);
303         MD4_Update(&ctx,(unsigned char*) unicode_user,userlen*2);
304         MD4_Final(pHash,&ctx);
305
306         /*
307         unsigned char unicode_pwd[256];
308         for (int i=0; i<nPlainLen; i++)
309         {
310                 unicode_pwd[i*2] = pPlain[i];
311                 unicode_pwd[i*2+1] = 0x00;
312         }*/
313         /*
314         unsigned char *buf = (unsigned char*)calloc(MSCACHE_HASH_SIZE + nSaltLength, sizeof(unsigned char));    
315         HashNTLM(pPlain, nPlainLen, buf, NULL);
316         //MD4(unicode_pwd, nPlainLen*2, buf);
317         memcpy(buf + MSCACHE_HASH_SIZE, pSalt, nSaltLength);
318         MD4(buf, MSCACHE_HASH_SIZE + nSaltLength, pHash); 
319         free(buf);
320         */
321 //}
322
323 //*********************************************************************************
324 // Code for MySQL password hashing
325 //*********************************************************************************
326 /*
327 inline void mysql_hash_password_323(unsigned long *result, const char *password) 
328 {
329         register unsigned long nr=1345345333L, add=7, nr2=0x12345671L;
330         unsigned long tmp;
331         for (; *password ; password++) 
332         {
333                 if (*password == ' ' || *password == '\t') continue;
334                 tmp= (unsigned long) (unsigned char) *password;
335                 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
336                 nr2+=(nr2 << 8) ^ nr;
337                 add+=tmp;
338         }
339         result[0]=nr & (((unsigned long) 1L << 31) -1L); // Don't use sign bit (str2int)
340         result[1]=nr2 & (((unsigned long) 1L << 31) -1L);
341         return;
342 }
343
344 void HashMySQL323(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
345 {
346         unsigned long hash_pass[2];     
347         unsigned char* f = (unsigned char*) hash_pass;
348
349         unsigned char* pass = (unsigned char*) calloc (nPlainLen+4,sizeof(unsigned char));
350         memcpy(pass,pPlain,nPlainLen);
351
352         mysql_hash_password_323(hash_pass, (char*) pass);
353         pHash[0]=*(f+3); pHash[1]=*(f+2); pHash[2]=*(f+1); pHash[3]=*(f+0);
354         pHash[4]=*(f+7); pHash[5]=*(f+6); pHash[6]=*(f+5); pHash[7]=*(f+4);
355
356         free (pass);
357 }
358
359 void HashMySQLSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
360 {
361         unsigned char hash_stage1[SHA_DIGEST_LENGTH];
362         SHA_CTX ctx;
363
364         SHA1_Init(&ctx);
365         SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
366         SHA1_Final(hash_stage1, &ctx);
367         SHA1_Init(&ctx);
368         SHA1_Update(&ctx, hash_stage1, SHA_DIGEST_LENGTH);
369         SHA1_Final(pHash, &ctx);
370 }
371 */
372
373 //*********************************************************************************
374 // Code for PIX password hashing
375 //*********************************************************************************
376 static char itoa64[] =          /* 0 ... 63 => ascii - 64 */
377         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
378
379 void _crypt_to64(char *s, unsigned long v, int n)
380 {
381         while (--n >= 0) {
382                 *s++ = itoa64[v&0x3f];
383                 v >>= 6;
384         }
385 }
386 /*
387 void HashPIX(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
388 {
389         char temp[MD5_DIGEST_LENGTH+1];
390         unsigned char final[MD5_DIGEST_LENGTH];
391         char* pass = (char*) calloc (nPlainLen+MD5_DIGEST_LENGTH,sizeof(char));
392
393         memcpy (pass,pPlain,nPlainLen);
394
395         MD5_CTX ctx;
396         MD5_Init(&ctx);
397         MD5_Update(&ctx, (unsigned char *) pass, MD5_DIGEST_LENGTH);
398         MD5_Final(final, &ctx);
399         MD5_NEW((unsigned char *) pass, MD5_DIGEST_LENGTH, final);
400
401         char* p = (char*) temp;
402         _crypt_to64(p,*(unsigned long*) (final+0),4); p += 4;
403         _crypt_to64(p,*(unsigned long*) (final+4),4); p += 4;
404         _crypt_to64(p,*(unsigned long*) (final+8),4); p += 4;
405         _crypt_to64(p,*(unsigned long*) (final+12),4); p += 4;
406         *p=0;
407
408         memcpy(pHash,temp,MD5_DIGEST_LENGTH);
409
410         free (pass);
411 }
412 */
413 #if !defined(_WIN32) || defined(__GNUC__)
414 char *strupr(char *s1)
415 {
416         char *p = s1;
417         while(*p)
418         {
419                 *p = (char) toupper(*p);
420                 p++;
421         }
422         return s1;
423 }
424 #endif