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