2017-05-09 18:20:21 +02:00
|
|
|
#line 2 "suites/helpers.function"
|
2016-02-18 00:34:30 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Headers */
|
|
|
|
|
2020-06-03 10:11:18 +02:00
|
|
|
#include <test/helpers.h>
|
2021-05-27 14:44:31 +02:00
|
|
|
#include <test/macros.h>
|
2020-06-09 16:57:42 +02:00
|
|
|
#include <test/random.h>
|
2020-11-24 18:33:13 +01:00
|
|
|
#include <test/psa_crypto_helpers.h>
|
2020-06-09 13:52:23 +02:00
|
|
|
|
2016-05-17 14:35:51 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-05-21 09:48:03 +02:00
|
|
|
#if defined (MBEDTLS_ERROR_C)
|
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2014-06-06 14:48:09 +02:00
|
|
|
#else
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdio.h>
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_fprintf fprintf
|
2016-09-30 14:11:29 +02:00
|
|
|
#define mbedtls_snprintf snprintf
|
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_free free
|
|
|
|
#define mbedtls_exit exit
|
2016-04-27 14:35:37 +02:00
|
|
|
#define mbedtls_time time
|
|
|
|
#define mbedtls_time_t time_t
|
2016-04-18 19:18:48 +02:00
|
|
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
|
|
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
2014-06-06 14:48:09 +02:00
|
|
|
#endif
|
|
|
|
|
2016-02-18 00:34:30 +01:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
|
|
|
#include "mbedtls/memory_buffer_alloc.h"
|
|
|
|
#endif
|
|
|
|
|
2011-03-13 17:57:25 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <basetsd.h>
|
2018-06-22 12:34:33 +02:00
|
|
|
typedef UINT8 uint8_t;
|
|
|
|
typedef INT32 int32_t;
|
2011-03-13 17:57:25 +01:00
|
|
|
typedef UINT32 uint32_t;
|
2015-11-14 14:09:01 +01:00
|
|
|
#define strncasecmp _strnicmp
|
|
|
|
#define strcasecmp _stricmp
|
2011-03-13 17:57:25 +01:00
|
|
|
#else
|
2015-06-22 19:21:23 +02:00
|
|
|
#include <stdint.h>
|
2011-03-13 17:57:25 +01:00
|
|
|
#endif
|
|
|
|
|
2013-08-16 13:31:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-10-05 11:57:49 +02:00
|
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
#include <unistd.h>
|
2017-12-05 13:08:15 +01:00
|
|
|
#include <strings.h>
|
2016-10-05 11:57:49 +02:00
|
|
|
#endif
|
2016-02-18 00:34:30 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
/* Type for Hex parameters */
|
2018-06-29 12:05:32 +02:00
|
|
|
typedef struct data_tag
|
2017-06-09 05:32:58 +02:00
|
|
|
{
|
|
|
|
uint8_t * x;
|
|
|
|
uint32_t len;
|
2018-06-29 12:05:32 +02:00
|
|
|
} data_t;
|
2017-06-09 05:32:58 +02:00
|
|
|
|
2016-02-18 00:34:30 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-06-29 11:02:54 +02:00
|
|
|
/* Status and error constants */
|
2016-02-18 00:34:30 +01:00
|
|
|
|
2018-06-29 11:02:54 +02: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-18 00:24:50 +02:00
|
|
|
|
2018-06-29 11:02:54 +02: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-18 00:34:30 +01:00
|
|
|
|
2016-04-18 00:24:50 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Global variables */
|
|
|
|
|
2017-07-24 13:27:09 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Helper flags for complex dependencies */
|
|
|
|
|
|
|
|
/* Indicates whether we expect mbedtls_entropy_init
|
|
|
|
* to initialize some strong entropy source. */
|
2021-04-30 13:28:22 +02:00
|
|
|
#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
|
|
|
|
( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
|
|
|
|
defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
|
|
|
|
defined(ENTROPY_NV_SEED) )
|
2017-09-07 09:09:33 +02:00
|
|
|
#define ENTROPY_HAVE_STRONG
|
2017-07-24 13:27:09 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-02-18 00:34:30 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Helper Functions */
|
2019-01-31 14:20:20 +01:00
|
|
|
|
2021-01-06 20:47:16 +01: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 16:56:42 +01:00
|
|
|
mbedtls_test_fail( msg, line_no, filename );
|
2021-01-06 20:47:16 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
|
|
|
|
|
2016-10-05 11:57:49 +02:00
|
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
2020-07-30 09:02:27 +02:00
|
|
|
static int redirect_output( FILE* out_stream, const char* path )
|
2016-10-05 11:57:49 +02:00
|
|
|
{
|
2020-07-30 09:02:27 +02:00
|
|
|
int out_fd, dup_fd;
|
|
|
|
FILE* path_stream;
|
2016-10-05 11:57:49 +02:00
|
|
|
|
2020-07-30 09:02:27 +02:00
|
|
|
out_fd = fileno( out_stream );
|
|
|
|
dup_fd = dup( out_fd );
|
|
|
|
|
|
|
|
if( dup_fd == -1 )
|
2016-10-05 11:57:49 +02:00
|
|
|
{
|
2020-07-30 09:02:27 +02:00
|
|
|
return( -1 );
|
2016-10-05 11:57:49 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:02:27 +02:00
|
|
|
path_stream = fopen( path, "w" );
|
|
|
|
if( path_stream == NULL )
|
|
|
|
{
|
|
|
|
close( dup_fd );
|
|
|
|
return( -1 );
|
|
|
|
}
|
2016-10-05 11:57:49 +02:00
|
|
|
|
2020-07-30 09:02:27 +02:00
|
|
|
fflush( out_stream );
|
|
|
|
if( dup2( fileno( path_stream ), out_fd ) == -1 )
|
2016-10-05 11:57:49 +02:00
|
|
|
{
|
2020-07-30 09:02:27 +02:00
|
|
|
close( dup_fd );
|
|
|
|
fclose( path_stream );
|
|
|
|
return( -1 );
|
2016-10-05 11:57:49 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:02:27 +02:00
|
|
|
fclose( path_stream );
|
|
|
|
return( dup_fd );
|
2016-10-05 11:57:49 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:02:27 +02:00
|
|
|
static int restore_output( FILE* out_stream, int dup_fd )
|
2016-10-05 11:57:49 +02:00
|
|
|
{
|
2020-07-30 09:02:27 +02:00
|
|
|
int out_fd = fileno( out_stream );
|
2016-10-05 11:57:49 +02:00
|
|
|
|
2020-07-30 09:02:27 +02:00
|
|
|
fflush( out_stream );
|
|
|
|
if( dup2( dup_fd, out_fd ) == -1 )
|
2016-10-05 11:57:49 +02:00
|
|
|
{
|
2020-07-30 09:02:27 +02:00
|
|
|
close( out_fd );
|
|
|
|
close( dup_fd );
|
|
|
|
return( -1 );
|
2016-10-05 11:57:49 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:02:27 +02:00
|
|
|
close( dup_fd );
|
|
|
|
return( 0 );
|
2016-10-13 00:07:30 +02:00
|
|
|
}
|
2016-10-05 11:57:49 +02:00
|
|
|
#endif /* __unix__ || __APPLE__ __MACH__ */
|