psa_util: improve check of raw_len in mbedtls_ecdsa_raw_to_der()
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
diff --git a/include/mbedtls/psa_util.h b/include/mbedtls/psa_util.h
index 8868bc1..a5f09a4 100644
--- a/include/mbedtls/psa_util.h
+++ b/include/mbedtls/psa_util.h
@@ -188,7 +188,8 @@
*
* \param bits Size of each coordinate in bits.
* \param raw Buffer that contains the signature in raw format.
- * \param raw_len Length of \p raw in bytes.
+ * \param raw_len Length of \p raw in bytes. This must be
+ * PSA_BITS_TO_BYTES(bits) bytes.
* \param[out] der Buffer that will be filled with the converted DER
* output. It can overlap with raw buffer.
* \param der_size Size of \p der in bytes. Given \p bits parameter:
diff --git a/library/psa_util.c b/library/psa_util.c
index 1bb02e9..f3fcd1d 100644
--- a/library/psa_util.c
+++ b/library/psa_util.c
@@ -413,7 +413,7 @@
unsigned char *p = der + der_size;
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
- if (raw_len < 2 * coordinate_len) {
+ if ((raw_len < 2 * coordinate_len) || (raw_len > 2 * coordinate_len)) {
return MBEDTLS_ERR_ASN1_INVALID_DATA;
}
diff --git a/tests/suites/test_suite_psa_crypto_util.data b/tests/suites/test_suite_psa_crypto_util.data
index 606e563..580622f 100644
--- a/tests/suites/test_suite_psa_crypto_util.data
+++ b/tests/suites/test_suite_psa_crypto_util.data
@@ -26,6 +26,14 @@
depends_on:PSA_WANT_ECC_SECP_K1_256
ecdsa_raw_to_der:256:"1111111111111111111111111111111111111111111111111111111111111111A222222222222222222222222222222222222222222222222222222222222222":"304502201111111111111111111111111111111111111111111111111111111111111111022100A222222222222222222222222222222222222222222222222222222222222222":0
+ECDSA Raw -> DER, 256bit, Invalid raw signature (1 byte shorter)
+depends_on:PSA_WANT_ECC_SECP_K1_256
+ecdsa_raw_to_der:256:"111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222":"30440220111111111111111111111111111111111111111111111111111111111111111102202222222222222222222222222222222222222222222222222222222222222222":MBEDTLS_ERR_ASN1_INVALID_DATA
+
+ECDSA Raw -> DER, 256bit, Invalid raw signature (1 byte longer)
+depends_on:PSA_WANT_ECC_SECP_K1_256
+ecdsa_raw_to_der:256:"1111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222":"30440220111111111111111111111111111111111111111111111111111111111111111102202222222222222222222222222222222222222222222222222222222222222222":MBEDTLS_ERR_ASN1_INVALID_DATA
+
ECDSA DER -> Raw, 256bit, Success
depends_on:PSA_WANT_ECC_SECP_K1_256
ecdsa_der_to_raw:256:"30440220111111111111111111111111111111111111111111111111111111111111111102202222222222222222222222222222222222222222222222222222222222222222":"11111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222":0