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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! Util functions for constant time comparison and memory wiping.
//!
//! [Official documentation](https://monocypher.org/manual/wipe)

use ffi;
use std::os::raw::c_void;

/// Constant time comparison of two equal sized buffers.
///
/// The lengths can be 16, 32 or 64. Everything else will return false.
/// If the length or the buffer content differ false will be returned.
///
/// # Example
///
/// ```
/// use monocypher::utils::verify;
///
/// if verify("one".as_bytes(), "one".as_bytes()) {
///     //continue
/// } else {
///     //abort
/// }
/// ```
#[inline]
pub fn verify(a: &[u8], b: &[u8]) -> bool {
    a.len() == b.len() && verify_internal(a, b) == 0
}

#[inline(never)]
fn verify_internal(a: &[u8], b: &[u8]) -> u8 {
    //be paranoid here
    if a.len() != b.len() {
        return 1;
    }
    //"useless", but lets the optimizer skip bounds checks.
    let len = a.len();
    let a = &a[..len];
    let b = &b[..len];

    let cmp = {
        let mut ret = 0;
        for i in 0..len {
            ret |= a[i] ^ b[i]
        }
        ret
    };

    match len {
        16 => return cmp,
        32 => return cmp,
        64 => return cmp,
        _ => return 1,
    }
}

/// Clears a memory region.
///
/// # Example
///
/// ```
/// use monocypher::utils::wipe;
///
/// let mut secret: [u8; 16] = [255; 16];
/// wipe(&mut secret);
/// ```
pub fn wipe(secret: &mut [u8]) {
    unsafe { ffi::crypto_wipe(secret.as_mut_ptr() as *mut c_void, secret.len()) }
}

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

    #[test]
    fn wipe() {
        let mut a: [u8; 16] = [0; 16];

        for i in 0..a.len() {
            a[i] = i as u8;
        }

        for i in 0..a.len() {
            assert_eq!(a[i], i as u8);
        }

        ::utils::wipe(&mut a);

        for i in 0..a.len() {
            assert_eq!(a[i], 0);
        }
    }

    #[test]
    fn verify_mix() {
        let a = [4; 16];
        let b = [4; 32];
        assert_eq!(verify(&a, &b), false)
    }

    #[test]
    fn verify16() {
        let a = [1u8; 16];
        let b = [1u8; 16];

        assert!(verify(&a, &b))
    }

    #[test]
    fn verify16_fail() {
        let a = [1u8; 16];
        let b = [3u8; 16];

        assert_eq!(verify(&a, &b), false)
    }

    #[test]
    fn verify32() {
        let a = [1u8; 32];
        let b = [1u8; 32];

        assert!(verify(&a, &b))
    }

    #[test]
    fn verify32_fail() {
        let a = [1u8; 32];
        let b = [3u8; 32];

        assert_eq!(verify(&a, &b), false)
    }

    #[test]
    fn verify64() {
        let a = [1u8; 64];
        let b = [1u8; 64];

        assert!(verify(&a, &b))
    }

    #[test]
    fn verify64_fail() {
        let a = [1u8; 64];
        let b = [3u8; 64];

        assert_eq!(verify(&a, &b), false)
    }

    #[test]
    fn verify_unsupported_length() {
        let a = [1u8; 1];
        let b = [1u8; 1];

        assert_eq!(verify(&a, &b), false)
    }
}