2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
use crate :: {
5
- abstraction:: { nv, IntoKeyCustomization , KeyCustomization } ,
5
+ abstraction:: { nv, AsymmetricAlgorithmSelection , IntoKeyCustomization , KeyCustomization } ,
6
6
attributes:: ObjectAttributesBuilder ,
7
7
handles:: { KeyHandle , NvIndexTpmHandle , TpmHandle } ,
8
8
interface_types:: {
9
- algorithm:: { AsymmetricAlgorithm , HashingAlgorithm , PublicAlgorithm } ,
9
+ algorithm:: { HashingAlgorithm , PublicAlgorithm } ,
10
10
ecc:: EccCurve ,
11
11
key_bits:: RsaKeyBits ,
12
12
resource_handles:: { Hierarchy , NvAuth } ,
@@ -24,12 +24,20 @@ use std::convert::TryFrom;
24
24
const RSA_2048_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c00002 ;
25
25
const ECC_P256_EK_CERTIFICATE_NV_INDEX : u32 = 0x01c0000a ;
26
26
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
+
27
35
/// Get the [`Public`] representing a default Endorsement Key
28
36
///
29
37
/// Source: TCG EK Credential Profile for TPM Family 2.0; Level 0 Version 2.3 Revision 2
30
38
/// Appendix B.3.3 and B.3.4
31
39
pub fn create_ek_public_from_default_template < IKC : IntoKeyCustomization > (
32
- alg : AsymmetricAlgorithm ,
40
+ alg : AsymmetricAlgorithmSelection ,
33
41
key_customization : IKC ,
34
42
) -> Result < Public > {
35
43
let key_customization = key_customization. into_key_customization ( ) ;
@@ -65,7 +73,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
65
73
] ;
66
74
67
75
let key_builder = match alg {
68
- AsymmetricAlgorithm :: Rsa => PublicBuilder :: new ( )
76
+ AsymmetricAlgorithmSelection :: Rsa ( key_bits ) => PublicBuilder :: new ( )
69
77
. with_public_algorithm ( PublicAlgorithm :: Rsa )
70
78
. with_name_hashing_algorithm ( HashingAlgorithm :: Sha256 )
71
79
. with_object_attributes ( obj_attrs)
@@ -74,15 +82,15 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
74
82
PublicRsaParametersBuilder :: new ( )
75
83
. with_symmetric ( SymmetricDefinitionObject :: AES_128_CFB )
76
84
. with_scheme ( RsaScheme :: Null )
77
- . with_key_bits ( RsaKeyBits :: Rsa2048 )
85
+ . with_key_bits ( key_bits )
78
86
. with_exponent ( RsaExponent :: default ( ) )
79
87
. with_is_signing_key ( obj_attrs. sign_encrypt ( ) )
80
88
. with_is_decryption_key ( obj_attrs. decrypt ( ) )
81
89
. with_restricted ( obj_attrs. decrypt ( ) )
82
90
. build ( ) ?,
83
91
)
84
92
. with_rsa_unique_identifier ( PublicKeyRsa :: new_empty_with_size ( RsaKeyBits :: Rsa2048 ) ) ,
85
- AsymmetricAlgorithm :: Ecc => PublicBuilder :: new ( )
93
+ AsymmetricAlgorithmSelection :: Ecc ( ecc_curve ) => PublicBuilder :: new ( )
86
94
. with_public_algorithm ( PublicAlgorithm :: Ecc )
87
95
. with_name_hashing_algorithm ( HashingAlgorithm :: Sha256 )
88
96
. with_object_attributes ( obj_attrs)
@@ -91,7 +99,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
91
99
PublicEccParametersBuilder :: new ( )
92
100
. with_symmetric ( SymmetricDefinitionObject :: AES_128_CFB )
93
101
. with_ecc_scheme ( EccScheme :: Null )
94
- . with_curve ( EccCurve :: NistP256 )
102
+ . with_curve ( ecc_curve )
95
103
. with_key_derivation_function_scheme ( KeyDerivationFunctionScheme :: Null )
96
104
. with_is_signing_key ( obj_attrs. sign_encrypt ( ) )
97
105
. with_is_decryption_key ( obj_attrs. decrypt ( ) )
@@ -102,10 +110,6 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
102
110
EccParameter :: try_from ( vec ! [ 0u8 ; 32 ] ) ?,
103
111
EccParameter :: try_from ( vec ! [ 0u8 ; 32 ] ) ?,
104
112
) ) ,
105
- AsymmetricAlgorithm :: Null => {
106
- // TDOD: Figure out what to with Null.
107
- return Err ( Error :: local_error ( WrapperErrorKind :: UnsupportedParam ) ) ;
108
- }
109
113
} ;
110
114
111
115
let key_builder = if let Some ( ref k) = key_customization {
@@ -119,7 +123,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
119
123
/// Create the Endorsement Key object from the specification templates
120
124
pub fn create_ek_object < IKC : IntoKeyCustomization > (
121
125
context : & mut Context ,
122
- alg : AsymmetricAlgorithm ,
126
+ alg : AsymmetricAlgorithmSelection ,
123
127
key_customization : IKC ,
124
128
) -> Result < KeyHandle > {
125
129
let ek_public = create_ek_public_from_default_template ( alg, key_customization) ?;
@@ -132,14 +136,21 @@ pub fn create_ek_object<IKC: IntoKeyCustomization>(
132
136
}
133
137
134
138
/// 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 > > {
136
143
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
142
152
}
153
+ _ => return Err ( Error :: local_error ( WrapperErrorKind :: UnsupportedParam ) ) ,
143
154
} ;
144
155
145
156
let nv_idx = NvIndexTpmHandle :: new ( nv_idx) . unwrap ( ) ;
0 commit comments