forked from openwall/john
/
opencl_rawsha256_fmt.c
715 lines (581 loc) · 23.3 KB
/
opencl_rawsha256_fmt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
* Developed by Claudio André <claudio.andre at correios.net.br> in 2012
* Based on source code provided by Samuele Giovanni Tonon
*
* More information at http://openwall.info/wiki/john/OpenCL-RAWSHA-256
*
* Copyright (c) 2011 Samuele Giovanni Tonon <samu at linuxasylum dot net>
* Copyright (c) 2012 Claudio André <claudio.andre at correios.net.br>
* This program comes with ABSOLUTELY NO WARRANTY; express or implied .
* This is free software, and you are welcome to redistribute it
* under certain conditions; as expressed here
* http://www.gnu.org/licenses/gpl-2.0.html
*/
#include <string.h>
#include "common-opencl.h"
#include "config.h"
#include "opencl_rawsha256.h"
#include "sha2.h"
#define FORMAT_LABEL "raw-sha256-opencl"
#define FORMAT_NAME "Raw SHA-256 (pwlen < " PLAINTEXT_TEXT ")"
#define ALGORITHM_NAME "OpenCL (inefficient, development use mostly)"
#define BENCHMARK_COMMENT ""
#define BENCHMARK_LENGTH -1
#define LWS_CONFIG "rawsha256_LWS"
#define GWS_CONFIG "rawsha256_GWS"
#define DUR_CONFIG "rawsha256_MaxDuration"
static sha256_password * plaintext; // plaintext ciphertexts
static uint32_t * calculated_hash; // calculated (partial) hashes
cl_mem pass_buffer; //Plaintext buffer.
cl_mem hash_buffer; //Partial hash keys (output).
cl_mem p_binary_buffer; //To compare partial binary ([3]).
cl_mem result_buffer; //To get the if a hash was found.
cl_mem pinned_saved_keys, pinned_partial_hashes;
cl_command_queue queue_prof;
cl_kernel crypt_kernel, cmp_kernel;
static int hash_found;
static struct fmt_tests tests[] = {
{"5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", "password"},
{"$SHA256$ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", "12345678"},
{"$SHA256$e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""},
#ifdef DEBUG //Special test cases.
{"a49c2c9d0c006c8cb55a9a7a38822b83e0cd442614cb416af952fa50156761dc", "openwall"},
#endif
{NULL}
};
/* ------- Helper functions ------- */
static size_t get_task_max_work_group_size(){
size_t max_available;
max_available = get_max_work_group_size(ocl_gpu_id);
if (max_available > get_current_work_group_size(ocl_gpu_id, crypt_kernel))
return get_current_work_group_size(ocl_gpu_id, crypt_kernel);
return max_available;
}
static size_t get_task_max_size(){
size_t max_available;
max_available = get_max_compute_units(ocl_gpu_id);
if (cpu(device_info[ocl_gpu_id]))
return max_available * KEYS_PER_CORE_CPU;
else
return max_available * KEYS_PER_CORE_GPU *
get_current_work_group_size(ocl_gpu_id, crypt_kernel);
}
static void crypt_one(int index, sha256_hash * hash) {
SHA256_CTX ctx;
SHA256_Init(&ctx);
SHA256_Update(&ctx, plaintext[index].pass, plaintext[index].length);
SHA256_Final((unsigned char *) (hash), &ctx);
}
/* ------- Create and destroy necessary objects ------- */
static void create_clobj(int gws, struct fmt_main * self) {
self->params.min_keys_per_crypt = self->params.max_keys_per_crypt = gws;
pinned_saved_keys = clCreateBuffer(context[ocl_gpu_id],
CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
sizeof(sha256_password) * gws, NULL, &ret_code);
HANDLE_CLERROR(ret_code, "Error creating page-locked memory pinned_saved_keys");
plaintext = (sha256_password *) clEnqueueMapBuffer(queue[ocl_gpu_id],
pinned_saved_keys, CL_TRUE, CL_MAP_WRITE | CL_MAP_READ, 0,
sizeof(sha256_password) * gws, 0, NULL, NULL, &ret_code);
HANDLE_CLERROR(ret_code, "Error mapping page-locked memory saved_plain");
pinned_partial_hashes = clCreateBuffer(context[ocl_gpu_id],
CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
sizeof(uint32_t) * gws, NULL, &ret_code);
HANDLE_CLERROR(ret_code, "Error creating page-locked memory pinned_partial_hashes");
calculated_hash = (uint32_t *) clEnqueueMapBuffer(queue[ocl_gpu_id],
pinned_partial_hashes, CL_TRUE, CL_MAP_READ, 0,
sizeof(uint32_t) * gws, 0, NULL, NULL, &ret_code);
HANDLE_CLERROR(ret_code, "Error mapping page-locked memory out_hashes");
// create arguments (buffers)
pass_buffer = clCreateBuffer(context[ocl_gpu_id], CL_MEM_READ_ONLY,
sizeof(sha256_password) * gws, NULL, &ret_code);
HANDLE_CLERROR(ret_code, "Error creating buffer argument buffer_keys");
hash_buffer = clCreateBuffer(context[ocl_gpu_id], CL_MEM_WRITE_ONLY,
sizeof(uint32_t) * gws, NULL, &ret_code);
HANDLE_CLERROR(ret_code, "Error creating buffer argument hash_buffer");
p_binary_buffer = clCreateBuffer(context[ocl_gpu_id], CL_MEM_READ_ONLY,
sizeof(uint32_t), NULL, &ret_code);
HANDLE_CLERROR(ret_code, "Error creating buffer argument p_binary_buffer");
result_buffer = clCreateBuffer(context[ocl_gpu_id], CL_MEM_READ_ONLY,
sizeof(int), NULL, &ret_code);
HANDLE_CLERROR(ret_code, "Error creating buffer argument result_buffer");
//Set kernel arguments
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 0, sizeof(cl_mem),
(void *) &pass_buffer), "Error setting argument 0");
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 1, sizeof(cl_mem),
(void *) &hash_buffer), "Error setting argument 1");
HANDLE_CLERROR(clSetKernelArg(cmp_kernel, 0, sizeof(cl_mem),
(void *) &hash_buffer), "Error setting argument 0");
HANDLE_CLERROR(clSetKernelArg(cmp_kernel, 1, sizeof(cl_mem),
(void *) &p_binary_buffer), "Error setting argument 1");
HANDLE_CLERROR(clSetKernelArg(cmp_kernel, 2, sizeof(cl_mem),
(void *) &result_buffer), "Error setting argument 2");
memset(plaintext, '\0', sizeof(sha256_password) * gws);
global_work_size = gws;
}
static void release_clobj(void) {
cl_int ret_code;
ret_code = clEnqueueUnmapMemObject(queue[ocl_gpu_id], pinned_saved_keys,
plaintext, 0, NULL, NULL);
HANDLE_CLERROR(ret_code, "Error Unmapping saved_plain");
ret_code = clReleaseMemObject(pass_buffer);
HANDLE_CLERROR(ret_code, "Error Releasing buffer_keys");
ret_code = clReleaseMemObject(hash_buffer);
HANDLE_CLERROR(ret_code, "Error Releasing hash_buffer");
ret_code = clReleaseMemObject(p_binary_buffer);
HANDLE_CLERROR(ret_code, "Error Releasing p_binary_buffer");
ret_code = clReleaseMemObject(result_buffer);
HANDLE_CLERROR(ret_code, "Error Releasing result_buffer");
ret_code = clReleaseMemObject(pinned_saved_keys);
HANDLE_CLERROR(ret_code, "Error Releasing pinned_saved_keys");
ret_code = clReleaseMemObject(pinned_partial_hashes);
HANDLE_CLERROR(ret_code, "Error Releasing pinned_partial_hashes");
}
/* ------- Key functions ------- */
static void set_key(char * key, int index) {
int len;
//Assure buffer has no "trash data".
memset(plaintext[index].pass, '\0', PLAINTEXT_LENGTH);
len = strlen(key);
//Put the tranfered key on password buffer.
memcpy(plaintext[index].pass, key, len);
plaintext[index].length = len ;
/* Prepare for GPU */
plaintext[index].pass->mem_08[len] = 0x80;
}
static char * get_key(int index) {
static char ret[PLAINTEXT_LENGTH + 1];
memcpy(ret, plaintext[index].pass, PLAINTEXT_LENGTH);
ret[plaintext[index].length] = '\0';
return ret;
}
/* ------- Try to find the best configuration ------- */
/* --
This function could be used to calculated the best num
for the workgroup
Work-items that make up a work-group (also referred to
as the size of the work-group)
LWS should never be a big number since every work-item
uses about 400 bytes of local memory. Local memory
is usually 32 KB
-- */
static void find_best_workgroup(struct fmt_main *self) {
size_t max_group_size;
max_group_size = get_task_max_work_group_size();
fprintf(stderr, "Max local worksize %d, ", (int) max_group_size);
//Call the default function.
opencl_find_best_workgroup_limit(self, max_group_size);
fprintf(stderr, "Optimal local worksize %d\n", (int) local_work_size);
fprintf(stderr, "(to avoid this test on next run, put \""
LWS_CONFIG " = %d\" in john.conf, section [" SECTION_OPTIONS
SUBSECTION_OPENCL "])\n", (int)local_work_size);
}
//Allow me to have a configurable step size.
static int get_step(size_t num, int step, int startup){
if (startup) {
if (step == 0)
return GET_MULTIPLE(STEP, local_work_size);
else
return GET_MULTIPLE(step, local_work_size);
}
if (step < 1)
return num * 2;
return num + step;
}
//Do the proper test using different sizes.
static cl_ulong gws_test(size_t num, struct fmt_main * self, int do_details) {
cl_event myEvent[3];
cl_int ret_code;
cl_uint *tmpbuffer;
cl_ulong startTime, endTime, runtime = 0;
int i, loops;
//Prepare buffers.
create_clobj(num, self);
tmpbuffer = mem_alloc(sizeof(sha256_hash) * num);
if (tmpbuffer == NULL) {
fprintf(stderr, "Malloc failure in find_best_gws\n");
exit(EXIT_FAILURE);
}
queue_prof = clCreateCommandQueue(context[ocl_gpu_id], devices[ocl_gpu_id],
CL_QUEUE_PROFILING_ENABLE, &ret_code);
HANDLE_CLERROR(ret_code, "Failed in clCreateCommandQueue");
// Set keys
for (i = 0; i < num; i++) {
set_key("aaabaabaaa", i);
}
//Send data to device.
HANDLE_CLERROR(clEnqueueWriteBuffer(queue_prof, pass_buffer, CL_FALSE, 0,
sizeof(sha256_password) * num, plaintext, 0, NULL, &myEvent[0]),
"Failed in clEnqueueWriteBuffer");
//Enqueue the kernel
ret_code = clEnqueueNDRangeKernel(queue_prof, crypt_kernel,
1, NULL, &num, &local_work_size, 0, NULL, &myEvent[1]);
//Read hashes back
HANDLE_CLERROR(clEnqueueReadBuffer(queue_prof, hash_buffer, CL_FALSE, 0,
sizeof(uint32_t) * num, tmpbuffer, 0, NULL, &myEvent[2]),
"Failed in clEnqueueReadBuffer");
loops = 3;
HANDLE_CLERROR(clFinish(queue_prof), "Failed in clFinish");
//** Get execution time **//
for (i = 0; i < loops; i++) {
HANDLE_CLERROR(clGetEventProfilingInfo(myEvent[i], CL_PROFILING_COMMAND_START,
sizeof(cl_ulong), &startTime, NULL), "Failed in clGetEventProfilingInfo I");
HANDLE_CLERROR(clGetEventProfilingInfo(myEvent[i], CL_PROFILING_COMMAND_END,
sizeof(cl_ulong), &endTime, NULL), "Failed in clGetEventProfilingInfo II");
runtime += (endTime - startTime);
if (do_details)
fprintf(stderr, "%s%.2f ms", warn[i], (double)(endTime-startTime)/1000000.);
}
if (do_details)
fprintf(stderr, "\n");
// Free resources.
for (i = 0; i < loops; i++)
HANDLE_CLERROR(clReleaseEvent(myEvent[i]), "Failed in clReleaseEvent");
release_clobj();
MEM_FREE(tmpbuffer);
HANDLE_CLERROR(clReleaseCommandQueue(queue_prof), "Failed in clReleaseCommandQueue");
if (ret_code != CL_SUCCESS) {
if (ret_code != CL_INVALID_WORK_GROUP_SIZE)
fprintf(stderr, "Error %d\n", ret_code);
return 0;
}
return runtime;
}
/* --
This function could be used to calculated the best num
of keys per crypt for the given format
-- */
static void find_best_gws(struct fmt_main * self) {
size_t num = 0;
cl_ulong run_time, min_time = CL_ULONG_MAX;
int optimal_gws = local_work_size, step = STEP;
int do_benchmark = 0, do_details = 0;
unsigned int SHAspeed, bestSHAspeed = 0;
unsigned long long int max_run_time = cpu(device_info[ocl_gpu_id]) ? 500000000ULL : 1000000000ULL;
char *tmp_value;
if (getenv("DETAILS")){
do_details = 1;
}
if ((tmp_value = getenv("STEP"))){
step = atoi(tmp_value);
do_benchmark = 1;
}
step = GET_MULTIPLE(step, local_work_size);
if ((tmp_value = cfg_get_param(SECTION_OPTIONS, SUBSECTION_OPENCL, DUR_CONFIG)))
max_run_time = atoi(tmp_value) * 1000000000ULL;
fprintf(stderr, "Calculating best global worksize (GWS) for LWS=%zd and max. %llu s duration.\n\n",
local_work_size, max_run_time / 1000000000ULL);
if (do_benchmark)
fprintf(stderr, "Raw speed figures including buffer transfers:\n");
for (num = get_step(num, step, 1); num; num = get_step(num, step, 0)) {
//Check if hardware can handle the size we are going to try now.
if (sizeof(sha256_password) * num * 1.2 > get_max_mem_alloc_size(ocl_gpu_id))
break;
if (! (run_time = gws_test(num, self, do_details)))
continue;
if (!do_benchmark && !do_details)
advance_cursor();
SHAspeed = num / (run_time / 1000000000.);
if (run_time < min_time)
min_time = run_time;
if (do_benchmark) {
fprintf(stderr, "gws: %8zu\t%12lu c/s %8.3f ms per crypt_all()",
num, (long) (num / (run_time / 1000000000.)),
(float) run_time / 1000000.);
if (run_time > max_run_time) {
fprintf(stderr, " - too slow\n");
break;
}
} else {
if (run_time > min_time * 20 || run_time > max_run_time)
break;
}
if (((long) SHAspeed - bestSHAspeed) > 10000) {
if (do_benchmark)
fprintf(stderr, "+");
bestSHAspeed = SHAspeed;
optimal_gws = num;
}
if (do_benchmark)
fprintf(stderr, "\n");
}
fprintf(stderr, "Optimal global worksize %d\n", optimal_gws);
fprintf(stderr, "(to avoid this test on next run, put \""
GWS_CONFIG " = %d\" in john.conf, section [" SECTION_OPTIONS
SUBSECTION_OPENCL "])\n", optimal_gws);
global_work_size = optimal_gws;
create_clobj(optimal_gws, self);
}
/* ------- Initialization ------- */
static void init(struct fmt_main * self) {
char * tmp_value;
char * task = "$JOHN/kernels/sha256_kernel.cl";
opencl_init_dev(device_id, platform_id);
opencl_build_kernel_save(task, ocl_gpu_id, NULL, 1, 1);
// create kernel(s) to execute
crypt_kernel = clCreateKernel(program[ocl_gpu_id], "kernel_crypt", &ret_code);
HANDLE_CLERROR(ret_code, "Error creating kernel. Double-check kernel name?");
cmp_kernel = clCreateKernel(program[ocl_gpu_id], "kernel_cmp", &ret_code);
HANDLE_CLERROR(ret_code, "Error creating kernel_cmp. Double-check kernel name?");
global_work_size = get_task_max_size();
local_work_size = 0;
if ((tmp_value = cfg_get_param(SECTION_OPTIONS,
SUBSECTION_OPENCL, LWS_CONFIG)))
local_work_size = atoi(tmp_value);
if ((tmp_value = getenv("LWS")))
local_work_size = atoi(tmp_value);
//Check if local_work_size is a valid number.
if (local_work_size > get_task_max_work_group_size()){
fprintf(stderr, "Error: invalid local worksize (LWS). Max value allowed is: %zd\n" ,
get_task_max_work_group_size());
local_work_size = 0; //Force find a valid number.
}
self->params.max_keys_per_crypt = global_work_size;
if (!local_work_size) {
local_work_size = get_task_max_work_group_size();
create_clobj(global_work_size, self);
find_best_workgroup(self);
release_clobj();
}
if ((tmp_value = cfg_get_param(SECTION_OPTIONS,
SUBSECTION_OPENCL, GWS_CONFIG)))
global_work_size = atoi(tmp_value);
if ((tmp_value = getenv("GWS")))
global_work_size = atoi(tmp_value);
//Check if a valid multiple is used.
global_work_size = GET_MULTIPLE(global_work_size, local_work_size);
if (global_work_size)
create_clobj(global_work_size, self);
else {
//user chose to die of boredom
global_work_size = get_task_max_size();
find_best_gws(self);
}
fprintf(stderr, "Local worksize (LWS) %d, global worksize (GWS) %zd\n",
(int) local_work_size, global_work_size);
self->params.min_keys_per_crypt = local_work_size;
self->params.max_keys_per_crypt = global_work_size;
}
static void done(void) {
release_clobj();
HANDLE_CLERROR(clReleaseKernel(crypt_kernel), "Release kernel");
HANDLE_CLERROR(clReleaseKernel(cmp_kernel), "Release kernel");
HANDLE_CLERROR(clReleaseProgram(program[ocl_gpu_id]), "Release Program");
HANDLE_CLERROR(clReleaseCommandQueue(queue[ocl_gpu_id]), "Release Queue");
HANDLE_CLERROR(clReleaseContext(context[ocl_gpu_id]), "Release Context");
}
/* ------- Check if the ciphertext if a valid SHA-256 ------- */
static int valid(char * ciphertext, struct fmt_main * self) {
char *p, *q;
p = ciphertext;
if (!strncmp(p, "$SHA256$", 8))
p += 8;
q = p;
while (atoi16[ARCH_INDEX(*q)] != 0x7F)
q++;
return !*q && q - p == CIPHERTEXT_LENGTH;
}
#if FMT_MAIN_VERSION > 9
static char *split(char *ciphertext, int index, struct fmt_main *pFmt) {
#else
static char *split(char *ciphertext, int index) {
#endif
static char out[8 + CIPHERTEXT_LENGTH + 1];
if (!strncmp(ciphertext, "$SHA256$", 8))
return ciphertext;
memcpy(out, "$SHA256$", 8);
memcpy(out + 8, ciphertext, CIPHERTEXT_LENGTH + 1);
strlwr(out + 8);
return out;
}
/* ------- To binary functions ------- */
static void * get_binary(char *ciphertext) {
static unsigned char *out;
uint32_t * b;
char *p;
int i;
if (!out) out = mem_alloc_tiny(FULL_BINARY_SIZE, MEM_ALIGN_WORD);
p = ciphertext + 8;
for (i = 0; i < (FULL_BINARY_SIZE / 2); i++) {
out[i] =
(atoi16[ARCH_INDEX(*p)] << 4) |
atoi16[ARCH_INDEX(p[1])];
p += 2;
}
b = (uint32_t *) out;
b[0] = SWAP32(b[3]) - H3;
return out;
}
static void * get_full_binary(char *ciphertext) {
static unsigned char *out;
char *p;
int i;
if (!out) out = mem_alloc_tiny(FULL_BINARY_SIZE, MEM_ALIGN_WORD);
p = ciphertext + 8;
for (i = 0; i < FULL_BINARY_SIZE; i++) {
out[i] =
(atoi16[ARCH_INDEX(*p)] << 4) |
atoi16[ARCH_INDEX(p[1])];
p += 2;
}
return out;
}
/* ------- Crypt function ------- */
static void crypt_all(int count) {
size_t gws;
gws = GET_MULTIPLE_BIGGER(count, local_work_size);
//Send data to device.
HANDLE_CLERROR(clEnqueueWriteBuffer(queue[ocl_gpu_id], pass_buffer, CL_FALSE, 0,
sizeof(sha256_password) * gws, plaintext, 0, NULL, NULL),
"failed in clEnqueueWriteBuffer pass_buffer");
//Enqueue the kernel
HANDLE_CLERROR(clEnqueueNDRangeKernel(queue[ocl_gpu_id], crypt_kernel, 1, NULL,
&gws, &local_work_size, 0, NULL, profilingEvent),
"failed in clEnqueueNDRangeKernel");
//Read back hashes
HANDLE_CLERROR(clEnqueueReadBuffer(queue[ocl_gpu_id], hash_buffer, CL_FALSE, 0,
sizeof(uint32_t) * gws, calculated_hash, 0, NULL, NULL),
"failed in reading data back");
//Do the work
HANDLE_CLERROR(clFinish(queue[ocl_gpu_id]), "failed in clFinish");
}
/* ------- Compare functins ------- */
static int cmp_all(void * binary, int count) {
uint32_t partial_binary;
size_t gws;
gws = GET_MULTIPLE_BIGGER(count, local_work_size);
partial_binary = ((uint32_t *) binary)[0];
hash_found = 0;
//Send data to device.
HANDLE_CLERROR(clEnqueueWriteBuffer(queue[ocl_gpu_id], p_binary_buffer, CL_FALSE, 0,
sizeof(uint32_t), &partial_binary, 0, NULL, NULL),
"failed in clEnqueueWriteBuffer p_binary_buffer");
HANDLE_CLERROR(clEnqueueWriteBuffer(queue[ocl_gpu_id], result_buffer, CL_FALSE, 0,
sizeof(int), &hash_found, 0, NULL, NULL),
"failed in clEnqueueWriteBuffer p_binary_buffer");
//Enqueue the kernel
HANDLE_CLERROR(clEnqueueNDRangeKernel(queue[ocl_gpu_id], cmp_kernel, 1, NULL,
&gws, &local_work_size, 0, NULL, NULL),
"failed in clEnqueueNDRangeKernel");
//Read results back.
HANDLE_CLERROR(clEnqueueReadBuffer(queue[ocl_gpu_id], result_buffer, CL_FALSE, 0,
sizeof(int), &hash_found, 0, NULL, NULL),
"failed in reading data back");
//Do the work
HANDLE_CLERROR(clFinish(queue[ocl_gpu_id]), "failed in clFinish");
return hash_found;
}
static int cmp_one(void *binary, int index) {
return (calculated_hash[index] == ((uint32_t *) binary)[0]);
}
static int cmp_exact(char *source, int index) {
//I don't know why, but this is called and i have to recheck.
//If i skip this final test i get:
//form=raw-sha512-ng-opencl guesses: 1468 time: 0:00:00:02 : Expected count(s) (1500) [!!!FAILED!!!]
//.pot CHK:raw-sha512-ng-opencl guesses: 1452 time: 0:00:00:02 : Expected count(s) (1500) [!!!FAILED!!!]
uint32_t * binary;
sha256_hash full_hash;
crypt_one(index, &full_hash);
binary = (uint32_t *) get_full_binary(source);
return !memcmp(binary, (void *) &full_hash, FULL_BINARY_SIZE);
}
/* ------- Binary Hash functions group ------- */
#ifdef DEBUG
static void print_binary(void * binary) {
uint32_t *bin = binary;
int i;
for (i = 0; i < 8; i++)
fprintf(stderr, "%08x ", bin[i]);
puts("(Ok)");
}
static void print_hash(int index) {
fprintf(stderr, "\n");
fprintf(stderr, "%08x ", calculated_hash[index]);
puts("");
}
#endif
static int binary_hash_0(void * binary) {
#ifdef DEBUG
print_binary(binary);
#endif
return *(ARCH_WORD_32 *) binary & 0xF;
}
static int binary_hash_1(void * binary) { return *(ARCH_WORD_32 *) binary & 0xFF; }
static int binary_hash_2(void * binary) { return *(ARCH_WORD_32 *) binary & 0xFFF; }
static int binary_hash_3(void * binary) { return *(ARCH_WORD_32 *) binary & 0xFFFF; }
static int binary_hash_4(void * binary) { return *(ARCH_WORD_32 *) binary & 0xFFFFF; }
static int binary_hash_5(void * binary) { return *(ARCH_WORD_32 *) binary & 0xFFFFFF; }
static int binary_hash_6(void * binary) { return *(ARCH_WORD_32 *) binary & 0x7FFFFFF; }
//Get Hash functions group.
static int get_hash_0(int index) {
#ifdef DEBUG
print_hash(index);
#endif
return calculated_hash[index] & 0xF;
}
static int get_hash_1(int index) { return calculated_hash[index] & 0xFF; }
static int get_hash_2(int index) { return calculated_hash[index] & 0xFFF; }
static int get_hash_3(int index) { return calculated_hash[index] & 0xFFFF; }
static int get_hash_4(int index) { return calculated_hash[index] & 0xFFFFF; }
static int get_hash_5(int index) { return calculated_hash[index] & 0xFFFFFF; }
static int get_hash_6(int index) { return calculated_hash[index] & 0x7FFFFFF; }
/* ------- Format structure ------- */
struct fmt_main fmt_opencl_rawsha256 = {
{
FORMAT_LABEL,
FORMAT_NAME,
ALGORITHM_NAME,
BENCHMARK_COMMENT,
BENCHMARK_LENGTH,
PLAINTEXT_LENGTH - 1,
BINARY_SIZE,
#if FMT_MAIN_VERSION > 9
4,
#endif
SALT_SIZE,
#if FMT_MAIN_VERSION > 9
1,
#endif
MIN_KEYS_PER_CRYPT,
MAX_KEYS_PER_CRYPT,
FMT_CASE | FMT_8_BIT,
tests
}, {
init,
done,
fmt_default_prepare,
valid,
split,
get_binary,
fmt_default_salt,
#if FMT_MAIN_VERSION > 9
fmt_default_source,
#endif
{
binary_hash_0,
binary_hash_1,
binary_hash_2,
binary_hash_3,
binary_hash_4,
binary_hash_5,
binary_hash_6
},
fmt_default_salt_hash,
fmt_default_set_salt,
set_key,
get_key,
fmt_default_clear_keys,
crypt_all,
{
get_hash_0,
get_hash_1,
get_hash_2,
get_hash_3,
get_hash_4,
get_hash_5,
get_hash_6
},
cmp_all,
cmp_one,
cmp_exact
}
};