Skip to content

Commit

Permalink
bridge digest crate with tpm digests
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Gautier <arthur.gautier@arista.com>
  • Loading branch information
baloo committed Aug 4, 2024
1 parent 05fbedf commit 1ff6a4e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions tss-esapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
33 changes: 33 additions & 0 deletions tss-esapi/src/structures/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -205,6 +210,34 @@ pub mod digest {
Digest(value_as_vec.into())
}
}

macro_rules! impl_from_digest {
($($size:ty),+) => {
$(impl From<GenericArray<u8, $size>> for Digest {
fn from(value: GenericArray<u8, $size>) -> Self {
Digest(value.as_slice().to_vec().into())
}
}

impl TryFrom<Digest> for GenericArray<u8, $size> {
type Error = Error;

fn try_from(value: Digest) -> Result<Self> {
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 {
Expand Down

0 comments on commit 1ff6a4e

Please sign in to comment.