diff --git a/tss-esapi/Cargo.toml b/tss-esapi/Cargo.toml index ee1a4b3e..7b76fdad 100644 --- a/tss-esapi/Cargo.toml +++ b/tss-esapi/Cargo.toml @@ -36,6 +36,7 @@ p384 = { version = "0.13.0", optional = true } p521 = { version = "0.13.3", optional = true } sm2 = { version = "0.13.3", optional = true } rsa = { version = "0.9", optional = true } +digest = "0.10.7" cfg-if = "1.0.0" strum = { version = "0.25.0", optional = true } strum_macros = { version = "0.25.0", optional = true } diff --git a/tss-esapi/src/structures/buffers.rs b/tss-esapi/src/structures/buffers.rs index 534f3c3f..1c559e44 100644 --- a/tss-esapi/src/structures/buffers.rs +++ b/tss-esapi/src/structures/buffers.rs @@ -111,6 +111,11 @@ pub mod data { } pub mod digest { + use digest::{ + consts::{U20, U32, U48, U64}, + generic_array::GenericArray, + typenum::Unsigned, + }; buffer_type!(Digest, 64, TPM2B_DIGEST); // Some implementations to get from Digest to [u8; N] for common values of N (sha* primarily) @@ -205,6 +210,34 @@ pub mod digest { Digest(value_as_vec.into()) } } + + macro_rules! impl_from_digest { + ($($size:ty),+) => { + $(impl From> for Digest { + fn from(value: GenericArray) -> Self { + Digest(value.as_slice().to_vec().into()) + } + } + + impl TryFrom for GenericArray { + type Error = Error; + + fn try_from(value: Digest) -> Result { + if value.len() != <$size>::USIZE { + return Err(Error::local_error(WrapperErrorKind::WrongParamSize)); + } + + let mut result = [0; <$size>::USIZE]; + + result.copy_from_slice(value.as_bytes()); + + Ok(result.into()) + } + })+ + } + } + + impl_from_digest!(U20, U32, U48, U64); } pub mod ecc_parameter {