Rework mbedtls_test_unhexify()

Rework mbedtls_test_unhexify to extend its scope of usage.
Return in error when the function detects an error instead
of calling mbedtls_exit().
Improve safety by checking the output buffer is not overrun.

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
This commit is contained in:
Ronald Cron 2020-06-18 10:10:46 +02:00
parent 9ed4073ea5
commit a0c2539c4c
4 changed files with 79 additions and 32 deletions

View file

@ -54,7 +54,29 @@
int mbedtls_test_platform_setup( void );
void mbedtls_test_platform_teardown( void );
int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf );
/**
* \brief This function translates an ASCII string encoding an
* hexadecimal number into the encoded hexadecimal number. The
* hexadecimal number is represented as an array of
* unsigned char.
*
* \note The output buffer can be the same as the input buffer. For
* any other overlapping of the input and output buffers, the
* behavior is undefined.
*
* \param obuf Output buffer.
* \param obufmax Size in number of bytes of \p obuf.
* \param ibuf Input buffer.
* \param len The number of unsigned char written in \p obuf. This must
* not be \c NULL.
*
* \return \c 0 on success.
* \return \c -1 if the output buffer is too small or the input string
* is not a valid ASCII encoding of an hexadecimal number.
*/
int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
const char *ibuf, size_t *len );
void mbedtls_test_hexify( unsigned char *obuf,
const unsigned char *ibuf,
int len );

View file

@ -41,38 +41,49 @@ void mbedtls_test_platform_teardown( void )
#endif /* MBEDTLS_PLATFORM_C */
}
int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf )
static int ascii2uc(const char c, unsigned char *uc)
{
unsigned char c, c2;
int len = strlen( ibuf ) / 2;
TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
if( ( c >= '0' ) && ( c <= '9' ) )
*uc = c - '0';
else if( ( c >= 'a' ) && ( c <= 'f' ) )
*uc = c - 'a' + 10;
else if( ( c >= 'A' ) && ( c <= 'F' ) )
*uc = c - 'A' + 10;
else
return( -1 );
return( 0 );
}
int mbedtls_test_unhexify( unsigned char *obuf,
size_t obufmax,
const char *ibuf,
size_t *len )
{
unsigned char uc, uc2;
*len = strlen( ibuf );
/* Must be even number of bytes. */
if ( ( *len ) & 1 )
return( -1 );
*len /= 2;
if ( (*len) > obufmax )
return( -1 );
while( *ibuf != 0 )
{
c = *ibuf++;
if( c >= '0' && c <= '9' )
c -= '0';
else if( c >= 'a' && c <= 'f' )
c -= 'a' - 10;
else if( c >= 'A' && c <= 'F' )
c -= 'A' - 10;
else
TEST_HELPER_ASSERT( 0 );
if ( ascii2uc( *(ibuf++), &uc ) != 0 )
return( -1 );
c2 = *ibuf++;
if( c2 >= '0' && c2 <= '9' )
c2 -= '0';
else if( c2 >= 'a' && c2 <= 'f' )
c2 -= 'a' - 10;
else if( c2 >= 'A' && c2 <= 'F' )
c2 -= 'A' - 10;
else
TEST_HELPER_ASSERT( 0 );
if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
return( -1 );
*obuf++ = ( c << 4 ) | c2;
*(obuf++) = ( uc << 4 ) | uc2;
}
return len;
return( 0 );
}
void mbedtls_test_hexify( unsigned char *obuf,
@ -117,6 +128,7 @@ unsigned char *mbedtls_test_zero_alloc( size_t len )
unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
{
unsigned char *obuf;
size_t len;
*olen = strlen( ibuf ) / 2;
@ -125,8 +137,7 @@ unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
obuf = mbedtls_calloc( 1, *olen );
TEST_HELPER_ASSERT( obuf != NULL );
(void) mbedtls_test_unhexify( obuf, ibuf );
TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
return( obuf );
}

View file

@ -277,8 +277,13 @@ static int convert_params( size_t cnt , char ** params , int * int_params_store
{
if ( verify_string( &val ) == 0 )
{
*int_params_store = mbedtls_test_unhexify(
(unsigned char *) val, val );
size_t len;
TEST_HELPER_ASSERT(
mbedtls_test_unhexify( (unsigned char *) val, strlen( val ),
val, &len ) == 0 );
*int_params_store = len;
*out++ = val;
*out++ = (char *)(int_params_store++);
}

View file

@ -70,12 +70,16 @@ uint8_t receive_byte()
{
uint8_t byte;
uint8_t c[3];
char *endptr;
size_t len;
c[0] = greentea_getc();
c[1] = greentea_getc();
c[2] = '\0';
TEST_HELPER_ASSERT( mbedtls_test_unhexify( &byte, c ) != 2 );
TEST_HELPER_ASSERT( mbedtls_test_unhexify( &byte, sizeof( byte ),
c, &len ) == 0 );
TEST_HELPER_ASSERT( len != 2 );
return( byte );
}
@ -90,6 +94,7 @@ uint8_t receive_byte()
uint32_t receive_uint32()
{
uint32_t value;
size_t len;
const uint8_t c_be[8] = { greentea_getc(),
greentea_getc(),
greentea_getc(),
@ -101,7 +106,11 @@ uint32_t receive_uint32()
};
const uint8_t c[9] = { c_be[6], c_be[7], c_be[4], c_be[5], c_be[2],
c_be[3], c_be[0], c_be[1], '\0' };
TEST_HELPER_ASSERT( mbedtls_test_unhexify( (uint8_t*)&value, c ) != 8 );
TEST_HELPER_ASSERT( mbedtls_test_unhexify( (uint8_t*)&value, sizeof( value ),
c, &len ) == 0 );
TEST_HELPER_ASSERT( len != 8 );
return( value );
}