ccmp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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);
  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. wpa_printf(MSG_INFO, "Invalid CCMP MIC in frame from " MACSTR,
  174. MAC2STR(hdr->addr2));
  175. os_free(plain);
  176. return NULL;
  177. }
  178. *decrypted_len = mlen;
  179. return plain;
  180. }
  181. void ccmp_get_pn(u8 *pn, const u8 *data)
  182. {
  183. pn[0] = data[7]; /* PN5 */
  184. pn[1] = data[6]; /* PN4 */
  185. pn[2] = data[5]; /* PN3 */
  186. pn[3] = data[4]; /* PN2 */
  187. pn[4] = data[1]; /* PN1 */
  188. pn[5] = data[0]; /* PN0 */
  189. }
  190. u8 * ccmp_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen, u8 *qos,
  191. u8 *pn, int keyid, size_t *encrypted_len)
  192. {
  193. u8 aad[2 + 30], nonce[13];
  194. size_t aad_len;
  195. u8 b[AES_BLOCK_SIZE], x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
  196. void *aes;
  197. u8 *crypt, *pos, *ppos, *mpos;
  198. size_t plen, last;
  199. struct ieee80211_hdr *hdr;
  200. int i;
  201. if (len < hdrlen || hdrlen < 24)
  202. return NULL;
  203. plen = len - hdrlen;
  204. last = plen % AES_BLOCK_SIZE;
  205. crypt = os_malloc(hdrlen + 8 + plen + 8);
  206. if (crypt == NULL)
  207. return NULL;
  208. os_memcpy(crypt, frame, hdrlen);
  209. hdr = (struct ieee80211_hdr *) crypt;
  210. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  211. pos = crypt + hdrlen;
  212. *pos++ = pn[5]; /* PN0 */
  213. *pos++ = pn[4]; /* PN1 */
  214. *pos++ = 0x00; /* Rsvd */
  215. *pos++ = 0x20 | (keyid << 6);
  216. *pos++ = pn[3]; /* PN2 */
  217. *pos++ = pn[2]; /* PN3 */
  218. *pos++ = pn[1]; /* PN4 */
  219. *pos++ = pn[0]; /* PN5 */
  220. aes = aes_encrypt_init(tk, 16);
  221. if (aes == NULL) {
  222. os_free(crypt);
  223. return NULL;
  224. }
  225. os_memset(aad, 0, sizeof(aad));
  226. ccmp_aad_nonce(hdr, crypt + hdrlen, &aad[2], &aad_len, nonce);
  227. WPA_PUT_BE16(aad, aad_len);
  228. wpa_hexdump(MSG_EXCESSIVE, "CCMP AAD", &aad[2], aad_len);
  229. wpa_hexdump(MSG_EXCESSIVE, "CCMP nonce", nonce, 13);
  230. /* Authentication */
  231. /* B_0: Flags | Nonce N | l(m) */
  232. b[0] = 0x40 /* Adata */ | (3 /* M' */ << 3) | 1 /* L' */;
  233. os_memcpy(&b[1], nonce, 13);
  234. WPA_PUT_BE16(&b[14], plen);
  235. wpa_hexdump(MSG_EXCESSIVE, "CCMP B_0", b, AES_BLOCK_SIZE);
  236. aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
  237. wpa_hexdump(MSG_EXCESSIVE, "CCMP B_1", aad, AES_BLOCK_SIZE);
  238. xor_aes_block(aad, x);
  239. aes_encrypt(aes, aad, x); /* X_2 = E(K, X_1 XOR B_1) */
  240. wpa_hexdump(MSG_EXCESSIVE, "CCMP B_2", &aad[AES_BLOCK_SIZE],
  241. AES_BLOCK_SIZE);
  242. xor_aes_block(&aad[AES_BLOCK_SIZE], x);
  243. aes_encrypt(aes, &aad[AES_BLOCK_SIZE], x); /* X_3 = E(K, X_2 XOR B_2)
  244. */
  245. ppos = frame + hdrlen;
  246. for (i = 0; i < plen / AES_BLOCK_SIZE; i++) {
  247. /* X_i+1 = E(K, X_i XOR B_i) */
  248. xor_aes_block(x, ppos);
  249. ppos += AES_BLOCK_SIZE;
  250. aes_encrypt(aes, x, x);
  251. }
  252. if (last) {
  253. /* XOR zero-padded last block */
  254. for (i = 0; i < last; i++)
  255. x[i] ^= *ppos++;
  256. aes_encrypt(aes, x, x);
  257. }
  258. /* Encryption */
  259. /* CCM: M=8 L=2, Adata=1, M' = (M-2)/2 = 3, L' = L-1 = 1 */
  260. /* A_i = Flags | Nonce N | Counter i */
  261. a[0] = 0x01; /* Flags = L' */
  262. os_memcpy(&a[1], nonce, 13);
  263. ppos = crypt + hdrlen + 8;
  264. /* crypt = msg XOR (S_1 | S_2 | ... | S_n) */
  265. mpos = frame + hdrlen;
  266. for (i = 1; i <= plen / AES_BLOCK_SIZE; i++) {
  267. WPA_PUT_BE16(&a[14], i);
  268. /* S_i = E(K, A_i) */
  269. aes_encrypt(aes, a, ppos);
  270. xor_aes_block(ppos, mpos);
  271. ppos += AES_BLOCK_SIZE;
  272. mpos += AES_BLOCK_SIZE;
  273. }
  274. if (last) {
  275. WPA_PUT_BE16(&a[14], i);
  276. aes_encrypt(aes, a, ppos);
  277. /* XOR zero-padded last block */
  278. for (i = 0; i < last; i++)
  279. *ppos++ ^= *mpos++;
  280. }
  281. wpa_hexdump(MSG_EXCESSIVE, "CCMP T", x, 8);
  282. /* U = T XOR S_0; S_0 = E(K, A_0) */
  283. WPA_PUT_BE16(&a[14], 0);
  284. aes_encrypt(aes, a, b);
  285. for (i = 0; i < 8; i++)
  286. ppos[i] = x[i] ^ b[i];
  287. wpa_hexdump(MSG_EXCESSIVE, "CCMP U", ppos, 8);
  288. wpa_hexdump(MSG_EXCESSIVE, "CCMP encrypted", crypt + hdrlen + 8, plen);
  289. aes_encrypt_deinit(aes);
  290. *encrypted_len = hdrlen + 8 + plen + 8;
  291. return crypt;
  292. }