blob: ee9a562a520b3c930415ad570d456a36a21e40e6 [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
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000031#ifdef _MSC_VER
32#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010033typedef UINT8 uint8_t;
34typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000035typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000036#define strncasecmp _strnicmp
37#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000038#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020039#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000040#endif
41
Paul Bakker19343182013-08-16 13:31:10 +020042#include <string.h>
43
Janos Follath8ca53b52016-10-05 10:57:49 +010044#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
45#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000046#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010047#endif
SimonB0269dad2016-02-17 23:34:30 +000048
Azim Khand30ca132017-06-09 04:32:58 +010049/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010050typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010051{
52 uint8_t * x;
53 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010054} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010055
SimonB0269dad2016-02-17 23:34:30 +000056/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010057/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000058
Azim Khan62a5d7d2018-06-29 10:02:54 +010059#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
60#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
61#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010062
Azim Khan62a5d7d2018-06-29 10:02:54 +010063#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
64#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
65#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
66#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
67 Only int, string, binary data
68 and integer expressions are
69 allowed */
70#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
71 build */
SimonB0269dad2016-02-17 23:34:30 +000072
SimonB0269dad2016-02-17 23:34:30 +000073/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010074/* Global variables */
75
SimonB8ca7bc42016-04-17 23:24:50 +010076/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010077/* Helper flags for complex dependencies */
78
79/* Indicates whether we expect mbedtls_entropy_init
80 * to initialize some strong entropy source. */
81#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
82 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
83 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
Hanno Becker47deec42017-07-24 12:27:09 +010084 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
85 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +010086#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010087#endif
88
89
90/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000091/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050092
Gilles Peskinec85c2012021-01-06 20:47:16 +010093#if defined(MBEDTLS_PSA_CRYPTO_C)
94/** Check that no PSA Crypto key slots are in use.
95 *
96 * If any slots are in use, mark the current test as failed.
97 *
98 * \return 0 if the key store is empty, 1 otherwise.
99 */
100int test_fail_if_psa_leaking( int line_no, const char *filename )
101{
102 const char *msg = mbedtls_test_helper_is_psa_leaking( );
103 if( msg == NULL )
104 return 0;
105 else
106 {
Chris Jones9634bb12021-01-20 15:56:42 +0000107 mbedtls_test_fail( msg, line_no, filename );
Gilles Peskinec85c2012021-01-06 20:47:16 +0100108 return 1;
109 }
110}
111#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
112
Janos Follath8ca53b52016-10-05 10:57:49 +0100113#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44067f6e02020-07-30 09:02:27 +0200114static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100115{
gufe44067f6e02020-07-30 09:02:27 +0200116 int out_fd, dup_fd;
117 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100118
gufe44067f6e02020-07-30 09:02:27 +0200119 out_fd = fileno( out_stream );
120 dup_fd = dup( out_fd );
121
122 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100123 {
gufe44067f6e02020-07-30 09:02:27 +0200124 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100125 }
126
gufe44067f6e02020-07-30 09:02:27 +0200127 path_stream = fopen( path, "w" );
128 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100129 {
gufe44067f6e02020-07-30 09:02:27 +0200130 close( dup_fd );
131 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100132 }
133
gufe44067f6e02020-07-30 09:02:27 +0200134 fflush( out_stream );
135 if( dup2( fileno( path_stream ), out_fd ) == -1 )
136 {
137 close( dup_fd );
138 fclose( path_stream );
139 return( -1 );
140 }
141
142 fclose( path_stream );
143 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100144}
145
gufe44067f6e02020-07-30 09:02:27 +0200146static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100147{
gufe44067f6e02020-07-30 09:02:27 +0200148 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100149
gufe44067f6e02020-07-30 09:02:27 +0200150 fflush( out_stream );
151 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100152 {
gufe44067f6e02020-07-30 09:02:27 +0200153 close( out_fd );
154 close( dup_fd );
155 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100156 }
157
gufe44067f6e02020-07-30 09:02:27 +0200158 close( dup_fd );
159 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100160}
Janos Follath8ca53b52016-10-05 10:57:49 +0100161#endif /* __unix__ || __APPLE__ __MACH__ */