Allocation-free MIC over zeroed EAPOL, shared PRF message stem
Browse files at this commit3dbcafb4c81d85127a6f024fee135751f3fa3919Parent: 93094a1- perf
- allocation-free MIC over zeroed EAPOL, shared PRF message stem
2 changed files +80 -25
- src/crypto.rs +45 -19
- src/handshake.rs +35 -6
src/crypto.rsmodified+45 -19
| @@ -69,18 +69,16 @@ pub fn pmk(pass: &[u8], ssid: &[u8]) -> [u8; 32] { | |||
| 69 | out | 69 | out |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | -/// PTK derivation per 802.11-2016 §12.7.1.2 (PRF-512): | 72 | +/// PRF message stem ("Pairwise key expansion" || 0x00 || A || B, 99 |
| 73 | -/// HMAC-SHA1(pmk, "Pairwise key expansion" || 0x00 || A || B || i) for i in 1.., | 73 | +/// bytes): A = min(ap,sta)||max(ap,sta), B = min(anonce,snonce)|| |
| 74 | -/// with A = min(ap,sta)||max(ap,sta), B = min(anonce,snonce)||max(anonce,snonce). | 74 | +/// max(anonce,snonce). Shared by [`ptk`] and the GPU kernel's per-target |
| 75 | -pub fn ptk( | 75 | +/// KCK message (which appends counter byte 0x01). |
| 76 | - pmk: &[u8; 32], | 76 | +pub(crate) fn prf_stem( |
| 77 | ap: &MacAddr, | 77 | ap: &MacAddr, |
| 78 | sta: &MacAddr, | 78 | sta: &MacAddr, |
| 79 | anonce: &[u8; 32], | 79 | anonce: &[u8; 32], |
| 80 | snonce: &[u8; 32], | 80 | snonce: &[u8; 32], |
| 81 | -) -> [u8; 64] { | 81 | +) -> Vec<u8> { |
| 82 | - use hmac::{Hmac, Mac}; | ||
| 83 | - | ||
| 84 | let mut a_pair = [0u8; 12]; | 82 | let mut a_pair = [0u8; 12]; |
| 85 | if ap.0 <= sta.0 { | 83 | if ap.0 <= sta.0 { |
| 86 | a_pair[..6].copy_from_slice(&ap.0); | 84 | a_pair[..6].copy_from_slice(&ap.0); |
| @@ -98,12 +96,27 @@ pub fn ptk( | |||
| 98 | b_pair[32..].copy_from_slice(anonce); | 96 | b_pair[32..].copy_from_slice(anonce); |
| 99 | } | 97 | } |
| 100 | 98 | ||
| 101 | - // "Pairwise key expansion" || 0x00 || A || B || i | 99 | + let mut msg = Vec::with_capacity(22 + 1 + 12 + 64); |
| 102 | - let mut msg = Vec::with_capacity(22 + 1 + 12 + 64 + 1); | ||
| 103 | msg.extend_from_slice(b"Pairwise key expansion"); | 100 | msg.extend_from_slice(b"Pairwise key expansion"); |
| 104 | msg.push(0u8); | 101 | msg.push(0u8); |
| 105 | msg.extend_from_slice(&a_pair); | 102 | msg.extend_from_slice(&a_pair); |
| 106 | msg.extend_from_slice(&b_pair); | 103 | msg.extend_from_slice(&b_pair); |
| 104 | + msg | ||
| 105 | +} | ||
| 106 | + | ||
| 107 | +/// PTK derivation per 802.11-2016 §12.7.1.2 (PRF-512): | ||
| 108 | +/// HMAC-SHA1(pmk, "Pairwise key expansion" || 0x00 || A || B || i) for i in 1.., | ||
| 109 | +/// with A = min(ap,sta)||max(ap,sta), B = min(anonce,snonce)||max(anonce,snonce). | ||
| 110 | +pub fn ptk( | ||
| 111 | + pmk: &[u8; 32], | ||
| 112 | + ap: &MacAddr, | ||
| 113 | + sta: &MacAddr, | ||
| 114 | + anonce: &[u8; 32], | ||
| 115 | + snonce: &[u8; 32], | ||
| 116 | +) -> [u8; 64] { | ||
| 117 | + use hmac::{Hmac, Mac}; | ||
| 118 | + | ||
| 119 | + let mut msg = prf_stem(ap, sta, anonce, snonce); | ||
| 107 | let stem_len = msg.len(); | 120 | let stem_len = msg.len(); |
| 108 | msg.push(0u8); // PRF counter byte, rewritten per round below | 121 | msg.push(0u8); // PRF counter byte, rewritten per round below |
| 109 | 122 | ||
| @@ -119,19 +132,24 @@ pub fn ptk( | |||
| 119 | out | 132 | out |
| 120 | } | 133 | } |
| 121 | 134 | ||
| 122 | -/// EAPOL-Key MIC. Zeroes bytes 81..97 of a copy of `eapol` (the MIC field | 135 | +/// EAPOL frame with its MIC field zeroed (bytes 81..97; short frames stay |
| 123 | -/// offset in a well-formed EAPOL-Key frame; short frames are left untouched), | 136 | +/// untouched). The zeroed frame is CONSTANT across all candidates of one |
| 124 | -/// then: key_ver 1|2 → HMAC-SHA1 keyed with KCK and truncated to 16 bytes, | 137 | +/// capture — crack loops compute it once and call [`mic_on_zeroed`] per |
| 125 | -/// key_ver 3 → AES-128-CMAC over IGTK||0^64||frame. | 138 | +/// candidate instead of re-copying the frame every time. |
| 126 | -pub fn mic(ptk: &[u8; 64], eapol: &[u8], key_ver: u8) -> [u8; 16] { | 139 | +pub fn zeroed_eapol(eapol: &[u8]) -> Vec<u8> { |
| 127 | - use hmac::{Hmac, Mac}; | ||
| 128 | - | ||
| 129 | let mut data = eapol.to_vec(); | 140 | let mut data = eapol.to_vec(); |
| 130 | const MIC_OFF: usize = 81; | 141 | const MIC_OFF: usize = 81; |
| 131 | const MIC_LEN: usize = 16; | 142 | const MIC_LEN: usize = 16; |
| 132 | if data.len() >= MIC_OFF + MIC_LEN { | 143 | if data.len() >= MIC_OFF + MIC_LEN { |
| 133 | data[MIC_OFF..MIC_OFF + MIC_LEN].fill(0); | 144 | data[MIC_OFF..MIC_OFF + MIC_LEN].fill(0); |
| 134 | } | 145 | } |
| 146 | + data | ||
| 147 | +} | ||
| 148 | + | ||
| 149 | +/// MIC over an already-MIC-zeroed frame (see [`zeroed_eapol`]). No | ||
| 150 | +/// allocation — the hot path of the cracker's per-candidate check. | ||
| 151 | +pub fn mic_on_zeroed(ptk: &[u8; 64], zeroed: &[u8], key_ver: u8) -> [u8; 16] { | ||
| 152 | + use hmac::{Hmac, Mac}; | ||
| 135 | 153 | ||
| 136 | if key_ver == 3 { | 154 | if key_ver == 3 { |
| 137 | // key_mic_len = 16 → blk_key = IGTK||0x00*8. EAPOL frames are never | 155 | // 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] { | |||
| 139 | // len ≡ 0 mod 16 correctly either way. | 157 | // len ≡ 0 mod 16 correctly either way. |
| 140 | let mut key = [0u8; 16]; | 158 | let mut key = [0u8; 16]; |
| 141 | key[..8].copy_from_slice(&ptk[48..56]); | 159 | key[..8].copy_from_slice(&ptk[48..56]); |
| 142 | - cmac_aes128(&key, &data) | 160 | + cmac_aes128(&key, zeroed) |
| 143 | } else { | 161 | } else { |
| 144 | let mut mac = | 162 | let mut mac = |
| 145 | <Hmac<sha1::Sha1> as Mac>::new_from_slice(&ptk[..16]).expect("KCK is 16 bytes"); | 163 | <Hmac<sha1::Sha1> as Mac>::new_from_slice(&ptk[..16]).expect("KCK is 16 bytes"); |
| 146 | - mac.update(&data); | 164 | + mac.update(zeroed); |
| 147 | let digest = mac.finalize().into_bytes(); | 165 | let digest = mac.finalize().into_bytes(); |
| 148 | let mut out = [0u8; 16]; | 166 | let mut out = [0u8; 16]; |
| 149 | out.copy_from_slice(&digest[..16]); | 167 | out.copy_from_slice(&digest[..16]); |
| @@ -151,6 +169,14 @@ pub fn mic(ptk: &[u8; 64], eapol: &[u8], key_ver: u8) -> [u8; 16] { | |||
| 151 | } | 169 | } |
| 152 | } | 170 | } |
| 153 | 171 | ||
| 172 | +/// EAPOL-Key MIC. Zeroes bytes 81..97 of a copy of `eapol` (the MIC field | ||
| 173 | +/// offset in a well-formed EAPOL-Key frame; short frames are left untouched), | ||
| 174 | +/// then: key_ver 1|2 → HMAC-SHA1 keyed with KCK and truncated to 16 bytes, | ||
| 175 | +/// key_ver 3 → AES-128-CMAC over IGTK||0^64||frame. | ||
| 176 | +pub fn mic(ptk: &[u8; 64], eapol: &[u8], key_ver: u8) -> [u8; 16] { | ||
| 177 | + mic_on_zeroed(ptk, &zeroed_eapol(eapol), key_ver) | ||
| 178 | +} | ||
| 179 | + | ||
| 154 | /// PMKID = HMAC-SHA1-128(pmk, "PMK Name" || ap || sta). | 180 | /// PMKID = HMAC-SHA1-128(pmk, "PMK Name" || ap || sta). |
| 155 | pub fn pmkid_hash(pmk: &[u8; 32], ap: &MacAddr, sta: &MacAddr) -> [u8; 16] { | 181 | pub fn pmkid_hash(pmk: &[u8; 32], ap: &MacAddr, sta: &MacAddr) -> [u8; 16] { |
| 156 | use hmac::{Hmac, Mac}; | 182 | use hmac::{Hmac, Mac}; |
src/handshake.rsmodified+35 -6
| @@ -252,20 +252,33 @@ pub const CAPTURE_POLL_MS: u32 = 250; | |||
| 252 | /// untouched. | 252 | /// untouched. |
| 253 | pub fn capture_loop( | 253 | pub fn capture_loop( |
| 254 | sock: &RawSocket, | 254 | sock: &RawSocket, |
| 255 | - ap: MacAddr, | 255 | + targets: &[MacAddr], |
| 256 | essid: &str, | 256 | essid: &str, |
| 257 | tx: &Sender<Event>, | 257 | tx: &Sender<Event>, |
| 258 | stop: &Arc<AtomicBool>, | 258 | stop: &Arc<AtomicBool>, |
| 259 | ) -> Result<bool> { | 259 | ) -> Result<bool> { |
| 260 | sock.set_timeout(CAPTURE_POLL_MS)?; | 260 | sock.set_timeout(CAPTURE_POLL_MS)?; |
| 261 | let _ = tx.send(Event::Log(format!( | 261 | let _ = tx.send(Event::Log(format!( |
| 262 | - "handshake: listening for 4-way on {ap} ({essid})" | 262 | + "handshake: listening for 4-way on {} ({essid})", |
| 263 | + if targets.len() == 1 { | ||
| 264 | + targets[0].to_string() | ||
| 265 | + } else { | ||
| 266 | + format!("{} bssids", targets.len()) | ||
| 267 | + } | ||
| 263 | ))); | 268 | ))); |
| 264 | 269 | ||
| 265 | let mut tracker = Tracker::new(); | 270 | let mut tracker = Tracker::new(); |
| 266 | let mut buf = vec![0u8; 4096]; | 271 | let mut buf = vec![0u8; 4096]; |
| 267 | // ~4 minutes of dead air without a single EAPOL message → give up. | 272 | // ~4 minutes of dead air without a single EAPOL message → give up. |
| 268 | - let idle_deadline = Instant::now() + Duration::from_secs(240); | 273 | + let mut idle_deadline = Instant::now() + Duration::from_secs(240); |
| 274 | + | ||
| 275 | + // Diagnostics: a silent capture must be distinguishable from deaf radio | ||
| 276 | + // (0 frames) vs wrong AP (EAPOL on other BSSIDs) vs client simply not | ||
| 277 | + // reassociating (EAPOL on target but no 4-way yet) → periodic stats. | ||
| 278 | + let mut frames: u64 = 0; | ||
| 279 | + let mut eapol_other: u64 = 0; | ||
| 280 | + let mut last_stats = Instant::now(); | ||
| 281 | + const STATS_EVERY: Duration = Duration::from_secs(30); | ||
| 269 | 282 | ||
| 270 | loop { | 283 | loop { |
| 271 | if stop.load(Ordering::Relaxed) { | 284 | if stop.load(Ordering::Relaxed) { |
| @@ -274,6 +287,12 @@ pub fn capture_loop( | |||
| 274 | if Instant::now() >= idle_deadline { | 287 | if Instant::now() >= idle_deadline { |
| 275 | return Ok(false); | 288 | return Ok(false); |
| 276 | } | 289 | } |
| 290 | + if last_stats.elapsed() >= STATS_EVERY { | ||
| 291 | + last_stats = Instant::now(); | ||
| 292 | + let _ = tx.send(Event::Log(format!( | ||
| 293 | + "handshake: still listening — {frames} frames, {eapol_other} EAPOL on other BSSIDs" | ||
| 294 | + ))); | ||
| 295 | + } | ||
| 277 | let n = match sock.recv(&mut buf) { | 296 | let n = match sock.recv(&mut buf) { |
| 278 | Ok(n) => n, | 297 | Ok(n) => n, |
| 279 | Err(e) | 298 | Err(e) |
| @@ -287,12 +306,21 @@ pub fn capture_loop( | |||
| 287 | let Some((_meta, frame)) = radiotap::strip(&buf[..n]) else { | 306 | let Some((_meta, frame)) = radiotap::strip(&buf[..n]) else { |
| 288 | continue; | 307 | continue; |
| 289 | }; | 308 | }; |
| 309 | + frames += 1; | ||
| 310 | + if frames == 1 { | ||
| 311 | + let _ = tx.send(Event::Log( | ||
| 312 | + "handshake: receiving frames (radio OK)".to_string(), | ||
| 313 | + )); | ||
| 314 | + } | ||
| 290 | let Some((bssid, _ap_is_sender, eapol)) = parse_eapol(frame) else { | 315 | let Some((bssid, _ap_is_sender, eapol)) = parse_eapol(frame) else { |
| 291 | continue; | 316 | continue; |
| 292 | }; | 317 | }; |
| 293 | - if bssid != ap { | 318 | + if !targets.contains(&bssid) { |
| 319 | + eapol_other += 1; | ||
| 294 | continue; | 320 | continue; |
| 295 | } | 321 | } |
| 322 | + // Relevant traffic renews the idle window. | ||
| 323 | + idle_deadline = Instant::now() + Duration::from_secs(240); | ||
| 296 | let sta = crate::dot11::data_endpoints(frame) | 324 | let sta = crate::dot11::data_endpoints(frame) |
| 297 | .map(|(a, b)| if a == bssid { b } else { a }) | 325 | .map(|(a, b)| if a == bssid { b } else { a }) |
| 298 | .unwrap_or(MacAddr::ZERO); | 326 | .unwrap_or(MacAddr::ZERO); |
| @@ -302,8 +330,9 @@ pub fn capture_loop( | |||
| 302 | let Some(key) = parse_eapol_key(eapol) else { | 330 | let Some(key) = parse_eapol_key(eapol) else { |
| 303 | continue; | 331 | continue; |
| 304 | }; | 332 | }; |
| 305 | - // Any relevant message renews the idle window. | 333 | + // The FRAME's BSSID is the truth (dual-band: may differ from the |
| 306 | - if let Some(cap) = tracker.ingest(ap, sta, essid, &key, tx) { | 334 | + // BSSID the user clicked). |
| 335 | + if let Some(cap) = tracker.ingest(bssid, sta, essid, &key, tx) { | ||
| 307 | let _ = tx.send(Event::HandshakeComplete(Box::new(cap))); | 336 | let _ = tx.send(Event::HandshakeComplete(Box::new(cap))); |
| 308 | return Ok(true); | 337 | return Ok(true); |
| 309 | } | 338 | } |