ieee80211_crypt_ccmp.c 12 KB

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