]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/rcrackiThread.cpp
backport rcracki_mt trunk from rcracki.sourceforge.net
[freerainbowtables] / Client Applications / rcracki_mt / rcrackiThread.cpp
1 /*\r
2  * rcracki_mt is a multithreaded implementation and fork of the original \r
3  * RainbowCrack\r
4  *\r
5  * Copyright 2009, 2010 DaniĆ«l Niggebrugge <niggebrugge@fox-it.com>\r
6  * Copyright 2009, 2010 James Nobis <frt@quelrod.net>\r
7  *\r
8  * This file is part of rcracki_mt.\r
9  *\r
10  * rcracki_mt is free software: you can redistribute it and/or modify\r
11  * it under the terms of the GNU General Public License as published by\r
12  * the Free Software Foundation, either version 2 of the License, or\r
13  * (at your option) any later version.\r
14  *\r
15  * rcracki_mt is distributed in the hope that it will be useful,\r
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
18  * GNU General Public License for more details.\r
19  *\r
20  * You should have received a copy of the GNU General Public License\r
21  * along with rcracki_mt.  If not, see <http://www.gnu.org/licenses/>.\r
22  */\r
23 \r
24 #if defined(_WIN32) && !defined(__GNUC__)\r
25         #pragma warning(disable : 4786 4267 4018)\r
26 #endif\r
27 \r
28 #include "rcrackiThread.h"\r
29 \r
30 // create job for pre-calculation\r
31 rcrackiThread::rcrackiThread(unsigned char* TargetHash, int thread_id, int nRainbowChainLen, int thread_count, uint64* pStartPosIndexE)\r
32 {\r
33         t_TargetHash = TargetHash;\r
34         t_nRainbowChainLen = nRainbowChainLen;\r
35         t_ID = thread_id;\r
36         t_count = thread_count;\r
37         t_pStartPosIndexE = pStartPosIndexE;\r
38         t_nChainWalkStep = 0;\r
39         falseAlarmChecker = false;\r
40         falseAlarmCheckerO = false;\r
41 }\r
42 \r
43 // create job for false alarm checking\r
44 rcrackiThread::rcrackiThread(unsigned char* pHash, bool oldFormat)\r
45 {\r
46         falseAlarmChecker = true;\r
47         falseAlarmCheckerO = oldFormat;\r
48         t_pChainsFound.clear();\r
49         t_nGuessedPoss.clear();\r
50         t_pHash = pHash;\r
51         t_nChainWalkStepDueToFalseAlarm = 0;\r
52         t_nFalseAlarm = 0;\r
53         foundHash = false;\r
54 }\r
55 \r
56 void rcrackiThread::AddAlarmCheck(RainbowChain* pChain, int nGuessedPos)\r
57 {\r
58         t_pChainsFound.push_back(pChain);\r
59         t_nGuessedPoss.push_back(nGuessedPos);\r
60 }\r
61 \r
62 void rcrackiThread::AddAlarmCheckO(RainbowChainO* pChain, int nGuessedPos)\r
63 {\r
64         t_pChainsFoundO.push_back(pChain);\r
65         t_nGuessedPoss.push_back(nGuessedPos);\r
66 }\r
67 \r
68 // Windows (beginthreadex) way of threads\r
69 //unsigned __stdcall rcrackiThread::rcrackiThreadStaticEntryPoint(void * pThis)\r
70 //{\r
71 //      rcrackiThread* pTT = (rcrackiThread*)pThis;\r
72 //      pTT->rcrackiThreadEntryPoint();\r
73 //      _endthreadex( 2 );\r
74 //      return 2;\r
75 //}\r
76 \r
77 // entry point for the posix thread\r
78 void * rcrackiThread::rcrackiThreadStaticEntryPointPthread(void * pThis)\r
79 {\r
80         rcrackiThread* pTT = (rcrackiThread*)pThis;\r
81         pTT->rcrackiThreadEntryPoint();\r
82         pthread_exit(NULL);\r
83         return NULL;\r
84 }\r
85 \r
86 // start processing of jobs\r
87 void rcrackiThread::rcrackiThreadEntryPoint()\r
88 {\r
89         if (falseAlarmChecker) {\r
90                 if (falseAlarmCheckerO) {\r
91                         CheckAlarmO();\r
92                 }\r
93                 else {\r
94                         CheckAlarm();\r
95                 }\r
96         }\r
97         else {\r
98                 PreCalculate();\r
99         }\r
100 }\r
101 \r
102 uint64 rcrackiThread::GetIndex(int nPos)\r
103 {\r
104         uint64 t_index = t_vStartPosIndexE[nPos - t_ID];\r
105         return t_index;\r
106 }\r
107 \r
108 int rcrackiThread::GetChainWalkStep()\r
109 {\r
110         return t_nChainWalkStep;\r
111 }\r
112 \r
113 int rcrackiThread::GetIndexCount()\r
114 {\r
115         return t_vStartPosIndexE.size();\r
116 }\r
117 \r
118 rcrackiThread::~rcrackiThread(void)\r
119 {\r
120 }\r
121 \r
122 void rcrackiThread::PreCalculate()\r
123 {\r
124         //XXX is this correct for multiple threads?\r
125         for (t_nPos = t_nRainbowChainLen - 2 - t_ID; t_nPos >= 0; t_nPos -= t_count)\r
126         {\r
127                 t_cwc.SetHash(t_TargetHash);\r
128                 t_cwc.HashToIndex(t_nPos);\r
129                 int i;\r
130                 for (i = t_nPos + 1; i <= t_nRainbowChainLen - 2; i++)\r
131                 {\r
132                         t_cwc.IndexToPlain();\r
133                         t_cwc.PlainToHash();\r
134                         t_cwc.HashToIndex(i);\r
135                 }\r
136                 t_pStartPosIndexE[t_nPos] = t_cwc.GetIndex();\r
137                 t_nChainWalkStep += t_nRainbowChainLen - 2 - t_nPos;\r
138         }\r
139 }\r
140 \r
141 void rcrackiThread::CheckAlarm()\r
142 {\r
143         uint32 i;\r
144         for (i = 0; i < t_pChainsFound.size(); i++)\r
145         {\r
146                 RainbowChain* t_pChain = t_pChainsFound[i];\r
147                 int t_nGuessedPos = t_nGuessedPoss[i];          \r
148                 \r
149                 CChainWalkContext cwc;\r
150                 //uint64 nIndexS = t_pChain->nIndexS & 0x0000FFFFFFFFFFFF; // for first 6 bytes\r
151                 //uint64 nIndexS = t_pChain->nIndexS >> 16;\r
152                 uint64 nIndexS = t_pChain->nIndexS & 0x0000FFFFFFFFFFFFULL; // for first 6 bytes\r
153                 cwc.SetIndex(nIndexS);\r
154                 //cwc.SetIndex(t_pChain->nIndexS);      \r
155                 int nPos;\r
156                 for (nPos = 0; nPos < t_nGuessedPos; nPos++)\r
157                 {\r
158                         cwc.IndexToPlain();\r
159                         cwc.PlainToHash();\r
160                         cwc.HashToIndex(nPos);\r
161                 }\r
162                 cwc.IndexToPlain();\r
163                 cwc.PlainToHash();\r
164                 if (cwc.CheckHash(t_pHash))\r
165                 {\r
166                         t_Hash = cwc.GetHash();\r
167                         t_Plain = cwc.GetPlain();\r
168                         t_Binary = cwc.GetBinary();\r
169 \r
170                         foundHash = true;\r
171                         break;\r
172                 }\r
173                 else {\r
174                         foundHash = false;\r
175                         t_nChainWalkStepDueToFalseAlarm += t_nGuessedPos + 1;\r
176                         t_nFalseAlarm++;\r
177                 }\r
178         }\r
179 }\r
180 \r
181 void rcrackiThread::CheckAlarmO()\r
182 {\r
183         uint32 i;\r
184         for (i = 0; i < t_pChainsFoundO.size(); i++)\r
185         {\r
186                 RainbowChainO* t_pChain = t_pChainsFoundO[i];\r
187                 int t_nGuessedPos = t_nGuessedPoss[i];          \r
188                 \r
189                 CChainWalkContext cwc;\r
190 \r
191                 uint64 nIndexS = t_pChain->nIndexS;\r
192                 cwc.SetIndex(nIndexS);\r
193 \r
194                 int nPos;\r
195                 for (nPos = 0; nPos < t_nGuessedPos; nPos++)\r
196                 {\r
197                         cwc.IndexToPlain();\r
198                         cwc.PlainToHash();\r
199                         cwc.HashToIndex(nPos);\r
200                 }\r
201                 cwc.IndexToPlain();\r
202                 cwc.PlainToHash();\r
203                 if (cwc.CheckHash(t_pHash))\r
204                 {\r
205                         t_Hash = cwc.GetHash();\r
206                         t_Plain = cwc.GetPlain();\r
207                         t_Binary = cwc.GetBinary();\r
208 \r
209                         foundHash = true;\r
210                         break;\r
211                 }\r
212                 else {\r
213                         foundHash = false;\r
214                         t_nChainWalkStepDueToFalseAlarm += t_nGuessedPos + 1;\r
215                         t_nFalseAlarm++;\r
216                 }\r
217         }\r
218 }\r
219 \r
220 bool rcrackiThread::FoundHash()\r
221 {\r
222         return foundHash;\r
223 }\r
224 \r
225 int rcrackiThread::GetChainWalkStepDueToFalseAlarm()\r
226 {\r
227         return t_nChainWalkStepDueToFalseAlarm;\r
228 }\r
229 \r
230 int rcrackiThread::GetnFalseAlarm()\r
231 {\r
232         return t_nFalseAlarm;\r
233 }\r
234 \r
235 string rcrackiThread::GetHash()\r
236 {\r
237         return t_Hash;\r
238 }\r
239 \r
240 string rcrackiThread::GetPlain()\r
241 {\r
242         return t_Plain;\r
243 }\r
244 \r
245 string rcrackiThread::GetBinary()\r
246 {\r
247         return t_Binary;\r
248 }\r