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
use ffi;
pub fn memzero(x: &mut [u8]) {
unsafe {
ffi::sodium_memzero(x.as_mut_ptr(), x.len());
}
}
pub fn memcmp(x: &[u8], y: &[u8]) -> bool {
if x.len() != y.len() {
return false
}
unsafe {
ffi::sodium_memcmp(x.as_ptr(), y.as_ptr(), x.len()) == 0
}
}
pub fn increment_le(x: &mut [u8]) {
unsafe {
ffi::sodium_increment(x.as_mut_ptr(), x.len());
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_memcmp() {
use randombytes::randombytes;
for i in 0usize..256 {
let x = randombytes(i);
assert!(memcmp(&x, &x));
let mut y = x.clone();
assert!(memcmp(&x, &y));
y.push(0);
assert!(!memcmp(&x, &y));
assert!(!memcmp(&y, &x));
y = randombytes(i);
if x == y {
assert!(memcmp(&x, &y))
} else {
assert!(!memcmp(&x, &y))
}
}
}
#[test]
fn test_increment_le_zero() {
for i in 1usize..256 {
let mut x = vec!(0u8; i);
increment_le(&mut x);
assert!(!x.iter().all(|x| { *x == 0 }));
let mut y = vec!(0u8; i);
y[0] += 1;
assert_eq!(x, y);
}
}
#[test]
fn test_increment_le_vectors() {
let mut x = [255, 2, 3, 4, 5];
let y = [0, 3, 3, 4, 5];
increment_le(&mut x);
assert!(!x.iter().all(|x| { *x == 0 }));
assert_eq!(x, y);
let mut x = [255, 255, 3, 4, 5];
let y = [0, 0, 4, 4, 5];
increment_le(&mut x);
assert!(!x.iter().all(|x| { *x == 0 }));
assert_eq!(x, y);
let mut x = [255, 255, 255, 4, 5];
let y = [0, 0, 0, 5, 5];
increment_le(&mut x);
assert!(!x.iter().all(|x| { *x == 0 }));
assert_eq!(x, y);
let mut x = [255, 255, 255, 255, 5];
let y = [0, 0, 0, 0, 6];
increment_le(&mut x);
assert!(!x.iter().all(|x| { *x == 0 }));
assert_eq!(x, y);
let mut x = [255, 255, 255, 255, 255];
let y = [0, 0, 0, 0, 0];
increment_le(&mut x);
assert!(x.iter().all(|x| { *x == 0 }));
assert_eq!(x, y);
}
#[test]
fn test_increment_le_overflow() {
for i in 1usize..256 {
let mut x = vec!(255u8; i);
increment_le(&mut x);
assert!(x.iter().all(|xi| { *xi == 0 }));
}
}
}