forked from briansmith/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstant_time_tests.rs
80 lines (71 loc) · 2.91 KB
/
constant_time_tests.rs
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
// Copyright 2020 Brian Smith.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use ring::{constant_time, error, rand};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test_configure!(run_in_browser);
// This logic is loosly based on BoringSSL's `TEST(ConstantTimeTest, MemCmp)`.
#[test]
fn test_verify_slices_are_equal() {
let initial: [u8; 256] = rand::generate(&rand::SystemRandom::new()).unwrap().expose();
{
let copy = initial;
for len in 0..copy.len() {
// Not equal because the lengths do not match.
assert_eq!(
constant_time::verify_slices_are_equal(&initial, ©[..len]),
Err(error::Unspecified)
);
// Equal lengths and equal contents.
assert_eq!(
constant_time::verify_slices_are_equal(&initial[..len], ©[..len]),
Ok(())
);
}
// Equal lengths and equal contents.
assert_eq!(
constant_time::verify_slices_are_equal(&initial, ©),
Ok(())
);
}
for i in 0..initial.len() {
for bit in 0..8 {
let mut copy = initial;
copy[i] ^= 1u8 << bit;
for len in 0..=initial.len() {
// We flipped at least one bit in `copy`.
assert_ne!(&initial[..], ©[..]);
let a = &initial[..len];
let b = ©[..len];
let expected_result = if i < len {
// The flipped bit is within `b` so `a` and `b` are not equal.
Err(error::Unspecified)
} else {
// The flipped bit is outside of `b` so `a` and `b` are equal.
Ok(())
};
assert_eq!(a == b, expected_result.is_ok()); // Sanity check.
assert_eq!(
constant_time::verify_slices_are_equal(a, b),
expected_result
);
assert_eq!(
constant_time::verify_slices_are_equal(b, a),
expected_result
);
}
}
}
}