Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix create_ak for ECC keys #464

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions tss-esapi/src/abstraction/ak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
session_handles::PolicySession,
},
structures::{
Auth, CreateKeyResult, EccScheme, KeyDerivationFunctionScheme, Private, Public,
Auth, CreateKeyResult, EccPoint, EccScheme, KeyDerivationFunctionScheme, Private, Public,
PublicBuilder, PublicEccParametersBuilder, PublicKeyRsa, PublicRsaParametersBuilder,
RsaExponent, RsaScheme, SymmetricDefinitionObject,
},
Expand Down Expand Up @@ -77,12 +77,17 @@ fn create_ak_public<IKC: IntoKeyCustomization>(
.with_ecc_scheme(EccScheme::create(
EccSchemeAlgorithm::try_from(AlgorithmIdentifier::from(sign_alg))?,
Some(hash_alg),
Some(0),
if sign_alg == SignatureSchemeAlgorithm::EcDaa {
Some(0)
} else {
None
},
)?)
.with_curve(ecc_curve)
.with_key_derivation_function_scheme(KeyDerivationFunctionScheme::Null)
.build()?,
),
)
.with_ecc_unique_identifier(EccPoint::default()),
};

let key_builder = if let Some(ref k) = key_customization {
Expand Down
30 changes: 25 additions & 5 deletions tss-esapi/src/structures/tagged/public/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,31 @@ impl PublicEccParametersBuilder {
return Err(Error::local_error(WrapperErrorKind::InconsistentParams));
}

if (ecc_curve == EccCurve::BnP256 || ecc_curve == EccCurve::BnP638)
&& ecc_scheme.algorithm() != EccSchemeAlgorithm::EcDaa
{
error!("Bn curve should use only EcDaa scheme");
return Err(Error::local_error(WrapperErrorKind::InconsistentParams));
match (ecc_curve, ecc_scheme.algorithm()) {
(EccCurve::BnP256 | EccCurve::BnP638, EccSchemeAlgorithm::EcDaa)
| (EccCurve::Sm2P256, EccSchemeAlgorithm::Sm2)
| (
EccCurve::NistP192
| EccCurve::NistP224
| EccCurve::NistP256
| EccCurve::NistP384
| EccCurve::NistP521,
EccSchemeAlgorithm::EcDh
| EccSchemeAlgorithm::EcDaa
| EccSchemeAlgorithm::EcDsa
| EccSchemeAlgorithm::EcMqv
| EccSchemeAlgorithm::EcSchnorr,
)
| (_, EccSchemeAlgorithm::Null) => (),

_ => {
error!(
"Mismatch between elliptic curve ({:#?}) and signing scheme ({:#?}) used",
ecc_curve,
ecc_scheme.algorithm()
);
return Err(Error::local_error(WrapperErrorKind::InconsistentParams));
}
}

Ok(PublicEccParameters {
Expand Down
57 changes: 51 additions & 6 deletions tss-esapi/tests/integration_tests/abstraction_tests/ak_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tss_esapi::{
session_handles::PolicySession,
},
structures::{Auth, Digest, PublicBuilder, SymmetricDefinition},
Error, WrapperErrorKind,
};

use crate::common::create_ctx_without_session;
Expand Down Expand Up @@ -51,22 +52,66 @@ fn test_create_ak_rsa_ecc() {
None,
)
.unwrap();
if ak::create_ak(
if let Err(Error::WrapperError(WrapperErrorKind::InconsistentParams)) = ak::create_ak(
&mut context,
ek_rsa,
HashingAlgorithm::Sha256,
AsymmetricAlgorithmSelection::Ecc(EccCurve::NistP256),
SignatureSchemeAlgorithm::Sm2,
None,
None,
)
.is_ok()
{
// We can't use unwrap_err because that requires Debug on the T
panic!("Should have errored");
) {
} else {
panic!(
"Should've gotten an 'InconsistentParams' error when trying to create an a P256 AK with an SM2 signing scheme."
);
}
}

#[test]
fn test_create_ak_ecc() {
let mut context = create_ctx_without_session();

let ek_ecc = ek::create_ek_object(
&mut context,
AsymmetricAlgorithmSelection::Ecc(EccCurve::NistP384),
None,
)
.unwrap();
ak::create_ak(
&mut context,
ek_ecc,
HashingAlgorithm::Sha256,
AsymmetricAlgorithmSelection::Ecc(EccCurve::NistP256),
SignatureSchemeAlgorithm::EcDsa,
None,
None,
)
.unwrap();
}

#[test]
fn test_create_ak_ecdaa() {
let mut context = create_ctx_without_session();

let ek_ecc = ek::create_ek_object(
&mut context,
AsymmetricAlgorithmSelection::Ecc(EccCurve::NistP384),
None,
)
.unwrap();
ak::create_ak(
&mut context,
ek_ecc,
HashingAlgorithm::Sha256,
AsymmetricAlgorithmSelection::Ecc(EccCurve::BnP256),
SignatureSchemeAlgorithm::EcDaa,
None,
None,
)
.unwrap();
}

#[test]
fn test_create_and_use_ak() {
let mut context = create_ctx_without_session();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,72 @@ fn test_signing_with_wrong_symmetric() {
Err(Error::WrapperError(WrapperErrorKind::InconsistentParams))
));
}

#[test]
fn test_signing_with_mismatched_scheme_nist() {
assert_eq!(
PublicEccParametersBuilder::new()
.with_restricted(false)
.with_is_decryption_key(false)
.with_is_signing_key(true)
.with_curve(EccCurve::NistP256)
.with_ecc_scheme(
EccScheme::create(
EccSchemeAlgorithm::Sm2,
Some(HashingAlgorithm::Sha256),
None
)
.unwrap()
)
.with_key_derivation_function_scheme(KeyDerivationFunctionScheme::Null)
.with_symmetric(SymmetricDefinitionObject::Null)
.build(),
Err(Error::WrapperError(WrapperErrorKind::InconsistentParams))
);
}

#[test]
fn test_signing_with_mismatched_scheme_sm2() {
assert_eq!(
PublicEccParametersBuilder::new()
.with_restricted(false)
.with_is_decryption_key(false)
.with_is_signing_key(true)
.with_curve(EccCurve::Sm2P256)
.with_ecc_scheme(
EccScheme::create(
EccSchemeAlgorithm::EcDsa,
Some(HashingAlgorithm::Sha256),
None
)
.unwrap()
)
.with_key_derivation_function_scheme(KeyDerivationFunctionScheme::Null)
.with_symmetric(SymmetricDefinitionObject::Null)
.build(),
Err(Error::WrapperError(WrapperErrorKind::InconsistentParams))
);
}

#[test]
fn test_signing_with_mismatched_scheme_bn() {
assert_eq!(
PublicEccParametersBuilder::new()
.with_restricted(false)
.with_is_decryption_key(false)
.with_is_signing_key(true)
.with_curve(EccCurve::BnP256)
.with_ecc_scheme(
EccScheme::create(
EccSchemeAlgorithm::EcDsa,
Some(HashingAlgorithm::Sha256),
None
)
.unwrap()
)
.with_key_derivation_function_scheme(KeyDerivationFunctionScheme::Null)
.with_symmetric(SymmetricDefinitionObject::Null)
.build(),
Err(Error::WrapperError(WrapperErrorKind::InconsistentParams))
);
}
Loading