1 /**
2  * Author......: See docs/credits.txt
3  * License.....: MIT
4  */
5 
6 #include "common.h"
7 #include "types.h"
8 #include "modules.h"
9 #include "bitops.h"
10 #include "convert.h"
11 #include "shared.h"
12 
13 static const u32   ATTACK_EXEC    = ATTACK_EXEC_OUTSIDE_KERNEL;
14 static const u32   DGST_POS0      = 0;
15 static const u32   DGST_POS1      = 1;
16 static const u32   DGST_POS2      = 2;
17 static const u32   DGST_POS3      = 3;
18 static const u32   DGST_SIZE      = DGST_SIZE_4_64;
19 static const u32   HASH_CATEGORY  = HASH_CATEGORY_GENERIC_KDF;
20 static const char *HASH_NAME      = "PBKDF2-HMAC-SHA256";
21 static const u64   KERN_TYPE      = 10900;
22 static const u32   OPTI_TYPE      = OPTI_TYPE_ZERO_BYTE
23                                   | OPTI_TYPE_SLOW_HASH_SIMD_LOOP;
24 static const u64   OPTS_TYPE      = OPTS_TYPE_PT_GENERATE_LE
25                                   | OPTS_TYPE_ST_BASE64
26                                   | OPTS_TYPE_HASH_COPY;
27 static const u32   SALT_TYPE      = SALT_TYPE_EMBEDDED;
28 static const char *ST_PASS        = "hashcat";
29 static const char *ST_HASH        = "sha256:1000:NjI3MDM3:vVfavLQL9ZWjg8BUMq6/FB8FtpkIGWYk";
30 
module_attack_exec(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)31 u32         module_attack_exec    (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return ATTACK_EXEC;     }
module_dgst_pos0(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)32 u32         module_dgst_pos0      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return DGST_POS0;       }
module_dgst_pos1(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)33 u32         module_dgst_pos1      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return DGST_POS1;       }
module_dgst_pos2(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)34 u32         module_dgst_pos2      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return DGST_POS2;       }
module_dgst_pos3(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)35 u32         module_dgst_pos3      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return DGST_POS3;       }
module_dgst_size(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)36 u32         module_dgst_size      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return DGST_SIZE;       }
module_hash_category(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)37 u32         module_hash_category  (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return HASH_CATEGORY;   }
module_hash_name(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)38 const char *module_hash_name      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return HASH_NAME;       }
module_kern_type(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)39 u64         module_kern_type      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return KERN_TYPE;       }
module_opti_type(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)40 u32         module_opti_type      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return OPTI_TYPE;       }
module_opts_type(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)41 u64         module_opts_type      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return OPTS_TYPE;       }
module_salt_type(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)42 u32         module_salt_type      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return SALT_TYPE;       }
module_st_hash(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)43 const char *module_st_hash        (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return ST_HASH;         }
module_st_pass(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)44 const char *module_st_pass        (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return ST_PASS;         }
45 
46 typedef struct pbkdf2_sha256
47 {
48   u32 salt_buf[64];
49 
50 } pbkdf2_sha256_t;
51 
52 typedef struct pbkdf2_sha256_tmp
53 {
54   u32  ipad[8];
55   u32  opad[8];
56 
57   u32  dgst[32];
58   u32  out[32];
59 
60 } pbkdf2_sha256_tmp_t;
61 
62 static const char *SIGNATURE_PBKDF2_SHA256 = "sha256";
63 
module_jit_build_options(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra,MAYBE_UNUSED const hashes_t * hashes,MAYBE_UNUSED const hc_device_param_t * device_param)64 char *module_jit_build_options (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra, MAYBE_UNUSED const hashes_t *hashes, MAYBE_UNUSED const hc_device_param_t *device_param)
65 {
66   char *jit_build_options = NULL;
67 
68   // Extra treatment for Apple systems
69   if (device_param->opencl_platform_vendor_id == VENDOR_ID_APPLE)
70   {
71     return jit_build_options;
72   }
73 
74   // NVIDIA GPU
75   if (device_param->opencl_device_vendor_id == VENDOR_ID_NV)
76   {
77     hc_asprintf (&jit_build_options, "-D _unroll");
78   }
79 
80   // HIP
81   if (device_param->opencl_device_vendor_id == VENDOR_ID_AMD_USE_HIP)
82   {
83     hc_asprintf (&jit_build_options, "-D _unroll");
84   }
85 
86   // ROCM
87   if ((device_param->opencl_device_vendor_id == VENDOR_ID_AMD) && (device_param->has_vperm == true))
88   {
89     hc_asprintf (&jit_build_options, "-D _unroll");
90   }
91 
92   return jit_build_options;
93 }
94 
module_esalt_size(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)95 u64 module_esalt_size (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra)
96 {
97   const u64 esalt_size = (const u64) sizeof (pbkdf2_sha256_t);
98 
99   return esalt_size;
100 }
101 
module_tmp_size(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)102 u64 module_tmp_size (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra)
103 {
104   const u64 tmp_size = (const u64) sizeof (pbkdf2_sha256_tmp_t);
105 
106   return tmp_size;
107 }
108 
module_pw_max(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const user_options_t * user_options,MAYBE_UNUSED const user_options_extra_t * user_options_extra)109 u32 module_pw_max (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra)
110 {
111   // this overrides the reductions of PW_MAX in case optimized kernel is selected
112   // IOW, even in optimized kernel mode it support length 256
113 
114   const u32 pw_max = PW_MAX;
115 
116   return pw_max;
117 }
118 
module_hash_decode(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED void * digest_buf,MAYBE_UNUSED salt_t * salt,MAYBE_UNUSED void * esalt_buf,MAYBE_UNUSED void * hook_salt_buf,MAYBE_UNUSED hashinfo_t * hash_info,const char * line_buf,MAYBE_UNUSED const int line_len)119 int module_hash_decode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED void *digest_buf, MAYBE_UNUSED salt_t *salt, MAYBE_UNUSED void *esalt_buf, MAYBE_UNUSED void *hook_salt_buf, MAYBE_UNUSED hashinfo_t *hash_info, const char *line_buf, MAYBE_UNUSED const int line_len)
120 {
121   u32 *digest = (u32 *) digest_buf;
122 
123   pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) esalt_buf;
124 
125   token_t token;
126 
127   token.token_cnt  = 4;
128 
129   token.signatures_cnt    = 1;
130   token.signatures_buf[0] = SIGNATURE_PBKDF2_SHA256;
131 
132   token.sep[0]     = ':';
133   token.len_min[0] = 6;
134   token.len_max[0] = 6;
135   token.attr[0]    = TOKEN_ATTR_VERIFY_LENGTH
136                    | TOKEN_ATTR_VERIFY_SIGNATURE;
137 
138   token.sep[1]     = ':';
139   token.len_min[1] = 1;
140   token.len_max[1] = 6;
141   token.attr[1]    = TOKEN_ATTR_VERIFY_LENGTH
142                    | TOKEN_ATTR_VERIFY_DIGIT;
143 
144   token.sep[2]     = ':';
145   token.len_min[2] = ((SALT_MIN * 8) / 6) + 0;
146   token.len_max[2] = ((SALT_MAX * 8) / 6) + 3;
147   token.attr[2]    = TOKEN_ATTR_VERIFY_LENGTH
148                    | TOKEN_ATTR_VERIFY_BASE64A;
149 
150   token.sep[3]     = ':';
151   token.len_min[3] = 16;
152   token.len_max[3] = 256;
153   token.attr[3]    = TOKEN_ATTR_VERIFY_LENGTH
154                    | TOKEN_ATTR_VERIFY_BASE64A;
155 
156   const int rc_tokenizer = input_tokenizer ((const u8 *) line_buf, line_len, &token);
157 
158   if (rc_tokenizer != PARSER_OK) return (rc_tokenizer);
159 
160   u8  tmp_buf[512];
161   int tmp_len;
162 
163   // iter
164 
165   const u8 *iter_pos = token.buf[1];
166 
167   const u32 iter = hc_strtoul ((const char *) iter_pos, NULL, 10);
168 
169   salt->salt_iter = iter - 1;
170 
171   // salt
172 
173   const u8 *salt_pos = token.buf[2];
174   const int salt_len = token.len[2];
175 
176   memset (tmp_buf, 0, sizeof (tmp_buf));
177 
178   tmp_len = base64_decode (base64_to_int, salt_pos, salt_len, tmp_buf);
179 
180   if (tmp_len > SALT_MAX) return (PARSER_SALT_LENGTH);
181 
182   memcpy (pbkdf2_sha256->salt_buf, tmp_buf, tmp_len);
183 
184   salt->salt_len = tmp_len;
185 
186   salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
187   salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
188   salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
189   salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
190   salt->salt_buf[4] = salt->salt_iter;
191 
192   // hash
193 
194   const u8 *hash_pos = token.buf[3];
195   const int hash_len = token.len[3];
196 
197   memset (tmp_buf, 0, sizeof (tmp_buf));
198 
199   tmp_len = base64_decode (base64_to_int, hash_pos, hash_len, tmp_buf);
200 
201   if (tmp_len < 16) return (PARSER_HASH_LENGTH);
202 
203   memcpy (digest, tmp_buf, 16);
204 
205   digest[0] = byte_swap_32 (digest[0]);
206   digest[1] = byte_swap_32 (digest[1]);
207   digest[2] = byte_swap_32 (digest[2]);
208   digest[3] = byte_swap_32 (digest[3]);
209 
210   return (PARSER_OK);
211 }
212 
module_hash_encode(MAYBE_UNUSED const hashconfig_t * hashconfig,MAYBE_UNUSED const void * digest_buf,MAYBE_UNUSED const salt_t * salt,MAYBE_UNUSED const void * esalt_buf,MAYBE_UNUSED const void * hook_salt_buf,MAYBE_UNUSED const hashinfo_t * hash_info,char * line_buf,MAYBE_UNUSED const int line_size)213 int module_hash_encode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const void *digest_buf, MAYBE_UNUSED const salt_t *salt, MAYBE_UNUSED const void *esalt_buf, MAYBE_UNUSED const void *hook_salt_buf, MAYBE_UNUSED const hashinfo_t *hash_info, char *line_buf, MAYBE_UNUSED const int line_size)
214 {
215   return snprintf (line_buf, line_size, "%s", hash_info->orighash);
216 }
217 
module_init(module_ctx_t * module_ctx)218 void module_init (module_ctx_t *module_ctx)
219 {
220   module_ctx->module_context_size             = MODULE_CONTEXT_SIZE_CURRENT;
221   module_ctx->module_interface_version        = MODULE_INTERFACE_VERSION_CURRENT;
222 
223   module_ctx->module_attack_exec              = module_attack_exec;
224   module_ctx->module_benchmark_esalt          = MODULE_DEFAULT;
225   module_ctx->module_benchmark_hook_salt      = MODULE_DEFAULT;
226   module_ctx->module_benchmark_mask           = MODULE_DEFAULT;
227   module_ctx->module_benchmark_salt           = MODULE_DEFAULT;
228   module_ctx->module_build_plain_postprocess  = MODULE_DEFAULT;
229   module_ctx->module_deep_comp_kernel         = MODULE_DEFAULT;
230   module_ctx->module_deprecated_notice        = MODULE_DEFAULT;
231   module_ctx->module_dgst_pos0                = module_dgst_pos0;
232   module_ctx->module_dgst_pos1                = module_dgst_pos1;
233   module_ctx->module_dgst_pos2                = module_dgst_pos2;
234   module_ctx->module_dgst_pos3                = module_dgst_pos3;
235   module_ctx->module_dgst_size                = module_dgst_size;
236   module_ctx->module_dictstat_disable         = MODULE_DEFAULT;
237   module_ctx->module_esalt_size               = module_esalt_size;
238   module_ctx->module_extra_buffer_size        = MODULE_DEFAULT;
239   module_ctx->module_extra_tmp_size           = MODULE_DEFAULT;
240   module_ctx->module_extra_tuningdb_block     = MODULE_DEFAULT;
241   module_ctx->module_forced_outfile_format    = MODULE_DEFAULT;
242   module_ctx->module_hash_binary_count        = MODULE_DEFAULT;
243   module_ctx->module_hash_binary_parse        = MODULE_DEFAULT;
244   module_ctx->module_hash_binary_save         = MODULE_DEFAULT;
245   module_ctx->module_hash_decode_potfile      = MODULE_DEFAULT;
246   module_ctx->module_hash_decode_zero_hash    = MODULE_DEFAULT;
247   module_ctx->module_hash_decode              = module_hash_decode;
248   module_ctx->module_hash_encode_status       = MODULE_DEFAULT;
249   module_ctx->module_hash_encode_potfile      = MODULE_DEFAULT;
250   module_ctx->module_hash_encode              = module_hash_encode;
251   module_ctx->module_hash_init_selftest       = MODULE_DEFAULT;
252   module_ctx->module_hash_mode                = MODULE_DEFAULT;
253   module_ctx->module_hash_category            = module_hash_category;
254   module_ctx->module_hash_name                = module_hash_name;
255   module_ctx->module_hashes_count_min         = MODULE_DEFAULT;
256   module_ctx->module_hashes_count_max         = MODULE_DEFAULT;
257   module_ctx->module_hlfmt_disable            = MODULE_DEFAULT;
258   module_ctx->module_hook_extra_param_size    = MODULE_DEFAULT;
259   module_ctx->module_hook_extra_param_init    = MODULE_DEFAULT;
260   module_ctx->module_hook_extra_param_term    = MODULE_DEFAULT;
261   module_ctx->module_hook12                   = MODULE_DEFAULT;
262   module_ctx->module_hook23                   = MODULE_DEFAULT;
263   module_ctx->module_hook_salt_size           = MODULE_DEFAULT;
264   module_ctx->module_hook_size                = MODULE_DEFAULT;
265   module_ctx->module_jit_build_options        = module_jit_build_options;
266   module_ctx->module_jit_cache_disable        = MODULE_DEFAULT;
267   module_ctx->module_kernel_accel_max         = MODULE_DEFAULT;
268   module_ctx->module_kernel_accel_min         = MODULE_DEFAULT;
269   module_ctx->module_kernel_loops_max         = MODULE_DEFAULT;
270   module_ctx->module_kernel_loops_min         = MODULE_DEFAULT;
271   module_ctx->module_kernel_threads_max       = MODULE_DEFAULT;
272   module_ctx->module_kernel_threads_min       = MODULE_DEFAULT;
273   module_ctx->module_kern_type                = module_kern_type;
274   module_ctx->module_kern_type_dynamic        = MODULE_DEFAULT;
275   module_ctx->module_opti_type                = module_opti_type;
276   module_ctx->module_opts_type                = module_opts_type;
277   module_ctx->module_outfile_check_disable    = MODULE_DEFAULT;
278   module_ctx->module_outfile_check_nocomp     = MODULE_DEFAULT;
279   module_ctx->module_potfile_custom_check     = MODULE_DEFAULT;
280   module_ctx->module_potfile_disable          = MODULE_DEFAULT;
281   module_ctx->module_potfile_keep_all_hashes  = MODULE_DEFAULT;
282   module_ctx->module_pwdump_column            = MODULE_DEFAULT;
283   module_ctx->module_pw_max                   = module_pw_max;
284   module_ctx->module_pw_min                   = MODULE_DEFAULT;
285   module_ctx->module_salt_max                 = MODULE_DEFAULT;
286   module_ctx->module_salt_min                 = MODULE_DEFAULT;
287   module_ctx->module_salt_type                = module_salt_type;
288   module_ctx->module_separator                = MODULE_DEFAULT;
289   module_ctx->module_st_hash                  = module_st_hash;
290   module_ctx->module_st_pass                  = module_st_pass;
291   module_ctx->module_tmp_size                 = module_tmp_size;
292   module_ctx->module_unstable_warning         = MODULE_DEFAULT;
293   module_ctx->module_warmup_disable           = MODULE_DEFAULT;
294 }
295