lib80211_crypt_ccmp.c 12 KB

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