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