pub struct TimingValidator { /* private fields */ }Expand description
Timing attack prevention validator
This validator provides utilities to prevent timing attacks by ensuring constant-time operations where necessary.
Implementations§
Source§impl TimingValidator
impl TimingValidator
Sourcepub fn constant_time_compare(&self, a: &[u8], b: &[u8]) -> bool
pub fn constant_time_compare(&self, a: &[u8], b: &[u8]) -> bool
Perform constant-time comparison of two byte slices
This function performs a constant-time comparison to prevent timing attacks. It returns true if the slices are equal, false otherwise.
§Arguments
a- First byte sliceb- Second byte slice
§Returns
Returns true if the slices are equal, false otherwise.
The comparison is performed in constant time to prevent timing attacks.
Sourcepub fn constant_time_select<T: ConditionallySelectable>(
&self,
choice: bool,
a: T,
b: T,
) -> T
pub fn constant_time_select<T: ConditionallySelectable>( &self, choice: bool, a: T, b: T, ) -> T
Constant-time selection between two values
Returns a if choice is true, b if choice is false.
Selection is branchless: a full-width mask is derived from choice via
subtle::ConditionallySelectable (no secret-dependent branch). Callers must derive
choice itself in constant time.
§Arguments
choice- Boolean choicea- First valueb- Second value
§Returns
Returns the selected value in constant time.
Sourcepub fn constant_time_assign<T: ConditionallySelectable>(
&self,
choice: bool,
dst: &mut T,
src: T,
)
pub fn constant_time_assign<T: ConditionallySelectable>( &self, choice: bool, dst: &mut T, src: T, )
Constant-time conditional assignment
Assigns src to dst if choice is true, otherwise leaves dst unchanged.
Branchless: dispatches to subtle::ConditionallySelectable::conditional_assign, deriving a
full-width mask from choice instead of branching. Callers must derive choice itself in
constant time.
§Arguments
choice- Boolean choicedst- Destination to potentially assign tosrc- Source value to assign
Sourcepub fn constant_time_copy(&self, choice: bool, dst: &mut [u8], src: &[u8])
pub fn constant_time_copy(&self, choice: bool, dst: &mut [u8], src: &[u8])
Constant-time conditional copy
Copies src to dst if choice is true, otherwise leaves dst unchanged.
Branchless: each byte is assigned via subtle::ConditionallySelectable::conditional_assign,
deriving a full-width mask from choice instead of branching. Callers must derive choice
itself in constant time.
§Arguments
choice- Boolean choicedst- Destination slicesrc- Source slice
§Panics
Panics if the slices have different lengths.
Sourcepub fn validate_timing_safety(&self, operation: &str) -> Result<()>
pub fn validate_timing_safety(&self, operation: &str) -> Result<()>
Validate that an operation is timing-safe
This function can be used to validate that operations are performed in constant time to prevent timing attacks.
§Arguments
operation- Name of the operation being validated
§Returns
Returns Ok(()) if timing validation is enabled and the operation
is considered safe, or an error if timing validation fails.
Sourcepub fn set_timing_validation(&mut self, enabled: bool)
pub fn set_timing_validation(&mut self, enabled: bool)
Sourcepub fn is_timing_validation_enabled(&self) -> bool
pub fn is_timing_validation_enabled(&self) -> bool
Check if timing validation is enabled
§Returns
Returns true if timing validation is enabled, false otherwise.
Trait Implementations§
Source§impl Clone for TimingValidator
impl Clone for TimingValidator
Source§fn clone(&self) -> TimingValidator
fn clone(&self) -> TimingValidator
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more