Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
61 changes: 61 additions & 0 deletions src/hash_algorithm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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")]
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 Default for HashAlgorithm {
fn default() -> Self {
Self::Sha384
}
}

fn hash_reportdata<D: Digest>(material: &[u8]) -> Vec<u8> {
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<u8> {
match self {
HashAlgorithm::Sha256 => hash_reportdata::<Sha256>(material),
HashAlgorithm::Sha384 => hash_reportdata::<Sha384>(material),
HashAlgorithm::Sha512 => hash_reportdata::<Sha512>(material),
}
}

/// Return a list of all supported hash algorithms.
pub fn list_all() -> Vec<Self> {
vec![
HashAlgorithm::Sha256,
HashAlgorithm::Sha384,
HashAlgorithm::Sha512,
]
}
}
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -247,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() {
Expand Down
Loading