wpa_test.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Host tests for wpa_crypto.c and wpa.c (make test-wpa).
  2. *
  3. * Known-answer vectors: SHA-1 ("abc"), RFC 6070 PBKDF2, the IEEE 802.11
  4. * PSK example (passphrase "password", SSID "IEEE"), the 802.11 PRF test
  5. * vector, FIPS-197 AES-128 and RFC 3394 4.1. Then a simulated
  6. * authenticator runs a full 4-way handshake, a group rekey, a replay and a
  7. * wrong-passphrase case through wpa_rx(). */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "wpa.h"
  11. #include "wpa_crypto.h"
  12. static int fails;
  13. static void hex2bin(const char *h, unsigned char *out)
  14. {
  15. while (h[0] && h[1]) {
  16. unsigned int v;
  17. sscanf(h, "%2x", &v);
  18. *out++ = (unsigned char)v;
  19. h += 2;
  20. }
  21. }
  22. static void expect(const char *name, const unsigned char *got,
  23. const char *hex)
  24. {
  25. unsigned char want[128];
  26. unsigned int n = (unsigned int)strlen(hex) / 2;
  27. hex2bin(hex, want);
  28. if (memcmp(got, want, n) != 0) {
  29. unsigned int i;
  30. printf("FAIL %s\n got ", name);
  31. for (i = 0; i < n; i++)
  32. printf("%02x", got[i]);
  33. printf("\n want %s\n", hex);
  34. fails++;
  35. } else {
  36. printf("ok %s\n", name);
  37. }
  38. }
  39. static void check(const char *name, int cond)
  40. {
  41. printf("%s %s\n", cond ? "ok " : "FAIL", name);
  42. if (!cond)
  43. fails++;
  44. }
  45. /* RFC 3394 wrap (authenticator side, test only). */
  46. static void aes_wrap(const unsigned char kek[16], const unsigned char *in,
  47. unsigned int n, unsigned char *out)
  48. {
  49. unsigned int rk[44], blocks = n / 8, i, j;
  50. unsigned char a[8], b[16];
  51. aes128_setkey(rk, kek);
  52. memset(a, 0xa6, 8);
  53. memcpy(out + 8, in, n);
  54. for (j = 0; j < 6; j++)
  55. for (i = 1; i <= blocks; i++) {
  56. unsigned int t = blocks * j + i;
  57. memcpy(b, a, 8);
  58. memcpy(b + 8, out + 8 * i, 8);
  59. aes128_encrypt(rk, b, b);
  60. memcpy(a, b, 8);
  61. a[7] ^= t; a[6] ^= t >> 8; a[5] ^= t >> 16; a[4] ^= t >> 24;
  62. memcpy(out + 8 * i, b + 8, 8);
  63. }
  64. memcpy(out, a, 8);
  65. }
  66. static void fixed_rng(unsigned char *out, unsigned int n)
  67. {
  68. unsigned int i;
  69. for (i = 0; i < n; i++)
  70. out[i] = (unsigned char)(0x50 + i);
  71. }
  72. /* Authenticator-side EAPOL-Key builder. */
  73. static unsigned int ap_frame(unsigned char *f, unsigned int info,
  74. unsigned long long replay,
  75. const unsigned char *nonce, const unsigned char *rsc,
  76. const unsigned char *data, unsigned int dlen,
  77. const unsigned char *kck)
  78. {
  79. unsigned int body = 95 + dlen, i;
  80. unsigned char h[20];
  81. memset(f, 0, 4 + body);
  82. f[0] = 2; f[1] = 3; f[2] = body >> 8; f[3] = body;
  83. f[4] = 2;
  84. f[5] = info >> 8; f[6] = info;
  85. f[8] = 16;
  86. for (i = 0; i < 8; i++)
  87. f[9 + i] = (unsigned char)(replay >> (56 - 8 * i));
  88. if (nonce)
  89. memcpy(f + 17, nonce, 32);
  90. if (rsc)
  91. memcpy(f + 65, rsc, 8);
  92. f[97] = dlen >> 8; f[98] = dlen;
  93. memcpy(f + 99, data, dlen);
  94. if (kck) {
  95. hmac_sha1(kck, 16, f, 4 + body, h);
  96. memcpy(f + 81, h, 16);
  97. }
  98. return 4 + body;
  99. }
  100. int main(void)
  101. {
  102. unsigned char out[128], key[16], pt[16], ct[24];
  103. unsigned int rk[44], i;
  104. /* ---- crypto known answers ---- */
  105. sha1((const unsigned char *)"abc", 3, out);
  106. expect("sha1(abc)", out, "a9993e364706816aba3e25717850c26c9cd0d89d");
  107. pbkdf2_sha1((const unsigned char *)"password", 8,
  108. (const unsigned char *)"salt", 4, 4096, out, 20);
  109. expect("pbkdf2 rfc6070 c=4096", out,
  110. "4b007901b765489abead49d926f721d065a429c1");
  111. wpa_pmk("password", (const unsigned char *)"IEEE", 4, out);
  112. expect("802.11 PSK->PMK", out,
  113. "f42c6fc52df0ebef9ebb4b90b38a5f902e83fe1b135a70e23aed762e9710a12e");
  114. memset(key, 0x0b, 16);
  115. {
  116. unsigned char k20[20];
  117. memset(k20, 0x0b, 20);
  118. wpa_prf(k20, 20, "prefix", (const unsigned char *)"Hi There",
  119. 8, out, 64);
  120. }
  121. expect("802.11 PRF-512", out,
  122. "bcd4c650b30b9684951829e0d75f9d54b862175ed9f00606e17d8da35402ffee"
  123. "75df78c3d31e0f889f012120c0862beb67753e7439ae242edb8373698356cf5a");
  124. for (i = 0; i < 16; i++)
  125. key[i] = (unsigned char)i;
  126. hex2bin("00112233445566778899aabbccddeeff", pt);
  127. aes128_setkey(rk, key);
  128. aes128_encrypt(rk, pt, out);
  129. expect("aes128 fips-197", out, "69c4e0d86a7b0430d8cdb78070b4c55a");
  130. aes128_decrypt(rk, out, out + 16);
  131. expect("aes128 decrypt", out + 16, "00112233445566778899aabbccddeeff");
  132. hex2bin("1fa68b0a8112b447aef34bd8fb5a7b829d3e862371d2cfe5", ct);
  133. check("rfc3394 unwrap", aes_unwrap(key, ct, 24, out) == 0);
  134. expect("rfc3394 plaintext", out, "00112233445566778899aabbccddeeff");
  135. ct[3] ^= 1;
  136. check("rfc3394 integrity", aes_unwrap(key, ct, 24, out) < 0);
  137. /* ---- 4-way handshake against a simulated authenticator ---- */
  138. {
  139. struct wpa_sm sm;
  140. unsigned char pmk[32], aa[6] = { 2, 0, 0, 0, 0, 1 };
  141. unsigned char spa[6] = { 2, 0, 0, 0, 0, 2 }, anonce[32];
  142. unsigned char ie[22], f[512], reply[256], kd[64], wrapped[72];
  143. unsigned char gtk[16], rsc[8] = { 5, 0, 0, 0, 0, 0, 0, 0 };
  144. unsigned int ielen = wpa_rsn_ie(ie), rlen, n, kdlen;
  145. unsigned char h[20];
  146. int r;
  147. for (i = 0; i < 32; i++) {
  148. pmk[i] = (unsigned char)i;
  149. anonce[i] = (unsigned char)(0x10 + i);
  150. }
  151. for (i = 0; i < 16; i++)
  152. gtk[i] = (unsigned char)(0xa0 + i);
  153. wpa_init(&sm, pmk, aa, spa, ie, ielen, ie, ielen);
  154. n = ap_frame(f, 0x008a, 1, anonce, 0, 0, 0, 0);
  155. r = wpa_rx(&sm, f, n, reply, &rlen, fixed_rng);
  156. check("msg1 -> reply", r == WPA_REPLY && rlen == 4 + 95 + ielen);
  157. expect("PTK", sm.ptk,
  158. "f16138820d8dd0e1a17427b8ac7e4d27032f9e3f1f168fa6"
  159. "099a77850f77449c1a60803e872c30bf3bb0e01c597418ec");
  160. check("msg2 key info 0x010a", reply[5] == 0x01 && reply[6] == 0x0a);
  161. check("msg2 carries our RSN IE",
  162. memcmp(reply + 99, ie, ielen) == 0);
  163. memcpy(out, reply + 81, 16);
  164. memset(reply + 81, 0, 16);
  165. hmac_sha1(WPA_KCK(&sm), 16, reply, rlen, h);
  166. check("msg2 MIC", memcmp(out, h, 16) == 0);
  167. /* msg 3: RSN IE + GTK KDE, padded and wrapped with the KEK. */
  168. memcpy(kd, ie, ielen);
  169. kdlen = ielen;
  170. kd[kdlen++] = 0xdd; kd[kdlen++] = 22;
  171. kd[kdlen++] = 0x00; kd[kdlen++] = 0x0f; kd[kdlen++] = 0xac;
  172. kd[kdlen++] = 1; kd[kdlen++] = 1; kd[kdlen++] = 0;
  173. memcpy(kd + kdlen, gtk, 16);
  174. kdlen += 16;
  175. kd[kdlen++] = 0xdd; /* padding: dd 00 ... */
  176. while (kdlen % 8)
  177. kd[kdlen++] = 0;
  178. aes_wrap(WPA_KEK(&sm), kd, kdlen, wrapped);
  179. n = ap_frame(f, 0x13ca, 2, anonce, rsc, wrapped, kdlen + 8,
  180. WPA_KCK(&sm));
  181. r = wpa_rx(&sm, f, n, reply, &rlen, fixed_rng);
  182. check("msg3 -> install", r == WPA_PTK);
  183. check("GTK index 1", sm.gtk_idx == 1 && sm.gtk_len == 16 &&
  184. memcmp(sm.gtk, gtk, 16) == 0);
  185. check("GTK RSC", sm.gtk_rsc[0] == 5);
  186. check("msg4 key info 0x030a", reply[5] == 0x03 && reply[6] == 0x0a);
  187. r = wpa_rx(&sm, f, n, reply, &rlen, fixed_rng);
  188. check("replayed msg3 rejected", r == WPA_ERR);
  189. /* group rekey: new GTK, key id 2 */
  190. kdlen = 0;
  191. kd[kdlen++] = 0xdd; kd[kdlen++] = 22;
  192. kd[kdlen++] = 0x00; kd[kdlen++] = 0x0f; kd[kdlen++] = 0xac;
  193. kd[kdlen++] = 1; kd[kdlen++] = 2; kd[kdlen++] = 0;
  194. for (i = 0; i < 16; i++)
  195. kd[kdlen++] = (unsigned char)(0xc0 + i);
  196. kd[kdlen++] = 0xdd;
  197. while (kdlen % 8)
  198. kd[kdlen++] = 0;
  199. aes_wrap(WPA_KEK(&sm), kd, kdlen, wrapped);
  200. n = ap_frame(f, 0x1382, 3, 0, rsc, wrapped, kdlen + 8,
  201. WPA_KCK(&sm));
  202. r = wpa_rx(&sm, f, n, reply, &rlen, fixed_rng);
  203. check("group msg1 -> new GTK", r == WPA_GTK && sm.gtk_idx == 2 &&
  204. sm.gtk[0] == 0xc0);
  205. check("group msg2 key info 0x0302",
  206. reply[5] == 0x03 && reply[6] == 0x02);
  207. /* wrong passphrase: msg3 MIC computed with another KCK */
  208. wpa_init(&sm, pmk, aa, spa, ie, ielen, ie, ielen);
  209. n = ap_frame(f, 0x008a, 1, anonce, 0, 0, 0, 0);
  210. wpa_rx(&sm, f, n, reply, &rlen, fixed_rng);
  211. {
  212. unsigned char bad[16];
  213. memset(bad, 0x77, 16);
  214. n = ap_frame(f, 0x13ca, 2, anonce, rsc, wrapped,
  215. kdlen + 8, bad);
  216. }
  217. check("bad MIC rejected", wpa_rx(&sm, f, n, reply, &rlen,
  218. fixed_rng) == WPA_ERR);
  219. }
  220. printf(fails ? "\n%d FAILED\n" : "\nall wpa tests passed\n", fails);
  221. return fails ? 1 : 0;
  222. }