| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- /* Host tests for wpa_crypto.c and wpa.c (make test-wpa).
- *
- * Known-answer vectors: SHA-1 ("abc"), RFC 6070 PBKDF2, the IEEE 802.11
- * PSK example (passphrase "password", SSID "IEEE"), the 802.11 PRF test
- * vector, FIPS-197 AES-128 and RFC 3394 4.1. Then a simulated
- * authenticator runs a full 4-way handshake, a group rekey, a replay and a
- * wrong-passphrase case through wpa_rx(). */
- #include <stdio.h>
- #include <string.h>
- #include "wpa.h"
- #include "wpa_crypto.h"
- static int fails;
- static void hex2bin(const char *h, unsigned char *out)
- {
- while (h[0] && h[1]) {
- unsigned int v;
- sscanf(h, "%2x", &v);
- *out++ = (unsigned char)v;
- h += 2;
- }
- }
- static void expect(const char *name, const unsigned char *got,
- const char *hex)
- {
- unsigned char want[128];
- unsigned int n = (unsigned int)strlen(hex) / 2;
- hex2bin(hex, want);
- if (memcmp(got, want, n) != 0) {
- unsigned int i;
- printf("FAIL %s\n got ", name);
- for (i = 0; i < n; i++)
- printf("%02x", got[i]);
- printf("\n want %s\n", hex);
- fails++;
- } else {
- printf("ok %s\n", name);
- }
- }
- static void check(const char *name, int cond)
- {
- printf("%s %s\n", cond ? "ok " : "FAIL", name);
- if (!cond)
- fails++;
- }
- /* RFC 3394 wrap (authenticator side, test only). */
- static void aes_wrap(const unsigned char kek[16], const unsigned char *in,
- unsigned int n, unsigned char *out)
- {
- unsigned int rk[44], blocks = n / 8, i, j;
- unsigned char a[8], b[16];
- aes128_setkey(rk, kek);
- memset(a, 0xa6, 8);
- memcpy(out + 8, in, n);
- for (j = 0; j < 6; j++)
- for (i = 1; i <= blocks; i++) {
- unsigned int t = blocks * j + i;
- memcpy(b, a, 8);
- memcpy(b + 8, out + 8 * i, 8);
- aes128_encrypt(rk, b, b);
- memcpy(a, b, 8);
- a[7] ^= t; a[6] ^= t >> 8; a[5] ^= t >> 16; a[4] ^= t >> 24;
- memcpy(out + 8 * i, b + 8, 8);
- }
- memcpy(out, a, 8);
- }
- static void fixed_rng(unsigned char *out, unsigned int n)
- {
- unsigned int i;
- for (i = 0; i < n; i++)
- out[i] = (unsigned char)(0x50 + i);
- }
- /* Authenticator-side EAPOL-Key builder. */
- static unsigned int ap_frame(unsigned char *f, unsigned int info,
- unsigned long long replay,
- const unsigned char *nonce, const unsigned char *rsc,
- const unsigned char *data, unsigned int dlen,
- const unsigned char *kck)
- {
- unsigned int body = 95 + dlen, i;
- unsigned char h[20];
- memset(f, 0, 4 + body);
- f[0] = 2; f[1] = 3; f[2] = body >> 8; f[3] = body;
- f[4] = 2;
- f[5] = info >> 8; f[6] = info;
- f[8] = 16;
- for (i = 0; i < 8; i++)
- f[9 + i] = (unsigned char)(replay >> (56 - 8 * i));
- if (nonce)
- memcpy(f + 17, nonce, 32);
- if (rsc)
- memcpy(f + 65, rsc, 8);
- f[97] = dlen >> 8; f[98] = dlen;
- memcpy(f + 99, data, dlen);
- if (kck) {
- hmac_sha1(kck, 16, f, 4 + body, h);
- memcpy(f + 81, h, 16);
- }
- return 4 + body;
- }
- int main(void)
- {
- unsigned char out[128], key[16], pt[16], ct[24];
- unsigned int rk[44], i;
- /* ---- crypto known answers ---- */
- sha1((const unsigned char *)"abc", 3, out);
- expect("sha1(abc)", out, "a9993e364706816aba3e25717850c26c9cd0d89d");
- pbkdf2_sha1((const unsigned char *)"password", 8,
- (const unsigned char *)"salt", 4, 4096, out, 20);
- expect("pbkdf2 rfc6070 c=4096", out,
- "4b007901b765489abead49d926f721d065a429c1");
- wpa_pmk("password", (const unsigned char *)"IEEE", 4, out);
- expect("802.11 PSK->PMK", out,
- "f42c6fc52df0ebef9ebb4b90b38a5f902e83fe1b135a70e23aed762e9710a12e");
- memset(key, 0x0b, 16);
- {
- unsigned char k20[20];
- memset(k20, 0x0b, 20);
- wpa_prf(k20, 20, "prefix", (const unsigned char *)"Hi There",
- 8, out, 64);
- }
- expect("802.11 PRF-512", out,
- "bcd4c650b30b9684951829e0d75f9d54b862175ed9f00606e17d8da35402ffee"
- "75df78c3d31e0f889f012120c0862beb67753e7439ae242edb8373698356cf5a");
- for (i = 0; i < 16; i++)
- key[i] = (unsigned char)i;
- hex2bin("00112233445566778899aabbccddeeff", pt);
- aes128_setkey(rk, key);
- aes128_encrypt(rk, pt, out);
- expect("aes128 fips-197", out, "69c4e0d86a7b0430d8cdb78070b4c55a");
- aes128_decrypt(rk, out, out + 16);
- expect("aes128 decrypt", out + 16, "00112233445566778899aabbccddeeff");
- hex2bin("1fa68b0a8112b447aef34bd8fb5a7b829d3e862371d2cfe5", ct);
- check("rfc3394 unwrap", aes_unwrap(key, ct, 24, out) == 0);
- expect("rfc3394 plaintext", out, "00112233445566778899aabbccddeeff");
- ct[3] ^= 1;
- check("rfc3394 integrity", aes_unwrap(key, ct, 24, out) < 0);
- /* ---- 4-way handshake against a simulated authenticator ---- */
- {
- struct wpa_sm sm;
- unsigned char pmk[32], aa[6] = { 2, 0, 0, 0, 0, 1 };
- unsigned char spa[6] = { 2, 0, 0, 0, 0, 2 }, anonce[32];
- unsigned char ie[22], f[512], reply[256], kd[64], wrapped[72];
- unsigned char gtk[16], rsc[8] = { 5, 0, 0, 0, 0, 0, 0, 0 };
- unsigned int ielen = wpa_rsn_ie(ie), rlen, n, kdlen;
- unsigned char h[20];
- int r;
- for (i = 0; i < 32; i++) {
- pmk[i] = (unsigned char)i;
- anonce[i] = (unsigned char)(0x10 + i);
- }
- for (i = 0; i < 16; i++)
- gtk[i] = (unsigned char)(0xa0 + i);
- wpa_init(&sm, pmk, aa, spa, ie, ielen, ie, ielen);
- n = ap_frame(f, 0x008a, 1, anonce, 0, 0, 0, 0);
- r = wpa_rx(&sm, f, n, reply, &rlen, fixed_rng);
- check("msg1 -> reply", r == WPA_REPLY && rlen == 4 + 95 + ielen);
- expect("PTK", sm.ptk,
- "f16138820d8dd0e1a17427b8ac7e4d27032f9e3f1f168fa6"
- "099a77850f77449c1a60803e872c30bf3bb0e01c597418ec");
- check("msg2 key info 0x010a", reply[5] == 0x01 && reply[6] == 0x0a);
- check("msg2 carries our RSN IE",
- memcmp(reply + 99, ie, ielen) == 0);
- memcpy(out, reply + 81, 16);
- memset(reply + 81, 0, 16);
- hmac_sha1(WPA_KCK(&sm), 16, reply, rlen, h);
- check("msg2 MIC", memcmp(out, h, 16) == 0);
- /* msg 3: RSN IE + GTK KDE, padded and wrapped with the KEK. */
- memcpy(kd, ie, ielen);
- kdlen = ielen;
- kd[kdlen++] = 0xdd; kd[kdlen++] = 22;
- kd[kdlen++] = 0x00; kd[kdlen++] = 0x0f; kd[kdlen++] = 0xac;
- kd[kdlen++] = 1; kd[kdlen++] = 1; kd[kdlen++] = 0;
- memcpy(kd + kdlen, gtk, 16);
- kdlen += 16;
- kd[kdlen++] = 0xdd; /* padding: dd 00 ... */
- while (kdlen % 8)
- kd[kdlen++] = 0;
- aes_wrap(WPA_KEK(&sm), kd, kdlen, wrapped);
- n = ap_frame(f, 0x13ca, 2, anonce, rsc, wrapped, kdlen + 8,
- WPA_KCK(&sm));
- r = wpa_rx(&sm, f, n, reply, &rlen, fixed_rng);
- check("msg3 -> install", r == WPA_PTK);
- check("GTK index 1", sm.gtk_idx == 1 && sm.gtk_len == 16 &&
- memcmp(sm.gtk, gtk, 16) == 0);
- check("GTK RSC", sm.gtk_rsc[0] == 5);
- check("msg4 key info 0x030a", reply[5] == 0x03 && reply[6] == 0x0a);
- r = wpa_rx(&sm, f, n, reply, &rlen, fixed_rng);
- check("replayed msg3 rejected", r == WPA_ERR);
- /* group rekey: new GTK, key id 2 */
- kdlen = 0;
- kd[kdlen++] = 0xdd; kd[kdlen++] = 22;
- kd[kdlen++] = 0x00; kd[kdlen++] = 0x0f; kd[kdlen++] = 0xac;
- kd[kdlen++] = 1; kd[kdlen++] = 2; kd[kdlen++] = 0;
- for (i = 0; i < 16; i++)
- kd[kdlen++] = (unsigned char)(0xc0 + i);
- kd[kdlen++] = 0xdd;
- while (kdlen % 8)
- kd[kdlen++] = 0;
- aes_wrap(WPA_KEK(&sm), kd, kdlen, wrapped);
- n = ap_frame(f, 0x1382, 3, 0, rsc, wrapped, kdlen + 8,
- WPA_KCK(&sm));
- r = wpa_rx(&sm, f, n, reply, &rlen, fixed_rng);
- check("group msg1 -> new GTK", r == WPA_GTK && sm.gtk_idx == 2 &&
- sm.gtk[0] == 0xc0);
- check("group msg2 key info 0x0302",
- reply[5] == 0x03 && reply[6] == 0x02);
- /* wrong passphrase: msg3 MIC computed with another KCK */
- wpa_init(&sm, pmk, aa, spa, ie, ielen, ie, ielen);
- n = ap_frame(f, 0x008a, 1, anonce, 0, 0, 0, 0);
- wpa_rx(&sm, f, n, reply, &rlen, fixed_rng);
- {
- unsigned char bad[16];
- memset(bad, 0x77, 16);
- n = ap_frame(f, 0x13ca, 2, anonce, rsc, wrapped,
- kdlen + 8, bad);
- }
- check("bad MIC rejected", wpa_rx(&sm, f, n, reply, &rlen,
- fixed_rng) == WPA_ERR);
- }
- printf(fails ? "\n%d FAILED\n" : "\nall wpa tests passed\n", fails);
- return fails ? 1 : 0;
- }
|