ieee80211_crypt_ccmp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
  3. *
  4. * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation. See README and COPYING for
  9. * more details.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/err.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/random.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/if_ether.h>
  20. #include <linux/if_arp.h>
  21. #include <asm/string.h>
  22. #include <linux/wireless.h>
  23. #include <net/ieee80211.h>
  24. #include <linux/crypto.h>
  25. #include <asm/scatterlist.h>
  26. MODULE_AUTHOR("Jouni Malinen");
  27. MODULE_DESCRIPTION("Host AP crypt: CCMP");
  28. MODULE_LICENSE("GPL");
  29. #define AES_BLOCK_LEN 16
  30. #define CCMP_HDR_LEN 8
  31. #define CCMP_MIC_LEN 8
  32. #define CCMP_TK_LEN 16
  33. #define CCMP_PN_LEN 6
  34. struct ieee80211_ccmp_data {
  35. u8 key[CCMP_TK_LEN];
  36. int key_set;
  37. u8 tx_pn[CCMP_PN_LEN];
  38. u8 rx_pn[CCMP_PN_LEN];
  39. u32 dot11RSNAStatsCCMPFormatErrors;
  40. u32 dot11RSNAStatsCCMPReplays;
  41. u32 dot11RSNAStatsCCMPDecryptErrors;
  42. int key_idx;
  43. struct crypto_cipher *tfm;
  44. /* scratch buffers for virt_to_page() (crypto API) */
  45. u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN],
  46. tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN];
  47. u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN];
  48. };
  49. static inline void ieee80211_ccmp_aes_encrypt(struct crypto_cipher *tfm,
  50. const u8 pt[16], u8 ct[16])
  51. {
  52. crypto_cipher_encrypt_one(tfm, ct, pt);
  53. }
  54. static void *ieee80211_ccmp_init(int key_idx)
  55. {
  56. struct ieee80211_ccmp_data *priv;
  57. priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
  58. if (priv == NULL)
  59. goto fail;
  60. priv->key_idx = key_idx;
  61. priv->tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
  62. if (IS_ERR(priv->tfm)) {
  63. printk(KERN_DEBUG "ieee80211_crypt_ccmp: could not allocate "
  64. "crypto API aes\n");
  65. priv->tfm = NULL;
  66. goto fail;
  67. }
  68. return priv;
  69. fail:
  70. if (priv) {
  71. if (priv->tfm)
  72. crypto_free_cipher(priv->tfm);
  73. kfree(priv);
  74. }
  75. return NULL;
  76. }
  77. static void ieee80211_ccmp_deinit(void *priv)
  78. {
  79. struct ieee80211_ccmp_data *_priv = priv;
  80. if (_priv && _priv->tfm)
  81. crypto_free_cipher(_priv->tfm);
  82. kfree(priv);
  83. }
  84. static inline void xor_block(u8 * b, u8 * a, size_t len)
  85. {
  86. int i;
  87. for (i = 0; i < len; i++)
  88. b[i] ^= a[i];
  89. }
  90. static void ccmp_init_blocks(struct crypto_cipher *tfm,
  91. struct ieee80211_hdr_4addr *hdr,
  92. u8 * pn, size_t dlen, u8 * b0, u8 * auth, u8 * s0)
  93. {
  94. u8 *pos, qc = 0;
  95. size_t aad_len;
  96. u16 fc;
  97. int a4_included, qc_included;
  98. u8 aad[2 * AES_BLOCK_LEN];
  99. fc = le16_to_cpu(hdr->frame_ctl);
  100. a4_included = ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  101. (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS));
  102. qc_included = ((WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA) &&
  103. (WLAN_FC_GET_STYPE(fc) & IEEE80211_STYPE_QOS_DATA));
  104. aad_len = 22;
  105. if (a4_included)
  106. aad_len += 6;
  107. if (qc_included) {
  108. pos = (u8 *) & hdr->addr4;
  109. if (a4_included)
  110. pos += 6;
  111. qc = *pos & 0x0f;
  112. aad_len += 2;
  113. }
  114. /* CCM Initial Block:
  115. * Flag (Include authentication header, M=3 (8-octet MIC),
  116. * L=1 (2-octet Dlen))
  117. * Nonce: 0x00 | A2 | PN
  118. * Dlen */
  119. b0[0] = 0x59;
  120. b0[1] = qc;
  121. memcpy(b0 + 2, hdr->addr2, ETH_ALEN);
  122. memcpy(b0 + 8, pn, CCMP_PN_LEN);
  123. b0[14] = (dlen >> 8) & 0xff;
  124. b0[15] = dlen & 0xff;
  125. /* AAD:
  126. * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
  127. * A1 | A2 | A3
  128. * SC with bits 4..15 (seq#) masked to zero
  129. * A4 (if present)
  130. * QC (if present)
  131. */
  132. pos = (u8 *) hdr;
  133. aad[0] = 0; /* aad_len >> 8 */
  134. aad[1] = aad_len & 0xff;
  135. aad[2] = pos[0] & 0x8f;
  136. aad[3] = pos[1] & 0xc7;
  137. memcpy(aad + 4, hdr->addr1, 3 * ETH_ALEN);
  138. pos = (u8 *) & hdr->seq_ctl;
  139. aad[22] = pos[0] & 0x0f;
  140. aad[23] = 0; /* all bits masked */
  141. memset(aad + 24, 0, 8);
  142. if (a4_included)
  143. memcpy(aad + 24, hdr->addr4, ETH_ALEN);
  144. if (qc_included) {
  145. aad[a4_included ? 30 : 24] = qc;
  146. /* rest of QC masked */
  147. }
  148. /* Start with the first block and AAD */
  149. ieee80211_ccmp_aes_encrypt(tfm, b0, auth);
  150. xor_block(auth, aad, AES_BLOCK_LEN);
  151. ieee80211_ccmp_aes_encrypt(tfm, auth, auth);
  152. xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
  153. ieee80211_ccmp_aes_encrypt(tfm, auth, auth);
  154. b0[0] &= 0x07;
  155. b0[14] = b0[15] = 0;
  156. ieee80211_ccmp_aes_encrypt(tfm, b0, s0);
  157. }
  158. static int ieee80211_ccmp_hdr(struct sk_buff *skb, int hdr_len,
  159. u8 *aeskey, int keylen, void *priv)
  160. {
  161. struct ieee80211_ccmp_data *key = priv;
  162. int i;
  163. u8 *pos;
  164. if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len)
  165. return -1;
  166. if (aeskey != NULL && keylen >= CCMP_TK_LEN)
  167. memcpy(aeskey, key->key, CCMP_TK_LEN);
  168. pos = skb_push(skb, CCMP_HDR_LEN);
  169. memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
  170. pos += hdr_len;
  171. i = CCMP_PN_LEN - 1;
  172. while (i >= 0) {
  173. key->tx_pn[i]++;
  174. if (key->tx_pn[i] != 0)
  175. break;
  176. i--;
  177. }
  178. *pos++ = key->tx_pn[5];
  179. *pos++ = key->tx_pn[4];
  180. *pos++ = 0;
  181. *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ;
  182. *pos++ = key->tx_pn[3];
  183. *pos++ = key->tx_pn[2];
  184. *pos++ = key->tx_pn[1];
  185. *pos++ = key->tx_pn[0];
  186. return CCMP_HDR_LEN;
  187. }
  188. static int ieee80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
  189. {
  190. struct ieee80211_ccmp_data *key = priv;
  191. int data_len, i, blocks, last, len;
  192. u8 *pos, *mic;
  193. struct ieee80211_hdr_4addr *hdr;
  194. u8 *b0 = key->tx_b0;
  195. u8 *b = key->tx_b;
  196. u8 *e = key->tx_e;
  197. u8 *s0 = key->tx_s0;
  198. if (skb_tailroom(skb) < CCMP_MIC_LEN || skb->len < hdr_len)
  199. return -1;
  200. data_len = skb->len - hdr_len;
  201. len = ieee80211_ccmp_hdr(skb, hdr_len, NULL, 0, priv);
  202. if (len < 0)
  203. return -1;
  204. pos = skb->data + hdr_len + CCMP_HDR_LEN;
  205. mic = skb_put(skb, CCMP_MIC_LEN);
  206. hdr = (struct ieee80211_hdr_4addr *)skb->data;
  207. ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0);
  208. blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
  209. last = data_len % AES_BLOCK_LEN;
  210. for (i = 1; i <= blocks; i++) {
  211. len = (i == blocks && last) ? last : AES_BLOCK_LEN;
  212. /* Authentication */
  213. xor_block(b, pos, len);
  214. ieee80211_ccmp_aes_encrypt(key->tfm, b, b);
  215. /* Encryption, with counter */
  216. b0[14] = (i >> 8) & 0xff;
  217. b0[15] = i & 0xff;
  218. ieee80211_ccmp_aes_encrypt(key->tfm, b0, e);
  219. xor_block(pos, e, len);
  220. pos += len;
  221. }
  222. for (i = 0; i < CCMP_MIC_LEN; i++)
  223. mic[i] = b[i] ^ s0[i];
  224. return 0;
  225. }
  226. /*
  227. * deal with seq counter wrapping correctly.
  228. * refer to timer_after() for jiffies wrapping handling
  229. */
  230. static inline int ccmp_replay_check(u8 *pn_n, u8 *pn_o)
  231. {
  232. u32 iv32_n, iv16_n;
  233. u32 iv32_o, iv16_o;
  234. iv32_n = (pn_n[0] << 24) | (pn_n[1] << 16) | (pn_n[2] << 8) | pn_n[3];
  235. iv16_n = (pn_n[4] << 8) | pn_n[5];
  236. iv32_o = (pn_o[0] << 24) | (pn_o[1] << 16) | (pn_o[2] << 8) | pn_o[3];
  237. iv16_o = (pn_o[4] << 8) | pn_o[5];
  238. if ((s32)iv32_n - (s32)iv32_o < 0 ||
  239. (iv32_n == iv32_o && iv16_n <= iv16_o))
  240. return 1;
  241. return 0;
  242. }
  243. static int ieee80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
  244. {
  245. struct ieee80211_ccmp_data *key = priv;
  246. u8 keyidx, *pos;
  247. struct ieee80211_hdr_4addr *hdr;
  248. u8 *b0 = key->rx_b0;
  249. u8 *b = key->rx_b;
  250. u8 *a = key->rx_a;
  251. u8 pn[6];
  252. int i, blocks, last, len;
  253. size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN - CCMP_MIC_LEN;
  254. u8 *mic = skb->data + skb->len - CCMP_MIC_LEN;
  255. DECLARE_MAC_BUF(mac);
  256. if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
  257. key->dot11RSNAStatsCCMPFormatErrors++;
  258. return -1;
  259. }
  260. hdr = (struct ieee80211_hdr_4addr *)skb->data;
  261. pos = skb->data + hdr_len;
  262. keyidx = pos[3];
  263. if (!(keyidx & (1 << 5))) {
  264. if (net_ratelimit()) {
  265. printk(KERN_DEBUG "CCMP: received packet without ExtIV"
  266. " flag from %s\n", print_mac(mac, hdr->addr2));
  267. }
  268. key->dot11RSNAStatsCCMPFormatErrors++;
  269. return -2;
  270. }
  271. keyidx >>= 6;
  272. if (key->key_idx != keyidx) {
  273. printk(KERN_DEBUG "CCMP: RX tkey->key_idx=%d frame "
  274. "keyidx=%d priv=%p\n", key->key_idx, keyidx, priv);
  275. return -6;
  276. }
  277. if (!key->key_set) {
  278. if (net_ratelimit()) {
  279. printk(KERN_DEBUG "CCMP: received packet from %s"
  280. " with keyid=%d that does not have a configured"
  281. " key\n", print_mac(mac, hdr->addr2), keyidx);
  282. }
  283. return -3;
  284. }
  285. pn[0] = pos[7];
  286. pn[1] = pos[6];
  287. pn[2] = pos[5];
  288. pn[3] = pos[4];
  289. pn[4] = pos[1];
  290. pn[5] = pos[0];
  291. pos += 8;
  292. if (ccmp_replay_check(pn, key->rx_pn)) {
  293. if (net_ratelimit()) {
  294. IEEE80211_DEBUG_DROP("CCMP: replay detected: STA=%s "
  295. "previous PN %02x%02x%02x%02x%02x%02x "
  296. "received PN %02x%02x%02x%02x%02x%02x\n",
  297. print_mac(mac, hdr->addr2),
  298. key->rx_pn[0], key->rx_pn[1], key->rx_pn[2],
  299. key->rx_pn[3], key->rx_pn[4], key->rx_pn[5],
  300. pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
  301. }
  302. key->dot11RSNAStatsCCMPReplays++;
  303. return -4;
  304. }
  305. ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b);
  306. xor_block(mic, b, CCMP_MIC_LEN);
  307. blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
  308. last = data_len % AES_BLOCK_LEN;
  309. for (i = 1; i <= blocks; i++) {
  310. len = (i == blocks && last) ? last : AES_BLOCK_LEN;
  311. /* Decrypt, with counter */
  312. b0[14] = (i >> 8) & 0xff;
  313. b0[15] = i & 0xff;
  314. ieee80211_ccmp_aes_encrypt(key->tfm, b0, b);
  315. xor_block(pos, b, len);
  316. /* Authentication */
  317. xor_block(a, pos, len);
  318. ieee80211_ccmp_aes_encrypt(key->tfm, a, a);
  319. pos += len;
  320. }
  321. if (memcmp(mic, a, CCMP_MIC_LEN) != 0) {
  322. if (net_ratelimit()) {
  323. printk(KERN_DEBUG "CCMP: decrypt failed: STA="
  324. "%s\n", print_mac(mac, hdr->addr2));
  325. }
  326. key->dot11RSNAStatsCCMPDecryptErrors++;
  327. return -5;
  328. }
  329. memcpy(key->rx_pn, pn, CCMP_PN_LEN);
  330. /* Remove hdr and MIC */
  331. memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
  332. skb_pull(skb, CCMP_HDR_LEN);
  333. skb_trim(skb, skb->len - CCMP_MIC_LEN);
  334. return keyidx;
  335. }
  336. static int ieee80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv)
  337. {
  338. struct ieee80211_ccmp_data *data = priv;
  339. int keyidx;
  340. struct crypto_cipher *tfm = data->tfm;
  341. keyidx = data->key_idx;
  342. memset(data, 0, sizeof(*data));
  343. data->key_idx = keyidx;
  344. data->tfm = tfm;
  345. if (len == CCMP_TK_LEN) {
  346. memcpy(data->key, key, CCMP_TK_LEN);
  347. data->key_set = 1;
  348. if (seq) {
  349. data->rx_pn[0] = seq[5];
  350. data->rx_pn[1] = seq[4];
  351. data->rx_pn[2] = seq[3];
  352. data->rx_pn[3] = seq[2];
  353. data->rx_pn[4] = seq[1];
  354. data->rx_pn[5] = seq[0];
  355. }
  356. crypto_cipher_setkey(data->tfm, data->key, CCMP_TK_LEN);
  357. } else if (len == 0)
  358. data->key_set = 0;
  359. else
  360. return -1;
  361. return 0;
  362. }
  363. static int ieee80211_ccmp_get_key(void *key, int len, u8 * seq, void *priv)
  364. {
  365. struct ieee80211_ccmp_data *data = priv;
  366. if (len < CCMP_TK_LEN)
  367. return -1;
  368. if (!data->key_set)
  369. return 0;
  370. memcpy(key, data->key, CCMP_TK_LEN);
  371. if (seq) {
  372. seq[0] = data->tx_pn[5];
  373. seq[1] = data->tx_pn[4];
  374. seq[2] = data->tx_pn[3];
  375. seq[3] = data->tx_pn[2];
  376. seq[4] = data->tx_pn[1];
  377. seq[5] = data->tx_pn[0];
  378. }
  379. return CCMP_TK_LEN;
  380. }
  381. static char *ieee80211_ccmp_print_stats(char *p, void *priv)
  382. {
  383. struct ieee80211_ccmp_data *ccmp = priv;
  384. p += sprintf(p, "key[%d] alg=CCMP key_set=%d "
  385. "tx_pn=%02x%02x%02x%02x%02x%02x "
  386. "rx_pn=%02x%02x%02x%02x%02x%02x "
  387. "format_errors=%d replays=%d decrypt_errors=%d\n",
  388. ccmp->key_idx, ccmp->key_set,
  389. ccmp->tx_pn[0], ccmp->tx_pn[1], ccmp->tx_pn[2],
  390. ccmp->tx_pn[3], ccmp->tx_pn[4], ccmp->tx_pn[5],
  391. ccmp->rx_pn[0], ccmp->rx_pn[1], ccmp->rx_pn[2],
  392. ccmp->rx_pn[3], ccmp->rx_pn[4], ccmp->rx_pn[5],
  393. ccmp->dot11RSNAStatsCCMPFormatErrors,
  394. ccmp->dot11RSNAStatsCCMPReplays,
  395. ccmp->dot11RSNAStatsCCMPDecryptErrors);
  396. return p;
  397. }
  398. static struct ieee80211_crypto_ops ieee80211_crypt_ccmp = {
  399. .name = "CCMP",
  400. .init = ieee80211_ccmp_init,
  401. .deinit = ieee80211_ccmp_deinit,
  402. .build_iv = ieee80211_ccmp_hdr,
  403. .encrypt_mpdu = ieee80211_ccmp_encrypt,
  404. .decrypt_mpdu = ieee80211_ccmp_decrypt,
  405. .encrypt_msdu = NULL,
  406. .decrypt_msdu = NULL,
  407. .set_key = ieee80211_ccmp_set_key,
  408. .get_key = ieee80211_ccmp_get_key,
  409. .print_stats = ieee80211_ccmp_print_stats,
  410. .extra_mpdu_prefix_len = CCMP_HDR_LEN,
  411. .extra_mpdu_postfix_len = CCMP_MIC_LEN,
  412. .owner = THIS_MODULE,
  413. };
  414. static int __init ieee80211_crypto_ccmp_init(void)
  415. {
  416. return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp);
  417. }
  418. static void __exit ieee80211_crypto_ccmp_exit(void)
  419. {
  420. ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp);
  421. }
  422. module_init(ieee80211_crypto_ccmp_init);
  423. module_exit(ieee80211_crypto_ccmp_exit);