-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmov: implement constant-time equality comparisons (#873)
- Loading branch information
Showing
5 changed files
with
475 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,112 @@ | ||
use crate::{Cmov, Condition}; | ||
use crate::{Cmov, CmovEq, Condition}; | ||
use core::arch::asm; | ||
|
||
macro_rules! csel { | ||
($cmp:expr, $csel:expr, $dst:expr, $src:expr, $condition:expr) => { | ||
($csel:expr, $dst:expr, $src:expr, $condition:expr) => { | ||
unsafe { | ||
asm! { | ||
$cmp, | ||
"cmp {0:w}, 0", | ||
$csel, | ||
in(reg) $condition, | ||
inlateout(reg) *$dst, | ||
in(reg) $src, | ||
in(reg) *$src, | ||
in(reg) *$dst, | ||
options(pure, nomem, nostack), | ||
}; | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! csel_eq { | ||
($instruction:expr, $lhs:expr, $rhs:expr, $condition:expr, $dst:expr) => { | ||
let mut tmp = *$dst as u16; | ||
unsafe { | ||
asm! { | ||
"eor {0:w}, {1:w}, {2:w}", | ||
"cmp {0:w}, 0", | ||
$instruction, | ||
out(reg) _, | ||
in(reg) *$lhs, | ||
in(reg) *$rhs, | ||
inlateout(reg) tmp, | ||
in(reg) $condition as u16, | ||
in(reg) tmp, | ||
options(pure, nomem, nostack), | ||
}; | ||
}; | ||
|
||
*$dst = tmp as u8; | ||
}; | ||
} | ||
|
||
impl Cmov for u16 { | ||
#[inline(always)] | ||
fn cmovnz(&mut self, value: &Self, condition: Condition) { | ||
csel!( | ||
"cmp {0:w}, 0", | ||
"csel {1:w}, {2:w}, {3:w}, NE", | ||
self, | ||
*value, | ||
condition | ||
); | ||
csel!("csel {1:w}, {2:w}, {3:w}, NE", self, value, condition); | ||
} | ||
|
||
#[inline(always)] | ||
fn cmovz(&mut self, value: &Self, condition: Condition) { | ||
csel!( | ||
"cmp {0:w}, 0", | ||
"csel {1:w}, {2:w}, {3:w}, EQ", | ||
self, | ||
*value, | ||
condition | ||
); | ||
csel!("csel {1:w}, {2:w}, {3:w}, EQ", self, value, condition); | ||
} | ||
} | ||
|
||
impl CmovEq for u16 { | ||
#[inline(always)] | ||
fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) { | ||
csel_eq!("csel {3:w}, {4:w}, {5:w}, NE", self, rhs, input, output); | ||
} | ||
|
||
#[inline(always)] | ||
fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) { | ||
csel_eq!("csel {3:w}, {4:w}, {5:w}, EQ", self, rhs, input, output); | ||
} | ||
} | ||
|
||
impl Cmov for u32 { | ||
#[inline(always)] | ||
fn cmovnz(&mut self, value: &Self, condition: Condition) { | ||
csel!( | ||
"cmp {0:w}, 0", | ||
"csel {1:w}, {2:w}, {3:w}, NE", | ||
self, | ||
*value, | ||
condition | ||
); | ||
csel!("csel {1:w}, {2:w}, {3:w}, NE", self, value, condition); | ||
} | ||
|
||
#[inline(always)] | ||
fn cmovz(&mut self, value: &Self, condition: Condition) { | ||
csel!( | ||
"cmp {0:w}, 0", | ||
"csel {1:w}, {2:w}, {3:w}, EQ", | ||
self, | ||
*value, | ||
condition | ||
); | ||
csel!("csel {1:w}, {2:w}, {3:w}, EQ", self, value, condition); | ||
} | ||
} | ||
|
||
impl CmovEq for u32 { | ||
#[inline(always)] | ||
fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) { | ||
csel_eq!("csel {3:w}, {4:w}, {5:w}, NE", self, rhs, input, output); | ||
} | ||
|
||
#[inline(always)] | ||
fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) { | ||
csel_eq!("csel {3:w}, {4:w}, {5:w}, EQ", self, rhs, input, output); | ||
} | ||
} | ||
|
||
impl Cmov for u64 { | ||
#[inline(always)] | ||
fn cmovnz(&mut self, value: &Self, condition: Condition) { | ||
csel!( | ||
"cmp {0:x}, 0", | ||
"csel {1:x}, {2:x}, {3:x}, NE", | ||
self, | ||
*value, | ||
condition | ||
); | ||
csel!("csel {1:x}, {2:x}, {3:x}, NE", self, value, condition); | ||
} | ||
|
||
#[inline(always)] | ||
fn cmovz(&mut self, value: &Self, condition: Condition) { | ||
csel!( | ||
"cmp {0:x}, 0", | ||
"csel {1:x}, {2:x}, {3:x}, EQ", | ||
self, | ||
*value, | ||
condition | ||
); | ||
csel!("csel {1:x}, {2:x}, {3:x}, EQ", self, value, condition); | ||
} | ||
} | ||
|
||
impl CmovEq for u64 { | ||
#[inline(always)] | ||
fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) { | ||
csel_eq!("csel {3:w}, {4:w}, {5:w}, NE", self, rhs, input, output); | ||
} | ||
|
||
#[inline(always)] | ||
fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) { | ||
csel_eq!("csel {3:w}, {4:w}, {5:w}, EQ", self, rhs, input, output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.