blob: 91ad925fbfa6065976aeee58309456fdb790ae9a [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 Cron4b8b1992020-06-09 13:52:23 +02005#include <test/macros.h>
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02006#include <test/helpers.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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000013#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020014#else
Rich Evans00ab4702015-02-06 13:43:58 +000015#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010017#define mbedtls_snprintf snprintf
18#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#define mbedtls_free free
20#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010021#define mbedtls_time time
22#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010023#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
24#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020025#endif
26
SimonB0269dad2016-02-17 23:34:30 +000027#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
28#include "mbedtls/memory_buffer_alloc.h"
29#endif
30
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050031#if defined(MBEDTLS_CHECK_PARAMS)
32#include "mbedtls/platform_util.h"
33#include <setjmp.h>
34#endif
35
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000036#ifdef _MSC_VER
37#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010038typedef UINT8 uint8_t;
39typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000040typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000041#define strncasecmp _strnicmp
42#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000043#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020044#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000045#endif
46
Paul Bakker19343182013-08-16 13:31:10 +020047#include <string.h>
48
Janos Follath8ca53b52016-10-05 10:57:49 +010049#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
50#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000051#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010052#endif
SimonB0269dad2016-02-17 23:34:30 +000053
Azim Khand30ca132017-06-09 04:32:58 +010054/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010055typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010056{
57 uint8_t * x;
58 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010059} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010060
SimonB0269dad2016-02-17 23:34:30 +000061/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010062/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000063
Azim Khan62a5d7d2018-06-29 10:02:54 +010064#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
65#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
66#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010067
Azim Khan62a5d7d2018-06-29 10:02:54 +010068#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
69#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
70#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
71#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
72 Only int, string, binary data
73 and integer expressions are
74 allowed */
75#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
76 build */
SimonB0269dad2016-02-17 23:34:30 +000077
SimonB0269dad2016-02-17 23:34:30 +000078/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010079/* Global variables */
80
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050081#if defined(MBEDTLS_CHECK_PARAMS)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050082jmp_buf jmp_tmp;
83#endif
84
SimonB8ca7bc42016-04-17 23:24:50 +010085/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010086/* Helper flags for complex dependencies */
87
88/* Indicates whether we expect mbedtls_entropy_init
89 * to initialize some strong entropy source. */
90#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
91 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
92 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
93 defined(MBEDTLS_HAVEGE_C) || \
94 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
95 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +010096#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010097#endif
98
99
100/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000101/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500102
Gilles Peskinec85c2012021-01-06 20:47:16 +0100103#if defined(MBEDTLS_PSA_CRYPTO_C)
104/** Check that no PSA Crypto key slots are in use.
105 *
106 * If any slots are in use, mark the current test as failed.
107 *
108 * \return 0 if the key store is empty, 1 otherwise.
109 */
110int test_fail_if_psa_leaking( int line_no, const char *filename )
111{
112 const char *msg = mbedtls_test_helper_is_psa_leaking( );
113 if( msg == NULL )
114 return 0;
115 else
116 {
Chris Jones9634bb12021-01-20 15:56:42 +0000117 mbedtls_test_fail( msg, line_no, filename );
Gilles Peskinec85c2012021-01-06 20:47:16 +0100118 return 1;
119 }
120}
121#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
122
Janos Follath8ca53b52016-10-05 10:57:49 +0100123#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44067f6e02020-07-30 09:02:27 +0200124static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100125{
gufe44067f6e02020-07-30 09:02:27 +0200126 int out_fd, dup_fd;
127 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100128
gufe44067f6e02020-07-30 09:02:27 +0200129 out_fd = fileno( out_stream );
130 dup_fd = dup( out_fd );
131
132 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100133 {
gufe44067f6e02020-07-30 09:02:27 +0200134 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100135 }
136
gufe44067f6e02020-07-30 09:02:27 +0200137 path_stream = fopen( path, "w" );
138 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100139 {
gufe44067f6e02020-07-30 09:02:27 +0200140 close( dup_fd );
141 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100142 }
143
gufe44067f6e02020-07-30 09:02:27 +0200144 fflush( out_stream );
145 if( dup2( fileno( path_stream ), out_fd ) == -1 )
146 {
147 close( dup_fd );
148 fclose( path_stream );
149 return( -1 );
150 }
151
152 fclose( path_stream );
153 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100154}
155
gufe44067f6e02020-07-30 09:02:27 +0200156static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100157{
gufe44067f6e02020-07-30 09:02:27 +0200158 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100159
gufe44067f6e02020-07-30 09:02:27 +0200160 fflush( out_stream );
161 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100162 {
gufe44067f6e02020-07-30 09:02:27 +0200163 close( out_fd );
164 close( dup_fd );
165 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100166 }
167
gufe44067f6e02020-07-30 09:02:27 +0200168 close( dup_fd );
169 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100170}
Janos Follath8ca53b52016-10-05 10:57:49 +0100171#endif /* __unix__ || __APPLE__ __MACH__ */