blob: ca186ddb12d6be77f93b9d2260a2dc9949952914 [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
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
Simon Butcher6542f6c2018-12-09 22:09:59 +000073typedef 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
Simon Butchera6463452018-12-06 17:41:56 +000086/**
87 * \brief This macro tests the expression passed to it as a test step or
88 * individual test in a test case.
89 *
90 * It allows a library function to return a value and return an error
91 * code that can be tested.
92 *
93 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +010094 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
95 * failure.
Simon Butchera6463452018-12-06 17:41:56 +000096 *
97 * This macro is not suitable for negative parameter validation tests,
98 * as it assumes the test step will not create an error.
99 *
100 * \param TEST The test expression to be tested.
101 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000102#define TEST_ASSERT( TEST ) \
103 do { \
104 if( ! (TEST) ) \
105 { \
106 test_fail( #TEST, __LINE__, __FILE__ ); \
107 goto exit; \
108 } \
SimonB0269dad2016-02-17 23:34:30 +0000109 } while( 0 )
110
Hanno Beckerd3369f62019-07-05 13:31:30 +0100111/**
112 * \brief This macro tests the expression passed to it and skips the
113 * running test if it doesn't evaluate to 'true'.
114 *
115 * \param TEST The test expression to be tested.
116 */
117#define TEST_ASSUME( TEST ) \
118 do { \
119 if( ! (TEST) ) \
120 { \
121 test_skip( #TEST, __LINE__, __FILE__ ); \
122 goto exit; \
123 } \
124 } while( 0 )
125
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100126#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
Simon Butchera6463452018-12-06 17:41:56 +0000127/**
128 * \brief This macro tests the statement passed to it as a test step or
129 * individual test in a test case. The macro assumes the test will fail
130 * and will generate an error.
131 *
132 * It allows a library function to return a value and tests the return
133 * code on return to confirm the given error code was returned.
134 *
135 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100136 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
Simon Butchera6463452018-12-06 17:41:56 +0000137 * expected failure, and the test will pass.
138 *
139 * This macro is intended for negative parameter validation tests,
140 * where the failing function may return an error value or call
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100141 * MBEDTLS_PARAM_FAILED() to indicate the error.
Simon Butchera6463452018-12-06 17:41:56 +0000142 *
143 * \param PARAM_ERROR_VALUE The expected error code.
144 *
145 * \param TEST The test expression to be tested.
146 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000147#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
148 do { \
149 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
Manuel Pégourié-Gonnardacfdc622018-12-11 10:36:21 +0100150 if( (TEST) != (PARAM_ERR_VALUE) || \
Simon Butcher6542f6c2018-12-09 22:09:59 +0000151 test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
152 { \
153 test_fail( #TEST, __LINE__, __FILE__ ); \
154 goto exit; \
155 } \
156 } while( 0 )
Simon Butchera6463452018-12-06 17:41:56 +0000157
158/**
159 * \brief This macro tests the statement passed to it as a test step or
160 * individual test in a test case. The macro assumes the test will fail
161 * and will generate an error.
162 *
163 * It assumes the library function under test cannot return a value and
164 * assumes errors can only be indicated byt calls to
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100165 * MBEDTLS_PARAM_FAILED().
Simon Butchera6463452018-12-06 17:41:56 +0000166 *
167 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100168 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
Simon Butchera6463452018-12-06 17:41:56 +0000169 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
170 * can be made.
171 *
172 * This macro is intended for negative parameter validation tests,
173 * where the failing function can only return an error by calling
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100174 * MBEDTLS_PARAM_FAILED() to indicate the error.
Simon Butchera6463452018-12-06 17:41:56 +0000175 *
176 * \param TEST The test expression to be tested.
177 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000178#define TEST_INVALID_PARAM( TEST ) \
179 do { \
180 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100181 if( setjmp( param_fail_jmp ) == 0 ) \
Simon Butcher6542f6c2018-12-09 22:09:59 +0000182 { \
183 TEST; \
184 test_fail( #TEST, __LINE__, __FILE__ ); \
185 goto exit; \
186 } \
187 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
Simon Butchera6463452018-12-06 17:41:56 +0000188 } while( 0 )
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100189#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
Simon Butchera6463452018-12-06 17:41:56 +0000190
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100191/**
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 not fail.
194 *
195 * It assumes the library function under test cannot return a value and
196 * assumes errors can only be indicated by calls to
197 * MBEDTLS_PARAM_FAILED().
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. If MBEDTLS_CHECK_PARAMS is not enabled, no test
202 * can be made.
203 *
Manuel Pégourié-Gonnardcd2b29c2018-12-12 10:23:57 +0100204 * This macro is intended to test that functions returning void
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100205 * accept all of the parameter values they're supposed to accept - eg
206 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
Manuel Pégourié-Gonnardcd2b29c2018-12-12 10:23:57 +0100207 * that's allowed to be NULL happens to be NULL.
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100208 *
209 * Note: for functions that return something other that void,
210 * checking that they accept all the parameters they're supposed to
211 * accept is best done by using TEST_ASSERT() and checking the return
212 * value as well.
213 *
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100214 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
215 * disabled, as it makes sense to check that the functions accept all
216 * legal values even if this option is disabled - only in that case,
Manuel Pégourié-Gonnarde7306d32018-12-13 09:45:49 +0100217 * the test is more about whether the function segfaults than about
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100218 * whether it invokes MBEDTLS_PARAM_FAILED().
219 *
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100220 * \param TEST The test expression to be tested.
221 */
222#define TEST_VALID_PARAM( TEST ) \
223 TEST_ASSERT( ( TEST, 1 ) );
Simon Butchera6463452018-12-06 17:41:56 +0000224
Gilles Peskine137d31b2019-06-07 14:52:07 +0200225#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
Rich Evans4c091142015-02-02 12:04:10 +0000226{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000228 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000230}
231
Ronald Cron07c83f22020-04-06 09:50:58 +0200232#if defined(__GNUC__)
233/* Test if arg and &(arg)[0] have the same type. This is true if arg is
234 * an array but not if it's a pointer. */
235#define IS_ARRAY_NOT_POINTER( arg ) \
236 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
237 __typeof__( &( arg )[0] ) ) )
238#else
239/* On platforms where we don't know how to implement this check,
240 * omit it. Oh well, a non-portable check is better than nothing. */
241#define IS_ARRAY_NOT_POINTER( arg ) 1
242#endif
243
244/* A compile-time constant with the value 0. If `const_expr` is not a
245 * compile-time constant with a nonzero value, cause a compile-time error. */
246#define STATIC_ASSERT_EXPR( const_expr ) \
makise-homura50f6a192020-08-23 00:39:15 +0300247 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
Ronald Cron07c83f22020-04-06 09:50:58 +0200248/* Return the scalar value `value` (possibly promoted). This is a compile-time
249 * constant if `value` is. `condition` must be a compile-time constant.
250 * If `condition` is false, arrange to cause a compile-time error. */
251#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
252 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
253
254#define ARRAY_LENGTH_UNSAFE( array ) \
255 ( sizeof( array ) / sizeof( *( array ) ) )
256/** Return the number of elements of a static or stack array.
257 *
258 * \param array A value of array (not pointer) type.
259 *
260 * \return The number of elements of the array.
261 */
262#define ARRAY_LENGTH( array ) \
263 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
264 ARRAY_LENGTH_UNSAFE( array ) ) )
265
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000266/*
267 * 32-bit integer manipulation macros (big endian)
268 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000269#ifndef GET_UINT32_BE
270#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000271{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000272 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
273 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
274 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
275 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000276}
277#endif
278
Paul Bakker5c2364c2012-10-01 14:41:15 +0000279#ifndef PUT_UINT32_BE
280#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000281{ \
282 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
283 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
284 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
285 (b)[(i) + 3] = (unsigned char) ( (n) ); \
286}
287#endif
288
SimonB0269dad2016-02-17 23:34:30 +0000289
290/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100291/* Global variables */
292
Hanno Beckerd3369f62019-07-05 13:31:30 +0100293typedef enum
294{
295 TEST_RESULT_SUCCESS = 0,
296 TEST_RESULT_FAILED,
297 TEST_RESULT_SKIPPED
298} test_result_t;
299
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100300static struct
301{
Simon Butcher6542f6c2018-12-09 22:09:59 +0000302 paramfail_test_state_t paramfail_test_state;
Hanno Beckerd3369f62019-07-05 13:31:30 +0100303 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100304 const char *test;
305 const char *filename;
306 int line_no;
307}
308test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100309
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400310#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurek1152fa82018-04-13 05:15:17 -0400311mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400312#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100313
Simon Butchera6463452018-12-06 17:41:56 +0000314#if defined(MBEDTLS_CHECK_PARAMS)
315jmp_buf param_fail_jmp;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000316jmp_buf jmp_tmp;
Simon Butchera6463452018-12-06 17:41:56 +0000317#endif
318
SimonB8ca7bc42016-04-17 23:24:50 +0100319/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100320/* Helper flags for complex dependencies */
321
322/* Indicates whether we expect mbedtls_entropy_init
323 * to initialize some strong entropy source. */
324#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
325 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
326 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
327 defined(MBEDTLS_HAVEGE_C) || \
328 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
329 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100330#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100331#endif
332
333
334/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000335/* Helper Functions */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000336
Simon Butcherecff2192018-10-03 16:17:41 +0100337void test_fail( const char *test, int line_no, const char* filename )
Simon Butcher6542f6c2018-12-09 22:09:59 +0000338{
Gilles Peskinedfb5cff2020-08-29 15:18:23 +0200339 if( test_info.result == TEST_RESULT_FAILED )
340 {
341 /* We've already recorded the test as having failed. Don't
342 * overwrite any previous information about the failure. */
343 return;
344 }
Hanno Beckerd3369f62019-07-05 13:31:30 +0100345 test_info.result = TEST_RESULT_FAILED;
346 test_info.test = test;
347 test_info.line_no = line_no;
348 test_info.filename = filename;
349}
350
351void test_skip( const char *test, int line_no, const char* filename )
352{
353 test_info.result = TEST_RESULT_SKIPPED;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000354 test_info.test = test;
355 test_info.line_no = line_no;
356 test_info.filename = filename;
357}
358
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400359static int platform_setup()
360{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400361 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400362#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400363 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400364#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400365 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400366}
367
368static void platform_teardown()
369{
370#if defined(MBEDTLS_PLATFORM_C)
371 mbedtls_platform_teardown( &platform_ctx );
372#endif /* MBEDTLS_PLATFORM_C */
373}
SimonB0269dad2016-02-17 23:34:30 +0000374
Simon Butchera6463452018-12-06 17:41:56 +0000375#if defined(MBEDTLS_CHECK_PARAMS)
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100376void mbedtls_param_failed( const char *failure_condition,
377 const char *file,
378 int line )
Simon Butchera6463452018-12-06 17:41:56 +0000379{
Simon Butcher6542f6c2018-12-09 22:09:59 +0000380 /* If we are testing the callback function... */
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100381 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
Simon Butcher6542f6c2018-12-09 22:09:59 +0000382 {
383 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
384 }
385 else
386 {
387 /* ...else we treat this as an error */
Simon Butchera6463452018-12-06 17:41:56 +0000388
Simon Butcher6542f6c2018-12-09 22:09:59 +0000389 /* Record the location of the failure, but not as a failure yet, in case
390 * it was part of the test */
391 test_fail( failure_condition, line, file );
Hanno Beckerd3369f62019-07-05 13:31:30 +0100392 test_info.result = TEST_RESULT_SUCCESS;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000393
394 longjmp( param_fail_jmp, 1 );
395 }
Simon Butchera6463452018-12-06 17:41:56 +0000396}
397#endif
398
Janos Follath8ca53b52016-10-05 10:57:49 +0100399#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44650ce762020-07-30 09:02:27 +0200400static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100401{
gufe44650ce762020-07-30 09:02:27 +0200402 int out_fd, dup_fd;
403 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100404
gufe44650ce762020-07-30 09:02:27 +0200405 out_fd = fileno( out_stream );
406 dup_fd = dup( out_fd );
407
408 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100409 {
gufe44650ce762020-07-30 09:02:27 +0200410 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100411 }
412
gufe44650ce762020-07-30 09:02:27 +0200413 path_stream = fopen( path, "w" );
414 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100415 {
gufe44650ce762020-07-30 09:02:27 +0200416 close( dup_fd );
417 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100418 }
419
gufe44650ce762020-07-30 09:02:27 +0200420 fflush( out_stream );
421 if( dup2( fileno( path_stream ), out_fd ) == -1 )
422 {
423 close( dup_fd );
424 fclose( path_stream );
425 return( -1 );
426 }
427
428 fclose( path_stream );
429 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100430}
431
gufe44650ce762020-07-30 09:02:27 +0200432static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100433{
gufe44650ce762020-07-30 09:02:27 +0200434 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100435
gufe44650ce762020-07-30 09:02:27 +0200436 fflush( out_stream );
437 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100438 {
gufe44650ce762020-07-30 09:02:27 +0200439 close( out_fd );
440 close( dup_fd );
441 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100442 }
443
gufe44650ce762020-07-30 09:02:27 +0200444 close( dup_fd );
445 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100446}
Janos Follath8ca53b52016-10-05 10:57:49 +0100447#endif /* __unix__ || __APPLE__ __MACH__ */
448
Ronald Crona0c9ff32020-06-08 17:05:57 +0200449int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000450{
451 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000452 int len = strlen( ibuf ) / 2;
Gilles Peskine137d31b2019-06-07 14:52:07 +0200453 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000454
Rich Evans4c091142015-02-02 12:04:10 +0000455 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000456 {
457 c = *ibuf++;
458 if( c >= '0' && c <= '9' )
459 c -= '0';
460 else if( c >= 'a' && c <= 'f' )
461 c -= 'a' - 10;
462 else if( c >= 'A' && c <= 'F' )
463 c -= 'A' - 10;
464 else
Gilles Peskine137d31b2019-06-07 14:52:07 +0200465 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000466
467 c2 = *ibuf++;
468 if( c2 >= '0' && c2 <= '9' )
469 c2 -= '0';
470 else if( c2 >= 'a' && c2 <= 'f' )
471 c2 -= 'a' - 10;
472 else if( c2 >= 'A' && c2 <= 'F' )
473 c2 -= 'A' - 10;
474 else
Gilles Peskine137d31b2019-06-07 14:52:07 +0200475 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000476
477 *obuf++ = ( c << 4 ) | c2;
478 }
479
480 return len;
481}
482
Ronald Crona0c9ff32020-06-08 17:05:57 +0200483void mbedtls_test_hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000484{
485 unsigned char l, h;
486
Rich Evans42914452015-02-02 12:09:25 +0000487 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000488 {
Rich Evans42914452015-02-02 12:09:25 +0000489 h = *ibuf / 16;
490 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000491
492 if( h < 10 )
493 *obuf++ = '0' + h;
494 else
495 *obuf++ = 'a' + h - 10;
496
497 if( l < 10 )
498 *obuf++ = '0' + l;
499 else
500 *obuf++ = 'a' + l - 10;
501
502 ++ibuf;
503 len--;
504 }
505}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000506
507/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200508 * Allocate and zeroize a buffer.
509 *
510 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
511 *
512 * For convenience, dies if allocation fails.
513 */
514static unsigned char *zero_alloc( size_t len )
515{
516 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000517 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200518
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200519 p = mbedtls_calloc( 1, actual_len );
Gilles Peskine137d31b2019-06-07 14:52:07 +0200520 TEST_HELPER_ASSERT( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200521
522 memset( p, 0x00, actual_len );
523
524 return( p );
525}
526
527/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200528 * Allocate and fill a buffer from hex data.
529 *
530 * The buffer is sized exactly as needed. This allows to detect buffer
531 * overruns (including overreads) when running the test suite under valgrind.
532 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200533 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
534 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200535 * For convenience, dies if allocation fails.
536 */
Simon Butcherecff2192018-10-03 16:17:41 +0100537unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200538{
539 unsigned char *obuf;
540
Rich Evans42914452015-02-02 12:09:25 +0000541 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200542
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200543 if( *olen == 0 )
544 return( zero_alloc( *olen ) );
545
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200546 obuf = mbedtls_calloc( 1, *olen );
Gilles Peskine137d31b2019-06-07 14:52:07 +0200547 TEST_HELPER_ASSERT( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200548
Ronald Crona0c9ff32020-06-08 17:05:57 +0200549 (void) mbedtls_test_unhexify( obuf, ibuf );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200550
551 return( obuf );
552}
553
554/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000555 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000556 * Although predictable and often similar on multiple
557 * runs, this does not result in identical random on
558 * each run. So do not use this if the results of a
559 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000560 *
561 * rng_state shall be NULL.
562 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000563static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000564{
gufe443fa7c642020-08-03 17:56:50 +0200565#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000566 size_t i;
567
Paul Bakker9dcc3222011-03-08 14:16:06 +0000568 if( rng_state != NULL )
569 rng_state = NULL;
570
Paul Bakkera3d195c2011-11-27 21:07:34 +0000571 for( i = 0; i < len; ++i )
572 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200573#else
574 if( rng_state != NULL )
575 rng_state = NULL;
576
577 arc4random_buf( output, len );
gufe443fa7c642020-08-03 17:56:50 +0200578#endif /* !OpenBSD && !NetBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000579
580 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000581}
582
583/**
584 * This function only returns zeros
585 *
586 * rng_state shall be NULL.
587 */
Simon Butcherecff2192018-10-03 16:17:41 +0100588int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000589{
590 if( rng_state != NULL )
591 rng_state = NULL;
592
Paul Bakkera3d195c2011-11-27 21:07:34 +0000593 memset( output, 0, len );
594
Paul Bakker9dcc3222011-03-08 14:16:06 +0000595 return( 0 );
596}
597
598typedef struct
599{
600 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000601 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000602} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000603
604/**
605 * This function returns random based on a buffer it receives.
606 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000607 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100608 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000609 * The number of bytes released from the buffer on each call to
610 * the random function is specified by per_call. (Can be between
611 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000612 *
613 * After the buffer is empty it will return rand();
614 */
Simon Butcherecff2192018-10-03 16:17:41 +0100615int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000616{
Paul Bakker997bbd12011-03-13 15:45:42 +0000617 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000618 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000619
620 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000621 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000622
Paul Bakkera3d195c2011-11-27 21:07:34 +0000623 use_len = len;
624 if( len > info->length )
625 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000626
Paul Bakkera3d195c2011-11-27 21:07:34 +0000627 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000628 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000629 memcpy( output, info->buf, use_len );
630 info->buf += use_len;
631 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000632 }
633
Paul Bakkera3d195c2011-11-27 21:07:34 +0000634 if( len - use_len > 0 )
635 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
636
637 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000638}
Paul Bakker997bbd12011-03-13 15:45:42 +0000639
640/**
641 * Info structure for the pseudo random function
642 *
643 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000644 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000645 * State( v0, v1 ) should be set to zero.
646 */
647typedef struct
648{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000649 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000650 uint32_t v0, v1;
651} rnd_pseudo_info;
652
653/**
654 * This function returns random based on a pseudo random function.
655 * This means the results should be identical on all systems.
656 * Pseudo random is based on the XTEA encryption algorithm to
657 * generate pseudorandom.
658 *
659 * rng_state shall be a pointer to a rnd_pseudo_info structure.
660 */
Simon Butcherecff2192018-10-03 16:17:41 +0100661int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000662{
663 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000664 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100665 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000666
667 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000668 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000669
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000670 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000671
672 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000673 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000674 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000675 sum = 0;
676
Paul Bakkera3d195c2011-11-27 21:07:34 +0000677 for( i = 0; i < 32; i++ )
678 {
Rich Evans42914452015-02-02 12:09:25 +0000679 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
680 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000681 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000682 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
683 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000684 }
685
Paul Bakker5c2364c2012-10-01 14:41:15 +0000686 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100687 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000688 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100689 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000690 }
691
Paul Bakkera3d195c2011-11-27 21:07:34 +0000692 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000693}
SimonB0269dad2016-02-17 23:34:30 +0000694
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100695int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100696{
697 int ret = 0;
698 uint32_t i = 0;
699
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100700 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100701 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100702
703 for( i = 0; i < a_len; i++ )
704 {
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100705 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100706 {
707 ret = -1;
708 break;
709 }
710 }
711 return ret;
712}