blob: 82495644e309a8489d3cfd297afaa49144f37c2e [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 Cronb6d6d4c2020-06-03 10:11:18 +02005#include <test/helpers.h>
Mateusz Starzyk3911e702021-05-27 14:44:31 +02006#include <test/macros.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +02007#include <test/random.h>
Gilles Peskine881447d2022-12-08 15:24:52 +01008#include <test/bignum_helpers.h>
Gilles Peskinef6be5902020-11-24 18:33:13 +01009#include <test/psa_crypto_helpers.h>
Ronald Cron4b8b1992020-06-09 13:52:23 +020010
Simon Butcheredb7fd92016-05-17 13:35:51 +010011#include <stdlib.h>
12
Mateusz Starzyk28c8cce2021-05-21 09:48:03 +020013#if defined (MBEDTLS_ERROR_C)
14#include "mbedtls/error.h"
15#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000016#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020017
SimonB0269dad2016-02-17 23:34:30 +000018#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
19#include "mbedtls/memory_buffer_alloc.h"
20#endif
21
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000022#ifdef _MSC_VER
23#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010024typedef UINT8 uint8_t;
25typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000026typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000027#define strncasecmp _strnicmp
28#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000029#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020030#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000031#endif
32
Paul Bakker19343182013-08-16 13:31:10 +020033#include <string.h>
34
Cameron Cawleyea5496c2021-03-08 23:29:26 +000035#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__MINGW32__)
36#include <strings.h>
37#endif
38
Janos Follath8ca53b52016-10-05 10:57:49 +010039#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
40#include <unistd.h>
41#endif
SimonB0269dad2016-02-17 23:34:30 +000042
43/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010044/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000045
Azim Khan62a5d7d2018-06-29 10:02:54 +010046#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
47#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
48#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010049
Azim Khan62a5d7d2018-06-29 10:02:54 +010050#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
51#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
52#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
53#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
54 Only int, string, binary data
55 and integer expressions are
56 allowed */
57#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
58 build */
SimonB0269dad2016-02-17 23:34:30 +000059
SimonB0269dad2016-02-17 23:34:30 +000060/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010061/* Global variables */
62
SimonB8ca7bc42016-04-17 23:24:50 +010063/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010064/* Helper flags for complex dependencies */
65
66/* Indicates whether we expect mbedtls_entropy_init
67 * to initialize some strong entropy source. */
Mateusz Starzyk72f60df2021-04-30 13:28:22 +020068#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
69 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
70 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
71 defined(ENTROPY_NV_SEED) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +010072#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010073#endif
74
75
76/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000077/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078
Gilles Peskinec85c2012021-01-06 20:47:16 +010079#if defined(MBEDTLS_PSA_CRYPTO_C)
80/** Check that no PSA Crypto key slots are in use.
81 *
82 * If any slots are in use, mark the current test as failed.
83 *
84 * \return 0 if the key store is empty, 1 otherwise.
85 */
86int test_fail_if_psa_leaking( int line_no, const char *filename )
87{
88 const char *msg = mbedtls_test_helper_is_psa_leaking( );
89 if( msg == NULL )
90 return 0;
91 else
92 {
Chris Jones9634bb12021-01-20 15:56:42 +000093 mbedtls_test_fail( msg, line_no, filename );
Gilles Peskinec85c2012021-01-06 20:47:16 +010094 return 1;
95 }
96}
97#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
98
Janos Follath8ca53b52016-10-05 10:57:49 +010099#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44067f6e02020-07-30 09:02:27 +0200100static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100101{
gufe44067f6e02020-07-30 09:02:27 +0200102 int out_fd, dup_fd;
103 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100104
gufe44067f6e02020-07-30 09:02:27 +0200105 out_fd = fileno( out_stream );
106 dup_fd = dup( out_fd );
107
108 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100109 {
gufe44067f6e02020-07-30 09:02:27 +0200110 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100111 }
112
gufe44067f6e02020-07-30 09:02:27 +0200113 path_stream = fopen( path, "w" );
114 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100115 {
gufe44067f6e02020-07-30 09:02:27 +0200116 close( dup_fd );
117 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100118 }
119
gufe44067f6e02020-07-30 09:02:27 +0200120 fflush( out_stream );
121 if( dup2( fileno( path_stream ), out_fd ) == -1 )
122 {
123 close( dup_fd );
124 fclose( path_stream );
125 return( -1 );
126 }
127
128 fclose( path_stream );
129 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100130}
131
gufe44067f6e02020-07-30 09:02:27 +0200132static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100133{
gufe44067f6e02020-07-30 09:02:27 +0200134 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100135
gufe44067f6e02020-07-30 09:02:27 +0200136 fflush( out_stream );
137 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100138 {
gufe44067f6e02020-07-30 09:02:27 +0200139 close( out_fd );
140 close( dup_fd );
141 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100142 }
143
gufe44067f6e02020-07-30 09:02:27 +0200144 close( dup_fd );
145 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100146}
Janos Follath8ca53b52016-10-05 10:57:49 +0100147#endif /* __unix__ || __APPLE__ __MACH__ */