lib80211_crypt_ccmp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * lib80211 crypt: host-based CCMP encryption implementation for lib80211
  4. *
  5. * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
  6. * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/err.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/random.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/if_arp.h>
  18. #include <asm/string.h>
  19. #include <linux/wireless.h>
  20. #include <linux/ieee80211.h>
  21. #include <linux/crypto.h>
  22. #include <crypto/aead.h>
  23. #include <net/lib80211.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 lib80211_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_aead *tfm;
  42. /* scratch buffers for virt_to_page() (crypto API) */
  43. u8 tx_aad[2 * AES_BLOCK_LEN];
  44. u8 rx_aad[2 * AES_BLOCK_LEN];
  45. };
  46. static void *lib80211_ccmp_init(int key_idx)
  47. {
  48. struct lib80211_ccmp_data *priv;
  49. priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
  50. if (priv == NULL)
  51. goto fail;
  52. priv->key_idx = key_idx;
  53. priv->tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
  54. if (IS_ERR(priv->tfm)) {
  55. priv->tfm = NULL;
  56. goto fail;
  57. }
  58. return priv;
  59. fail:
  60. if (priv) {
  61. if (priv->tfm)
  62. crypto_free_aead(priv->tfm);
  63. kfree(priv);
  64. }
  65. return NULL;
  66. }
  67. static void lib80211_ccmp_deinit(void *priv)
  68. {
  69. struct lib80211_ccmp_data *_priv = priv;
  70. if (_priv && _priv->tfm)
  71. crypto_free_aead(_priv->tfm);
  72. kfree(priv);
  73. }
  74. static int ccmp_init_iv_and_aad(const struct ieee80211_hdr *hdr,
  75. const u8 *pn, u8 *iv, u8 *aad)
  76. {
  77. u8 *pos, qc = 0;
  78. size_t aad_len;
  79. int a4_included, qc_included;
  80. a4_included = ieee80211_has_a4(hdr->frame_control);
  81. qc_included = ieee80211_is_data_qos(hdr->frame_control);
  82. aad_len = 22;
  83. if (a4_included)
  84. aad_len += 6;
  85. if (qc_included) {
  86. pos = (u8 *) & hdr->addr4;
  87. if (a4_included)
  88. pos += 6;
  89. qc = *pos & 0x0f;
  90. aad_len += 2;
  91. }
  92. /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
  93. * mode authentication are not allowed to collide, yet both are derived
  94. * from the same vector. We only set L := 1 here to indicate that the
  95. * data size can be represented in (L+1) bytes. The CCM layer will take
  96. * care of storing the data length in the top (L+1) bytes and setting
  97. * and clearing the other bits as is required to derive the two IVs.
  98. */
  99. iv[0] = 0x1;
  100. /* Nonce: QC | A2 | PN */
  101. iv[1] = qc;
  102. memcpy(iv + 2, hdr->addr2, ETH_ALEN);
  103. memcpy(iv + 8, pn, CCMP_PN_LEN);
  104. /* AAD:
  105. * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
  106. * A1 | A2 | A3
  107. * SC with bits 4..15 (seq#) masked to zero
  108. * A4 (if present)
  109. * QC (if present)
  110. */
  111. pos = (u8 *) hdr;
  112. aad[0] = pos[0] & 0x8f;
  113. aad[1] = pos[1] & 0xc7;
  114. memcpy(aad + 2, hdr->addr1, 3 * ETH_ALEN);
  115. pos = (u8 *) & hdr->seq_ctrl;
  116. aad[20] = pos[0] & 0x0f;
  117. aad[21] = 0; /* all bits masked */
  118. memset(aad + 22, 0, 8);
  119. if (a4_included)
  120. memcpy(aad + 22, hdr->addr4, ETH_ALEN);
  121. if (qc_included) {
  122. aad[a4_included ? 28 : 22] = qc;
  123. /* rest of QC masked */
  124. }
  125. return aad_len;
  126. }
  127. static int lib80211_ccmp_hdr(struct sk_buff *skb, int hdr_len,
  128. u8 *aeskey, int keylen, void *priv)
  129. {
  130. struct lib80211_ccmp_data *key = priv;
  131. int i;
  132. u8 *pos;
  133. if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len)
  134. return -1;
  135. if (aeskey != NULL && keylen >= CCMP_TK_LEN)
  136. memcpy(aeskey, key->key, CCMP_TK_LEN);
  137. pos = skb_push(skb, CCMP_HDR_LEN);
  138. memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
  139. pos += hdr_len;
  140. i = CCMP_PN_LEN - 1;
  141. while (i >= 0) {
  142. key->tx_pn[i]++;
  143. if (key->tx_pn[i] != 0)
  144. break;
  145. i--;
  146. }
  147. *pos++ = key->tx_pn[5];
  148. *pos++ = key->tx_pn[4];
  149. *pos++ = 0;
  150. *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ;
  151. *pos++ = key->tx_pn[3];
  152. *pos++ = key->tx_pn[2];
  153. *pos++ = key->tx_pn[1];
  154. *pos++ = key->tx_pn[0];
  155. return CCMP_HDR_LEN;
  156. }
  157. static int lib80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
  158. {
  159. struct lib80211_ccmp_data *key = priv;
  160. struct ieee80211_hdr *hdr;
  161. struct aead_request *req;
  162. struct scatterlist sg[2];
  163. u8 *aad = key->tx_aad;
  164. u8 iv[AES_BLOCK_LEN];
  165. int len, data_len, aad_len;
  166. int ret;
  167. if (skb_tailroom(skb) < CCMP_MIC_LEN || skb->len < hdr_len)
  168. return -1;
  169. data_len = skb->len - hdr_len;
  170. len = lib80211_ccmp_hdr(skb, hdr_len, NULL, 0, priv);
  171. if (len < 0)
  172. return -1;
  173. req = aead_request_alloc(key->tfm, GFP_ATOMIC);
  174. if (!req)
  175. return -ENOMEM;
  176. hdr = (struct ieee80211_hdr *)skb->data;
  177. aad_len = ccmp_init_iv_and_aad(hdr, key->tx_pn, iv, aad);
  178. skb_put(skb, CCMP_MIC_LEN);
  179. sg_init_table(sg, 2);
  180. sg_set_buf(&sg[0], aad, aad_len);
  181. sg_set_buf(&sg[1], skb->data + hdr_len + CCMP_HDR_LEN,
  182. data_len + CCMP_MIC_LEN);
  183. aead_request_set_callback(req, 0, NULL, NULL);
  184. aead_request_set_ad(req, aad_len);
  185. aead_request_set_crypt(req, sg, sg, data_len, iv);
  186. ret = crypto_aead_encrypt(req);
  187. aead_request_free(req);
  188. return ret;
  189. }
  190. /*
  191. * deal with seq counter wrapping correctly.
  192. * refer to timer_after() for jiffies wrapping handling
  193. */
  194. static inline int ccmp_replay_check(u8 *pn_n, u8 *pn_o)
  195. {
  196. u32 iv32_n, iv16_n;
  197. u32 iv32_o, iv16_o;
  198. iv32_n = (pn_n[0] << 24) | (pn_n[1] << 16) | (pn_n[2] << 8) | pn_n[3];
  199. iv16_n = (pn_n[4] << 8) | pn_n[5];
  200. iv32_o = (pn_o[0] << 24) | (pn_o[1] << 16) | (pn_o[2] << 8) | pn_o[3];
  201. iv16_o = (pn_o[4] << 8) | pn_o[5];
  202. if ((s32)iv32_n - (s32)iv32_o < 0 ||
  203. (iv32_n == iv32_o && iv16_n <= iv16_o))
  204. return 1;
  205. return 0;
  206. }
  207. static int lib80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
  208. {
  209. struct lib80211_ccmp_data *key = priv;
  210. u8 keyidx, *pos;
  211. struct ieee80211_hdr *hdr;
  212. struct aead_request *req;
  213. struct scatterlist sg[2];
  214. u8 *aad = key->rx_aad;
  215. u8 iv[AES_BLOCK_LEN];
  216. u8 pn[6];
  217. int aad_len, ret;
  218. size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN;
  219. if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
  220. key->dot11RSNAStatsCCMPFormatErrors++;
  221. return -1;
  222. }
  223. hdr = (struct ieee80211_hdr *)skb->data;
  224. pos = skb->data + hdr_len;
  225. keyidx = pos[3];
  226. if (!(keyidx & (1 << 5))) {
  227. net_dbg_ratelimited("CCMP: received packet without ExtIV flag from %pM\n",
  228. hdr->addr2);
  229. key->dot11RSNAStatsCCMPFormatErrors++;
  230. return -2;
  231. }
  232. keyidx >>= 6;
  233. if (key->key_idx != keyidx) {
  234. net_dbg_ratelimited("CCMP: RX tkey->key_idx=%d frame keyidx=%d\n",
  235. key->key_idx, keyidx);
  236. return -6;
  237. }
  238. if (!key->key_set) {
  239. net_dbg_ratelimited("CCMP: received packet from %pM with keyid=%d that does not have a configured key\n",
  240. hdr->addr2, keyidx);
  241. return -3;
  242. }
  243. pn[0] = pos[7];
  244. pn[1] = pos[6];
  245. pn[2] = pos[5];
  246. pn[3] = pos[4];
  247. pn[4] = pos[1];
  248. pn[5] = pos[0];
  249. pos += 8;
  250. if (ccmp_replay_check(pn, key->rx_pn)) {
  251. #ifdef CONFIG_LIB80211_DEBUG
  252. net_dbg_ratelimited("CCMP: replay detected: STA=%pM previous PN %02x%02x%02x%02x%02x%02x received PN %02x%02x%02x%02x%02x%02x\n",
  253. hdr->addr2,
  254. key->rx_pn[0], key->rx_pn[1], key->rx_pn[2],
  255. key->rx_pn[3], key->rx_pn[4], key->rx_pn[5],
  256. pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
  257. #endif
  258. key->dot11RSNAStatsCCMPReplays++;
  259. return -4;
  260. }
  261. req = aead_request_alloc(key->tfm, GFP_ATOMIC);
  262. if (!req)
  263. return -ENOMEM;
  264. aad_len = ccmp_init_iv_and_aad(hdr, pn, iv, aad);
  265. sg_init_table(sg, 2);
  266. sg_set_buf(&sg[0], aad, aad_len);
  267. sg_set_buf(&sg[1], pos, data_len);
  268. aead_request_set_callback(req, 0, NULL, NULL);
  269. aead_request_set_ad(req, aad_len);
  270. aead_request_set_crypt(req, sg, sg, data_len, iv);
  271. ret = crypto_aead_decrypt(req);
  272. aead_request_free(req);
  273. if (ret) {
  274. net_dbg_ratelimited("CCMP: decrypt failed: STA=%pM (%d)\n",
  275. hdr->addr2, ret);
  276. key->dot11RSNAStatsCCMPDecryptErrors++;
  277. return -5;
  278. }
  279. memcpy(key->rx_pn, pn, CCMP_PN_LEN);
  280. /* Remove hdr and MIC */
  281. memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
  282. skb_pull(skb, CCMP_HDR_LEN);
  283. skb_trim(skb, skb->len - CCMP_MIC_LEN);
  284. return keyidx;
  285. }
  286. static int lib80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv)
  287. {
  288. struct lib80211_ccmp_data *data = priv;
  289. int keyidx;
  290. struct crypto_aead *tfm = data->tfm;
  291. keyidx = data->key_idx;
  292. memset(data, 0, sizeof(*data));
  293. data->key_idx = keyidx;
  294. data->tfm = tfm;
  295. if (len == CCMP_TK_LEN) {
  296. memcpy(data->key, key, CCMP_TK_LEN);
  297. data->key_set = 1;
  298. if (seq) {
  299. data->rx_pn[0] = seq[5];
  300. data->rx_pn[1] = seq[4];
  301. data->rx_pn[2] = seq[3];
  302. data->rx_pn[3] = seq[2];
  303. data->rx_pn[4] = seq[1];
  304. data->rx_pn[5] = seq[0];
  305. }
  306. if (crypto_aead_setauthsize(data->tfm, CCMP_MIC_LEN) ||
  307. crypto_aead_setkey(data->tfm, data->key, CCMP_TK_LEN))
  308. return -1;
  309. } else if (len == 0)
  310. data->key_set = 0;
  311. else
  312. return -1;
  313. return 0;
  314. }
  315. static int lib80211_ccmp_get_key(void *key, int len, u8 * seq, void *priv)
  316. {
  317. struct lib80211_ccmp_data *data = priv;
  318. if (len < CCMP_TK_LEN)
  319. return -1;
  320. if (!data->key_set)
  321. return 0;
  322. memcpy(key, data->key, CCMP_TK_LEN);
  323. if (seq) {
  324. seq[0] = data->tx_pn[5];
  325. seq[1] = data->tx_pn[4];
  326. seq[2] = data->tx_pn[3];
  327. seq[3] = data->tx_pn[2];
  328. seq[4] = data->tx_pn[1];
  329. seq[5] = data->tx_pn[0];
  330. }
  331. return CCMP_TK_LEN;
  332. }
  333. static void lib80211_ccmp_print_stats(struct seq_file *m, void *priv)
  334. {
  335. struct lib80211_ccmp_data *ccmp = priv;
  336. seq_printf(m,
  337. "key[%d] alg=CCMP key_set=%d "
  338. "tx_pn=%02x%02x%02x%02x%02x%02x "
  339. "rx_pn=%02x%02x%02x%02x%02x%02x "
  340. "format_errors=%d replays=%d decrypt_errors=%d\n",
  341. ccmp->key_idx, ccmp->key_set,
  342. ccmp->tx_pn[0], ccmp->tx_pn[1], ccmp->tx_pn[2],
  343. ccmp->tx_pn[3], ccmp->tx_pn[4], ccmp->tx_pn[5],
  344. ccmp->rx_pn[0], ccmp->rx_pn[1], ccmp->rx_pn[2],
  345. ccmp->rx_pn[3], ccmp->rx_pn[4], ccmp->rx_pn[5],
  346. ccmp->dot11RSNAStatsCCMPFormatErrors,
  347. ccmp->dot11RSNAStatsCCMPReplays,
  348. ccmp->dot11RSNAStatsCCMPDecryptErrors);
  349. }
  350. static struct lib80211_crypto_ops lib80211_crypt_ccmp = {
  351. .name = "CCMP",
  352. .init = lib80211_ccmp_init,
  353. .deinit = lib80211_ccmp_deinit,
  354. .encrypt_mpdu = lib80211_ccmp_encrypt,
  355. .decrypt_mpdu = lib80211_ccmp_decrypt,
  356. .encrypt_msdu = NULL,
  357. .decrypt_msdu = NULL,
  358. .set_key = lib80211_ccmp_set_key,
  359. .get_key = lib80211_ccmp_get_key,
  360. .print_stats = lib80211_ccmp_print_stats,
  361. .extra_mpdu_prefix_len = CCMP_HDR_LEN,
  362. .extra_mpdu_postfix_len = CCMP_MIC_LEN,
  363. .owner = THIS_MODULE,
  364. };
  365. static int __init lib80211_crypto_ccmp_init(void)
  366. {
  367. return lib80211_register_crypto_ops(&lib80211_crypt_ccmp);
  368. }
  369. static void __exit lib80211_crypto_ccmp_exit(void)
  370. {
  371. lib80211_unregister_crypto_ops(&lib80211_crypt_ccmp);
  372. }
  373. module_init(lib80211_crypto_ccmp_init);
  374. module_exit(lib80211_crypto_ccmp_exit);