mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
improve documentation on how the CSPRNG is reseeded from the TRNG.
This commit is contained in:
parent
10a90be082
commit
a9b0ba2666
1 changed files with 33 additions and 15 deletions
|
|
@ -72,7 +72,9 @@ pub fn init() {
|
|||
let mut trng_kernel = sce::trng::Trng::new(TRNG_KERNEL_ADDR);
|
||||
trng_kernel.setup_raw_generation(256);
|
||||
|
||||
// setup the initial seeds with stuff out of the TRNG.
|
||||
// Setup the initial seeds with raw data directly out of the TRNG. You'd think this
|
||||
// would give you 256 bits of entropy, but TRNGs can be imperfect so we assume it is
|
||||
// much less than this.
|
||||
for state in LOCAL_RNG_STATE.iter() {
|
||||
state.store(trng_kernel.get_u32().expect("TRNG error"), Ordering::SeqCst);
|
||||
}
|
||||
|
|
@ -82,19 +84,17 @@ pub fn init() {
|
|||
TRNG_KERNEL = Some(trng_kernel);
|
||||
}
|
||||
|
||||
// Accumulate more TRNG data, because I don't trust it. Note that ChaCha8's seed never changes:
|
||||
// the output is just the seed + some generation distance from the seed. Thus to change the seed,
|
||||
// we have to extract it, XOR it with data, and put it back into the machine.
|
||||
// Accumulate more TRNG data, because I don't trust it.
|
||||
//
|
||||
// Repeat N times for good measure:
|
||||
// 1. XOR in another round of HW TRNG data into the ChaCha8 state
|
||||
// 2. Create the ChaCha8 cipher from the state
|
||||
// 3. Run ChaCha8 and XOR its result into the state vector (obfuscate temporary dropouts in HW TRNG)
|
||||
// 4. Store the state vector
|
||||
// For the kernel, every 32 bits extracted is accompanied by a reseeding operation. Thus,
|
||||
// we can effectively improve the seed pool by just requesting u32's out of the pool and throwing
|
||||
// the result away.
|
||||
//
|
||||
// Each round pulls in 8*32 = 256 bits from HW TRNG 64 rounds of this would fold in 16,384 bits total.
|
||||
// Every subsequent call to get_u32() adds more entropy to the pool. This should give us about 100x safety
|
||||
// margin. The latest settings improvement on the TRNG makes me think this is extremely conservative, we
|
||||
// margin to a target of 128 bits of true entropy.
|
||||
//
|
||||
// The latest settings improvement on the TRNG makes me think this is extremely conservative, we
|
||||
// could probably do fine with a 10x margin; but, the operation is fast enough that this allows us
|
||||
// to be safe even if the TRNG is completely misconfigured.
|
||||
for _ in 0..64 {
|
||||
|
|
@ -102,11 +102,24 @@ pub fn init() {
|
|||
}
|
||||
}
|
||||
|
||||
/// Retrieve random `u32`.
|
||||
/// Retrieve a true random `u32`. The data comes from the output of a CPRNG that is seeded by
|
||||
/// the TRNG. Every pull of a `u32` adds more TRNG data to the CPRNG seed pool.
|
||||
///
|
||||
/// Note that ChaCha8's seed never changes: the output is just the seed + some generation distance from the
|
||||
/// seed. Thus to change the seed, we have to extract it, XOR it with data, and put it back into the machine.
|
||||
///
|
||||
/// 1. XOR in another round of HW TRNG data into the ChaCha8 state
|
||||
/// 2. Create the ChaCha8 cipher from the state
|
||||
/// 3. Run ChaCha8 and XOR its result into the state vector (obfuscate temporary dropouts in HW TRNG)
|
||||
/// 4. Store the state vector
|
||||
pub fn get_u32() -> u32 {
|
||||
// Local storage for the seed.
|
||||
let mut seed = [0u8; 32];
|
||||
|
||||
// mix in more data from the TRNG while recovering the state from the kernel holding area
|
||||
// Pull our true seed data from the static AtomicU32 variables. We have to do this because
|
||||
// we're the kernel: any machine persistent state is by definition, global mutable state.
|
||||
//
|
||||
// Mix in more data from the TRNG while recovering the state from the kernel holding area.
|
||||
for (s, state) in seed.chunks_mut(4).zip(LOCAL_RNG_STATE.iter()) {
|
||||
let incoming = get_raw_u32() ^ state.load(Ordering::SeqCst);
|
||||
for (s_byte, &incoming_byte) in s.iter_mut().zip(incoming.to_le_bytes().iter()) {
|
||||
|
|
@ -114,21 +127,26 @@ pub fn get_u32() -> u32 {
|
|||
}
|
||||
}
|
||||
let mut cstrng = ChaCha8Rng::from_seed(seed);
|
||||
// mix up the internal state with output from the CSTRNG
|
||||
// Mix up the internal state with output from the CSPRNG. We do this because the TRNG bits
|
||||
// could be biased, and by running the CSPRNG forward based on the new seed, we have a chance to
|
||||
// diffuse any true entropy over all bits in the seed pool.
|
||||
for s in seed.chunks_mut(8) {
|
||||
for (s_byte, chacha_byte) in s.iter_mut().zip(cstrng.next_u64().to_le_bytes()) {
|
||||
*s_byte ^= chacha_byte;
|
||||
}
|
||||
}
|
||||
// now extract one value from the CSPRNG
|
||||
// Now extract one value from the CSPRNG: this is the number we reveal to the outside world.
|
||||
// It should not, in theory, be possible to deduce the seed from this value.
|
||||
let ret_val = cstrng.next_u32();
|
||||
// save the state into the kernel holding area
|
||||
|
||||
// Save the mixed state into the kernel holding area
|
||||
for (s, state) in seed.chunks(4).zip(LOCAL_RNG_STATE.iter()) {
|
||||
state.store(u32::from_le_bytes(s.try_into().unwrap()), Ordering::SeqCst);
|
||||
}
|
||||
ret_val
|
||||
}
|
||||
|
||||
/// This returns a raw, unwhitened, unprocessed TRNG value.
|
||||
pub fn get_raw_u32() -> u32 {
|
||||
unsafe {
|
||||
TRNG_KERNEL
|
||||
|
|
|
|||
Loading…
Reference in a new issue