tss_esapi/abstraction/
hashing.rs1use crate::interface_types::algorithm::HashingAlgorithm;
5
6#[cfg(feature = "rustcrypto")]
7use {
8 crate::{Error, Result, WrapperErrorKind, structures::HashAgile},
9 digest::{Output, OutputSizeUser},
10};
11
12pub trait AssociatedHashingAlgorithm {
14 const TPM_DIGEST: HashingAlgorithm;
16}
17
18#[cfg(feature = "sha1")]
19impl AssociatedHashingAlgorithm for sha1::Sha1 {
20 const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha1;
21}
22
23#[cfg(feature = "sha2")]
24impl AssociatedHashingAlgorithm for sha2::Sha256 {
25 const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha256;
26}
27
28#[cfg(feature = "sha2")]
29impl AssociatedHashingAlgorithm for sha2::Sha384 {
30 const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha384;
31}
32
33#[cfg(feature = "sha2")]
34impl AssociatedHashingAlgorithm for sha2::Sha512 {
35 const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha512;
36}
37
38#[cfg(feature = "sm3")]
39impl AssociatedHashingAlgorithm for sm3::Sm3 {
40 const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sm3_256;
41}
42
43#[cfg(feature = "sha3")]
44impl AssociatedHashingAlgorithm for sha3::Sha3_256 {
45 const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha3_256;
46}
47
48#[cfg(feature = "sha3")]
49impl AssociatedHashingAlgorithm for sha3::Sha3_384 {
50 const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha3_384;
51}
52
53#[cfg(feature = "sha3")]
54impl AssociatedHashingAlgorithm for sha3::Sha3_512 {
55 const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha3_512;
56}
57
58impl HashAgile {
59 #[cfg(feature = "rustcrypto")]
60 #[expect(
61 clippy::unnecessary_fallible_conversions,
62 reason = "GenericArray::From<&[T]> will panic if the payload is not the same length"
63 )]
64 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}