1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

pub mod exchange;
pub mod gtk;
pub mod igtk;
pub mod ptk;

use mundane::bytes;

pub trait Tk {
    fn tk(&self) -> &[u8];

    fn eq_tk(&self, other: &impl Tk) -> bool {
        // Constant-time equivalence is used to prevent timing attacks.
        bytes::constant_time_eq(self.tk(), other.tk())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use hex::FromHex;

    #[derive(Clone)]
    struct DummyTk {
        tk_field: Vec<u8>,
    }
    impl Tk for DummyTk {
        fn tk(&self) -> &[u8] {
            &self.tk_field[..]
        }
    }

    #[test]
    fn test_eq_tk() {
        let dummy_tk_a =
            DummyTk { tk_field: Vec::from_hex("aaaaaaaa").expect("could not make Vec") };
        let dummy_tk_equivalent_a =
            DummyTk { tk_field: Vec::from_hex("aaaaaaaa").expect("could not make Vec") };
        let dummy_tk_b =
            DummyTk { tk_field: Vec::from_hex("bbbbbbbb").expect("could not make Vec") };

        assert!(dummy_tk_a.eq_tk(&dummy_tk_equivalent_a));
        assert!(!dummy_tk_a.eq_tk(&dummy_tk_b));
    }
}