wlan_rsn/key/
mod.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
5pub mod exchange;
6pub mod gtk;
7pub mod igtk;
8pub mod ptk;
9
10use mundane::bytes;
11
12pub trait Tk {
13    fn tk(&self) -> &[u8];
14
15    fn eq_tk(&self, other: &impl Tk) -> bool {
16        // Constant-time equivalence is used to prevent timing attacks.
17        bytes::constant_time_eq(self.tk(), other.tk())
18    }
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24    use hex::FromHex;
25
26    #[derive(Clone)]
27    struct DummyTk {
28        tk_field: Vec<u8>,
29    }
30    impl Tk for DummyTk {
31        fn tk(&self) -> &[u8] {
32            &self.tk_field[..]
33        }
34    }
35
36    #[test]
37    fn test_eq_tk() {
38        let dummy_tk_a =
39            DummyTk { tk_field: Vec::from_hex("aaaaaaaa").expect("could not make Vec") };
40        let dummy_tk_equivalent_a =
41            DummyTk { tk_field: Vec::from_hex("aaaaaaaa").expect("could not make Vec") };
42        let dummy_tk_b =
43            DummyTk { tk_field: Vec::from_hex("bbbbbbbb").expect("could not make Vec") };
44
45        assert!(dummy_tk_a.eq_tk(&dummy_tk_equivalent_a));
46        assert!(!dummy_tk_a.eq_tk(&dummy_tk_b));
47    }
48}