1 /**
2 * Author......: See docs/credits.txt
3 * License.....: MIT
4 */
5
6 #include "common.h"
7 #include "types.h"
8 #include "memory.h"
9 #include "shared.h"
10 #include "event.h"
11 #include "ext_nvml.h"
12
13 #include "dynloader.h"
14
15 #if defined (__CYGWIN__)
16 #include <sys/cygwin.h>
17 #endif
18
nvml_init(void * hashcat_ctx)19 int nvml_init (void *hashcat_ctx)
20 {
21 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
22
23 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
24
25 memset (nvml, 0, sizeof (NVML_PTR));
26
27 #if defined (_WIN)
28
29 nvml->lib = hc_dlopen ("nvml.dll");
30
31 if (!nvml->lib)
32 {
33 DWORD BufferSize = 1024;
34
35 DWORD Type = REG_SZ;
36
37 char *Buffer = (char *) hcmalloc (BufferSize + 1);
38
39 HKEY hKey = 0;
40
41 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, "SOFTWARE\\NVIDIA Corporation\\Global\\NVSMI", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
42 {
43 if (RegQueryValueExA (hKey, "NVSMIPATH", NULL, &Type, (LPBYTE)Buffer, &BufferSize) == ERROR_SUCCESS)
44 {
45 Buffer[BufferSize] = 0;
46 }
47 else
48 {
49 //if (user_options->quiet == false)
50 // event_log_error (hashcat_ctx, "NVML library load failed. Proceeding without NVML HWMon enabled.");
51
52 return -1;
53 }
54
55 RegCloseKey (hKey);
56 }
57 else
58 {
59 //if (user_options->quiet == false)
60 // event_log_error (hashcat_ctx, "NVML library load failed. Proceeding without NVML HWMon enabled.");
61
62 return -1;
63 }
64
65 strcat (Buffer, "\\nvml.dll");
66
67 nvml->lib = hc_dlopen (Buffer);
68
69 hcfree (Buffer);
70 }
71
72 #elif defined (__CYGWIN__)
73
74 nvml->lib = hc_dlopen ("nvml.dll");
75
76 if (!nvml->lib)
77 {
78 HCFILE nvml_lib;
79
80 if (hc_fopen (&nvml_lib, "/proc/registry/HKEY_LOCAL_MACHINE/SOFTWARE/NVIDIA Corporation/Global/NVSMI/NVSMIPATH", "rb") == false)
81 {
82 //if (user_options->quiet == false)
83 // event_log_error (hashcat_ctx, "NVML library load failed: %m. Proceeding without NVML HWMon enabled.");
84
85 return -1;
86 }
87
88 char *nvml_winpath, *nvml_cygpath;
89
90 nvml_winpath = (char *) hcmalloc (100);
91
92 hc_fread (nvml_winpath, 100, 1, &nvml_lib);
93
94 hc_fclose (&nvml_lib);
95
96 ssize_t size = cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_PROC_CYGDRIVE, nvml_winpath, NULL, 0);
97
98 if (size > 0)
99 {
100 nvml_cygpath = (char *) hcmalloc (size + 9);
101
102 cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_PROC_CYGDRIVE, nvml_winpath, nvml_cygpath, size);
103 }
104 else
105 {
106 //if (user_options->quiet == false)
107 // event_log_error (hashcat_ctx, "Could not find NVML on this system. Proceeding without NVML HWMon enabled.");
108
109 return -1;
110 }
111
112 strcat (nvml_cygpath, "/nvml.dll");
113
114 nvml->lib = hc_dlopen (nvml_cygpath);
115 }
116
117 #elif defined (_POSIX)
118
119 nvml->lib = hc_dlopen ("libnvidia-ml.so");
120
121 if (!nvml->lib)
122 {
123 nvml->lib = hc_dlopen ("libnvidia-ml.so.1");
124 }
125
126 #endif
127
128 if (!nvml->lib)
129 {
130 //if (user_options->quiet == false)
131 // event_log_error (hashcat_ctx, "NVML library load failed. Proceeding without NVML HWMon enabled.");
132
133 return -1;
134 }
135
136 HC_LOAD_FUNC(nvml, nvmlErrorString, NVML_ERROR_STRING, NVML, 0);
137 HC_LOAD_FUNC(nvml, nvmlInit, NVML_INIT, NVML, 0);
138 HC_LOAD_FUNC(nvml, nvmlShutdown, NVML_SHUTDOWN, NVML, 0);
139 HC_LOAD_FUNC(nvml, nvmlDeviceGetCount, NVML_DEVICE_GET_COUNT, NVML, 0);
140 HC_LOAD_FUNC(nvml, nvmlDeviceGetName, NVML_DEVICE_GET_NAME, NVML, 0);
141 HC_LOAD_FUNC(nvml, nvmlDeviceGetHandleByIndex, NVML_DEVICE_GET_HANDLE_BY_INDEX, NVML, 0);
142 HC_LOAD_FUNC(nvml, nvmlDeviceGetTemperature, NVML_DEVICE_GET_TEMPERATURE, NVML, 0);
143 HC_LOAD_FUNC(nvml, nvmlDeviceGetFanSpeed, NVML_DEVICE_GET_FAN_SPEED, NVML, 0);
144 HC_LOAD_FUNC(nvml, nvmlDeviceGetUtilizationRates, NVML_DEVICE_GET_UTILIZATION_RATES, NVML, 0);
145 HC_LOAD_FUNC(nvml, nvmlDeviceGetClockInfo, NVML_DEVICE_GET_CLOCKINFO, NVML, 0);
146 HC_LOAD_FUNC(nvml, nvmlDeviceGetTemperatureThreshold, NVML_DEVICE_GET_THRESHOLD, NVML, 0);
147 HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrPcieLinkGeneration, NVML_DEVICE_GET_CURRPCIELINKGENERATION, NVML, 0);
148 HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrPcieLinkWidth, NVML_DEVICE_GET_CURRPCIELINKWIDTH, NVML, 0);
149 HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrentClocksThrottleReasons, NVML_DEVICE_GET_CURRENTCLOCKSTHROTTLEREASONS, NVML, 0);
150 HC_LOAD_FUNC(nvml, nvmlDeviceGetSupportedClocksThrottleReasons, NVML_DEVICE_GET_SUPPORTEDCLOCKSTHROTTLEREASONS, NVML, 0);
151 HC_LOAD_FUNC(nvml, nvmlDeviceGetPciInfo, NVML_DEVICE_GET_PCIINFO, NVML, 0);
152
153 return 0;
154 }
155
nvml_close(void * hashcat_ctx)156 void nvml_close (void *hashcat_ctx)
157 {
158 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
159
160 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
161
162 if (nvml)
163 {
164 if (nvml->lib)
165 hc_dlclose (nvml->lib);
166
167 hcfree (nvml);
168 }
169 }
170
hm_NVML_nvmlErrorString(NVML_PTR * nvml,const nvmlReturn_t nvml_rc)171 const char *hm_NVML_nvmlErrorString (NVML_PTR *nvml, const nvmlReturn_t nvml_rc)
172 {
173 return nvml->nvmlErrorString (nvml_rc);
174 }
175
hm_NVML_nvmlInit(void * hashcat_ctx)176 int hm_NVML_nvmlInit (void *hashcat_ctx)
177 {
178 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
179
180 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
181
182 const nvmlReturn_t nvml_rc = (nvmlReturn_t) nvml->nvmlInit ();
183
184 if (nvml_rc != NVML_SUCCESS && nvml_rc != NVML_ERROR_DRIVER_NOT_LOADED)
185 {
186 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
187
188 event_log_error (hashcat_ctx, "nvmlInit(): %s", string);
189
190 return -1;
191 }
192
193 return 0;
194 }
195
hm_NVML_nvmlShutdown(void * hashcat_ctx)196 int hm_NVML_nvmlShutdown (void *hashcat_ctx)
197 {
198 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
199
200 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
201
202 const nvmlReturn_t nvml_rc = (nvmlReturn_t) nvml->nvmlShutdown ();
203
204 if (nvml_rc != NVML_SUCCESS && nvml_rc != NVML_ERROR_DRIVER_NOT_LOADED)
205 {
206 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
207
208 event_log_error (hashcat_ctx, "nvmlShutdown(): %s", string);
209
210 return -1;
211 }
212
213 return 0;
214 }
215
hm_NVML_nvmlDeviceGetCount(void * hashcat_ctx,unsigned int * deviceCount)216 int hm_NVML_nvmlDeviceGetCount (void *hashcat_ctx, unsigned int *deviceCount)
217 {
218 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
219
220 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
221
222 const nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCount (deviceCount);
223
224 if (nvml_rc != NVML_SUCCESS && nvml_rc != NVML_ERROR_DRIVER_NOT_LOADED)
225 {
226 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
227
228 event_log_error (hashcat_ctx, "nvmlDeviceGetCount(): %s", string);
229
230 return -1;
231 }
232
233 return 0;
234 }
235
hm_NVML_nvmlDeviceGetHandleByIndex(void * hashcat_ctx,unsigned int device_index,nvmlDevice_t * device)236 int hm_NVML_nvmlDeviceGetHandleByIndex (void *hashcat_ctx, unsigned int device_index, nvmlDevice_t *device)
237 {
238 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
239
240 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
241
242 const nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetHandleByIndex (device_index, device);
243
244 if (nvml_rc != NVML_SUCCESS)
245 {
246 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
247
248 event_log_error (hashcat_ctx, "nvmlDeviceGetHandleByIndex(): %s", string);
249
250 return -1;
251 }
252
253 return 0;
254 }
255
hm_NVML_nvmlDeviceGetTemperature(void * hashcat_ctx,nvmlDevice_t device,nvmlTemperatureSensors_t sensorType,unsigned int * temp)256 int hm_NVML_nvmlDeviceGetTemperature (void *hashcat_ctx, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp)
257 {
258 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
259
260 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
261
262 const nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperature (device, sensorType, temp);
263
264 if (nvml_rc != NVML_SUCCESS)
265 {
266 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
267
268 event_log_error (hashcat_ctx, "nvmlDeviceGetTemperature(): %s", string);
269
270 return -1;
271 }
272
273 return 0;
274 }
275
hm_NVML_nvmlDeviceGetFanSpeed(void * hashcat_ctx,nvmlDevice_t device,unsigned int * speed)276 int hm_NVML_nvmlDeviceGetFanSpeed (void *hashcat_ctx, nvmlDevice_t device, unsigned int *speed)
277 {
278 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
279
280 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
281
282 const nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetFanSpeed (device, speed);
283
284 if (nvml_rc != NVML_SUCCESS)
285 {
286 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
287
288 event_log_error (hashcat_ctx, "nvmlDeviceGetFanSpeed(): %s", string);
289
290 return -1;
291 }
292
293 return 0;
294 }
295
hm_NVML_nvmlDeviceGetUtilizationRates(void * hashcat_ctx,nvmlDevice_t device,nvmlUtilization_t * utilization)296 int hm_NVML_nvmlDeviceGetUtilizationRates (void *hashcat_ctx, nvmlDevice_t device, nvmlUtilization_t *utilization)
297 {
298 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
299
300 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
301
302 const nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetUtilizationRates (device, utilization);
303
304 if (nvml_rc != NVML_SUCCESS)
305 {
306 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
307
308 event_log_error (hashcat_ctx, "nvmlDeviceGetUtilizationRates(): %s", string);
309
310 return -1;
311 }
312
313 return 0;
314 }
315
hm_NVML_nvmlDeviceGetClockInfo(void * hashcat_ctx,nvmlDevice_t device,nvmlClockType_t type,unsigned int * clockfreq)316 int hm_NVML_nvmlDeviceGetClockInfo (void *hashcat_ctx, nvmlDevice_t device, nvmlClockType_t type, unsigned int *clockfreq)
317 {
318 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
319
320 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
321
322 const nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetClockInfo (device, type, clockfreq);
323
324 if (nvml_rc != NVML_SUCCESS)
325 {
326 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
327
328 event_log_error (hashcat_ctx, "nvmlDeviceGetClockInfo(): %s", string);
329
330 return -1;
331 }
332
333 return 0;
334 }
335
hm_NVML_nvmlDeviceGetTemperatureThreshold(void * hashcat_ctx,nvmlDevice_t device,nvmlTemperatureThresholds_t thresholdType,unsigned int * temp)336 int hm_NVML_nvmlDeviceGetTemperatureThreshold (void *hashcat_ctx, nvmlDevice_t device, nvmlTemperatureThresholds_t thresholdType, unsigned int *temp)
337 {
338 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
339
340 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
341
342 const nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperatureThreshold (device, thresholdType, temp);
343
344 if (nvml_rc != NVML_SUCCESS)
345 {
346 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
347
348 event_log_error (hashcat_ctx, "nvmlDeviceGetTemperatureThreshold(): %s", string);
349
350 return -1;
351 }
352
353 return 0;
354 }
355
hm_NVML_nvmlDeviceGetCurrPcieLinkWidth(void * hashcat_ctx,nvmlDevice_t device,unsigned int * currLinkWidth)356 int hm_NVML_nvmlDeviceGetCurrPcieLinkWidth (void *hashcat_ctx, nvmlDevice_t device, unsigned int *currLinkWidth)
357 {
358 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
359
360 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
361
362 const nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkWidth (device, currLinkWidth);
363
364 if (nvml_rc != NVML_SUCCESS)
365 {
366 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
367
368 event_log_error (hashcat_ctx, "nvmlDeviceGetCurrPcieLinkWidth(): %s", string);
369
370 return -1;
371 }
372
373 return 0;
374 }
375
hm_NVML_nvmlDeviceGetPciInfo(void * hashcat_ctx,nvmlDevice_t device,nvmlPciInfo_t * pci)376 int hm_NVML_nvmlDeviceGetPciInfo (void *hashcat_ctx, nvmlDevice_t device, nvmlPciInfo_t *pci)
377 {
378 hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
379
380 NVML_PTR *nvml = (NVML_PTR *) hwmon_ctx->hm_nvml;
381
382 const nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPciInfo (device, pci);
383
384 if (nvml_rc != NVML_SUCCESS)
385 {
386 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
387
388 event_log_error (hashcat_ctx, "nvmlDeviceGetPciInfo(): %s", string);
389
390 return -1;
391 }
392
393 return 0;
394 }
395