blob: 4b53da7bacf3978eaffb31dc31fb8a59bb1f967a [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
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000238/*
239 * 32-bit integer manipulation macros (big endian)
240 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000241#ifndef GET_UINT32_BE
242#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000243{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000244 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
245 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
246 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
247 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000248}
249#endif
250
Paul Bakker5c2364c2012-10-01 14:41:15 +0000251#ifndef PUT_UINT32_BE
252#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000253{ \
254 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
255 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
256 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
257 (b)[(i) + 3] = (unsigned char) ( (n) ); \
258}
259#endif
260
SimonB0269dad2016-02-17 23:34:30 +0000261
262/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100263/* Global variables */
264
Hanno Becker930fbf62019-07-05 13:31:30 +0100265typedef enum
266{
267 TEST_RESULT_SUCCESS = 0,
268 TEST_RESULT_FAILED,
269 TEST_RESULT_SKIPPED
270} test_result_t;
271
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100272static struct
273{
Simon Butcher6542f6c2018-12-09 22:09:59 +0000274 paramfail_test_state_t paramfail_test_state;
Hanno Becker930fbf62019-07-05 13:31:30 +0100275 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100276 const char *test;
277 const char *filename;
278 int line_no;
279}
280test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100281
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400282#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurek1152fa82018-04-13 05:15:17 -0400283mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400284#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100285
Simon Butchera6463452018-12-06 17:41:56 +0000286#if defined(MBEDTLS_CHECK_PARAMS)
287jmp_buf param_fail_jmp;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000288jmp_buf jmp_tmp;
Simon Butchera6463452018-12-06 17:41:56 +0000289#endif
290
SimonB8ca7bc42016-04-17 23:24:50 +0100291/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100292/* Helper flags for complex dependencies */
293
294/* Indicates whether we expect mbedtls_entropy_init
295 * to initialize some strong entropy source. */
296#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
297 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
298 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
299 defined(MBEDTLS_HAVEGE_C) || \
300 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
301 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100302#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100303#endif
304
Arto Kinnunen0a8f87f2020-01-07 13:15:40 +0200305/* Set flag if number of entropy sources is more than 2 */
306#if ( MBEDTLS_ENTROPY_MAX_SOURCES > 2 )
307#define ENTROPY_SOURCE_COUNT_MORE_THAN_TWO
308#endif
Hanno Becker47deec42017-07-24 12:27:09 +0100309
310/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000311/* Helper Functions */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000312
Simon Butcherecff2192018-10-03 16:17:41 +0100313void test_fail( const char *test, int line_no, const char* filename )
Simon Butcher6542f6c2018-12-09 22:09:59 +0000314{
Hanno Becker930fbf62019-07-05 13:31:30 +0100315 test_info.result = TEST_RESULT_FAILED;
316 test_info.test = test;
317 test_info.line_no = line_no;
318 test_info.filename = filename;
319}
320
321void test_skip( const char *test, int line_no, const char* filename )
322{
323 test_info.result = TEST_RESULT_SKIPPED;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000324 test_info.test = test;
325 test_info.line_no = line_no;
326 test_info.filename = filename;
327}
328
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400329static int platform_setup()
330{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400331 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400332#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400333 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400334#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400335 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400336}
337
338static void platform_teardown()
339{
340#if defined(MBEDTLS_PLATFORM_C)
341 mbedtls_platform_teardown( &platform_ctx );
342#endif /* MBEDTLS_PLATFORM_C */
343}
SimonB0269dad2016-02-17 23:34:30 +0000344
Simon Butchera6463452018-12-06 17:41:56 +0000345#if defined(MBEDTLS_CHECK_PARAMS)
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100346void mbedtls_param_failed( const char *failure_condition,
347 const char *file,
348 int line )
Simon Butchera6463452018-12-06 17:41:56 +0000349{
Simon Butcher6542f6c2018-12-09 22:09:59 +0000350 /* If we are testing the callback function... */
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100351 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
Simon Butcher6542f6c2018-12-09 22:09:59 +0000352 {
353 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
354 }
355 else
356 {
357 /* ...else we treat this as an error */
Simon Butchera6463452018-12-06 17:41:56 +0000358
Simon Butcher6542f6c2018-12-09 22:09:59 +0000359 /* Record the location of the failure, but not as a failure yet, in case
360 * it was part of the test */
361 test_fail( failure_condition, line, file );
Hanno Becker930fbf62019-07-05 13:31:30 +0100362 test_info.result = TEST_RESULT_SUCCESS;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000363
364 longjmp( param_fail_jmp, 1 );
365 }
Simon Butchera6463452018-12-06 17:41:56 +0000366}
367#endif
368
Hanno Becker572d4482019-07-23 13:47:53 +0100369#if defined(MBEDTLS_SSL_CONF_RNG)
370int rng_wrap( void *ctx, unsigned char *dst, size_t len )
371{
372 ((void) ctx);
373 ((void) dst);
374 ((void) len);
375 /* We don't expect test suites to use SSL functionality
376 * that would trigger the hardcoded RNG. */
377 return( -1 );
378}
379#endif /* MBEDTLS_SSL_CONF_RNG */
380
Janos Follath8ca53b52016-10-05 10:57:49 +0100381#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
382static int redirect_output( FILE** out_stream, const char* path )
383{
384 int stdout_fd = dup( fileno( *out_stream ) );
385
386 if( stdout_fd == -1 )
387 {
388 return -1;
389 }
390
391 fflush( *out_stream );
392 fclose( *out_stream );
393 *out_stream = fopen( path, "w" );
394
395 if( *out_stream == NULL )
396 {
Andres Amaya Garcia687d6732018-11-26 21:23:28 +0000397 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100398 return -1;
399 }
400
401 return stdout_fd;
402}
403
404static int restore_output( FILE** out_stream, int old_fd )
405{
406 fflush( *out_stream );
407 fclose( *out_stream );
408
409 *out_stream = fdopen( old_fd, "w" );
410 if( *out_stream == NULL )
411 {
412 return -1;
413 }
414
415 return 0;
416}
Simon Butchere0192962016-10-12 23:07:30 +0100417
Janos Follathe709f7c2016-10-13 11:26:29 +0100418static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100419{
Janos Follathe709f7c2016-10-13 11:26:29 +0100420 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100421}
Janos Follath8ca53b52016-10-05 10:57:49 +0100422#endif /* __unix__ || __APPLE__ __MACH__ */
423
Simon Butcherecff2192018-10-03 16:17:41 +0100424int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000425{
426 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000427 int len = strlen( ibuf ) / 2;
Gilles Peskine137d31b2019-06-07 14:52:07 +0200428 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000429
Rich Evans4c091142015-02-02 12:04:10 +0000430 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000431 {
432 c = *ibuf++;
433 if( c >= '0' && c <= '9' )
434 c -= '0';
435 else if( c >= 'a' && c <= 'f' )
436 c -= 'a' - 10;
437 else if( c >= 'A' && c <= 'F' )
438 c -= 'A' - 10;
439 else
Gilles Peskine137d31b2019-06-07 14:52:07 +0200440 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000441
442 c2 = *ibuf++;
443 if( c2 >= '0' && c2 <= '9' )
444 c2 -= '0';
445 else if( c2 >= 'a' && c2 <= 'f' )
446 c2 -= 'a' - 10;
447 else if( c2 >= 'A' && c2 <= 'F' )
448 c2 -= 'A' - 10;
449 else
Gilles Peskine137d31b2019-06-07 14:52:07 +0200450 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000451
452 *obuf++ = ( c << 4 ) | c2;
453 }
454
455 return len;
456}
457
Simon Butcherecff2192018-10-03 16:17:41 +0100458void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000459{
460 unsigned char l, h;
461
Rich Evans42914452015-02-02 12:09:25 +0000462 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000463 {
Rich Evans42914452015-02-02 12:09:25 +0000464 h = *ibuf / 16;
465 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000466
467 if( h < 10 )
468 *obuf++ = '0' + h;
469 else
470 *obuf++ = 'a' + h - 10;
471
472 if( l < 10 )
473 *obuf++ = '0' + l;
474 else
475 *obuf++ = 'a' + l - 10;
476
477 ++ibuf;
478 len--;
479 }
480}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000481
482/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200483 * Allocate and zeroize a buffer.
484 *
485 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
486 *
487 * For convenience, dies if allocation fails.
488 */
489static unsigned char *zero_alloc( size_t len )
490{
491 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000492 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200493
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200494 p = mbedtls_calloc( 1, actual_len );
Gilles Peskine137d31b2019-06-07 14:52:07 +0200495 TEST_HELPER_ASSERT( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200496
497 memset( p, 0x00, actual_len );
498
499 return( p );
500}
501
502/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200503 * Allocate and fill a buffer from hex data.
504 *
505 * The buffer is sized exactly as needed. This allows to detect buffer
506 * overruns (including overreads) when running the test suite under valgrind.
507 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200508 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
509 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200510 * For convenience, dies if allocation fails.
511 */
Simon Butcherecff2192018-10-03 16:17:41 +0100512unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200513{
514 unsigned char *obuf;
515
Rich Evans42914452015-02-02 12:09:25 +0000516 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200517
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200518 if( *olen == 0 )
519 return( zero_alloc( *olen ) );
520
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200521 obuf = mbedtls_calloc( 1, *olen );
Gilles Peskine137d31b2019-06-07 14:52:07 +0200522 TEST_HELPER_ASSERT( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200523
524 (void) unhexify( obuf, ibuf );
525
526 return( obuf );
527}
528
529/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000530 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000531 * Although predictable and often similar on multiple
532 * runs, this does not result in identical random on
533 * each run. So do not use this if the results of a
534 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000535 *
536 * rng_state shall be NULL.
537 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000538static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000539{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200540#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000541 size_t i;
542
Paul Bakker9dcc3222011-03-08 14:16:06 +0000543 if( rng_state != NULL )
544 rng_state = NULL;
545
Paul Bakkera3d195c2011-11-27 21:07:34 +0000546 for( i = 0; i < len; ++i )
547 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200548#else
549 if( rng_state != NULL )
550 rng_state = NULL;
551
552 arc4random_buf( output, len );
553#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000554
555 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000556}
557
Hanno Beckerf2bf1152019-08-20 16:15:58 +0100558#if defined(MBEDTLS_USE_TINYCRYPT)
559static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
560{
561 int ret;
562 ret = rnd_std_rand( NULL, dest, size );
563 if( ret == 0 )
564 return( (int) size );
565
566 return( 0 );
567}
568#endif /* MBEDTLS_USE_TINYCRYPT */
569
Jarno Lamsaf098b262019-10-04 12:51:45 +0300570#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
571int mbedtls_hardware_poll( void *data, unsigned char *output,
572 size_t len, size_t *olen )
573{
574 (void) data;
575 *olen = len;
576 return( rnd_std_rand( NULL, output, len ) );
577}
578#endif
579
Paul Bakker9dcc3222011-03-08 14:16:06 +0000580/**
581 * This function only returns zeros
582 *
583 * rng_state shall be NULL.
584 */
Simon Butcherecff2192018-10-03 16:17:41 +0100585int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000586{
587 if( rng_state != NULL )
588 rng_state = NULL;
589
Paul Bakkera3d195c2011-11-27 21:07:34 +0000590 memset( output, 0, len );
591
Paul Bakker9dcc3222011-03-08 14:16:06 +0000592 return( 0 );
593}
594
595typedef struct
596{
597 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000598 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000599} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000600
601/**
602 * This function returns random based on a buffer it receives.
603 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000604 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100605 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000606 * The number of bytes released from the buffer on each call to
607 * the random function is specified by per_call. (Can be between
608 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000609 *
610 * After the buffer is empty it will return rand();
611 */
Simon Butcherecff2192018-10-03 16:17:41 +0100612int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000613{
Paul Bakker997bbd12011-03-13 15:45:42 +0000614 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000615 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000616
617 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000618 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000619
Paul Bakkera3d195c2011-11-27 21:07:34 +0000620 use_len = len;
621 if( len > info->length )
622 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000623
Paul Bakkera3d195c2011-11-27 21:07:34 +0000624 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000625 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000626 memcpy( output, info->buf, use_len );
627 info->buf += use_len;
628 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000629 }
630
Paul Bakkera3d195c2011-11-27 21:07:34 +0000631 if( len - use_len > 0 )
632 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
633
634 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000635}
Paul Bakker997bbd12011-03-13 15:45:42 +0000636
637/**
638 * Info structure for the pseudo random function
639 *
640 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000641 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000642 * State( v0, v1 ) should be set to zero.
643 */
644typedef struct
645{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000646 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000647 uint32_t v0, v1;
648} rnd_pseudo_info;
649
650/**
651 * This function returns random based on a pseudo random function.
652 * This means the results should be identical on all systems.
653 * Pseudo random is based on the XTEA encryption algorithm to
654 * generate pseudorandom.
655 *
656 * rng_state shall be a pointer to a rnd_pseudo_info structure.
657 */
Simon Butcherecff2192018-10-03 16:17:41 +0100658int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000659{
660 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000661 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100662 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000663
664 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000665 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000666
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000667 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000668
669 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000670 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000671 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000672 sum = 0;
673
Paul Bakkera3d195c2011-11-27 21:07:34 +0000674 for( i = 0; i < 32; i++ )
675 {
Rich Evans42914452015-02-02 12:09:25 +0000676 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
677 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000678 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000679 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
680 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000681 }
682
Paul Bakker5c2364c2012-10-01 14:41:15 +0000683 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100684 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000685 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100686 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000687 }
688
Paul Bakkera3d195c2011-11-27 21:07:34 +0000689 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000690}
SimonB0269dad2016-02-17 23:34:30 +0000691
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100692int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100693{
694 int ret = 0;
695 uint32_t i = 0;
696
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100697 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100698 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100699
700 for( i = 0; i < a_len; i++ )
701 {
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100702 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100703 {
704 ret = -1;
705 break;
706 }
707 }
708 return ret;
709}