1 /*
2  * This file is part of John the Ripper password cracker,
3  * Copyright (c) 2012 by Sayantan Datta <std2048 at gmail dot com>
4  * It is hereby released to the general public under the following terms:
5  * Redistribution and use in source and binary forms, with or without modification, are permitted.
6  */
7 
8 #ifdef HAVE_OPENCL
9 
10 #if FMT_EXTERNS_H
11 extern struct fmt_main fmt_opencl_bf;
12 #elif FMT_REGISTERS_H
13 john_register_one(&fmt_opencl_bf);
14 #else
15 
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include "arch.h"
20 #include "misc.h"
21 #include "opencl_bf_std.h"
22 #include "common.h"
23 #include "formats.h"
24 #include "config.h"
25 #include "BF_common.h"
26 
27 #define FORMAT_LABEL			"bcrypt-opencl"
28 #define FORMAT_NAME			""
29 
30 #define ALGORITHM_NAME			"Blowfish OpenCL"
31 
32 #define BENCHMARK_COMMENT		" (\"$2a$05\", 32 iterations)"
33 #define BENCHMARK_LENGTH		0x107
34 
35 #define PLAINTEXT_LENGTH		72
36 //#define CIPHERTEXT_LENGTH		60 // in BF_commmon.h
37 
38 #define BINARY_SIZE			4
39 #define BINARY_ALIGN			4
40 #define SALT_SIZE			sizeof(BF_salt)
41 #define SALT_ALIGN			4
42 
43 #define MIN_KEYS_PER_CRYPT		DEFAULT_LWS
44 #define MAX_KEYS_PER_CRYPT		BF_N
45 
46 // static struct fmt_tests BF_common_tests[] = {  // defined in BF_common.c
47 
48 static char 	(*saved_key)[PLAINTEXT_LENGTH + 1];
49 static char 	keys_mode;
50 static int 	sign_extension_bug;
51 static BF_salt 	saved_salt;
52 
done(void)53 static void done(void) {
54 	BF_clear_buffer();
55 	MEM_FREE(saved_key);
56 }
57 
init(struct fmt_main * self)58 static void init(struct fmt_main *self) {
59 	saved_key = mem_calloc(BF_N, sizeof(*saved_key));
60 	global_work_size = 0;
61 
62 	//Prepare OpenCL environment.
63 	opencl_load_environment();
64 
65 	// Check if specific LWS/GWS was requested
66 	opencl_get_user_preferences(FORMAT_LABEL);
67 
68 	// BF_select_device(platform,device);
69 	//platform_id = get_platform_id(gpu_id);
70         BF_select_device(self);
71 	keys_mode = 'a';
72 	sign_extension_bug = 0;
73 	//fprintf(stderr, "****Please see 'opencl_bf_std.h' for device specific optimizations****\n");
74 }
75 
get_hash_0(int index)76 static int get_hash_0(int index) {
77 	return opencl_BF_out[index][0] & PH_MASK_0;
78 }
79 
get_hash_1(int index)80 static int get_hash_1(int index) {
81 	return opencl_BF_out[index][0] & PH_MASK_1;
82 }
83 
get_hash_2(int index)84 static int get_hash_2(int index) {
85 	return opencl_BF_out[index][0] & PH_MASK_2;
86 }
87 
get_hash_3(int index)88 static int get_hash_3(int index) {
89 	return opencl_BF_out[index][0] & PH_MASK_3;
90 }
91 
get_hash_4(int index)92 static int get_hash_4(int index) {
93 	return opencl_BF_out[index][0] & PH_MASK_4;
94 }
95 
get_hash_5(int index)96 static int get_hash_5(int index) {
97 	return opencl_BF_out[index][0] & PH_MASK_5;
98 }
99 
get_hash_6(int index)100 static int get_hash_6(int index) {
101 	return opencl_BF_out[index][0] & PH_MASK_6;
102 }
103 
salt_hash(void * salt)104 static int salt_hash(void *salt) {
105 	return ((BF_salt *)salt)->salt[0] & (SALT_HASH_SIZE - 1);
106 }
107 
set_salt(void * salt)108 static void set_salt(void *salt) {
109 	memcpy(&saved_salt, salt, sizeof(saved_salt));
110 }
111 
set_key(char * key,int index)112 static void set_key(char *key, int index) {
113 	opencl_BF_std_set_key(key, index, sign_extension_bug);
114 	strnzcpy(saved_key[index], key, PLAINTEXT_LENGTH + 1);
115 }
116 
get_key(int index)117 static char *get_key(int index) {
118 	return saved_key[index];
119 }
120 
crypt_all(int * pcount,struct db_salt * salt)121 static int crypt_all(int *pcount, struct db_salt *salt) {
122 	const int count = *pcount;
123 	if (keys_mode != saved_salt.subtype) {
124 		int i;
125 
126 		keys_mode = saved_salt.subtype;
127 		sign_extension_bug = (keys_mode == 'x');
128 		for (i = 0; i < count; i++)
129 			opencl_BF_std_set_key(saved_key[i], i, sign_extension_bug);
130 	}
131 
132 	opencl_BF_std_crypt(&saved_salt, count);
133 	return count;
134 }
135 
cmp_all(void * binary,int count)136 static int cmp_all(void *binary, int count) {
137 	int i;
138 	for (i = 0; i < count; i++)
139 		if (*(BF_word *)binary == opencl_BF_out[i][0])
140 			return 1;
141 	return 0;
142 }
143 
cmp_one(void * binary,int index)144 static int cmp_one(void *binary, int index)
145 {
146 	return *(BF_word *)binary == opencl_BF_out[index][0];
147 }
148 
cmp_exact(char * source,int index)149 static int cmp_exact(char *source, int index)
150 {
151 	opencl_BF_std_crypt_exact(index);
152 
153 	return !memcmp(BF_common_get_binary(source), opencl_BF_out[index],
154 	    sizeof(BF_binary));
155 }
156 
157 struct fmt_main fmt_opencl_bf = {
158 	{
159 		FORMAT_LABEL,
160 		FORMAT_NAME,
161 		ALGORITHM_NAME,
162 		BENCHMARK_COMMENT,
163 		BENCHMARK_LENGTH,
164 		0,
165 		PLAINTEXT_LENGTH,
166 		BINARY_SIZE,
167 		BINARY_ALIGN,
168 		SALT_SIZE,
169 		SALT_ALIGN,
170 		MIN_KEYS_PER_CRYPT,
171 		MAX_KEYS_PER_CRYPT,
172 		FMT_CASE | FMT_8_BIT,
173 		{
174 			"iteration count",
175 		},
176 		{
177 			FORMAT_TAG,
178 			FORMAT_TAG2,
179 			FORMAT_TAG3,
180 			FORMAT_TAG4
181 		},
182 		BF_common_tests
183 	}, {
184 		init,
185 		done,
186 		fmt_default_reset,
187 		fmt_default_prepare,
188 		BF_common_valid,
189 		fmt_default_split,
190 		BF_common_get_binary,
191 		BF_common_get_salt,
192 		{
193 			BF_common_iteration_count,
194 		},
195 		fmt_default_source,
196 		{
197 			fmt_default_binary_hash_0,
198 			fmt_default_binary_hash_1,
199 			fmt_default_binary_hash_2,
200 			fmt_default_binary_hash_3,
201 			fmt_default_binary_hash_4,
202 			fmt_default_binary_hash_5,
203 			fmt_default_binary_hash_6
204 		},
205 		salt_hash,
206 		NULL,
207 		set_salt,
208 		set_key,
209 		get_key,
210 		fmt_default_clear_keys,
211 		crypt_all,
212 		{
213 			get_hash_0,
214 			get_hash_1,
215 			get_hash_2,
216 			get_hash_3,
217 			get_hash_4,
218 			get_hash_5,
219 			get_hash_6
220 		},
221 		cmp_all,
222 		cmp_one,
223 		cmp_exact
224 	}
225 };
226 
227 #endif /* plugin stanza */
228 
229 #endif /* HAVE_OPENCL */
230