diff --git a/Cargo.lock b/Cargo.lock index ab3691eb5..9c9ab5116 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -493,6 +493,33 @@ dependencies = [ "cipher", ] +[[package]] +name = "curve25519-dalek" +version = "3.1.0" +dependencies = [ + "byteorder", + "digest", + "engine-25519", + "engine25519-as", + "rand 0.7.3", + "rand_core 0.5.1", + "subtle", + "zeroize", +] + +[[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 = "digest" version = "0.9.0" @@ -566,6 +593,10 @@ dependencies = [ "xous-names", ] +[[package]] +name = "engine25519-as" +version = "0.1.0" + [[package]] name = "enum_dispatch" version = "0.3.7" @@ -1256,7 +1287,7 @@ checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" dependencies = [ "libc", "rand_chacha 0.3.0", - "rand_core 0.6.1", + "rand_core 0.6.3", "rand_hc 0.3.0", ] @@ -1277,7 +1308,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" dependencies = [ "ppv-lite86", - "rand_core 0.6.1", + "rand_core 0.6.3", ] [[package]] @@ -1288,9 +1319,9 @@ checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" [[package]] name = "rand_core" -version = "0.6.1" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" dependencies = [ "getrandom", ] @@ -1310,7 +1341,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" dependencies = [ - "rand_core 0.6.1", + "rand_core 0.6.3", ] [[package]] @@ -1553,6 +1584,7 @@ dependencies = [ "codec", "com", "content-plugin-api", + "curve25519-dalek 3.1.0", "digest", "engine-25519", "engine-sha512", @@ -1567,12 +1599,14 @@ dependencies = [ "log-server", "num-derive", "num-traits", + "rand_core 0.5.1", "rkyv 0.4.3", "rtc", "susres", "ticktimer-server", "trng", "utralib", + "x25519-dalek", "xous", "xous-ipc", "xous-names", @@ -1602,6 +1636,12 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "subtle" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" + [[package]] name = "susres" version = "0.1.0" @@ -1659,6 +1699,18 @@ dependencies = [ "unicode-xid 0.2.1", ] +[[package]] +name = "synstructure" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" +dependencies = [ + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.60", + "unicode-xid 0.2.1", +] + [[package]] name = "tar" version = "0.4.32" @@ -1758,6 +1810,7 @@ dependencies = [ "log-server", "num-derive", "num-traits", + "rand_core 0.5.1", "rkyv 0.4.3", "susres", "ticktimer-server", @@ -1986,6 +2039,17 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "x25519-dalek" +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)", + "rand_core 0.5.1", + "zeroize", +] + [[package]] name = "xattr" version = "0.2.2" @@ -2098,3 +2162,24 @@ name = "zero" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f1bc8a6b2005884962297587045002d8cfb8dcec9db332f4ca216ddc5de82c5" + +[[package]] +name = "zeroize" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4756f7db3f7b5574938c3eb1c117038b8e07f95ee6718c0efad4ac21508f1efd" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c1e130bebaeab2f23886bf9acbaca14b092408c452543c857f66399cd6dab1" +dependencies = [ + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.60", + "synstructure", +] diff --git a/Cargo.toml b/Cargo.toml index 2b649faf5..639df5193 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -90,3 +90,4 @@ lto = "fat" [patch.crates-io] xous = {path = "xous-rs"} xous-macros = {path = "macros"} +engine-25519 = {path = "services/engine-25519"} \ No newline at end of file diff --git a/services/engine-25519/Cargo.toml b/services/engine-25519/Cargo.toml index 487c05270..119b4655a 100644 --- a/services/engine-25519/Cargo.toml +++ b/services/engine-25519/Cargo.toml @@ -19,10 +19,6 @@ num-derive = {version = "0.3", default-features = false} num-traits = {version = "0.2", default-features = false} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} -#curve25519-dalek = {git = "https://github.com/betrusted-io/curve25519-dalek.git", rev = "2ea17d554a54066efb3bc4895bee92e5b2e1bd50"} -#curve25519-dalek-hw = {path = "../../../curve25519-dalek"} -#curve25519-dalek = "3.1.0" - [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/services/engine-25519/src/api.rs b/services/engine-25519/src/api.rs index 40cb2ca29..450412204 100644 --- a/services/engine-25519/src/api.rs +++ b/services/engine-25519/src/api.rs @@ -41,6 +41,25 @@ pub struct Job { pub window: Option, } +pub enum EngineError { + InternalError, + UnsupportedFeature, + ServerNotFound, + EngineBusy, + IllegalOpcode, + UnknownError, +} + +// a trait for plugging into external crates +// mainly, this just wraps xous::Error into something more informative, and is +// a vestige of an attempt to do a differnt type of integration to the Dalek +// crates that did not work out very well, but it seemed a shame to throw +// away the more descriptive custom error types. +pub trait XousEngine25519 { + fn new() -> Self; + fn run_job(&mut self, job: Job) -> Result<[u32; RF_SIZE_IN_U32], EngineError>; +} + #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { /// Runs a job, if the server is not already occupied diff --git a/services/engine-25519/src/lib.rs b/services/engine-25519/src/lib.rs index 169121435..6a1267b00 100644 --- a/services/engine-25519/src/lib.rs +++ b/services/engine-25519/src/lib.rs @@ -149,3 +149,24 @@ fn engine_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { } xous::destroy_server(sid).unwrap(); } + + +impl XousEngine25519 for Engine25519 { + fn new() -> Self { + Engine25519::new() + } + fn run_job(&mut self, job: Job) -> Result<[u32; RF_SIZE_IN_U32], EngineError> { + match self.spawn_job(job) { + Ok(r) => Ok(r), + Err(e) => match e { + xous::Error::InternalError => Err(EngineError::InternalError), + xous::Error::InvalidSyscall => Err(EngineError::UnsupportedFeature), + xous::Error::ServerNotFound => Err(EngineError::ServerNotFound), + xous::Error::ServerQueueFull => Err(EngineError::EngineBusy), + xous::Error::InvalidString => Err(EngineError::IllegalOpcode), + _ => Err(EngineError::UnknownError), + } + } + } +} + diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 1093e4c0f..17e773368 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -36,6 +36,21 @@ num-derive = {version = "0.3", default-features = false} num-traits = {version = "0.2", default-features = false} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} +# for the curve25519 tests +#curve25519-dalek = "3.1.0" +#curve25519-dalek = {git = "https://github.com/betrusted-io/curve25519-dalek.git", rev = "2ea17d554a54066efb3bc4895bee92e5b2e1bd50"} +# hardware acceleration adaptations are inserted into a fork of the main branch. +rand_core = "0.5" +[dependencies.curve25519-dalek] +path = "../../../curve25519-dalek" +default-features = false +features = ["u32_backend", "betrusted"] + +[dependencies.x25519-dalek] +version = "1.1.1" +default-features = false +features = ["u32_backend"] + [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = {path = "../../utralib"} diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs index d61161e35..985a41c70 100644 --- a/services/shellchat/src/cmds.rs +++ b/services/shellchat/src/cmds.rs @@ -36,7 +36,7 @@ macro_rules! cmd_api { }; } - +use trng::*; /////////////////////////// Command shell integration #[derive(Debug)] pub struct CommonEnv { @@ -45,7 +45,7 @@ pub struct CommonEnv { ticktimer: ticktimer_server::Ticktimer, gam: gam::Gam, cb_registrations: heapless::FnvIndexMap::, 8>, - trng: trng::Trng, + trng: Trng, xns: xous_names::XousNames, } impl CommonEnv { @@ -129,7 +129,7 @@ impl CmdEnv { ticktimer: ticktimer, gam: gam::Gam::new(&xns).expect("couldn't connect to GAM"), cb_registrations: FnvIndexMap::new(), - trng: trng::Trng::new(&xns).unwrap(), + trng: Trng::new(&xns).unwrap(), xns: xous_names::XousNames::new().unwrap(), }; //let fcc = Fcc::new(&mut common); diff --git a/services/shellchat/src/cmds/engine.rs b/services/shellchat/src/cmds/engine.rs index ddfdabe93..c985b0985 100644 --- a/services/shellchat/src/cmds/engine.rs +++ b/services/shellchat/src/cmds/engine.rs @@ -214,6 +214,28 @@ impl<'a> ShellCmdApi<'a> for Engine { self.susres.initiate_suspend().unwrap(); write!(ret, "Interrupted Engine hardware benchmark with a suspend/resume").unwrap(); } + "dh" => { + use x25519_dalek::{EphemeralSecret, PublicKey}; + let alice_secret = EphemeralSecret::new(&mut env.trng); + let alice_public = PublicKey::from(&alice_secret); + let bob_secret = EphemeralSecret::new(&mut env.trng); + let bob_public = PublicKey::from(&bob_secret); + 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; + } + } + log::info!("alice: {:?}", alice_shared_secret.as_bytes()); + log::info!("bob: {:?}", bob_shared_secret.as_bytes()); + if pass { + write!(ret, "x25519 key exchange pass").unwrap(); + } else { + write!(ret, "x25519 key exchange fail").unwrap(); + } + } _ => { write!(ret, "{}", helpstring).unwrap(); } diff --git a/services/trng/Cargo.toml b/services/trng/Cargo.toml index 51b402e29..915aefab8 100644 --- a/services/trng/Cargo.toml +++ b/services/trng/Cargo.toml @@ -16,6 +16,7 @@ num-traits = {version = "0.2", default-features = false} susres = {path = "../susres"} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} xous-ipc = {path = "../../xous-ipc"} +rand_core = "0.5" [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/services/trng/src/lib.rs b/services/trng/src/lib.rs index 3ca32267a..e74726f81 100644 --- a/services/trng/src/lib.rs +++ b/services/trng/src/lib.rs @@ -118,4 +118,75 @@ fn error_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { } } xous::destroy_server(sid).unwrap(); -} \ No newline at end of file +} + +use rand_core::{impls, CryptoRng, RngCore}; +impl CryptoRng for Trng {} +impl RngCore for Trng { + fn next_u32(&mut self) -> u32 { + self.get_u32().expect("couldn't get random u32 from server") + } + fn next_u64(&mut self) -> u64 { + self.get_u64().expect("couldn't get random u64 from server") + } + fn fill_bytes(&mut self, dest: &mut[u8]) { + // smaller than 64 bytes (512 bits), just use 8x next_u64 calls to fill. + if dest.len() < 64 { + return impls::fill_bytes_via_next(self, dest); + } + // one page is the max size we can do with the fill_buf() API + let chunks_page = + dest.chunks_exact_mut(4096); + for chunks in chunks_page.into_iter() { + let mut data: [u32; 4096/4] = [0; 4096/4]; + self.fill_buf(&mut data).expect("couldn't fill page-sized TRNG buffer"); + for(&src, dst) in data.iter().zip(chunks.chunks_exact_mut(4)) { + for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) { + *dst_byte = src_byte; + } + } + } + // a mid-sized chunk to span the gap between page and our smallest granularity + let chunks_512 = + dest.chunks_exact_mut(4096) + .into_remainder() + .chunks_exact_mut(512); + for chunks in chunks_512.into_iter() { + let mut data: [u32; 512/4] = [0; 512/4]; + self.fill_buf(&mut data).expect("couldn't fill mid-sized TRNG buffer"); + for(&src, dst) in data.iter().zip(chunks.chunks_exact_mut(4)) { + for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) { + *dst_byte = src_byte; + } + } + } + // our smallest-sized "standard" chunk + let chunks_smallest = + dest.chunks_exact_mut(4096) + .into_remainder() + .chunks_exact_mut(512) + .into_remainder() + .chunks_exact_mut(64); + for chunks in chunks_smallest.into_iter() { + let mut data: [u32; 64/4] = [0; 64/4]; + self.fill_buf(&mut data).expect("couldn't fill small-sized TRNG buffer"); + for(&src, dst) in data.iter().zip(chunks.chunks_exact_mut(4)) { + for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) { + *dst_byte = src_byte; + } + } + } + // any leftover bytes + let leftovers = + dest.chunks_exact_mut(4096) + .into_remainder() + .chunks_exact_mut(512) + .into_remainder() + .chunks_exact_mut(64) + .into_remainder(); + impls::fill_bytes_via_next(self, leftovers); + } + fn try_fill_bytes(&mut self, dest: &mut[u8]) -> Result<(), rand_core::Error> { + Ok(self.fill_bytes(dest)) + } +}