test_vectors.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * test_vectors - IEEE 802.11 test vector generator
  3. * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "wlantest.h"
  12. extern int wpa_debug_level;
  13. extern int wpa_debug_show_keys;
  14. static void test_vector_tkip(void)
  15. {
  16. u8 tk[] = {
  17. 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56,
  18. 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12,
  19. 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78,
  20. 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34
  21. };
  22. u8 pn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
  23. u8 frame[] = {
  24. 0x08, 0x42, 0x2c, 0x00, 0x02, 0x03, 0x04, 0x05,
  25. 0x06, 0x08, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  26. 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xd0, 0x02,
  27. /* 0x00, 0x20, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, */
  28. 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x08, 0x00,
  29. 0x45, 0x00, 0x00, 0x54, 0x00, 0x00, 0x40, 0x00,
  30. 0x40, 0x01, 0xa5, 0x55, 0xc0, 0xa8, 0x0a, 0x02,
  31. 0xc0, 0xa8, 0x0a, 0x01, 0x08, 0x00, 0x3a, 0xb0,
  32. 0x00, 0x00, 0x00, 0x00, 0xcd, 0x4c, 0x05, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x08, 0x09, 0x0a, 0x0b,
  34. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
  35. 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  36. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  37. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
  38. 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33,
  39. 0x34, 0x35, 0x36, 0x37,
  40. /* 0x68, 0x81, 0xa3, 0xf3, 0xd6, 0x48, 0xd0, 0x3c */
  41. };
  42. u8 *enc, *plain;
  43. size_t enc_len, plain_len;
  44. wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.6.3 TKIP test "
  45. "vector\n");
  46. wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
  47. wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
  48. wpa_hexdump(MSG_INFO, "Plaintext MPDU", frame, sizeof(frame));
  49. enc = tkip_encrypt(tk, frame, sizeof(frame), 24, NULL, pn, 0, &enc_len);
  50. if (enc == NULL) {
  51. wpa_printf(MSG_ERROR, "Failed to encrypt TKIP frame");
  52. return;
  53. }
  54. wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
  55. wpa_debug_level = MSG_INFO;
  56. plain = tkip_decrypt(tk, (const struct ieee80211_hdr *) enc,
  57. enc + 24, enc_len - 24, &plain_len);
  58. wpa_debug_level = MSG_EXCESSIVE;
  59. os_free(enc);
  60. if (plain == NULL) {
  61. wpa_printf(MSG_ERROR, "Failed to decrypt TKIP frame");
  62. return;
  63. }
  64. if (plain_len != sizeof(frame) - 24 ||
  65. os_memcmp(plain, frame + 24, plain_len) != 0) {
  66. wpa_hexdump(MSG_ERROR, "Decryption result did not match",
  67. plain, plain_len);
  68. }
  69. os_free(plain);
  70. }
  71. static void test_vector_ccmp(void)
  72. {
  73. u8 tk[] = { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
  74. 0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f };
  75. u8 pn[] = { 0xB5, 0x03, 0x97, 0x76, 0xE7, 0x0C };
  76. u8 frame[] = {
  77. 0x08, 0x48, 0xc3, 0x2c, 0x0f, 0xd2, 0xe1, 0x28,
  78. 0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
  79. 0xab, 0xae, 0xa5, 0xb8, 0xfc, 0xba, 0x80, 0x33,
  80. 0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
  81. 0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
  82. 0x7e, 0x78, 0xa0, 0x50
  83. };
  84. u8 *enc, *plain;
  85. size_t enc_len, plain_len;
  86. u8 fcs[4];
  87. wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.6.4 CCMP test "
  88. "vector\n");
  89. wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
  90. wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
  91. wpa_hexdump(MSG_INFO, "802.11 Header", frame, 24);
  92. wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 24, sizeof(frame) - 24);
  93. enc = ccmp_encrypt(tk, frame, sizeof(frame), 24, NULL, pn, 0, &enc_len);
  94. if (enc == NULL) {
  95. wpa_printf(MSG_ERROR, "Failed to encrypt CCMP frame");
  96. return;
  97. }
  98. wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
  99. WPA_PUT_LE32(fcs, crc32(enc, enc_len));
  100. wpa_hexdump(MSG_INFO, "FCS", fcs, sizeof(fcs));
  101. wpa_debug_level = MSG_INFO;
  102. plain = ccmp_decrypt(tk, (const struct ieee80211_hdr *) enc,
  103. enc + 24, enc_len - 24, &plain_len);
  104. wpa_debug_level = MSG_EXCESSIVE;
  105. os_free(enc);
  106. if (plain == NULL) {
  107. wpa_printf(MSG_ERROR, "Failed to decrypt CCMP frame");
  108. return;
  109. }
  110. if (plain_len != sizeof(frame) - 24 ||
  111. os_memcmp(plain, frame + 24, plain_len) != 0) {
  112. wpa_hexdump(MSG_ERROR, "Decryption result did not match",
  113. plain, plain_len);
  114. }
  115. os_free(plain);
  116. }
  117. static void test_vector_bip(void)
  118. {
  119. u8 igtk[] = {
  120. 0x4e, 0xa9, 0x54, 0x3e, 0x09, 0xcf, 0x2b, 0x1e,
  121. 0xca, 0x66, 0xff, 0xc5, 0x8b, 0xde, 0xcb, 0xcf
  122. };
  123. u8 ipn[] = { 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 };
  124. u8 frame[] = {
  125. 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  126. 0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
  128. 0x02, 0x00
  129. };
  130. u8 *prot;
  131. size_t prot_len;
  132. wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.9.1 BIP with broadcast "
  133. "Deauthentication frame\n");
  134. wpa_hexdump(MSG_INFO, "IGTK", igtk, sizeof(igtk));
  135. wpa_hexdump(MSG_INFO, "IPN", ipn, sizeof(ipn));
  136. wpa_hexdump(MSG_INFO, "Plaintext frame", frame, sizeof(frame));
  137. prot = bip_protect(igtk, frame, sizeof(frame), ipn, 4, &prot_len);
  138. if (prot == NULL) {
  139. wpa_printf(MSG_ERROR, "Failed to protect BIP frame");
  140. return;
  141. }
  142. wpa_hexdump(MSG_INFO, "Protected MPDU (without FCS)", prot, prot_len);
  143. os_free(prot);
  144. }
  145. static void test_vector_ccmp_mgmt(void)
  146. {
  147. u8 tk[] = { 0x66, 0xed, 0x21, 0x04, 0x2f, 0x9f, 0x26, 0xd7,
  148. 0x11, 0x57, 0x06, 0xe4, 0x04, 0x14, 0xcf, 0x2e };
  149. u8 pn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
  150. u8 frame[] = {
  151. 0xc0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  152. 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  153. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00,
  154. 0x02, 0x00
  155. };
  156. u8 *enc, *plain;
  157. size_t enc_len, plain_len;
  158. wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.9.2 CCMP with unicast "
  159. "Deauthentication frame\n");
  160. wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
  161. wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
  162. wpa_hexdump(MSG_INFO, "802.11 Header", frame, 24);
  163. wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 24, sizeof(frame) - 24);
  164. enc = ccmp_encrypt(tk, frame, sizeof(frame), 24, NULL, pn, 0, &enc_len);
  165. if (enc == NULL) {
  166. wpa_printf(MSG_ERROR, "Failed to encrypt CCMP frame");
  167. return;
  168. }
  169. wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
  170. wpa_debug_level = MSG_INFO;
  171. plain = ccmp_decrypt(tk, (const struct ieee80211_hdr *) enc,
  172. enc + 24, enc_len - 24, &plain_len);
  173. wpa_debug_level = MSG_EXCESSIVE;
  174. os_free(enc);
  175. if (plain == NULL) {
  176. wpa_printf(MSG_ERROR, "Failed to decrypt CCMP frame");
  177. return;
  178. }
  179. if (plain_len != sizeof(frame) - 24 ||
  180. os_memcmp(plain, frame + 24, plain_len) != 0) {
  181. wpa_hexdump(MSG_ERROR, "Decryption result did not match",
  182. plain, plain_len);
  183. }
  184. os_free(plain);
  185. }
  186. static void test_vector_gcmp(void)
  187. {
  188. u8 tk[] = { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
  189. 0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f };
  190. u8 pn[] = {
  191. 0x00, 0x89, 0x5F, 0x5F, 0x2B, 0x08
  192. };
  193. u8 frame[] = {
  194. 0x88, 0x48, 0x0b, 0x00, 0x0f, 0xd2, 0xe1, 0x28,
  195. 0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
  196. 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0x80, 0x33,
  197. 0x03, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
  198. 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
  199. 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  200. 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
  201. 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
  202. 0x26, 0x27
  203. };
  204. u8 *enc, *plain;
  205. size_t enc_len, plain_len;
  206. u8 fcs[4];
  207. wpa_printf(MSG_INFO, "\nIEEE P802.11ad/D9.0, M.11.1 GCMP test "
  208. "vector #2\n");
  209. wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
  210. wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
  211. wpa_hexdump(MSG_INFO, "802.11 Header", frame, 26);
  212. wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 26, sizeof(frame) - 26);
  213. enc = gcmp_encrypt(tk, frame, sizeof(frame), 26, frame + 24, pn, 0,
  214. &enc_len);
  215. if (enc == NULL) {
  216. wpa_printf(MSG_ERROR, "Failed to encrypt GCMP frame");
  217. return;
  218. }
  219. wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
  220. WPA_PUT_LE32(fcs, crc32(enc, enc_len));
  221. wpa_hexdump(MSG_INFO, "FCS", fcs, sizeof(fcs));
  222. wpa_debug_level = MSG_INFO;
  223. plain = gcmp_decrypt(tk, (const struct ieee80211_hdr *) enc,
  224. enc + 26, enc_len - 26, &plain_len);
  225. wpa_debug_level = MSG_EXCESSIVE;
  226. os_free(enc);
  227. if (plain == NULL) {
  228. wpa_printf(MSG_ERROR, "Failed to decrypt GCMP frame");
  229. return;
  230. }
  231. if (plain_len != sizeof(frame) - 26 ||
  232. os_memcmp(plain, frame + 26, plain_len) != 0) {
  233. wpa_hexdump(MSG_ERROR, "Decryption result did not match",
  234. plain, plain_len);
  235. }
  236. os_free(plain);
  237. }
  238. int main(int argc, char *argv[])
  239. {
  240. wpa_debug_level = MSG_EXCESSIVE;
  241. wpa_debug_show_keys = 1;
  242. if (os_program_init())
  243. return -1;
  244. test_vector_tkip();
  245. test_vector_ccmp();
  246. test_vector_bip();
  247. test_vector_ccmp_mgmt();
  248. test_vector_gcmp();
  249. os_program_deinit();
  250. return 0;
  251. }