blob: ca03532ba20acd1e19724506a0d35a0cf6f70eb7 [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
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +02005#define MBEDTLS_ALLOW_PRIVATE_ACCESS
6
Ronald Cron4b8b1992020-06-09 13:52:23 +02007#include <test/macros.h>
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02008#include <test/helpers.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +02009#include <test/random.h>
Gilles Peskinef6be5902020-11-24 18:33:13 +010010#include <test/psa_crypto_helpers.h>
Ronald Cron4b8b1992020-06-09 13:52:23 +020011
Simon Butcheredb7fd92016-05-17 13:35:51 +010012#include <stdlib.h>
13
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020016#else
Rich Evans00ab4702015-02-06 13:43:58 +000017#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010019#define mbedtls_snprintf snprintf
20#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#define mbedtls_free free
22#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010023#define mbedtls_time time
24#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010025#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
26#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020027#endif
28
SimonB0269dad2016-02-17 23:34:30 +000029#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
30#include "mbedtls/memory_buffer_alloc.h"
31#endif
32
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050033#if defined(MBEDTLS_CHECK_PARAMS)
34#include "mbedtls/platform_util.h"
35#include <setjmp.h>
36#endif
37
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000038#ifdef _MSC_VER
39#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010040typedef UINT8 uint8_t;
41typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000042typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000043#define strncasecmp _strnicmp
44#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000045#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020046#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000047#endif
48
Paul Bakker19343182013-08-16 13:31:10 +020049#include <string.h>
50
Janos Follath8ca53b52016-10-05 10:57:49 +010051#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
52#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000053#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010054#endif
SimonB0269dad2016-02-17 23:34:30 +000055
Azim Khand30ca132017-06-09 04:32:58 +010056/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010057typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010058{
59 uint8_t * x;
60 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010061} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010062
SimonB0269dad2016-02-17 23:34:30 +000063/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010064/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000065
Azim Khan62a5d7d2018-06-29 10:02:54 +010066#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
67#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
68#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010069
Azim Khan62a5d7d2018-06-29 10:02:54 +010070#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
71#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
72#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
73#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
74 Only int, string, binary data
75 and integer expressions are
76 allowed */
77#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
78 build */
SimonB0269dad2016-02-17 23:34:30 +000079
SimonB0269dad2016-02-17 23:34:30 +000080/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010081/* Global variables */
82
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050083#if defined(MBEDTLS_CHECK_PARAMS)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050084jmp_buf jmp_tmp;
85#endif
86
SimonB8ca7bc42016-04-17 23:24:50 +010087/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010088/* Helper flags for complex dependencies */
89
90/* Indicates whether we expect mbedtls_entropy_init
91 * to initialize some strong entropy source. */
Mateusz Starzyk72f60df2021-04-30 13:28:22 +020092#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
93 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
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__ */