Struct TpBool

Source
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

Source

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
Source

pub fn as_u8(self) -> TpU8

Casts from one number type to another, following the same conventions as Rust’s as keyword.

Source

pub fn as_u16(self) -> TpU16

Casts from one number type to another, following the same conventions as Rust’s as keyword.

Source

pub fn as_u32(self) -> TpU32

Casts from one number type to another, following the same conventions as Rust’s as keyword.

Source

pub fn as_u64(self) -> TpU64

Casts from one number type to another, following the same conventions as Rust’s as keyword.

Source

pub fn as_i8(self) -> TpI8

Casts from one number type to another, following the same conventions as Rust’s as keyword.

Source

pub fn as_i16(self) -> TpI16

Casts from one number type to another, following the same conventions as Rust’s as keyword.

Source

pub fn as_i32(self) -> TpI32

Casts from one number type to another, following the same conventions as Rust’s as keyword.

Source

pub fn as_i64(self) -> TpI64

Casts from one number type to another, following the same conventions as Rust’s as keyword.

Source

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).

Source

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.

Source

pub fn select<T>(self, when_true: T, when_false: T) -> T
where 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 BitAnd<TpBool> for bool

Source§

type Output = TpBool

The resulting type after applying the & operator.
Source§

fn bitand(self, other: TpBool) -> TpBool

Performs the & operation. Read more
Source§

impl BitAnd<bool> for TpBool

Source§

type Output = TpBool

The resulting type after applying the & operator.
Source§

fn bitand(self, other: bool) -> TpBool

Performs the & operation. Read more
Source§

impl BitAnd for TpBool

Source§

type Output = TpBool

The resulting type after applying the & operator.
Source§

fn bitand(self, other: TpBool) -> TpBool

Performs the & operation. Read more
Source§

impl BitAndAssign<bool> for TpBool

Source§

fn bitand_assign(&mut self, rhs: bool)

Performs the &= operation. Read more
Source§

impl BitAndAssign for TpBool

Source§

fn bitand_assign(&mut self, rhs: TpBool)

Performs the &= operation. Read more
Source§

impl BitOr<TpBool> for bool

Source§

type Output = TpBool

The resulting type after applying the | operator.
Source§

fn bitor(self, other: TpBool) -> TpBool

Performs the | operation. Read more
Source§

impl BitOr<bool> for TpBool

Source§

type Output = TpBool

The resulting type after applying the | operator.
Source§

fn bitor(self, other: bool) -> TpBool

Performs the | operation. Read more
Source§

impl BitOr for TpBool

Source§

type Output = TpBool

The resulting type after applying the | operator.
Source§

fn bitor(self, other: TpBool) -> TpBool

Performs the | operation. Read more
Source§

impl BitOrAssign<bool> for TpBool

Source§

fn bitor_assign(&mut self, rhs: bool)

Performs the |= operation. Read more
Source§

impl BitOrAssign for TpBool

Source§

fn bitor_assign(&mut self, rhs: TpBool)

Performs the |= operation. Read more
Source§

impl BitXor<TpBool> for bool

Source§

type Output = TpBool

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: TpBool) -> TpBool

Performs the ^ operation. Read more
Source§

impl BitXor<bool> for TpBool

Source§

type Output = TpBool

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: bool) -> TpBool

Performs the ^ operation. Read more
Source§

impl BitXor for TpBool

Source§

type Output = TpBool

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: TpBool) -> TpBool

Performs the ^ operation. Read more
Source§

impl BitXorAssign<bool> for TpBool

Source§

fn bitxor_assign(&mut self, rhs: bool)

Performs the ^= operation. Read more
Source§

impl BitXorAssign for TpBool

Source§

fn bitxor_assign(&mut self, rhs: TpBool)

Performs the ^= operation. Read more
Source§

impl Clone for TpBool

Source§

fn clone(&self) -> TpBool

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Not for TpBool

Source§

type Output = TpBool

The resulting type after applying the ! operator.
Source§

fn not(self) -> TpBool

Performs the unary ! operation. Read more
Source§

impl TpCondSwap for TpBool

Source§

fn tp_cond_swap(condition: TpBool, a: &mut TpBool, b: &mut TpBool)

Swap a and b if and only if condition is true. Read more
Source§

impl TpEq<TpBool> for bool

Source§

fn tp_eq(&self, other: &TpBool) -> TpBool

Compare 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 more
Source§

fn tp_not_eq(&self, other: &TpBool) -> TpBool

Compare 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
Source§

impl TpEq<bool> for TpBool

Source§

fn tp_eq(&self, other: &bool) -> TpBool

Compare 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 more
Source§

fn tp_not_eq(&self, other: &bool) -> TpBool

Compare 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
Source§

impl TpEq for TpBool

Source§

fn tp_eq(&self, other: &TpBool) -> TpBool

Compare 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 more
Source§

fn tp_not_eq(&self, other: &TpBool) -> TpBool

Compare 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
Source§

impl Copy for TpBool

Auto Trait Implementations§

§

impl Freeze for TpBool

§

impl RefUnwindSafe for TpBool

§

impl Send for TpBool

§

impl Sync for TpBool

§

impl Unpin for TpBool

§

impl UnwindSafe for TpBool

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.