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