blob: 95b658da427325df799e940c125a4bcb541122a5 [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
Simon Butchera6463452018-12-06 17:41:56 +000026#if defined(MBEDTLS_CHECK_PARAMS)
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +010027#include "mbedtls/platform_util.h"
Simon Butchera6463452018-12-06 17:41:56 +000028#include <setjmp.h>
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +010029#endif
Simon Butcher747f5fe2018-12-07 16:53:57 +000030
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
Manuel Pégourié-Gonnarda2377222020-07-28 10:53:06 +020049#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
50#include <sanitizer/msan_interface.h>
51
52/* Use macros to avoid messing up with origin tracking */
53#define TEST_CF_SECRET __msan_allocated_memory
54// void __msan_allocated_memory(const volatile void* data, size_t size);
55#define TEST_CF_PUBLIC __msan_unpoison
56// void __msan_unpoison(const volatile void *a, size_t size);
57
58#else /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN */
59
60#define TEST_CF_SECRET(ptr, size)
61#define TEST_CF_PUBLIC(ptr, size)
62
63#endif /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN */
64
Azim Khand30ca132017-06-09 04:32:58 +010065/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010066typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010067{
68 uint8_t * x;
69 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010070} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010071
SimonB0269dad2016-02-17 23:34:30 +000072/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010073/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000074
Azim Khan62a5d7d2018-06-29 10:02:54 +010075#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
76#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
77#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010078
Azim Khan62a5d7d2018-06-29 10:02:54 +010079#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
80#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
81#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
82#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
83 Only int, string, binary data
84 and integer expressions are
85 allowed */
86#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
87 build */
SimonB0269dad2016-02-17 23:34:30 +000088
Simon Butcher6542f6c2018-12-09 22:09:59 +000089typedef enum
90{
91 PARAMFAIL_TESTSTATE_IDLE = 0, /* No parameter failure call test */
92 PARAMFAIL_TESTSTATE_PENDING, /* Test call to the parameter failure
93 * is pending */
94 PARAMFAIL_TESTSTATE_CALLED /* The test call to the parameter
95 * failure function has been made */
96} paramfail_test_state_t;
97
SimonB0269dad2016-02-17 23:34:30 +000098
99/*----------------------------------------------------------------------------*/
100/* Macros */
101
Simon Butchera6463452018-12-06 17:41:56 +0000102/**
103 * \brief This macro tests the expression passed to it as a test step or
104 * individual test in a test case.
105 *
106 * It allows a library function to return a value and return an error
107 * code that can be tested.
108 *
109 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100110 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
111 * failure.
Simon Butchera6463452018-12-06 17:41:56 +0000112 *
113 * This macro is not suitable for negative parameter validation tests,
114 * as it assumes the test step will not create an error.
115 *
116 * \param TEST The test expression to be tested.
117 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000118#define TEST_ASSERT( TEST ) \
119 do { \
120 if( ! (TEST) ) \
121 { \
122 test_fail( #TEST, __LINE__, __FILE__ ); \
123 goto exit; \
124 } \
SimonB0269dad2016-02-17 23:34:30 +0000125 } while( 0 )
126
Hanno Beckerd3369f62019-07-05 13:31:30 +0100127/**
128 * \brief This macro tests the expression passed to it and skips the
129 * running test if it doesn't evaluate to 'true'.
130 *
131 * \param TEST The test expression to be tested.
132 */
133#define TEST_ASSUME( TEST ) \
134 do { \
135 if( ! (TEST) ) \
136 { \
137 test_skip( #TEST, __LINE__, __FILE__ ); \
138 goto exit; \
139 } \
140 } while( 0 )
141
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100142#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
Simon Butchera6463452018-12-06 17:41:56 +0000143/**
144 * \brief This macro tests the statement passed to it as a test step or
145 * individual test in a test case. The macro assumes the test will fail
146 * and will generate an error.
147 *
148 * It allows a library function to return a value and tests the return
149 * code on return to confirm the given error code was returned.
150 *
151 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100152 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
Simon Butchera6463452018-12-06 17:41:56 +0000153 * expected failure, and the test will pass.
154 *
155 * This macro is intended for negative parameter validation tests,
156 * where the failing function may return an error value or call
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100157 * MBEDTLS_PARAM_FAILED() to indicate the error.
Simon Butchera6463452018-12-06 17:41:56 +0000158 *
159 * \param PARAM_ERROR_VALUE The expected error code.
160 *
161 * \param TEST The test expression to be tested.
162 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000163#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
164 do { \
165 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
Manuel Pégourié-Gonnardacfdc622018-12-11 10:36:21 +0100166 if( (TEST) != (PARAM_ERR_VALUE) || \
Simon Butcher6542f6c2018-12-09 22:09:59 +0000167 test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
168 { \
169 test_fail( #TEST, __LINE__, __FILE__ ); \
170 goto exit; \
171 } \
172 } while( 0 )
Simon Butchera6463452018-12-06 17:41:56 +0000173
174/**
175 * \brief This macro tests the statement passed to it as a test step or
176 * individual test in a test case. The macro assumes the test will fail
177 * and will generate an error.
178 *
179 * It assumes the library function under test cannot return a value and
180 * assumes errors can only be indicated byt calls to
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100181 * MBEDTLS_PARAM_FAILED().
Simon Butchera6463452018-12-06 17:41:56 +0000182 *
183 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100184 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
Simon Butchera6463452018-12-06 17:41:56 +0000185 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
186 * can be made.
187 *
188 * This macro is intended for negative parameter validation tests,
189 * where the failing function can only return an error by calling
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100190 * MBEDTLS_PARAM_FAILED() to indicate the error.
Simon Butchera6463452018-12-06 17:41:56 +0000191 *
192 * \param TEST The test expression to be tested.
193 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000194#define TEST_INVALID_PARAM( TEST ) \
195 do { \
196 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100197 if( setjmp( param_fail_jmp ) == 0 ) \
Simon Butcher6542f6c2018-12-09 22:09:59 +0000198 { \
199 TEST; \
200 test_fail( #TEST, __LINE__, __FILE__ ); \
201 goto exit; \
202 } \
203 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
Simon Butchera6463452018-12-06 17:41:56 +0000204 } while( 0 )
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100205#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
Simon Butchera6463452018-12-06 17:41:56 +0000206
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100207/**
208 * \brief This macro tests the statement passed to it as a test step or
209 * individual test in a test case. The macro assumes the test will not fail.
210 *
211 * It assumes the library function under test cannot return a value and
212 * assumes errors can only be indicated by calls to
213 * MBEDTLS_PARAM_FAILED().
214 *
215 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
216 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
217 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
218 * can be made.
219 *
Manuel Pégourié-Gonnardcd2b29c2018-12-12 10:23:57 +0100220 * This macro is intended to test that functions returning void
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100221 * accept all of the parameter values they're supposed to accept - eg
222 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
Manuel Pégourié-Gonnardcd2b29c2018-12-12 10:23:57 +0100223 * that's allowed to be NULL happens to be NULL.
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100224 *
225 * Note: for functions that return something other that void,
226 * checking that they accept all the parameters they're supposed to
227 * accept is best done by using TEST_ASSERT() and checking the return
228 * value as well.
229 *
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100230 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
231 * disabled, as it makes sense to check that the functions accept all
232 * legal values even if this option is disabled - only in that case,
Manuel Pégourié-Gonnarde7306d32018-12-13 09:45:49 +0100233 * the test is more about whether the function segfaults than about
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100234 * whether it invokes MBEDTLS_PARAM_FAILED().
235 *
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100236 * \param TEST The test expression to be tested.
237 */
238#define TEST_VALID_PARAM( TEST ) \
239 TEST_ASSERT( ( TEST, 1 ) );
Simon Butchera6463452018-12-06 17:41:56 +0000240
Gilles Peskine137d31b2019-06-07 14:52:07 +0200241#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
Rich Evans4c091142015-02-02 12:04:10 +0000242{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000244 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000246}
247
Ronald Cron07c83f22020-04-06 09:50:58 +0200248#if defined(__GNUC__)
249/* Test if arg and &(arg)[0] have the same type. This is true if arg is
250 * an array but not if it's a pointer. */
251#define IS_ARRAY_NOT_POINTER( arg ) \
252 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
253 __typeof__( &( arg )[0] ) ) )
254#else
255/* On platforms where we don't know how to implement this check,
256 * omit it. Oh well, a non-portable check is better than nothing. */
257#define IS_ARRAY_NOT_POINTER( arg ) 1
258#endif
259
260/* A compile-time constant with the value 0. If `const_expr` is not a
261 * compile-time constant with a nonzero value, cause a compile-time error. */
262#define STATIC_ASSERT_EXPR( const_expr ) \
263 ( 0 && sizeof( struct { int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
264/* Return the scalar value `value` (possibly promoted). This is a compile-time
265 * constant if `value` is. `condition` must be a compile-time constant.
266 * If `condition` is false, arrange to cause a compile-time error. */
267#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
268 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
269
270#define ARRAY_LENGTH_UNSAFE( array ) \
271 ( sizeof( array ) / sizeof( *( array ) ) )
272/** Return the number of elements of a static or stack array.
273 *
274 * \param array A value of array (not pointer) type.
275 *
276 * \return The number of elements of the array.
277 */
278#define ARRAY_LENGTH( array ) \
279 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
280 ARRAY_LENGTH_UNSAFE( array ) ) )
281
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000282/*
283 * 32-bit integer manipulation macros (big endian)
284 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000285#ifndef GET_UINT32_BE
286#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000287{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000288 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
289 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
290 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
291 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000292}
293#endif
294
Paul Bakker5c2364c2012-10-01 14:41:15 +0000295#ifndef PUT_UINT32_BE
296#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000297{ \
298 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
299 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
300 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
301 (b)[(i) + 3] = (unsigned char) ( (n) ); \
302}
303#endif
304
SimonB0269dad2016-02-17 23:34:30 +0000305
306/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100307/* Global variables */
308
Hanno Beckerd3369f62019-07-05 13:31:30 +0100309typedef enum
310{
311 TEST_RESULT_SUCCESS = 0,
312 TEST_RESULT_FAILED,
313 TEST_RESULT_SKIPPED
314} test_result_t;
315
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100316static struct
317{
Simon Butcher6542f6c2018-12-09 22:09:59 +0000318 paramfail_test_state_t paramfail_test_state;
Hanno Beckerd3369f62019-07-05 13:31:30 +0100319 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100320 const char *test;
321 const char *filename;
322 int line_no;
323}
324test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100325
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400326#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurek1152fa82018-04-13 05:15:17 -0400327mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400328#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100329
Simon Butchera6463452018-12-06 17:41:56 +0000330#if defined(MBEDTLS_CHECK_PARAMS)
331jmp_buf param_fail_jmp;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000332jmp_buf jmp_tmp;
Simon Butchera6463452018-12-06 17:41:56 +0000333#endif
334
SimonB8ca7bc42016-04-17 23:24:50 +0100335/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100336/* Helper flags for complex dependencies */
337
338/* Indicates whether we expect mbedtls_entropy_init
339 * to initialize some strong entropy source. */
340#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
341 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
342 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
343 defined(MBEDTLS_HAVEGE_C) || \
344 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
345 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100346#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100347#endif
348
349
350/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000351/* Helper Functions */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000352
Simon Butcherecff2192018-10-03 16:17:41 +0100353void test_fail( const char *test, int line_no, const char* filename )
Simon Butcher6542f6c2018-12-09 22:09:59 +0000354{
Hanno Beckerd3369f62019-07-05 13:31:30 +0100355 test_info.result = TEST_RESULT_FAILED;
356 test_info.test = test;
357 test_info.line_no = line_no;
358 test_info.filename = filename;
359}
360
361void test_skip( const char *test, int line_no, const char* filename )
362{
363 test_info.result = TEST_RESULT_SKIPPED;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000364 test_info.test = test;
365 test_info.line_no = line_no;
366 test_info.filename = filename;
367}
368
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400369static int platform_setup()
370{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400371 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400372#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400373 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400374#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400375 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400376}
377
378static void platform_teardown()
379{
380#if defined(MBEDTLS_PLATFORM_C)
381 mbedtls_platform_teardown( &platform_ctx );
382#endif /* MBEDTLS_PLATFORM_C */
383}
SimonB0269dad2016-02-17 23:34:30 +0000384
Simon Butchera6463452018-12-06 17:41:56 +0000385#if defined(MBEDTLS_CHECK_PARAMS)
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100386void mbedtls_param_failed( const char *failure_condition,
387 const char *file,
388 int line )
Simon Butchera6463452018-12-06 17:41:56 +0000389{
Simon Butcher6542f6c2018-12-09 22:09:59 +0000390 /* If we are testing the callback function... */
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100391 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
Simon Butcher6542f6c2018-12-09 22:09:59 +0000392 {
393 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
394 }
395 else
396 {
397 /* ...else we treat this as an error */
Simon Butchera6463452018-12-06 17:41:56 +0000398
Simon Butcher6542f6c2018-12-09 22:09:59 +0000399 /* Record the location of the failure, but not as a failure yet, in case
400 * it was part of the test */
401 test_fail( failure_condition, line, file );
Hanno Beckerd3369f62019-07-05 13:31:30 +0100402 test_info.result = TEST_RESULT_SUCCESS;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000403
404 longjmp( param_fail_jmp, 1 );
405 }
Simon Butchera6463452018-12-06 17:41:56 +0000406}
407#endif
408
Janos Follath8ca53b52016-10-05 10:57:49 +0100409#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
410static int redirect_output( FILE** out_stream, const char* path )
411{
412 int stdout_fd = dup( fileno( *out_stream ) );
413
414 if( stdout_fd == -1 )
415 {
416 return -1;
417 }
418
419 fflush( *out_stream );
420 fclose( *out_stream );
421 *out_stream = fopen( path, "w" );
422
423 if( *out_stream == NULL )
424 {
Andres Amaya Garcia687d6732018-11-26 21:23:28 +0000425 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100426 return -1;
427 }
428
429 return stdout_fd;
430}
431
432static int restore_output( FILE** out_stream, int old_fd )
433{
434 fflush( *out_stream );
435 fclose( *out_stream );
436
437 *out_stream = fdopen( old_fd, "w" );
438 if( *out_stream == NULL )
439 {
440 return -1;
441 }
442
443 return 0;
444}
Simon Butchere0192962016-10-12 23:07:30 +0100445
Janos Follathe709f7c2016-10-13 11:26:29 +0100446static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100447{
Janos Follathe709f7c2016-10-13 11:26:29 +0100448 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100449}
Janos Follath8ca53b52016-10-05 10:57:49 +0100450#endif /* __unix__ || __APPLE__ __MACH__ */
451
Ronald Crona0c9ff32020-06-08 17:05:57 +0200452int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000453{
454 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000455 int len = strlen( ibuf ) / 2;
Gilles Peskine137d31b2019-06-07 14:52:07 +0200456 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000457
Rich Evans4c091142015-02-02 12:04:10 +0000458 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000459 {
460 c = *ibuf++;
461 if( c >= '0' && c <= '9' )
462 c -= '0';
463 else if( c >= 'a' && c <= 'f' )
464 c -= 'a' - 10;
465 else if( c >= 'A' && c <= 'F' )
466 c -= 'A' - 10;
467 else
Gilles Peskine137d31b2019-06-07 14:52:07 +0200468 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000469
470 c2 = *ibuf++;
471 if( c2 >= '0' && c2 <= '9' )
472 c2 -= '0';
473 else if( c2 >= 'a' && c2 <= 'f' )
474 c2 -= 'a' - 10;
475 else if( c2 >= 'A' && c2 <= 'F' )
476 c2 -= 'A' - 10;
477 else
Gilles Peskine137d31b2019-06-07 14:52:07 +0200478 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000479
480 *obuf++ = ( c << 4 ) | c2;
481 }
482
483 return len;
484}
485
Ronald Crona0c9ff32020-06-08 17:05:57 +0200486void mbedtls_test_hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000487{
488 unsigned char l, h;
489
Rich Evans42914452015-02-02 12:09:25 +0000490 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000491 {
Rich Evans42914452015-02-02 12:09:25 +0000492 h = *ibuf / 16;
493 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000494
495 if( h < 10 )
496 *obuf++ = '0' + h;
497 else
498 *obuf++ = 'a' + h - 10;
499
500 if( l < 10 )
501 *obuf++ = '0' + l;
502 else
503 *obuf++ = 'a' + l - 10;
504
505 ++ibuf;
506 len--;
507 }
508}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000509
510/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200511 * Allocate and zeroize a buffer.
512 *
513 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
514 *
515 * For convenience, dies if allocation fails.
516 */
517static unsigned char *zero_alloc( size_t len )
518{
519 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000520 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200521
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200522 p = mbedtls_calloc( 1, actual_len );
Gilles Peskine137d31b2019-06-07 14:52:07 +0200523 TEST_HELPER_ASSERT( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200524
525 memset( p, 0x00, actual_len );
526
527 return( p );
528}
529
530/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200531 * Allocate and fill a buffer from hex data.
532 *
533 * The buffer is sized exactly as needed. This allows to detect buffer
534 * overruns (including overreads) when running the test suite under valgrind.
535 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200536 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
537 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200538 * For convenience, dies if allocation fails.
539 */
Simon Butcherecff2192018-10-03 16:17:41 +0100540unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200541{
542 unsigned char *obuf;
543
Rich Evans42914452015-02-02 12:09:25 +0000544 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200545
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200546 if( *olen == 0 )
547 return( zero_alloc( *olen ) );
548
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200549 obuf = mbedtls_calloc( 1, *olen );
Gilles Peskine137d31b2019-06-07 14:52:07 +0200550 TEST_HELPER_ASSERT( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200551
Ronald Crona0c9ff32020-06-08 17:05:57 +0200552 (void) mbedtls_test_unhexify( obuf, ibuf );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200553
554 return( obuf );
555}
556
557/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000558 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000559 * Although predictable and often similar on multiple
560 * runs, this does not result in identical random on
561 * each run. So do not use this if the results of a
562 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000563 *
564 * rng_state shall be NULL.
565 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000566static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000567{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200568#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000569 size_t i;
570
Paul Bakker9dcc3222011-03-08 14:16:06 +0000571 if( rng_state != NULL )
572 rng_state = NULL;
573
Paul Bakkera3d195c2011-11-27 21:07:34 +0000574 for( i = 0; i < len; ++i )
575 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200576#else
577 if( rng_state != NULL )
578 rng_state = NULL;
579
580 arc4random_buf( output, len );
581#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000582
583 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000584}
585
586/**
587 * This function only returns zeros
588 *
589 * rng_state shall be NULL.
590 */
Simon Butcherecff2192018-10-03 16:17:41 +0100591int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000592{
593 if( rng_state != NULL )
594 rng_state = NULL;
595
Paul Bakkera3d195c2011-11-27 21:07:34 +0000596 memset( output, 0, len );
597
Paul Bakker9dcc3222011-03-08 14:16:06 +0000598 return( 0 );
599}
600
601typedef struct
602{
603 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000604 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000605} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000606
607/**
608 * This function returns random based on a buffer it receives.
609 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000610 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100611 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000612 * The number of bytes released from the buffer on each call to
613 * the random function is specified by per_call. (Can be between
614 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000615 *
616 * After the buffer is empty it will return rand();
617 */
Simon Butcherecff2192018-10-03 16:17:41 +0100618int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000619{
Paul Bakker997bbd12011-03-13 15:45:42 +0000620 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000621 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000622
623 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000624 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000625
Paul Bakkera3d195c2011-11-27 21:07:34 +0000626 use_len = len;
627 if( len > info->length )
628 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000629
Paul Bakkera3d195c2011-11-27 21:07:34 +0000630 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000631 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000632 memcpy( output, info->buf, use_len );
633 info->buf += use_len;
634 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000635 }
636
Paul Bakkera3d195c2011-11-27 21:07:34 +0000637 if( len - use_len > 0 )
638 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
639
640 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000641}
Paul Bakker997bbd12011-03-13 15:45:42 +0000642
643/**
644 * Info structure for the pseudo random function
645 *
646 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000647 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000648 * State( v0, v1 ) should be set to zero.
649 */
650typedef struct
651{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000652 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000653 uint32_t v0, v1;
654} rnd_pseudo_info;
655
656/**
657 * This function returns random based on a pseudo random function.
658 * This means the results should be identical on all systems.
659 * Pseudo random is based on the XTEA encryption algorithm to
660 * generate pseudorandom.
661 *
662 * rng_state shall be a pointer to a rnd_pseudo_info structure.
663 */
Simon Butcherecff2192018-10-03 16:17:41 +0100664int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000665{
666 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000667 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100668 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000669
670 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000671 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000672
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000673 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000674
675 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000676 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000677 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000678 sum = 0;
679
Paul Bakkera3d195c2011-11-27 21:07:34 +0000680 for( i = 0; i < 32; i++ )
681 {
Rich Evans42914452015-02-02 12:09:25 +0000682 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
683 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000684 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000685 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
686 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000687 }
688
Paul Bakker5c2364c2012-10-01 14:41:15 +0000689 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100690 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000691 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100692 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000693 }
694
Paul Bakkera3d195c2011-11-27 21:07:34 +0000695 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000696}
SimonB0269dad2016-02-17 23:34:30 +0000697
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100698int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100699{
700 int ret = 0;
701 uint32_t i = 0;
702
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100703 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100704 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100705
706 for( i = 0; i < a_len; i++ )
707 {
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100708 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100709 {
710 ret = -1;
711 break;
712 }
713 }
714 return ret;
715}