Lines Matching +full:mic +full:- +full:pos

1 // SPDX-License-Identifier: GPL-2.0-only
3 * lib80211 crypt: host-based TKIP encryption implementation for lib80211
5 * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
79 unsigned long old_flags = _priv->flags; in lib80211_tkip_set_flags()
80 _priv->flags = flags; in lib80211_tkip_set_flags()
87 return _priv->flags; in lib80211_tkip_get_flags()
101 priv->key_idx = key_idx; in lib80211_tkip_init()
103 priv->tx_tfm_michael = crypto_alloc_shash("michael_mic", 0, 0); in lib80211_tkip_init()
104 if (IS_ERR(priv->tx_tfm_michael)) { in lib80211_tkip_init()
105 priv->tx_tfm_michael = NULL; in lib80211_tkip_init()
109 priv->rx_tfm_michael = crypto_alloc_shash("michael_mic", 0, 0); in lib80211_tkip_init()
110 if (IS_ERR(priv->rx_tfm_michael)) { in lib80211_tkip_init()
111 priv->rx_tfm_michael = NULL; in lib80211_tkip_init()
119 crypto_free_shash(priv->tx_tfm_michael); in lib80211_tkip_init()
120 crypto_free_shash(priv->rx_tfm_michael); in lib80211_tkip_init()
131 crypto_free_shash(_priv->tx_tfm_michael); in lib80211_tkip_deinit()
132 crypto_free_shash(_priv->rx_tfm_michael); in lib80211_tkip_deinit()
220 /* Initialize the 80-bit TTAK from TSC (IV32) and TA[0..5] */ in tkip_mixing_phase1()
244 /* Step 1 - make copy of TTAK and bring in TSC */ in tkip_mixing_phase2()
252 /* Step 2 - 96-bit bijective mixing using S-box */ in tkip_mixing_phase2()
267 /* Step 3 - bring in last of TK bits, assign 24-bit WEP IV value in tkip_mixing_phase2()
287 u8 *pos; in lib80211_tkip_hdr() local
290 hdr = (struct ieee80211_hdr *)skb->data; in lib80211_tkip_hdr()
292 if (skb_headroom(skb) < TKIP_HDR_LEN || skb->len < hdr_len) in lib80211_tkip_hdr()
293 return -1; in lib80211_tkip_hdr()
296 return -1; in lib80211_tkip_hdr()
298 if (!tkey->tx_phase1_done) { in lib80211_tkip_hdr()
299 tkip_mixing_phase1(tkey->tx_ttak, tkey->key, hdr->addr2, in lib80211_tkip_hdr()
300 tkey->tx_iv32); in lib80211_tkip_hdr()
301 tkey->tx_phase1_done = 1; in lib80211_tkip_hdr()
303 tkip_mixing_phase2(rc4key, tkey->key, tkey->tx_ttak, tkey->tx_iv16); in lib80211_tkip_hdr()
305 pos = skb_push(skb, TKIP_HDR_LEN); in lib80211_tkip_hdr()
306 memmove(pos, pos + TKIP_HDR_LEN, hdr_len); in lib80211_tkip_hdr()
307 pos += hdr_len; in lib80211_tkip_hdr()
309 *pos++ = *rc4key; in lib80211_tkip_hdr()
310 *pos++ = *(rc4key + 1); in lib80211_tkip_hdr()
311 *pos++ = *(rc4key + 2); in lib80211_tkip_hdr()
312 *pos++ = (tkey->key_idx << 6) | (1 << 5) /* Ext IV included */ ; in lib80211_tkip_hdr()
313 *pos++ = tkey->tx_iv32 & 0xff; in lib80211_tkip_hdr()
314 *pos++ = (tkey->tx_iv32 >> 8) & 0xff; in lib80211_tkip_hdr()
315 *pos++ = (tkey->tx_iv32 >> 16) & 0xff; in lib80211_tkip_hdr()
316 *pos++ = (tkey->tx_iv32 >> 24) & 0xff; in lib80211_tkip_hdr()
318 tkey->tx_iv16++; in lib80211_tkip_hdr()
319 if (tkey->tx_iv16 == 0) { in lib80211_tkip_hdr()
320 tkey->tx_phase1_done = 0; in lib80211_tkip_hdr()
321 tkey->tx_iv32++; in lib80211_tkip_hdr()
331 u8 rc4key[16], *pos, *icv; in lib80211_tkip_encrypt() local
334 if (tkey->flags & IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) { in lib80211_tkip_encrypt()
335 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; in lib80211_tkip_encrypt()
337 hdr->addr1); in lib80211_tkip_encrypt()
338 return -1; in lib80211_tkip_encrypt()
341 if (skb_tailroom(skb) < 4 || skb->len < hdr_len) in lib80211_tkip_encrypt()
342 return -1; in lib80211_tkip_encrypt()
344 len = skb->len - hdr_len; in lib80211_tkip_encrypt()
345 pos = skb->data + hdr_len; in lib80211_tkip_encrypt()
348 return -1; in lib80211_tkip_encrypt()
350 crc = ~crc32_le(~0, pos, len); in lib80211_tkip_encrypt()
357 arc4_setkey(&tkey->tx_ctx_arc4, rc4key, 16); in lib80211_tkip_encrypt()
358 arc4_crypt(&tkey->tx_ctx_arc4, pos, pos, len + 4); in lib80211_tkip_encrypt()
370 if ((s32)iv32_n - (s32)iv32_o < 0 || in tkip_replay_check()
380 u8 keyidx, *pos; in lib80211_tkip_decrypt() local
388 hdr = (struct ieee80211_hdr *)skb->data; in lib80211_tkip_decrypt()
390 if (tkey->flags & IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) { in lib80211_tkip_decrypt()
392 hdr->addr2); in lib80211_tkip_decrypt()
393 return -1; in lib80211_tkip_decrypt()
396 if (skb->len < hdr_len + TKIP_HDR_LEN + 4) in lib80211_tkip_decrypt()
397 return -1; in lib80211_tkip_decrypt()
399 pos = skb->data + hdr_len; in lib80211_tkip_decrypt()
400 keyidx = pos[3]; in lib80211_tkip_decrypt()
403 hdr->addr2); in lib80211_tkip_decrypt()
404 return -2; in lib80211_tkip_decrypt()
407 if (tkey->key_idx != keyidx) { in lib80211_tkip_decrypt()
408 net_dbg_ratelimited("TKIP: RX tkey->key_idx=%d frame keyidx=%d\n", in lib80211_tkip_decrypt()
409 tkey->key_idx, keyidx); in lib80211_tkip_decrypt()
410 return -6; in lib80211_tkip_decrypt()
412 if (!tkey->key_set) { in lib80211_tkip_decrypt()
414 hdr->addr2, keyidx); in lib80211_tkip_decrypt()
415 return -3; in lib80211_tkip_decrypt()
417 iv16 = (pos[0] << 8) | pos[2]; in lib80211_tkip_decrypt()
418 iv32 = pos[4] | (pos[5] << 8) | (pos[6] << 16) | (pos[7] << 24); in lib80211_tkip_decrypt()
419 pos += TKIP_HDR_LEN; in lib80211_tkip_decrypt()
421 if (tkip_replay_check(iv32, iv16, tkey->rx_iv32, tkey->rx_iv16)) { in lib80211_tkip_decrypt()
424 hdr->addr2, tkey->rx_iv32, tkey->rx_iv16, in lib80211_tkip_decrypt()
427 tkey->dot11RSNAStatsTKIPReplays++; in lib80211_tkip_decrypt()
428 return -4; in lib80211_tkip_decrypt()
431 if (iv32 != tkey->rx_iv32 || !tkey->rx_phase1_done) { in lib80211_tkip_decrypt()
432 tkip_mixing_phase1(tkey->rx_ttak, tkey->key, hdr->addr2, iv32); in lib80211_tkip_decrypt()
433 tkey->rx_phase1_done = 1; in lib80211_tkip_decrypt()
435 tkip_mixing_phase2(rc4key, tkey->key, tkey->rx_ttak, iv16); in lib80211_tkip_decrypt()
437 plen = skb->len - hdr_len - 12; in lib80211_tkip_decrypt()
439 arc4_setkey(&tkey->rx_ctx_arc4, rc4key, 16); in lib80211_tkip_decrypt()
440 arc4_crypt(&tkey->rx_ctx_arc4, pos, pos, plen + 4); in lib80211_tkip_decrypt()
442 crc = ~crc32_le(~0, pos, plen); in lib80211_tkip_decrypt()
447 if (memcmp(icv, pos + plen, 4) != 0) { in lib80211_tkip_decrypt()
448 if (iv32 != tkey->rx_iv32) { in lib80211_tkip_decrypt()
451 tkey->rx_phase1_done = 0; in lib80211_tkip_decrypt()
455 hdr->addr2); in lib80211_tkip_decrypt()
457 tkey->dot11RSNAStatsTKIPICVErrors++; in lib80211_tkip_decrypt()
458 return -5; in lib80211_tkip_decrypt()
461 /* Update real counters only after Michael MIC verification has in lib80211_tkip_decrypt()
463 tkey->rx_iv32_new = iv32; in lib80211_tkip_decrypt()
464 tkey->rx_iv16_new = iv16; in lib80211_tkip_decrypt()
467 memmove(skb->data + TKIP_HDR_LEN, skb->data, hdr_len); in lib80211_tkip_decrypt()
469 skb_trim(skb, skb->len - 4); in lib80211_tkip_decrypt()
475 u8 *data, size_t data_len, u8 *mic) in michael_mic() argument
482 return -1; in michael_mic()
485 desc->tfm = tfm_michael; in michael_mic()
488 return -1; in michael_mic()
499 err = crypto_shash_final(desc, mic); in michael_mic()
510 hdr11 = (struct ieee80211_hdr *)skb->data; in michael_mic_hdr()
512 switch (le16_to_cpu(hdr11->frame_control) & in michael_mic_hdr()
515 memcpy(hdr, hdr11->addr3, ETH_ALEN); /* DA */ in michael_mic_hdr()
516 memcpy(hdr + ETH_ALEN, hdr11->addr2, ETH_ALEN); /* SA */ in michael_mic_hdr()
519 memcpy(hdr, hdr11->addr1, ETH_ALEN); /* DA */ in michael_mic_hdr()
520 memcpy(hdr + ETH_ALEN, hdr11->addr3, ETH_ALEN); /* SA */ in michael_mic_hdr()
523 memcpy(hdr, hdr11->addr3, ETH_ALEN); /* DA */ in michael_mic_hdr()
524 memcpy(hdr + ETH_ALEN, hdr11->addr4, ETH_ALEN); /* SA */ in michael_mic_hdr()
527 memcpy(hdr, hdr11->addr1, ETH_ALEN); /* DA */ in michael_mic_hdr()
528 memcpy(hdr + ETH_ALEN, hdr11->addr2, ETH_ALEN); /* SA */ in michael_mic_hdr()
532 if (ieee80211_is_data_qos(hdr11->frame_control)) { in michael_mic_hdr()
545 u8 *pos; in lib80211_michael_mic_add() local
547 if (skb_tailroom(skb) < 8 || skb->len < hdr_len) { in lib80211_michael_mic_add()
548 printk(KERN_DEBUG "Invalid packet for Michael MIC add " in lib80211_michael_mic_add()
549 "(tailroom=%d hdr_len=%d skb->len=%d)\n", in lib80211_michael_mic_add()
550 skb_tailroom(skb), hdr_len, skb->len); in lib80211_michael_mic_add()
551 return -1; in lib80211_michael_mic_add()
554 michael_mic_hdr(skb, tkey->tx_hdr); in lib80211_michael_mic_add()
555 pos = skb_put(skb, 8); in lib80211_michael_mic_add()
556 if (michael_mic(tkey->tx_tfm_michael, &tkey->key[16], tkey->tx_hdr, in lib80211_michael_mic_add()
557 skb->data + hdr_len, skb->len - 8 - hdr_len, pos)) in lib80211_michael_mic_add()
558 return -1; in lib80211_michael_mic_add()
573 if (hdr->addr1[0] & 0x01) in lib80211_michael_mic_failure()
578 memcpy(ev.src_addr.sa_data, hdr->addr2, ETH_ALEN); in lib80211_michael_mic_failure()
588 u8 mic[8]; in lib80211_michael_mic_verify() local
590 if (!tkey->key_set) in lib80211_michael_mic_verify()
591 return -1; in lib80211_michael_mic_verify()
593 michael_mic_hdr(skb, tkey->rx_hdr); in lib80211_michael_mic_verify()
594 if (michael_mic(tkey->rx_tfm_michael, &tkey->key[24], tkey->rx_hdr, in lib80211_michael_mic_verify()
595 skb->data + hdr_len, skb->len - 8 - hdr_len, mic)) in lib80211_michael_mic_verify()
596 return -1; in lib80211_michael_mic_verify()
597 if (memcmp(mic, skb->data + skb->len - 8, 8) != 0) { in lib80211_michael_mic_verify()
599 hdr = (struct ieee80211_hdr *)skb->data; in lib80211_michael_mic_verify()
600 printk(KERN_DEBUG "%s: Michael MIC verification failed for " in lib80211_michael_mic_verify()
602 skb->dev ? skb->dev->name : "N/A", hdr->addr2, in lib80211_michael_mic_verify()
604 if (skb->dev) in lib80211_michael_mic_verify()
605 lib80211_michael_mic_failure(skb->dev, hdr, keyidx); in lib80211_michael_mic_verify()
606 tkey->dot11RSNAStatsTKIPLocalMICFailures++; in lib80211_michael_mic_verify()
607 return -1; in lib80211_michael_mic_verify()
612 tkey->rx_iv32 = tkey->rx_iv32_new; in lib80211_michael_mic_verify()
613 tkey->rx_iv16 = tkey->rx_iv16_new; in lib80211_michael_mic_verify()
615 skb_trim(skb, skb->len - 8); in lib80211_michael_mic_verify()
624 struct crypto_shash *tfm = tkey->tx_tfm_michael; in lib80211_tkip_set_key()
625 struct arc4_ctx *tfm2 = &tkey->tx_ctx_arc4; in lib80211_tkip_set_key()
626 struct crypto_shash *tfm3 = tkey->rx_tfm_michael; in lib80211_tkip_set_key()
627 struct arc4_ctx *tfm4 = &tkey->rx_ctx_arc4; in lib80211_tkip_set_key()
629 keyidx = tkey->key_idx; in lib80211_tkip_set_key()
631 tkey->key_idx = keyidx; in lib80211_tkip_set_key()
632 tkey->tx_tfm_michael = tfm; in lib80211_tkip_set_key()
633 tkey->tx_ctx_arc4 = *tfm2; in lib80211_tkip_set_key()
634 tkey->rx_tfm_michael = tfm3; in lib80211_tkip_set_key()
635 tkey->rx_ctx_arc4 = *tfm4; in lib80211_tkip_set_key()
637 memcpy(tkey->key, key, TKIP_KEY_LEN); in lib80211_tkip_set_key()
638 tkey->key_set = 1; in lib80211_tkip_set_key()
639 tkey->tx_iv16 = 1; /* TSC is initialized to 1 */ in lib80211_tkip_set_key()
641 tkey->rx_iv32 = (seq[5] << 24) | (seq[4] << 16) | in lib80211_tkip_set_key()
643 tkey->rx_iv16 = (seq[1] << 8) | seq[0]; in lib80211_tkip_set_key()
646 tkey->key_set = 0; in lib80211_tkip_set_key()
648 return -1; in lib80211_tkip_set_key()
658 return -1; in lib80211_tkip_get_key()
660 if (!tkey->key_set) in lib80211_tkip_get_key()
662 memcpy(key, tkey->key, TKIP_KEY_LEN); in lib80211_tkip_get_key()
667 * or - as the code previously seemed to partially in lib80211_tkip_get_key()
668 * have been written as - subtract one from it. It in lib80211_tkip_get_key()
671 seq[0] = tkey->tx_iv16; in lib80211_tkip_get_key()
672 seq[1] = tkey->tx_iv16 >> 8; in lib80211_tkip_get_key()
673 seq[2] = tkey->tx_iv32; in lib80211_tkip_get_key()
674 seq[3] = tkey->tx_iv32 >> 8; in lib80211_tkip_get_key()
675 seq[4] = tkey->tx_iv32 >> 16; in lib80211_tkip_get_key()
676 seq[5] = tkey->tx_iv32 >> 24; in lib80211_tkip_get_key()
690 tkip->key_idx, tkip->key_set, in lib80211_tkip_print_stats()
691 (tkip->tx_iv32 >> 24) & 0xff, in lib80211_tkip_print_stats()
692 (tkip->tx_iv32 >> 16) & 0xff, in lib80211_tkip_print_stats()
693 (tkip->tx_iv32 >> 8) & 0xff, in lib80211_tkip_print_stats()
694 tkip->tx_iv32 & 0xff, in lib80211_tkip_print_stats()
695 (tkip->tx_iv16 >> 8) & 0xff, in lib80211_tkip_print_stats()
696 tkip->tx_iv16 & 0xff, in lib80211_tkip_print_stats()
697 (tkip->rx_iv32 >> 24) & 0xff, in lib80211_tkip_print_stats()
698 (tkip->rx_iv32 >> 16) & 0xff, in lib80211_tkip_print_stats()
699 (tkip->rx_iv32 >> 8) & 0xff, in lib80211_tkip_print_stats()
700 tkip->rx_iv32 & 0xff, in lib80211_tkip_print_stats()
701 (tkip->rx_iv16 >> 8) & 0xff, in lib80211_tkip_print_stats()
702 tkip->rx_iv16 & 0xff, in lib80211_tkip_print_stats()
703 tkip->dot11RSNAStatsTKIPReplays, in lib80211_tkip_print_stats()
704 tkip->dot11RSNAStatsTKIPICVErrors, in lib80211_tkip_print_stats()
705 tkip->dot11RSNAStatsTKIPLocalMICFailures); in lib80211_tkip_print_stats()
721 .extra_msdu_postfix_len = 8, /* MIC */