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