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