@@ -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,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