1#line 2 "suites/helpers.function" 2/*----------------------------------------------------------------------------*/ 3/* Headers */ 4 5#include <test/helpers.h> 6#include <test/macros.h> 7#include <test/random.h> 8#include <test/psa_crypto_helpers.h> 9 10#include <stdlib.h> 11 12#if defined (MBEDTLS_ERROR_C) 13#include "mbedtls/error.h" 14#endif 15#if defined(MBEDTLS_PLATFORM_C) 16#include "mbedtls/platform.h" 17#else 18#include <stdio.h> 19#define mbedtls_fprintf fprintf 20#define mbedtls_snprintf snprintf 21#define mbedtls_calloc calloc 22#define mbedtls_free free 23#define mbedtls_exit exit 24#define mbedtls_time time 25#define mbedtls_time_t time_t 26#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS 27#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE 28#endif 29 30#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) 31#include "mbedtls/memory_buffer_alloc.h" 32#endif 33 34#ifdef _MSC_VER 35#include <basetsd.h> 36typedef UINT8 uint8_t; 37typedef INT32 int32_t; 38typedef UINT32 uint32_t; 39#define strncasecmp _strnicmp 40#define strcasecmp _stricmp 41#else 42#include <stdint.h> 43#endif 44 45#include <string.h> 46 47#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 48#include <unistd.h> 49#include <strings.h> 50#endif 51 52/* Type for Hex parameters */ 53typedef struct data_tag 54{ 55 uint8_t * x; 56 uint32_t len; 57} data_t; 58 59/*----------------------------------------------------------------------------*/ 60/* Status and error constants */ 61 62#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */ 63#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */ 64#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */ 65 66#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */ 67#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */ 68#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */ 69#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type. 70 Only int, string, binary data 71 and integer expressions are 72 allowed */ 73#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the 74 build */ 75 76/*----------------------------------------------------------------------------*/ 77/* Global variables */ 78 79/*----------------------------------------------------------------------------*/ 80/* Helper flags for complex dependencies */ 81 82/* Indicates whether we expect mbedtls_entropy_init 83 * to initialize some strong entropy source. */ 84#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \ 85 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \ 86 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \ 87 defined(ENTROPY_NV_SEED) ) 88#define ENTROPY_HAVE_STRONG 89#endif 90 91 92/*----------------------------------------------------------------------------*/ 93/* Helper Functions */ 94 95#if defined(MBEDTLS_PSA_CRYPTO_C) 96/** Check that no PSA Crypto key slots are in use. 97 * 98 * If any slots are in use, mark the current test as failed. 99 * 100 * \return 0 if the key store is empty, 1 otherwise. 101 */ 102int test_fail_if_psa_leaking( int line_no, const char *filename ) 103{ 104 const char *msg = mbedtls_test_helper_is_psa_leaking( ); 105 if( msg == NULL ) 106 return 0; 107 else 108 { 109 mbedtls_test_fail( msg, line_no, filename ); 110 return 1; 111 } 112} 113#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */ 114 115#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 116static int redirect_output( FILE* out_stream, const char* path ) 117{ 118 int out_fd, dup_fd; 119 FILE* path_stream; 120 121 out_fd = fileno( out_stream ); 122 dup_fd = dup( out_fd ); 123 124 if( dup_fd == -1 ) 125 { 126 return( -1 ); 127 } 128 129 path_stream = fopen( path, "w" ); 130 if( path_stream == NULL ) 131 { 132 close( dup_fd ); 133 return( -1 ); 134 } 135 136 fflush( out_stream ); 137 if( dup2( fileno( path_stream ), out_fd ) == -1 ) 138 { 139 close( dup_fd ); 140 fclose( path_stream ); 141 return( -1 ); 142 } 143 144 fclose( path_stream ); 145 return( dup_fd ); 146} 147 148static int restore_output( FILE* out_stream, int dup_fd ) 149{ 150 int out_fd = fileno( out_stream ); 151 152 fflush( out_stream ); 153 if( dup2( dup_fd, out_fd ) == -1 ) 154 { 155 close( out_fd ); 156 close( dup_fd ); 157 return( -1 ); 158 } 159 160 close( dup_fd ); 161 return( 0 ); 162} 163#endif /* __unix__ || __APPLE__ __MACH__ */ 164