diff --git a/Cargo.lock b/Cargo.lock index 85e2e1a78..25bd4b319 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -441,15 +441,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" -[[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" @@ -590,27 +581,6 @@ dependencies = [ "xous-names", ] -[[package]] -name = "engine-sha512" -version = "0.1.0" -dependencies = [ - "block-buffer", - "digest", - "log", - "log-server", - "num-derive", - "num-traits", - "opaque-debug", - "rkyv 0.4.3", - "susres", - "ticktimer-server", - "trng", - "utralib", - "xous", - "xous-ipc", - "xous-names", -] - [[package]] name = "engine25519-as" version = "0.1.0" @@ -1583,14 +1553,22 @@ dependencies = [ [[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", + "log", + "log-server", + "num-derive", + "num-traits", "opaque-debug", + "rkyv 0.4.3", + "susres", + "ticktimer-server", + "trng", + "utralib", + "xous", + "xous-ipc", + "xous-names", ] [[package]] @@ -1607,7 +1585,6 @@ dependencies = [ "digest", "ed25519-dalek", "engine-25519", - "engine-sha512", "gam", "graphics-server", "hex", diff --git a/Cargo.toml b/Cargo.toml index 524821295..eac8f2374 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,8 +99,10 @@ 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.sha2] +path = "services/engine-sha512" [patch.crates-io.curve25519-dalek] git="https://github.com/betrusted-io/curve25519-dalek.git" diff --git a/services/engine-sha512/Cargo.toml b/services/engine-sha512/Cargo.toml index 1624f2f3a..6f545bc89 100644 --- a/services/engine-sha512/Cargo.toml +++ b/services/engine-sha512/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "engine-sha512" -version = "0.1.0" +name = "sha2" +version = "0.9.5" authors = ["bunnie "] edition = "2018" description = "Sha512 hardware accelerator engine" diff --git a/services/engine-sha512/src/main.rs b/services/engine-sha512/src/main.rs index 7bee1e9fd..f912e7c00 100644 --- a/services/engine-sha512/src/main.rs +++ b/services/engine-sha512/src/main.rs @@ -385,9 +385,9 @@ fn xmain() -> ! { } } Some(Opcode::Finalize) => { - //if job_count % 100 == 0 { + 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(); diff --git a/services/engine-sha512/src/sha512.rs b/services/engine-sha512/src/sha512.rs index 91e93beac..2526b7dc8 100755 --- a/services/engine-sha512/src/sha512.rs +++ b/services/engine-sha512/src/sha512.rs @@ -187,15 +187,19 @@ pub struct Sha512 { } impl Sha512 { // use this function instead of default for more control over configuration of the hardware engine - pub fn new(maybe_strategy: Option) -> Self { - let strategy = if let Some(strat) = maybe_strategy { - strat - } else { - FallbackStrategy::HardwareThenSoftware - }; + pub fn new() -> Self { Sha512 { use_soft: false, - strategy, + strategy: FallbackStrategy::HardwareThenSoftware, + engine: Engine512::new(&H512), + in_progress: false, + length: 0, + } + } + pub fn new_with_strategy(strat: FallbackStrategy) -> Self { + Sha512 { + use_soft: false, + strategy: strat, engine: Engine512::new(&H512), in_progress: false, length: 0, @@ -207,7 +211,7 @@ impl Sha512 { impl Default for Sha512 { fn default() -> Self { - Sha512::new(Some(FallbackStrategy::HardwareThenSoftware)) + Sha512::new_with_strategy(FallbackStrategy::HardwareThenSoftware) // xns should Drop here and release the connection allocated by it automatically } } @@ -331,15 +335,19 @@ pub struct Sha512Trunc256 { } impl Sha512Trunc256 { // use this function instead of default for more control over configuration of the hardware engine - pub fn new(maybe_strategy: Option) -> Self { - let strategy = if let Some(strat) = maybe_strategy { - strat - } else { - FallbackStrategy::HardwareThenSoftware - }; + pub fn new() -> Self { Sha512Trunc256 { use_soft: false, - strategy, + strategy: FallbackStrategy::HardwareThenSoftware, + engine: Engine512::new(&H512_TRUNC_256), + in_progress: false, + length: 0, + } + } + pub fn new_with_strategy(strat: FallbackStrategy) -> Self { + Sha512Trunc256 { + use_soft: false, + strategy: strat, engine: Engine512::new(&H512_TRUNC_256), in_progress: false, length: 0, @@ -359,7 +367,7 @@ impl Drop for Sha512Trunc256 { impl Default for Sha512Trunc256 { fn default() -> Self { - Sha512Trunc256::new(Some(FallbackStrategy::HardwareThenSoftware)) + Sha512Trunc256::new_with_strategy(FallbackStrategy::HardwareThenSoftware) } } diff --git a/services/root-keys/Cargo.toml b/services/root-keys/Cargo.toml index 47074bc21..cf5002738 100644 --- a/services/root-keys/Cargo.toml +++ b/services/root-keys/Cargo.toml @@ -32,8 +32,8 @@ locales = {path = "../../locales"} graphics-server = {path = "../graphics-server"} # private keys -sha2 = {version = "0.9.5", default-features = false, features = []} -#sha2 = {path = "../engine-sha512", package = "engine-sha512"} +#sha2 = {version = "0.9.5", default-features = false, features = []} +sha2 = {path = "../engine-sha512"} digest = "0.9" aes-xous={path="../aes"} engine-25519 = {path="../engine-25519"} diff --git a/services/root-keys/src/implementation.rs b/services/root-keys/src/implementation.rs index 7b0f96ea2..7f56b6a1e 100644 --- a/services/root-keys/src/implementation.rs +++ b/services/root-keys/src/implementation.rs @@ -13,7 +13,7 @@ use core::convert::TryInto; use ed25519_dalek::{Keypair, Signature, PublicKey, Signer, ExpandedSecretKey}; use curve25519_dalek::scalar::Scalar; use curve25519_dalek::constants; -use sha2::{Sha512, Sha512Trunc256, Sha256}; +use sha2::{FallbackStrategy, Sha256, Sha512, Sha512Trunc256}; use digest::Digest; use graphics_server::BulkRead; use core::mem::size_of; @@ -599,8 +599,7 @@ impl<'a> RootKeys { // expand the 24-byte (192-bit) bcrypt result into 256 bits, so we can use it directly as XOR key material // against 256-bit AES and curve25519 keys // for such a small hash, software is the most performant choice - //let mut hasher = Sha512Trunc256::new(Some(FallbackStrategy::SoftwareOnly)); - let mut hasher = Sha512Trunc256::new(); + let mut hasher = Sha512Trunc256::new_with_strategy(FallbackStrategy::SoftwareOnly); hasher.update(hashed_password); let digest = hasher.finalize(); @@ -1386,8 +1385,7 @@ impl<'a> RootKeys { + 8; // two u32 words are appended to the end, which repeat the "version" and "length" fields encoded in the signature block // this is a huge hash, so, get a hardware hasher, even if it means waiting for it - //let mut hasher = Sha512::new(Some(FallbackStrategy::WaitForHardware)); - let mut hasher = Sha512::new(); + let mut hasher = Sha512::new_with_strategy(FallbackStrategy::WaitForHardware); // extract the secret key so we can prime the hash let expanded_key = ExpandedSecretKey::from(&signing_key.secret); @@ -1427,8 +1425,7 @@ impl<'a> RootKeys { let r = Scalar::from_hash(hasher); let R = (&r * &constants::ED25519_BASEPOINT_TABLE).compress(); - //let mut hasher = Sha512::new(Some(FallbackStrategy::WaitForHardware)); - let mut hasher = Sha512::new(); + let mut hasher = Sha512::new_with_strategy(FallbackStrategy::WaitForHardware); hasher.update(R.as_bytes()); hasher.update(signing_key.public.as_bytes()); @@ -1526,7 +1523,7 @@ impl<'a> RootKeys { } pub fn sign_gateware(&self, signing_key: &Keypair) -> (Signature, u32) { - let mut hasher = Sha512::new(); + let mut hasher = Sha512::new_with_strategy(FallbackStrategy::WaitForHardware); let gateware_region = self.gateware(); // sign everything except a hole just big enough to fit the digital signature record @@ -1544,12 +1541,11 @@ impl<'a> RootKeys { // read the public key directly out of the keyrom let pubkey = PublicKey::from_bytes(&self.read_key_256(KeyRomLocs::SELFSIGN_PUBKEY)).expect("public key was not valid"); - //let mut hasher = Sha512::new(Some(FallbackStrategy::WaitForHardware)); - let mut hasher = Sha512::new(); + let mut hasher = Sha512::new_with_strategy(FallbackStrategy::WaitForHardware); let gateware_region = self.gateware(); // verify everything except a hole just big enough to fit the digital signature record - log::info!("hashing kernel"); + log::info!("hashing gateware"); hasher.update(&gateware_region[..CSR_CSV_OFFSET - core::mem::size_of::()]); hasher.update(&gateware_region[CSR_CSV_OFFSET..]); log::info!("hash done"); diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index d5431449e..7ba4d2a81 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -23,7 +23,8 @@ xous-names = {path = "../xous-names"} keyboard = {path = "../keyboard"} susres = {path = "../susres"} codec = {path = "../codec"} -engine-sha512 = {path="../engine-sha512"} +#engine-sha512 = {path="../engine-sha512"} +sha2 = {path="../engine-sha512"} digest = "0.9" aes-xous={path="../aes"} cipher = "0.3" @@ -41,7 +42,7 @@ rkyv = {version = "0.4.3", default-features = false, features = ["const_generics # 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 = []} -sha2 = {version = "0.9.5", default-features = false, features = []} +#sha2 = {version = "0.9.5", default-features = false, features = []} [dependencies.curve25519-dalek] version = "3.1.0" # note this is patched to our fork in ./Cargo.toml default-features = false diff --git a/services/shellchat/src/cmds/ecup.rs b/services/shellchat/src/cmds/ecup.rs index 4cd9e156a..e5f915d59 100644 --- a/services/shellchat/src/cmds/ecup.rs +++ b/services/shellchat/src/cmds/ecup.rs @@ -5,7 +5,7 @@ use core::sync::atomic::{AtomicU32, Ordering}; static CB_ID: AtomicU32 = AtomicU32::new(0); use num_traits::*; -use engine_sha512::*; +use sha2::*; use digest::Digest; use core::fmt::Write; @@ -31,7 +31,7 @@ enum PackageType { } fn validate_package(pkg: &[u8], pkg_type: PackageType) -> bool { - let mut hasher = engine_sha512::Sha512Trunc256::new(Some(FallbackStrategy::HardwareThenSoftware)); + let mut hasher = sha2::Sha512Trunc256::new_with_strategy(FallbackStrategy::HardwareThenSoftware); let mut temp: [u8; 4] = Default::default(); temp.copy_from_slice(&pkg[0x20..0x24]); if pkg_type == PackageType::Ec { diff --git a/services/shellchat/src/cmds/sha.rs b/services/shellchat/src/cmds/sha.rs index ac658050d..101a88eed 100644 --- a/services/shellchat/src/cmds/sha.rs +++ b/services/shellchat/src/cmds/sha.rs @@ -1,7 +1,7 @@ use crate::{ShellCmdApi, CommonEnv}; use xous_ipc::String; -use engine_sha512::*; +use sha2::*; use digest::Digest; use num_traits::*; @@ -71,8 +71,8 @@ pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { Some(BenchOp::StartSha512Hw) | Some(BenchOp::StartSha512Sw) => { let mut hw_mode = true; let mut hasher = match FromPrimitive::from_usize(msg.body.id()) { - Some(BenchOp::StartSha512Sw) => {hw_mode = false; engine_sha512::Sha512::new(Some(FallbackStrategy::SoftwareOnly))}, - _ => engine_sha512::Sha512::new(Some(FallbackStrategy::WaitForHardware)), + Some(BenchOp::StartSha512Sw) => {hw_mode = false; sha2::Sha512::new_with_strategy(FallbackStrategy::SoftwareOnly)}, + _ => sha2::Sha512::new_with_strategy(FallbackStrategy::WaitForHardware), }; let mut accumulator = [0 as u8; 64]; for i in 0..TEST_ITERS { @@ -167,7 +167,7 @@ impl<'a> ShellCmdApi<'a> for Sha { ]; let mut pass: bool = true; - let mut hasher = engine_sha512::Sha512::new(Some(FallbackStrategy::WaitForHardware)); + let mut hasher = sha2::Sha512::new_with_strategy(FallbackStrategy::WaitForHardware); hasher.update(K_DATA); let digest = hasher.finalize(); @@ -192,7 +192,7 @@ impl<'a> ShellCmdApi<'a> for Sha { ]; let mut pass: bool = true; - let mut hasher = engine_sha512::Sha512Trunc256::new(Some(FallbackStrategy::WaitForHardware)); + let mut hasher = sha2::Sha512Trunc256::new_with_strategy(FallbackStrategy::WaitForHardware); hasher.update(K_DATA); let digest = hasher.finalize(); diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 2d4e75fe7..0a10ec3cd 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -47,7 +47,7 @@ fn main() -> Result<(), Box> { "rtc", "susres", "codec", - "engine-sha512", + "sha2", "engine-25519", "spinor", "root-keys",