rtllib_crypt_ccmp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 "rtllib.h"
  22. #include <linux/crypto.h>
  23. #include <linux/scatterlist.h>
  24. #define AES_BLOCK_LEN 16
  25. #define CCMP_HDR_LEN 8
  26. #define CCMP_MIC_LEN 8
  27. #define CCMP_TK_LEN 16
  28. #define CCMP_PN_LEN 6
  29. struct rtllib_ccmp_data {
  30. u8 key[CCMP_TK_LEN];
  31. int key_set;
  32. u8 tx_pn[CCMP_PN_LEN];
  33. u8 rx_pn[CCMP_PN_LEN];
  34. u32 dot11RSNAStatsCCMPFormatErrors;
  35. u32 dot11RSNAStatsCCMPReplays;
  36. u32 dot11RSNAStatsCCMPDecryptErrors;
  37. int key_idx;
  38. struct crypto_tfm *tfm;
  39. /* scratch buffers for virt_to_page() (crypto API) */
  40. u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN],
  41. tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN];
  42. u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN];
  43. };
  44. static void rtllib_ccmp_aes_encrypt(struct crypto_tfm *tfm,
  45. const u8 pt[16], u8 ct[16])
  46. {
  47. crypto_cipher_encrypt_one((void *)tfm, ct, pt);
  48. }
  49. static void *rtllib_ccmp_init(int key_idx)
  50. {
  51. struct rtllib_ccmp_data *priv;
  52. priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
  53. if (priv == NULL)
  54. goto fail;
  55. priv->key_idx = key_idx;
  56. priv->tfm = (void *)crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
  57. if (IS_ERR(priv->tfm)) {
  58. pr_debug("Could not allocate crypto API aes\n");
  59. priv->tfm = NULL;
  60. goto fail;
  61. }
  62. return priv;
  63. fail:
  64. if (priv) {
  65. if (priv->tfm)
  66. crypto_free_cipher((void *)priv->tfm);
  67. kfree(priv);
  68. }
  69. return NULL;
  70. }
  71. static void rtllib_ccmp_deinit(void *priv)
  72. {
  73. struct rtllib_ccmp_data *_priv = priv;
  74. if (_priv && _priv->tfm)
  75. crypto_free_cipher((void *)_priv->tfm);
  76. kfree(priv);
  77. }
  78. static inline void xor_block(u8 *b, u8 *a, size_t len)
  79. {
  80. int i;
  81. for (i = 0; i < len; i++)
  82. b[i] ^= a[i];
  83. }
  84. static void ccmp_init_blocks(struct crypto_tfm *tfm,
  85. struct rtllib_hdr_4addr *hdr,
  86. u8 *pn, size_t dlen, u8 *b0, u8 *auth,
  87. u8 *s0)
  88. {
  89. u8 *pos, qc = 0;
  90. size_t aad_len;
  91. u16 fc;
  92. int a4_included, qc_included;
  93. u8 aad[2 * AES_BLOCK_LEN];
  94. fc = le16_to_cpu(hdr->frame_ctl);
  95. a4_included = ((fc & (RTLLIB_FCTL_TODS | RTLLIB_FCTL_FROMDS)) ==
  96. (RTLLIB_FCTL_TODS | RTLLIB_FCTL_FROMDS));
  97. qc_included = ((WLAN_FC_GET_TYPE(fc) == RTLLIB_FTYPE_DATA) &&
  98. (WLAN_FC_GET_STYPE(fc) & 0x80));
  99. aad_len = 22;
  100. if (a4_included)
  101. aad_len += 6;
  102. if (qc_included) {
  103. pos = (u8 *) &hdr->addr4;
  104. if (a4_included)
  105. pos += 6;
  106. qc = *pos & 0x0f;
  107. aad_len += 2;
  108. }
  109. /* CCM Initial Block:
  110. * Flag (Include authentication header, M=3 (8-octet MIC),
  111. * L=1 (2-octet Dlen))
  112. * Nonce: 0x00 | A2 | PN
  113. * Dlen
  114. */
  115. b0[0] = 0x59;
  116. b0[1] = qc;
  117. memcpy(b0 + 2, hdr->addr2, ETH_ALEN);
  118. memcpy(b0 + 8, pn, CCMP_PN_LEN);
  119. b0[14] = (dlen >> 8) & 0xff;
  120. b0[15] = dlen & 0xff;
  121. /* AAD:
  122. * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
  123. * A1 | A2 | A3
  124. * SC with bits 4..15 (seq#) masked to zero
  125. * A4 (if present)
  126. * QC (if present)
  127. */
  128. pos = (u8 *) hdr;
  129. aad[0] = 0; /* aad_len >> 8 */
  130. aad[1] = aad_len & 0xff;
  131. aad[2] = pos[0] & 0x8f;
  132. aad[3] = pos[1] & 0xc7;
  133. memcpy(aad + 4, hdr->addr1, 3 * ETH_ALEN);
  134. pos = (u8 *) &hdr->seq_ctl;
  135. aad[22] = pos[0] & 0x0f;
  136. aad[23] = 0; /* all bits masked */
  137. memset(aad + 24, 0, 8);
  138. if (a4_included)
  139. memcpy(aad + 24, hdr->addr4, ETH_ALEN);
  140. if (qc_included) {
  141. aad[a4_included ? 30 : 24] = qc;
  142. /* rest of QC masked */
  143. }
  144. /* Start with the first block and AAD */
  145. rtllib_ccmp_aes_encrypt(tfm, b0, auth);
  146. xor_block(auth, aad, AES_BLOCK_LEN);
  147. rtllib_ccmp_aes_encrypt(tfm, auth, auth);
  148. xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
  149. rtllib_ccmp_aes_encrypt(tfm, auth, auth);
  150. b0[0] &= 0x07;
  151. b0[14] = b0[15] = 0;
  152. rtllib_ccmp_aes_encrypt(tfm, b0, s0);
  153. }
  154. static int rtllib_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
  155. {
  156. struct rtllib_ccmp_data *key = priv;
  157. int data_len, i;
  158. u8 *pos;
  159. struct rtllib_hdr_4addr *hdr;
  160. struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
  161. MAX_DEV_ADDR_SIZE);
  162. if (skb_headroom(skb) < CCMP_HDR_LEN ||
  163. skb_tailroom(skb) < CCMP_MIC_LEN ||
  164. skb->len < hdr_len)
  165. return -1;
  166. data_len = skb->len - hdr_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. hdr = (struct rtllib_hdr_4addr *) skb->data;
  186. if (!tcb_desc->bHwSec) {
  187. int blocks, last, len;
  188. u8 *mic;
  189. u8 *b0 = key->tx_b0;
  190. u8 *b = key->tx_b;
  191. u8 *e = key->tx_e;
  192. u8 *s0 = key->tx_s0;
  193. mic = skb_put(skb, CCMP_MIC_LEN);
  194. ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len,
  195. b0, b, s0);
  196. blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
  197. last = data_len % AES_BLOCK_LEN;
  198. for (i = 1; i <= blocks; i++) {
  199. len = (i == blocks && last) ? last : AES_BLOCK_LEN;
  200. /* Authentication */
  201. xor_block(b, pos, len);
  202. rtllib_ccmp_aes_encrypt(key->tfm, b, b);
  203. /* Encryption, with counter */
  204. b0[14] = (i >> 8) & 0xff;
  205. b0[15] = i & 0xff;
  206. rtllib_ccmp_aes_encrypt(key->tfm, b0, e);
  207. xor_block(pos, e, len);
  208. pos += len;
  209. }
  210. for (i = 0; i < CCMP_MIC_LEN; i++)
  211. mic[i] = b[i] ^ s0[i];
  212. }
  213. return 0;
  214. }
  215. static int rtllib_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
  216. {
  217. struct rtllib_ccmp_data *key = priv;
  218. u8 keyidx, *pos;
  219. struct rtllib_hdr_4addr *hdr;
  220. struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
  221. MAX_DEV_ADDR_SIZE);
  222. u8 pn[6];
  223. if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
  224. key->dot11RSNAStatsCCMPFormatErrors++;
  225. return -1;
  226. }
  227. hdr = (struct rtllib_hdr_4addr *) skb->data;
  228. pos = skb->data + hdr_len;
  229. keyidx = pos[3];
  230. if (!(keyidx & (1 << 5))) {
  231. if (net_ratelimit()) {
  232. pr_debug("CCMP: received packet without ExtIV flag from %pM\n",
  233. hdr->addr2);
  234. }
  235. key->dot11RSNAStatsCCMPFormatErrors++;
  236. return -2;
  237. }
  238. keyidx >>= 6;
  239. if (key->key_idx != keyidx) {
  240. pr_debug("CCMP: RX tkey->key_idx=%d frame keyidx=%d priv=%p\n",
  241. key->key_idx, keyidx, priv);
  242. return -6;
  243. }
  244. if (!key->key_set) {
  245. if (net_ratelimit()) {
  246. pr_debug("CCMP: received packet from %pM with keyid=%d that does not have a configured key\n",
  247. hdr->addr2, keyidx);
  248. }
  249. return -3;
  250. }
  251. pn[0] = pos[7];
  252. pn[1] = pos[6];
  253. pn[2] = pos[5];
  254. pn[3] = pos[4];
  255. pn[4] = pos[1];
  256. pn[5] = pos[0];
  257. pos += 8;
  258. if (memcmp(pn, key->rx_pn, CCMP_PN_LEN) <= 0) {
  259. key->dot11RSNAStatsCCMPReplays++;
  260. return -4;
  261. }
  262. if (!tcb_desc->bHwSec) {
  263. size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN -
  264. CCMP_MIC_LEN;
  265. u8 *mic = skb->data + skb->len - CCMP_MIC_LEN;
  266. u8 *b0 = key->rx_b0;
  267. u8 *b = key->rx_b;
  268. u8 *a = key->rx_a;
  269. int i, blocks, last, len;
  270. ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b);
  271. xor_block(mic, b, CCMP_MIC_LEN);
  272. blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
  273. last = data_len % AES_BLOCK_LEN;
  274. for (i = 1; i <= blocks; i++) {
  275. len = (i == blocks && last) ? last : AES_BLOCK_LEN;
  276. /* Decrypt, with counter */
  277. b0[14] = (i >> 8) & 0xff;
  278. b0[15] = i & 0xff;
  279. rtllib_ccmp_aes_encrypt(key->tfm, b0, b);
  280. xor_block(pos, b, len);
  281. /* Authentication */
  282. xor_block(a, pos, len);
  283. rtllib_ccmp_aes_encrypt(key->tfm, a, a);
  284. pos += len;
  285. }
  286. if (memcmp(mic, a, CCMP_MIC_LEN) != 0) {
  287. if (net_ratelimit()) {
  288. pr_debug("CCMP: decrypt failed: STA= %pM\n",
  289. hdr->addr2);
  290. }
  291. key->dot11RSNAStatsCCMPDecryptErrors++;
  292. return -5;
  293. }
  294. memcpy(key->rx_pn, pn, CCMP_PN_LEN);
  295. }
  296. /* Remove hdr and MIC */
  297. memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
  298. skb_pull(skb, CCMP_HDR_LEN);
  299. skb_trim(skb, skb->len - CCMP_MIC_LEN);
  300. return keyidx;
  301. }
  302. static int rtllib_ccmp_set_key(void *key, int len, u8 *seq, void *priv)
  303. {
  304. struct rtllib_ccmp_data *data = priv;
  305. int keyidx;
  306. struct crypto_tfm *tfm = data->tfm;
  307. keyidx = data->key_idx;
  308. memset(data, 0, sizeof(*data));
  309. data->key_idx = keyidx;
  310. data->tfm = tfm;
  311. if (len == CCMP_TK_LEN) {
  312. memcpy(data->key, key, CCMP_TK_LEN);
  313. data->key_set = 1;
  314. if (seq) {
  315. data->rx_pn[0] = seq[5];
  316. data->rx_pn[1] = seq[4];
  317. data->rx_pn[2] = seq[3];
  318. data->rx_pn[3] = seq[2];
  319. data->rx_pn[4] = seq[1];
  320. data->rx_pn[5] = seq[0];
  321. }
  322. crypto_cipher_setkey((void *)data->tfm, data->key, CCMP_TK_LEN);
  323. } else if (len == 0)
  324. data->key_set = 0;
  325. else
  326. return -1;
  327. return 0;
  328. }
  329. static int rtllib_ccmp_get_key(void *key, int len, u8 *seq, void *priv)
  330. {
  331. struct rtllib_ccmp_data *data = priv;
  332. if (len < CCMP_TK_LEN)
  333. return -1;
  334. if (!data->key_set)
  335. return 0;
  336. memcpy(key, data->key, CCMP_TK_LEN);
  337. if (seq) {
  338. seq[0] = data->tx_pn[5];
  339. seq[1] = data->tx_pn[4];
  340. seq[2] = data->tx_pn[3];
  341. seq[3] = data->tx_pn[2];
  342. seq[4] = data->tx_pn[1];
  343. seq[5] = data->tx_pn[0];
  344. }
  345. return CCMP_TK_LEN;
  346. }
  347. static void rtllib_ccmp_print_stats(struct seq_file *m, void *priv)
  348. {
  349. struct rtllib_ccmp_data *ccmp = priv;
  350. seq_printf(m,
  351. "key[%d] alg=CCMP key_set=%d tx_pn=%pM rx_pn=%pM format_errors=%d replays=%d decrypt_errors=%d\n",
  352. ccmp->key_idx, ccmp->key_set,
  353. ccmp->tx_pn, ccmp->rx_pn,
  354. ccmp->dot11RSNAStatsCCMPFormatErrors,
  355. ccmp->dot11RSNAStatsCCMPReplays,
  356. ccmp->dot11RSNAStatsCCMPDecryptErrors);
  357. }
  358. static struct lib80211_crypto_ops rtllib_crypt_ccmp = {
  359. .name = "R-CCMP",
  360. .init = rtllib_ccmp_init,
  361. .deinit = rtllib_ccmp_deinit,
  362. .encrypt_mpdu = rtllib_ccmp_encrypt,
  363. .decrypt_mpdu = rtllib_ccmp_decrypt,
  364. .encrypt_msdu = NULL,
  365. .decrypt_msdu = NULL,
  366. .set_key = rtllib_ccmp_set_key,
  367. .get_key = rtllib_ccmp_get_key,
  368. .print_stats = rtllib_ccmp_print_stats,
  369. .extra_mpdu_prefix_len = CCMP_HDR_LEN,
  370. .extra_mpdu_postfix_len = CCMP_MIC_LEN,
  371. .owner = THIS_MODULE,
  372. };
  373. static int __init rtllib_crypto_ccmp_init(void)
  374. {
  375. return lib80211_register_crypto_ops(&rtllib_crypt_ccmp);
  376. }
  377. static void __exit rtllib_crypto_ccmp_exit(void)
  378. {
  379. lib80211_unregister_crypto_ops(&rtllib_crypt_ccmp);
  380. }
  381. module_init(rtllib_crypto_ccmp_init);
  382. module_exit(rtllib_crypto_ccmp_exit);
  383. MODULE_LICENSE("GPL");