blob: 84b4dcd4f98982447febc206f2f78d4a94bdb153 [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
Hanno Beckerf2bf1152019-08-20 16:15:58 +010031#if defined(MBEDTLS_USE_TINYCRYPT)
32#include "tinycrypt/ecc.h"
33#endif /* MBEDTLS_USE_TINYCRYPT */
34
Arto Kinnunen0a8f87f2020-01-07 13:15:40 +020035#include "mbedtls/entropy.h"
36
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000037#ifdef _MSC_VER
38#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010039typedef UINT8 uint8_t;
40typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000041typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000042#define strncasecmp _strnicmp
43#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000044#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020045#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000046#endif
47
Paul Bakker19343182013-08-16 13:31:10 +020048#include <string.h>
49
Janos Follath8ca53b52016-10-05 10:57:49 +010050#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
51#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000052#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010053#endif
SimonB0269dad2016-02-17 23:34:30 +000054
Azim Khand30ca132017-06-09 04:32:58 +010055/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010056typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010057{
58 uint8_t * x;
59 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010060} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010061
SimonB0269dad2016-02-17 23:34:30 +000062/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010063/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000064
Azim Khan62a5d7d2018-06-29 10:02:54 +010065#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
66#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
67#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010068
Azim Khan62a5d7d2018-06-29 10:02:54 +010069#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
70#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
71#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
72#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
73 Only int, string, binary data
74 and integer expressions are
75 allowed */
76#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
77 build */
SimonB0269dad2016-02-17 23:34:30 +000078
Simon Butcher6542f6c2018-12-09 22:09:59 +000079typedef enum
80{
81 PARAMFAIL_TESTSTATE_IDLE = 0, /* No parameter failure call test */
82 PARAMFAIL_TESTSTATE_PENDING, /* Test call to the parameter failure
83 * is pending */
84 PARAMFAIL_TESTSTATE_CALLED /* The test call to the parameter
85 * failure function has been made */
86} paramfail_test_state_t;
87
SimonB0269dad2016-02-17 23:34:30 +000088
89/*----------------------------------------------------------------------------*/
90/* Macros */
91
Simon Butchera6463452018-12-06 17:41:56 +000092/**
93 * \brief This macro tests the expression passed to it as a test step or
94 * individual test in a test case.
95 *
96 * It allows a library function to return a value and return an error
97 * code that can be tested.
98 *
99 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100100 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
101 * failure.
Simon Butchera6463452018-12-06 17:41:56 +0000102 *
103 * This macro is not suitable for negative parameter validation tests,
104 * as it assumes the test step will not create an error.
105 *
106 * \param TEST The test expression to be tested.
107 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000108#define TEST_ASSERT( TEST ) \
109 do { \
110 if( ! (TEST) ) \
111 { \
112 test_fail( #TEST, __LINE__, __FILE__ ); \
113 goto exit; \
114 } \
SimonB0269dad2016-02-17 23:34:30 +0000115 } while( 0 )
116
Hanno Becker930fbf62019-07-05 13:31:30 +0100117/**
118 * \brief This macro tests the expression passed to it and skips the
119 * running test if it doesn't evaluate to 'true'.
120 *
121 * \param TEST The test expression to be tested.
122 */
123#define TEST_ASSUME( TEST ) \
124 do { \
125 if( ! (TEST) ) \
126 { \
127 test_skip( #TEST, __LINE__, __FILE__ ); \
128 goto exit; \
129 } \
130 } while( 0 )
131
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100132#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
Simon Butchera6463452018-12-06 17:41:56 +0000133/**
134 * \brief This macro tests the statement passed to it as a test step or
135 * individual test in a test case. The macro assumes the test will fail
136 * and will generate an error.
137 *
138 * It allows a library function to return a value and tests the return
139 * code on return to confirm the given error code was returned.
140 *
141 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100142 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
Simon Butchera6463452018-12-06 17:41:56 +0000143 * expected failure, and the test will pass.
144 *
145 * This macro is intended for negative parameter validation tests,
146 * where the failing function may return an error value or call
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100147 * MBEDTLS_PARAM_FAILED() to indicate the error.
Simon Butchera6463452018-12-06 17:41:56 +0000148 *
149 * \param PARAM_ERROR_VALUE The expected error code.
150 *
151 * \param TEST The test expression to be tested.
152 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000153#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
154 do { \
155 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
Manuel Pégourié-Gonnardacfdc622018-12-11 10:36:21 +0100156 if( (TEST) != (PARAM_ERR_VALUE) || \
Simon Butcher6542f6c2018-12-09 22:09:59 +0000157 test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
158 { \
159 test_fail( #TEST, __LINE__, __FILE__ ); \
160 goto exit; \
161 } \
162 } while( 0 )
Simon Butchera6463452018-12-06 17:41:56 +0000163
164/**
165 * \brief This macro tests the statement passed to it as a test step or
166 * individual test in a test case. The macro assumes the test will fail
167 * and will generate an error.
168 *
169 * It assumes the library function under test cannot return a value and
170 * assumes errors can only be indicated byt calls to
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100171 * MBEDTLS_PARAM_FAILED().
Simon Butchera6463452018-12-06 17:41:56 +0000172 *
173 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100174 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
Simon Butchera6463452018-12-06 17:41:56 +0000175 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
176 * can be made.
177 *
178 * This macro is intended for negative parameter validation tests,
179 * where the failing function can only return an error by calling
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100180 * MBEDTLS_PARAM_FAILED() to indicate the error.
Simon Butchera6463452018-12-06 17:41:56 +0000181 *
182 * \param TEST The test expression to be tested.
183 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000184#define TEST_INVALID_PARAM( TEST ) \
185 do { \
186 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100187 if( setjmp( param_fail_jmp ) == 0 ) \
Simon Butcher6542f6c2018-12-09 22:09:59 +0000188 { \
189 TEST; \
190 test_fail( #TEST, __LINE__, __FILE__ ); \
191 goto exit; \
192 } \
193 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
Simon Butchera6463452018-12-06 17:41:56 +0000194 } while( 0 )
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100195#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
Simon Butchera6463452018-12-06 17:41:56 +0000196
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100197/**
198 * \brief This macro tests the statement passed to it as a test step or
199 * individual test in a test case. The macro assumes the test will not fail.
200 *
201 * It assumes the library function under test cannot return a value and
202 * assumes errors can only be indicated by calls to
203 * MBEDTLS_PARAM_FAILED().
204 *
205 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
206 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
207 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
208 * can be made.
209 *
Manuel Pégourié-Gonnardcd2b29c2018-12-12 10:23:57 +0100210 * This macro is intended to test that functions returning void
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100211 * accept all of the parameter values they're supposed to accept - eg
212 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
Manuel Pégourié-Gonnardcd2b29c2018-12-12 10:23:57 +0100213 * that's allowed to be NULL happens to be NULL.
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100214 *
215 * Note: for functions that return something other that void,
216 * checking that they accept all the parameters they're supposed to
217 * accept is best done by using TEST_ASSERT() and checking the return
218 * value as well.
219 *
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100220 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
221 * disabled, as it makes sense to check that the functions accept all
222 * legal values even if this option is disabled - only in that case,
Manuel Pégourié-Gonnarde7306d32018-12-13 09:45:49 +0100223 * the test is more about whether the function segfaults than about
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100224 * whether it invokes MBEDTLS_PARAM_FAILED().
225 *
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100226 * \param TEST The test expression to be tested.
227 */
228#define TEST_VALID_PARAM( TEST ) \
229 TEST_ASSERT( ( TEST, 1 ) );
Simon Butchera6463452018-12-06 17:41:56 +0000230
Gilles Peskine137d31b2019-06-07 14:52:07 +0200231#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
Rich Evans4c091142015-02-02 12:04:10 +0000232{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000234 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000236}
237
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400238#if defined(__GNUC__)
239/* Test if arg and &(arg)[0] have the same type. This is true if arg is
240 * an array but not if it's a pointer. */
241#define IS_ARRAY_NOT_POINTER( arg ) \
242 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
243 __typeof__( &( arg )[0] ) ) )
244#else
245/* On platforms where we don't know how to implement this check,
246 * omit it. Oh well, a non-portable check is better than nothing. */
247#define IS_ARRAY_NOT_POINTER( arg ) 1
248#endif
249
250/* A compile-time constant with the value 0. If `const_expr` is not a
251 * compile-time constant with a nonzero value, cause a compile-time error. */
252#define STATIC_ASSERT_EXPR( const_expr ) \
253 ( 0 && sizeof( struct { int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
254/* Return the scalar value `value` (possibly promoted). This is a compile-time
255 * constant if `value` is. `condition` must be a compile-time constant.
256 * If `condition` is false, arrange to cause a compile-time error. */
257#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
258 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
259
260#define ARRAY_LENGTH_UNSAFE( array ) \
261 ( sizeof( array ) / sizeof( *( array ) ) )
262/** Return the number of elements of a static or stack array.
263 *
264 * \param array A value of array (not pointer) type.
265 *
266 * \return The number of elements of the array.
267 */
268#define ARRAY_LENGTH( array ) \
269 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
270 ARRAY_LENGTH_UNSAFE( array ) ) )
271
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000272/*
273 * 32-bit integer manipulation macros (big endian)
274 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000275#ifndef GET_UINT32_BE
276#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000277{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000278 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
279 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
280 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
281 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000282}
283#endif
284
Paul Bakker5c2364c2012-10-01 14:41:15 +0000285#ifndef PUT_UINT32_BE
286#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000287{ \
288 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
289 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
290 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
291 (b)[(i) + 3] = (unsigned char) ( (n) ); \
292}
293#endif
294
SimonB0269dad2016-02-17 23:34:30 +0000295
296/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100297/* Global variables */
298
Hanno Becker930fbf62019-07-05 13:31:30 +0100299typedef enum
300{
301 TEST_RESULT_SUCCESS = 0,
302 TEST_RESULT_FAILED,
303 TEST_RESULT_SKIPPED
304} test_result_t;
305
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100306static struct
307{
Simon Butcher6542f6c2018-12-09 22:09:59 +0000308 paramfail_test_state_t paramfail_test_state;
Hanno Becker930fbf62019-07-05 13:31:30 +0100309 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100310 const char *test;
311 const char *filename;
312 int line_no;
313}
314test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100315
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400316#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurek1152fa82018-04-13 05:15:17 -0400317mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400318#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100319
Simon Butchera6463452018-12-06 17:41:56 +0000320#if defined(MBEDTLS_CHECK_PARAMS)
321jmp_buf param_fail_jmp;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000322jmp_buf jmp_tmp;
Simon Butchera6463452018-12-06 17:41:56 +0000323#endif
324
SimonB8ca7bc42016-04-17 23:24:50 +0100325/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100326/* Helper flags for complex dependencies */
327
328/* Indicates whether we expect mbedtls_entropy_init
329 * to initialize some strong entropy source. */
330#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
331 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
332 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
333 defined(MBEDTLS_HAVEGE_C) || \
334 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
335 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100336#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100337#endif
338
Arto Kinnunen0a8f87f2020-01-07 13:15:40 +0200339/* Set flag if number of entropy sources is more than 2 */
340#if ( MBEDTLS_ENTROPY_MAX_SOURCES > 2 )
341#define ENTROPY_SOURCE_COUNT_MORE_THAN_TWO
342#endif
Hanno Becker47deec42017-07-24 12:27:09 +0100343
344/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000345/* Helper Functions */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000346
Simon Butcherecff2192018-10-03 16:17:41 +0100347void test_fail( const char *test, int line_no, const char* filename )
Simon Butcher6542f6c2018-12-09 22:09:59 +0000348{
Hanno Becker930fbf62019-07-05 13:31:30 +0100349 test_info.result = TEST_RESULT_FAILED;
350 test_info.test = test;
351 test_info.line_no = line_no;
352 test_info.filename = filename;
353}
354
355void test_skip( const char *test, int line_no, const char* filename )
356{
357 test_info.result = TEST_RESULT_SKIPPED;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000358 test_info.test = test;
359 test_info.line_no = line_no;
360 test_info.filename = filename;
361}
362
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400363static int platform_setup()
364{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400365 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400366#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400367 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400368#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400369 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400370}
371
372static void platform_teardown()
373{
374#if defined(MBEDTLS_PLATFORM_C)
375 mbedtls_platform_teardown( &platform_ctx );
376#endif /* MBEDTLS_PLATFORM_C */
377}
SimonB0269dad2016-02-17 23:34:30 +0000378
Simon Butchera6463452018-12-06 17:41:56 +0000379#if defined(MBEDTLS_CHECK_PARAMS)
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100380void mbedtls_param_failed( const char *failure_condition,
381 const char *file,
382 int line )
Simon Butchera6463452018-12-06 17:41:56 +0000383{
Simon Butcher6542f6c2018-12-09 22:09:59 +0000384 /* If we are testing the callback function... */
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100385 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
Simon Butcher6542f6c2018-12-09 22:09:59 +0000386 {
387 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
388 }
389 else
390 {
391 /* ...else we treat this as an error */
Simon Butchera6463452018-12-06 17:41:56 +0000392
Simon Butcher6542f6c2018-12-09 22:09:59 +0000393 /* Record the location of the failure, but not as a failure yet, in case
394 * it was part of the test */
395 test_fail( failure_condition, line, file );
Hanno Becker930fbf62019-07-05 13:31:30 +0100396 test_info.result = TEST_RESULT_SUCCESS;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000397
398 longjmp( param_fail_jmp, 1 );
399 }
Simon Butchera6463452018-12-06 17:41:56 +0000400}
401#endif
402
Hanno Becker572d4482019-07-23 13:47:53 +0100403#if defined(MBEDTLS_SSL_CONF_RNG)
404int rng_wrap( void *ctx, unsigned char *dst, size_t len )
405{
406 ((void) ctx);
407 ((void) dst);
408 ((void) len);
409 /* We don't expect test suites to use SSL functionality
410 * that would trigger the hardcoded RNG. */
411 return( -1 );
412}
413#endif /* MBEDTLS_SSL_CONF_RNG */
414
Janos Follath8ca53b52016-10-05 10:57:49 +0100415#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
416static int redirect_output( FILE** out_stream, const char* path )
417{
418 int stdout_fd = dup( fileno( *out_stream ) );
419
420 if( stdout_fd == -1 )
421 {
422 return -1;
423 }
424
425 fflush( *out_stream );
426 fclose( *out_stream );
427 *out_stream = fopen( path, "w" );
428
429 if( *out_stream == NULL )
430 {
Andres Amaya Garcia687d6732018-11-26 21:23:28 +0000431 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100432 return -1;
433 }
434
435 return stdout_fd;
436}
437
438static int restore_output( FILE** out_stream, int old_fd )
439{
440 fflush( *out_stream );
441 fclose( *out_stream );
442
443 *out_stream = fdopen( old_fd, "w" );
444 if( *out_stream == NULL )
445 {
446 return -1;
447 }
448
449 return 0;
450}
Simon Butchere0192962016-10-12 23:07:30 +0100451
Janos Follathe709f7c2016-10-13 11:26:29 +0100452static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100453{
Janos Follathe709f7c2016-10-13 11:26:29 +0100454 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100455}
Janos Follath8ca53b52016-10-05 10:57:49 +0100456#endif /* __unix__ || __APPLE__ __MACH__ */
457
Simon Butcherecff2192018-10-03 16:17:41 +0100458int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000459{
460 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000461 int len = strlen( ibuf ) / 2;
Gilles Peskine137d31b2019-06-07 14:52:07 +0200462 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000463
Rich Evans4c091142015-02-02 12:04:10 +0000464 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000465 {
466 c = *ibuf++;
467 if( c >= '0' && c <= '9' )
468 c -= '0';
469 else if( c >= 'a' && c <= 'f' )
470 c -= 'a' - 10;
471 else if( c >= 'A' && c <= 'F' )
472 c -= 'A' - 10;
473 else
Gilles Peskine137d31b2019-06-07 14:52:07 +0200474 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000475
476 c2 = *ibuf++;
477 if( c2 >= '0' && c2 <= '9' )
478 c2 -= '0';
479 else if( c2 >= 'a' && c2 <= 'f' )
480 c2 -= 'a' - 10;
481 else if( c2 >= 'A' && c2 <= 'F' )
482 c2 -= 'A' - 10;
483 else
Gilles Peskine137d31b2019-06-07 14:52:07 +0200484 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000485
486 *obuf++ = ( c << 4 ) | c2;
487 }
488
489 return len;
490}
491
Simon Butcherecff2192018-10-03 16:17:41 +0100492void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000493{
494 unsigned char l, h;
495
Rich Evans42914452015-02-02 12:09:25 +0000496 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000497 {
Rich Evans42914452015-02-02 12:09:25 +0000498 h = *ibuf / 16;
499 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000500
501 if( h < 10 )
502 *obuf++ = '0' + h;
503 else
504 *obuf++ = 'a' + h - 10;
505
506 if( l < 10 )
507 *obuf++ = '0' + l;
508 else
509 *obuf++ = 'a' + l - 10;
510
511 ++ibuf;
512 len--;
513 }
514}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000515
516/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200517 * Allocate and zeroize a buffer.
518 *
519 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
520 *
521 * For convenience, dies if allocation fails.
522 */
523static unsigned char *zero_alloc( size_t len )
524{
525 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000526 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200527
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200528 p = mbedtls_calloc( 1, actual_len );
Gilles Peskine137d31b2019-06-07 14:52:07 +0200529 TEST_HELPER_ASSERT( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200530
531 memset( p, 0x00, actual_len );
532
533 return( p );
534}
535
536/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200537 * Allocate and fill a buffer from hex data.
538 *
539 * The buffer is sized exactly as needed. This allows to detect buffer
540 * overruns (including overreads) when running the test suite under valgrind.
541 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200542 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
543 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200544 * For convenience, dies if allocation fails.
545 */
Simon Butcherecff2192018-10-03 16:17:41 +0100546unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200547{
548 unsigned char *obuf;
549
Rich Evans42914452015-02-02 12:09:25 +0000550 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200551
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200552 if( *olen == 0 )
553 return( zero_alloc( *olen ) );
554
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200555 obuf = mbedtls_calloc( 1, *olen );
Gilles Peskine137d31b2019-06-07 14:52:07 +0200556 TEST_HELPER_ASSERT( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200557
558 (void) unhexify( obuf, ibuf );
559
560 return( obuf );
561}
562
563/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000564 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000565 * Although predictable and often similar on multiple
566 * runs, this does not result in identical random on
567 * each run. So do not use this if the results of a
568 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000569 *
570 * rng_state shall be NULL.
571 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000572static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000573{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200574#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000575 size_t i;
576
Paul Bakker9dcc3222011-03-08 14:16:06 +0000577 if( rng_state != NULL )
578 rng_state = NULL;
579
Paul Bakkera3d195c2011-11-27 21:07:34 +0000580 for( i = 0; i < len; ++i )
581 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200582#else
583 if( rng_state != NULL )
584 rng_state = NULL;
585
586 arc4random_buf( output, len );
587#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000588
589 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000590}
591
Hanno Beckerf2bf1152019-08-20 16:15:58 +0100592#if defined(MBEDTLS_USE_TINYCRYPT)
593static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
594{
595 int ret;
596 ret = rnd_std_rand( NULL, dest, size );
597 if( ret == 0 )
598 return( (int) size );
599
600 return( 0 );
601}
602#endif /* MBEDTLS_USE_TINYCRYPT */
603
Jarno Lamsaf098b262019-10-04 12:51:45 +0300604#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
605int mbedtls_hardware_poll( void *data, unsigned char *output,
606 size_t len, size_t *olen )
607{
608 (void) data;
609 *olen = len;
610 return( rnd_std_rand( NULL, output, len ) );
611}
612#endif
613
Paul Bakker9dcc3222011-03-08 14:16:06 +0000614/**
615 * This function only returns zeros
616 *
617 * rng_state shall be NULL.
618 */
Simon Butcherecff2192018-10-03 16:17:41 +0100619int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000620{
621 if( rng_state != NULL )
622 rng_state = NULL;
623
Paul Bakkera3d195c2011-11-27 21:07:34 +0000624 memset( output, 0, len );
625
Paul Bakker9dcc3222011-03-08 14:16:06 +0000626 return( 0 );
627}
628
629typedef struct
630{
631 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000632 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000633} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000634
635/**
636 * This function returns random based on a buffer it receives.
637 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000638 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100639 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000640 * The number of bytes released from the buffer on each call to
641 * the random function is specified by per_call. (Can be between
642 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000643 *
644 * After the buffer is empty it will return rand();
645 */
Simon Butcherecff2192018-10-03 16:17:41 +0100646int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000647{
Paul Bakker997bbd12011-03-13 15:45:42 +0000648 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000649 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000650
651 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000652 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000653
Paul Bakkera3d195c2011-11-27 21:07:34 +0000654 use_len = len;
655 if( len > info->length )
656 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000657
Paul Bakkera3d195c2011-11-27 21:07:34 +0000658 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000659 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000660 memcpy( output, info->buf, use_len );
661 info->buf += use_len;
662 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000663 }
664
Paul Bakkera3d195c2011-11-27 21:07:34 +0000665 if( len - use_len > 0 )
666 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
667
668 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000669}
Paul Bakker997bbd12011-03-13 15:45:42 +0000670
671/**
672 * Info structure for the pseudo random function
673 *
674 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000675 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000676 * State( v0, v1 ) should be set to zero.
677 */
678typedef struct
679{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000680 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000681 uint32_t v0, v1;
682} rnd_pseudo_info;
683
684/**
685 * This function returns random based on a pseudo random function.
686 * This means the results should be identical on all systems.
687 * Pseudo random is based on the XTEA encryption algorithm to
688 * generate pseudorandom.
689 *
690 * rng_state shall be a pointer to a rnd_pseudo_info structure.
691 */
Simon Butcherecff2192018-10-03 16:17:41 +0100692int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000693{
694 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000695 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100696 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000697
698 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000699 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000700
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000701 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000702
703 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000704 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000705 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000706 sum = 0;
707
Paul Bakkera3d195c2011-11-27 21:07:34 +0000708 for( i = 0; i < 32; i++ )
709 {
Rich Evans42914452015-02-02 12:09:25 +0000710 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
711 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000712 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000713 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
714 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000715 }
716
Paul Bakker5c2364c2012-10-01 14:41:15 +0000717 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100718 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000719 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100720 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000721 }
722
Paul Bakkera3d195c2011-11-27 21:07:34 +0000723 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000724}
SimonB0269dad2016-02-17 23:34:30 +0000725
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100726int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100727{
728 int ret = 0;
729 uint32_t i = 0;
730
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100731 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100732 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100733
734 for( i = 0; i < a_len; i++ )
735 {
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100736 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100737 {
738 ret = -1;
739 break;
740 }
741 }
742 return ret;
743}