Debian Codegraph @

test/unit/test-wpapsk.c

raw
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdarg.h>
6 #include <stddef.h>
7 #include <setjmp.h>
8 #include <cmocka.h>
9
10 #include <assert.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/types.h>
14
15 #include "aircrack-ng/crypto/crypto.h"
16 #include "aircrack-ng/support/common.h"
17 #include "aircrack-ng/cpu/trampoline.h"
18 #include "aircrack-ng/ce-wpa/crypto_engine.h"
19 #include "aircrack-ng/support/crypto_engine_loader.h"
20
21 /*
22 * We must force linking to one of the support crypto libraries; however,
23 * because they are linked with our binary and not the crypto engine
24 * DSOs, at run-time we fail to run due to missing symbols.
25 *
26 * The "proper" way to handle this situation is to use --no-as-needed
27 * linker flag, specifying the libraries to always link against.
28 *
29 * Then there is Autoconf... It does not support the above flag.
30 *
31 * So, we force a bit of hacks to ensure we do link against it.
32 */
33 #ifdef USE_GCRYPT
34 void * keep_libgcrypt_ = (void *) ((uintptr_t) &gcry_md_open);
35 #else
36 void * keep_libcrypto_ = (void *) ((uintptr_t) &HMAC);
37 #endif
38
39 void perform_unit_testing(void ** state)
40 {
41 (void) state;
42
43 wpapsk_password key[MAX_KEYS_PER_CRYPT_SUPPORTED];
44 uint8_t mic[8][20];
45 uint8_t expected_mic[20]
46 = "\xd5\x35\x53\x82\xb8\xa9\xb8\x06\xdc\xaf\x99\xcd\xaf\x56\x4e\xb6";
47 uint8_t stmac[6] = "\x00\x13\x46\xfe\x32\x0c";
48 uint8_t snonce[32]
49 = "\x59\x16\x8b\xc3\xa5\xdf\x18\xd7\x1e\xfb\x64\x23\xf3\x40\x08\x8d"
50 "\xab\x9e\x1b\xa2\xbb\xc5\x86\x59\xe0\x7b\x37\x64\xb0\xde\x85\x70";
51 uint8_t anonce[32]
52 = "\x22\x58\x54\xb0\x44\x4d\xe3\xaf\x06\xd1\x49\x2b\x85\x29\x84\xf0"
53 "\x4c\xf6\x27\x4c\x0e\x32\x18\xb8\x68\x17\x56\x86\x4d\xb7\xa0\x55";
54 uint8_t eapol[256]
55 = "\x01\x03\x00\x75\x02\x01\x0a\x00\x10\x00\x00\x00\x00\x00\x00\x00"
56 "\x01\x59\x16\x8b\xc3\xa5\xdf\x18\xd7\x1e\xfb\x64\x23\xf3\x40\x08"
57 "\x8d\xab\x9e\x1b\xa2\xbb\xc5\x86\x59\xe0\x7b\x37\x64\xb0\xde\x85"
58 "\x70\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
59 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
60 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
61 "\x00\x00\x16\x30\x14\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac"
62 "\x04\x01\x00\x00\x0f\xac\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00"
63 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
64 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
65 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
66 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
67 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
68 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
69 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
70 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
71 uint32_t eapol_size = 121;
72 ac_crypto_engine_t engine;
73 uint8_t bssid[6] = "\x00\x14\x6c\x7e\x40\x80";
74 uint8_t essid[33] = "Harkonen";
75 int nparallel = dso_ac_crypto_engine_simd_width();
76
77 memset(&engine, 0, sizeof(engine));
78 dso_ac_crypto_engine_init(&engine);
79 dso_ac_crypto_engine_set_essid(&engine, &essid[0]);
80 dso_ac_crypto_engine_thread_init(&engine, 1);
81 dso_ac_crypto_engine_calc_pke(&engine, bssid, stmac, anonce, snonce, 1);
82
83 for (int i = 0; i < nparallel; ++i)
84 {
85 int rc;
86
87 memset(key, 0, sizeof(key));
88
89 strcpy((char *) (key[i].v), "12345678");
90 key[i].length = 8;
91
92 if ((rc = dso_ac_crypto_engine_wpa_crack(&engine,
93 key,
94 eapol,
95 eapol_size,
96 mic,
97 2,
98 expected_mic,
99 nparallel,
100 1))
101 >= 0)
102 {
103 // does the returned SIMD lane equal where we placed the key?
104 assert_int_equal(rc, i);
105 }
106 else
107 {
108 fail();
109 }
110 }
111
112 dso_ac_crypto_engine_thread_destroy(&engine, 1);
113 dso_ac_crypto_engine_destroy(&engine);
114 }
115
116 void perform_unit_testing_for(void ** state, int simd_flag)
117 {
118 int simd_features = (int) ((uintptr_t) *state);
119
120 // load the DSO
121 ac_crypto_engine_loader_load(simd_flag);
122
123 // Check if this shared library CAN run on the machine, if not; skip testing it.
124 if (simd_features < dso_ac_crypto_engine_supported_features())
125 {
126 // unit-test cannot run without an illegal instruction.
127 skip();
128 }
129 else
130 {
131 // Perform the unit-testing; we can run without an illegal instruction exception.
132 perform_unit_testing(state);
133 }
134
135 #if !defined(SANITIZE_ADDRESS)
136 ac_crypto_engine_loader_unload();
137 #endif
138 }
139
140 void test_crypto_engine_x86_avx512f(void ** state)
141 {
142 perform_unit_testing_for(state, SIMD_SUPPORTS_AVX512F);
143 }
144
145 void test_crypto_engine_x86_avx2(void ** state)
146 {
147 perform_unit_testing_for(state, SIMD_SUPPORTS_AVX2);
148 }
149
150 void test_crypto_engine_x86_avx(void ** state)
151 {
152 perform_unit_testing_for(state, SIMD_SUPPORTS_AVX);
153 }
154
155 void test_crypto_engine_x86_sse2(void ** state)
156 {
157 perform_unit_testing_for(state, SIMD_SUPPORTS_SSE2);
158 }
159
160 void test_crypto_engine_arm_neon(void ** state)
161 {
162 perform_unit_testing_for(state, SIMD_SUPPORTS_NEON);
163 }
164
165 void test_crypto_engine_ppc_altivec(void ** state)
166 {
167 perform_unit_testing_for(state, SIMD_SUPPORTS_ALTIVEC);
168 }
169
170 void test_crypto_engine_ppc_power8(void ** state)
171 {
172 perform_unit_testing_for(state, SIMD_SUPPORTS_POWER8);
173 }
174
175 void test_crypto_engine_generic(void ** state)
176 {
177 perform_unit_testing_for(state, SIMD_SUPPORTS_NONE);
178 }
179
180 int group_setup(void ** state)
181 {
182 *state = (void *) ((uintptr_t) simd_get_supported_features());
183
184 return 0;
185 }
186
187 int main(int argc, char * argv[])
188 {
189 (void) argc;
190 (void) argv;
191
192 const struct CMUnitTest tests[]
193 = { cmocka_unit_test(test_crypto_engine_generic),
194 #if defined(__x86_64__) || defined(__i386__) || defined(_M_IX86)
195 #if defined(__AVX512F__)
196 cmocka_unit_test(test_crypto_engine_x86_avx512f),
197 #endif
198 cmocka_unit_test(test_crypto_engine_x86_avx2),
199 cmocka_unit_test(test_crypto_engine_x86_avx),
200 cmocka_unit_test(test_crypto_engine_x86_sse2),
201 #elif defined(__arm) || defined(__aarch64) || defined(__aarch64__)
202 cmocka_unit_test(test_crypto_engine_arm_neon),
203 #elif defined(__PPC__) || defined(__PPC64__)
204 cmocka_unit_test(test_crypto_engine_ppc_altivec),
205 cmocka_unit_test(test_crypto_engine_ppc_power8),
206 #else
207 /* warning "SIMD not available." */
208 #endif
209 };
210 return cmocka_run_group_tests(tests, group_setup, NULL);
211 }