Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * lib80211 crypt: host-based CCMP encryption implementation for lib80211
0004  *
0005  * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
0006  * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/err.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/slab.h>
0014 #include <linux/random.h>
0015 #include <linux/skbuff.h>
0016 #include <linux/netdevice.h>
0017 #include <linux/if_ether.h>
0018 #include <linux/if_arp.h>
0019 #include <asm/string.h>
0020 #include <linux/wireless.h>
0021 
0022 #include <linux/ieee80211.h>
0023 
0024 #include <linux/crypto.h>
0025 #include <crypto/aead.h>
0026 
0027 #include <net/lib80211.h>
0028 
0029 MODULE_AUTHOR("Jouni Malinen");
0030 MODULE_DESCRIPTION("Host AP crypt: CCMP");
0031 MODULE_LICENSE("GPL");
0032 
0033 #define AES_BLOCK_LEN 16
0034 #define CCMP_HDR_LEN 8
0035 #define CCMP_MIC_LEN 8
0036 #define CCMP_TK_LEN 16
0037 #define CCMP_PN_LEN 6
0038 
0039 struct lib80211_ccmp_data {
0040     u8 key[CCMP_TK_LEN];
0041     int key_set;
0042 
0043     u8 tx_pn[CCMP_PN_LEN];
0044     u8 rx_pn[CCMP_PN_LEN];
0045 
0046     u32 dot11RSNAStatsCCMPFormatErrors;
0047     u32 dot11RSNAStatsCCMPReplays;
0048     u32 dot11RSNAStatsCCMPDecryptErrors;
0049 
0050     int key_idx;
0051 
0052     struct crypto_aead *tfm;
0053 
0054     /* scratch buffers for virt_to_page() (crypto API) */
0055     u8 tx_aad[2 * AES_BLOCK_LEN];
0056     u8 rx_aad[2 * AES_BLOCK_LEN];
0057 };
0058 
0059 static void *lib80211_ccmp_init(int key_idx)
0060 {
0061     struct lib80211_ccmp_data *priv;
0062 
0063     priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
0064     if (priv == NULL)
0065         goto fail;
0066     priv->key_idx = key_idx;
0067 
0068     priv->tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
0069     if (IS_ERR(priv->tfm)) {
0070         priv->tfm = NULL;
0071         goto fail;
0072     }
0073 
0074     return priv;
0075 
0076       fail:
0077     if (priv) {
0078         if (priv->tfm)
0079             crypto_free_aead(priv->tfm);
0080         kfree(priv);
0081     }
0082 
0083     return NULL;
0084 }
0085 
0086 static void lib80211_ccmp_deinit(void *priv)
0087 {
0088     struct lib80211_ccmp_data *_priv = priv;
0089     if (_priv && _priv->tfm)
0090         crypto_free_aead(_priv->tfm);
0091     kfree(priv);
0092 }
0093 
0094 static int ccmp_init_iv_and_aad(const struct ieee80211_hdr *hdr,
0095                 const u8 *pn, u8 *iv, u8 *aad)
0096 {
0097     u8 *pos, qc = 0;
0098     size_t aad_len;
0099     int a4_included, qc_included;
0100 
0101     a4_included = ieee80211_has_a4(hdr->frame_control);
0102     qc_included = ieee80211_is_data_qos(hdr->frame_control);
0103 
0104     aad_len = 22;
0105     if (a4_included)
0106         aad_len += 6;
0107     if (qc_included) {
0108         pos = (u8 *) & hdr->addr4;
0109         if (a4_included)
0110             pos += 6;
0111         qc = *pos & 0x0f;
0112         aad_len += 2;
0113     }
0114 
0115     /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
0116      * mode authentication are not allowed to collide, yet both are derived
0117      * from the same vector. We only set L := 1 here to indicate that the
0118      * data size can be represented in (L+1) bytes. The CCM layer will take
0119      * care of storing the data length in the top (L+1) bytes and setting
0120      * and clearing the other bits as is required to derive the two IVs.
0121      */
0122     iv[0] = 0x1;
0123 
0124     /* Nonce: QC | A2 | PN */
0125     iv[1] = qc;
0126     memcpy(iv + 2, hdr->addr2, ETH_ALEN);
0127     memcpy(iv + 8, pn, CCMP_PN_LEN);
0128 
0129     /* AAD:
0130      * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
0131      * A1 | A2 | A3
0132      * SC with bits 4..15 (seq#) masked to zero
0133      * A4 (if present)
0134      * QC (if present)
0135      */
0136     pos = (u8 *) hdr;
0137     aad[0] = pos[0] & 0x8f;
0138     aad[1] = pos[1] & 0xc7;
0139     memcpy(aad + 2, &hdr->addrs, 3 * ETH_ALEN);
0140     pos = (u8 *) & hdr->seq_ctrl;
0141     aad[20] = pos[0] & 0x0f;
0142     aad[21] = 0;        /* all bits masked */
0143     memset(aad + 22, 0, 8);
0144     if (a4_included)
0145         memcpy(aad + 22, hdr->addr4, ETH_ALEN);
0146     if (qc_included) {
0147         aad[a4_included ? 28 : 22] = qc;
0148         /* rest of QC masked */
0149     }
0150     return aad_len;
0151 }
0152 
0153 static int lib80211_ccmp_hdr(struct sk_buff *skb, int hdr_len,
0154                   u8 *aeskey, int keylen, void *priv)
0155 {
0156     struct lib80211_ccmp_data *key = priv;
0157     int i;
0158     u8 *pos;
0159 
0160     if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len)
0161         return -1;
0162 
0163     if (aeskey != NULL && keylen >= CCMP_TK_LEN)
0164         memcpy(aeskey, key->key, CCMP_TK_LEN);
0165 
0166     pos = skb_push(skb, CCMP_HDR_LEN);
0167     memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
0168     pos += hdr_len;
0169 
0170     i = CCMP_PN_LEN - 1;
0171     while (i >= 0) {
0172         key->tx_pn[i]++;
0173         if (key->tx_pn[i] != 0)
0174             break;
0175         i--;
0176     }
0177 
0178     *pos++ = key->tx_pn[5];
0179     *pos++ = key->tx_pn[4];
0180     *pos++ = 0;
0181     *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ;
0182     *pos++ = key->tx_pn[3];
0183     *pos++ = key->tx_pn[2];
0184     *pos++ = key->tx_pn[1];
0185     *pos++ = key->tx_pn[0];
0186 
0187     return CCMP_HDR_LEN;
0188 }
0189 
0190 static int lib80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
0191 {
0192     struct lib80211_ccmp_data *key = priv;
0193     struct ieee80211_hdr *hdr;
0194     struct aead_request *req;
0195     struct scatterlist sg[2];
0196     u8 *aad = key->tx_aad;
0197     u8 iv[AES_BLOCK_LEN];
0198     int len, data_len, aad_len;
0199     int ret;
0200 
0201     if (skb_tailroom(skb) < CCMP_MIC_LEN || skb->len < hdr_len)
0202         return -1;
0203 
0204     data_len = skb->len - hdr_len;
0205     len = lib80211_ccmp_hdr(skb, hdr_len, NULL, 0, priv);
0206     if (len < 0)
0207         return -1;
0208 
0209     req = aead_request_alloc(key->tfm, GFP_ATOMIC);
0210     if (!req)
0211         return -ENOMEM;
0212 
0213     hdr = (struct ieee80211_hdr *)skb->data;
0214     aad_len = ccmp_init_iv_and_aad(hdr, key->tx_pn, iv, aad);
0215 
0216     skb_put(skb, CCMP_MIC_LEN);
0217 
0218     sg_init_table(sg, 2);
0219     sg_set_buf(&sg[0], aad, aad_len);
0220     sg_set_buf(&sg[1], skb->data + hdr_len + CCMP_HDR_LEN,
0221            data_len + CCMP_MIC_LEN);
0222 
0223     aead_request_set_callback(req, 0, NULL, NULL);
0224     aead_request_set_ad(req, aad_len);
0225     aead_request_set_crypt(req, sg, sg, data_len, iv);
0226 
0227     ret = crypto_aead_encrypt(req);
0228     aead_request_free(req);
0229 
0230     return ret;
0231 }
0232 
0233 /*
0234  * deal with seq counter wrapping correctly.
0235  * refer to timer_after() for jiffies wrapping handling
0236  */
0237 static inline int ccmp_replay_check(u8 *pn_n, u8 *pn_o)
0238 {
0239     u32 iv32_n, iv16_n;
0240     u32 iv32_o, iv16_o;
0241 
0242     iv32_n = (pn_n[0] << 24) | (pn_n[1] << 16) | (pn_n[2] << 8) | pn_n[3];
0243     iv16_n = (pn_n[4] << 8) | pn_n[5];
0244 
0245     iv32_o = (pn_o[0] << 24) | (pn_o[1] << 16) | (pn_o[2] << 8) | pn_o[3];
0246     iv16_o = (pn_o[4] << 8) | pn_o[5];
0247 
0248     if ((s32)iv32_n - (s32)iv32_o < 0 ||
0249         (iv32_n == iv32_o && iv16_n <= iv16_o))
0250         return 1;
0251     return 0;
0252 }
0253 
0254 static int lib80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
0255 {
0256     struct lib80211_ccmp_data *key = priv;
0257     u8 keyidx, *pos;
0258     struct ieee80211_hdr *hdr;
0259     struct aead_request *req;
0260     struct scatterlist sg[2];
0261     u8 *aad = key->rx_aad;
0262     u8 iv[AES_BLOCK_LEN];
0263     u8 pn[6];
0264     int aad_len, ret;
0265     size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN;
0266 
0267     if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
0268         key->dot11RSNAStatsCCMPFormatErrors++;
0269         return -1;
0270     }
0271 
0272     hdr = (struct ieee80211_hdr *)skb->data;
0273     pos = skb->data + hdr_len;
0274     keyidx = pos[3];
0275     if (!(keyidx & (1 << 5))) {
0276         net_dbg_ratelimited("CCMP: received packet without ExtIV flag from %pM\n",
0277                     hdr->addr2);
0278         key->dot11RSNAStatsCCMPFormatErrors++;
0279         return -2;
0280     }
0281     keyidx >>= 6;
0282     if (key->key_idx != keyidx) {
0283         net_dbg_ratelimited("CCMP: RX tkey->key_idx=%d frame keyidx=%d\n",
0284                     key->key_idx, keyidx);
0285         return -6;
0286     }
0287     if (!key->key_set) {
0288         net_dbg_ratelimited("CCMP: received packet from %pM with keyid=%d that does not have a configured key\n",
0289                     hdr->addr2, keyidx);
0290         return -3;
0291     }
0292 
0293     pn[0] = pos[7];
0294     pn[1] = pos[6];
0295     pn[2] = pos[5];
0296     pn[3] = pos[4];
0297     pn[4] = pos[1];
0298     pn[5] = pos[0];
0299     pos += 8;
0300 
0301     if (ccmp_replay_check(pn, key->rx_pn)) {
0302 #ifdef CONFIG_LIB80211_DEBUG
0303         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",
0304                     hdr->addr2,
0305                     key->rx_pn[0], key->rx_pn[1], key->rx_pn[2],
0306                     key->rx_pn[3], key->rx_pn[4], key->rx_pn[5],
0307                     pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
0308 #endif
0309         key->dot11RSNAStatsCCMPReplays++;
0310         return -4;
0311     }
0312 
0313     req = aead_request_alloc(key->tfm, GFP_ATOMIC);
0314     if (!req)
0315         return -ENOMEM;
0316 
0317     aad_len = ccmp_init_iv_and_aad(hdr, pn, iv, aad);
0318 
0319     sg_init_table(sg, 2);
0320     sg_set_buf(&sg[0], aad, aad_len);
0321     sg_set_buf(&sg[1], pos, data_len);
0322 
0323     aead_request_set_callback(req, 0, NULL, NULL);
0324     aead_request_set_ad(req, aad_len);
0325     aead_request_set_crypt(req, sg, sg, data_len, iv);
0326 
0327     ret = crypto_aead_decrypt(req);
0328     aead_request_free(req);
0329 
0330     if (ret) {
0331         net_dbg_ratelimited("CCMP: decrypt failed: STA=%pM (%d)\n",
0332                     hdr->addr2, ret);
0333         key->dot11RSNAStatsCCMPDecryptErrors++;
0334         return -5;
0335     }
0336 
0337     memcpy(key->rx_pn, pn, CCMP_PN_LEN);
0338 
0339     /* Remove hdr and MIC */
0340     memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
0341     skb_pull(skb, CCMP_HDR_LEN);
0342     skb_trim(skb, skb->len - CCMP_MIC_LEN);
0343 
0344     return keyidx;
0345 }
0346 
0347 static int lib80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv)
0348 {
0349     struct lib80211_ccmp_data *data = priv;
0350     int keyidx;
0351     struct crypto_aead *tfm = data->tfm;
0352 
0353     keyidx = data->key_idx;
0354     memset(data, 0, sizeof(*data));
0355     data->key_idx = keyidx;
0356     data->tfm = tfm;
0357     if (len == CCMP_TK_LEN) {
0358         memcpy(data->key, key, CCMP_TK_LEN);
0359         data->key_set = 1;
0360         if (seq) {
0361             data->rx_pn[0] = seq[5];
0362             data->rx_pn[1] = seq[4];
0363             data->rx_pn[2] = seq[3];
0364             data->rx_pn[3] = seq[2];
0365             data->rx_pn[4] = seq[1];
0366             data->rx_pn[5] = seq[0];
0367         }
0368         if (crypto_aead_setauthsize(data->tfm, CCMP_MIC_LEN) ||
0369             crypto_aead_setkey(data->tfm, data->key, CCMP_TK_LEN))
0370             return -1;
0371     } else if (len == 0)
0372         data->key_set = 0;
0373     else
0374         return -1;
0375 
0376     return 0;
0377 }
0378 
0379 static int lib80211_ccmp_get_key(void *key, int len, u8 * seq, void *priv)
0380 {
0381     struct lib80211_ccmp_data *data = priv;
0382 
0383     if (len < CCMP_TK_LEN)
0384         return -1;
0385 
0386     if (!data->key_set)
0387         return 0;
0388     memcpy(key, data->key, CCMP_TK_LEN);
0389 
0390     if (seq) {
0391         seq[0] = data->tx_pn[5];
0392         seq[1] = data->tx_pn[4];
0393         seq[2] = data->tx_pn[3];
0394         seq[3] = data->tx_pn[2];
0395         seq[4] = data->tx_pn[1];
0396         seq[5] = data->tx_pn[0];
0397     }
0398 
0399     return CCMP_TK_LEN;
0400 }
0401 
0402 static void lib80211_ccmp_print_stats(struct seq_file *m, void *priv)
0403 {
0404     struct lib80211_ccmp_data *ccmp = priv;
0405 
0406     seq_printf(m,
0407            "key[%d] alg=CCMP key_set=%d "
0408            "tx_pn=%02x%02x%02x%02x%02x%02x "
0409            "rx_pn=%02x%02x%02x%02x%02x%02x "
0410            "format_errors=%d replays=%d decrypt_errors=%d\n",
0411            ccmp->key_idx, ccmp->key_set,
0412            ccmp->tx_pn[0], ccmp->tx_pn[1], ccmp->tx_pn[2],
0413            ccmp->tx_pn[3], ccmp->tx_pn[4], ccmp->tx_pn[5],
0414            ccmp->rx_pn[0], ccmp->rx_pn[1], ccmp->rx_pn[2],
0415            ccmp->rx_pn[3], ccmp->rx_pn[4], ccmp->rx_pn[5],
0416            ccmp->dot11RSNAStatsCCMPFormatErrors,
0417            ccmp->dot11RSNAStatsCCMPReplays,
0418            ccmp->dot11RSNAStatsCCMPDecryptErrors);
0419 }
0420 
0421 static struct lib80211_crypto_ops lib80211_crypt_ccmp = {
0422     .name = "CCMP",
0423     .init = lib80211_ccmp_init,
0424     .deinit = lib80211_ccmp_deinit,
0425     .encrypt_mpdu = lib80211_ccmp_encrypt,
0426     .decrypt_mpdu = lib80211_ccmp_decrypt,
0427     .encrypt_msdu = NULL,
0428     .decrypt_msdu = NULL,
0429     .set_key = lib80211_ccmp_set_key,
0430     .get_key = lib80211_ccmp_get_key,
0431     .print_stats = lib80211_ccmp_print_stats,
0432     .extra_mpdu_prefix_len = CCMP_HDR_LEN,
0433     .extra_mpdu_postfix_len = CCMP_MIC_LEN,
0434     .owner = THIS_MODULE,
0435 };
0436 
0437 static int __init lib80211_crypto_ccmp_init(void)
0438 {
0439     return lib80211_register_crypto_ops(&lib80211_crypt_ccmp);
0440 }
0441 
0442 static void __exit lib80211_crypto_ccmp_exit(void)
0443 {
0444     lib80211_unregister_crypto_ops(&lib80211_crypt_ccmp);
0445 }
0446 
0447 module_init(lib80211_crypto_ccmp_init);
0448 module_exit(lib80211_crypto_ccmp_exit);