ccmp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * CTR with CBC-MAC Protocol (CCMP)
  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.h"
  12. #include "crypto/aes_wrap.h"
  13. #include "wlantest.h"
  14. static void ccmp_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. nonce[0] = 0;
  21. fc = le_to_host16(hdr->frame_control);
  22. stype = WLAN_FC_GET_STYPE(fc);
  23. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  24. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  25. addr4 = 1;
  26. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
  27. fc &= ~0x0070; /* Mask subtype bits */
  28. if (stype & 0x08) {
  29. const u8 *qc;
  30. qos = 1;
  31. fc &= ~WLAN_FC_ORDER;
  32. qc = (const u8 *) (hdr + 1);
  33. if (addr4)
  34. qc += ETH_ALEN;
  35. nonce[0] = qc[0] & 0x0f;
  36. }
  37. } else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
  38. nonce[0] |= 0x10; /* Management */
  39. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  40. fc |= WLAN_FC_ISWEP;
  41. WPA_PUT_LE16(aad, fc);
  42. pos = aad + 2;
  43. os_memcpy(pos, hdr->addr1, 3 * ETH_ALEN);
  44. pos += 3 * ETH_ALEN;
  45. seq = le_to_host16(hdr->seq_ctrl);
  46. seq &= ~0xfff0; /* Mask Seq#; do not modify Frag# */
  47. WPA_PUT_LE16(pos, seq);
  48. pos += 2;
  49. os_memcpy(pos, hdr + 1, addr4 * ETH_ALEN + qos * 2);
  50. pos += addr4 * ETH_ALEN;
  51. if (qos) {
  52. pos[0] &= ~0x70;
  53. if (1 /* FIX: either device has SPP A-MSDU Capab = 0 */)
  54. pos[0] &= ~0x80;
  55. pos++;
  56. *pos++ = 0x00;
  57. }
  58. *aad_len = pos - aad;
  59. os_memcpy(nonce + 1, hdr->addr2, ETH_ALEN);
  60. nonce[7] = data[7]; /* PN5 */
  61. nonce[8] = data[6]; /* PN4 */
  62. nonce[9] = data[5]; /* PN3 */
  63. nonce[10] = data[4]; /* PN2 */
  64. nonce[11] = data[1]; /* PN1 */
  65. nonce[12] = data[0]; /* PN0 */
  66. }
  67. static void ccmp_aad_nonce_pv1(const u8 *hdr, const u8 *a1, const u8 *a2,
  68. const u8 *a3, const u8 *pn,
  69. u8 *aad, size_t *aad_len, u8 *nonce)
  70. {
  71. u16 fc, type;
  72. u8 *pos;
  73. nonce[0] = BIT(5); /* PV1 */
  74. /* TODO: Priority for QMF; 0 is used for Data frames */
  75. fc = WPA_GET_LE16(hdr);
  76. type = (fc & (BIT(2) | BIT(3) | BIT(4))) >> 2;
  77. if (type == 1)
  78. nonce[0] |= 0x10; /* Management */
  79. fc &= ~(BIT(10) | BIT(11) | BIT(13) | BIT(14) | BIT(15));
  80. fc |= BIT(12);
  81. WPA_PUT_LE16(aad, fc);
  82. pos = aad + 2;
  83. if (type == 0 || type == 3) {
  84. const u8 *sc;
  85. os_memcpy(pos, a1, ETH_ALEN);
  86. pos += ETH_ALEN;
  87. os_memcpy(pos, a2, ETH_ALEN);
  88. pos += ETH_ALEN;
  89. if (type == 0) {
  90. /* Either A1 or A2 contains SID */
  91. sc = hdr + 2 + 2 + ETH_ALEN;
  92. } else {
  93. /* Both A1 and A2 contain full addresses */
  94. sc = hdr + 2 + 2 * ETH_ALEN;
  95. }
  96. /* SC with Sequence Number subfield (bits 4-15 of the Sequence
  97. * Control field) masked to 0. */
  98. *pos++ = *sc & 0x0f;
  99. *pos++ = 0;
  100. if (a3) {
  101. os_memcpy(pos, a3, ETH_ALEN);
  102. pos += ETH_ALEN;
  103. }
  104. }
  105. *aad_len = pos - aad;
  106. os_memcpy(nonce + 1, a2, ETH_ALEN);
  107. nonce[7] = pn[5]; /* PN5 */
  108. nonce[8] = pn[4]; /* PN4 */
  109. nonce[9] = pn[3]; /* PN3 */
  110. nonce[10] = pn[2]; /* PN2 */
  111. nonce[11] = pn[1]; /* PN1 */
  112. nonce[12] = pn[0]; /* PN0 */
  113. }
  114. u8 * ccmp_decrypt(const u8 *tk, const struct ieee80211_hdr *hdr,
  115. const u8 *data, size_t data_len, size_t *decrypted_len)
  116. {
  117. u8 aad[30], nonce[13];
  118. size_t aad_len;
  119. size_t mlen;
  120. u8 *plain;
  121. if (data_len < 8 + 8)
  122. return NULL;
  123. plain = os_malloc(data_len + AES_BLOCK_SIZE);
  124. if (plain == NULL)
  125. return NULL;
  126. mlen = data_len - 8 - 8;
  127. os_memset(aad, 0, sizeof(aad));
  128. ccmp_aad_nonce(hdr, data, aad, &aad_len, nonce);
  129. wpa_hexdump(MSG_EXCESSIVE, "CCMP AAD", aad, aad_len);
  130. wpa_hexdump(MSG_EXCESSIVE, "CCMP nonce", nonce, 13);
  131. if (aes_ccm_ad(tk, 16, nonce, 8, data + 8, mlen, aad, aad_len,
  132. data + 8 + mlen, plain) < 0) {
  133. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  134. wpa_printf(MSG_INFO, "Invalid CCMP MIC in frame: A1=" MACSTR
  135. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
  136. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  137. MAC2STR(hdr->addr3),
  138. WLAN_GET_SEQ_SEQ(seq_ctrl),
  139. WLAN_GET_SEQ_FRAG(seq_ctrl));
  140. os_free(plain);
  141. return NULL;
  142. }
  143. wpa_hexdump(MSG_EXCESSIVE, "CCMP decrypted", plain, mlen);
  144. *decrypted_len = mlen;
  145. return plain;
  146. }
  147. void ccmp_get_pn(u8 *pn, const u8 *data)
  148. {
  149. pn[0] = data[7]; /* PN5 */
  150. pn[1] = data[6]; /* PN4 */
  151. pn[2] = data[5]; /* PN3 */
  152. pn[3] = data[4]; /* PN2 */
  153. pn[4] = data[1]; /* PN1 */
  154. pn[5] = data[0]; /* PN0 */
  155. }
  156. u8 * ccmp_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen, u8 *qos,
  157. u8 *pn, int keyid, size_t *encrypted_len)
  158. {
  159. u8 aad[30], nonce[13];
  160. size_t aad_len, plen;
  161. u8 *crypt, *pos;
  162. struct ieee80211_hdr *hdr;
  163. if (len < hdrlen || hdrlen < 24)
  164. return NULL;
  165. plen = len - hdrlen;
  166. crypt = os_malloc(hdrlen + 8 + plen + 8 + AES_BLOCK_SIZE);
  167. if (crypt == NULL)
  168. return NULL;
  169. os_memcpy(crypt, frame, hdrlen);
  170. hdr = (struct ieee80211_hdr *) crypt;
  171. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  172. pos = crypt + hdrlen;
  173. *pos++ = pn[5]; /* PN0 */
  174. *pos++ = pn[4]; /* PN1 */
  175. *pos++ = 0x00; /* Rsvd */
  176. *pos++ = 0x20 | (keyid << 6);
  177. *pos++ = pn[3]; /* PN2 */
  178. *pos++ = pn[2]; /* PN3 */
  179. *pos++ = pn[1]; /* PN4 */
  180. *pos++ = pn[0]; /* PN5 */
  181. os_memset(aad, 0, sizeof(aad));
  182. ccmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce);
  183. wpa_hexdump(MSG_EXCESSIVE, "CCMP AAD", aad, aad_len);
  184. wpa_hexdump(MSG_EXCESSIVE, "CCMP nonce", nonce, 13);
  185. if (aes_ccm_ae(tk, 16, nonce, 8, frame + hdrlen, plen, aad, aad_len,
  186. pos, pos + plen) < 0) {
  187. os_free(crypt);
  188. return NULL;
  189. }
  190. wpa_hexdump(MSG_EXCESSIVE, "CCMP encrypted", crypt + hdrlen + 8, plen);
  191. *encrypted_len = hdrlen + 8 + plen + 8;
  192. return crypt;
  193. }
  194. u8 * ccmp_encrypt_pv1(const u8 *tk, const u8 *a1, const u8 *a2, const u8 *a3,
  195. const u8 *frame, size_t len,
  196. size_t hdrlen, const u8 *pn, int keyid,
  197. size_t *encrypted_len)
  198. {
  199. u8 aad[24], nonce[13];
  200. size_t aad_len, plen;
  201. u8 *crypt, *pos;
  202. struct ieee80211_hdr *hdr;
  203. if (len < hdrlen || hdrlen < 12)
  204. return NULL;
  205. plen = len - hdrlen;
  206. crypt = os_malloc(hdrlen + plen + 8 + AES_BLOCK_SIZE);
  207. if (crypt == NULL)
  208. return NULL;
  209. os_memcpy(crypt, frame, hdrlen);
  210. hdr = (struct ieee80211_hdr *) crypt;
  211. hdr->frame_control |= host_to_le16(BIT(12)); /* Protected Frame */
  212. pos = crypt + hdrlen;
  213. os_memset(aad, 0, sizeof(aad));
  214. ccmp_aad_nonce_pv1(crypt, a1, a2, a3, pn, aad, &aad_len, nonce);
  215. wpa_hexdump(MSG_EXCESSIVE, "CCMP AAD", aad, aad_len);
  216. wpa_hexdump(MSG_EXCESSIVE, "CCMP nonce", nonce, sizeof(nonce));
  217. if (aes_ccm_ae(tk, 16, nonce, 8, frame + hdrlen, plen, aad, aad_len,
  218. pos, pos + plen) < 0) {
  219. os_free(crypt);
  220. return NULL;
  221. }
  222. wpa_hexdump(MSG_EXCESSIVE, "CCMP encrypted", crypt + hdrlen, plen);
  223. *encrypted_len = hdrlen + plen + 8;
  224. return crypt;
  225. }
  226. u8 * ccmp_256_decrypt(const u8 *tk, const struct ieee80211_hdr *hdr,
  227. const u8 *data, size_t data_len, size_t *decrypted_len)
  228. {
  229. u8 aad[30], nonce[13];
  230. size_t aad_len;
  231. size_t mlen;
  232. u8 *plain;
  233. if (data_len < 8 + 16)
  234. return NULL;
  235. plain = os_malloc(data_len + AES_BLOCK_SIZE);
  236. if (plain == NULL)
  237. return NULL;
  238. mlen = data_len - 8 - 16;
  239. os_memset(aad, 0, sizeof(aad));
  240. ccmp_aad_nonce(hdr, data, aad, &aad_len, nonce);
  241. wpa_hexdump(MSG_EXCESSIVE, "CCMP-256 AAD", aad, aad_len);
  242. wpa_hexdump(MSG_EXCESSIVE, "CCMP-256 nonce", nonce, 13);
  243. if (aes_ccm_ad(tk, 32, nonce, 16, data + 8, mlen, aad, aad_len,
  244. data + 8 + mlen, plain) < 0) {
  245. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  246. wpa_printf(MSG_INFO, "Invalid CCMP-256 MIC in frame: A1=" MACSTR
  247. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
  248. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  249. MAC2STR(hdr->addr3),
  250. WLAN_GET_SEQ_SEQ(seq_ctrl),
  251. WLAN_GET_SEQ_FRAG(seq_ctrl));
  252. os_free(plain);
  253. return NULL;
  254. }
  255. wpa_hexdump(MSG_EXCESSIVE, "CCMP-256 decrypted", plain, mlen);
  256. *decrypted_len = mlen;
  257. return plain;
  258. }
  259. u8 * ccmp_256_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen,
  260. u8 *qos, u8 *pn, int keyid, size_t *encrypted_len)
  261. {
  262. u8 aad[30], nonce[13];
  263. size_t aad_len, plen;
  264. u8 *crypt, *pos;
  265. struct ieee80211_hdr *hdr;
  266. if (len < hdrlen || hdrlen < 24)
  267. return NULL;
  268. plen = len - hdrlen;
  269. crypt = os_malloc(hdrlen + 8 + plen + 16 + AES_BLOCK_SIZE);
  270. if (crypt == NULL)
  271. return NULL;
  272. os_memcpy(crypt, frame, hdrlen);
  273. hdr = (struct ieee80211_hdr *) crypt;
  274. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  275. pos = crypt + hdrlen;
  276. *pos++ = pn[5]; /* PN0 */
  277. *pos++ = pn[4]; /* PN1 */
  278. *pos++ = 0x00; /* Rsvd */
  279. *pos++ = 0x20 | (keyid << 6);
  280. *pos++ = pn[3]; /* PN2 */
  281. *pos++ = pn[2]; /* PN3 */
  282. *pos++ = pn[1]; /* PN4 */
  283. *pos++ = pn[0]; /* PN5 */
  284. os_memset(aad, 0, sizeof(aad));
  285. ccmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce);
  286. wpa_hexdump(MSG_EXCESSIVE, "CCMP-256 AAD", aad, aad_len);
  287. wpa_hexdump(MSG_EXCESSIVE, "CCMP-256 nonce", nonce, 13);
  288. if (aes_ccm_ae(tk, 32, nonce, 16, frame + hdrlen, plen, aad, aad_len,
  289. pos, pos + plen) < 0) {
  290. os_free(crypt);
  291. return NULL;
  292. }
  293. wpa_hexdump(MSG_EXCESSIVE, "CCMP-256 encrypted", crypt + hdrlen + 8,
  294. plen);
  295. *encrypted_len = hdrlen + 8 + plen + 16;
  296. return crypt;
  297. }