blob: 27d92492b7fad2548a14d16a747151369635de26 [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) || \
Hanno Becker47deec42017-07-24 12:27:09 +010093 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
94 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +010095#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010096#endif
97
98
99/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000100/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101
Gilles Peskinec85c2012021-01-06 20:47:16 +0100102#if defined(MBEDTLS_PSA_CRYPTO_C)
103/** Check that no PSA Crypto key slots are in use.
104 *
105 * If any slots are in use, mark the current test as failed.
106 *
107 * \return 0 if the key store is empty, 1 otherwise.
108 */
109int test_fail_if_psa_leaking( int line_no, const char *filename )
110{
111 const char *msg = mbedtls_test_helper_is_psa_leaking( );
112 if( msg == NULL )
113 return 0;
114 else
115 {
Chris Jones9634bb12021-01-20 15:56:42 +0000116 mbedtls_test_fail( msg, line_no, filename );
Gilles Peskinec85c2012021-01-06 20:47:16 +0100117 return 1;
118 }
119}
120#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
121
Janos Follath8ca53b52016-10-05 10:57:49 +0100122#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44067f6e02020-07-30 09:02:27 +0200123static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100124{
gufe44067f6e02020-07-30 09:02:27 +0200125 int out_fd, dup_fd;
126 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100127
gufe44067f6e02020-07-30 09:02:27 +0200128 out_fd = fileno( out_stream );
129 dup_fd = dup( out_fd );
130
131 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100132 {
gufe44067f6e02020-07-30 09:02:27 +0200133 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100134 }
135
gufe44067f6e02020-07-30 09:02:27 +0200136 path_stream = fopen( path, "w" );
137 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100138 {
gufe44067f6e02020-07-30 09:02:27 +0200139 close( dup_fd );
140 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100141 }
142
gufe44067f6e02020-07-30 09:02:27 +0200143 fflush( out_stream );
144 if( dup2( fileno( path_stream ), out_fd ) == -1 )
145 {
146 close( dup_fd );
147 fclose( path_stream );
148 return( -1 );
149 }
150
151 fclose( path_stream );
152 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100153}
154
gufe44067f6e02020-07-30 09:02:27 +0200155static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100156{
gufe44067f6e02020-07-30 09:02:27 +0200157 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100158
gufe44067f6e02020-07-30 09:02:27 +0200159 fflush( out_stream );
160 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100161 {
gufe44067f6e02020-07-30 09:02:27 +0200162 close( out_fd );
163 close( dup_fd );
164 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100165 }
166
gufe44067f6e02020-07-30 09:02:27 +0200167 close( dup_fd );
168 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100169}
Janos Follath8ca53b52016-10-05 10:57:49 +0100170#endif /* __unix__ || __APPLE__ __MACH__ */