From 4b5634f11d9dbae9b224c8e572e0aa5b3a02eb54 Mon Sep 17 00:00:00 2001 From: Spencer Solit Date: Tue, 24 Jun 2025 18:21:51 -0400 Subject: [PATCH 1/3] port HashAlgorithm enum from guest-components and trustee Consolidates code related HashAlgorithm from across different crates in guest-components and trustee and exposes it for external use. This reduces code duplication, makes behavior more consistent, and ensures external projects never need to convert between multiple HashAlgorithm types Signed-off-by: ssolit --- Cargo.toml | 2 ++ src/hash_algorithm.rs | 80 +++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 ++ 3 files changed, 85 insertions(+) create mode 100644 src/hash_algorithm.rs diff --git a/Cargo.toml b/Cargo.toml index c0a1934..d3504a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,8 @@ base64 = { version = "0.22.1", default-features = false } serde = { version = "1.0", default-features = false, features = ["derive"] } serde_json = { version = "1.0", default-features = false } sev = { version = "6.0.0", features = ["openssl"], optional = true } +sha2 = "0.10" +strum = { version = "0.27", features = ["derive"] } thiserror = { version = "2.0.3", default-features = false } [dev-dependencies] diff --git a/src/hash_algorithm.rs b/src/hash_algorithm.rs new file mode 100644 index 0000000..aa926e9 --- /dev/null +++ b/src/hash_algorithm.rs @@ -0,0 +1,80 @@ +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256, Sha384, Sha512}; +use strum::{AsRefStr, Display, EnumString}; + +/// Hash algorithms used to calculate runtime/init data binding +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, AsRefStr, Display, EnumString)] +#[serde(rename_all = "lowercase")] +pub enum HashAlgorithm { + #[strum(ascii_case_insensitive)] + #[strum(serialize = "sha256")] + Sha256, + + #[strum(ascii_case_insensitive)] + #[strum(serialize = "sha384")] + Sha384, + + #[strum(ascii_case_insensitive)] + #[strum(serialize = "sha512")] + Sha512, +} + +impl HashAlgorithm { + pub fn accumulate_hash(&self, materials: Vec) -> Vec { + match self { + HashAlgorithm::Sha256 => { + let mut hasher = Sha256::new(); + hasher.update(materials); + hasher.finalize().to_vec() + } + HashAlgorithm::Sha384 => { + let mut hasher = Sha384::new(); + hasher.update(materials); + hasher.finalize().to_vec() + } + HashAlgorithm::Sha512 => { + let mut hasher = Sha512::new(); + hasher.update(materials); + hasher.finalize().to_vec() + } + } + } +} + +impl Default for HashAlgorithm { + fn default() -> Self { + Self::Sha384 + } +} + +fn hash_reportdata(material: &[u8]) -> Vec { + D::new().chain_update(material).finalize().to_vec() +} + +impl HashAlgorithm { + /// Return the hash value length in bytes + pub fn digest_len(&self) -> usize { + match self { + HashAlgorithm::Sha256 => 32, + HashAlgorithm::Sha384 => 48, + HashAlgorithm::Sha512 => 64, + } + } + + pub fn digest(&self, material: &[u8]) -> Vec { + match self { + HashAlgorithm::Sha256 => hash_reportdata::(material), + HashAlgorithm::Sha384 => hash_reportdata::(material), + HashAlgorithm::Sha512 => hash_reportdata::(material), + } + } + + /// Return a list of all supported hash algorithms. + pub fn list_all() -> Vec { + vec![ + HashAlgorithm::Sha256, + HashAlgorithm::Sha384, + HashAlgorithm::Sha512, + ] + } +} diff --git a/src/lib.rs b/src/lib.rs index 6827882..8402b07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,9 @@ extern crate alloc; mod error; pub use error::{KbsTypesError, Result}; +mod hash_algorithm; +pub use hash_algorithm::HashAlgorithm; + #[cfg(all(feature = "alloc", not(feature = "std")))] use alloc::{string::String, vec::Vec}; use base64::{prelude::BASE64_URL_SAFE_NO_PAD, Engine}; From e6af513565355abd6ee0e2f5e779520bb25aaf89 Mon Sep 17 00:00:00 2001 From: ssolit Date: Mon, 14 Jul 2025 19:03:43 +0000 Subject: [PATCH 2/3] add alloc imports Adds relevant alloc imports, gated by the alloc feature an no std features Signed-off-by: ssolit --- src/hash_algorithm.rs | 3 +++ src/lib.rs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/hash_algorithm.rs b/src/hash_algorithm.rs index aa926e9..0d0535f 100644 --- a/src/hash_algorithm.rs +++ b/src/hash_algorithm.rs @@ -2,6 +2,9 @@ use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256, Sha384, Sha512}; use strum::{AsRefStr, Display, EnumString}; +#[cfg(all(feature = "alloc", not(feature = "std")))] +use alloc::{vec, vec::Vec}; + /// Hash algorithms used to calculate runtime/init data binding #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, AsRefStr, Display, EnumString)] #[serde(rename_all = "lowercase")] diff --git a/src/lib.rs b/src/lib.rs index 8402b07..3cea258 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -250,7 +250,7 @@ mod tests { use crate::*; #[cfg(all(feature = "alloc", not(feature = "std")))] - use alloc::{collections::btree_map::BTreeMap, string::ToString}; + use alloc::string::ToString; #[test] fn parse_request() { From bb31260cb42c29d5667d8b336b1222c784693e18 Mon Sep 17 00:00:00 2001 From: ssolit Date: Wed, 16 Jul 2025 15:38:23 +0000 Subject: [PATCH 3/3] remove accumulate_hash from HashAlgorithm removes the accumulate_hash function. Users are expected to call digest() instead. This helps unify the interface and remove duplicate code Signed-off-by: ssolit --- src/hash_algorithm.rs | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/src/hash_algorithm.rs b/src/hash_algorithm.rs index 0d0535f..dbddcba 100644 --- a/src/hash_algorithm.rs +++ b/src/hash_algorithm.rs @@ -22,28 +22,6 @@ pub enum HashAlgorithm { Sha512, } -impl HashAlgorithm { - pub fn accumulate_hash(&self, materials: Vec) -> Vec { - match self { - HashAlgorithm::Sha256 => { - let mut hasher = Sha256::new(); - hasher.update(materials); - hasher.finalize().to_vec() - } - HashAlgorithm::Sha384 => { - let mut hasher = Sha384::new(); - hasher.update(materials); - hasher.finalize().to_vec() - } - HashAlgorithm::Sha512 => { - let mut hasher = Sha512::new(); - hasher.update(materials); - hasher.finalize().to_vec() - } - } - } -} - impl Default for HashAlgorithm { fn default() -> Self { Self::Sha384