//! WPA2 CCMP (AES-CTR) data frame decryption using the Temporal Key (TK) derived //! from a cracked 4-way handshake. //! //! Used passively to inspect cleartext upper-layer protocols (DHCP, mDNS, ARP, HTTP) //! of a station whose own 4-way handshake was captured and cracked, allowing //! hostname/device identification. Because each association derives an ephemeral //! PTK, the TK is scoped to a specific `(BSSID, station)` pair. //! //! //! //! //! Implements AES-CTR payload decryption (skipping 8-byte CBC-MAC MIC verification //! since we only extract passive identity hints from known-key traffic). //! //! //! //! use aes::cipher::{BlockEncrypt, KeyInit}; use aes::Aes128; /// Decrypts a CCMP-protected data frame payload starting at the 8-byte CCMP header /// (`frame.body[frame.header_len..]`). /// `transmitter` is Address 2 (`A2`), and `qos_tid` is the QoS TID (`0` for non-QoS). /// /// /// Returns the decrypted LLC/SNAP + payload (excluding the trailing 8-byte MIC). /// pub fn decrypt_data( tk: &[u8; 16], transmitter: &[u8; 6], qos_tid: u8, body: &[u8], ) -> Option> { const HEADER_LEN: usize = 8; const MIC_LEN: usize = 8; if body.len() < HEADER_LEN + MIC_LEN { return None; } let pn0 = body[0]; let pn1 = body[1]; let key_id_octet = body[3]; // ExtIV (bit 5) must be set for a valid 8-byte CCMP IV header. // if key_id_octet & 0x20 == 0 { return None; } let pn2 = body[4]; let pn3 = body[5]; let pn4 = body[6]; let pn5 = body[7]; let ciphertext_and_mic = &body[HEADER_LEN..]; let ciphertext = &ciphertext_and_mic[..ciphertext_and_mic.len() - MIC_LEN]; // 13-byte CCMP nonce: `flags/priority (1) || A2 (6) || PN5..PN0 (6)`. // let mut nonce = [0u8; 13]; nonce[0] = qos_tid & 0x0f; nonce[1..7].copy_from_slice(transmitter); nonce[7] = pn5; nonce[8] = pn4; nonce[9] = pn3; nonce[10] = pn2; nonce[11] = pn1; nonce[12] = pn0; let cipher = Aes128::new(tk.into()); let mut out = Vec::with_capacity(ciphertext.len()); let mut counter: u16 = 1; // counter block 0 encrypts the MIC; payload blocks start at counter 1. for chunk in ciphertext.chunks(16) { let mut block = [0u8; 16]; block[0] = 0x01; // CCMP counter mode flags octet (`L = 1`) block[1..14].copy_from_slice(&nonce); block[14..16].copy_from_slice(&counter.to_be_bytes()); let mut keystream = block.into(); cipher.encrypt_block(&mut keystream); for (c, k) in chunk.iter().zip(keystream.iter()) { out.push(c ^ k); } counter = counter.wrapping_add(1); } Some(out) } #[cfg(test)] mod tests { use super::*; /// NIST FIPS-197 Appendix B AES-128 test vector. /// /// #[test] fn aes128_matches_fips197_vector() { let key: [u8; 16] = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, ]; let plaintext: [u8; 16] = [ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, ]; let expected: [u8; 16] = [ 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a, ]; let cipher = Aes128::new(&key.into()); let mut block = plaintext.into(); cipher.encrypt_block(&mut block); assert_eq!(block.as_slice(), &expected); } /// CTR symmetry round-trip: encrypting and decrypting with the same CCMP /// keystream recovers the original LLC/SNAP payload. /// /// #[test] fn ctr_round_trip_recovers_plaintext() { let tk = [0x42u8; 16]; let transmitter = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]; let plaintext = b"AA AA 03 00 00 00 08 00 payload de teste bem maior que um bloco de 16 bytes para cobrir mais de uma rodada do contador"; // Build a synthetic CCMP payload using the symmetric CTR keystream. // let header = [0x11u8, 0x22, 0x00, 0x20, 0x33, 0x44, 0x55, 0x66]; // ExtIV bit set let mut nonce = [0u8; 13]; nonce[0] = 3; // TID nonce[1..7].copy_from_slice(&transmitter); nonce[7] = header[7]; nonce[8] = header[6]; nonce[9] = header[5]; nonce[10] = header[4]; nonce[11] = header[1]; nonce[12] = header[0]; let cipher = Aes128::new(&tk.into()); let mut ciphertext = Vec::with_capacity(plaintext.len()); let mut counter: u16 = 1; for chunk in plaintext.chunks(16) { let mut block = [0u8; 16]; block[0] = 0x01; block[1..14].copy_from_slice(&nonce); block[14..16].copy_from_slice(&counter.to_be_bytes()); let mut keystream = block.into(); cipher.encrypt_block(&mut keystream); for (p, k) in chunk.iter().zip(keystream.iter()) { ciphertext.push(p ^ k); } counter = counter.wrapping_add(1); } ciphertext.extend_from_slice(&[0u8; 8]); // dummy 8-byte MIC (stripped during decryption) let mut body = Vec::new(); body.extend_from_slice(&header); body.extend_from_slice(&ciphertext); let out = decrypt_data(&tk, &transmitter, 3, &body).expect("decifra"); assert_eq!(out, plaintext); } #[test] fn rejects_body_without_extiv_bit() { let tk = [0u8; 16]; let body = [0u8; 20]; // key_id_octet (index 3) without the ExtIV bit set assert!(decrypt_data(&tk, &[0; 6], 0, &body).is_none()); } #[test] fn rejects_body_too_short() { let tk = [0u8; 16]; let body = [0u8; 10]; assert!(decrypt_data(&tk, &[0; 6], 0, &body).is_none()); } }