2024-01-08 16:50:30 +01:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include <test/helpers.h>
|
|
|
|
#include <mbedtls/psa_util.h>
|
|
|
|
/* END_HEADER */
|
|
|
|
|
2024-01-16 09:18:40 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_UTIL_HAVE_ECDSA */
|
2024-01-10 09:00:55 +01:00
|
|
|
void ecdsa_raw_to_der(int key_bits, data_t *input, data_t *exp_result, int exp_ret)
|
2024-01-08 16:50:30 +01:00
|
|
|
{
|
|
|
|
unsigned char *tmp_buf = NULL;
|
|
|
|
size_t tmp_buf_len = exp_result->len;
|
|
|
|
size_t ret_len;
|
|
|
|
|
|
|
|
TEST_CALLOC(tmp_buf, tmp_buf_len);
|
|
|
|
|
2024-02-05 10:09:15 +01:00
|
|
|
TEST_EQUAL(mbedtls_ecdsa_raw_to_der(key_bits, input->x, input->len,
|
|
|
|
tmp_buf, tmp_buf_len, &ret_len), exp_ret);
|
2024-01-10 09:00:55 +01:00
|
|
|
|
|
|
|
if (exp_ret == 0) {
|
|
|
|
ASSERT_COMPARE(exp_result->x, exp_result->len, tmp_buf, ret_len);
|
2024-01-08 16:50:30 +01:00
|
|
|
}
|
|
|
|
|
2024-01-10 09:00:55 +01:00
|
|
|
exit:
|
|
|
|
mbedtls_free(tmp_buf);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2024-01-29 17:34:07 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_UTIL_HAVE_ECDSA */
|
|
|
|
void ecdsa_raw_to_der_incremental(int key_bits, data_t *input, data_t *exp_result)
|
|
|
|
{
|
|
|
|
unsigned char *tmp_buf = NULL;
|
|
|
|
size_t ret_len;
|
|
|
|
size_t i;
|
|
|
|
|
2024-02-05 12:06:46 +01:00
|
|
|
/* Test with an output buffer smaller than required (expexted to fail). */
|
2024-02-06 15:49:06 +01:00
|
|
|
for (i = 1; i < exp_result->len; i++) {
|
2024-01-30 15:46:02 +01:00
|
|
|
TEST_CALLOC(tmp_buf, i);
|
2024-02-05 10:09:15 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_raw_to_der(key_bits, input->x, input->len,
|
|
|
|
tmp_buf, i, &ret_len) != 0);
|
2024-01-30 15:46:02 +01:00
|
|
|
mbedtls_free(tmp_buf);
|
|
|
|
tmp_buf = NULL;
|
2024-01-29 17:34:07 +01:00
|
|
|
}
|
2024-02-05 12:06:46 +01:00
|
|
|
/* Test with an output buffer larger/equal than required (expexted to
|
|
|
|
* succeed). */
|
2024-02-06 15:49:06 +01:00
|
|
|
for (i = exp_result->len; i < (2 * exp_result->len); i++) {
|
2024-02-05 12:06:46 +01:00
|
|
|
TEST_CALLOC(tmp_buf, i);
|
|
|
|
TEST_ASSERT(mbedtls_ecdsa_raw_to_der(key_bits, input->x, input->len,
|
|
|
|
tmp_buf, i, &ret_len) == 0);
|
|
|
|
mbedtls_free(tmp_buf);
|
|
|
|
tmp_buf = NULL;
|
|
|
|
}
|
2024-01-30 15:46:02 +01:00
|
|
|
|
2024-01-29 17:34:07 +01:00
|
|
|
exit:
|
|
|
|
mbedtls_free(tmp_buf);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2024-01-16 09:18:40 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_UTIL_HAVE_ECDSA */
|
2024-01-10 09:00:55 +01:00
|
|
|
void ecdsa_der_to_raw(int key_bits, data_t *input, data_t *exp_result, int exp_ret)
|
|
|
|
{
|
2024-02-05 17:59:42 +01:00
|
|
|
unsigned char *in_buf = NULL;
|
|
|
|
size_t in_buf_len;
|
|
|
|
unsigned char *out_buf = NULL;
|
|
|
|
size_t out_buf_len = exp_result->len;
|
2024-01-10 09:00:55 +01:00
|
|
|
size_t ret_len;
|
|
|
|
|
2024-02-05 17:59:42 +01:00
|
|
|
TEST_CALLOC(out_buf, out_buf_len);
|
|
|
|
|
|
|
|
/* Verify that parsing of truncated input always fails. */
|
|
|
|
for (in_buf_len = 1; in_buf_len < input->len; in_buf_len++) {
|
|
|
|
/* We alloc a copy of input buffer with limited length so that sanitizers
|
|
|
|
* can detect overreads. */
|
|
|
|
TEST_CALLOC(in_buf, in_buf_len);
|
|
|
|
memcpy(in_buf, input->x, in_buf_len);
|
|
|
|
TEST_ASSERT(mbedtls_ecdsa_der_to_raw(key_bits, in_buf, in_buf_len,
|
|
|
|
out_buf, out_buf_len, &ret_len) != 0);
|
|
|
|
mbedtls_free(in_buf);
|
|
|
|
in_buf = NULL;
|
|
|
|
}
|
2024-01-10 09:00:55 +01:00
|
|
|
|
2024-02-05 10:09:15 +01:00
|
|
|
TEST_EQUAL(mbedtls_ecdsa_der_to_raw(key_bits, input->x, input->len,
|
2024-02-05 17:59:42 +01:00
|
|
|
out_buf, out_buf_len, &ret_len), exp_ret);
|
2024-01-10 09:00:55 +01:00
|
|
|
|
2024-01-08 16:50:30 +01:00
|
|
|
if (exp_ret == 0) {
|
2024-02-05 17:59:42 +01:00
|
|
|
ASSERT_COMPARE(exp_result->x, exp_result->len, out_buf, ret_len);
|
2024-01-08 16:50:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2024-02-05 17:59:42 +01:00
|
|
|
mbedtls_free(in_buf);
|
|
|
|
mbedtls_free(out_buf);
|
2024-01-08 16:50:30 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|