refactor engine-sha512 to be named sha2

this makes patch.crates-io much easier to debug, we now
have the hardware hasher working in all the places i expect it to.
This commit is contained in:
bunnie 2021-08-09 04:40:33 +08:00
parent ec6a3fd1a3
commit 04dc6a4c2b
11 changed files with 63 additions and 79 deletions

47
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -1,6 +1,6 @@
[package]
name = "engine-sha512"
version = "0.1.0"
name = "sha2"
version = "0.9.5"
authors = ["bunnie <bunnie@kosagi.com>"]
edition = "2018"
description = "Sha512 hardware accelerator engine"

View file

@ -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::<Sha2Finalize, _>().unwrap();

View file

@ -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<FallbackStrategy>) -> 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<FallbackStrategy>) -> 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)
}
}

View file

@ -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"}

View file

@ -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::<SignatureInFlash>()]);
hasher.update(&gateware_region[CSR_CSV_OFFSET..]);
log::info!("hash done");

View file

@ -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

View file

@ -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 {

View file

@ -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();

View file

@ -47,7 +47,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"rtc",
"susres",
"codec",
"engine-sha512",
"sha2",
"engine-25519",
"spinor",
"root-keys",