1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/* BEGIN_HEADER */
#include "mbedtls/base64.h"
#include "base64_internal.h"
#include "constant_time_internal.h"
#include <test/constant_flow.h>
#if defined(MBEDTLS_TEST_HOOKS)
static const char base64_digits[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#endif /* MBEDTLS_TEST_HOOKS */
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_BASE64_C
* END_DEPENDENCIES
*/
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
void enc_chars()
{
for (unsigned value = 0; value < 64; value++) {
mbedtls_test_set_step(value);
TEST_CF_SECRET(&value, sizeof(value));
unsigned char digit = mbedtls_ct_base64_enc_char(value);
TEST_CF_PUBLIC(&value, sizeof(value));
TEST_CF_PUBLIC(&digit, sizeof(digit));
TEST_EQUAL(digit, base64_digits[value]);
}
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
void dec_chars()
{
char *p;
signed char expected;
for (unsigned c = 0; c <= 0xff; c++) {
mbedtls_test_set_step(c);
/* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
p = memchr(base64_digits, c, sizeof(base64_digits) - 1);
if (p == NULL) {
expected = -1;
} else {
expected = p - base64_digits;
}
TEST_CF_SECRET(&c, sizeof(c));
signed char actual = mbedtls_ct_base64_dec_value(c);
TEST_CF_PUBLIC(&c, sizeof(c));
TEST_CF_PUBLIC(&actual, sizeof(actual));
TEST_EQUAL(actual, expected);
}
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_base64_encode(char *src_string, char *dst_string,
int dst_buf_size, int result)
{
unsigned char src_str[1000];
unsigned char dst_str[1000];
size_t len, src_len;
memset(src_str, 0x00, 1000);
memset(dst_str, 0x00, 1000);
strncpy((char *) src_str, src_string, sizeof(src_str) - 1);
src_len = strlen((char *) src_str);
TEST_CF_SECRET(src_str, sizeof(src_str));
TEST_ASSERT(mbedtls_base64_encode(dst_str, dst_buf_size, &len, src_str, src_len) == result);
TEST_CF_PUBLIC(src_str, sizeof(src_str));
/* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
CF failures by unmarking it. */
TEST_CF_PUBLIC(dst_str, len);
if (result == 0) {
TEST_ASSERT(strcmp((char *) dst_str, dst_string) == 0);
}
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_base64_decode(char *src_string, char *dst_string, int result)
{
unsigned char *src = NULL;
size_t src_len = strlen(src_string);
unsigned char *dst = NULL;
size_t correct_dst_len = strlen(dst_string);
size_t dst_size = correct_dst_len;
size_t len;
/* Allocate exactly the size of the input, to ensure there's no buffer
* overread in builds with ASan. (src_string has at least one extra null
* character at the end.) */
TEST_CALLOC(src, src_len);
if (src_len != 0) {
memcpy(src, src_string, src_len);
}
/* Allocate exactly the size of the input, to ensure there's no buffer
* overflow in builds with ASan. */
TEST_CALLOC(dst, dst_size);
/* Test normal operation */
TEST_EQUAL(mbedtls_base64_decode(dst, dst_size, &len,
src, src_len),
result);
if (result == 0) {
TEST_MEMORY_COMPARE(dst_string, correct_dst_len, dst, len);
}
/* Test an output buffer that's one byte too small */
if (result == 0 && dst_size != 0) {
mbedtls_free(dst);
dst = NULL;
TEST_CALLOC(dst, dst_size - 1);
TEST_EQUAL(mbedtls_base64_decode(dst, dst_size - 1, &len,
src, src_len),
MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
TEST_EQUAL(correct_dst_len, len);
}
/* Test an empty output buffer. `mbedtls_base64_decode()` must return
* `BUFFER_TOO_SMALL` but report the correct output length.
* Skip this when dst_size==0 since that would be a valid call to
* `mbedtls_base64_decode()` which should return 0.
*/
if (result == 0 && dst_size != 0) {
TEST_EQUAL(mbedtls_base64_decode(NULL, 0, &len,
src, src_len),
MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
TEST_EQUAL(correct_dst_len, len);
}
/* Test dst=NULL with dlen!=0 (explicitly documented as supported) */
if (result == 0 && dst_size != 0) {
TEST_EQUAL(mbedtls_base64_decode(NULL, 42, &len,
src, src_len),
MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
TEST_EQUAL(correct_dst_len, len);
}
exit:
mbedtls_free(src);
mbedtls_free(dst);
}
/* END_CASE */
/* BEGIN_CASE */
void base64_encode_hex(data_t *src, char *dst, int dst_buf_size,
int result)
{
unsigned char *res = NULL;
size_t len;
res = mbedtls_test_zero_alloc(dst_buf_size);
TEST_CF_SECRET(src->x, src->len);
TEST_ASSERT(mbedtls_base64_encode(res, dst_buf_size, &len, src->x, src->len) == result);
TEST_CF_PUBLIC(src->x, src->len);
/* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
CF failures by unmarking it. */
TEST_CF_PUBLIC(res, len);
if (result == 0) {
TEST_ASSERT(len == strlen(dst));
TEST_ASSERT(memcmp(dst, res, len) == 0);
}
exit:
mbedtls_free(res);
}
/* END_CASE */
/* BEGIN_CASE */
void base64_decode_hex(char *src, data_t *dst, int dst_buf_size,
int result)
{
unsigned char *res = NULL;
size_t len;
res = mbedtls_test_zero_alloc(dst_buf_size);
TEST_ASSERT(mbedtls_base64_decode(res, dst_buf_size, &len, (unsigned char *) src,
strlen(src)) == result);
if (result == 0) {
TEST_ASSERT(len == dst->len);
TEST_ASSERT(memcmp(dst->x, res, len) == 0);
}
exit:
mbedtls_free(res);
}
/* END_CASE */
/* BEGIN_CASE */
void base64_decode_hex_src(data_t *src, char *dst_ref, int result)
{
unsigned char dst[1000] = { 0 };
size_t len;
TEST_ASSERT(mbedtls_base64_decode(dst, sizeof(dst), &len, src->x, src->len) == result);
if (result == 0) {
TEST_ASSERT(len == strlen(dst_ref));
TEST_ASSERT(memcmp(dst, dst_ref, len) == 0);
}
exit:
;;
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
void base64_selftest()
{
TEST_ASSERT(mbedtls_base64_self_test(1) == 0);
}
/* END_CASE */
|