improved autotune engine
[hashcat.git] / src / oclHashcat.c
index bd720e8..035a97d 100644 (file)
@@ -148,7 +148,7 @@ double TARGET_MS_PROFILE[3]     = { 8, 16, 96 };
 
 #define MAX_DICTSTAT            10000
 
-#define NUM_DEFAULT_BENCHMARK_ALGORITHMS 135
+#define NUM_DEFAULT_BENCHMARK_ALGORITHMS 136
 
 #define global_free(attr)       \
 {                               \
@@ -268,6 +268,7 @@ static uint default_benchmark_algorithms[NUM_DEFAULT_BENCHMARK_ALGORITHMS] =
   8700,
   9100,
   133,
+  13500,
   11600,
   12500,
   13000,
@@ -696,6 +697,7 @@ const char *USAGE_BIG[] =
   "   8700 = Lotus Notes/Domino 6",
   "   9100 = Lotus Notes/Domino 8",
   "    133 = PeopleSoft",
+  "  13500 = PeopleSoft Token",
   "",
   "[[ Archives ]]",
   "",
@@ -805,12 +807,6 @@ void status_display_automat ()
 
     for (int i = 0; i < SPEED_CACHE; i++)
     {
-      double rec_ms;
-
-      hc_timer_get (device_param->speed_rec[i], rec_ms);
-
-      if (rec_ms > SPEED_MAXAGE) continue;
-
       speed_cnt  += device_param->speed_cnt[i];
       speed_ms   += device_param->speed_ms[i];
     }
@@ -1149,25 +1145,11 @@ void status_display ()
 
     if (device_param->skipped) continue;
 
-    // we need to clear values (set to 0) because in case the device does
-    // not get new candidates it idles around but speed display would
-    // show it as working.
-    // if we instantly set it to 0 after reading it happens that the
-    // speed can be shown as zero if the users refreshes too fast.
-    // therefore, we add a timestamp when a stat was recorded and if its
-    // too old we will not use it
-
     speed_cnt[device_id] = 0;
     speed_ms[device_id]  = 0;
 
     for (int i = 0; i < SPEED_CACHE; i++)
     {
-      double rec_ms;
-
-      hc_timer_get (device_param->speed_rec[i], rec_ms);
-
-      if (rec_ms > SPEED_MAXAGE) continue;
-
       speed_cnt[device_id] += device_param->speed_cnt[i];
       speed_ms[device_id]  += device_param->speed_ms[i];
     }
@@ -2442,8 +2424,18 @@ static void run_kernel (const uint kern_run, hc_device_param_t *device_param, co
 
     hc_clGetKernelWorkGroupInfo (data.ocl, kernel, device_param->device, CL_KERNEL_WORK_GROUP_SIZE, sizeof (size_t), &workgroup_size, NULL);
 
+    if (kern_run == KERN_RUN_2)
+    {
+      if (data.opti_type & OPTI_TYPE_SLOW_HASH_SIMD)
+      {
+        num_elements = CEIL ((float) num_elements / device_param->vector_width);
+      }
+    }
+
     if (kernel_threads > workgroup_size) kernel_threads = workgroup_size;
 
+    while (num_elements % kernel_threads) num_elements++;
+
     const size_t global_work_size[3] = { num_elements,   1, 1 };
     const size_t local_work_size[3]  = { kernel_threads, 1, 1 };
 
@@ -2694,6 +2686,24 @@ static void choose_kernel (hc_device_param_t *device_param, const uint attack_ex
       if (data.devices_status == STATUS_CRACKED) break;
       if (data.devices_status == STATUS_ABORTED) break;
       if (data.devices_status == STATUS_QUIT)    break;
+
+      /**
+       * speed
+       */
+
+      const float iter_part = (float) (loop_pos + loop_left) / iter;
+
+      const u64 perf_sum_all = pws_cnt * iter_part;
+
+      double speed_ms;
+
+      hc_timer_get (device_param->timer_speed, speed_ms);
+
+      const u32 speed_pos = device_param->speed_pos;
+
+      device_param->speed_cnt[speed_pos] = perf_sum_all;
+
+      device_param->speed_ms[speed_pos] = speed_ms;
     }
 
     if (opts_type & OPTS_TYPE_HOOK23)
@@ -3008,6 +3018,20 @@ static void autotune (hc_device_param_t *device_param)
     }
   }
 
+  // because of the balance we may have some free space left!
+  // at this point, allow a small variance to overdrive the limit
+
+  const int exec_left = (target_ms * 1.2) / exec_best;
+
+  const int accel_left = kernel_accel_max / kernel_accel_best;
+
+  const int exec_accel_min = MIN (exec_left, accel_left);
+
+  if (exec_accel_min)
+  {
+    kernel_accel_best *= exec_accel_min;
+  }
+
   // reset timer
 
   device_param->exec_pos = 0;
@@ -3371,8 +3395,6 @@ static void run_cracker (hc_device_param_t *device_param, const uint pws_cnt)
 
       device_param->speed_ms[speed_pos] = speed_ms;
 
-      device_param->speed_rec[speed_pos] = device_param->timer_speed;
-
       hc_thread_mutex_unlock (mux_display);
 
       speed_pos++;
@@ -3382,12 +3404,6 @@ static void run_cracker (hc_device_param_t *device_param, const uint pws_cnt)
         speed_pos = 0;
       }
 
-      // average speed
-
-      device_param->speed_cnt_total += perf_sum_all;
-
-      device_param->speed_ms_total += speed_ms;
-
       /**
        * benchmark
        */
@@ -5321,6 +5337,9 @@ int main (int argc, char **argv)
   if (getenv ("CUDA_CACHE_DISABLE") == NULL)
     putenv ((char *) "CUDA_CACHE_DISABLE=1");
 
+  if (getenv ("POCL_KERNEL_CACHE") == NULL)
+    putenv ((char *) "POCL_KERNEL_CACHE=0");
+
   /**
    * Real init
    */
@@ -5981,7 +6000,7 @@ int main (int argc, char **argv)
     return (-1);
   }
 
-  if (hash_mode_chgd && hash_mode > 13400) // just added to remove compiler warnings for hash_mode_chgd
+  if (hash_mode_chgd && hash_mode > 13500) // just added to remove compiler warnings for hash_mode_chgd
   {
     log_error ("ERROR: Invalid hash-type specified");
 
@@ -6328,13 +6347,7 @@ int main (int argc, char **argv)
 
   if (loopback == 1)
   {
-    if (attack_mode == ATTACK_MODE_BF)
-    {
-      log_error ("ERROR: Parameter loopback not allowed in attack-mode 3");
-
-      return (-1);
-    }
-    else if (attack_mode == ATTACK_MODE_STRAIGHT)
+    if (attack_mode == ATTACK_MODE_STRAIGHT)
     {
       if ((rp_files_cnt == 0) && (rp_gen == 0))
       {
@@ -6343,6 +6356,12 @@ int main (int argc, char **argv)
         return (-1);
       }
     }
+    else
+    {
+      log_error ("ERROR: Parameter loopback allowed in attack-mode 0 only");
+
+      return (-1);
+    }
   }
 
   if (debug_mode > 0)
@@ -7619,7 +7638,8 @@ int main (int argc, char **argv)
                    dgst_size   = DGST_SIZE_4_4;
                    parse_func  = phpass_parse_hash;
                    sort_by_digest = sort_by_digest_4_4;
-                   opti_type   = OPTI_TYPE_ZERO_BYTE;
+                   opti_type   = OPTI_TYPE_ZERO_BYTE
+                               | OPTI_TYPE_SLOW_HASH_SIMD;
                    dgst_pos0   = 0;
                    dgst_pos1   = 1;
                    dgst_pos2   = 2;
@@ -10307,6 +10327,28 @@ int main (int argc, char **argv)
                    dgst_pos3   = 3;
                    break;
 
+      case 13500:  hash_type   = HASH_TYPE_SHA1;
+                   salt_type   = SALT_TYPE_EMBEDDED;
+                   attack_exec = ATTACK_EXEC_INSIDE_KERNEL;
+                   opts_type   = OPTS_TYPE_PT_GENERATE_BE
+                               | OPTS_TYPE_PT_UNICODE
+                                                | OPTS_TYPE_PT_ADD80;
+                   kern_type   = KERN_TYPE_PSTOKEN;
+                   dgst_size   = DGST_SIZE_4_5;
+                   parse_func  = pstoken_parse_hash;
+                   sort_by_digest = sort_by_digest_4_5;
+                   opti_type   = OPTI_TYPE_ZERO_BYTE
+                               | OPTI_TYPE_PRECOMPUTE_INIT
+                               | OPTI_TYPE_EARLY_SKIP
+                               | OPTI_TYPE_NOT_ITERATED
+                               | OPTI_TYPE_PREPENDED_SALT
+                               | OPTI_TYPE_RAW_HASH;
+                   dgst_pos0   = 3;
+                   dgst_pos1   = 4;
+                   dgst_pos2   = 2;
+                   dgst_pos3   = 1;
+                   break;
+
       default:     usage_mini_print (PROGNAME); return (-1);
     }
 
@@ -10412,6 +10454,7 @@ int main (int argc, char **argv)
       case 13000:  esalt_size = sizeof (rar5_t);          break;
       case 13100:  esalt_size = sizeof (krb5tgs_t);       break;
       case 13400:  esalt_size = sizeof (keepass_t);       break;
+      case 13500:  esalt_size = sizeof (pstoken_t);       break;
     }
 
     data.esalt_size = esalt_size;
@@ -11398,7 +11441,8 @@ int main (int argc, char **argv)
 
         switch (hash_mode)
         {
-          case  1500: hashes_buf[0].salt->salt_len = 2;
+          case  1500: hashes_buf[0].salt->salt_len    = 2;
+                      hashes_buf[0].salt->salt_buf[0] = 388; // pure magic
                       break;
           case  1731: hashes_buf[0].salt->salt_len = 4;
                       break;
@@ -11516,6 +11560,8 @@ int main (int argc, char **argv)
                       break;
           case 13400: ((keepass_t *) hashes_buf[0].esalt)->version       = 2;
                       break;
+          case 13500: ((pstoken_t *) hashes_buf[0].esalt)->salt_len      = 113;
+                      break;
         }
       }
 
@@ -12685,6 +12731,49 @@ int main (int argc, char **argv)
 
       hc_clGetDeviceIDs (data.ocl, platform, CL_DEVICE_TYPE_ALL, DEVICES_MAX, platform_devices, &platform_devices_cnt);
 
+      char platform_vendor[INFOSZ] = { 0 };
+
+      hc_clGetPlatformInfo (data.ocl, platform, CL_PLATFORM_VENDOR, sizeof (platform_vendor), platform_vendor, NULL);
+
+      // find our own platform vendor because pocl and mesa are pushing original vendor_id through opencl
+      // this causes trouble with vendor id based macros
+      // we'll assign generic to those without special optimization available
+
+      cl_uint vendor_id = 0;
+
+      if (strcmp (platform_vendor, CL_VENDOR_AMD) == 0)
+      {
+        vendor_id = VENDOR_ID_AMD;
+      }
+      else if (strcmp (platform_vendor, CL_VENDOR_APPLE) == 0)
+      {
+        vendor_id = VENDOR_ID_GENERIC;
+      }
+      else if (strcmp (platform_vendor, CL_VENDOR_INTEL_BEIGNET) == 0)
+      {
+        vendor_id = VENDOR_ID_GENERIC;
+      }
+      else if (strcmp (platform_vendor, CL_VENDOR_INTEL_SDK) == 0)
+      {
+        vendor_id = VENDOR_ID_GENERIC;
+      }
+      else if (strcmp (platform_vendor, CL_VENDOR_MESA) == 0)
+      {
+        vendor_id = VENDOR_ID_GENERIC;
+      }
+      else if (strcmp (platform_vendor, CL_VENDOR_NV) == 0)
+      {
+        vendor_id = VENDOR_ID_NV;
+      }
+      else if (strcmp (platform_vendor, CL_VENDOR_POCL) == 0)
+      {
+        vendor_id = VENDOR_ID_GENERIC;
+      }
+      else
+      {
+        vendor_id = VENDOR_ID_GENERIC;
+      }
+
       for (uint platform_devices_id = 0; platform_devices_id < platform_devices_cnt; platform_devices_id++)
       {
         size_t param_value_size = 0;
@@ -12693,6 +12782,8 @@ int main (int argc, char **argv)
 
         hc_device_param_t *device_param = &data.devices_param[device_id];
 
+        device_param->vendor_id = vendor_id;
+
         device_param->device = platform_devices[platform_devices_id];
 
         device_param->device_id = device_id;
@@ -12709,14 +12800,6 @@ int main (int argc, char **argv)
 
         device_param->device_type = device_type;
 
-        // vendor_id
-
-        cl_uint vendor_id = 0;
-
-        hc_clGetDeviceInfo (data.ocl, device_param->device, CL_DEVICE_VENDOR_ID, sizeof (vendor_id), &vendor_id, NULL);
-
-        device_param->vendor_id = vendor_id;
-
         // device_name
 
         hc_clGetDeviceInfo (data.ocl, device_param->device, CL_DEVICE_NAME, 0, NULL, &param_value_size);
@@ -12753,16 +12836,6 @@ int main (int argc, char **argv)
 
         myfree (device_opencl_version);
 
-        if (strstr (device_version, "pocl"))
-        {
-          // pocl returns the real vendor_id in CL_DEVICE_VENDOR_ID which causes many problems because of hms and missing amd_bfe () etc
-          // we need to overwrite vendor_id to avoid this. maybe open pocl issue?
-
-          cl_uint vendor_id = VENDOR_ID_GENERIC;
-
-          device_param->vendor_id = vendor_id;
-        }
-
         // vector_width
 
         cl_uint vector_width;
@@ -12945,31 +13018,9 @@ int main (int argc, char **argv)
 
         if (device_param->skipped == 0)
         {
-          if (strstr (device_version, "pocl"))
-          {
-            if (force == 0)
-            {
-              log_info ("");
-              log_info ("ATTENTION! All pocl drivers are known to be broken due to broken LLVM <= 3.7");
-              log_info ("You are STRONGLY encouraged not to use it");
-              log_info ("You can use --force to override this but do not post error reports if you do so");
-              log_info ("");
-
-              return (-1);
-            }
-          }
-
           if (device_type & CL_DEVICE_TYPE_GPU)
           {
-            if (vendor_id == VENDOR_ID_NV)
-            {
-              if (device_param->kernel_exec_timeout != 0)
-              {
-                if (data.quiet == 0) log_info ("Device #%u: WARNING! Kernel exec timeout is not disabled, it might cause you errors of code 702", device_id + 1);
-                if (data.quiet == 0) log_info ("           See the wiki on how to disable it: https://hashcat.net/wiki/doku.php?id=timeout_patch");
-              }
-            }
-            else if (vendor_id == VENDOR_ID_AMD)
+            if (vendor_id == VENDOR_ID_AMD)
             {
               int catalyst_check = (force == 1) ? 0 : 1;
 
@@ -13014,6 +13065,27 @@ int main (int argc, char **argv)
                 log_info ("You can use --force to override this but do not post error reports if you do so");
                 log_info ("");
 
+                return (-1);
+              }
+            }
+            else if (vendor_id == VENDOR_ID_NV)
+            {
+              if (device_param->kernel_exec_timeout != 0)
+              {
+                if (data.quiet == 0) log_info ("Device #%u: WARNING! Kernel exec timeout is not disabled, it might cause you errors of code 702", device_id + 1);
+                if (data.quiet == 0) log_info ("           See the wiki on how to disable it: https://hashcat.net/wiki/doku.php?id=timeout_patch");
+              }
+            }
+            else if (vendor_id == VENDOR_ID_POCL)
+            {
+              if (force == 0)
+              {
+                log_info ("");
+                log_info ("ATTENTION! All pocl drivers are known to be broken due to broken LLVM <= 3.7");
+                log_info ("You are STRONGLY encouraged not to use it");
+                log_info ("You can use --force to override this but do not post error reports if you do so");
+                log_info ("");
+
                 return (-1);
               }
             }
@@ -13885,7 +13957,7 @@ int main (int argc, char **argv)
 
       // we don't have sm_* on vendors not NV but it doesn't matter
 
-      snprintf (build_opts, sizeof (build_opts) - 1, "-I\"%s/\" -DVENDOR_ID=%u -DCUDA_ARCH=%d -DVECT_SIZE=%u -DDEVICE_TYPE=%u", shared_dir, device_param->vendor_id, (device_param->sm_major * 100) + device_param->sm_minor, device_param->vector_width, (u32) device_param->device_type);
+      snprintf (build_opts, sizeof (build_opts) - 1, "-cl-std=CL1.1 -I\"%s/\" -DVENDOR_ID=%u -DCUDA_ARCH=%d -DVECT_SIZE=%u -DDEVICE_TYPE=%u", shared_dir, device_param->vendor_id, (device_param->sm_major * 100) + device_param->sm_minor, device_param->vector_width, (u32) device_param->device_type);
 
       /**
        * main kernel
@@ -16033,10 +16105,6 @@ int main (int argc, char **argv)
 
           memset (device_param->speed_cnt, 0, SPEED_CACHE * sizeof (u64));
           memset (device_param->speed_ms,  0, SPEED_CACHE * sizeof (double));
-          memset (device_param->speed_rec, 0, SPEED_CACHE * sizeof (hc_timer_t));
-
-          device_param->speed_cnt_total = 0;
-          device_param->speed_ms_total  = 0;
 
           device_param->exec_pos = 0;