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 | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 371 | #if defined(MBEDTLS_PLATFORM_C) |
Ronald Cron | f91c495 | 2020-05-27 16:22:17 +0200 | [diff] [blame] | 372 | static mbedtls_platform_context platform_ctx; |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 373 | #endif |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 374 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 375 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 376 | jmp_buf param_fail_jmp; |
| 377 | jmp_buf jmp_tmp; |
| 378 | #endif |
| 379 | |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 380 | /*----------------------------------------------------------------------------*/ |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 381 | /* Helper flags for complex dependencies */ |
| 382 | |
| 383 | /* Indicates whether we expect mbedtls_entropy_init |
| 384 | * to initialize some strong entropy source. */ |
| 385 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) || \ |
| 386 | ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \ |
| 387 | ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \ |
| 388 | defined(MBEDTLS_HAVEGE_C) || \ |
| 389 | defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \ |
| 390 | defined(ENTROPY_NV_SEED) ) ) |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 391 | #define ENTROPY_HAVE_STRONG |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 392 | #endif |
| 393 | |
| 394 | |
| 395 | /*----------------------------------------------------------------------------*/ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 396 | /* Helper Functions */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 397 | |
Gilles Peskine | 5605591 | 2019-03-01 14:26:30 +0100 | [diff] [blame] | 398 | /** Set the test step number for failure reports. |
| 399 | * |
| 400 | * Call this function to display "step NNN" in addition to the line number |
| 401 | * and file name if a test fails. Typically the "step number" is the index |
| 402 | * of a for loop but it can be whatever you want. |
| 403 | * |
| 404 | * \param step The step number to report. |
| 405 | */ |
| 406 | void test_set_step( unsigned long step ) |
| 407 | { |
| 408 | test_info.step = step; |
| 409 | } |
| 410 | |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 411 | void test_fail( const char *test, int line_no, const char* filename ) |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 412 | { |
Hanno Becker | e69d015 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 413 | test_info.result = TEST_RESULT_FAILED; |
| 414 | test_info.test = test; |
| 415 | test_info.line_no = line_no; |
| 416 | test_info.filename = filename; |
| 417 | } |
| 418 | |
| 419 | void test_skip( const char *test, int line_no, const char* filename ) |
| 420 | { |
| 421 | test_info.result = TEST_RESULT_SKIPPED; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 422 | test_info.test = test; |
| 423 | test_info.line_no = line_no; |
| 424 | test_info.filename = filename; |
| 425 | } |
| 426 | |
Ronald Cron | f91c495 | 2020-05-27 16:22:17 +0200 | [diff] [blame] | 427 | int platform_setup() |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 428 | { |
Andrzej Kurek | f13ca95 | 2018-04-18 04:14:31 -0400 | [diff] [blame] | 429 | int ret = 0; |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 430 | #if defined(MBEDTLS_PLATFORM_C) |
Andrzej Kurek | f13ca95 | 2018-04-18 04:14:31 -0400 | [diff] [blame] | 431 | ret = mbedtls_platform_setup( &platform_ctx ); |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 432 | #endif /* MBEDTLS_PLATFORM_C */ |
Andrzej Kurek | f13ca95 | 2018-04-18 04:14:31 -0400 | [diff] [blame] | 433 | return( ret ); |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Ronald Cron | f91c495 | 2020-05-27 16:22:17 +0200 | [diff] [blame] | 436 | void platform_teardown() |
Andrzej Kurek | 32a675f | 2018-04-13 06:16:04 -0400 | [diff] [blame] | 437 | { |
| 438 | #if defined(MBEDTLS_PLATFORM_C) |
| 439 | mbedtls_platform_teardown( &platform_ctx ); |
| 440 | #endif /* MBEDTLS_PLATFORM_C */ |
| 441 | } |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 442 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 443 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 444 | void mbedtls_param_failed( const char *failure_condition, |
| 445 | const char *file, |
| 446 | int line ) |
| 447 | { |
| 448 | /* If we are testing the callback function... */ |
| 449 | if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING ) |
| 450 | { |
| 451 | test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED; |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | /* ...else we treat this as an error */ |
| 456 | |
| 457 | /* Record the location of the failure, but not as a failure yet, in case |
| 458 | * it was part of the test */ |
| 459 | test_fail( failure_condition, line, file ); |
Hanno Becker | e69d015 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 460 | test_info.result = TEST_RESULT_SUCCESS; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 461 | |
| 462 | longjmp( param_fail_jmp, 1 ); |
| 463 | } |
| 464 | } |
| 465 | #endif |
| 466 | |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 467 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 468 | static int redirect_output( FILE** out_stream, const char* path ) |
| 469 | { |
| 470 | int stdout_fd = dup( fileno( *out_stream ) ); |
| 471 | |
| 472 | if( stdout_fd == -1 ) |
| 473 | { |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | fflush( *out_stream ); |
| 478 | fclose( *out_stream ); |
| 479 | *out_stream = fopen( path, "w" ); |
| 480 | |
| 481 | if( *out_stream == NULL ) |
| 482 | { |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 483 | close( stdout_fd ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 484 | return -1; |
| 485 | } |
| 486 | |
| 487 | return stdout_fd; |
| 488 | } |
| 489 | |
| 490 | static int restore_output( FILE** out_stream, int old_fd ) |
| 491 | { |
| 492 | fflush( *out_stream ); |
| 493 | fclose( *out_stream ); |
| 494 | |
| 495 | *out_stream = fdopen( old_fd, "w" ); |
| 496 | if( *out_stream == NULL ) |
| 497 | { |
| 498 | return -1; |
| 499 | } |
| 500 | |
| 501 | return 0; |
| 502 | } |
Simon Butcher | e019296 | 2016-10-12 23:07:30 +0100 | [diff] [blame] | 503 | |
Janos Follath | e709f7c | 2016-10-13 11:26:29 +0100 | [diff] [blame] | 504 | static void close_output( FILE* out_stream ) |
Simon Butcher | e019296 | 2016-10-12 23:07:30 +0100 | [diff] [blame] | 505 | { |
Janos Follath | e709f7c | 2016-10-13 11:26:29 +0100 | [diff] [blame] | 506 | fclose( out_stream ); |
Simon Butcher | e019296 | 2016-10-12 23:07:30 +0100 | [diff] [blame] | 507 | } |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 508 | #endif /* __unix__ || __APPLE__ __MACH__ */ |
| 509 | |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 510 | int unhexify( unsigned char *obuf, const char *ibuf ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 511 | { |
| 512 | unsigned char c, c2; |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 513 | int len = strlen( ibuf ) / 2; |
Gilles Peskine | 9e23bea | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 514 | 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] | 515 | |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 516 | while( *ibuf != 0 ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 517 | { |
| 518 | c = *ibuf++; |
| 519 | if( c >= '0' && c <= '9' ) |
| 520 | c -= '0'; |
| 521 | else if( c >= 'a' && c <= 'f' ) |
| 522 | c -= 'a' - 10; |
| 523 | else if( c >= 'A' && c <= 'F' ) |
| 524 | c -= 'A' - 10; |
| 525 | else |
Gilles Peskine | 9e23bea | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 526 | TEST_HELPER_ASSERT( 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 527 | |
| 528 | c2 = *ibuf++; |
| 529 | if( c2 >= '0' && c2 <= '9' ) |
| 530 | c2 -= '0'; |
| 531 | else if( c2 >= 'a' && c2 <= 'f' ) |
| 532 | c2 -= 'a' - 10; |
| 533 | else if( c2 >= 'A' && c2 <= 'F' ) |
| 534 | c2 -= 'A' - 10; |
| 535 | else |
Gilles Peskine | 9e23bea | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 536 | TEST_HELPER_ASSERT( 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 537 | |
| 538 | *obuf++ = ( c << 4 ) | c2; |
| 539 | } |
| 540 | |
| 541 | return len; |
| 542 | } |
| 543 | |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 544 | void hexify( unsigned char *obuf, const unsigned char *ibuf, int len ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 545 | { |
| 546 | unsigned char l, h; |
| 547 | |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 548 | while( len != 0 ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 549 | { |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 550 | h = *ibuf / 16; |
| 551 | l = *ibuf % 16; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 552 | |
| 553 | if( h < 10 ) |
| 554 | *obuf++ = '0' + h; |
| 555 | else |
| 556 | *obuf++ = 'a' + h - 10; |
| 557 | |
| 558 | if( l < 10 ) |
| 559 | *obuf++ = '0' + l; |
| 560 | else |
| 561 | *obuf++ = 'a' + l - 10; |
| 562 | |
| 563 | ++ibuf; |
| 564 | len--; |
| 565 | } |
| 566 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 567 | |
| 568 | /** |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 569 | * Allocate and zeroize a buffer. |
| 570 | * |
| 571 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 572 | * |
| 573 | * For convenience, dies if allocation fails. |
| 574 | */ |
Ronald Cron | f91c495 | 2020-05-27 16:22:17 +0200 | [diff] [blame] | 575 | unsigned char *zero_alloc( size_t len ) |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 576 | { |
| 577 | void *p; |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 578 | size_t actual_len = ( len != 0 ) ? len : 1; |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 579 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 580 | p = mbedtls_calloc( 1, actual_len ); |
Gilles Peskine | 9e23bea | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 581 | TEST_HELPER_ASSERT( p != NULL ); |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 582 | |
| 583 | memset( p, 0x00, actual_len ); |
| 584 | |
| 585 | return( p ); |
| 586 | } |
| 587 | |
| 588 | /** |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 589 | * Allocate and fill a buffer from hex data. |
| 590 | * |
| 591 | * The buffer is sized exactly as needed. This allows to detect buffer |
| 592 | * overruns (including overreads) when running the test suite under valgrind. |
| 593 | * |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 594 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 595 | * |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 596 | * For convenience, dies if allocation fails. |
| 597 | */ |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 598 | unsigned char *unhexify_alloc( const char *ibuf, size_t *olen ) |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 599 | { |
| 600 | unsigned char *obuf; |
| 601 | |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 602 | *olen = strlen( ibuf ) / 2; |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 603 | |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 604 | if( *olen == 0 ) |
| 605 | return( zero_alloc( *olen ) ); |
| 606 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 607 | obuf = mbedtls_calloc( 1, *olen ); |
Gilles Peskine | 9e23bea | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 608 | TEST_HELPER_ASSERT( obuf != NULL ); |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 609 | |
| 610 | (void) unhexify( obuf, ibuf ); |
| 611 | |
| 612 | return( obuf ); |
| 613 | } |
| 614 | |
| 615 | /** |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 616 | * This function just returns data from rand(). |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 617 | * Although predictable and often similar on multiple |
| 618 | * runs, this does not result in identical random on |
| 619 | * each run. So do not use this if the results of a |
| 620 | * test depend on the random data that is generated. |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 621 | * |
| 622 | * rng_state shall be NULL. |
| 623 | */ |
Ronald Cron | f91c495 | 2020-05-27 16:22:17 +0200 | [diff] [blame] | 624 | 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] | 625 | { |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 626 | #if !defined(__OpenBSD__) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 627 | size_t i; |
| 628 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 629 | if( rng_state != NULL ) |
| 630 | rng_state = NULL; |
| 631 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 632 | for( i = 0; i < len; ++i ) |
| 633 | output[i] = rand(); |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 634 | #else |
| 635 | if( rng_state != NULL ) |
| 636 | rng_state = NULL; |
| 637 | |
| 638 | arc4random_buf( output, len ); |
| 639 | #endif /* !OpenBSD */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 640 | |
| 641 | return( 0 ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /** |
| 645 | * This function only returns zeros |
| 646 | * |
| 647 | * rng_state shall be NULL. |
| 648 | */ |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 649 | 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] | 650 | { |
| 651 | if( rng_state != NULL ) |
| 652 | rng_state = NULL; |
| 653 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 654 | memset( output, 0, len ); |
| 655 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 656 | return( 0 ); |
| 657 | } |
| 658 | |
| 659 | typedef struct |
| 660 | { |
| 661 | unsigned char *buf; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 662 | size_t length; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 663 | } rnd_buf_info; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 664 | |
| 665 | /** |
| 666 | * This function returns random based on a buffer it receives. |
| 667 | * |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 668 | * 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] | 669 | * |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 670 | * The number of bytes released from the buffer on each call to |
| 671 | * the random function is specified by per_call. (Can be between |
| 672 | * 1 and 4) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 673 | * |
| 674 | * After the buffer is empty it will return rand(); |
| 675 | */ |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 676 | 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] | 677 | { |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 678 | rnd_buf_info *info = (rnd_buf_info *) rng_state; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 679 | size_t use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 680 | |
| 681 | if( rng_state == NULL ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 682 | return( rnd_std_rand( NULL, output, len ) ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 683 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 684 | use_len = len; |
| 685 | if( len > info->length ) |
| 686 | use_len = info->length; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 687 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 688 | if( use_len ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 689 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 690 | memcpy( output, info->buf, use_len ); |
| 691 | info->buf += use_len; |
| 692 | info->length -= use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 695 | if( len - use_len > 0 ) |
| 696 | return( rnd_std_rand( NULL, output + use_len, len - use_len ) ); |
| 697 | |
| 698 | return( 0 ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 699 | } |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 700 | |
| 701 | /** |
| 702 | * Info structure for the pseudo random function |
| 703 | * |
| 704 | * Key should be set at the start to a test-unique value. |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 705 | * Do not forget endianness! |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 706 | * State( v0, v1 ) should be set to zero. |
| 707 | */ |
| 708 | typedef struct |
| 709 | { |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 710 | uint32_t key[16]; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 711 | uint32_t v0, v1; |
| 712 | } rnd_pseudo_info; |
| 713 | |
| 714 | /** |
| 715 | * This function returns random based on a pseudo random function. |
| 716 | * This means the results should be identical on all systems. |
| 717 | * Pseudo random is based on the XTEA encryption algorithm to |
| 718 | * generate pseudorandom. |
| 719 | * |
| 720 | * rng_state shall be a pointer to a rnd_pseudo_info structure. |
| 721 | */ |
Simon Butcher | ecff219 | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 722 | 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] | 723 | { |
| 724 | rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 725 | uint32_t i, *k, sum, delta=0x9E3779B9; |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 726 | unsigned char result[4], *out = output; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 727 | |
| 728 | if( rng_state == NULL ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 729 | return( rnd_std_rand( NULL, output, len ) ); |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 730 | |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 731 | k = info->key; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 732 | |
| 733 | while( len > 0 ) |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 734 | { |
Paul Bakker | 40dd530 | 2012-05-15 15:02:38 +0000 | [diff] [blame] | 735 | size_t use_len = ( len > 4 ) ? 4 : len; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 736 | sum = 0; |
| 737 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 738 | for( i = 0; i < 32; i++ ) |
| 739 | { |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 740 | info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) ) |
| 741 | + info->v1 ) ^ ( sum + k[sum & 3] ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 742 | sum += delta; |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 743 | info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) ) |
| 744 | + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 747 | PUT_UINT32_BE( info->v0, result, 0 ); |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 748 | memcpy( out, result, use_len ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 749 | len -= use_len; |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 750 | out += 4; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 753 | return( 0 ); |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 754 | } |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 755 | |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 756 | 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] | 757 | { |
| 758 | int ret = 0; |
| 759 | uint32_t i = 0; |
| 760 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 761 | if( a_len != b_len ) |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 762 | return( -1 ); |
Azim Khan | 3499a9e | 2017-05-30 00:06:49 +0100 | [diff] [blame] | 763 | |
| 764 | for( i = 0; i < a_len; i++ ) |
| 765 | { |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 766 | if( a[i] != b[i] ) |
Azim Khan | 3499a9e | 2017-05-30 00:06:49 +0100 | [diff] [blame] | 767 | { |
| 768 | ret = -1; |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | return ret; |
| 773 | } |