Skip to content

Commit f0c9fe7

Browse files
committed
Cryptosafe-opencl: Use shared macros for buffer creation
1 parent 23d19f3 commit f0c9fe7

File tree

2 files changed

+43
-75
lines changed

2 files changed

+43
-75
lines changed

src/opencl_cryptosafe_fmt_plug.c

Lines changed: 42 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This software is Copyright (c) 2021, magnum
2+
* This software is Copyright (c) 2021-2025, magnum
33
* and it is hereby released to the general public under the following terms:
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted.
@@ -30,6 +30,7 @@ john_register_one(&FORMAT_STRUCT);
3030
#include "options.h"
3131
#include "cryptosafe_common.h"
3232
#include "mask_ext.h"
33+
#include "opencl_helper_macros.h"
3334

3435
#define FORMAT_LABEL "cryptosafe-opencl"
3536
#define FORMAT_NAME ""
@@ -40,16 +41,16 @@ john_register_one(&FORMAT_STRUCT);
4041

4142
static struct custom_salt *cur_salt;
4243

43-
static char *saved_key;
44+
static char *keys;
4445
static int new_keys;
4546

46-
static unsigned int *saved_idx, key_idx;
47+
static unsigned int *idx, key_idx;
4748
static unsigned int *cracked, crack_count_ret;
4849
static size_t key_offset, idx_offset;
49-
static cl_mem cl_saved_key, cl_saved_idx, cl_salt, cl_result, cl_crack_count_ret;
50-
static cl_mem pinned_key, pinned_idx, pinned_result;
51-
static cl_mem pinned_int_key_loc, buffer_int_keys, buffer_int_key_loc;
52-
static cl_uint *saved_int_key_loc;
50+
static cl_mem cl_keys, cl_idx, cl_salt, cl_cracked, cl_crack_count_ret;
51+
static cl_mem pinned_keys, pinned_idx, pinned_cracked;
52+
static cl_mem pinned_int_key_loc, cl_int_keys, cl_int_key_loc;
53+
static cl_uint *int_key_loc;
5354
static int static_gpu_locations[MASK_FMT_INT_PLHDR];
5455
static struct fmt_main *self;
5556

@@ -77,26 +78,10 @@ static void create_clobj(size_t gws, struct fmt_main *self)
7778

7879
release_clobj();
7980

80-
pinned_key = clCreateBuffer(context[gpu_id], CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR, PLAINTEXT_LENGTH * gws, NULL, &ret_code);
81-
HANDLE_CLERROR(ret_code, "Error creating page-locked buffer");
82-
cl_saved_key = clCreateBuffer(context[gpu_id], CL_MEM_READ_ONLY, PLAINTEXT_LENGTH * gws, NULL, &ret_code);
83-
HANDLE_CLERROR(ret_code, "Error creating device buffer");
84-
saved_key = clEnqueueMapBuffer(queue[gpu_id], pinned_key, CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, PLAINTEXT_LENGTH * gws, 0, NULL, NULL, &ret_code);
85-
HANDLE_CLERROR(ret_code, "Error mapping saved_key");
86-
87-
pinned_idx = clCreateBuffer(context[gpu_id], CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR, sizeof(cl_uint) * (gws + 1), NULL, &ret_code);
88-
HANDLE_CLERROR(ret_code, "Error creating page-locked buffer");
89-
cl_saved_idx = clCreateBuffer(context[gpu_id], CL_MEM_READ_ONLY, sizeof(cl_uint) * (gws + 1), NULL, &ret_code);
90-
HANDLE_CLERROR(ret_code, "Error creating device buffer");
91-
saved_idx = clEnqueueMapBuffer(queue[gpu_id], pinned_idx, CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, sizeof(cl_uint) * (gws + 1), 0, NULL, NULL, &ret_code);
92-
HANDLE_CLERROR(ret_code, "Error mapping saved_idx");
93-
94-
pinned_result = clCreateBuffer(context[gpu_id], CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, sizeof(unsigned int) * gws * mask_int_cand.num_int_cand, NULL, &ret_code);
95-
HANDLE_CLERROR(ret_code, "Error creating page-locked buffer");
96-
cl_result = clCreateBuffer(context[gpu_id], CL_MEM_READ_WRITE, sizeof(unsigned int) * gws * mask_int_cand.num_int_cand, NULL, &ret_code);
97-
HANDLE_CLERROR(ret_code, "Error creating device buffer");
98-
cracked = clEnqueueMapBuffer(queue[gpu_id], pinned_result, CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, sizeof(unsigned int) * gws * mask_int_cand.num_int_cand, 0, NULL, NULL, &ret_code);
99-
HANDLE_CLERROR(ret_code, "Error mapping cracked");
81+
CLCREATEPINNED(keys, CL_RO, PLAINTEXT_LENGTH * gws);
82+
CLCREATEPINNED(idx, CL_RO, sizeof(cl_uint) * (gws + 1));
83+
CLCREATEPINNED(cracked, CL_RW, sizeof(unsigned int) * gws * mask_int_cand.num_int_cand);
84+
CLCREATEPINNED(int_key_loc, CL_RO, sizeof(cl_uint) * gws);
10085

10186
cl_salt = clCreateBuffer(context[gpu_id], CL_MEM_READ_ONLY, sizeof(struct custom_salt), NULL, &ret_code);
10287
HANDLE_CLERROR(ret_code, "Error creating device buffer");
@@ -106,46 +91,29 @@ static void create_clobj(size_t gws, struct fmt_main *self)
10691
crack_count_ret = 0;
10792
HANDLE_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_crack_count_ret, CL_FALSE, 0, sizeof(cl_uint), &crack_count_ret, 0, NULL, NULL), "Failed resetting crack return");
10893

109-
pinned_int_key_loc = clCreateBuffer(context[gpu_id], CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR, sizeof(cl_uint) * gws, NULL, &ret_code);
110-
HANDLE_CLERROR(ret_code, "Error creating page-locked memory pinned_int_key_loc.");
111-
saved_int_key_loc = (cl_uint *) clEnqueueMapBuffer(queue[gpu_id], pinned_int_key_loc, CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, sizeof(cl_uint) * gws, 0, NULL, NULL, &ret_code);
112-
HANDLE_CLERROR(ret_code, "Error mapping page-locked memory saved_int_key_loc.");
113-
114-
buffer_int_key_loc = clCreateBuffer(context[gpu_id], CL_MEM_READ_ONLY, sizeof(cl_uint) * gws, NULL, &ret_code);
115-
HANDLE_CLERROR(ret_code, "Error creating buffer_int_key_loc.");
94+
cl_int_keys = clCreateBuffer(context[gpu_id], CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, 4 * mask_int_cand.num_int_cand, mask_int_cand.int_cand ? mask_int_cand.int_cand : (void *)&dummy, &ret_code);
95+
HANDLE_CLERROR(ret_code, "Error creating buffer argument cl_int_keys.");
11696

117-
buffer_int_keys = clCreateBuffer(context[gpu_id], CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, 4 * mask_int_cand.num_int_cand, mask_int_cand.int_cand ? mask_int_cand.int_cand : (void *)&dummy, &ret_code);
118-
HANDLE_CLERROR(ret_code, "Error creating buffer argument buffer_int_keys.");
119-
120-
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 0, sizeof(cl_mem), (void*)&cl_saved_key), "Error setting argument 0");
121-
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 1, sizeof(cl_mem), (void*)&cl_saved_idx), "Error setting argument 1");
97+
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 0, sizeof(cl_mem), (void*)&cl_keys), "Error setting argument 0");
98+
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 1, sizeof(cl_mem), (void*)&cl_idx), "Error setting argument 1");
12299
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 2, sizeof(cl_mem), (void*)&cl_salt), "Error setting argument 2");
123-
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 3, sizeof(cl_mem), (void*)&cl_result), "Error setting argument 3");
100+
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 3, sizeof(cl_mem), (void*)&cl_cracked), "Error setting argument 3");
124101
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 4, sizeof(cl_mem), (void*)&cl_crack_count_ret), "Error setting argument 4");
125-
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 5, sizeof(buffer_int_key_loc), (void *) &buffer_int_key_loc), "Error setting argument 5.");
126-
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 6, sizeof(buffer_int_keys), (void *) &buffer_int_keys), "Error setting argument 6.");
102+
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 5, sizeof(cl_int_key_loc), (void*)&cl_int_key_loc), "Error setting argument 5.");
103+
HANDLE_CLERROR(clSetKernelArg(crypt_kernel, 6, sizeof(cl_int_keys), (void*)&cl_int_keys), "Error setting argument 6.");
127104
}
128105

129106
static void release_clobj(void)
130107
{
131108
if (cl_salt) {
132-
HANDLE_CLERROR(clEnqueueUnmapMemObject(queue[gpu_id], pinned_result, cracked, 0, NULL, NULL), "Error Unmapping cracked");
133-
HANDLE_CLERROR(clEnqueueUnmapMemObject(queue[gpu_id], pinned_key, saved_key, 0, NULL, NULL), "Error Unmapping saved_key");
134-
HANDLE_CLERROR(clEnqueueUnmapMemObject(queue[gpu_id], pinned_idx, saved_idx, 0, NULL, NULL), "Error Unmapping saved_idx");
135-
HANDLE_CLERROR(clEnqueueUnmapMemObject(queue[gpu_id], pinned_int_key_loc, saved_int_key_loc, 0, NULL, NULL), "Error Unmapping saved_int_key_loc.");
136-
HANDLE_CLERROR(clFinish(queue[gpu_id]), "Error releasing memory mappings");
137-
138-
HANDLE_CLERROR(clReleaseMemObject(pinned_result), "Release pinned result buffer");
139-
HANDLE_CLERROR(clReleaseMemObject(pinned_key), "Release pinned key buffer");
140-
HANDLE_CLERROR(clReleaseMemObject(pinned_idx), "Release pinned index buffer");
109+
CLRELEASEPINNED(keys);
110+
CLRELEASEPINNED(idx);
111+
CLRELEASEPINNED(cracked);
112+
CLRELEASEPINNED(int_key_loc);
113+
141114
HANDLE_CLERROR(clReleaseMemObject(cl_crack_count_ret), "Release crack flag buffer");
142115
HANDLE_CLERROR(clReleaseMemObject(cl_salt), "Release salt buffer");
143-
HANDLE_CLERROR(clReleaseMemObject(cl_result), "Release result buffer");
144-
HANDLE_CLERROR(clReleaseMemObject(cl_saved_key), "Release key buffer");
145-
HANDLE_CLERROR(clReleaseMemObject(cl_saved_idx), "Release index buffer");
146-
HANDLE_CLERROR(clReleaseMemObject(buffer_int_keys), "Error Releasing buffer_int_keys.");
147-
HANDLE_CLERROR(clReleaseMemObject(buffer_int_key_loc), "Error Releasing buffer_int_key_loc.");
148-
HANDLE_CLERROR(clReleaseMemObject(pinned_int_key_loc), "Error Releasing pinned_int_key_loc.");
116+
HANDLE_CLERROR(clReleaseMemObject(cl_int_keys), "Error Releasing cl_int_keys.");
149117

150118
cl_salt = NULL;
151119
}
@@ -242,7 +210,7 @@ static void reset(struct db_main *db)
242210
static void clear_keys(void)
243211
{
244212
key_idx = 0;
245-
saved_idx[0] = 0;
213+
idx[0] = 0;
246214
key_offset = 0;
247215
idx_offset = 0;
248216
}
@@ -252,32 +220,32 @@ static void set_key(char *key, int index)
252220
if (mask_int_cand.num_int_cand > 1 && !mask_gpu_is_static) {
253221
int i;
254222

255-
saved_int_key_loc[index] = 0;
223+
int_key_loc[index] = 0;
256224
for (i = 0; i < MASK_FMT_INT_PLHDR; i++) {
257225
if (mask_skip_ranges[i] != -1) {
258-
saved_int_key_loc[index] |= ((mask_int_cand.
226+
int_key_loc[index] |= ((mask_int_cand.
259227
int_cpu_mask_ctx->ranges[mask_skip_ranges[i]].offset +
260228
mask_int_cand.int_cpu_mask_ctx->
261229
ranges[mask_skip_ranges[i]].pos) & 0xff) << (i << 3);
262230
}
263231
else
264-
saved_int_key_loc[index] |= 0x80 << (i << 3);
232+
int_key_loc[index] |= 0x80 << (i << 3);
265233
}
266234
}
267235

268236
while (*key)
269-
saved_key[key_idx++] = *key++;
237+
keys[key_idx++] = *key++;
270238

271-
saved_idx[index + 1] = key_idx;
239+
idx[index + 1] = key_idx;
272240
new_keys = 1;
273241

274242
/* Early partial transfer to GPU */
275243
if (index && !(index & (256*1024 - 1))) {
276-
HANDLE_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_saved_key, CL_FALSE, key_offset, key_idx - key_offset, saved_key + key_offset, 0, NULL, NULL), "Failed transferring keys");
277-
HANDLE_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_saved_idx, CL_FALSE, idx_offset, 4 * (index + 2) - idx_offset, saved_idx + (idx_offset / 4), 0, NULL, NULL), "Failed transferring index");
244+
HANDLE_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_keys, CL_FALSE, key_offset, key_idx - key_offset, keys + key_offset, 0, NULL, NULL), "Failed transferring keys");
245+
HANDLE_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_idx, CL_FALSE, idx_offset, 4 * (index + 2) - idx_offset, idx + (idx_offset / 4), 0, NULL, NULL), "Failed transferring index");
278246

279247
if (!mask_gpu_is_static)
280-
HANDLE_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], buffer_int_key_loc, CL_FALSE, idx_offset, 4 * (index + 1) - idx_offset, saved_int_key_loc + (idx_offset / 4), 0, NULL, NULL), "failed transferring buffer_int_key_loc.");
248+
HANDLE_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_int_key_loc, CL_FALSE, idx_offset, 4 * (index + 1) - idx_offset, int_key_loc + (idx_offset / 4), 0, NULL, NULL), "failed transferring cl_int_key_loc.");
281249

282250
HANDLE_CLERROR(clFlush(queue[gpu_id]), "failed in clFlush");
283251

@@ -302,8 +270,8 @@ static char *get_key(int index)
302270
else if (t >= global_work_size)
303271
t = 0;
304272

305-
len = saved_idx[t + 1] - saved_idx[t];
306-
key = (char*)&saved_key[saved_idx[t]];
273+
len = idx[t + 1] - idx[t];
274+
key = (char*)&keys[idx[t]];
307275

308276
for (i = 0; i < len; i++)
309277
out[i] = *key++;
@@ -316,7 +284,7 @@ static char *get_key(int index)
316284
out[static_gpu_locations[i]] =
317285
mask_int_cand.int_cand[int_index].x[i];
318286
else
319-
out[(saved_int_key_loc[t] & (0xff << (i * 8))) >> (i * 8)] =
287+
out[(int_key_loc[t] & (0xff << (i * 8))) >> (i * 8)] =
320288
mask_int_cand.int_cand[int_index].x[i];
321289
}
322290

@@ -348,13 +316,13 @@ static int crypt_all(int *pcount, struct db_salt *salt)
348316

349317
/* Safety for when count < GWS */
350318
for (int i = count; i <= gws; i++)
351-
saved_idx[i] = key_idx;
319+
idx[i] = key_idx;
352320

353-
BENCH_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_saved_key, CL_FALSE, key_offset, key_idx - key_offset, saved_key + key_offset, 0, NULL, multi_profilingEvent[0]), "Failed transferring keys");
354-
BENCH_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_saved_idx, CL_FALSE, idx_offset, 4 * (gws + 1) - idx_offset, saved_idx + (idx_offset / 4), 0, NULL, multi_profilingEvent[1]), "Failed transferring index");
321+
BENCH_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_keys, CL_FALSE, key_offset, key_idx - key_offset, keys + key_offset, 0, NULL, multi_profilingEvent[0]), "Failed transferring keys");
322+
BENCH_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_idx, CL_FALSE, idx_offset, 4 * (gws + 1) - idx_offset, idx + (idx_offset / 4), 0, NULL, multi_profilingEvent[1]), "Failed transferring index");
355323

356324
if (!mask_gpu_is_static)
357-
BENCH_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], buffer_int_key_loc, CL_FALSE, idx_offset, 4 * gws - idx_offset, saved_int_key_loc + (idx_offset / 4), 0, NULL, NULL), "failed transferring buffer_int_key_loc.");
325+
BENCH_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_int_key_loc, CL_FALSE, idx_offset, 4 * gws - idx_offset, int_key_loc + (idx_offset / 4), 0, NULL, NULL), "failed transferring cl_int_key_loc.");
358326

359327
new_keys = 0;
360328
}
@@ -368,7 +336,7 @@ static int crypt_all(int *pcount, struct db_salt *salt)
368336
if (crack_count_ret > *pcount)
369337
crack_count_ret = *pcount;
370338

371-
BENCH_CLERROR(clEnqueueReadBuffer(queue[gpu_id], cl_result, CL_TRUE, 0, sizeof(unsigned int) * crack_count_ret, cracked, 0, NULL, NULL), "failed reading results back");
339+
BENCH_CLERROR(clEnqueueReadBuffer(queue[gpu_id], cl_cracked, CL_TRUE, 0, sizeof(unsigned int) * crack_count_ret, cracked, 0, NULL, NULL), "failed reading results back");
372340

373341
static const cl_uint zero = 0;
374342
HANDLE_CLERROR(clEnqueueWriteBuffer(queue[gpu_id], cl_crack_count_ret, CL_FALSE, 0, sizeof(cl_uint), &zero, 0, NULL, NULL), "Failed resetting crack return");

src/opencl_helper_macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@
9090
HANDLE_CLERROR(clEnqueueUnmapMemObject(queue[gpu_id], pinned_##VAR, VAR, 0, NULL, NULL), \
9191
"Error Unmapping buffer"); \
9292
CLFINISH(); \
93-
VAR = NULL; \
9493
HANDLE_CLERROR(clReleaseMemObject(pinned_##VAR), "Error releasing pinned buffer"); \
9594
pinned_##VAR = NULL; \
95+
VAR = NULL; \
9696
} else \
9797
MEM_FREE(VAR); \
9898
HANDLE_CLERROR(clReleaseMemObject(cl_##VAR), "Error releasing buffer"); \

0 commit comments

Comments
 (0)