2020-06-15 11:59:37 +02:00
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2020-06-03 10:11:18 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/helpers.h>
|
2020-06-09 16:27:37 +02:00
|
|
|
#include <test/macros.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-07-01 16:01:21 +02:00
|
|
|
#if defined(MBEDTLS_CHECK_PARAMS)
|
|
|
|
#include <setjmp.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Static global variables */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CHECK_PARAMS)
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t expected_call;
|
|
|
|
uint8_t expected_call_happened;
|
|
|
|
|
|
|
|
jmp_buf state;
|
|
|
|
|
|
|
|
mbedtls_test_param_failed_location_record_t location_record;
|
|
|
|
}
|
|
|
|
param_failed_ctx_t;
|
|
|
|
static param_failed_ctx_t param_failed_ctx;
|
|
|
|
#endif
|
|
|
|
|
2020-06-09 16:27:37 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
static mbedtls_platform_context platform_ctx;
|
|
|
|
#endif
|
|
|
|
|
2021-01-20 18:51:47 +01:00
|
|
|
mbedtls_test_info_t mbedtls_test_info;
|
2021-01-20 16:56:42 +01:00
|
|
|
|
2020-07-01 16:01:21 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Helper Functions */
|
|
|
|
|
2020-06-08 16:44:58 +02:00
|
|
|
int mbedtls_test_platform_setup( void )
|
2020-06-09 16:27:37 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
ret = mbedtls_platform_setup( &platform_ctx );
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2020-06-08 16:44:58 +02:00
|
|
|
void mbedtls_test_platform_teardown( void )
|
2020-06-09 16:27:37 +02:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
mbedtls_platform_teardown( &platform_ctx );
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
|
|
|
}
|
|
|
|
|
2020-06-18 10:10:46 +02:00
|
|
|
static int ascii2uc(const char c, unsigned char *uc)
|
2020-06-09 16:27:37 +02:00
|
|
|
{
|
2020-06-18 10:10:46 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2021-01-20 16:56:42 +01:00
|
|
|
void mbedtls_test_fail( const char *test, int line_no, const char* filename )
|
|
|
|
{
|
2021-01-20 18:51:47 +01:00
|
|
|
if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
|
2021-01-20 16:56:42 +01:00
|
|
|
{
|
|
|
|
/* We've already recorded the test as having failed. Don't
|
|
|
|
* overwrite any previous information about the failure. */
|
|
|
|
return;
|
|
|
|
}
|
2021-01-20 18:51:47 +01:00
|
|
|
mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
|
|
|
|
mbedtls_test_info.test = test;
|
|
|
|
mbedtls_test_info.line_no = line_no;
|
|
|
|
mbedtls_test_info.filename = filename;
|
2021-01-20 16:56:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void mbedtls_test_skip( const char *test, int line_no, const char* filename )
|
|
|
|
{
|
2021-01-20 18:51:47 +01:00
|
|
|
mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
|
|
|
|
mbedtls_test_info.test = test;
|
|
|
|
mbedtls_test_info.line_no = line_no;
|
|
|
|
mbedtls_test_info.filename = filename;
|
2021-01-20 16:56:42 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 17:20:45 +01:00
|
|
|
void mbedtls_test_set_step( unsigned long step )
|
|
|
|
{
|
|
|
|
mbedtls_test_info.step = step;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mbedtls_test_info_reset( void )
|
|
|
|
{
|
|
|
|
mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
|
|
|
|
mbedtls_test_info.step = (unsigned long)( -1 );
|
|
|
|
mbedtls_test_info.test = 0;
|
|
|
|
mbedtls_test_info.line_no = 0;
|
|
|
|
mbedtls_test_info.filename = 0;
|
|
|
|
}
|
|
|
|
|
2020-06-18 10:10:46 +02:00
|
|
|
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 );
|
2020-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
while( *ibuf != 0 )
|
|
|
|
{
|
2020-06-18 10:10:46 +02:00
|
|
|
if ( ascii2uc( *(ibuf++), &uc ) != 0 )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
|
|
|
|
return( -1 );
|
2020-06-09 16:27:37 +02:00
|
|
|
|
2020-06-18 10:10:46 +02:00
|
|
|
*(obuf++) = ( uc << 4 ) | uc2;
|
2020-06-09 16:27:37 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 10:10:46 +02:00
|
|
|
return( 0 );
|
2020-06-09 16:27:37 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:05:57 +02:00
|
|
|
void mbedtls_test_hexify( unsigned char *obuf,
|
|
|
|
const unsigned char *ibuf,
|
|
|
|
int len )
|
2020-06-09 16:27:37 +02:00
|
|
|
{
|
|
|
|
unsigned char l, h;
|
|
|
|
|
|
|
|
while( len != 0 )
|
|
|
|
{
|
|
|
|
h = *ibuf / 16;
|
|
|
|
l = *ibuf % 16;
|
|
|
|
|
|
|
|
if( h < 10 )
|
|
|
|
*obuf++ = '0' + h;
|
|
|
|
else
|
|
|
|
*obuf++ = 'a' + h - 10;
|
|
|
|
|
|
|
|
if( l < 10 )
|
|
|
|
*obuf++ = '0' + l;
|
|
|
|
else
|
|
|
|
*obuf++ = 'a' + l - 10;
|
|
|
|
|
|
|
|
++ibuf;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 10:42:18 +02:00
|
|
|
unsigned char *mbedtls_test_zero_alloc( size_t len )
|
2020-06-09 16:27:37 +02:00
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
size_t actual_len = ( len != 0 ) ? len : 1;
|
|
|
|
|
|
|
|
p = mbedtls_calloc( 1, actual_len );
|
|
|
|
TEST_HELPER_ASSERT( p != NULL );
|
|
|
|
|
|
|
|
memset( p, 0x00, actual_len );
|
|
|
|
|
|
|
|
return( p );
|
|
|
|
}
|
|
|
|
|
2020-06-10 10:53:11 +02:00
|
|
|
unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
|
2020-06-09 16:27:37 +02:00
|
|
|
{
|
|
|
|
unsigned char *obuf;
|
2020-06-18 10:10:46 +02:00
|
|
|
size_t len;
|
2020-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
*olen = strlen( ibuf ) / 2;
|
|
|
|
|
|
|
|
if( *olen == 0 )
|
2020-06-10 10:42:18 +02:00
|
|
|
return( mbedtls_test_zero_alloc( *olen ) );
|
2020-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
obuf = mbedtls_calloc( 1, *olen );
|
|
|
|
TEST_HELPER_ASSERT( obuf != NULL );
|
2020-06-18 10:10:46 +02:00
|
|
|
TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
|
2020-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
return( obuf );
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:03:08 +02:00
|
|
|
int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
|
|
|
|
uint32_t a_len, uint32_t b_len )
|
2020-06-09 16:27:37 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
|
|
|
if( a_len != b_len )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
for( i = 0; i < a_len; i++ )
|
|
|
|
{
|
|
|
|
if( a[i] != b[i] )
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2020-07-01 16:01:21 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_CHECK_PARAMS)
|
|
|
|
void mbedtls_test_param_failed_get_location_record(
|
|
|
|
mbedtls_test_param_failed_location_record_t *location_record )
|
|
|
|
{
|
|
|
|
*location_record = param_failed_ctx.location_record;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mbedtls_test_param_failed_expect_call( void )
|
|
|
|
{
|
|
|
|
param_failed_ctx.expected_call_happened = 0;
|
|
|
|
param_failed_ctx.expected_call = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_test_param_failed_check_expected_call( void )
|
|
|
|
{
|
|
|
|
param_failed_ctx.expected_call = 0;
|
|
|
|
|
|
|
|
if( param_failed_ctx.expected_call_happened != 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void* mbedtls_test_param_failed_get_state_buf( void )
|
|
|
|
{
|
2020-09-25 10:45:06 +02:00
|
|
|
return ¶m_failed_ctx.state;
|
2020-07-01 16:01:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void mbedtls_test_param_failed_reset_state( void )
|
|
|
|
{
|
|
|
|
memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void mbedtls_param_failed( const char *failure_condition,
|
|
|
|
const char *file,
|
|
|
|
int line )
|
|
|
|
{
|
|
|
|
/* Record the location of the failure */
|
|
|
|
param_failed_ctx.location_record.failure_condition = failure_condition;
|
|
|
|
param_failed_ctx.location_record.file = file;
|
|
|
|
param_failed_ctx.location_record.line = line;
|
|
|
|
|
|
|
|
/* If we are testing the callback function... */
|
|
|
|
if( param_failed_ctx.expected_call != 0 )
|
|
|
|
{
|
|
|
|
param_failed_ctx.expected_call = 0;
|
|
|
|
param_failed_ctx.expected_call_happened = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* ...else try a long jump. If the execution state has not been set-up
|
|
|
|
* or reset then the long jump buffer is all zero's and the call will
|
|
|
|
* with high probability fault, emphasizing there is something to look
|
|
|
|
* at.
|
|
|
|
*/
|
|
|
|
|
|
|
|
longjmp( param_failed_ctx.state, 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_CHECK_PARAMS */
|
2021-01-08 18:04:59 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_TEST_HOOKS)
|
|
|
|
void mbedtls_test_err_add_check( int high, int low,
|
|
|
|
const char *file, int line )
|
|
|
|
{
|
2021-03-31 10:34:22 +02:00
|
|
|
/* Error codes are always negative (a value of zero is a success) however
|
|
|
|
* their positive opposites can be easier to understand. The following
|
|
|
|
* examples given in comments have been made positive for ease of
|
|
|
|
* understanding. The structure of an error code is such:
|
|
|
|
*
|
2021-04-12 16:44:47 +02:00
|
|
|
* shhhhhhhhlllllll
|
2021-03-31 10:34:22 +02:00
|
|
|
*
|
|
|
|
* s = sign bit.
|
|
|
|
* h = high level error code (includes high and module error codes).
|
|
|
|
* l = low level error code.
|
|
|
|
*/
|
2021-03-31 17:09:28 +02:00
|
|
|
if ( high > -0x1000 && high != 0 ) // high < 0001000000000000
|
2021-01-08 18:04:59 +01:00
|
|
|
{
|
2021-02-08 13:32:41 +01:00
|
|
|
mbedtls_test_fail( "'high' is not a high-level error code",
|
|
|
|
line, file );
|
2021-01-29 15:56:20 +01:00
|
|
|
}
|
2021-03-31 10:34:22 +02:00
|
|
|
else if ( high < -0x7F80 ) // high > 0111111110000000
|
2021-01-29 15:56:20 +01:00
|
|
|
{
|
2021-03-31 10:34:22 +02:00
|
|
|
mbedtls_test_fail( "'high' error code is greater than 15 bits",
|
|
|
|
line, file );
|
2021-01-29 15:56:20 +01:00
|
|
|
}
|
2021-04-12 16:44:47 +02:00
|
|
|
else if ( ( high & 0x7F ) != 0 ) // high & 0000000001111111
|
2021-01-29 15:56:20 +01:00
|
|
|
{
|
2021-02-08 13:32:41 +01:00
|
|
|
mbedtls_test_fail( "'high' contains a low-level error code",
|
|
|
|
line, file );
|
2021-01-29 15:56:20 +01:00
|
|
|
}
|
2021-03-31 10:34:22 +02:00
|
|
|
else if ( low < -0x007F ) // low > 0000000001111111
|
2021-01-29 15:56:20 +01:00
|
|
|
{
|
2021-03-31 10:34:22 +02:00
|
|
|
mbedtls_test_fail( "'low' error code is greater than 7 bits",
|
|
|
|
line, file );
|
2021-01-29 15:56:20 +01:00
|
|
|
}
|
|
|
|
else if ( low > 0 )
|
|
|
|
{
|
2021-03-31 10:34:22 +02:00
|
|
|
mbedtls_test_fail( "'low' error code is greater than zero",
|
|
|
|
line, file );
|
2021-01-08 18:04:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_TEST_HOOKS */
|