mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
fix seeding algorithm
- use proper seeding of entire state - chacha8 does not evolve internal state when extracting the seed, so we have to run the cipher some to force mixing of the seed state.
This commit is contained in:
parent
7743d96e91
commit
84fe4c96ff
1 changed files with 28 additions and 18 deletions
|
|
@ -288,25 +288,35 @@ impl SwapHal {
|
|||
crate::print!("Generating swap key...");
|
||||
let mut trng = sce::trng::Trng::new(utralib::generated::HW_TRNG_BASE as usize);
|
||||
trng.setup_raw_generation(32);
|
||||
let mut seed = 0u64;
|
||||
seed |= trng.get_u32().expect("TRNG error") as u64;
|
||||
seed |= (trng.get_u32().expect("TRNG error") as u64) << 32;
|
||||
let mut cstrng = ChaCha8Rng::seed_from_u64(seed);
|
||||
// accumulate more TRNG data, because I don't trust it.
|
||||
// 1. whiten the existing TRNG data with ChaCha8
|
||||
// 2. XOR in another 32 bits of TRNG data
|
||||
// 3. create a new ChaCha8 from the resulting data
|
||||
for _ in 0..128 {
|
||||
seed = cstrng.next_u64();
|
||||
for _ in 0..7 {
|
||||
let next = trng.get_u32().expect("TRNG error");
|
||||
// crate::print!("{:x?}", next); // uncomment to sanity check TRNG
|
||||
seed ^= next as u64;
|
||||
let next = trng.get_u32().expect("TRNG error");
|
||||
// crate::print!("{:x?}", next); // uncomment to sanity check TRNG
|
||||
seed ^= (next as u64) << 32;
|
||||
|
||||
// The first seed is always 0.
|
||||
let mut seed = [0u8; 32];
|
||||
// println!("seed: {:x?}", seed); // used to eyeball that things are working correctly
|
||||
let mut cstrng = ChaCha8Rng::from_seed(seed);
|
||||
|
||||
// Accumulate TRNG data into the seed (which starts at 0).
|
||||
// Repeat N times for good measure:
|
||||
// 1. XOR in another round of HW TRNG data into ChaCha8
|
||||
// 2. create a new ChaCha8 from the resulting data
|
||||
// 3. Run the ChaCha8 for a few rounds and XOR that back into the state
|
||||
// Each round pulls in 8*32 = 256 bits from HW TRNG
|
||||
// 64 rounds of this would fold in 16,384 bits total, about 100x safety margin.
|
||||
for _ in 0..64 {
|
||||
seed = cstrng.get_seed();
|
||||
for s in seed.chunks_mut(4) {
|
||||
let incoming = trng.get_u32().expect("TRNG error") ^ cstrng.next_u32();
|
||||
for (s_byte, &incoming_byte) in s.iter_mut().zip(incoming.to_le_bytes().iter()) {
|
||||
*s_byte ^= incoming_byte;
|
||||
}
|
||||
}
|
||||
// println!("seed: {:x?}", seed); // eyeball if things are working correctly
|
||||
cstrng = ChaCha8Rng::from_seed(seed);
|
||||
// mix up the internal state with output from the CSTRNG
|
||||
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;
|
||||
}
|
||||
}
|
||||
cstrng = ChaCha8Rng::seed_from_u64(seed);
|
||||
}
|
||||
// now we might have a properly seeded cryptographically secure TRNG...
|
||||
let mut dest_key = [0u8; 32];
|
||||
|
|
|
|||
Loading…
Reference in a new issue