2017-05-09 16:20:21 +00:00
|
|
|
#line 2 "suites/helpers.function"
|
2016-02-17 23:34:30 +00:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Headers */
|
|
|
|
|
2020-06-09 11:52:23 +00:00
|
|
|
#include <test/macros.h>
|
2020-06-03 08:11:18 +00:00
|
|
|
#include <test/helpers.h>
|
2020-06-09 14:57:42 +00:00
|
|
|
#include <test/random.h>
|
2020-11-24 17:33:13 +00:00
|
|
|
#include <test/psa_crypto_helpers.h>
|
2020-06-09 11:52:23 +00:00
|
|
|
|
2016-05-17 12:35:51 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2014-06-06 12:48:09 +00:00
|
|
|
#else
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <stdio.h>
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_fprintf fprintf
|
2016-09-30 12:11:29 +00:00
|
|
|
#define mbedtls_snprintf snprintf
|
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_free free
|
|
|
|
#define mbedtls_exit exit
|
2016-04-27 12:35:37 +00:00
|
|
|
#define mbedtls_time time
|
|
|
|
#define mbedtls_time_t time_t
|
2016-04-18 17:18:48 +00:00
|
|
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
|
|
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
2014-06-06 12:48:09 +00:00
|
|
|
#endif
|
|
|
|
|
2016-02-17 23:34:30 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
|
|
|
#include "mbedtls/memory_buffer_alloc.h"
|
|
|
|
#endif
|
|
|
|
|
2019-01-31 13:20:20 +00:00
|
|
|
#if defined(MBEDTLS_CHECK_PARAMS)
|
|
|
|
#include "mbedtls/platform_util.h"
|
|
|
|
#include <setjmp.h>
|
|
|
|
#endif
|
|
|
|
|
2011-03-13 16:57:25 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <basetsd.h>
|
2018-06-22 10:34:33 +00:00
|
|
|
typedef UINT8 uint8_t;
|
|
|
|
typedef INT32 int32_t;
|
2011-03-13 16:57:25 +00:00
|
|
|
typedef UINT32 uint32_t;
|
2015-11-14 13:09:01 +00:00
|
|
|
#define strncasecmp _strnicmp
|
|
|
|
#define strcasecmp _stricmp
|
2011-03-13 16:57:25 +00:00
|
|
|
#else
|
2015-06-22 17:21:23 +00:00
|
|
|
#include <stdint.h>
|
2011-03-13 16:57:25 +00:00
|
|
|
#endif
|
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-10-05 09:57:49 +00:00
|
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
#include <unistd.h>
|
2017-12-05 12:08:15 +00:00
|
|
|
#include <strings.h>
|
2016-10-05 09:57:49 +00:00
|
|
|
#endif
|
2016-02-17 23:34:30 +00:00
|
|
|
|
2017-06-09 03:32:58 +00:00
|
|
|
/* Type for Hex parameters */
|
2018-06-29 10:05:32 +00:00
|
|
|
typedef struct data_tag
|
2017-06-09 03:32:58 +00:00
|
|
|
{
|
|
|
|
uint8_t * x;
|
|
|
|
uint32_t len;
|
2018-06-29 10:05:32 +00:00
|
|
|
} data_t;
|
2017-06-09 03:32:58 +00:00
|
|
|
|
2016-02-17 23:34:30 +00:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-06-29 09:02:54 +00:00
|
|
|
/* Status and error constants */
|
2016-02-17 23:34:30 +00:00
|
|
|
|
2018-06-29 09:02:54 +00:00
|
|
|
#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
|
|
|
|
#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
|
|
|
|
#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
|
2016-04-17 22:24:50 +00:00
|
|
|
|
2018-06-29 09:02:54 +00:00
|
|
|
#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
|
|
|
|
#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
|
|
|
|
#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
|
|
|
|
#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
|
|
|
|
Only int, string, binary data
|
|
|
|
and integer expressions are
|
|
|
|
allowed */
|
|
|
|
#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
|
|
|
|
build */
|
2016-02-17 23:34:30 +00:00
|
|
|
|
2016-04-17 22:24:50 +00:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Global variables */
|
|
|
|
|
2019-01-31 13:20:20 +00:00
|
|
|
#if defined(MBEDTLS_CHECK_PARAMS)
|
|
|
|
jmp_buf jmp_tmp;
|
|
|
|
#endif
|
|
|
|
|
2017-07-24 11:27:09 +00:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Helper flags for complex dependencies */
|
|
|
|
|
|
|
|
/* Indicates whether we expect mbedtls_entropy_init
|
|
|
|
* to initialize some strong entropy source. */
|
|
|
|
#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
|
|
|
|
( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
|
|
|
|
( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
|
|
|
|
defined(MBEDTLS_HAVEGE_C) || \
|
|
|
|
defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
|
|
|
|
defined(ENTROPY_NV_SEED) ) )
|
2017-09-07 07:09:33 +00:00
|
|
|
#define ENTROPY_HAVE_STRONG
|
2017-07-24 11:27:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-02-17 23:34:30 +00:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Helper Functions */
|
2019-01-31 13:20:20 +00:00
|
|
|
|
2021-01-06 19:47:16 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
|
|
|
/** Check that no PSA Crypto key slots are in use.
|
|
|
|
*
|
|
|
|
* If any slots are in use, mark the current test as failed.
|
|
|
|
*
|
|
|
|
* \return 0 if the key store is empty, 1 otherwise.
|
|
|
|
*/
|
|
|
|
int test_fail_if_psa_leaking( int line_no, const char *filename )
|
|
|
|
{
|
|
|
|
const char *msg = mbedtls_test_helper_is_psa_leaking( );
|
|
|
|
if( msg == NULL )
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
{
|
2021-01-20 15:56:42 +00:00
|
|
|
mbedtls_test_fail( msg, line_no, filename );
|
2021-01-06 19:47:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
|
|
|
|
|
2016-10-05 09:57:49 +00:00
|
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
2020-07-30 07:02:27 +00:00
|
|
|
static int redirect_output( FILE* out_stream, const char* path )
|
2016-10-05 09:57:49 +00:00
|
|
|
{
|
2020-07-30 07:02:27 +00:00
|
|
|
int out_fd, dup_fd;
|
|
|
|
FILE* path_stream;
|
2016-10-05 09:57:49 +00:00
|
|
|
|
2020-07-30 07:02:27 +00:00
|
|
|
out_fd = fileno( out_stream );
|
|
|
|
dup_fd = dup( out_fd );
|
|
|
|
|
|
|
|
if( dup_fd == -1 )
|
2016-10-05 09:57:49 +00:00
|
|
|
{
|
2020-07-30 07:02:27 +00:00
|
|
|
return( -1 );
|
2016-10-05 09:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 07:02:27 +00:00
|
|
|
path_stream = fopen( path, "w" );
|
|
|
|
if( path_stream == NULL )
|
|
|
|
{
|
|
|
|
close( dup_fd );
|
|
|
|
return( -1 );
|
|
|
|
}
|
2016-10-05 09:57:49 +00:00
|
|
|
|
2020-07-30 07:02:27 +00:00
|
|
|
fflush( out_stream );
|
|
|
|
if( dup2( fileno( path_stream ), out_fd ) == -1 )
|
2016-10-05 09:57:49 +00:00
|
|
|
{
|
2020-07-30 07:02:27 +00:00
|
|
|
close( dup_fd );
|
|
|
|
fclose( path_stream );
|
|
|
|
return( -1 );
|
2016-10-05 09:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 07:02:27 +00:00
|
|
|
fclose( path_stream );
|
|
|
|
return( dup_fd );
|
2016-10-05 09:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 07:02:27 +00:00
|
|
|
static int restore_output( FILE* out_stream, int dup_fd )
|
2016-10-05 09:57:49 +00:00
|
|
|
{
|
2020-07-30 07:02:27 +00:00
|
|
|
int out_fd = fileno( out_stream );
|
2016-10-05 09:57:49 +00:00
|
|
|
|
2020-07-30 07:02:27 +00:00
|
|
|
fflush( out_stream );
|
|
|
|
if( dup2( dup_fd, out_fd ) == -1 )
|
2016-10-05 09:57:49 +00:00
|
|
|
{
|
2020-07-30 07:02:27 +00:00
|
|
|
close( out_fd );
|
|
|
|
close( dup_fd );
|
|
|
|
return( -1 );
|
2016-10-05 09:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 07:02:27 +00:00
|
|
|
close( dup_fd );
|
|
|
|
return( 0 );
|
2016-10-12 22:07:30 +00:00
|
|
|
}
|
2016-10-05 09:57:49 +00:00
|
|
|
#endif /* __unix__ || __APPLE__ __MACH__ */
|