pub struct TpU32(/* private fields */);
Expand description
A number type that prevents its value from being leaked to attackers through timing information.
Use this type’s protect
method as early as possible to prevent the value from being
used in variable-time computations.
Unlike Rust’s built-in number types, rust-timing-shield
number types have no overflow
checking, even in debug mode. In other words, they behave like Rust’s
Wrapping types.
Additionally, all shift distances are reduced mod the bit width of the type
(e.g. some_i64 << 104
is equivalent to some_i64 << 40
).
// Protect the value as early as possible to limit the risk
let protected_value = TpU8::protect(some_u8);
let other_protected_value = TpU8::protect(some_other_u8);
// Do some computation with the protected values
let x = (other_protected_value + protected_value) & 0x40;
// If needed, remove protection using `expose`
println!("{}", x.expose());
Implementations§
Source§impl TpU32
impl TpU32
Sourcepub fn protect(input: u32) -> Self
pub fn protect(input: u32) -> 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 = TpU32::protect(secret_u32);
// Use `protected` instead of `secret_u32` 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_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 rotate_left(self, n: u32) -> Self
pub fn rotate_left(self, n: u32) -> Self
Shifts left by n
bits, wrapping truncated bits around to the right side of the
resulting value.
If n
is larger than the bitwidth of this number type,
n
is reduced mod that bitwidth.
For example, rotating an i16
with n = 35
is equivalent to rotating with n = 3
, since 35 = 3 mod 16
.
Sourcepub fn rotate_right(self, n: u32) -> Self
pub fn rotate_right(self, n: u32) -> Self
Shifts right by n
bits, wrapping truncated bits around to the left side of the
resulting value.
If n
is larger than the bitwidth of this number type,
n
is reduced mod that bitwidth.
For example, rotating an i16
with n = 35
is equivalent to rotating with n = 3
, since 35 = 3 mod 16
.
Sourcepub fn expose(self) -> u32
pub fn expose(self) -> u32
Remove the timing protection and expose the raw number value. Once a value is exposed, it is the library user’s responsibility to prevent timing leaks (if necessary).
Commonly, this method is used when a value is safe to make public (e.g. when an
encryption algorithm outputs a ciphertext). Alternatively, this method may need to
be used when providing a secret value to an interface that does not use
timing-shield
’s types (e.g. writing a secret key to a file using a file system
API).
Trait Implementations§
Source§impl AddAssign<u32> for TpU32
impl AddAssign<u32> for TpU32
Source§fn add_assign(&mut self, rhs: u32)
fn add_assign(&mut self, rhs: u32)
+=
operation. Read moreSource§impl AddAssign for TpU32
impl AddAssign for TpU32
Source§fn add_assign(&mut self, rhs: TpU32)
fn add_assign(&mut self, rhs: TpU32)
+=
operation. Read moreSource§impl BitAndAssign<u32> for TpU32
impl BitAndAssign<u32> for TpU32
Source§fn bitand_assign(&mut self, rhs: u32)
fn bitand_assign(&mut self, rhs: u32)
&=
operation. Read moreSource§impl BitAndAssign for TpU32
impl BitAndAssign for TpU32
Source§fn bitand_assign(&mut self, rhs: TpU32)
fn bitand_assign(&mut self, rhs: TpU32)
&=
operation. Read moreSource§impl BitOrAssign<u32> for TpU32
impl BitOrAssign<u32> for TpU32
Source§fn bitor_assign(&mut self, rhs: u32)
fn bitor_assign(&mut self, rhs: u32)
|=
operation. Read moreSource§impl BitOrAssign for TpU32
impl BitOrAssign for TpU32
Source§fn bitor_assign(&mut self, rhs: TpU32)
fn bitor_assign(&mut self, rhs: TpU32)
|=
operation. Read moreSource§impl BitXorAssign<u32> for TpU32
impl BitXorAssign<u32> for TpU32
Source§fn bitxor_assign(&mut self, rhs: u32)
fn bitxor_assign(&mut self, rhs: u32)
^=
operation. Read moreSource§impl BitXorAssign for TpU32
impl BitXorAssign for TpU32
Source§fn bitxor_assign(&mut self, rhs: TpU32)
fn bitxor_assign(&mut self, rhs: TpU32)
^=
operation. Read moreSource§impl MulAssign<u32> for TpU32
impl MulAssign<u32> for TpU32
Source§fn mul_assign(&mut self, rhs: u32)
fn mul_assign(&mut self, rhs: u32)
*=
operation. Read moreSource§impl MulAssign for TpU32
impl MulAssign for TpU32
Source§fn mul_assign(&mut self, rhs: TpU32)
fn mul_assign(&mut self, rhs: TpU32)
*=
operation. Read moreSource§impl ShlAssign<u32> for TpU32
impl ShlAssign<u32> for TpU32
Source§fn shl_assign(&mut self, rhs: u32)
fn shl_assign(&mut self, rhs: u32)
<<=
operation. Read moreSource§impl ShrAssign<u32> for TpU32
impl ShrAssign<u32> for TpU32
Source§fn shr_assign(&mut self, rhs: u32)
fn shr_assign(&mut self, rhs: u32)
>>=
operation. Read moreSource§impl SubAssign<u32> for TpU32
impl SubAssign<u32> for TpU32
Source§fn sub_assign(&mut self, rhs: u32)
fn sub_assign(&mut self, rhs: u32)
-=
operation. Read moreSource§impl SubAssign for TpU32
impl SubAssign for TpU32
Source§fn sub_assign(&mut self, rhs: TpU32)
fn sub_assign(&mut self, rhs: TpU32)
-=
operation. Read moreSource§impl TpCondSwap for TpU32
impl TpCondSwap for TpU32
Source§impl TpEq<TpU32> for u32
impl TpEq<TpU32> for u32
Source§fn tp_eq(&self, other: &TpU32) -> TpBool
fn tp_eq(&self, other: &TpU32) -> 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: &TpU32) -> TpBool
fn tp_not_eq(&self, other: &TpU32) -> 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<u32> for TpU32
impl TpEq<u32> for TpU32
Source§fn tp_eq(&self, other: &u32) -> TpBool
fn tp_eq(&self, other: &u32) -> 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: &u32) -> TpBool
fn tp_not_eq(&self, other: &u32) -> 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 TpU32
impl TpEq for TpU32
Source§fn tp_eq(&self, other: &TpU32) -> TpBool
fn tp_eq(&self, other: &TpU32) -> 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: &TpU32) -> TpBool
fn tp_not_eq(&self, other: &TpU32) -> 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 TpOrd<TpU32> for u32
impl TpOrd<TpU32> for u32
Source§fn tp_lt(&self, other: &TpU32) -> TpBool
fn tp_lt(&self, other: &TpU32) -> TpBool
self < other
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.Source§fn tp_gt(&self, other: &TpU32) -> TpBool
fn tp_gt(&self, other: &TpU32) -> TpBool
self > other
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.Source§impl TpOrd<u32> for TpU32
impl TpOrd<u32> for TpU32
Source§fn tp_lt(&self, other: &u32) -> TpBool
fn tp_lt(&self, other: &u32) -> TpBool
self < other
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.Source§fn tp_gt(&self, other: &u32) -> TpBool
fn tp_gt(&self, other: &u32) -> TpBool
self > other
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.Source§impl TpOrd for TpU32
impl TpOrd for TpU32
Source§fn tp_lt(&self, other: &TpU32) -> TpBool
fn tp_lt(&self, other: &TpU32) -> TpBool
self < other
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.Source§fn tp_gt(&self, other: &TpU32) -> TpBool
fn tp_gt(&self, other: &TpU32) -> TpBool
self > other
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.