Disable some of NVMLs useless throttle reasons
[hashcat.git] / src / shared.c
index ac99216..8cfa46e 100644 (file)
@@ -83,7 +83,7 @@ u64 byte_swap_64 (const u64 n)
 
 int last_len = 0;
 
-void log_final (FILE *fp, const char *fmt, va_list ap)
+int log_final (FILE *fp, const char *fmt, va_list ap)
 {
   if (last_len)
   {
@@ -110,84 +110,96 @@ void log_final (FILE *fp, const char *fmt, va_list ap)
   fflush (fp);
 
   last_len = len;
+
+  return len;
 }
 
-void log_out_nn (FILE *fp, const char *fmt, ...)
+int log_out_nn (FILE *fp, const char *fmt, ...)
 {
-  if (SUPPRESS_OUTPUT) return;
+  if (SUPPRESS_OUTPUT) return 0;
 
   va_list ap;
 
   va_start (ap, fmt);
 
-  log_final (fp, fmt, ap);
+  const int len = log_final (fp, fmt, ap);
 
   va_end (ap);
+
+  return len;
 }
 
-void log_info_nn (const char *fmt, ...)
+int log_info_nn (const char *fmt, ...)
 {
-  if (SUPPRESS_OUTPUT) return;
+  if (SUPPRESS_OUTPUT) return 0;
 
   va_list ap;
 
   va_start (ap, fmt);
 
-  log_final (stdout, fmt, ap);
+  const int len = log_final (stdout, fmt, ap);
 
   va_end (ap);
+
+  return len;
 }
 
-void log_error_nn (const char *fmt, ...)
+int log_error_nn (const char *fmt, ...)
 {
-  if (SUPPRESS_OUTPUT) return;
+  if (SUPPRESS_OUTPUT) return 0;
 
   va_list ap;
 
   va_start (ap, fmt);
 
-  log_final (stderr, fmt, ap);
+  const int len = log_final (stderr, fmt, ap);
 
   va_end (ap);
+
+  return len;
 }
 
-void log_out (FILE *fp, const char *fmt, ...)
+int log_out (FILE *fp, const char *fmt, ...)
 {
-  if (SUPPRESS_OUTPUT) return;
+  if (SUPPRESS_OUTPUT) return 0;
 
   va_list ap;
 
   va_start (ap, fmt);
 
-  log_final (fp, fmt, ap);
+  const int len = log_final (fp, fmt, ap);
 
   va_end (ap);
 
   fputc ('\n', fp);
 
   last_len = 0;
+
+  return len;
 }
 
-void log_info (const char *fmt, ...)
+int log_info (const char *fmt, ...)
 {
-  if (SUPPRESS_OUTPUT) return;
+  if (SUPPRESS_OUTPUT) return 0;
 
   va_list ap;
 
   va_start (ap, fmt);
 
-  log_final (stdout, fmt, ap);
+  const int len = log_final (stdout, fmt, ap);
 
   va_end (ap);
 
   fputc ('\n', stdout);
 
   last_len = 0;
+
+  return len;
 }
 
-void log_error (const char *fmt, ...)
+int log_error (const char *fmt, ...)
 {
-  if (SUPPRESS_OUTPUT) return;
+  if (SUPPRESS_OUTPUT) return 0;
 
   fputc ('\n', stderr);
   fputc ('\n', stderr);
@@ -196,7 +208,7 @@ void log_error (const char *fmt, ...)
 
   va_start (ap, fmt);
 
-  log_final (stderr, fmt, ap);
+  const int len = log_final (stderr, fmt, ap);
 
   va_end (ap);
 
@@ -204,6 +216,8 @@ void log_error (const char *fmt, ...)
   fputc ('\n', stderr);
 
   last_len = 0;
+
+  return len;
 }
 
 /**
@@ -2639,7 +2653,7 @@ void lock_file (FILE *fp)
   {
     if (errno != EINTR)
     {
-      log_error ("ERROR: failed acquiring write lock: %s", strerror (errno));
+      log_error ("ERROR: Failed acquiring write lock: %s", strerror (errno));
 
       exit (-1);
     }
@@ -2657,7 +2671,7 @@ void unlock_file (FILE *fp)
 }
 #endif // F_SETLKW
 
-#ifdef _WIN
+#ifdef WIN
 void fsync (int fd)
 {
   HANDLE h = (HANDLE) _get_osfhandle (fd);
@@ -3194,13 +3208,7 @@ int hm_get_fanpolicy_with_device_id (const uint device_id)
 
     if (data.devices_param[device_id].device_vendor_id == VENDOR_ID_NV)
     {
-      #if defined(LINUX)
-      return 0;
-      #endif
-
-      #if defined(WIN)
       return 1;
-      #endif
     }
   }
 
@@ -3394,11 +3402,17 @@ int hm_get_throttle_with_device_id (const uint device_id)
     if (hm_NVML_nvmlDeviceGetCurrentClocksThrottleReasons   (data.hm_nvml, 1, data.hm_device[device_id].nvml, &clocksThrottleReasons)    != NVML_SUCCESS) return -1;
     if (hm_NVML_nvmlDeviceGetSupportedClocksThrottleReasons (data.hm_nvml, 1, data.hm_device[device_id].nvml, &supportedThrottleReasons) != NVML_SUCCESS) return -1;
 
-    clocksThrottleReasons &= supportedThrottleReasons;
-
+    clocksThrottleReasons &=  supportedThrottleReasons;
+    clocksThrottleReasons &= ~nvmlClocksThrottleReasonGpuIdle;
+    clocksThrottleReasons &= ~nvmlClocksThrottleReasonApplicationsClocksSetting;
     clocksThrottleReasons &= ~nvmlClocksThrottleReasonUnknown;
 
-    return (clocksThrottleReasons > 0);
+    if (data.kernel_power_final)
+    {
+      clocksThrottleReasons &= ~nvmlClocksThrottleReasonHwSlowdown;
+    }
+
+    return (clocksThrottleReasons != nvmlClocksThrottleReasonNone);
   }
 
   return -1;
@@ -3444,6 +3458,43 @@ int hm_set_fanspeed_with_device_id_adl (const uint device_id, const int fanspeed
   return -1;
 }
 
+int hm_set_fanspeed_with_device_id_nvapi (const uint device_id, const int fanspeed, const int fanpolicy)
+{
+  if (data.hm_device[device_id].fan_set_supported == 1)
+  {
+    if (data.hm_nvapi)
+    {
+      NV_GPU_COOLER_LEVELS CoolerLevels = { 0 };
+
+      CoolerLevels.Version = GPU_COOLER_LEVELS_VER | sizeof (NV_GPU_COOLER_LEVELS);
+
+      CoolerLevels.Levels[0].Level  = fanspeed;
+      CoolerLevels.Levels[0].Policy = fanpolicy;
+
+      if (hm_NvAPI_GPU_SetCoolerLevels (data.hm_nvapi, data.hm_device[device_id].nvapi, 0, &CoolerLevels) != NVAPI_OK) return -1;
+
+      return 0;
+    }
+  }
+
+  return -1;
+}
+
+int hm_set_fanspeed_with_device_id_xnvctrl (const uint device_id, const int fanspeed)
+{
+  if (data.hm_device[device_id].fan_set_supported == 1)
+  {
+    if (data.hm_xnvctrl)
+    {
+      if (set_fan_speed_target (data.hm_xnvctrl, data.hm_device[device_id].xnvctrl, fanspeed) != 0) return -1;
+
+      return 0;
+    }
+  }
+
+  return -1;
+}
+
 #endif // HAVE_HWMON
 
 /**
@@ -3456,7 +3507,7 @@ void mp_css_to_uniq_tbl (uint css_cnt, cs_t *css, uint uniq_tbls[SP_PW_MAX][CHAR
 
   if (css_cnt > SP_PW_MAX)
   {
-    log_error ("ERROR: mask length is too long");
+    log_error ("ERROR: Mask length is too long");
 
     exit (-1);
   }
@@ -3568,7 +3619,7 @@ void mp_expand (char *in_buf, size_t in_len, cs_t *mp_sys, cs_t *mp_usr, int mp_
 
         if (in_pos == in_len)
         {
-          log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", in_buf);
+          log_error ("ERROR: The hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", in_buf);
 
           exit (-1);
         }
@@ -3577,7 +3628,7 @@ void mp_expand (char *in_buf, size_t in_len, cs_t *mp_sys, cs_t *mp_usr, int mp_
 
         if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
         {
-          log_error ("ERROR: invalid hex character detected in mask %s", in_buf);
+          log_error ("ERROR: Invalid hex character detected in mask %s", in_buf);
 
           exit (-1);
         }
@@ -3660,7 +3711,7 @@ cs_t *mp_gen_css (char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, u
                   break;
         case '?': mp_add_cs_buf (&chr, 1, css, css_pos);
                   break;
-        default:  log_error ("ERROR: syntax error: %s", mask_buf);
+        default:  log_error ("ERROR: Syntax error: %s", mask_buf);
                   exit (-1);
       }
     }
@@ -3674,7 +3725,7 @@ cs_t *mp_gen_css (char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, u
 
         if (mask_pos == mask_len)
         {
-          log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
+          log_error ("ERROR: The hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
 
           exit (-1);
         }
@@ -3685,7 +3736,7 @@ cs_t *mp_gen_css (char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, u
 
         if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
         {
-          log_error ("ERROR: invalid hex character detected in mask %s", mask_buf);
+          log_error ("ERROR: Invalid hex character detected in mask %s", mask_buf);
 
           exit (-1);
         }
@@ -3708,7 +3759,7 @@ cs_t *mp_gen_css (char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, u
 
   if (css_pos == 0)
   {
-    log_error ("ERROR: invalid mask length (0)");
+    log_error ("ERROR: Invalid mask length (0)");
 
     exit (-1);
   }
@@ -3793,7 +3844,7 @@ void mp_setup_usr (cs_t *mp_sys, cs_t *mp_usr, char *buf, uint index)
 
     if (len == 0)
     {
-      log_info ("WARNING: charset file corrupted");
+      log_info ("WARNING: Charset file corrupted");
 
       mp_expand (buf, strlen (buf), mp_sys, mp_usr, index, 1);
     }
@@ -3843,7 +3894,7 @@ char *mp_get_truncated_mask (char *mask_buf, size_t mask_len, uint len)
 
         if (mask_pos == mask_len)
         {
-          log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
+          log_error ("ERROR: The hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
 
           exit (-1);
         }
@@ -3854,7 +3905,7 @@ char *mp_get_truncated_mask (char *mask_buf, size_t mask_len, uint len)
 
         if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
         {
-          log_error ("ERROR: invalid hex character detected in mask: %s", mask_buf);
+          log_error ("ERROR: Invalid hex character detected in mask: %s", mask_buf);
 
           exit (-1);
         }
@@ -4443,7 +4494,7 @@ int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_se
 
 void set_cpu_affinity (char *cpu_affinity)
 {
-  #ifdef WIN
+  #ifdef _WIN
   DWORD_PTR aff_mask = 0;
   #elif _POSIX
   cpu_set_t cpuset;
@@ -4462,7 +4513,7 @@ void set_cpu_affinity (char *cpu_affinity)
 
       if (cpu_id == 0)
       {
-        #ifdef WIN
+        #ifdef _WIN
         aff_mask = 0;
         #elif _POSIX
         CPU_ZERO (&cpuset);
@@ -4473,12 +4524,12 @@ void set_cpu_affinity (char *cpu_affinity)
 
       if (cpu_id > 32)
       {
-        log_error ("ERROR: invalid cpu_id %u specified", cpu_id);
+        log_error ("ERROR: Invalid cpu_id %u specified", cpu_id);
 
         exit (-1);
       }
 
-      #ifdef WIN
+      #ifdef _WIN
       aff_mask |= 1 << (cpu_id - 1);
       #elif _POSIX
       CPU_SET ((cpu_id - 1), &cpuset);
@@ -4489,7 +4540,7 @@ void set_cpu_affinity (char *cpu_affinity)
     free (devices);
   }
 
-  #ifdef WIN
+  #ifdef _WIN
   SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
   SetThreadAffinityMask (GetCurrentThread (), aff_mask);
   #elif _POSIX
@@ -4712,7 +4763,7 @@ int sort_by_dictstat (const void *s1, const void *s2)
   dictstat_t *d1 = (dictstat_t *) s1;
   dictstat_t *d2 = (dictstat_t *) s2;
 
-  #ifdef LINUX
+  #ifdef _LINUX
   d2->stat.st_atim = d1->stat.st_atim;
   #else
   d2->stat.st_atime = d1->stat.st_atime;
@@ -5394,7 +5445,7 @@ uint setup_opencl_platforms_filter (char *opencl_platforms)
 
       if (platform < 1 || platform > 32)
       {
-        log_error ("ERROR: invalid OpenCL platform %u specified", platform);
+        log_error ("ERROR: Invalid OpenCL platform %u specified", platform);
 
         exit (-1);
       }
@@ -5429,7 +5480,7 @@ u32 setup_devices_filter (char *opencl_devices)
 
       if (device_id < 1 || device_id > 32)
       {
-        log_error ("ERROR: invalid device_id %u specified", device_id);
+        log_error ("ERROR: Invalid device_id %u specified", device_id);
 
         exit (-1);
       }
@@ -5464,7 +5515,7 @@ cl_device_type setup_device_types_filter (char *opencl_device_types)
 
       if (device_type < 1 || device_type > 3)
       {
-        log_error ("ERROR: invalid device_type %u specified", device_type);
+        log_error ("ERROR: Invalid device_type %u specified", device_type);
 
         exit (-1);
       }
@@ -5858,7 +5909,6 @@ char *strhashtype (const uint hash_mode)
     case   141: return ((char *) HT_00141); break;
     case   150: return ((char *) HT_00150); break;
     case   160: return ((char *) HT_00160); break;
-    case   190: return ((char *) HT_00190); break;
     case   200: return ((char *) HT_00200); break;
     case   300: return ((char *) HT_00300); break;
     case   400: return ((char *) HT_00400); break;
@@ -9131,7 +9181,7 @@ void stop_at_checkpoint ()
 
   if (data.restore_disable == 1)
   {
-    log_info ("WARNING: this feature is disabled when --restore-disable was specified");
+    log_info ("WARNING: This feature is disabled when --restore-disable is specified");
 
     return;
   }
@@ -9146,7 +9196,7 @@ void stop_at_checkpoint ()
 
     data.checkpoint_cur_words = get_lowest_words_done ();
 
-    log_info ("Checkpoint enabled: will quit at next Restore Point update");
+    log_info ("Checkpoint enabled: Will quit at next Restore Point update");
   }
   else
   {
@@ -9162,20 +9212,62 @@ void stop_at_checkpoint ()
 
 void myabort ()
 {
-  if (data.devices_status == STATUS_INIT)     return;
-  if (data.devices_status == STATUS_STARTING) return;
+  //if (data.devices_status == STATUS_INIT)     return;
+  //if (data.devices_status == STATUS_STARTING) return;
 
   data.devices_status = STATUS_ABORTED;
 }
 
 void myquit ()
 {
-  if (data.devices_status == STATUS_INIT)     return;
-  if (data.devices_status == STATUS_STARTING) return;
+  //if (data.devices_status == STATUS_INIT)     return;
+  //if (data.devices_status == STATUS_STARTING) return;
 
   data.devices_status = STATUS_QUIT;
 }
 
+void naive_replace (char *s, const u8 key_char, const u8 replace_char)
+{
+  const size_t len = strlen (s);
+
+  for (size_t in = 0; in < len; in++)
+  {
+    const u8 c = s[in];
+
+    if (c == key_char)
+    {
+      s[in] = replace_char;
+    }
+  }
+}
+
+void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_char)
+{
+  char s_escaped[1024] = { 0 };
+
+  size_t s_escaped_max = sizeof (s_escaped);
+
+  const size_t len = strlen (s);
+
+  for (size_t in = 0, out = 0; in < len; in++, out++)
+  {
+    const u8 c = s[in];
+
+    if (c == key_char)
+    {
+      s_escaped[out] = escape_char;
+
+      out++;
+    }
+
+    if (out == s_escaped_max - 2) break;
+
+    s_escaped[out] = c;
+  }
+
+  strncpy (s, s_escaped, s_max - 1);
+}
+
 void load_kernel (const char *kernel_file, int num_devices, size_t *kernel_lengths, const u8 **kernel_sources)
 {
   FILE *fp = fopen (kernel_file, "rb");
@@ -9252,7 +9344,7 @@ restore_data_t *init_restore (int argc, char **argv)
 
       if (nread != 1)
       {
-        log_error ("ERROR: cannot read %s", data.eff_restore_file);
+        log_error ("ERROR: Cannot read %s", data.eff_restore_file);
 
         exit (-1);
       }
@@ -9288,7 +9380,7 @@ restore_data_t *init_restore (int argc, char **argv)
 
           if (strcmp (argv0_r, pidbin_r) == 0)
           {
-            log_error ("ERROR: already an instance %s running on pid %d", pidbin, rd->pid);
+            log_error ("ERROR: Already an instance %s running on pid %d", pidbin, rd->pid);
 
             exit (-1);
           }
@@ -9311,7 +9403,7 @@ restore_data_t *init_restore (int argc, char **argv)
         {
           if (strcmp (pidbin, pidbin2) == 0)
           {
-            log_error ("ERROR: already an instance %s running on pid %d", pidbin2, rd->pid);
+            log_error ("ERROR: Already an instance %s running on pid %d", pidbin2, rd->pid);
 
             exit (-1);
           }
@@ -9326,7 +9418,7 @@ restore_data_t *init_restore (int argc, char **argv)
 
       if (rd->version_bin < RESTORE_MIN)
       {
-        log_error ("ERROR: cannot use outdated %s. Please remove it.", data.eff_restore_file);
+        log_error ("ERROR: Cannot use outdated %s. Please remove it.", data.eff_restore_file);
 
         exit (-1);
       }
@@ -9362,14 +9454,14 @@ void read_restore (const char *eff_restore_file, restore_data_t *rd)
 
   if (fp == NULL)
   {
-    log_error ("ERROR: restore file '%s': %s", eff_restore_file, strerror (errno));
+    log_error ("ERROR: Restore file '%s': %s", eff_restore_file, strerror (errno));
 
     exit (-1);
   }
 
   if (fread (rd, sizeof (restore_data_t), 1, fp) != 1)
   {
-    log_error ("ERROR: cannot read %s", eff_restore_file);
+    log_error ("ERROR: Can't read %s", eff_restore_file);
 
     exit (-1);
   }
@@ -9382,7 +9474,7 @@ void read_restore (const char *eff_restore_file, restore_data_t *rd)
   {
     if (fgets (buf, HCBUFSIZ - 1, fp) == NULL)
     {
-      log_error ("ERROR: cannot read %s", eff_restore_file);
+      log_error ("ERROR: Can't read %s", eff_restore_file);
 
       exit (-1);
     }
@@ -9491,13 +9583,13 @@ void cycle_restore ()
   {
     if (unlink (eff_restore_file))
     {
-      log_info ("WARN: unlink file '%s': %s", eff_restore_file, strerror (errno));
+      log_info ("WARN: Unlink file '%s': %s", eff_restore_file, strerror (errno));
     }
   }
 
   if (rename (new_restore_file, eff_restore_file))
   {
-    log_info ("WARN: rename file '%s' to '%s': %s", new_restore_file, eff_restore_file, strerror (errno));
+    log_info ("WARN: Rename file '%s' to '%s': %s", new_restore_file, eff_restore_file, strerror (errno));
   }
 }
 
@@ -10460,7 +10552,7 @@ int wpa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   if (salt_len > 36)
   {
-    log_info ("WARNING: the length of the ESSID is too long. The hccap file may be invalid or corrupted");
+    log_info ("WARNING: The ESSID length is too long, the hccap file may be invalid or corrupted");
 
     return (PARSER_SALT_LENGTH);
   }
@@ -11960,21 +12052,6 @@ int sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
   return (PARSER_OK);
 }
 
-int sha1linkedin_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
-{
-  if ((input_len < DISPLAY_LEN_MIN_100) || (input_len > DISPLAY_LEN_MAX_100)) return (PARSER_GLOBAL_LENGTH);
-
-  u32 *digest = (u32 *) hash_buf->digest;
-
-  digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
-  digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
-  digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
-  digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
-  digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
-
-  return (PARSER_OK);
-}
-
 int sha1axcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 {
   if ((input_len < DISPLAY_LEN_MIN_13300) || (input_len > DISPLAY_LEN_MAX_13300)) return (PARSER_GLOBAL_LENGTH);
@@ -18561,7 +18638,7 @@ int sip_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   if (memcmp (directive_pos, "MD5", 3))
   {
-    log_info ("ERROR: only the MD5 directive is currently supported\n");
+    log_info ("ERROR: Only the MD5 directive is currently supported\n");
 
     myfree (temp_input_buf);
 
@@ -20698,8 +20775,6 @@ void status_display ();
 
 void *thread_keypress (void *p)
 {
-  int benchmark = *((int *) p);
-
   uint quiet = data.quiet;
 
   tty_break();
@@ -20781,8 +20856,6 @@ void *thread_keypress (void *p)
 
         log_info ("");
 
-        if (benchmark == 1) break;
-
         stop_at_checkpoint ();
 
         log_info ("");
@@ -20796,14 +20869,7 @@ void *thread_keypress (void *p)
 
         log_info ("");
 
-        if (benchmark == 1)
-        {
-          myquit ();
-        }
-        else
-        {
-          myabort ();
-        }
+        myabort ();
 
         break;
     }