test_suite_base64.function 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. {
  22. mbedtls_test_set_step( c );
  23. TEST_CF_SECRET( &c, sizeof( c ) );
  24. unsigned char m = mbedtls_ct_uchar_mask_of_range( low, high, c );
  25. TEST_CF_PUBLIC( &c, sizeof( c ) );
  26. TEST_CF_PUBLIC( &m, sizeof( m ) );
  27. if( low <= c && c <= high )
  28. TEST_EQUAL( m, 0xff );
  29. else
  30. TEST_EQUAL( m, 0 );
  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. {
  39. mbedtls_test_set_step( value );
  40. TEST_CF_SECRET( &value, sizeof( value ) );
  41. unsigned char digit = mbedtls_ct_base64_enc_char( value );
  42. TEST_CF_PUBLIC( &value, sizeof( value ) );
  43. TEST_CF_PUBLIC( &digit, sizeof( digit ) );
  44. TEST_EQUAL( digit, base64_digits[value] );
  45. }
  46. }
  47. /* END_CASE */
  48. /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
  49. void dec_chars( )
  50. {
  51. char *p;
  52. signed char expected;
  53. for( unsigned c = 0; c <= 0xff; c++ )
  54. {
  55. mbedtls_test_set_step( c );
  56. /* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
  57. p = memchr( base64_digits, c, sizeof( base64_digits ) - 1 );
  58. if( p == NULL )
  59. expected = -1;
  60. else
  61. expected = p - base64_digits;
  62. TEST_CF_SECRET( &c, sizeof( c ) );
  63. signed char actual = mbedtls_ct_base64_dec_value( c );
  64. TEST_CF_PUBLIC( &c, sizeof( c ) );
  65. TEST_CF_PUBLIC( &actual, sizeof( actual ) );
  66. TEST_EQUAL( actual, expected );
  67. }
  68. }
  69. /* END_CASE */
  70. /* BEGIN_CASE */
  71. void mbedtls_base64_encode( char * src_string, char * dst_string,
  72. int dst_buf_size, int result )
  73. {
  74. unsigned char src_str[1000];
  75. unsigned char dst_str[1000];
  76. size_t len, src_len;
  77. memset(src_str, 0x00, 1000);
  78. memset(dst_str, 0x00, 1000);
  79. strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
  80. src_len = strlen( (char *) src_str );
  81. TEST_CF_SECRET( src_str, sizeof( src_str ) );
  82. TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, src_len) == result );
  83. TEST_CF_PUBLIC( src_str, sizeof( src_str ) );
  84. /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
  85. CF failures by unmarking it. */
  86. TEST_CF_PUBLIC( dst_str, len );
  87. if( result == 0 )
  88. {
  89. TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
  90. }
  91. }
  92. /* END_CASE */
  93. /* BEGIN_CASE */
  94. void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
  95. {
  96. unsigned char src_str[1000];
  97. unsigned char dst_str[1000];
  98. size_t len;
  99. int res;
  100. memset(src_str, 0x00, 1000);
  101. memset(dst_str, 0x00, 1000);
  102. strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
  103. res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
  104. TEST_ASSERT( res == result );
  105. if( result == 0 )
  106. {
  107. TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
  108. }
  109. }
  110. /* END_CASE */
  111. /* BEGIN_CASE */
  112. void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
  113. int result )
  114. {
  115. unsigned char *res = NULL;
  116. size_t len;
  117. res = mbedtls_test_zero_alloc( dst_buf_size );
  118. TEST_CF_SECRET( src->x, src->len );
  119. TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
  120. TEST_CF_PUBLIC( src->x, src->len );
  121. /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
  122. CF failures by unmarking it. */
  123. TEST_CF_PUBLIC( res, len );
  124. if( result == 0 )
  125. {
  126. TEST_ASSERT( len == strlen( dst ) );
  127. TEST_ASSERT( memcmp( dst, res, len ) == 0 );
  128. }
  129. exit:
  130. mbedtls_free( res );
  131. }
  132. /* END_CASE */
  133. /* BEGIN_CASE */
  134. void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
  135. int result )
  136. {
  137. unsigned char *res = NULL;
  138. size_t len;
  139. res = mbedtls_test_zero_alloc( dst_buf_size );
  140. TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
  141. strlen( src ) ) == result );
  142. if( result == 0 )
  143. {
  144. TEST_ASSERT( len == dst->len );
  145. TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
  146. }
  147. exit:
  148. mbedtls_free( res );
  149. }
  150. /* END_CASE */
  151. /* BEGIN_CASE */
  152. void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
  153. {
  154. unsigned char dst[1000] = { 0 };
  155. size_t len;
  156. TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
  157. if( result == 0 )
  158. {
  159. TEST_ASSERT( len == strlen( dst_ref ) );
  160. TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
  161. }
  162. exit:
  163. ;;
  164. }
  165. /* END_CASE */
  166. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  167. void base64_selftest( )
  168. {
  169. TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
  170. }
  171. /* END_CASE */