test_suite_base64.function 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/base64.h"
  3. #include "constant_time_internal.h"
  4. #include "constant_time_invasive.h"
  5. #include <test/constant_flow.h>
  6. #if defined(MBEDTLS_TEST_HOOKS)
  7. static const char base64_digits[] =
  8. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  9. #endif /* MBEDTLS_TEST_HOOKS */
  10. /* END_HEADER */
  11. /* BEGIN_DEPENDENCIES
  12. * depends_on:MBEDTLS_BASE64_C
  13. * END_DEPENDENCIES
  14. */
  15. /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
  16. void mask_of_range(int low_arg, int high_arg)
  17. {
  18. unsigned char low = low_arg, high = high_arg;
  19. unsigned c;
  20. for (c = 0; c <= 0xff; c++) {
  21. mbedtls_test_set_step(c);
  22. TEST_CF_SECRET(&c, sizeof(c));
  23. unsigned char m = mbedtls_ct_uchar_mask_of_range(low, high, c);
  24. TEST_CF_PUBLIC(&c, sizeof(c));
  25. TEST_CF_PUBLIC(&m, sizeof(m));
  26. if (low <= c && c <= high) {
  27. TEST_EQUAL(m, 0xff);
  28. } else {
  29. TEST_EQUAL(m, 0);
  30. }
  31. }
  32. }
  33. /* END_CASE */
  34. /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
  35. void enc_chars()
  36. {
  37. for (unsigned value = 0; value < 64; value++) {
  38. mbedtls_test_set_step(value);
  39. TEST_CF_SECRET(&value, sizeof(value));
  40. unsigned char digit = mbedtls_ct_base64_enc_char(value);
  41. TEST_CF_PUBLIC(&value, sizeof(value));
  42. TEST_CF_PUBLIC(&digit, sizeof(digit));
  43. TEST_EQUAL(digit, base64_digits[value]);
  44. }
  45. }
  46. /* END_CASE */
  47. /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
  48. void dec_chars()
  49. {
  50. char *p;
  51. signed char expected;
  52. for (unsigned c = 0; c <= 0xff; c++) {
  53. mbedtls_test_set_step(c);
  54. /* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
  55. p = memchr(base64_digits, c, sizeof(base64_digits) - 1);
  56. if (p == NULL) {
  57. expected = -1;
  58. } else {
  59. expected = p - base64_digits;
  60. }
  61. TEST_CF_SECRET(&c, sizeof(c));
  62. signed char actual = mbedtls_ct_base64_dec_value(c);
  63. TEST_CF_PUBLIC(&c, sizeof(c));
  64. TEST_CF_PUBLIC(&actual, sizeof(actual));
  65. TEST_EQUAL(actual, expected);
  66. }
  67. }
  68. /* END_CASE */
  69. /* BEGIN_CASE */
  70. void mbedtls_base64_encode(char *src_string, char *dst_string,
  71. int dst_buf_size, int result)
  72. {
  73. unsigned char src_str[1000];
  74. unsigned char dst_str[1000];
  75. size_t len, src_len;
  76. memset(src_str, 0x00, 1000);
  77. memset(dst_str, 0x00, 1000);
  78. strncpy((char *) src_str, src_string, sizeof(src_str) - 1);
  79. src_len = strlen((char *) src_str);
  80. TEST_CF_SECRET(src_str, sizeof(src_str));
  81. TEST_ASSERT(mbedtls_base64_encode(dst_str, dst_buf_size, &len, src_str, src_len) == result);
  82. TEST_CF_PUBLIC(src_str, sizeof(src_str));
  83. /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
  84. CF failures by unmarking it. */
  85. TEST_CF_PUBLIC(dst_str, len);
  86. if (result == 0) {
  87. TEST_ASSERT(strcmp((char *) dst_str, dst_string) == 0);
  88. }
  89. }
  90. /* END_CASE */
  91. /* BEGIN_CASE */
  92. void mbedtls_base64_decode(char *src_string, char *dst_string, int result)
  93. {
  94. unsigned char src_str[1000];
  95. unsigned char dst_str[1000];
  96. size_t len;
  97. int res;
  98. memset(src_str, 0x00, 1000);
  99. memset(dst_str, 0x00, 1000);
  100. strncpy((char *) src_str, src_string, sizeof(src_str) - 1);
  101. res = mbedtls_base64_decode(dst_str, sizeof(dst_str), &len, src_str, strlen((char *) src_str));
  102. TEST_ASSERT(res == result);
  103. if (result == 0) {
  104. TEST_ASSERT(strcmp((char *) dst_str, dst_string) == 0);
  105. }
  106. }
  107. /* END_CASE */
  108. /* BEGIN_CASE */
  109. void base64_encode_hex(data_t *src, char *dst, int dst_buf_size,
  110. int result)
  111. {
  112. unsigned char *res = NULL;
  113. size_t len;
  114. res = mbedtls_test_zero_alloc(dst_buf_size);
  115. TEST_CF_SECRET(src->x, src->len);
  116. TEST_ASSERT(mbedtls_base64_encode(res, dst_buf_size, &len, src->x, src->len) == result);
  117. TEST_CF_PUBLIC(src->x, src->len);
  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. if (result == 0) {
  122. TEST_ASSERT(len == strlen(dst));
  123. TEST_ASSERT(memcmp(dst, res, len) == 0);
  124. }
  125. exit:
  126. mbedtls_free(res);
  127. }
  128. /* END_CASE */
  129. /* BEGIN_CASE */
  130. void base64_decode_hex(char *src, data_t *dst, int dst_buf_size,
  131. int result)
  132. {
  133. unsigned char *res = NULL;
  134. size_t len;
  135. res = mbedtls_test_zero_alloc(dst_buf_size);
  136. TEST_ASSERT(mbedtls_base64_decode(res, dst_buf_size, &len, (unsigned char *) src,
  137. strlen(src)) == result);
  138. if (result == 0) {
  139. TEST_ASSERT(len == dst->len);
  140. TEST_ASSERT(memcmp(dst->x, res, len) == 0);
  141. }
  142. exit:
  143. mbedtls_free(res);
  144. }
  145. /* END_CASE */
  146. /* BEGIN_CASE */
  147. void base64_decode_hex_src(data_t *src, char *dst_ref, int result)
  148. {
  149. unsigned char dst[1000] = { 0 };
  150. size_t len;
  151. TEST_ASSERT(mbedtls_base64_decode(dst, sizeof(dst), &len, src->x, src->len) == result);
  152. if (result == 0) {
  153. TEST_ASSERT(len == strlen(dst_ref));
  154. TEST_ASSERT(memcmp(dst, dst_ref, len) == 0);
  155. }
  156. exit:
  157. ;;
  158. }
  159. /* END_CASE */
  160. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  161. void base64_selftest()
  162. {
  163. TEST_ASSERT(mbedtls_base64_self_test(1) == 0);
  164. }
  165. /* END_CASE */