Rewrite some code to workaround strict aliasing rule violation for older compilers
[hashcat.git] / src / hashcat.c
index 13ee720..59e682a 100644 (file)
@@ -6,7 +6,7 @@
  * License.....: MIT
  */
 
-#ifdef DARWIN
+#if defined(DARWIN) || defined(__FreeBSD__)
 #include <stdio.h>
 #endif
 
@@ -86,7 +86,7 @@ double TARGET_MS_PROFILE[4]     = { 2, 12, 96, 480 };
 #define KERNEL_COMBS            1024
 #define KERNEL_BFS              1024
 #define KERNEL_THREADS_MAX      256
-#define KERNEL_THREADS_MAX_CPU  16
+#define KERNEL_THREADS_MAX_CPU  1
 #define POWERTUNE_ENABLE        0
 #define LOGFILE_DISABLE         0
 #define SCRYPT_TMTO             0
@@ -925,6 +925,9 @@ void status_display ()
   if (data.devices_status == STATUS_INIT)     return;
   if (data.devices_status == STATUS_STARTING) return;
 
+  // in this case some required buffers are free'd, ascii_digest() would run into segfault
+  if (data.shutdown_inner == 1) return;
+
   if (data.machine_readable == 1)
   {
     status_display_machine_readable ();
@@ -1715,6 +1718,8 @@ static void status_benchmark ()
   if (data.devices_status == STATUS_INIT)     return;
   if (data.devices_status == STATUS_STARTING) return;
 
+  if (data.shutdown_inner == 1) return;
+
   if (data.machine_readable == 1)
   {
     status_benchmark_automate ();
@@ -4211,8 +4216,11 @@ static void *thread_monitor (void *p)
         {
           if (data.hm_nvapi)
           {
-            NV_GPU_PERF_POLICIES_INFO_PARAMS_V1   perfPolicies_info   = { 0 };
-            NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1 perfPolicies_status = { 0 };
+            NV_GPU_PERF_POLICIES_INFO_PARAMS_V1   perfPolicies_info;
+            NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1 perfPolicies_status;
+
+            memset (&perfPolicies_info,   0, sizeof (NV_GPU_PERF_POLICIES_INFO_PARAMS_V1));
+            memset (&perfPolicies_status, 0, sizeof (NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1));
 
             perfPolicies_info.version   = MAKE_NVAPI_VERSION (NV_GPU_PERF_POLICIES_INFO_PARAMS_V1, 1);
             perfPolicies_status.version = MAKE_NVAPI_VERSION (NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1, 1);
@@ -6166,7 +6174,8 @@ int main (int argc, char **argv)
 
   char *exec_path = get_exec_path ();
 
-  #ifdef LINUX
+
+  #if defined(LINUX) || defined(__APPLE__) || defined(__FreeBSD__)
 
   char *resolved_install_folder = realpath (INSTALL_FOLDER, NULL);
   char *resolved_exec_path      = realpath (exec_path, NULL);
@@ -13012,6 +13021,7 @@ int main (int argc, char **argv)
                   // here we have in line_buf: ESSID:MAC1:MAC2   (without the plain)
                   // manipulate salt_buf
 
+                  memset (line_buf_cpy, 0, HCBUFSIZ);
                   memcpy (line_buf_cpy, line_buf, i);
 
                   char *mac2_pos = strrchr (line_buf_cpy, ':');
@@ -13910,6 +13920,8 @@ int main (int argc, char **argv)
 
         device_param->platform_devices_id = platform_devices_id;
 
+        device_param->platform = platform;
+
         // device_type
 
         cl_device_type device_type;
@@ -14510,10 +14522,15 @@ int main (int argc, char **argv)
      */
 
     #ifdef HAVE_HWMON
-    hm_attrs_t hm_adapters_adl[DEVICES_MAX]     = { { 0 } };
-    hm_attrs_t hm_adapters_nvapi[DEVICES_MAX]   = { { 0 } };
-    hm_attrs_t hm_adapters_nvml[DEVICES_MAX]    = { { 0 } };
-    hm_attrs_t hm_adapters_xnvctrl[DEVICES_MAX] = { { 0 } };
+    hm_attrs_t hm_adapters_adl[DEVICES_MAX];
+    hm_attrs_t hm_adapters_nvapi[DEVICES_MAX];
+    hm_attrs_t hm_adapters_nvml[DEVICES_MAX];
+    hm_attrs_t hm_adapters_xnvctrl[DEVICES_MAX];
+
+    memset (hm_adapters_adl,     0, sizeof (hm_adapters_adl));
+    memset (hm_adapters_nvapi,   0, sizeof (hm_adapters_nvapi));
+    memset (hm_adapters_nvml,    0, sizeof (hm_adapters_nvml));
+    memset (hm_adapters_xnvctrl, 0, sizeof (hm_adapters_xnvctrl));
 
     if (gpu_temp_disable == 0)
     {
@@ -15029,7 +15046,13 @@ int main (int argc, char **argv)
        * create context for each device
        */
 
-      device_param->context = hc_clCreateContext (data.ocl, NULL, 1, &device_param->device, NULL, NULL);
+      cl_context_properties properties[3];
+
+      properties[0] = CL_CONTEXT_PLATFORM;
+      properties[1] = (cl_context_properties) device_param->platform;
+      properties[2] = 0;
+
+      device_param->context = hc_clCreateContext (data.ocl, properties, 1, &device_param->device, NULL, NULL);
 
       /**
        * create command-queue
@@ -15048,6 +15071,9 @@ int main (int argc, char **argv)
 
       uint kernel_threads = MIN (KERNEL_THREADS_MAX, device_param->device_maxworkgroup_size);
 
+      if (hash_mode ==  8900) kernel_threads = 64; // Scrypt
+      if (hash_mode ==  9300) kernel_threads = 64; // Scrypt
+
       if (device_param->device_type & CL_DEVICE_TYPE_CPU)
       {
         kernel_threads = KERNEL_THREADS_MAX_CPU;
@@ -15057,9 +15083,7 @@ int main (int argc, char **argv)
       if (hash_mode ==  3000) kernel_threads = 64; // DES
       if (hash_mode ==  3200) kernel_threads = 8;  // Blowfish
       if (hash_mode ==  7500) kernel_threads = 64; // RC4
-      if (hash_mode ==  8900) kernel_threads = 64; // Scrypt
       if (hash_mode ==  9000) kernel_threads = 8;  // Blowfish
-      if (hash_mode ==  9300) kernel_threads = 64; // Scrypt
       if (hash_mode ==  9700) kernel_threads = 64; // RC4
       if (hash_mode ==  9710) kernel_threads = 64; // RC4
       if (hash_mode ==  9800) kernel_threads = 64; // RC4
@@ -15163,7 +15187,7 @@ int main (int argc, char **argv)
           }
         }
 
-        data.scrypt_tmp_size = (128 * scrypt_r);
+        data.scrypt_tmp_size = (128 * scrypt_r * scrypt_p);
 
         device_param->kernel_accel_min = 1;
         device_param->kernel_accel_max = 8;
@@ -15207,7 +15231,7 @@ int main (int argc, char **argv)
           return -1;
         }
 
-        if (quiet == 0) log_info ("SCRYPT tmto optimizer value set to: %u, mem: %u\n", data.scrypt_tmto_final, size_scrypt);
+        if (quiet == 0) log_info ("SCRYPT tmto optimizer value set to: %u, mem: %llu\n", data.scrypt_tmto_final, (unsigned long long int) size_scrypt);
       }
 
       size_t size_scrypt4 = size_scrypt / 4;
@@ -17494,6 +17518,36 @@ int main (int argc, char **argv)
 
             mask = mask + str_pos + 1;
           }
+
+          /**
+           * What follows is a very special case where "\," is within the mask field of a line in a .hcmask file only because otherwise (without the "\")
+           * it would be interpreted as a custom charset definition.
+           *
+           * We need to replace all "\," with just "," within the mask (but allow the special case "\\," which means "\" followed by ",")
+           * Note: "\\" is not needed to replace all "\" within the mask! The meaning of "\\" within a line containing the string "\\," is just to allow "\" followed by ","
+           */
+
+          uint mask_len_cur = strlen (mask);
+
+          uint mask_out_pos = 0;
+          char mask_prev = 0;
+
+          for (uint mask_iter = 0; mask_iter < mask_len_cur; mask_iter++, mask_out_pos++)
+          {
+            if (mask[mask_iter] == ',')
+            {
+              if (mask_prev == '\\')
+              {
+                mask_out_pos -= 1; // this means: skip the previous "\"
+              }
+            }
+
+            mask_prev = mask[mask_iter];
+
+            mask[mask_out_pos] = mask[mask_iter];
+          }
+
+          mask[mask_out_pos] = '\0';
         }
 
         if ((attack_mode == ATTACK_MODE_HYBRID1) || (attack_mode == ATTACK_MODE_HYBRID2))
@@ -18357,6 +18411,10 @@ int main (int argc, char **argv)
         if (induction_dictionaries_cnt)
         {
           qsort (induction_dictionaries, induction_dictionaries_cnt, sizeof (char *), sort_by_mtime);
+
+          // yeah, this next statement is a little hack to make sure that --loopback runs correctly (because with it we guarantee that the loop iterates one more time)
+
+          dictpos--;
         }
 
         time_t runtime_stop;