Tom Rini | 0344c60 | 2024-10-08 13:56:50 -0600 | [diff] [blame^] | 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_str[1000]; |
| 89 | unsigned char dst_str[1000]; |
| 90 | size_t len; |
| 91 | int res; |
| 92 | |
| 93 | memset(src_str, 0x00, 1000); |
| 94 | memset(dst_str, 0x00, 1000); |
| 95 | |
| 96 | 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); |
| 101 | } |
| 102 | } |
| 103 | /* END_CASE */ |
| 104 | |
| 105 | /* BEGIN_CASE */ |
| 106 | void base64_encode_hex(data_t *src, char *dst, int dst_buf_size, |
| 107 | int result) |
| 108 | { |
| 109 | unsigned char *res = NULL; |
| 110 | size_t len; |
| 111 | |
| 112 | res = mbedtls_test_zero_alloc(dst_buf_size); |
| 113 | |
| 114 | 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); |
| 117 | |
| 118 | /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering |
| 119 | CF failures by unmarking it. */ |
| 120 | TEST_CF_PUBLIC(res, len); |
| 121 | |
| 122 | if (result == 0) { |
| 123 | TEST_ASSERT(len == strlen(dst)); |
| 124 | TEST_ASSERT(memcmp(dst, res, len) == 0); |
| 125 | } |
| 126 | |
| 127 | exit: |
| 128 | mbedtls_free(res); |
| 129 | } |
| 130 | /* END_CASE */ |
| 131 | |
| 132 | /* BEGIN_CASE */ |
| 133 | void base64_decode_hex(char *src, data_t *dst, int dst_buf_size, |
| 134 | int result) |
| 135 | { |
| 136 | unsigned char *res = NULL; |
| 137 | size_t len; |
| 138 | |
| 139 | res = mbedtls_test_zero_alloc(dst_buf_size); |
| 140 | |
| 141 | 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); |
| 146 | } |
| 147 | |
| 148 | exit: |
| 149 | mbedtls_free(res); |
| 150 | } |
| 151 | /* END_CASE */ |
| 152 | |
| 153 | /* BEGIN_CASE */ |
| 154 | void base64_decode_hex_src(data_t *src, char *dst_ref, int result) |
| 155 | { |
| 156 | unsigned char dst[1000] = { 0 }; |
| 157 | size_t len; |
| 158 | |
| 159 | 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); |
| 163 | } |
| 164 | |
| 165 | exit: |
| 166 | ;; |
| 167 | } |
| 168 | /* END_CASE */ |
| 169 | |
| 170 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
| 171 | void base64_selftest() |
| 172 | { |
| 173 | TEST_ASSERT(mbedtls_base64_self_test(1) == 0); |
| 174 | } |
| 175 | /* END_CASE */ |