1a8e1175bSopenharmony_ci/* BEGIN_HEADER */ 2a8e1175bSopenharmony_ci#include <test/helpers.h> 3a8e1175bSopenharmony_ci#include <mbedtls/psa_util.h> 4a8e1175bSopenharmony_ci/* END_HEADER */ 5a8e1175bSopenharmony_ci 6a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_PSA_UTIL_HAVE_ECDSA */ 7a8e1175bSopenharmony_civoid ecdsa_raw_to_der(int key_bits, data_t *input, data_t *exp_result, int exp_ret) 8a8e1175bSopenharmony_ci{ 9a8e1175bSopenharmony_ci unsigned char *tmp_buf = NULL; 10a8e1175bSopenharmony_ci size_t tmp_buf_len = exp_result->len; 11a8e1175bSopenharmony_ci size_t ret_len; 12a8e1175bSopenharmony_ci 13a8e1175bSopenharmony_ci TEST_CALLOC(tmp_buf, tmp_buf_len); 14a8e1175bSopenharmony_ci 15a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_ecdsa_raw_to_der(key_bits, input->x, input->len, 16a8e1175bSopenharmony_ci tmp_buf, tmp_buf_len, &ret_len), exp_ret); 17a8e1175bSopenharmony_ci 18a8e1175bSopenharmony_ci if (exp_ret == 0) { 19a8e1175bSopenharmony_ci ASSERT_COMPARE(exp_result->x, exp_result->len, tmp_buf, ret_len); 20a8e1175bSopenharmony_ci } 21a8e1175bSopenharmony_ci 22a8e1175bSopenharmony_ciexit: 23a8e1175bSopenharmony_ci mbedtls_free(tmp_buf); 24a8e1175bSopenharmony_ci} 25a8e1175bSopenharmony_ci/* END_CASE */ 26a8e1175bSopenharmony_ci 27a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_PSA_UTIL_HAVE_ECDSA */ 28a8e1175bSopenharmony_civoid ecdsa_raw_to_der_incremental(int key_bits, data_t *input, data_t *exp_result) 29a8e1175bSopenharmony_ci{ 30a8e1175bSopenharmony_ci unsigned char *tmp_buf = NULL; 31a8e1175bSopenharmony_ci size_t ret_len; 32a8e1175bSopenharmony_ci size_t i; 33a8e1175bSopenharmony_ci 34a8e1175bSopenharmony_ci /* Test with an output buffer smaller than required (expexted to fail). */ 35a8e1175bSopenharmony_ci for (i = 1; i < exp_result->len; i++) { 36a8e1175bSopenharmony_ci TEST_CALLOC(tmp_buf, i); 37a8e1175bSopenharmony_ci TEST_ASSERT(mbedtls_ecdsa_raw_to_der(key_bits, input->x, input->len, 38a8e1175bSopenharmony_ci tmp_buf, i, &ret_len) != 0); 39a8e1175bSopenharmony_ci mbedtls_free(tmp_buf); 40a8e1175bSopenharmony_ci tmp_buf = NULL; 41a8e1175bSopenharmony_ci } 42a8e1175bSopenharmony_ci /* Test with an output buffer larger/equal than required (expexted to 43a8e1175bSopenharmony_ci * succeed). */ 44a8e1175bSopenharmony_ci for (i = exp_result->len; i < (2 * exp_result->len); i++) { 45a8e1175bSopenharmony_ci TEST_CALLOC(tmp_buf, i); 46a8e1175bSopenharmony_ci TEST_ASSERT(mbedtls_ecdsa_raw_to_der(key_bits, input->x, input->len, 47a8e1175bSopenharmony_ci tmp_buf, i, &ret_len) == 0); 48a8e1175bSopenharmony_ci mbedtls_free(tmp_buf); 49a8e1175bSopenharmony_ci tmp_buf = NULL; 50a8e1175bSopenharmony_ci } 51a8e1175bSopenharmony_ci 52a8e1175bSopenharmony_ciexit: 53a8e1175bSopenharmony_ci mbedtls_free(tmp_buf); 54a8e1175bSopenharmony_ci} 55a8e1175bSopenharmony_ci/* END_CASE */ 56a8e1175bSopenharmony_ci 57a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_PSA_UTIL_HAVE_ECDSA */ 58a8e1175bSopenharmony_civoid ecdsa_der_to_raw(int key_bits, data_t *input, data_t *exp_result, int exp_ret) 59a8e1175bSopenharmony_ci{ 60a8e1175bSopenharmony_ci unsigned char *in_buf = NULL; 61a8e1175bSopenharmony_ci size_t in_buf_len; 62a8e1175bSopenharmony_ci unsigned char *out_buf = NULL; 63a8e1175bSopenharmony_ci size_t out_buf_len = exp_result->len; 64a8e1175bSopenharmony_ci size_t ret_len; 65a8e1175bSopenharmony_ci 66a8e1175bSopenharmony_ci TEST_CALLOC(out_buf, out_buf_len); 67a8e1175bSopenharmony_ci 68a8e1175bSopenharmony_ci /* Verify that parsing of truncated input always fails. */ 69a8e1175bSopenharmony_ci for (in_buf_len = 1; in_buf_len < input->len; in_buf_len++) { 70a8e1175bSopenharmony_ci /* We alloc a copy of input buffer with limited length so that sanitizers 71a8e1175bSopenharmony_ci * can detect overreads. */ 72a8e1175bSopenharmony_ci TEST_CALLOC(in_buf, in_buf_len); 73a8e1175bSopenharmony_ci memcpy(in_buf, input->x, in_buf_len); 74a8e1175bSopenharmony_ci TEST_ASSERT(mbedtls_ecdsa_der_to_raw(key_bits, in_buf, in_buf_len, 75a8e1175bSopenharmony_ci out_buf, out_buf_len, &ret_len) != 0); 76a8e1175bSopenharmony_ci mbedtls_free(in_buf); 77a8e1175bSopenharmony_ci in_buf = NULL; 78a8e1175bSopenharmony_ci } 79a8e1175bSopenharmony_ci 80a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_ecdsa_der_to_raw(key_bits, input->x, input->len, 81a8e1175bSopenharmony_ci out_buf, out_buf_len, &ret_len), exp_ret); 82a8e1175bSopenharmony_ci 83a8e1175bSopenharmony_ci if (exp_ret == 0) { 84a8e1175bSopenharmony_ci ASSERT_COMPARE(exp_result->x, exp_result->len, out_buf, ret_len); 85a8e1175bSopenharmony_ci } 86a8e1175bSopenharmony_ci 87a8e1175bSopenharmony_ciexit: 88a8e1175bSopenharmony_ci mbedtls_free(in_buf); 89a8e1175bSopenharmony_ci mbedtls_free(out_buf); 90a8e1175bSopenharmony_ci} 91a8e1175bSopenharmony_ci/* END_CASE */ 92