1a8e1175bSopenharmony_ci/* BEGIN_HEADER */
2a8e1175bSopenharmony_ci#include "mbedtls/base64.h"
3a8e1175bSopenharmony_ci#include "base64_internal.h"
4a8e1175bSopenharmony_ci#include "constant_time_internal.h"
5a8e1175bSopenharmony_ci#include <test/constant_flow.h>
6a8e1175bSopenharmony_ci
7a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_HOOKS)
8a8e1175bSopenharmony_cistatic const char base64_digits[] =
9a8e1175bSopenharmony_ci    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
10a8e1175bSopenharmony_ci#endif /* MBEDTLS_TEST_HOOKS */
11a8e1175bSopenharmony_ci
12a8e1175bSopenharmony_ci/* END_HEADER */
13a8e1175bSopenharmony_ci
14a8e1175bSopenharmony_ci/* BEGIN_DEPENDENCIES
15a8e1175bSopenharmony_ci * depends_on:MBEDTLS_BASE64_C
16a8e1175bSopenharmony_ci * END_DEPENDENCIES
17a8e1175bSopenharmony_ci */
18a8e1175bSopenharmony_ci
19a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
20a8e1175bSopenharmony_civoid enc_chars()
21a8e1175bSopenharmony_ci{
22a8e1175bSopenharmony_ci    for (unsigned value = 0; value < 64; value++) {
23a8e1175bSopenharmony_ci        mbedtls_test_set_step(value);
24a8e1175bSopenharmony_ci        TEST_CF_SECRET(&value, sizeof(value));
25a8e1175bSopenharmony_ci        unsigned char digit = mbedtls_ct_base64_enc_char(value);
26a8e1175bSopenharmony_ci        TEST_CF_PUBLIC(&value, sizeof(value));
27a8e1175bSopenharmony_ci        TEST_CF_PUBLIC(&digit, sizeof(digit));
28a8e1175bSopenharmony_ci        TEST_EQUAL(digit, base64_digits[value]);
29a8e1175bSopenharmony_ci    }
30a8e1175bSopenharmony_ci}
31a8e1175bSopenharmony_ci/* END_CASE */
32a8e1175bSopenharmony_ci
33a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
34a8e1175bSopenharmony_civoid dec_chars()
35a8e1175bSopenharmony_ci{
36a8e1175bSopenharmony_ci    char *p;
37a8e1175bSopenharmony_ci    signed char expected;
38a8e1175bSopenharmony_ci
39a8e1175bSopenharmony_ci    for (unsigned c = 0; c <= 0xff; c++) {
40a8e1175bSopenharmony_ci        mbedtls_test_set_step(c);
41a8e1175bSopenharmony_ci        /* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
42a8e1175bSopenharmony_ci        p = memchr(base64_digits, c, sizeof(base64_digits) - 1);
43a8e1175bSopenharmony_ci        if (p == NULL) {
44a8e1175bSopenharmony_ci            expected = -1;
45a8e1175bSopenharmony_ci        } else {
46a8e1175bSopenharmony_ci            expected = p - base64_digits;
47a8e1175bSopenharmony_ci        }
48a8e1175bSopenharmony_ci        TEST_CF_SECRET(&c, sizeof(c));
49a8e1175bSopenharmony_ci        signed char actual = mbedtls_ct_base64_dec_value(c);
50a8e1175bSopenharmony_ci        TEST_CF_PUBLIC(&c, sizeof(c));
51a8e1175bSopenharmony_ci        TEST_CF_PUBLIC(&actual, sizeof(actual));
52a8e1175bSopenharmony_ci        TEST_EQUAL(actual, expected);
53a8e1175bSopenharmony_ci    }
54a8e1175bSopenharmony_ci}
55a8e1175bSopenharmony_ci/* END_CASE */
56a8e1175bSopenharmony_ci
57a8e1175bSopenharmony_ci/* BEGIN_CASE */
58a8e1175bSopenharmony_civoid mbedtls_base64_encode(char *src_string, char *dst_string,
59a8e1175bSopenharmony_ci                           int dst_buf_size, int result)
60a8e1175bSopenharmony_ci{
61a8e1175bSopenharmony_ci    unsigned char src_str[1000];
62a8e1175bSopenharmony_ci    unsigned char dst_str[1000];
63a8e1175bSopenharmony_ci    size_t len, src_len;
64a8e1175bSopenharmony_ci
65a8e1175bSopenharmony_ci    memset(src_str, 0x00, 1000);
66a8e1175bSopenharmony_ci    memset(dst_str, 0x00, 1000);
67a8e1175bSopenharmony_ci
68a8e1175bSopenharmony_ci    strncpy((char *) src_str, src_string, sizeof(src_str) - 1);
69a8e1175bSopenharmony_ci    src_len = strlen((char *) src_str);
70a8e1175bSopenharmony_ci
71a8e1175bSopenharmony_ci    TEST_CF_SECRET(src_str, sizeof(src_str));
72a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_base64_encode(dst_str, dst_buf_size, &len, src_str, src_len) == result);
73a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(src_str, sizeof(src_str));
74a8e1175bSopenharmony_ci
75a8e1175bSopenharmony_ci    /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
76a8e1175bSopenharmony_ci       CF failures by unmarking it. */
77a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(dst_str, len);
78a8e1175bSopenharmony_ci
79a8e1175bSopenharmony_ci    if (result == 0) {
80a8e1175bSopenharmony_ci        TEST_ASSERT(strcmp((char *) dst_str, dst_string) == 0);
81a8e1175bSopenharmony_ci    }
82a8e1175bSopenharmony_ci}
83a8e1175bSopenharmony_ci/* END_CASE */
84a8e1175bSopenharmony_ci
85a8e1175bSopenharmony_ci/* BEGIN_CASE */
86a8e1175bSopenharmony_civoid mbedtls_base64_decode(char *src_string, char *dst_string, int result)
87a8e1175bSopenharmony_ci{
88a8e1175bSopenharmony_ci    unsigned char src_str[1000];
89a8e1175bSopenharmony_ci    unsigned char dst_str[1000];
90a8e1175bSopenharmony_ci    size_t len;
91a8e1175bSopenharmony_ci    int res;
92a8e1175bSopenharmony_ci
93a8e1175bSopenharmony_ci    memset(src_str, 0x00, 1000);
94a8e1175bSopenharmony_ci    memset(dst_str, 0x00, 1000);
95a8e1175bSopenharmony_ci
96a8e1175bSopenharmony_ci    strncpy((char *) src_str, src_string, sizeof(src_str) - 1);
97a8e1175bSopenharmony_ci    res = mbedtls_base64_decode(dst_str, sizeof(dst_str), &len, src_str, strlen((char *) src_str));
98a8e1175bSopenharmony_ci    TEST_ASSERT(res == result);
99a8e1175bSopenharmony_ci    if (result == 0) {
100a8e1175bSopenharmony_ci        TEST_ASSERT(strcmp((char *) dst_str, dst_string) == 0);
101a8e1175bSopenharmony_ci    }
102a8e1175bSopenharmony_ci}
103a8e1175bSopenharmony_ci/* END_CASE */
104a8e1175bSopenharmony_ci
105a8e1175bSopenharmony_ci/* BEGIN_CASE */
106a8e1175bSopenharmony_civoid base64_encode_hex(data_t *src, char *dst, int dst_buf_size,
107a8e1175bSopenharmony_ci                       int result)
108a8e1175bSopenharmony_ci{
109a8e1175bSopenharmony_ci    unsigned char *res = NULL;
110a8e1175bSopenharmony_ci    size_t len;
111a8e1175bSopenharmony_ci
112a8e1175bSopenharmony_ci    res = mbedtls_test_zero_alloc(dst_buf_size);
113a8e1175bSopenharmony_ci
114a8e1175bSopenharmony_ci    TEST_CF_SECRET(src->x, src->len);
115a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_base64_encode(res, dst_buf_size, &len, src->x, src->len) == result);
116a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(src->x, src->len);
117a8e1175bSopenharmony_ci
118a8e1175bSopenharmony_ci    /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
119a8e1175bSopenharmony_ci       CF failures by unmarking it. */
120a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(res, len);
121a8e1175bSopenharmony_ci
122a8e1175bSopenharmony_ci    if (result == 0) {
123a8e1175bSopenharmony_ci        TEST_ASSERT(len == strlen(dst));
124a8e1175bSopenharmony_ci        TEST_ASSERT(memcmp(dst, res, len) == 0);
125a8e1175bSopenharmony_ci    }
126a8e1175bSopenharmony_ci
127a8e1175bSopenharmony_ciexit:
128a8e1175bSopenharmony_ci    mbedtls_free(res);
129a8e1175bSopenharmony_ci}
130a8e1175bSopenharmony_ci/* END_CASE */
131a8e1175bSopenharmony_ci
132a8e1175bSopenharmony_ci/* BEGIN_CASE */
133a8e1175bSopenharmony_civoid base64_decode_hex(char *src, data_t *dst, int dst_buf_size,
134a8e1175bSopenharmony_ci                       int result)
135a8e1175bSopenharmony_ci{
136a8e1175bSopenharmony_ci    unsigned char *res = NULL;
137a8e1175bSopenharmony_ci    size_t len;
138a8e1175bSopenharmony_ci
139a8e1175bSopenharmony_ci    res = mbedtls_test_zero_alloc(dst_buf_size);
140a8e1175bSopenharmony_ci
141a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_base64_decode(res, dst_buf_size, &len, (unsigned char *) src,
142a8e1175bSopenharmony_ci                                      strlen(src)) == result);
143a8e1175bSopenharmony_ci    if (result == 0) {
144a8e1175bSopenharmony_ci        TEST_ASSERT(len == dst->len);
145a8e1175bSopenharmony_ci        TEST_ASSERT(memcmp(dst->x, res, len) == 0);
146a8e1175bSopenharmony_ci    }
147a8e1175bSopenharmony_ci
148a8e1175bSopenharmony_ciexit:
149a8e1175bSopenharmony_ci    mbedtls_free(res);
150a8e1175bSopenharmony_ci}
151a8e1175bSopenharmony_ci/* END_CASE */
152a8e1175bSopenharmony_ci
153a8e1175bSopenharmony_ci/* BEGIN_CASE */
154a8e1175bSopenharmony_civoid base64_decode_hex_src(data_t *src, char *dst_ref, int result)
155a8e1175bSopenharmony_ci{
156a8e1175bSopenharmony_ci    unsigned char dst[1000] = { 0 };
157a8e1175bSopenharmony_ci    size_t len;
158a8e1175bSopenharmony_ci
159a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_base64_decode(dst, sizeof(dst), &len, src->x, src->len) == result);
160a8e1175bSopenharmony_ci    if (result == 0) {
161a8e1175bSopenharmony_ci        TEST_ASSERT(len == strlen(dst_ref));
162a8e1175bSopenharmony_ci        TEST_ASSERT(memcmp(dst, dst_ref, len) == 0);
163a8e1175bSopenharmony_ci    }
164a8e1175bSopenharmony_ci
165a8e1175bSopenharmony_ciexit:
166a8e1175bSopenharmony_ci    ;;
167a8e1175bSopenharmony_ci}
168a8e1175bSopenharmony_ci/* END_CASE */
169a8e1175bSopenharmony_ci
170a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
171a8e1175bSopenharmony_civoid base64_selftest()
172a8e1175bSopenharmony_ci{
173a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_base64_self_test(1) == 0);
174a8e1175bSopenharmony_ci}
175a8e1175bSopenharmony_ci/* END_CASE */
176