/
crypto_engine_loader.c
547 lines (501 loc) · 16.1 KB
/
crypto_engine_loader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* Copyright (C) 2018 Joseph Benden <joe@benden.us>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#ifndef STATIC_BUILD
#include <dlfcn.h>
#endif
#include "aircrack-ng/ce-wpa/crypto_engine.h"
#include "aircrack-ng/support/crypto_engine_loader.h"
#include "aircrack-ng/support/common.h"
#include "aircrack-ng/cpu/trampoline.h"
#ifndef STATIC_BUILD
static void * module = NULL;
#endif
#ifdef STATIC_BUILD
int (*dso_ac_crypto_engine_init)(ac_crypto_engine_t * engine)
= &ac_crypto_engine_init;
void (*dso_ac_crypto_engine_destroy)(ac_crypto_engine_t * engine)
= &ac_crypto_engine_destroy;
void (*dso_ac_crypto_engine_set_essid)(ac_crypto_engine_t * engine,
const uint8_t * essid)
= &ac_crypto_engine_set_essid;
int (*dso_ac_crypto_engine_thread_init)(ac_crypto_engine_t * engine,
int threadid)
= &ac_crypto_engine_thread_init;
void (*dso_ac_crypto_engine_thread_destroy)(ac_crypto_engine_t * engine,
int threadid)
= &ac_crypto_engine_thread_destroy;
int (*dso_ac_crypto_engine_simd_width)(void) = &ac_crypto_engine_simd_width;
int (*dso_ac_crypto_engine_wpa_crack)(
ac_crypto_engine_t * engine,
const wpapsk_password key[MAX_KEYS_PER_CRYPT_SUPPORTED],
const uint8_t eapol[256],
uint32_t eapol_size,
uint8_t mic[MAX_KEYS_PER_CRYPT_SUPPORTED][20],
uint8_t keyver,
const uint8_t cmpmic[20],
int nparallel,
int threadid)
= &ac_crypto_engine_wpa_crack;
void (*dso_ac_crypto_engine_calc_pke)(ac_crypto_engine_t * engine,
const uint8_t bssid[6],
const uint8_t stmac[6],
const uint8_t anonce[32],
const uint8_t snonce[32],
int threadid)
= &ac_crypto_engine_calc_pke;
int (*dso_ac_crypto_engine_wpa_pmkid_crack)(
ac_crypto_engine_t * engine,
const wpapsk_password key[MAX_KEYS_PER_CRYPT_SUPPORTED],
const uint8_t pmkid[32],
int nparallel,
int threadid)
= &ac_crypto_engine_wpa_pmkid_crack;
void (*dso_ac_crypto_engine_set_pmkid_salt)(ac_crypto_engine_t * engine,
const uint8_t bssid[6],
const uint8_t stmac[6],
int threadid)
= &ac_crypto_engine_set_pmkid_salt;
int (*dso_ac_crypto_engine_supported_features)(void)
= &ac_crypto_engine_supported_features;
uint8_t * (*dso_ac_crypto_engine_get_pmk)(ac_crypto_engine_t * engine,
int threadid,
int index)
= &ac_crypto_engine_get_pmk;
uint8_t * (*dso_ac_crypto_engine_get_ptk)(ac_crypto_engine_t * engine,
int threadid,
int index)
= &ac_crypto_engine_get_ptk;
void (*dso_ac_crypto_engine_calc_one_pmk)(const uint8_t * key,
const uint8_t * essid,
uint32_t essid_length,
uint8_t pmk[40])
= &ac_crypto_engine_calc_one_pmk;
void (*dso_ac_crypto_engine_calc_pmk)(
ac_crypto_engine_t * engine,
const wpapsk_password key[MAX_KEYS_PER_CRYPT_SUPPORTED],
int nparallel,
int threadid)
= &ac_crypto_engine_calc_pmk;
void (*dso_ac_crypto_engine_calc_mic)(ac_crypto_engine_t * engine,
const uint8_t eapol[256],
const uint32_t eapol_size,
uint8_t mic[MAX_KEYS_PER_CRYPT_SUPPORTED]
[20],
const uint8_t keyver,
const int vectorIdx,
const int threadid)
= &ac_crypto_engine_calc_mic;
#else
int (*dso_ac_crypto_engine_init)(ac_crypto_engine_t * engine) = NULL;
void (*dso_ac_crypto_engine_destroy)(ac_crypto_engine_t * engine) = NULL;
void (*dso_ac_crypto_engine_set_essid)(ac_crypto_engine_t * engine,
const uint8_t * essid)
= NULL;
int (*dso_ac_crypto_engine_thread_init)(ac_crypto_engine_t * engine,
int threadid)
= NULL;
void (*dso_ac_crypto_engine_thread_destroy)(ac_crypto_engine_t * engine,
int threadid)
= NULL;
int (*dso_ac_crypto_engine_simd_width)(void) = NULL;
int (*dso_ac_crypto_engine_wpa_crack)(
ac_crypto_engine_t * engine,
const wpapsk_password key[MAX_KEYS_PER_CRYPT_SUPPORTED],
const uint8_t eapol[256],
uint32_t eapol_size,
uint8_t mic[MAX_KEYS_PER_CRYPT_SUPPORTED][20],
uint8_t keyver,
const uint8_t cmpmic[20],
int nparallel,
int threadid)
= NULL;
void (*dso_ac_crypto_engine_calc_pke)(ac_crypto_engine_t * engine,
const uint8_t bssid[6],
const uint8_t stmac[6],
const uint8_t anonce[32],
const uint8_t snonce[32],
int threadid)
= NULL;
int (*dso_ac_crypto_engine_wpa_pmkid_crack)(
ac_crypto_engine_t * engine,
const wpapsk_password key[MAX_KEYS_PER_CRYPT_SUPPORTED],
const uint8_t pmkid[32],
int nparallel,
int threadid)
= NULL;
void (*dso_ac_crypto_engine_set_pmkid_salt)(ac_crypto_engine_t * engine,
const uint8_t bssid[6],
const uint8_t stmac[6],
int threadid)
= NULL;
int (*dso_ac_crypto_engine_supported_features)(void) = NULL;
uint8_t * (*dso_ac_crypto_engine_get_pmk)(ac_crypto_engine_t * engine,
int threadid,
int index)
= NULL;
uint8_t * (*dso_ac_crypto_engine_get_ptk)(ac_crypto_engine_t * engine,
int threadid,
int index)
= NULL;
void (*dso_ac_crypto_engine_calc_one_pmk)(const uint8_t * key,
const uint8_t * essid,
uint32_t essid_length,
uint8_t pmk[40])
= NULL;
void (*dso_ac_crypto_engine_calc_pmk)(
ac_crypto_engine_t * engine,
const wpapsk_password key[MAX_KEYS_PER_CRYPT_SUPPORTED],
int nparallel,
int threadid)
= NULL;
void (*dso_ac_crypto_engine_calc_mic)(ac_crypto_engine_t * engine,
const uint8_t eapol[256],
const uint32_t eapol_size,
uint8_t mic[MAX_KEYS_PER_CRYPT_SUPPORTED]
[20],
const uint8_t keyver,
const int vectorIdx,
const int threadid)
= NULL;
#endif
#if defined(CYGWIN)
/*
This is merely a hack until code refactoring can occur.
A new module is needed for handling file and path operations, because
this here is only a step towards correctly getting the executable
path for all operating systems.
It was required for Cygwin to determine the location of the
Crypto Engine DLLs which are in the same folder as the
executable.
*/
#include <stdarg.h>
#include <stdio.h>
#include <wtypes.h>
#include <wchar.h>
#include <sys/cygwin.h>
#include <windows.h>
#include <shlwapi.h>
static char * get_executable_directory(void)
{
HMODULE hModule = GetModuleHandle(NULL);
CHAR path[MAX_PATH];
GetModuleFileNameA(hModule, path, MAX_PATH);
PathRemoveFileSpecA(path);
cygwin_conv_path_t flags = CCP_WIN_A_TO_POSIX;
char * winpath = (char *) cygwin_create_path(flags, path);
return winpath;
}
#endif
EXPORT int ac_crypto_engine_loader_get_available(void)
{
int simd_flags = SIMD_SUPPORTS_NONE;
char library_path[8192];
#if defined(WIN32_PORTABLE)
char * working_directory = get_executable_directory();
#else
// are we inside of the build path?
char * working_directory = get_current_working_directory();
#endif
if (strncmp(working_directory, ABS_TOP_BUILDDIR, strlen(ABS_TOP_BUILDDIR))
== 0
|| strncmp(working_directory, ABS_TOP_SRCDIR, strlen(ABS_TOP_SRCDIR))
== 0)
{
// use development paths
snprintf(library_path,
sizeof(library_path) - 1,
"%s%s",
LIBAIRCRACK_CE_WPA_PATH,
LT_OBJDIR);
}
else
{
#if defined(WIN32_PORTABLE)
// use the current directory
snprintf(library_path, sizeof(library_path) - 1, working_directory);
#else
// use installation paths
snprintf(library_path, sizeof(library_path) - 1, "%s", LIBDIR);
#endif
}
free(working_directory);
// enumerate all DSOs in folder, opening, searching symbols, and testing
// them.
DIR * dsos = opendir(library_path);
if (!dsos) goto out;
struct dirent * entry = NULL;
while ((entry = readdir(dsos)) != NULL)
{
#if defined(__APPLE__)
if (string_has_suffix((char *) entry->d_name, ".dylib"))
#elif defined(WIN32) || defined(_WIN32) || defined(CYGWIN)
if (string_has_suffix((char *) entry->d_name, ".dll"))
#else
if (string_has_suffix((char *) entry->d_name, ".so"))
#endif
{
char * search = strstr(entry->d_name, "aircrack-ce-wpa-");
if (search)
{
search += 16;
int flag;
if ((flag = ac_crypto_engine_loader_string_to_flag(search))
!= -1)
simd_flags |= flag;
}
}
}
closedir(dsos);
out:
return simd_flags;
}
EXPORT char * ac_crypto_engine_loader_best_library_for(int simd_features)
{
char buffer[8192] = {"aircrack-ce-wpa"};
char library_path[8192];
char module_filename[8192];
size_t buffer_remaining = 8192 - strlen(buffer) - 1;
if (simd_features & SIMD_SUPPORTS_AVX512F)
{
strncat(buffer, "-x86-avx512", buffer_remaining);
}
else if (simd_features & SIMD_SUPPORTS_AVX2)
{
strncat(buffer, "-x86-avx2", buffer_remaining);
}
else if (simd_features & SIMD_SUPPORTS_AVX)
{
strncat(buffer, "-x86-avx", buffer_remaining);
}
else if (simd_features & SIMD_SUPPORTS_SSE2)
{
strncat(buffer, "-x86-sse2", buffer_remaining);
}
else if (simd_features & SIMD_SUPPORTS_ASIMD)
{
strncat(buffer, "-arm-neon", buffer_remaining);
}
else if (simd_features & SIMD_SUPPORTS_NEON)
{
strncat(buffer, "-arm-neon", buffer_remaining);
}
else if (simd_features & SIMD_SUPPORTS_POWER8)
{
strncat(buffer, "-ppc-power8", buffer_remaining);
}
else if (simd_features & SIMD_SUPPORTS_ALTIVEC)
{
strncat(buffer, "-ppc-altivec", buffer_remaining);
}
char * working_directory
= get_current_working_directory(); // or the binary's path?
if (strncmp(
working_directory, ABS_TOP_BUILDDIR, sizeof(ABS_TOP_BUILDDIR) - 1)
== 0
|| strncmp(
working_directory, ABS_TOP_SRCDIR, sizeof(ABS_TOP_SRCDIR) - 1)
== 0)
{
// use development paths
snprintf(library_path,
sizeof(library_path) - 1,
"%s%s",
LIBAIRCRACK_CE_WPA_PATH,
LT_OBJDIR);
}
else
{
// use installation paths
snprintf(library_path, sizeof(library_path) - 1, "%s", LIBDIR);
}
free(working_directory);
snprintf(module_filename,
sizeof(module_filename) - 1,
#if defined(WIN32_PORTABLE)
"%s%s%s",
#else
"%s/%s%s%s",
library_path,
#endif
#if defined(WIN32) || defined(_WIN32) || defined(CYGWIN)
#if defined(MSYS2)
"msys-",
#else
"cyg",
#endif
#else
"lib",
#endif
buffer,
#if defined(WIN32) || defined(_WIN32) || defined(CYGWIN)
LT_CYGWIN_VER
#elif defined(__APPLE__)
".dylib"
#else
".so"
#endif
)
< 0
? abort()
: (void) 0;
return strdup(module_filename);
}
EXPORT int ac_crypto_engine_loader_string_to_flag(const char * const str)
{
int simd_features = -1;
if (strncmp(str, "avx512", 6) == 0 || strncmp(str, "x86-avx512", 10) == 0)
simd_features = SIMD_SUPPORTS_AVX512F;
else if (strncmp(str, "avx2", 4) == 0 || strncmp(str, "x86-avx2", 8) == 0)
simd_features = SIMD_SUPPORTS_AVX2;
else if (strncmp(str, "avx", 3) == 0 || strncmp(str, "x86-avx", 7) == 0)
simd_features = SIMD_SUPPORTS_AVX;
else if (strncmp(str, "sse2", 4) == 0 || strncmp(str, "x86-sse2", 8) == 0)
simd_features = SIMD_SUPPORTS_SSE2;
else if (strncmp(str, "neon", 4) == 0 || strncmp(str, "arm-neon", 8) == 0)
simd_features = SIMD_SUPPORTS_NEON;
else if (strncmp(str, "asimd", 5) == 0 || strncmp(str, "arm-asimd", 9) == 0)
simd_features = SIMD_SUPPORTS_ASIMD;
else if (strncmp(str, "altivec", 7) == 0
|| strncmp(str, "ppc-altivec", 11) == 0)
simd_features = SIMD_SUPPORTS_ALTIVEC;
else if (strncmp(str, "power8", 6) == 0
|| strncmp(str, "ppc-power8", 10) == 0)
simd_features = SIMD_SUPPORTS_POWER8;
else if (strncmp(str, "generic", 7) == 0)
simd_features = SIMD_SUPPORTS_NONE;
return simd_features;
}
EXPORT char * ac_crypto_engine_loader_flags_to_string(int flags)
{
char buffer[8192] = {0};
if (flags & SIMD_SUPPORTS_AVX512F) strncat(buffer, "avx512 ", 8);
if (flags & SIMD_SUPPORTS_AVX2) strncat(buffer, "avx2 ", 6);
if (flags & SIMD_SUPPORTS_AVX) strncat(buffer, "avx ", 5);
if (flags & SIMD_SUPPORTS_SSE2) strncat(buffer, "sse2 ", 6);
if (flags & SIMD_SUPPORTS_NEON) strncat(buffer, "neon ", 6);
if (flags & SIMD_SUPPORTS_ASIMD) strncat(buffer, "asimd ", 7);
if (flags & SIMD_SUPPORTS_ALTIVEC) strncat(buffer, "altivec ", 9);
if (flags & SIMD_SUPPORTS_POWER8) strncat(buffer, "power8 ", 8);
strncat(buffer, "generic", 8);
return strdup(buffer);
}
EXPORT int ac_crypto_engine_loader_load(int flags)
{
#ifndef STATIC_BUILD
if (flags == -1) flags = ac_crypto_engine_loader_get_available();
char * module_filename = ac_crypto_engine_loader_best_library_for(flags);
module = dlopen(module_filename, RTLD_LAZY);
if (!module)
{
const char * msg = dlerror();
fprintf(stderr,
"Could not open '%s': %s\n",
module_filename,
msg ? msg : "<none reported>");
free(module_filename);
return 1;
}
// resolve symbols needed
struct _dso_symbols
{
char const * sym;
void * addr;
} dso_symbols[] = {
{"ac_crypto_engine_init", (void *) &dso_ac_crypto_engine_init},
{"ac_crypto_engine_destroy", (void *) &dso_ac_crypto_engine_destroy},
{"ac_crypto_engine_thread_init",
(void *) &dso_ac_crypto_engine_thread_init},
{"ac_crypto_engine_thread_destroy",
(void *) &dso_ac_crypto_engine_thread_destroy},
{"ac_crypto_engine_set_essid",
(void *) &dso_ac_crypto_engine_set_essid},
{"ac_crypto_engine_simd_width",
(void *) &dso_ac_crypto_engine_simd_width},
{"ac_crypto_engine_wpa_crack",
(void *) &dso_ac_crypto_engine_wpa_crack},
{"ac_crypto_engine_wpa_pmkid_crack",
(void *) &dso_ac_crypto_engine_wpa_pmkid_crack},
{"ac_crypto_engine_calc_pke", (void *) &dso_ac_crypto_engine_calc_pke},
{"ac_crypto_engine_set_pmkid_salt",
(void *) &dso_ac_crypto_engine_set_pmkid_salt},
{"ac_crypto_engine_supported_features",
(void *) &dso_ac_crypto_engine_supported_features},
{"ac_crypto_engine_get_pmk", (void *) &dso_ac_crypto_engine_get_pmk},
{"ac_crypto_engine_get_ptk", (void *) &dso_ac_crypto_engine_get_ptk},
{"ac_crypto_engine_calc_one_pmk",
(void *) &dso_ac_crypto_engine_calc_one_pmk},
{"ac_crypto_engine_calc_pmk", (void *) &dso_ac_crypto_engine_calc_pmk},
{"ac_crypto_engine_calc_mic", (void *) &dso_ac_crypto_engine_calc_mic},
{NULL, NULL}};
struct _dso_symbols * cur = &dso_symbols[0];
for (; cur->addr != NULL; ++cur)
{
if (!(*((void **) cur->addr) = dlsym(module, cur->sym)))
{
fprintf(stderr,
"Could not find symbol %s in %s.\n",
cur->sym,
module_filename);
dlclose(module);
free(module_filename);
return 1;
}
}
free(module_filename);
#endif
return 0;
}
EXPORT void ac_crypto_engine_loader_unload(void)
{
#ifndef STATIC_BUILD
dlclose(module);
module = NULL;
dso_ac_crypto_engine_init = NULL;
dso_ac_crypto_engine_destroy = NULL;
dso_ac_crypto_engine_thread_init = NULL;
dso_ac_crypto_engine_thread_destroy = NULL;
dso_ac_crypto_engine_set_essid = NULL;
dso_ac_crypto_engine_simd_width = NULL;
dso_ac_crypto_engine_wpa_crack = NULL;
dso_ac_crypto_engine_calc_pke = NULL;
dso_ac_crypto_engine_supported_features = NULL;
#endif
}