ieee80211_crypt_ccmp.c 11 KB

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