pub struct TpBool(/* private fields */);
Expand description
A boolean type that prevents its value from being leaked to attackers through timing information.
let protected = TpBool::protect(some_boolean);
// Use `protected` from now on instead of `some_boolean`
Use the protect
method as early as possible in the computation for maximum protection:
// DANGEROUS:
let badly_protected_boolean = TpU8::protect(some_boolean as u8);
// Safe:
let protected = TpBool::protect(some_boolean).as_u8();
// DANGEROUS:
let badly_protected_value = TpBool::protect(byte1 == byte2);
// Safe:
let protected_bool = TpU8::protect(byte1).tp_eq(&TpU8::protect(byte2));
Note that &
and |
are provided instead of &&
and ||
because the usual boolean
short-circuiting behaviour leaks information about the values of the booleans.
Implementations§
Source§impl TpBool
impl TpBool
Sourcepub fn protect(input: bool) -> Self
pub fn protect(input: bool) -> Self
Hide input
behind a protective abstraction to prevent the value from being used
in such a way that the value could leak out via a timing side channel.
let protected_bool = TpBool::protect(some_secret_bool);
// Use `protected_bool` instead of `some_secret_bool` to avoid timing leaks
Sourcepub fn as_u8(self) -> TpU8
pub fn as_u8(self) -> TpU8
Casts from one number type to another, following the same conventions as Rust’s as
keyword.
Sourcepub fn as_u16(self) -> TpU16
pub fn as_u16(self) -> TpU16
Casts from one number type to another, following the same conventions as Rust’s as
keyword.
Sourcepub fn as_u32(self) -> TpU32
pub fn as_u32(self) -> TpU32
Casts from one number type to another, following the same conventions as Rust’s as
keyword.
Sourcepub fn as_u64(self) -> TpU64
pub fn as_u64(self) -> TpU64
Casts from one number type to another, following the same conventions as Rust’s as
keyword.
Sourcepub fn as_i8(self) -> TpI8
pub fn as_i8(self) -> TpI8
Casts from one number type to another, following the same conventions as Rust’s as
keyword.
Sourcepub fn as_i16(self) -> TpI16
pub fn as_i16(self) -> TpI16
Casts from one number type to another, following the same conventions as Rust’s as
keyword.
Sourcepub fn as_i32(self) -> TpI32
pub fn as_i32(self) -> TpI32
Casts from one number type to another, following the same conventions as Rust’s as
keyword.
Sourcepub fn as_i64(self) -> TpI64
pub fn as_i64(self) -> TpI64
Casts from one number type to another, following the same conventions as Rust’s as
keyword.
Sourcepub fn expose(self) -> bool
pub fn expose(self) -> bool
Remove the timing protection and expose the raw boolean value. Once the boolean is exposed, it is the library user’s responsibility to prevent timing leaks (if necessary). Note: this can be very difficult to do correctly with boolean values.
Commonly, this method is used when a value is safe to make public (e.g. the result of a signature verification).
Sourcepub fn cond_swap<T>(self, a: &mut T, b: &mut T)where
T: TpCondSwap + ?Sized,
pub fn cond_swap<T>(self, a: &mut T, b: &mut T)where
T: TpCondSwap + ?Sized,
Constant-time conditional swap. Swaps a
and b
if this boolean is true, otherwise has no
effect. This operation is implemented without branching on the boolean value, and it will
not leak information about whether the values were swapped.
Sourcepub fn select<T>(self, when_true: T, when_false: T) -> Twhere
T: TpCondSwap,
pub fn select<T>(self, when_true: T, when_false: T) -> Twhere
T: TpCondSwap,
Returns one of the arguments, depending on the value of this boolean. The return value is selected without branching on the boolean value, and no information about which value was selected will be leaked.
Trait Implementations§
Source§impl BitAndAssign<bool> for TpBool
impl BitAndAssign<bool> for TpBool
Source§fn bitand_assign(&mut self, rhs: bool)
fn bitand_assign(&mut self, rhs: bool)
&=
operation. Read moreSource§impl BitAndAssign for TpBool
impl BitAndAssign for TpBool
Source§fn bitand_assign(&mut self, rhs: TpBool)
fn bitand_assign(&mut self, rhs: TpBool)
&=
operation. Read moreSource§impl BitOrAssign<bool> for TpBool
impl BitOrAssign<bool> for TpBool
Source§fn bitor_assign(&mut self, rhs: bool)
fn bitor_assign(&mut self, rhs: bool)
|=
operation. Read moreSource§impl BitOrAssign for TpBool
impl BitOrAssign for TpBool
Source§fn bitor_assign(&mut self, rhs: TpBool)
fn bitor_assign(&mut self, rhs: TpBool)
|=
operation. Read moreSource§impl BitXorAssign<bool> for TpBool
impl BitXorAssign<bool> for TpBool
Source§fn bitxor_assign(&mut self, rhs: bool)
fn bitxor_assign(&mut self, rhs: bool)
^=
operation. Read moreSource§impl BitXorAssign for TpBool
impl BitXorAssign for TpBool
Source§fn bitxor_assign(&mut self, rhs: TpBool)
fn bitxor_assign(&mut self, rhs: TpBool)
^=
operation. Read moreSource§impl TpCondSwap for TpBool
impl TpCondSwap for TpBool
Source§impl TpEq<TpBool> for bool
impl TpEq<TpBool> for bool
Source§fn tp_eq(&self, other: &TpBool) -> TpBool
fn tp_eq(&self, other: &TpBool) -> TpBool
self
with other
for equality without leaking the result.
Important: if either input is not a timing-protected type, this operation might leak the
value of that type. To prevent timing leaks, protect values before performing any operations
on them. Read moreSource§fn tp_not_eq(&self, other: &TpBool) -> TpBool
fn tp_not_eq(&self, other: &TpBool) -> TpBool
self
with other
for inequality without leaking the result.
Important: if either input is not a timing-protected type, this operation might leak the
value of that type. To prevent timing leaks, protect values before performing any operations
on them. Read moreSource§impl TpEq<bool> for TpBool
impl TpEq<bool> for TpBool
Source§fn tp_eq(&self, other: &bool) -> TpBool
fn tp_eq(&self, other: &bool) -> TpBool
self
with other
for equality without leaking the result.
Important: if either input is not a timing-protected type, this operation might leak the
value of that type. To prevent timing leaks, protect values before performing any operations
on them. Read moreSource§fn tp_not_eq(&self, other: &bool) -> TpBool
fn tp_not_eq(&self, other: &bool) -> TpBool
self
with other
for inequality without leaking the result.
Important: if either input is not a timing-protected type, this operation might leak the
value of that type. To prevent timing leaks, protect values before performing any operations
on them. Read moreSource§impl TpEq for TpBool
impl TpEq for TpBool
Source§fn tp_eq(&self, other: &TpBool) -> TpBool
fn tp_eq(&self, other: &TpBool) -> TpBool
self
with other
for equality without leaking the result.
Important: if either input is not a timing-protected type, this operation might leak the
value of that type. To prevent timing leaks, protect values before performing any operations
on them. Read moreSource§fn tp_not_eq(&self, other: &TpBool) -> TpBool
fn tp_not_eq(&self, other: &TpBool) -> TpBool
self
with other
for inequality without leaking the result.
Important: if either input is not a timing-protected type, this operation might leak the
value of that type. To prevent timing leaks, protect values before performing any operations
on them. Read more