mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
add hardened_get() and hardened_get2() to one way counters
One way counters are used ot record some security-critical properties, such as key revocations and paranoid mode. Two methods of retrieving the counters so that they are hardened are provide here. Both of them rely on the fact that any value other than 0 for the counter means it is set. `hardened_get()` retrieves the counter value and sums it several times. It does this *twice*, and returns *both* values as a tuple. By returning both values as a tuple, it reduces the likelihood that the compiler optimizes out the redundant read. The receiver should implement serial checks that operate on the hardened_get() result (i.e., test one value of the tuple, then test the other value of the tuple redundantly, instead of OR'ing them together into a single test) `hardened_get2()` is for counters that are fully duplicated in the array. For extremely critical counters, they are stored in two physically separate elements. This is done to make bypass of the counter harder than just FIB'ing out a single bit in the memory array. In the case of `hardened_get2()`, a tuple is returned, but instead of reading the same value twice, the tuple corresponds to the summed value over each of the redundant counters.
This commit is contained in:
parent
9294c06d45
commit
05af4f3f62
1 changed files with 91 additions and 0 deletions
|
|
@ -8,10 +8,12 @@
|
|||
//! restrictions are not a concern.
|
||||
|
||||
use bao1x_api::OneWayEncoding;
|
||||
use bao1x_api::bollard;
|
||||
use bao1x_api::offsets::*;
|
||||
#[cfg(feature = "std")]
|
||||
use xous::MemoryRange;
|
||||
|
||||
use crate::cache_flush;
|
||||
use crate::coreuser::CoreuserId;
|
||||
use crate::rram::Reram;
|
||||
|
||||
|
|
@ -70,6 +72,95 @@ impl OneWayCounter {
|
|||
}
|
||||
}
|
||||
|
||||
/// This routine repeatedly reads the value of the counter, summing it up, so that
|
||||
/// a glitch of `hardened_get()` is unlikely to return e.g. 0 if the counter has
|
||||
/// any value larger than 0.
|
||||
///
|
||||
/// Inlining the hardened code means there isn't a single region of the chip to attack, either.
|
||||
///
|
||||
/// It's recommended to also insert some random delay before calling this.
|
||||
///
|
||||
/// The return value is a (u32, u32). The caller should check that these two values are equal,
|
||||
/// if they are not, then something bad happened.
|
||||
#[inline(always)]
|
||||
pub fn hardened_get(&self, offset: usize) -> Result<(u32, u32), OneWayErr> {
|
||||
bollard!(4);
|
||||
const HARDENING_FACTOR: usize = 4;
|
||||
#[cfg(not(feature = "std"))]
|
||||
let base = ONEWAY_START as *const u32;
|
||||
#[cfg(feature = "std")]
|
||||
let base = self.mapping.as_ptr() as *const u32;
|
||||
if offset < MAX_ONEWAY_COUNTERS {
|
||||
// safety: only safe because the pointer is length-checked
|
||||
// we use this form to access the array because we need to read_volatile()
|
||||
let mut value_1 = unsafe { base.add(offset * COUNTER_STRIDE_U32).read_volatile() };
|
||||
for _ in 0..HARDENING_FACTOR {
|
||||
cache_flush();
|
||||
bollard!(4);
|
||||
value_1 =
|
||||
value_1.saturating_add(unsafe { base.add(offset * COUNTER_STRIDE_U32).read_volatile() });
|
||||
bollard!(4);
|
||||
}
|
||||
bollard!(4);
|
||||
// repeated twice in case the previous loop was glitched over
|
||||
let mut value_2 = unsafe { base.add(offset * COUNTER_STRIDE_U32).read_volatile() };
|
||||
for _ in 0..HARDENING_FACTOR {
|
||||
cache_flush();
|
||||
bollard!(4);
|
||||
value_2 =
|
||||
value_2.saturating_add(unsafe { base.add(offset * COUNTER_STRIDE_U32).read_volatile() });
|
||||
bollard!(4);
|
||||
}
|
||||
Ok((value_1, value_2))
|
||||
} else {
|
||||
Err(OneWayErr::OutOfBounds)
|
||||
}
|
||||
}
|
||||
|
||||
/// This version of hardened_get takes two counters that are ostensibly duplicates of each
|
||||
/// other. This is useful for state that is extremely sensitive, such as key revocation counters.
|
||||
///
|
||||
/// Inlining the hardened code means there isn't a single region of the chip to attack, either.
|
||||
///
|
||||
/// It's recommended to also insert some random delay before calling this.
|
||||
///
|
||||
/// The return value is a (u32, u32). The caller should check that these two values are equal,
|
||||
/// if they are not, then something bad happened.
|
||||
#[inline(always)]
|
||||
pub fn hardened_get2(&self, offset: usize, dupe: usize) -> Result<(u32, u32), OneWayErr> {
|
||||
bollard!(4);
|
||||
const HARDENING_FACTOR: usize = 4;
|
||||
#[cfg(not(feature = "std"))]
|
||||
let base = ONEWAY_START as *const u32;
|
||||
#[cfg(feature = "std")]
|
||||
let base = self.mapping.as_ptr() as *const u32;
|
||||
if offset < MAX_ONEWAY_COUNTERS && dupe < MAX_ONEWAY_COUNTERS {
|
||||
// safety: only safe because the pointer is length-checked
|
||||
// we use this form to access the array because we need to read_volatile()
|
||||
let mut value_1 = unsafe { base.add(offset * COUNTER_STRIDE_U32).read_volatile() };
|
||||
for _ in 0..HARDENING_FACTOR {
|
||||
cache_flush();
|
||||
bollard!(4);
|
||||
value_1 =
|
||||
value_1.saturating_add(unsafe { base.add(offset * COUNTER_STRIDE_U32).read_volatile() });
|
||||
bollard!(4);
|
||||
}
|
||||
bollard!(4);
|
||||
// read the duplicate value
|
||||
let mut value_2 = unsafe { base.add(dupe * COUNTER_STRIDE_U32).read_volatile() };
|
||||
for _ in 0..HARDENING_FACTOR {
|
||||
cache_flush();
|
||||
bollard!(4);
|
||||
value_2 =
|
||||
value_2.saturating_add(unsafe { base.add(dupe * COUNTER_STRIDE_U32).read_volatile() });
|
||||
bollard!(4);
|
||||
}
|
||||
Ok((value_1, value_2))
|
||||
} else {
|
||||
Err(OneWayErr::OutOfBounds)
|
||||
}
|
||||
}
|
||||
|
||||
/// Marked as `unsafe` because the offset needs to be correct. It's recommended to use
|
||||
/// `inc_coded()` where possible. This function is necessary for the cases that don't
|
||||
/// fit into the `encode_oneway` mechanism, e.g. key revocations, etc.
|
||||
|
|
|
|||
Loading…
Reference in a new issue