Fix calling conventions for external libraries (mingw doesn't handle it alone); Fix...
[hashcat.git] / src / hashcat.c
index 10c70f0..14e2170 100644 (file)
@@ -164,6 +164,12 @@ double TARGET_MS_PROFILE[4]     = { 2, 12, 96, 480 };
   attr = NULL;            \
 }
 
+#if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
+#define HC_API_CALL __stdcall
+#else
+#define HC_API_CALL
+#endif
+
 static uint default_benchmark_algorithms[NUM_DEFAULT_BENCHMARK_ALGORITHMS] =
 {
   900,
@@ -3816,7 +3822,7 @@ static void *thread_monitor (void *p)
   uint status_left  = data.status_timer;
 
   #ifdef HAVE_HWMON
-  uint hwmon_check   = 0;
+  uint hwmon_check = 0;
 
   int slowdown_warnings = 0;
 
@@ -3884,7 +3890,7 @@ static void *thread_monitor (void *p)
 
     #ifdef HAVE_HWMON
 
-    if (1)
+    if (hwmon_check == 1)
     {
       hc_thread_mutex_lock (mux_adl);
 
@@ -3894,35 +3900,47 @@ static void *thread_monitor (void *p)
 
         if (device_param->skipped) continue;
 
-        if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) continue;
+        if (device_param->device_vendor_id == VENDOR_ID_NV)
+        {
+          if (data.hm_nvapi)
+          {
+            NV_GPU_PERF_POLICIES_INFO_PARAMS_V1   perfPolicies_info   = { 0 };
+            NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1 perfPolicies_status = { 0 };
 
-        const int temperature = hm_get_temperature_with_device_id (device_id);
+            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);
 
-        const int threshold = data.hm_device[device_id].gpu_temp_threshold_slowdown;
+            hm_NvAPI_GPU_GetPerfPoliciesInfo (data.hm_nvapi, data.hm_device[device_id].nvapi, &perfPolicies_info);
 
-        if (temperature >= threshold)
-        {
-          if (slowdown_warnings < 3)
-          {
-            if (data.quiet == 0) clear_prompt ();
+            perfPolicies_status.info_value = perfPolicies_info.info_value;
 
-            log_info ("WARNING: Drivers temperature threshold (%dc) hit on GPU #%d, expect performance to drop...", threshold, device_id + 1);
+            hm_NvAPI_GPU_GetPerfPoliciesStatus (data.hm_nvapi, data.hm_device[device_id].nvapi, &perfPolicies_status);
 
-            if (slowdown_warnings == 2)
+            if (perfPolicies_status.throttle & 2)
             {
-              log_info ("");
-            }
+              if (slowdown_warnings < 3)
+              {
+                if (data.quiet == 0) clear_prompt ();
+
+                log_info ("WARNING: Drivers temperature threshold hit on GPU #%d, expect performance to drop...", device_id + 1);
+
+                if (slowdown_warnings == 2)
+                {
+                  log_info ("");
+                }
 
-            if (data.quiet == 0) fprintf (stdout, "%s", PROMPT);
-            if (data.quiet == 0) fflush (stdout);
+                if (data.quiet == 0) fprintf (stdout, "%s", PROMPT);
+                if (data.quiet == 0) fflush (stdout);
 
-            slowdown_warnings++;
+                slowdown_warnings++;
+              }
+            }
+            else
+            {
+              slowdown_warnings = 0;
+            }
           }
         }
-        else
-        {
-          slowdown_warnings = 0;
-        }
       }
 
       hc_thread_mutex_unlock (mux_adl);
@@ -5360,7 +5378,7 @@ static uint hlfmt_detect (FILE *fp, uint max_check)
 // wrapper around mymalloc for ADL
 
 #if defined(HAVE_HWMON)
-void *__stdcall ADL_Main_Memory_Alloc (const int iSize)
+void *HC_API_CALL ADL_Main_Memory_Alloc (const int iSize)
 {
   return mymalloc (iSize);
 }
@@ -5435,8 +5453,8 @@ void SetConsoleWindowSize (const int x)
 
   COORD co;
 
-  co.X = sr->Right  + 1;
-  co.Y = sr->Bottom + 1;
+  co.X = sr->Right + 1;
+  co.Y = 9999;
 
   if (!SetConsoleScreenBufferSize (h, co)) return;
 
@@ -13673,8 +13691,11 @@ int main (int argc, char **argv)
           {
             if (data.force == 0)
             {
-              log_info ("Device #%u: WARNING: not native intel opencl runtime, expect massive speed loss", device_id + 1);
-              log_info ("           You can use --force to override this but do not post error reports if you do so");
+              if (algorithm_pos == 0)
+              {
+                log_info ("Device #%u: WARNING: not native intel opencl runtime, expect massive speed loss", device_id + 1);
+                log_info ("           You can use --force to override this but do not post error reports if you do so");
+              }
 
               device_param->skipped = 1;
             }
@@ -14004,9 +14025,9 @@ int main (int argc, char **argv)
      */
 
     #ifdef HAVE_HWMON
-    hm_attrs_t hm_adapters_adl[DEVICES_MAX]   = { { { 0 }, 0, 0, 0, 0, 0 } };
-    hm_attrs_t hm_adapters_nvapi[DEVICES_MAX] = { { { 0 }, 0, 0, 0, 0, 0 } };
-    hm_attrs_t hm_adapters_nvml[DEVICES_MAX]  = { { { 0 }, 0, 0, 0, 0, 0 } };
+    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 } };
 
     if (gpu_temp_disable == 0)
     {
@@ -14035,18 +14056,18 @@ int main (int argc, char **argv)
 
           for (int i = 0; i < tmp_in; i++)
           {
-            hm_adapters_nvml[tmp_out++].adapter_index.nvml = nvmlGPUHandle[i];
+            hm_adapters_nvml[tmp_out++].nvml = nvmlGPUHandle[i];
           }
 
           for (int i = 0; i < tmp_out; i++)
           {
             unsigned int speed;
 
-            if (hm_NVML_nvmlDeviceGetFanSpeed (data.hm_nvml, 0, hm_adapters_nvml[i].adapter_index.nvml, &speed) == NVML_SUCCESS) hm_adapters_nvml[i].fan_get_supported = 1;
+            if (hm_NVML_nvmlDeviceGetFanSpeed (data.hm_nvml, 0, hm_adapters_nvml[i].nvml, &speed) == NVML_SUCCESS) hm_adapters_nvml[i].fan_get_supported = 1;
 
-            hm_NVML_nvmlDeviceSetComputeMode (data.hm_nvml, 1, hm_adapters_nvml[i].adapter_index.nvml, NVML_COMPUTEMODE_EXCLUSIVE_PROCESS);
+            hm_NVML_nvmlDeviceSetComputeMode (data.hm_nvml, 1, hm_adapters_nvml[i].nvml, NVML_COMPUTEMODE_EXCLUSIVE_PROCESS);
 
-            hm_NVML_nvmlDeviceSetGpuOperationMode (data.hm_nvml, 1, hm_adapters_nvml[i].adapter_index.nvml, NVML_GOM_ALL_ON);
+            hm_NVML_nvmlDeviceSetGpuOperationMode (data.hm_nvml, 1, hm_adapters_nvml[i].nvml, NVML_GOM_ALL_ON);
           }
         }
       }
@@ -14068,7 +14089,7 @@ int main (int argc, char **argv)
 
           for (int i = 0; i < tmp_in; i++)
           {
-            hm_adapters_nvapi[tmp_out++].adapter_index.nvapi = nvGPUHandle[i];
+            hm_adapters_nvapi[tmp_out++].nvapi = nvGPUHandle[i];
           }
         }
       }
@@ -14253,14 +14274,24 @@ int main (int argc, char **argv)
 
         const uint platform_devices_id = device_param->platform_devices_id;
 
-        if (device_param->device_vendor_id == VENDOR_ID_NV)
+        if (device_param->device_vendor_id == VENDOR_ID_AMD)
         {
-          memcpy (&data.hm_device[device_id], &hm_adapters_nvml[platform_devices_id], sizeof (hm_attrs_t));
+          data.hm_device[device_id].adl               = hm_adapters_adl[platform_devices_id].adl;
+          data.hm_device[device_id].nvapi             = 0;
+          data.hm_device[device_id].nvml              = 0;
+          data.hm_device[device_id].od_version        = hm_adapters_adl[platform_devices_id].od_version;
+          data.hm_device[device_id].fan_get_supported = hm_adapters_adl[platform_devices_id].fan_get_supported;
+          data.hm_device[device_id].fan_set_supported = hm_adapters_adl[platform_devices_id].fan_set_supported;
         }
 
-        if (device_param->device_vendor_id == VENDOR_ID_AMD)
+        if (device_param->device_vendor_id == VENDOR_ID_NV)
         {
-          memcpy (&data.hm_device[device_id], &hm_adapters_adl[platform_devices_id], sizeof (hm_attrs_t));
+          data.hm_device[device_id].adl               = 0;
+          data.hm_device[device_id].nvapi             = hm_adapters_nvapi[platform_devices_id].nvapi;
+          data.hm_device[device_id].nvml              = hm_adapters_nvml[platform_devices_id].nvml;
+          data.hm_device[device_id].od_version        = 0;
+          data.hm_device[device_id].fan_get_supported = hm_adapters_nvml[platform_devices_id].fan_get_supported;
+          data.hm_device[device_id].fan_set_supported = 0;
         }
       }
     }
@@ -14297,7 +14328,7 @@ int main (int argc, char **argv)
 
             int powertune_supported = 0;
 
-            if ((ADL_rc = hm_ADL_Overdrive6_PowerControl_Caps (data.hm_adl, data.hm_device[device_id].adapter_index.adl, &powertune_supported)) != ADL_OK)
+            if ((ADL_rc = hm_ADL_Overdrive6_PowerControl_Caps (data.hm_adl, data.hm_device[device_id].adl, &powertune_supported)) != ADL_OK)
             {
               log_error ("ERROR: Failed to get ADL PowerControl Capabilities");
 
@@ -14312,9 +14343,9 @@ int main (int argc, char **argv)
 
               ADLOD6PowerControlInfo powertune = {0, 0, 0, 0, 0};
 
-              if ((ADL_rc = hm_ADL_Overdrive_PowerControlInfo_Get (data.hm_adl, data.hm_device[device_id].adapter_index.adl, &powertune)) == ADL_OK)
+              if ((ADL_rc = hm_ADL_Overdrive_PowerControlInfo_Get (data.hm_adl, data.hm_device[device_id].adl, &powertune)) == ADL_OK)
               {
-                ADL_rc = hm_ADL_Overdrive_PowerControl_Get (data.hm_adl, data.hm_device[device_id].adapter_index.adl, &od_power_control_status[device_id]);
+                ADL_rc = hm_ADL_Overdrive_PowerControl_Get (data.hm_adl, data.hm_device[device_id].adl, &od_power_control_status[device_id]);
               }
 
               if (ADL_rc != ADL_OK)
@@ -14324,7 +14355,7 @@ int main (int argc, char **argv)
                 return (-1);
               }
 
-              if ((ADL_rc = hm_ADL_Overdrive_PowerControl_Set (data.hm_adl, data.hm_device[device_id].adapter_index.adl, powertune.iMaxValue)) != ADL_OK)
+              if ((ADL_rc = hm_ADL_Overdrive_PowerControl_Set (data.hm_adl, data.hm_device[device_id].adl, powertune.iMaxValue)) != ADL_OK)
               {
                 log_error ("ERROR: Failed to set new ADL PowerControl values");
 
@@ -14337,7 +14368,7 @@ int main (int argc, char **argv)
 
               od_clock_mem_status[device_id].state.iNumberOfPerformanceLevels = 2;
 
-              if ((ADL_rc = hm_ADL_Overdrive_StateInfo_Get (data.hm_adl, data.hm_device[device_id].adapter_index.adl, ADL_OD6_GETSTATEINFO_CUSTOM_PERFORMANCE, &od_clock_mem_status[device_id])) != ADL_OK)
+              if ((ADL_rc = hm_ADL_Overdrive_StateInfo_Get (data.hm_adl, data.hm_device[device_id].adl, ADL_OD6_GETSTATEINFO_CUSTOM_PERFORMANCE, &od_clock_mem_status[device_id])) != ADL_OK)
               {
                 log_error ("ERROR: Failed to get ADL memory and engine clock frequency");
 
@@ -14348,7 +14379,7 @@ int main (int argc, char **argv)
 
               ADLOD6Capabilities caps = {0, 0, 0, {0, 0, 0}, {0, 0, 0}, 0, 0};
 
-              if ((ADL_rc = hm_ADL_Overdrive_Capabilities_Get (data.hm_adl, data.hm_device[device_id].adapter_index.adl, &caps)) != ADL_OK)
+              if ((ADL_rc = hm_ADL_Overdrive_Capabilities_Get (data.hm_adl, data.hm_device[device_id].adl, &caps)) != ADL_OK)
               {
                 log_error ("ERROR: Failed to get ADL device capabilities");
 
@@ -14385,7 +14416,7 @@ int main (int argc, char **argv)
               performance_state->aLevels[0].iMemoryClock = memory_clock_profile_max;
               performance_state->aLevels[1].iMemoryClock = memory_clock_profile_max;
 
-              if ((ADL_rc = hm_ADL_Overdrive_State_Set (data.hm_adl, data.hm_device[device_id].adapter_index.adl, ADL_OD6_SETSTATE_PERFORMANCE, performance_state)) != ADL_OK)
+              if ((ADL_rc = hm_ADL_Overdrive_State_Set (data.hm_adl, data.hm_device[device_id].adl, ADL_OD6_SETSTATE_PERFORMANCE, performance_state)) != ADL_OK)
               {
                 log_info ("ERROR: Failed to set ADL performance state");
 
@@ -14402,14 +14433,14 @@ int main (int argc, char **argv)
               // powertune set
               ADLOD6PowerControlInfo powertune = {0, 0, 0, 0, 0};
 
-              if ((ADL_rc = hm_ADL_Overdrive_PowerControlInfo_Get (data.hm_adl, data.hm_device[device_id].adapter_index.adl, &powertune)) != ADL_OK)
+              if ((ADL_rc = hm_ADL_Overdrive_PowerControlInfo_Get (data.hm_adl, data.hm_device[device_id].adl, &powertune)) != ADL_OK)
               {
                 log_error ("ERROR: Failed to get current ADL PowerControl settings");
 
                 return (-1);
               }
 
-              if ((ADL_rc = hm_ADL_Overdrive_PowerControl_Set (data.hm_adl, data.hm_device[device_id].adapter_index.adl, powertune.iMaxValue)) != ADL_OK)
+              if ((ADL_rc = hm_ADL_Overdrive_PowerControl_Set (data.hm_adl, data.hm_device[device_id].adl, powertune.iMaxValue)) != ADL_OK)
               {
                 log_error ("ERROR: Failed to set new ADL PowerControl values");
 
@@ -14427,7 +14458,7 @@ int main (int argc, char **argv)
 
           int powertune_supported = 0;
 
-          if (hm_NVML_nvmlDeviceGetPowerManagementLimit (data.hm_nvml, 0, data.hm_device[device_id].adapter_index.nvml, &limit) == NVML_SUCCESS)
+          if (hm_NVML_nvmlDeviceGetPowerManagementLimit (data.hm_nvml, 0, data.hm_device[device_id].nvml, &limit) == NVML_SUCCESS)
           {
             powertune_supported = 1;
           }
@@ -14439,11 +14470,11 @@ int main (int argc, char **argv)
             unsigned int minLimit;
             unsigned int maxLimit;
 
-            if (hm_NVML_nvmlDeviceGetPowerManagementLimitConstraints (data.hm_nvml, 0, data.hm_device[device_id].adapter_index.nvml, &minLimit, &maxLimit) == NVML_SUCCESS)
+            if (hm_NVML_nvmlDeviceGetPowerManagementLimitConstraints (data.hm_nvml, 0, data.hm_device[device_id].nvml, &minLimit, &maxLimit) == NVML_SUCCESS)
             {
               if (maxLimit > 0)
               {
-                if (hm_NVML_nvmlDeviceSetPowerManagementLimit (data.hm_nvml, 0, data.hm_device[device_id].adapter_index.nvml, maxLimit) == NVML_SUCCESS)
+                if (hm_NVML_nvmlDeviceSetPowerManagementLimit (data.hm_nvml, 0, data.hm_device[device_id].nvml, maxLimit) == NVML_SUCCESS)
                 {
                   // now we can be sure we need to reset later
 
@@ -15738,21 +15769,6 @@ int main (int argc, char **argv)
 
       #if defined(HAVE_HWMON)
 
-      /**
-       * Store thermal target temperature so we can send a notice to user
-       */
-
-      if (gpu_temp_disable == 0)
-      {
-        const int gpu_temp_threshold_slowdown = hm_get_threshold_slowdown_with_device_id (device_id);
-        const int gpu_temp_threshold_shutdown = hm_get_threshold_slowdown_with_device_id (device_id);
-
-        data.hm_device[device_id].gpu_temp_threshold_slowdown = (gpu_temp_threshold_slowdown > 0) ? gpu_temp_threshold_slowdown : 10000;
-        data.hm_device[device_id].gpu_temp_threshold_shutdown = (gpu_temp_threshold_shutdown > 0) ? gpu_temp_threshold_shutdown : 10000;
-
-        // we could use those numbers for gpu_temp_retain and gpu_temp_abort, too
-      }
-
       /**
        * Store initial fanspeed if gpu_temp_retain is enabled
        */
@@ -17547,35 +17563,12 @@ int main (int argc, char **argv)
           }
         }
 
-        data.devices_status = STATUS_RUNNING;
-
-        if (initial_restore_done == 0)
-        {
-          if (data.restore_disable == 0) cycle_restore ();
-
-          initial_restore_done = 1;
-        }
-
-        hc_timer_set (&data.timer_running);
-
-        if ((wordlist_mode == WL_MODE_FILE) || (wordlist_mode == WL_MODE_MASK))
-        {
-          if ((quiet == 0) && (status == 0) && (benchmark == 0))
-          {
-            if (quiet == 0) fprintf (stdout, "%s", PROMPT);
-            if (quiet == 0) fflush (stdout);
-          }
-        }
-        else if (wordlist_mode == WL_MODE_STDIN)
-        {
-          if (data.quiet == 0) log_info ("Starting attack in stdin mode...");
-          if (data.quiet == 0) log_info ("");
-        }
-
         /**
          * create autotune threads
          */
 
+        data.devices_status = STATUS_AUTOTUNE;
+
         hc_thread_t *c_threads = (hc_thread_t *) mycalloc (data.devices_cnt, sizeof (hc_thread_t));
 
         for (uint device_id = 0; device_id < data.devices_cnt; device_id++)
@@ -17622,6 +17615,31 @@ int main (int argc, char **argv)
          * create cracker threads
          */
 
+        data.devices_status = STATUS_RUNNING;
+
+        if (initial_restore_done == 0)
+        {
+          if (data.restore_disable == 0) cycle_restore ();
+
+          initial_restore_done = 1;
+        }
+
+        hc_timer_set (&data.timer_running);
+
+        if ((wordlist_mode == WL_MODE_FILE) || (wordlist_mode == WL_MODE_MASK))
+        {
+          if ((quiet == 0) && (status == 0) && (benchmark == 0))
+          {
+            if (quiet == 0) fprintf (stdout, "%s", PROMPT);
+            if (quiet == 0) fflush (stdout);
+          }
+        }
+        else if (wordlist_mode == WL_MODE_STDIN)
+        {
+          if (data.quiet == 0) log_info ("Starting attack in stdin mode...");
+          if (data.quiet == 0) log_info ("");
+        }
+
         time_t runtime_start;
 
         time (&runtime_start);
@@ -17998,7 +18016,7 @@ int main (int argc, char **argv)
 
             int powertune_supported = 0;
 
-            if ((hm_ADL_Overdrive6_PowerControl_Caps (data.hm_adl, data.hm_device[device_id].adapter_index.adl, &powertune_supported)) != ADL_OK)
+            if ((hm_ADL_Overdrive6_PowerControl_Caps (data.hm_adl, data.hm_device[device_id].adl, &powertune_supported)) != ADL_OK)
             {
               log_error ("ERROR: Failed to get ADL PowerControl Capabilities");
 
@@ -18009,7 +18027,7 @@ int main (int argc, char **argv)
             {
               // powercontrol settings
 
-              if ((hm_ADL_Overdrive_PowerControl_Set (data.hm_adl, data.hm_device[device_id].adapter_index.adl, od_power_control_status[device_id])) != ADL_OK)
+              if ((hm_ADL_Overdrive_PowerControl_Set (data.hm_adl, data.hm_device[device_id].adl, od_power_control_status[device_id])) != ADL_OK)
               {
                 log_info ("ERROR: Failed to restore the ADL PowerControl values");
 
@@ -18027,7 +18045,7 @@ int main (int argc, char **argv)
               performance_state->aLevels[0].iMemoryClock = od_clock_mem_status[device_id].state.aLevels[0].iMemoryClock;
               performance_state->aLevels[1].iMemoryClock = od_clock_mem_status[device_id].state.aLevels[1].iMemoryClock;
 
-              if ((hm_ADL_Overdrive_State_Set (data.hm_adl, data.hm_device[device_id].adapter_index.adl, ADL_OD6_SETSTATE_PERFORMANCE, performance_state)) != ADL_OK)
+              if ((hm_ADL_Overdrive_State_Set (data.hm_adl, data.hm_device[device_id].adl, ADL_OD6_SETSTATE_PERFORMANCE, performance_state)) != ADL_OK)
               {
                 log_info ("ERROR: Failed to restore ADL performance state");
 
@@ -18045,7 +18063,7 @@ int main (int argc, char **argv)
 
           if (limit > 0)
           {
-            hm_NVML_nvmlDeviceSetPowerManagementLimit (data.hm_nvml, 0, data.hm_device[device_id].adapter_index.nvml, limit);
+            hm_NVML_nvmlDeviceSetPowerManagementLimit (data.hm_nvml, 0, data.hm_device[device_id].nvml, limit);
           }
         }
       }