Skip to content

Commit d14caf0

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 d14caf0

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ use crate::tss2_esys::{TPMT_HA, TPMU_HA};
66
use crate::{Error, Result, WrapperErrorKind};
77
use std::convert::{TryFrom, TryInto};
88

9+
#[cfg(all(feature = "abstraction", feature = "rustcrypto"))]
10+
use {
11+
crate::abstraction::AssociatedHashingAlgorithm,
12+
digest::{Output, OutputSizeUser},
13+
};
14+
915
#[derive(Debug, Clone, PartialEq, Eq)]
1016
pub struct HashAgile {
1117
algorithm: HashingAlgorithm,
@@ -65,3 +71,29 @@ impl TryFrom<TPMT_HA> for HashAgile {
6571
})
6672
}
6773
}
74+
75+
impl HashAgile {
76+
/// Return the [`HashingAlgorithm`] of the [`HashAgile`]
77+
pub fn hashing_algorithm(&self) -> HashingAlgorithm {
78+
self.algorithm
79+
}
80+
}
81+
82+
#[cfg(all(feature = "abstraction", feature = "rustcrypto"))]
83+
impl HashAgile {
84+
/// Return the hash content of the [`HashAgile`]
85+
///
86+
/// # Errors
87+
///
88+
/// Return an error if the specified [`AssociatedHashingAlgorithm`] is not the
89+
/// one in the [`HashAgile`].
90+
pub fn to_output<H: AssociatedHashingAlgorithm + OutputSizeUser>(&self) -> Result<Output<H>> {
91+
if self.algorithm == H::TPM_DIGEST {
92+
<&Output<H>>::try_from(self.digest.as_bytes())
93+
.map_err(|_| Error::local_error(WrapperErrorKind::WrongValueFromTpm))
94+
.cloned()
95+
} else {
96+
Err(Error::local_error(WrapperErrorKind::InvalidParam))
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)