Skip to content

Commit 455334f

Browse files
committed
HashAgile: expose the algorithm and the content
This allows to verify an hmac signature emitted by a TPM. Signed-off-by: Arthur Gautier <baloo@superbaloo.net>
1 parent f2af12f commit 455334f

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

tss-esapi/src/abstraction/hashing.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
use crate::interface_types::algorithm::HashingAlgorithm;
55

6+
#[cfg(feature = "rustcrypto")]
7+
use {
8+
crate::{Error, Result, WrapperErrorKind, structures::HashAgile},
9+
digest::{Output, OutputSizeUser},
10+
};
11+
612
/// Provides the value of the digest used in this crate for the digest.
713
pub trait AssociatedHashingAlgorithm {
814
/// Value of the digest when interacting with the TPM.
@@ -48,3 +54,26 @@ impl AssociatedHashingAlgorithm for sha3::Sha3_384 {
4854
impl AssociatedHashingAlgorithm for sha3::Sha3_512 {
4955
const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha3_512;
5056
}
57+
58+
impl HashAgile {
59+
#[cfg(feature = "rustcrypto")]
60+
#[allow(
61+
clippy::unnecessary_fallible_conversions,
62+
reason = "GenericArray::From<&[T]> will panic if the payload is not the same length"
63+
)]
64+
/// Return the hash content of the [`HashAgile`]
65+
///
66+
/// # Errors
67+
///
68+
/// Return an error if the specified [`AssociatedHashingAlgorithm`] is not the
69+
/// one in the [`HashAgile`].
70+
pub fn to_output<H: AssociatedHashingAlgorithm + OutputSizeUser>(&self) -> Result<Output<H>> {
71+
if self.algorithm == H::TPM_DIGEST {
72+
<&Output<H>>::try_from(self.digest.as_bytes())
73+
.map_err(|_| Error::local_error(WrapperErrorKind::WrongValueFromTpm))
74+
.cloned()
75+
} else {
76+
Err(Error::local_error(WrapperErrorKind::InvalidParam))
77+
}
78+
}
79+
}

tss-esapi/src/structures/hash/agile.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::convert::{TryFrom, TryInto};
88

99
#[derive(Debug, Clone, PartialEq, Eq)]
1010
pub struct HashAgile {
11-
algorithm: HashingAlgorithm,
12-
digest: Digest,
11+
pub(crate) algorithm: HashingAlgorithm,
12+
pub(crate) digest: Digest,
1313
}
1414

1515
impl HashAgile {
@@ -65,3 +65,10 @@ impl TryFrom<TPMT_HA> for HashAgile {
6565
})
6666
}
6767
}
68+
69+
impl HashAgile {
70+
/// Return the [`HashingAlgorithm`] of the [`HashAgile`]
71+
pub fn hashing_algorithm(&self) -> HashingAlgorithm {
72+
self.algorithm
73+
}
74+
}

0 commit comments

Comments
 (0)