@@ -6,8 +6,7 @@ use crate::{
66 attributes:: ObjectAttributesBuilder ,
77 handles:: { KeyHandle , NvIndexTpmHandle , TpmHandle } ,
88 interface_types:: {
9- algorithm:: { AsymmetricAlgorithm , HashingAlgorithm , PublicAlgorithm } ,
10- ecc:: EccCurve ,
9+ algorithm:: { AsymmetricAlgorithmSelection , HashingAlgorithm , PublicAlgorithm } ,
1110 key_bits:: RsaKeyBits ,
1211 resource_handles:: { Hierarchy , NvAuth } ,
1312 } ,
@@ -16,20 +15,28 @@ use crate::{
1615 PublicBuilder , PublicEccParametersBuilder , PublicKeyRsa , PublicRsaParametersBuilder ,
1716 RsaExponent , RsaScheme , SymmetricDefinitionObject ,
1817 } ,
19- Context , Error , Result , WrapperErrorKind ,
18+ Context , Result ,
2019} ;
21- use std:: convert:: TryFrom ;
20+ use std:: convert:: { TryFrom , TryInto } ;
2221// Source: TCG EK Credential Profile for TPM Family 2.0; Level 0 Version 2.3 Revision 2
2322// Section 2.2.1.4 (Low Range) for Windows compatibility
2423const RSA_2048_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c00002 ;
2524const ECC_P256_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c0000a ;
2625
26+ // Source: TCG EK Credential Profile for TPM Family 2.0; Level 0 Version 2.3 Revision 2
27+ // Section 2.2.1.5 (High Range)
28+ const ECC_P384_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c00016 ;
29+ const ECC_P521_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c00018 ;
30+ const ECC_P256_SM2_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c0001a ;
31+ const RSA_3072_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c0001c ;
32+ const RSA_4096_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c0001e ;
33+
2734/// Get the [`Public`] representing a default Endorsement Key
2835///
2936/// Source: TCG EK Credential Profile for TPM Family 2.0; Level 0 Version 2.3 Revision 2
3037/// Appendix B.3.3 and B.3.4
3138pub fn create_ek_public_from_default_template < IKC : IntoKeyCustomization > (
32- alg : AsymmetricAlgorithm ,
39+ alg : AsymmetricAlgorithmSelection ,
3340 key_customization : IKC ,
3441) -> Result < Public > {
3542 let key_customization = key_customization. into_key_customization ( ) ;
@@ -65,7 +72,9 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
6572 ] ;
6673
6774 let key_builder = match alg {
68- AsymmetricAlgorithm :: Rsa => PublicBuilder :: new ( )
75+ AsymmetricAlgorithmSelection :: Rsa2048
76+ | AsymmetricAlgorithmSelection :: Rsa3072
77+ | AsymmetricAlgorithmSelection :: Rsa4096 => PublicBuilder :: new ( )
6978 . with_public_algorithm ( PublicAlgorithm :: Rsa )
7079 . with_name_hashing_algorithm ( HashingAlgorithm :: Sha256 )
7180 . with_object_attributes ( obj_attrs)
@@ -74,15 +83,18 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
7483 PublicRsaParametersBuilder :: new ( )
7584 . with_symmetric ( SymmetricDefinitionObject :: AES_128_CFB )
7685 . with_scheme ( RsaScheme :: Null )
77- . with_key_bits ( RsaKeyBits :: Rsa2048 )
86+ . with_key_bits ( alg . try_into ( ) ? )
7887 . with_exponent ( RsaExponent :: default ( ) )
7988 . with_is_signing_key ( obj_attrs. sign_encrypt ( ) )
8089 . with_is_decryption_key ( obj_attrs. decrypt ( ) )
8190 . with_restricted ( obj_attrs. decrypt ( ) )
8291 . build ( ) ?,
8392 )
8493 . with_rsa_unique_identifier ( PublicKeyRsa :: new_empty_with_size ( RsaKeyBits :: Rsa2048 ) ) ,
85- AsymmetricAlgorithm :: Ecc => PublicBuilder :: new ( )
94+ AsymmetricAlgorithmSelection :: EccP256
95+ | AsymmetricAlgorithmSelection :: EccP384
96+ | AsymmetricAlgorithmSelection :: EccP521
97+ | AsymmetricAlgorithmSelection :: EccP256Sm2 => PublicBuilder :: new ( )
8698 . with_public_algorithm ( PublicAlgorithm :: Ecc )
8799 . with_name_hashing_algorithm ( HashingAlgorithm :: Sha256 )
88100 . with_object_attributes ( obj_attrs)
@@ -91,7 +103,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
91103 PublicEccParametersBuilder :: new ( )
92104 . with_symmetric ( SymmetricDefinitionObject :: AES_128_CFB )
93105 . with_ecc_scheme ( EccScheme :: Null )
94- . with_curve ( EccCurve :: NistP256 )
106+ . with_curve ( alg . try_into ( ) ? )
95107 . with_key_derivation_function_scheme ( KeyDerivationFunctionScheme :: Null )
96108 . with_is_signing_key ( obj_attrs. sign_encrypt ( ) )
97109 . with_is_decryption_key ( obj_attrs. decrypt ( ) )
@@ -102,10 +114,6 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
102114 EccParameter :: try_from ( vec ! [ 0u8 ; 32 ] ) ?,
103115 EccParameter :: try_from ( vec ! [ 0u8 ; 32 ] ) ?,
104116 ) ) ,
105- AsymmetricAlgorithm :: Null => {
106- // TDOD: Figure out what to with Null.
107- return Err ( Error :: local_error ( WrapperErrorKind :: UnsupportedParam ) ) ;
108- }
109117 } ;
110118
111119 let key_builder = if let Some ( ref k) = key_customization {
@@ -119,7 +127,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
119127/// Create the Endorsement Key object from the specification templates
120128pub fn create_ek_object < IKC : IntoKeyCustomization > (
121129 context : & mut Context ,
122- alg : AsymmetricAlgorithm ,
130+ alg : AsymmetricAlgorithmSelection ,
123131 key_customization : IKC ,
124132) -> Result < KeyHandle > {
125133 let ek_public = create_ek_public_from_default_template ( alg, key_customization) ?;
@@ -132,14 +140,18 @@ pub fn create_ek_object<IKC: IntoKeyCustomization>(
132140}
133141
134142/// Retrieve the Endorsement Key public certificate from the TPM
135- pub fn retrieve_ek_pubcert ( context : & mut Context , alg : AsymmetricAlgorithm ) -> Result < Vec < u8 > > {
143+ pub fn retrieve_ek_pubcert (
144+ context : & mut Context ,
145+ alg : AsymmetricAlgorithmSelection ,
146+ ) -> Result < Vec < u8 > > {
136147 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- }
148+ AsymmetricAlgorithmSelection :: Rsa2048 => RSA_2048_EK_CERTIFICATE_NV_INDEX ,
149+ AsymmetricAlgorithmSelection :: Rsa3072 => RSA_3072_EK_CERTIFICATE_NV_INDEX ,
150+ AsymmetricAlgorithmSelection :: Rsa4096 => RSA_4096_EK_CERTIFICATE_NV_INDEX ,
151+ AsymmetricAlgorithmSelection :: EccP256 => ECC_P256_EK_CERTIFICATE_NV_INDEX ,
152+ AsymmetricAlgorithmSelection :: EccP384 => ECC_P384_EK_CERTIFICATE_NV_INDEX ,
153+ AsymmetricAlgorithmSelection :: EccP521 => ECC_P521_EK_CERTIFICATE_NV_INDEX ,
154+ AsymmetricAlgorithmSelection :: EccP256Sm2 => ECC_P256_SM2_EK_CERTIFICATE_NV_INDEX ,
143155 } ;
144156
145157 let nv_idx = NvIndexTpmHandle :: new ( nv_idx) . unwrap ( ) ;
0 commit comments