blob: ce6bd42b7e8d7e4e6de889d7cf226dc835e53740 [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"
Gabor Mezei28d61152021-11-15 16:13:01 +01003#include "constant_time_internal.h"
4#include "constant_time_invasive.h"
Paul Elliott448d5462021-02-24 15:32:42 +00005#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 mask_of_range(int low_arg, int high_arg)
Gilles Peskinea64417a2021-08-03 12:38:55 +020021{
22 unsigned char low = low_arg, high = high_arg;
23 unsigned c;
Gilles Peskine449bd832023-01-11 14:50:10 +010024 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 }
Gilles Peskinea64417a2021-08-03 12:38:55 +020035 }
36}
37/* END_CASE */
38
39/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
Gilles Peskine449bd832023-01-11 14:50:10 +010040void enc_chars()
Gilles Peskinea64417a2021-08-03 12:38:55 +020041{
Gilles Peskine449bd832023-01-11 14:50:10 +010042 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]);
Gilles Peskinea64417a2021-08-03 12:38:55 +020049 }
50}
51/* END_CASE */
52
53/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
Gilles Peskine449bd832023-01-11 14:50:10 +010054void dec_chars()
Gilles Peskinea64417a2021-08-03 12:38:55 +020055{
56 char *p;
Gilles Peskinea64417a2021-08-03 12:38:55 +020057 signed char expected;
58
Gilles Peskine449bd832023-01-11 14:50:10 +010059 for (unsigned c = 0; c <= 0xff; c++) {
60 mbedtls_test_set_step(c);
Gilles Peskine93365a72021-08-06 16:54:22 +020061 /* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +010062 p = memchr(base64_digits, c, sizeof(base64_digits) - 1);
63 if (p == NULL) {
Gilles Peskinea64417a2021-08-03 12:38:55 +020064 expected = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +010065 } else {
Gilles Peskine93365a72021-08-06 16:54:22 +020066 expected = p - base64_digits;
Gilles Peskine449bd832023-01-11 14:50:10 +010067 }
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);
Gilles Peskinea64417a2021-08-03 12:38:55 +020073 }
74}
75/* END_CASE */
76
Paul Bakker33b43f12013-08-20 11:48:36 +020077/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010078void mbedtls_base64_encode(char *src_string, char *dst_string,
79 int dst_buf_size, int result)
Paul Bakker367dae42009-06-28 21:50:27 +000080{
81 unsigned char src_str[1000];
82 unsigned char dst_str[1000];
Paul Elliott448d5462021-02-24 15:32:42 +000083 size_t len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000084
85 memset(src_str, 0x00, 1000);
86 memset(dst_str, 0x00, 1000);
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 strncpy((char *) src_str, src_string, sizeof(src_str) - 1);
89 src_len = strlen((char *) src_str);
Paul Elliott448d5462021-02-24 15:32:42 +000090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 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));
Paul Elliott448d5462021-02-24 15:32:42 +000094
Paul Elliottc48cb802021-03-02 22:48:40 +000095 /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
96 CF failures by unmarking it. */
Gilles Peskine449bd832023-01-11 14:50:10 +010097 TEST_CF_PUBLIC(dst_str, len);
Paul Elliottc48cb802021-03-02 22:48:40 +000098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (result == 0) {
100 TEST_ASSERT(strcmp((char *) dst_str, dst_string) == 0);
Paul Bakker5946fd92009-07-11 15:29:30 +0000101 }
Paul Bakker367dae42009-06-28 21:50:27 +0000102}
Paul Bakker33b43f12013-08-20 11:48:36 +0200103/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000104
Paul Bakker33b43f12013-08-20 11:48:36 +0200105/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106void mbedtls_base64_decode(char *src_string, char *dst_string, int result)
Paul Bakker367dae42009-06-28 21:50:27 +0000107{
108 unsigned char src_str[1000];
109 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100110 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200111 int res;
Paul Bakker367dae42009-06-28 21:50:27 +0000112
113 memset(src_str, 0x00, 1000);
114 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 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);
Paul Bakker5946fd92009-07-11 15:29:30 +0000121 }
Paul Bakker367dae42009-06-28 21:50:27 +0000122}
Paul Bakker33b43f12013-08-20 11:48:36 +0200123/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +0000124
Paul Bakkerd5983182014-07-04 13:50:31 +0200125/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126void base64_encode_hex(data_t *src, char *dst, int dst_buf_size,
127 int result)
Paul Bakkerd5983182014-07-04 13:50:31 +0200128{
Azim Khand30ca132017-06-09 04:32:58 +0100129 unsigned char *res = NULL;
130 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 res = mbedtls_test_zero_alloc(dst_buf_size);
Paul Bakkerd5983182014-07-04 13:50:31 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 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);
Paul Elliott448d5462021-02-24 15:32:42 +0000137
Paul Elliottc48cb802021-03-02 22:48:40 +0000138 /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
139 CF failures by unmarking it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 TEST_CF_PUBLIC(res, len);
Paul Elliottc48cb802021-03-02 22:48:40 +0000141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (result == 0) {
143 TEST_ASSERT(len == strlen(dst));
144 TEST_ASSERT(memcmp(dst, res, len) == 0);
Paul Bakkerd5983182014-07-04 13:50:31 +0200145 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200146
Paul Bakkerbd51b262014-07-10 15:26:12 +0200147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_free(res);
Paul Bakkerd5983182014-07-04 13:50:31 +0200149}
150/* END_CASE */
151
152/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153void base64_decode_hex(char *src, data_t *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_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);
Paul Bakkerd5983182014-07-04 13:50:31 +0200166 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200167
Paul Bakkerbd51b262014-07-10 15:26:12 +0200168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 mbedtls_free(res);
Paul Bakkerd5983182014-07-04 13:50:31 +0200170}
171/* END_CASE */
172
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200173/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174void base64_decode_hex_src(data_t *src, char *dst_ref, int result)
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200175{
176 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100177 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 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);
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200183 }
184
185exit:
Azim Khand30ca132017-06-09 04:32:58 +0100186 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200187}
188/* END_CASE */
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191void base64_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000192{
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 TEST_ASSERT(mbedtls_base64_self_test(1) == 0);
Paul Bakker3d360822009-07-05 11:29:38 +0000194}
Paul Bakker33b43f12013-08-20 11:48:36 +0200195/* END_CASE */