blob: 5d8ed9bf9ff0514f040eb739ab35d35c6209aeaf [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;
119 dst_size -= 1;
120 TEST_CALLOC(dst, dst_size);
121 TEST_EQUAL(mbedtls_base64_decode(dst, dst_size, &len,
122 src, src_len),
123 MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
124 TEST_EQUAL(correct_dst_len, len);
125 }
126
127 /* Test an empty output buffer */
128 if (result == 0 && dst_size != 0) {
129 TEST_EQUAL(mbedtls_base64_decode(NULL, 0, &len,
130 src, src_len),
131 MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
132 TEST_EQUAL(correct_dst_len, len);
133 }
134
Gilles Peskine13cc0c22025-06-05 16:02:55 +0200135 /* Test dst=NULL with dlen!=0 (explicitly documented as supported) */
136 if (result == 0 && dst_size != 0) {
137 TEST_EQUAL(mbedtls_base64_decode(NULL, 42, &len,
138 src, src_len),
139 MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
140 TEST_EQUAL(correct_dst_len, len);
141 }
142
Gilles Peskine715bbf32025-06-03 22:00:58 +0200143exit:
144 mbedtls_free(src);
145 mbedtls_free(dst);
Paul Bakker367dae42009-06-28 21:50:27 +0000146}
Paul Bakker33b43f12013-08-20 11:48:36 +0200147/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +0000148
Paul Bakkerd5983182014-07-04 13:50:31 +0200149/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150void base64_encode_hex(data_t *src, char *dst, int dst_buf_size,
151 int result)
Paul Bakkerd5983182014-07-04 13:50:31 +0200152{
Azim Khand30ca132017-06-09 04:32:58 +0100153 unsigned char *res = NULL;
154 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 res = mbedtls_test_zero_alloc(dst_buf_size);
Paul Bakkerd5983182014-07-04 13:50:31 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 TEST_CF_SECRET(src->x, src->len);
159 TEST_ASSERT(mbedtls_base64_encode(res, dst_buf_size, &len, src->x, src->len) == result);
160 TEST_CF_PUBLIC(src->x, src->len);
Paul Elliott448d5462021-02-24 15:32:42 +0000161
Paul Elliottc48cb802021-03-02 22:48:40 +0000162 /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
163 CF failures by unmarking it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 TEST_CF_PUBLIC(res, len);
Paul Elliottc48cb802021-03-02 22:48:40 +0000165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (result == 0) {
167 TEST_ASSERT(len == strlen(dst));
168 TEST_ASSERT(memcmp(dst, res, len) == 0);
Paul Bakkerd5983182014-07-04 13:50:31 +0200169 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200170
Paul Bakkerbd51b262014-07-10 15:26:12 +0200171exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 mbedtls_free(res);
Paul Bakkerd5983182014-07-04 13:50:31 +0200173}
174/* END_CASE */
175
176/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177void base64_decode_hex(char *src, data_t *dst, int dst_buf_size,
178 int result)
Paul Bakkerd5983182014-07-04 13:50:31 +0200179{
Azim Khand30ca132017-06-09 04:32:58 +0100180 unsigned char *res = NULL;
181 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 res = mbedtls_test_zero_alloc(dst_buf_size);
Paul Bakkerd5983182014-07-04 13:50:31 +0200184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 TEST_ASSERT(mbedtls_base64_decode(res, dst_buf_size, &len, (unsigned char *) src,
186 strlen(src)) == result);
187 if (result == 0) {
188 TEST_ASSERT(len == dst->len);
189 TEST_ASSERT(memcmp(dst->x, res, len) == 0);
Paul Bakkerd5983182014-07-04 13:50:31 +0200190 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200191
Paul Bakkerbd51b262014-07-10 15:26:12 +0200192exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_free(res);
Paul Bakkerd5983182014-07-04 13:50:31 +0200194}
195/* END_CASE */
196
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200197/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198void base64_decode_hex_src(data_t *src, char *dst_ref, int result)
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200199{
200 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100201 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 TEST_ASSERT(mbedtls_base64_decode(dst, sizeof(dst), &len, src->x, src->len) == result);
204 if (result == 0) {
205 TEST_ASSERT(len == strlen(dst_ref));
206 TEST_ASSERT(memcmp(dst, dst_ref, len) == 0);
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200207 }
208
209exit:
Azim Khand30ca132017-06-09 04:32:58 +0100210 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200211}
212/* END_CASE */
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215void base64_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000216{
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 TEST_ASSERT(mbedtls_base64_self_test(1) == 0);
Paul Bakker3d360822009-07-05 11:29:38 +0000218}
Paul Bakker33b43f12013-08-20 11:48:36 +0200219/* END_CASE */