eapol/
lib.rs

1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#![cfg_attr(feature = "benchmarks", feature(test))]
6
7use core::mem;
8use log::warn;
9use thiserror::Error;
10use wlan_bitfield::bitfield;
11use wlan_common::append::{Append, BufferTooSmall};
12use wlan_common::buffer_reader::BufferReader;
13use zerocopy::byteorder::big_endian::{U16, U64};
14use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, SplitByteSlice, Unaligned};
15
16#[derive(Debug, Error)]
17pub enum Error {
18    #[error("unexpected end of buffer while parsing frame")]
19    FrameTruncated,
20    #[error("buffer too short to write frame")]
21    BufferTooShort,
22    #[error("attempted to parse the wrong frame type")]
23    WrongEapolFrame,
24    #[error("packet body length is {} but {} bytes are available", _0, _1)]
25    WrongPacketBodyLength(u16, u16),
26    #[error("failed to calculate mic: {}", _0)]
27    MicFunctionFailed(anyhow::Error),
28    #[error("expected mic length of {} but got a mic of length {}", _0, _1)]
29    WrongMicLen(usize, usize),
30    #[error("called finalize with a mic, but key_mic is false")]
31    UnexpectedMic,
32    #[error("called finalize without a mic, but key_mic is true")]
33    ExpectedMic,
34}
35
36impl From<BufferTooSmall> for Error {
37    fn from(_src: BufferTooSmall) -> Error {
38        Error::BufferTooShort
39    }
40}
41
42pub enum Frame<B: SplitByteSlice> {
43    Key(KeyFrameRx<B>),
44    Unsupported(Ref<B, EapolFields>),
45}
46
47impl<B: SplitByteSlice> Frame<B> {
48    pub fn parse_fixed_fields(bytes: B) -> Result<Ref<B, EapolFields>, Error> {
49        let mut reader = BufferReader::new(bytes);
50        reader.read().ok_or(Error::FrameTruncated)
51    }
52}
53
54// IEEE Std 802.11-2016, 12.7.2, Figure 12-32
55#[derive(Debug)]
56pub struct KeyFrameRx<B: SplitByteSlice> {
57    pub eapol_fields: Ref<B, EapolFields>,
58    pub key_frame_fields: Ref<B, KeyFrameFields>,
59    pub key_mic: B, /* AKM dependent size */
60    // 2 octets omitted - key data length is calculated from key_data.len()
61    pub key_data: B,
62}
63
64impl<B: SplitByteSlice> KeyFrameRx<B> {
65    pub fn parse(mic_len: usize, eapol_pdu_buf: B) -> Result<Self, Error> {
66        let mut reader = BufferReader::new(eapol_pdu_buf);
67        let eapol_fields = reader.read::<EapolFields>().ok_or(Error::FrameTruncated)?;
68        if eapol_fields.packet_body_len.get() > reader.bytes_remaining() as u16 {
69            return Err(Error::WrongPacketBodyLength(
70                eapol_fields.packet_body_len.get(),
71                reader.bytes_remaining() as u16,
72            ));
73        }
74        match eapol_fields.packet_type {
75            PacketType::KEY => {
76                let key_frame_fields = reader.read().ok_or(Error::FrameTruncated)?;
77                let key_mic = reader.read_bytes(mic_len).ok_or(Error::FrameTruncated)?;
78                let key_data_len = reader.read_unaligned::<U16>().ok_or(Error::FrameTruncated)?;
79                let key_data =
80                    reader.read_bytes(key_data_len.get().into()).ok_or(Error::FrameTruncated)?;
81                // Some APs add additional bytes to the 802.1X body. This is odd, but doesn't break anything.
82                match reader.peek_remaining().len() {
83                    0 => (),
84                    extra => warn!(bytes = extra; "Ignoring extra bytes in eapol frame body"),
85                }
86                Ok(KeyFrameRx { eapol_fields, key_frame_fields, key_mic, key_data })
87            }
88            _ => Err(Error::WrongEapolFrame),
89        }
90    }
91
92    pub fn to_bytes(&self, clear_mic: bool) -> Vec<u8> {
93        let mut buf = Vec::new();
94        self.write_into(clear_mic, &mut buf).unwrap(); // Write to Vec never fails
95        buf.into()
96    }
97
98    /// Populates buf with the underlying bytes of this keyframe.
99    ///
100    /// If clear_mic is true, the MIC field will be populated with zeroes. This should be used when
101    /// recalculating the frame MIC during a key exchange.
102    pub fn write_into<A: Append>(&self, clear_mic: bool, buf: &mut A) -> Result<(), Error> {
103        let required_size =
104            self.eapol_fields.packet_body_len.get() as usize + mem::size_of::<EapolFields>();
105        if !buf.can_append(required_size) {
106            return Err(Error::BufferTooShort);
107        }
108        buf.append_value(self.eapol_fields.as_bytes())?;
109        buf.append_value(self.key_frame_fields.as_bytes())?;
110        if clear_mic {
111            buf.append_bytes_zeroed(self.key_mic.len())?;
112        } else {
113            buf.append_bytes(self.key_mic.as_bytes())?;
114        }
115        buf.append_value(&U16::new(self.key_data.len() as u16))?;
116        buf.append_bytes(&self.key_data)?;
117        Ok(())
118    }
119}
120
121#[derive(Debug, Clone)]
122pub struct KeyFrameTx {
123    pub protocol_version: ProtocolVersion,
124    pub key_frame_fields: KeyFrameFields,
125    mic_len: usize,
126    key_data: Vec<u8>,
127}
128
129const KEY_DATA_LEN_BYTES: usize = 2;
130
131impl KeyFrameTx {
132    pub fn new(
133        protocol_version: ProtocolVersion,
134        key_frame_fields: KeyFrameFields,
135        key_data: Vec<u8>,
136        mic_len: usize,
137    ) -> Self {
138        KeyFrameTx { protocol_version, key_frame_fields, mic_len, key_data }
139    }
140
141    pub fn serialize(self) -> KeyFrameTxFinalizer {
142        KeyFrameTxFinalizer::new(
143            self.protocol_version,
144            self.key_frame_fields,
145            self.key_data,
146            self.mic_len,
147        )
148    }
149}
150
151/// KeyFrameTxFinalizer stores a key frame that has been serialized into a buffer, but which may
152/// still require the addition of a MIC before it can be transmitted.
153pub struct KeyFrameTxFinalizer {
154    buf: Vec<u8>,
155    mic_offset: Option<usize>,
156    mic_len: usize,
157}
158
159impl KeyFrameTxFinalizer {
160    fn new(
161        version: ProtocolVersion,
162        key_frame_fields: KeyFrameFields,
163        key_data: Vec<u8>,
164        mic_len: usize,
165    ) -> Self {
166        let packet_body_len =
167            mem::size_of::<KeyFrameFields>() + mic_len + KEY_DATA_LEN_BYTES + key_data.len();
168        let size = mem::size_of::<EapolFields>() + packet_body_len;
169        let packet_body_len = U16::new(packet_body_len as u16);
170        // The Vec implementation of Append will never fail to append, so none of the expects
171        // here should ever trigger.
172        let mut buf = Vec::with_capacity(size);
173        buf.append_value(&EapolFields { version, packet_type: PacketType::KEY, packet_body_len })
174            .expect("bad eapol allocation");
175        buf.append_value(&key_frame_fields).expect("bad eapol allocation");
176        let mic_offset = if KeyInformation(key_frame_fields.key_info.get()).key_mic() {
177            Some(mem::size_of::<EapolFields>() + mem::size_of::<KeyFrameFields>())
178        } else {
179            None
180        };
181        buf.append_bytes_zeroed(mic_len as usize).expect("bad eapol allocation");
182        buf.append_value(&U16::new(key_data.len() as u16)).expect("bad eapol allocation");
183        buf.append_bytes(&key_data[..]).expect("bad eapol allocation");
184        KeyFrameTxFinalizer { buf: buf.into(), mic_offset, mic_len }
185    }
186
187    /// Access a key frame buffer that may still need to have its MIC field populated. This should
188    /// generally only be used when calculating the MIC to pass to finalize_with_mic.
189    pub fn unfinalized_buf(&self) -> &[u8] {
190        &self.buf[..]
191    }
192
193    /// Generate a final key frame buffer. Fails if the given MIC is the wrong size, or if the
194    /// key_mic bit is not set for this frame.
195    pub fn finalize_with_mic(mut self, mic: &[u8]) -> Result<KeyFrameBuf, Error> {
196        match self.mic_offset {
197            Some(offset) => {
198                if self.mic_len != mic.len() {
199                    Err(Error::WrongMicLen(self.mic_len, mic.len()))
200                } else {
201                    self.buf[offset..offset + self.mic_len].copy_from_slice(mic);
202                    Ok(KeyFrameBuf { buf: self.buf, mic_len: self.mic_len })
203                }
204            }
205            None => Err(Error::UnexpectedMic),
206        }
207    }
208
209    /// Generate a final key frame buffer. Fails if the key_mic bit is set for this frame.
210    pub fn finalize_without_mic(self) -> Result<KeyFrameBuf, Error> {
211        match self.mic_offset {
212            None => Ok(KeyFrameBuf { buf: self.buf, mic_len: self.mic_len }),
213            _ => Err(Error::ExpectedMic),
214        }
215    }
216}
217
218/// A KeyFrameBuf is a wrapper for a Vec<u8> that is guaranteed to contain a parseable
219/// eapol key frame.
220#[derive(Debug, PartialEq, Eq, Clone)]
221pub struct KeyFrameBuf {
222    buf: Vec<u8>,
223    mic_len: usize,
224}
225
226impl KeyFrameBuf {
227    /// A FinalizedKeyFrameBuf guarantees that the buffer can be parsed as a KeyFrame, so this
228    /// should always succeed. Panics if the parse somehow fails.
229    pub fn keyframe(&self) -> KeyFrameRx<&[u8]> {
230        KeyFrameRx::parse(self.mic_len, &self.buf[..])
231            .expect("finalized eapol keyframe buffer failed to parse")
232    }
233
234    /// A mutable keyframe may be rendered invalid, violating the KeyFrameBuf contract, so this
235    /// instead populates the passed buffer with the keyframe contents. Panics if the parse somehow
236    /// fails.
237    pub fn copy_keyframe_mut<'a>(&self, buf: &'a mut Vec<u8>) -> KeyFrameRx<&'a mut [u8]> {
238        buf.extend_from_slice(&self.buf[..]);
239        KeyFrameRx::parse(self.mic_len, &mut buf[..])
240            .expect("finalized eapol keyframe buffer failed to parse")
241    }
242}
243
244impl std::ops::Deref for KeyFrameBuf {
245    type Target = [u8];
246
247    fn deref(&self) -> &Self::Target {
248        &self.buf[..]
249    }
250}
251
252impl From<KeyFrameBuf> for Vec<u8> {
253    fn from(src: KeyFrameBuf) -> Vec<u8> {
254        src.buf
255    }
256}
257
258// IEEE Std 802.1X-2010, 11.9, Table 11-5
259#[derive(
260    IntoBytes,
261    KnownLayout,
262    FromBytes,
263    Immutable,
264    Debug,
265    Clone,
266    Copy,
267    PartialEq,
268    Eq,
269    Unaligned,
270    Default,
271)]
272#[repr(C)]
273pub struct KeyDescriptor(u8);
274
275impl KeyDescriptor {
276    pub const RESERVED: Self = Self(0);
277    pub const RC4: Self = Self(1);
278    pub const IEEE802DOT11: Self = Self(2);
279
280    // This descriptor is specified by the WiFi Alliance WPA standard rather than IEEE.
281    pub const LEGACY_WPA1: Self = Self(254);
282}
283
284// IEEE Std 802.11-2016, 12.7.2 b.2)
285#[derive(Debug, Clone, PartialEq, Eq, Default)]
286#[repr(C)]
287pub struct KeyType(bool);
288
289impl KeyType {
290    pub const GROUP_SMK: Self = Self(false);
291    pub const PAIRWISE: Self = Self(true);
292}
293
294// IEEE Std 802.1X-2010, 11.3.1
295#[derive(
296    IntoBytes,
297    KnownLayout,
298    FromBytes,
299    Immutable,
300    Debug,
301    Clone,
302    Copy,
303    Unaligned,
304    PartialEq,
305    Eq,
306    PartialOrd,
307    Ord,
308)]
309#[repr(C)]
310pub struct ProtocolVersion(u8);
311
312impl ProtocolVersion {
313    pub const IEEE802DOT1X2001: Self = Self(1);
314    pub const IEEE802DOT1X2004: Self = Self(2);
315    pub const IEEE802DOT1X2010: Self = Self(3);
316}
317
318// IEEE Std 802.1X-2010, 11.3.2, Table 11-3
319#[derive(IntoBytes, KnownLayout, FromBytes, Immutable, Debug, Clone, Copy, PartialEq, Eq)]
320#[repr(C)]
321pub struct PacketType(u8);
322
323impl PacketType {
324    pub const EAP: Self = Self(0);
325    pub const START: Self = Self(1);
326    pub const LOGOFF: Self = Self(2);
327    pub const KEY: Self = Self(3);
328    pub const ASF_ALERT: Self = Self(4);
329    pub const MKA: Self = Self(5);
330    pub const ANNOUNCEMENT_GENERIC: Self = Self(6);
331    pub const ANNOUNCEMENT_SPECIFIC: Self = Self(7);
332    pub const ANNOUNCEMENT_REQ: Self = Self(8);
333}
334
335// IEEE Std 802.11-2016, 12.7.2, Figure 12-33
336#[bitfield(
337    0..=2   key_descriptor_version,
338    3       key_type as KeyType(bool),
339    // WFA, WPA1 Spec. 3.1, Chapter 2.2.4, Key Information.
340    // These bits are reserved for non-WPA1 protection.
341    4..=5   legacy_wpa1_key_id,
342    6       install,
343    7       key_ack,
344    8       key_mic,
345    9       secure,
346    10      error,
347    11      request,
348    12      encrypted_key_data,
349    13      smk_message,
350    14..=15 _, // reserved
351)]
352#[derive(IntoBytes, KnownLayout, FromBytes, Immutable, PartialEq, Clone, Default)]
353#[repr(C)]
354pub struct KeyInformation(pub u16);
355
356#[derive(IntoBytes, KnownLayout, FromBytes, Immutable, Debug, Clone, Unaligned)]
357#[repr(C, packed)]
358pub struct EapolFields {
359    pub version: ProtocolVersion,
360    pub packet_type: PacketType,
361    pub packet_body_len: U16,
362}
363
364// IEEE Std 802.11-2016, 12.7.2, Figure 12-32
365#[derive(IntoBytes, KnownLayout, FromBytes, Immutable, Default, Debug, Clone, Unaligned)]
366#[repr(C, packed)]
367pub struct KeyFrameFields {
368    pub descriptor_type: KeyDescriptor,
369    key_info: U16,
370    pub key_len: U16,
371    pub key_replay_counter: U64,
372    pub key_nonce: [u8; 32],
373    pub key_iv: [u8; 16],
374    pub key_rsc: U64,
375    _reserved: [u8; 8],
376}
377
378impl KeyFrameFields {
379    pub fn new(
380        descriptor_type: KeyDescriptor,
381        key_info: KeyInformation,
382        key_len: u16,
383        key_replay_counter: u64,
384        key_nonce: [u8; 32],
385        key_iv: [u8; 16],
386        key_rsc: u64,
387    ) -> Self {
388        let KeyInformation(key_info) = key_info;
389        Self {
390            descriptor_type,
391            key_info: U16::new(key_info),
392            key_len: U16::new(key_len),
393            key_replay_counter: U64::new(key_replay_counter),
394            key_nonce,
395            key_iv,
396            key_rsc: U64::new(key_rsc),
397            _reserved: [0u8; 8],
398        }
399    }
400
401    pub fn key_info(&self) -> KeyInformation {
402        KeyInformation(self.key_info.get())
403    }
404    pub fn set_key_info(&mut self, key_info: KeyInformation) {
405        let KeyInformation(key_info) = key_info;
406        self.key_info = U16::new(key_info);
407    }
408}
409
410pub fn to_array<A>(slice: &[u8]) -> A
411where
412    A: Sized + Default + AsMut<[u8]>,
413{
414    let mut array = Default::default();
415    <A as AsMut<[u8]>>::as_mut(&mut array).clone_from_slice(slice);
416    array
417}
418
419#[cfg(test)]
420mod tests {
421    use super::*;
422    use assert_matches::assert_matches;
423    use wlan_common::buffer_writer::BufferWriter;
424
425    #[cfg(feature = "benchmarks")]
426    mod benches {
427        use super::*;
428        use test::{Bencher, black_box};
429
430        #[bench]
431        fn bench_key_frame_from_bytes(b: &mut Bencher) {
432            let frame: Vec<u8> = vec![
433                0x01, 0x03, 0x00, 0xb3, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
434                0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
435                0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
436                0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
437                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
438                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
439                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
440                0x54, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01,
441                0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03,
442                0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02,
443                0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01,
444                0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03,
445                0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x01, 0x02,
446                0x03,
447            ];
448            b.iter(|| KeyFrameRx::parse(black_box(16), &frame[..]));
449        }
450    }
451
452    #[test]
453    fn test_key_info() {
454        let value = 0b1010_0000_0000_0000u16;
455        let key_info = KeyInformation(value);
456        assert_eq!(key_info.key_descriptor_version(), 0);
457        assert!(key_info.smk_message());
458        let cloned = key_info.clone();
459        assert_eq!(key_info, cloned);
460    }
461
462    #[test]
463    fn test_not_key_frame() {
464        let frame: Vec<u8> = vec![
465            0x01, 0x01, 0x00, 0x5f, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
466            0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
467            0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
468            0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
469            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
470            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
471            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
472            0x00,
473        ];
474        let result = KeyFrameRx::parse(16, &frame[..]);
475        assert_matches!(result, Err(Error::WrongEapolFrame));
476    }
477
478    #[test]
479    fn test_padding_okay() {
480        let frame: Vec<u8> = vec![
481            0x01, 0x03, 0x00, 0x63, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
482            0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
483            0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
484            0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
485            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
486            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
487            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
488            0x03, 0x01, 0x02, 0x03, 0x04,
489        ];
490        KeyFrameRx::parse(16, &frame[..]).expect("parsing keyframe failed");
491    }
492
493    #[test]
494    fn test_padding_past_pdu_len_okay() {
495        let frame: Vec<u8> = vec![
496            0x01, 0x03, 0x00, 0x62, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
497            0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
498            0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
499            0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
500            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
501            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
502            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
503            0x03, 0x01, 0x02, 0x03, 0x04,
504        ];
505        KeyFrameRx::parse(16, &frame[..]).expect("parsing keyframe failed");
506    }
507
508    #[test]
509    fn test_too_short() {
510        let frame: Vec<u8> = vec![
511            0x01, 0x03, 0x00, 0x60, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
512            0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
513            0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
514            0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
515            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
516            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
517            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
518            0x03, 0x01,
519        ];
520        let result = KeyFrameRx::parse(16, &frame[..]);
521        assert_matches!(result, Err(Error::FrameTruncated));
522    }
523
524    #[test]
525    fn test_bad_packet_body_len() {
526        let frame: Vec<u8> = vec![
527            0x01, 0x03, 0x00, 0xff, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
528            0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
529            0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
530            0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
531            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
532            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
533            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
534            0x03, 0x01, 0x02, 0x03,
535        ];
536        let result = KeyFrameRx::parse(16, &frame[..]);
537        assert_matches!(result, Err(Error::WrongPacketBodyLength(0xff, 0x62)));
538    }
539
540    #[test]
541    fn test_dynamic_mic_size() {
542        let frame: Vec<u8> = vec![
543            0x01, 0x03, 0x00, 0x72, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
544            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
545            0x07, 0x08, 0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1,
546            0x22, 0x79, 0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38,
547            0x98, 0x25, 0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00,
548            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
549            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
550            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
551            0x00, 0x00, 0x03, 0x01, 0x02, 0x03,
552        ];
553        KeyFrameRx::parse(32, &frame[..]).expect("parsing keyframe failed");
554    }
555
556    #[test]
557    fn test_as_bytes() {
558        let frame: Vec<u8> = vec![
559            0x01, 0x03, 0x00, 0x62, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
560            0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
561            0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
562            0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
563            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
564            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
565            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
566            0x03, 0x01, 0x02, 0x03,
567        ];
568        let keyframe = KeyFrameRx::parse(16, &frame[..]).expect("parsing keyframe failed");
569        verify_as_bytes_result(keyframe, false, &frame[..]);
570    }
571
572    #[test]
573    fn test_as_bytes_dynamic_mic_size() {
574        let frame: Vec<u8> = vec![
575            0x01, 0x03, 0x00, 0x72, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
576            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
577            0x07, 0x08, 0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1,
578            0x22, 0x79, 0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38,
579            0x98, 0x25, 0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00,
580            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
581            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
582            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
583            0x00, 0x00, 0x03, 0x01, 0x02, 0x03,
584        ];
585        let keyframe = KeyFrameRx::parse(32, &frame[..]).expect("parsing keyframe failed");
586        verify_as_bytes_result(keyframe, false, &frame[..]);
587    }
588
589    #[test]
590    fn test_as_bytes_buffer_too_small() {
591        let frame: Vec<u8> = vec![
592            0x01, 0x03, 0x00, 0x72, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
593            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
594            0x07, 0x08, 0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1,
595            0x22, 0x79, 0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38,
596            0x98, 0x25, 0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00,
597            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
598            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
599            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
600            0x00, 0x00, 0x03, 0x01, 0x02, 0x03,
601        ];
602        let keyframe = KeyFrameRx::parse(32, &frame[..]).expect("parsing keyframe failed");
603        let mut buf = [0u8; 40];
604        let mut writer = BufferWriter::new(&mut buf[..]);
605        let result = keyframe.write_into(true, &mut writer);
606        assert_matches!(result, Err(Error::BufferTooShort));
607    }
608
609    #[test]
610    fn test_as_bytes_clear_mic() {
611        #[rustfmt::skip]
612            let frame: Vec<u8> = vec![
613                0x01, 0x03, 0x00, 0x62, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
614                0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
615                0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
616                0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
617                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
618                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
619                // MIC
620                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
621                0x0F, 0x10,
622                0x00, 0x03, 0x01, 0x02, 0x03,
623            ];
624        let keyframe = KeyFrameRx::parse(16, &frame[..]).expect("parsing keyframe failed");
625
626        #[rustfmt::skip]
627            let expected: Vec<u8> = vec![
628                0x01, 0x03, 0x00, 0x62, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
629                0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
630                0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
631                0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
632                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
633                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
634                // Cleared MIC
635                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
636                0x00, 0x00,
637                0x00, 0x03, 0x01, 0x02, 0x03,
638            ];
639        verify_as_bytes_result(keyframe, true, &expected[..]);
640    }
641
642    fn verify_as_bytes_result(keyframe: KeyFrameRx<&[u8]>, clear_mic: bool, expected: &[u8]) {
643        let mut buf = Vec::with_capacity(128);
644        keyframe.write_into(clear_mic, &mut buf).expect("failed to convert keyframe to bytes");
645        let written = buf.len();
646        let left_over = buf.split_off(written);
647        assert_eq!(&buf[..], expected);
648        assert!(left_over.iter().all(|b| *b == 0));
649    }
650
651    #[test]
652    fn test_correct_packet() {
653        let frame: Vec<u8> = vec![
654            0x01, 0x03, 0x00, 0x62, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
655            0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
656            0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
657            0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
658            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
659            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
660            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
661            0x03, 0x01, 0x02, 0x03,
662        ];
663        let result = KeyFrameRx::parse(16, &frame[..]);
664        let keyframe = result.expect("parsing keyframe failed");
665        assert_eq!({ keyframe.eapol_fields.version }, ProtocolVersion::IEEE802DOT1X2001);
666        assert_eq!({ keyframe.eapol_fields.packet_type }, PacketType::KEY);
667        assert_eq!(keyframe.eapol_fields.packet_body_len.get(), 98);
668        assert_eq!({ keyframe.key_frame_fields.descriptor_type }, KeyDescriptor::IEEE802DOT11);
669        assert_eq!(keyframe.key_frame_fields.key_info(), KeyInformation(0x008a));
670        assert_eq!(keyframe.key_frame_fields.key_info().key_descriptor_version(), 2);
671        assert!(keyframe.key_frame_fields.key_info().key_ack());
672        assert_eq!(keyframe.key_frame_fields.key_len.get(), 16);
673        assert_eq!(keyframe.key_frame_fields.key_replay_counter.get(), 1);
674        let nonce: Vec<u8> = vec![
675            0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79, 0xfe, 0xc3, 0xb9,
676            0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25, 0xf8, 0xc7, 0xca,
677            0x55, 0x86, 0xbc, 0xda,
678        ];
679        assert_eq!(&keyframe.key_frame_fields.key_nonce[..], &nonce[..]);
680        assert_eq!(keyframe.key_frame_fields.key_rsc.get(), 0);
681        let mic = [0; 16];
682        assert_eq!(&keyframe.key_mic[..], mic);
683        let data: Vec<u8> = vec![0x01, 0x02, 0x03];
684        assert_eq!(&keyframe.key_data[..], &data[..]);
685    }
686
687    #[test]
688    fn test_correct_construct() {
689        let expected_frame: Vec<u8> = vec![
690            0x01, 0x03, 0x00, 0x62, 0x02, 0x00, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
691            0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
692            0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
693            0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
694            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
695            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
696            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
697            0x03, 0x01, 0x02, 0x03,
698        ];
699        let nonce: [u8; 32] = [
700            0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79, 0xfe, 0xc3, 0xb9,
701            0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25, 0xf8, 0xc7, 0xca,
702            0x55, 0x86, 0xbc, 0xda,
703        ];
704        let iv = [0u8; 16];
705        let data: Vec<u8> = vec![0x01, 0x02, 0x03];
706        let new_frame = KeyFrameTx::new(
707            ProtocolVersion::IEEE802DOT1X2001,
708            KeyFrameFields::new(
709                KeyDescriptor::IEEE802DOT11,
710                KeyInformation(0x008a),
711                16,
712                1,
713                nonce,
714                iv,
715                0,
716            ),
717            data,
718            16,
719        )
720        .serialize()
721        .finalize_without_mic()
722        .expect("failed to construct eapol keyframe without mic");
723        assert_eq!(&new_frame[..], &expected_frame[..]);
724    }
725
726    #[test]
727    fn test_construct_wrong_mic() {
728        let nonce: [u8; 32] = [
729            0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79, 0xfe, 0xc3, 0xb9,
730            0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25, 0xf8, 0xc7, 0xca,
731            0x55, 0x86, 0xbc, 0xda,
732        ];
733        let iv = [0u8; 16];
734        let data: Vec<u8> = vec![0x01, 0x02, 0x03];
735        let mut new_frame = KeyFrameTx::new(
736            ProtocolVersion::IEEE802DOT1X2001,
737            KeyFrameFields::new(
738                KeyDescriptor::IEEE802DOT11,
739                KeyInformation(0x018a),
740                16,
741                1,
742                nonce,
743                iv,
744                0,
745            ),
746            data,
747            16,
748        );
749        new_frame
750            .clone()
751            .serialize()
752            .finalize_without_mic()
753            .expect_err("should fail when finalizing keyframe without expected mic");
754        new_frame.key_frame_fields.key_info = U16::new(0x008a);
755        new_frame
756            .serialize()
757            .finalize_with_mic(&vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16][..])
758            .expect_err("should fail when finalizing keyframe with unexpected mic");
759    }
760
761    #[test]
762    fn test_construct_with_mic() {
763        #[rustfmt::skip]
764        let expected_frame: Vec<u8> = vec![
765            0x01, 0x03, 0x00, 0x62, 0x02, 0x01, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
766            0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
767            0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
768            0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
769            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
770            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
771            // MIC
772            0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
773            0xff, 0x00,
774            0x00, 0x03, 0x01, 0x02, 0x03,
775        ];
776        #[rustfmt::skip]
777        let zeroed_mic_frame: Vec<u8> = vec![
778            0x01, 0x03, 0x00, 0x62, 0x02, 0x01, 0x8a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
779            0x00, 0x00, 0x01, 0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79,
780            0xfe, 0xc3, 0xb9, 0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25,
781            0xf8, 0xc7, 0xca, 0x55, 0x86, 0xbc, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
782            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
783            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
784            // MIC
785            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
786            0x00, 0x00,
787            0x00, 0x03, 0x01, 0x02, 0x03,
788        ];
789        let nonce: [u8; 32] = [
790            0x39, 0x5c, 0xc7, 0x6e, 0x1a, 0xe9, 0x9f, 0xa0, 0xb1, 0x22, 0x79, 0xfe, 0xc3, 0xb9,
791            0xa9, 0x9e, 0x1d, 0x9a, 0x21, 0xb8, 0x47, 0x51, 0x38, 0x98, 0x25, 0xf8, 0xc7, 0xca,
792            0x55, 0x86, 0xbc, 0xda,
793        ];
794        let iv = [0u8; 16];
795        let data: Vec<u8> = vec![0x01, 0x02, 0x03];
796        let mic = vec![
797            0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
798            0xff, 0x00,
799        ];
800
801        let new_frame = KeyFrameTx::new(
802            ProtocolVersion::IEEE802DOT1X2001,
803            KeyFrameFields::new(
804                KeyDescriptor::IEEE802DOT11,
805                KeyInformation(0x018a),
806                16,
807                1,
808                nonce,
809                iv,
810                0,
811            ),
812            data,
813            16,
814        )
815        .serialize();
816        assert_eq!(new_frame.unfinalized_buf(), &zeroed_mic_frame[..]);
817        let new_frame = new_frame
818            .finalize_with_mic(&mic[..])
819            .expect("failed to finalize eapol keyframe with mic");
820        assert_eq!(&new_frame[..], &expected_frame[..]);
821    }
822}