| 1 |
//! SSE2/AVX implementation of `constant_time_eq` and `constant_time_eq_n`. |
| 2 |
//! |
| 3 |
//! Note: some microarchitectures split vector operations and/or vector registers larger than |
| 4 |
//! 128-bit, and might have optimizations for when one of the halves is all-zeros. To protect |
| 5 |
//! against that, only 128-bit vectors are used, even though larger vectors might be faster. |
| 6 |
|
| 7 |
use core::arch::asm; |
| 8 |
use core::mem::size_of; |
| 9 |
|
| 10 |
#[cfg(target_arch = "x86")] |
| 11 |
use core::arch::x86::*; |
| 12 |
|
| 13 |
#[cfg(target_arch = "x86_64")] |
| 14 |
use core::arch::x86_64::*; |
| 15 |
|
| 16 |
use crate::with_dit; |
| 17 |
|
| 18 |
/// Equivalent to `_mm_cmpeq_epi8`, but hidden from the compiler. |
| 19 |
/// |
| 20 |
/// The use of inline assembly instead of an intrinsic prevents a sufficiently |
| 21 |
/// smart compiler from computing the mask in other ways which might not be |
| 22 |
/// constant time (for instance, looping through the input and using branching |
| 23 |
/// to set the vector elements). |
| 24 |
#[must_use] |
| 25 |
#[inline(always)] |
| 26 |
fn cmpeq_epi8(a: __m128i, b: __m128i) -> __m128i { |
| 27 |
let mut c; |
| 28 |
// When AVX is available, the compiler will use the VEX prefix for all |
| 29 |
// SIMD instructions; do the same for this inline assembly. |
| 30 |
if cfg!(target_feature = "avx") { |
| 31 |
// SAFETY: used only when AVX is available |
| 32 |
// SAFETY: assembly instruction touches only these registers |
| 33 |
unsafe { |
| 34 |
asm!("vpcmpeqb {c}, {a}, {b}", |
| 35 |
c = lateout(xmm_reg) c, |
| 36 |
a = in(xmm_reg) a, |
| 37 |
b = in(xmm_reg) b, |
| 38 |
options(pure, nomem, preserves_flags, nostack)); |
| 39 |
} |
| 40 |
} else { |
| 41 |
// SAFETY: this file is compiled only when SSE2 is available |
| 42 |
// SAFETY: assembly instruction touches only these registers |
| 43 |
unsafe { |
| 44 |
asm!("pcmpeqb {a}, {b}", |
| 45 |
a = inlateout(xmm_reg) a => c, |
| 46 |
b = in(xmm_reg) b, |
| 47 |
options(pure, nomem, preserves_flags, nostack)); |
| 48 |
} |
| 49 |
} |
| 50 |
c |
| 51 |
} |
| 52 |
|
| 53 |
/// Equivalent to `_mm_and_si128`, but hidden from the compiler. |
| 54 |
/// |
| 55 |
/// The use of inline assembly instead of an intrinsic prevents a sufficiently |
| 56 |
/// smart compiler from short circuiting the computation once the mask becomes |
| 57 |
/// all zeros. |
| 58 |
#[must_use] |
| 59 |
#[inline(always)] |
| 60 |
fn and_si128(a: __m128i, b: __m128i) -> __m128i { |
| 61 |
let mut c; |
| 62 |
// When AVX is available, the compiler will use the VEX prefix for all |
| 63 |
// SIMD instructions; do the same for this inline assembly. |
| 64 |
if cfg!(target_feature = "avx") { |
| 65 |
// SAFETY: used only when AVX is available |
| 66 |
// SAFETY: assembly instruction touches only these registers |
| 67 |
unsafe { |
| 68 |
asm!("vpand {c}, {a}, {b}", |
| 69 |
c = lateout(xmm_reg) c, |
| 70 |
a = in(xmm_reg) a, |
| 71 |
b = in(xmm_reg) b, |
| 72 |
options(pure, nomem, preserves_flags, nostack)); |
| 73 |
} |
| 74 |
} else { |
| 75 |
// SAFETY: this file is compiled only when SSE2 is available |
| 76 |
// SAFETY: assembly instruction touches only these registers |
| 77 |
unsafe { |
| 78 |
asm!("pand {a}, {b}", |
| 79 |
a = inlateout(xmm_reg) a => c, |
| 80 |
b = in(xmm_reg) b, |
| 81 |
options(pure, nomem, preserves_flags, nostack)); |
| 82 |
} |
| 83 |
} |
| 84 |
c |
| 85 |
} |
| 86 |
|
| 87 |
/// Equivalent to `_mm_movemask_epi8`, but hidden from the compiler. |
| 88 |
/// |
| 89 |
/// The use of inline assembly instead of an intrinsic prevents a sufficiently |
| 90 |
/// smart compiler from extracting the mask in other ways which might not be |
| 91 |
/// constant time (for instance, looping through the elements of the vector). |
| 92 |
#[must_use] |
| 93 |
#[inline(always)] |
| 94 |
fn movemask_epi8(a: __m128i) -> u32 { |
| 95 |
let mut mask; |
| 96 |
// When AVX is available, the compiler will use the VEX prefix for all |
| 97 |
// SIMD instructions; do the same for this inline assembly. |
| 98 |
if cfg!(target_feature = "avx") { |
| 99 |
// SAFETY: used only when AVX is available |
| 100 |
// SAFETY: assembly instruction touches only these registers |
| 101 |
// SAFETY: 32-bit operations zero-extend the 64-bit register |
| 102 |
unsafe { |
| 103 |
asm!("vpmovmskb {mask:e}, {a}", |
| 104 |
mask = lateout(reg) mask, |
| 105 |
a = in(xmm_reg) a, |
| 106 |
options(pure, nomem, preserves_flags, nostack)); |
| 107 |
} |
| 108 |
} else { |
| 109 |
// SAFETY: this file is compiled only when SSE2 is available |
| 110 |
// SAFETY: assembly instruction touches only these registers |
| 111 |
// SAFETY: 32-bit operations zero-extend the 64-bit register |
| 112 |
unsafe { |
| 113 |
asm!("pmovmskb {mask:e}, {a}", |
| 114 |
mask = lateout(reg) mask, |
| 115 |
a = in(xmm_reg) a, |
| 116 |
options(pure, nomem, preserves_flags, nostack)); |
| 117 |
} |
| 118 |
} |
| 119 |
// The return type is u32 instead of i32 to avoid a sign extension. |
| 120 |
mask |
| 121 |
} |
| 122 |
|
| 123 |
/// Safe equivalent to `_mm_loadu_si128` for byte slices. |
| 124 |
#[must_use] |
| 125 |
#[inline(always)] |
| 126 |
fn loadu_si128(src: &[u8]) -> __m128i { |
| 127 |
assert_eq!(src.len(), size_of::<__m128i>()); |
| 128 |
|
| 129 |
// SAFETY: this file is compiled only when SSE2 is available |
| 130 |
// SAFETY: the slice has enough bytes for a __m128i |
| 131 |
unsafe { _mm_loadu_si128(src.as_ptr().cast::<__m128i>()) } |
| 132 |
} |
| 133 |
|
| 134 |
/// SSE2/AVX implementation of `constant_time_eq` and `constant_time_eq_n`. |
| 135 |
#[must_use] |
| 136 |
#[inline(always)] |
| 137 |
fn constant_time_eq_sse2(mut a: &[u8], mut b: &[u8]) -> bool { |
| 138 |
if a.len() != b.len() { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
// This statement does nothing, because a.len() == b.len() here, |
| 143 |
// but it makes the optimizer elide some useless bounds checks. |
| 144 |
b = &b[..a.len()]; |
| 145 |
|
| 146 |
const LANES: usize = size_of::<__m128i>(); |
| 147 |
|
| 148 |
let tmp = if a.len() >= LANES * 2 { |
| 149 |
let tmpa0 = loadu_si128(&a[..LANES]); |
| 150 |
let tmpb0 = loadu_si128(&b[..LANES]); |
| 151 |
let tmpa1 = loadu_si128(&a[LANES..LANES * 2]); |
| 152 |
let tmpb1 = loadu_si128(&b[LANES..LANES * 2]); |
| 153 |
|
| 154 |
a = &a[LANES * 2..]; |
| 155 |
b = &b[LANES * 2..]; |
| 156 |
|
| 157 |
let mut mask0 = cmpeq_epi8(tmpa0, tmpb0); |
| 158 |
let mut mask1 = cmpeq_epi8(tmpa1, tmpb1); |
| 159 |
|
| 160 |
while a.len() >= LANES * 2 { |
| 161 |
let tmpa0 = loadu_si128(&a[..LANES]); |
| 162 |
let tmpb0 = loadu_si128(&b[..LANES]); |
| 163 |
let tmpa1 = loadu_si128(&a[LANES..LANES * 2]); |
| 164 |
let tmpb1 = loadu_si128(&b[LANES..LANES * 2]); |
| 165 |
|
| 166 |
a = &a[LANES * 2..]; |
| 167 |
b = &b[LANES * 2..]; |
| 168 |
|
| 169 |
let tmp0 = cmpeq_epi8(tmpa0, tmpb0); |
| 170 |
let tmp1 = cmpeq_epi8(tmpa1, tmpb1); |
| 171 |
|
| 172 |
mask0 = and_si128(mask0, tmp0); |
| 173 |
mask1 = and_si128(mask1, tmp1); |
| 174 |
} |
| 175 |
|
| 176 |
if a.len() >= LANES { |
| 177 |
let tmpa = loadu_si128(&a[..LANES]); |
| 178 |
let tmpb = loadu_si128(&b[..LANES]); |
| 179 |
|
| 180 |
a = &a[LANES..]; |
| 181 |
b = &b[LANES..]; |
| 182 |
|
| 183 |
let tmp = cmpeq_epi8(tmpa, tmpb); |
| 184 |
|
| 185 |
mask0 = and_si128(mask0, tmp); |
| 186 |
} |
| 187 |
|
| 188 |
let mask = and_si128(mask0, mask1); |
| 189 |
movemask_epi8(mask) ^ 0xFFFF |
| 190 |
} else if a.len() >= LANES { |
| 191 |
let tmpa = loadu_si128(&a[..LANES]); |
| 192 |
let tmpb = loadu_si128(&b[..LANES]); |
| 193 |
|
| 194 |
a = &a[LANES..]; |
| 195 |
b = &b[LANES..]; |
| 196 |
|
| 197 |
let mask = cmpeq_epi8(tmpa, tmpb); |
| 198 |
|
| 199 |
movemask_epi8(mask) ^ 0xFFFF |
| 200 |
} else { |
| 201 |
0 |
| 202 |
}; |
| 203 |
|
| 204 |
// Note: be careful to not short-circuit ("tmp == 0 &&") the comparison here |
| 205 |
crate::generic::constant_time_eq_impl(a, b, tmp.into()) |
| 206 |
} |
| 207 |
|
| 208 |
#[must_use] |
| 209 |
pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { |
| 210 |
with_dit(|| constant_time_eq_sse2(a, b)) |
| 211 |
} |
| 212 |
|
| 213 |
#[must_use] |
| 214 |
pub fn constant_time_eq_n<const N: usize>(a: &[u8; N], b: &[u8; N]) -> bool { |
| 215 |
with_dit(|| constant_time_eq_sse2(&a[..], &b[..])) |
| 216 |
} |