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 | 4b8b199 | 2020-06-09 13:52:23 +0200 | [diff] [blame] | 7 | |
Simon Butcher | edb7fd9 | 2016-05-17 13:35:51 +0100 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 11 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 12 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 14 | #define mbedtls_fprintf fprintf |
Simon Butcher | 2573136 | 2016-09-30 13:11:29 +0100 | [diff] [blame] | 15 | #define mbedtls_snprintf snprintf |
| 16 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 17 | #define mbedtls_free free |
| 18 | #define mbedtls_exit exit |
Simon Butcher | b2d5dd1 | 2016-04-27 13:35:37 +0100 | [diff] [blame] | 19 | #define mbedtls_time time |
| 20 | #define mbedtls_time_t time_t |
Janos Follath | 55abc21 | 2016-04-18 18:18:48 +0100 | [diff] [blame] | 21 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 22 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 23 | #endif |
| 24 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 25 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 26 | #include "mbedtls/memory_buffer_alloc.h" |
| 27 | #endif |
| 28 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 29 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 30 | #include "mbedtls/platform_util.h" |
| 31 | #include <setjmp.h> |
| 32 | #endif |
| 33 | |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 34 | #ifdef _MSC_VER |
| 35 | #include <basetsd.h> |
Azim Khan | 0fa3504 | 2018-06-22 11:34:33 +0100 | [diff] [blame] | 36 | typedef UINT8 uint8_t; |
| 37 | typedef INT32 int32_t; |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 38 | typedef UINT32 uint32_t; |
Nicholas Wilson | 733676b | 2015-11-14 13:09:01 +0000 | [diff] [blame] | 39 | #define strncasecmp _strnicmp |
| 40 | #define strcasecmp _stricmp |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 41 | #else |
Manuel Pégourié-Gonnard | 9386664 | 2015-06-22 19:21:23 +0200 | [diff] [blame] | 42 | #include <stdint.h> |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 45 | #include <string.h> |
| 46 | |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 47 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 48 | #include <unistd.h> |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 49 | #include <strings.h> |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 50 | #endif |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 51 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 52 | /* Type for Hex parameters */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 53 | typedef struct data_tag |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 54 | { |
| 55 | uint8_t * x; |
| 56 | uint32_t len; |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 57 | } data_t; |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 58 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 59 | /*----------------------------------------------------------------------------*/ |
Azim Khan | 62a5d7d | 2018-06-29 10:02:54 +0100 | [diff] [blame] | 60 | /* Status and error constants */ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 61 | |
Azim Khan | 62a5d7d | 2018-06-29 10:02:54 +0100 | [diff] [blame] | 62 | #define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */ |
| 63 | #define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */ |
| 64 | #define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */ |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 65 | |
Azim Khan | 62a5d7d | 2018-06-29 10:02:54 +0100 | [diff] [blame] | 66 | #define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */ |
| 67 | #define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */ |
| 68 | #define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */ |
| 69 | #define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type. |
| 70 | Only int, string, binary data |
| 71 | and integer expressions are |
| 72 | allowed */ |
| 73 | #define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the |
| 74 | build */ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 75 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 76 | typedef enum |
| 77 | { |
| 78 | PARAMFAIL_TESTSTATE_IDLE = 0, /* No parameter failure call test */ |
| 79 | PARAMFAIL_TESTSTATE_PENDING, /* Test call to the parameter failure |
| 80 | * is pending */ |
| 81 | PARAMFAIL_TESTSTATE_CALLED /* The test call to the parameter |
| 82 | * failure function has been made */ |
| 83 | } paramfail_test_state_t; |
| 84 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 85 | |
| 86 | /*----------------------------------------------------------------------------*/ |
| 87 | /* Macros */ |
| 88 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 89 | /** |
| 90 | * \brief This macro tests the expression passed to it as a test step or |
| 91 | * individual test in a test case. |
Gilles Peskine | 8954d0c | 2018-09-27 13:51:25 +0200 | [diff] [blame] | 92 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 93 | * It allows a library function to return a value and return an error |
| 94 | * code that can be tested. |
Gilles Peskine | 8954d0c | 2018-09-27 13:51:25 +0200 | [diff] [blame] | 95 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 96 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 97 | * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test |
| 98 | * failure. |
Gilles Peskine | 8954d0c | 2018-09-27 13:51:25 +0200 | [diff] [blame] | 99 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 100 | * This macro is not suitable for negative parameter validation tests, |
| 101 | * as it assumes the test step will not create an error. |
| 102 | * |
Jaeden Amero | 67ea2c5 | 2019-02-11 12:05:54 +0000 | [diff] [blame] | 103 | * Failing the test means: |
| 104 | * - Mark this test case as failed. |
| 105 | * - Print a message identifying the failure. |
| 106 | * - Jump to the \c exit label. |
| 107 | * |
| 108 | * This macro expands to an instruction, not an expression. |
| 109 | * It may jump to the \c exit label. |
| 110 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 111 | * \param TEST The test expression to be tested. |
Gilles Peskine | 8954d0c | 2018-09-27 13:51:25 +0200 | [diff] [blame] | 112 | */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 113 | #define TEST_ASSERT( TEST ) \ |
| 114 | do { \ |
| 115 | if( ! (TEST) ) \ |
| 116 | { \ |
| 117 | test_fail( #TEST, __LINE__, __FILE__ ); \ |
| 118 | goto exit; \ |
| 119 | } \ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 120 | } while( 0 ) |
| 121 | |
Gilles Peskine | 5f7aeee | 2018-12-17 23:26:52 +0100 | [diff] [blame] | 122 | /** Evaluate two expressions and fail the test case if they have different |
| 123 | * values. |
| 124 | * |
| 125 | * \param expr1 An expression to evaluate. |
| 126 | * \param expr2 The expected value of \p expr1. This can be any |
| 127 | * expression, but it is typically a constant. |
| 128 | */ |
| 129 | #define TEST_EQUAL( expr1, expr2 ) \ |
| 130 | TEST_ASSERT( ( expr1 ) == ( expr2 ) ) |
| 131 | |
Gilles Peskine | b75125c | 2018-09-27 13:52:16 +0200 | [diff] [blame] | 132 | /** Allocate memory dynamically and fail the test case if this fails. |
| 133 | * |
| 134 | * You must set \p pointer to \c NULL before calling this macro and |
| 135 | * put `mbedtls_free( pointer )` in the test's cleanup code. |
| 136 | * |
Gilles Peskine | 7f6e3a8 | 2018-11-30 18:51:45 +0100 | [diff] [blame] | 137 | * If \p length is zero, the resulting \p pointer will be \c NULL. |
Gilles Peskine | b75125c | 2018-09-27 13:52:16 +0200 | [diff] [blame] | 138 | * This is usually what we want in tests since API functions are |
| 139 | * supposed to accept null pointers when a buffer size is zero. |
| 140 | * |
| 141 | * This macro expands to an instruction, not an expression. |
| 142 | * It may jump to the \c exit label. |
| 143 | * |
| 144 | * \param pointer An lvalue where the address of the allocated buffer |
| 145 | * will be stored. |
| 146 | * This expression may be evaluated multiple times. |
Gilles Peskine | 7f6e3a8 | 2018-11-30 18:51:45 +0100 | [diff] [blame] | 147 | * \param length Number of elements to allocate. |
Gilles Peskine | b75125c | 2018-09-27 13:52:16 +0200 | [diff] [blame] | 148 | * This expression may be evaluated multiple times. |
| 149 | * |
| 150 | */ |
Gilles Peskine | 7f6e3a8 | 2018-11-30 18:51:45 +0100 | [diff] [blame] | 151 | #define ASSERT_ALLOC( pointer, length ) \ |
| 152 | do \ |
| 153 | { \ |
| 154 | TEST_ASSERT( ( pointer ) == NULL ); \ |
| 155 | if( ( length ) != 0 ) \ |
| 156 | { \ |
| 157 | ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \ |
| 158 | ( length ) ); \ |
| 159 | TEST_ASSERT( ( pointer ) != NULL ); \ |
| 160 | } \ |
| 161 | } \ |
Gilles Peskine | b75125c | 2018-09-27 13:52:16 +0200 | [diff] [blame] | 162 | while( 0 ) |
| 163 | |
Gilles Peskine | 292672e | 2020-01-21 16:20:04 +0100 | [diff] [blame] | 164 | /** Allocate memory dynamically. If the allocation fails, skip the test case. |
Gilles Peskine | 2cd8ecc | 2019-03-04 17:13:43 +0100 | [diff] [blame] | 165 | * |
| 166 | * This macro behaves like #ASSERT_ALLOC, except that if the allocation |
Gilles Peskine | 292672e | 2020-01-21 16:20:04 +0100 | [diff] [blame] | 167 | * fails, it marks the test as skipped rather than failed. |
Gilles Peskine | 2cd8ecc | 2019-03-04 17:13:43 +0100 | [diff] [blame] | 168 | */ |
| 169 | #define ASSERT_ALLOC_WEAK( pointer, length ) \ |
| 170 | do \ |
| 171 | { \ |
| 172 | TEST_ASSERT( ( pointer ) == NULL ); \ |
| 173 | if( ( length ) != 0 ) \ |
| 174 | { \ |
| 175 | ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \ |
| 176 | ( length ) ); \ |
Gilles Peskine | 292672e | 2020-01-21 16:20:04 +0100 | [diff] [blame] | 177 | TEST_ASSUME( ( pointer ) != NULL ); \ |
Gilles Peskine | 2cd8ecc | 2019-03-04 17:13:43 +0100 | [diff] [blame] | 178 | } \ |
| 179 | } \ |
| 180 | while( 0 ) |
| 181 | |
Gilles Peskine | 3c22596 | 2018-09-27 13:56:31 +0200 | [diff] [blame] | 182 | /** Compare two buffers and fail the test case if they differ. |
| 183 | * |
| 184 | * This macro expands to an instruction, not an expression. |
| 185 | * It may jump to the \c exit label. |
| 186 | * |
| 187 | * \param p1 Pointer to the start of the first buffer. |
| 188 | * \param size1 Size of the first buffer in bytes. |
| 189 | * This expression may be evaluated multiple times. |
| 190 | * \param p2 Pointer to the start of the second buffer. |
| 191 | * \param size2 Size of the second buffer in bytes. |
| 192 | * This expression may be evaluated multiple times. |
| 193 | */ |
| 194 | #define ASSERT_COMPARE( p1, size1, p2, size2 ) \ |
| 195 | do \ |
| 196 | { \ |
| 197 | TEST_ASSERT( ( size1 ) == ( size2 ) ); \ |
| 198 | if( ( size1 ) != 0 ) \ |
| 199 | TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \ |
| 200 | } \ |
| 201 | while( 0 ) |
| 202 | |
Hanno Becker | e69d015 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 203 | /** |
| 204 | * \brief This macro tests the expression passed to it and skips the |
| 205 | * running test if it doesn't evaluate to 'true'. |
| 206 | * |
| 207 | * \param TEST The test expression to be tested. |
| 208 | */ |
| 209 | #define TEST_ASSUME( TEST ) \ |
| 210 | do { \ |
| 211 | if( ! (TEST) ) \ |
| 212 | { \ |
| 213 | test_skip( #TEST, __LINE__, __FILE__ ); \ |
| 214 | goto exit; \ |
| 215 | } \ |
| 216 | } while( 0 ) |
| 217 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 218 | #if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT) |
| 219 | /** |
| 220 | * \brief This macro tests the statement passed to it as a test step or |
| 221 | * individual test in a test case. The macro assumes the test will fail |
| 222 | * and will generate an error. |
| 223 | * |
| 224 | * It allows a library function to return a value and tests the return |
| 225 | * code on return to confirm the given error code was returned. |
| 226 | * |
| 227 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 228 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
| 229 | * expected failure, and the test will pass. |
| 230 | * |
| 231 | * This macro is intended for negative parameter validation tests, |
| 232 | * where the failing function may return an error value or call |
| 233 | * MBEDTLS_PARAM_FAILED() to indicate the error. |
| 234 | * |
| 235 | * \param PARAM_ERROR_VALUE The expected error code. |
| 236 | * |
| 237 | * \param TEST The test expression to be tested. |
| 238 | */ |
| 239 | #define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \ |
| 240 | do { \ |
| 241 | test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \ |
| 242 | if( (TEST) != (PARAM_ERR_VALUE) || \ |
| 243 | test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \ |
| 244 | { \ |
| 245 | test_fail( #TEST, __LINE__, __FILE__ ); \ |
| 246 | goto exit; \ |
| 247 | } \ |
| 248 | } while( 0 ) |
| 249 | |
| 250 | /** |
| 251 | * \brief This macro tests the statement passed to it as a test step or |
| 252 | * individual test in a test case. The macro assumes the test will fail |
| 253 | * and will generate an error. |
| 254 | * |
| 255 | * It assumes the library function under test cannot return a value and |
| 256 | * assumes errors can only be indicated byt calls to |
| 257 | * MBEDTLS_PARAM_FAILED(). |
| 258 | * |
| 259 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 260 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
| 261 | * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test |
| 262 | * can be made. |
| 263 | * |
| 264 | * This macro is intended for negative parameter validation tests, |
| 265 | * where the failing function can only return an error by calling |
| 266 | * MBEDTLS_PARAM_FAILED() to indicate the error. |
| 267 | * |
| 268 | * \param TEST The test expression to be tested. |
| 269 | */ |
| 270 | #define TEST_INVALID_PARAM( TEST ) \ |
| 271 | do { \ |
| 272 | memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \ |
| 273 | if( setjmp( param_fail_jmp ) == 0 ) \ |
| 274 | { \ |
| 275 | TEST; \ |
| 276 | test_fail( #TEST, __LINE__, __FILE__ ); \ |
| 277 | goto exit; \ |
| 278 | } \ |
| 279 | memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \ |
| 280 | } while( 0 ) |
| 281 | #endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */ |
| 282 | |
| 283 | /** |
| 284 | * \brief This macro tests the statement passed to it as a test step or |
| 285 | * individual test in a test case. The macro assumes the test will not fail. |
| 286 | * |
| 287 | * It assumes the library function under test cannot return a value and |
| 288 | * assumes errors can only be indicated by calls to |
| 289 | * MBEDTLS_PARAM_FAILED(). |
| 290 | * |
| 291 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 292 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
| 293 | * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test |
| 294 | * can be made. |
| 295 | * |
| 296 | * This macro is intended to test that functions returning void |
| 297 | * accept all of the parameter values they're supposed to accept - eg |
| 298 | * that they don't call MBEDTLS_PARAM_FAILED() when a parameter |
| 299 | * that's allowed to be NULL happens to be NULL. |
| 300 | * |
| 301 | * Note: for functions that return something other that void, |
| 302 | * checking that they accept all the parameters they're supposed to |
| 303 | * accept is best done by using TEST_ASSERT() and checking the return |
| 304 | * value as well. |
| 305 | * |
| 306 | * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is |
| 307 | * disabled, as it makes sense to check that the functions accept all |
| 308 | * legal values even if this option is disabled - only in that case, |
| 309 | * the test is more about whether the function segfaults than about |
| 310 | * whether it invokes MBEDTLS_PARAM_FAILED(). |
| 311 | * |
| 312 | * \param TEST The test expression to be tested. |
| 313 | */ |
| 314 | #define TEST_VALID_PARAM( TEST ) \ |
| 315 | TEST_ASSERT( ( TEST, 1 ) ); |
| 316 | |
Gilles Peskine | 2840530 | 2018-09-27 13:52:16 +0200 | [diff] [blame] | 317 | /** Allocate memory dynamically and fail the test case if this fails. |
| 318 | * |
| 319 | * You must set \p pointer to \c NULL before calling this macro and |
| 320 | * put `mbedtls_free( pointer )` in the test's cleanup code. |
| 321 | * |
Gilles Peskine | 6608e71 | 2018-11-30 18:51:45 +0100 | [diff] [blame] | 322 | * If \p length is zero, the resulting \p pointer will be \c NULL. |
Gilles Peskine | 2840530 | 2018-09-27 13:52:16 +0200 | [diff] [blame] | 323 | * This is usually what we want in tests since API functions are |
| 324 | * supposed to accept null pointers when a buffer size is zero. |
| 325 | * |
| 326 | * This macro expands to an instruction, not an expression. |
| 327 | * It may jump to the \c exit label. |
| 328 | * |
| 329 | * \param pointer An lvalue where the address of the allocated buffer |
| 330 | * will be stored. |
| 331 | * This expression may be evaluated multiple times. |
Gilles Peskine | 6608e71 | 2018-11-30 18:51:45 +0100 | [diff] [blame] | 332 | * \param length Number of elements to allocate. |
Gilles Peskine | 2840530 | 2018-09-27 13:52:16 +0200 | [diff] [blame] | 333 | * This expression may be evaluated multiple times. |
| 334 | * |
| 335 | */ |
Gilles Peskine | 6608e71 | 2018-11-30 18:51:45 +0100 | [diff] [blame] | 336 | #define ASSERT_ALLOC( pointer, length ) \ |
| 337 | do \ |
| 338 | { \ |
| 339 | TEST_ASSERT( ( pointer ) == NULL ); \ |
| 340 | if( ( length ) != 0 ) \ |
| 341 | { \ |
| 342 | ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \ |
| 343 | ( length ) ); \ |
| 344 | TEST_ASSERT( ( pointer ) != NULL ); \ |
| 345 | } \ |
| 346 | } \ |
Gilles Peskine | 2840530 | 2018-09-27 13:52:16 +0200 | [diff] [blame] | 347 | while( 0 ) |
| 348 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 349 | /*----------------------------------------------------------------------------*/ |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 350 | /* Global variables */ |
| 351 | |
Hanno Becker | e69d015 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 352 | typedef enum |
| 353 | { |
| 354 | TEST_RESULT_SUCCESS = 0, |
| 355 | TEST_RESULT_FAILED, |
| 356 | TEST_RESULT_SKIPPED |
| 357 | } test_result_t; |
| 358 | |
Gilles Peskine | 47b7540 | 2019-09-16 15:12:51 +0200 | [diff] [blame] | 359 | typedef struct |
Andres Amaya Garcia | 3f50f51 | 2017-10-01 16:42:29 +0100 | [diff] [blame] | 360 | { |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 361 | paramfail_test_state_t paramfail_test_state; |
Hanno Becker | e69d015 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 362 | test_result_t result; |
Andres Amaya Garcia | 3f50f51 | 2017-10-01 16:42:29 +0100 | [diff] [blame] | 363 | const char *test; |
| 364 | const char *filename; |
| 365 | int line_no; |
Gilles Peskine | 5605591 | 2019-03-01 14:26:30 +0100 | [diff] [blame] | 366 | unsigned long step; |
Andres Amaya Garcia | 3f50f51 | 2017-10-01 16:42:29 +0100 | [diff] [blame] | 367 | } |
Gilles Peskine | 47b7540 | 2019-09-16 15:12:51 +0200 | [diff] [blame] | 368 | test_info_t; |
| 369 | static test_info_t test_info; |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 370 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 371 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 372 | jmp_buf param_fail_jmp; |
| 373 | jmp_buf jmp_tmp; |
| 374 | #endif |
| 375 | |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 376 | /*----------------------------------------------------------------------------*/ |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 377 | /* Helper flags for complex dependencies */ |
| 378 | |
| 379 | /* Indicates whether we expect mbedtls_entropy_init |
| 380 | * to initialize some strong entropy source. */ |
| 381 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) || \ |
| 382 | ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \ |
| 383 | ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \ |
| 384 | defined(MBEDTLS_HAVEGE_C) || \ |
| 385 | defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \ |
| 386 | defined(ENTROPY_NV_SEED) ) ) |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 387 | #define ENTROPY_HAVE_STRONG |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 388 | #endif |
| 389 | |
| 390 | |
| 391 | /*----------------------------------------------------------------------------*/ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 392 | /* Helper Functions */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 393 | |
Gilles Peskine | 5605591 | 2019-03-01 14:26:30 +0100 | [diff] [blame] | 394 | /** Set the test step number for failure reports. |
| 395 | * |
| 396 | * Call this function to display "step NNN" in addition to the line number |
| 397 | * and file name if a test fails. Typically the "step number" is the index |
| 398 | * of a for loop but it can be whatever you want. |
| 399 | * |
| 400 | * \param step The step number to report. |
| 401 | */ |
| 402 | void test_set_step( unsigned long step ) |
| 403 | { |
| 404 | test_info.step = step; |
| 405 | } |
| 406 | |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 407 | void test_fail( const char *test, int line_no, const char* filename ) |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 408 | { |
Hanno Becker | e69d015 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 409 | test_info.result = TEST_RESULT_FAILED; |
| 410 | test_info.test = test; |
| 411 | test_info.line_no = line_no; |
| 412 | test_info.filename = filename; |
| 413 | } |
| 414 | |
| 415 | void test_skip( const char *test, int line_no, const char* filename ) |
| 416 | { |
| 417 | test_info.result = TEST_RESULT_SKIPPED; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 418 | test_info.test = test; |
| 419 | test_info.line_no = line_no; |
| 420 | test_info.filename = filename; |
| 421 | } |
| 422 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 423 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 424 | void mbedtls_param_failed( const char *failure_condition, |
| 425 | const char *file, |
| 426 | int line ) |
| 427 | { |
| 428 | /* If we are testing the callback function... */ |
| 429 | if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING ) |
| 430 | { |
| 431 | test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED; |
| 432 | } |
| 433 | else |
| 434 | { |
| 435 | /* ...else we treat this as an error */ |
| 436 | |
| 437 | /* Record the location of the failure, but not as a failure yet, in case |
| 438 | * it was part of the test */ |
| 439 | test_fail( failure_condition, line, file ); |
Hanno Becker | e69d015 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 440 | test_info.result = TEST_RESULT_SUCCESS; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 441 | |
| 442 | longjmp( param_fail_jmp, 1 ); |
| 443 | } |
| 444 | } |
| 445 | #endif |
| 446 | |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 447 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 448 | static int redirect_output( FILE** out_stream, const char* path ) |
| 449 | { |
| 450 | int stdout_fd = dup( fileno( *out_stream ) ); |
| 451 | |
| 452 | if( stdout_fd == -1 ) |
| 453 | { |
| 454 | return -1; |
| 455 | } |
| 456 | |
| 457 | fflush( *out_stream ); |
| 458 | fclose( *out_stream ); |
| 459 | *out_stream = fopen( path, "w" ); |
| 460 | |
| 461 | if( *out_stream == NULL ) |
| 462 | { |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 463 | close( stdout_fd ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 464 | return -1; |
| 465 | } |
| 466 | |
| 467 | return stdout_fd; |
| 468 | } |
| 469 | |
| 470 | static int restore_output( FILE** out_stream, int old_fd ) |
| 471 | { |
| 472 | fflush( *out_stream ); |
| 473 | fclose( *out_stream ); |
| 474 | |
| 475 | *out_stream = fdopen( old_fd, "w" ); |
| 476 | if( *out_stream == NULL ) |
| 477 | { |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
Simon Butcher | e019296 | 2016-10-12 23:07:30 +0100 | [diff] [blame] | 483 | |
Janos Follath | e709f7c | 2016-10-13 11:26:29 +0100 | [diff] [blame] | 484 | static void close_output( FILE* out_stream ) |
Simon Butcher | e019296 | 2016-10-12 23:07:30 +0100 | [diff] [blame] | 485 | { |
Janos Follath | e709f7c | 2016-10-13 11:26:29 +0100 | [diff] [blame] | 486 | fclose( out_stream ); |
Simon Butcher | e019296 | 2016-10-12 23:07:30 +0100 | [diff] [blame] | 487 | } |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 488 | #endif /* __unix__ || __APPLE__ __MACH__ */ |
| 489 | |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 490 | /** |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 491 | * This function just returns data from rand(). |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 492 | * Although predictable and often similar on multiple |
| 493 | * runs, this does not result in identical random on |
| 494 | * each run. So do not use this if the results of a |
| 495 | * test depend on the random data that is generated. |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 496 | * |
| 497 | * rng_state shall be NULL. |
| 498 | */ |
Ronald Cron | f91c495 | 2020-05-27 16:22:17 +0200 | [diff] [blame] | 499 | 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] | 500 | { |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 501 | #if !defined(__OpenBSD__) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 502 | size_t i; |
| 503 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 504 | if( rng_state != NULL ) |
| 505 | rng_state = NULL; |
| 506 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 507 | for( i = 0; i < len; ++i ) |
| 508 | output[i] = rand(); |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 509 | #else |
| 510 | if( rng_state != NULL ) |
| 511 | rng_state = NULL; |
| 512 | |
| 513 | arc4random_buf( output, len ); |
| 514 | #endif /* !OpenBSD */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 515 | |
| 516 | return( 0 ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /** |
| 520 | * This function only returns zeros |
| 521 | * |
| 522 | * rng_state shall be NULL. |
| 523 | */ |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 524 | 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] | 525 | { |
| 526 | if( rng_state != NULL ) |
| 527 | rng_state = NULL; |
| 528 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 529 | memset( output, 0, len ); |
| 530 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 531 | return( 0 ); |
| 532 | } |
| 533 | |
| 534 | typedef struct |
| 535 | { |
| 536 | unsigned char *buf; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 537 | size_t length; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 538 | } rnd_buf_info; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 539 | |
| 540 | /** |
| 541 | * This function returns random based on a buffer it receives. |
| 542 | * |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 543 | * 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] | 544 | * |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 545 | * The number of bytes released from the buffer on each call to |
| 546 | * the random function is specified by per_call. (Can be between |
| 547 | * 1 and 4) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 548 | * |
| 549 | * After the buffer is empty it will return rand(); |
| 550 | */ |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 551 | 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] | 552 | { |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 553 | rnd_buf_info *info = (rnd_buf_info *) rng_state; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 554 | size_t use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 555 | |
| 556 | if( rng_state == NULL ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 557 | return( rnd_std_rand( NULL, output, len ) ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 558 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 559 | use_len = len; |
| 560 | if( len > info->length ) |
| 561 | use_len = info->length; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 562 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 563 | if( use_len ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 564 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 565 | memcpy( output, info->buf, use_len ); |
| 566 | info->buf += use_len; |
| 567 | info->length -= use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 570 | if( len - use_len > 0 ) |
| 571 | return( rnd_std_rand( NULL, output + use_len, len - use_len ) ); |
| 572 | |
| 573 | return( 0 ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 574 | } |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 575 | |
| 576 | /** |
| 577 | * Info structure for the pseudo random function |
| 578 | * |
| 579 | * Key should be set at the start to a test-unique value. |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 580 | * Do not forget endianness! |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 581 | * State( v0, v1 ) should be set to zero. |
| 582 | */ |
| 583 | typedef struct |
| 584 | { |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 585 | uint32_t key[16]; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 586 | uint32_t v0, v1; |
| 587 | } rnd_pseudo_info; |
| 588 | |
| 589 | /** |
| 590 | * This function returns random based on a pseudo random function. |
| 591 | * This means the results should be identical on all systems. |
| 592 | * Pseudo random is based on the XTEA encryption algorithm to |
| 593 | * generate pseudorandom. |
| 594 | * |
| 595 | * rng_state shall be a pointer to a rnd_pseudo_info structure. |
| 596 | */ |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 597 | 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] | 598 | { |
| 599 | rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 600 | uint32_t i, *k, sum, delta=0x9E3779B9; |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 601 | unsigned char result[4], *out = output; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 602 | |
| 603 | if( rng_state == NULL ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 604 | return( rnd_std_rand( NULL, output, len ) ); |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 605 | |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 606 | k = info->key; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 607 | |
| 608 | while( len > 0 ) |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 609 | { |
Paul Bakker | 40dd530 | 2012-05-15 15:02:38 +0000 | [diff] [blame] | 610 | size_t use_len = ( len > 4 ) ? 4 : len; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 611 | sum = 0; |
| 612 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 613 | for( i = 0; i < 32; i++ ) |
| 614 | { |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 615 | info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) ) |
| 616 | + info->v1 ) ^ ( sum + k[sum & 3] ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 617 | sum += delta; |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 618 | info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) ) |
| 619 | + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 622 | PUT_UINT32_BE( info->v0, result, 0 ); |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 623 | memcpy( out, result, use_len ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 624 | len -= use_len; |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 625 | out += 4; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 628 | return( 0 ); |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 629 | } |