blob: 313459e4fa62e6413bb2be3f0475cabda87c4a9c [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
Gilles Peskine449bd832023-01-11 14:50:10 +010013#if defined(MBEDTLS_ERROR_C)
Mateusz Starzyk28c8cce2021-05-21 09:48:03 +020014#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) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010069 (!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 */
Gilles Peskine449bd832023-01-11 14:50:10 +010086int test_fail_if_psa_leaking(int line_no, const char *filename)
Gilles Peskinec85c2012021-01-06 20:47:16 +010087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 const char *msg = mbedtls_test_helper_is_psa_leaking();
89 if (msg == NULL) {
Gilles Peskinec85c2012021-01-06 20:47:16 +010090 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010091 } else {
92 mbedtls_test_fail(msg, line_no, filename);
Gilles Peskinec85c2012021-01-06 20:47:16 +010093 return 1;
94 }
95}
96#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
97
Janos Follath8ca53b52016-10-05 10:57:49 +010098#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine449bd832023-01-11 14:50:10 +010099static int redirect_output(FILE *out_stream, const char *path)
Janos Follath8ca53b52016-10-05 10:57:49 +0100100{
gufe44067f6e02020-07-30 09:02:27 +0200101 int out_fd, dup_fd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 out_fd = fileno(out_stream);
105 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +0200106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (dup_fd == -1) {
108 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100109 }
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 path_stream = fopen(path, "w");
112 if (path_stream == NULL) {
113 close(dup_fd);
114 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100115 }
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 fflush(out_stream);
118 if (dup2(fileno(path_stream), out_fd) == -1) {
119 close(dup_fd);
120 fclose(path_stream);
121 return -1;
gufe44067f6e02020-07-30 09:02:27 +0200122 }
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 fclose(path_stream);
125 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +0100126}
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +0100129{
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +0100131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 fflush(out_stream);
133 if (dup2(dup_fd, out_fd) == -1) {
134 close(out_fd);
135 close(dup_fd);
136 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100137 }
138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 close(dup_fd);
140 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100141}
Janos Follath8ca53b52016-10-05 10:57:49 +0100142#endif /* __unix__ || __APPLE__ __MACH__ */