diff --git a/.cargo/config b/.cargo/config index afa361c65..58625288e 100644 --- a/.cargo/config +++ b/.cargo/config @@ -15,3 +15,6 @@ rustflags = ["--cfg", "crossbeam_no_atomic_64"] # "-C", "link-arg=-shared", # "--cfg", "crossbeam_no_atomic_64" # ] + +[target.riscv32imac-unknown-xous-elf] +rustflags = ["--cfg", 'curve25519_dalek_backend="u32e_backend"'] \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index dba188ae9..3d96ace26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1150,26 +1150,27 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" [[package]] name = "curve25519-dalek" -version = "4.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e89b8c6a2e4b1f45971ad09761aafb85514a84744b67a95e32c3cc1352d1f65c" +version = "4.1.2" +source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#468e304624b4bc4004863c9147176238cfc0dc10" dependencies = [ "cfg-if", "cpufeatures", "curve25519-dalek-derive", "digest 0.10.7", - "fiat-crypto 0.2.6", - "platforms", + "engine25519-as", + "fiat-crypto 0.2.7", + "log", "rustc_version 0.4.0", "subtle", + "utralib 0.1.24", + "xous 0.9.58 (registry+https://github.com/rust-lang/crates.io-index)", "zeroize", ] [[package]] name = "curve25519-dalek-derive" version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#468e304624b4bc4004863c9147176238cfc0dc10" dependencies = [ "proc-macro2", "quote", @@ -1520,9 +1521,9 @@ checksum = "e18997d4604542d0736fae2c5ad6de987f0a50530cbcc14a7ce5a685328a252d" [[package]] name = "ed25519-dalek" -version = "2.1.1" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871" +checksum = "1f628eaec48bfd21b865dc2950cfa014450c01d2fa2b69a86c2fd5844ec523c0" dependencies = [ "curve25519-dalek", "ed25519 2.2.3", @@ -1632,26 +1633,12 @@ dependencies = [ [[package]] name = "engine-25519" version = "0.1.0" -dependencies = [ - "engine25519-as", - "llio", - "log", - "num-derive 0.3.3", - "num-traits", - "rkyv", - "utralib 0.1.24", - "xous", - "xous-api-log", - "xous-api-names", - "xous-api-susres", - "xous-api-ticktimer", - "xous-ipc 0.9.58 (registry+https://github.com/rust-lang/crates.io-index)", -] +source = "git+https://github.com/betrusted-io/xous-engine-25519.git?rev=63d3d1f30736022e791deaacf4dd62c00b42fe2e#63d3d1f30736022e791deaacf4dd62c00b42fe2e" [[package]] name = "engine25519-as" version = "0.1.0" -source = "git+https://github.com/betrusted-io/engine25519-as.git?rev=d249c967556b02ab5439eacb5078fa00c60b93d6#d249c967556b02ab5439eacb5078fa00c60b93d6" +source = "git+https://github.com/betrusted-io/engine25519-as.git?rev=775e8406eb4aad08f05ae10619fcb4ca891ba0a6#775e8406eb4aad08f05ae10619fcb4ca891ba0a6" [[package]] name = "enum-iterator" @@ -1815,9 +1802,9 @@ checksum = "e825f6987101665dea6ec934c09ec6d721de7bc1bf92248e1d5810c8cd636b77" [[package]] name = "fiat-crypto" -version = "0.2.6" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1676f435fc1dadde4d03e43f5d62b259e1ce5f40bd4ffb21db2b42ebe59c1382" +checksum = "c007b1ae3abe1cb6f85a16305acd418b7ca6343b953633fee2b76d8f108b830f" [[package]] name = "filetime" @@ -3656,12 +3643,6 @@ version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb" -[[package]] -name = "platforms" -version = "3.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "626dec3cac7cc0e1577a2ec3fc496277ec2baa084bebad95bb6fdbfae235f84c" - [[package]] name = "plotters" version = "0.3.5" @@ -4207,7 +4188,6 @@ dependencies = [ "curve25519-dalek", "digest 0.10.7", "ed25519-dalek", - "engine-25519", "gam", "graphics-server", "hex 0.4.3", @@ -4592,7 +4572,6 @@ dependencies = [ "digest 0.10.7", "dns", "ed25519-dalek", - "engine-25519", "gam", "graphics-server", "hex 0.4.3", diff --git a/Cargo.toml b/Cargo.toml index d02d953b4..05064169b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,8 +17,6 @@ default-members = [ "services/shellchat", "services/llio", "services/codec", - # "services/engine-sha512", - "services/engine-25519", "services/aes", "services/spinor", "services/root-keys", @@ -60,8 +58,6 @@ members = [ "services/content-plugin-api", "services/llio", "services/codec", - # "services/engine-sha512", - "services/engine-25519", "services/aes", "services/spinor", "services/root-keys", @@ -147,15 +143,12 @@ sha2_legacy = { git = "https://github.com/RustCrypto/hashes.git", tag = "sha2-v0 [patch.crates-io.aes] path = "services/aes" -#[patch.crates-io.curve25519-dalek] -#git = "https://github.com/betrusted-io/curve25519-dalek.git" -#branch = "main" -#path = "../curve25519-dalek/curve25519-dalek" # when doing local dev work +[patch.crates-io.curve25519-dalek] +git = "https://github.com/betrusted-io/curve25519-dalek.git" +branch = "main" +# path = "../curve25519-dalek/curve25519-dalek" # when doing local dev work # feature overrides are specified at the crate level -[patch."https://github.com/betrusted-io/xous-engine-25519.git"] -engine-25519 = { path = "services/engine-25519" } - [patch.crates-io.usb-device] git = "https://github.com/betrusted-io/usb-device.git" branch = "main" diff --git a/RELEASE-v0.9.md b/RELEASE-v0.9.md index 84b4876f3..24fae8954 100644 --- a/RELEASE-v0.9.md +++ b/RELEASE-v0.9.md @@ -471,6 +471,7 @@ perform the Xous firmware upgrade. This requires running manual update commands, - a number of other crates and pins were upgraded in the process due to a cargo `update` run; the `build.rs` changes were reviewed and nothing nefarious was found, so at least this process did not introduce any obvious attacks against build hosts through supply chain contamination. - @gsora has added the `hidapi` - apps can now register a HID descriptor for custom interactions over USB. See `apps/hidv2` for democumentation. - change kernel and loader targets to riscv-unknown-elf-none because `xous` is now a proper target (required for Rust 1.76 compatibility) +- `curve25519-dalek` API is now at 4.1.2, thanks to @kotval for pulling it together. The new API removes `engine-25519` and rolls hardware allocate/release into the forked crate, similar to how sha2 was ported. `engine-25519` crate now removed from source tree, as it is now depracted since all the functionality was pulled into `curve25519-dalek`. ## Roadmap - Lots of testing and bug fixes diff --git a/loader/curve25519-dalek-loader/Cargo.toml b/loader/curve25519-dalek-loader/Cargo.toml index 22ab8a575..0c86eb7bb 100644 --- a/loader/curve25519-dalek-loader/Cargo.toml +++ b/loader/curve25519-dalek-loader/Cargo.toml @@ -66,7 +66,7 @@ optional = true [dependencies.engine25519-as] git = "https://github.com/betrusted-io/engine25519-as.git" -rev = "d249c967556b02ab5439eacb5078fa00c60b93d6" +rev = "775e8406eb4aad08f05ae10619fcb4ca891ba0a6" default-features = false features = [] optional = true diff --git a/services/engine-25519/Cargo.toml b/services/engine-25519/Cargo.toml deleted file mode 100644 index ffc11c4a0..000000000 --- a/services/engine-25519/Cargo.toml +++ /dev/null @@ -1,36 +0,0 @@ -[package] -name = "engine-25519" -version = "0.1.0" -authors = ["bunnie "] -edition = "2018" -description = "Curve25519 Engine" - -# Dependency versions enforced by Cargo.lock. -[dependencies] -xous = "0.9.58" -log-server = { package = "xous-api-log", version = "0.1.54" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.54" } -xous-names = { package = "xous-api-names", version = "0.9.56" } -log = "0.4.14" -susres = { package = "xous-api-susres", version = "0.9.54" } -llio = { path = "../llio" } # temporary for testing some power stuff -utralib = { version = "0.1.24", optional = true, default-features = false } - -xous-ipc = "0.9.58" -num-derive = { version = "0.3.3", default-features = false } -num-traits = { version = "0.2.14", default-features = false } -rkyv = { version = "0.4.3", default-features = false, features = [ - "const_generics", -] } - -[dependencies.engine25519-as] -git = "https://github.com/betrusted-io/engine25519-as.git" -rev = "d249c967556b02ab5439eacb5078fa00c60b93d6" -default-features = false -features = [] - -[features] -precursor = ["utralib/precursor"] -hosted = ["utralib/hosted"] -renode = ["utralib/renode"] -default = [] diff --git a/services/engine-25519/README.md b/services/engine-25519/README.md deleted file mode 100755 index 554058fa6..000000000 --- a/services/engine-25519/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Curve 25519 engine - -Please refer to the [datasheet](https://ci.betrusted.io/betrusted-soc/doc/engine.html) for comprehensive documentation -on how the hardware engine itself works. - diff --git a/services/engine-25519/src/api.rs b/services/engine-25519/src/api.rs deleted file mode 100644 index d022b8ddc..000000000 --- a/services/engine-25519/src/api.rs +++ /dev/null @@ -1,93 +0,0 @@ -pub(crate) const SERVER_NAME_ENGINE25519: &str = "_Curve-25519 Accelerator Engine_"; - -// I don't understand why clippy says these are unused. But clippy is wrong. Shut up, clippy. -#[allow(dead_code)] -pub(crate) const UCODE_U8_BASE: usize = 0x0; -#[allow(dead_code)] -pub(crate) const UCODE_U32_BASE: usize = 0x0; -#[allow(dead_code)] -pub(crate) const UCODE_U32_SIZE: usize = 0x1000 / 4; -#[allow(dead_code)] -pub(crate) const UCODE_U8_SIZE: usize = 0x1000; -#[allow(dead_code)] -pub(crate) const RF_U8_BASE: usize = 0x1_0000; -#[allow(dead_code)] -pub(crate) const RF_U32_BASE: usize = 0x1_0000 / 4; -#[allow(dead_code)] -pub(crate) const RF_TOTAL_U32_SIZE: usize = 0x4000 / 4; -#[allow(dead_code)] -pub(crate) const RF_TOTAL_U8_SIZE: usize = 0x4000; - -pub(crate) const NUM_REGS: usize = 32; -pub(crate) const BITWIDTH: usize = 256; -#[allow(dead_code)] // not used in hosted -pub(crate) const NUM_WINDOWS: usize = 16; -pub const RF_SIZE_IN_U32: usize = NUM_REGS * (BITWIDTH / 32); // 32 registers, 256 bits/register/32 bits per u32 -#[allow(dead_code)] // not used in hosted -pub const TOTAL_RF_SIZE_IN_U32: usize = NUM_REGS * (BITWIDTH / 32) * NUM_WINDOWS; // 32 registers, 256 bits/register/32 bits per u32, times 16 windows - -#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone, Copy)] -pub struct Job { - /// If present the SID of the server to which we should return results asynchronously. - /// If None, then the job will run synchronously. - pub id: Option<[u32; 4]>, - /// start location for microcode load - pub uc_start: u32, - /// length of the microcode to run - pub uc_len: u32, - /// microcode program - pub ucode: [u32; 1024], - /// initial register file contents (also contains any arguments to the program) - pub rf: [u32; RF_SIZE_IN_U32], - /// which register window, if any, to use for the job - pub window: Option, -} - -#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone, Copy)] -pub struct MontgomeryJob { - pub x0_u: [u8; 32], - pub x0_w: [u8; 32], - pub x1_u: [u8; 32], - pub x1_w: [u8; 32], - pub affine_u: [u8; 32], - pub scalar: [u8; 32], -} - -mod rkyv_enum; -pub use rkyv_enum::*; - -#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] -pub(crate) enum Opcode { - /// Runs a job, if the server is not already occupied - RunJob, - - /// MontgomeryJob - MontgomeryJob, - - /// a function that can be polled to determine if the block has been currently acquired - IsFree, - - /// IRQ handler feedback - EngineDone, - IllegalOpcode, - - // note: suspend/resume handled by a separate thread and server - /// exit the server - Quit, - // note that suspend/resume is handled by a secondary thread that can concurrently - // interrupt the main thread and store the state -} - -#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] -pub(crate) enum Return { - Result, - Quit, -} - -#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] -pub(crate) enum SusResOps { - /// Suspend/resume callback - SuspendResume, - /// exit the thread - Quit, -} diff --git a/services/engine-25519/src/api/rkyv_enum.rs b/services/engine-25519/src/api/rkyv_enum.rs deleted file mode 100644 index 89c6be724..000000000 --- a/services/engine-25519/src/api/rkyv_enum.rs +++ /dev/null @@ -1,26 +0,0 @@ -// As of Rust 1.64.0: -// -// Rkyv-derived enums throw warnings that rkyv::Archive derived enums are never used -// and I can't figure out how to make them go away. Since they spam the build log, -// rkyv-derived enums are now isolated to their own file with a file-wide `dead_code` -// allow on top. -// -// This might be a temporary compiler regression, or it could just -// be yet another indicator that it's time to upgrade rkyv. However, we are waiting -// until rkyv hits 0.8 (the "shouldn't ever change again but still not sure enough -// for 1.0") release until we rework the entire system to chase the latest rkyv. -// As of now, the current version is 0.7.x and there isn't a timeline yet for 0.8. - -#![allow(dead_code)] - -#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] -pub enum JobResult { - /// returns a copy of the entire register file as a result - Result([u32; crate::RF_SIZE_IN_U32]), - SingleResult([u8; 32]), - Started, - EngineUnavailable, - NotAsyncObject, // attempt to run an async job on an object that was setup for sync jobs - IllegalOpcodeException, - SuspendError, -} diff --git a/services/engine-25519/src/lib.rs b/services/engine-25519/src/lib.rs deleted file mode 100644 index 1af20c3d1..000000000 --- a/services/engine-25519/src/lib.rs +++ /dev/null @@ -1,202 +0,0 @@ -#![cfg_attr(target_os = "none", no_std)] - -//! Detailed docs are parked under Structs/Engine25519 down below - -/* -#[cfg(any(feature="precursor", feature="renode"))] -pub use curve25519_dalek_hw::*; - -#[cfg(not(target_os = "xous"))] -pub use curve25519_dalek::*; -*/ - -pub mod api; -pub use api::*; -use num_traits::*; -use xous::{Message, CID}; -use xous_ipc::Buffer; - -static mut ENGINE_CB: Option = None; - -#[doc = include_str!("../README.md")] -pub struct Engine25519 { - conn: CID, - cb_sid: Option<[u32; 4]>, -} -impl Engine25519 { - // this is used to set up a system with async callbacks - pub fn new_async(xns: &xous_names::XousNames) -> Result { - REFCOUNT.fetch_add(1, Ordering::Relaxed); - let conn = xns - .request_connection_blocking(api::SERVER_NAME_ENGINE25519) - .expect("Can't connect to Engine25519 server"); - - let sid = xous::create_server().unwrap(); - let sid_tuple = sid.to_u32(); - xous::create_thread_4( - engine_cb_server, - sid_tuple.0 as usize, - sid_tuple.1 as usize, - sid_tuple.2 as usize, - sid_tuple.3 as usize, - ) - .unwrap(); - - Ok(Engine25519 { conn, cb_sid: Some(sid.to_array()) }) - } - - // typically, we just want synchronous callbacks. This integrates more nicely into single-threaded code. - pub fn new() -> Self { - REFCOUNT.fetch_add(1, Ordering::Relaxed); - let xns = xous_names::XousNames::new().unwrap(); - let conn = xns - .request_connection_blocking(api::SERVER_NAME_ENGINE25519) - .expect("Can't connect to Engine25519 server"); - - Engine25519 { conn, cb_sid: None } - } - - /// this is unsafe because the caller must make sure that within their process space, multiple threads - /// are not spawing concurrent jobs; or, if they do, they all share a common result_callback method: - /// we always blindly replace result_callback! - pub unsafe fn spawn_async_job( - &mut self, - job: &mut Job, - result_callback: fn(JobResult), - ) -> Result { - if let Some(cb_sid) = self.cb_sid { - ENGINE_CB = Some(result_callback); // this is the unsafe bit! - job.id = Some(cb_sid); - let mut buf = Buffer::into_buf(*job).or(Err(xous::Error::InternalError))?; - buf.lend_mut(self.conn, Opcode::RunJob.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; - - match buf.to_original().unwrap() { - JobResult::Started => Ok(true), - _ => Ok(false), - } - } else { - Err(xous::Error::InvalidSyscall) - } - } - - pub fn montgomery_job(&mut self, job: MontgomeryJob) -> Result<[u8; 32], xous::Error> { - let mut buf = Buffer::into_buf(job).or(Err(xous::Error::OutOfMemory))?; - match buf.lend_mut(self.conn, Opcode::MontgomeryJob.to_u32().unwrap()) { - Ok(_) => (), - Err(e) => { - if e == xous::Error::ServerNotFound { - log::error!( - "Looks like another thread called disconnect() on us while we weren't looking: {:?}", - e - ); - } else { - log::error!("couldn't lend buffer: {:?}", e); - } - return Err(e); - } - } - - match buf.to_original().unwrap() { - JobResult::SingleResult(r) => Ok(r), - JobResult::EngineUnavailable => { - log::debug!("spawn job: engine unavailable"); - Err(xous::Error::ServerQueueFull) - } - JobResult::IllegalOpcodeException => { - log::error!("spawn job: illegal opcode"); - Err(xous::Error::InvalidString) - } - _ => { - log::error!("spawn job: other error"); - Err(xous::Error::UnknownError) - } - } - } - - /// this is a blocking version of spawn_async_job. - /// if the engine is free, it will block until a result is returned - /// if the engine is busy, it will return an EngineUnavailable result. - pub fn spawn_job(&mut self, job: Job) -> Result<[u32; RF_SIZE_IN_U32], xous::Error> { - if job.id.is_some() { - log::error!("spawn sync job: don't set a job id if you want a synchronous job!"); - return Err(xous::Error::InvalidSyscall); // the job.id should be None for a sync job. Do not set it to Some, even as a joke. - } - let mut buf = Buffer::into_buf(job).or(Err(xous::Error::OutOfMemory))?; - match buf.lend_mut(self.conn, Opcode::RunJob.to_u32().unwrap()) { - Ok(_) => (), - Err(e) => { - if e == xous::Error::ServerNotFound { - log::error!( - "Looks like another thread called disconnect() on us while we weren't looking: {:?}", - e - ); - } else { - log::error!("couldn't lend buffer: {:?}", e); - } - return Err(e); - } - } - - match buf.to_original().unwrap() { - JobResult::Result(rf) => Ok(rf), - JobResult::EngineUnavailable => { - log::debug!("spawn job: engine unavailable"); - Err(xous::Error::ServerQueueFull) - } - JobResult::IllegalOpcodeException => { - log::error!("spawn job: illegal opcode"); - Err(xous::Error::InvalidString) - } - _ => { - log::error!("spawn job: other error"); - Err(xous::Error::UnknownError) - } - } - } -} - -use core::sync::atomic::{AtomicU32, Ordering}; -static REFCOUNT: AtomicU32 = AtomicU32::new(0); -impl Drop for Engine25519 { - fn drop(&mut self) { - // disconnect from the main server, only if there are no more instances active - if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { - unsafe { - xous::disconnect(self.conn).unwrap(); - } - } - // disconnect and destroy the callback server that is specific to this instance - if let Some(cb_sid) = self.cb_sid { - let cid = xous::connect(xous::SID::from_array(cb_sid)).unwrap(); - xous::send_message(cid, Message::new_scalar(Return::Quit.to_usize().unwrap(), 0, 0, 0, 0)) - .unwrap(); - unsafe { - xous::disconnect(cid).unwrap(); - } - } - } -} - -/// handles callback messages from the engine server, in the library user's process space. -fn engine_cb_server(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); - loop { - let msg = xous::receive_message(sid).unwrap(); - match FromPrimitive::from_usize(msg.body.id()) { - Some(Return::Result) => { - let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; - let job_result = buffer.to_original::().unwrap(); - unsafe { - if let Some(cb) = ENGINE_CB { - cb(job_result) - } - } - } - Some(Return::Quit) => { - break; // this exits the loop and kills the thread - } - None => (), - } - } - xous::destroy_server(sid).unwrap(); -} diff --git a/services/engine-25519/src/main.rs b/services/engine-25519/src/main.rs deleted file mode 100644 index 1f85e97e6..000000000 --- a/services/engine-25519/src/main.rs +++ /dev/null @@ -1,1013 +0,0 @@ -#![cfg_attr(target_os = "none", no_std)] -#![cfg_attr(target_os = "none", no_main)] -#![recursion_limit = "512"] - -mod api; -use core::sync::atomic::{AtomicBool, Ordering}; - -use api::*; -use num_traits::*; -use xous::msg_blocking_scalar_unpack; -use xous_ipc::Buffer; - -#[cfg(any(feature = "precursor", feature = "renode"))] -#[macro_use] -extern crate engine25519_as; - -static RUN_IN_PROGRESS: AtomicBool = AtomicBool::new(false); -static DISALLOW_SUSPEND: AtomicBool = AtomicBool::new(false); -static SUSPEND_IN_PROGRESS: AtomicBool = AtomicBool::new(false); - -#[cfg(any(feature = "precursor", feature = "renode"))] -mod implementation { - use core::convert::TryInto; - use core::sync::atomic::Ordering; - - use num_traits::*; - use susres::{RegManager, RegOrField, SuspendResume}; - use utralib::generated::*; - - use crate::api::*; - use crate::DISALLOW_SUSPEND; - use crate::RUN_IN_PROGRESS; - - pub struct Engine25519Hw { - csr: utralib::CSR, - // these are slices mapped directly to the hardware memory space - ucode_hw: &'static mut [u32], - rf_hw: &'static mut [u32], - susres: RegManager<{ utra::engine::ENGINE_NUMREGS }>, - handler_conn: Option, - // don't use the susres ManagedMem primitive; it blows out the stack. Instead, heap-allocate the - // backing stores. - ucode_backing: &'static mut [u32], - rf_backing: &'static mut [u32], - mpc_resume: Option, - clean_resume: Option, - do_notify: bool, - illegal_opcode: bool, - montgomery_len: Option, - } - fn handle_engine_irq(_irq_no: usize, arg: *mut usize) { - let engine = unsafe { &mut *(arg as *mut Engine25519Hw) }; - - let reason = engine.csr.r(utra::engine::EV_PENDING); - RUN_IN_PROGRESS.store(false, Ordering::Relaxed); - if reason & engine.csr.ms(utra::engine::EV_PENDING_ILLEGAL_OPCODE, 1) != 0 { - engine.illegal_opcode = true; - } else { - engine.illegal_opcode = false; - } - - if engine.do_notify { - if let Some(conn) = engine.handler_conn { - if reason & engine.csr.ms(utra::engine::EV_PENDING_FINISHED, 1) != 0 { - xous::try_send_message( - conn, - xous::Message::new_scalar(Opcode::EngineDone.to_usize().unwrap(), 0, 0, 0, 0), - ) - .map(|_| ()) - .unwrap(); - } - if reason & engine.csr.ms(utra::engine::EV_PENDING_ILLEGAL_OPCODE, 1) != 0 { - xous::try_send_message( - conn, - xous::Message::new_scalar(Opcode::IllegalOpcode.to_usize().unwrap(), 0, 0, 0, 0), - ) - .map(|_| ()) - .unwrap(); - } - } else { - panic!("engine interrupt happened without a handler"); - } - } - // clear the interrupt - engine.csr.wo(utra::engine::EV_PENDING, reason); - } - - impl Engine25519Hw { - pub fn new(handler_conn: xous::CID) -> Engine25519Hw { - assert!( - TOTAL_RF_SIZE_IN_U32 == RF_TOTAL_U32_SIZE, - "sanity check has failed on logical dimensions of register file vs hardware aperture sizes" - ); - - log::trace!("creating engine25519 CSR"); - let csr = xous::syscall::map_memory( - xous::MemoryAddress::new(utra::engine::HW_ENGINE_BASE), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map engine CSR range"); - log::trace!("creating engine25519 memrange"); - let mem = xous::syscall::map_memory( - xous::MemoryAddress::new(utralib::HW_ENGINE_MEM), - None, - utralib::HW_ENGINE_MEM_LEN, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map engine memory window range"); - - let ucode_mem = xous::syscall::map_memory( - None, - None, - UCODE_U8_SIZE, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map backing store for microcode"); - let rf_mem = xous::syscall::map_memory( - None, - None, - RF_TOTAL_U8_SIZE, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map RF backing store"); - Engine25519Hw { - csr: CSR::new(csr.as_mut_ptr() as *mut u32), - susres: RegManager::new(csr.as_mut_ptr() as *mut u32), - ucode_hw: unsafe { - core::slice::from_raw_parts_mut( - mem.as_mut_ptr().add(UCODE_U8_BASE) as *mut u32, - UCODE_U32_SIZE, - ) - }, - rf_hw: unsafe { - core::slice::from_raw_parts_mut( - mem.as_mut_ptr().add(RF_U8_BASE) as *mut u32, - RF_TOTAL_U32_SIZE, - ) - }, - handler_conn: Some(handler_conn), - ucode_backing: unsafe { - core::slice::from_raw_parts_mut(ucode_mem.as_mut_ptr() as *mut u32, UCODE_U32_SIZE) - }, - rf_backing: unsafe { - core::slice::from_raw_parts_mut(rf_mem.as_mut_ptr() as *mut u32, RF_TOTAL_U32_SIZE) - }, - mpc_resume: None, - clean_resume: None, - do_notify: false, - illegal_opcode: false, - montgomery_len: None, - } - } - - pub fn init(&mut self) { - log::trace!("claiming interrupt"); - xous::claim_interrupt( - utra::engine::ENGINE_IRQ, - handle_engine_irq, - self as *mut Engine25519Hw as *mut usize, - ) - .expect("couldn't claim engine irq"); - - log::trace!("enabling interrupt"); - self.csr.wo(utra::engine::EV_PENDING, 0xFFFF_FFFF); // clear any droppings. - self.csr.wo( - utra::engine::EV_ENABLE, - self.csr.ms(utra::engine::EV_ENABLE_FINISHED, 1) - | self.csr.ms(utra::engine::EV_ENABLE_ILLEGAL_OPCODE, 1), - ); - - // setup the susres context. Most of the defaults are fine, so they aren't explicitly initialized - // in the code above, but they still have to show up down here in case we're suspended - // mid-op - log::trace!("setting up susres"); - self.susres.push(RegOrField::Reg(utra::engine::POWER), None); // on resume, this needs to be setup first, so that the "pause" state is captured correctly - self.susres.push(RegOrField::Reg(utra::engine::WINDOW), None); - self.susres.push(RegOrField::Reg(utra::engine::MPSTART), None); - self.susres.push(RegOrField::Reg(utra::engine::MPLEN), None); - self.susres.push_fixed_value(RegOrField::Reg(utra::engine::EV_PENDING), 0xFFFF_FFFF); - - // manually handle event enable, because the bootloader could have messed with our current state - // self.susres.push(RegOrField::Reg(utra::engine::EV_ENABLE), None); - - // self.susres.push(RegOrField::Reg(utra::engine::CONTROL), None); // don't push this, we need to - // manually coordinate `mpcresume` before resuming - } - - pub fn suspend(&mut self) { - self.clean_resume = Some(false); // if this isn't set to try by the resume, then we've had a failure - - if self.csr.rf(utra::engine::STATUS_RUNNING) == 1 { - // request a pause from the engine. it will stop executing at the next microcode op - // and assert STATUS_PAUSE_GNT - self.csr.rmwf(utra::engine::POWER_PAUSE_REQ, 1); - while (self.csr.rf(utra::engine::STATUS_PAUSE_GNT) == 0) - && (self.csr.rf(utra::engine::STATUS_RUNNING) == 1) - { - // busy wait for this to clear, or the engine to stop running; should happen in << 1us - if self.csr.rf(utra::engine::STATUS_PAUSE_GNT) == 1 { - // store the current PC value as a resume note - self.mpc_resume = Some(self.csr.rf(utra::engine::STATUS_MPC)); - } else { - // the implication here is the engine actually finished its last opcode, so it would - // not enter the paused state; rather, it is now stopped. - self.mpc_resume = None; - } - } - } else { - self.mpc_resume = None; - } - // disable interrupts - self.csr.wo(utra::engine::EV_ENABLE, 0); - - // accessing ucode & rf requires clocks to be on - let orig_state = if self.csr.rf(utra::engine::POWER_ON) == 0 { - self.csr.rmwf(utra::engine::POWER_ON, 1); - false - } else { - true - }; - - // copy the ucode & rf into the backing memory - for (&src, dst) in self.rf_hw.iter().zip(self.rf_backing.iter_mut()) { - *dst = src; - } - for (&src, dst) in self.ucode_hw.iter().zip(self.ucode_backing.iter_mut()) { - *dst = src; - } - - // restore the power state setting - if !orig_state { - self.csr.rmwf(utra::engine::POWER_ON, 0); - } - // now backup all the machine registers - self.susres.suspend(); - } - - pub fn resume(&mut self) { - self.susres.resume(); - // if the power wasn't on, we have to flip it on temporarily to access the backing memories - let orig_state = if self.csr.rf(utra::engine::POWER_ON) == 0 { - self.csr.rmwf(utra::engine::POWER_ON, 1); - false - } else { - true - }; - - // restore ucode & rf - for (&src, dst) in self.rf_backing.iter().zip(self.rf_hw.iter_mut()) { - *dst = src; - } - for (&src, dst) in self.ucode_backing.iter().zip(self.ucode_hw.iter_mut()) { - *dst = src; - } - - // restore the power state setting - if !orig_state { - self.csr.rmwf(utra::engine::POWER_ON, 0); - } - - log::info!("orig_state: {:?}", orig_state); - // clear any droppings from the bootloader, and then re-enable - self.csr.wo(utra::engine::EV_PENDING, 0xFFFF_FFFF); - self.csr.wo( - utra::engine::EV_ENABLE, - self.csr.ms(utra::engine::EV_ENABLE_FINISHED, 1) - | self.csr.ms(utra::engine::EV_ENABLE_ILLEGAL_OPCODE, 1), - ); - - // in the case of a resume from pause, we need to specify the PC to resume from - // clear the pause - if let Some(mpc) = self.mpc_resume { - log::info!("suspended during engine25519 transaction, resuming..."); - if self.csr.rf(utra::engine::POWER_PAUSE_REQ) != 1 { - log::error!( - "resuming from an unexpected state: we had mpc of {} set, but pause was not requested!", - mpc - ); - self.clean_resume = Some(false); - // we don't resume execution. Presumably this will cause terrible things to happen such as - // the interrupt waiting for execution to be done to never trigger. - // perhaps we could try to trigger that somehow...? - } else { - // the pause was requested, but crucially, the engine was not in the "go" state. This - // means that the engine will get its starting PC from the resume PC - // when we hit go again, instead of the mpstart register. - self.csr.wfo(utra::engine::MPRESUME_MPRESUME, mpc); - // start the engine - self.csr.wfo(utra::engine::CONTROL_GO, 1); - // it should grab the PC from `mpresume` and then go to the paused state. Wait until - // we have achieved the identical paused state that happened before resume, before - // unpausing! - while self.csr.rf(utra::engine::STATUS_PAUSE_GNT) == 0 { - // this should be very fast, within a couple CPU cycles - } - self.clean_resume = Some(true); // note that we had a clean resume before resuming the execution - // this resumes execution of the CPU - self.csr.rmwf(utra::engine::POWER_PAUSE_REQ, 0); - } - } else { - log::info!("engine25519 simple resume"); - // if we didn't have a resume PC set, we weren't paused, so we just continue on our merry way. - self.clean_resume = Some(true); - } - } - - #[allow(dead_code)] - pub(crate) fn power_dbg(&self) -> u32 { self.csr.r(utra::engine::POWER) } - - pub fn power_on(&mut self, on: bool) { - if on { - self.csr.rmwf(utra::engine::POWER_ON, 1); - } else { - self.csr.rmwf(utra::engine::POWER_ON, 0); - } - } - - pub fn run(&mut self, job: Job) { - self.montgomery_len = None; - log::trace!("entering run"); - // block any suspends from happening while we set up the engine - DISALLOW_SUSPEND.store(true, Ordering::Relaxed); - - log::trace!("extracting windows"); - let window = if let Some(w) = job.window { - w as usize - } else { - 0 as usize // default window is 0 - }; - log::trace!("using window {}", window); - // this should "just panic" if we have a bad window arg, which is the desired behavior - //let mut num = 0; - for (&src, dst) in job - .rf - .iter() - .zip(self.rf_hw[window * RF_SIZE_IN_U32..(window + 1) * RF_SIZE_IN_U32].iter_mut()) - { - //*dst = src; // this gets optimized away, so replace it with the below line of code - // since these are given to us by an iter, and we aren't doing .add() etc. on the pointers, - // should be safe. - unsafe { (dst as *mut u32).write_volatile(src) }; - - // performance critical, so comment it out if not using this debug code - /*if src != 0 { - log::trace!("rf {}: 0x{:08x}", num, src); - } - num += 1;*/ - } - // copy in the microcode - //num = 0; - for (&src, dst) in job.ucode.iter().zip(self.ucode_hw.iter_mut()) { - //*dst = src; // this gets optimized away, so replace it with the below line of code - // since these are given to us by an iter, and we aren't doing .add() etc. on the pointers, - // should be safe. - unsafe { (dst as *mut u32).write_volatile(src) }; - - /*if src != 0 { - log::trace!("ucode {}: 0x{:08x}", num, src); - } - num += 1;*/ - } - self.csr.wfo(utra::engine::WINDOW_WINDOW, window as u32); // this value should now be validated because an invalid window would cause a panic on slice copy - self.csr.wfo(utra::engine::MPSTART_MPSTART, job.uc_start); - self.csr.wfo(utra::engine::MPLEN_MPLEN, job.uc_len); - - log::trace!("sanity check uc{:08x}, rf{:08x}", self.ucode_hw[0], self.rf_hw[0]); - - // determine if this a sync or async call - if job.id.is_some() { - // async calls need a notification message - self.do_notify = true; - } else { - // sync calls poll a state variable, and thus no message is sent - self.do_notify = false; - } - // setup the sync polling variable - RUN_IN_PROGRESS.store(true, Ordering::Relaxed); - // this will start the run. interrupts should *already* be enabled for the completion - // notification... - self.csr.wfo(utra::engine::CONTROL_GO, 1); - - // we are now in a stable config, suspends are allowed - DISALLOW_SUSPEND.store(false, Ordering::Relaxed); - } - - fn copy_reg(&mut self, r: [u8; 32], ra: usize, window: usize) { - for (src, dst) in r.chunks_exact(4).zip( - self.rf_hw[window * RF_SIZE_IN_U32 + ra * 8..window * RF_SIZE_IN_U32 + (ra + 1) * 8] - .iter_mut(), - ) { - unsafe { - (dst as *mut u32).write_volatile(u32::from_le_bytes(src[0..4].try_into().unwrap())); - } - } - } - - fn load_montgomery(&mut self, mpstart: u32) -> u32 { - let mcode = assemble_engine25519!( - start: - // P.U in %20 - // P.W in %21 - // Q.U in %22 - // Q.W in %23 - // affine_PmQ in %24 - // %30 is the TRD scratch register and cswap dummy - // %29 is the subtraction temporary value register and k_t - // x0.U in %25 - // x0.W in %26 - // x1.U in %27 - // x1.W in %28 - // %19 is the loop counter, starts with 254 (if 0, loop runs exactly once) - // %31 is the scalar - // %18 is the swap variable - psa %18, #0 - - // for i in (0..255).rev() - mainloop: - // let choice: u8 = (bits[i + 1] ^ bits[i]) as u8; - // ProjectivePoint::conditional_swap(&mut x0, &mut x1, choice.into()); - xbt %29, %31 // orignally[k_t = (k>>t) & 1] now[k_t = k[254]] - shl %31, %31 // k = k<<1 - xor %18, %18, %29 // swap ^= k_t - - // cswap x0.U (%25), x1.U (%27) - xor %30, %25, %27 - msk %30, %18, %30 - xor %25, %30, %25 - xor %27, %30, %27 - // cswap x0.W (%26), x1.W (%28) - xor %30, %26, %28 - msk %30, %18, %30 - xor %26, %30, %26 - xor %28, %30, %28 - - psa %18, %29 // swap = k_t - - // differential_add_and_double(&mut x0, &mut x1, &affine_u); - psa %20, %25 - psa %21, %26 - psa %22, %27 - psa %23, %28 - // affine_u is already in %24 - - // let t0 = &P.U + &P.W; - add %0, %20, %21 - trd %30, %0 - sub %0, %0, %30 - // let t1 = &P.U - &P.W; - sub %21, #3, %21 // negate &P.W using #FIELDPRIME (#3) - add %1, %20, %21 - trd %30, %1 - sub %1, %1, %30 - // let t2 = &Q.U + &Q.W; - add %2, %22, %23 - trd %30, %2 - sub %2, %2, %30 - // let t3 = &Q.U - &Q.W; - sub %23, #3, %23 - add %3, %22, %23 - trd %30, %3 - sub %3, %3, %30 - // let t4 = t0.square(); // (U_P + W_P)^2 = U_P^2 + 2 U_P W_P + W_P^2 - mul %4, %0, %0 - // let t5 = t1.square(); // (U_P - W_P)^2 = U_P^2 - 2 U_P W_P + W_P^2 - mul %5, %1, %1 - // let t6 = &t4 - &t5; // 4 U_P W_P - sub %29, #3, %5 - add %6, %4, %29 - trd %30, %6 - sub %6, %6, %30 - // let t7 = &t0 * &t3; // (U_P + W_P) (U_Q - W_Q) = U_P U_Q + W_P U_Q - U_P W_Q - W_P W_Q - mul %7, %0, %3 - // let t8 = &t1 * &t2; // (U_P - W_P) (U_Q + W_Q) = U_P U_Q - W_P U_Q + U_P W_Q - W_P W_Q - mul %8, %1, %2 - // let t9 = &t7 + &t8; // 2 (U_P U_Q - W_P W_Q) - add %9, %7, %8 - trd %30, %9 - sub %9, %9, %30 - // let t10 = &t7 - &t8; // 2 (W_P U_Q - U_P W_Q) - sub %29, #3, %8 - add %10, %7, %29 - trd %30, %10 - sub %10, %10, %30 - // let t11 = t9.square(); // 4 (U_P U_Q - W_P W_Q)^2 - mul %11, %9, %9 - // let t12 = t10.square(); // 4 (W_P U_Q - U_P W_Q)^2 - mul %12, %10, %10 - // let t13 = &APLUS2_OVER_FOUR * &t6; // (A + 2) U_P U_Q - mul %13, #4, %6 // #4 is A+2/4 - // let t14 = &t4 * &t5; // ((U_P + W_P)(U_P - W_P))^2 = (U_P^2 - W_P^2)^2 - mul %14, %4, %5 - // let t15 = &t13 + &t5; // (U_P - W_P)^2 + (A + 2) U_P W_P - add %15, %13, %5 - trd %30, %15 - sub %15, %15, %30 - // let t16 = &t6 * &t15; // 4 (U_P W_P) ((U_P - W_P)^2 + (A + 2) U_P W_P) - mul %16, %6, %15 - // let t17 = affine_PmQ * &t12; // U_D * 4 (W_P U_Q - U_P W_Q)^2 - mul %17, %24, %12 // affine_PmQ loaded into %24 - - ///// these can be eliminated down the road, but included for 1:1 algorithm correspodence to reference in early testing - // P.U = t14; // U_{P'} = (U_P + W_P)^2 (U_P - W_P)^2 - psa %20, %14 - // P.W = t16; // W_{P'} = (4 U_P W_P) ((U_P - W_P)^2 + ((A + 2)/4) 4 U_P W_P) - psa %21, %16 - // let t18 = t11; // W_D * 4 (U_P U_Q - W_P W_Q)^2 - // Q.U = t18; // U_{Q'} = W_D * 4 (U_P U_Q - W_P W_Q)^2 - psa %22, %11 // collapsed two to save a register - // Q.W = t17; // W_{Q'} = U_D * 4 (W_P U_Q - U_P W_Q)^2 - psa %23, %17 - - ///// 'return' arguments for next iteration, can be optimized out later - psa %25, %20 - psa %26, %21 - psa %27, %22 - psa %28, %23 - - brz end, %19 // if loop counter is 0, quit - sub %19, %19, #1 // subtract one from the loop counter and run again - brz mainloop, #0 // go back to the top - end: - // ProjectivePoint::conditional_swap(&mut x0, &mut x1, Choice::from(bits[0] as u8)); - // cswap x0.U (%25), x1.U (%27) - xor %30, %25, %27 - msk %30, %18, %30 - xor %25, %30, %25 - xor %27, %30, %27 - // cswap x0.W (%26), x1.W (%28) - xor %30, %26, %28 - msk %30, %18, %30 - xor %26, %30, %26 - xor %28, %30, %28 - - // AFFINE SPLICE -- pass arguments to the affine block - psa %29, %25 - psa %30, %26 - // W.invert() in %21 - // U in %29 - // W in %30 - // result in %31 - // loop counter in %28 - - // from FieldElement.invert() - // let (t19, t3) = self.pow22501(); // t19: 249..0 ; t3: 3,1,0 - // let t0 = self.square(); // 1 e_0 = 2^1 - mul %0, %30, %30 // self is W, e.g. %30 - // let t1 = t0.square().square(); // 3 e_1 = 2^3 - mul %1, %0, %0 - mul %1, %1, %1 - // let t2 = self * &t1; // 3,0 e_2 = 2^3 + 2^0 - mul %2, %30, %1 - // let t3 = &t0 * &t2; // 3,1,0 - mul %3, %0, %2 - // let t4 = t3.square(); // 4,2,1 - mul %4, %3, %3 - // let t5 = &t2 * &t4; // 4,3,2,1,0 - mul %5, %2, %4 - - // let t6 = t5.pow2k(5); // 9,8,7,6,5 - psa %28, #5 // coincidentally, constant #5 is the number 5 - mul %6, %5, %5 - pow2k_5: - sub %28, %28, #1 // %28 = %28 - 1 - brz pow2k_5_exit, %28 - mul %6, %6, %6 - brz pow2k_5, #0 - pow2k_5_exit: - // let t7 = &t6 * &t5; // 9,8,7,6,5,4,3,2,1,0 - mul %7, %6, %5 - - // let t8 = t7.pow2k(10); // 19..10 - psa %28, #6 // constant #6 is the number 10 - mul %8, %7, %7 - pow2k_10: - sub %28, %28, #1 - brz pow2k_10_exit, %28 - mul %8, %8, %8 - brz pow2k_10, #0 - pow2k_10_exit: - // let t9 = &t8 * &t7; // 19..0 - mul %9, %8, %7 - - // let t10 = t9.pow2k(20); // 39..20 - psa %28, #7 // constant #7 is the number 20 - mul %10, %9, %9 - pow2k_20: - sub %28, %28, #1 - brz pow2k_20_exit, %28 - mul %10, %10, %10 - brz pow2k_20, #0 - pow2k_20_exit: - // let t11 = &t10 * &t9; // 39..0 - mul %11, %10, %9 - - // let t12 = t11.pow2k(10); // 49..10 - psa %28, #6 // constant #6 is the number 10 - mul %12, %11, %11 - pow2k_10b: - sub %28, %28, #1 - brz pow2k_10b_exit, %28 - mul %12, %12, %12 - brz pow2k_10b, #0 - pow2k_10b_exit: - // let t13 = &t12 * &t7; // 49..0 - mul %13, %12, %7 - - // let t14 = t13.pow2k(50); // 99..50 - psa %28, #8 // constant #8 is the number 50 - mul %14, %13, %13 - pow2k_50a: - sub %28, %28, #1 - brz pow2k_50a_exit, %28 - mul %14, %14, %14 - brz pow2k_50a, #0 - pow2k_50a_exit: - // let t15 = &t14 * &t13; // 99..0 - mul %15, %14, %13 - - // let t16 = t15.pow2k(100); // 199..100 - psa %28, #9 // constant #9 is the number 100 - mul %16, %15, %15 - pow2k_100: - sub %28, %28, #1 - brz pow2k_100_exit, %28 - mul %16, %16, %16 - brz pow2k_100, #0 - pow2k_100_exit: - // let t17 = &t16 * &t15; // 199..0 - mul %17, %16, %15 - - // let t18 = t17.pow2k(50); // 249..50 - psa %28, #8 // constant #8 is the number 50 - mul %18, %17, %17 - pow2k_50b: - sub %28, %28, #1 - brz pow2k_50b_exit, %28 - mul %18, %18, %18 - brz pow2k_50b, #0 - pow2k_50b_exit: - // let t19 = &t18 * &t13; // 249..0 - mul %19, %18, %13 - //(t19, t3) // just a return value, values are already there, do nothing - - //let t20 = t19.pow2k(5); // 254..5 - psa %28, #5 - mul %20, %19, %19 - pow2k_5_last: - sub %28, %28, #1 - brz pow2k_5_last_exit, %28 - mul %20, %20, %20 - brz pow2k_5_last, #0 - pow2k_5_last_exit: - - //let t21 = &t20 * &t3; // 254..5,3,1,0 - mul %21, %20, %3 - - // u = &self.U * &self.W.invert() - mul %31, %29, %21 - fin // finish execution - ); - for (&src, dst) in mcode.iter().zip(self.ucode_hw[mpstart as usize..].iter_mut()) { - unsafe { (dst as *mut u32).write_volatile(src as u32) }; - } - mcode.len() as u32 - } - - pub fn montgomery(&mut self, job: MontgomeryJob) { - log::trace!("entering run"); - // block any suspends from happening while we set up the engine - DISALLOW_SUSPEND.store(true, Ordering::Relaxed); - - let window: usize = 0; - - self.copy_reg(job.x0_u, 25, window); - self.copy_reg(job.x0_w, 26, window); - self.copy_reg(job.x1_u, 27, window); - self.copy_reg(job.x1_w, 28, window); - self.copy_reg(job.affine_u, 24, window); - self.copy_reg(job.scalar, 31, window); - self.copy_reg( - [ - 254, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, - ], - 19, - window, - ); // 254 as loop counter - - let mpstart = 0; - // this optimization shaves off about 0.13ms per iteration - if self.montgomery_len.is_none() { - self.montgomery_len = Some(self.load_montgomery(mpstart) as usize); - } - self.csr.wfo(utra::engine::WINDOW_WINDOW, window as u32); // this value should now be validated because an invalid window would cause a panic on slice copy - self.csr.wfo(utra::engine::MPSTART_MPSTART, mpstart); - self.csr.wfo(utra::engine::MPLEN_MPLEN, self.montgomery_len.unwrap() as u32); - - log::trace!("sanity check uc{:08x}, rf{:08x}", self.ucode_hw[0], self.rf_hw[0]); - - // sync calls poll a state variable, and thus no message is sent - self.do_notify = false; - - // setup the sync polling variable - RUN_IN_PROGRESS.store(true, Ordering::Relaxed); - // this will start the run. interrupts should *already* be enabled for the completion - // notification... - self.csr.wfo(utra::engine::CONTROL_GO, 1); - - // we are now in a stable config, suspends are allowed - DISALLOW_SUSPEND.store(false, Ordering::Relaxed); - } - - pub fn get_result(&mut self) -> JobResult { - if let Some(clean_resume) = self.clean_resume { - if !clean_resume { - return JobResult::SuspendError; - } - } - if self.illegal_opcode { - return JobResult::IllegalOpcodeException; - } - - let mut ret_rf: [u32; RF_SIZE_IN_U32] = [0; RF_SIZE_IN_U32]; - let window = self.csr.rf(utra::engine::WINDOW_WINDOW) as usize; - //let mut num = 0; - for (&src, dst) in self.rf_hw[window * RF_SIZE_IN_U32..(window + 1) * RF_SIZE_IN_U32] - .iter() - .zip(ret_rf.iter_mut()) - { - //*dst = src; // this gets optimized away, so replace it with the below line of code - // since these are given to us by an iter, and we aren't doing .add() etc. on the pointers, - // should be safe. - unsafe { (dst as *mut u32).write_volatile(src) }; - /* - if src != 0 { - log::debug!("result {}: 0x{:08x}", num, src); - } - num += 1;*/ - } - - JobResult::Result(ret_rf) - } - - pub fn get_single_result(&mut self, r: usize) -> JobResult { - if let Some(clean_resume) = self.clean_resume { - if !clean_resume { - return JobResult::SuspendError; - } - } - if self.illegal_opcode { - return JobResult::IllegalOpcodeException; - } - - let mut ret_r: [u8; 32] = [0; 32]; - let window = self.csr.rf(utra::engine::WINDOW_WINDOW) as usize; - for (&src, dst) in self.rf_hw - [window * RF_SIZE_IN_U32 + r * 8..window * RF_SIZE_IN_U32 + (r + 1) * 8] - .iter() - .zip(ret_r.chunks_exact_mut(4)) - { - for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) { - *dst_byte = src_byte; - } - } - - JobResult::SingleResult(ret_r) - } - } -} - -// a stub to try to avoid breaking hosted mode for as long as possible. -#[cfg(not(target_os = "xous"))] -mod implementation { - use crate::api::*; - - pub struct Engine25519Hw {} - - impl Engine25519Hw { - pub fn new(_handler_conn: xous::CID) -> Engine25519Hw { Engine25519Hw {} } - - pub fn init(&mut self) {} - - pub fn suspend(&self) {} - - pub fn resume(&self) {} - - pub fn run(&mut self, _job: Job) {} - - pub fn get_result(&mut self) -> JobResult { JobResult::IllegalOpcodeException } - - pub fn power_on(&mut self, _on: bool) {} - - pub fn montgomery(&mut self, _job: MontgomeryJob) {} - - pub fn get_single_result(&mut self, _r: usize) -> JobResult { JobResult::EngineUnavailable } - } -} - -fn susres_thread(engine_arg: usize) { - use crate::implementation::Engine25519Hw; - let engine25519 = unsafe { &mut *(engine_arg as *mut Engine25519Hw) }; - - let susres_sid = xous::create_server().unwrap(); - let xns = xous_names::XousNames::new().unwrap(); - - // register a suspend/resume listener - let sr_cid = xous::connect(susres_sid).expect("couldn't create suspend callback connection"); - let mut susres = susres::Susres::new( - Some(susres::SuspendOrder::Late), - &xns, - api::SusResOps::SuspendResume as u32, - sr_cid, - ) - .expect("couldn't create suspend/resume object"); - - log::trace!("starting engine25519 suspend/resume manager loop"); - loop { - let msg = xous::receive_message(susres_sid).unwrap(); - log::trace!("Message: {:?}", msg); - match FromPrimitive::from_usize(msg.body.id()) { - Some(SusResOps::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, { - // prevent new jobs from starting while we're in suspend - // we do this first, because the race condition we're trying to catch is between a job - // being set up, and it running. - SUSPEND_IN_PROGRESS.store(true, Ordering::Relaxed); - - // this check will catch the case that a job happened to be started before we could set - // our flag above. - while DISALLOW_SUSPEND.load(Ordering::Relaxed) { - // don't start a suspend if we're in the middle of a critical region - xous::yield_slice(); - } - - // at this point: - // - there should be no new jobs in progress - // - any job that was being set up, will have been set up so its safe to interrupt execution - engine25519.suspend(); - susres.suspend_until_resume(token).expect("couldn't execute suspend/resume"); - engine25519.resume(); - SUSPEND_IN_PROGRESS.store(false, Ordering::Relaxed); - }), - Some(SusResOps::Quit) => { - log::info!("Received quit opcode, exiting!"); - break; - } - None => { - log::error!("Received unknown opcode: {:?}", msg); - } - } - } - xns.unregister_server(susres_sid).unwrap(); - xous::destroy_server(susres_sid).unwrap(); -} - -fn main() -> ! { - use crate::implementation::Engine25519Hw; - - log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Info); - log::info!("my PID is {}", xous::process::id()); - - let xns = xous_names::XousNames::new().unwrap(); - let engine25519_sid = - xns.register_name(api::SERVER_NAME_ENGINE25519, None).expect("can't register server"); - log::trace!("registered with NS -- {:?}", engine25519_sid); - - let handler_conn = xous::connect(engine25519_sid).expect("couldn't create IRQ handler connection"); - log::trace!("creating engine25519 object"); - let mut engine25519 = Box::new(Engine25519Hw::new(handler_conn)); - engine25519.init(); - - log::trace!("ready to accept requests"); - - // register a suspend/resume listener - xous::create_thread_1(susres_thread, engine25519.as_mut() as *mut Engine25519Hw as usize) - .expect("couldn't start susres handler thread"); - - let mut client_cid: Option = None; - let mut job_count = 0; - let mut mont_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::MontgomeryJob) => { - if mont_count % 100 == 0 { - log::info!("montgomery job {}", mont_count); // leave this here for now so we can confirm that HW acceleration is being selected when we think it is! - } - mont_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"); - xous::yield_slice(); - } - engine25519.power_on(true); - let mut buffer = - unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let montgomery_job = buffer.to_original::().unwrap(); - engine25519.montgomery(montgomery_job); - while RUN_IN_PROGRESS.load(Ordering::Relaxed) { - // block until the job is done - xous::yield_slice(); - } - let result = engine25519.get_single_result(31); // return the result - engine25519.power_on(false); - buffer.replace(result).unwrap(); - } - 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"); - xous::yield_slice(); - } - engine25519.power_on(true); - - let mut buffer = - unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let job = buffer.to_original::().unwrap(); - - let response = if client_cid.is_none() { - if let Some(job_id) = job.id { - log::trace!("running async job"); - // async job - // the presence of an ID indicates we are doing an async method - client_cid = Some( - xous::connect(xous::SID::from_array(job_id)) - .expect("couldn't connect to the caller's server"), - ); - engine25519.run(job); - // just let the caller know we started a job, but don't return any results - JobResult::Started - } else { - // log::trace!("running sync job {:?}", job); - // sync job - // start the job, which should set RUN_IN_PROGRESS to true - engine25519.run(job); - while RUN_IN_PROGRESS.load(Ordering::Relaxed) { - // block until the job is done - xous::yield_slice(); - } - let result = engine25519.get_result(); // return the result - engine25519.power_on(false); - result - } - } else { - JobResult::EngineUnavailable - }; - buffer.replace(response).unwrap(); - } - Some(Opcode::IsFree) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { - if client_cid.is_none() { - xous::return_scalar(msg.sender, 1).expect("couldn't return IsIdle query"); - } else { - xous::return_scalar(msg.sender, 0).expect("couldn't return IsIdle query"); - } - }), - Some(Opcode::EngineDone) => { - if let Some(cid) = client_cid { - let result = engine25519.get_result(); - let buf = Buffer::into_buf(result).or(Err(xous::Error::InternalError)).unwrap(); - buf.send(cid, Return::Result.to_u32().unwrap()) - .expect("couldn't return result to caller"); - - // this simultaneously releases the lock and disconnects from the caller - unsafe { - xous::disconnect(client_cid.take().unwrap()) - .expect("couldn't disconnect from the caller"); - } - } else { - log::error!( - "illegal state: got a result, but no client was registered. Did we forget to disable interrupts on a synchronous call??" - ); - } - engine25519.power_on(false); - } - Some(Opcode::IllegalOpcode) => { - if let Some(cid) = client_cid { - let buf = Buffer::into_buf(JobResult::IllegalOpcodeException) - .or(Err(xous::Error::InternalError)) - .unwrap(); - buf.send(cid, Return::Result.to_u32().unwrap()) - .expect("couldn't return result to caller"); - } else { - log::error!( - "illegal state: got a result, but no client was registered. Did we forget to disable interrupts on a synchronous call??" - ); - } - engine25519.power_on(false); - } - Some(Opcode::Quit) => { - log::info!("Received quit opcode, exiting!"); - engine25519.power_on(false); - break; - } - None => { - log::error!("couldn't convert opcode"); - } - } - } - // clean up our program - log::trace!("main loop exit, destroying servers"); - xns.unregister_server(engine25519_sid).unwrap(); - xous::destroy_server(engine25519_sid).unwrap(); - log::trace!("quitting"); - xous::terminate_process(0) -} diff --git a/services/root-keys/Cargo.toml b/services/root-keys/Cargo.toml index 95a08319e..72f4b0fb0 100644 --- a/services/root-keys/Cargo.toml +++ b/services/root-keys/Cargo.toml @@ -65,14 +65,15 @@ keyboard = { path = "../keyboard" } sha2 = { version = "0.10.8" } digest = "0.10.7" aes = { path = "../aes" } -engine-25519 = { path = "../engine-25519" } -byteorder = "1.4.3" # used by keywrap +# engine-25519 = { path = "../engine-25519" } +byteorder = "1.4.3" # used by keywrap # hardware acceleration adaptations are inserted into a fork of the main branch. hex = { version = "0.4.3", default-features = false, features = [] } [dependencies.curve25519-dalek] -version = "=4.1.1" # note this is patched to our fork in ./Cargo.toml +version = "=4.1.2" # note this is patched to our fork in ./Cargo.toml default-features = false +features = ["auto-release", "warn-fallback"] [dependencies.ed25519-dalek] version = "2.1.0" diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index a6db1cfc8..ef5c20dc7 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -28,7 +28,6 @@ sha2 = { version = "0.10.8" } digest = "0.10.7" aes = { path = "../aes" } cipher = "0.4.4" -engine-25519 = { path = "../engine-25519" } spinor = { path = "../spinor" } root-keys = { path = "../root-keys" } jtag = { path = "../jtag" } @@ -101,17 +100,18 @@ random-pick = { version = "1.2.16", optional = true } hex = { version = "0.4.3", default-features = false, features = [] } #sha2 = {version = "0.9.5", default-features = false, features = []} [dependencies.curve25519-dalek] -version = "=4.1.1" # note this is patched to our fork in ./Cargo.toml +version = "=4.1.2" # note this is patched to our fork in ./Cargo.toml default-features = false +features = ["auto-release", "warn-fallback"] [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"] diff --git a/services/shellchat/src/cmds/engine.rs b/services/shellchat/src/cmds/engine.rs index 90bfe5359..055104992 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; @@ -38,7 +39,15 @@ fn vector_read(word_offset: usize) -> u32 { u32::from_le_bytes(bytes) } -fn run_vectors(engine: &mut Engine25519) -> (usize, usize) { +fn run_vectors() -> (usize, usize) { + use curve25519_dalek::backend::serial::u32e::*; + while ensure_engine().is_err() { + xous::yield_slice(); + } + // safety: it's safe because it's after ensure_engine() + let ucode_hw = unsafe { get_ucode() }; + let rf_hw = unsafe { get_rf() }; + let mut test_offset: usize = 0x0; let mut passes: usize = 0; let mut fails: usize = 0; @@ -58,17 +67,9 @@ fn run_vectors(engine: &mut Engine25519) -> (usize, usize) { let num_vectors = (vector_read(test_offset) >> 0) & 0x3F_FFFF; test_offset += 1; - let mut job = Job { - id: None, - uc_start: load_addr, - uc_len: code_len, - ucode: [0; 1024], - rf: [0; RF_SIZE_IN_U32], - window: Some(window as u8), - }; - - for i in load_addr as usize..(load_addr + code_len) as usize { - job.ucode[i] = vector_read(test_offset); + let mut mcode = Vec::::new(); + for _i in load_addr as usize..(load_addr + code_len) as usize { + mcode.push(vector_read(test_offset) as i32); test_offset += 1; } @@ -79,32 +80,20 @@ fn run_vectors(engine: &mut Engine25519) -> (usize, usize) { // a test suite can have numerous vectors against a common code base for argcnt in 0..num_args { for word in 0..8 { - job.rf[(/* window * 32 * 8 + */argcnt * 8 + word) as usize] = vector_read(test_offset); + rf_hw[(window * 32 * 8 + argcnt * 8 + word) as usize] = vector_read(test_offset); test_offset += 1; } } let mut passed = true; log::trace!("spawning job"); - match engine.spawn_job(job) { - Ok(rf_result) => { - for word in 0..8 { - let expect = vector_read(test_offset); - test_offset += 1; - let actual = rf_result[(/* window * 32 * 8 + */31 * 8 + word) as usize]; - if expect != actual { - log::error!("e/a {:08x}/{:08x}", expect, actual); - passed = false; - } - } - } - Err(e) => { - log::error!( - "system error {:?} in running test vector: {}/0x{:x}", - e, - vector, - test_offset - ); + run_job(ucode_hw, rf_hw, &mcode, window as usize); + for word in 0..8 { + let expect = vector_read(test_offset); + test_offset += 1; + let actual = rf_hw[(window * 32 * 8 + 31 * 8 + word) as usize]; + if expect != actual { + log::error!("e/a {:08x}/{:08x}", expect, actual); passed = false; } } @@ -121,6 +110,7 @@ fn run_vectors(engine: &mut Engine25519) -> (usize, usize) { } } } + curve25519_dalek::backend::serial::u32e::free_engine(); (passes, fails) } /* @@ -128,14 +118,16 @@ 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) + +With new curve25519 api: +53.6ms/check vector iteration (10 iters total, 1450 vectors total) with engine retained +56.5ms/check with auto-free */ 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(); let callback_conn = xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap(); - let mut engine = engine_25519::Engine25519::new(); - let mut trng = trng::Trng::new(&xns).unwrap(); loop { @@ -146,7 +138,7 @@ pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { let mut passes = 0; let mut fails = 0; for _ in 0..TEST_ITERS { - let (p, f) = run_vectors(&mut engine); + let (p, f) = run_vectors(); passes += p; fails += f; } @@ -174,6 +166,9 @@ pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { 13.54ms/2xop (200 iters -hw) -- microcode pre-loaded, minimal registers transferred using MontgomeryJob call 12.2ms/2xop (200 iters -hw) -- with L2 cache on (128k) 12.29ms/2xop (200 iters -hw) -- with L2 cache on (64k) + + 8.37ms/2xop (200 iters - hw) - with new curve25519 lib and engine retained after every loop + 33.04ms/2xop (200 iters - hw) - with new curve25519 lib and auto-free engine after every loop */ Some(BenchOp::StartDh) => { let mut passes = 0; @@ -299,6 +294,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,16 +313,18 @@ 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 { "check" => { - let mut engine = engine_25519::Engine25519::new(); log::debug!("running vectors"); - let (passes, fails) = run_vectors(&mut engine); + let (passes, fails) = run_vectors(); write!(ret, "Engine passed {} vectors, failed {} vectors", passes, fails).unwrap(); } @@ -364,13 +362,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 +574,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)) } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 26ec1c9ee..7cea1137a 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -99,7 +99,7 @@ fn main() -> Result<(), Box> { "root-keys", "trng", "sha2@0.10.8", - "engine-25519", + // "engine-25519", "jtag", // GUI front end "status",