@@ -18,18 +18,79 @@ use crate::{
1818 } ,
1919 Context , Error , Result , WrapperErrorKind ,
2020} ;
21- use std:: convert:: TryFrom ;
21+ use std:: convert:: { TryFrom , TryInto } ;
2222// Source: TCG EK Credential Profile for TPM Family 2.0; Level 0 Version 2.3 Revision 2
2323// Section 2.2.1.4 (Low Range) for Windows compatibility
2424const RSA_2048_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c00002 ;
2525const ECC_P256_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c0000a ;
2626
27+ // Source: TCG EK Credential Profile for TPM Family 2.0; Level 0 Version 2.3 Revision 2
28+ // Section 2.2.1.5 (High Range)
29+ const ECC_P384_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c00016 ;
30+ const ECC_P521_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c00018 ;
31+ const ECC_P256_SM2_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c0001a ;
32+ const RSA_3072_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c0001c ;
33+ const RSA_4096_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c0001e ;
34+
35+ /// Enum representing the asymmetric algorithm interface type with specific properties.
36+ #[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash ) ]
37+ pub enum AsymmetricAlgorithmSelection {
38+ Rsa2048 ,
39+ Rsa3072 ,
40+ Rsa4096 ,
41+ EccP256 ,
42+ EccP384 ,
43+ EccP521 ,
44+ EccP256Sm2 ,
45+ }
46+
47+ impl TryFrom < AsymmetricAlgorithm > for AsymmetricAlgorithmSelection {
48+ type Error = Error ;
49+
50+ fn try_from ( value : AsymmetricAlgorithm ) -> std:: result:: Result < Self , Self :: Error > {
51+ match value {
52+ AsymmetricAlgorithm :: Rsa => Ok ( AsymmetricAlgorithmSelection :: Rsa2048 ) ,
53+ AsymmetricAlgorithm :: Ecc => Ok ( AsymmetricAlgorithmSelection :: EccP256 ) ,
54+ AsymmetricAlgorithm :: Null => {
55+ Err ( Error :: local_error ( WrapperErrorKind :: UnsupportedParam ) )
56+ }
57+ }
58+ }
59+ }
60+
61+ impl TryFrom < AsymmetricAlgorithmSelection > for RsaKeyBits {
62+ type Error = Error ;
63+
64+ fn try_from ( value : AsymmetricAlgorithmSelection ) -> std:: result:: Result < Self , Self :: Error > {
65+ match value {
66+ AsymmetricAlgorithmSelection :: Rsa2048 => Ok ( RsaKeyBits :: Rsa2048 ) ,
67+ AsymmetricAlgorithmSelection :: Rsa3072 => Ok ( RsaKeyBits :: Rsa3072 ) ,
68+ AsymmetricAlgorithmSelection :: Rsa4096 => Ok ( RsaKeyBits :: Rsa4096 ) ,
69+ _ => Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ,
70+ }
71+ }
72+ }
73+
74+ impl TryFrom < AsymmetricAlgorithmSelection > for EccCurve {
75+ type Error = Error ;
76+
77+ fn try_from ( value : AsymmetricAlgorithmSelection ) -> std:: result:: Result < Self , Self :: Error > {
78+ match value {
79+ AsymmetricAlgorithmSelection :: EccP256 => Ok ( EccCurve :: NistP256 ) ,
80+ AsymmetricAlgorithmSelection :: EccP384 => Ok ( EccCurve :: NistP384 ) ,
81+ AsymmetricAlgorithmSelection :: EccP521 => Ok ( EccCurve :: NistP521 ) ,
82+ AsymmetricAlgorithmSelection :: EccP256Sm2 => Ok ( EccCurve :: Sm2P256 ) ,
83+ _ => Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ,
84+ }
85+ }
86+ }
87+
2788/// Get the [`Public`] representing a default Endorsement Key
2889///
2990/// Source: TCG EK Credential Profile for TPM Family 2.0; Level 0 Version 2.3 Revision 2
3091/// Appendix B.3.3 and B.3.4
3192pub fn create_ek_public_from_default_template < IKC : IntoKeyCustomization > (
32- alg : AsymmetricAlgorithm ,
93+ alg : AsymmetricAlgorithmSelection ,
3394 key_customization : IKC ,
3495) -> Result < Public > {
3596 let key_customization = key_customization. into_key_customization ( ) ;
@@ -65,7 +126,9 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
65126 ] ;
66127
67128 let key_builder = match alg {
68- AsymmetricAlgorithm :: Rsa => PublicBuilder :: new ( )
129+ AsymmetricAlgorithmSelection :: Rsa2048
130+ | AsymmetricAlgorithmSelection :: Rsa3072
131+ | AsymmetricAlgorithmSelection :: Rsa4096 => PublicBuilder :: new ( )
69132 . with_public_algorithm ( PublicAlgorithm :: Rsa )
70133 . with_name_hashing_algorithm ( HashingAlgorithm :: Sha256 )
71134 . with_object_attributes ( obj_attrs)
@@ -74,15 +137,18 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
74137 PublicRsaParametersBuilder :: new ( )
75138 . with_symmetric ( SymmetricDefinitionObject :: AES_128_CFB )
76139 . with_scheme ( RsaScheme :: Null )
77- . with_key_bits ( RsaKeyBits :: Rsa2048 )
140+ . with_key_bits ( alg . try_into ( ) ? )
78141 . with_exponent ( RsaExponent :: default ( ) )
79142 . with_is_signing_key ( obj_attrs. sign_encrypt ( ) )
80143 . with_is_decryption_key ( obj_attrs. decrypt ( ) )
81144 . with_restricted ( obj_attrs. decrypt ( ) )
82145 . build ( ) ?,
83146 )
84147 . with_rsa_unique_identifier ( PublicKeyRsa :: new_empty_with_size ( RsaKeyBits :: Rsa2048 ) ) ,
85- AsymmetricAlgorithm :: Ecc => PublicBuilder :: new ( )
148+ AsymmetricAlgorithmSelection :: EccP256
149+ | AsymmetricAlgorithmSelection :: EccP384
150+ | AsymmetricAlgorithmSelection :: EccP521
151+ | AsymmetricAlgorithmSelection :: EccP256Sm2 => PublicBuilder :: new ( )
86152 . with_public_algorithm ( PublicAlgorithm :: Ecc )
87153 . with_name_hashing_algorithm ( HashingAlgorithm :: Sha256 )
88154 . with_object_attributes ( obj_attrs)
@@ -91,7 +157,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
91157 PublicEccParametersBuilder :: new ( )
92158 . with_symmetric ( SymmetricDefinitionObject :: AES_128_CFB )
93159 . with_ecc_scheme ( EccScheme :: Null )
94- . with_curve ( EccCurve :: NistP256 )
160+ . with_curve ( alg . try_into ( ) ? )
95161 . with_key_derivation_function_scheme ( KeyDerivationFunctionScheme :: Null )
96162 . with_is_signing_key ( obj_attrs. sign_encrypt ( ) )
97163 . with_is_decryption_key ( obj_attrs. decrypt ( ) )
@@ -102,10 +168,6 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
102168 EccParameter :: try_from ( vec ! [ 0u8 ; 32 ] ) ?,
103169 EccParameter :: try_from ( vec ! [ 0u8 ; 32 ] ) ?,
104170 ) ) ,
105- AsymmetricAlgorithm :: Null => {
106- // TDOD: Figure out what to with Null.
107- return Err ( Error :: local_error ( WrapperErrorKind :: UnsupportedParam ) ) ;
108- }
109171 } ;
110172
111173 let key_builder = if let Some ( ref k) = key_customization {
@@ -119,7 +181,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
119181/// Create the Endorsement Key object from the specification templates
120182pub fn create_ek_object < IKC : IntoKeyCustomization > (
121183 context : & mut Context ,
122- alg : AsymmetricAlgorithm ,
184+ alg : AsymmetricAlgorithmSelection ,
123185 key_customization : IKC ,
124186) -> Result < KeyHandle > {
125187 let ek_public = create_ek_public_from_default_template ( alg, key_customization) ?;
@@ -132,14 +194,18 @@ pub fn create_ek_object<IKC: IntoKeyCustomization>(
132194}
133195
134196/// Retrieve the Endorsement Key public certificate from the TPM
135- pub fn retrieve_ek_pubcert ( context : & mut Context , alg : AsymmetricAlgorithm ) -> Result < Vec < u8 > > {
197+ pub fn retrieve_ek_pubcert (
198+ context : & mut Context ,
199+ alg : AsymmetricAlgorithmSelection ,
200+ ) -> Result < Vec < u8 > > {
136201 let nv_idx = match alg {
137- AsymmetricAlgorithm :: Rsa => RSA_2048_EK_CERTIFICATE_NV_INDEX ,
138- AsymmetricAlgorithm :: Ecc => ECC_P256_EK_CERTIFICATE_NV_INDEX ,
139- AsymmetricAlgorithm :: Null => {
140- // TDOD: Figure out what to with Null.
141- return Err ( Error :: local_error ( WrapperErrorKind :: UnsupportedParam ) ) ;
142- }
202+ AsymmetricAlgorithmSelection :: Rsa2048 => RSA_2048_EK_CERTIFICATE_NV_INDEX ,
203+ AsymmetricAlgorithmSelection :: Rsa3072 => RSA_3072_EK_CERTIFICATE_NV_INDEX ,
204+ AsymmetricAlgorithmSelection :: Rsa4096 => RSA_4096_EK_CERTIFICATE_NV_INDEX ,
205+ AsymmetricAlgorithmSelection :: EccP256 => ECC_P256_EK_CERTIFICATE_NV_INDEX ,
206+ AsymmetricAlgorithmSelection :: EccP384 => ECC_P384_EK_CERTIFICATE_NV_INDEX ,
207+ AsymmetricAlgorithmSelection :: EccP521 => ECC_P521_EK_CERTIFICATE_NV_INDEX ,
208+ AsymmetricAlgorithmSelection :: EccP256Sm2 => ECC_P256_SM2_EK_CERTIFICATE_NV_INDEX ,
143209 } ;
144210
145211 let nv_idx = NvIndexTpmHandle :: new ( nv_idx) . unwrap ( ) ;
0 commit comments