2 * Author......: Jens Steube <jens.steube@gmail.com>
8 #include "inc_vendor.cl"
9 #include "inc_hash_constants.h"
10 #include "inc_hash_functions.cl"
11 #include "inc_types.cl"
12 #include "inc_common.cl"
14 #define COMPARE_S "inc_comp_single.cl"
15 #define COMPARE_M "inc_comp_multi.cl"
20 #define STATE_DECL volatile
26 #define PUTCHAR64_BE(a,p,c) ((u8 *)(a))[(p) ^ 7] = (u8) (c)
27 #define GETCHAR64_BE(a,p) ((u8 *)(a))[(p) ^ 7]
37 __constant u64 k_sha512[80] =
39 SHA512C00, SHA512C01, SHA512C02, SHA512C03,
40 SHA512C04, SHA512C05, SHA512C06, SHA512C07,
41 SHA512C08, SHA512C09, SHA512C0a, SHA512C0b,
42 SHA512C0c, SHA512C0d, SHA512C0e, SHA512C0f,
43 SHA512C10, SHA512C11, SHA512C12, SHA512C13,
44 SHA512C14, SHA512C15, SHA512C16, SHA512C17,
45 SHA512C18, SHA512C19, SHA512C1a, SHA512C1b,
46 SHA512C1c, SHA512C1d, SHA512C1e, SHA512C1f,
47 SHA512C20, SHA512C21, SHA512C22, SHA512C23,
48 SHA512C24, SHA512C25, SHA512C26, SHA512C27,
49 SHA512C28, SHA512C29, SHA512C2a, SHA512C2b,
50 SHA512C2c, SHA512C2d, SHA512C2e, SHA512C2f,
51 SHA512C30, SHA512C31, SHA512C32, SHA512C33,
52 SHA512C34, SHA512C35, SHA512C36, SHA512C37,
53 SHA512C38, SHA512C39, SHA512C3a, SHA512C3b,
54 SHA512C3c, SHA512C3d, SHA512C3e, SHA512C3f,
55 SHA512C40, SHA512C41, SHA512C42, SHA512C43,
56 SHA512C44, SHA512C45, SHA512C46, SHA512C47,
57 SHA512C48, SHA512C49, SHA512C4a, SHA512C4b,
58 SHA512C4c, SHA512C4d, SHA512C4e, SHA512C4f,
61 void sha512_transform (const u64 w[16], u64 digest[8])
80 STATE_DECL u64 a = digest[0];
81 STATE_DECL u64 b = digest[1];
82 STATE_DECL u64 c = digest[2];
83 STATE_DECL u64 d = digest[3];
84 STATE_DECL u64 e = digest[4];
85 STATE_DECL u64 f = digest[5];
86 STATE_DECL u64 g = digest[6];
87 STATE_DECL u64 h = digest[7];
89 #define ROUND_EXPAND() \
91 w0_t = SHA512_EXPAND (we_t, w9_t, w1_t, w0_t); \
92 w1_t = SHA512_EXPAND (wf_t, wa_t, w2_t, w1_t); \
93 w2_t = SHA512_EXPAND (w0_t, wb_t, w3_t, w2_t); \
94 w3_t = SHA512_EXPAND (w1_t, wc_t, w4_t, w3_t); \
95 w4_t = SHA512_EXPAND (w2_t, wd_t, w5_t, w4_t); \
96 w5_t = SHA512_EXPAND (w3_t, we_t, w6_t, w5_t); \
97 w6_t = SHA512_EXPAND (w4_t, wf_t, w7_t, w6_t); \
98 w7_t = SHA512_EXPAND (w5_t, w0_t, w8_t, w7_t); \
99 w8_t = SHA512_EXPAND (w6_t, w1_t, w9_t, w8_t); \
100 w9_t = SHA512_EXPAND (w7_t, w2_t, wa_t, w9_t); \
101 wa_t = SHA512_EXPAND (w8_t, w3_t, wb_t, wa_t); \
102 wb_t = SHA512_EXPAND (w9_t, w4_t, wc_t, wb_t); \
103 wc_t = SHA512_EXPAND (wa_t, w5_t, wd_t, wc_t); \
104 wd_t = SHA512_EXPAND (wb_t, w6_t, we_t, wd_t); \
105 we_t = SHA512_EXPAND (wc_t, w7_t, wf_t, we_t); \
106 wf_t = SHA512_EXPAND (wd_t, w8_t, w0_t, wf_t); \
109 #define ROUND_STEP(i) \
111 SHA512_STEP (SHA512_F0o, SHA512_F1o, a, b, c, d, e, f, g, h, w0_t, k_sha512[i + 0]); \
112 SHA512_STEP (SHA512_F0o, SHA512_F1o, h, a, b, c, d, e, f, g, w1_t, k_sha512[i + 1]); \
113 SHA512_STEP (SHA512_F0o, SHA512_F1o, g, h, a, b, c, d, e, f, w2_t, k_sha512[i + 2]); \
114 SHA512_STEP (SHA512_F0o, SHA512_F1o, f, g, h, a, b, c, d, e, w3_t, k_sha512[i + 3]); \
115 SHA512_STEP (SHA512_F0o, SHA512_F1o, e, f, g, h, a, b, c, d, w4_t, k_sha512[i + 4]); \
116 SHA512_STEP (SHA512_F0o, SHA512_F1o, d, e, f, g, h, a, b, c, w5_t, k_sha512[i + 5]); \
117 SHA512_STEP (SHA512_F0o, SHA512_F1o, c, d, e, f, g, h, a, b, w6_t, k_sha512[i + 6]); \
118 SHA512_STEP (SHA512_F0o, SHA512_F1o, b, c, d, e, f, g, h, a, w7_t, k_sha512[i + 7]); \
119 SHA512_STEP (SHA512_F0o, SHA512_F1o, a, b, c, d, e, f, g, h, w8_t, k_sha512[i + 8]); \
120 SHA512_STEP (SHA512_F0o, SHA512_F1o, h, a, b, c, d, e, f, g, w9_t, k_sha512[i + 9]); \
121 SHA512_STEP (SHA512_F0o, SHA512_F1o, g, h, a, b, c, d, e, f, wa_t, k_sha512[i + 10]); \
122 SHA512_STEP (SHA512_F0o, SHA512_F1o, f, g, h, a, b, c, d, e, wb_t, k_sha512[i + 11]); \
123 SHA512_STEP (SHA512_F0o, SHA512_F1o, e, f, g, h, a, b, c, d, wc_t, k_sha512[i + 12]); \
124 SHA512_STEP (SHA512_F0o, SHA512_F1o, d, e, f, g, h, a, b, c, wd_t, k_sha512[i + 13]); \
125 SHA512_STEP (SHA512_F0o, SHA512_F1o, c, d, e, f, g, h, a, b, we_t, k_sha512[i + 14]); \
126 SHA512_STEP (SHA512_F0o, SHA512_F1o, b, c, d, e, f, g, h, a, wf_t, k_sha512[i + 15]); \
134 for (int i = 16; i < 80; i += 16)
136 ROUND_EXPAND (); ROUND_STEP (i);
149 void sha512_init (sha512_ctx_t *sha512_ctx)
151 sha512_ctx->state[0] = SHA512M_A;
152 sha512_ctx->state[1] = SHA512M_B;
153 sha512_ctx->state[2] = SHA512M_C;
154 sha512_ctx->state[3] = SHA512M_D;
155 sha512_ctx->state[4] = SHA512M_E;
156 sha512_ctx->state[5] = SHA512M_F;
157 sha512_ctx->state[6] = SHA512M_G;
158 sha512_ctx->state[7] = SHA512M_H;
163 void sha512_update (sha512_ctx_t *sha512_ctx, const u64 *buf, int len)
165 int pos = sha512_ctx->len & 0x7f;
167 sha512_ctx->len += len;
169 if ((pos + len) < 128)
171 for (int i = 0; i < len; i++)
173 PUTCHAR64_BE (sha512_ctx->buf, pos++, GETCHAR64_BE (buf, i));
181 for (int i = 0; i < cnt; i++)
183 PUTCHAR64_BE (sha512_ctx->buf, pos++, GETCHAR64_BE (buf, i));
186 sha512_transform (sha512_ctx->buf, sha512_ctx->state);
190 for (int i = 0; i < len; i++)
192 PUTCHAR64_BE (sha512_ctx->buf, i, GETCHAR64_BE (buf, cnt + i));
196 void sha512_final (sha512_ctx_t *sha512_ctx)
198 int pos = sha512_ctx->len & 0x7f;
200 for (int i = pos; i < 128; i++)
202 PUTCHAR64_BE (sha512_ctx->buf, i, 0);
205 PUTCHAR64_BE (sha512_ctx->buf, pos, 0x80);
209 sha512_transform (sha512_ctx->buf, sha512_ctx->state);
211 sha512_ctx->buf[ 0] = 0;
212 sha512_ctx->buf[ 1] = 0;
213 sha512_ctx->buf[ 2] = 0;
214 sha512_ctx->buf[ 3] = 0;
215 sha512_ctx->buf[ 4] = 0;
216 sha512_ctx->buf[ 5] = 0;
217 sha512_ctx->buf[ 6] = 0;
218 sha512_ctx->buf[ 7] = 0;
219 sha512_ctx->buf[ 8] = 0;
220 sha512_ctx->buf[ 9] = 0;
221 sha512_ctx->buf[10] = 0;
222 sha512_ctx->buf[11] = 0;
223 sha512_ctx->buf[12] = 0;
224 sha512_ctx->buf[13] = 0;
225 sha512_ctx->buf[14] = 0;
226 sha512_ctx->buf[15] = 0;
229 sha512_ctx->buf[15] = sha512_ctx->len * 8;
231 sha512_transform (sha512_ctx->buf, sha512_ctx->state);
234 __kernel void m01800_init (__global pw_t *pws, __global kernel_rule_t *rules_buf, __global comb_t *combs_buf, __global bf_t *bfs_buf, __global sha512crypt_tmp_t *tmps, __global void *hooks, __global u32 *bitmaps_buf_s1_a, __global u32 *bitmaps_buf_s1_b, __global u32 *bitmaps_buf_s1_c, __global u32 *bitmaps_buf_s1_d, __global u32 *bitmaps_buf_s2_a, __global u32 *bitmaps_buf_s2_b, __global u32 *bitmaps_buf_s2_c, __global u32 *bitmaps_buf_s2_d, __global plain_t *plains_buf, __global digest_t *digests_buf, __global u32 *hashes_shown, __global salt_t *salt_bufs, __global void *esalt_bufs, __global u32 *d_return_buf, __global u32 *d_scryptV0_buf, __global u32 *d_scryptV1_buf, __global u32 *d_scryptV2_buf, __global u32 *d_scryptV3_buf, const u32 bitmap_mask, const u32 bitmap_shift1, const u32 bitmap_shift2, const u32 salt_pos, const u32 loop_pos, const u32 loop_cnt, const u32 il_cnt, const u32 digests_cnt, const u32 digests_offset, const u32 combs_mode, const u32 gid_max)
240 const u32 gid = get_global_id (0);
242 if (gid >= gid_max) return;
246 w0[0] = pws[gid].i[0];
247 w0[1] = pws[gid].i[1];
248 w0[2] = pws[gid].i[2];
249 w0[3] = pws[gid].i[3];
251 const u32 pw_len = pws[gid].pw_len;
259 salt_buf[0] = salt_bufs[salt_pos].salt_buf[0];
260 salt_buf[1] = salt_bufs[salt_pos].salt_buf[1];
261 salt_buf[2] = salt_bufs[salt_pos].salt_buf[2];
262 salt_buf[3] = salt_bufs[salt_pos].salt_buf[3];
264 u32 salt_len = salt_bufs[salt_pos].salt_len;
272 pw[0] = swap64 (hl32_to_64 (w0[1], w0[0]));
273 pw[1] = swap64 (hl32_to_64 (w0[3], w0[2]));
277 salt[0] = swap64 (hl32_to_64 (salt_buf[1], salt_buf[0]));
278 salt[1] = swap64 (hl32_to_64 (salt_buf[3], salt_buf[2]));
284 sha512_ctx_t sha512_ctx;
286 sha512_init (&sha512_ctx);
288 sha512_update (&sha512_ctx, pw, pw_len);
289 sha512_update (&sha512_ctx, salt, salt_len);
290 sha512_update (&sha512_ctx, pw, pw_len);
292 sha512_final (&sha512_ctx);
296 tmp[0] = sha512_ctx.state[0];
297 tmp[1] = sha512_ctx.state[1];
298 tmp[2] = sha512_ctx.state[2];
299 tmp[3] = sha512_ctx.state[3];
300 tmp[4] = sha512_ctx.state[4];
301 tmp[5] = sha512_ctx.state[5];
302 tmp[6] = sha512_ctx.state[6];
303 tmp[7] = sha512_ctx.state[7];
305 sha512_init (&sha512_ctx);
307 sha512_update (&sha512_ctx, pw, pw_len);
308 sha512_update (&sha512_ctx, salt, salt_len);
309 sha512_update (&sha512_ctx, tmp, pw_len);
311 for (u32 j = pw_len; j; j >>= 1)
315 sha512_update (&sha512_ctx, tmp, 64);
319 sha512_update (&sha512_ctx, pw, pw_len);
323 sha512_final (&sha512_ctx);
325 tmps[gid].l_alt_result[0] = sha512_ctx.state[0];
326 tmps[gid].l_alt_result[1] = sha512_ctx.state[1];
327 tmps[gid].l_alt_result[2] = sha512_ctx.state[2];
328 tmps[gid].l_alt_result[3] = sha512_ctx.state[3];
329 tmps[gid].l_alt_result[4] = sha512_ctx.state[4];
330 tmps[gid].l_alt_result[5] = sha512_ctx.state[5];
331 tmps[gid].l_alt_result[6] = sha512_ctx.state[6];
332 tmps[gid].l_alt_result[7] = sha512_ctx.state[7];
336 sha512_init (&sha512_ctx);
338 for (u32 j = 0; j < pw_len; j++)
340 sha512_update (&sha512_ctx, pw, pw_len);
343 sha512_final (&sha512_ctx);
345 tmps[gid].l_p_bytes[0] = sha512_ctx.state[0];
346 tmps[gid].l_p_bytes[1] = sha512_ctx.state[1];
350 sha512_init (&sha512_ctx);
352 for (u32 j = 0; j < 16 + ((tmps[gid].l_alt_result[0] >> 56) & 0xff); j++)
354 sha512_update (&sha512_ctx, salt, salt_len);
357 sha512_final (&sha512_ctx);
359 tmps[gid].l_s_bytes[0] = sha512_ctx.state[0];
360 tmps[gid].l_s_bytes[1] = sha512_ctx.state[1];
363 __kernel void m01800_loop (__global pw_t *pws, __global kernel_rule_t *rules_buf, __global comb_t *combs_buf, __global bf_t *bfs_buf, __global sha512crypt_tmp_t *tmps, __global void *hooks, __global u32 *bitmaps_buf_s1_a, __global u32 *bitmaps_buf_s1_b, __global u32 *bitmaps_buf_s1_c, __global u32 *bitmaps_buf_s1_d, __global u32 *bitmaps_buf_s2_a, __global u32 *bitmaps_buf_s2_b, __global u32 *bitmaps_buf_s2_c, __global u32 *bitmaps_buf_s2_d, __global plain_t *plains_buf, __global digest_t *digests_buf, __global u32 *hashes_shown, __global salt_t *salt_bufs, __global void *esalt_bufs, __global u32 *d_return_buf, __global u32 *d_scryptV0_buf, __global u32 *d_scryptV1_buf, __global u32 *d_scryptV2_buf, __global u32 *d_scryptV3_buf, const u32 bitmap_mask, const u32 bitmap_shift1, const u32 bitmap_shift2, const u32 salt_pos, const u32 loop_pos, const u32 loop_cnt, const u32 il_cnt, const u32 digests_cnt, const u32 digests_offset, const u32 combs_mode, const u32 gid_max)
369 const u32 gid = get_global_id (0);
371 if (gid >= gid_max) return;
375 l_p_bytes0[0] = tmps[gid].l_p_bytes[0];
376 l_p_bytes0[1] = tmps[gid].l_p_bytes[1];
378 const u32 pw_len = pws[gid].pw_len;
382 l_s_bytes0[0] = tmps[gid].l_s_bytes[0];
383 l_s_bytes0[1] = tmps[gid].l_s_bytes[1];
385 const u32 salt_len = salt_bufs[salt_pos].salt_len;
389 wpc_len[0] = 64 + 0 + 0 + pw_len;
390 wpc_len[1] = pw_len + 0 + 0 + 64;
391 wpc_len[2] = 64 + salt_len + 0 + pw_len;
392 wpc_len[3] = pw_len + salt_len + 0 + 64;
393 wpc_len[4] = 64 + 0 + pw_len + pw_len;
394 wpc_len[5] = pw_len + 0 + pw_len + 64;
395 wpc_len[6] = 64 + salt_len + pw_len + pw_len;
396 wpc_len[7] = pw_len + salt_len + pw_len + 64;
398 u64 wpc[8][16] = { { 0 } };
400 for (u32 i = 0; i < 8; i++)
406 for (u32 j = 0; j < pw_len; j++)
408 PUTCHAR64_BE (wpc[i], block_len++, GETCHAR64_BE (l_p_bytes0, j));
418 for (u32 j = 0; j < salt_len; j++)
420 PUTCHAR64_BE (wpc[i], block_len++, GETCHAR64_BE (l_s_bytes0, j));
426 for (u32 j = 0; j < pw_len; j++)
428 PUTCHAR64_BE (wpc[i], block_len++, GETCHAR64_BE (l_p_bytes0, j));
438 for (u32 j = 0; j < pw_len; j++)
440 PUTCHAR64_BE (wpc[i], block_len++, GETCHAR64_BE (l_p_bytes0, j));
444 PUTCHAR64_BE (wpc[i], block_len, 0x80);
446 wpc[i][15] = block_len * 8;
455 l_alt_result[0] = tmps[gid].l_alt_result[0];
456 l_alt_result[1] = tmps[gid].l_alt_result[1];
457 l_alt_result[2] = tmps[gid].l_alt_result[2];
458 l_alt_result[3] = tmps[gid].l_alt_result[3];
459 l_alt_result[4] = tmps[gid].l_alt_result[4];
460 l_alt_result[5] = tmps[gid].l_alt_result[5];
461 l_alt_result[6] = tmps[gid].l_alt_result[6];
462 l_alt_result[7] = tmps[gid].l_alt_result[7];
465 /* Repeatedly run the collected hash value through SHA512 to burn
468 for (u32 i = 0, j = loop_pos; i < loop_cnt; i++, j++)
470 const u32 j1 = (j & 1) ? 1 : 0;
471 const u32 j3 = (j % 3) ? 2 : 0;
472 const u32 j7 = (j % 7) ? 4 : 0;
474 const u32 pc = j1 + j3 + j7;
478 block[ 0] = wpc[pc][ 0];
479 block[ 1] = wpc[pc][ 1];
480 block[ 2] = wpc[pc][ 2];
481 block[ 3] = wpc[pc][ 3];
482 block[ 4] = wpc[pc][ 4];
483 block[ 5] = wpc[pc][ 5];
484 block[ 6] = wpc[pc][ 6];
485 block[ 7] = wpc[pc][ 7];
486 block[ 8] = wpc[pc][ 8];
487 block[ 9] = wpc[pc][ 9];
488 block[10] = wpc[pc][10];
489 block[11] = wpc[pc][11];
490 block[12] = wpc[pc][12];
491 block[13] = wpc[pc][13];
492 block[14] = wpc[pc][14];
493 block[15] = wpc[pc][15];
497 const u32 block_len = wpc_len[pc];
502 for (u32 k = 0, p = block_len - 64; k < 64; k++, p++)
504 PUTCHAR64_BE (block, p, GETCHAR64_BE (l_alt_result, k));
509 block[0] = l_alt_result[0];
510 block[1] = l_alt_result[1];
511 block[2] = l_alt_result[2];
512 block[3] = l_alt_result[3];
513 block[4] = l_alt_result[4];
514 block[5] = l_alt_result[5];
515 block[6] = l_alt_result[6];
516 block[7] = l_alt_result[7];
519 l_alt_result[0] = SHA512M_A;
520 l_alt_result[1] = SHA512M_B;
521 l_alt_result[2] = SHA512M_C;
522 l_alt_result[3] = SHA512M_D;
523 l_alt_result[4] = SHA512M_E;
524 l_alt_result[5] = SHA512M_F;
525 l_alt_result[6] = SHA512M_G;
526 l_alt_result[7] = SHA512M_H;
528 sha512_transform (block, l_alt_result);
531 tmps[gid].l_alt_result[0] = l_alt_result[0];
532 tmps[gid].l_alt_result[1] = l_alt_result[1];
533 tmps[gid].l_alt_result[2] = l_alt_result[2];
534 tmps[gid].l_alt_result[3] = l_alt_result[3];
535 tmps[gid].l_alt_result[4] = l_alt_result[4];
536 tmps[gid].l_alt_result[5] = l_alt_result[5];
537 tmps[gid].l_alt_result[6] = l_alt_result[6];
538 tmps[gid].l_alt_result[7] = l_alt_result[7];
541 __kernel void m01800_comp (__global pw_t *pws, __global kernel_rule_t *rules_buf, __global comb_t *combs_buf, __global bf_t *bfs_buf, __global sha512crypt_tmp_t *tmps, __global void *hooks, __global u32 *bitmaps_buf_s1_a, __global u32 *bitmaps_buf_s1_b, __global u32 *bitmaps_buf_s1_c, __global u32 *bitmaps_buf_s1_d, __global u32 *bitmaps_buf_s2_a, __global u32 *bitmaps_buf_s2_b, __global u32 *bitmaps_buf_s2_c, __global u32 *bitmaps_buf_s2_d, __global plain_t *plains_buf, __global digest_t *digests_buf, __global u32 *hashes_shown, __global salt_t *salt_bufs, __global void *esalt_bufs, __global u32 *d_return_buf, __global u32 *d_scryptV0_buf, __global u32 *d_scryptV1_buf, __global u32 *d_scryptV2_buf, __global u32 *d_scryptV3_buf, const u32 bitmap_mask, const u32 bitmap_shift1, const u32 bitmap_shift2, const u32 salt_pos, const u32 loop_pos, const u32 loop_cnt, const u32 il_cnt, const u32 digests_cnt, const u32 digests_offset, const u32 combs_mode, const u32 gid_max)
547 const u32 gid = get_global_id (0);
549 if (gid >= gid_max) return;
551 const u32 lid = get_local_id (0);
553 const u64 a = swap64 (tmps[gid].l_alt_result[0]);
554 const u64 b = swap64 (tmps[gid].l_alt_result[1]);
556 const u32 r0 = l32_from_64 (a);
557 const u32 r1 = h32_from_64 (a);
558 const u32 r2 = l32_from_64 (b);
559 const u32 r3 = h32_from_64 (b);