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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use core::{ fmt, mem };
use core::cmp::{ self, Ordering };
use memsec::{ memeq, memcmp };


/// Constant Time Compare
///
/// # Note
///
/// it compare memory value.
pub struct CmpKey<T: ?Sized + 'static>(pub T);


impl<T: ?Sized> CmpKey<T> {
    pub fn from(t: &T) -> &CmpKey<T> {
        unsafe { mem::transmute(t) }
    }
}


impl<T: ?Sized> fmt::Debug for CmpKey<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("CmpKey")
            .field(&format_args!("{:p}", &self.0))
            .finish()
    }
}


impl<T: ?Sized> PartialEq<T> for CmpKey<T> {
    fn eq(&self, rhs: &T) -> bool {
        let len1 = mem::size_of_val(&self.0);
        let len2 = mem::size_of_val(rhs);

        let r = unsafe { memeq(
            &self.0 as *const T as *const u8,
            rhs as *const T as *const u8,
            cmp::min(len1, len2)
        ) };
        len1 == len2 && r
    }
}

impl<T: ?Sized> PartialEq<CmpKey<T>> for CmpKey<T> {
    fn eq(&self, &CmpKey(ref rhs): &CmpKey<T>) -> bool {
        self.eq(rhs)
    }
}

impl<T: ?Sized> Eq for CmpKey<T> {}

impl<T: ?Sized> PartialOrd<T> for CmpKey<T> {
    fn partial_cmp(&self, rhs: &T) -> Option<Ordering> {
        self.partial_cmp(CmpKey::from(rhs))
    }
}

impl<T: ?Sized> PartialOrd<CmpKey<T>> for CmpKey<T> {
    fn partial_cmp(&self, rhs: &CmpKey<T>) -> Option<Ordering> {
        Some(self.cmp(rhs))
    }
}

impl<T: ?Sized> Ord for CmpKey<T> {
    fn cmp(&self, &CmpKey(ref rhs): &CmpKey<T>) -> Ordering {
        let len1 = mem::size_of_val(&self.0);
        let len2 = mem::size_of_val(rhs);

        let order = unsafe { memcmp(
            &self.0 as *const T as *const u8,
            rhs as *const T as *const u8,
            cmp::min(len1, len2))
        };

        let r = len1.cmp(&len2);
        match order.cmp(&0) {
            Ordering::Equal => r,
            order => order
        }
    }
}