Skip to content

Commit d909944

Browse files
committed
ek abstractions: allow to specficy exact key type
This adds support other keys than RSA2048 and ECC Nist P256. Signed-off-by: Thore Sommer <[email protected]>
1 parent 1e04007 commit d909944

File tree

2 files changed

+87
-17
lines changed

2 files changed

+87
-17
lines changed

tss-esapi/src/abstraction/ek.rs

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,76 @@ 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
2424
const RSA_2048_EK_CERTIFICATE_NV_INDEX: u32 = 0x01c00002;
2525
const 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+
Null,
46+
}
47+
48+
impl From<AsymmetricAlgorithm> for AsymmetricAlgorithmSelection {
49+
fn from(value: AsymmetricAlgorithm) -> Self {
50+
match value {
51+
AsymmetricAlgorithm::Rsa => AsymmetricAlgorithmSelection::Rsa2048,
52+
AsymmetricAlgorithm::Ecc => AsymmetricAlgorithmSelection::EccP256,
53+
AsymmetricAlgorithm::Null => AsymmetricAlgorithmSelection::Null,
54+
}
55+
}
56+
}
57+
58+
impl TryFrom<AsymmetricAlgorithmSelection> for RsaKeyBits {
59+
type Error = Error;
60+
61+
fn try_from(value: AsymmetricAlgorithmSelection) -> std::result::Result<Self, Self::Error> {
62+
match value {
63+
AsymmetricAlgorithmSelection::Rsa2048 => Ok(RsaKeyBits::Rsa2048),
64+
AsymmetricAlgorithmSelection::Rsa3072 => Ok(RsaKeyBits::Rsa3072),
65+
AsymmetricAlgorithmSelection::Rsa4096 => Ok(RsaKeyBits::Rsa4096),
66+
_ => Err(Error::local_error(WrapperErrorKind::InvalidParam)),
67+
}
68+
}
69+
}
70+
71+
impl TryFrom<AsymmetricAlgorithmSelection> for EccCurve {
72+
type Error = Error;
73+
74+
fn try_from(value: AsymmetricAlgorithmSelection) -> std::result::Result<Self, Self::Error> {
75+
match value {
76+
AsymmetricAlgorithmSelection::EccP256 => Ok(EccCurve::NistP256),
77+
AsymmetricAlgorithmSelection::EccP384 => Ok(EccCurve::NistP384),
78+
AsymmetricAlgorithmSelection::EccP521 => Ok(EccCurve::NistP521),
79+
AsymmetricAlgorithmSelection::EccP256Sm2 => Ok(EccCurve::Sm2P256),
80+
_ => Err(Error::local_error(WrapperErrorKind::InvalidParam)),
81+
}
82+
}
83+
}
84+
2785
/// Get the [`Public`] representing a default Endorsement Key
2886
///
2987
/// Source: TCG EK Credential Profile for TPM Family 2.0; Level 0 Version 2.3 Revision 2
3088
/// Appendix B.3.3 and B.3.4
3189
pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
32-
alg: AsymmetricAlgorithm,
90+
alg: AsymmetricAlgorithmSelection,
3391
key_customization: IKC,
3492
) -> Result<Public> {
3593
let key_customization = key_customization.into_key_customization();
@@ -65,7 +123,9 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
65123
];
66124

67125
let key_builder = match alg {
68-
AsymmetricAlgorithm::Rsa => PublicBuilder::new()
126+
AsymmetricAlgorithmSelection::Rsa2048
127+
| AsymmetricAlgorithmSelection::Rsa3072
128+
| AsymmetricAlgorithmSelection::Rsa4096 => PublicBuilder::new()
69129
.with_public_algorithm(PublicAlgorithm::Rsa)
70130
.with_name_hashing_algorithm(HashingAlgorithm::Sha256)
71131
.with_object_attributes(obj_attrs)
@@ -74,15 +134,18 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
74134
PublicRsaParametersBuilder::new()
75135
.with_symmetric(SymmetricDefinitionObject::AES_128_CFB)
76136
.with_scheme(RsaScheme::Null)
77-
.with_key_bits(RsaKeyBits::Rsa2048)
137+
.with_key_bits(alg.try_into()?)
78138
.with_exponent(RsaExponent::default())
79139
.with_is_signing_key(obj_attrs.sign_encrypt())
80140
.with_is_decryption_key(obj_attrs.decrypt())
81141
.with_restricted(obj_attrs.decrypt())
82142
.build()?,
83143
)
84144
.with_rsa_unique_identifier(PublicKeyRsa::new_empty_with_size(RsaKeyBits::Rsa2048)),
85-
AsymmetricAlgorithm::Ecc => PublicBuilder::new()
145+
AsymmetricAlgorithmSelection::EccP256
146+
| AsymmetricAlgorithmSelection::EccP384
147+
| AsymmetricAlgorithmSelection::EccP521
148+
| AsymmetricAlgorithmSelection::EccP256Sm2 => PublicBuilder::new()
86149
.with_public_algorithm(PublicAlgorithm::Ecc)
87150
.with_name_hashing_algorithm(HashingAlgorithm::Sha256)
88151
.with_object_attributes(obj_attrs)
@@ -91,7 +154,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
91154
PublicEccParametersBuilder::new()
92155
.with_symmetric(SymmetricDefinitionObject::AES_128_CFB)
93156
.with_ecc_scheme(EccScheme::Null)
94-
.with_curve(EccCurve::NistP256)
157+
.with_curve(alg.try_into()?)
95158
.with_key_derivation_function_scheme(KeyDerivationFunctionScheme::Null)
96159
.with_is_signing_key(obj_attrs.sign_encrypt())
97160
.with_is_decryption_key(obj_attrs.decrypt())
@@ -102,7 +165,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
102165
EccParameter::try_from(vec![0u8; 32])?,
103166
EccParameter::try_from(vec![0u8; 32])?,
104167
)),
105-
AsymmetricAlgorithm::Null => {
168+
AsymmetricAlgorithmSelection::Null => {
106169
// TDOD: Figure out what to with Null.
107170
return Err(Error::local_error(WrapperErrorKind::UnsupportedParam));
108171
}
@@ -119,7 +182,7 @@ pub fn create_ek_public_from_default_template<IKC: IntoKeyCustomization>(
119182
/// Create the Endorsement Key object from the specification templates
120183
pub fn create_ek_object<IKC: IntoKeyCustomization>(
121184
context: &mut Context,
122-
alg: AsymmetricAlgorithm,
185+
alg: AsymmetricAlgorithmSelection,
123186
key_customization: IKC,
124187
) -> Result<KeyHandle> {
125188
let ek_public = create_ek_public_from_default_template(alg, key_customization)?;
@@ -132,11 +195,19 @@ pub fn create_ek_object<IKC: IntoKeyCustomization>(
132195
}
133196

134197
/// Retrieve the Endorsement Key public certificate from the TPM
135-
pub fn retrieve_ek_pubcert(context: &mut Context, alg: AsymmetricAlgorithm) -> Result<Vec<u8>> {
198+
pub fn retrieve_ek_pubcert(
199+
context: &mut Context,
200+
alg: AsymmetricAlgorithmSelection,
201+
) -> Result<Vec<u8>> {
136202
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 => {
203+
AsymmetricAlgorithmSelection::Rsa2048 => RSA_2048_EK_CERTIFICATE_NV_INDEX,
204+
AsymmetricAlgorithmSelection::Rsa3072 => RSA_3072_EK_CERTIFICATE_NV_INDEX,
205+
AsymmetricAlgorithmSelection::Rsa4096 => RSA_4096_EK_CERTIFICATE_NV_INDEX,
206+
AsymmetricAlgorithmSelection::EccP256 => ECC_P256_EK_CERTIFICATE_NV_INDEX,
207+
AsymmetricAlgorithmSelection::EccP384 => ECC_P384_EK_CERTIFICATE_NV_INDEX,
208+
AsymmetricAlgorithmSelection::EccP521 => ECC_P521_EK_CERTIFICATE_NV_INDEX,
209+
AsymmetricAlgorithmSelection::EccP256Sm2 => ECC_P256_SM2_EK_CERTIFICATE_NV_INDEX,
210+
AsymmetricAlgorithmSelection::Null => {
140211
// TDOD: Figure out what to with Null.
141212
return Err(Error::local_error(WrapperErrorKind::UnsupportedParam));
142213
}

tss-esapi/src/abstraction/transient/key_attestation.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,12 @@ impl TransientKeyContext {
154154
None,
155155
);
156156
Ok((
157-
ek::create_ek_object(&mut self.context, AsymmetricAlgorithm::Rsa, None).or_else(
158-
|e| {
157+
ek::create_ek_object(&mut self.context, AsymmetricAlgorithm::Rsa.into(), None)
158+
.or_else(|e| {
159159
self.context
160160
.flush_context(SessionHandle::from(session).into())?;
161161
Err(e)
162-
},
163-
)?,
162+
})?,
164163
session,
165164
))
166165
}
@@ -191,7 +190,7 @@ impl TransientKeyContext {
191190
}
192191

193192
fn get_ek_object_public(context: &mut crate::Context) -> Result<PublicKey> {
194-
let key_handle = ek::create_ek_object(context, AsymmetricAlgorithm::Rsa, None)?;
193+
let key_handle = ek::create_ek_object(context, AsymmetricAlgorithm::Rsa.into(), None)?;
195194
let (attesting_key_pub, _, _) = context.read_public(key_handle).or_else(|e| {
196195
context.flush_context(key_handle.into())?;
197196
Err(e)

0 commit comments

Comments
 (0)