]> git.sesse.net Git - freerainbowtables/blob - BOINC software/BOINC client apps/distrrtgen_cuda/distrrtgen.cpp
9ed924ea062b26654e55847952e206f98a0e2125
[freerainbowtables] / BOINC software / BOINC client apps / distrrtgen_cuda / distrrtgen.cpp
1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17
18 // This program serves as both
19 // - An example BOINC application, illustrating the use of the BOINC API
20 // - A program for testing various features of BOINC
21 //
22 // NOTE: this file exists as both
23 // boinc/apps/upper_case.C
24 // and
25 // boinc_samples/example_app/uc2.C
26 // If you update one, please update the other!
27
28 // The program converts a mixed-case file to upper case:
29 // read "in", convert to upper case, write to "out"
30 //
31 // command line options
32 // -run_slow: sleep 1 second after each character
33 // -cpu_time N: use about N CPU seconds after copying files
34 // -early_exit: exit(10) after 30 chars
35 // -early_crash: crash after 30 chars
36 //
37
38 #ifdef _WIN32
39 #include "boinc_win.h"
40 #else
41 #include "config.h"
42 #include <cstdio>
43 #include <cctype>
44 #include <ctime>
45 #include <cstring>
46 #include <cstdlib>
47 #include <csignal>
48 #include <unistd.h>
49 #endif
50
51 #include <string>
52 #include <fstream>
53 #include <iostream>
54 #include "str_util.h"
55 #include "util.h"
56 #include "filesys.h"
57 #include "boinc_api.h"
58 #include "Public.h"
59 // Rainbowcrack code
60 #include "ChainWalkContext.h"
61 //typedef unsigned int uint32;
62 //typedef unsigned __int64 uint64;
63 #include "rcuda.h"
64 #include "rcuda_ext.h"
65
66
67 using std::string;
68
69 /*
70 bool early_exit = false;
71 bool early_crash = false;
72 bool early_sleep = false;
73 double cpu_time = 20, comp_result;
74 */
75 /*
76 int QuickSortPartition(RainbowChainCP* pChain, int nLow, int nHigh)
77 {
78         int nRandomIndex = nLow + ((uint32)rand() * ((uint32)RAND_MAX + 1) + (uint32)rand()) % (nHigh - nLow + 1);
79         RainbowChainCP TempChain;
80         TempChain = pChain[nLow];
81         pChain[nLow] = pChain[nRandomIndex];
82         pChain[nRandomIndex] = TempChain;
83
84         TempChain = pChain[nLow];
85         uint64 nPivotKey = pChain[nLow].nIndexE;
86         while (nLow < nHigh)
87         {
88                 while (nLow < nHigh && pChain[nHigh].nIndexE >= nPivotKey)
89                         nHigh--;
90                 pChain[nLow] = pChain[nHigh];
91                 while (nLow < nHigh && pChain[nLow].nIndexE <= nPivotKey)
92                         nLow++;
93                 pChain[nHigh] = pChain[nLow];
94         }
95         pChain[nLow] = TempChain;
96         return nLow;
97 }
98
99 void QuickSort(RainbowChainCP* pChain, int nLow, int nHigh)
100 {
101         if (nLow < nHigh)
102         {
103                 int nPivotLoc = QuickSortPartition(pChain, nLow, nHigh);
104                 QuickSort(pChain, nLow, nPivotLoc - 1);
105                 QuickSort(pChain, nPivotLoc + 1, nHigh);
106         }
107 }
108 */
109 int main(int argc, char **argv) {    
110     int retval;
111     double fd;
112     char output_path[512], chkpt_path[512];
113     FILE* state;        
114     retval = boinc_init();
115     if (retval) {
116         fprintf(stderr, "boinc_init returned %d\n", retval);
117         exit(retval);
118     }
119
120         // extract a --device option
121         std::vector<char*> argVec;
122         int cudaDevice = -1;
123         for(int ii = 0; ii < argc; ii++) {
124                 if(cudaDevice < 0 && strcmp(argv[ii], "--device") == 0 && ii + 1 < argc)
125                         cudaDevice = atoi(argv[++ii]);
126                 else
127                         argVec.push_back(argv[ii]);
128         }
129         argc = (int)argVec.size();
130         argv = &argVec[0];
131         if(!(cudaDevice < 0))
132                 // set the cuda device
133                 if(rcuda::SetCudaDevice(cudaDevice) != 0)
134                 {
135                         //XXX this call doesn't work on linux
136                         // fixed in upstream source 2010-09-16
137                         // http://bolt.berkeley.edu/trac/changeset/22382
138                         #ifdef _WIN32
139                                 boinc_temporary_exit(60);
140                         #endif
141                 }
142
143         if(argc < 10)
144         {
145                 fprintf(stderr, "Not enough parameters");
146                 return -1;
147         }
148         string sHashRoutineName, sCharsetName, sSalt, sCheckPoints;
149         uint32 nRainbowChainCount, nPlainLenMin, nPlainLenMax, nRainbowTableIndex, nRainbowChainLen;
150         uint64 nChainStart;
151         sHashRoutineName = argv[1];
152         sCharsetName = argv[2];
153         nPlainLenMin = atoi(argv[3]);
154         nPlainLenMax = atoi(argv[4]);
155         nRainbowTableIndex = atoi(argv[5]);
156         nRainbowChainLen = atoi(argv[6]);
157         nRainbowChainCount = atoi(argv[7]);
158 #ifdef _WIN32
159
160         nChainStart = _atoi64(argv[8]);
161
162 #else
163         nChainStart = atoll(argv[8]);
164 #endif
165         sCheckPoints = argv[9];
166         vector<int> vCPPositions;
167         char *cp = strtok((char *)sCheckPoints.c_str(), ",");
168         while(cp != NULL)
169         {
170                 vCPPositions.push_back(atoi(cp));
171                 cp = strtok(NULL, ",");
172         }
173         if(argc == 11)
174         {
175                 sSalt = argv[10];
176         }
177         //std::cout << "Starting ChainGenerator" << std::endl;
178         // Setup CChainWalkContext
179         //std::cout << "ChainGenerator started." << std::endl;
180
181         if (!CChainWalkContext::SetHashRoutine(sHashRoutineName))
182         {
183                 fprintf(stderr, "hash routine %s not supported\n", sHashRoutineName.c_str());
184                 return 1;
185         }
186         //std::cout << "Hash routine validated" << std::endl;
187
188         if (!CChainWalkContext::SetPlainCharset(sCharsetName, nPlainLenMin, nPlainLenMax))
189         {       
190                 std::cerr << "charset " << sCharsetName << " not supported" << std::endl;
191                 return 2;
192         }
193         //std::cout << "Plain charset validated" << std::endl;
194
195         if (!CChainWalkContext::SetRainbowTableIndex(nRainbowTableIndex))
196         {
197                 std::cerr << "invalid rainbow table index " << nRainbowTableIndex << std::endl;
198                 return 3;
199         }
200         //std::cout << "Rainbowtable index validated" << std::endl;
201
202         if(sHashRoutineName == "mscache")// || sHashRoutineName == "lmchall" || sHashRoutineName == "halflmchall")
203         {
204                 // Convert username to unicode
205                 const char *szSalt = sSalt.c_str();
206                 int salt_length = strlen(szSalt);
207                 unsigned char cur_salt[256];
208                 for (int i=0; i<salt_length; i++)
209                 {
210                         cur_salt[i*2] = szSalt[i];
211                         cur_salt[i*2+1] = 0x00;
212                 }
213                 CChainWalkContext::SetSalt(cur_salt, salt_length*2);
214         }
215         else if(sHashRoutineName == "halflmchall")
216         { // The salt is hardcoded into the hash routine
217         //      CChainWalkContext::SetSalt((unsigned char*)&salt, 8);
218         }
219         else if(sHashRoutineName == "oracle")
220         {
221                 CChainWalkContext::SetSalt((unsigned char *)sSalt.c_str(), sSalt.length());
222         }
223         //std::cout << "Opening chain file" << std::endl;
224
225         
226         // Open file
227         boinc_resolve_filename("result", output_path, sizeof(output_path));
228         fclose(boinc_fopen(output_path, "a"));
229         FILE *outfile = boinc_fopen(output_path, "r+b");
230         
231         if (outfile == NULL)
232         {
233                 std::cerr << "failed to create " << output_path << std::endl;
234                 return 4;
235         }
236         
237         
238         // Check existing chains
239         unsigned int nDataLen = (unsigned int)GetFileLen(outfile);
240         unsigned int nFileLen;
241         
242         // Round to boundary
243         nDataLen = nDataLen / 10 * 10;
244         if (nDataLen == nRainbowChainCount * 10)
245         {               
246                 std::cerr << "precomputation of this rainbow table already finished" << std::endl;
247                 fclose(outfile);
248                 return 0;
249         }
250
251         fseek(outfile, nDataLen, SEEK_SET);
252         //XXX size_t isn't 32/64 clean
253         size_t nReturn;
254         CChainWalkContext cwc;
255         uint64 nIndex[2];
256         time_t tStart = time(NULL);
257
258 //      std::cout << "Starting to generate chains" << std::endl;
259         int maxCalcBuffSize = rcuda::GetChainsBufferSize(0x2000);
260         uint64 *calcBuff = new uint64[2*maxCalcBuffSize];
261         int ii;
262
263         CudaCWCExtender ex(&cwc);
264         rcuda::RCudaTask cuTask;
265         std::vector<unsigned char> stPlain;
266         ex.Init();
267
268         for(int nCurrentCalculatedChains = nDataLen / 10, calcSize; nCurrentCalculatedChains < nRainbowChainCount; )
269         {               
270                 fd = (double)nCurrentCalculatedChains / (double)nRainbowChainCount;
271                 boinc_fraction_done(fd);
272
273                 cuTask.hash = ex.GetHash();
274                 cuTask.startIdx = nChainStart + nCurrentCalculatedChains;
275                 cuTask.idxCount = std::min<int>(nRainbowChainCount - nCurrentCalculatedChains, maxCalcBuffSize);
276                 cuTask.stPlainSize = ex.IndexToStartPlain(0, stPlain);
277                 cuTask.stPlain = &stPlain[0];
278                 cuTask.dimVec = ex.GetPlainDimVec();
279                 cuTask.dimVecSize = ex.GetPlainDimVecSize()/3;
280                 cuTask.charSet = ex.GetCharSet();
281                 cuTask.charSetSize = ex.GetCharSetSize();
282                 cuTask.cpPositions = &vCPPositions[0];
283                 cuTask.cpPosSize = vCPPositions.size();
284                 cuTask.reduceOffset = ex.GetReduceOffset();
285                 cuTask.plainSpaceTotal = ex.GetPlainSpaceTotal();
286                 cuTask.rainbowChainLen = nRainbowChainLen;
287                 for(ii = 0; ii < cuTask.idxCount; ii++) {
288                         calcBuff[2*ii] = cuTask.startIdx + ii;
289                         calcBuff[2*ii+1] = 0;
290                 }
291                 calcSize = rcuda::CalcChainsOnCUDA(&cuTask, calcBuff);
292
293                 if(calcSize > 0) {
294                         nCurrentCalculatedChains += calcSize;
295                         for(ii = 0; ii < cuTask.idxCount; ii++) {
296                                 nIndex[0] = cuTask.startIdx + ii;
297 //                              nReturn = fwrite(nIndex, 1, 8, outfile);
298                                 nReturn = fwrite(calcBuff+(2*ii), 1, 8, outfile);
299                                 nReturn += fwrite(calcBuff+(2*ii+1), 1, 2, outfile);
300                                 if(nReturn != 10) {
301                                         std::cerr << "disk write fail" << std::endl;
302                                         fclose(outfile);
303                                         return 9;
304                                 }
305                         }
306                 } else {
307                         std::cerr << "Calculations on CUDA failed!" << std::endl;
308                         fclose(outfile);
309                         return 0;
310                 }
311         }
312         delete [] calcBuff;
313 #ifdef _DEBUG
314         std::cout << "Generation completed" << std::endl;
315 #endif
316 /*
317     fseek(outfile, 0, SEEK_SET);
318         nFileLen = GetFileLen(outfile);
319         nRainbowChainCount = nFileLen / 18;
320
321         RainbowChainCP* pChain = (RainbowChainCP*)new unsigned char[sizeof(RainbowChainCP) * nRainbowChainCount];
322
323         if (pChain != NULL)
324         {
325                 // Load file
326 #ifdef _DEBUG
327         std::cout << "Sorting file" << std::endl;
328 #endif
329                 fseek(outfile, 0, SEEK_SET);
330                 for(uint32 i = 0; i < nRainbowChainCount; i++)
331                 {
332                         if(fread(&pChain[i], 1, 16, outfile) != 16)
333                         {
334                                 printf("disk read fail\n");
335                                 return 9;
336                         }
337                         if(fread(&pChain[i].nCheckPoint, 1, sizeof(pChain[i].nCheckPoint), outfile) != 2)
338                         {
339                                 printf("disk read fail\n");
340                                 return 9;
341                         }
342                 }
343
344                 // Sort file
345                 QuickSort(pChain, 0, nRainbowChainCount - 1);
346
347                 // Write file
348                 fseek(outfile, 0, SEEK_SET);
349                 for(uint32 i = 0; i < nRainbowChainCount; i++)
350                 {
351                         fwrite(&pChain[i], 1, 16, outfile);
352                         fwrite(&pChain[i].nCheckPoint, 2, 1, outfile);
353                 }
354                 delete[] pChain;
355         }
356 */
357         fclose(outfile);
358     
359         // main loop - read characters, convert to UC, write
360     //
361
362     boinc_fraction_done(1);
363     boinc_finish(0);
364 }
365
366 #ifdef _WIN32
367 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
368     LPSTR command_line;
369     char* argv[100];
370     int argc;
371
372     command_line = GetCommandLine();
373     argc = parse_command_line( command_line, argv );
374     return main(argc, argv);
375 }
376 #endif