blob: 052d1d097bb9351f9b9acc4511114ad4c7f756d9 [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"
Paul Elliott448d5462021-02-24 15:32:42 +00004#include <test/constant_flow.h>
Gilles Peskineba951f52021-08-06 14:55:55 +02005
6#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine93365a72021-08-06 16:54:22 +02007static const char base64_digits[] =
Gilles Peskineba951f52021-08-06 14:55:55 +02008 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
9#endif /* MBEDTLS_TEST_HOOKS */
10
Paul Bakker33b43f12013-08-20 11:48:36 +020011/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014 * depends_on:MBEDTLS_BASE64_C
Paul Bakker33b43f12013-08-20 11:48:36 +020015 * END_DEPENDENCIES
16 */
Paul Bakker5690efc2011-05-26 13:16:06 +000017
Gilles Peskinea64417a2021-08-03 12:38:55 +020018/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
Gilles Peskine449bd832023-01-11 14:50:10 +010019void enc_chars()
Gilles Peskinea64417a2021-08-03 12:38:55 +020020{
Gilles Peskine449bd832023-01-11 14:50:10 +010021 for (unsigned value = 0; value < 64; value++) {
22 mbedtls_test_set_step(value);
23 TEST_CF_SECRET(&value, sizeof(value));
24 unsigned char digit = mbedtls_ct_base64_enc_char(value);
25 TEST_CF_PUBLIC(&value, sizeof(value));
26 TEST_CF_PUBLIC(&digit, sizeof(digit));
27 TEST_EQUAL(digit, base64_digits[value]);
Gilles Peskinea64417a2021-08-03 12:38:55 +020028 }
29}
30/* END_CASE */
31
32/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
Gilles Peskine449bd832023-01-11 14:50:10 +010033void dec_chars()
Gilles Peskinea64417a2021-08-03 12:38:55 +020034{
35 char *p;
Gilles Peskinea64417a2021-08-03 12:38:55 +020036 signed char expected;
37
Gilles Peskine449bd832023-01-11 14:50:10 +010038 for (unsigned c = 0; c <= 0xff; c++) {
39 mbedtls_test_set_step(c);
Gilles Peskine93365a72021-08-06 16:54:22 +020040 /* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +010041 p = memchr(base64_digits, c, sizeof(base64_digits) - 1);
42 if (p == NULL) {
Gilles Peskinea64417a2021-08-03 12:38:55 +020043 expected = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +010044 } else {
Gilles Peskine93365a72021-08-06 16:54:22 +020045 expected = p - base64_digits;
Gilles Peskine449bd832023-01-11 14:50:10 +010046 }
47 TEST_CF_SECRET(&c, sizeof(c));
48 signed char actual = mbedtls_ct_base64_dec_value(c);
49 TEST_CF_PUBLIC(&c, sizeof(c));
50 TEST_CF_PUBLIC(&actual, sizeof(actual));
51 TEST_EQUAL(actual, expected);
Gilles Peskinea64417a2021-08-03 12:38:55 +020052 }
53}
54/* END_CASE */
55
Paul Bakker33b43f12013-08-20 11:48:36 +020056/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010057void mbedtls_base64_encode(char *src_string, char *dst_string,
58 int dst_buf_size, int result)
Paul Bakker367dae42009-06-28 21:50:27 +000059{
60 unsigned char src_str[1000];
61 unsigned char dst_str[1000];
Paul Elliott448d5462021-02-24 15:32:42 +000062 size_t len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000063
64 memset(src_str, 0x00, 1000);
65 memset(dst_str, 0x00, 1000);
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067 strncpy((char *) src_str, src_string, sizeof(src_str) - 1);
68 src_len = strlen((char *) src_str);
Paul Elliott448d5462021-02-24 15:32:42 +000069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 TEST_CF_SECRET(src_str, sizeof(src_str));
71 TEST_ASSERT(mbedtls_base64_encode(dst_str, dst_buf_size, &len, src_str, src_len) == result);
72 TEST_CF_PUBLIC(src_str, sizeof(src_str));
Paul Elliott448d5462021-02-24 15:32:42 +000073
Paul Elliottc48cb802021-03-02 22:48:40 +000074 /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
75 CF failures by unmarking it. */
Gilles Peskine449bd832023-01-11 14:50:10 +010076 TEST_CF_PUBLIC(dst_str, len);
Paul Elliottc48cb802021-03-02 22:48:40 +000077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 if (result == 0) {
79 TEST_ASSERT(strcmp((char *) dst_str, dst_string) == 0);
Paul Bakker5946fd92009-07-11 15:29:30 +000080 }
Paul Bakker367dae42009-06-28 21:50:27 +000081}
Paul Bakker33b43f12013-08-20 11:48:36 +020082/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000083
Paul Bakker33b43f12013-08-20 11:48:36 +020084/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010085void mbedtls_base64_decode(char *src_string, char *dst_string, int result)
Paul Bakker367dae42009-06-28 21:50:27 +000086{
87 unsigned char src_str[1000];
88 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010089 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +020090 int res;
Paul Bakker367dae42009-06-28 21:50:27 +000091
92 memset(src_str, 0x00, 1000);
93 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +020094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 strncpy((char *) src_str, src_string, sizeof(src_str) - 1);
96 res = mbedtls_base64_decode(dst_str, sizeof(dst_str), &len, src_str, strlen((char *) src_str));
97 TEST_ASSERT(res == result);
98 if (result == 0) {
99 TEST_ASSERT(strcmp((char *) dst_str, dst_string) == 0);
Paul Bakker5946fd92009-07-11 15:29:30 +0000100 }
Paul Bakker367dae42009-06-28 21:50:27 +0000101}
Paul Bakker33b43f12013-08-20 11:48:36 +0200102/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +0000103
Paul Bakkerd5983182014-07-04 13:50:31 +0200104/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105void base64_encode_hex(data_t *src, char *dst, int dst_buf_size,
106 int result)
Paul Bakkerd5983182014-07-04 13:50:31 +0200107{
Azim Khand30ca132017-06-09 04:32:58 +0100108 unsigned char *res = NULL;
109 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 res = mbedtls_test_zero_alloc(dst_buf_size);
Paul Bakkerd5983182014-07-04 13:50:31 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 TEST_CF_SECRET(src->x, src->len);
114 TEST_ASSERT(mbedtls_base64_encode(res, dst_buf_size, &len, src->x, src->len) == result);
115 TEST_CF_PUBLIC(src->x, src->len);
Paul Elliott448d5462021-02-24 15:32:42 +0000116
Paul Elliottc48cb802021-03-02 22:48:40 +0000117 /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
118 CF failures by unmarking it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 TEST_CF_PUBLIC(res, len);
Paul Elliottc48cb802021-03-02 22:48:40 +0000120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (result == 0) {
122 TEST_ASSERT(len == strlen(dst));
123 TEST_ASSERT(memcmp(dst, res, len) == 0);
Paul Bakkerd5983182014-07-04 13:50:31 +0200124 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200125
Paul Bakkerbd51b262014-07-10 15:26:12 +0200126exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 mbedtls_free(res);
Paul Bakkerd5983182014-07-04 13:50:31 +0200128}
129/* END_CASE */
130
131/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132void base64_decode_hex(char *src, data_t *dst, int dst_buf_size,
133 int result)
Paul Bakkerd5983182014-07-04 13:50:31 +0200134{
Azim Khand30ca132017-06-09 04:32:58 +0100135 unsigned char *res = NULL;
136 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 res = mbedtls_test_zero_alloc(dst_buf_size);
Paul Bakkerd5983182014-07-04 13:50:31 +0200139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 TEST_ASSERT(mbedtls_base64_decode(res, dst_buf_size, &len, (unsigned char *) src,
141 strlen(src)) == result);
142 if (result == 0) {
143 TEST_ASSERT(len == dst->len);
144 TEST_ASSERT(memcmp(dst->x, 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
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200152/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153void base64_decode_hex_src(data_t *src, char *dst_ref, int result)
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200154{
155 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100156 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 TEST_ASSERT(mbedtls_base64_decode(dst, sizeof(dst), &len, src->x, src->len) == result);
159 if (result == 0) {
160 TEST_ASSERT(len == strlen(dst_ref));
161 TEST_ASSERT(memcmp(dst, dst_ref, len) == 0);
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200162 }
163
164exit:
Azim Khand30ca132017-06-09 04:32:58 +0100165 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200166}
167/* END_CASE */
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100170void base64_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000171{
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 TEST_ASSERT(mbedtls_base64_self_test(1) == 0);
Paul Bakker3d360822009-07-05 11:29:38 +0000173}
Paul Bakker33b43f12013-08-20 11:48:36 +0200174/* END_CASE */