psa_util: change parameters order in ECDSA conversion functions

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
This commit is contained in:
Valerio Setti 2024-02-05 10:09:15 +01:00
parent 8645197937
commit 315e4afc0a
4 changed files with 21 additions and 30 deletions

View file

@ -186,6 +186,7 @@ static inline mbedtls_md_type_t mbedtls_md_type_from_psa_alg(psa_algorithm_t psa
/** Convert an ECDSA signature from raw format to DER ASN.1 format. /** Convert an ECDSA signature from raw format to DER ASN.1 format.
* *
* \param bits Size of each coordinate in bits.
* \param raw Buffer that contains the signature in raw format. * \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.
* \param[out] der Buffer that will be filled with the converted DER * \param[out] der Buffer that will be filled with the converted DER
@ -194,14 +195,13 @@ static inline mbedtls_md_type_t mbedtls_md_type_from_psa_alg(psa_algorithm_t psa
* \param[out] der_len On success it contains the amount of valid data * \param[out] der_len On success it contains the amount of valid data
* (in bytes) written to \p der. It's undefined * (in bytes) written to \p der. It's undefined
* in case of failure. * in case of failure.
* \param bits Size of each coordinate in bits.
*/ */
int mbedtls_ecdsa_raw_to_der(const unsigned char *raw, size_t raw_len, int mbedtls_ecdsa_raw_to_der(size_t bits, const unsigned char *raw, size_t raw_len,
unsigned char *der, size_t der_size, size_t *der_len, unsigned char *der, size_t der_size, size_t *der_len);
size_t bits);
/** Convert an ECDSA signature from DER ASN.1 format to raw format. /** Convert an ECDSA signature from DER ASN.1 format to raw format.
* *
* \param bits Size of each coordinate in bits.
* \param der Buffer that contains the signature in DER format. * \param der Buffer that contains the signature in DER format.
* \param der_len Size of \p der in bytes. * \param der_len Size of \p der in bytes.
* \param[out] raw Buffer that will be filled with the converted raw * \param[out] raw Buffer that will be filled with the converted raw
@ -210,11 +210,9 @@ int mbedtls_ecdsa_raw_to_der(const unsigned char *raw, size_t raw_len,
* \param[out] raw_len On success it is updated with the amount of valid * \param[out] raw_len On success it is updated with the amount of valid
* data (in bytes) written to \p raw. It's undefined * data (in bytes) written to \p raw. It's undefined
* in case of failure. * in case of failure.
* \param bits Size of each coordinate in bits.
*/ */
int mbedtls_ecdsa_der_to_raw(const unsigned char *der, size_t der_len, int mbedtls_ecdsa_der_to_raw(size_t bits, const unsigned char *der, size_t der_len,
unsigned char *raw, size_t raw_size, size_t *raw_len, unsigned char *raw, size_t raw_size, size_t *raw_len);
size_t bits);
#endif /* MBEDTLS_PSA_UTIL_HAVE_ECDSA */ #endif /* MBEDTLS_PSA_UTIL_HAVE_ECDSA */

View file

@ -573,9 +573,8 @@ static int ecdsa_verify_psa(unsigned char *key, size_t key_len,
} }
p = (unsigned char *) sig; p = (unsigned char *) sig;
ret = mbedtls_ecdsa_der_to_raw(p, sig_len, extracted_sig, ret = mbedtls_ecdsa_der_to_raw(curve_bits, p, sig_len, extracted_sig,
sizeof(extracted_sig), &converted_sig_len, sizeof(extracted_sig), &converted_sig_len);
curve_bits);
if (ret != 0) { if (ret != 0) {
goto cleanup; goto cleanup;
} }
@ -730,7 +729,7 @@ static int ecdsa_sign_psa(mbedtls_svc_key_id_t key_id, mbedtls_md_type_t md_alg,
} }
done: done:
ret = mbedtls_ecdsa_raw_to_der(sig, sig_size, sig, sig_size, sig_len, key_bits); ret = mbedtls_ecdsa_raw_to_der(key_bits, sig, sig_size, sig, sig_size, sig_len);
return ret; return ret;
} }

View file

@ -402,9 +402,8 @@ static int convert_raw_to_der_single_int(const unsigned char *raw_buf, size_t ra
return len; return len;
} }
int mbedtls_ecdsa_raw_to_der(const unsigned char *raw, size_t raw_len, int mbedtls_ecdsa_raw_to_der(size_t bits, const unsigned char *raw, size_t raw_len,
unsigned char *der, size_t der_size, size_t *der_len, unsigned char *der, size_t der_size, size_t *der_len)
size_t bits)
{ {
unsigned char r[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)]; unsigned char r[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
unsigned char s[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)]; unsigned char s[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
@ -511,9 +510,8 @@ static int convert_der_to_raw_single_int(unsigned char *der, size_t der_len,
return (int) (p - der); return (int) (p - der);
} }
int mbedtls_ecdsa_der_to_raw(const unsigned char *der, size_t der_len, int mbedtls_ecdsa_der_to_raw(size_t bits, const unsigned char *der, size_t der_len,
unsigned char *raw, size_t raw_size, size_t *raw_len, unsigned char *raw, size_t raw_size, size_t *raw_len)
size_t bits)
{ {
unsigned char raw_tmp[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE]; unsigned char raw_tmp[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
unsigned char *p = (unsigned char *) der; unsigned char *p = (unsigned char *) der;

View file

@ -12,9 +12,8 @@ void ecdsa_raw_to_der(int key_bits, data_t *input, data_t *exp_result, int exp_r
TEST_CALLOC(tmp_buf, tmp_buf_len); TEST_CALLOC(tmp_buf, tmp_buf_len);
TEST_EQUAL(mbedtls_ecdsa_raw_to_der(input->x, input->len, TEST_EQUAL(mbedtls_ecdsa_raw_to_der(key_bits, input->x, input->len,
tmp_buf, tmp_buf_len, &ret_len, tmp_buf, tmp_buf_len, &ret_len), exp_ret);
key_bits), exp_ret);
if (exp_ret == 0) { if (exp_ret == 0) {
ASSERT_COMPARE(exp_result->x, exp_result->len, tmp_buf, ret_len); ASSERT_COMPARE(exp_result->x, exp_result->len, tmp_buf, ret_len);
@ -35,17 +34,15 @@ void ecdsa_raw_to_der_incremental(int key_bits, data_t *input, data_t *exp_resul
for (i = 1; i < tmp_buf_len; i++) { for (i = 1; i < tmp_buf_len; i++) {
TEST_CALLOC(tmp_buf, i); TEST_CALLOC(tmp_buf, i);
TEST_ASSERT(mbedtls_ecdsa_raw_to_der(input->x, input->len, TEST_ASSERT(mbedtls_ecdsa_raw_to_der(key_bits, input->x, input->len,
tmp_buf, i, &ret_len, tmp_buf, i, &ret_len) != 0);
key_bits) != 0);
mbedtls_free(tmp_buf); mbedtls_free(tmp_buf);
tmp_buf = NULL; tmp_buf = NULL;
} }
TEST_CALLOC(tmp_buf, i); TEST_CALLOC(tmp_buf, i);
TEST_EQUAL(mbedtls_ecdsa_raw_to_der(input->x, input->len, TEST_EQUAL(mbedtls_ecdsa_raw_to_der(key_bits, input->x, input->len,
tmp_buf, i, &ret_len, tmp_buf, i, &ret_len), 0);
key_bits), 0);
exit: exit:
mbedtls_free(tmp_buf); mbedtls_free(tmp_buf);
} }
@ -60,9 +57,8 @@ void ecdsa_der_to_raw(int key_bits, data_t *input, data_t *exp_result, int exp_r
TEST_CALLOC(tmp_buf, tmp_buf_len); TEST_CALLOC(tmp_buf, tmp_buf_len);
TEST_EQUAL(mbedtls_ecdsa_der_to_raw(input->x, input->len, TEST_EQUAL(mbedtls_ecdsa_der_to_raw(key_bits, input->x, input->len,
tmp_buf, tmp_buf_len, &ret_len, tmp_buf, tmp_buf_len, &ret_len), exp_ret);
key_bits), exp_ret);
if (exp_ret == 0) { if (exp_ret == 0) {
ASSERT_COMPARE(exp_result->x, exp_result->len, tmp_buf, ret_len); ASSERT_COMPARE(exp_result->x, exp_result->len, tmp_buf, ret_len);