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 | |
Simon Butcher | edb7fd9 | 2016-05-17 13:35:51 +0100 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 8 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 9 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11 | #define mbedtls_fprintf fprintf |
Simon Butcher | 2573136 | 2016-09-30 13:11:29 +0100 | [diff] [blame] | 12 | #define mbedtls_snprintf snprintf |
| 13 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 14 | #define mbedtls_free free |
| 15 | #define mbedtls_exit exit |
Simon Butcher | b2d5dd1 | 2016-04-27 13:35:37 +0100 | [diff] [blame] | 16 | #define mbedtls_time time |
| 17 | #define mbedtls_time_t time_t |
Janos Follath | 55abc21 | 2016-04-18 18:18:48 +0100 | [diff] [blame] | 18 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 19 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 20 | #endif |
| 21 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 22 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 23 | #include "mbedtls/memory_buffer_alloc.h" |
| 24 | #endif |
| 25 | |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 26 | #if defined(MBEDTLS_CHECK_PARAMS) |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 27 | #include "mbedtls/platform_util.h" |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 28 | #include <setjmp.h> |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 29 | #endif |
Simon Butcher | 747f5fe | 2018-12-07 16:53:57 +0000 | [diff] [blame] | 30 | |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 31 | #ifdef _MSC_VER |
| 32 | #include <basetsd.h> |
Azim Khan | 0fa3504 | 2018-06-22 11:34:33 +0100 | [diff] [blame] | 33 | typedef UINT8 uint8_t; |
| 34 | typedef INT32 int32_t; |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 35 | typedef UINT32 uint32_t; |
Nicholas Wilson | 733676b | 2015-11-14 13:09:01 +0000 | [diff] [blame] | 36 | #define strncasecmp _strnicmp |
| 37 | #define strcasecmp _stricmp |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 38 | #else |
Manuel Pégourié-Gonnard | 9386664 | 2015-06-22 19:21:23 +0200 | [diff] [blame] | 39 | #include <stdint.h> |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 42 | #include <string.h> |
| 43 | |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 44 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 45 | #include <unistd.h> |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 46 | #include <strings.h> |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 47 | #endif |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 48 | |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 49 | #if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \ |
| 50 | defined(MBEDTLS_TEST_HOOKS) |
| 51 | #include "mbedtls/threading.h" |
| 52 | #define MBEDTLS_TEST_MUTEX_USAGE |
| 53 | #endif |
| 54 | |
Manuel Pégourié-Gonnard | f082847 | 2020-08-25 11:26:37 +0200 | [diff] [blame] | 55 | /* |
| 56 | * Define the two macros |
| 57 | * |
| 58 | * #define TEST_CF_SECRET(ptr, size) |
| 59 | * #define TEST_CF_PUBLIC(ptr, size) |
| 60 | * |
| 61 | * that can be used in tests to mark a memory area as secret (no branch or |
| 62 | * memory access should depend on it) or public (default, only needs to be |
| 63 | * marked explicitly when it was derived from secret data). |
| 64 | * |
| 65 | * Arguments: |
| 66 | * - ptr: a pointer to the memory area to be marked |
| 67 | * - size: the size in bytes of the memory area |
| 68 | * |
| 69 | * Implementation: |
| 70 | * The basic idea is that of ctgrind <https://github.com/agl/ctgrind>: we can |
| 71 | * re-use tools that were designed for checking use of uninitialized memory. |
| 72 | * This file contains two implementations: one based on MemorySanitizer, the |
| 73 | * other on valgrind's memcheck. If none of them is enabled, dummy macros that |
| 74 | * do nothing are defined for convenience. |
| 75 | */ |
Manuel Pégourié-Gonnard | a237722 | 2020-07-28 10:53:06 +0200 | [diff] [blame] | 76 | #if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) |
| 77 | #include <sanitizer/msan_interface.h> |
| 78 | |
| 79 | /* Use macros to avoid messing up with origin tracking */ |
| 80 | #define TEST_CF_SECRET __msan_allocated_memory |
| 81 | // void __msan_allocated_memory(const volatile void* data, size_t size); |
| 82 | #define TEST_CF_PUBLIC __msan_unpoison |
| 83 | // void __msan_unpoison(const volatile void *a, size_t size); |
| 84 | |
Manuel Pégourié-Gonnard | f082847 | 2020-08-25 11:26:37 +0200 | [diff] [blame] | 85 | #elif defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND) |
| 86 | #include <valgrind/memcheck.h> |
| 87 | |
| 88 | #define TEST_CF_SECRET VALGRIND_MAKE_MEM_UNDEFINED |
| 89 | // VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len) |
| 90 | #define TEST_CF_PUBLIC VALGRIND_MAKE_MEM_DEFINED |
| 91 | // VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len) |
| 92 | |
| 93 | #else /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN || |
| 94 | MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */ |
Manuel Pégourié-Gonnard | a237722 | 2020-07-28 10:53:06 +0200 | [diff] [blame] | 95 | |
| 96 | #define TEST_CF_SECRET(ptr, size) |
| 97 | #define TEST_CF_PUBLIC(ptr, size) |
| 98 | |
| 99 | #endif /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN */ |
| 100 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 101 | /* Type for Hex parameters */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 102 | typedef struct data_tag |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 103 | { |
| 104 | uint8_t * x; |
| 105 | uint32_t len; |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 106 | } data_t; |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 107 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 108 | /*----------------------------------------------------------------------------*/ |
Azim Khan | 62a5d7d | 2018-06-29 10:02:54 +0100 | [diff] [blame] | 109 | /* Status and error constants */ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 110 | |
Azim Khan | 62a5d7d | 2018-06-29 10:02:54 +0100 | [diff] [blame] | 111 | #define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */ |
| 112 | #define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */ |
| 113 | #define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */ |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 114 | |
Azim Khan | 62a5d7d | 2018-06-29 10:02:54 +0100 | [diff] [blame] | 115 | #define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */ |
| 116 | #define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */ |
| 117 | #define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */ |
| 118 | #define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type. |
| 119 | Only int, string, binary data |
| 120 | and integer expressions are |
| 121 | allowed */ |
| 122 | #define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the |
| 123 | build */ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 124 | |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 125 | typedef enum |
| 126 | { |
| 127 | PARAMFAIL_TESTSTATE_IDLE = 0, /* No parameter failure call test */ |
| 128 | PARAMFAIL_TESTSTATE_PENDING, /* Test call to the parameter failure |
| 129 | * is pending */ |
| 130 | PARAMFAIL_TESTSTATE_CALLED /* The test call to the parameter |
| 131 | * failure function has been made */ |
| 132 | } paramfail_test_state_t; |
| 133 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 134 | |
| 135 | /*----------------------------------------------------------------------------*/ |
| 136 | /* Macros */ |
| 137 | |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 138 | /** |
| 139 | * \brief This macro tests the expression passed to it as a test step or |
| 140 | * individual test in a test case. |
| 141 | * |
| 142 | * It allows a library function to return a value and return an error |
| 143 | * code that can be tested. |
| 144 | * |
| 145 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 146 | * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test |
| 147 | * failure. |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 148 | * |
| 149 | * This macro is not suitable for negative parameter validation tests, |
| 150 | * as it assumes the test step will not create an error. |
| 151 | * |
| 152 | * \param TEST The test expression to be tested. |
| 153 | */ |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 154 | #define TEST_ASSERT( TEST ) \ |
| 155 | do { \ |
| 156 | if( ! (TEST) ) \ |
| 157 | { \ |
| 158 | test_fail( #TEST, __LINE__, __FILE__ ); \ |
| 159 | goto exit; \ |
| 160 | } \ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 161 | } while( 0 ) |
| 162 | |
Gilles Peskine | 5f45bd2 | 2021-05-31 15:40:31 +0200 | [diff] [blame] | 163 | /** Evaluate two expressions and fail the test case if they have different |
| 164 | * values. |
| 165 | * |
| 166 | * \param expr1 An expression to evaluate. |
| 167 | * \param expr2 The expected value of \p expr1. This can be any |
| 168 | * expression, but it is typically a constant. |
| 169 | */ |
| 170 | #define TEST_EQUAL( expr1, expr2 ) \ |
| 171 | TEST_ASSERT( ( expr1 ) == ( expr2 ) ) |
| 172 | |
| 173 | /** Allocate memory dynamically and fail the test case if this fails. |
| 174 | * The allocated memory will be filled with zeros. |
| 175 | * |
| 176 | * You must set \p pointer to \c NULL before calling this macro and |
| 177 | * put `mbedtls_free( pointer )` in the test's cleanup code. |
| 178 | * |
| 179 | * If \p length is zero, the resulting \p pointer will be \c NULL. |
| 180 | * This is usually what we want in tests since API functions are |
| 181 | * supposed to accept null pointers when a buffer size is zero. |
| 182 | * |
| 183 | * This macro expands to an instruction, not an expression. |
| 184 | * It may jump to the \c exit label. |
| 185 | * |
| 186 | * \param pointer An lvalue where the address of the allocated buffer |
| 187 | * will be stored. |
| 188 | * This expression may be evaluated multiple times. |
| 189 | * \param length Number of elements to allocate. |
| 190 | * This expression may be evaluated multiple times. |
| 191 | * |
| 192 | */ |
| 193 | #define ASSERT_ALLOC( pointer, length ) \ |
| 194 | do \ |
| 195 | { \ |
| 196 | TEST_ASSERT( ( pointer ) == NULL ); \ |
| 197 | if( ( length ) != 0 ) \ |
| 198 | { \ |
| 199 | ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \ |
| 200 | ( length ) ); \ |
| 201 | TEST_ASSERT( ( pointer ) != NULL ); \ |
| 202 | } \ |
| 203 | } \ |
| 204 | while( 0 ) |
| 205 | |
| 206 | /** Allocate memory dynamically. If the allocation fails, skip the test case. |
| 207 | * |
| 208 | * This macro behaves like #ASSERT_ALLOC, except that if the allocation |
| 209 | * fails, it marks the test as skipped rather than failed. |
| 210 | */ |
| 211 | #define ASSERT_ALLOC_WEAK( pointer, length ) \ |
| 212 | do \ |
| 213 | { \ |
| 214 | TEST_ASSERT( ( pointer ) == NULL ); \ |
| 215 | if( ( length ) != 0 ) \ |
| 216 | { \ |
| 217 | ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \ |
| 218 | ( length ) ); \ |
| 219 | TEST_ASSUME( ( pointer ) != NULL ); \ |
| 220 | } \ |
| 221 | } \ |
| 222 | while( 0 ) |
Ronald Cron | 8e8898d | 2020-07-30 14:18:02 +0200 | [diff] [blame] | 223 | /** Compare two buffers and fail the test case if they differ. |
| 224 | * |
| 225 | * This macro expands to an instruction, not an expression. |
| 226 | * It may jump to the \c exit label. |
| 227 | * |
| 228 | * \param p1 Pointer to the start of the first buffer. |
| 229 | * \param size1 Size of the first buffer in bytes. |
| 230 | * This expression may be evaluated multiple times. |
| 231 | * \param p2 Pointer to the start of the second buffer. |
| 232 | * \param size2 Size of the second buffer in bytes. |
| 233 | * This expression may be evaluated multiple times. |
| 234 | */ |
| 235 | #define ASSERT_COMPARE( p1, size1, p2, size2 ) \ |
| 236 | do \ |
| 237 | { \ |
| 238 | TEST_ASSERT( ( size1 ) == ( size2 ) ); \ |
| 239 | if( ( size1 ) != 0 ) \ |
| 240 | TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \ |
| 241 | } \ |
| 242 | while( 0 ) |
| 243 | |
Hanno Becker | d3369f6 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 244 | /** |
| 245 | * \brief This macro tests the expression passed to it and skips the |
| 246 | * running test if it doesn't evaluate to 'true'. |
| 247 | * |
| 248 | * \param TEST The test expression to be tested. |
| 249 | */ |
| 250 | #define TEST_ASSUME( TEST ) \ |
| 251 | do { \ |
| 252 | if( ! (TEST) ) \ |
| 253 | { \ |
| 254 | test_skip( #TEST, __LINE__, __FILE__ ); \ |
| 255 | goto exit; \ |
| 256 | } \ |
| 257 | } while( 0 ) |
| 258 | |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 259 | #if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT) |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 260 | /** |
| 261 | * \brief This macro tests the statement passed to it as a test step or |
| 262 | * individual test in a test case. The macro assumes the test will fail |
| 263 | * and will generate an error. |
| 264 | * |
| 265 | * It allows a library function to return a value and tests the return |
| 266 | * code on return to confirm the given error code was returned. |
| 267 | * |
| 268 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 269 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 270 | * expected failure, and the test will pass. |
| 271 | * |
| 272 | * This macro is intended for negative parameter validation tests, |
| 273 | * where the failing function may return an error value or call |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 274 | * MBEDTLS_PARAM_FAILED() to indicate the error. |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 275 | * |
| 276 | * \param PARAM_ERROR_VALUE The expected error code. |
| 277 | * |
| 278 | * \param TEST The test expression to be tested. |
| 279 | */ |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 280 | #define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \ |
| 281 | do { \ |
| 282 | test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \ |
Manuel Pégourié-Gonnard | acfdc62 | 2018-12-11 10:36:21 +0100 | [diff] [blame] | 283 | if( (TEST) != (PARAM_ERR_VALUE) || \ |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 284 | test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \ |
| 285 | { \ |
| 286 | test_fail( #TEST, __LINE__, __FILE__ ); \ |
| 287 | goto exit; \ |
| 288 | } \ |
| 289 | } while( 0 ) |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 290 | |
| 291 | /** |
| 292 | * \brief This macro tests the statement passed to it as a test step or |
| 293 | * individual test in a test case. The macro assumes the test will fail |
| 294 | * and will generate an error. |
| 295 | * |
| 296 | * It assumes the library function under test cannot return a value and |
| 297 | * assumes errors can only be indicated byt calls to |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 298 | * MBEDTLS_PARAM_FAILED(). |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 299 | * |
| 300 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 301 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 302 | * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test |
| 303 | * can be made. |
| 304 | * |
| 305 | * This macro is intended for negative parameter validation tests, |
| 306 | * where the failing function can only return an error by calling |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 307 | * MBEDTLS_PARAM_FAILED() to indicate the error. |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 308 | * |
| 309 | * \param TEST The test expression to be tested. |
| 310 | */ |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 311 | #define TEST_INVALID_PARAM( TEST ) \ |
| 312 | do { \ |
| 313 | memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \ |
Manuel Pégourié-Gonnard | aae10fa | 2018-12-12 10:24:19 +0100 | [diff] [blame] | 314 | if( setjmp( param_fail_jmp ) == 0 ) \ |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 315 | { \ |
| 316 | TEST; \ |
| 317 | test_fail( #TEST, __LINE__, __FILE__ ); \ |
| 318 | goto exit; \ |
| 319 | } \ |
| 320 | memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \ |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 321 | } while( 0 ) |
Manuel Pégourié-Gonnard | 54e7f31 | 2018-12-12 11:56:09 +0100 | [diff] [blame] | 322 | #endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */ |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 323 | |
Manuel Pégourié-Gonnard | 44c5d58 | 2018-12-10 16:56:14 +0100 | [diff] [blame] | 324 | /** |
| 325 | * \brief This macro tests the statement passed to it as a test step or |
| 326 | * individual test in a test case. The macro assumes the test will not fail. |
| 327 | * |
| 328 | * It assumes the library function under test cannot return a value and |
| 329 | * assumes errors can only be indicated by calls to |
| 330 | * MBEDTLS_PARAM_FAILED(). |
| 331 | * |
| 332 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 333 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
| 334 | * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test |
| 335 | * can be made. |
| 336 | * |
Manuel Pégourié-Gonnard | cd2b29c | 2018-12-12 10:23:57 +0100 | [diff] [blame] | 337 | * This macro is intended to test that functions returning void |
Manuel Pégourié-Gonnard | 44c5d58 | 2018-12-10 16:56:14 +0100 | [diff] [blame] | 338 | * accept all of the parameter values they're supposed to accept - eg |
| 339 | * that they don't call MBEDTLS_PARAM_FAILED() when a parameter |
Manuel Pégourié-Gonnard | cd2b29c | 2018-12-12 10:23:57 +0100 | [diff] [blame] | 340 | * that's allowed to be NULL happens to be NULL. |
Manuel Pégourié-Gonnard | 44c5d58 | 2018-12-10 16:56:14 +0100 | [diff] [blame] | 341 | * |
| 342 | * Note: for functions that return something other that void, |
| 343 | * checking that they accept all the parameters they're supposed to |
| 344 | * accept is best done by using TEST_ASSERT() and checking the return |
| 345 | * value as well. |
| 346 | * |
Manuel Pégourié-Gonnard | 54e7f31 | 2018-12-12 11:56:09 +0100 | [diff] [blame] | 347 | * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is |
| 348 | * disabled, as it makes sense to check that the functions accept all |
| 349 | * legal values even if this option is disabled - only in that case, |
Manuel Pégourié-Gonnard | e7306d3 | 2018-12-13 09:45:49 +0100 | [diff] [blame] | 350 | * the test is more about whether the function segfaults than about |
Manuel Pégourié-Gonnard | 54e7f31 | 2018-12-12 11:56:09 +0100 | [diff] [blame] | 351 | * whether it invokes MBEDTLS_PARAM_FAILED(). |
| 352 | * |
Manuel Pégourié-Gonnard | 44c5d58 | 2018-12-10 16:56:14 +0100 | [diff] [blame] | 353 | * \param TEST The test expression to be tested. |
| 354 | */ |
| 355 | #define TEST_VALID_PARAM( TEST ) \ |
| 356 | TEST_ASSERT( ( TEST, 1 ) ); |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 357 | |
Gilles Peskine | 137d31b | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 358 | #define TEST_HELPER_ASSERT(a) if( !( a ) ) \ |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 359 | { \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 360 | mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \ |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 361 | __FILE__, __LINE__, #a ); \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | mbedtls_exit( 1 ); \ |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Ronald Cron | 07c83f2 | 2020-04-06 09:50:58 +0200 | [diff] [blame] | 365 | #if defined(__GNUC__) |
| 366 | /* Test if arg and &(arg)[0] have the same type. This is true if arg is |
| 367 | * an array but not if it's a pointer. */ |
| 368 | #define IS_ARRAY_NOT_POINTER( arg ) \ |
| 369 | ( ! __builtin_types_compatible_p( __typeof__( arg ), \ |
| 370 | __typeof__( &( arg )[0] ) ) ) |
| 371 | #else |
| 372 | /* On platforms where we don't know how to implement this check, |
| 373 | * omit it. Oh well, a non-portable check is better than nothing. */ |
| 374 | #define IS_ARRAY_NOT_POINTER( arg ) 1 |
| 375 | #endif |
| 376 | |
| 377 | /* A compile-time constant with the value 0. If `const_expr` is not a |
| 378 | * compile-time constant with a nonzero value, cause a compile-time error. */ |
| 379 | #define STATIC_ASSERT_EXPR( const_expr ) \ |
makise-homura | 50f6a19 | 2020-08-23 00:39:15 +0300 | [diff] [blame] | 380 | ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) ) |
Ronald Cron | 07c83f2 | 2020-04-06 09:50:58 +0200 | [diff] [blame] | 381 | /* Return the scalar value `value` (possibly promoted). This is a compile-time |
| 382 | * constant if `value` is. `condition` must be a compile-time constant. |
| 383 | * If `condition` is false, arrange to cause a compile-time error. */ |
| 384 | #define STATIC_ASSERT_THEN_RETURN( condition, value ) \ |
| 385 | ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) ) |
| 386 | |
| 387 | #define ARRAY_LENGTH_UNSAFE( array ) \ |
| 388 | ( sizeof( array ) / sizeof( *( array ) ) ) |
| 389 | /** Return the number of elements of a static or stack array. |
| 390 | * |
| 391 | * \param array A value of array (not pointer) type. |
| 392 | * |
| 393 | * \return The number of elements of the array. |
| 394 | */ |
| 395 | #define ARRAY_LENGTH( array ) \ |
| 396 | ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \ |
| 397 | ARRAY_LENGTH_UNSAFE( array ) ) ) |
| 398 | |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 399 | /* |
| 400 | * 32-bit integer manipulation macros (big endian) |
| 401 | */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 402 | #ifndef GET_UINT32_BE |
| 403 | #define GET_UINT32_BE(n,b,i) \ |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 404 | { \ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 405 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ |
| 406 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ |
| 407 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ |
| 408 | | ( (uint32_t) (b)[(i) + 3] ); \ |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 409 | } |
| 410 | #endif |
| 411 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 412 | #ifndef PUT_UINT32_BE |
| 413 | #define PUT_UINT32_BE(n,b,i) \ |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 414 | { \ |
| 415 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
| 416 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
| 417 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
| 418 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
| 419 | } |
| 420 | #endif |
| 421 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 422 | |
| 423 | /*----------------------------------------------------------------------------*/ |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 424 | /* Global variables */ |
| 425 | |
Hanno Becker | d3369f6 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 426 | typedef enum |
| 427 | { |
| 428 | TEST_RESULT_SUCCESS = 0, |
| 429 | TEST_RESULT_FAILED, |
| 430 | TEST_RESULT_SKIPPED |
| 431 | } test_result_t; |
| 432 | |
Andres Amaya Garcia | 3f50f51 | 2017-10-01 16:42:29 +0100 | [diff] [blame] | 433 | static struct |
| 434 | { |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 435 | paramfail_test_state_t paramfail_test_state; |
Hanno Becker | d3369f6 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 436 | test_result_t result; |
Andres Amaya Garcia | 3f50f51 | 2017-10-01 16:42:29 +0100 | [diff] [blame] | 437 | const char *test; |
| 438 | const char *filename; |
| 439 | int line_no; |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 440 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 441 | const char *mutex_usage_error; |
| 442 | #endif |
Andres Amaya Garcia | 3f50f51 | 2017-10-01 16:42:29 +0100 | [diff] [blame] | 443 | } |
| 444 | test_info; |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 445 | |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 446 | #if defined(MBEDTLS_PLATFORM_C) |
Andrzej Kurek | 1152fa8 | 2018-04-13 05:15:17 -0400 | [diff] [blame] | 447 | mbedtls_platform_context platform_ctx; |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 448 | #endif |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 449 | |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 450 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 451 | jmp_buf param_fail_jmp; |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 452 | jmp_buf jmp_tmp; |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 453 | #endif |
| 454 | |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 455 | /*----------------------------------------------------------------------------*/ |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 456 | /* Helper flags for complex dependencies */ |
| 457 | |
| 458 | /* Indicates whether we expect mbedtls_entropy_init |
| 459 | * to initialize some strong entropy source. */ |
| 460 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) || \ |
| 461 | ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \ |
| 462 | ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \ |
| 463 | defined(MBEDTLS_HAVEGE_C) || \ |
| 464 | defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \ |
| 465 | defined(ENTROPY_NV_SEED) ) ) |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 466 | #define ENTROPY_HAVE_STRONG |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 467 | #endif |
| 468 | |
| 469 | |
| 470 | /*----------------------------------------------------------------------------*/ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 471 | /* Helper Functions */ |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 472 | |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 473 | void test_fail( const char *test, int line_no, const char* filename ) |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 474 | { |
Gilles Peskine | dfb5cff | 2020-08-29 15:18:23 +0200 | [diff] [blame] | 475 | if( test_info.result == TEST_RESULT_FAILED ) |
| 476 | { |
| 477 | /* We've already recorded the test as having failed. Don't |
| 478 | * overwrite any previous information about the failure. */ |
| 479 | return; |
| 480 | } |
Hanno Becker | d3369f6 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 481 | test_info.result = TEST_RESULT_FAILED; |
| 482 | test_info.test = test; |
| 483 | test_info.line_no = line_no; |
| 484 | test_info.filename = filename; |
| 485 | } |
| 486 | |
| 487 | void test_skip( const char *test, int line_no, const char* filename ) |
| 488 | { |
| 489 | test_info.result = TEST_RESULT_SKIPPED; |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 490 | test_info.test = test; |
| 491 | test_info.line_no = line_no; |
| 492 | test_info.filename = filename; |
| 493 | } |
| 494 | |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 495 | static int platform_setup() |
| 496 | { |
Andrzej Kurek | f13ca95 | 2018-04-18 04:14:31 -0400 | [diff] [blame] | 497 | int ret = 0; |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 498 | #if defined(MBEDTLS_PLATFORM_C) |
Andrzej Kurek | f13ca95 | 2018-04-18 04:14:31 -0400 | [diff] [blame] | 499 | ret = mbedtls_platform_setup( &platform_ctx ); |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 500 | #endif /* MBEDTLS_PLATFORM_C */ |
Andrzej Kurek | f13ca95 | 2018-04-18 04:14:31 -0400 | [diff] [blame] | 501 | return( ret ); |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static void platform_teardown() |
| 505 | { |
| 506 | #if defined(MBEDTLS_PLATFORM_C) |
| 507 | mbedtls_platform_teardown( &platform_ctx ); |
| 508 | #endif /* MBEDTLS_PLATFORM_C */ |
| 509 | } |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 510 | |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 511 | #if defined(MBEDTLS_CHECK_PARAMS) |
Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 512 | void mbedtls_param_failed( const char *failure_condition, |
| 513 | const char *file, |
| 514 | int line ) |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 515 | { |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 516 | /* If we are testing the callback function... */ |
Manuel Pégourié-Gonnard | aae10fa | 2018-12-12 10:24:19 +0100 | [diff] [blame] | 517 | if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING ) |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 518 | { |
| 519 | test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED; |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | /* ...else we treat this as an error */ |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 524 | |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 525 | /* Record the location of the failure, but not as a failure yet, in case |
| 526 | * it was part of the test */ |
| 527 | test_fail( failure_condition, line, file ); |
Hanno Becker | d3369f6 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 528 | test_info.result = TEST_RESULT_SUCCESS; |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 529 | |
| 530 | longjmp( param_fail_jmp, 1 ); |
| 531 | } |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 532 | } |
| 533 | #endif |
| 534 | |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 535 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 536 | static int redirect_output( FILE* out_stream, const char* path ) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 537 | { |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 538 | int out_fd, dup_fd; |
| 539 | FILE* path_stream; |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 540 | |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 541 | out_fd = fileno( out_stream ); |
| 542 | dup_fd = dup( out_fd ); |
| 543 | |
| 544 | if( dup_fd == -1 ) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 545 | { |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 546 | return( -1 ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 547 | } |
| 548 | |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 549 | path_stream = fopen( path, "w" ); |
| 550 | if( path_stream == NULL ) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 551 | { |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 552 | close( dup_fd ); |
| 553 | return( -1 ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 554 | } |
| 555 | |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 556 | fflush( out_stream ); |
| 557 | if( dup2( fileno( path_stream ), out_fd ) == -1 ) |
| 558 | { |
| 559 | close( dup_fd ); |
| 560 | fclose( path_stream ); |
| 561 | return( -1 ); |
| 562 | } |
| 563 | |
| 564 | fclose( path_stream ); |
| 565 | return( dup_fd ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 566 | } |
| 567 | |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 568 | static int restore_output( FILE* out_stream, int dup_fd ) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 569 | { |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 570 | int out_fd = fileno( out_stream ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 571 | |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 572 | fflush( out_stream ); |
| 573 | if( dup2( dup_fd, out_fd ) == -1 ) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 574 | { |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 575 | close( out_fd ); |
| 576 | close( dup_fd ); |
| 577 | return( -1 ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 578 | } |
| 579 | |
gufe44 | 650ce76 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 580 | close( dup_fd ); |
| 581 | return( 0 ); |
Simon Butcher | e019296 | 2016-10-12 23:07:30 +0100 | [diff] [blame] | 582 | } |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 583 | #endif /* __unix__ || __APPLE__ __MACH__ */ |
| 584 | |
Ronald Cron | a0c9ff3 | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 585 | int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 586 | { |
| 587 | unsigned char c, c2; |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 588 | int len = strlen( ibuf ) / 2; |
Gilles Peskine | 137d31b | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 589 | TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 590 | |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 591 | while( *ibuf != 0 ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 592 | { |
| 593 | c = *ibuf++; |
| 594 | if( c >= '0' && c <= '9' ) |
| 595 | c -= '0'; |
| 596 | else if( c >= 'a' && c <= 'f' ) |
| 597 | c -= 'a' - 10; |
| 598 | else if( c >= 'A' && c <= 'F' ) |
| 599 | c -= 'A' - 10; |
| 600 | else |
Gilles Peskine | 137d31b | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 601 | TEST_HELPER_ASSERT( 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 602 | |
| 603 | c2 = *ibuf++; |
| 604 | if( c2 >= '0' && c2 <= '9' ) |
| 605 | c2 -= '0'; |
| 606 | else if( c2 >= 'a' && c2 <= 'f' ) |
| 607 | c2 -= 'a' - 10; |
| 608 | else if( c2 >= 'A' && c2 <= 'F' ) |
| 609 | c2 -= 'A' - 10; |
| 610 | else |
Gilles Peskine | 137d31b | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 611 | TEST_HELPER_ASSERT( 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 612 | |
| 613 | *obuf++ = ( c << 4 ) | c2; |
| 614 | } |
| 615 | |
| 616 | return len; |
| 617 | } |
| 618 | |
Ronald Cron | a0c9ff3 | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 619 | void mbedtls_test_hexify( unsigned char *obuf, const unsigned char *ibuf, int len ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 620 | { |
| 621 | unsigned char l, h; |
| 622 | |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 623 | while( len != 0 ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 624 | { |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 625 | h = *ibuf / 16; |
| 626 | l = *ibuf % 16; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 627 | |
| 628 | if( h < 10 ) |
| 629 | *obuf++ = '0' + h; |
| 630 | else |
| 631 | *obuf++ = 'a' + h - 10; |
| 632 | |
| 633 | if( l < 10 ) |
| 634 | *obuf++ = '0' + l; |
| 635 | else |
| 636 | *obuf++ = 'a' + l - 10; |
| 637 | |
| 638 | ++ibuf; |
| 639 | len--; |
| 640 | } |
| 641 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 642 | |
| 643 | /** |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 644 | * Allocate and zeroize a buffer. |
| 645 | * |
| 646 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 647 | * |
| 648 | * For convenience, dies if allocation fails. |
| 649 | */ |
| 650 | static unsigned char *zero_alloc( size_t len ) |
| 651 | { |
| 652 | void *p; |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 653 | size_t actual_len = ( len != 0 ) ? len : 1; |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 654 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 655 | p = mbedtls_calloc( 1, actual_len ); |
Gilles Peskine | 137d31b | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 656 | TEST_HELPER_ASSERT( p != NULL ); |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 657 | |
| 658 | memset( p, 0x00, actual_len ); |
| 659 | |
| 660 | return( p ); |
| 661 | } |
| 662 | |
| 663 | /** |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 664 | * Allocate and fill a buffer from hex data. |
| 665 | * |
| 666 | * The buffer is sized exactly as needed. This allows to detect buffer |
| 667 | * overruns (including overreads) when running the test suite under valgrind. |
| 668 | * |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 669 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 670 | * |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 671 | * For convenience, dies if allocation fails. |
| 672 | */ |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 673 | unsigned char *unhexify_alloc( const char *ibuf, size_t *olen ) |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 674 | { |
| 675 | unsigned char *obuf; |
| 676 | |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 677 | *olen = strlen( ibuf ) / 2; |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 678 | |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 679 | if( *olen == 0 ) |
| 680 | return( zero_alloc( *olen ) ); |
| 681 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 682 | obuf = mbedtls_calloc( 1, *olen ); |
Gilles Peskine | 137d31b | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 683 | TEST_HELPER_ASSERT( obuf != NULL ); |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 684 | |
Ronald Cron | a0c9ff3 | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 685 | (void) mbedtls_test_unhexify( obuf, ibuf ); |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 686 | |
| 687 | return( obuf ); |
| 688 | } |
| 689 | |
| 690 | /** |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 691 | * This function just returns data from rand(). |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 692 | * Although predictable and often similar on multiple |
| 693 | * runs, this does not result in identical random on |
| 694 | * each run. So do not use this if the results of a |
| 695 | * test depend on the random data that is generated. |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 696 | * |
| 697 | * rng_state shall be NULL. |
| 698 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 699 | static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 700 | { |
gufe44 | 3fa7c64 | 2020-08-03 17:56:50 +0200 | [diff] [blame] | 701 | #if !defined(__OpenBSD__) && !defined(__NetBSD__) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 702 | size_t i; |
| 703 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 704 | if( rng_state != NULL ) |
| 705 | rng_state = NULL; |
| 706 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 707 | for( i = 0; i < len; ++i ) |
| 708 | output[i] = rand(); |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 709 | #else |
| 710 | if( rng_state != NULL ) |
| 711 | rng_state = NULL; |
| 712 | |
| 713 | arc4random_buf( output, len ); |
gufe44 | 3fa7c64 | 2020-08-03 17:56:50 +0200 | [diff] [blame] | 714 | #endif /* !OpenBSD && !NetBSD */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 715 | |
| 716 | return( 0 ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /** |
| 720 | * This function only returns zeros |
| 721 | * |
| 722 | * rng_state shall be NULL. |
| 723 | */ |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 724 | int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 725 | { |
| 726 | if( rng_state != NULL ) |
| 727 | rng_state = NULL; |
| 728 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 729 | memset( output, 0, len ); |
| 730 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 731 | return( 0 ); |
| 732 | } |
| 733 | |
| 734 | typedef struct |
| 735 | { |
| 736 | unsigned char *buf; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 737 | size_t length; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 738 | } rnd_buf_info; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 739 | |
| 740 | /** |
| 741 | * This function returns random based on a buffer it receives. |
| 742 | * |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 743 | * rng_state shall be a pointer to a rnd_buf_info structure. |
Manuel Pégourié-Gonnard | e670f90 | 2015-10-30 09:23:19 +0100 | [diff] [blame] | 744 | * |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 745 | * The number of bytes released from the buffer on each call to |
| 746 | * the random function is specified by per_call. (Can be between |
| 747 | * 1 and 4) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 748 | * |
| 749 | * After the buffer is empty it will return rand(); |
| 750 | */ |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 751 | int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 752 | { |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 753 | rnd_buf_info *info = (rnd_buf_info *) rng_state; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 754 | size_t use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 755 | |
| 756 | if( rng_state == NULL ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 757 | return( rnd_std_rand( NULL, output, len ) ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 758 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 759 | use_len = len; |
| 760 | if( len > info->length ) |
| 761 | use_len = info->length; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 762 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 763 | if( use_len ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 764 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 765 | memcpy( output, info->buf, use_len ); |
| 766 | info->buf += use_len; |
| 767 | info->length -= use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 770 | if( len - use_len > 0 ) |
| 771 | return( rnd_std_rand( NULL, output + use_len, len - use_len ) ); |
| 772 | |
| 773 | return( 0 ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 774 | } |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 775 | |
| 776 | /** |
| 777 | * Info structure for the pseudo random function |
| 778 | * |
| 779 | * Key should be set at the start to a test-unique value. |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 780 | * Do not forget endianness! |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 781 | * State( v0, v1 ) should be set to zero. |
| 782 | */ |
| 783 | typedef struct |
| 784 | { |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 785 | uint32_t key[16]; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 786 | uint32_t v0, v1; |
| 787 | } rnd_pseudo_info; |
| 788 | |
| 789 | /** |
| 790 | * This function returns random based on a pseudo random function. |
| 791 | * This means the results should be identical on all systems. |
| 792 | * Pseudo random is based on the XTEA encryption algorithm to |
| 793 | * generate pseudorandom. |
| 794 | * |
| 795 | * rng_state shall be a pointer to a rnd_pseudo_info structure. |
| 796 | */ |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 797 | int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 798 | { |
| 799 | rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 800 | uint32_t i, *k, sum, delta=0x9E3779B9; |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 801 | unsigned char result[4], *out = output; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 802 | |
| 803 | if( rng_state == NULL ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 804 | return( rnd_std_rand( NULL, output, len ) ); |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 805 | |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 806 | k = info->key; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 807 | |
| 808 | while( len > 0 ) |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 809 | { |
Paul Bakker | 40dd530 | 2012-05-15 15:02:38 +0000 | [diff] [blame] | 810 | size_t use_len = ( len > 4 ) ? 4 : len; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 811 | sum = 0; |
| 812 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 813 | for( i = 0; i < 32; i++ ) |
| 814 | { |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 815 | info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) ) |
| 816 | + info->v1 ) ^ ( sum + k[sum & 3] ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 817 | sum += delta; |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 818 | info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) ) |
| 819 | + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 822 | PUT_UINT32_BE( info->v0, result, 0 ); |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 823 | memcpy( out, result, use_len ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 824 | len -= use_len; |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 825 | out += 4; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 828 | return( 0 ); |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 829 | } |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 830 | |
Ronald Cron | d239794 | 2020-06-10 11:03:08 +0200 | [diff] [blame] | 831 | int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len ) |
Azim Khan | 3499a9e | 2017-05-30 00:06:49 +0100 | [diff] [blame] | 832 | { |
| 833 | int ret = 0; |
| 834 | uint32_t i = 0; |
| 835 | |
Manuel Pégourié-Gonnard | aae10fa | 2018-12-12 10:24:19 +0100 | [diff] [blame] | 836 | if( a_len != b_len ) |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 837 | return( -1 ); |
Azim Khan | 3499a9e | 2017-05-30 00:06:49 +0100 | [diff] [blame] | 838 | |
| 839 | for( i = 0; i < a_len; i++ ) |
| 840 | { |
Manuel Pégourié-Gonnard | aae10fa | 2018-12-12 10:24:19 +0100 | [diff] [blame] | 841 | if( a[i] != b[i] ) |
Azim Khan | 3499a9e | 2017-05-30 00:06:49 +0100 | [diff] [blame] | 842 | { |
| 843 | ret = -1; |
| 844 | break; |
| 845 | } |
| 846 | } |
| 847 | return ret; |
| 848 | } |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 849 | |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 850 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 851 | /** Mutex usage verification framework. |
| 852 | * |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 853 | * The mutex usage verification code below aims to detect bad usage of |
| 854 | * Mbed TLS's mutex abstraction layer at runtime. Note that this is solely |
| 855 | * about the use of the mutex itself, not about checking whether the mutex |
| 856 | * correctly protects whatever it is supposed to protect. |
| 857 | * |
| 858 | * The normal usage of a mutex is: |
| 859 | * ``` |
| 860 | * digraph mutex_states { |
| 861 | * "UNINITIALIZED"; // the initial state |
| 862 | * "IDLE"; |
| 863 | * "FREED"; |
| 864 | * "LOCKED"; |
| 865 | * "UNINITIALIZED" -> "IDLE" [label="init"]; |
| 866 | * "FREED" -> "IDLE" [label="init"]; |
| 867 | * "IDLE" -> "LOCKED" [label="lock"]; |
| 868 | * "LOCKED" -> "IDLE" [label="unlock"]; |
| 869 | * "IDLE" -> "FREED" [label="free"]; |
| 870 | * } |
| 871 | * ``` |
| 872 | * |
| 873 | * All bad transitions that can be unambiguously detected are reported. |
| 874 | * An attempt to use an uninitialized mutex cannot be detected in general |
| 875 | * since the memory content may happen to denote a valid state. For the same |
| 876 | * reason, a double init cannot be detected. |
| 877 | * All-bits-zero is the state of a freed mutex, which is distinct from an |
| 878 | * initialized mutex, so attempting to use zero-initialized memory as a mutex |
| 879 | * without calling the init function is detected. |
| 880 | * |
Gilles Peskine | 7252ec3 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 881 | * The framework attempts to detect missing calls to init and free by counting |
| 882 | * calls to init and free. If there are more calls to init than free, this |
| 883 | * means that a mutex is not being freed somewhere, which is a memory leak |
| 884 | * on platforms where a mutex consumes resources other than the |
| 885 | * mbedtls_threading_mutex_t object itself. If there are more calls to free |
| 886 | * than init, this indicates a missing init, which is likely to be detected |
| 887 | * by an attempt to lock the mutex as well. A limitation of this framework is |
| 888 | * that it cannot detect scenarios where there is exactly the same number of |
| 889 | * calls to init and free but the calls don't match. A bug like this is |
Gilles Peskine | ed9f798 | 2021-02-15 18:21:55 +0100 | [diff] [blame] | 890 | * unlikely to happen uniformly throughout the whole test suite though. |
Gilles Peskine | 7252ec3 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 891 | * |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 892 | * If an error is detected, this framework will report what happened and the |
| 893 | * test case will be marked as failed. Unfortunately, the error report cannot |
| 894 | * indicate the exact location of the problematic call. To locate the error, |
| 895 | * use a debugger and set a breakpoint on mbedtls_test_mutex_usage_error(). |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 896 | */ |
Gilles Peskine | 7ba73e5 | 2021-02-09 15:35:29 +0100 | [diff] [blame] | 897 | enum value_of_mutex_is_valid_field |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 898 | { |
Gilles Peskine | 7ba73e5 | 2021-02-09 15:35:29 +0100 | [diff] [blame] | 899 | /* Potential values for the is_valid field of mbedtls_threading_mutex_t. |
| 900 | * Note that MUTEX_FREED must be 0 and MUTEX_IDLE must be 1 for |
| 901 | * compatibility with threading_mutex_init_pthread() and |
| 902 | * threading_mutex_free_pthread(). MUTEX_LOCKED could be any nonzero |
| 903 | * value. */ |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 904 | MUTEX_FREED = 0, //!< Set by threading_mutex_free_pthread |
| 905 | MUTEX_IDLE = 1, //!< Set by threading_mutex_init_pthread and by our unlock |
| 906 | MUTEX_LOCKED = 2, //!< Set by our lock |
| 907 | }; |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 908 | |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 909 | typedef struct |
| 910 | { |
| 911 | void (*init)( mbedtls_threading_mutex_t * ); |
| 912 | void (*free)( mbedtls_threading_mutex_t * ); |
| 913 | int (*lock)( mbedtls_threading_mutex_t * ); |
| 914 | int (*unlock)( mbedtls_threading_mutex_t * ); |
| 915 | } mutex_functions_t; |
| 916 | static mutex_functions_t mutex_functions; |
| 917 | |
Gilles Peskine | 7252ec3 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 918 | /** The total number of calls to mbedtls_mutex_init(), minus the total number |
| 919 | * of calls to mbedtls_mutex_free(). |
| 920 | * |
| 921 | * Reset to 0 after each test case. |
| 922 | */ |
| 923 | static int live_mutexes; |
| 924 | |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 925 | static void mbedtls_test_mutex_usage_error( mbedtls_threading_mutex_t *mutex, |
| 926 | const char *msg ) |
| 927 | { |
| 928 | (void) mutex; |
| 929 | if( test_info.mutex_usage_error == NULL ) |
| 930 | test_info.mutex_usage_error = msg; |
| 931 | mbedtls_fprintf( stdout, "[mutex: %s] ", msg ); |
| 932 | /* Don't mark the test as failed yet. This way, if the test fails later |
| 933 | * for a functional reason, the test framework will report the message |
| 934 | * and location for this functional reason. If the test passes, |
| 935 | * mbedtls_test_mutex_usage_check() will mark it as failed. */ |
| 936 | } |
| 937 | |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 938 | static void mbedtls_test_wrap_mutex_init( mbedtls_threading_mutex_t *mutex ) |
| 939 | { |
| 940 | mutex_functions.init( mutex ); |
Gilles Peskine | 7252ec3 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 941 | if( mutex->is_valid ) |
| 942 | ++live_mutexes; |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | static void mbedtls_test_wrap_mutex_free( mbedtls_threading_mutex_t *mutex ) |
| 946 | { |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 947 | switch( mutex->is_valid ) |
| 948 | { |
| 949 | case MUTEX_FREED: |
| 950 | mbedtls_test_mutex_usage_error( mutex, "free without init or double free" ); |
| 951 | break; |
| 952 | case MUTEX_IDLE: |
| 953 | /* Do nothing. The underlying free function will reset is_valid |
| 954 | * to 0. */ |
| 955 | break; |
| 956 | case MUTEX_LOCKED: |
| 957 | mbedtls_test_mutex_usage_error( mutex, "free without unlock" ); |
| 958 | break; |
| 959 | default: |
| 960 | mbedtls_test_mutex_usage_error( mutex, "corrupted state" ); |
| 961 | break; |
| 962 | } |
Gilles Peskine | 7252ec3 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 963 | if( mutex->is_valid ) |
| 964 | --live_mutexes; |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 965 | mutex_functions.free( mutex ); |
| 966 | } |
| 967 | |
| 968 | static int mbedtls_test_wrap_mutex_lock( mbedtls_threading_mutex_t *mutex ) |
| 969 | { |
| 970 | int ret = mutex_functions.lock( mutex ); |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 971 | switch( mutex->is_valid ) |
| 972 | { |
| 973 | case MUTEX_FREED: |
| 974 | mbedtls_test_mutex_usage_error( mutex, "lock without init" ); |
| 975 | break; |
| 976 | case MUTEX_IDLE: |
| 977 | if( ret == 0 ) |
| 978 | mutex->is_valid = 2; |
| 979 | break; |
| 980 | case MUTEX_LOCKED: |
| 981 | mbedtls_test_mutex_usage_error( mutex, "double lock" ); |
| 982 | break; |
| 983 | default: |
| 984 | mbedtls_test_mutex_usage_error( mutex, "corrupted state" ); |
| 985 | break; |
| 986 | } |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 987 | return( ret ); |
| 988 | } |
| 989 | |
| 990 | static int mbedtls_test_wrap_mutex_unlock( mbedtls_threading_mutex_t *mutex ) |
| 991 | { |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 992 | int ret = mutex_functions.unlock( mutex ); |
| 993 | switch( mutex->is_valid ) |
| 994 | { |
| 995 | case MUTEX_FREED: |
| 996 | mbedtls_test_mutex_usage_error( mutex, "unlock without init" ); |
| 997 | break; |
| 998 | case MUTEX_IDLE: |
| 999 | mbedtls_test_mutex_usage_error( mutex, "unlock without lock" ); |
| 1000 | break; |
| 1001 | case MUTEX_LOCKED: |
| 1002 | if( ret == 0 ) |
| 1003 | mutex->is_valid = MUTEX_IDLE; |
| 1004 | break; |
| 1005 | default: |
| 1006 | mbedtls_test_mutex_usage_error( mutex, "corrupted state" ); |
| 1007 | break; |
| 1008 | } |
| 1009 | return( ret ); |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | static void mbedtls_test_mutex_usage_init( void ) |
| 1013 | { |
| 1014 | mutex_functions.init = mbedtls_mutex_init; |
| 1015 | mutex_functions.free = mbedtls_mutex_free; |
| 1016 | mutex_functions.lock = mbedtls_mutex_lock; |
| 1017 | mutex_functions.unlock = mbedtls_mutex_unlock; |
| 1018 | mbedtls_mutex_init = &mbedtls_test_wrap_mutex_init; |
| 1019 | mbedtls_mutex_free = &mbedtls_test_wrap_mutex_free; |
| 1020 | mbedtls_mutex_lock = &mbedtls_test_wrap_mutex_lock; |
| 1021 | mbedtls_mutex_unlock = &mbedtls_test_wrap_mutex_unlock; |
| 1022 | } |
| 1023 | |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 1024 | static void mbedtls_test_mutex_usage_check( void ) |
| 1025 | { |
Gilles Peskine | 7252ec3 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 1026 | if( live_mutexes != 0 ) |
| 1027 | { |
| 1028 | /* A positive number (more init than free) means that a mutex resource |
| 1029 | * is leaking (on platforms where a mutex consumes more than the |
| 1030 | * mbedtls_threading_mutex_t object itself). The rare case of a |
| 1031 | * negative number means a missing init somewhere. */ |
| 1032 | mbedtls_fprintf( stdout, "[mutex: %d leaked] ", live_mutexes ); |
| 1033 | live_mutexes = 0; |
| 1034 | if( test_info.mutex_usage_error == NULL ) |
| 1035 | test_info.mutex_usage_error = "missing free"; |
| 1036 | } |
Gilles Peskine | cd2e248 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 1037 | if( test_info.mutex_usage_error != NULL && |
| 1038 | test_info.result != TEST_RESULT_FAILED ) |
| 1039 | { |
| 1040 | /* Functionally, the test passed. But there was a mutex usage error, |
| 1041 | * so mark the test as failed after all. */ |
| 1042 | test_fail( "Mutex usage error", __LINE__, __FILE__ ); |
| 1043 | } |
| 1044 | test_info.mutex_usage_error = NULL; |
| 1045 | } |
| 1046 | |
Gilles Peskine | c071373 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 1047 | #endif /* MBEDTLS_TEST_MUTEX_USAGE */ |