ieee80211_crypt_ccmp.c 12 KB

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