| 1 |
//! Classic implementation of constant_time_eq and constant_time_eq_n |
| 2 |
//! |
| 3 |
//! This is the implementation from classic versions of this crate. Kept for benchmarking purposes |
| 4 |
//! only, since its protection against the optimizer is weaker: a sufficiently smart optimizer |
| 5 |
//! might notice that the accumulator will not change anymore once it becomes all-ones, and |
| 6 |
//! introduce an early exit. On the other hand, this weaker protection allows the optimizer to use |
| 7 |
//! vector registers to compare many bytes in parallel, without having to write explicit SIMD code. |
| 8 |
|
| 9 |
use crate::with_dit; |
| 10 |
|
| 11 |
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 12 |
#[cfg(not(miri))] |
| 13 |
#[inline] |
| 14 |
#[must_use] |
| 15 |
fn optimizer_hide(mut value: u8) -> u8 { |
| 16 |
// SAFETY: the input value is passed unchanged to the output, the inline assembly does nothing. |
| 17 |
unsafe { |
| 18 |
core::arch::asm!("/* {0} */", inout(reg_byte) value, options(pure, nomem, nostack, preserves_flags)); |
| 19 |
value |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
#[cfg(any( |
| 24 |
target_arch = "arm", |
| 25 |
target_arch = "aarch64", |
| 26 |
target_arch = "riscv32", |
| 27 |
target_arch = "riscv64" |
| 28 |
))] |
| 29 |
#[cfg(not(miri))] |
| 30 |
#[inline] |
| 31 |
#[must_use] |
| 32 |
#[allow(asm_sub_register)] |
| 33 |
fn optimizer_hide(mut value: u8) -> u8 { |
| 34 |
// SAFETY: the input value is passed unchanged to the output, the inline assembly does nothing. |
| 35 |
unsafe { |
| 36 |
core::arch::asm!("/* {0} */", inout(reg) value, options(pure, nomem, nostack, preserves_flags)); |
| 37 |
value |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
#[cfg(any( |
| 42 |
not(any( |
| 43 |
target_arch = "x86", |
| 44 |
target_arch = "x86_64", |
| 45 |
target_arch = "arm", |
| 46 |
target_arch = "aarch64", |
| 47 |
target_arch = "riscv32", |
| 48 |
target_arch = "riscv64", |
| 49 |
)), |
| 50 |
miri, |
| 51 |
))] |
| 52 |
#[inline(never)] |
| 53 |
#[must_use] |
| 54 |
fn optimizer_hide(value: u8) -> u8 { |
| 55 |
// The current implementation of black_box in the main codegen backends is similar to |
| 56 |
// { |
| 57 |
// let result = value; |
| 58 |
// asm!("", in(reg) &result); |
| 59 |
// result |
| 60 |
// } |
| 61 |
// which round-trips the value through the stack, instead of leaving it in a register. |
| 62 |
// Experimental codegen backends might implement black_box as a pure identity function, |
| 63 |
// without the expected optimization barrier, so it's less guaranteed than inline asm. |
| 64 |
// For that reason, we also use the #[inline(never)] hint, which makes it harder for an |
| 65 |
// optimizer to look inside this function. |
| 66 |
core::hint::black_box(value) |
| 67 |
} |
| 68 |
|
| 69 |
#[inline] |
| 70 |
#[must_use] |
| 71 |
fn constant_time_ne(a: &[u8], b: &[u8]) -> u8 { |
| 72 |
assert!(a.len() == b.len()); |
| 73 |
|
| 74 |
// These useless slices make the optimizer elide the bounds checks. |
| 75 |
// See the comment in clone_from_slice() added on Rust commit 6a7bc47. |
| 76 |
let len = a.len(); |
| 77 |
let a = &a[..len]; |
| 78 |
let b = &b[..len]; |
| 79 |
|
| 80 |
let mut tmp = 0; |
| 81 |
for i in 0..len { |
| 82 |
tmp |= a[i] ^ b[i]; |
| 83 |
} |
| 84 |
|
| 85 |
// The compare with 0 must happen outside this function. |
| 86 |
optimizer_hide(tmp) |
| 87 |
} |
| 88 |
|
| 89 |
/// Compares two equal-sized byte strings in constant time. |
| 90 |
/// |
| 91 |
/// # Examples |
| 92 |
/// |
| 93 |
/// ``` |
| 94 |
/// use constant_time_eq::constant_time_eq; |
| 95 |
/// |
| 96 |
/// assert!(constant_time_eq(b"foo", b"foo")); |
| 97 |
/// assert!(!constant_time_eq(b"foo", b"bar")); |
| 98 |
/// assert!(!constant_time_eq(b"bar", b"baz")); |
| 99 |
/// # assert!(constant_time_eq(b"", b"")); |
| 100 |
/// |
| 101 |
/// // Not equal-sized, so won't take constant time. |
| 102 |
/// assert!(!constant_time_eq(b"foo", b"")); |
| 103 |
/// assert!(!constant_time_eq(b"foo", b"quux")); |
| 104 |
/// ``` |
| 105 |
#[must_use] |
| 106 |
pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { |
| 107 |
with_dit(|| a.len() == b.len() && constant_time_ne(a, b) == 0) |
| 108 |
} |
| 109 |
|
| 110 |
// Fixed-size array variant. |
| 111 |
|
| 112 |
#[inline] |
| 113 |
#[must_use] |
| 114 |
fn constant_time_ne_n<const N: usize>(a: &[u8; N], b: &[u8; N]) -> u8 { |
| 115 |
let mut tmp = 0; |
| 116 |
for i in 0..N { |
| 117 |
tmp |= a[i] ^ b[i]; |
| 118 |
} |
| 119 |
|
| 120 |
// The compare with 0 must happen outside this function. |
| 121 |
optimizer_hide(tmp) |
| 122 |
} |
| 123 |
|
| 124 |
/// Compares two fixed-size byte strings in constant time. |
| 125 |
/// |
| 126 |
/// # Examples |
| 127 |
/// |
| 128 |
/// ``` |
| 129 |
/// use constant_time_eq::constant_time_eq_n; |
| 130 |
/// |
| 131 |
/// assert!(constant_time_eq_n(&[3; 20], &[3; 20])); |
| 132 |
/// assert!(!constant_time_eq_n(&[3; 20], &[7; 20])); |
| 133 |
/// ``` |
| 134 |
#[must_use] |
| 135 |
pub fn constant_time_eq_n<const N: usize>(a: &[u8; N], b: &[u8; N]) -> bool { |
| 136 |
with_dit(|| constant_time_ne_n(a, b) == 0) |
| 137 |
} |
| 138 |
|
| 139 |
// Fixed-size variants for the most common sizes. |
| 140 |
|
| 141 |
/// Compares two 128-bit byte strings in constant time. |
| 142 |
/// |
| 143 |
/// # Examples |
| 144 |
/// |
| 145 |
/// ``` |
| 146 |
/// use constant_time_eq::constant_time_eq_16; |
| 147 |
/// |
| 148 |
/// assert!(constant_time_eq_16(&[3; 16], &[3; 16])); |
| 149 |
/// assert!(!constant_time_eq_16(&[3; 16], &[7; 16])); |
| 150 |
/// ``` |
| 151 |
#[inline] |
| 152 |
#[must_use] |
| 153 |
pub fn constant_time_eq_16(a: &[u8; 16], b: &[u8; 16]) -> bool { |
| 154 |
constant_time_eq_n(a, b) |
| 155 |
} |
| 156 |
|
| 157 |
/// Compares two 256-bit byte strings in constant time. |
| 158 |
/// |
| 159 |
/// # Examples |
| 160 |
/// |
| 161 |
/// ``` |
| 162 |
/// use constant_time_eq::constant_time_eq_32; |
| 163 |
/// |
| 164 |
/// assert!(constant_time_eq_32(&[3; 32], &[3; 32])); |
| 165 |
/// assert!(!constant_time_eq_32(&[3; 32], &[7; 32])); |
| 166 |
/// ``` |
| 167 |
#[inline] |
| 168 |
#[must_use] |
| 169 |
pub fn constant_time_eq_32(a: &[u8; 32], b: &[u8; 32]) -> bool { |
| 170 |
constant_time_eq_n(a, b) |
| 171 |
} |
| 172 |
|
| 173 |
/// Compares two 512-bit byte strings in constant time. |
| 174 |
/// |
| 175 |
/// # Examples |
| 176 |
/// |
| 177 |
/// ``` |
| 178 |
/// use constant_time_eq::constant_time_eq_64; |
| 179 |
/// |
| 180 |
/// assert!(constant_time_eq_64(&[3; 64], &[3; 64])); |
| 181 |
/// assert!(!constant_time_eq_64(&[3; 64], &[7; 64])); |
| 182 |
/// ``` |
| 183 |
#[inline] |
| 184 |
#[must_use] |
| 185 |
pub fn constant_time_eq_64(a: &[u8; 64], b: &[u8; 64]) -> bool { |
| 186 |
constant_time_eq_n(a, b) |
| 187 |
} |
| 188 |
|
| 189 |
#[cfg(test)] |
| 190 |
mod tests { |
| 191 |
#[cfg(feature = "count_instructions_test")] |
| 192 |
extern crate std; |
| 193 |
|
| 194 |
#[cfg(feature = "count_instructions_test")] |
| 195 |
#[test] |
| 196 |
fn count_optimizer_hide_instructions() -> std::io::Result<()> { |
| 197 |
use super::optimizer_hide; |
| 198 |
use count_instructions::count_instructions; |
| 199 |
|
| 200 |
fn count() -> std::io::Result<usize> { |
| 201 |
// If optimizer_hide does not work, constant propagation and folding |
| 202 |
// will make this identical to count_optimized() below. |
| 203 |
let mut count = 0; |
| 204 |
assert_eq!( |
| 205 |
10u8, |
| 206 |
count_instructions( |
| 207 |
|| optimizer_hide(1) |
| 208 |
+ optimizer_hide(2) |
| 209 |
+ optimizer_hide(3) |
| 210 |
+ optimizer_hide(4), |
| 211 |
|_| count += 1 |
| 212 |
)? |
| 213 |
); |
| 214 |
Ok(count) |
| 215 |
} |
| 216 |
|
| 217 |
fn count_optimized() -> std::io::Result<usize> { |
| 218 |
#[inline] |
| 219 |
fn inline_identity(value: u8) -> u8 { |
| 220 |
value |
| 221 |
} |
| 222 |
|
| 223 |
let mut count = 0; |
| 224 |
assert_eq!( |
| 225 |
10u8, |
| 226 |
count_instructions( |
| 227 |
|| inline_identity(1) |
| 228 |
+ inline_identity(2) |
| 229 |
+ inline_identity(3) |
| 230 |
+ inline_identity(4), |
| 231 |
|_| count += 1 |
| 232 |
)? |
| 233 |
); |
| 234 |
Ok(count) |
| 235 |
} |
| 236 |
|
| 237 |
assert!(count()? > count_optimized()?); |
| 238 |
Ok(()) |
| 239 |
} |
| 240 |
} |