From 90362b260435d3261715abc8033443872a29733d Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 18 Nov 2025 23:59:17 +0800 Subject: [PATCH] fix hosted mode --- services/bao-console/src/cmds/trng_cmd.rs | 2 ++ services/bao1x-emu/Cargo.toml | 6 ++---- services/bao1x-emu/src/trng.rs | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/services/bao-console/src/cmds/trng_cmd.rs b/services/bao-console/src/cmds/trng_cmd.rs index abf028ad6..0aca2faa9 100644 --- a/services/bao-console/src/cmds/trng_cmd.rs +++ b/services/bao-console/src/cmds/trng_cmd.rs @@ -1,4 +1,5 @@ use String; +#[cfg(not(feature = "hosted-baosec"))] use rand_core::RngCore; use crate::{CommonEnv, ShellCmdApi}; @@ -38,6 +39,7 @@ impl<'a> ShellCmdApi<'a> for TrngCmd { "u64" => { log::info!("A u64 trng value: {:x}", env.trng.get_u64().unwrap()); } + #[cfg(not(feature = "hosted-baosec"))] "buf" => { let mut test1 = [0u8; 8192]; let mut trng = bao1x_hal_service::trng::Trng::new(&env.xns).unwrap(); diff --git a/services/bao1x-emu/Cargo.toml b/services/bao1x-emu/Cargo.toml index fc30dd5d8..34f36b6ea 100644 --- a/services/bao1x-emu/Cargo.toml +++ b/services/bao1x-emu/Cargo.toml @@ -10,6 +10,7 @@ xous-names = { package = "xous-api-names", version = "0.9.68" } "ux-api" = { path = "../../libs/ux-api" } "rand" = "0.8" rand_core = "0.6.4" +rand_chacha = "0.3.1" log = "0.4.14" log-server = { package = "xous-api-log", version = "0.1.66" } num-derive = { version = "0.4.2", default-features = false } @@ -18,10 +19,7 @@ rkyv = { version = "0.8.8", default-features = false, features = [ "std", "alloc", ] } -bao1x-api = { path = "../../libs/bao1x-api", features = [ - "derive-rkyv", - "std", -] } +bao1x-api = { path = "../../libs/bao1x-api", features = ["derive-rkyv", "std"] } [target.'cfg(any(windows,unix))'.dependencies] minifb = "0.26.0" diff --git a/services/bao1x-emu/src/trng.rs b/services/bao1x-emu/src/trng.rs index eee4acc9a..b696ae499 100644 --- a/services/bao1x-emu/src/trng.rs +++ b/services/bao1x-emu/src/trng.rs @@ -34,6 +34,23 @@ impl Trng { left.copy_from_slice(&chunk[..n]); } } + + pub fn fill_seeded_bytes(&mut self, dest: &mut [u8]) { + use rand_chacha::ChaCha8Rng; + use rand_chacha::rand_core::RngCore; + use rand_chacha::rand_core::SeedableRng; + + // for very short runs, fill directly from the HW TRNG + if dest.len() < 64 { + self.fill_bytes_via_next(dest); + return; + } + // larger runs, seed a ChaCha8 and use its results to fill the buffer + let mut seed = [0u8; 32]; + self.fill_bytes_via_next(&mut seed); + let mut cstrng = ChaCha8Rng::from_seed(seed); + cstrng.fill_bytes(dest); + } } impl RngCore for Trng {