| 1 |
#[cfg(feature = "count_instructions_test")] |
| 2 |
mod tests { |
| 3 |
use std::io::Result; |
| 4 |
|
| 5 |
use constant_time_eq::{constant_time_eq, constant_time_eq_n}; |
| 6 |
use count_instructions::{Address, count_instructions}; |
| 7 |
|
| 8 |
fn detect_features() { |
| 9 |
// The first time with_dit() is called, the feature detection will run. |
| 10 |
// That will lead to a difference in the number of instructions, which |
| 11 |
// is unrelated to the data being compared. |
| 12 |
// Run a dummy comparison at least once before each test to avoid this. |
| 13 |
let _ = constant_time_eq(b"", b""); |
| 14 |
} |
| 15 |
|
| 16 |
#[inline(never)] |
| 17 |
fn count(l: &[u8], r: &[u8], capacity: usize) -> Result<Vec<Address>> { |
| 18 |
let mut addresses = Vec::with_capacity(capacity); |
| 19 |
assert!(!count_instructions( |
| 20 |
|| constant_time_eq(l, r), |
| 21 |
|instruction| addresses.push(instruction.address()) |
| 22 |
)?); |
| 23 |
Ok(addresses) |
| 24 |
} |
| 25 |
|
| 26 |
#[inline(never)] |
| 27 |
fn count_n<const N: usize>(l: &[u8; N], r: &[u8; N], capacity: usize) -> Result<Vec<Address>> { |
| 28 |
let mut addresses = Vec::with_capacity(capacity); |
| 29 |
assert!(!count_instructions( |
| 30 |
|| constant_time_eq_n(l, r), |
| 31 |
|instruction| addresses.push(instruction.address()) |
| 32 |
)?); |
| 33 |
Ok(addresses) |
| 34 |
} |
| 35 |
|
| 36 |
fn test(a: u8, b: u8) -> Result<()> { |
| 37 |
detect_features(); |
| 38 |
|
| 39 |
const N: usize = 64; |
| 40 |
let l = vec![a; N]; |
| 41 |
let r = vec![b; N]; |
| 42 |
let baseline = count(&l, &r, 0)?; |
| 43 |
|
| 44 |
let mut t = r.clone(); |
| 45 |
for n in 0..(N - 1) { |
| 46 |
t[n] = a; |
| 47 |
assert_eq!(count(&l, &t, baseline.len())?, baseline); |
| 48 |
} |
| 49 |
|
| 50 |
t[N - 1] = a; |
| 51 |
assert!(constant_time_eq(&l, &t)); |
| 52 |
|
| 53 |
let mut t = r.clone(); |
| 54 |
for n in 1..N { |
| 55 |
t[N - n] = a; |
| 56 |
assert_eq!(count(&l, &t, baseline.len())?, baseline); |
| 57 |
} |
| 58 |
|
| 59 |
t[0] = a; |
| 60 |
assert!(constant_time_eq(&l, &t)); |
| 61 |
|
| 62 |
Ok(()) |
| 63 |
} |
| 64 |
|
| 65 |
fn test_n<const N: usize>(a: u8, b: u8) -> Result<()> { |
| 66 |
detect_features(); |
| 67 |
|
| 68 |
let l = [a; N]; |
| 69 |
let r = [b; N]; |
| 70 |
let baseline = count_n(&l, &r, 0)?; |
| 71 |
|
| 72 |
let mut t = r.clone(); |
| 73 |
for n in 0..(N - 1) { |
| 74 |
t[n] = a; |
| 75 |
assert_eq!(count_n(&l, &t, baseline.len())?, baseline); |
| 76 |
} |
| 77 |
|
| 78 |
t[N - 1] = a; |
| 79 |
assert!(constant_time_eq_n(&l, &t)); |
| 80 |
|
| 81 |
let mut t = r.clone(); |
| 82 |
for n in 1..N { |
| 83 |
t[N - n] = a; |
| 84 |
assert_eq!(count_n(&l, &t, baseline.len())?, baseline); |
| 85 |
} |
| 86 |
|
| 87 |
t[0] = a; |
| 88 |
assert!(constant_time_eq_n(&l, &t)); |
| 89 |
|
| 90 |
Ok(()) |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn count_instructions_test() -> Result<()> { |
| 95 |
test(b'A', b'B')?; |
| 96 |
test(0x55, 0xAA)?; |
| 97 |
Ok(()) |
| 98 |
} |
| 99 |
|
| 100 |
fn count_instructions_test_n<const N: usize>() -> Result<()> { |
| 101 |
test_n::<N>(b'A', b'B')?; |
| 102 |
test_n::<N>(0x55, 0xAA)?; |
| 103 |
Ok(()) |
| 104 |
} |
| 105 |
|
| 106 |
#[test] |
| 107 |
fn count_instructions_test_n_16() -> Result<()> { |
| 108 |
count_instructions_test_n::<16>() |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn count_instructions_test_n_20() -> Result<()> { |
| 113 |
count_instructions_test_n::<20>() |
| 114 |
} |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn count_instructions_test_n_24() -> Result<()> { |
| 118 |
count_instructions_test_n::<24>() |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn count_instructions_test_n_32() -> Result<()> { |
| 123 |
count_instructions_test_n::<32>() |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn count_instructions_test_n_48() -> Result<()> { |
| 128 |
count_instructions_test_n::<48>() |
| 129 |
} |
| 130 |
|
| 131 |
#[test] |
| 132 |
fn count_instructions_test_n_64() -> Result<()> { |
| 133 |
count_instructions_test_n::<64>() |
| 134 |
} |
| 135 |
|
| 136 |
// This silly test shows that count_instructions() can detect early returns. |
| 137 |
#[test] |
| 138 |
fn count_instructions_test_variable() -> Result<()> { |
| 139 |
#[inline(never)] |
| 140 |
fn variable_time_eq(a: &[u8], b: &[u8]) -> bool { |
| 141 |
if a.len() != b.len() { |
| 142 |
false |
| 143 |
} else { |
| 144 |
for i in 0..a.len() { |
| 145 |
if a[i] != b[i] { |
| 146 |
return false; |
| 147 |
} |
| 148 |
} |
| 149 |
true |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
#[inline(never)] |
| 154 |
fn count_variable(l: &[u8], r: &[u8], capacity: usize) -> Result<Vec<Address>> { |
| 155 |
let mut addresses = Vec::with_capacity(capacity); |
| 156 |
assert!(!count_instructions( |
| 157 |
|| variable_time_eq(l, r), |
| 158 |
|instruction| addresses.push(instruction.address()) |
| 159 |
)?); |
| 160 |
Ok(addresses) |
| 161 |
} |
| 162 |
|
| 163 |
const N: usize = 64; |
| 164 |
let l = vec![b'A'; N]; |
| 165 |
let r = vec![b'B'; N]; |
| 166 |
|
| 167 |
let mut t = r.clone(); |
| 168 |
t[0] = b'A'; |
| 169 |
let short = count_variable(&l, &t, 0)?; |
| 170 |
|
| 171 |
let mut t = l.clone(); |
| 172 |
t[N - 1] = b'B'; |
| 173 |
let long = count_variable(&l, &t, short.len())?; |
| 174 |
|
| 175 |
assert_ne!(short, long); |
| 176 |
Ok(()) |
| 177 |
} |
| 178 |
} |