Skip to content

Commit 4c1ee47

Browse files
committed
rework feature set
This allows to bring the rustcrypto "base" and then limit the support to only the type of keys or hash you need Signed-off-by: Arthur Gautier <[email protected]>
1 parent 697f280 commit 4c1ee47

File tree

5 files changed

+50
-29
lines changed

5 files changed

+50
-29
lines changed

tss-esapi/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ required-features = ["abstraction"]
2222
[dependencies]
2323
bitfield = "0.17.0"
2424
serde = { version = "1.0.115", features = [
25+
"alloc",
2526
"derive",
2627
], optional = true, default-features = false }
2728
malloced = "1.3.1"
@@ -34,7 +35,7 @@ regex = "1.3.9"
3435
zeroize = { version = "1.5.7", features = ["zeroize_derive"] }
3536
tss-esapi-sys = { path = "../tss-esapi-sys", version = "0.5.0" }
3637
x509-cert = { version = "0.2.0", optional = true }
37-
ecdsa = { version = "0.16.9", optional = true }
38+
ecdsa = { version = "0.16.9", features = ["der", "hazmat", "arithmetic", "verifying"], optional = true }
3839
elliptic-curve = { version = "0.13.8", optional = true, features = ["alloc", "pkcs8"] }
3940
p192 = { version = "0.13.0", optional = true }
4041
p224 = { version = "0.13.2", optional = true }
@@ -48,7 +49,7 @@ sha3 = { version = "0.10.8", optional = true }
4849
sm2 = { version = "0.13.3", optional = true }
4950
sm3 = { version = "0.4.2", optional = true }
5051
digest = "0.10.7"
51-
signature = { version = "2.2.0", optional = true}
52+
signature = { version = "2.2.0", features = ["std"], optional = true}
5253
cfg-if = "1.0.0"
5354
strum = { version = "0.26.3", optional = true }
5455
strum_macros = { version = "0.26.4", optional = true }
@@ -63,6 +64,7 @@ tss-esapi = { path = ".", features = [
6364
"integration-tests",
6465
"serde",
6566
"abstraction",
67+
"rustcrypto-full",
6668
] }
6769
x509-cert = { version = "0.2.0", features = ["builder"] }
6870

@@ -72,5 +74,7 @@ semver = "1.0.7"
7274
[features]
7375
default = ["abstraction"]
7476
generate-bindings = ["tss-esapi-sys/generate-bindings"]
75-
abstraction = ["ecdsa", "elliptic-curve", "signature", "rsa", "x509-cert", "p192", "p224", "p256", "p384", "p521", "sha1", "sha2", "sha3", "sm2", "sm3"]
77+
abstraction = ["rustcrypto"]
7678
integration-tests = ["strum", "strum_macros"]
79+
rustcrypto = ["ecdsa", "elliptic-curve", "signature", "x509-cert"]
80+
rustcrypto-full = ["rustcrypto", "p192", "p224", "p256", "p384", "p521", "rsa", "sha1", "sha2", "sha3", "sm2", "sm3"]

tss-esapi/src/abstraction/hashing.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,42 @@ pub trait AssociatedHashingAlgorithm {
99
const TPM_DIGEST: HashingAlgorithm;
1010
}
1111

12-
#[cfg(feature = "sha1")]
12+
#[cfg(all(feature = "rustcrypto", feature = "sha1"))]
1313
impl AssociatedHashingAlgorithm for sha1::Sha1 {
1414
const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha1;
1515
}
1616

17-
#[cfg(feature = "sha2")]
17+
#[cfg(all(feature = "rustcrypto", feature = "sha2"))]
1818
impl AssociatedHashingAlgorithm for sha2::Sha256 {
1919
const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha256;
2020
}
2121

22-
#[cfg(feature = "sha2")]
22+
#[cfg(all(feature = "rustcrypto", feature = "sha2"))]
2323
impl AssociatedHashingAlgorithm for sha2::Sha384 {
2424
const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha384;
2525
}
2626

27-
#[cfg(feature = "sha2")]
27+
#[cfg(all(feature = "rustcrypto", feature = "sha2"))]
2828
impl AssociatedHashingAlgorithm for sha2::Sha512 {
2929
const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha512;
3030
}
3131

32-
#[cfg(feature = "sm3")]
32+
#[cfg(all(feature = "rustcrypto", feature = "sm3"))]
3333
impl AssociatedHashingAlgorithm for sm3::Sm3 {
3434
const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sm3_256;
3535
}
3636

37-
#[cfg(feature = "sha3")]
37+
#[cfg(all(feature = "rustcrypto", feature = "sha3"))]
3838
impl AssociatedHashingAlgorithm for sha3::Sha3_256 {
3939
const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha3_256;
4040
}
4141

42-
#[cfg(feature = "sha3")]
42+
#[cfg(all(feature = "rustcrypto", feature = "sha3"))]
4343
impl AssociatedHashingAlgorithm for sha3::Sha3_384 {
4444
const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha3_384;
4545
}
4646

47-
#[cfg(feature = "sha3")]
47+
#[cfg(all(feature = "rustcrypto", feature = "sha3"))]
4848
impl AssociatedHashingAlgorithm for sha3::Sha3_512 {
4949
const TPM_DIGEST: HashingAlgorithm = HashingAlgorithm::Sha3_512;
5050
}

tss-esapi/src/abstraction/public.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::interface_types::ecc::EccCurve;
5-
use crate::structures::{Public, RsaExponent};
5+
use crate::structures::Public;
66
use crate::utils::PublicKey as TpmPublicKey;
77
use crate::{Error, WrapperErrorKind};
88

@@ -18,11 +18,19 @@ use elliptic_curve::{
1818
FieldBytesSize,
1919
PublicKey,
2020
};
21-
use rsa::{pkcs8::EncodePublicKey, BigUint, RsaPublicKey};
21+
22+
#[cfg(feature = "rustcrypto")]
2223
use x509_cert::spki::SubjectPublicKeyInfoOwned;
2324

25+
#[cfg(all(feature = "rustcrypto", feature = "rsa"))]
26+
use {
27+
crate::structures::RsaExponent,
28+
rsa::{pkcs8::EncodePublicKey, BigUint, RsaPublicKey},
29+
};
30+
2431
/// Default exponent for RSA keys.
2532
// Also known as 0x10001
33+
#[cfg(all(feature = "rustcrypto", feature = "rsa"))]
2634
const RSA_DEFAULT_EXP: u64 = 65537;
2735

2836
impl<C> TryFrom<&Public> for PublicKey<C>
@@ -66,6 +74,7 @@ where
6674
}
6775
}
6876

77+
#[cfg(all(feature = "rustcrypto", feature = "rsa"))]
6978
impl TryFrom<&Public> for RsaPublicKey {
7079
type Error = Error;
7180

@@ -102,6 +111,7 @@ impl TryFrom<&Public> for SubjectPublicKeyInfoOwned {
102111
/// * if other instances of [`crate::structures::Public`] are used `UnsupportedParam` will be returned.
103112
fn try_from(value: &Public) -> Result<Self, Self::Error> {
104113
match value {
114+
#[cfg(all(feature = "rustcrypto", feature = "rsa"))]
105115
Public::Rsa { .. } => {
106116
let public_key = RsaPublicKey::try_from(value)?;
107117

@@ -127,17 +137,17 @@ impl TryFrom<&Public> for SubjectPublicKeyInfoOwned {
127137
};
128138
}
129139

130-
#[cfg(feature = "p192")]
140+
#[cfg(all(feature = "rustcrypto", feature = "p192"))]
131141
read_key!(EccCurve::NistP192, p192::NistP192);
132-
#[cfg(feature = "p224")]
142+
#[cfg(all(feature = "rustcrypto", feature = "p224"))]
133143
read_key!(EccCurve::NistP224, p224::NistP224);
134-
#[cfg(feature = "p256")]
144+
#[cfg(all(feature = "rustcrypto", feature = "p256"))]
135145
read_key!(EccCurve::NistP256, p256::NistP256);
136-
#[cfg(feature = "p384")]
146+
#[cfg(all(feature = "rustcrypto", feature = "p384"))]
137147
read_key!(EccCurve::NistP384, p384::NistP384);
138-
#[cfg(feature = "p521")]
148+
#[cfg(all(feature = "rustcrypto", feature = "p521"))]
139149
read_key!(EccCurve::NistP521, p521::NistP521);
140-
#[cfg(feature = "sm2")]
150+
#[cfg(all(feature = "rustcrypto", feature = "sm2"))]
141151
read_key!(EccCurve::Sm2P256, sm2::Sm2);
142152

143153
Err(Error::local_error(WrapperErrorKind::UnsupportedParam))
@@ -182,6 +192,7 @@ where
182192
}
183193
}
184194

195+
#[cfg(all(feature = "rustcrypto", feature = "rsa"))]
185196
impl TryFrom<&TpmPublicKey> for RsaPublicKey {
186197
type Error = Error;
187198

@@ -207,32 +218,32 @@ pub trait AssociatedTpmCurve {
207218
const TPM_CURVE: EccCurve;
208219
}
209220

210-
#[cfg(feature = "p192")]
221+
#[cfg(all(feature = "rustcrypto", feature = "p192"))]
211222
impl AssociatedTpmCurve for p192::NistP192 {
212223
const TPM_CURVE: EccCurve = EccCurve::NistP192;
213224
}
214225

215-
#[cfg(feature = "p224")]
226+
#[cfg(all(feature = "rustcrypto", feature = "p224"))]
216227
impl AssociatedTpmCurve for p224::NistP224 {
217228
const TPM_CURVE: EccCurve = EccCurve::NistP224;
218229
}
219230

220-
#[cfg(feature = "p256")]
231+
#[cfg(all(feature = "rustcrypto", feature = "p256"))]
221232
impl AssociatedTpmCurve for p256::NistP256 {
222233
const TPM_CURVE: EccCurve = EccCurve::NistP256;
223234
}
224235

225-
#[cfg(feature = "p384")]
236+
#[cfg(all(feature = "rustcrypto", feature = "p384"))]
226237
impl AssociatedTpmCurve for p384::NistP384 {
227238
const TPM_CURVE: EccCurve = EccCurve::NistP384;
228239
}
229240

230-
#[cfg(feature = "p521")]
241+
#[cfg(all(feature = "rustcrypto", feature = "p521"))]
231242
impl AssociatedTpmCurve for p521::NistP521 {
232243
const TPM_CURVE: EccCurve = EccCurve::NistP521;
233244
}
234245

235-
#[cfg(feature = "sm2")]
246+
#[cfg(all(feature = "rustcrypto", feature = "sm2"))]
236247
impl AssociatedTpmCurve for sm2::Sm2 {
237248
const TPM_CURVE: EccCurve = EccCurve::Sm2P256;
238249
}

tss-esapi/src/abstraction/signatures.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Copyright 2024 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::{
5-
structures::{EccSignature, Signature},
6-
Error, Result, WrapperErrorKind,
7-
};
4+
use crate::{structures::EccSignature, Error, Result, WrapperErrorKind};
85

96
use std::convert::TryFrom;
107

@@ -14,6 +11,9 @@ use elliptic_curve::{
1411
FieldBytes, FieldBytesSize, PrimeCurve,
1512
};
1613

14+
#[cfg(all(feature = "rustcrypto", feature = "rsa"))]
15+
use crate::structures::Signature;
16+
1717
impl<C> TryFrom<EccSignature> for ecdsa::Signature<C>
1818
where
1919
C: PrimeCurve,
@@ -43,6 +43,7 @@ where
4343

4444
// Note: this does not implement `TryFrom<RsaSignature>` because `RsaSignature` does not carry the
4545
// information whether the signatures was generated using PKCS#1v1.5 or PSS.
46+
#[cfg(all(feature = "rustcrypto", feature = "rsa"))]
4647
impl TryFrom<Signature> for rsa::pkcs1v15::Signature {
4748
type Error = Error;
4849

@@ -58,6 +59,7 @@ impl TryFrom<Signature> for rsa::pkcs1v15::Signature {
5859

5960
// Note: this does not implement `TryFrom<RsaSignature>` because `RsaSignature` does not carry the
6061
// information whether the signatures was generated using PKCS#1v1.5 or PSS.
62+
#[cfg(all(feature = "rustcrypto", feature = "rsa"))]
6163
impl TryFrom<Signature> for rsa::pss::Signature {
6264
type Error = Error;
6365

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ use std::convert::{AsMut, AsRef, TryFrom, TryInto};
3434
use zeroize::Zeroize;
3535

3636
mod key_attestation;
37+
38+
#[cfg(feature = "rustcrypto")]
3739
mod signer;
3840

3941
pub use key_attestation::MakeCredParams;
42+
43+
#[cfg(feature = "rustcrypto")]
4044
pub use signer::EcSigner;
4145

4246
/// Parameters for the kinds of keys supported by the context

0 commit comments

Comments
 (0)