ieee80211_crypt_ccmp.c 11 KB

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