blob: 9403d9905490b31b328a15a1a52ee9105f7ac051 [file] [log] [blame]
Azim Khanec024482017-05-09 17:20:21 +01001#line 2 "suites/helpers.function"
SimonB0269dad2016-02-17 23:34:30 +00002/*----------------------------------------------------------------------------*/
3/* Headers */
4
Simon Butcheredb7fd92016-05-17 13:35:51 +01005#include <stdlib.h>
6
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00008#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +02009#else
Rich Evans00ab4702015-02-06 13:43:58 +000010#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010012#define mbedtls_snprintf snprintf
13#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#define mbedtls_free free
15#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010016#define mbedtls_time time
17#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010018#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
19#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020020#endif
21
SimonB0269dad2016-02-17 23:34:30 +000022#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
23#include "mbedtls/memory_buffer_alloc.h"
24#endif
25
Simon Butchera6463452018-12-06 17:41:56 +000026#if defined(MBEDTLS_CHECK_PARAMS)
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +010027#include "mbedtls/platform_util.h"
Simon Butchera6463452018-12-06 17:41:56 +000028#include <setjmp.h>
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +010029#endif
Simon Butcher747f5fe2018-12-07 16:53:57 +000030
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000031#ifdef _MSC_VER
32#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010033typedef UINT8 uint8_t;
34typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000035typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000036#define strncasecmp _strnicmp
37#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000038#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020039#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000040#endif
41
Paul Bakker19343182013-08-16 13:31:10 +020042#include <string.h>
43
Janos Follath8ca53b52016-10-05 10:57:49 +010044#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
45#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000046#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010047#endif
SimonB0269dad2016-02-17 23:34:30 +000048
Manuel Pégourié-Gonnardf0828472020-08-25 11:26:37 +020049/*
50 * Define the two macros
51 *
52 * #define TEST_CF_SECRET(ptr, size)
53 * #define TEST_CF_PUBLIC(ptr, size)
54 *
55 * that can be used in tests to mark a memory area as secret (no branch or
56 * memory access should depend on it) or public (default, only needs to be
57 * marked explicitly when it was derived from secret data).
58 *
59 * Arguments:
60 * - ptr: a pointer to the memory area to be marked
61 * - size: the size in bytes of the memory area
62 *
63 * Implementation:
64 * The basic idea is that of ctgrind <https://github.com/agl/ctgrind>: we can
65 * re-use tools that were designed for checking use of uninitialized memory.
66 * This file contains two implementations: one based on MemorySanitizer, the
67 * other on valgrind's memcheck. If none of them is enabled, dummy macros that
68 * do nothing are defined for convenience.
69 */
Manuel Pégourié-Gonnarda2377222020-07-28 10:53:06 +020070#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
71#include <sanitizer/msan_interface.h>
72
73/* Use macros to avoid messing up with origin tracking */
74#define TEST_CF_SECRET __msan_allocated_memory
75// void __msan_allocated_memory(const volatile void* data, size_t size);
76#define TEST_CF_PUBLIC __msan_unpoison
77// void __msan_unpoison(const volatile void *a, size_t size);
78
Manuel Pégourié-Gonnardf0828472020-08-25 11:26:37 +020079#elif defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND)
80#include <valgrind/memcheck.h>
81
82#define TEST_CF_SECRET VALGRIND_MAKE_MEM_UNDEFINED
83// VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len)
84#define TEST_CF_PUBLIC VALGRIND_MAKE_MEM_DEFINED
85// VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
86
87#else /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN ||
88 MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */
Manuel Pégourié-Gonnarda2377222020-07-28 10:53:06 +020089
90#define TEST_CF_SECRET(ptr, size)
91#define TEST_CF_PUBLIC(ptr, size)
92
93#endif /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN */
94
Azim Khand30ca132017-06-09 04:32:58 +010095/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010096typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010097{
98 uint8_t * x;
99 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +0100100} data_t;
Azim Khand30ca132017-06-09 04:32:58 +0100101
SimonB0269dad2016-02-17 23:34:30 +0000102/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +0100103/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +0000104
Azim Khan62a5d7d2018-06-29 10:02:54 +0100105#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
106#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
107#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +0100108
Azim Khan62a5d7d2018-06-29 10:02:54 +0100109#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
110#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
111#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
112#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
113 Only int, string, binary data
114 and integer expressions are
115 allowed */
116#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
117 build */
SimonB0269dad2016-02-17 23:34:30 +0000118
Simon Butcher6542f6c2018-12-09 22:09:59 +0000119typedef enum
120{
121 PARAMFAIL_TESTSTATE_IDLE = 0, /* No parameter failure call test */
122 PARAMFAIL_TESTSTATE_PENDING, /* Test call to the parameter failure
123 * is pending */
124 PARAMFAIL_TESTSTATE_CALLED /* The test call to the parameter
125 * failure function has been made */
126} paramfail_test_state_t;
127
SimonB0269dad2016-02-17 23:34:30 +0000128
129/*----------------------------------------------------------------------------*/
130/* Macros */
131
Simon Butchera6463452018-12-06 17:41:56 +0000132/**
133 * \brief This macro tests the expression passed to it as a test step or
134 * individual test in a test case.
135 *
136 * It allows a library function to return a value and return an error
137 * code that can be tested.
138 *
139 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100140 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
141 * failure.
Simon Butchera6463452018-12-06 17:41:56 +0000142 *
143 * This macro is not suitable for negative parameter validation tests,
144 * as it assumes the test step will not create an error.
145 *
146 * \param TEST The test expression to be tested.
147 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000148#define TEST_ASSERT( TEST ) \
149 do { \
150 if( ! (TEST) ) \
151 { \
152 test_fail( #TEST, __LINE__, __FILE__ ); \
153 goto exit; \
154 } \
SimonB0269dad2016-02-17 23:34:30 +0000155 } while( 0 )
156
Ronald Cron8e8898d2020-07-30 14:18:02 +0200157/** Compare two buffers and fail the test case if they differ.
158 *
159 * This macro expands to an instruction, not an expression.
160 * It may jump to the \c exit label.
161 *
162 * \param p1 Pointer to the start of the first buffer.
163 * \param size1 Size of the first buffer in bytes.
164 * This expression may be evaluated multiple times.
165 * \param p2 Pointer to the start of the second buffer.
166 * \param size2 Size of the second buffer in bytes.
167 * This expression may be evaluated multiple times.
168 */
169#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
170 do \
171 { \
172 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
173 if( ( size1 ) != 0 ) \
174 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
175 } \
176 while( 0 )
177
Hanno Beckerd3369f62019-07-05 13:31:30 +0100178/**
179 * \brief This macro tests the expression passed to it and skips the
180 * running test if it doesn't evaluate to 'true'.
181 *
182 * \param TEST The test expression to be tested.
183 */
184#define TEST_ASSUME( TEST ) \
185 do { \
186 if( ! (TEST) ) \
187 { \
188 test_skip( #TEST, __LINE__, __FILE__ ); \
189 goto exit; \
190 } \
191 } while( 0 )
192
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100193#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
Simon Butchera6463452018-12-06 17:41:56 +0000194/**
195 * \brief This macro tests the statement passed to it as a test step or
196 * individual test in a test case. The macro assumes the test will fail
197 * and will generate an error.
198 *
199 * It allows a library function to return a value and tests the return
200 * code on return to confirm the given error code was returned.
201 *
202 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100203 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
Simon Butchera6463452018-12-06 17:41:56 +0000204 * expected failure, and the test will pass.
205 *
206 * This macro is intended for negative parameter validation tests,
207 * where the failing function may return an error value or call
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100208 * MBEDTLS_PARAM_FAILED() to indicate the error.
Simon Butchera6463452018-12-06 17:41:56 +0000209 *
210 * \param PARAM_ERROR_VALUE The expected error code.
211 *
212 * \param TEST The test expression to be tested.
213 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000214#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
215 do { \
216 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
Manuel Pégourié-Gonnardacfdc622018-12-11 10:36:21 +0100217 if( (TEST) != (PARAM_ERR_VALUE) || \
Simon Butcher6542f6c2018-12-09 22:09:59 +0000218 test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
219 { \
220 test_fail( #TEST, __LINE__, __FILE__ ); \
221 goto exit; \
222 } \
223 } while( 0 )
Simon Butchera6463452018-12-06 17:41:56 +0000224
225/**
226 * \brief This macro tests the statement passed to it as a test step or
227 * individual test in a test case. The macro assumes the test will fail
228 * and will generate an error.
229 *
230 * It assumes the library function under test cannot return a value and
231 * assumes errors can only be indicated byt calls to
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100232 * MBEDTLS_PARAM_FAILED().
Simon Butchera6463452018-12-06 17:41:56 +0000233 *
234 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100235 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
Simon Butchera6463452018-12-06 17:41:56 +0000236 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
237 * can be made.
238 *
239 * This macro is intended for negative parameter validation tests,
240 * where the failing function can only return an error by calling
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100241 * MBEDTLS_PARAM_FAILED() to indicate the error.
Simon Butchera6463452018-12-06 17:41:56 +0000242 *
243 * \param TEST The test expression to be tested.
244 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000245#define TEST_INVALID_PARAM( TEST ) \
246 do { \
247 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100248 if( setjmp( param_fail_jmp ) == 0 ) \
Simon Butcher6542f6c2018-12-09 22:09:59 +0000249 { \
250 TEST; \
251 test_fail( #TEST, __LINE__, __FILE__ ); \
252 goto exit; \
253 } \
254 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
Simon Butchera6463452018-12-06 17:41:56 +0000255 } while( 0 )
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100256#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
Simon Butchera6463452018-12-06 17:41:56 +0000257
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100258/**
259 * \brief This macro tests the statement passed to it as a test step or
260 * individual test in a test case. The macro assumes the test will not fail.
261 *
262 * It assumes the library function under test cannot return a value and
263 * assumes errors can only be indicated by calls to
264 * MBEDTLS_PARAM_FAILED().
265 *
266 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
267 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
268 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
269 * can be made.
270 *
Manuel Pégourié-Gonnardcd2b29c2018-12-12 10:23:57 +0100271 * This macro is intended to test that functions returning void
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100272 * accept all of the parameter values they're supposed to accept - eg
273 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
Manuel Pégourié-Gonnardcd2b29c2018-12-12 10:23:57 +0100274 * that's allowed to be NULL happens to be NULL.
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100275 *
276 * Note: for functions that return something other that void,
277 * checking that they accept all the parameters they're supposed to
278 * accept is best done by using TEST_ASSERT() and checking the return
279 * value as well.
280 *
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100281 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
282 * disabled, as it makes sense to check that the functions accept all
283 * legal values even if this option is disabled - only in that case,
Manuel Pégourié-Gonnarde7306d32018-12-13 09:45:49 +0100284 * the test is more about whether the function segfaults than about
Manuel Pégourié-Gonnard54e7f312018-12-12 11:56:09 +0100285 * whether it invokes MBEDTLS_PARAM_FAILED().
286 *
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100287 * \param TEST The test expression to be tested.
288 */
289#define TEST_VALID_PARAM( TEST ) \
290 TEST_ASSERT( ( TEST, 1 ) );
Simon Butchera6463452018-12-06 17:41:56 +0000291
Gilles Peskine137d31b2019-06-07 14:52:07 +0200292#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
Rich Evans4c091142015-02-02 12:04:10 +0000293{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000295 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000297}
298
Ronald Cron07c83f22020-04-06 09:50:58 +0200299#if defined(__GNUC__)
300/* Test if arg and &(arg)[0] have the same type. This is true if arg is
301 * an array but not if it's a pointer. */
302#define IS_ARRAY_NOT_POINTER( arg ) \
303 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
304 __typeof__( &( arg )[0] ) ) )
305#else
306/* On platforms where we don't know how to implement this check,
307 * omit it. Oh well, a non-portable check is better than nothing. */
308#define IS_ARRAY_NOT_POINTER( arg ) 1
309#endif
310
311/* A compile-time constant with the value 0. If `const_expr` is not a
312 * compile-time constant with a nonzero value, cause a compile-time error. */
313#define STATIC_ASSERT_EXPR( const_expr ) \
makise-homura50f6a192020-08-23 00:39:15 +0300314 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
Ronald Cron07c83f22020-04-06 09:50:58 +0200315/* Return the scalar value `value` (possibly promoted). This is a compile-time
316 * constant if `value` is. `condition` must be a compile-time constant.
317 * If `condition` is false, arrange to cause a compile-time error. */
318#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
319 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
320
321#define ARRAY_LENGTH_UNSAFE( array ) \
322 ( sizeof( array ) / sizeof( *( array ) ) )
323/** Return the number of elements of a static or stack array.
324 *
325 * \param array A value of array (not pointer) type.
326 *
327 * \return The number of elements of the array.
328 */
329#define ARRAY_LENGTH( array ) \
330 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
331 ARRAY_LENGTH_UNSAFE( array ) ) )
332
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000333/*
334 * 32-bit integer manipulation macros (big endian)
335 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000336#ifndef GET_UINT32_BE
337#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000338{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000339 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
340 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
341 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
342 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000343}
344#endif
345
Paul Bakker5c2364c2012-10-01 14:41:15 +0000346#ifndef PUT_UINT32_BE
347#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000348{ \
349 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
350 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
351 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
352 (b)[(i) + 3] = (unsigned char) ( (n) ); \
353}
354#endif
355
SimonB0269dad2016-02-17 23:34:30 +0000356
357/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100358/* Global variables */
359
Hanno Beckerd3369f62019-07-05 13:31:30 +0100360typedef enum
361{
362 TEST_RESULT_SUCCESS = 0,
363 TEST_RESULT_FAILED,
364 TEST_RESULT_SKIPPED
365} test_result_t;
366
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100367static struct
368{
Simon Butcher6542f6c2018-12-09 22:09:59 +0000369 paramfail_test_state_t paramfail_test_state;
Hanno Beckerd3369f62019-07-05 13:31:30 +0100370 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100371 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
Simon Butchera6463452018-12-06 17:41:56 +0000381#if defined(MBEDTLS_CHECK_PARAMS)
382jmp_buf param_fail_jmp;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000383jmp_buf jmp_tmp;
Simon Butchera6463452018-12-06 17:41:56 +0000384#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 */
Simon Butcher6542f6c2018-12-09 22:09:59 +0000403
Simon Butcherecff2192018-10-03 16:17:41 +0100404void test_fail( const char *test, int line_no, const char* filename )
Simon Butcher6542f6c2018-12-09 22:09:59 +0000405{
Gilles Peskinedfb5cff2020-08-29 15:18:23 +0200406 if( test_info.result == TEST_RESULT_FAILED )
407 {
408 /* We've already recorded the test as having failed. Don't
409 * overwrite any previous information about the failure. */
410 return;
411 }
Hanno Beckerd3369f62019-07-05 13:31:30 +0100412 test_info.result = TEST_RESULT_FAILED;
413 test_info.test = test;
414 test_info.line_no = line_no;
415 test_info.filename = filename;
416}
417
418void test_skip( const char *test, int line_no, const char* filename )
419{
420 test_info.result = TEST_RESULT_SKIPPED;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000421 test_info.test = test;
422 test_info.line_no = line_no;
423 test_info.filename = filename;
424}
425
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400426static int platform_setup()
427{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400428 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400429#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400430 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400431#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400432 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400433}
434
435static void platform_teardown()
436{
437#if defined(MBEDTLS_PLATFORM_C)
438 mbedtls_platform_teardown( &platform_ctx );
439#endif /* MBEDTLS_PLATFORM_C */
440}
SimonB0269dad2016-02-17 23:34:30 +0000441
Simon Butchera6463452018-12-06 17:41:56 +0000442#if defined(MBEDTLS_CHECK_PARAMS)
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100443void mbedtls_param_failed( const char *failure_condition,
444 const char *file,
445 int line )
Simon Butchera6463452018-12-06 17:41:56 +0000446{
Simon Butcher6542f6c2018-12-09 22:09:59 +0000447 /* If we are testing the callback function... */
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100448 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
Simon Butcher6542f6c2018-12-09 22:09:59 +0000449 {
450 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
451 }
452 else
453 {
454 /* ...else we treat this as an error */
Simon Butchera6463452018-12-06 17:41:56 +0000455
Simon Butcher6542f6c2018-12-09 22:09:59 +0000456 /* Record the location of the failure, but not as a failure yet, in case
457 * it was part of the test */
458 test_fail( failure_condition, line, file );
Hanno Beckerd3369f62019-07-05 13:31:30 +0100459 test_info.result = TEST_RESULT_SUCCESS;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000460
461 longjmp( param_fail_jmp, 1 );
462 }
Simon Butchera6463452018-12-06 17:41:56 +0000463}
464#endif
465
Janos Follath8ca53b52016-10-05 10:57:49 +0100466#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44650ce762020-07-30 09:02:27 +0200467static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100468{
gufe44650ce762020-07-30 09:02:27 +0200469 int out_fd, dup_fd;
470 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100471
gufe44650ce762020-07-30 09:02:27 +0200472 out_fd = fileno( out_stream );
473 dup_fd = dup( out_fd );
474
475 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100476 {
gufe44650ce762020-07-30 09:02:27 +0200477 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100478 }
479
gufe44650ce762020-07-30 09:02:27 +0200480 path_stream = fopen( path, "w" );
481 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100482 {
gufe44650ce762020-07-30 09:02:27 +0200483 close( dup_fd );
484 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100485 }
486
gufe44650ce762020-07-30 09:02:27 +0200487 fflush( out_stream );
488 if( dup2( fileno( path_stream ), out_fd ) == -1 )
489 {
490 close( dup_fd );
491 fclose( path_stream );
492 return( -1 );
493 }
494
495 fclose( path_stream );
496 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100497}
498
gufe44650ce762020-07-30 09:02:27 +0200499static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100500{
gufe44650ce762020-07-30 09:02:27 +0200501 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100502
gufe44650ce762020-07-30 09:02:27 +0200503 fflush( out_stream );
504 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100505 {
gufe44650ce762020-07-30 09:02:27 +0200506 close( out_fd );
507 close( dup_fd );
508 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100509 }
510
gufe44650ce762020-07-30 09:02:27 +0200511 close( dup_fd );
512 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100513}
Janos Follath8ca53b52016-10-05 10:57:49 +0100514#endif /* __unix__ || __APPLE__ __MACH__ */
515
Ronald Crona0c9ff32020-06-08 17:05:57 +0200516int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000517{
518 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000519 int len = strlen( ibuf ) / 2;
Gilles Peskine137d31b2019-06-07 14:52:07 +0200520 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000521
Rich Evans4c091142015-02-02 12:04:10 +0000522 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000523 {
524 c = *ibuf++;
525 if( c >= '0' && c <= '9' )
526 c -= '0';
527 else if( c >= 'a' && c <= 'f' )
528 c -= 'a' - 10;
529 else if( c >= 'A' && c <= 'F' )
530 c -= 'A' - 10;
531 else
Gilles Peskine137d31b2019-06-07 14:52:07 +0200532 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000533
534 c2 = *ibuf++;
535 if( c2 >= '0' && c2 <= '9' )
536 c2 -= '0';
537 else if( c2 >= 'a' && c2 <= 'f' )
538 c2 -= 'a' - 10;
539 else if( c2 >= 'A' && c2 <= 'F' )
540 c2 -= 'A' - 10;
541 else
Gilles Peskine137d31b2019-06-07 14:52:07 +0200542 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000543
544 *obuf++ = ( c << 4 ) | c2;
545 }
546
547 return len;
548}
549
Ronald Crona0c9ff32020-06-08 17:05:57 +0200550void mbedtls_test_hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000551{
552 unsigned char l, h;
553
Rich Evans42914452015-02-02 12:09:25 +0000554 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000555 {
Rich Evans42914452015-02-02 12:09:25 +0000556 h = *ibuf / 16;
557 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000558
559 if( h < 10 )
560 *obuf++ = '0' + h;
561 else
562 *obuf++ = 'a' + h - 10;
563
564 if( l < 10 )
565 *obuf++ = '0' + l;
566 else
567 *obuf++ = 'a' + l - 10;
568
569 ++ibuf;
570 len--;
571 }
572}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000573
574/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200575 * Allocate and zeroize a buffer.
576 *
577 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
578 *
579 * For convenience, dies if allocation fails.
580 */
581static unsigned char *zero_alloc( size_t len )
582{
583 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000584 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200585
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200586 p = mbedtls_calloc( 1, actual_len );
Gilles Peskine137d31b2019-06-07 14:52:07 +0200587 TEST_HELPER_ASSERT( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200588
589 memset( p, 0x00, actual_len );
590
591 return( p );
592}
593
594/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200595 * Allocate and fill a buffer from hex data.
596 *
597 * The buffer is sized exactly as needed. This allows to detect buffer
598 * overruns (including overreads) when running the test suite under valgrind.
599 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200600 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
601 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200602 * For convenience, dies if allocation fails.
603 */
Simon Butcherecff2192018-10-03 16:17:41 +0100604unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200605{
606 unsigned char *obuf;
607
Rich Evans42914452015-02-02 12:09:25 +0000608 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200609
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200610 if( *olen == 0 )
611 return( zero_alloc( *olen ) );
612
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200613 obuf = mbedtls_calloc( 1, *olen );
Gilles Peskine137d31b2019-06-07 14:52:07 +0200614 TEST_HELPER_ASSERT( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200615
Ronald Crona0c9ff32020-06-08 17:05:57 +0200616 (void) mbedtls_test_unhexify( obuf, ibuf );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200617
618 return( obuf );
619}
620
621/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000622 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000623 * Although predictable and often similar on multiple
624 * runs, this does not result in identical random on
625 * each run. So do not use this if the results of a
626 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000627 *
628 * rng_state shall be NULL.
629 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000630static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000631{
gufe443fa7c642020-08-03 17:56:50 +0200632#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000633 size_t i;
634
Paul Bakker9dcc3222011-03-08 14:16:06 +0000635 if( rng_state != NULL )
636 rng_state = NULL;
637
Paul Bakkera3d195c2011-11-27 21:07:34 +0000638 for( i = 0; i < len; ++i )
639 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200640#else
641 if( rng_state != NULL )
642 rng_state = NULL;
643
644 arc4random_buf( output, len );
gufe443fa7c642020-08-03 17:56:50 +0200645#endif /* !OpenBSD && !NetBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000646
647 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000648}
649
650/**
651 * This function only returns zeros
652 *
653 * rng_state shall be NULL.
654 */
Simon Butcherecff2192018-10-03 16:17:41 +0100655int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000656{
657 if( rng_state != NULL )
658 rng_state = NULL;
659
Paul Bakkera3d195c2011-11-27 21:07:34 +0000660 memset( output, 0, len );
661
Paul Bakker9dcc3222011-03-08 14:16:06 +0000662 return( 0 );
663}
664
665typedef struct
666{
667 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000668 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000669} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000670
671/**
672 * This function returns random based on a buffer it receives.
673 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000674 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100675 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000676 * The number of bytes released from the buffer on each call to
677 * the random function is specified by per_call. (Can be between
678 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000679 *
680 * After the buffer is empty it will return rand();
681 */
Simon Butcherecff2192018-10-03 16:17:41 +0100682int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000683{
Paul Bakker997bbd12011-03-13 15:45:42 +0000684 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000685 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000686
687 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000688 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000689
Paul Bakkera3d195c2011-11-27 21:07:34 +0000690 use_len = len;
691 if( len > info->length )
692 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000693
Paul Bakkera3d195c2011-11-27 21:07:34 +0000694 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000695 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000696 memcpy( output, info->buf, use_len );
697 info->buf += use_len;
698 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000699 }
700
Paul Bakkera3d195c2011-11-27 21:07:34 +0000701 if( len - use_len > 0 )
702 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
703
704 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000705}
Paul Bakker997bbd12011-03-13 15:45:42 +0000706
707/**
708 * Info structure for the pseudo random function
709 *
710 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000711 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000712 * State( v0, v1 ) should be set to zero.
713 */
714typedef struct
715{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000716 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000717 uint32_t v0, v1;
718} rnd_pseudo_info;
719
720/**
721 * This function returns random based on a pseudo random function.
722 * This means the results should be identical on all systems.
723 * Pseudo random is based on the XTEA encryption algorithm to
724 * generate pseudorandom.
725 *
726 * rng_state shall be a pointer to a rnd_pseudo_info structure.
727 */
Simon Butcherecff2192018-10-03 16:17:41 +0100728int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000729{
730 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000731 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100732 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000733
734 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000735 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000736
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000737 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000738
739 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000740 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000741 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000742 sum = 0;
743
Paul Bakkera3d195c2011-11-27 21:07:34 +0000744 for( i = 0; i < 32; i++ )
745 {
Rich Evans42914452015-02-02 12:09:25 +0000746 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
747 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000748 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000749 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
750 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000751 }
752
Paul Bakker5c2364c2012-10-01 14:41:15 +0000753 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100754 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000755 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100756 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000757 }
758
Paul Bakkera3d195c2011-11-27 21:07:34 +0000759 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000760}
SimonB0269dad2016-02-17 23:34:30 +0000761
Ronald Crond2397942020-06-10 11:03:08 +0200762int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100763{
764 int ret = 0;
765 uint32_t i = 0;
766
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100767 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100768 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100769
770 for( i = 0; i < a_len; i++ )
771 {
Manuel Pégourié-Gonnardaae10fa2018-12-12 10:24:19 +0100772 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100773 {
774 ret = -1;
775 break;
776 }
777 }
778 return ret;
779}