1 // SPDX-License-Identifier: GPL-2.0 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 7 #include <linux/module.h> 8 #include <linux/init.h> 9 #include <linux/slab.h> 10 #include <linux/random.h> 11 #include <linux/skbuff.h> 12 #include <linux/netdevice.h> 13 #include <linux/if_ether.h> 14 #include <linux/if_arp.h> 15 #include <linux/string.h> 16 #include <linux/wireless.h> 17 #include "rtllib.h" 18 19 #include <linux/crypto.h> 20 #include <crypto/aead.h> 21 22 #include <linux/scatterlist.h> 23 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 30 struct rtllib_ccmp_data { 31 u8 key[CCMP_TK_LEN]; 32 int key_set; 33 34 u8 tx_pn[CCMP_PN_LEN]; 35 u8 rx_pn[CCMP_PN_LEN]; 36 37 u32 dot11rsna_stats_ccmp_format_errors; 38 u32 dot11rsna_stats_ccmp_replays; 39 u32 dot11rsna_stats_ccmp_decrypt_errors; 40 41 int key_idx; 42 43 struct crypto_aead *tfm; 44 45 /* scratch buffers for virt_to_page() (crypto API) */ 46 u8 tx_aad[2 * AES_BLOCK_LEN]; 47 u8 rx_aad[2 * AES_BLOCK_LEN]; 48 }; 49 50 static void *rtllib_ccmp_init(int key_idx) 51 { 52 struct rtllib_ccmp_data *priv; 53 54 priv = kzalloc(sizeof(*priv), GFP_ATOMIC); 55 if (priv == NULL) 56 goto fail; 57 priv->key_idx = key_idx; 58 59 priv->tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC); 60 if (IS_ERR(priv->tfm)) { 61 pr_debug("Could not allocate crypto API aes\n"); 62 priv->tfm = NULL; 63 goto fail; 64 } 65 return priv; 66 67 fail: 68 if (priv) { 69 if (priv->tfm) 70 crypto_free_aead(priv->tfm); 71 kfree(priv); 72 } 73 74 return NULL; 75 } 76 77 static void rtllib_ccmp_deinit(void *priv) 78 { 79 struct rtllib_ccmp_data *_priv = priv; 80 81 if (_priv && _priv->tfm) 82 crypto_free_aead(_priv->tfm); 83 kfree(priv); 84 } 85 86 static int ccmp_init_iv_and_aad(struct rtllib_hdr_4addr *hdr, 87 u8 *pn, u8 *iv, u8 *aad) 88 { 89 u8 *pos, qc = 0; 90 size_t aad_len; 91 u16 fc; 92 int a4_included, qc_included; 93 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 98 qc_included = ((WLAN_FC_GET_TYPE(fc) == RTLLIB_FTYPE_DATA) && 99 (WLAN_FC_GET_STYPE(fc) & 0x80)); 100 aad_len = 22; 101 if (a4_included) 102 aad_len += 6; 103 if (qc_included) { 104 pos = (u8 *)&hdr->addr4; 105 if (a4_included) 106 pos += 6; 107 qc = *pos & 0x0f; 108 aad_len += 2; 109 } 110 /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC 111 * mode authentication are not allowed to collide, yet both are derived 112 * from the same vector. We only set L := 1 here to indicate that the 113 * data size can be represented in (L+1) bytes. The CCM layer will take 114 * care of storing the data length in the top (L+1) bytes and setting 115 * and clearing the other bits as is required to derive the two IVs. 116 */ 117 iv[0] = 0x1; 118 119 /* Nonce: QC | A2 | PN */ 120 iv[1] = qc; 121 memcpy(iv + 2, hdr->addr2, ETH_ALEN); 122 memcpy(iv + 8, pn, CCMP_PN_LEN); 123 124 /* AAD: 125 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one 126 * A1 | A2 | A3 127 * SC with bits 4..15 (seq#) masked to zero 128 * A4 (if present) 129 * QC (if present) 130 */ 131 pos = (u8 *)hdr; 132 aad[0] = pos[0] & 0x8f; 133 aad[1] = pos[1] & 0xc7; 134 memcpy(&aad[2], &hdr->addr1, ETH_ALEN); 135 memcpy(&aad[8], &hdr->addr2, ETH_ALEN); 136 memcpy(&aad[14], &hdr->addr3, ETH_ALEN); 137 pos = (u8 *)&hdr->seq_ctl; 138 aad[20] = pos[0] & 0x0f; 139 aad[21] = 0; /* all bits masked */ 140 memset(aad + 22, 0, 8); 141 if (a4_included) 142 memcpy(aad + 22, hdr->addr4, ETH_ALEN); 143 if (qc_included) { 144 aad[a4_included ? 28 : 22] = qc; 145 /* rest of QC masked */ 146 } 147 148 return aad_len; 149 } 150 151 static int rtllib_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv) 152 { 153 struct rtllib_ccmp_data *key = priv; 154 int i; 155 u8 *pos; 156 struct rtllib_hdr_4addr *hdr; 157 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 158 MAX_DEV_ADDR_SIZE); 159 if (skb_headroom(skb) < CCMP_HDR_LEN || 160 skb_tailroom(skb) < CCMP_MIC_LEN || 161 skb->len < hdr_len) 162 return -1; 163 164 pos = skb_push(skb, CCMP_HDR_LEN); 165 memmove(pos, pos + CCMP_HDR_LEN, hdr_len); 166 pos += hdr_len; 167 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 176 *pos++ = key->tx_pn[5]; 177 *pos++ = key->tx_pn[4]; 178 *pos++ = 0; 179 *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */; 180 *pos++ = key->tx_pn[3]; 181 *pos++ = key->tx_pn[2]; 182 *pos++ = key->tx_pn[1]; 183 *pos++ = key->tx_pn[0]; 184 185 hdr = (struct rtllib_hdr_4addr *)skb->data; 186 if (!tcb_desc->bHwSec) { 187 struct aead_request *req; 188 struct scatterlist sg[2]; 189 u8 *aad = key->tx_aad; 190 u8 iv[AES_BLOCK_LEN]; 191 int aad_len, ret; 192 int data_len = skb->len - hdr_len - CCMP_HDR_LEN; 193 194 req = aead_request_alloc(key->tfm, GFP_ATOMIC); 195 if (!req) 196 return -ENOMEM; 197 198 aad_len = ccmp_init_iv_and_aad(hdr, key->tx_pn, iv, aad); 199 200 skb_put(skb, CCMP_MIC_LEN); 201 sg_init_table(sg, 2); 202 sg_set_buf(&sg[0], aad, aad_len); 203 sg_set_buf(&sg[1], skb->data + hdr_len + CCMP_HDR_LEN, 204 data_len + CCMP_MIC_LEN); 205 206 aead_request_set_callback(req, 0, NULL, NULL); 207 aead_request_set_ad(req, aad_len); 208 aead_request_set_crypt(req, sg, sg, data_len, iv); 209 210 ret = crypto_aead_encrypt(req); 211 aead_request_free(req); 212 213 return ret; 214 } 215 216 return 0; 217 } 218 219 static int rtllib_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv) 220 { 221 struct rtllib_ccmp_data *key = priv; 222 u8 keyidx, *pos; 223 struct rtllib_hdr_4addr *hdr; 224 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 225 MAX_DEV_ADDR_SIZE); 226 u8 pn[6]; 227 228 if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) { 229 key->dot11rsna_stats_ccmp_format_errors++; 230 return -1; 231 } 232 233 hdr = (struct rtllib_hdr_4addr *)skb->data; 234 pos = skb->data + hdr_len; 235 keyidx = pos[3]; 236 if (!(keyidx & (1 << 5))) { 237 if (net_ratelimit()) { 238 pr_debug("CCMP: received packet without ExtIV flag from %pM\n", 239 hdr->addr2); 240 } 241 key->dot11rsna_stats_ccmp_format_errors++; 242 return -2; 243 } 244 keyidx >>= 6; 245 if (key->key_idx != keyidx) { 246 pr_debug("CCMP: RX tkey->key_idx=%d frame keyidx=%d priv=%p\n", 247 key->key_idx, keyidx, priv); 248 return -6; 249 } 250 if (!key->key_set) { 251 if (net_ratelimit()) { 252 pr_debug("CCMP: received packet from %pM with keyid=%d that does not have a configured key\n", 253 hdr->addr2, keyidx); 254 } 255 return -3; 256 } 257 258 pn[0] = pos[7]; 259 pn[1] = pos[6]; 260 pn[2] = pos[5]; 261 pn[3] = pos[4]; 262 pn[4] = pos[1]; 263 pn[5] = pos[0]; 264 pos += 8; 265 if (memcmp(pn, key->rx_pn, CCMP_PN_LEN) <= 0) { 266 key->dot11rsna_stats_ccmp_replays++; 267 return -4; 268 } 269 if (!tcb_desc->bHwSec) { 270 size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN; 271 struct aead_request *req; 272 struct scatterlist sg[2]; 273 u8 *aad = key->rx_aad; 274 u8 iv[AES_BLOCK_LEN]; 275 int aad_len, ret; 276 277 req = aead_request_alloc(key->tfm, GFP_ATOMIC); 278 if (!req) 279 return -ENOMEM; 280 281 aad_len = ccmp_init_iv_and_aad(hdr, pn, iv, aad); 282 283 sg_init_table(sg, 2); 284 sg_set_buf(&sg[0], aad, aad_len); 285 sg_set_buf(&sg[1], pos, data_len); 286 287 aead_request_set_callback(req, 0, NULL, NULL); 288 aead_request_set_ad(req, aad_len); 289 aead_request_set_crypt(req, sg, sg, data_len, iv); 290 291 ret = crypto_aead_decrypt(req); 292 aead_request_free(req); 293 294 if (ret) { 295 if (net_ratelimit()) { 296 pr_debug("CCMP: decrypt failed: STA= %pM\n", 297 hdr->addr2); 298 } 299 key->dot11rsna_stats_ccmp_decrypt_errors++; 300 return -5; 301 } 302 303 memcpy(key->rx_pn, pn, CCMP_PN_LEN); 304 } 305 /* Remove hdr and MIC */ 306 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len); 307 skb_pull(skb, CCMP_HDR_LEN); 308 skb_trim(skb, skb->len - CCMP_MIC_LEN); 309 310 return keyidx; 311 } 312 313 static int rtllib_ccmp_set_key(void *key, int len, u8 *seq, void *priv) 314 { 315 struct rtllib_ccmp_data *data = priv; 316 int keyidx; 317 struct crypto_aead *tfm = data->tfm; 318 319 keyidx = data->key_idx; 320 memset(data, 0, sizeof(*data)); 321 data->key_idx = keyidx; 322 data->tfm = tfm; 323 if (len == CCMP_TK_LEN) { 324 memcpy(data->key, key, CCMP_TK_LEN); 325 data->key_set = 1; 326 if (seq) { 327 data->rx_pn[0] = seq[5]; 328 data->rx_pn[1] = seq[4]; 329 data->rx_pn[2] = seq[3]; 330 data->rx_pn[3] = seq[2]; 331 data->rx_pn[4] = seq[1]; 332 data->rx_pn[5] = seq[0]; 333 } 334 if (crypto_aead_setauthsize(data->tfm, CCMP_MIC_LEN) || 335 crypto_aead_setkey(data->tfm, data->key, CCMP_TK_LEN)) 336 return -1; 337 } else if (len == 0) { 338 data->key_set = 0; 339 } else { 340 return -1; 341 } 342 343 return 0; 344 } 345 346 static int rtllib_ccmp_get_key(void *key, int len, u8 *seq, void *priv) 347 { 348 struct rtllib_ccmp_data *data = priv; 349 350 if (len < CCMP_TK_LEN) 351 return -1; 352 353 if (!data->key_set) 354 return 0; 355 memcpy(key, data->key, CCMP_TK_LEN); 356 357 if (seq) { 358 seq[0] = data->tx_pn[5]; 359 seq[1] = data->tx_pn[4]; 360 seq[2] = data->tx_pn[3]; 361 seq[3] = data->tx_pn[2]; 362 seq[4] = data->tx_pn[1]; 363 seq[5] = data->tx_pn[0]; 364 } 365 366 return CCMP_TK_LEN; 367 } 368 369 static void rtllib_ccmp_print_stats(struct seq_file *m, void *priv) 370 { 371 struct rtllib_ccmp_data *ccmp = priv; 372 373 seq_printf(m, 374 "key[%d] alg=CCMP key_set=%d tx_pn=%pM rx_pn=%pM format_errors=%d replays=%d decrypt_errors=%d\n", 375 ccmp->key_idx, ccmp->key_set, 376 ccmp->tx_pn, ccmp->rx_pn, 377 ccmp->dot11rsna_stats_ccmp_format_errors, 378 ccmp->dot11rsna_stats_ccmp_replays, 379 ccmp->dot11rsna_stats_ccmp_decrypt_errors); 380 } 381 382 static struct lib80211_crypto_ops rtllib_crypt_ccmp = { 383 .name = "R-CCMP", 384 .init = rtllib_ccmp_init, 385 .deinit = rtllib_ccmp_deinit, 386 .encrypt_mpdu = rtllib_ccmp_encrypt, 387 .decrypt_mpdu = rtllib_ccmp_decrypt, 388 .encrypt_msdu = NULL, 389 .decrypt_msdu = NULL, 390 .set_key = rtllib_ccmp_set_key, 391 .get_key = rtllib_ccmp_get_key, 392 .print_stats = rtllib_ccmp_print_stats, 393 .extra_mpdu_prefix_len = CCMP_HDR_LEN, 394 .extra_mpdu_postfix_len = CCMP_MIC_LEN, 395 .owner = THIS_MODULE, 396 }; 397 398 static int __init rtllib_crypto_ccmp_init(void) 399 { 400 return lib80211_register_crypto_ops(&rtllib_crypt_ccmp); 401 } 402 403 static void __exit rtllib_crypto_ccmp_exit(void) 404 { 405 lib80211_unregister_crypto_ops(&rtllib_crypt_ccmp); 406 } 407 408 module_init(rtllib_crypto_ccmp_init); 409 module_exit(rtllib_crypto_ccmp_exit); 410 411 MODULE_LICENSE("GPL"); 412