blob: 03f2062dd5d045e5ea52ca7cd7df53d266379b5f [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 Peskineb75125c2018-09-27 13:52:16 +0200129/** Allocate memory dynamically and fail the test case if this fails.
130 *
131 * You must set \p pointer to \c NULL before calling this macro and
132 * put `mbedtls_free( pointer )` in the test's cleanup code.
133 *
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100134 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200135 * This is usually what we want in tests since API functions are
136 * supposed to accept null pointers when a buffer size is zero.
137 *
138 * This macro expands to an instruction, not an expression.
139 * It may jump to the \c exit label.
140 *
141 * \param pointer An lvalue where the address of the allocated buffer
142 * will be stored.
143 * This expression may be evaluated multiple times.
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100144 * \param length Number of elements to allocate.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200145 * This expression may be evaluated multiple times.
146 *
147 */
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100148#define ASSERT_ALLOC( pointer, length ) \
149 do \
150 { \
151 TEST_ASSERT( ( pointer ) == NULL ); \
152 if( ( length ) != 0 ) \
153 { \
154 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
155 ( length ) ); \
156 TEST_ASSERT( ( pointer ) != NULL ); \
157 } \
158 } \
Gilles Peskineb75125c2018-09-27 13:52:16 +0200159 while( 0 )
160
Gilles Peskine3c225962018-09-27 13:56:31 +0200161/** Compare two buffers and fail the test case if they differ.
162 *
163 * This macro expands to an instruction, not an expression.
164 * It may jump to the \c exit label.
165 *
166 * \param p1 Pointer to the start of the first buffer.
167 * \param size1 Size of the first buffer in bytes.
168 * This expression may be evaluated multiple times.
169 * \param p2 Pointer to the start of the second buffer.
170 * \param size2 Size of the second buffer in bytes.
171 * This expression may be evaluated multiple times.
172 */
173#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
174 do \
175 { \
176 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
177 if( ( size1 ) != 0 ) \
178 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
179 } \
180 while( 0 )
181
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500182#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
183/**
184 * \brief This macro tests the statement passed to it as a test step or
185 * individual test in a test case. The macro assumes the test will fail
186 * and will generate an error.
187 *
188 * It allows a library function to return a value and tests the return
189 * code on return to confirm the given error code was returned.
190 *
191 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
192 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
193 * expected failure, and the test will pass.
194 *
195 * This macro is intended for negative parameter validation tests,
196 * where the failing function may return an error value or call
197 * MBEDTLS_PARAM_FAILED() to indicate the error.
198 *
199 * \param PARAM_ERROR_VALUE The expected error code.
200 *
201 * \param TEST The test expression to be tested.
202 */
203#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
204 do { \
205 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
206 if( (TEST) != (PARAM_ERR_VALUE) || \
207 test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
208 { \
209 test_fail( #TEST, __LINE__, __FILE__ ); \
210 goto exit; \
211 } \
212 } while( 0 )
213
214/**
215 * \brief This macro tests the statement passed to it as a test step or
216 * individual test in a test case. The macro assumes the test will fail
217 * and will generate an error.
218 *
219 * It assumes the library function under test cannot return a value and
220 * assumes errors can only be indicated byt calls to
221 * MBEDTLS_PARAM_FAILED().
222 *
223 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
224 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
225 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
226 * can be made.
227 *
228 * This macro is intended for negative parameter validation tests,
229 * where the failing function can only return an error by calling
230 * MBEDTLS_PARAM_FAILED() to indicate the error.
231 *
232 * \param TEST The test expression to be tested.
233 */
234#define TEST_INVALID_PARAM( TEST ) \
235 do { \
236 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
237 if( setjmp( param_fail_jmp ) == 0 ) \
238 { \
239 TEST; \
240 test_fail( #TEST, __LINE__, __FILE__ ); \
241 goto exit; \
242 } \
243 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
244 } while( 0 )
245#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
246
247/**
248 * \brief This macro tests the statement passed to it as a test step or
249 * individual test in a test case. The macro assumes the test will not fail.
250 *
251 * It assumes the library function under test cannot return a value and
252 * assumes errors can only be indicated by calls to
253 * MBEDTLS_PARAM_FAILED().
254 *
255 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
256 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
257 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
258 * can be made.
259 *
260 * This macro is intended to test that functions returning void
261 * accept all of the parameter values they're supposed to accept - eg
262 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
263 * that's allowed to be NULL happens to be NULL.
264 *
265 * Note: for functions that return something other that void,
266 * checking that they accept all the parameters they're supposed to
267 * accept is best done by using TEST_ASSERT() and checking the return
268 * value as well.
269 *
270 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
271 * disabled, as it makes sense to check that the functions accept all
272 * legal values even if this option is disabled - only in that case,
273 * the test is more about whether the function segfaults than about
274 * whether it invokes MBEDTLS_PARAM_FAILED().
275 *
276 * \param TEST The test expression to be tested.
277 */
278#define TEST_VALID_PARAM( TEST ) \
279 TEST_ASSERT( ( TEST, 1 ) );
280
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200281#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
Rich Evans4c091142015-02-02 12:04:10 +0000282{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000284 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000286}
287
Gilles Peskinef055ad72018-12-17 23:18:00 +0100288#if defined(__GNUC__)
289/* Test if arg and &(arg)[0] have the same type. This is true if arg is
290 * an array but not if it's a pointer. */
291#define IS_ARRAY_NOT_POINTER( arg ) \
292 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
293 __typeof__( &( arg )[0] ) ) )
294#else
295/* On platforms where we don't know how to implement this check,
296 * omit it. Oh well, a non-portable check is better than nothing. */
297#define IS_ARRAY_NOT_POINTER( arg ) 1
298#endif
299
300/* A compile-time constant with the value 0. If `const_expr` is not a
301 * compile-time constant with a nonzero value, cause a compile-time error. */
302#define STATIC_ASSERT_EXPR( const_expr ) \
303 ( 0 && sizeof( struct { int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
304/* Return the scalar value `value` (possibly promoted). This is a compile-time
305 * constant if `value` is. `condition` must be a compile-time constant.
306 * If `condition` is false, arrange to cause a compile-time error. */
307#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
308 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
309
310#define ARRAY_LENGTH_UNSAFE( array ) \
Gilles Peskine3d2f9492018-12-17 23:17:17 +0100311 ( sizeof( array ) / sizeof( *( array ) ) )
Gilles Peskinef055ad72018-12-17 23:18:00 +0100312/** Return the number of elements of a static or stack array.
313 *
314 * \param array A value of array (not pointer) type.
315 *
316 * \return The number of elements of the array.
317 */
318#define ARRAY_LENGTH( array ) \
319 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
320 ARRAY_LENGTH_UNSAFE( array ) ) )
321
Gilles Peskinec08fc1d2018-12-18 08:47:00 +0100322/** Return the smaller of two values.
323 *
324 * \param x An integer-valued expression without side effects.
325 * \param y An integer-valued expression without side effects.
326 *
327 * \return The smaller of \p x and \p y.
328 */
329#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
330
331/** Return the larger of two values.
332 *
333 * \param x An integer-valued expression without side effects.
334 * \param y An integer-valued expression without side effects.
335 *
336 * \return The larger of \p x and \p y.
337 */
338#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
Gilles Peskinef055ad72018-12-17 23:18:00 +0100339
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000340/*
341 * 32-bit integer manipulation macros (big endian)
342 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000343#ifndef GET_UINT32_BE
344#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000345{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000346 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
347 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
348 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
349 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000350}
351#endif
352
Paul Bakker5c2364c2012-10-01 14:41:15 +0000353#ifndef PUT_UINT32_BE
354#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000355{ \
356 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
357 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
358 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
359 (b)[(i) + 3] = (unsigned char) ( (n) ); \
360}
361#endif
362
SimonB0269dad2016-02-17 23:34:30 +0000363
364/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100365/* Global variables */
366
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100367static struct
368{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500369 paramfail_test_state_t paramfail_test_state;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100370 int failed;
371 const char *test;
372 const char *filename;
373 int line_no;
374}
375test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100376
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400377#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurek1152fa82018-04-13 05:15:17 -0400378mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400379#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100380
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500381#if defined(MBEDTLS_CHECK_PARAMS)
382jmp_buf param_fail_jmp;
383jmp_buf jmp_tmp;
384#endif
385
SimonB8ca7bc42016-04-17 23:24:50 +0100386/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100387/* Helper flags for complex dependencies */
388
389/* Indicates whether we expect mbedtls_entropy_init
390 * to initialize some strong entropy source. */
391#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
392 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
393 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
394 defined(MBEDTLS_HAVEGE_C) || \
395 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
396 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100397#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100398#endif
399
400
401/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000402/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500403
Simon Butcherecff2192018-10-03 16:17:41 +0100404void test_fail( const char *test, int line_no, const char* filename )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500405{
406 test_info.failed = 1;
407 test_info.test = test;
408 test_info.line_no = line_no;
409 test_info.filename = filename;
410}
411
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400412static int platform_setup()
413{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400414 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400415#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400416 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400417#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400418 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400419}
420
421static void platform_teardown()
422{
423#if defined(MBEDTLS_PLATFORM_C)
424 mbedtls_platform_teardown( &platform_ctx );
425#endif /* MBEDTLS_PLATFORM_C */
426}
SimonB0269dad2016-02-17 23:34:30 +0000427
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500428#if defined(MBEDTLS_CHECK_PARAMS)
429void mbedtls_param_failed( const char *failure_condition,
430 const char *file,
431 int line )
432{
433 /* If we are testing the callback function... */
434 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
435 {
436 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
437 }
438 else
439 {
440 /* ...else we treat this as an error */
441
442 /* Record the location of the failure, but not as a failure yet, in case
443 * it was part of the test */
444 test_fail( failure_condition, line, file );
445 test_info.failed = 0;
446
447 longjmp( param_fail_jmp, 1 );
448 }
449}
450#endif
451
Janos Follath8ca53b52016-10-05 10:57:49 +0100452#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
453static int redirect_output( FILE** out_stream, const char* path )
454{
455 int stdout_fd = dup( fileno( *out_stream ) );
456
457 if( stdout_fd == -1 )
458 {
459 return -1;
460 }
461
462 fflush( *out_stream );
463 fclose( *out_stream );
464 *out_stream = fopen( path, "w" );
465
466 if( *out_stream == NULL )
467 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500468 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100469 return -1;
470 }
471
472 return stdout_fd;
473}
474
475static int restore_output( FILE** out_stream, int old_fd )
476{
477 fflush( *out_stream );
478 fclose( *out_stream );
479
480 *out_stream = fdopen( old_fd, "w" );
481 if( *out_stream == NULL )
482 {
483 return -1;
484 }
485
486 return 0;
487}
Simon Butchere0192962016-10-12 23:07:30 +0100488
Janos Follathe709f7c2016-10-13 11:26:29 +0100489static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100490{
Janos Follathe709f7c2016-10-13 11:26:29 +0100491 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100492}
Janos Follath8ca53b52016-10-05 10:57:49 +0100493#endif /* __unix__ || __APPLE__ __MACH__ */
494
Simon Butcherecff2192018-10-03 16:17:41 +0100495int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000496{
497 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000498 int len = strlen( ibuf ) / 2;
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200499 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000500
Rich Evans4c091142015-02-02 12:04:10 +0000501 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000502 {
503 c = *ibuf++;
504 if( c >= '0' && c <= '9' )
505 c -= '0';
506 else if( c >= 'a' && c <= 'f' )
507 c -= 'a' - 10;
508 else if( c >= 'A' && c <= 'F' )
509 c -= 'A' - 10;
510 else
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200511 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000512
513 c2 = *ibuf++;
514 if( c2 >= '0' && c2 <= '9' )
515 c2 -= '0';
516 else if( c2 >= 'a' && c2 <= 'f' )
517 c2 -= 'a' - 10;
518 else if( c2 >= 'A' && c2 <= 'F' )
519 c2 -= 'A' - 10;
520 else
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200521 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000522
523 *obuf++ = ( c << 4 ) | c2;
524 }
525
526 return len;
527}
528
Simon Butcherecff2192018-10-03 16:17:41 +0100529void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000530{
531 unsigned char l, h;
532
Rich Evans42914452015-02-02 12:09:25 +0000533 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000534 {
Rich Evans42914452015-02-02 12:09:25 +0000535 h = *ibuf / 16;
536 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000537
538 if( h < 10 )
539 *obuf++ = '0' + h;
540 else
541 *obuf++ = 'a' + h - 10;
542
543 if( l < 10 )
544 *obuf++ = '0' + l;
545 else
546 *obuf++ = 'a' + l - 10;
547
548 ++ibuf;
549 len--;
550 }
551}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000552
553/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200554 * Allocate and zeroize a buffer.
555 *
556 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
557 *
558 * For convenience, dies if allocation fails.
559 */
560static unsigned char *zero_alloc( size_t len )
561{
562 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000563 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200564
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200565 p = mbedtls_calloc( 1, actual_len );
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200566 TEST_HELPER_ASSERT( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200567
568 memset( p, 0x00, actual_len );
569
570 return( p );
571}
572
573/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200574 * Allocate and fill a buffer from hex data.
575 *
576 * The buffer is sized exactly as needed. This allows to detect buffer
577 * overruns (including overreads) when running the test suite under valgrind.
578 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200579 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
580 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200581 * For convenience, dies if allocation fails.
582 */
Simon Butcherecff2192018-10-03 16:17:41 +0100583unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200584{
585 unsigned char *obuf;
586
Rich Evans42914452015-02-02 12:09:25 +0000587 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200588
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200589 if( *olen == 0 )
590 return( zero_alloc( *olen ) );
591
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200592 obuf = mbedtls_calloc( 1, *olen );
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200593 TEST_HELPER_ASSERT( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200594
595 (void) unhexify( obuf, ibuf );
596
597 return( obuf );
598}
599
600/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000601 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000602 * Although predictable and often similar on multiple
603 * runs, this does not result in identical random on
604 * each run. So do not use this if the results of a
605 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000606 *
607 * rng_state shall be NULL.
608 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000609static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000610{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200611#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000612 size_t i;
613
Paul Bakker9dcc3222011-03-08 14:16:06 +0000614 if( rng_state != NULL )
615 rng_state = NULL;
616
Paul Bakkera3d195c2011-11-27 21:07:34 +0000617 for( i = 0; i < len; ++i )
618 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200619#else
620 if( rng_state != NULL )
621 rng_state = NULL;
622
623 arc4random_buf( output, len );
624#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000625
626 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000627}
628
629/**
630 * This function only returns zeros
631 *
632 * rng_state shall be NULL.
633 */
Simon Butcherecff2192018-10-03 16:17:41 +0100634int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000635{
636 if( rng_state != NULL )
637 rng_state = NULL;
638
Paul Bakkera3d195c2011-11-27 21:07:34 +0000639 memset( output, 0, len );
640
Paul Bakker9dcc3222011-03-08 14:16:06 +0000641 return( 0 );
642}
643
644typedef struct
645{
646 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000647 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000648} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000649
650/**
651 * This function returns random based on a buffer it receives.
652 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000653 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100654 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000655 * The number of bytes released from the buffer on each call to
656 * the random function is specified by per_call. (Can be between
657 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000658 *
659 * After the buffer is empty it will return rand();
660 */
Simon Butcherecff2192018-10-03 16:17:41 +0100661int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000662{
Paul Bakker997bbd12011-03-13 15:45:42 +0000663 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000664 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000665
666 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000667 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000668
Paul Bakkera3d195c2011-11-27 21:07:34 +0000669 use_len = len;
670 if( len > info->length )
671 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000672
Paul Bakkera3d195c2011-11-27 21:07:34 +0000673 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000674 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000675 memcpy( output, info->buf, use_len );
676 info->buf += use_len;
677 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000678 }
679
Paul Bakkera3d195c2011-11-27 21:07:34 +0000680 if( len - use_len > 0 )
681 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
682
683 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000684}
Paul Bakker997bbd12011-03-13 15:45:42 +0000685
686/**
687 * Info structure for the pseudo random function
688 *
689 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000690 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000691 * State( v0, v1 ) should be set to zero.
692 */
693typedef struct
694{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000695 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000696 uint32_t v0, v1;
697} rnd_pseudo_info;
698
699/**
700 * This function returns random based on a pseudo random function.
701 * This means the results should be identical on all systems.
702 * Pseudo random is based on the XTEA encryption algorithm to
703 * generate pseudorandom.
704 *
705 * rng_state shall be a pointer to a rnd_pseudo_info structure.
706 */
Simon Butcherecff2192018-10-03 16:17:41 +0100707int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000708{
709 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000710 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100711 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000712
713 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000714 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000715
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000716 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000717
718 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000719 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000720 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000721 sum = 0;
722
Paul Bakkera3d195c2011-11-27 21:07:34 +0000723 for( i = 0; i < 32; i++ )
724 {
Rich Evans42914452015-02-02 12:09:25 +0000725 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
726 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000727 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000728 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
729 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000730 }
731
Paul Bakker5c2364c2012-10-01 14:41:15 +0000732 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100733 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000734 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100735 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000736 }
737
Paul Bakkera3d195c2011-11-27 21:07:34 +0000738 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000739}
SimonB0269dad2016-02-17 23:34:30 +0000740
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100741int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100742{
743 int ret = 0;
744 uint32_t i = 0;
745
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500746 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100747 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100748
749 for( i = 0; i < a_len; i++ )
750 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500751 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100752 {
753 ret = -1;
754 break;
755 }
756 }
757 return ret;
758}