Azim Khan | ec02448 | 2017-05-09 17:20:21 +0100 | [diff] [blame] | 1 | #line 2 "suites/helpers.function" |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 2 | /*----------------------------------------------------------------------------*/ |
| 3 | /* Headers */ |
| 4 | |
Ronald Cron | 4b8b199 | 2020-06-09 13:52:23 +0200 | [diff] [blame] | 5 | #include <test/macros.h> |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 6 | #include <test/helpers.h> |
Ronald Cron | b7eb67f | 2020-06-09 16:57:42 +0200 | [diff] [blame] | 7 | #include <test/random.h> |
Gilles Peskine | f6be590 | 2020-11-24 18:33:13 +0100 | [diff] [blame] | 8 | #include <test/psa_crypto_helpers.h> |
Ronald Cron | 4b8b199 | 2020-06-09 13:52:23 +0200 | [diff] [blame] | 9 | |
Simon Butcher | edb7fd9 | 2016-05-17 13:35:51 +0100 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 12 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 13 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 14 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 15 | #include "mbedtls/memory_buffer_alloc.h" |
| 16 | #endif |
| 17 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 18 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 19 | #include "mbedtls/platform_util.h" |
| 20 | #include <setjmp.h> |
| 21 | #endif |
| 22 | |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 23 | #ifdef _MSC_VER |
| 24 | #include <basetsd.h> |
Azim Khan | 0fa3504 | 2018-06-22 11:34:33 +0100 | [diff] [blame] | 25 | typedef UINT8 uint8_t; |
| 26 | typedef INT32 int32_t; |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 27 | typedef UINT32 uint32_t; |
Nicholas Wilson | 733676b | 2015-11-14 13:09:01 +0000 | [diff] [blame] | 28 | #define strncasecmp _strnicmp |
| 29 | #define strcasecmp _stricmp |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 30 | #else |
Manuel Pégourié-Gonnard | 9386664 | 2015-06-22 19:21:23 +0200 | [diff] [blame] | 31 | #include <stdint.h> |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 36 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 37 | #include <unistd.h> |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 38 | #include <strings.h> |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 39 | #endif |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 40 | |
| 41 | /*----------------------------------------------------------------------------*/ |
Azim Khan | 62a5d7d | 2018-06-29 10:02:54 +0100 | [diff] [blame] | 42 | /* Status and error constants */ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 43 | |
Azim Khan | 62a5d7d | 2018-06-29 10:02:54 +0100 | [diff] [blame] | 44 | #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 */ |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 47 | |
Azim Khan | 62a5d7d | 2018-06-29 10:02:54 +0100 | [diff] [blame] | 48 | #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 */ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 57 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 58 | /*----------------------------------------------------------------------------*/ |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 59 | /* Global variables */ |
| 60 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 61 | #if defined(MBEDTLS_CHECK_PARAMS) |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 62 | jmp_buf jmp_tmp; |
| 63 | #endif |
| 64 | |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 65 | /*----------------------------------------------------------------------------*/ |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 66 | /* 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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 71 | (!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 Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 76 | #define ENTROPY_HAVE_STRONG |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | |
| 80 | /*----------------------------------------------------------------------------*/ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 81 | /* Helper Functions */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 82 | |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 83 | #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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 90 | int test_fail_if_psa_leaking(int line_no, const char *filename) |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 91 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 92 | const char *msg = mbedtls_test_helper_is_psa_leaking(); |
| 93 | if (msg == NULL) { |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 94 | return 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | } else { |
| 96 | mbedtls_test_fail(msg, line_no, filename); |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 97 | return 1; |
| 98 | } |
| 99 | } |
| 100 | #endif /* defined(MBEDTLS_PSA_CRYPTO_C) */ |
| 101 | |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 102 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 103 | static int redirect_output(FILE *out_stream, const char *path) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 104 | { |
gufe44 | 067f6e0 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 105 | int out_fd, dup_fd; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 106 | FILE *path_stream; |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 107 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | out_fd = fileno(out_stream); |
| 109 | dup_fd = dup(out_fd); |
gufe44 | 067f6e0 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 110 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 111 | if (dup_fd == -1) { |
| 112 | return -1; |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | path_stream = fopen(path, "w"); |
| 116 | if (path_stream == NULL) { |
| 117 | close(dup_fd); |
| 118 | return -1; |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | fflush(out_stream); |
| 122 | if (dup2(fileno(path_stream), out_fd) == -1) { |
| 123 | close(dup_fd); |
| 124 | fclose(path_stream); |
| 125 | return -1; |
gufe44 | 067f6e0 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | fclose(path_stream); |
| 129 | return dup_fd; |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | static int restore_output(FILE *out_stream, int dup_fd) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 133 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 134 | int out_fd = fileno(out_stream); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 135 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | fflush(out_stream); |
| 137 | if (dup2(dup_fd, out_fd) == -1) { |
| 138 | close(out_fd); |
| 139 | close(dup_fd); |
| 140 | return -1; |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 143 | close(dup_fd); |
| 144 | return 0; |
Simon Butcher | e019296 | 2016-10-12 23:07:30 +0100 | [diff] [blame] | 145 | } |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 146 | #endif /* __unix__ || __APPLE__ __MACH__ */ |