blob: c085b18846dccf0a6bd8f1c7555c94da7ad5cf77 [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é-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020013
SimonB0269dad2016-02-17 23:34:30 +000014#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
15#include "mbedtls/memory_buffer_alloc.h"
16#endif
17
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050018#if defined(MBEDTLS_CHECK_PARAMS)
19#include "mbedtls/platform_util.h"
20#include <setjmp.h>
21#endif
22
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000023#ifdef _MSC_VER
24#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010025typedef UINT8 uint8_t;
26typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000027typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000028#define strncasecmp _strnicmp
29#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000030#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020031#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000032#endif
33
Paul Bakker19343182013-08-16 13:31:10 +020034#include <string.h>
35
Janos Follath8ca53b52016-10-05 10:57:49 +010036#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
37#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000038#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010039#endif
SimonB0269dad2016-02-17 23:34:30 +000040
41/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010042/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000043
Azim Khan62a5d7d2018-06-29 10:02:54 +010044#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
45#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
46#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010047
Azim Khan62a5d7d2018-06-29 10:02:54 +010048#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
49#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
50#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
51#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
52 Only int, string, binary data
53 and integer expressions are
54 allowed */
55#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
56 build */
SimonB0269dad2016-02-17 23:34:30 +000057
SimonB0269dad2016-02-17 23:34:30 +000058/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010059/* Global variables */
60
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050061#if defined(MBEDTLS_CHECK_PARAMS)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050062jmp_buf jmp_tmp;
63#endif
64
SimonB8ca7bc42016-04-17 23:24:50 +010065/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010066/* Helper flags for complex dependencies */
67
68/* Indicates whether we expect mbedtls_entropy_init
69 * to initialize some strong entropy source. */
70#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
71 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
72 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
73 defined(MBEDTLS_HAVEGE_C) || \
74 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
75 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +010076#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010077#endif
78
79
80/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000081/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050082
Gilles Peskinec85c2012021-01-06 20:47:16 +010083#if defined(MBEDTLS_PSA_CRYPTO_C)
84/** Check that no PSA Crypto key slots are in use.
85 *
86 * If any slots are in use, mark the current test as failed.
87 *
88 * \return 0 if the key store is empty, 1 otherwise.
89 */
90int test_fail_if_psa_leaking( int line_no, const char *filename )
91{
92 const char *msg = mbedtls_test_helper_is_psa_leaking( );
93 if( msg == NULL )
94 return 0;
95 else
96 {
Chris Jones9634bb12021-01-20 15:56:42 +000097 mbedtls_test_fail( msg, line_no, filename );
Gilles Peskinec85c2012021-01-06 20:47:16 +010098 return 1;
99 }
100}
101#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
102
Janos Follath8ca53b52016-10-05 10:57:49 +0100103#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44067f6e02020-07-30 09:02:27 +0200104static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100105{
gufe44067f6e02020-07-30 09:02:27 +0200106 int out_fd, dup_fd;
107 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100108
gufe44067f6e02020-07-30 09:02:27 +0200109 out_fd = fileno( out_stream );
110 dup_fd = dup( out_fd );
111
112 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100113 {
gufe44067f6e02020-07-30 09:02:27 +0200114 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100115 }
116
gufe44067f6e02020-07-30 09:02:27 +0200117 path_stream = fopen( path, "w" );
118 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100119 {
gufe44067f6e02020-07-30 09:02:27 +0200120 close( dup_fd );
121 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100122 }
123
gufe44067f6e02020-07-30 09:02:27 +0200124 fflush( out_stream );
125 if( dup2( fileno( path_stream ), out_fd ) == -1 )
126 {
127 close( dup_fd );
128 fclose( path_stream );
129 return( -1 );
130 }
131
132 fclose( path_stream );
133 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100134}
135
gufe44067f6e02020-07-30 09:02:27 +0200136static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100137{
gufe44067f6e02020-07-30 09:02:27 +0200138 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100139
gufe44067f6e02020-07-30 09:02:27 +0200140 fflush( out_stream );
141 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100142 {
gufe44067f6e02020-07-30 09:02:27 +0200143 close( out_fd );
144 close( dup_fd );
145 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100146 }
147
gufe44067f6e02020-07-30 09:02:27 +0200148 close( dup_fd );
149 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100150}
Janos Follath8ca53b52016-10-05 10:57:49 +0100151#endif /* __unix__ || __APPLE__ __MACH__ */