@@ -6,7 +6,7 @@ use crate::{
66    attributes:: ObjectAttributesBuilder , 
77    handles:: { KeyHandle ,  NvIndexTpmHandle ,  TpmHandle } , 
88    interface_types:: { 
9-         algorithm:: { AsymmetricAlgorithm ,  HashingAlgorithm ,  PublicAlgorithm } , 
9+         algorithm:: { AsymmetricAlgorithmSelection ,  HashingAlgorithm ,  PublicAlgorithm } , 
1010        ecc:: EccCurve , 
1111        key_bits:: RsaKeyBits , 
1212        resource_handles:: { Hierarchy ,  NvAuth } , 
@@ -24,12 +24,20 @@ use std::convert::TryFrom;
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+ 
2735/// Get the [`Public`] representing a default Endorsement Key 
2836/// 
2937/// Source: TCG EK Credential Profile for TPM Family 2.0; Level 0 Version 2.3 Revision 2 
3038/// Appendix B.3.3 and B.3.4 
3139pub  fn  create_ek_public_from_default_template < IKC :  IntoKeyCustomization > ( 
32-     alg :  AsymmetricAlgorithm , 
40+     alg :  AsymmetricAlgorithmSelection , 
3341    key_customization :  IKC , 
3442)  -> Result < Public >  { 
3543    let  key_customization = key_customization. into_key_customization ( ) ; 
@@ -65,7 +73,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
6573    ] ; 
6674
6775    let  key_builder = match  alg { 
68-         AsymmetricAlgorithm :: Rsa  => PublicBuilder :: new ( ) 
76+         AsymmetricAlgorithmSelection :: Rsa ( key_bits )  => PublicBuilder :: new ( ) 
6977            . with_public_algorithm ( PublicAlgorithm :: Rsa ) 
7078            . with_name_hashing_algorithm ( HashingAlgorithm :: Sha256 ) 
7179            . with_object_attributes ( obj_attrs) 
@@ -74,15 +82,15 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
7482                PublicRsaParametersBuilder :: new ( ) 
7583                    . with_symmetric ( SymmetricDefinitionObject :: AES_128_CFB ) 
7684                    . with_scheme ( RsaScheme :: Null ) 
77-                     . with_key_bits ( RsaKeyBits :: Rsa2048 ) 
85+                     . with_key_bits ( key_bits ) 
7886                    . with_exponent ( RsaExponent :: default ( ) ) 
7987                    . with_is_signing_key ( obj_attrs. sign_encrypt ( ) ) 
8088                    . with_is_decryption_key ( obj_attrs. decrypt ( ) ) 
8189                    . with_restricted ( obj_attrs. decrypt ( ) ) 
8290                    . build ( ) ?, 
8391            ) 
8492            . with_rsa_unique_identifier ( PublicKeyRsa :: new_empty_with_size ( RsaKeyBits :: Rsa2048 ) ) , 
85-         AsymmetricAlgorithm :: Ecc  => PublicBuilder :: new ( ) 
93+         AsymmetricAlgorithmSelection :: Ecc ( ecc_curve )  => PublicBuilder :: new ( ) 
8694            . with_public_algorithm ( PublicAlgorithm :: Ecc ) 
8795            . with_name_hashing_algorithm ( HashingAlgorithm :: Sha256 ) 
8896            . with_object_attributes ( obj_attrs) 
@@ -91,7 +99,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
9199                PublicEccParametersBuilder :: new ( ) 
92100                    . with_symmetric ( SymmetricDefinitionObject :: AES_128_CFB ) 
93101                    . with_ecc_scheme ( EccScheme :: Null ) 
94-                     . with_curve ( EccCurve :: NistP256 ) 
102+                     . with_curve ( ecc_curve ) 
95103                    . with_key_derivation_function_scheme ( KeyDerivationFunctionScheme :: Null ) 
96104                    . with_is_signing_key ( obj_attrs. sign_encrypt ( ) ) 
97105                    . with_is_decryption_key ( obj_attrs. decrypt ( ) ) 
@@ -102,10 +110,6 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
102110                EccParameter :: try_from ( vec ! [ 0u8 ;  32 ] ) ?, 
103111                EccParameter :: try_from ( vec ! [ 0u8 ;  32 ] ) ?, 
104112            ) ) , 
105-         AsymmetricAlgorithm :: Null  => { 
106-             // TDOD: Figure out what to with Null. 
107-             return  Err ( Error :: local_error ( WrapperErrorKind :: UnsupportedParam ) ) ; 
108-         } 
109113    } ; 
110114
111115    let  key_builder = if  let  Some ( ref  k)  = key_customization { 
@@ -119,7 +123,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
119123/// Create the Endorsement Key object from the specification templates 
120124pub  fn  create_ek_object < IKC :  IntoKeyCustomization > ( 
121125    context :  & mut  Context , 
122-     alg :  AsymmetricAlgorithm , 
126+     alg :  AsymmetricAlgorithmSelection , 
123127    key_customization :  IKC , 
124128)  -> Result < KeyHandle >  { 
125129    let  ek_public = create_ek_public_from_default_template ( alg,  key_customization) ?; 
@@ -132,14 +136,21 @@ pub fn create_ek_object<IKC: IntoKeyCustomization>(
132136} 
133137
134138/// Retrieve the Endorsement Key public certificate from the TPM 
135- pub  fn  retrieve_ek_pubcert ( context :  & mut  Context ,  alg :  AsymmetricAlgorithm )  -> Result < Vec < u8 > >  { 
139+ pub  fn  retrieve_ek_pubcert ( 
140+     context :  & mut  Context , 
141+     alg :  AsymmetricAlgorithmSelection , 
142+ )  -> Result < Vec < u8 > >  { 
136143    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 ) ) ; 
144+         AsymmetricAlgorithmSelection :: Rsa ( RsaKeyBits :: Rsa2048 )  => RSA_2048_EK_CERTIFICATE_NV_INDEX , 
145+         AsymmetricAlgorithmSelection :: Rsa ( RsaKeyBits :: Rsa3072 )  => RSA_3072_EK_CERTIFICATE_NV_INDEX , 
146+         AsymmetricAlgorithmSelection :: Rsa ( RsaKeyBits :: Rsa4096 )  => RSA_4096_EK_CERTIFICATE_NV_INDEX , 
147+         AsymmetricAlgorithmSelection :: Ecc ( EccCurve :: NistP256 )  => ECC_P256_EK_CERTIFICATE_NV_INDEX , 
148+         AsymmetricAlgorithmSelection :: Ecc ( EccCurve :: NistP384 )  => ECC_P384_EK_CERTIFICATE_NV_INDEX , 
149+         AsymmetricAlgorithmSelection :: Ecc ( EccCurve :: NistP521 )  => ECC_P521_EK_CERTIFICATE_NV_INDEX , 
150+         AsymmetricAlgorithmSelection :: Ecc ( EccCurve :: Sm2P256 )  => { 
151+             ECC_P256_SM2_EK_CERTIFICATE_NV_INDEX 
142152        } 
153+         _ => return  Err ( Error :: local_error ( WrapperErrorKind :: UnsupportedParam ) ) , 
143154    } ; 
144155
145156    let  nv_idx = NvIndexTpmHandle :: new ( nv_idx) . unwrap ( ) ; 
0 commit comments