bip.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * BIP
  3. * Copyright (c) 2010-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 "common/ieee802_11_defs.h"
  11. #include "crypto/aes_wrap.h"
  12. #include "wlantest.h"
  13. u8 * bip_protect(const u8 *igtk, size_t igtk_len, u8 *frame, size_t len,
  14. u8 *ipn, int keyid, size_t *prot_len)
  15. {
  16. u8 *prot, *pos, *buf;
  17. u8 mic[16];
  18. u16 fc;
  19. struct ieee80211_hdr *hdr;
  20. size_t plen;
  21. plen = len + (igtk_len == 32 ? 26 : 18);
  22. prot = os_malloc(plen);
  23. if (prot == NULL)
  24. return NULL;
  25. os_memcpy(prot, frame, len);
  26. pos = prot + len;
  27. *pos++ = WLAN_EID_MMIE;
  28. *pos++ = igtk_len == 32 ? 24 : 16;
  29. WPA_PUT_LE16(pos, keyid);
  30. pos += 2;
  31. os_memcpy(pos, ipn, 6);
  32. pos += 6;
  33. os_memset(pos, 0, igtk_len == 32 ? 16 : 8); /* MIC */
  34. buf = os_malloc(plen + 20 - 24);
  35. if (buf == NULL) {
  36. os_free(prot);
  37. return NULL;
  38. }
  39. /* BIP AAD: FC(masked) A1 A2 A3 */
  40. hdr = (struct ieee80211_hdr *) frame;
  41. fc = le_to_host16(hdr->frame_control);
  42. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  43. WPA_PUT_LE16(buf, fc);
  44. os_memcpy(buf + 2, hdr->addr1, 3 * ETH_ALEN);
  45. os_memcpy(buf + 20, prot + 24, plen - 24);
  46. wpa_hexdump(MSG_MSGDUMP, "BIP: AAD|Body(masked)", buf, plen + 20 - 24);
  47. /* MIC = L(AES-128-CMAC(AAD || Frame Body(masked)), 0, 64) */
  48. if (omac1_aes_128(igtk, buf, plen + 20 - 24, mic) < 0) {
  49. os_free(prot);
  50. os_free(buf);
  51. return NULL;
  52. }
  53. os_free(buf);
  54. os_memcpy(pos, mic, igtk_len == 32 ? 16 : 8);
  55. wpa_hexdump(MSG_DEBUG, "BIP MMIE MIC", pos, igtk_len == 32 ? 16 : 8);
  56. *prot_len = plen;
  57. return prot;
  58. }
  59. u8 * bip_gmac_protect(const u8 *igtk, size_t igtk_len, u8 *frame, size_t len,
  60. u8 *ipn, int keyid, size_t *prot_len)
  61. {
  62. u8 *prot, *pos, *buf;
  63. u16 fc;
  64. struct ieee80211_hdr *hdr;
  65. size_t plen;
  66. u8 nonce[12], *npos;
  67. plen = len + 26;
  68. prot = os_malloc(plen);
  69. if (prot == NULL)
  70. return NULL;
  71. os_memcpy(prot, frame, len);
  72. pos = prot + len;
  73. *pos++ = WLAN_EID_MMIE;
  74. *pos++ = 24;
  75. WPA_PUT_LE16(pos, keyid);
  76. pos += 2;
  77. os_memcpy(pos, ipn, 6);
  78. pos += 6;
  79. os_memset(pos, 0, 16); /* MIC */
  80. buf = os_malloc(plen + 20 - 24);
  81. if (buf == NULL) {
  82. os_free(prot);
  83. return NULL;
  84. }
  85. /* BIP AAD: FC(masked) A1 A2 A3 */
  86. hdr = (struct ieee80211_hdr *) frame;
  87. fc = le_to_host16(hdr->frame_control);
  88. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  89. WPA_PUT_LE16(buf, fc);
  90. os_memcpy(buf + 2, hdr->addr1, 3 * ETH_ALEN);
  91. os_memcpy(buf + 20, prot + 24, plen - 24);
  92. wpa_hexdump(MSG_MSGDUMP, "BIP-GMAC: AAD|Body(masked)",
  93. buf, plen + 20 - 24);
  94. /* Nonce: A2 | IPN */
  95. os_memcpy(nonce, hdr->addr2, ETH_ALEN);
  96. npos = nonce + ETH_ALEN;
  97. *npos++ = ipn[5];
  98. *npos++ = ipn[4];
  99. *npos++ = ipn[3];
  100. *npos++ = ipn[2];
  101. *npos++ = ipn[1];
  102. *npos++ = ipn[0];
  103. wpa_hexdump(MSG_EXCESSIVE, "BIP-GMAC: Nonce", nonce, sizeof(nonce));
  104. /* MIC = AES-GMAC(AAD || Frame Body(masked)) */
  105. if (aes_gmac(igtk, igtk_len, nonce, sizeof(nonce),
  106. buf, plen + 20 - 24, pos) < 0) {
  107. os_free(prot);
  108. os_free(buf);
  109. return NULL;
  110. }
  111. os_free(buf);
  112. wpa_hexdump(MSG_DEBUG, "BIP-GMAC MMIE MIC", pos, 16);
  113. *prot_len = plen;
  114. return prot;
  115. }