fix: use branchless operations in constant_time_eq

- Use bitwise & instead of && to avoid short-circuit timing leak
- Use get().unwrap_or(&0) instead of if/else for branchless byte access

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
fettpl 2026-02-15 00:30:23 +01:00
parent 6776373e8e
commit 2f2f56fc0c

View file

@ -190,11 +190,11 @@ pub fn constant_time_eq(a: &str, b: &str) -> bool {
let max_len = a.len().max(b.len());
let mut byte_diff = 0u8;
for i in 0..max_len {
let x = if i < a.len() { a[i] } else { 0 };
let y = if i < b.len() { b[i] } else { 0 };
let x = *a.get(i).unwrap_or(&0);
let y = *b.get(i).unwrap_or(&0);
byte_diff |= x ^ y;
}
len_diff == 0 && byte_diff == 0
(len_diff == 0) & (byte_diff == 0)
}
/// Check if a host string represents a non-localhost bind address.