Add --mangle switch for mangling password before hashing
[hashcat.git] / src / ext_nvml.c
1 /**
2 * Authors.....: Jens Steube <jens.steube@gmail.com>
3 * Gabriele Gristina <matrix@hashcat.net>
4 *
5 * License.....: MIT
6 */
7
8 #include <ext_nvml.h>
9
10 int nvml_init (NVML_PTR *nvml)
11 {
12 if (!nvml) return -1;
13
14 memset (nvml, 0, sizeof (NVML_PTR));
15
16 #ifdef _WIN
17 nvml->lib = hc_dlopen ("nvml.dll");
18
19 if (!nvml->lib)
20 {
21 DWORD BufferSize = 1024;
22
23 DWORD Type = REG_SZ;
24
25 char *Buffer = (char *) mymalloc (BufferSize + 1);
26
27 HKEY hKey = 0;
28
29 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\NVIDIA Corporation\\Global\\NVSMI"), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
30 {
31 if (RegQueryValueEx (hKey, TEXT("NVSMIPATH"), NULL, &Type, (PVOID) Buffer, &BufferSize) == ERROR_SUCCESS)
32 {
33 Buffer[BufferSize] = 0;
34 }
35 else
36 {
37 if (data.quiet == 0)
38 log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled.");
39
40 return -1;
41 }
42
43 RegCloseKey (hKey);
44 }
45 else
46 {
47 if (data.quiet == 0)
48 log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled.");
49
50 return -1;
51 }
52
53 strcat (Buffer, "\\nvml.dll");
54
55 nvml->lib = hc_dlopen (Buffer);
56
57 myfree (Buffer);
58 }
59
60 #elif _POSIX
61 nvml->lib = hc_dlopen ("libnvidia-ml.so", RTLD_NOW);
62 #endif
63
64 if (!nvml->lib)
65 {
66 if (data.quiet == 0)
67 log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled.");
68
69 return -1;
70 }
71
72 HC_LOAD_FUNC(nvml, nvmlErrorString, NVML_ERROR_STRING, NVML, 0)
73 HC_LOAD_FUNC(nvml, nvmlInit, NVML_INIT, NVML, 0)
74 HC_LOAD_FUNC(nvml, nvmlShutdown, NVML_SHUTDOWN, NVML, 0)
75 HC_LOAD_FUNC(nvml, nvmlDeviceGetName, NVML_DEVICE_GET_NAME, NVML, 0)
76 HC_LOAD_FUNC(nvml, nvmlDeviceGetHandleByIndex, NVML_DEVICE_GET_HANDLE_BY_INDEX, NVML, 0)
77 HC_LOAD_FUNC(nvml, nvmlDeviceGetTemperature, NVML_DEVICE_GET_TEMPERATURE, NVML, 0)
78 HC_LOAD_FUNC(nvml, nvmlDeviceGetFanSpeed, NVML_DEVICE_GET_FAN_SPEED, NVML, 0)
79 HC_LOAD_FUNC(nvml, nvmlDeviceGetPowerUsage, NVML_DEVICE_GET_POWER_USAGE, NVML, 0)
80 HC_LOAD_FUNC(nvml, nvmlDeviceGetUtilizationRates, NVML_DEVICE_GET_UTILIZATION_RATES, NVML, 0)
81 HC_LOAD_FUNC(nvml, nvmlDeviceGetClockInfo, NVML_DEVICE_GET_CLOCKINFO, NVML, 0)
82 HC_LOAD_FUNC(nvml, nvmlDeviceGetTemperatureThreshold, NVML_DEVICE_GET_THRESHOLD, NVML, 0)
83 HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrPcieLinkGeneration, NVML_DEVICE_GET_CURRPCIELINKGENERATION, NVML, 0)
84 HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrPcieLinkWidth, NVML_DEVICE_GET_CURRPCIELINKWIDTH, NVML, 0)
85 HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrentClocksThrottleReasons, NVML_DEVICE_GET_CURRENTCLOCKSTHROTTLEREASONS, NVML, 0)
86 HC_LOAD_FUNC(nvml, nvmlDeviceGetSupportedClocksThrottleReasons, NVML_DEVICE_GET_SUPPORTEDCLOCKSTHROTTLEREASONS, NVML, 0)
87 HC_LOAD_FUNC(nvml, nvmlDeviceSetComputeMode, NVML_DEVICE_SET_COMPUTEMODE, NVML, 0)
88 HC_LOAD_FUNC(nvml, nvmlDeviceSetGpuOperationMode, NVML_DEVICE_SET_OPERATIONMODE, NVML, 0)
89 HC_LOAD_FUNC(nvml, nvmlDeviceGetPowerManagementLimitConstraints, NVML_DEVICE_GET_POWERMANAGEMENTLIMITCONSTRAINTS, NVML, 0)
90 HC_LOAD_FUNC(nvml, nvmlDeviceSetPowerManagementLimit, NVML_DEVICE_SET_POWERMANAGEMENTLIMIT, NVML, 0)
91 HC_LOAD_FUNC(nvml, nvmlDeviceGetPowerManagementLimit, NVML_DEVICE_GET_POWERMANAGEMENTLIMIT, NVML, 0)
92
93 return 0;
94 }
95
96 void nvml_close (NVML_PTR *nvml)
97 {
98 if (nvml)
99 {
100 if (nvml->lib)
101 hc_dlclose (nvml->lib);
102
103 myfree (nvml);
104 }
105 }
106
107 const char *hm_NVML_nvmlErrorString (NVML_PTR *nvml, nvmlReturn_t nvml_rc)
108 {
109 if (!nvml) return NULL;
110
111 return nvml->nvmlErrorString (nvml_rc);
112 }
113
114 nvmlReturn_t hm_NVML_nvmlInit (NVML_PTR *nvml)
115 {
116 if (!nvml) return -1;
117
118 nvmlReturn_t nvml_rc = nvml->nvmlInit ();
119
120 if (nvml_rc != NVML_SUCCESS)
121 {
122 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
123
124 log_info ("WARN: %s %d %s\n", "nvmlInit()", nvml_rc, string);
125 }
126
127 return nvml_rc;
128 }
129
130 nvmlReturn_t hm_NVML_nvmlShutdown (NVML_PTR *nvml)
131 {
132 if (!nvml) return -1;
133
134 nvmlReturn_t nvml_rc = nvml->nvmlShutdown ();
135
136 if (nvml_rc != NVML_SUCCESS)
137 {
138 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
139
140 log_info ("WARN: %s %d %s\n", "nvmlShutdown()", nvml_rc, string);
141 }
142
143 return nvml_rc;
144 }
145
146 nvmlReturn_t hm_NVML_nvmlDeviceGetName (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, char *name, unsigned int length)
147 {
148 if (!nvml) return -1;
149
150 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetName (device, name, length);
151
152 if (nvml_rc != NVML_SUCCESS)
153 {
154 if (skip_warnings == 0)
155 {
156 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
157
158 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetName()", nvml_rc, string);
159 }
160 }
161
162 return nvml_rc;
163 }
164
165 nvmlReturn_t hm_NVML_nvmlDeviceGetHandleByIndex (NVML_PTR *nvml, int skip_warnings, unsigned int index, nvmlDevice_t *device)
166 {
167 if (!nvml) return -1;
168
169 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetHandleByIndex (index, device);
170
171 if (nvml_rc != NVML_SUCCESS)
172 {
173 if (skip_warnings == 0)
174 {
175 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
176
177 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetHandleByIndex()", nvml_rc, string);
178 }
179 }
180
181 return nvml_rc;
182 }
183
184 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperature (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp)
185 {
186 if (!nvml) return -1;
187
188 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperature (device, sensorType, temp);
189
190 if (nvml_rc != NVML_SUCCESS)
191 {
192 if (skip_warnings == 0)
193 {
194 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
195
196 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperature()", nvml_rc, string);
197 }
198 }
199
200 return nvml_rc;
201 }
202
203 nvmlReturn_t hm_NVML_nvmlDeviceGetFanSpeed (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *speed)
204 {
205 if (!nvml) return -1;
206
207 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetFanSpeed (device, speed);
208
209 if (nvml_rc != NVML_SUCCESS)
210 {
211 if (skip_warnings == 0)
212 {
213 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
214
215 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetFanSpeed()", nvml_rc, string);
216 }
217 }
218
219 return nvml_rc;
220 }
221
222 nvmlReturn_t hm_NVML_nvmlDeviceGetPowerUsage (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *power)
223 {
224 if (!nvml) return -1;
225
226 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerUsage (device, power);
227
228 if (nvml_rc != NVML_SUCCESS)
229 {
230 if (skip_warnings == 0)
231 {
232 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
233
234 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerUsage()", nvml_rc, string);
235 }
236 }
237
238 return nvml_rc;
239 }
240
241 nvmlReturn_t hm_NVML_nvmlDeviceGetUtilizationRates (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlUtilization_t *utilization)
242 {
243 if (!nvml) return -1;
244
245 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetUtilizationRates (device, utilization);
246
247 if (nvml_rc != NVML_SUCCESS)
248 {
249 if (skip_warnings == 0)
250 {
251 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
252
253 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
254 }
255 }
256
257 return nvml_rc;
258 }
259
260 nvmlReturn_t hm_NVML_nvmlDeviceGetClockInfo (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlClockType_t type, unsigned int *clock)
261 {
262 if (!nvml) return -1;
263
264 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetClockInfo (device, type, clock);
265
266 if (nvml_rc != NVML_SUCCESS)
267 {
268 if (skip_warnings == 0)
269 {
270 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
271
272 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetClockInfo()", nvml_rc, string);
273 }
274 }
275
276 return nvml_rc;
277 }
278
279 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperatureThreshold (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlTemperatureThresholds_t thresholdType, unsigned int *temp)
280 {
281 if (!nvml) return -1;
282
283 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperatureThreshold (device, thresholdType, temp);
284
285 if (nvml_rc != NVML_SUCCESS)
286 {
287 if (skip_warnings == 0)
288 {
289 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
290
291 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperatureThreshold()", nvml_rc, string);
292 }
293 }
294
295 return nvml_rc;
296 }
297
298 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkGeneration (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *currLinkGen)
299 {
300 if (!nvml) return -1;
301
302 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkGeneration (device, currLinkGen);
303
304 if (nvml_rc != NVML_SUCCESS)
305 {
306 if (skip_warnings == 0)
307 {
308 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
309
310 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetCurrPcieLinkGeneration()", nvml_rc, string);
311 }
312 }
313
314 return nvml_rc;
315 }
316
317 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkWidth (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *currLinkWidth)
318 {
319 if (!nvml) return -1;
320
321 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkWidth (device, currLinkWidth);
322
323 if (nvml_rc != NVML_SUCCESS)
324 {
325 if (skip_warnings == 0)
326 {
327 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
328
329 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetCurrPcieLinkWidth()", nvml_rc, string);
330 }
331 }
332
333 return nvml_rc;
334 }
335
336 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrentClocksThrottleReasons (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned long long *clocksThrottleReasons)
337 {
338 if (!nvml) return -1;
339
340 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrentClocksThrottleReasons (device, clocksThrottleReasons);
341
342 if (nvml_rc != NVML_SUCCESS)
343 {
344 if (skip_warnings == 0)
345 {
346 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
347
348 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetCurrentClocksThrottleReasons()", nvml_rc, string);
349 }
350 }
351
352 return nvml_rc;
353 }
354
355 nvmlReturn_t hm_NVML_nvmlDeviceGetSupportedClocksThrottleReasons (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned long long *supportedClocksThrottleReasons)
356 {
357 if (!nvml) return -1;
358
359 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetSupportedClocksThrottleReasons (device, supportedClocksThrottleReasons);
360
361 if (nvml_rc != NVML_SUCCESS)
362 {
363 if (skip_warnings == 0)
364 {
365 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
366
367 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetSupportedClocksThrottleReasons()", nvml_rc, string);
368 }
369 }
370
371 return nvml_rc;
372 }
373
374 nvmlReturn_t hm_NVML_nvmlDeviceSetComputeMode (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlComputeMode_t mode)
375 {
376 if (!nvml) return -1;
377
378 nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetComputeMode (device, mode);
379
380 if (nvml_rc != NVML_SUCCESS)
381 {
382 if (skip_warnings == 0)
383 {
384 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
385
386 log_info ("WARN: %s %d %s\n", "nvmlDeviceSetComputeMode()", nvml_rc, string);
387 }
388 }
389
390 return nvml_rc;
391 }
392
393 nvmlReturn_t hm_NVML_nvmlDeviceSetGpuOperationMode (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlGpuOperationMode_t mode)
394 {
395 if (!nvml) return -1;
396
397 nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetGpuOperationMode (device, mode);
398
399 if (nvml_rc != NVML_SUCCESS)
400 {
401 if (skip_warnings == 0)
402 {
403 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
404
405 log_info ("WARN: %s %d %s\n", "nvmlDeviceSetGpuOperationMode()", nvml_rc, string);
406 }
407 }
408
409 return nvml_rc;
410 }
411
412 nvmlReturn_t hm_NVML_nvmlDeviceGetPowerManagementLimitConstraints (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *minLimit, unsigned int *maxLimit)
413 {
414 if (!nvml) return -1;
415
416 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerManagementLimitConstraints (device, minLimit, maxLimit);
417
418 if (nvml_rc != NVML_SUCCESS)
419 {
420 if (skip_warnings == 0)
421 {
422 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
423
424 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerManagementLimitConstraints()", nvml_rc, string);
425 }
426 }
427
428 return nvml_rc;
429 }
430
431 nvmlReturn_t hm_NVML_nvmlDeviceSetPowerManagementLimit (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int limit)
432 {
433 if (!nvml) return -1;
434
435 nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetPowerManagementLimit (device, limit);
436
437 if (nvml_rc != NVML_SUCCESS)
438 {
439 if (skip_warnings == 0)
440 {
441 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
442
443 log_info ("WARN: %s %d %s\n", "nvmlDeviceSetPowerManagementLimit()", nvml_rc, string);
444 }
445 }
446
447 return nvml_rc;
448 }
449
450 nvmlReturn_t hm_NVML_nvmlDeviceGetPowerManagementLimit (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *limit)
451 {
452 if (!nvml) return -1;
453
454 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerManagementLimit (device, limit);
455
456 if (nvml_rc != NVML_SUCCESS)
457 {
458 if (skip_warnings == 0)
459 {
460 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
461
462 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerManagementLimit()", nvml_rc, string);
463 }
464 }
465
466 return nvml_rc;
467 }