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