From cd9dc989ce511b0aeafe91a5e0cdbeccffdc6f9f Mon Sep 17 00:00:00 2001 From: DoZ10 Date: Sat, 6 May 2017 14:24:50 -0400 Subject: [PATCH] Implemented Chacha20 crypto. --- OpenCL/m00670_a0.cl | 110 ++++++++++++++++++++++++++++++++++++++++---- chachaTest.dict | 2 +- chachaTest.hash | 2 +- include/interface.h | 4 +- src/interface.c | 4 +- src/opencl.c | 2 +- 6 files changed, 108 insertions(+), 16 deletions(-) diff --git a/OpenCL/m00670_a0.cl b/OpenCL/m00670_a0.cl index 93c6146f4..c378944ad 100644 --- a/OpenCL/m00670_a0.cl +++ b/OpenCL/m00670_a0.cl @@ -14,6 +14,101 @@ #include "inc_rp.cl" #include "inc_simd.cl" +#define CHACHA_CONST_00 0x61707865 +#define CHACHA_CONST_01 0x3320646e +#define CHACHA_CONST_02 0x79622d32 +#define CHACHA_CONST_03 0x6b206574 + +#define QR(a, b, c, d) \ + do { \ + x[a] = x[a] + x[b]; \ + x[d] = rotl32(x[d] ^ x[a], 16); \ + x[c] = x[c] + x[d]; \ + x[b] = rotl32(x[b] ^ x[c], 12); \ + x[a] = x[a] + x[b]; \ + x[d] = rotl32(x[d] ^ x[a], 8); \ + x[c] = x[c] + x[d]; \ + x[b] = rotl32(x[b] ^ x[c], 7); \ + } while (0); + +void chacha20_transform (const u32x w0[4], const u32x w1[4], const u32 position, const u32 iv[2], const u32 plain[4], u32x digest[4]) +{ + u32x ctx[16]; + + ctx[ 0] = CHACHA_CONST_00; + ctx[ 1] = CHACHA_CONST_01; + ctx[ 2] = CHACHA_CONST_02; + ctx[ 3] = CHACHA_CONST_03; + ctx[ 4] = w0[0]; + ctx[ 5] = w0[1]; + ctx[ 6] = w0[2]; + ctx[ 7] = w0[3]; + ctx[ 8] = w1[0]; + ctx[ 9] = w1[1]; + ctx[10] = w1[2]; + ctx[11] = w1[3]; + ctx[12] = 0; + ctx[13] = 0; + ctx[14] = iv[1]; + ctx[15] = iv[0]; + + u32x x[16]; + + x[ 0] = ctx[ 0]; + x[ 1] = ctx[ 1]; + x[ 2] = ctx[ 2]; + x[ 3] = ctx[ 3]; + x[ 4] = ctx[ 4]; + x[ 5] = ctx[ 5]; + x[ 6] = ctx[ 6]; + x[ 7] = ctx[ 7]; + x[ 8] = ctx[ 8]; + x[ 9] = ctx[ 9]; + x[10] = ctx[10]; + x[11] = ctx[11]; + x[12] = ctx[12]; + x[13] = ctx[13]; + x[14] = ctx[14]; + x[15] = ctx[15]; + + for (int i = 0; i < 10; ++i) { + + /* Column round */ + QR(0, 4, 8, 12); + QR(1, 5, 9, 13); + QR(2, 6, 10, 14); + QR(3, 7, 11, 15); + + /* Diagonal round */ + QR(0, 5, 10, 15); + QR(1, 6, 11, 12); + QR(2, 7, 8, 13); + QR(3, 4, 9, 14); + } + + x[ 0] += ctx[ 0]; + x[ 1] += ctx[ 1]; + x[ 2] += ctx[ 2]; + x[ 3] += ctx[ 3]; + x[ 4] += ctx[ 4]; + x[ 5] += ctx[ 5]; + x[ 6] += ctx[ 6]; + x[ 7] += ctx[ 7]; + x[ 8] += ctx[ 8]; + x[ 9] += ctx[ 9]; + x[10] += ctx[10]; + x[11] += ctx[11]; + x[12] += ctx[12]; + x[13] += ctx[13]; + x[14] += ctx[14]; + x[15] += ctx[15]; + + digest[0] = plain[0] ^ x[0]; + digest[1] = plain[1] ^ x[1]; + digest[2] = plain[2] ^ x[2]; + digest[3] = plain[3] ^ x[3]; +} + __kernel void m00670_m04 (__global pw_t *pws, __global const kernel_rule_t *rules_buf, __global const comb_t *combs_buf, __global const bf_t *bfs_buf, __global void *tmps, __global void *hooks, __global const u32 *bitmaps_buf_s1_a, __global const u32 *bitmaps_buf_s1_b, __global const u32 *bitmaps_buf_s1_c, __global const u32 *bitmaps_buf_s1_d, __global const u32 *bitmaps_buf_s2_a, __global const u32 *bitmaps_buf_s2_b, __global const u32 *bitmaps_buf_s2_c, __global const u32 *bitmaps_buf_s2_d, __global plain_t *plains_buf, __global const digest_t *digests_buf, __global u32 *hashes_shown, __global const salt_t *salt_bufs, __global const chacha20_t *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) { /** @@ -107,8 +202,6 @@ __kernel void m00670_s04 (__global pw_t *pws, __global const kernel_rule_t *rule plain[0] = esalt_bufs->plain[0]; plain[1] = esalt_bufs->plain[1]; - printf("s04-> position: %d, iv: %08x%08x, plain_length: %d, plain: %08x%08x, cipher: %08x%08x\n", position, iv[0], iv[1], plain_length, plain[0], plain[1], digests_buf[digests_offset].digest_buf[0], digests_buf[digests_offset].digest_buf[1]); - /** * digest */ @@ -134,15 +227,14 @@ __kernel void m00670_s04 (__global pw_t *pws, __global const kernel_rule_t *rule const u32x out_len = apply_rules_vect(pw_buf0, pw_buf1, pw_len, rules_buf, il_pos, w0, w1); - u64x digest[8]; - u64x m[16]; - u64x v[16]; + u32x digest[4] = { 0 }; + chacha20_transform (w0, w1, position, iv, plain, digest); - const u32x r0 = h32_from_64(digest[0]); - const u32x r1 = l32_from_64(digest[0]); - const u32x r2 = h32_from_64(digest[1]); - const u32x r3 = l32_from_64(digest[1]); + const u32x r0 = digest[1]; + const u32x r1 = digest[0]; + const u32x r2 = digest[3]; + const u32x r3 = digest[2]; COMPARE_S_SIMD(r0, r1, r2, r3); } diff --git a/chachaTest.dict b/chachaTest.dict index cc6718945..9f358a4ad 100644 --- a/chachaTest.dict +++ b/chachaTest.dict @@ -1 +1 @@ -0123456789abcdef0123456789abcdef +123456 diff --git a/chachaTest.hash b/chachaTest.hash index 073813433..0223dbbf4 100644 --- a/chachaTest.hash +++ b/chachaTest.hash @@ -1 +1 @@ -$Chacha20$*1*0102030405060708*0000000000000000*1370b47e3c509a43 +$Chacha20$*0*0001020304050607*00000000000000000000000000000000*8c697432d1159fede2a9c8c4a0002f10 diff --git a/include/interface.h b/include/interface.h index e56c61e36..92fb4b88c 100644 --- a/include/interface.h +++ b/include/interface.h @@ -912,8 +912,8 @@ typedef enum display_len DISPLAY_LEN_MAX_501 = 104, DISPLAY_LEN_MIN_600 = 8 + 128, DISPLAY_LEN_MAX_600 = 8 + 128, - DISPLAY_LEN_MIN_670 = 10 + 1 + 1 + 1 + 16 + 1 + 16 + 1 + 16, - DISPLAY_LEN_MAX_670 = 10 + 1 + 5 + 1 + 16 + 1 + 16 + 1 + 16, + DISPLAY_LEN_MIN_670 = 10 + 1 + 1 + 1 + 16 + 1 + 32 + 1 + 32, + DISPLAY_LEN_MAX_670 = 10 + 1 + 5 + 1 + 16 + 1 + 32 + 1 + 32, DISPLAY_LEN_MIN_900 = 32, DISPLAY_LEN_MAX_900 = 32, DISPLAY_LEN_MIN_910 = 32 + 1 + 0, diff --git a/src/interface.c b/src/interface.c index 24b444fe3..72fd80079 100644 --- a/src/interface.c +++ b/src/interface.c @@ -5334,7 +5334,7 @@ int chacha20_parse_hash (u8 *input_buf, u32 input_len, hash_t *hash_buf, MAYBE_U u8 *cipher_marker = (u8 *) strchr ((const char *) plain_marker, '*') + 1; if (cipher_marker == NULL) return (PARSER_SEPARATOR_UNMATCHED); - chacha20->position = 1; + chacha20->position = 0; chacha20->plain_length = 16; chacha20->iv[0] = hex_to_u32 ((const u8 *) iv_marker + 8); @@ -19464,7 +19464,7 @@ int hashconfig_init (hashcat_ctx_t *hashcat_ctx) hashconfig->attack_exec = ATTACK_EXEC_INSIDE_KERNEL; hashconfig->opts_type = OPTS_TYPE_PT_GENERATE_LE; hashconfig->kern_type = KERN_TYPE_CHACHA20; - hashconfig->dgst_size = DGST_SIZE_8_8; + hashconfig->dgst_size = DGST_SIZE_4_4; hashconfig->parse_func = chacha20_parse_hash; hashconfig->opti_type = OPTI_TYPE_ZERO_BYTE | OPTI_TYPE_USES_BITS_32 diff --git a/src/opencl.c b/src/opencl.c index d5fcd300c..cf6ffca8c 100644 --- a/src/opencl.c +++ b/src/opencl.c @@ -1131,7 +1131,7 @@ int choose_kernel (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, if (CL_rc == -1) return -1; } - else if (highest_pw_len < 32) + else if (highest_pw_len <= 32) { CL_rc = run_kernel (hashcat_ctx, device_param, KERN_RUN_2, pws_cnt, true, fast_iteration);