blob: 095f980f3fc4dc058a628ab36cc24edb1425c17e [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/base64.h"
Dave Rodgman0fec4392023-05-18 15:24:36 +01003#include "base64_internal.h"
Gabor Mezei28d61152021-11-15 16:13:01 +01004#include "constant_time_internal.h"
Dave Rodgman378280e2023-07-31 16:34:19 +01005#include <test/constant_flow.h>
Gilles Peskineba951f52021-08-06 14:55:55 +02006
7#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine93365a72021-08-06 16:54:22 +02008static const char base64_digits[] =
Gilles Peskineba951f52021-08-06 14:55:55 +02009 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
10#endif /* MBEDTLS_TEST_HOOKS */
11
Paul Bakker33b43f12013-08-20 11:48:36 +020012/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000013
Paul Bakker33b43f12013-08-20 11:48:36 +020014/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015 * depends_on:MBEDTLS_BASE64_C
Paul Bakker33b43f12013-08-20 11:48:36 +020016 * END_DEPENDENCIES
17 */
Paul Bakker5690efc2011-05-26 13:16:06 +000018
Gilles Peskinea64417a2021-08-03 12:38:55 +020019/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
Gilles Peskine449bd832023-01-11 14:50:10 +010020void enc_chars()
Gilles Peskinea64417a2021-08-03 12:38:55 +020021{
Gilles Peskine449bd832023-01-11 14:50:10 +010022 for (unsigned value = 0; value < 64; value++) {
23 mbedtls_test_set_step(value);
24 TEST_CF_SECRET(&value, sizeof(value));
25 unsigned char digit = mbedtls_ct_base64_enc_char(value);
26 TEST_CF_PUBLIC(&value, sizeof(value));
27 TEST_CF_PUBLIC(&digit, sizeof(digit));
28 TEST_EQUAL(digit, base64_digits[value]);
Gilles Peskinea64417a2021-08-03 12:38:55 +020029 }
30}
31/* END_CASE */
32
33/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
Gilles Peskine449bd832023-01-11 14:50:10 +010034void dec_chars()
Gilles Peskinea64417a2021-08-03 12:38:55 +020035{
36 char *p;
Gilles Peskinea64417a2021-08-03 12:38:55 +020037 signed char expected;
38
Gilles Peskine449bd832023-01-11 14:50:10 +010039 for (unsigned c = 0; c <= 0xff; c++) {
40 mbedtls_test_set_step(c);
Gilles Peskine93365a72021-08-06 16:54:22 +020041 /* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +010042 p = memchr(base64_digits, c, sizeof(base64_digits) - 1);
43 if (p == NULL) {
Gilles Peskinea64417a2021-08-03 12:38:55 +020044 expected = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +010045 } else {
Gilles Peskine93365a72021-08-06 16:54:22 +020046 expected = p - base64_digits;
Gilles Peskine449bd832023-01-11 14:50:10 +010047 }
48 TEST_CF_SECRET(&c, sizeof(c));
49 signed char actual = mbedtls_ct_base64_dec_value(c);
50 TEST_CF_PUBLIC(&c, sizeof(c));
51 TEST_CF_PUBLIC(&actual, sizeof(actual));
52 TEST_EQUAL(actual, expected);
Gilles Peskinea64417a2021-08-03 12:38:55 +020053 }
54}
55/* END_CASE */
56
Paul Bakker33b43f12013-08-20 11:48:36 +020057/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010058void mbedtls_base64_encode(char *src_string, char *dst_string,
59 int dst_buf_size, int result)
Paul Bakker367dae42009-06-28 21:50:27 +000060{
61 unsigned char src_str[1000];
62 unsigned char dst_str[1000];
Paul Elliott448d5462021-02-24 15:32:42 +000063 size_t len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000064
65 memset(src_str, 0x00, 1000);
66 memset(dst_str, 0x00, 1000);
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068 strncpy((char *) src_str, src_string, sizeof(src_str) - 1);
69 src_len = strlen((char *) src_str);
Paul Elliott448d5462021-02-24 15:32:42 +000070
Gilles Peskine449bd832023-01-11 14:50:10 +010071 TEST_CF_SECRET(src_str, sizeof(src_str));
72 TEST_ASSERT(mbedtls_base64_encode(dst_str, dst_buf_size, &len, src_str, src_len) == result);
73 TEST_CF_PUBLIC(src_str, sizeof(src_str));
Paul Elliott448d5462021-02-24 15:32:42 +000074
Paul Elliottc48cb802021-03-02 22:48:40 +000075 /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
76 CF failures by unmarking it. */
Gilles Peskine449bd832023-01-11 14:50:10 +010077 TEST_CF_PUBLIC(dst_str, len);
Paul Elliottc48cb802021-03-02 22:48:40 +000078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 if (result == 0) {
80 TEST_ASSERT(strcmp((char *) dst_str, dst_string) == 0);
Paul Bakker5946fd92009-07-11 15:29:30 +000081 }
Paul Bakker367dae42009-06-28 21:50:27 +000082}
Paul Bakker33b43f12013-08-20 11:48:36 +020083/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000084
Paul Bakker33b43f12013-08-20 11:48:36 +020085/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010086void mbedtls_base64_decode(char *src_string, char *dst_string, int result)
Paul Bakker367dae42009-06-28 21:50:27 +000087{
Gilles Peskine715bbf32025-06-03 22:00:58 +020088 unsigned char *src = NULL;
89 size_t src_len = strlen(src_string);
90 unsigned char *dst = NULL;
91 size_t correct_dst_len = strlen(dst_string);
92 size_t dst_size = correct_dst_len;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010093 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +000094
Gilles Peskinee7ed8c42025-06-05 16:00:27 +020095 /* Allocate exactly the size of the input, to ensure there's no buffer
96 * overread in builds with ASan. (src_string has at least one extra null
97 * character at the end.) */
Gilles Peskine715bbf32025-06-03 22:00:58 +020098 TEST_CALLOC(src, src_len);
99 if (src_len != 0) {
100 memcpy(src, src_string, src_len);
Paul Bakker5946fd92009-07-11 15:29:30 +0000101 }
Gilles Peskine715bbf32025-06-03 22:00:58 +0200102
Gilles Peskinee7ed8c42025-06-05 16:00:27 +0200103 /* Allocate exactly the size of the input, to ensure there's no buffer
104 * overflow in builds with ASan. */
Gilles Peskine715bbf32025-06-03 22:00:58 +0200105 TEST_CALLOC(dst, dst_size);
106
Gilles Peskine715bbf32025-06-03 22:00:58 +0200107 /* Test normal operation */
108 TEST_EQUAL(mbedtls_base64_decode(dst, dst_size, &len,
109 src, src_len),
110 result);
111 if (result == 0) {
Gilles Peskine2b3d6a82025-06-04 11:22:25 +0200112 TEST_MEMORY_COMPARE(dst_string, correct_dst_len, dst, len);
Gilles Peskine715bbf32025-06-03 22:00:58 +0200113 }
114
115 /* Test an output buffer that's one byte too small */
116 if (result == 0 && dst_size != 0) {
117 mbedtls_free(dst);
118 dst = NULL;
Gilles Peskine03303d82025-06-11 18:24:26 +0200119 TEST_CALLOC(dst, dst_size - 1);
120 TEST_EQUAL(mbedtls_base64_decode(dst, dst_size - 1, &len,
Gilles Peskine715bbf32025-06-03 22:00:58 +0200121 src, src_len),
122 MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
123 TEST_EQUAL(correct_dst_len, len);
124 }
125
Gilles Peskine51dccfb2025-06-11 18:47:31 +0200126 /* Test an empty output buffer. `mbedtls_base64_decode()` must return
127 * `BUFFER_TOO_SMALL` but report the correct output length.
128 * Skip this when dst_size==0 since that would be a valid call to
129 * `mbedtls_base64_decode()` which should return 0.
130 */
Gilles Peskine715bbf32025-06-03 22:00:58 +0200131 if (result == 0 && dst_size != 0) {
132 TEST_EQUAL(mbedtls_base64_decode(NULL, 0, &len,
133 src, src_len),
134 MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
135 TEST_EQUAL(correct_dst_len, len);
136 }
137
Gilles Peskine13cc0c22025-06-05 16:02:55 +0200138 /* Test dst=NULL with dlen!=0 (explicitly documented as supported) */
139 if (result == 0 && dst_size != 0) {
140 TEST_EQUAL(mbedtls_base64_decode(NULL, 42, &len,
141 src, src_len),
142 MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
143 TEST_EQUAL(correct_dst_len, len);
144 }
145
Gilles Peskine715bbf32025-06-03 22:00:58 +0200146exit:
147 mbedtls_free(src);
148 mbedtls_free(dst);
Paul Bakker367dae42009-06-28 21:50:27 +0000149}
Paul Bakker33b43f12013-08-20 11:48:36 +0200150/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +0000151
Paul Bakkerd5983182014-07-04 13:50:31 +0200152/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153void base64_encode_hex(data_t *src, char *dst, int dst_buf_size,
154 int result)
Paul Bakkerd5983182014-07-04 13:50:31 +0200155{
Azim Khand30ca132017-06-09 04:32:58 +0100156 unsigned char *res = NULL;
157 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 res = mbedtls_test_zero_alloc(dst_buf_size);
Paul Bakkerd5983182014-07-04 13:50:31 +0200160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 TEST_CF_SECRET(src->x, src->len);
162 TEST_ASSERT(mbedtls_base64_encode(res, dst_buf_size, &len, src->x, src->len) == result);
163 TEST_CF_PUBLIC(src->x, src->len);
Paul Elliott448d5462021-02-24 15:32:42 +0000164
Paul Elliottc48cb802021-03-02 22:48:40 +0000165 /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
166 CF failures by unmarking it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 TEST_CF_PUBLIC(res, len);
Paul Elliottc48cb802021-03-02 22:48:40 +0000168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (result == 0) {
170 TEST_ASSERT(len == strlen(dst));
171 TEST_ASSERT(memcmp(dst, res, len) == 0);
Paul Bakkerd5983182014-07-04 13:50:31 +0200172 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200173
Paul Bakkerbd51b262014-07-10 15:26:12 +0200174exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 mbedtls_free(res);
Paul Bakkerd5983182014-07-04 13:50:31 +0200176}
177/* END_CASE */
178
179/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180void base64_decode_hex(char *src, data_t *dst, int dst_buf_size,
181 int result)
Paul Bakkerd5983182014-07-04 13:50:31 +0200182{
Azim Khand30ca132017-06-09 04:32:58 +0100183 unsigned char *res = NULL;
184 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 res = mbedtls_test_zero_alloc(dst_buf_size);
Paul Bakkerd5983182014-07-04 13:50:31 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 TEST_ASSERT(mbedtls_base64_decode(res, dst_buf_size, &len, (unsigned char *) src,
189 strlen(src)) == result);
190 if (result == 0) {
191 TEST_ASSERT(len == dst->len);
192 TEST_ASSERT(memcmp(dst->x, res, len) == 0);
Paul Bakkerd5983182014-07-04 13:50:31 +0200193 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200194
Paul Bakkerbd51b262014-07-10 15:26:12 +0200195exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 mbedtls_free(res);
Paul Bakkerd5983182014-07-04 13:50:31 +0200197}
198/* END_CASE */
199
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200200/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201void base64_decode_hex_src(data_t *src, char *dst_ref, int result)
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200202{
203 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100204 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 TEST_ASSERT(mbedtls_base64_decode(dst, sizeof(dst), &len, src->x, src->len) == result);
207 if (result == 0) {
208 TEST_ASSERT(len == strlen(dst_ref));
209 TEST_ASSERT(memcmp(dst, dst_ref, len) == 0);
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200210 }
211
212exit:
Azim Khand30ca132017-06-09 04:32:58 +0100213 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200214}
215/* END_CASE */
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218void base64_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000219{
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 TEST_ASSERT(mbedtls_base64_self_test(1) == 0);
Paul Bakker3d360822009-07-05 11:29:38 +0000221}
Paul Bakker33b43f12013-08-20 11:48:36 +0200222/* END_CASE */