blob: 5c22007eebe5f9c63197f1b241a6dace76be8f49 [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 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020012#if defined(MBEDTLS_ERROR_C)
13# include "mbedtls/error.h"
Mateusz Starzyk28c8cce2021-05-21 09:48:03 +020014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020016# include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020017#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020018# include <stdio.h>
19# define mbedtls_fprintf fprintf
20# define mbedtls_snprintf snprintf
21# define mbedtls_calloc calloc
22# define mbedtls_free free
23# define mbedtls_exit exit
24# define mbedtls_time time
25# define mbedtls_time_t time_t
26# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
27# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020028#endif
29
SimonB0269dad2016-02-17 23:34:30 +000030#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020031# include "mbedtls/memory_buffer_alloc.h"
SimonB0269dad2016-02-17 23:34:30 +000032#endif
33
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000034#ifdef _MSC_VER
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020035# include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010036typedef UINT8 uint8_t;
37typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000038typedef UINT32 uint32_t;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020039# define strncasecmp _strnicmp
40# define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000041#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042# include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000043#endif
44
Paul Bakker19343182013-08-16 13:31:10 +020045#include <string.h>
46
Janos Follath8ca53b52016-10-05 10:57:49 +010047#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048# include <unistd.h>
49# include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010050#endif
SimonB0269dad2016-02-17 23:34:30 +000051
Azim Khand30ca132017-06-09 04:32:58 +010052/* Type for Hex parameters */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053typedef struct data_tag {
54 uint8_t *x;
55 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010056} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010057
SimonB0269dad2016-02-17 23:34:30 +000058/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010059/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000060
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020061#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
62#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
63#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010064
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
66#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
67#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
68#define DISPATCH_INVALID_TEST_DATA \
69 -4 /* Invalid test parameter type. \
70 Only int, string, binary data \
71 and integer expressions are \
72 allowed */
73#define DISPATCH_UNSUPPORTED_SUITE \
74 -5 /* Test suite not supported by the \
75 build */
SimonB0269dad2016-02-17 23:34:30 +000076
SimonB0269dad2016-02-17 23:34:30 +000077/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010078/* Global variables */
79
SimonB8ca7bc42016-04-17 23:24:50 +010080/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010081/* Helper flags for complex dependencies */
82
83/* Indicates whether we expect mbedtls_entropy_init
84 * to initialize some strong entropy source. */
Mateusz Starzyk72f60df2021-04-30 13:28:22 +020085#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086 (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
87 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || defined(ENTROPY_NV_SEED))
88# define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010089#endif
90
Hanno Becker47deec42017-07-24 12:27:09 +010091/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000092/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093
Gilles Peskinec85c2012021-01-06 20:47:16 +010094#if defined(MBEDTLS_PSA_CRYPTO_C)
95/** Check that no PSA Crypto key slots are in use.
96 *
97 * If any slots are in use, mark the current test as failed.
98 *
99 * \return 0 if the key store is empty, 1 otherwise.
100 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200101int test_fail_if_psa_leaking(int line_no, const char *filename)
Gilles Peskinec85c2012021-01-06 20:47:16 +0100102{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200103 const char *msg = mbedtls_test_helper_is_psa_leaking();
104 if (msg == NULL)
Gilles Peskinec85c2012021-01-06 20:47:16 +0100105 return 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200106 else {
107 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__))
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +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;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200117 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100118
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200119 out_fd = fileno(out_stream);
120 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +0200121
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200122 if (dup_fd == -1) {
123 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100124 }
125
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200126 path_stream = fopen(path, "w");
127 if (path_stream == NULL) {
128 close(dup_fd);
129 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100130 }
131
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200132 fflush(out_stream);
133 if (dup2(fileno(path_stream), out_fd) == -1) {
134 close(dup_fd);
135 fclose(path_stream);
136 return -1;
gufe44067f6e02020-07-30 09:02:27 +0200137 }
138
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200139 fclose(path_stream);
140 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +0100141}
142
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200143static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +0100144{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200145 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +0100146
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200147 fflush(out_stream);
148 if (dup2(dup_fd, out_fd) == -1) {
149 close(out_fd);
150 close(dup_fd);
151 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100152 }
153
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154 close(dup_fd);
155 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100156}
Janos Follath8ca53b52016-10-05 10:57:49 +0100157#endif /* __unix__ || __APPLE__ __MACH__ */