blob: eb0dc03fb484b67c0f12ead07be293f3dc2c52e3 [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) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 (!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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090int test_fail_if_psa_leaking(int line_no, const char *filename)
Gilles Peskinec85c2012021-01-06 20:47:16 +010091{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 const char *msg = mbedtls_test_helper_is_psa_leaking();
93 if (msg == NULL) {
Gilles Peskinec85c2012021-01-06 20:47:16 +010094 return 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 } else {
96 mbedtls_test_fail(msg, line_no, filename);
Gilles Peskinec85c2012021-01-06 20:47:16 +010097 return 1;
98 }
99}
100#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
101
Janos Follath8ca53b52016-10-05 10:57:49 +0100102#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103static int redirect_output(FILE *out_stream, const char *path)
Janos Follath8ca53b52016-10-05 10:57:49 +0100104{
gufe44067f6e02020-07-30 09:02:27 +0200105 int out_fd, dup_fd;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 out_fd = fileno(out_stream);
109 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +0200110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 if (dup_fd == -1) {
112 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100113 }
114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 path_stream = fopen(path, "w");
116 if (path_stream == NULL) {
117 close(dup_fd);
118 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100119 }
120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 fflush(out_stream);
122 if (dup2(fileno(path_stream), out_fd) == -1) {
123 close(dup_fd);
124 fclose(path_stream);
125 return -1;
gufe44067f6e02020-07-30 09:02:27 +0200126 }
127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 fclose(path_stream);
129 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +0100130}
131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +0100133{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +0100135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 fflush(out_stream);
137 if (dup2(dup_fd, out_fd) == -1) {
138 close(out_fd);
139 close(dup_fd);
140 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100141 }
142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 close(dup_fd);
144 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100145}
Janos Follath8ca53b52016-10-05 10:57:49 +0100146#endif /* __unix__ || __APPLE__ __MACH__ */