"Fossies" - the Fresh Open Source Software Archive

Member "hashcat-6.2.6/src/hashes.c" (2 Sep 2022, 77267 Bytes) of package /linux/privat/hashcat-6.2.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "hashes.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 6.2.5_vs_6.2.6.

    1 /**
    2  * Author......: See docs/credits.txt
    3  * License.....: MIT
    4  */
    5 
    6 #include "common.h"
    7 #include "types.h"
    8 #include "memory.h"
    9 #include "event.h"
   10 #include "convert.h"
   11 #include "debugfile.h"
   12 #include "filehandling.h"
   13 #include "hlfmt.h"
   14 #include "terminal.h"
   15 #include "logfile.h"
   16 #include "loopback.h"
   17 #include "backend.h"
   18 #include "outfile.h"
   19 #include "potfile.h"
   20 #include "rp.h"
   21 #include "shared.h"
   22 #include "thread.h"
   23 #include "locking.h"
   24 #include "hashes.h"
   25 
   26 #ifdef WITH_BRAIN
   27 #include "brain.h"
   28 #endif
   29 
   30 int sort_by_digest_p0p1 (const void *v1, const void *v2, void *v3)
   31 {
   32   const u32 *d1 = (const u32 *) v1;
   33   const u32 *d2 = (const u32 *) v2;
   34 
   35   hashconfig_t *hashconfig = (hashconfig_t *) v3;
   36 
   37   const u32 dgst_pos0 = hashconfig->dgst_pos0;
   38   const u32 dgst_pos1 = hashconfig->dgst_pos1;
   39   const u32 dgst_pos2 = hashconfig->dgst_pos2;
   40   const u32 dgst_pos3 = hashconfig->dgst_pos3;
   41 
   42   if (d1[dgst_pos3] > d2[dgst_pos3]) return  1;
   43   if (d1[dgst_pos3] < d2[dgst_pos3]) return -1;
   44   if (d1[dgst_pos2] > d2[dgst_pos2]) return  1;
   45   if (d1[dgst_pos2] < d2[dgst_pos2]) return -1;
   46   if (d1[dgst_pos1] > d2[dgst_pos1]) return  1;
   47   if (d1[dgst_pos1] < d2[dgst_pos1]) return -1;
   48   if (d1[dgst_pos0] > d2[dgst_pos0]) return  1;
   49   if (d1[dgst_pos0] < d2[dgst_pos0]) return -1;
   50 
   51   return 0;
   52 }
   53 
   54 int sort_by_salt (const void *v1, const void *v2)
   55 {
   56   const salt_t *s1 = (const salt_t *) v1;
   57   const salt_t *s2 = (const salt_t *) v2;
   58 
   59   const int res_pos = (int) s1->orig_pos - (int) s2->orig_pos;
   60 
   61   if (res_pos != 0) return (res_pos);
   62 
   63   const int res1 = (int) s1->salt_len - (int) s2->salt_len;
   64 
   65   if (res1 != 0) return (res1);
   66 
   67   const int res2 = (int) s1->salt_iter - (int) s2->salt_iter;
   68 
   69   if (res2 != 0) return (res2);
   70 
   71   for (int n = 0; n < 64; n++)
   72   {
   73     if (s1->salt_buf[n] > s2->salt_buf[n]) return  1;
   74     if (s1->salt_buf[n] < s2->salt_buf[n]) return -1;
   75   }
   76 
   77   for (int n = 0; n < 64; n++)
   78   {
   79     if (s1->salt_buf_pc[n] > s2->salt_buf_pc[n]) return  1;
   80     if (s1->salt_buf_pc[n] < s2->salt_buf_pc[n]) return -1;
   81   }
   82 
   83   return 0;
   84 }
   85 
   86 int sort_by_hash (const void *v1, const void *v2, void *v3)
   87 {
   88   const hash_t *h1 = (const hash_t *) v1;
   89   const hash_t *h2 = (const hash_t *) v2;
   90 
   91   hashconfig_t *hashconfig = (hashconfig_t *) v3;
   92 
   93   if (hashconfig->is_salted == true)
   94   {
   95     const salt_t *s1 = h1->salt;
   96     const salt_t *s2 = h2->salt;
   97 
   98     int res = sort_by_salt (s1, s2);
   99 
  100     if (res != 0) return (res);
  101   }
  102 
  103   const void *d1 = h1->digest;
  104   const void *d2 = h2->digest;
  105 
  106   return sort_by_digest_p0p1 (d1, d2, v3);
  107 }
  108 
  109 int sort_by_hash_no_salt (const void *v1, const void *v2, void *v3)
  110 {
  111   const hash_t *h1 = (const hash_t *) v1;
  112   const hash_t *h2 = (const hash_t *) v2;
  113 
  114   const void *d1 = h1->digest;
  115   const void *d2 = h2->digest;
  116 
  117   return sort_by_digest_p0p1 (d1, d2, v3);
  118 }
  119 
  120 int hash_encode (const hashconfig_t *hashconfig, const hashes_t *hashes, const module_ctx_t *module_ctx, char *out_buf, const int out_size, const u32 salt_pos, const u32 digest_pos)
  121 {
  122   if (module_ctx->module_hash_encode == MODULE_DEFAULT)
  123   {
  124     return snprintf (out_buf, out_size, "%s", hashes->hashfile);
  125   }
  126 
  127   salt_t *salts_buf = hashes->salts_buf;
  128 
  129   salts_buf += salt_pos;
  130 
  131   const u32 digest_cur = salts_buf->digests_offset + digest_pos;
  132 
  133   void        *digests_buf    = hashes->digests_buf;
  134   void        *esalts_buf     = hashes->esalts_buf;
  135   void        *hook_salts_buf = hashes->hook_salts_buf;
  136   hashinfo_t **hash_info      = hashes->hash_info;
  137 
  138   char       *digests_buf_ptr    = (char *) digests_buf;
  139   char       *esalts_buf_ptr     = (char *) esalts_buf;
  140   char       *hook_salts_buf_ptr = (char *) hook_salts_buf;
  141   hashinfo_t *hash_info_ptr      = NULL;
  142 
  143   digests_buf_ptr    += digest_cur * hashconfig->dgst_size;
  144   esalts_buf_ptr     += digest_cur * hashconfig->esalt_size;
  145   hook_salts_buf_ptr += digest_cur * hashconfig->hook_salt_size;
  146 
  147   if (hash_info) hash_info_ptr = hash_info[digest_cur];
  148 
  149   const int out_len = module_ctx->module_hash_encode
  150   (
  151     hashconfig,
  152     digests_buf_ptr,
  153     salts_buf,
  154     esalts_buf_ptr,
  155     hook_salts_buf_ptr,
  156     hash_info_ptr,
  157     out_buf,
  158     out_size
  159   );
  160 
  161   return out_len;
  162 }
  163 
  164 int save_hash (hashcat_ctx_t *hashcat_ctx)
  165 {
  166   hashes_t        *hashes       = hashcat_ctx->hashes;
  167   hashconfig_t    *hashconfig   = hashcat_ctx->hashconfig;
  168   module_ctx_t    *module_ctx   = hashcat_ctx->module_ctx;
  169   user_options_t  *user_options = hashcat_ctx->user_options;
  170 
  171   const char *hashfile = hashes->hashfile;
  172 
  173   char *new_hashfile;
  174   char *old_hashfile;
  175 
  176   hc_asprintf (&new_hashfile, "%s.new", hashfile);
  177   hc_asprintf (&old_hashfile, "%s.old", hashfile);
  178 
  179   unlink (new_hashfile);
  180 
  181   char separator = hashconfig->separator;
  182 
  183   HCFILE fp;
  184 
  185   if (hc_fopen (&fp, new_hashfile, "wb") == false)
  186   {
  187     event_log_error (hashcat_ctx, "%s: %s", new_hashfile, strerror (errno));
  188 
  189     hcfree (new_hashfile);
  190     hcfree (old_hashfile);
  191 
  192     return -1;
  193   }
  194 
  195   if (hc_lockfile (&fp) == -1)
  196   {
  197     hc_fclose (&fp);
  198 
  199     event_log_error (hashcat_ctx, "%s: %s", new_hashfile, strerror (errno));
  200 
  201     hcfree (new_hashfile);
  202     hcfree (old_hashfile);
  203 
  204     return -1;
  205   }
  206 
  207   u8 *out_buf = (u8 *) hcmalloc (HCBUFSIZ_LARGE);
  208 
  209   for (u32 salt_pos = 0; salt_pos < hashes->salts_cnt; salt_pos++)
  210   {
  211     if (hashes->salts_shown[salt_pos] == 1) continue;
  212 
  213     salt_t *salt_buf = &hashes->salts_buf[salt_pos];
  214 
  215     for (u32 digest_pos = 0; digest_pos < salt_buf->digests_cnt; digest_pos++)
  216     {
  217       const u32 idx = salt_buf->digests_offset + digest_pos;
  218 
  219       if (hashes->digests_shown[idx] == 1) continue;
  220 
  221       if (module_ctx->module_hash_binary_save != MODULE_DEFAULT)
  222       {
  223         char *binary_buf = NULL;
  224 
  225         const int binary_len = module_ctx->module_hash_binary_save (hashes, salt_pos, digest_pos, &binary_buf);
  226 
  227         hc_fwrite (binary_buf, binary_len, 1, &fp);
  228 
  229         hcfree (binary_buf);
  230       }
  231       else
  232       {
  233         if (user_options->username == true)
  234         {
  235           user_t *user = hashes->hash_info[idx]->user;
  236 
  237           u32 i;
  238 
  239           for (i = 0; i < user->user_len; i++) hc_fputc (user->user_name[i], &fp);
  240 
  241           hc_fputc (separator, &fp);
  242         }
  243 
  244         const int out_len = hash_encode (hashcat_ctx->hashconfig, hashcat_ctx->hashes, hashcat_ctx->module_ctx, (char *) out_buf, HCBUFSIZ_LARGE, salt_pos, digest_pos);
  245 
  246         out_buf[out_len] = 0;
  247 
  248         hc_fprintf (&fp, "%s" EOL, out_buf);
  249       }
  250     }
  251   }
  252 
  253   hcfree (out_buf);
  254 
  255   hc_fflush (&fp);
  256 
  257   if (hc_unlockfile (&fp) == -1)
  258   {
  259     hc_fclose (&fp);
  260 
  261     event_log_error (hashcat_ctx, "%s: %s", new_hashfile, strerror (errno));
  262 
  263     hcfree (new_hashfile);
  264     hcfree (old_hashfile);
  265 
  266     return -1;
  267   }
  268 
  269   hc_fclose (&fp);
  270 
  271   unlink (old_hashfile);
  272 
  273   if (rename (hashfile, old_hashfile) != 0)
  274   {
  275     event_log_error (hashcat_ctx, "Rename file '%s' to '%s': %s", hashfile, old_hashfile, strerror (errno));
  276 
  277     hcfree (new_hashfile);
  278     hcfree (old_hashfile);
  279 
  280     return -1;
  281   }
  282 
  283   unlink (hashfile);
  284 
  285   if (rename (new_hashfile, hashfile) != 0)
  286   {
  287     event_log_error (hashcat_ctx, "Rename file '%s' to '%s': %s", new_hashfile, hashfile, strerror (errno));
  288 
  289     hcfree (new_hashfile);
  290     hcfree (old_hashfile);
  291 
  292     return -1;
  293   }
  294 
  295   unlink (old_hashfile);
  296 
  297   hcfree (new_hashfile);
  298   hcfree (old_hashfile);
  299 
  300   return 0;
  301 }
  302 
  303 int check_hash (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain)
  304 {
  305   const debugfile_ctx_t *debugfile_ctx = hashcat_ctx->debugfile_ctx;
  306   const hashes_t        *hashes        = hashcat_ctx->hashes;
  307   const hashconfig_t    *hashconfig    = hashcat_ctx->hashconfig;
  308   const loopback_ctx_t  *loopback_ctx  = hashcat_ctx->loopback_ctx;
  309   const module_ctx_t    *module_ctx    = hashcat_ctx->module_ctx;
  310 
  311   const u32 salt_pos    = plain->salt_pos;
  312   const u32 digest_pos  = plain->digest_pos;  // relative
  313 
  314   void *tmps = NULL;
  315 
  316   cl_event opencl_event;
  317 
  318   int rc = -1;
  319 
  320   if (hashconfig->opts_type & OPTS_TYPE_COPY_TMPS)
  321   {
  322     tmps = hcmalloc (hashconfig->tmp_size);
  323 
  324     if (device_param->is_cuda == true)
  325     {
  326       rc = hc_cuMemcpyDtoHAsync (hashcat_ctx, tmps, device_param->cuda_d_tmps + (plain->gidvid * hashconfig->tmp_size), hashconfig->tmp_size, device_param->cuda_stream);
  327 
  328       if (rc == 0)
  329       {
  330         rc = hc_cuEventRecord (hashcat_ctx, device_param->cuda_event3, device_param->cuda_stream);
  331       }
  332 
  333       if (rc == -1)
  334       {
  335         hcfree (tmps);
  336 
  337         return -1;
  338       }
  339     }
  340 
  341     if (device_param->is_hip == true)
  342     {
  343       rc = hc_hipMemcpyDtoHAsync (hashcat_ctx, tmps, device_param->hip_d_tmps + (plain->gidvid * hashconfig->tmp_size), hashconfig->tmp_size, device_param->hip_stream);
  344 
  345       if (rc == 0)
  346       {
  347         rc = hc_hipEventRecord (hashcat_ctx, device_param->hip_event3, device_param->hip_stream);
  348       }
  349 
  350       if (rc == -1)
  351       {
  352         hcfree (tmps);
  353 
  354         return -1;
  355       }
  356     }
  357 
  358     #if defined (__APPLE__)
  359     if (device_param->is_metal == true)
  360     {
  361       rc = hc_mtlMemcpyDtoH (hashcat_ctx, device_param->metal_command_queue, tmps, device_param->metal_d_tmps, plain->gidvid * hashconfig->tmp_size, hashconfig->tmp_size);
  362 
  363       if (rc == -1)
  364       {
  365         hcfree (tmps);
  366 
  367         return -1;
  368       }
  369     }
  370     #endif
  371 
  372     if (device_param->is_opencl == true)
  373     {
  374       rc = hc_clEnqueueReadBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_tmps, CL_FALSE, plain->gidvid * hashconfig->tmp_size, hashconfig->tmp_size, tmps, 0, NULL, &opencl_event);
  375 
  376       if (rc == 0)
  377       {
  378         rc = hc_clFlush (hashcat_ctx, device_param->opencl_command_queue);
  379       }
  380 
  381       if (rc == -1)
  382       {
  383         hcfree (tmps);
  384 
  385         return -1;
  386       }
  387     }
  388   }
  389 
  390   // hash
  391 
  392   u8 *out_buf = hashes->out_buf;
  393 
  394   int out_len = hash_encode (hashconfig, hashes, module_ctx, (char *) out_buf, HCBUFSIZ_LARGE, salt_pos, digest_pos);
  395 
  396   out_buf[out_len] = 0;
  397 
  398   // plain
  399 
  400   u8 plain_buf[HCBUFSIZ_TINY] = { 0 }; // while the password itself can have only length 256, the module could encode it with something like base64 which inflates the requires buffer size
  401   u8 postprocess_buf[HCBUFSIZ_TINY] = { 0 };
  402 
  403   u8 *plain_ptr = plain_buf;
  404 
  405   int plain_len = 0;
  406 
  407   build_plain (hashcat_ctx, device_param, plain, (u32 *) plain_buf, &plain_len);
  408 
  409   if (module_ctx->module_build_plain_postprocess != MODULE_DEFAULT)
  410   {
  411     if (hashconfig->opts_type & OPTS_TYPE_COPY_TMPS)
  412     {
  413       if (device_param->is_cuda == true)
  414       {
  415         if (hc_cuEventSynchronize (hashcat_ctx, device_param->cuda_event3) == -1) return -1;
  416       }
  417 
  418       if (device_param->is_hip == true)
  419       {
  420         if (hc_hipEventSynchronize (hashcat_ctx, device_param->hip_event3) == -1) return -1;
  421       }
  422 
  423       if (device_param->is_opencl == true)
  424       {
  425         if (hc_clWaitForEvents (hashcat_ctx, 1, &opencl_event) == -1) return -1;
  426       }
  427     }
  428 
  429     plain_len = module_ctx->module_build_plain_postprocess (hashconfig, hashes, tmps, (u32 *) plain_buf, sizeof (plain_buf), plain_len, (u32 *) postprocess_buf, sizeof (postprocess_buf));
  430 
  431     plain_ptr = postprocess_buf;
  432   }
  433 
  434   // crackpos
  435 
  436   u64 crackpos = 0;
  437 
  438   build_crackpos (hashcat_ctx, device_param, plain, &crackpos);
  439 
  440   // debug
  441 
  442   u8  debug_rule_buf[RP_PASSWORD_SIZE] = { 0 };
  443   int debug_rule_len  = 0; // -1 error
  444 
  445   u8  debug_plain_ptr[RP_PASSWORD_SIZE] = { 0 };
  446   int debug_plain_len = 0;
  447 
  448   build_debugdata (hashcat_ctx, device_param, plain, debug_rule_buf, &debug_rule_len, debug_plain_ptr, &debug_plain_len);
  449 
  450   // outfile, can be either to file or stdout
  451   // if an error occurs opening the file, send to stdout as fallback
  452   // the fp gets opened for each cracked hash so that the user can modify (move) the outfile while hashcat runs
  453 
  454   outfile_write_open (hashcat_ctx);
  455 
  456   u8 *tmp_buf = hashes->tmp_buf;
  457 
  458   tmp_buf[0] = 0;
  459 
  460   const int tmp_len = outfile_write (hashcat_ctx, (char *) out_buf, out_len, plain_ptr, plain_len, crackpos, NULL, 0, true, (char *) tmp_buf);
  461 
  462   EVENT_DATA (EVENT_CRACKER_HASH_CRACKED, tmp_buf, tmp_len);
  463 
  464   outfile_write_close (hashcat_ctx);
  465 
  466   // potfile
  467   // we can have either used-defined hooks or reuse the same format as input format
  468   // no need for locking, we're in a mutex protected function
  469 
  470   if (module_ctx->module_hash_encode_potfile != MODULE_DEFAULT)
  471   {
  472     if (hashconfig->opts_type & OPTS_TYPE_COPY_TMPS)
  473     {
  474       if (device_param->is_cuda == true)
  475       {
  476         if (hc_cuEventSynchronize (hashcat_ctx, device_param->cuda_event3) == -1) return -1;
  477       }
  478 
  479       if (device_param->is_hip == true)
  480       {
  481         if (hc_hipEventSynchronize (hashcat_ctx, device_param->hip_event3) == -1) return -1;
  482       }
  483 
  484       if (device_param->is_opencl == true)
  485       {
  486         if (hc_clWaitForEvents (hashcat_ctx, 1, &opencl_event) == -1) return -1;
  487       }
  488     }
  489 
  490     salt_t *salts_buf = hashes->salts_buf;
  491 
  492     salts_buf += salt_pos;
  493 
  494     const u32 digest_cur = salts_buf->digests_offset + digest_pos;
  495 
  496     void        *digests_buf    = hashes->digests_buf;
  497     void        *esalts_buf     = hashes->esalts_buf;
  498     void        *hook_salts_buf = hashes->hook_salts_buf;
  499     hashinfo_t **hash_info      = hashes->hash_info;
  500 
  501     char       *digests_buf_ptr    = (char *) digests_buf;
  502     char       *esalts_buf_ptr     = (char *) esalts_buf;
  503     char       *hook_salts_buf_ptr = (char *) hook_salts_buf;
  504     hashinfo_t *hash_info_ptr      = NULL;
  505 
  506     digests_buf_ptr    += digest_cur * hashconfig->dgst_size;
  507     esalts_buf_ptr     += digest_cur * hashconfig->esalt_size;
  508     hook_salts_buf_ptr += digest_cur * hashconfig->hook_salt_size;
  509 
  510     if (hash_info) hash_info_ptr = hash_info[digest_cur];
  511 
  512     out_len = module_ctx->module_hash_encode_potfile
  513     (
  514       hashconfig,
  515       digests_buf_ptr,
  516       salts_buf,
  517       esalts_buf_ptr,
  518       hook_salts_buf_ptr,
  519       hash_info_ptr,
  520       (char *) out_buf,
  521       HCBUFSIZ_LARGE,
  522       tmps
  523     );
  524 
  525     out_buf[out_len] = 0;
  526   }
  527 
  528   potfile_write_append (hashcat_ctx, (char *) out_buf, out_len, plain_ptr, plain_len);
  529 
  530   // if enabled, update also the loopback file
  531 
  532   if (loopback_ctx->fp.pfp != NULL)
  533   {
  534     loopback_write_append (hashcat_ctx, plain_ptr, plain_len);
  535   }
  536 
  537   // if enabled, update also the (rule) debug file
  538 
  539   if (debugfile_ctx->fp.pfp != NULL)
  540   {
  541     // the next check implies that:
  542     // - (user_options->attack_mode == ATTACK_MODE_STRAIGHT)
  543     // - debug_mode > 0
  544 
  545     if ((debug_plain_len > 0) || (debug_rule_len > 0))
  546     {
  547       debugfile_write_append (hashcat_ctx, debug_rule_buf, debug_rule_len, plain_ptr, plain_len, debug_plain_ptr, debug_plain_len);
  548     }
  549   }
  550 
  551   if (hashconfig->opts_type & OPTS_TYPE_COPY_TMPS)
  552   {
  553     hcfree (tmps);
  554 
  555     if (device_param->is_opencl == true)
  556     {
  557       if (hc_clReleaseEvent (hashcat_ctx, opencl_event) == -1) return -1;
  558     }
  559   }
  560 
  561   return 0;
  562 }
  563 
  564 //int check_cracked (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, const u32 salt_pos)
  565 int check_cracked (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param)
  566 {
  567   cpt_ctx_t      *cpt_ctx      = hashcat_ctx->cpt_ctx;
  568   hashconfig_t   *hashconfig   = hashcat_ctx->hashconfig;
  569   hashes_t       *hashes       = hashcat_ctx->hashes;
  570   status_ctx_t   *status_ctx   = hashcat_ctx->status_ctx;
  571   user_options_t *user_options = hashcat_ctx->user_options;
  572 
  573   u32 num_cracked = 0;
  574 
  575   int rc = -1;
  576 
  577   if (device_param->is_cuda == true)
  578   {
  579     if (hc_cuMemcpyDtoHAsync (hashcat_ctx, &num_cracked, device_param->cuda_d_result, sizeof (u32), device_param->cuda_stream) == -1) return -1;
  580 
  581     if (hc_cuStreamSynchronize (hashcat_ctx, device_param->cuda_stream) == -1) return -1;
  582   }
  583 
  584   if (device_param->is_hip == true)
  585   {
  586     if (hc_hipMemcpyDtoHAsync (hashcat_ctx, &num_cracked, device_param->hip_d_result, sizeof (u32), device_param->hip_stream) == -1) return -1;
  587 
  588     if (hc_hipStreamSynchronize (hashcat_ctx, device_param->hip_stream) == -1) return -1;
  589   }
  590 
  591   #if defined (__APPLE__)
  592   if (device_param->is_metal == true)
  593   {
  594     if (hc_mtlMemcpyDtoH (hashcat_ctx, device_param->metal_command_queue, &num_cracked, device_param->metal_d_result, 0, sizeof (u32)) == -1) return -1;
  595   }
  596   #endif
  597 
  598   if (device_param->is_opencl == true)
  599   {
  600     /* blocking */
  601     if (hc_clEnqueueReadBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_result, CL_TRUE, 0, sizeof (u32), &num_cracked, 0, NULL, NULL) == -1) return -1;
  602   }
  603 
  604   if (num_cracked == 0 || user_options->speed_only == true)
  605   {
  606     // we want to get the num_cracked in benchmark mode because it has an influence in performance
  607     // however if the benchmark cracks the artificial hash used for benchmarks we don't want to see that!
  608 
  609     return 0;
  610   }
  611 
  612   plain_t *cracked = (plain_t *) hcmalloc (num_cracked * sizeof (plain_t));
  613 
  614   if (device_param->is_cuda == true)
  615   {
  616     rc = hc_cuMemcpyDtoHAsync (hashcat_ctx, cracked, device_param->cuda_d_plain_bufs, num_cracked * sizeof (plain_t), device_param->cuda_stream);
  617 
  618     if (rc == 0)
  619     {
  620       rc = hc_cuStreamSynchronize (hashcat_ctx, device_param->cuda_stream);
  621     }
  622 
  623     if (rc == -1)
  624     {
  625       hcfree (cracked);
  626 
  627       return -1;
  628     }
  629   }
  630 
  631   if (device_param->is_hip == true)
  632   {
  633     rc = hc_hipMemcpyDtoHAsync (hashcat_ctx, cracked, device_param->hip_d_plain_bufs, num_cracked * sizeof (plain_t), device_param->hip_stream);
  634 
  635     if (rc == 0)
  636     {
  637       rc = hc_hipStreamSynchronize (hashcat_ctx, device_param->hip_stream);
  638     }
  639 
  640     if (rc == -1)
  641     {
  642       hcfree (cracked);
  643 
  644       return -1;
  645     }
  646   }
  647 
  648   #if defined (__APPLE__)
  649   if (device_param->is_metal == true)
  650   {
  651     rc = hc_mtlMemcpyDtoH (hashcat_ctx, device_param->metal_command_queue, cracked, device_param->metal_d_plain_bufs, 0, num_cracked * sizeof (plain_t));
  652 
  653     if (rc == -1)
  654     {
  655       hcfree (cracked);
  656 
  657       return -1;
  658     }
  659   }
  660   #endif
  661 
  662   if (device_param->is_opencl == true)
  663   {
  664     /* blocking */
  665     rc = hc_clEnqueueReadBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_plain_bufs, CL_TRUE, 0, num_cracked * sizeof (plain_t), cracked, 0, NULL, NULL);
  666 
  667     if (rc == -1)
  668     {
  669       hcfree (cracked);
  670 
  671       return -1;
  672     }
  673   }
  674 
  675   u32 cpt_cracked = 0;
  676 
  677   hc_thread_mutex_lock (status_ctx->mux_display);
  678 
  679   for (u32 i = 0; i < num_cracked; i++)
  680   {
  681     const u32 hash_pos = cracked[i].hash_pos;
  682 
  683     if (hashes->digests_shown[hash_pos] == 1) continue;
  684 
  685     const u32 salt_pos = cracked[i].salt_pos;
  686     salt_t *salt_buf = &hashes->salts_buf[salt_pos];
  687 
  688     if ((hashconfig->opts_type & OPTS_TYPE_PT_NEVERCRACK) == 0)
  689     {
  690       hashes->digests_shown[hash_pos] = 1;
  691 
  692       hashes->digests_done++;
  693 
  694       hashes->digests_done_new++;
  695 
  696       cpt_cracked++;
  697 
  698       salt_buf->digests_done++;
  699 
  700       if (salt_buf->digests_done == salt_buf->digests_cnt)
  701       {
  702         hashes->salts_shown[salt_pos] = 1;
  703 
  704         hashes->salts_done++;
  705       }
  706     }
  707 
  708     if (hashes->salts_done == hashes->salts_cnt) mycracked (hashcat_ctx);
  709 
  710     rc = check_hash (hashcat_ctx, device_param, &cracked[i]);
  711 
  712     if (rc == -1)
  713     {
  714       break;
  715     }
  716 
  717     if (hashconfig->opts_type & OPTS_TYPE_PT_NEVERCRACK)
  718     {
  719       // we need to reset cracked state on the device
  720       // otherwise host thinks again and again the hash was cracked
  721       // and returns invalid password each time
  722 
  723       if (device_param->is_cuda == true)
  724       {
  725         rc = run_cuda_kernel_bzero (hashcat_ctx, device_param, device_param->cuda_d_digests_shown + (salt_buf->digests_offset * sizeof (u32)), salt_buf->digests_cnt * sizeof (u32));
  726 
  727         if (rc == -1)
  728         {
  729           break;
  730         }
  731       }
  732 
  733       if (device_param->is_hip == true)
  734       {
  735         rc = run_hip_kernel_bzero (hashcat_ctx, device_param, device_param->hip_d_digests_shown + (salt_buf->digests_offset * sizeof (u32)), salt_buf->digests_cnt * sizeof (u32));
  736 
  737         if (rc == -1)
  738         {
  739           break;
  740         }
  741       }
  742 
  743       #if defined (__APPLE__)
  744       if (device_param->is_metal == true)
  745       {
  746         rc = run_metal_kernel_memset32 (hashcat_ctx, device_param, device_param->metal_d_digests_shown, salt_buf->digests_offset * sizeof (u32), 0, salt_buf->digests_cnt * sizeof (u32));
  747 
  748         if (rc == -1)
  749         {
  750           break;
  751         }
  752       }
  753       #endif
  754 
  755       if (device_param->is_opencl == true)
  756       {
  757         /* NOTE: run_opencl_kernel_bzero() does not handle buffer offset */
  758         rc = run_opencl_kernel_memset32 (hashcat_ctx, device_param, device_param->opencl_d_digests_shown, salt_buf->digests_offset * sizeof (u32), 0, salt_buf->digests_cnt * sizeof (u32));
  759 
  760         if (rc == -1)
  761         {
  762           break;
  763         }
  764       }
  765     }
  766   }
  767 
  768   hc_thread_mutex_unlock (status_ctx->mux_display);
  769 
  770   hcfree (cracked);
  771 
  772   if (rc == -1)
  773   {
  774     return -1;
  775   }
  776 
  777   if (cpt_cracked > 0)
  778   {
  779     hc_thread_mutex_lock (status_ctx->mux_display);
  780 
  781     cpt_ctx->cpt_buf[cpt_ctx->cpt_pos].timestamp = time (NULL);
  782     cpt_ctx->cpt_buf[cpt_ctx->cpt_pos].cracked   = cpt_cracked;
  783 
  784     cpt_ctx->cpt_pos++;
  785 
  786     cpt_ctx->cpt_total += cpt_cracked;
  787 
  788     if (cpt_ctx->cpt_pos == CPT_CACHE) cpt_ctx->cpt_pos = 0;
  789 
  790     hc_thread_mutex_unlock (status_ctx->mux_display);
  791   }
  792 
  793   if (device_param->is_cuda == true)
  794   {
  795     if (run_cuda_kernel_bzero (hashcat_ctx, device_param, device_param->cuda_d_result, sizeof (u32)) == -1) return -1;
  796   }
  797 
  798   if (device_param->is_hip == true)
  799   {
  800     if (run_hip_kernel_bzero (hashcat_ctx, device_param, device_param->hip_d_result, sizeof (u32)) == -1) return -1;
  801   }
  802 
  803   #if defined (__APPLE__)
  804   if (device_param->is_metal == true)
  805   {
  806     if (run_metal_kernel_bzero (hashcat_ctx, device_param, device_param->metal_d_result, sizeof (u32)) == -1) return -1;
  807   }
  808   #endif
  809 
  810   if (device_param->is_opencl == true)
  811   {
  812     if (run_opencl_kernel_bzero (hashcat_ctx, device_param, device_param->opencl_d_result, sizeof (u32)) == -1) return -1;
  813 
  814     if (hc_clFlush (hashcat_ctx, device_param->opencl_command_queue) == -1) return -1;
  815   }
  816 
  817   return 0;
  818 }
  819 
  820 int hashes_init_filename (hashcat_ctx_t *hashcat_ctx)
  821 {
  822   hashconfig_t         *hashconfig         = hashcat_ctx->hashconfig;
  823   hashes_t             *hashes             = hashcat_ctx->hashes;
  824   user_options_t       *user_options       = hashcat_ctx->user_options;
  825   user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
  826 
  827   if (user_options->benchmark == true) return 0;
  828 
  829   /**
  830    * load hashes, part I: find input mode, count hashes
  831    */
  832 
  833   if (hashconfig->opts_type & OPTS_TYPE_BINARY_HASHFILE)
  834   {
  835     if (hashconfig->opts_type & OPTS_TYPE_BINARY_HASHFILE_OPTIONAL)
  836     {
  837       if ((user_options->benchmark == false) && (user_options->keyspace == false))
  838       {
  839         hashes->hashlist_mode = (hc_path_exist (user_options_extra->hc_hash) == true) ? HL_MODE_FILE_PLAIN : HL_MODE_ARG;
  840 
  841         if (hashes->hashlist_mode == HL_MODE_FILE_PLAIN)
  842         {
  843           hashes->hashfile = user_options_extra->hc_hash;
  844         }
  845       }
  846     }
  847     else
  848     {
  849       hashes->hashlist_mode = HL_MODE_FILE_BINARY;
  850 
  851       if ((user_options->benchmark == false) && (user_options->keyspace == false))
  852       {
  853         if (hc_path_read (user_options_extra->hc_hash) == false)
  854         {
  855           event_log_error (hashcat_ctx, "%s: %s", user_options_extra->hc_hash, strerror (errno));
  856 
  857           return -1;
  858         }
  859 
  860         hashes->hashfile = user_options_extra->hc_hash;
  861       }
  862     }
  863   }
  864   else
  865   {
  866     hashes->hashlist_mode = (hc_path_exist (user_options_extra->hc_hash) == true) ? HL_MODE_FILE_PLAIN : HL_MODE_ARG;
  867 
  868     if (hashes->hashlist_mode == HL_MODE_FILE_PLAIN)
  869     {
  870       hashes->hashfile = user_options_extra->hc_hash;
  871     }
  872   }
  873 
  874   hashes->parser_token_length_cnt = 0;
  875 
  876   return 0;
  877 }
  878 
  879 int hashes_init_stage1 (hashcat_ctx_t *hashcat_ctx)
  880 {
  881   hashconfig_t          *hashconfig         = hashcat_ctx->hashconfig;
  882   hashes_t              *hashes             = hashcat_ctx->hashes;
  883   module_ctx_t          *module_ctx         = hashcat_ctx->module_ctx;
  884   user_options_t        *user_options       = hashcat_ctx->user_options;
  885   user_options_extra_t  *user_options_extra = hashcat_ctx->user_options_extra;
  886 
  887   /**
  888    * load hashes, part I: find input mode, count hashes
  889    */
  890 
  891   const char *hashfile      = hashes->hashfile;
  892   const u32   hashlist_mode = hashes->hashlist_mode;
  893 
  894   u32 hashlist_format = HLFMT_HASHCAT;
  895 
  896   u64 hashes_avail = 0;
  897 
  898   if ((user_options->benchmark == false) && (user_options->stdout_flag == false) && (user_options->keyspace == false))
  899   {
  900     if (hashlist_mode == HL_MODE_ARG)
  901     {
  902       hashes_avail = 1;
  903     }
  904     else if (hashlist_mode == HL_MODE_FILE_PLAIN)
  905     {
  906       HCFILE fp;
  907 
  908       if (hc_fopen (&fp, hashfile, "rb") == false)
  909       {
  910         event_log_error (hashcat_ctx, "%s: %s", hashfile, strerror (errno));
  911 
  912         return -1;
  913       }
  914 
  915       EVENT_DATA (EVENT_HASHLIST_COUNT_LINES_PRE, hashfile, strlen (hashfile));
  916 
  917       hashes_avail = count_lines (&fp);
  918 
  919       EVENT_DATA (EVENT_HASHLIST_COUNT_LINES_POST, hashfile, strlen (hashfile));
  920 
  921       hc_rewind (&fp);
  922 
  923       if (hashes_avail == 0)
  924       {
  925         event_log_error (hashcat_ctx, "hashfile is empty or corrupt.");
  926 
  927         hc_fclose (&fp);
  928 
  929         return -1;
  930       }
  931 
  932       hashlist_format = hlfmt_detect (hashcat_ctx, &fp, 100); // 100 = max numbers to "scan". could be hashes_avail, too
  933 
  934       hc_fclose (&fp);
  935 
  936       if ((user_options->remove == true) && (hashlist_format != HLFMT_HASHCAT))
  937       {
  938         event_log_error (hashcat_ctx, "Use of --remove is not supported in native hashfile-format mode.");
  939 
  940         return -1;
  941       }
  942     }
  943     else if (hashlist_mode == HL_MODE_FILE_BINARY)
  944     {
  945       struct stat st;
  946 
  947       if (stat (hashes->hashfile, &st) == -1)
  948       {
  949         event_log_error (hashcat_ctx, "%s: %s", hashes->hashfile, strerror (errno));
  950 
  951         return -1;
  952       }
  953 
  954       if (module_ctx->module_hash_binary_count != MODULE_DEFAULT)
  955       {
  956         const int binary_count = module_ctx->module_hash_binary_count (hashes);
  957 
  958         if (binary_count > 0)
  959         {
  960           hashes_avail = binary_count;
  961         }
  962         else if (binary_count == 0)
  963         {
  964           event_log_error (hashcat_ctx, "No hashes loaded.");
  965 
  966           return -1;
  967         }
  968         else if (binary_count == PARSER_HAVE_ERRNO)
  969         {
  970           event_log_error (hashcat_ctx, "%s: %s", hashes->hashfile, strerror (errno));
  971 
  972           return -1;
  973         }
  974         else
  975         {
  976           event_log_error (hashcat_ctx, "%s: %s", hashes->hashfile, strerror (binary_count));
  977 
  978           return -1;
  979         }
  980       }
  981       else
  982       {
  983         hashes_avail = 1;
  984       }
  985     }
  986   }
  987   else
  988   {
  989     hashes_avail = 1;
  990   }
  991 
  992   if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT) hashes_avail *= 2;
  993 
  994   hashes->hashlist_format = hashlist_format;
  995 
  996   /**
  997    * load hashes, part II: allocate required memory, set pointers
  998    */
  999 
 1000   hash_t *hashes_buf     = (hash_t *) hccalloc (hashes_avail, sizeof (hash_t));
 1001   void   *digests_buf    =            hccalloc (hashes_avail, hashconfig->dgst_size);
 1002   salt_t *salts_buf      = NULL;
 1003   void   *esalts_buf     = NULL;
 1004   void   *hook_salts_buf = NULL;
 1005 
 1006   if ((user_options->username == true) || (hashconfig->opts_type & OPTS_TYPE_HASH_COPY) || (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT))
 1007   {
 1008     u64 hash_pos;
 1009 
 1010     for (hash_pos = 0; hash_pos < hashes_avail; hash_pos++)
 1011     {
 1012       hashinfo_t *hash_info = (hashinfo_t *) hcmalloc (sizeof (hashinfo_t));
 1013 
 1014       hashes_buf[hash_pos].hash_info = hash_info;
 1015 
 1016       if (user_options->username == true)
 1017       {
 1018         hash_info->user = (user_t *) hcmalloc (sizeof (user_t));
 1019       }
 1020 
 1021       if (hashconfig->opts_type & OPTS_TYPE_HASH_COPY)
 1022       {
 1023         if (user_options->benchmark == false)
 1024         {
 1025           hash_info->orighash = (char *) hcmalloc (256);
 1026         }
 1027       }
 1028 
 1029       if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
 1030       {
 1031         hash_info->split = (split_t *) hcmalloc (sizeof (split_t));
 1032       }
 1033     }
 1034   }
 1035 
 1036   if (hashconfig->is_salted == true)
 1037   {
 1038     salts_buf = (salt_t *) hccalloc (hashes_avail, sizeof (salt_t));
 1039 
 1040     if (user_options->attack_mode == ATTACK_MODE_ASSOCIATION)
 1041     {
 1042       // this disables:
 1043       // - sorting by salt value
 1044       // - grouping by salt value
 1045       // - keep the salt in position relative to hashfile (not equal because of some hashes maybe failed to load)
 1046 
 1047       u64 hash_pos;
 1048 
 1049       for (hash_pos = 0; hash_pos < hashes_avail; hash_pos++)
 1050       {
 1051         salt_t *salt = &salts_buf[hash_pos];
 1052 
 1053         salt->orig_pos = hash_pos;
 1054       }
 1055     }
 1056 
 1057     if (hashconfig->esalt_size > 0)
 1058     {
 1059       esalts_buf = hccalloc (hashes_avail, hashconfig->esalt_size);
 1060     }
 1061 
 1062     if (hashconfig->hook_salt_size > 0)
 1063     {
 1064       hook_salts_buf = hccalloc (hashes_avail, hashconfig->hook_salt_size);
 1065     }
 1066   }
 1067   else
 1068   {
 1069     salts_buf = (salt_t *) hccalloc (1, sizeof (salt_t));
 1070   }
 1071 
 1072   for (u64 hash_pos = 0; hash_pos < hashes_avail; hash_pos++)
 1073   {
 1074     /**
 1075      * Initialize some values for later use
 1076      */
 1077 
 1078     hashes_buf[hash_pos].orig_line_pos = hash_pos;
 1079 
 1080     hashes_buf[hash_pos].digest = ((char *) digests_buf) + (hash_pos * hashconfig->dgst_size);
 1081 
 1082     if (hashconfig->is_salted == true)
 1083     {
 1084       hashes_buf[hash_pos].salt = &salts_buf[hash_pos];
 1085 
 1086       if (hashconfig->esalt_size > 0)
 1087       {
 1088         hashes_buf[hash_pos].esalt = ((char *) esalts_buf) + (hash_pos * hashconfig->esalt_size);
 1089       }
 1090 
 1091       if (hashconfig->hook_salt_size > 0)
 1092       {
 1093         hashes_buf[hash_pos].hook_salt = ((char *) hook_salts_buf) + (hash_pos * hashconfig->hook_salt_size);
 1094       }
 1095     }
 1096     else
 1097     {
 1098       hashes_buf[hash_pos].salt = &salts_buf[0];
 1099     }
 1100   }
 1101 
 1102   hashes->hashes_buf     = hashes_buf;
 1103   hashes->digests_buf    = digests_buf;
 1104   hashes->salts_buf      = salts_buf;
 1105   hashes->esalts_buf     = esalts_buf;
 1106   hashes->hook_salts_buf = hook_salts_buf;
 1107 
 1108   /**
 1109    * load hashes, part III: parse hashes
 1110    */
 1111 
 1112   u32 hashes_cnt = 0;
 1113 
 1114   if (user_options->benchmark == true)
 1115   {
 1116     hashes->hashfile = "-";
 1117 
 1118     hashes_cnt = 1;
 1119   }
 1120   else if (user_options->hash_info == true)
 1121   {
 1122   }
 1123   else if (user_options->keyspace == true)
 1124   {
 1125   }
 1126   else if (user_options->stdout_flag == true)
 1127   {
 1128   }
 1129   else if (user_options->backend_info > 0)
 1130   {
 1131   }
 1132   else
 1133   {
 1134     if (hashlist_mode == HL_MODE_ARG)
 1135     {
 1136       char *input_buf = user_options_extra->hc_hash;
 1137 
 1138       size_t input_len = strlen (input_buf);
 1139 
 1140       char  *hash_buf = NULL;
 1141       int    hash_len = 0;
 1142 
 1143       hlfmt_hash (hashcat_ctx, hashlist_format, input_buf, input_len, &hash_buf, &hash_len);
 1144 
 1145       bool hash_fmt_error = false;
 1146 
 1147       if (hash_len < 1)     hash_fmt_error = true;
 1148       if (hash_buf == NULL) hash_fmt_error = true;
 1149 
 1150       if (hash_fmt_error)
 1151       {
 1152         event_log_warning (hashcat_ctx, "Failed to parse hashes using the '%s' format.", strhlfmt (hashlist_format));
 1153       }
 1154       else
 1155       {
 1156         if (hashconfig->opts_type & OPTS_TYPE_HASH_COPY)
 1157         {
 1158           hashinfo_t *hash_info_tmp = hashes_buf[hashes_cnt].hash_info;
 1159 
 1160           hash_info_tmp->orighash = hcstrdup (hash_buf);
 1161         }
 1162 
 1163         if (hashconfig->is_salted == true)
 1164         {
 1165           memset (hashes_buf[0].salt, 0, sizeof (salt_t));
 1166         }
 1167 
 1168         if (hashconfig->esalt_size > 0)
 1169         {
 1170           memset (hashes_buf[0].esalt, 0, hashconfig->esalt_size);
 1171         }
 1172 
 1173         if (hashconfig->hook_salt_size > 0)
 1174         {
 1175           memset (hashes_buf[0].hook_salt, 0, hashconfig->hook_salt_size);
 1176         }
 1177 
 1178         int parser_status = PARSER_OK;
 1179 
 1180         if (user_options->username == true)
 1181         {
 1182           char *user_buf = NULL;
 1183           int   user_len = 0;
 1184 
 1185           hlfmt_user (hashcat_ctx, hashlist_format, input_buf, input_len, &user_buf, &user_len);
 1186 
 1187           // special case:
 1188           // both hash_t need to have the username info if the pwdump format is used (i.e. we have 2 hashes for 3000, both with same user)
 1189 
 1190           u32 hashes_per_user = 1;
 1191 
 1192           if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
 1193           {
 1194             // the following conditions should be true if (hashlist_format == HLFMT_PWDUMP)
 1195 
 1196             if (hash_len == 32)
 1197             {
 1198               hashes_per_user = 2;
 1199             }
 1200           }
 1201 
 1202           for (u32 i = 0; i < hashes_per_user; i++)
 1203           {
 1204             user_t **user = &hashes_buf[hashes_cnt + i].hash_info->user;
 1205 
 1206             *user = (user_t *) hcmalloc (sizeof (user_t));
 1207 
 1208             user_t *user_ptr = *user;
 1209 
 1210             if (user_buf != NULL)
 1211             {
 1212               user_ptr->user_name = hcstrdup (user_buf);
 1213             }
 1214             else
 1215             {
 1216               user_ptr->user_name = hcstrdup ("");
 1217             }
 1218 
 1219             user_ptr->user_len = (u32) user_len;
 1220           }
 1221         }
 1222 
 1223         if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
 1224         {
 1225           if (hash_len == 32)
 1226           {
 1227             hash_t *hash;
 1228 
 1229             hash = &hashes_buf[hashes_cnt];
 1230 
 1231             parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf +  0, 16);
 1232 
 1233             if (parser_status == PARSER_OK)
 1234             {
 1235               if (module_ctx->module_hash_decode_postprocess != MODULE_DEFAULT)
 1236               {
 1237                 parser_status = module_ctx->module_hash_decode_postprocess (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, user_options, user_options_extra);
 1238 
 1239                 if (parser_status == PARSER_OK)
 1240                 {
 1241                   // nothing to do
 1242                 }
 1243                 else
 1244                 {
 1245                   event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
 1246                 }
 1247               }
 1248 
 1249               hashes_buf[hashes_cnt].hash_info->split->split_group  = 0;
 1250               hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_LEFT;
 1251 
 1252               hashes_cnt++;
 1253             }
 1254             else
 1255             {
 1256               event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
 1257             }
 1258 
 1259             if (parser_status == PARSER_TOKEN_LENGTH)
 1260             {
 1261               hashes->parser_token_length_cnt++;
 1262             }
 1263 
 1264             hash = &hashes_buf[hashes_cnt];
 1265 
 1266             parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf + 16, 16);
 1267 
 1268             if (parser_status == PARSER_OK)
 1269             {
 1270               if (module_ctx->module_hash_decode_postprocess != MODULE_DEFAULT)
 1271               {
 1272                 parser_status = module_ctx->module_hash_decode_postprocess (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, user_options, user_options_extra);
 1273 
 1274                 if (parser_status == PARSER_OK)
 1275                 {
 1276                   // nothing to do
 1277                 }
 1278                 else
 1279                 {
 1280                   event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
 1281                 }
 1282               }
 1283 
 1284               hashes_buf[hashes_cnt].hash_info->split->split_group  = 0;
 1285               hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_RIGHT;
 1286 
 1287               hashes_cnt++;
 1288             }
 1289             else
 1290             {
 1291               event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
 1292             }
 1293 
 1294             if (parser_status == PARSER_TOKEN_LENGTH)
 1295             {
 1296               hashes->parser_token_length_cnt++;
 1297             }
 1298           }
 1299           else
 1300           {
 1301             hash_t *hash = &hashes_buf[hashes_cnt];
 1302 
 1303             parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf, hash_len);
 1304 
 1305             if (parser_status == PARSER_OK)
 1306             {
 1307               if (module_ctx->module_hash_decode_postprocess != MODULE_DEFAULT)
 1308               {
 1309                 parser_status = module_ctx->module_hash_decode_postprocess (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, user_options, user_options_extra);
 1310 
 1311                 if (parser_status == PARSER_OK)
 1312                 {
 1313                   // nothing to do
 1314                 }
 1315                 else
 1316                 {
 1317                   event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
 1318                 }
 1319               }
 1320 
 1321               hashes_buf[hashes_cnt].hash_info->split->split_group  = 0;
 1322               hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_NONE;
 1323 
 1324               hashes_cnt++;
 1325             }
 1326             else
 1327             {
 1328               event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
 1329             }
 1330 
 1331             if (parser_status == PARSER_TOKEN_LENGTH)
 1332             {
 1333               hashes->parser_token_length_cnt++;
 1334             }
 1335           }
 1336         }
 1337         else
 1338         {
 1339           hash_t *hash = &hashes_buf[hashes_cnt];
 1340 
 1341           parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf, hash_len);
 1342 
 1343           if (parser_status == PARSER_OK)
 1344           {
 1345             if (module_ctx->module_hash_decode_postprocess != MODULE_DEFAULT)
 1346             {
 1347               parser_status = module_ctx->module_hash_decode_postprocess (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, user_options, user_options_extra);
 1348 
 1349               if (parser_status == PARSER_OK)
 1350               {
 1351                 // nothing to do
 1352               }
 1353               else
 1354               {
 1355                 event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
 1356               }
 1357             }
 1358 
 1359             hashes_cnt++;
 1360           }
 1361           else
 1362           {
 1363             event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
 1364           }
 1365 
 1366           if (parser_status == PARSER_TOKEN_LENGTH)
 1367           {
 1368             hashes->parser_token_length_cnt++;
 1369           }
 1370         }
 1371       }
 1372     }
 1373     else if (hashlist_mode == HL_MODE_FILE_PLAIN)
 1374     {
 1375       HCFILE fp;
 1376 
 1377       if (hc_fopen (&fp, hashfile, "rb") == false)
 1378       {
 1379         event_log_error (hashcat_ctx, "%s: %s", hashfile, strerror (errno));
 1380 
 1381         return -1;
 1382       }
 1383 
 1384       u32 line_num = 0;
 1385 
 1386       char *line_buf = (char *) hcmalloc (HCBUFSIZ_LARGE);
 1387 
 1388       time_t prev = 0;
 1389       time_t now  = 0;
 1390 
 1391       while (!hc_feof (&fp))
 1392       {
 1393         line_num++;
 1394 
 1395         const size_t line_len = fgetl (&fp, line_buf, HCBUFSIZ_LARGE);
 1396 
 1397         if (line_len == 0) continue;
 1398 
 1399         if (hashes_avail == hashes_cnt)
 1400         {
 1401           event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u: File changed during runtime. Skipping new data.", hashes->hashfile, line_num);
 1402 
 1403           break;
 1404         }
 1405 
 1406         char *hash_buf = NULL;
 1407         int   hash_len = 0;
 1408 
 1409         hlfmt_hash (hashcat_ctx, hashlist_format, line_buf, line_len, &hash_buf, &hash_len);
 1410 
 1411         bool hash_fmt_error = false;
 1412 
 1413         if (hash_len < 1)     hash_fmt_error = true;
 1414         if (hash_buf == NULL) hash_fmt_error = true;
 1415 
 1416         if (hash_fmt_error)
 1417         {
 1418           event_log_warning (hashcat_ctx, "Failed to parse hashes using the '%s' format.", strhlfmt (hashlist_format));
 1419 
 1420           continue;
 1421         }
 1422 
 1423         if (user_options->username == true)
 1424         {
 1425           char *user_buf = NULL;
 1426           int   user_len = 0;
 1427 
 1428           hlfmt_user (hashcat_ctx, hashlist_format, line_buf, line_len, &user_buf, &user_len);
 1429 
 1430           // special case:
 1431           // both hash_t need to have the username info if the pwdump format is used (i.e. we have 2 hashes for 3000, both with same user)
 1432 
 1433           u32 hashes_per_user = 1;
 1434 
 1435           if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
 1436           {
 1437             // the following conditions should be true if (hashlist_format == HLFMT_PWDUMP)
 1438 
 1439             if (hash_len == 32)
 1440             {
 1441               hashes_per_user = 2;
 1442             }
 1443           }
 1444 
 1445           for (u32 i = 0; i < hashes_per_user; i++)
 1446           {
 1447             user_t **user = &hashes_buf[hashes_cnt + i].hash_info->user;
 1448 
 1449             *user = (user_t *) hcmalloc (sizeof (user_t));
 1450 
 1451             user_t *user_ptr = *user;
 1452 
 1453             if (user_buf != NULL)
 1454             {
 1455               user_ptr->user_name = hcstrdup (user_buf);
 1456             }
 1457             else
 1458             {
 1459               user_ptr->user_name = hcstrdup ("");
 1460             }
 1461 
 1462             user_ptr->user_len = (u32) user_len;
 1463           }
 1464         }
 1465 
 1466         if (hashconfig->opts_type & OPTS_TYPE_HASH_COPY)
 1467         {
 1468           hashinfo_t *hash_info_tmp = hashes_buf[hashes_cnt].hash_info;
 1469 
 1470           hash_info_tmp->orighash = hcstrdup (hash_buf);
 1471         }
 1472 
 1473         if (hashconfig->is_salted == true)
 1474         {
 1475           const u32 orig_pos = hashes_buf[hashes_cnt].salt->orig_pos;
 1476 
 1477           memset (hashes_buf[hashes_cnt].salt, 0, sizeof (salt_t));
 1478 
 1479           hashes_buf[hashes_cnt].salt->orig_pos = orig_pos;
 1480         }
 1481 
 1482         if (hashconfig->esalt_size > 0)
 1483         {
 1484           memset (hashes_buf[hashes_cnt].esalt, 0, hashconfig->esalt_size);
 1485         }
 1486 
 1487         if (hashconfig->hook_salt_size > 0)
 1488         {
 1489           memset (hashes_buf[hashes_cnt].hook_salt, 0, hashconfig->hook_salt_size);
 1490         }
 1491 
 1492         if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
 1493         {
 1494           if (hash_len == 32)
 1495           {
 1496             hash_t *hash;
 1497 
 1498             hash = &hashes_buf[hashes_cnt];
 1499 
 1500             int parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf +  0, 16);
 1501 
 1502             if (parser_status < PARSER_GLOBAL_ZERO)
 1503             {
 1504               char *tmp_line_buf;
 1505 
 1506               hc_asprintf (&tmp_line_buf, "%s", line_buf);
 1507 
 1508               compress_terminal_line_length (tmp_line_buf, 38, 32);
 1509 
 1510               if (user_options->machine_readable == true)
 1511               {
 1512                 event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
 1513               }
 1514               else
 1515               {
 1516                 event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
 1517               }
 1518 
 1519               hcfree (tmp_line_buf);
 1520 
 1521               continue;
 1522             }
 1523 
 1524             if (module_ctx->module_hash_decode_postprocess != MODULE_DEFAULT)
 1525             {
 1526               int parser_status_postprocess = module_ctx->module_hash_decode_postprocess (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, user_options, user_options_extra);
 1527 
 1528               if (parser_status_postprocess < PARSER_GLOBAL_ZERO)
 1529               {
 1530                 char *tmp_line_buf;
 1531 
 1532                 hc_asprintf (&tmp_line_buf, "%s", line_buf);
 1533 
 1534                 compress_terminal_line_length (tmp_line_buf, 38, 32);
 1535 
 1536                 if (user_options->machine_readable == true)
 1537                 {
 1538                   event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status_postprocess));
 1539                 }
 1540                 else
 1541                 {
 1542                   event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status_postprocess));
 1543                 }
 1544 
 1545                 hcfree (tmp_line_buf);
 1546 
 1547                 continue;
 1548               }
 1549             }
 1550 
 1551             hashes_buf[hashes_cnt].hash_info->split->split_group  = line_num;
 1552             hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_LEFT;
 1553 
 1554             hashes_cnt++;
 1555 
 1556             hash = &hashes_buf[hashes_cnt];
 1557 
 1558             parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf + 16, 16);
 1559 
 1560             if (parser_status < PARSER_GLOBAL_ZERO)
 1561             {
 1562               char *tmp_line_buf;
 1563 
 1564               hc_asprintf (&tmp_line_buf, "%s", line_buf);
 1565 
 1566               compress_terminal_line_length (tmp_line_buf, 38, 32);
 1567 
 1568               if (user_options->machine_readable == true)
 1569               {
 1570                 event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
 1571               }
 1572               else
 1573               {
 1574                 event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
 1575               }
 1576 
 1577               hcfree (tmp_line_buf);
 1578 
 1579               continue;
 1580             }
 1581 
 1582             if (module_ctx->module_hash_decode_postprocess != MODULE_DEFAULT)
 1583             {
 1584               int parser_status_postprocess = module_ctx->module_hash_decode_postprocess (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, user_options, user_options_extra);
 1585 
 1586               if (parser_status_postprocess < PARSER_GLOBAL_ZERO)
 1587               {
 1588                 char *tmp_line_buf;
 1589 
 1590                 hc_asprintf (&tmp_line_buf, "%s", line_buf);
 1591 
 1592                 compress_terminal_line_length (tmp_line_buf, 38, 32);
 1593 
 1594                 if (user_options->machine_readable == true)
 1595                 {
 1596                   event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status_postprocess));
 1597                 }
 1598                 else
 1599                 {
 1600                   event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status_postprocess));
 1601                 }
 1602 
 1603                 hcfree (tmp_line_buf);
 1604 
 1605                 continue;
 1606               }
 1607             }
 1608 
 1609             hashes_buf[hashes_cnt].hash_info->split->split_group  = line_num;
 1610             hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_RIGHT;
 1611 
 1612             hashes_cnt++;
 1613           }
 1614           else
 1615           {
 1616             hash_t *hash = &hashes_buf[hashes_cnt];
 1617 
 1618             int parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf, hash_len);
 1619 
 1620             if (parser_status < PARSER_GLOBAL_ZERO)
 1621             {
 1622               char *tmp_line_buf;
 1623 
 1624               hc_asprintf (&tmp_line_buf, "%s", line_buf);
 1625 
 1626               compress_terminal_line_length (tmp_line_buf, 38, 32);
 1627 
 1628               if (user_options->machine_readable == true)
 1629               {
 1630                 event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
 1631               }
 1632               else
 1633               {
 1634                 event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
 1635               }
 1636 
 1637               hcfree (tmp_line_buf);
 1638 
 1639               continue;
 1640             }
 1641 
 1642             if (module_ctx->module_hash_decode_postprocess != MODULE_DEFAULT)
 1643             {
 1644               int parser_status_postprocess = module_ctx->module_hash_decode_postprocess (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, user_options, user_options_extra);
 1645 
 1646               if (parser_status_postprocess < PARSER_GLOBAL_ZERO)
 1647               {
 1648                 char *tmp_line_buf;
 1649 
 1650                 hc_asprintf (&tmp_line_buf, "%s", line_buf);
 1651 
 1652                 compress_terminal_line_length (tmp_line_buf, 38, 32);
 1653 
 1654                 if (user_options->machine_readable == true)
 1655                 {
 1656                   event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status_postprocess));
 1657                 }
 1658                 else
 1659                 {
 1660                   event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status_postprocess));
 1661                 }
 1662 
 1663                 hcfree (tmp_line_buf);
 1664 
 1665                 continue;
 1666               }
 1667             }
 1668 
 1669             hashes_buf[hashes_cnt].hash_info->split->split_group  = line_num;
 1670             hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_NONE;
 1671 
 1672             hashes_cnt++;
 1673           }
 1674         }
 1675         else
 1676         {
 1677           hash_t *hash = &hashes_buf[hashes_cnt];
 1678 
 1679           int parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf, hash_len);
 1680 
 1681           if (parser_status < PARSER_GLOBAL_ZERO)
 1682           {
 1683             char *tmp_line_buf;
 1684 
 1685             hc_asprintf (&tmp_line_buf, "%s", line_buf);
 1686 
 1687             compress_terminal_line_length (tmp_line_buf, 38, 32);
 1688 
 1689             if (user_options->machine_readable == true)
 1690             {
 1691               event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
 1692             }
 1693             else
 1694             {
 1695               event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
 1696             }
 1697 
 1698             hcfree (tmp_line_buf);
 1699 
 1700             if (parser_status == PARSER_TOKEN_LENGTH)
 1701             {
 1702               hashes->parser_token_length_cnt++;
 1703             }
 1704 
 1705             continue;
 1706           }
 1707 
 1708           if (module_ctx->module_hash_decode_postprocess != MODULE_DEFAULT)
 1709           {
 1710             int parser_status_postprocess = module_ctx->module_hash_decode_postprocess (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, user_options, user_options_extra);
 1711 
 1712             if (parser_status_postprocess < PARSER_GLOBAL_ZERO)
 1713             {
 1714               char *tmp_line_buf;
 1715 
 1716               hc_asprintf (&tmp_line_buf, "%s", line_buf);
 1717 
 1718               compress_terminal_line_length (tmp_line_buf, 38, 32);
 1719 
 1720               if (user_options->machine_readable == true)
 1721               {
 1722                 event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status_postprocess));
 1723               }
 1724               else
 1725               {
 1726                 event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status_postprocess));
 1727               }
 1728 
 1729               hcfree (tmp_line_buf);
 1730 
 1731               if (parser_status_postprocess == PARSER_TOKEN_LENGTH)
 1732               {
 1733                 hashes->parser_token_length_cnt++;
 1734               }
 1735 
 1736               continue;
 1737             }
 1738           }
 1739 
 1740           hashes_cnt++;
 1741         }
 1742 
 1743         time (&now);
 1744 
 1745         if ((now - prev) == 0) continue;
 1746 
 1747         time (&prev);
 1748 
 1749         hashlist_parse_t hashlist_parse;
 1750 
 1751         hashlist_parse.hashes_cnt   = hashes_cnt;
 1752         hashlist_parse.hashes_avail = hashes_avail;
 1753 
 1754         EVENT_DATA (EVENT_HASHLIST_PARSE_HASH, &hashlist_parse, sizeof (hashlist_parse_t));
 1755       }
 1756 
 1757       hashlist_parse_t hashlist_parse;
 1758 
 1759       hashlist_parse.hashes_cnt   = hashes_cnt;
 1760       hashlist_parse.hashes_avail = hashes_avail;
 1761 
 1762       EVENT_DATA (EVENT_HASHLIST_PARSE_HASH, &hashlist_parse, sizeof (hashlist_parse_t));
 1763 
 1764       hcfree (line_buf);
 1765 
 1766       hc_fclose (&fp);
 1767     }
 1768     else if (hashlist_mode == HL_MODE_FILE_BINARY)
 1769     {
 1770       char *input_buf = user_options_extra->hc_hash;
 1771 
 1772       size_t input_len = strlen (input_buf);
 1773 
 1774       if (hashconfig->opts_type & OPTS_TYPE_HASH_COPY)
 1775       {
 1776         hashinfo_t *hash_info_tmp = hashes_buf[hashes_cnt].hash_info;
 1777 
 1778         hash_info_tmp->orighash = hcstrdup (input_buf);
 1779       }
 1780 
 1781       if (hashconfig->is_salted == true)
 1782       {
 1783         memset (hashes_buf[0].salt, 0, sizeof (salt_t));
 1784       }
 1785 
 1786       if (hashconfig->esalt_size > 0)
 1787       {
 1788         memset (hashes_buf[0].esalt, 0, hashconfig->esalt_size);
 1789       }
 1790 
 1791       if (hashconfig->hook_salt_size > 0)
 1792       {
 1793         memset (hashes_buf[0].hook_salt, 0, hashconfig->hook_salt_size);
 1794       }
 1795 
 1796       if (module_ctx->module_hash_binary_parse != MODULE_DEFAULT)
 1797       {
 1798         const int hashes_parsed = module_ctx->module_hash_binary_parse (hashconfig, user_options, user_options_extra, hashes);
 1799 
 1800         if (hashes_parsed > 0)
 1801         {
 1802           hashes_cnt = hashes_parsed;
 1803         }
 1804         else if (hashes_parsed == 0)
 1805         {
 1806           event_log_warning (hashcat_ctx, "No hashes loaded.");
 1807         }
 1808         else if (hashes_parsed == PARSER_HAVE_ERRNO)
 1809         {
 1810           event_log_warning (hashcat_ctx, "Hashfile '%s': %s", hashes->hashfile, strerror (errno));
 1811         }
 1812         else
 1813         {
 1814           event_log_warning (hashcat_ctx, "Hashfile '%s': %s", hashes->hashfile, strparser (hashes_parsed));
 1815         }
 1816       }
 1817       else
 1818       {
 1819         hash_t *hash = &hashes_buf[hashes_cnt];
 1820 
 1821         int parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, input_buf, input_len);
 1822 
 1823         if (parser_status == PARSER_OK)
 1824         {
 1825           if (module_ctx->module_hash_decode_postprocess != MODULE_DEFAULT)
 1826           {
 1827             parser_status = module_ctx->module_hash_decode_postprocess (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, user_options, user_options_extra);
 1828 
 1829             if (parser_status == PARSER_OK)
 1830             {
 1831               // nothing to do
 1832             }
 1833             else
 1834             {
 1835               event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
 1836             }
 1837           }
 1838 
 1839           hashes_cnt++;
 1840         }
 1841         else
 1842         {
 1843           event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
 1844         }
 1845 
 1846         if (parser_status == PARSER_TOKEN_LENGTH)
 1847         {
 1848           hashes->parser_token_length_cnt++;
 1849         }
 1850       }
 1851     }
 1852   }
 1853 
 1854   hashes->hashes_cnt = hashes_cnt;
 1855 
 1856   if (hashes_cnt)
 1857   {
 1858     EVENT (EVENT_HASHLIST_SORT_HASH_PRE);
 1859 
 1860     if (hashconfig->is_salted == true)
 1861     {
 1862       hc_qsort_r (hashes_buf, hashes_cnt, sizeof (hash_t), sort_by_hash, (void *) hashconfig);
 1863     }
 1864     else
 1865     {
 1866       hc_qsort_r (hashes_buf, hashes_cnt, sizeof (hash_t), sort_by_hash_no_salt, (void *) hashconfig);
 1867     }
 1868 
 1869     EVENT (EVENT_HASHLIST_SORT_HASH_POST);
 1870   }
 1871 
 1872   if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
 1873   {
 1874     // update split split_neighbor after sorting
 1875     // see https://github.com/hashcat/hashcat/issues/1034 for good examples for testing
 1876 
 1877     for (u32 i = 0; i < hashes_cnt; i++)
 1878     {
 1879       split_t *split1 = hashes_buf[i].hash_info->split;
 1880 
 1881       if (split1->split_origin != SPLIT_ORIGIN_LEFT) continue;
 1882 
 1883       for (u32 j = 0; j < hashes_cnt; j++)
 1884       {
 1885         split_t *split2 = hashes_buf[j].hash_info->split;
 1886 
 1887         if (split2->split_origin != SPLIT_ORIGIN_RIGHT) continue;
 1888 
 1889         if (split1->split_group != split2->split_group) continue;
 1890 
 1891         split1->split_neighbor = j;
 1892         split2->split_neighbor = i;
 1893 
 1894         break;
 1895       }
 1896     }
 1897   }
 1898 
 1899   if (hashes->parser_token_length_cnt > 0)
 1900   {
 1901     event_log_advice (hashcat_ctx, NULL); // we can guarantee that the previous line was not an empty line
 1902     event_log_advice (hashcat_ctx, "* Token length exception: %u/%u hashes", hashes->parser_token_length_cnt, hashes->parser_token_length_cnt + hashes->hashes_cnt);
 1903     event_log_advice (hashcat_ctx, "  This error happens if the wrong hash type is specified, if the hashes are");
 1904     event_log_advice (hashcat_ctx, "  malformed, or if input is otherwise not as expected (for example, if the");
 1905     event_log_advice (hashcat_ctx, "  --username option is used but no username is present)");
 1906     event_log_advice (hashcat_ctx, NULL);
 1907   }
 1908 
 1909   return 0;
 1910 }
 1911 
 1912 int hashes_init_stage2 (hashcat_ctx_t *hashcat_ctx)
 1913 {
 1914   const hashconfig_t   *hashconfig   = hashcat_ctx->hashconfig;
 1915         hashes_t       *hashes       = hashcat_ctx->hashes;
 1916   const user_options_t *user_options = hashcat_ctx->user_options;
 1917 
 1918   hash_t *hashes_buf = hashes->hashes_buf;
 1919   u32     hashes_cnt = hashes->hashes_cnt;
 1920 
 1921   /**
 1922    * Remove duplicates
 1923    */
 1924 
 1925   EVENT (EVENT_HASHLIST_UNIQUE_HASH_PRE);
 1926 
 1927   u32 hashes_cnt_new = 1;
 1928 
 1929   for (u32 hashes_pos = 1; hashes_pos < hashes_cnt; hashes_pos++)
 1930   {
 1931     if (hashconfig->potfile_keep_all_hashes == true)
 1932     {
 1933       // do not sort, because we need to keep all hashes in this particular case
 1934     }
 1935     else if (hashconfig->is_salted == true)
 1936     {
 1937       if (sort_by_salt (hashes_buf[hashes_pos].salt, hashes_buf[hashes_pos - 1].salt) == 0)
 1938       {
 1939         if (sort_by_digest_p0p1 (hashes_buf[hashes_pos].digest, hashes_buf[hashes_pos - 1].digest, (void *) hashconfig) == 0) continue;
 1940       }
 1941     }
 1942     else
 1943     {
 1944       if (sort_by_digest_p0p1 (hashes_buf[hashes_pos].digest, hashes_buf[hashes_pos - 1].digest, (void *) hashconfig) == 0) continue;
 1945     }
 1946 
 1947     hash_t tmp;
 1948 
 1949     memcpy (&tmp, &hashes_buf[hashes_pos], sizeof (hash_t));
 1950 
 1951     memcpy (&hashes_buf[hashes_cnt_new], &tmp, sizeof (hash_t));
 1952 
 1953     hashes_cnt_new++;
 1954   }
 1955 
 1956   for (u32 i = hashes_cnt_new; i < hashes->hashes_cnt; i++)
 1957   {
 1958     memset (&hashes_buf[i], 0, sizeof (hash_t));
 1959   }
 1960 
 1961   hashes_cnt = hashes_cnt_new;
 1962 
 1963   hashes->hashes_cnt = hashes_cnt;
 1964 
 1965   EVENT (EVENT_HASHLIST_UNIQUE_HASH_POST);
 1966 
 1967   /**
 1968    * Now generate all the buffers required for later
 1969    */
 1970 
 1971   void   *digests_buf_new    = hccalloc (hashes_cnt, hashconfig->dgst_size);
 1972   salt_t *salts_buf_new      = NULL;
 1973   void   *esalts_buf_new     = NULL;
 1974   void   *hook_salts_buf_new = NULL;
 1975 
 1976   if (hashconfig->is_salted == true)
 1977   {
 1978     salts_buf_new = (salt_t *) hccalloc (hashes_cnt, sizeof (salt_t));
 1979   }
 1980   else
 1981   {
 1982     salts_buf_new = (salt_t *) hccalloc (1, sizeof (salt_t));
 1983   }
 1984 
 1985   if (hashconfig->esalt_size > 0)
 1986   {
 1987     esalts_buf_new = hccalloc (hashes_cnt, hashconfig->esalt_size);
 1988   }
 1989 
 1990   if (hashconfig->hook_salt_size > 0)
 1991   {
 1992     hook_salts_buf_new = hccalloc (hashes_cnt, hashconfig->hook_salt_size);
 1993   }
 1994 
 1995   EVENT (EVENT_HASHLIST_SORT_SALT_PRE);
 1996 
 1997   u32 digests_cnt  = hashes_cnt;
 1998   u32 digests_done = 0;
 1999 
 2000   u32 *digests_shown = (u32 *) hccalloc (digests_cnt, sizeof (u32));
 2001 
 2002   u32 salts_cnt   = 0;
 2003   u32 salts_done  = 0;
 2004 
 2005   hashinfo_t **hash_info = NULL;
 2006 
 2007   if ((user_options->username == true) || (hashconfig->opts_type & OPTS_TYPE_HASH_COPY) || (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT))
 2008   {
 2009     hash_info = (hashinfo_t **) hccalloc (hashes_cnt, sizeof (hashinfo_t *));
 2010   }
 2011 
 2012   u32 *salts_shown = (u32 *) hccalloc (digests_cnt, sizeof (u32));
 2013 
 2014   salt_t *salt_buf;
 2015 
 2016   {
 2017     // copied from inner loop
 2018 
 2019     salt_buf = &salts_buf_new[salts_cnt];
 2020 
 2021     memcpy (salt_buf, hashes_buf[0].salt, sizeof (salt_t));
 2022 
 2023     hashes_buf[0].salt = salt_buf;
 2024 
 2025     if (hashconfig->hook_salt_size > 0)
 2026     {
 2027       char *hook_salts_buf_new_ptr = ((char *) hook_salts_buf_new) + (salts_cnt * hashconfig->hook_salt_size);
 2028 
 2029       memcpy (hook_salts_buf_new_ptr, hashes_buf[0].hook_salt, hashconfig->hook_salt_size);
 2030 
 2031       hashes_buf[0].hook_salt = hook_salts_buf_new_ptr;
 2032     }
 2033 
 2034     salt_buf->digests_cnt    = 0;
 2035     salt_buf->digests_done   = 0;
 2036     salt_buf->digests_offset = 0;
 2037 
 2038     salts_cnt++;
 2039   }
 2040 
 2041   salt_buf->digests_cnt++;
 2042 
 2043   char *digests_buf_new_ptr = ((char *) digests_buf_new) + (0 * hashconfig->dgst_size);
 2044 
 2045   memcpy (digests_buf_new_ptr, hashes_buf[0].digest, hashconfig->dgst_size);
 2046 
 2047   hashes_buf[0].digest = digests_buf_new_ptr;
 2048 
 2049   if (hashconfig->esalt_size > 0)
 2050   {
 2051     char *esalts_buf_new_ptr = ((char *) esalts_buf_new) + (0 * hashconfig->esalt_size);
 2052 
 2053     memcpy (esalts_buf_new_ptr, hashes_buf[0].esalt, hashconfig->esalt_size);
 2054 
 2055     hashes_buf[0].esalt = esalts_buf_new_ptr;
 2056   }
 2057 
 2058   if ((user_options->username == true) || (hashconfig->opts_type & OPTS_TYPE_HASH_COPY) || (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT))
 2059   {
 2060     hash_info[0] = hashes_buf[0].hash_info;
 2061   }
 2062 
 2063   // copy from inner loop
 2064 
 2065   for (u32 hashes_pos = 1; hashes_pos < hashes_cnt; hashes_pos++)
 2066   {
 2067     if (hashconfig->is_salted == true)
 2068     {
 2069       if (sort_by_salt (hashes_buf[hashes_pos].salt, hashes_buf[hashes_pos - 1].salt) != 0)
 2070       {
 2071         salt_buf = &salts_buf_new[salts_cnt];
 2072 
 2073         memcpy (salt_buf, hashes_buf[hashes_pos].salt, sizeof (salt_t));
 2074 
 2075         hashes_buf[hashes_pos].salt = salt_buf;
 2076 
 2077         if (hashconfig->hook_salt_size > 0)
 2078         {
 2079           char *hook_salts_buf_new_ptr = ((char *) hook_salts_buf_new) + (salts_cnt * hashconfig->hook_salt_size);
 2080 
 2081           memcpy (hook_salts_buf_new_ptr, hashes_buf[hashes_pos].hook_salt, hashconfig->hook_salt_size);
 2082 
 2083           hashes_buf[hashes_pos].hook_salt = hook_salts_buf_new_ptr;
 2084         }
 2085 
 2086         salt_buf->digests_cnt    = 0;
 2087         salt_buf->digests_done   = 0;
 2088         salt_buf->digests_offset = hashes_pos;
 2089 
 2090         salts_cnt++;
 2091       }
 2092 
 2093       hashes_buf[hashes_pos].salt = salt_buf;
 2094 
 2095       if (hashconfig->hook_salt_size > 0)
 2096       {
 2097         char *hook_salts_buf_new_ptr = ((char *) hook_salts_buf_new) + (salts_cnt * hashconfig->hook_salt_size);
 2098 
 2099         hashes_buf[hashes_pos].hook_salt = hook_salts_buf_new_ptr;
 2100       }
 2101     }
 2102 
 2103     salt_buf->digests_cnt++;
 2104 
 2105     digests_buf_new_ptr = ((char *) digests_buf_new) + (hashes_pos * hashconfig->dgst_size);
 2106 
 2107     memcpy (digests_buf_new_ptr, hashes_buf[hashes_pos].digest, hashconfig->dgst_size);
 2108 
 2109     hashes_buf[hashes_pos].digest = digests_buf_new_ptr;
 2110 
 2111     if (hashconfig->esalt_size > 0)
 2112     {
 2113       char *esalts_buf_new_ptr = ((char *) esalts_buf_new) + (hashes_pos * hashconfig->esalt_size);
 2114 
 2115       memcpy (esalts_buf_new_ptr, hashes_buf[hashes_pos].esalt, hashconfig->esalt_size);
 2116 
 2117       hashes_buf[hashes_pos].esalt = esalts_buf_new_ptr;
 2118     }
 2119 
 2120     if ((user_options->username == true) || (hashconfig->opts_type & OPTS_TYPE_HASH_COPY) || (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT))
 2121     {
 2122       hash_info[hashes_pos] = hashes_buf[hashes_pos].hash_info;
 2123     }
 2124   }
 2125 
 2126   EVENT (EVENT_HASHLIST_SORT_SALT_POST);
 2127 
 2128   hcfree (hashes->digests_buf);
 2129   hcfree (hashes->salts_buf);
 2130   hcfree (hashes->esalts_buf);
 2131   hcfree (hashes->hook_salts_buf);
 2132 
 2133   hashes->digests_cnt       = digests_cnt;
 2134   hashes->digests_done      = digests_done;
 2135   hashes->digests_buf       = digests_buf_new;
 2136   hashes->digests_shown     = digests_shown;
 2137 
 2138   hashes->salts_cnt         = salts_cnt;
 2139   hashes->salts_done        = salts_done;
 2140   hashes->salts_buf         = salts_buf_new;
 2141   hashes->salts_shown       = salts_shown;
 2142 
 2143   hashes->esalts_buf        = esalts_buf_new;
 2144   hashes->hook_salts_buf    = hook_salts_buf_new;
 2145 
 2146   hashes->hash_info         = hash_info;
 2147 
 2148   return 0;
 2149 }
 2150 
 2151 int hashes_init_stage3 (hashcat_ctx_t *hashcat_ctx)
 2152 {
 2153   hashes_t *hashes = hashcat_ctx->hashes;
 2154 
 2155   u32  digests_done      = hashes->digests_done;
 2156   u32  digests_done_zero = hashes->digests_done_zero;
 2157   u32  digests_done_pot  = hashes->digests_done_pot;
 2158   u32 *digests_shown     = hashes->digests_shown;
 2159 
 2160   u32  salts_cnt         = hashes->salts_cnt;
 2161   u32  salts_done        = hashes->salts_done;
 2162   u32 *salts_shown       = hashes->salts_shown;
 2163 
 2164   hash_t *hashes_buf     = hashes->hashes_buf;
 2165   salt_t *salts_buf      = hashes->salts_buf;
 2166 
 2167   for (u32 salt_idx = 0; salt_idx < salts_cnt; salt_idx++)
 2168   {
 2169     salt_t *salt_buf = salts_buf + salt_idx;
 2170 
 2171     u32 digests_cnt = salt_buf->digests_cnt;
 2172 
 2173     for (u32 digest_idx = 0; digest_idx < digests_cnt; digest_idx++)
 2174     {
 2175       const u32 hashes_idx = salt_buf->digests_offset + digest_idx;
 2176 
 2177       if (hashes_buf[hashes_idx].cracked_pot == 1)
 2178       {
 2179         digests_shown[hashes_idx] = 1;
 2180 
 2181         digests_done++;
 2182 
 2183         digests_done_pot++;
 2184 
 2185         salt_buf->digests_done++;
 2186       }
 2187 
 2188       if (hashes_buf[hashes_idx].cracked_zero == 1)
 2189       {
 2190         digests_shown[hashes_idx] = 1;
 2191 
 2192         digests_done++;
 2193 
 2194         digests_done_zero++;
 2195 
 2196         salt_buf->digests_done++;
 2197       }
 2198     }
 2199 
 2200     if (salt_buf->digests_done == salt_buf->digests_cnt)
 2201     {
 2202       salts_shown[salt_idx] = 1;
 2203 
 2204       salts_done++;
 2205     }
 2206 
 2207     if (salts_done == salts_cnt) mycracked (hashcat_ctx);
 2208   }
 2209 
 2210   hashes->digests_done      = digests_done;
 2211   hashes->digests_done_zero = digests_done_zero;
 2212   hashes->digests_done_pot  = digests_done_pot;
 2213 
 2214   hashes->salts_cnt         = salts_cnt;
 2215   hashes->salts_done        = salts_done;
 2216 
 2217   return 0;
 2218 }
 2219 
 2220 int hashes_init_stage4 (hashcat_ctx_t *hashcat_ctx)
 2221 {
 2222   hashconfig_t         *hashconfig         = hashcat_ctx->hashconfig;
 2223   hashes_t             *hashes             = hashcat_ctx->hashes;
 2224   module_ctx_t         *module_ctx         = hashcat_ctx->module_ctx;
 2225   user_options_t       *user_options       = hashcat_ctx->user_options;
 2226   user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
 2227 
 2228   if (hashes->salts_cnt == 1)
 2229     hashconfig->opti_type |= OPTI_TYPE_SINGLE_SALT;
 2230 
 2231   if (hashes->digests_cnt == 1)
 2232     hashconfig->opti_type |= OPTI_TYPE_SINGLE_HASH;
 2233 
 2234   if (hashconfig->attack_exec == ATTACK_EXEC_INSIDE_KERNEL)
 2235     hashconfig->opti_type |= OPTI_TYPE_NOT_ITERATED;
 2236 
 2237   if (user_options->attack_mode == ATTACK_MODE_BF)
 2238     hashconfig->opti_type |= OPTI_TYPE_BRUTE_FORCE;
 2239 
 2240   if (hashconfig->opti_type & OPTI_TYPE_BRUTE_FORCE)
 2241   {
 2242     if (hashconfig->opti_type & OPTI_TYPE_SINGLE_HASH)
 2243     {
 2244       if (hashconfig->opti_type & OPTI_TYPE_APPENDED_SALT)
 2245       {
 2246         if (hashconfig->opts_type & OPTS_TYPE_ST_ADD80)
 2247         {
 2248           hashconfig->opts_type &= ~OPTS_TYPE_ST_ADD80;
 2249           hashconfig->opts_type |=  OPTS_TYPE_PT_ADD80;
 2250         }
 2251 
 2252         if (hashconfig->opts_type & OPTS_TYPE_ST_ADDBITS14)
 2253         {
 2254           hashconfig->opts_type &= ~OPTS_TYPE_ST_ADDBITS14;
 2255           hashconfig->opts_type |=  OPTS_TYPE_PT_ADDBITS14;
 2256         }
 2257 
 2258         if (hashconfig->opts_type & OPTS_TYPE_ST_ADDBITS15)
 2259         {
 2260           hashconfig->opts_type &= ~OPTS_TYPE_ST_ADDBITS15;
 2261           hashconfig->opts_type |=  OPTS_TYPE_PT_ADDBITS15;
 2262         }
 2263       }
 2264     }
 2265   }
 2266 
 2267   // test iteration count in association attack
 2268 
 2269   if (user_options->attack_mode == ATTACK_MODE_ASSOCIATION)
 2270   {
 2271     salt_t *salts_buf = hashes->salts_buf;
 2272 
 2273     for (u32 salt_idx = 1; salt_idx < hashes->salts_cnt; salt_idx++)
 2274     {
 2275       if (salts_buf[salt_idx - 1].salt_iter != salts_buf[salt_idx].salt_iter)
 2276       {
 2277         event_log_error (hashcat_ctx, "Mixed iteration counts are not supported in association attack-mode.");
 2278 
 2279         return -1;
 2280       }
 2281     }
 2282   }
 2283 
 2284   // time to update extra_tmp_size which is tmp_size value based on hash configuration
 2285 
 2286   if (module_ctx->module_extra_tmp_size != MODULE_DEFAULT)
 2287   {
 2288     const u64 extra_tmp_size = module_ctx->module_extra_tmp_size (hashconfig, user_options, user_options_extra, hashes);
 2289 
 2290     if (extra_tmp_size == (u64) -1)
 2291     {
 2292       event_log_error (hashcat_ctx, "Mixed hash settings are not supported.");
 2293 
 2294       return -1;
 2295     }
 2296 
 2297     hashconfig->tmp_size = extra_tmp_size;
 2298   }
 2299 
 2300   // at this point we no longer need hash_t* structure
 2301 
 2302   hash_t *hashes_buf = hashes->hashes_buf;
 2303 
 2304   hcfree (hashes_buf);
 2305 
 2306   hashes->hashes_cnt = 0;
 2307   hashes->hashes_buf = NULL;
 2308 
 2309   // starting from here, we should allocate some scratch buffer for later use
 2310 
 2311   u8 *out_buf = (u8 *) hcmalloc (HCBUFSIZ_LARGE);
 2312 
 2313   hashes->out_buf = out_buf;
 2314 
 2315   // we need two buffers in parallel
 2316 
 2317   u8 *tmp_buf = (u8 *) hcmalloc (HCBUFSIZ_LARGE);
 2318 
 2319   hashes->tmp_buf = tmp_buf;
 2320 
 2321   // brain session
 2322 
 2323   #ifdef WITH_BRAIN
 2324   if (user_options->brain_client == true)
 2325   {
 2326     const u32 brain_session = brain_compute_session (hashcat_ctx);
 2327 
 2328     user_options->brain_session = brain_session;
 2329   }
 2330   #endif
 2331 
 2332   return 0;
 2333 }
 2334 
 2335 int hashes_init_selftest (hashcat_ctx_t *hashcat_ctx)
 2336 {
 2337   folder_config_t *folder_config = hashcat_ctx->folder_config;
 2338   hashconfig_t    *hashconfig    = hashcat_ctx->hashconfig;
 2339   hashes_t        *hashes        = hashcat_ctx->hashes;
 2340   module_ctx_t    *module_ctx    = hashcat_ctx->module_ctx;
 2341   user_options_t  *user_options  = hashcat_ctx->user_options;
 2342 
 2343   if (hashconfig->st_hash == NULL) return 0;
 2344 
 2345   void   *st_digests_buf    = NULL;
 2346   salt_t *st_salts_buf      = NULL;
 2347   void   *st_esalts_buf     = NULL;
 2348   void   *st_hook_salts_buf = NULL;
 2349 
 2350   st_digests_buf =          hccalloc (1, hashconfig->dgst_size);
 2351 
 2352   st_salts_buf = (salt_t *) hccalloc (1, sizeof (salt_t));
 2353 
 2354   if (hashconfig->esalt_size > 0)
 2355   {
 2356     st_esalts_buf = hccalloc (1, hashconfig->esalt_size);
 2357   }
 2358 
 2359   if (hashconfig->hook_salt_size > 0)
 2360   {
 2361     st_hook_salts_buf = hccalloc (1, hashconfig->hook_salt_size);
 2362   }
 2363 
 2364   hash_t hash;
 2365 
 2366   hash.digest    = st_digests_buf;
 2367   hash.salt      = st_salts_buf;
 2368   hash.esalt     = st_esalts_buf;
 2369   hash.hook_salt = st_hook_salts_buf;
 2370   hash.cracked   = 0;
 2371   hash.hash_info = NULL;
 2372   hash.pw_buf    = NULL;
 2373   hash.pw_len    = 0;
 2374 
 2375   int parser_status;
 2376 
 2377   if (module_ctx->module_hash_init_selftest != MODULE_DEFAULT)
 2378   {
 2379     parser_status = module_ctx->module_hash_init_selftest (hashconfig, &hash);
 2380   }
 2381   else
 2382   {
 2383     if (hashconfig->opts_type & OPTS_TYPE_BINARY_HASHFILE)
 2384     {
 2385       if (hashconfig->opts_type & OPTS_TYPE_BINARY_HASHFILE_OPTIONAL)
 2386       {
 2387         parser_status = module_ctx->module_hash_decode (hashconfig, hash.digest, hash.salt, hash.esalt, hash.hook_salt, hash.hash_info, hashconfig->st_hash, strlen (hashconfig->st_hash));
 2388       }
 2389       else
 2390       {
 2391         char *tmpfile_bin;
 2392 
 2393         hc_asprintf (&tmpfile_bin, "%s/selftest.hash", folder_config->session_dir);
 2394 
 2395         HCFILE fp;
 2396 
 2397         hc_fopen (&fp, tmpfile_bin, "wb");
 2398 
 2399         const size_t st_hash_len = strlen (hashconfig->st_hash);
 2400 
 2401         for (size_t i = 0; i < st_hash_len; i += 2)
 2402         {
 2403           const u8 c = hex_to_u8 ((const u8 *) hashconfig->st_hash + i);
 2404 
 2405           hc_fputc (c, &fp);
 2406         }
 2407 
 2408         hc_fclose (&fp);
 2409 
 2410         parser_status = module_ctx->module_hash_decode (hashconfig, hash.digest, hash.salt, hash.esalt, hash.hook_salt, hash.hash_info, tmpfile_bin, strlen (tmpfile_bin));
 2411 
 2412         unlink (tmpfile_bin);
 2413 
 2414         hcfree (tmpfile_bin);
 2415       }
 2416     }
 2417     else
 2418     {
 2419       hashconfig_t *hashconfig_st = (hashconfig_t *) hcmalloc (sizeof (hashconfig_t));
 2420 
 2421       memcpy (hashconfig_st, hashconfig, sizeof (hashconfig_t));
 2422 
 2423       hashconfig_st->separator = ':';
 2424 
 2425       if (user_options->hex_salt)
 2426       {
 2427         if (hashconfig->salt_type == SALT_TYPE_GENERIC)
 2428         {
 2429           // this is save as there's no hash mode that has both SALT_TYPE_GENERIC and OPTS_TYPE_ST_HEX by default
 2430 
 2431           hashconfig_st->opts_type &= ~OPTS_TYPE_ST_HEX;
 2432         }
 2433       }
 2434 
 2435       parser_status = module_ctx->module_hash_decode (hashconfig_st, hash.digest, hash.salt, hash.esalt, hash.hook_salt, hash.hash_info, hashconfig->st_hash, strlen (hashconfig->st_hash));
 2436 
 2437       hcfree (hashconfig_st);
 2438     }
 2439   }
 2440 
 2441   if (parser_status == PARSER_OK)
 2442   {
 2443     // nothing to do
 2444   }
 2445   else
 2446   {
 2447     event_log_error (hashcat_ctx, "Self-test hash parsing error: %s", strparser (parser_status));
 2448 
 2449     return -1;
 2450   }
 2451 
 2452   hashes->st_digests_buf    = st_digests_buf;
 2453   hashes->st_salts_buf      = st_salts_buf;
 2454   hashes->st_esalts_buf     = st_esalts_buf;
 2455   hashes->st_hook_salts_buf = st_hook_salts_buf;
 2456 
 2457   return 0;
 2458 }
 2459 
 2460 int hashes_init_benchmark (hashcat_ctx_t *hashcat_ctx)
 2461 {
 2462   const hashconfig_t          *hashconfig         = hashcat_ctx->hashconfig;
 2463         hashes_t              *hashes             = hashcat_ctx->hashes;
 2464   const module_ctx_t          *module_ctx         = hashcat_ctx->module_ctx;
 2465   const user_options_t        *user_options       = hashcat_ctx->user_options;
 2466   const user_options_extra_t  *user_options_extra = hashcat_ctx->user_options_extra;
 2467 
 2468   if (user_options->benchmark == false) return 0;
 2469 
 2470   if (hashconfig->is_salted == false) return 0;
 2471 
 2472   if (module_ctx->module_benchmark_salt != MODULE_DEFAULT)
 2473   {
 2474     salt_t *ptr = module_ctx->module_benchmark_salt (hashconfig, user_options, user_options_extra);
 2475 
 2476     memcpy (hashes->salts_buf, ptr, sizeof (salt_t));
 2477 
 2478     hcfree (ptr);
 2479   }
 2480   else
 2481   {
 2482     memcpy (hashes->salts_buf, hashes->st_salts_buf, sizeof (salt_t));
 2483   }
 2484 
 2485   if (hashconfig->esalt_size > 0)
 2486   {
 2487     if (module_ctx->module_benchmark_esalt != MODULE_DEFAULT)
 2488     {
 2489       void *ptr = module_ctx->module_benchmark_esalt (hashconfig, user_options, user_options_extra);
 2490 
 2491       memcpy (hashes->esalts_buf, ptr, hashconfig->esalt_size);
 2492 
 2493       hcfree (ptr);
 2494     }
 2495     else
 2496     {
 2497       memcpy (hashes->esalts_buf, hashes->st_esalts_buf, hashconfig->esalt_size);
 2498     }
 2499   }
 2500 
 2501   if (hashconfig->hook_salt_size > 0)
 2502   {
 2503     if (module_ctx->module_benchmark_hook_salt != MODULE_DEFAULT)
 2504     {
 2505       void *ptr = module_ctx->module_benchmark_hook_salt (hashconfig, user_options, user_options_extra);
 2506 
 2507       memcpy (hashes->hook_salts_buf, ptr, hashconfig->hook_salt_size);
 2508 
 2509       hcfree (ptr);
 2510     }
 2511     else
 2512     {
 2513       memcpy (hashes->hook_salts_buf, hashes->st_hook_salts_buf, hashconfig->hook_salt_size);
 2514     }
 2515   }
 2516 
 2517   return 0;
 2518 }
 2519 
 2520 int hashes_init_zerohash (hashcat_ctx_t *hashcat_ctx)
 2521 {
 2522   const hashconfig_t   *hashconfig   = hashcat_ctx->hashconfig;
 2523   const hashes_t       *hashes       = hashcat_ctx->hashes;
 2524   const module_ctx_t   *module_ctx   = hashcat_ctx->module_ctx;
 2525 
 2526   // do not use this unless really needed, for example as in LM
 2527 
 2528   if (module_ctx->module_hash_decode_zero_hash == MODULE_DEFAULT) return 0;
 2529 
 2530   hash_t *hashes_buf = hashes->hashes_buf;
 2531   u32     hashes_cnt = hashes->hashes_cnt;
 2532 
 2533   // no solution for these special hash types (for instane because they use hashfile in output etc)
 2534 
 2535   hash_t hash_buf;
 2536 
 2537   hash_buf.digest    = hcmalloc (hashconfig->dgst_size);
 2538   hash_buf.salt      = NULL;
 2539   hash_buf.esalt     = NULL;
 2540   hash_buf.hook_salt = NULL;
 2541   hash_buf.cracked   = 0;
 2542   hash_buf.hash_info = NULL;
 2543   hash_buf.pw_buf    = NULL;
 2544   hash_buf.pw_len    = 0;
 2545 
 2546   if (hashconfig->is_salted == true)
 2547   {
 2548     hash_buf.salt = (salt_t *) hcmalloc (sizeof (salt_t));
 2549   }
 2550 
 2551   if (hashconfig->esalt_size > 0)
 2552   {
 2553     hash_buf.esalt = hcmalloc (hashconfig->esalt_size);
 2554   }
 2555 
 2556   if (hashconfig->hook_salt_size > 0)
 2557   {
 2558     hash_buf.hook_salt = hcmalloc (hashconfig->hook_salt_size);
 2559   }
 2560 
 2561   module_ctx->module_hash_decode_zero_hash (hashconfig, hash_buf.digest, hash_buf.salt, hash_buf.esalt, hash_buf.hook_salt, hash_buf.hash_info);
 2562 
 2563   for (u32 i = 0; i < hashes_cnt; i++)
 2564   {
 2565     hash_t *next = &hashes_buf[i];
 2566 
 2567     int rc = sort_by_hash_no_salt (&hash_buf, next, (void *) hashconfig);
 2568 
 2569     if (rc == 0)
 2570     {
 2571       next->pw_buf = (char *) hcmalloc (1);
 2572       next->pw_len = 0;
 2573 
 2574       next->cracked_zero = 1;
 2575 
 2576       // should we show the cracked zero hash to the user?
 2577 
 2578       if (false)
 2579       {
 2580         // digest pos
 2581 
 2582         const u32 digest_pos = next - hashes_buf;
 2583 
 2584         // show the crack
 2585 
 2586         u8 *out_buf = (u8 *) hcmalloc (HCBUFSIZ_LARGE);
 2587 
 2588         int out_len = hash_encode (hashcat_ctx->hashconfig, hashcat_ctx->hashes, hashcat_ctx->module_ctx, (char *) out_buf, HCBUFSIZ_LARGE, 0, digest_pos);
 2589 
 2590         out_buf[out_len] = 0;
 2591 
 2592         // outfile, can be either to file or stdout
 2593         // if an error occurs opening the file, send to stdout as fallback
 2594         // the fp gets opened for each cracked hash so that the user can modify (move) the outfile while hashcat runs
 2595 
 2596         outfile_write_open (hashcat_ctx);
 2597 
 2598         const u8 *plain = (const u8 *) "";
 2599 
 2600         u8 *tmp_buf = (u8 *) hcmalloc (HCBUFSIZ_LARGE);
 2601 
 2602         tmp_buf[0] = 0;
 2603 
 2604         const int tmp_len = outfile_write (hashcat_ctx, (char *) out_buf, out_len, plain, 0, 0, NULL, 0, true, (char *) tmp_buf);
 2605 
 2606         EVENT_DATA (EVENT_CRACKER_HASH_CRACKED, tmp_buf, tmp_len);
 2607 
 2608         outfile_write_close (hashcat_ctx);
 2609 
 2610         hcfree (tmp_buf);
 2611         hcfree (out_buf);
 2612       }
 2613     }
 2614   }
 2615 
 2616   if (hashconfig->esalt_size > 0)
 2617   {
 2618     hcfree (hash_buf.esalt);
 2619   }
 2620 
 2621   if (hashconfig->hook_salt_size > 0)
 2622   {
 2623     hcfree (hash_buf.hook_salt);
 2624   }
 2625 
 2626   if (hashconfig->is_salted == true)
 2627   {
 2628     hcfree (hash_buf.salt);
 2629   }
 2630 
 2631   hcfree (hash_buf.digest);
 2632 
 2633   return 0;
 2634 }
 2635 
 2636 void hashes_destroy (hashcat_ctx_t *hashcat_ctx)
 2637 {
 2638   hashconfig_t   *hashconfig   = hashcat_ctx->hashconfig;
 2639   hashes_t       *hashes       = hashcat_ctx->hashes;
 2640   user_options_t *user_options = hashcat_ctx->user_options;
 2641 
 2642   hcfree (hashes->digests_buf);
 2643   hcfree (hashes->digests_shown);
 2644 
 2645   hcfree (hashes->salts_buf);
 2646   hcfree (hashes->salts_shown);
 2647 
 2648   if ((user_options->username == true) || (hashconfig->opts_type & OPTS_TYPE_HASH_COPY))
 2649   {
 2650     for (u32 hash_pos = 0; hash_pos < hashes->hashes_cnt; hash_pos++)
 2651     {
 2652       if (user_options->username == true)
 2653       {
 2654         hcfree (hashes->hash_info[hash_pos]->user);
 2655       }
 2656 
 2657       if (hashconfig->opts_type & OPTS_TYPE_HASH_COPY)
 2658       {
 2659         hcfree (hashes->hash_info[hash_pos]->orighash);
 2660       }
 2661 
 2662       if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
 2663       {
 2664         hcfree (hashes->hash_info[hash_pos]->split);
 2665       }
 2666     }
 2667   }
 2668 
 2669   hcfree (hashes->hash_info);
 2670 
 2671   hcfree (hashes->esalts_buf);
 2672   hcfree (hashes->hook_salts_buf);
 2673 
 2674   hcfree (hashes->out_buf);
 2675   hcfree (hashes->tmp_buf);
 2676 
 2677   hcfree (hashes->st_digests_buf);
 2678   hcfree (hashes->st_salts_buf);
 2679   hcfree (hashes->st_esalts_buf);
 2680   hcfree (hashes->st_hook_salts_buf);
 2681 
 2682   memset (hashes, 0, sizeof (hashes_t));
 2683 }
 2684 
 2685 void hashes_logger (hashcat_ctx_t *hashcat_ctx)
 2686 {
 2687   hashes_t      *hashes      = hashcat_ctx->hashes;
 2688   logfile_ctx_t *logfile_ctx = hashcat_ctx->logfile_ctx;
 2689 
 2690   logfile_top_string (hashes->hashfile);
 2691   logfile_top_uint   (hashes->hashlist_mode);
 2692   logfile_top_uint   (hashes->hashlist_format);
 2693   logfile_top_uint   (hashes->hashes_cnt);
 2694   logfile_top_uint   (hashes->digests_cnt);
 2695   logfile_top_uint   (hashes->digests_done_pot);
 2696   logfile_top_uint   (hashes->digests_done_zero);
 2697   logfile_top_uint   (hashes->digests_done);
 2698   logfile_top_uint   (hashes->salts_cnt);
 2699   logfile_top_uint   (hashes->salts_done);
 2700 }