Add --mangle switch for mangling password before hashing
[hashcat.git] / src / ext_nvml.c
index 2a66214..6c3b6b4 100644 (file)
@@ -9,18 +9,64 @@
 
 int nvml_init (NVML_PTR *nvml)
 {
-  if (!nvml) return (-1);
+  if (!nvml) return -1;
 
   memset (nvml, 0, sizeof (NVML_PTR));
 
+  #ifdef _WIN
+  nvml->lib = hc_dlopen ("nvml.dll");
+
+  if (!nvml->lib)
+  {
+    DWORD BufferSize = 1024;
+
+    DWORD Type = REG_SZ;
+
+    char *Buffer = (char *) mymalloc (BufferSize + 1);
+
+    HKEY hKey = 0;
+
+    if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\NVIDIA Corporation\\Global\\NVSMI"), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
+    {
+      if (RegQueryValueEx (hKey, TEXT("NVSMIPATH"), NULL, &Type, (PVOID) Buffer, &BufferSize) == ERROR_SUCCESS)
+      {
+        Buffer[BufferSize] = 0;
+      }
+      else
+      {
+        if (data.quiet == 0)
+          log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled.");
+
+        return -1;
+      }
+
+      RegCloseKey (hKey);
+    }
+    else
+    {
+      if (data.quiet == 0)
+        log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled.");
+
+      return -1;
+    }
+
+    strcat (Buffer, "\\nvml.dll");
+
+    nvml->lib = hc_dlopen (Buffer);
+
+    myfree (Buffer);
+  }
+
+  #elif _POSIX
   nvml->lib = hc_dlopen ("libnvidia-ml.so", RTLD_NOW);
+  #endif
 
   if (!nvml->lib)
   {
-    //if (data.quiet == 0)
-    //  log_info ("WARNING: load NVML library failed, proceed without NVML HWMon enabled.");
+    if (data.quiet == 0)
+      log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled.");
 
-    return (-1);
+    return -1;
   }
 
   HC_LOAD_FUNC(nvml, nvmlErrorString, NVML_ERROR_STRING, NVML, 0)
@@ -33,6 +79,16 @@ int nvml_init (NVML_PTR *nvml)
   HC_LOAD_FUNC(nvml, nvmlDeviceGetPowerUsage, NVML_DEVICE_GET_POWER_USAGE, NVML, 0)
   HC_LOAD_FUNC(nvml, nvmlDeviceGetUtilizationRates, NVML_DEVICE_GET_UTILIZATION_RATES, NVML, 0)
   HC_LOAD_FUNC(nvml, nvmlDeviceGetClockInfo, NVML_DEVICE_GET_CLOCKINFO, NVML, 0)
+  HC_LOAD_FUNC(nvml, nvmlDeviceGetTemperatureThreshold, NVML_DEVICE_GET_THRESHOLD, NVML, 0)
+  HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrPcieLinkGeneration, NVML_DEVICE_GET_CURRPCIELINKGENERATION, NVML, 0)
+  HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrPcieLinkWidth, NVML_DEVICE_GET_CURRPCIELINKWIDTH, NVML, 0)
+  HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrentClocksThrottleReasons, NVML_DEVICE_GET_CURRENTCLOCKSTHROTTLEREASONS, NVML, 0)
+  HC_LOAD_FUNC(nvml, nvmlDeviceGetSupportedClocksThrottleReasons, NVML_DEVICE_GET_SUPPORTEDCLOCKSTHROTTLEREASONS, NVML, 0)
+  HC_LOAD_FUNC(nvml, nvmlDeviceSetComputeMode, NVML_DEVICE_SET_COMPUTEMODE, NVML, 0)
+  HC_LOAD_FUNC(nvml, nvmlDeviceSetGpuOperationMode, NVML_DEVICE_SET_OPERATIONMODE, NVML, 0)
+  HC_LOAD_FUNC(nvml, nvmlDeviceGetPowerManagementLimitConstraints, NVML_DEVICE_GET_POWERMANAGEMENTLIMITCONSTRAINTS, NVML, 0)
+  HC_LOAD_FUNC(nvml, nvmlDeviceSetPowerManagementLimit, NVML_DEVICE_SET_POWERMANAGEMENTLIMIT, NVML, 0)
+  HC_LOAD_FUNC(nvml, nvmlDeviceGetPowerManagementLimit, NVML_DEVICE_GET_POWERMANAGEMENTLIMIT, NVML, 0)
 
   return 0;
 }
@@ -87,7 +143,7 @@ nvmlReturn_t hm_NVML_nvmlShutdown (NVML_PTR *nvml)
   return nvml_rc;
 }
 
-nvmlReturn_t hm_NVML_nvmlDeviceGetName (NVML_PTR *nvml, nvmlDevice_t device, char *name, unsigned int length)
+nvmlReturn_t hm_NVML_nvmlDeviceGetName (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, char *name, unsigned int length)
 {
   if (!nvml) return -1;
 
@@ -95,9 +151,12 @@ nvmlReturn_t hm_NVML_nvmlDeviceGetName (NVML_PTR *nvml, nvmlDevice_t device, cha
 
   if (nvml_rc != NVML_SUCCESS)
   {
-    const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
 
-    log_info ("WARN: %s %d %s\n", "nvmlDeviceGetName()", nvml_rc, string);
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetName()", nvml_rc, string);
+    }
   }
 
   return nvml_rc;
@@ -122,7 +181,7 @@ nvmlReturn_t hm_NVML_nvmlDeviceGetHandleByIndex (NVML_PTR *nvml, int skip_warnin
   return nvml_rc;
 }
 
-nvmlReturn_t hm_NVML_nvmlDeviceGetTemperature (NVML_PTR *nvml, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp)
+nvmlReturn_t hm_NVML_nvmlDeviceGetTemperature (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp)
 {
   if (!nvml) return -1;
 
@@ -130,11 +189,12 @@ nvmlReturn_t hm_NVML_nvmlDeviceGetTemperature (NVML_PTR *nvml, nvmlDevice_t devi
 
   if (nvml_rc != NVML_SUCCESS)
   {
-    *temp = -1;
-
-    //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
 
-    //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperature()", nvml_rc, string);
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperature()", nvml_rc, string);
+    }
   }
 
   return nvml_rc;
@@ -148,8 +208,6 @@ nvmlReturn_t hm_NVML_nvmlDeviceGetFanSpeed (NVML_PTR *nvml, int skip_warnings, n
 
   if (nvml_rc != NVML_SUCCESS)
   {
-    *speed = -1;
-
     if (skip_warnings == 0)
     {
       const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
@@ -161,9 +219,7 @@ nvmlReturn_t hm_NVML_nvmlDeviceGetFanSpeed (NVML_PTR *nvml, int skip_warnings, n
   return nvml_rc;
 }
 
-/* only tesla following */
-
-nvmlReturn_t hm_NVML_nvmlDeviceGetPowerUsage (NVML_PTR *nvml, nvmlDevice_t device, unsigned int *power)
+nvmlReturn_t hm_NVML_nvmlDeviceGetPowerUsage (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *power)
 {
   if (!nvml) return -1;
 
@@ -171,17 +227,18 @@ nvmlReturn_t hm_NVML_nvmlDeviceGetPowerUsage (NVML_PTR *nvml, nvmlDevice_t devic
 
   if (nvml_rc != NVML_SUCCESS)
   {
-    *power = -1;
-
-    //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
 
-    //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerUsage()", nvml_rc, string);
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerUsage()", nvml_rc, string);
+    }
   }
 
   return nvml_rc;
 }
 
-nvmlReturn_t hm_NVML_nvmlDeviceGetUtilizationRates (NVML_PTR *nvml, nvmlDevice_t device, nvmlUtilization_t *utilization)
+nvmlReturn_t hm_NVML_nvmlDeviceGetUtilizationRates (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlUtilization_t *utilization)
 {
   if (!nvml) return -1;
 
@@ -189,18 +246,18 @@ nvmlReturn_t hm_NVML_nvmlDeviceGetUtilizationRates (NVML_PTR *nvml, nvmlDevice_t
 
   if (nvml_rc != NVML_SUCCESS)
   {
-    utilization->gpu    = -1;
-    utilization->memory = -1;
-
-    //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
 
-    //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
+    }
   }
 
   return nvml_rc;
 }
 
-nvmlReturn_t hm_NVML_nvmlDeviceGetClockInfo (NVML_PTR *nvml, nvmlDevice_t device, nvmlClockType_t type, unsigned int *clock)
+nvmlReturn_t hm_NVML_nvmlDeviceGetClockInfo (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlClockType_t type, unsigned int *clock)
 {
   if (!nvml) return -1;
 
@@ -208,13 +265,203 @@ nvmlReturn_t hm_NVML_nvmlDeviceGetClockInfo (NVML_PTR *nvml, nvmlDevice_t device
 
   if (nvml_rc != NVML_SUCCESS)
   {
-    *clock = -1;
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetClockInfo()", nvml_rc, string);
+    }
+  }
+
+  return nvml_rc;
+}
+
+nvmlReturn_t hm_NVML_nvmlDeviceGetTemperatureThreshold (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlTemperatureThresholds_t thresholdType, unsigned int *temp)
+{
+  if (!nvml) return -1;
+
+  nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperatureThreshold (device, thresholdType, temp);
+
+  if (nvml_rc != NVML_SUCCESS)
+  {
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperatureThreshold()", nvml_rc, string);
+    }
+  }
+
+  return nvml_rc;
+}
+
+nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkGeneration (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *currLinkGen)
+{
+  if (!nvml) return -1;
+
+  nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkGeneration (device, currLinkGen);
+
+  if (nvml_rc != NVML_SUCCESS)
+  {
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetCurrPcieLinkGeneration()", nvml_rc, string);
+    }
+  }
+
+  return nvml_rc;
+}
+
+nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkWidth (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *currLinkWidth)
+{
+  if (!nvml) return -1;
+
+  nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkWidth (device, currLinkWidth);
+
+  if (nvml_rc != NVML_SUCCESS)
+  {
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetCurrPcieLinkWidth()", nvml_rc, string);
+    }
+  }
+
+  return nvml_rc;
+}
+
+nvmlReturn_t hm_NVML_nvmlDeviceGetCurrentClocksThrottleReasons (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned long long *clocksThrottleReasons)
+{
+  if (!nvml) return -1;
+
+  nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrentClocksThrottleReasons (device, clocksThrottleReasons);
+
+  if (nvml_rc != NVML_SUCCESS)
+  {
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetCurrentClocksThrottleReasons()", nvml_rc, string);
+    }
+  }
+
+  return nvml_rc;
+}
+
+nvmlReturn_t hm_NVML_nvmlDeviceGetSupportedClocksThrottleReasons (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned long long *supportedClocksThrottleReasons)
+{
+  if (!nvml) return -1;
 
-    //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+  nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetSupportedClocksThrottleReasons (device, supportedClocksThrottleReasons);
 
-    //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
+  if (nvml_rc != NVML_SUCCESS)
+  {
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetSupportedClocksThrottleReasons()", nvml_rc, string);
+    }
   }
 
   return nvml_rc;
 }
 
+nvmlReturn_t hm_NVML_nvmlDeviceSetComputeMode (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlComputeMode_t mode)
+{
+  if (!nvml) return -1;
+
+  nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetComputeMode (device, mode);
+
+  if (nvml_rc != NVML_SUCCESS)
+  {
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceSetComputeMode()", nvml_rc, string);
+    }
+  }
+
+  return nvml_rc;
+}
+
+nvmlReturn_t hm_NVML_nvmlDeviceSetGpuOperationMode (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlGpuOperationMode_t mode)
+{
+  if (!nvml) return -1;
+
+  nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetGpuOperationMode (device, mode);
+
+  if (nvml_rc != NVML_SUCCESS)
+  {
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceSetGpuOperationMode()", nvml_rc, string);
+    }
+  }
+
+  return nvml_rc;
+}
+
+nvmlReturn_t hm_NVML_nvmlDeviceGetPowerManagementLimitConstraints (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *minLimit, unsigned int *maxLimit)
+{
+  if (!nvml) return -1;
+
+  nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerManagementLimitConstraints (device, minLimit, maxLimit);
+
+  if (nvml_rc != NVML_SUCCESS)
+  {
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerManagementLimitConstraints()", nvml_rc, string);
+    }
+  }
+
+  return nvml_rc;
+}
+
+nvmlReturn_t hm_NVML_nvmlDeviceSetPowerManagementLimit (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int limit)
+{
+  if (!nvml) return -1;
+
+  nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetPowerManagementLimit (device, limit);
+
+  if (nvml_rc != NVML_SUCCESS)
+  {
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceSetPowerManagementLimit()", nvml_rc, string);
+    }
+  }
+
+  return nvml_rc;
+}
+
+nvmlReturn_t hm_NVML_nvmlDeviceGetPowerManagementLimit (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *limit)
+{
+  if (!nvml) return -1;
+
+  nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerManagementLimit (device, limit);
+
+  if (nvml_rc != NVML_SUCCESS)
+  {
+    if (skip_warnings == 0)
+    {
+      const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
+
+      log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerManagementLimit()", nvml_rc, string);
+    }
+  }
+
+  return nvml_rc;
+}