blob: d3d613f14fb1df9b38275ef9b9cd551738a1ae87 [file] [log] [blame]
Azim Khanec024482017-05-09 17:20:21 +01001#line 2 "suites/helpers.function"
SimonB0269dad2016-02-17 23:34:30 +00002/*----------------------------------------------------------------------------*/
3/* Headers */
4
Simon Butcheredb7fd92016-05-17 13:35:51 +01005#include <stdlib.h>
6
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00008#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +02009#else
Rich Evans00ab4702015-02-06 13:43:58 +000010#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010012#define mbedtls_snprintf snprintf
13#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#define mbedtls_free free
15#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010016#define mbedtls_time time
17#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010018#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
19#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020020#endif
21
SimonB0269dad2016-02-17 23:34:30 +000022#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
23#include "mbedtls/memory_buffer_alloc.h"
24#endif
25
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050026#if defined(MBEDTLS_CHECK_PARAMS)
27#include "mbedtls/platform_util.h"
28#include <setjmp.h>
29#endif
30
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000031#ifdef _MSC_VER
32#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010033typedef UINT8 uint8_t;
34typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000035typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000036#define strncasecmp _strnicmp
37#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000038#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020039#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000040#endif
41
Paul Bakker19343182013-08-16 13:31:10 +020042#include <string.h>
43
Janos Follath8ca53b52016-10-05 10:57:49 +010044#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
45#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000046#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010047#endif
SimonB0269dad2016-02-17 23:34:30 +000048
Azim Khand30ca132017-06-09 04:32:58 +010049/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010050typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010051{
52 uint8_t * x;
53 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010054} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010055
SimonB0269dad2016-02-17 23:34:30 +000056/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010057/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000058
Azim Khan62a5d7d2018-06-29 10:02:54 +010059#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
60#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
61#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010062
Azim Khan62a5d7d2018-06-29 10:02:54 +010063#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
64#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
65#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
66#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
67 Only int, string, binary data
68 and integer expressions are
69 allowed */
70#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
71 build */
SimonB0269dad2016-02-17 23:34:30 +000072
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050073typedef enum
74{
75 PARAMFAIL_TESTSTATE_IDLE = 0, /* No parameter failure call test */
76 PARAMFAIL_TESTSTATE_PENDING, /* Test call to the parameter failure
77 * is pending */
78 PARAMFAIL_TESTSTATE_CALLED /* The test call to the parameter
79 * failure function has been made */
80} paramfail_test_state_t;
81
SimonB0269dad2016-02-17 23:34:30 +000082
83/*----------------------------------------------------------------------------*/
84/* Macros */
85
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086/**
87 * \brief This macro tests the expression passed to it as a test step or
88 * individual test in a test case.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020089 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050090 * It allows a library function to return a value and return an error
91 * code that can be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020092 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
94 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
95 * failure.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020096 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * This macro is not suitable for negative parameter validation tests,
98 * as it assumes the test step will not create an error.
99 *
Jaeden Amero67ea2c52019-02-11 12:05:54 +0000100 * Failing the test means:
101 * - Mark this test case as failed.
102 * - Print a message identifying the failure.
103 * - Jump to the \c exit label.
104 *
105 * This macro expands to an instruction, not an expression.
106 * It may jump to the \c exit label.
107 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 * \param TEST The test expression to be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +0200109 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110#define TEST_ASSERT( TEST ) \
111 do { \
112 if( ! (TEST) ) \
113 { \
114 test_fail( #TEST, __LINE__, __FILE__ ); \
115 goto exit; \
116 } \
SimonB0269dad2016-02-17 23:34:30 +0000117 } while( 0 )
118
Gilles Peskine5f7aeee2018-12-17 23:26:52 +0100119/** Evaluate two expressions and fail the test case if they have different
120 * values.
121 *
122 * \param expr1 An expression to evaluate.
123 * \param expr2 The expected value of \p expr1. This can be any
124 * expression, but it is typically a constant.
125 */
126#define TEST_EQUAL( expr1, expr2 ) \
127 TEST_ASSERT( ( expr1 ) == ( expr2 ) )
128
Gilles Peskine0174be22018-12-17 23:26:01 +0100129/** Evaluate an expression and fail the test case if it returns an error.
130 *
131 * \param expr The expression to evaluate. This is typically a call
132 * to a \c psa_xxx function that returns a value of type
133 * #psa_status_t.
134 */
Gilles Peskine5f7aeee2018-12-17 23:26:52 +0100135#define PSA_ASSERT( expr ) TEST_EQUAL( ( expr ), PSA_SUCCESS )
Gilles Peskine0174be22018-12-17 23:26:01 +0100136
Gilles Peskineb75125c2018-09-27 13:52:16 +0200137/** Allocate memory dynamically and fail the test case if this fails.
138 *
139 * You must set \p pointer to \c NULL before calling this macro and
140 * put `mbedtls_free( pointer )` in the test's cleanup code.
141 *
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100142 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200143 * This is usually what we want in tests since API functions are
144 * supposed to accept null pointers when a buffer size is zero.
145 *
146 * This macro expands to an instruction, not an expression.
147 * It may jump to the \c exit label.
148 *
149 * \param pointer An lvalue where the address of the allocated buffer
150 * will be stored.
151 * This expression may be evaluated multiple times.
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100152 * \param length Number of elements to allocate.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200153 * This expression may be evaluated multiple times.
154 *
155 */
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100156#define ASSERT_ALLOC( pointer, length ) \
157 do \
158 { \
159 TEST_ASSERT( ( pointer ) == NULL ); \
160 if( ( length ) != 0 ) \
161 { \
162 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
163 ( length ) ); \
164 TEST_ASSERT( ( pointer ) != NULL ); \
165 } \
166 } \
Gilles Peskineb75125c2018-09-27 13:52:16 +0200167 while( 0 )
168
Gilles Peskine3c225962018-09-27 13:56:31 +0200169/** Compare two buffers and fail the test case if they differ.
170 *
171 * This macro expands to an instruction, not an expression.
172 * It may jump to the \c exit label.
173 *
174 * \param p1 Pointer to the start of the first buffer.
175 * \param size1 Size of the first buffer in bytes.
176 * This expression may be evaluated multiple times.
177 * \param p2 Pointer to the start of the second buffer.
178 * \param size2 Size of the second buffer in bytes.
179 * This expression may be evaluated multiple times.
180 */
181#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
182 do \
183 { \
184 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
185 if( ( size1 ) != 0 ) \
186 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
187 } \
188 while( 0 )
189
Hanno Beckere69d0152019-07-05 13:31:30 +0100190/**
191 * \brief This macro tests the expression passed to it and skips the
192 * running test if it doesn't evaluate to 'true'.
193 *
194 * \param TEST The test expression to be tested.
195 */
196#define TEST_ASSUME( TEST ) \
197 do { \
198 if( ! (TEST) ) \
199 { \
200 test_skip( #TEST, __LINE__, __FILE__ ); \
201 goto exit; \
202 } \
203 } while( 0 )
204
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500205#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
206/**
207 * \brief This macro tests the statement passed to it as a test step or
208 * individual test in a test case. The macro assumes the test will fail
209 * and will generate an error.
210 *
211 * It allows a library function to return a value and tests the return
212 * code on return to confirm the given error code was returned.
213 *
214 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
215 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
216 * expected failure, and the test will pass.
217 *
218 * This macro is intended for negative parameter validation tests,
219 * where the failing function may return an error value or call
220 * MBEDTLS_PARAM_FAILED() to indicate the error.
221 *
222 * \param PARAM_ERROR_VALUE The expected error code.
223 *
224 * \param TEST The test expression to be tested.
225 */
226#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
227 do { \
228 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
229 if( (TEST) != (PARAM_ERR_VALUE) || \
230 test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
231 { \
232 test_fail( #TEST, __LINE__, __FILE__ ); \
233 goto exit; \
234 } \
235 } while( 0 )
236
237/**
238 * \brief This macro tests the statement passed to it as a test step or
239 * individual test in a test case. The macro assumes the test will fail
240 * and will generate an error.
241 *
242 * It assumes the library function under test cannot return a value and
243 * assumes errors can only be indicated byt calls to
244 * MBEDTLS_PARAM_FAILED().
245 *
246 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
247 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
248 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
249 * can be made.
250 *
251 * This macro is intended for negative parameter validation tests,
252 * where the failing function can only return an error by calling
253 * MBEDTLS_PARAM_FAILED() to indicate the error.
254 *
255 * \param TEST The test expression to be tested.
256 */
257#define TEST_INVALID_PARAM( TEST ) \
258 do { \
259 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
260 if( setjmp( param_fail_jmp ) == 0 ) \
261 { \
262 TEST; \
263 test_fail( #TEST, __LINE__, __FILE__ ); \
264 goto exit; \
265 } \
266 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
267 } while( 0 )
268#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
269
270/**
271 * \brief This macro tests the statement passed to it as a test step or
272 * individual test in a test case. The macro assumes the test will not fail.
273 *
274 * It assumes the library function under test cannot return a value and
275 * assumes errors can only be indicated by calls to
276 * MBEDTLS_PARAM_FAILED().
277 *
278 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
279 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
280 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
281 * can be made.
282 *
283 * This macro is intended to test that functions returning void
284 * accept all of the parameter values they're supposed to accept - eg
285 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
286 * that's allowed to be NULL happens to be NULL.
287 *
288 * Note: for functions that return something other that void,
289 * checking that they accept all the parameters they're supposed to
290 * accept is best done by using TEST_ASSERT() and checking the return
291 * value as well.
292 *
293 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
294 * disabled, as it makes sense to check that the functions accept all
295 * legal values even if this option is disabled - only in that case,
296 * the test is more about whether the function segfaults than about
297 * whether it invokes MBEDTLS_PARAM_FAILED().
298 *
299 * \param TEST The test expression to be tested.
300 */
301#define TEST_VALID_PARAM( TEST ) \
302 TEST_ASSERT( ( TEST, 1 ) );
303
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200304#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
Rich Evans4c091142015-02-02 12:04:10 +0000305{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000307 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000309}
310
Gilles Peskinef055ad72018-12-17 23:18:00 +0100311#if defined(__GNUC__)
312/* Test if arg and &(arg)[0] have the same type. This is true if arg is
313 * an array but not if it's a pointer. */
314#define IS_ARRAY_NOT_POINTER( arg ) \
315 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
316 __typeof__( &( arg )[0] ) ) )
317#else
318/* On platforms where we don't know how to implement this check,
319 * omit it. Oh well, a non-portable check is better than nothing. */
320#define IS_ARRAY_NOT_POINTER( arg ) 1
321#endif
322
323/* A compile-time constant with the value 0. If `const_expr` is not a
324 * compile-time constant with a nonzero value, cause a compile-time error. */
325#define STATIC_ASSERT_EXPR( const_expr ) \
326 ( 0 && sizeof( struct { int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
327/* Return the scalar value `value` (possibly promoted). This is a compile-time
328 * constant if `value` is. `condition` must be a compile-time constant.
329 * If `condition` is false, arrange to cause a compile-time error. */
330#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
331 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
332
333#define ARRAY_LENGTH_UNSAFE( array ) \
Gilles Peskine3d2f9492018-12-17 23:17:17 +0100334 ( sizeof( array ) / sizeof( *( array ) ) )
Gilles Peskinef055ad72018-12-17 23:18:00 +0100335/** Return the number of elements of a static or stack array.
336 *
337 * \param array A value of array (not pointer) type.
338 *
339 * \return The number of elements of the array.
340 */
341#define ARRAY_LENGTH( array ) \
342 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
343 ARRAY_LENGTH_UNSAFE( array ) ) )
344
Gilles Peskinec08fc1d2018-12-18 08:47:00 +0100345/** Return the smaller of two values.
346 *
347 * \param x An integer-valued expression without side effects.
348 * \param y An integer-valued expression without side effects.
349 *
350 * \return The smaller of \p x and \p y.
351 */
352#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
353
354/** Return the larger of two values.
355 *
356 * \param x An integer-valued expression without side effects.
357 * \param y An integer-valued expression without side effects.
358 *
359 * \return The larger of \p x and \p y.
360 */
361#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
Gilles Peskinef055ad72018-12-17 23:18:00 +0100362
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000363/*
364 * 32-bit integer manipulation macros (big endian)
365 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000366#ifndef GET_UINT32_BE
367#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000368{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000369 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
370 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
371 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
372 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000373}
374#endif
375
Paul Bakker5c2364c2012-10-01 14:41:15 +0000376#ifndef PUT_UINT32_BE
377#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000378{ \
379 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
380 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
381 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
382 (b)[(i) + 3] = (unsigned char) ( (n) ); \
383}
384#endif
385
SimonB0269dad2016-02-17 23:34:30 +0000386
387/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100388/* Global variables */
389
Hanno Beckere69d0152019-07-05 13:31:30 +0100390typedef enum
391{
392 TEST_RESULT_SUCCESS = 0,
393 TEST_RESULT_FAILED,
394 TEST_RESULT_SKIPPED
395} test_result_t;
396
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100397static struct
398{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500399 paramfail_test_state_t paramfail_test_state;
Hanno Beckere69d0152019-07-05 13:31:30 +0100400 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100401 const char *test;
402 const char *filename;
403 int line_no;
404}
405test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100406
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400407#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurek1152fa82018-04-13 05:15:17 -0400408mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400409#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100410
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500411#if defined(MBEDTLS_CHECK_PARAMS)
412jmp_buf param_fail_jmp;
413jmp_buf jmp_tmp;
414#endif
415
SimonB8ca7bc42016-04-17 23:24:50 +0100416/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100417/* Helper flags for complex dependencies */
418
419/* Indicates whether we expect mbedtls_entropy_init
420 * to initialize some strong entropy source. */
421#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
422 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
423 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
424 defined(MBEDTLS_HAVEGE_C) || \
425 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
426 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100427#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100428#endif
429
430
431/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000432/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500433
Simon Butcherecff2192018-10-03 16:17:41 +0100434void test_fail( const char *test, int line_no, const char* filename )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500435{
Hanno Beckere69d0152019-07-05 13:31:30 +0100436 test_info.result = TEST_RESULT_FAILED;
437 test_info.test = test;
438 test_info.line_no = line_no;
439 test_info.filename = filename;
440}
441
442void test_skip( const char *test, int line_no, const char* filename )
443{
444 test_info.result = TEST_RESULT_SKIPPED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500445 test_info.test = test;
446 test_info.line_no = line_no;
447 test_info.filename = filename;
448}
449
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400450static int platform_setup()
451{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400452 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400453#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400454 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400455#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400456 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400457}
458
459static void platform_teardown()
460{
461#if defined(MBEDTLS_PLATFORM_C)
462 mbedtls_platform_teardown( &platform_ctx );
463#endif /* MBEDTLS_PLATFORM_C */
464}
SimonB0269dad2016-02-17 23:34:30 +0000465
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500466#if defined(MBEDTLS_CHECK_PARAMS)
467void mbedtls_param_failed( const char *failure_condition,
468 const char *file,
469 int line )
470{
471 /* If we are testing the callback function... */
472 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
473 {
474 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
475 }
476 else
477 {
478 /* ...else we treat this as an error */
479
480 /* Record the location of the failure, but not as a failure yet, in case
481 * it was part of the test */
482 test_fail( failure_condition, line, file );
Hanno Beckere69d0152019-07-05 13:31:30 +0100483 test_info.result = TEST_RESULT_SUCCESS;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500484
485 longjmp( param_fail_jmp, 1 );
486 }
487}
488#endif
489
Janos Follath8ca53b52016-10-05 10:57:49 +0100490#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
491static int redirect_output( FILE** out_stream, const char* path )
492{
493 int stdout_fd = dup( fileno( *out_stream ) );
494
495 if( stdout_fd == -1 )
496 {
497 return -1;
498 }
499
500 fflush( *out_stream );
501 fclose( *out_stream );
502 *out_stream = fopen( path, "w" );
503
504 if( *out_stream == NULL )
505 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500506 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100507 return -1;
508 }
509
510 return stdout_fd;
511}
512
513static int restore_output( FILE** out_stream, int old_fd )
514{
515 fflush( *out_stream );
516 fclose( *out_stream );
517
518 *out_stream = fdopen( old_fd, "w" );
519 if( *out_stream == NULL )
520 {
521 return -1;
522 }
523
524 return 0;
525}
Simon Butchere0192962016-10-12 23:07:30 +0100526
Janos Follathe709f7c2016-10-13 11:26:29 +0100527static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100528{
Janos Follathe709f7c2016-10-13 11:26:29 +0100529 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100530}
Janos Follath8ca53b52016-10-05 10:57:49 +0100531#endif /* __unix__ || __APPLE__ __MACH__ */
532
Simon Butcherecff2192018-10-03 16:17:41 +0100533int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000534{
535 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000536 int len = strlen( ibuf ) / 2;
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200537 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000538
Rich Evans4c091142015-02-02 12:04:10 +0000539 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000540 {
541 c = *ibuf++;
542 if( c >= '0' && c <= '9' )
543 c -= '0';
544 else if( c >= 'a' && c <= 'f' )
545 c -= 'a' - 10;
546 else if( c >= 'A' && c <= 'F' )
547 c -= 'A' - 10;
548 else
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200549 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000550
551 c2 = *ibuf++;
552 if( c2 >= '0' && c2 <= '9' )
553 c2 -= '0';
554 else if( c2 >= 'a' && c2 <= 'f' )
555 c2 -= 'a' - 10;
556 else if( c2 >= 'A' && c2 <= 'F' )
557 c2 -= 'A' - 10;
558 else
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200559 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000560
561 *obuf++ = ( c << 4 ) | c2;
562 }
563
564 return len;
565}
566
Simon Butcherecff2192018-10-03 16:17:41 +0100567void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000568{
569 unsigned char l, h;
570
Rich Evans42914452015-02-02 12:09:25 +0000571 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000572 {
Rich Evans42914452015-02-02 12:09:25 +0000573 h = *ibuf / 16;
574 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000575
576 if( h < 10 )
577 *obuf++ = '0' + h;
578 else
579 *obuf++ = 'a' + h - 10;
580
581 if( l < 10 )
582 *obuf++ = '0' + l;
583 else
584 *obuf++ = 'a' + l - 10;
585
586 ++ibuf;
587 len--;
588 }
589}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000590
591/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200592 * Allocate and zeroize a buffer.
593 *
594 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
595 *
596 * For convenience, dies if allocation fails.
597 */
598static unsigned char *zero_alloc( size_t len )
599{
600 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000601 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200602
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200603 p = mbedtls_calloc( 1, actual_len );
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200604 TEST_HELPER_ASSERT( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200605
606 memset( p, 0x00, actual_len );
607
608 return( p );
609}
610
611/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200612 * Allocate and fill a buffer from hex data.
613 *
614 * The buffer is sized exactly as needed. This allows to detect buffer
615 * overruns (including overreads) when running the test suite under valgrind.
616 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200617 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
618 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200619 * For convenience, dies if allocation fails.
620 */
Simon Butcherecff2192018-10-03 16:17:41 +0100621unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200622{
623 unsigned char *obuf;
624
Rich Evans42914452015-02-02 12:09:25 +0000625 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200626
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200627 if( *olen == 0 )
628 return( zero_alloc( *olen ) );
629
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200630 obuf = mbedtls_calloc( 1, *olen );
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200631 TEST_HELPER_ASSERT( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200632
633 (void) unhexify( obuf, ibuf );
634
635 return( obuf );
636}
637
638/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000639 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000640 * Although predictable and often similar on multiple
641 * runs, this does not result in identical random on
642 * each run. So do not use this if the results of a
643 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000644 *
645 * rng_state shall be NULL.
646 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000647static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000648{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200649#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000650 size_t i;
651
Paul Bakker9dcc3222011-03-08 14:16:06 +0000652 if( rng_state != NULL )
653 rng_state = NULL;
654
Paul Bakkera3d195c2011-11-27 21:07:34 +0000655 for( i = 0; i < len; ++i )
656 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200657#else
658 if( rng_state != NULL )
659 rng_state = NULL;
660
661 arc4random_buf( output, len );
662#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000663
664 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000665}
666
667/**
668 * This function only returns zeros
669 *
670 * rng_state shall be NULL.
671 */
Simon Butcherecff2192018-10-03 16:17:41 +0100672int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000673{
674 if( rng_state != NULL )
675 rng_state = NULL;
676
Paul Bakkera3d195c2011-11-27 21:07:34 +0000677 memset( output, 0, len );
678
Paul Bakker9dcc3222011-03-08 14:16:06 +0000679 return( 0 );
680}
681
682typedef struct
683{
684 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000685 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000686} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000687
688/**
689 * This function returns random based on a buffer it receives.
690 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000691 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100692 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000693 * The number of bytes released from the buffer on each call to
694 * the random function is specified by per_call. (Can be between
695 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000696 *
697 * After the buffer is empty it will return rand();
698 */
Simon Butcherecff2192018-10-03 16:17:41 +0100699int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000700{
Paul Bakker997bbd12011-03-13 15:45:42 +0000701 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000702 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000703
704 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000705 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000706
Paul Bakkera3d195c2011-11-27 21:07:34 +0000707 use_len = len;
708 if( len > info->length )
709 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000710
Paul Bakkera3d195c2011-11-27 21:07:34 +0000711 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000712 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000713 memcpy( output, info->buf, use_len );
714 info->buf += use_len;
715 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000716 }
717
Paul Bakkera3d195c2011-11-27 21:07:34 +0000718 if( len - use_len > 0 )
719 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
720
721 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000722}
Paul Bakker997bbd12011-03-13 15:45:42 +0000723
724/**
725 * Info structure for the pseudo random function
726 *
727 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000728 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000729 * State( v0, v1 ) should be set to zero.
730 */
731typedef struct
732{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000733 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000734 uint32_t v0, v1;
735} rnd_pseudo_info;
736
737/**
738 * This function returns random based on a pseudo random function.
739 * This means the results should be identical on all systems.
740 * Pseudo random is based on the XTEA encryption algorithm to
741 * generate pseudorandom.
742 *
743 * rng_state shall be a pointer to a rnd_pseudo_info structure.
744 */
Simon Butcherecff2192018-10-03 16:17:41 +0100745int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000746{
747 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000748 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100749 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000750
751 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000752 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000753
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000754 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000755
756 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000757 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000758 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000759 sum = 0;
760
Paul Bakkera3d195c2011-11-27 21:07:34 +0000761 for( i = 0; i < 32; i++ )
762 {
Rich Evans42914452015-02-02 12:09:25 +0000763 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
764 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000765 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000766 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
767 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000768 }
769
Paul Bakker5c2364c2012-10-01 14:41:15 +0000770 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100771 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000772 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100773 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000774 }
775
Paul Bakkera3d195c2011-11-27 21:07:34 +0000776 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000777}
SimonB0269dad2016-02-17 23:34:30 +0000778
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100779int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100780{
781 int ret = 0;
782 uint32_t i = 0;
783
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500784 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100785 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100786
787 for( i = 0; i < a_len; i++ )
788 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500789 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100790 {
791 ret = -1;
792 break;
793 }
794 }
795 return ret;
796}