From 3bcc0dcd88828edfe99aba6f13ae0973567e0bfd Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 11 Mar 2024 18:17:42 +0800 Subject: [PATCH] split low-level engine verification routines into a feature flag to use these, one must include engine-25519 and also not use the curve25519-dalek native primitives. Probably what we *should* do is create some backend calls that allow us to queue up local jobs only for this sort of testing. --- services/shellchat/Cargo.toml | 9 +++++---- services/shellchat/src/cmds/engine.rs | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index a6db1cfc8..d4f49f655 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -28,7 +28,7 @@ sha2 = { version = "0.10.8" } digest = "0.10.7" aes = { path = "../aes" } cipher = "0.4.4" -engine-25519 = { path = "../engine-25519" } +engine-25519 = { path = "../engine-25519", optional = true } spinor = { path = "../spinor" } root-keys = { path = "../root-keys" } jtag = { path = "../jtag" } @@ -105,13 +105,13 @@ version = "=4.1.1" # note this is patched to our fork in ./Cargo.toml default-features = false [dependencies.x25519-dalek] -version = "2.0.1" +version = "=2.0.1" # TODO: static_secrets is only needed by the engine tests. Ideally, we would put the static_secrets version in dev deps only default-features = false features = ["static_secrets"] [dependencies.ed25519-dalek] -version = "2.1.0" +version = "=2.1.0" #path = "../../../ed25519-dalek" default-features = false features = ["rand_core"] @@ -127,6 +127,7 @@ benchmarks = [ ] # adds the benchmark routines. Left off normally to free up code and memory space. hashtest = [] aestests = [] # adds AES tests +engine-ll = [] # low-level engine tests tts = [] # adds text to speech plugin pddbtest = ["rand_chacha"] autobasis = ["rand_chacha"] @@ -153,4 +154,4 @@ rand-api = [] locktests = [ ] # for debugging some specific `std` tests cherry-picked out of the test suite simple-tls = ["rustls", "webpki-roots"] -default = [] # "debugprint" +default = ["benchmarks"] # "debugprint" diff --git a/services/shellchat/src/cmds/engine.rs b/services/shellchat/src/cmds/engine.rs index 90bfe5359..03108d314 100644 --- a/services/shellchat/src/cmds/engine.rs +++ b/services/shellchat/src/cmds/engine.rs @@ -1,5 +1,6 @@ use core::sync::atomic::{AtomicU32, Ordering}; +#[cfg(feature = "engine-ll")] use engine_25519::*; use num_traits::*; use xous_ipc::String; @@ -27,9 +28,12 @@ pub(crate) enum BenchResult { DhDone, } +#[cfg(feature = "engine-ll")] const TEST_ITERS: usize = 10; +#[cfg(feature = "engine-ll")] const TEST_ITERS_DH: usize = 200; +#[cfg(feature = "engine-ll")] fn vector_read(word_offset: usize) -> u32 { let mut bytes: [u8; 4] = [0; 4]; for i in 0..4 { @@ -38,6 +42,7 @@ fn vector_read(word_offset: usize) -> u32 { u32::from_le_bytes(bytes) } +#[cfg(feature = "engine-ll")] fn run_vectors(engine: &mut Engine25519) -> (usize, usize) { let mut test_offset: usize = 0x0; let mut passes: usize = 0; @@ -129,6 +134,7 @@ benchmark notes: +59mA +/-1mA current draw off fully charged battery when running the benchmark 1246-1261ms/check vector iteration (10 iters total, 1450 vectors total) */ +#[cfg(feature = "engine-ll")] pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32); let xns = xous_names::XousNames::new().unwrap(); @@ -284,11 +290,13 @@ pub struct Engine { impl Engine { pub fn new(xns: &xous_names::XousNames, env: &mut CommonEnv) -> Self { let sid = xous::create_server().unwrap(); + #[cfg(feature = "engine-ll")] let sid_tuple = sid.to_u32(); let cb_id = env.register_handler(String::<256>::from_str("engine")); CB_ID.store(cb_id, Ordering::Relaxed); + #[cfg(feature = "engine-ll")] xous::create_thread_4( benchmark_thread, sid_tuple.0 as usize, @@ -299,6 +307,7 @@ impl Engine { .unwrap(); Engine { susres: susres::Susres::new_without_hook(&xns).unwrap(), + // this is a dummy and hangs if engine-ll is not an active feature benchmark_cid: xous::connect(sid).unwrap(), start_time: None, } @@ -317,12 +326,16 @@ impl<'a> ShellCmdApi<'a> for Engine { ) -> Result>, xous::Error> { use core::fmt::Write; let mut ret = String::<1024>::new(); + #[cfg(feature = "engine-ll")] let helpstring = "engine [check] [bench] [benchdh] [susres] [dh] [ed] [wycheproof]"; + #[cfg(not(feature = "engine-ll"))] + let helpstring = "engine [susres] [dh] [ed] [wycheproof]"; let mut tokens = args.as_str().unwrap().split(' '); if let Some(sub_cmd) = tokens.next() { match sub_cmd { + #[cfg(feature = "engine-ll")] "check" => { let mut engine = engine_25519::Engine25519::new(); log::debug!("running vectors"); @@ -330,6 +343,7 @@ impl<'a> ShellCmdApi<'a> for Engine { write!(ret, "Engine passed {} vectors, failed {} vectors", passes, fails).unwrap(); } + #[cfg(feature = "engine-ll")] "bench" => { let start = env.ticktimer.elapsed_ms(); self.start_time = Some(start); @@ -340,6 +354,7 @@ impl<'a> ShellCmdApi<'a> for Engine { .unwrap(); write!(ret, "Starting Engine hardware benchmark with {} iters", TEST_ITERS).unwrap(); } + #[cfg(feature = "engine-ll")] "benchdh" => { let start = env.ticktimer.elapsed_ms(); self.start_time = Some(start); @@ -364,13 +379,18 @@ impl<'a> ShellCmdApi<'a> for Engine { write!(ret, "Interrupted Engine hardware benchmark with a suspend/resume").unwrap(); } "dh" => { + log::info!("starting DH test"); use x25519_dalek::{EphemeralSecret, PublicKey}; let alice_secret = EphemeralSecret::random_from_rng(&mut env.trng); + log::info!("1"); let alice_public = PublicKey::from(&alice_secret); let bob_secret = EphemeralSecret::random_from_rng(&mut env.trng); let bob_public = PublicKey::from(&bob_secret); + log::info!("2"); let alice_shared_secret = alice_secret.diffie_hellman(&bob_public); + log::info!("3"); let bob_shared_secret = bob_secret.diffie_hellman(&alice_public); + log::info!("4"); let mut pass = true; for (&alice, &bob) in alice_shared_secret.as_bytes().iter().zip(bob_shared_secret.as_bytes().iter()) @@ -571,6 +591,8 @@ impl<'a> ShellCmdApi<'a> for Engine { } else { write!(ret, "{}", helpstring).unwrap(); } + // de-allocate the engine at the end of the test, so other processes can grab it + curve25519_dalek::backend::serial::u32e::free_engine(); Ok(Some(ret)) }