ccmp.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * CTR with CBC-MAC Protocol (CCMP)
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "crypto/aes.h"
  18. #include "wlantest.h"
  19. static void ccmp_aad_nonce(const struct ieee80211_hdr *hdr, const u8 *data,
  20. u8 *aad, size_t *aad_len, u8 *nonce)
  21. {
  22. u16 fc, stype, seq;
  23. int qos = 0, addr4 = 0;
  24. u8 *pos;
  25. nonce[0] = 0;
  26. fc = le_to_host16(hdr->frame_control);
  27. stype = WLAN_FC_GET_STYPE(fc);
  28. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  29. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  30. addr4 = 1;
  31. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
  32. fc &= ~0x0070; /* Mask subtype bits */
  33. if (stype & 0x08) {
  34. const u8 *qc;
  35. qos = 1;
  36. fc &= ~WLAN_FC_ORDER;
  37. qc = (const u8 *) (hdr + 1);
  38. if (addr4)
  39. qc += ETH_ALEN;
  40. nonce[0] = qc[0] & 0x0f;
  41. }
  42. } else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
  43. nonce[0] |= 0x10; /* Management */
  44. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  45. fc |= WLAN_FC_ISWEP;
  46. WPA_PUT_LE16(aad, fc);
  47. pos = aad + 2;
  48. os_memcpy(pos, hdr->addr1, 3 * ETH_ALEN);
  49. pos += 3 * ETH_ALEN;
  50. seq = le_to_host16(hdr->seq_ctrl);
  51. seq &= ~0xfff0; /* Mask Seq#; do not modify Frag# */
  52. WPA_PUT_LE16(pos, seq);
  53. pos += 2;
  54. os_memcpy(pos, hdr + 1, addr4 * ETH_ALEN + qos * 2);
  55. pos += addr4 * ETH_ALEN;
  56. if (qos) {
  57. pos[0] &= ~0x70;
  58. if (1 /* FIX: either device has SPP A-MSDU Capab = 0 */)
  59. pos[0] &= ~0x80;
  60. pos++;
  61. *pos++ = 0x00;
  62. }
  63. *aad_len = pos - aad;
  64. os_memcpy(nonce + 1, hdr->addr2, ETH_ALEN);
  65. nonce[7] = data[7]; /* PN5 */
  66. nonce[8] = data[6]; /* PN4 */
  67. nonce[9] = data[5]; /* PN3 */
  68. nonce[10] = data[4]; /* PN2 */
  69. nonce[11] = data[1]; /* PN1 */
  70. nonce[12] = data[0]; /* PN0 */
  71. }
  72. static void xor_aes_block(u8 *dst, const u8 *src)
  73. {
  74. u32 *d = (u32 *) dst;
  75. u32 *s = (u32 *) src;
  76. *d++ ^= *s++;
  77. *d++ ^= *s++;
  78. *d++ ^= *s++;
  79. *d++ ^= *s++;
  80. }
  81. u8 * ccmp_decrypt(const u8 *tk, const struct ieee80211_hdr *hdr,
  82. const u8 *data, size_t data_len, size_t *decrypted_len)
  83. {
  84. u8 aad[2 + 30], nonce[13];
  85. size_t aad_len;
  86. u8 b[AES_BLOCK_SIZE], x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
  87. void *aes;
  88. const u8 *m, *mpos, *mic;
  89. size_t mlen, last;
  90. int i;
  91. u8 *plain, *ppos;
  92. u8 t[8];
  93. if (data_len < 8 + 8)
  94. return NULL;
  95. plain = os_malloc(data_len + AES_BLOCK_SIZE);
  96. if (plain == NULL)
  97. return NULL;
  98. aes = aes_encrypt_init(tk, 16);
  99. if (aes == NULL) {
  100. os_free(plain);
  101. return NULL;
  102. }
  103. m = data + 8;
  104. mlen = data_len - 8 - 8;
  105. last = mlen % AES_BLOCK_SIZE;
  106. os_memset(aad, 0, sizeof(aad));
  107. ccmp_aad_nonce(hdr, data, &aad[2], &aad_len, nonce);
  108. WPA_PUT_BE16(aad, aad_len);
  109. wpa_hexdump(MSG_EXCESSIVE, "CCMP AAD", &aad[2], aad_len);
  110. wpa_hexdump(MSG_EXCESSIVE, "CCMP nonce", nonce, 13);
  111. /* CCM: M=8 L=2, Adata=1, M' = (M-2)/2 = 3, L' = L-1 = 1 */
  112. /* A_i = Flags | Nonce N | Counter i */
  113. a[0] = 0x01; /* Flags = L' */
  114. os_memcpy(&a[1], nonce, 13);
  115. /* Decryption */
  116. mic = data + data_len - 8;
  117. wpa_hexdump(MSG_EXCESSIVE, "CCMP U", mic, 8);
  118. /* U = T XOR S_0; S_0 = E(K, A_0) */
  119. WPA_PUT_BE16(&a[14], 0);
  120. aes_encrypt(aes, a, x);
  121. for (i = 0; i < 8; i++)
  122. t[i] = mic[i] ^ x[i];
  123. wpa_hexdump(MSG_EXCESSIVE, "CCMP T", t, 8);
  124. /* plaintext = msg XOR (S_1 | S_2 | ... | S_n) */
  125. ppos = plain;
  126. mpos = m;
  127. for (i = 1; i <= mlen / AES_BLOCK_SIZE; i++) {
  128. WPA_PUT_BE16(&a[14], i);
  129. /* S_i = E(K, A_i) */
  130. aes_encrypt(aes, a, ppos);
  131. xor_aes_block(ppos, mpos);
  132. ppos += AES_BLOCK_SIZE;
  133. mpos += AES_BLOCK_SIZE;
  134. }
  135. if (last) {
  136. WPA_PUT_BE16(&a[14], i);
  137. aes_encrypt(aes, a, ppos);
  138. /* XOR zero-padded last block */
  139. for (i = 0; i < last; i++)
  140. *ppos++ ^= *mpos++;
  141. }
  142. wpa_hexdump(MSG_EXCESSIVE, "CCMP decrypted", plain, mlen);
  143. /* Authentication */
  144. /* B_0: Flags | Nonce N | l(m) */
  145. b[0] = 0x40 /* Adata */ | (3 /* M' */ << 3) | 1 /* L' */;
  146. os_memcpy(&b[1], nonce, 13);
  147. WPA_PUT_BE16(&b[14], mlen);
  148. wpa_hexdump(MSG_EXCESSIVE, "CCMP B_0", b, AES_BLOCK_SIZE);
  149. aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
  150. wpa_hexdump(MSG_EXCESSIVE, "CCMP B_1", aad, AES_BLOCK_SIZE);
  151. xor_aes_block(aad, x);
  152. aes_encrypt(aes, aad, x); /* X_2 = E(K, X_1 XOR B_1) */
  153. wpa_hexdump(MSG_EXCESSIVE, "CCMP B_2", &aad[AES_BLOCK_SIZE],
  154. AES_BLOCK_SIZE);
  155. xor_aes_block(&aad[AES_BLOCK_SIZE], x);
  156. aes_encrypt(aes, &aad[AES_BLOCK_SIZE], x); /* X_3 = E(K, X_2 XOR B_2)
  157. */
  158. ppos = plain;
  159. for (i = 0; i < mlen / AES_BLOCK_SIZE; i++) {
  160. /* X_i+1 = E(K, X_i XOR B_i) */
  161. xor_aes_block(x, ppos);
  162. ppos += AES_BLOCK_SIZE;
  163. aes_encrypt(aes, x, x);
  164. }
  165. if (last) {
  166. /* XOR zero-padded last block */
  167. for (i = 0; i < last; i++)
  168. x[i] ^= *ppos++;
  169. aes_encrypt(aes, x, x);
  170. }
  171. aes_encrypt_deinit(aes);
  172. if (os_memcmp(x, t, 8) != 0) {
  173. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  174. wpa_printf(MSG_INFO, "Invalid CCMP MIC in frame: A1=" MACSTR
  175. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
  176. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  177. MAC2STR(hdr->addr3),
  178. WLAN_GET_SEQ_SEQ(seq_ctrl),
  179. WLAN_GET_SEQ_FRAG(seq_ctrl));
  180. wpa_hexdump(MSG_DEBUG, "CCMP decrypted", plain, mlen);
  181. os_free(plain);
  182. return NULL;
  183. }
  184. *decrypted_len = mlen;
  185. return plain;
  186. }
  187. void ccmp_get_pn(u8 *pn, const u8 *data)
  188. {
  189. pn[0] = data[7]; /* PN5 */
  190. pn[1] = data[6]; /* PN4 */
  191. pn[2] = data[5]; /* PN3 */
  192. pn[3] = data[4]; /* PN2 */
  193. pn[4] = data[1]; /* PN1 */
  194. pn[5] = data[0]; /* PN0 */
  195. }
  196. u8 * ccmp_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen, u8 *qos,
  197. u8 *pn, int keyid, size_t *encrypted_len)
  198. {
  199. u8 aad[2 + 30], nonce[13];
  200. size_t aad_len;
  201. u8 b[AES_BLOCK_SIZE], x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
  202. void *aes;
  203. u8 *crypt, *pos, *ppos, *mpos;
  204. size_t plen, last;
  205. struct ieee80211_hdr *hdr;
  206. int i;
  207. if (len < hdrlen || hdrlen < 24)
  208. return NULL;
  209. plen = len - hdrlen;
  210. last = plen % AES_BLOCK_SIZE;
  211. crypt = os_malloc(hdrlen + 8 + plen + 8 + AES_BLOCK_SIZE);
  212. if (crypt == NULL)
  213. return NULL;
  214. os_memcpy(crypt, frame, hdrlen);
  215. hdr = (struct ieee80211_hdr *) crypt;
  216. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  217. pos = crypt + hdrlen;
  218. *pos++ = pn[5]; /* PN0 */
  219. *pos++ = pn[4]; /* PN1 */
  220. *pos++ = 0x00; /* Rsvd */
  221. *pos++ = 0x20 | (keyid << 6);
  222. *pos++ = pn[3]; /* PN2 */
  223. *pos++ = pn[2]; /* PN3 */
  224. *pos++ = pn[1]; /* PN4 */
  225. *pos++ = pn[0]; /* PN5 */
  226. aes = aes_encrypt_init(tk, 16);
  227. if (aes == NULL) {
  228. os_free(crypt);
  229. return NULL;
  230. }
  231. os_memset(aad, 0, sizeof(aad));
  232. ccmp_aad_nonce(hdr, crypt + hdrlen, &aad[2], &aad_len, nonce);
  233. WPA_PUT_BE16(aad, aad_len);
  234. wpa_hexdump(MSG_EXCESSIVE, "CCMP AAD", &aad[2], aad_len);
  235. wpa_hexdump(MSG_EXCESSIVE, "CCMP nonce", nonce, 13);
  236. /* Authentication */
  237. /* B_0: Flags | Nonce N | l(m) */
  238. b[0] = 0x40 /* Adata */ | (3 /* M' */ << 3) | 1 /* L' */;
  239. os_memcpy(&b[1], nonce, 13);
  240. WPA_PUT_BE16(&b[14], plen);
  241. wpa_hexdump(MSG_EXCESSIVE, "CCMP B_0", b, AES_BLOCK_SIZE);
  242. aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
  243. wpa_hexdump(MSG_EXCESSIVE, "CCMP B_1", aad, AES_BLOCK_SIZE);
  244. xor_aes_block(aad, x);
  245. aes_encrypt(aes, aad, x); /* X_2 = E(K, X_1 XOR B_1) */
  246. wpa_hexdump(MSG_EXCESSIVE, "CCMP B_2", &aad[AES_BLOCK_SIZE],
  247. AES_BLOCK_SIZE);
  248. xor_aes_block(&aad[AES_BLOCK_SIZE], x);
  249. aes_encrypt(aes, &aad[AES_BLOCK_SIZE], x); /* X_3 = E(K, X_2 XOR B_2)
  250. */
  251. ppos = frame + hdrlen;
  252. for (i = 0; i < plen / AES_BLOCK_SIZE; i++) {
  253. /* X_i+1 = E(K, X_i XOR B_i) */
  254. xor_aes_block(x, ppos);
  255. ppos += AES_BLOCK_SIZE;
  256. aes_encrypt(aes, x, x);
  257. }
  258. if (last) {
  259. /* XOR zero-padded last block */
  260. for (i = 0; i < last; i++)
  261. x[i] ^= *ppos++;
  262. aes_encrypt(aes, x, x);
  263. }
  264. /* Encryption */
  265. /* CCM: M=8 L=2, Adata=1, M' = (M-2)/2 = 3, L' = L-1 = 1 */
  266. /* A_i = Flags | Nonce N | Counter i */
  267. a[0] = 0x01; /* Flags = L' */
  268. os_memcpy(&a[1], nonce, 13);
  269. ppos = crypt + hdrlen + 8;
  270. /* crypt = msg XOR (S_1 | S_2 | ... | S_n) */
  271. mpos = frame + hdrlen;
  272. for (i = 1; i <= plen / AES_BLOCK_SIZE; i++) {
  273. WPA_PUT_BE16(&a[14], i);
  274. /* S_i = E(K, A_i) */
  275. aes_encrypt(aes, a, ppos);
  276. xor_aes_block(ppos, mpos);
  277. ppos += AES_BLOCK_SIZE;
  278. mpos += AES_BLOCK_SIZE;
  279. }
  280. if (last) {
  281. WPA_PUT_BE16(&a[14], i);
  282. aes_encrypt(aes, a, ppos);
  283. /* XOR zero-padded last block */
  284. for (i = 0; i < last; i++)
  285. *ppos++ ^= *mpos++;
  286. }
  287. wpa_hexdump(MSG_EXCESSIVE, "CCMP T", x, 8);
  288. /* U = T XOR S_0; S_0 = E(K, A_0) */
  289. WPA_PUT_BE16(&a[14], 0);
  290. aes_encrypt(aes, a, b);
  291. for (i = 0; i < 8; i++)
  292. ppos[i] = x[i] ^ b[i];
  293. wpa_hexdump(MSG_EXCESSIVE, "CCMP U", ppos, 8);
  294. wpa_hexdump(MSG_EXCESSIVE, "CCMP encrypted", crypt + hdrlen + 8, plen);
  295. aes_encrypt_deinit(aes);
  296. *encrypted_len = hdrlen + 8 + plen + 8;
  297. return crypt;
  298. }