2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/base64.h"
|
2021-11-15 15:13:01 +00:00
|
|
|
#include "constant_time_internal.h"
|
|
|
|
#include "constant_time_invasive.h"
|
2021-02-24 15:32:42 +00:00
|
|
|
#include <test/constant_flow.h>
|
2021-08-06 12:55:55 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_TEST_HOOKS)
|
2021-08-06 14:54:22 +00:00
|
|
|
static const char base64_digits[] =
|
2021-08-06 12:55:55 +00:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
#endif /* MBEDTLS_TEST_HOOKS */
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_HEADER */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 10:49:31 +00:00
|
|
|
* depends_on:MBEDTLS_BASE64_C
|
2013-08-20 09:48:36 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 13:16:06 +00:00
|
|
|
|
2021-08-03 10:38:55 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
|
|
|
|
void mask_of_range( int low_arg, int high_arg )
|
|
|
|
{
|
|
|
|
unsigned char low = low_arg, high = high_arg;
|
|
|
|
unsigned c;
|
|
|
|
for( c = 0; c <= 0xff; c++ )
|
|
|
|
{
|
|
|
|
mbedtls_test_set_step( c );
|
|
|
|
TEST_CF_SECRET( &c, sizeof( c ) );
|
2021-11-15 15:03:24 +00:00
|
|
|
unsigned char m = mbedtls_ct_uchar_mask_of_range( low, high, c );
|
2021-08-03 10:38:55 +00:00
|
|
|
TEST_CF_PUBLIC( &c, sizeof( c ) );
|
2021-08-03 15:41:49 +00:00
|
|
|
TEST_CF_PUBLIC( &m, sizeof( m ) );
|
2021-08-03 10:38:55 +00:00
|
|
|
if( low <= c && c <= high )
|
|
|
|
TEST_EQUAL( m, 0xff );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( m, 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
|
2021-08-06 12:55:55 +00:00
|
|
|
void enc_chars( )
|
2021-08-03 10:38:55 +00:00
|
|
|
{
|
|
|
|
for( unsigned value = 0; value < 64; value++ )
|
|
|
|
{
|
|
|
|
mbedtls_test_set_step( value );
|
|
|
|
TEST_CF_SECRET( &value, sizeof( value ) );
|
2021-11-15 15:03:24 +00:00
|
|
|
unsigned char digit = mbedtls_ct_base64_enc_char( value );
|
2021-08-03 10:38:55 +00:00
|
|
|
TEST_CF_PUBLIC( &value, sizeof( value ) );
|
|
|
|
TEST_CF_PUBLIC( &digit, sizeof( digit ) );
|
2021-08-06 14:54:22 +00:00
|
|
|
TEST_EQUAL( digit, base64_digits[value] );
|
2021-08-03 10:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
|
2021-08-06 12:55:55 +00:00
|
|
|
void dec_chars( )
|
2021-08-03 10:38:55 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
signed char expected;
|
|
|
|
|
|
|
|
for( unsigned c = 0; c <= 0xff; c++ )
|
|
|
|
{
|
|
|
|
mbedtls_test_set_step( c );
|
2021-08-06 14:54:22 +00:00
|
|
|
/* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
|
|
|
|
p = memchr( base64_digits, c, sizeof( base64_digits ) - 1 );
|
2021-08-03 10:38:55 +00:00
|
|
|
if( p == NULL )
|
|
|
|
expected = -1;
|
|
|
|
else
|
2021-08-06 14:54:22 +00:00
|
|
|
expected = p - base64_digits;
|
2021-08-03 10:38:55 +00:00
|
|
|
TEST_CF_SECRET( &c, sizeof( c ) );
|
2021-11-15 15:03:24 +00:00
|
|
|
signed char actual = mbedtls_ct_base64_dec_value( c );
|
2021-08-03 10:38:55 +00:00
|
|
|
TEST_CF_PUBLIC( &c, sizeof( c ) );
|
|
|
|
TEST_CF_PUBLIC( &actual, sizeof( actual ) );
|
|
|
|
TEST_EQUAL( actual, expected );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 13:23:15 +00:00
|
|
|
void mbedtls_base64_encode( char * src_string, char * dst_string,
|
|
|
|
int dst_buf_size, int result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char dst_str[1000];
|
2021-02-24 15:32:42 +00:00
|
|
|
size_t len, src_len;
|
2009-06-28 21:50:27 +00:00
|
|
|
|
|
|
|
memset(src_str, 0x00, 1000);
|
|
|
|
memset(dst_str, 0x00, 1000);
|
|
|
|
|
2014-04-17 14:06:37 +00:00
|
|
|
strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
|
2021-02-24 15:32:42 +00:00
|
|
|
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 ) );
|
|
|
|
|
2021-03-02 22:48:40 +00:00
|
|
|
/* 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 );
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
if( result == 0 )
|
2009-07-11 15:29:30 +00:00
|
|
|
{
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
|
2009-07-11 15:29:30 +00:00
|
|
|
}
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 13:23:15 +00:00
|
|
|
void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char dst_str[1000];
|
2015-06-02 15:30:35 +00:00
|
|
|
size_t len;
|
2014-10-15 19:45:39 +00:00
|
|
|
int res;
|
2009-06-28 21:50:27 +00:00
|
|
|
|
|
|
|
memset(src_str, 0x00, 1000);
|
|
|
|
memset(dst_str, 0x00, 1000);
|
2014-04-17 14:06:37 +00:00
|
|
|
|
|
|
|
strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
|
2015-06-02 15:30:35 +00:00
|
|
|
res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
|
2014-04-17 14:07:20 +00:00
|
|
|
TEST_ASSERT( res == result );
|
2013-08-20 09:48:36 +00:00
|
|
|
if( result == 0 )
|
2009-07-11 15:29:30 +00:00
|
|
|
{
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
|
2009-07-11 15:29:30 +00:00
|
|
|
}
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-05 11:29:38 +00:00
|
|
|
|
2014-07-04 11:50:31 +00:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 10:05:32 +00:00
|
|
|
void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
|
2014-07-04 11:50:31 +00:00
|
|
|
int result )
|
|
|
|
{
|
2017-06-09 03:32:58 +00:00
|
|
|
unsigned char *res = NULL;
|
|
|
|
size_t len;
|
2014-07-04 11:50:31 +00:00
|
|
|
|
2020-06-10 08:42:18 +00:00
|
|
|
res = mbedtls_test_zero_alloc( dst_buf_size );
|
2014-07-04 11:50:31 +00:00
|
|
|
|
2021-02-24 15:32:42 +00:00
|
|
|
TEST_CF_SECRET( src->x, src->len );
|
2017-06-09 03:32:58 +00:00
|
|
|
TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
|
2021-02-24 15:32:42 +00:00
|
|
|
TEST_CF_PUBLIC( src->x, src->len );
|
|
|
|
|
2021-03-02 22:48:40 +00:00
|
|
|
/* 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 );
|
|
|
|
|
2014-07-04 11:50:31 +00:00
|
|
|
if( result == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( len == strlen( dst ) );
|
|
|
|
TEST_ASSERT( memcmp( dst, res, len ) == 0 );
|
|
|
|
}
|
2014-07-04 16:35:50 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( res );
|
2014-07-04 11:50:31 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-29 10:05:32 +00:00
|
|
|
void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
|
2014-07-04 11:50:31 +00:00
|
|
|
int result )
|
|
|
|
{
|
2017-06-09 03:32:58 +00:00
|
|
|
unsigned char *res = NULL;
|
|
|
|
size_t len;
|
2014-07-04 11:50:31 +00:00
|
|
|
|
2020-06-10 08:42:18 +00:00
|
|
|
res = mbedtls_test_zero_alloc( dst_buf_size );
|
2014-07-04 11:50:31 +00:00
|
|
|
|
2015-06-02 15:30:35 +00:00
|
|
|
TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
|
2014-07-04 11:50:31 +00:00
|
|
|
strlen( src ) ) == result );
|
|
|
|
if( result == 0 )
|
|
|
|
{
|
2017-06-09 03:32:58 +00:00
|
|
|
TEST_ASSERT( len == dst->len );
|
|
|
|
TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
|
2014-07-04 11:50:31 +00:00
|
|
|
}
|
2014-07-04 16:35:50 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( res );
|
2014-07-04 11:50:31 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-10-15 19:45:39 +00:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 10:05:32 +00:00
|
|
|
void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
|
2014-10-15 19:45:39 +00:00
|
|
|
{
|
|
|
|
unsigned char dst[1000] = { 0 };
|
2017-06-09 03:32:58 +00:00
|
|
|
size_t len;
|
2014-10-15 19:45:39 +00:00
|
|
|
|
2017-06-09 03:32:58 +00:00
|
|
|
TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
|
2014-10-15 19:45:39 +00:00
|
|
|
if( result == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( len == strlen( dst_ref ) );
|
|
|
|
TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2017-06-09 03:32:58 +00:00
|
|
|
;;
|
2014-10-15 19:45:39 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2017-05-30 13:23:15 +00:00
|
|
|
void base64_selftest( )
|
2009-07-05 11:29:38 +00:00
|
|
|
{
|
2016-09-09 08:10:28 +00:00
|
|
|
TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
|
2009-07-05 11:29:38 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|