diff --git a/Cargo.lock b/Cargo.lock index be8a4d2f9..2a9dde91a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -444,6 +444,15 @@ dependencies = [ "volatile-register", ] +[[package]] +name = "cpufeatures" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed00c67cb5d0a7d64a44f6ad2668db7e7530311dd53ea79bcd4fb022c64911c8" +dependencies = [ + "libc", +] + [[package]] name = "crc" version = "1.8.1" @@ -496,25 +505,13 @@ dependencies = [ [[package]] name = "curve25519-dalek" version = "3.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "639891fde0dbea823fc3d798a0fdf9d2f9440a42d64a78ab3488b0ca025117b3" -dependencies = [ - "byteorder", - "digest", - "rand_core 0.5.1", - "subtle", - "zeroize", -] - -[[package]] -name = "curve25519-dalek" -version = "3.1.0" -source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#6ee5e8aeb1e53bf76bacc9708656a8a1897cc027" +source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#9ffc75dd3cccd93478521414f5e1298da4eb831e" dependencies = [ "byteorder", "digest", "engine-25519", "engine25519-as", + "log", "rand_core 0.5.1", "subtle", "zeroize", @@ -535,6 +532,27 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +[[package]] +name = "ed25519" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d0860415b12243916284c67a9be413e044ee6668247b99ba26d94b2bc06c8f6" +dependencies = [ + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" +dependencies = [ + "curve25519-dalek", + "ed25519", + "sha2", + "zeroize", +] + [[package]] name = "embedded-hal" version = "0.2.5" @@ -820,9 +838,9 @@ dependencies = [ [[package]] name = "hex" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "hex-literal" @@ -1576,6 +1594,19 @@ version = "1.0.123" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" +[[package]] +name = "sha2" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b362ae5752fd2137731f9fa25fd4d9058af34666ca1966fb969119cc35719f12" +dependencies = [ + "block-buffer", + "cfg-if 1.0.0", + "cpufeatures", + "digest", + "opaque-debug", +] + [[package]] name = "shellchat" version = "0.1.0" @@ -1585,13 +1616,15 @@ dependencies = [ "codec", "com", "content-plugin-api", - "curve25519-dalek 3.1.0 (git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main)", + "curve25519-dalek", "digest", + "ed25519-dalek", "engine-25519", "engine-sha512", "gam", "graphics-server", "heapless 0.7.0", + "hex", "ime-plugin-api", "ime-plugin-shell", "keyboard", @@ -1619,6 +1652,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" +[[package]] +name = "signature" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f0242b8e50dd9accdd56170e94ca1ebd223b098eb9c83539a6e367d0f36ae68" + [[package]] name = "smallvec" version = "1.6.1" @@ -2046,7 +2085,7 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a0c105152107e3b96f6a00a65e86ce82d9b125230e1c4302940eca58ff71f4f" dependencies = [ - "curve25519-dalek 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "curve25519-dalek", "rand_core 0.5.1", "zeroize", ] diff --git a/Cargo.toml b/Cargo.toml index a518428e7..8b811c6f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -90,6 +90,13 @@ lto = "fat" [patch.crates-io] xous = {path = "xous-rs"} xous-macros = {path = "macros"} +# prefer hardware-accelerated versions of services +sha2 = {path = "services/engine-sha512", package = "engine-sha512"} +[patch.crates-io.curve25519-dalek] +git="https://github.com/betrusted-io/curve25519-dalek.git" +branch="main" +default-features = false +features = ["u32_backend", "betrusted"] [patch."https://github.com/betrusted-io/xous-engine-25519.git"] engine-25519 = {path = "services/engine-25519"} \ No newline at end of file diff --git a/emulation/soc/betrusted-soc.repl b/emulation/soc/betrusted-soc.repl index 43050e33b..776fad2ac 100755 --- a/emulation/soc/betrusted-soc.repl +++ b/emulation/soc/betrusted-soc.repl @@ -86,7 +86,7 @@ trng_server: Miscellaneous.BetrustedRNGServer @ sysbus 0xf001c000 // Physical base addresses of memory regions rom: Memory.MappedMemory @ sysbus 0x00000000 - size: 0x00008000 + size: 0x00001000 sram: Memory.MappedMemory @ sysbus 0x10000000 size: 0x00020000 diff --git a/emulation/soc/renode.svd b/emulation/soc/renode.svd index 05760c02e..9a31026ac 100644 --- a/emulation/soc/renode.svd +++ b/emulation/soc/renode.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1222,10 +1222,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0x92629e91 + 0xfce2f15c 32 @@ -1242,7 +1242,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x16 + 0x21 32 @@ -3451,7 +3451,7 @@ inject too fast.]]> SEED1 0x0000 - 0x24a0246f + 0x199653c4 32 @@ -3466,7 +3466,7 @@ inject too fast.]]> SEED0 0x0004 - 0x43f81cb4 + 0x5a781786 32 @@ -6406,10 +6406,25 @@ microcode instructions starting at mpstart]]> - POWER + MPRESUME 0x0010 0x00 32 + + + mpresume + 9 + [9:0] + 0 + + + + + + POWER + 0x0014 + 0x00 + 32 on @@ -6417,13 +6432,23 @@ microcode instructions starting at mpstart]]> [0:0] 0 +savings). The handling of the clock gate is in a different module, this is just +a flag to that block.]]> + + + pause_req + 1 + [1:1] + 1 + STATUS - 0x0014 + 0x0018 0x00 32 @@ -6442,12 +6467,20 @@ microcode memory areas will stall until this bit is clear]]> 1 + + pause_gnt + 11 + [11:11] + 11 + + EV_STATUS - 0x0018 + 0x001c 0x00 32 @@ -6470,7 +6503,7 @@ microcode memory areas will stall until this bit is clear]]> EV_PENDING - 0x001c + 0x0020 0x00 32 @@ -6493,7 +6526,7 @@ microcode memory areas will stall until this bit is clear]]> EV_ENABLE - 0x0020 + 0x0024 0x00 32 @@ -6517,7 +6550,7 @@ microcode memory areas will stall until this bit is clear]]> INSTRUCTION - 0x0024 + 0x0028 0x00 32 @@ -6575,7 +6608,7 @@ exactly reflects the binary layout of an Engine instruction.]]> 0 - 0x28 + 0x2c registers @@ -6919,7 +6952,7 @@ of the PLL]]> ROM 0x00000000 - 0x00008000 + 0x0000000C SRAM diff --git a/services/engine-25519/src/main.rs b/services/engine-25519/src/main.rs index b2bfeeccf..4adae829b 100644 --- a/services/engine-25519/src/main.rs +++ b/services/engine-25519/src/main.rs @@ -447,11 +447,16 @@ fn xmain() -> ! { xous::create_thread_1(susres_thread, (&mut engine25519) as *mut Engine25519Hw as usize).expect("couldn't start susres handler thread"); let mut client_cid: Option = None; + let mut job_count = 0; loop { let mut msg = xous::receive_message(engine25519_sid).unwrap(); log::trace!("Message: {:?}", msg); match FromPrimitive::from_usize(msg.body.id()) { Some(Opcode::RunJob) => { + if job_count % 100 == 0 { + log::info!("engine job {}", job_count); // leave this here for now so we can confirm that HW acceleration is being selected when we think it is! + } + job_count += 1; // don't start a new job if a suspend is in progress while SUSPEND_IN_PROGRESS.load(Ordering::Relaxed) { log::trace!("waiting for suspend to finish"); diff --git a/services/engine-sha512/src/lib.rs b/services/engine-sha512/src/lib.rs index 14eea2aa6..b036b98a6 100644 --- a/services/engine-sha512/src/lib.rs +++ b/services/engine-sha512/src/lib.rs @@ -201,7 +201,7 @@ impl Sha512 { FallbackStrategy::HardwareThenSoftware }; Sha512 { - use_soft: true, + use_soft: false, strategy, engine: Engine512::new(&H512), in_progress: false, @@ -344,7 +344,7 @@ impl Sha512Trunc256 { FallbackStrategy::HardwareThenSoftware }; Sha512Trunc256 { - use_soft: true, + use_soft: false, strategy, engine: Engine512::new(&H512_TRUNC_256), in_progress: false, diff --git a/services/engine-sha512/src/main.rs b/services/engine-sha512/src/main.rs index 5328c9bf7..dfc4c32d1 100644 --- a/services/engine-sha512/src/main.rs +++ b/services/engine-sha512/src/main.rs @@ -350,7 +350,7 @@ fn xmain() -> ! { let mut client_id: Option<[u32; 3]> = None; let mut mode: Option = None; - + let mut job_count = 0; loop { let mut msg = xous::receive_message(engine512_sid).unwrap(); match FromPrimitive::from_usize(msg.body.id()) { @@ -401,6 +401,10 @@ fn xmain() -> ! { } } Some(Opcode::Finalize) => { + if job_count % 100 == 0 { + log::info!("sha512 job {}", job_count); // leave this here for now so we can confirm HW accel is being used when we think it is! + } + job_count += 1; let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut finalized = buffer.to_original::().unwrap(); match client_id { diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 4de456208..c25e77eab 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -39,6 +39,7 @@ rkyv = {version = "0.4.3", default-features = false, features = ["const_generics # for the curve25519 tests # hardware acceleration adaptations are inserted into a fork of the main branch. rand_core = "0.5" +hex = {version = "0.4.3", default-features = false, features = []} [dependencies.curve25519-dalek] git="https://github.com/betrusted-io/curve25519-dalek.git" branch="main" @@ -51,6 +52,12 @@ version = "1.1.1" default-features = false features = ["u32_backend"] +[dependencies.ed25519-dalek] +version = "1.0.1" +#path = "../../../ed25519-dalek" +default-features = false +features = ["u32_backend"] + [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = {path = "../../utralib"} diff --git a/services/shellchat/src/cmds/engine.rs b/services/shellchat/src/cmds/engine.rs index 9a316506c..747c71551 100644 --- a/services/shellchat/src/cmds/engine.rs +++ b/services/shellchat/src/cmds/engine.rs @@ -18,15 +18,18 @@ pub static ENGINE_VECTORS: &[u8; 15652] = include_bytes!("engine25519_vectors.bi #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum BenchOp { StartEngine, + StartDh, Quit, } #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum BenchResult { EngineDone, + DhDone, } const TEST_ITERS: usize = 10; +const TEST_ITERS_DH: usize = 200; fn vector_read(word_offset: usize) -> u32 { let mut bytes: [u8; 4] = [0; 4]; @@ -125,6 +128,8 @@ pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { let mut engine = engine_25519::Engine25519::new(); + let mut trng = trng::Trng::new(&xns).unwrap(); + loop { let msg = xous::receive_message(sid).unwrap(); log::debug!("benchmark got msg {:?}", msg); @@ -142,9 +147,47 @@ pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { xous::Message::new_scalar(CB_ID.load(Ordering::Relaxed) as usize, passes as usize, fails as usize, - 0, 0) + BenchResult::EngineDone.to_usize().unwrap(), TEST_ITERS as usize) ).unwrap(); }, + /* + 2xop => each iteration has 2x DH ops in it (one for alice, one for bob) + 202ms/2xop (10 x 10 iters - sw) + 40.5ms/2xop (10 x 10 iters - hw) + 33ms/2xop (200 iters - hw) + 190ms/2xop (200 iters - sw) + */ + Some(BenchOp::StartDh) => { + let mut passes = 0; + let mut fails = 0; + + use x25519_dalek::{StaticSecret, PublicKey}; + let alice_secret = StaticSecret::new(&mut trng); + let alice_public = PublicKey::from(&alice_secret); + let bob_secret = StaticSecret::new(&mut trng); + let bob_public = PublicKey::from(&bob_secret); + for _ in 0..TEST_ITERS_DH { + let alice_shared_secret = alice_secret.diffie_hellman(&bob_public); + let bob_shared_secret = bob_secret.diffie_hellman(&alice_public); + let mut pass = true; + for (&alice, &bob) in alice_shared_secret.as_bytes().iter().zip(bob_shared_secret.as_bytes().iter()) { + if alice != bob { + pass = false; + } + } + if pass { + passes += 2; // 2 diffie_hellman ops / iter + } else { + fails += 2; + } + } + xous::send_message(callback_conn, + xous::Message::new_scalar(CB_ID.load(Ordering::Relaxed) as usize, + passes as usize, + fails as usize, + BenchResult::DhDone.to_usize().unwrap(), TEST_ITERS_DH as usize) + ).unwrap(); + } Some(BenchOp::Quit) => { log::info!("quitting benchmark thread"); break; @@ -186,7 +229,7 @@ impl<'a> ShellCmdApi<'a> for Engine { fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result>, xous::Error> { use core::fmt::Write; let mut ret = String::<1024>::new(); - let helpstring = "engine [check] [bench] [susres]"; + let helpstring = "engine [check] [bench] [benchdh] [susres] [dh] [ed]"; let mut tokens = args.as_str().unwrap().split(' '); @@ -207,6 +250,14 @@ impl<'a> ShellCmdApi<'a> for Engine { ).unwrap(); write!(ret, "Starting Engine hardware benchmark with {} iters", TEST_ITERS).unwrap(); } + "benchdh" => { + let start = env.ticktimer.elapsed_ms(); + self.start_time = Some(start); + xous::send_message(self.benchmark_cid, + xous::Message::new_scalar(BenchOp::StartDh.to_usize().unwrap(), 0, 0, 0, 0) + ).unwrap(); + write!(ret, "Starting DH hardware benchmark").unwrap(); + } "susres" => { let start = env.ticktimer.elapsed_ms(); self.start_time = Some(start); @@ -232,14 +283,59 @@ impl<'a> ShellCmdApi<'a> for Engine { pass = false; } } - log::info!("alice: {:?}", alice_shared_secret.as_bytes()); - log::info!("bob: {:?}", bob_shared_secret.as_bytes()); + log::info!("alice: {:02x?}", alice_shared_secret.as_bytes()); + log::info!("bob: {:02x?}", bob_shared_secret.as_bytes()); if pass { write!(ret, "x25519 key exchange pass").unwrap(); } else { write!(ret, "x25519 key exchange fail").unwrap(); } } + "ed" => { + use ed25519_dalek::*; + use ed25519::signature::Signature as _; + use hex::FromHex; + let secret_key: &[u8] = b"833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42"; + let public_key: &[u8] = b"ec172b93ad5e563bf4932c70e1245034c35467ef2efd4d64ebf819683467e2bf"; + let message: &[u8] = b"616263"; + let signature: &[u8] = b"98a70222f0b8121aa9d30f813d683f809e462b469c7ff87639499bb94e6dae4131f85042463c2a355a2003d062adf5aaa10b8c61e636062aaad11c2a26083406"; + + let sec_bytes = <[u8; 32]>::from_hex(secret_key).unwrap(); + let pub_bytes = <[u8; 32]>::from_hex(public_key).unwrap(); + let msg_bytes = <[u8; 3]>::from_hex(message).unwrap(); + let sig_bytes = <[u8; 64]>::from_hex(signature).unwrap(); + + let secret: SecretKey = SecretKey::from_bytes(&sec_bytes[..SECRET_KEY_LENGTH]).unwrap(); + let public: PublicKey = PublicKey::from_bytes(&pub_bytes[..PUBLIC_KEY_LENGTH]).unwrap(); + let keypair: Keypair = Keypair{ secret: secret, public: public }; + let sig1: Signature = Signature::from_bytes(&sig_bytes[..]).unwrap(); + + let mut prehash_for_signing = engine_sha512::Sha512::default(); // this defaults to Hw then Sw strategy + let mut prehash_for_verifying = engine_sha512::Sha512::default(); + + prehash_for_signing.update(&msg_bytes[..]); + prehash_for_verifying.update(&msg_bytes[..]); + + let sig2: Signature = keypair.sign_prehashed(prehash_for_signing, None).unwrap(); + + log::info!("original: {:02x?}", sig1); + log::info!("produced: {:02x?}", sig2); + let mut pass = true; + if sig1 != sig2 { + pass = false; + write!(ret, + "Original signature from test vectors doesn't equal signature produced:\ + \noriginal:\n{:?}\nproduced:\n{:?}", sig1, sig2).unwrap(); + } + if keypair.verify_prehashed(prehash_for_verifying, None, &sig2).is_err() { + pass = false; + write!(ret, + "Could not verify ed25519ph signature!").unwrap(); + } + if pass { + write!(ret, "Passed ed25519 simple check").unwrap(); + } + } _ => { write!(ret, "{}", helpstring).unwrap(); } @@ -257,10 +353,20 @@ impl<'a> ShellCmdApi<'a> for Engine { log::debug!("benchmark callback"); let mut ret = String::<1024>::new(); - xous::msg_scalar_unpack!(msg, passes, fails, _, _, { + xous::msg_scalar_unpack!(msg, passes, fails, result_type, iters, { let end = env.ticktimer.elapsed_ms(); - let elapsed: f64 = ((end - self.start_time.unwrap()) as f64) / TEST_ITERS as f64; - write!(ret, "{}ms/check_iter; In total, Engine passed {} vectors, failed {} vectors", elapsed, passes, fails).unwrap(); + let elapsed: f64 = ((end - self.start_time.unwrap()) as f64) / iters as f64; + match FromPrimitive::from_usize(result_type) { + Some(BenchResult::EngineDone) => { + write!(ret, "{}ms/check_iter; In total, Engine passed {} vectors, failed {} vectors", elapsed, passes, fails).unwrap(); + }, + Some(BenchResult::DhDone) => { + write!(ret, "{}ms/DH_iter; Passed {} ops, failed {} ops", elapsed, passes, fails).unwrap(); + }, + _ => { + write!(ret, "Engine bench callback with unknown bench type").unwrap(); + } + } }); Ok(Some(ret)) }