ieee80211_crypt_ccmp.c 11 KB

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