From e65c7f97706063f20e82288bc199b8d92bb50e73 Mon Sep 17 00:00:00 2001 From: "Yang, Longlong" Date: Sun, 24 Dec 2023 21:01:42 -0500 Subject: [PATCH] add assert for curve check to stop API misuse. Signed-off-by: Yang, Longlong --- spdmlib/src/crypto/spdm_ring/asym_verify_impl.rs | 10 +++++++--- spdmlib_crypto_mbedtls/src/asym_verify_impl.rs | 5 +++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/spdmlib/src/crypto/spdm_ring/asym_verify_impl.rs b/spdmlib/src/crypto/spdm_ring/asym_verify_impl.rs index 2ab1f28c..3eedecaa 100644 --- a/spdmlib/src/crypto/spdm_ring/asym_verify_impl.rs +++ b/spdmlib/src/crypto/spdm_ring/asym_verify_impl.rs @@ -120,7 +120,11 @@ fn asym_verify( // add ASN.1 for the ECDSA binary signature fn ecc_signature_bin_to_der(signature: &[u8], der_signature: &mut [u8]) -> SpdmResult { let sign_size = signature.len(); - assert_eq!(sign_size % 2, 0); + assert!( + // prevent API misuse + sign_size == crate::protocol::ECDSA_ECC_NIST_P256_KEY_SIZE + || sign_size == crate::protocol::ECDSA_ECC_NIST_P384_KEY_SIZE + ); let half_size = sign_size / 2; let mut r_index = half_size; @@ -205,7 +209,7 @@ mod tests { } #[test] fn test_case1_ecc_signature_bin_to_der() { - let signature = &mut [0x00u8; 32]; + let signature = &mut [0x00u8; 64]; for i in 10..signature.len() { signature[i] = 0xff; } @@ -213,7 +217,7 @@ mod tests { let der_signature = &mut [0u8; 64]; let der_sign_size = ecc_signature_bin_to_der(signature, der_signature).unwrap(); - assert_eq!(der_sign_size, 30); + assert_eq!(der_sign_size, 62); } #[test] fn test_case2_ecc_signature_bin_to_der() { diff --git a/spdmlib_crypto_mbedtls/src/asym_verify_impl.rs b/spdmlib_crypto_mbedtls/src/asym_verify_impl.rs index 2f1c2b73..48abd692 100644 --- a/spdmlib_crypto_mbedtls/src/asym_verify_impl.rs +++ b/spdmlib_crypto_mbedtls/src/asym_verify_impl.rs @@ -74,6 +74,11 @@ fn asym_verify( // add ASN.1 for the ECDSA binary signature fn ecc_signature_bin_to_der(signature: &[u8], der_signature: &mut [u8]) -> SpdmResult { let sign_size = signature.len(); + assert!( + // prevent API misuse + sign_size == spdmlib::protocol::ECDSA_ECC_NIST_P256_KEY_SIZE + || sign_size == spdmlib::protocol::ECDSA_ECC_NIST_P384_KEY_SIZE + ); let half_size = sign_size / 2; let mut r_index = half_size;