blob: ae1bc888d58ddb8778e9c755e92c603658b3673f [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
Mateusz Starzyk28c8cce2021-05-21 09:48:03 +020014#if defined (MBEDTLS_ERROR_C)
15#include "mbedtls/error.h"
16#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020019#else
Rich Evans00ab4702015-02-06 13:43:58 +000020#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010022#define mbedtls_snprintf snprintf
23#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#define mbedtls_free free
25#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010026#define mbedtls_time time
27#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010028#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
29#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020030#endif
31
SimonB0269dad2016-02-17 23:34:30 +000032#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
33#include "mbedtls/memory_buffer_alloc.h"
34#endif
35
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050036#if defined(MBEDTLS_CHECK_PARAMS)
37#include "mbedtls/platform_util.h"
38#include <setjmp.h>
39#endif
40
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000041#ifdef _MSC_VER
42#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010043typedef UINT8 uint8_t;
44typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000045typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000046#define strncasecmp _strnicmp
47#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000048#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020049#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000050#endif
51
Paul Bakker19343182013-08-16 13:31:10 +020052#include <string.h>
53
Janos Follath8ca53b52016-10-05 10:57:49 +010054#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
55#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000056#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010057#endif
SimonB0269dad2016-02-17 23:34:30 +000058
Azim Khand30ca132017-06-09 04:32:58 +010059/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010060typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010061{
62 uint8_t * x;
63 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010064} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010065
SimonB0269dad2016-02-17 23:34:30 +000066/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010067/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000068
Azim Khan62a5d7d2018-06-29 10:02:54 +010069#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
70#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
71#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010072
Azim Khan62a5d7d2018-06-29 10:02:54 +010073#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
74#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
75#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
76#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
77 Only int, string, binary data
78 and integer expressions are
79 allowed */
80#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
81 build */
SimonB0269dad2016-02-17 23:34:30 +000082
SimonB0269dad2016-02-17 23:34:30 +000083/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010084/* Global variables */
85
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086#if defined(MBEDTLS_CHECK_PARAMS)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050087jmp_buf jmp_tmp;
88#endif
89
SimonB8ca7bc42016-04-17 23:24:50 +010090/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010091/* Helper flags for complex dependencies */
92
93/* Indicates whether we expect mbedtls_entropy_init
94 * to initialize some strong entropy source. */
Mateusz Starzyk72f60df2021-04-30 13:28:22 +020095#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
96 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
97 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
98 defined(ENTROPY_NV_SEED) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +010099#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100100#endif
101
102
103/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000104/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500105
Gilles Peskinec85c2012021-01-06 20:47:16 +0100106#if defined(MBEDTLS_PSA_CRYPTO_C)
107/** Check that no PSA Crypto key slots are in use.
108 *
109 * If any slots are in use, mark the current test as failed.
110 *
111 * \return 0 if the key store is empty, 1 otherwise.
112 */
113int test_fail_if_psa_leaking( int line_no, const char *filename )
114{
115 const char *msg = mbedtls_test_helper_is_psa_leaking( );
116 if( msg == NULL )
117 return 0;
118 else
119 {
Chris Jones9634bb12021-01-20 15:56:42 +0000120 mbedtls_test_fail( msg, line_no, filename );
Gilles Peskinec85c2012021-01-06 20:47:16 +0100121 return 1;
122 }
123}
124#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
125
Janos Follath8ca53b52016-10-05 10:57:49 +0100126#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44067f6e02020-07-30 09:02:27 +0200127static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100128{
gufe44067f6e02020-07-30 09:02:27 +0200129 int out_fd, dup_fd;
130 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100131
gufe44067f6e02020-07-30 09:02:27 +0200132 out_fd = fileno( out_stream );
133 dup_fd = dup( out_fd );
134
135 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100136 {
gufe44067f6e02020-07-30 09:02:27 +0200137 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100138 }
139
gufe44067f6e02020-07-30 09:02:27 +0200140 path_stream = fopen( path, "w" );
141 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100142 {
gufe44067f6e02020-07-30 09:02:27 +0200143 close( dup_fd );
144 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100145 }
146
gufe44067f6e02020-07-30 09:02:27 +0200147 fflush( out_stream );
148 if( dup2( fileno( path_stream ), out_fd ) == -1 )
149 {
150 close( dup_fd );
151 fclose( path_stream );
152 return( -1 );
153 }
154
155 fclose( path_stream );
156 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100157}
158
gufe44067f6e02020-07-30 09:02:27 +0200159static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100160{
gufe44067f6e02020-07-30 09:02:27 +0200161 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100162
gufe44067f6e02020-07-30 09:02:27 +0200163 fflush( out_stream );
164 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100165 {
gufe44067f6e02020-07-30 09:02:27 +0200166 close( out_fd );
167 close( dup_fd );
168 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100169 }
170
gufe44067f6e02020-07-30 09:02:27 +0200171 close( dup_fd );
172 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100173}
Janos Follath8ca53b52016-10-05 10:57:49 +0100174#endif /* __unix__ || __APPLE__ __MACH__ */