@@ -6,6 +6,12 @@ use crate::tss2_esys::{TPMT_HA, TPMU_HA};
66use crate :: { Error , Result , WrapperErrorKind } ;
77use 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 ) ]
1016pub struct HashAgile {
1117 algorithm : HashingAlgorithm ,
@@ -65,3 +71,31 @@ 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+ #[ cfg( all( feature = "abstraction" , feature = "rustcrypto" ) ) ]
82+ #[ allow(
83+ clippy:: unnecessary_fallible_conversions,
84+ reason = "GenericArray::From<&[T]> will panic if the payload is not the same length"
85+ ) ]
86+ /// Return the hash content of the [`HashAgile`]
87+ ///
88+ /// # Errors
89+ ///
90+ /// Return an error if the specified [`AssociatedHashingAlgorithm`] is not the
91+ /// one in the [`HashAgile`].
92+ pub fn to_output < H : AssociatedHashingAlgorithm + OutputSizeUser > ( & self ) -> Result < Output < H > > {
93+ if self . algorithm == H :: TPM_DIGEST {
94+ <& Output < H > >:: try_from ( self . digest . as_bytes ( ) )
95+ . map_err ( |_| Error :: local_error ( WrapperErrorKind :: WrongValueFromTpm ) )
96+ . cloned ( )
97+ } else {
98+ Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) )
99+ }
100+ }
101+ }
0 commit comments