bip.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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, u8 *frame, size_t len, u8 *ipn, int keyid,
  14. 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 + 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++ = 16;
  29. WPA_PUT_LE16(pos, keyid);
  30. pos += 2;
  31. os_memcpy(pos, ipn, 6);
  32. pos += 6;
  33. os_memset(pos, 0, 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, 8);
  55. wpa_hexdump(MSG_DEBUG, "BIP MMIE MIC", pos, 8);
  56. *prot_len = plen;
  57. return prot;
  58. }