diff --git a/src/crypto.rs b/src/crypto.rs index 154734f..00c2c8c 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -69,18 +69,16 @@ pub fn pmk(pass: &[u8], ssid: &[u8]) -> [u8; 32] { out } -/// PTK derivation per 802.11-2016 §12.7.1.2 (PRF-512): -/// HMAC-SHA1(pmk, "Pairwise key expansion" || 0x00 || A || B || i) for i in 1.., -/// with A = min(ap,sta)||max(ap,sta), B = min(anonce,snonce)||max(anonce,snonce). -pub fn ptk( - pmk: &[u8; 32], +/// PRF message stem ("Pairwise key expansion" || 0x00 || A || B, 99 +/// bytes): A = min(ap,sta)||max(ap,sta), B = min(anonce,snonce)|| +/// max(anonce,snonce). Shared by [`ptk`] and the GPU kernel's per-target +/// KCK message (which appends counter byte 0x01). +pub(crate) fn prf_stem( ap: &MacAddr, sta: &MacAddr, anonce: &[u8; 32], snonce: &[u8; 32], -) -> [u8; 64] { - use hmac::{Hmac, Mac}; - +) -> Vec { let mut a_pair = [0u8; 12]; if ap.0 <= sta.0 { a_pair[..6].copy_from_slice(&ap.0); @@ -98,12 +96,27 @@ pub fn ptk( b_pair[32..].copy_from_slice(anonce); } - // "Pairwise key expansion" || 0x00 || A || B || i - let mut msg = Vec::with_capacity(22 + 1 + 12 + 64 + 1); + let mut msg = Vec::with_capacity(22 + 1 + 12 + 64); msg.extend_from_slice(b"Pairwise key expansion"); msg.push(0u8); msg.extend_from_slice(&a_pair); msg.extend_from_slice(&b_pair); + msg +} + +/// PTK derivation per 802.11-2016 §12.7.1.2 (PRF-512): +/// HMAC-SHA1(pmk, "Pairwise key expansion" || 0x00 || A || B || i) for i in 1.., +/// with A = min(ap,sta)||max(ap,sta), B = min(anonce,snonce)||max(anonce,snonce). +pub fn ptk( + pmk: &[u8; 32], + ap: &MacAddr, + sta: &MacAddr, + anonce: &[u8; 32], + snonce: &[u8; 32], +) -> [u8; 64] { + use hmac::{Hmac, Mac}; + + let mut msg = prf_stem(ap, sta, anonce, snonce); let stem_len = msg.len(); msg.push(0u8); // PRF counter byte, rewritten per round below @@ -119,19 +132,24 @@ pub fn ptk( out } -/// EAPOL-Key MIC. Zeroes bytes 81..97 of a copy of `eapol` (the MIC field -/// offset in a well-formed EAPOL-Key frame; short frames are left untouched), -/// then: key_ver 1|2 → HMAC-SHA1 keyed with KCK and truncated to 16 bytes, -/// key_ver 3 → AES-128-CMAC over IGTK||0^64||frame. -pub fn mic(ptk: &[u8; 64], eapol: &[u8], key_ver: u8) -> [u8; 16] { - use hmac::{Hmac, Mac}; - +/// EAPOL frame with its MIC field zeroed (bytes 81..97; short frames stay +/// untouched). The zeroed frame is CONSTANT across all candidates of one +/// capture — crack loops compute it once and call [`mic_on_zeroed`] per +/// candidate instead of re-copying the frame every time. +pub fn zeroed_eapol(eapol: &[u8]) -> Vec { let mut data = eapol.to_vec(); const MIC_OFF: usize = 81; const MIC_LEN: usize = 16; if data.len() >= MIC_OFF + MIC_LEN { data[MIC_OFF..MIC_OFF + MIC_LEN].fill(0); } + data +} + +/// MIC over an already-MIC-zeroed frame (see [`zeroed_eapol`]). No +/// allocation — the hot path of the cracker's per-candidate check. +pub fn mic_on_zeroed(ptk: &[u8; 64], zeroed: &[u8], key_ver: u8) -> [u8; 16] { + use hmac::{Hmac, Mac}; if key_ver == 3 { // key_mic_len = 16 → blk_key = IGTK||0x00*8. EAPOL frames are never @@ -139,11 +157,11 @@ pub fn mic(ptk: &[u8; 64], eapol: &[u8], key_ver: u8) -> [u8; 16] { // len ≡ 0 mod 16 correctly either way. let mut key = [0u8; 16]; key[..8].copy_from_slice(&ptk[48..56]); - cmac_aes128(&key, &data) + cmac_aes128(&key, zeroed) } else { let mut mac = as Mac>::new_from_slice(&ptk[..16]).expect("KCK is 16 bytes"); - mac.update(&data); + mac.update(zeroed); let digest = mac.finalize().into_bytes(); let mut out = [0u8; 16]; out.copy_from_slice(&digest[..16]); @@ -151,6 +169,14 @@ pub fn mic(ptk: &[u8; 64], eapol: &[u8], key_ver: u8) -> [u8; 16] { } } +/// EAPOL-Key MIC. Zeroes bytes 81..97 of a copy of `eapol` (the MIC field +/// offset in a well-formed EAPOL-Key frame; short frames are left untouched), +/// then: key_ver 1|2 → HMAC-SHA1 keyed with KCK and truncated to 16 bytes, +/// key_ver 3 → AES-128-CMAC over IGTK||0^64||frame. +pub fn mic(ptk: &[u8; 64], eapol: &[u8], key_ver: u8) -> [u8; 16] { + mic_on_zeroed(ptk, &zeroed_eapol(eapol), key_ver) +} + /// PMKID = HMAC-SHA1-128(pmk, "PMK Name" || ap || sta). pub fn pmkid_hash(pmk: &[u8; 32], ap: &MacAddr, sta: &MacAddr) -> [u8; 16] { use hmac::{Hmac, Mac}; diff --git a/src/handshake.rs b/src/handshake.rs index f8875db..29339e1 100644 --- a/src/handshake.rs +++ b/src/handshake.rs @@ -252,20 +252,33 @@ pub const CAPTURE_POLL_MS: u32 = 250; /// untouched. pub fn capture_loop( sock: &RawSocket, - ap: MacAddr, + targets: &[MacAddr], essid: &str, tx: &Sender, stop: &Arc, ) -> Result { sock.set_timeout(CAPTURE_POLL_MS)?; let _ = tx.send(Event::Log(format!( - "handshake: listening for 4-way on {ap} ({essid})" + "handshake: listening for 4-way on {} ({essid})", + if targets.len() == 1 { + targets[0].to_string() + } else { + format!("{} bssids", targets.len()) + } ))); let mut tracker = Tracker::new(); let mut buf = vec![0u8; 4096]; // ~4 minutes of dead air without a single EAPOL message → give up. - let idle_deadline = Instant::now() + Duration::from_secs(240); + let mut idle_deadline = Instant::now() + Duration::from_secs(240); + + // Diagnostics: a silent capture must be distinguishable from deaf radio + // (0 frames) vs wrong AP (EAPOL on other BSSIDs) vs client simply not + // reassociating (EAPOL on target but no 4-way yet) → periodic stats. + let mut frames: u64 = 0; + let mut eapol_other: u64 = 0; + let mut last_stats = Instant::now(); + const STATS_EVERY: Duration = Duration::from_secs(30); loop { if stop.load(Ordering::Relaxed) { @@ -274,6 +287,12 @@ pub fn capture_loop( if Instant::now() >= idle_deadline { return Ok(false); } + if last_stats.elapsed() >= STATS_EVERY { + last_stats = Instant::now(); + let _ = tx.send(Event::Log(format!( + "handshake: still listening — {frames} frames, {eapol_other} EAPOL on other BSSIDs" + ))); + } let n = match sock.recv(&mut buf) { Ok(n) => n, Err(e) @@ -287,12 +306,21 @@ pub fn capture_loop( let Some((_meta, frame)) = radiotap::strip(&buf[..n]) else { continue; }; + frames += 1; + if frames == 1 { + let _ = tx.send(Event::Log( + "handshake: receiving frames (radio OK)".to_string(), + )); + } let Some((bssid, _ap_is_sender, eapol)) = parse_eapol(frame) else { continue; }; - if bssid != ap { + if !targets.contains(&bssid) { + eapol_other += 1; continue; } + // Relevant traffic renews the idle window. + idle_deadline = Instant::now() + Duration::from_secs(240); let sta = crate::dot11::data_endpoints(frame) .map(|(a, b)| if a == bssid { b } else { a }) .unwrap_or(MacAddr::ZERO); @@ -302,8 +330,9 @@ pub fn capture_loop( let Some(key) = parse_eapol_key(eapol) else { continue; }; - // Any relevant message renews the idle window. - if let Some(cap) = tracker.ingest(ap, sta, essid, &key, tx) { + // The FRAME's BSSID is the truth (dual-band: may differ from the + // BSSID the user clicked). + if let Some(cap) = tracker.ingest(bssid, sta, essid, &key, tx) { let _ = tx.send(Event::HandshakeComplete(Box::new(cap))); return Ok(true); }