Skip to content
Open
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
29 changes: 29 additions & 0 deletions tss-esapi/src/abstraction/hashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

use crate::interface_types::algorithm::HashingAlgorithm;

#[cfg(feature = "rustcrypto")]
use {
crate::{Error, Result, WrapperErrorKind, structures::HashAgile},
digest::{Output, OutputSizeUser},
};

/// Provides the value of the digest used in this crate for the digest.
pub trait AssociatedHashingAlgorithm {
/// Value of the digest when interacting with the TPM.
Expand Down Expand Up @@ -48,3 +54,26 @@ impl AssociatedHashingAlgorithm for sha3::Sha3_384 {
impl AssociatedHashingAlgorithm for sha3::Sha3_512 {
const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha3_512;
}

impl HashAgile {
#[cfg(feature = "rustcrypto")]
#[allow(
clippy::unnecessary_fallible_conversions,
reason = "GenericArray::From<&[T]> will panic if the payload is not the same length"
Comment thread
Superhepper marked this conversation as resolved.
)]
/// Return the hash content of the [`HashAgile`]
///
/// # Errors
///
/// Return an error if the specified [`AssociatedHashingAlgorithm`] is not the
/// one in the [`HashAgile`].
pub fn to_output<H: AssociatedHashingAlgorithm + OutputSizeUser>(&self) -> Result<Output<H>> {
if self.algorithm == H::TPM_DIGEST {
<&Output<H>>::try_from(self.digest.as_bytes())
.map_err(|_| Error::local_error(WrapperErrorKind::WrongValueFromTpm))
.cloned()
} else {
Err(Error::local_error(WrapperErrorKind::InvalidParam))
}
}
}
11 changes: 9 additions & 2 deletions tss-esapi/src/structures/hash/agile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::convert::{TryFrom, TryInto};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HashAgile {
algorithm: HashingAlgorithm,
digest: Digest,
pub(crate) algorithm: HashingAlgorithm,
pub(crate) digest: Digest,
}

impl HashAgile {
Expand Down Expand Up @@ -65,3 +65,10 @@ impl TryFrom<TPMT_HA> for HashAgile {
})
}
}

impl HashAgile {
/// Returns the hashing algorithm for this [`HashAgile`].
pub const fn hashing_algorithm(&self) -> HashingAlgorithm {
self.algorithm
}
}