blob: 122a17da75f21eac416dff1b7573bcd46adda2c8 [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
Simon Butcheredb7fd92016-05-17 13:35:51 +01005#include <stdlib.h>
6
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00008#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +02009#else
Rich Evans00ab4702015-02-06 13:43:58 +000010#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010012#define mbedtls_snprintf snprintf
13#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#define mbedtls_free free
15#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010016#define mbedtls_time time
17#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010018#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
19#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020020#endif
21
SimonB0269dad2016-02-17 23:34:30 +000022#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
23#include "mbedtls/memory_buffer_alloc.h"
24#endif
25
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050026#if defined(MBEDTLS_CHECK_PARAMS)
27#include "mbedtls/platform_util.h"
28#include <setjmp.h>
29#endif
30
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000031#ifdef _MSC_VER
32#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010033typedef UINT8 uint8_t;
34typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000035typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000036#define strncasecmp _strnicmp
37#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000038#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020039#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000040#endif
41
Paul Bakker19343182013-08-16 13:31:10 +020042#include <string.h>
43
Janos Follath8ca53b52016-10-05 10:57:49 +010044#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
45#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000046#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010047#endif
SimonB0269dad2016-02-17 23:34:30 +000048
Azim Khand30ca132017-06-09 04:32:58 +010049/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010050typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010051{
52 uint8_t * x;
53 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010054} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010055
SimonB0269dad2016-02-17 23:34:30 +000056/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010057/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000058
Azim Khan62a5d7d2018-06-29 10:02:54 +010059#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
60#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
61#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010062
Azim Khan62a5d7d2018-06-29 10:02:54 +010063#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
64#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
65#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
66#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
67 Only int, string, binary data
68 and integer expressions are
69 allowed */
70#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
71 build */
SimonB0269dad2016-02-17 23:34:30 +000072
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050073typedef enum
74{
75 PARAMFAIL_TESTSTATE_IDLE = 0, /* No parameter failure call test */
76 PARAMFAIL_TESTSTATE_PENDING, /* Test call to the parameter failure
77 * is pending */
78 PARAMFAIL_TESTSTATE_CALLED /* The test call to the parameter
79 * failure function has been made */
80} paramfail_test_state_t;
81
SimonB0269dad2016-02-17 23:34:30 +000082
83/*----------------------------------------------------------------------------*/
84/* Macros */
85
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086/**
87 * \brief This macro tests the expression passed to it as a test step or
88 * individual test in a test case.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020089 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050090 * It allows a library function to return a value and return an error
91 * code that can be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020092 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
94 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
95 * failure.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020096 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * This macro is not suitable for negative parameter validation tests,
98 * as it assumes the test step will not create an error.
99 *
Jaeden Amero67ea2c52019-02-11 12:05:54 +0000100 * Failing the test means:
101 * - Mark this test case as failed.
102 * - Print a message identifying the failure.
103 * - Jump to the \c exit label.
104 *
105 * This macro expands to an instruction, not an expression.
106 * It may jump to the \c exit label.
107 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 * \param TEST The test expression to be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +0200109 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110#define TEST_ASSERT( TEST ) \
111 do { \
112 if( ! (TEST) ) \
113 { \
114 test_fail( #TEST, __LINE__, __FILE__ ); \
115 goto exit; \
116 } \
SimonB0269dad2016-02-17 23:34:30 +0000117 } while( 0 )
118
Gilles Peskine5f7aeee2018-12-17 23:26:52 +0100119/** Evaluate two expressions and fail the test case if they have different
120 * values.
121 *
122 * \param expr1 An expression to evaluate.
123 * \param expr2 The expected value of \p expr1. This can be any
124 * expression, but it is typically a constant.
125 */
126#define TEST_EQUAL( expr1, expr2 ) \
127 TEST_ASSERT( ( expr1 ) == ( expr2 ) )
128
Gilles Peskine0174be22018-12-17 23:26:01 +0100129/** Evaluate an expression and fail the test case if it returns an error.
130 *
131 * \param expr The expression to evaluate. This is typically a call
132 * to a \c psa_xxx function that returns a value of type
133 * #psa_status_t.
134 */
Gilles Peskine5f7aeee2018-12-17 23:26:52 +0100135#define PSA_ASSERT( expr ) TEST_EQUAL( ( expr ), PSA_SUCCESS )
Gilles Peskine0174be22018-12-17 23:26:01 +0100136
Gilles Peskineb75125c2018-09-27 13:52:16 +0200137/** Allocate memory dynamically and fail the test case if this fails.
138 *
139 * You must set \p pointer to \c NULL before calling this macro and
140 * put `mbedtls_free( pointer )` in the test's cleanup code.
141 *
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100142 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200143 * This is usually what we want in tests since API functions are
144 * supposed to accept null pointers when a buffer size is zero.
145 *
146 * This macro expands to an instruction, not an expression.
147 * It may jump to the \c exit label.
148 *
149 * \param pointer An lvalue where the address of the allocated buffer
150 * will be stored.
151 * This expression may be evaluated multiple times.
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100152 * \param length Number of elements to allocate.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200153 * This expression may be evaluated multiple times.
154 *
155 */
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100156#define ASSERT_ALLOC( pointer, length ) \
157 do \
158 { \
159 TEST_ASSERT( ( pointer ) == NULL ); \
160 if( ( length ) != 0 ) \
161 { \
162 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
163 ( length ) ); \
164 TEST_ASSERT( ( pointer ) != NULL ); \
165 } \
166 } \
Gilles Peskineb75125c2018-09-27 13:52:16 +0200167 while( 0 )
168
Gilles Peskine3c225962018-09-27 13:56:31 +0200169/** Compare two buffers and fail the test case if they differ.
170 *
171 * This macro expands to an instruction, not an expression.
172 * It may jump to the \c exit label.
173 *
174 * \param p1 Pointer to the start of the first buffer.
175 * \param size1 Size of the first buffer in bytes.
176 * This expression may be evaluated multiple times.
177 * \param p2 Pointer to the start of the second buffer.
178 * \param size2 Size of the second buffer in bytes.
179 * This expression may be evaluated multiple times.
180 */
181#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
182 do \
183 { \
184 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
185 if( ( size1 ) != 0 ) \
186 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
187 } \
188 while( 0 )
189
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500190#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
191/**
192 * \brief This macro tests the statement passed to it as a test step or
193 * individual test in a test case. The macro assumes the test will fail
194 * and will generate an error.
195 *
196 * It allows a library function to return a value and tests the return
197 * code on return to confirm the given error code was returned.
198 *
199 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
200 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
201 * expected failure, and the test will pass.
202 *
203 * This macro is intended for negative parameter validation tests,
204 * where the failing function may return an error value or call
205 * MBEDTLS_PARAM_FAILED() to indicate the error.
206 *
207 * \param PARAM_ERROR_VALUE The expected error code.
208 *
209 * \param TEST The test expression to be tested.
210 */
211#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
212 do { \
213 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
214 if( (TEST) != (PARAM_ERR_VALUE) || \
215 test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
216 { \
217 test_fail( #TEST, __LINE__, __FILE__ ); \
218 goto exit; \
219 } \
220 } while( 0 )
221
222/**
223 * \brief This macro tests the statement passed to it as a test step or
224 * individual test in a test case. The macro assumes the test will fail
225 * and will generate an error.
226 *
227 * It assumes the library function under test cannot return a value and
228 * assumes errors can only be indicated byt calls to
229 * MBEDTLS_PARAM_FAILED().
230 *
231 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
232 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
233 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
234 * can be made.
235 *
236 * This macro is intended for negative parameter validation tests,
237 * where the failing function can only return an error by calling
238 * MBEDTLS_PARAM_FAILED() to indicate the error.
239 *
240 * \param TEST The test expression to be tested.
241 */
242#define TEST_INVALID_PARAM( TEST ) \
243 do { \
244 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
245 if( setjmp( param_fail_jmp ) == 0 ) \
246 { \
247 TEST; \
248 test_fail( #TEST, __LINE__, __FILE__ ); \
249 goto exit; \
250 } \
251 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
252 } while( 0 )
253#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
254
255/**
256 * \brief This macro tests the statement passed to it as a test step or
257 * individual test in a test case. The macro assumes the test will not fail.
258 *
259 * It assumes the library function under test cannot return a value and
260 * assumes errors can only be indicated by calls to
261 * MBEDTLS_PARAM_FAILED().
262 *
263 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
264 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
265 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
266 * can be made.
267 *
268 * This macro is intended to test that functions returning void
269 * accept all of the parameter values they're supposed to accept - eg
270 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
271 * that's allowed to be NULL happens to be NULL.
272 *
273 * Note: for functions that return something other that void,
274 * checking that they accept all the parameters they're supposed to
275 * accept is best done by using TEST_ASSERT() and checking the return
276 * value as well.
277 *
278 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
279 * disabled, as it makes sense to check that the functions accept all
280 * legal values even if this option is disabled - only in that case,
281 * the test is more about whether the function segfaults than about
282 * whether it invokes MBEDTLS_PARAM_FAILED().
283 *
284 * \param TEST The test expression to be tested.
285 */
286#define TEST_VALID_PARAM( TEST ) \
287 TEST_ASSERT( ( TEST, 1 ) );
288
Rich Evans4c091142015-02-02 12:04:10 +0000289#define assert(a) if( !( a ) ) \
290{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000292 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000294}
295
Gilles Peskinef055ad72018-12-17 23:18:00 +0100296#if defined(__GNUC__)
297/* Test if arg and &(arg)[0] have the same type. This is true if arg is
298 * an array but not if it's a pointer. */
299#define IS_ARRAY_NOT_POINTER( arg ) \
300 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
301 __typeof__( &( arg )[0] ) ) )
302#else
303/* On platforms where we don't know how to implement this check,
304 * omit it. Oh well, a non-portable check is better than nothing. */
305#define IS_ARRAY_NOT_POINTER( arg ) 1
306#endif
307
308/* A compile-time constant with the value 0. If `const_expr` is not a
309 * compile-time constant with a nonzero value, cause a compile-time error. */
310#define STATIC_ASSERT_EXPR( const_expr ) \
311 ( 0 && sizeof( struct { int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
312/* Return the scalar value `value` (possibly promoted). This is a compile-time
313 * constant if `value` is. `condition` must be a compile-time constant.
314 * If `condition` is false, arrange to cause a compile-time error. */
315#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
316 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
317
318#define ARRAY_LENGTH_UNSAFE( array ) \
Gilles Peskine3d2f9492018-12-17 23:17:17 +0100319 ( sizeof( array ) / sizeof( *( array ) ) )
Gilles Peskinef055ad72018-12-17 23:18:00 +0100320/** Return the number of elements of a static or stack array.
321 *
322 * \param array A value of array (not pointer) type.
323 *
324 * \return The number of elements of the array.
325 */
326#define ARRAY_LENGTH( array ) \
327 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
328 ARRAY_LENGTH_UNSAFE( array ) ) )
329
Gilles Peskinec08fc1d2018-12-18 08:47:00 +0100330/** Return the smaller of two values.
331 *
332 * \param x An integer-valued expression without side effects.
333 * \param y An integer-valued expression without side effects.
334 *
335 * \return The smaller of \p x and \p y.
336 */
337#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
338
339/** Return the larger of two values.
340 *
341 * \param x An integer-valued expression without side effects.
342 * \param y An integer-valued expression without side effects.
343 *
344 * \return The larger of \p x and \p y.
345 */
346#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
Gilles Peskinef055ad72018-12-17 23:18:00 +0100347
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000348/*
349 * 32-bit integer manipulation macros (big endian)
350 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000351#ifndef GET_UINT32_BE
352#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000353{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000354 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
355 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
356 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
357 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000358}
359#endif
360
Paul Bakker5c2364c2012-10-01 14:41:15 +0000361#ifndef PUT_UINT32_BE
362#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000363{ \
364 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
365 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
366 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
367 (b)[(i) + 3] = (unsigned char) ( (n) ); \
368}
369#endif
370
SimonB0269dad2016-02-17 23:34:30 +0000371
372/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100373/* Global variables */
374
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100375static struct
376{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500377 paramfail_test_state_t paramfail_test_state;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100378 int failed;
379 const char *test;
380 const char *filename;
381 int line_no;
382}
383test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100384
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400385#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurek1152fa82018-04-13 05:15:17 -0400386mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400387#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100388
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500389#if defined(MBEDTLS_CHECK_PARAMS)
390jmp_buf param_fail_jmp;
391jmp_buf jmp_tmp;
392#endif
393
SimonB8ca7bc42016-04-17 23:24:50 +0100394/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100395/* Helper flags for complex dependencies */
396
397/* Indicates whether we expect mbedtls_entropy_init
398 * to initialize some strong entropy source. */
399#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
400 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
401 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
402 defined(MBEDTLS_HAVEGE_C) || \
403 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
404 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100405#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100406#endif
407
408
409/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000410/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500411
412static void test_fail( const char *test, int line_no, const char* filename )
413{
414 test_info.failed = 1;
415 test_info.test = test;
416 test_info.line_no = line_no;
417 test_info.filename = filename;
418}
419
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400420static int platform_setup()
421{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400422 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400423#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400424 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400425#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400426 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400427}
428
429static void platform_teardown()
430{
431#if defined(MBEDTLS_PLATFORM_C)
432 mbedtls_platform_teardown( &platform_ctx );
433#endif /* MBEDTLS_PLATFORM_C */
434}
SimonB0269dad2016-02-17 23:34:30 +0000435
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500436#if defined(MBEDTLS_CHECK_PARAMS)
437void mbedtls_param_failed( const char *failure_condition,
438 const char *file,
439 int line )
440{
441 /* If we are testing the callback function... */
442 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
443 {
444 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
445 }
446 else
447 {
448 /* ...else we treat this as an error */
449
450 /* Record the location of the failure, but not as a failure yet, in case
451 * it was part of the test */
452 test_fail( failure_condition, line, file );
453 test_info.failed = 0;
454
455 longjmp( param_fail_jmp, 1 );
456 }
457}
458#endif
459
Janos Follath8ca53b52016-10-05 10:57:49 +0100460#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
461static int redirect_output( FILE** out_stream, const char* path )
462{
463 int stdout_fd = dup( fileno( *out_stream ) );
464
465 if( stdout_fd == -1 )
466 {
467 return -1;
468 }
469
470 fflush( *out_stream );
471 fclose( *out_stream );
472 *out_stream = fopen( path, "w" );
473
474 if( *out_stream == NULL )
475 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500476 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100477 return -1;
478 }
479
480 return stdout_fd;
481}
482
483static int restore_output( FILE** out_stream, int old_fd )
484{
485 fflush( *out_stream );
486 fclose( *out_stream );
487
488 *out_stream = fdopen( old_fd, "w" );
489 if( *out_stream == NULL )
490 {
491 return -1;
492 }
493
494 return 0;
495}
Simon Butchere0192962016-10-12 23:07:30 +0100496
Janos Follathe709f7c2016-10-13 11:26:29 +0100497static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100498{
Janos Follathe709f7c2016-10-13 11:26:29 +0100499 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100500}
Janos Follath8ca53b52016-10-05 10:57:49 +0100501#endif /* __unix__ || __APPLE__ __MACH__ */
502
Rich Evans4c091142015-02-02 12:04:10 +0000503static int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000504{
505 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000506 int len = strlen( ibuf ) / 2;
SimonB0269dad2016-02-17 23:34:30 +0000507 assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000508
Rich Evans4c091142015-02-02 12:04:10 +0000509 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000510 {
511 c = *ibuf++;
512 if( c >= '0' && c <= '9' )
513 c -= '0';
514 else if( c >= 'a' && c <= 'f' )
515 c -= 'a' - 10;
516 else if( c >= 'A' && c <= 'F' )
517 c -= 'A' - 10;
518 else
519 assert( 0 );
520
521 c2 = *ibuf++;
522 if( c2 >= '0' && c2 <= '9' )
523 c2 -= '0';
524 else if( c2 >= 'a' && c2 <= 'f' )
525 c2 -= 'a' - 10;
526 else if( c2 >= 'A' && c2 <= 'F' )
527 c2 -= 'A' - 10;
528 else
529 assert( 0 );
530
531 *obuf++ = ( c << 4 ) | c2;
532 }
533
534 return len;
535}
536
Rich Evans42914452015-02-02 12:09:25 +0000537static void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000538{
539 unsigned char l, h;
540
Rich Evans42914452015-02-02 12:09:25 +0000541 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000542 {
Rich Evans42914452015-02-02 12:09:25 +0000543 h = *ibuf / 16;
544 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000545
546 if( h < 10 )
547 *obuf++ = '0' + h;
548 else
549 *obuf++ = 'a' + h - 10;
550
551 if( l < 10 )
552 *obuf++ = '0' + l;
553 else
554 *obuf++ = 'a' + l - 10;
555
556 ++ibuf;
557 len--;
558 }
559}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000560
561/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200562 * Allocate and zeroize a buffer.
563 *
564 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
565 *
566 * For convenience, dies if allocation fails.
567 */
568static unsigned char *zero_alloc( size_t len )
569{
570 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000571 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200572
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200573 p = mbedtls_calloc( 1, actual_len );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200574 assert( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200575
576 memset( p, 0x00, actual_len );
577
578 return( p );
579}
580
581/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200582 * Allocate and fill a buffer from hex data.
583 *
584 * The buffer is sized exactly as needed. This allows to detect buffer
585 * overruns (including overreads) when running the test suite under valgrind.
586 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200587 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
588 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200589 * For convenience, dies if allocation fails.
590 */
591static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
592{
593 unsigned char *obuf;
594
Rich Evans42914452015-02-02 12:09:25 +0000595 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200596
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200597 if( *olen == 0 )
598 return( zero_alloc( *olen ) );
599
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200600 obuf = mbedtls_calloc( 1, *olen );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200601 assert( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200602
603 (void) unhexify( obuf, ibuf );
604
605 return( obuf );
606}
607
608/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000609 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000610 * Although predictable and often similar on multiple
611 * runs, this does not result in identical random on
612 * each run. So do not use this if the results of a
613 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000614 *
615 * rng_state shall be NULL.
616 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000617static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000618{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200619#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000620 size_t i;
621
Paul Bakker9dcc3222011-03-08 14:16:06 +0000622 if( rng_state != NULL )
623 rng_state = NULL;
624
Paul Bakkera3d195c2011-11-27 21:07:34 +0000625 for( i = 0; i < len; ++i )
626 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200627#else
628 if( rng_state != NULL )
629 rng_state = NULL;
630
631 arc4random_buf( output, len );
632#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000633
634 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000635}
636
637/**
638 * This function only returns zeros
639 *
640 * rng_state shall be NULL.
641 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000642static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000643{
644 if( rng_state != NULL )
645 rng_state = NULL;
646
Paul Bakkera3d195c2011-11-27 21:07:34 +0000647 memset( output, 0, len );
648
Paul Bakker9dcc3222011-03-08 14:16:06 +0000649 return( 0 );
650}
651
652typedef struct
653{
654 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000655 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000656} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000657
658/**
659 * This function returns random based on a buffer it receives.
660 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000661 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100662 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000663 * The number of bytes released from the buffer on each call to
664 * the random function is specified by per_call. (Can be between
665 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000666 *
667 * After the buffer is empty it will return rand();
668 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000669static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000670{
Paul Bakker997bbd12011-03-13 15:45:42 +0000671 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000672 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000673
674 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000675 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000676
Paul Bakkera3d195c2011-11-27 21:07:34 +0000677 use_len = len;
678 if( len > info->length )
679 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000680
Paul Bakkera3d195c2011-11-27 21:07:34 +0000681 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000682 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000683 memcpy( output, info->buf, use_len );
684 info->buf += use_len;
685 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000686 }
687
Paul Bakkera3d195c2011-11-27 21:07:34 +0000688 if( len - use_len > 0 )
689 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
690
691 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000692}
Paul Bakker997bbd12011-03-13 15:45:42 +0000693
694/**
695 * Info structure for the pseudo random function
696 *
697 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000698 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000699 * State( v0, v1 ) should be set to zero.
700 */
701typedef struct
702{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000703 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000704 uint32_t v0, v1;
705} rnd_pseudo_info;
706
707/**
708 * This function returns random based on a pseudo random function.
709 * This means the results should be identical on all systems.
710 * Pseudo random is based on the XTEA encryption algorithm to
711 * generate pseudorandom.
712 *
713 * rng_state shall be a pointer to a rnd_pseudo_info structure.
714 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000715static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000716{
717 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000718 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100719 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000720
721 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000722 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000723
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000724 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000725
726 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000727 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000728 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000729 sum = 0;
730
Paul Bakkera3d195c2011-11-27 21:07:34 +0000731 for( i = 0; i < 32; i++ )
732 {
Rich Evans42914452015-02-02 12:09:25 +0000733 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
734 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000735 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000736 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
737 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000738 }
739
Paul Bakker5c2364c2012-10-01 14:41:15 +0000740 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100741 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000742 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100743 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000744 }
745
Paul Bakkera3d195c2011-11-27 21:07:34 +0000746 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000747}
SimonB0269dad2016-02-17 23:34:30 +0000748
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100749int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100750{
751 int ret = 0;
752 uint32_t i = 0;
753
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500754 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100755 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100756
757 for( i = 0; i < a_len; i++ )
758 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500759 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100760 {
761 ret = -1;
762 break;
763 }
764 }
765 return ret;
766}