Debian Codegraph @

test/unit/test-wpapsk-cmac.c

raw
1 /*
2 * Copyright (C) 2018 Joseph Benden <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
11 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
12 * NON-INFRINGEMENT. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
17 * MA 02110-1301, USA.
18 *
19 * In addition, as a special exception, the copyright holders give
20 * permission to link the code of portions of this program with the
21 * OpenSSL library under certain conditions as described in each
22 * individual source file, and distribute linked combinations
23 * including the two.
24 * You must obey the GNU General Public License in all respects
25 * for all of the code used other than OpenSSL. If you modify
26 * file(s) with this exception, you may extend this exception to your
27 * version of the file(s), but you are not obligated to do so. If you
28 * do not wish to do so, delete this exception statement from your
29 * version. If you delete this exception statement from all source
30 * files in the program, then also delete it here.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <stdarg.h>
38 #include <stddef.h>
39 #include <setjmp.h>
40 #include <cmocka.h>
41
42 #include <assert.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <sys/types.h>
46
47 #include "aircrack-ng/crypto/crypto.h"
48 #include "aircrack-ng/support/common.h"
49 #include "aircrack-ng/cpu/trampoline.h"
50 #include "aircrack-ng/ce-wpa/crypto_engine.h"
51 #include "aircrack-ng/support/crypto_engine_loader.h"
52
53 /*
54 * We must force linking to one of the support crypto libraries; however,
55 * because they are linked with our binary and not the crypto engine
56 * DSOs, at run-time we fail to run due to missing symbols.
57 *
58 * The "proper" way to handle this situation is to use --no-as-needed
59 * linker flag, specifying the libraries to always link against.
60 *
61 * Then there is Autoconf... It does not support the above flag.
62 *
63 * So, we force a bit of hacks to ensure we do link against it.
64 */
65 #ifdef USE_GCRYPT
66 void * keep_libgcrypt_ = (void *) ((uintptr_t) &gcry_md_open);
67 #else
68 void * keep_libcrypto_ = (void *) ((uintptr_t) &HMAC);
69 #endif
70
71 void perform_unit_testing(void ** state)
72 {
73 (void) state;
74
75 wpapsk_password key[MAX_KEYS_PER_CRYPT_SUPPORTED];
76 uint8_t mic[8][20];
77 uint8_t expected_mic[20]
78 = "\x2e\x13\xc4\x0c\xa1\xc2\xe4\xe2\x03\x7f\x99\xa2\xda\x18\xa4\x6b";
79 uint8_t stmac[6] = "\x2c\xf0\xa2\xdd\xbc\xd0";
80 uint8_t snonce[32]
81 = "\x64\x67\x23\x3e\x73\x07\x67\xc3\x3e\x1d\xf8\x75\xc3\xad\x0e\xb5"
82 "\x8a\x51\xad\x70\x4a\x3f\xae\x06\xb8\x18\xc0\xc5\xfc\xeb\xf3\xaf";
83 uint8_t anonce[32]
84 = "\x02\x18\xc7\xb6\x4e\xce\xf4\x0c\x4f\x15\x91\x5f\xbc\xeb\x19\xc8"
85 "\xd6\x26\x08\x38\x7e\xb6\xb9\x86\xd9\x59\x9a\x8b\xd7\x0d\xc8\x5d";
86 uint8_t eapol[256]
87 = "\x02\x03\x00\x75\x02\x01\x0B\x00\x10\x00\x00\x00\x00\x00\x00\x00"
88 "\x03\x64\x67\x23\x3E\x73\x07\x67\xC3\x3E\x1D\xF8\x75\xC3\xAD\x0E"
89 "\xB5\x8A\x51\xAD\x70\x4A\x3F\xAE\x06\xB8\x18\xC0\xC5\xFC\xEB\xF3"
90 "\xAF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
91 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
92 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
93 "\x00\x00\x16\x30\x14\x01\x00\x00\x0F\xAC\x04\x01\x00\x00\x0F\xAC"
94 "\x04\x01\x00\x00\x0F\xAC\x06\x8C\x00\x00\x00\x00\x00\x00\x00\x00"
95 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
96 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
97 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
98 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
99 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
100 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
101 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
102 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
103 uint32_t eapol_size = 121;
104 ac_crypto_engine_t engine;
105 uint8_t bssid[6] = "\xb0\xb9\x8a\x56\x8d\xea";
106 uint8_t essid[33] = "Neheb";
107 int nparallel = dso_ac_crypto_engine_simd_width();
108
109 memset(&engine, 0, sizeof(engine));
110 dso_ac_crypto_engine_init(&engine);
111 dso_ac_crypto_engine_set_essid(&engine, &essid[0]);
112 dso_ac_crypto_engine_thread_init(&engine, 1);
113 dso_ac_crypto_engine_calc_pke(&engine, bssid, stmac, anonce, snonce, 1);
114
115 // PMK: Ensure single calculator functions
116 memset(key, 0, sizeof(key));
117 strcpy((char *) (key[0].v), "bo$$password");
118 key[0].length = 12;
119
120 uint8_t pmk[40];
121 memset(pmk, 0, sizeof(pmk));
122
123 dso_ac_crypto_engine_calc_one_pmk(
124 key[0].v, essid, strlen((char *) essid), pmk);
125 assert_memory_equal(pmk,
126 "\xfb\x57\x66\x8c\xd3\x38\x37\x44\x12\xc2\x62"
127 "\x08\xd7\x9a\xa5\xc3\x0c\xe4\x0a\x11\x02\x24"
128 "\xf3\xcf\xb5\x92\xa8\xf2\xe8\xbf\x53\xe8",
129 32);
130
131 // PMK: Ensure parallel calculator functions
132 memset(key, 0, sizeof(key));
133 strcpy((char *) (key[0].v), "bo$$password");
134 key[0].length = 12;
135
136 dso_ac_crypto_engine_calc_pmk(&engine, key, nparallel, 1);
137 assert_memory_equal(dso_ac_crypto_engine_get_pmk(&engine, 1, 0),
138 "\xfb\x57\x66\x8c\xd3\x38\x37\x44\x12\xc2\x62"
139 "\x08\xd7\x9a\xa5\xc3\x0c\xe4\x0a\x11\x02\x24"
140 "\xf3\xcf\xb5\x92\xa8\xf2\xe8\xbf\x53\xe8",
141 32);
142
143 for (int i = 0; i < nparallel; ++i)
144 {
145 int rc = -1;
146
147 memset(key, 0, sizeof(key));
148
149 strcpy((char *) (key[i].v), "bo$$password");
150 key[i].length = 12;
151
152 if ((rc = dso_ac_crypto_engine_wpa_crack(&engine,
153 key,
154 eapol,
155 eapol_size,
156 mic,
157 3,
158 expected_mic,
159 nparallel,
160 1))
161 >= 0)
162 {
163 // does the returned SIMD lane equal where we placed the key?
164 assert_int_equal(rc, i);
165 }
166 else
167 {
168 assert_memory_equal(dso_ac_crypto_engine_get_pmk(&engine, 1, i),
169 "\xfb\x57\x66\x8c\xd3\x38\x37\x44\x12\xc2\x62"
170 "\x08\xd7\x9a\xa5\xc3\x0c\xe4\x0a\x11\x02\x24"
171 "\xf3\xcf\xb5\x92\xa8\xf2\xe8\xbf\x53\xe8",
172 32);
173
174 assert_memory_equal(dso_ac_crypto_engine_get_ptk(&engine, 1, i),
175 "\x2c\x76\xdc\x59\x2c\x3b\x67\x1b\xac\x23\x0f"
176 "\x6c\x9e\x38\xa0\x62\xa0\xdd\xc9\x8f\x4a\xb4"
177 "\xd6\x12\x90\x22\xfc\x7f\x45\xfe\x92\x64",
178 32);
179
180 fail_msg("%s",
181 "While PMK and PTK computed correctly, MIC, etc. failed.");
182 }
183 }
184
185 dso_ac_crypto_engine_thread_destroy(&engine, 1);
186 dso_ac_crypto_engine_destroy(&engine);
187 }
188
189 void perform_unit_testing_for(void ** state, int simd_flag)
190 {
191 int simd_features = (int) ((uintptr_t) *state);
192
193 // load the DSO
194 ac_crypto_engine_loader_load(simd_flag);
195
196 // Check if this shared library CAN run on the machine, if not; skip testing it.
197 if (simd_features < dso_ac_crypto_engine_supported_features())
198 {
199 // unit-test cannot run without an illegal instruction.
200 skip();
201 }
202 else
203 {
204 // Perform the unit-testing; we can run without an illegal instruction exception.
205 perform_unit_testing(state);
206 }
207
208 #if !defined(SANITIZE_ADDRESS)
209 ac_crypto_engine_loader_unload();
210 #endif
211 }
212
213 void test_crypto_engine_x86_avx512f(void ** state)
214 {
215 perform_unit_testing_for(state, SIMD_SUPPORTS_AVX512F);
216 }
217
218 void test_crypto_engine_x86_avx2(void ** state)
219 {
220 perform_unit_testing_for(state, SIMD_SUPPORTS_AVX2);
221 }
222
223 void test_crypto_engine_x86_avx(void ** state)
224 {
225 perform_unit_testing_for(state, SIMD_SUPPORTS_AVX);
226 }
227
228 void test_crypto_engine_x86_sse2(void ** state)
229 {
230 perform_unit_testing_for(state, SIMD_SUPPORTS_SSE2);
231 }
232
233 void test_crypto_engine_arm_neon(void ** state)
234 {
235 perform_unit_testing_for(state, SIMD_SUPPORTS_NEON);
236 }
237
238 void test_crypto_engine_ppc_altivec(void ** state)
239 {
240 perform_unit_testing_for(state, SIMD_SUPPORTS_ALTIVEC);
241 }
242
243 void test_crypto_engine_ppc_power8(void ** state)
244 {
245 perform_unit_testing_for(state, SIMD_SUPPORTS_POWER8);
246 }
247
248 void test_crypto_engine_generic(void ** state)
249 {
250 perform_unit_testing_for(state, SIMD_SUPPORTS_NONE);
251 }
252
253 int group_setup(void ** state)
254 {
255 *state = (void *) ((uintptr_t) simd_get_supported_features());
256
257 return 0;
258 }
259
260 int main(int argc, char * argv[])
261 {
262 (void) argc;
263 (void) argv;
264
265 #if defined(OPENSSL_WITH_CMAC) || defined(GCRYPT_WITH_CMAC_AES)
266
267 const struct CMUnitTest tests[]
268 = { cmocka_unit_test(test_crypto_engine_generic),
269 #if defined(__x86_64__) || defined(__i386__) || defined(_M_IX86)
270 #if defined(__AVX512F__)
271 cmocka_unit_test(test_crypto_engine_x86_avx512f),
272 #endif
273 cmocka_unit_test(test_crypto_engine_x86_avx2),
274 cmocka_unit_test(test_crypto_engine_x86_avx),
275 cmocka_unit_test(test_crypto_engine_x86_sse2),
276 #elif defined(__arm) || defined(__aarch64) || defined(__aarch64__)
277 cmocka_unit_test(test_crypto_engine_arm_neon),
278 #elif defined(__PPC__) || defined(__PPC64__)
279 cmocka_unit_test(test_crypto_engine_ppc_altivec),
280 cmocka_unit_test(test_crypto_engine_ppc_power8),
281 #else
282 /* warning "SIMD not available." */
283 #endif
284 };
285 return cmocka_run_group_tests(tests, group_setup, NULL);
286
287 #else
288 fprintf(stderr, "SKIP: Missing CMAC algorithm.\n");
289 return 0;
290 #endif
291 }