blob: 2a7575217baa03803bc7d82a562612cdfd60b651 [file] [log] [blame]
Azim Khanec024482017-05-09 17:20:21 +01001#line 2 "suites/helpers.function"
SimonB0269dad2016-02-17 23:34:30 +00002/*----------------------------------------------------------------------------*/
3/* Headers */
4
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02005#include <test/helpers.h>
Mateusz Starzyk3911e702021-05-27 14:44:31 +02006#include <test/macros.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +02007#include <test/random.h>
Gilles Peskinef6be5902020-11-24 18:33:13 +01008#include <test/psa_crypto_helpers.h>
Ronald Cron4b8b1992020-06-09 13:52:23 +02009
Simon Butcheredb7fd92016-05-17 13:35:51 +010010#include <stdlib.h>
11
Mateusz Starzyk28c8cce2021-05-21 09:48:03 +020012#if defined (MBEDTLS_ERROR_C)
13#include "mbedtls/error.h"
14#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000016#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020017#else
Rich Evans00ab4702015-02-06 13:43:58 +000018#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010020#define mbedtls_snprintf snprintf
21#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#define mbedtls_free free
23#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010024#define mbedtls_time time
25#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010026#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
27#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020028#endif
29
SimonB0269dad2016-02-17 23:34:30 +000030#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
31#include "mbedtls/memory_buffer_alloc.h"
32#endif
33
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050034#if defined(MBEDTLS_CHECK_PARAMS)
35#include "mbedtls/platform_util.h"
36#include <setjmp.h>
37#endif
38
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000039#ifdef _MSC_VER
40#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010041typedef UINT8 uint8_t;
42typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000043typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000044#define strncasecmp _strnicmp
45#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000046#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020047#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000048#endif
49
Paul Bakker19343182013-08-16 13:31:10 +020050#include <string.h>
51
Janos Follath8ca53b52016-10-05 10:57:49 +010052#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
53#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000054#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010055#endif
SimonB0269dad2016-02-17 23:34:30 +000056
Azim Khand30ca132017-06-09 04:32:58 +010057/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010058typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010059{
60 uint8_t * x;
61 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010062} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010063
SimonB0269dad2016-02-17 23:34:30 +000064/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010065/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000066
Azim Khan62a5d7d2018-06-29 10:02:54 +010067#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
68#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
69#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010070
Azim Khan62a5d7d2018-06-29 10:02:54 +010071#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
72#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
73#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
74#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
75 Only int, string, binary data
76 and integer expressions are
77 allowed */
78#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
79 build */
SimonB0269dad2016-02-17 23:34:30 +000080
SimonB0269dad2016-02-17 23:34:30 +000081/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010082/* Global variables */
83
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050084#if defined(MBEDTLS_CHECK_PARAMS)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050085jmp_buf jmp_tmp;
86#endif
87
SimonB8ca7bc42016-04-17 23:24:50 +010088/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010089/* Helper flags for complex dependencies */
90
91/* Indicates whether we expect mbedtls_entropy_init
92 * to initialize some strong entropy source. */
Mateusz Starzyk72f60df2021-04-30 13:28:22 +020093#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
94 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
95 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
96 defined(ENTROPY_NV_SEED) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +010097#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010098#endif
99
100
101/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000102/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500103
Gilles Peskinec85c2012021-01-06 20:47:16 +0100104#if defined(MBEDTLS_PSA_CRYPTO_C)
105/** Check that no PSA Crypto key slots are in use.
106 *
107 * If any slots are in use, mark the current test as failed.
108 *
109 * \return 0 if the key store is empty, 1 otherwise.
110 */
111int test_fail_if_psa_leaking( int line_no, const char *filename )
112{
113 const char *msg = mbedtls_test_helper_is_psa_leaking( );
114 if( msg == NULL )
115 return 0;
116 else
117 {
Chris Jones9634bb12021-01-20 15:56:42 +0000118 mbedtls_test_fail( msg, line_no, filename );
Gilles Peskinec85c2012021-01-06 20:47:16 +0100119 return 1;
120 }
121}
122#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
123
Janos Follath8ca53b52016-10-05 10:57:49 +0100124#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44067f6e02020-07-30 09:02:27 +0200125static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100126{
gufe44067f6e02020-07-30 09:02:27 +0200127 int out_fd, dup_fd;
128 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100129
gufe44067f6e02020-07-30 09:02:27 +0200130 out_fd = fileno( out_stream );
131 dup_fd = dup( out_fd );
132
133 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100134 {
gufe44067f6e02020-07-30 09:02:27 +0200135 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100136 }
137
gufe44067f6e02020-07-30 09:02:27 +0200138 path_stream = fopen( path, "w" );
139 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100140 {
gufe44067f6e02020-07-30 09:02:27 +0200141 close( dup_fd );
142 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100143 }
144
gufe44067f6e02020-07-30 09:02:27 +0200145 fflush( out_stream );
146 if( dup2( fileno( path_stream ), out_fd ) == -1 )
147 {
148 close( dup_fd );
149 fclose( path_stream );
150 return( -1 );
151 }
152
153 fclose( path_stream );
154 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100155}
156
gufe44067f6e02020-07-30 09:02:27 +0200157static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100158{
gufe44067f6e02020-07-30 09:02:27 +0200159 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100160
gufe44067f6e02020-07-30 09:02:27 +0200161 fflush( out_stream );
162 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100163 {
gufe44067f6e02020-07-30 09:02:27 +0200164 close( out_fd );
165 close( dup_fd );
166 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100167 }
168
gufe44067f6e02020-07-30 09:02:27 +0200169 close( dup_fd );
170 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100171}
Janos Follath8ca53b52016-10-05 10:57:49 +0100172#endif /* __unix__ || __APPLE__ __MACH__ */