blob: 445c5c9a5a814b9b02bc907ad8ff8aa1997c2012 [file] [log] [blame]
Azim Khanec024482017-05-09 17:20:21 +01001#line 2 "suites/helpers.function"
SimonB0269dad2016-02-17 23:34:30 +00002/*----------------------------------------------------------------------------*/
3/* Headers */
4
Ronald Cron4b8b1992020-06-09 13:52:23 +02005#include <test/macros.h>
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02006#include <test/helpers.h>
Ronald Cron4b8b1992020-06-09 13:52:23 +02007
Simon Butcheredb7fd92016-05-17 13:35:51 +01008#include <stdlib.h>
9
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000011#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020012#else
Rich Evans00ab4702015-02-06 13:43:58 +000013#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010015#define mbedtls_snprintf snprintf
16#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#define mbedtls_free free
18#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010019#define mbedtls_time time
20#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010021#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
22#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020023#endif
24
SimonB0269dad2016-02-17 23:34:30 +000025#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
26#include "mbedtls/memory_buffer_alloc.h"
27#endif
28
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050029#if defined(MBEDTLS_CHECK_PARAMS)
30#include "mbedtls/platform_util.h"
31#include <setjmp.h>
32#endif
33
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000034#ifdef _MSC_VER
35#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010036typedef UINT8 uint8_t;
37typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000038typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000039#define strncasecmp _strnicmp
40#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000041#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020042#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000043#endif
44
Paul Bakker19343182013-08-16 13:31:10 +020045#include <string.h>
46
Janos Follath8ca53b52016-10-05 10:57:49 +010047#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
48#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000049#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010050#endif
SimonB0269dad2016-02-17 23:34:30 +000051
Azim Khand30ca132017-06-09 04:32:58 +010052/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010053typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010054{
55 uint8_t * x;
56 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010057} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010058
SimonB0269dad2016-02-17 23:34:30 +000059/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010060/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000061
Azim Khan62a5d7d2018-06-29 10:02:54 +010062#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 */
SimonB8ca7bc42016-04-17 23:24:50 +010065
Azim Khan62a5d7d2018-06-29 10:02:54 +010066#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 */
SimonB0269dad2016-02-17 23:34:30 +000075
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050076typedef 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
SimonB0269dad2016-02-17 23:34:30 +000085
86/*----------------------------------------------------------------------------*/
87/* Macros */
88
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050089/**
90 * \brief This macro tests the expression passed to it as a test step or
91 * individual test in a test case.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020092 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * It allows a library function to return a value and return an error
94 * code that can be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020095 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050096 * 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 Peskine8954d0c2018-09-27 13:51:25 +020099 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500100 * This macro is not suitable for negative parameter validation tests,
101 * as it assumes the test step will not create an error.
102 *
Jaeden Amero67ea2c52019-02-11 12:05:54 +0000103 * 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 Kurekc470b6b2019-01-31 08:20:20 -0500111 * \param TEST The test expression to be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +0200112 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500113#define TEST_ASSERT( TEST ) \
114 do { \
115 if( ! (TEST) ) \
116 { \
117 test_fail( #TEST, __LINE__, __FILE__ ); \
118 goto exit; \
119 } \
SimonB0269dad2016-02-17 23:34:30 +0000120 } while( 0 )
121
Gilles Peskine5f7aeee2018-12-17 23:26:52 +0100122/** 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 Peskineb75125c2018-09-27 13:52:16 +0200132/** 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 Peskine7f6e3a82018-11-30 18:51:45 +0100137 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200138 * 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 Peskine7f6e3a82018-11-30 18:51:45 +0100147 * \param length Number of elements to allocate.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200148 * This expression may be evaluated multiple times.
149 *
150 */
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100151#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 Peskineb75125c2018-09-27 13:52:16 +0200162 while( 0 )
163
Gilles Peskine292672e2020-01-21 16:20:04 +0100164/** Allocate memory dynamically. If the allocation fails, skip the test case.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100165 *
166 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
Gilles Peskine292672e2020-01-21 16:20:04 +0100167 * fails, it marks the test as skipped rather than failed.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100168 */
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 Peskine292672e2020-01-21 16:20:04 +0100177 TEST_ASSUME( ( pointer ) != NULL ); \
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100178 } \
179 } \
180 while( 0 )
181
Gilles Peskine3c225962018-09-27 13:56:31 +0200182/** 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 Beckere69d0152019-07-05 13:31:30 +0100203/**
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 Kurekc470b6b2019-01-31 08:20:20 -0500218#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 Peskine28405302018-09-27 13:52:16 +0200317/** 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 Peskine6608e712018-11-30 18:51:45 +0100322 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskine28405302018-09-27 13:52:16 +0200323 * 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 Peskine6608e712018-11-30 18:51:45 +0100332 * \param length Number of elements to allocate.
Gilles Peskine28405302018-09-27 13:52:16 +0200333 * This expression may be evaluated multiple times.
334 *
335 */
Gilles Peskine6608e712018-11-30 18:51:45 +0100336#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 Peskine28405302018-09-27 13:52:16 +0200347 while( 0 )
348
SimonB0269dad2016-02-17 23:34:30 +0000349/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100350/* Global variables */
351
Hanno Beckere69d0152019-07-05 13:31:30 +0100352typedef enum
353{
354 TEST_RESULT_SUCCESS = 0,
355 TEST_RESULT_FAILED,
356 TEST_RESULT_SKIPPED
357} test_result_t;
358
Gilles Peskine47b75402019-09-16 15:12:51 +0200359typedef struct
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100360{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500361 paramfail_test_state_t paramfail_test_state;
Hanno Beckere69d0152019-07-05 13:31:30 +0100362 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100363 const char *test;
364 const char *filename;
365 int line_no;
Gilles Peskine56055912019-03-01 14:26:30 +0100366 unsigned long step;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100367}
Gilles Peskine47b75402019-09-16 15:12:51 +0200368test_info_t;
369static test_info_t test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100370
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400371#if defined(MBEDTLS_PLATFORM_C)
Ronald Cronf91c4952020-05-27 16:22:17 +0200372static mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400373#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100374
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500375#if defined(MBEDTLS_CHECK_PARAMS)
376jmp_buf param_fail_jmp;
377jmp_buf jmp_tmp;
378#endif
379
SimonB8ca7bc42016-04-17 23:24:50 +0100380/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100381/* 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 Beckerd4a872e2017-09-07 08:09:33 +0100391#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100392#endif
393
394
395/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000396/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500397
Gilles Peskine56055912019-03-01 14:26:30 +0100398/** 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 */
406void test_set_step( unsigned long step )
407{
408 test_info.step = step;
409}
410
Simon Butcherecff2192018-10-03 16:17:41 +0100411void test_fail( const char *test, int line_no, const char* filename )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500412{
Hanno Beckere69d0152019-07-05 13:31:30 +0100413 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
419void test_skip( const char *test, int line_no, const char* filename )
420{
421 test_info.result = TEST_RESULT_SKIPPED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500422 test_info.test = test;
423 test_info.line_no = line_no;
424 test_info.filename = filename;
425}
426
Ronald Cronf91c4952020-05-27 16:22:17 +0200427int platform_setup()
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400428{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400429 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400430#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400431 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400432#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400433 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400434}
435
Ronald Cronf91c4952020-05-27 16:22:17 +0200436void platform_teardown()
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400437{
438#if defined(MBEDTLS_PLATFORM_C)
439 mbedtls_platform_teardown( &platform_ctx );
440#endif /* MBEDTLS_PLATFORM_C */
441}
SimonB0269dad2016-02-17 23:34:30 +0000442
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500443#if defined(MBEDTLS_CHECK_PARAMS)
444void 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 Beckere69d0152019-07-05 13:31:30 +0100460 test_info.result = TEST_RESULT_SUCCESS;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500461
462 longjmp( param_fail_jmp, 1 );
463 }
464}
465#endif
466
Janos Follath8ca53b52016-10-05 10:57:49 +0100467#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
468static 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 Kurekc470b6b2019-01-31 08:20:20 -0500483 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100484 return -1;
485 }
486
487 return stdout_fd;
488}
489
490static 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 Butchere0192962016-10-12 23:07:30 +0100503
Janos Follathe709f7c2016-10-13 11:26:29 +0100504static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100505{
Janos Follathe709f7c2016-10-13 11:26:29 +0100506 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100507}
Janos Follath8ca53b52016-10-05 10:57:49 +0100508#endif /* __unix__ || __APPLE__ __MACH__ */
509
Simon Butcherecff2192018-10-03 16:17:41 +0100510int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000511{
512 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000513 int len = strlen( ibuf ) / 2;
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200514 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000515
Rich Evans4c091142015-02-02 12:04:10 +0000516 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000517 {
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 Peskine9e23bea2019-06-07 14:52:07 +0200526 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000527
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 Peskine9e23bea2019-06-07 14:52:07 +0200536 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000537
538 *obuf++ = ( c << 4 ) | c2;
539 }
540
541 return len;
542}
543
Simon Butcherecff2192018-10-03 16:17:41 +0100544void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000545{
546 unsigned char l, h;
547
Rich Evans42914452015-02-02 12:09:25 +0000548 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000549 {
Rich Evans42914452015-02-02 12:09:25 +0000550 h = *ibuf / 16;
551 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000552
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 Bakker9dcc3222011-03-08 14:16:06 +0000567
568/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200569 * 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 Cronf91c4952020-05-27 16:22:17 +0200575unsigned char *zero_alloc( size_t len )
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200576{
577 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000578 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200579
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200580 p = mbedtls_calloc( 1, actual_len );
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200581 TEST_HELPER_ASSERT( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200582
583 memset( p, 0x00, actual_len );
584
585 return( p );
586}
587
588/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200589 * 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é-Gonnard0dc5e0d2014-06-13 21:09:26 +0200594 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
595 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200596 * For convenience, dies if allocation fails.
597 */
Simon Butcherecff2192018-10-03 16:17:41 +0100598unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200599{
600 unsigned char *obuf;
601
Rich Evans42914452015-02-02 12:09:25 +0000602 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200603
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200604 if( *olen == 0 )
605 return( zero_alloc( *olen ) );
606
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200607 obuf = mbedtls_calloc( 1, *olen );
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200608 TEST_HELPER_ASSERT( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200609
610 (void) unhexify( obuf, ibuf );
611
612 return( obuf );
613}
614
615/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000616 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000617 * 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 Bakker9dcc3222011-03-08 14:16:06 +0000621 *
622 * rng_state shall be NULL.
623 */
Ronald Cronf91c4952020-05-27 16:22:17 +0200624int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000625{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200626#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000627 size_t i;
628
Paul Bakker9dcc3222011-03-08 14:16:06 +0000629 if( rng_state != NULL )
630 rng_state = NULL;
631
Paul Bakkera3d195c2011-11-27 21:07:34 +0000632 for( i = 0; i < len; ++i )
633 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200634#else
635 if( rng_state != NULL )
636 rng_state = NULL;
637
638 arc4random_buf( output, len );
639#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000640
641 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000642}
643
644/**
645 * This function only returns zeros
646 *
647 * rng_state shall be NULL.
648 */
Simon Butcherecff2192018-10-03 16:17:41 +0100649int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000650{
651 if( rng_state != NULL )
652 rng_state = NULL;
653
Paul Bakkera3d195c2011-11-27 21:07:34 +0000654 memset( output, 0, len );
655
Paul Bakker9dcc3222011-03-08 14:16:06 +0000656 return( 0 );
657}
658
659typedef struct
660{
661 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000662 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000663} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000664
665/**
666 * This function returns random based on a buffer it receives.
667 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000668 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100669 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000670 * 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 Bakker9dcc3222011-03-08 14:16:06 +0000673 *
674 * After the buffer is empty it will return rand();
675 */
Simon Butcherecff2192018-10-03 16:17:41 +0100676int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000677{
Paul Bakker997bbd12011-03-13 15:45:42 +0000678 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000679 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000680
681 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000682 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000683
Paul Bakkera3d195c2011-11-27 21:07:34 +0000684 use_len = len;
685 if( len > info->length )
686 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000687
Paul Bakkera3d195c2011-11-27 21:07:34 +0000688 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000689 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000690 memcpy( output, info->buf, use_len );
691 info->buf += use_len;
692 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000693 }
694
Paul Bakkera3d195c2011-11-27 21:07:34 +0000695 if( len - use_len > 0 )
696 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
697
698 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000699}
Paul Bakker997bbd12011-03-13 15:45:42 +0000700
701/**
702 * Info structure for the pseudo random function
703 *
704 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000705 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000706 * State( v0, v1 ) should be set to zero.
707 */
708typedef struct
709{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000710 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000711 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 Butcherecff2192018-10-03 16:17:41 +0100722int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000723{
724 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000725 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100726 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000727
728 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000729 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000730
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000731 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000732
733 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000734 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000735 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000736 sum = 0;
737
Paul Bakkera3d195c2011-11-27 21:07:34 +0000738 for( i = 0; i < 32; i++ )
739 {
Rich Evans42914452015-02-02 12:09:25 +0000740 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
741 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000742 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000743 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
744 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000745 }
746
Paul Bakker5c2364c2012-10-01 14:41:15 +0000747 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100748 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000749 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100750 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000751 }
752
Paul Bakkera3d195c2011-11-27 21:07:34 +0000753 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000754}
SimonB0269dad2016-02-17 23:34:30 +0000755
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100756int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100757{
758 int ret = 0;
759 uint32_t i = 0;
760
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500761 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100762 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100763
764 for( i = 0; i < a_len; i++ )
765 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500766 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100767 {
768 ret = -1;
769 break;
770 }
771 }
772 return ret;
773}