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
//! This module provides access to libsodium utility and memory functions

use super::{check_init, secbuf::SecBuf};
use lib3h_crypto_api::CryptoError;

/// Check if length of buffer is of approprate size
/// it should be either of size 8,16,32 or 64
pub fn check_buf_len(sb: usize) -> bool {
    sb != 8 && sb != 16 && sb != 32 && sb != 64
}

impl SecBuf {
    /// Return true if memory is only zeroes, i.e. [0,0,0,0,0,0,0,0]
    fn is_zero(&mut self) -> bool {
        check_init();
        let mut a = self.write_lock();
        unsafe { rust_sodium_sys::sodium_is_zero(raw_ptr_char!(a), a.len()) == 1 }
    }

    /// Zero all memory
    pub fn zero(&mut self) {
        check_init();
        let mut b = self.write_lock();
        unsafe {
            rust_sodium_sys::sodium_memzero(raw_ptr_void!(b), b.len());
        }
    }

    /// Increments all memory by 1
    pub fn increment(&mut self) {
        check_init();
        let mut b = self.write_lock();
        unsafe {
            rust_sodium_sys::sodium_increment(raw_ptr_char!(b), b.len());
        }
    }

    /// Compares two SecBuf
    /// Return :
    /// | if a > b; return 1
    /// | if a < b; return -1
    /// | if a == b; return 0
    pub fn compare(&mut self, b: &mut SecBuf) -> i32 {
        check_init();
        let mut a = self.write_lock();
        let mut b = b.write_lock();
        unsafe { rust_sodium_sys::sodium_compare(raw_ptr_char!(a), raw_ptr_char!(b), a.len()) }
    }

    /// Load the [u8] into the SecBuf
    pub fn from_array(&mut self, data: &[u8]) -> Result<(), CryptoError> {
        if (data.len() != self.len()) {
            return Err(CryptoError::Generic(
                "Input does not have same size as SecBuf".to_string(),
            ));
        }
        self.write(0, data)
    }
}

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

    #[test]
    fn it_should_zero_buffer() {
        let mut b = SecBuf::with_insecure(1);
        {
            let mut b = b.write_lock();
            b[0] = 42;
        }
        b.zero();
        {
            let b = b.read_lock();
            assert_eq!(0, b[0]);
        }
    }

    #[test]
    fn it_should_increment_buffer() {
        let mut b = SecBuf::with_insecure(1);
        {
            let mut b = b.write_lock();
            b[0] = 42;
        }
        b.increment();
        {
            let b = b.read_lock();
            assert_eq!(43, b[0]);
        }
    }

    #[test]
    fn it_should_compare_buffer() {
        let mut a = SecBuf::with_insecure(1);
        {
            let mut a = a.write_lock();
            a[0] = 50;
        }
        let mut b = SecBuf::with_insecure(1);
        {
            let mut b = b.write_lock();
            b[0] = 45;
        }
        let mut c = SecBuf::with_insecure(1);
        {
            let mut c = c.write_lock();
            c[0] = 45;
        }
        let val_1 = a.compare(&mut b);
        let val_2 = b.compare(&mut a);
        let val_3 = b.compare(&mut c);
        assert_eq!(1, val_1);
        assert_eq!(-1, val_2);
        assert_eq!(0, val_3);
    }

    #[test]
    fn it_should_be_zero() {
        let mut buf = SecBuf::with_insecure(4);
        assert!(buf.is_zero());
        buf.increment();
        assert!(!buf.is_zero());
        buf.zero();
        assert!(buf.is_zero());
    }

    #[test]
    fn it_should_from_array() {
        let mut b = SecBuf::with_insecure(4);
        let bad_array = vec![42];
        let good_array = vec![0, 1, 2, 3];
        // Wrong size should give error
        let res = b.from_array(&bad_array);
        assert!(res.is_err());
        // Correct size should copy
        b.from_array(&good_array).unwrap();
        let b = b.read_lock();
        assert_eq!("[0, 1, 2, 3]", format!("{:?}", *b));
    }
}