blob: e351ad8a25bec0114e60f67f721a4ca27dad0a7b [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"
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 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{
88 unsigned char src_str[1000];
89 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010090 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +020091 int res;
Paul Bakker367dae42009-06-28 21:50:27 +000092
93 memset(src_str, 0x00, 1000);
94 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +020095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 strncpy((char *) src_str, src_string, sizeof(src_str) - 1);
97 res = mbedtls_base64_decode(dst_str, sizeof(dst_str), &len, src_str, strlen((char *) src_str));
98 TEST_ASSERT(res == result);
99 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 Bakker3d360822009-07-05 11:29:38 +0000104
Paul Bakkerd5983182014-07-04 13:50:31 +0200105/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106void base64_encode_hex(data_t *src, char *dst, int dst_buf_size,
107 int result)
Paul Bakkerd5983182014-07-04 13:50:31 +0200108{
Azim Khand30ca132017-06-09 04:32:58 +0100109 unsigned char *res = NULL;
110 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 res = mbedtls_test_zero_alloc(dst_buf_size);
Paul Bakkerd5983182014-07-04 13:50:31 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 TEST_CF_SECRET(src->x, src->len);
115 TEST_ASSERT(mbedtls_base64_encode(res, dst_buf_size, &len, src->x, src->len) == result);
116 TEST_CF_PUBLIC(src->x, src->len);
Paul Elliott448d5462021-02-24 15:32:42 +0000117
Paul Elliottc48cb802021-03-02 22:48:40 +0000118 /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
119 CF failures by unmarking it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 TEST_CF_PUBLIC(res, len);
Paul Elliottc48cb802021-03-02 22:48:40 +0000121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (result == 0) {
123 TEST_ASSERT(len == strlen(dst));
124 TEST_ASSERT(memcmp(dst, res, len) == 0);
Paul Bakkerd5983182014-07-04 13:50:31 +0200125 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200126
Paul Bakkerbd51b262014-07-10 15:26:12 +0200127exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_free(res);
Paul Bakkerd5983182014-07-04 13:50:31 +0200129}
130/* END_CASE */
131
132/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133void base64_decode_hex(char *src, data_t *dst, int dst_buf_size,
134 int result)
Paul Bakkerd5983182014-07-04 13:50:31 +0200135{
Azim Khand30ca132017-06-09 04:32:58 +0100136 unsigned char *res = NULL;
137 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 res = mbedtls_test_zero_alloc(dst_buf_size);
Paul Bakkerd5983182014-07-04 13:50:31 +0200140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 TEST_ASSERT(mbedtls_base64_decode(res, dst_buf_size, &len, (unsigned char *) src,
142 strlen(src)) == result);
143 if (result == 0) {
144 TEST_ASSERT(len == dst->len);
145 TEST_ASSERT(memcmp(dst->x, res, len) == 0);
Paul Bakkerd5983182014-07-04 13:50:31 +0200146 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200147
Paul Bakkerbd51b262014-07-10 15:26:12 +0200148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 mbedtls_free(res);
Paul Bakkerd5983182014-07-04 13:50:31 +0200150}
151/* END_CASE */
152
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200153/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154void base64_decode_hex_src(data_t *src, char *dst_ref, int result)
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200155{
156 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100157 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 TEST_ASSERT(mbedtls_base64_decode(dst, sizeof(dst), &len, src->x, src->len) == result);
160 if (result == 0) {
161 TEST_ASSERT(len == strlen(dst_ref));
162 TEST_ASSERT(memcmp(dst, dst_ref, len) == 0);
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200163 }
164
165exit:
Azim Khand30ca132017-06-09 04:32:58 +0100166 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200167}
168/* END_CASE */
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171void base64_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000172{
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 TEST_ASSERT(mbedtls_base64_self_test(1) == 0);
Paul Bakker3d360822009-07-05 11:29:38 +0000174}
Paul Bakker33b43f12013-08-20 11:48:36 +0200175/* END_CASE */