gcmp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * GCM with GMAC Protocol (GCMP)
  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 "common/ieee802_11_defs.h"
  11. #include "crypto/aes.h"
  12. #include "crypto/aes_wrap.h"
  13. #include "wlantest.h"
  14. static void gcmp_aad_nonce(const struct ieee80211_hdr *hdr, const u8 *data,
  15. u8 *aad, size_t *aad_len, u8 *nonce)
  16. {
  17. u16 fc, stype, seq;
  18. int qos = 0, addr4 = 0;
  19. u8 *pos;
  20. fc = le_to_host16(hdr->frame_control);
  21. stype = WLAN_FC_GET_STYPE(fc);
  22. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  23. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  24. addr4 = 1;
  25. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
  26. fc &= ~0x0070; /* Mask subtype bits */
  27. if (stype & 0x08) {
  28. const u8 *qc;
  29. qos = 1;
  30. fc &= ~WLAN_FC_ORDER;
  31. qc = (const u8 *) (hdr + 1);
  32. if (addr4)
  33. qc += ETH_ALEN;
  34. }
  35. }
  36. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  37. fc |= WLAN_FC_ISWEP;
  38. WPA_PUT_LE16(aad, fc);
  39. pos = aad + 2;
  40. os_memcpy(pos, hdr->addr1, 3 * ETH_ALEN);
  41. pos += 3 * ETH_ALEN;
  42. seq = le_to_host16(hdr->seq_ctrl);
  43. seq &= ~0xfff0; /* Mask Seq#; do not modify Frag# */
  44. WPA_PUT_LE16(pos, seq);
  45. pos += 2;
  46. os_memcpy(pos, hdr + 1, addr4 * ETH_ALEN + qos * 2);
  47. pos += addr4 * ETH_ALEN;
  48. if (qos) {
  49. pos[0] &= ~0x70;
  50. if (1 /* FIX: either device has SPP A-MSDU Capab = 0 */)
  51. pos[0] &= ~0x80;
  52. pos++;
  53. *pos++ = 0x00;
  54. }
  55. *aad_len = pos - aad;
  56. os_memcpy(nonce, hdr->addr2, ETH_ALEN);
  57. nonce[6] = data[7]; /* PN5 */
  58. nonce[7] = data[6]; /* PN4 */
  59. nonce[8] = data[5]; /* PN3 */
  60. nonce[9] = data[4]; /* PN2 */
  61. nonce[10] = data[1]; /* PN1 */
  62. nonce[11] = data[0]; /* PN0 */
  63. }
  64. u8 * gcmp_decrypt(const u8 *tk, size_t tk_len, const struct ieee80211_hdr *hdr,
  65. const u8 *data, size_t data_len, size_t *decrypted_len)
  66. {
  67. u8 aad[30], nonce[12], *plain;
  68. size_t aad_len, mlen;
  69. const u8 *m;
  70. if (data_len < 8 + 16)
  71. return NULL;
  72. plain = os_malloc(data_len + AES_BLOCK_SIZE);
  73. if (plain == NULL)
  74. return NULL;
  75. m = data + 8;
  76. mlen = data_len - 8 - 16;
  77. os_memset(aad, 0, sizeof(aad));
  78. gcmp_aad_nonce(hdr, data, aad, &aad_len, nonce);
  79. wpa_hexdump(MSG_EXCESSIVE, "GCMP AAD", aad, aad_len);
  80. wpa_hexdump(MSG_EXCESSIVE, "GCMP nonce", nonce, sizeof(nonce));
  81. if (aes_gcm_ad(tk, tk_len, nonce, sizeof(nonce), m, mlen, aad, aad_len,
  82. m + mlen, plain) < 0) {
  83. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  84. wpa_printf(MSG_INFO, "Invalid GCMP frame: A1=" MACSTR
  85. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
  86. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  87. MAC2STR(hdr->addr3),
  88. WLAN_GET_SEQ_SEQ(seq_ctrl),
  89. WLAN_GET_SEQ_FRAG(seq_ctrl));
  90. os_free(plain);
  91. return NULL;
  92. }
  93. *decrypted_len = mlen;
  94. return plain;
  95. }
  96. u8 * gcmp_encrypt(const u8 *tk, size_t tk_len, u8 *frame, size_t len,
  97. size_t hdrlen, u8 *qos,
  98. u8 *pn, int keyid, size_t *encrypted_len)
  99. {
  100. u8 aad[30], nonce[12], *crypt, *pos;
  101. size_t aad_len, plen;
  102. struct ieee80211_hdr *hdr;
  103. if (len < hdrlen || hdrlen < 24)
  104. return NULL;
  105. plen = len - hdrlen;
  106. crypt = os_malloc(hdrlen + 8 + plen + 16 + AES_BLOCK_SIZE);
  107. if (crypt == NULL)
  108. return NULL;
  109. os_memcpy(crypt, frame, hdrlen);
  110. hdr = (struct ieee80211_hdr *) crypt;
  111. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  112. pos = crypt + hdrlen;
  113. *pos++ = pn[5]; /* PN0 */
  114. *pos++ = pn[4]; /* PN1 */
  115. *pos++ = 0x00; /* Rsvd */
  116. *pos++ = 0x20 | (keyid << 6);
  117. *pos++ = pn[3]; /* PN2 */
  118. *pos++ = pn[2]; /* PN3 */
  119. *pos++ = pn[1]; /* PN4 */
  120. *pos++ = pn[0]; /* PN5 */
  121. os_memset(aad, 0, sizeof(aad));
  122. gcmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce);
  123. wpa_hexdump(MSG_EXCESSIVE, "GCMP AAD", aad, aad_len);
  124. wpa_hexdump(MSG_EXCESSIVE, "GCMP nonce", nonce, sizeof(nonce));
  125. if (aes_gcm_ae(tk, tk_len, nonce, sizeof(nonce), frame + hdrlen, plen,
  126. aad, aad_len, pos, pos + plen) < 0) {
  127. os_free(crypt);
  128. return NULL;
  129. }
  130. wpa_hexdump(MSG_EXCESSIVE, "GCMP MIC", pos + plen, 16);
  131. wpa_hexdump(MSG_EXCESSIVE, "GCMP encrypted", pos, plen);
  132. *encrypted_len = hdrlen + 8 + plen + 16;
  133. return crypt;
  134. }