hostap_crypt_ccmp.c 11 KB

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