blob: fe398e218fb1962a193c59215b3cefe305f36417 [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
Ronald Cron4b8b1992020-06-09 13:52:23 +02005#include <test/macros.h>
6
Simon Butcheredb7fd92016-05-17 13:35:51 +01007#include <stdlib.h>
8
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020011#else
Rich Evans00ab4702015-02-06 13:43:58 +000012#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010014#define mbedtls_snprintf snprintf
15#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#define mbedtls_free free
17#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010018#define mbedtls_time time
19#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010020#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
21#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020022#endif
23
SimonB0269dad2016-02-17 23:34:30 +000024#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
25#include "mbedtls/memory_buffer_alloc.h"
26#endif
27
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050028#if defined(MBEDTLS_CHECK_PARAMS)
29#include "mbedtls/platform_util.h"
30#include <setjmp.h>
31#endif
32
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000033#ifdef _MSC_VER
34#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010035typedef UINT8 uint8_t;
36typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000037typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000038#define strncasecmp _strnicmp
39#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000040#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020041#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000042#endif
43
Paul Bakker19343182013-08-16 13:31:10 +020044#include <string.h>
45
Janos Follath8ca53b52016-10-05 10:57:49 +010046#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
47#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000048#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010049#endif
SimonB0269dad2016-02-17 23:34:30 +000050
Azim Khand30ca132017-06-09 04:32:58 +010051/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010052typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010053{
54 uint8_t * x;
55 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010056} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010057
SimonB0269dad2016-02-17 23:34:30 +000058/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010059/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000060
Azim Khan62a5d7d2018-06-29 10:02:54 +010061#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
62#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
63#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010064
Azim Khan62a5d7d2018-06-29 10:02:54 +010065#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
66#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
67#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
68#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
69 Only int, string, binary data
70 and integer expressions are
71 allowed */
72#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
73 build */
SimonB0269dad2016-02-17 23:34:30 +000074
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050075typedef enum
76{
77 PARAMFAIL_TESTSTATE_IDLE = 0, /* No parameter failure call test */
78 PARAMFAIL_TESTSTATE_PENDING, /* Test call to the parameter failure
79 * is pending */
80 PARAMFAIL_TESTSTATE_CALLED /* The test call to the parameter
81 * failure function has been made */
82} paramfail_test_state_t;
83
SimonB0269dad2016-02-17 23:34:30 +000084
85/*----------------------------------------------------------------------------*/
86/* Macros */
87
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088/**
89 * \brief This macro tests the expression passed to it as a test step or
90 * individual test in a test case.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020091 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050092 * It allows a library function to return a value and return an error
93 * code that can be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020094 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050095 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
96 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
97 * failure.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020098 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050099 * This macro is not suitable for negative parameter validation tests,
100 * as it assumes the test step will not create an error.
101 *
Jaeden Amero67ea2c52019-02-11 12:05:54 +0000102 * Failing the test means:
103 * - Mark this test case as failed.
104 * - Print a message identifying the failure.
105 * - Jump to the \c exit label.
106 *
107 * This macro expands to an instruction, not an expression.
108 * It may jump to the \c exit label.
109 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 * \param TEST The test expression to be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +0200111 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500112#define TEST_ASSERT( TEST ) \
113 do { \
114 if( ! (TEST) ) \
115 { \
116 test_fail( #TEST, __LINE__, __FILE__ ); \
117 goto exit; \
118 } \
SimonB0269dad2016-02-17 23:34:30 +0000119 } while( 0 )
120
Gilles Peskine5f7aeee2018-12-17 23:26:52 +0100121/** Evaluate two expressions and fail the test case if they have different
122 * values.
123 *
124 * \param expr1 An expression to evaluate.
125 * \param expr2 The expected value of \p expr1. This can be any
126 * expression, but it is typically a constant.
127 */
128#define TEST_EQUAL( expr1, expr2 ) \
129 TEST_ASSERT( ( expr1 ) == ( expr2 ) )
130
Gilles Peskineb75125c2018-09-27 13:52:16 +0200131/** Allocate memory dynamically and fail the test case if this fails.
132 *
133 * You must set \p pointer to \c NULL before calling this macro and
134 * put `mbedtls_free( pointer )` in the test's cleanup code.
135 *
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100136 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200137 * This is usually what we want in tests since API functions are
138 * supposed to accept null pointers when a buffer size is zero.
139 *
140 * This macro expands to an instruction, not an expression.
141 * It may jump to the \c exit label.
142 *
143 * \param pointer An lvalue where the address of the allocated buffer
144 * will be stored.
145 * This expression may be evaluated multiple times.
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100146 * \param length Number of elements to allocate.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200147 * This expression may be evaluated multiple times.
148 *
149 */
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100150#define ASSERT_ALLOC( pointer, length ) \
151 do \
152 { \
153 TEST_ASSERT( ( pointer ) == NULL ); \
154 if( ( length ) != 0 ) \
155 { \
156 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
157 ( length ) ); \
158 TEST_ASSERT( ( pointer ) != NULL ); \
159 } \
160 } \
Gilles Peskineb75125c2018-09-27 13:52:16 +0200161 while( 0 )
162
Gilles Peskine292672e2020-01-21 16:20:04 +0100163/** Allocate memory dynamically. If the allocation fails, skip the test case.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100164 *
165 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
Gilles Peskine292672e2020-01-21 16:20:04 +0100166 * fails, it marks the test as skipped rather than failed.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100167 */
168#define ASSERT_ALLOC_WEAK( pointer, length ) \
169 do \
170 { \
171 TEST_ASSERT( ( pointer ) == NULL ); \
172 if( ( length ) != 0 ) \
173 { \
174 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
175 ( length ) ); \
Gilles Peskine292672e2020-01-21 16:20:04 +0100176 TEST_ASSUME( ( pointer ) != NULL ); \
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100177 } \
178 } \
179 while( 0 )
180
Gilles Peskine3c225962018-09-27 13:56:31 +0200181/** Compare two buffers and fail the test case if they differ.
182 *
183 * This macro expands to an instruction, not an expression.
184 * It may jump to the \c exit label.
185 *
186 * \param p1 Pointer to the start of the first buffer.
187 * \param size1 Size of the first buffer in bytes.
188 * This expression may be evaluated multiple times.
189 * \param p2 Pointer to the start of the second buffer.
190 * \param size2 Size of the second buffer in bytes.
191 * This expression may be evaluated multiple times.
192 */
193#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
194 do \
195 { \
196 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
197 if( ( size1 ) != 0 ) \
198 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
199 } \
200 while( 0 )
201
Hanno Beckere69d0152019-07-05 13:31:30 +0100202/**
203 * \brief This macro tests the expression passed to it and skips the
204 * running test if it doesn't evaluate to 'true'.
205 *
206 * \param TEST The test expression to be tested.
207 */
208#define TEST_ASSUME( TEST ) \
209 do { \
210 if( ! (TEST) ) \
211 { \
212 test_skip( #TEST, __LINE__, __FILE__ ); \
213 goto exit; \
214 } \
215 } while( 0 )
216
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500217#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
218/**
219 * \brief This macro tests the statement passed to it as a test step or
220 * individual test in a test case. The macro assumes the test will fail
221 * and will generate an error.
222 *
223 * It allows a library function to return a value and tests the return
224 * code on return to confirm the given error code was returned.
225 *
226 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
227 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
228 * expected failure, and the test will pass.
229 *
230 * This macro is intended for negative parameter validation tests,
231 * where the failing function may return an error value or call
232 * MBEDTLS_PARAM_FAILED() to indicate the error.
233 *
234 * \param PARAM_ERROR_VALUE The expected error code.
235 *
236 * \param TEST The test expression to be tested.
237 */
238#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
239 do { \
240 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
241 if( (TEST) != (PARAM_ERR_VALUE) || \
242 test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
243 { \
244 test_fail( #TEST, __LINE__, __FILE__ ); \
245 goto exit; \
246 } \
247 } while( 0 )
248
249/**
250 * \brief This macro tests the statement passed to it as a test step or
251 * individual test in a test case. The macro assumes the test will fail
252 * and will generate an error.
253 *
254 * It assumes the library function under test cannot return a value and
255 * assumes errors can only be indicated byt calls to
256 * MBEDTLS_PARAM_FAILED().
257 *
258 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
259 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
260 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
261 * can be made.
262 *
263 * This macro is intended for negative parameter validation tests,
264 * where the failing function can only return an error by calling
265 * MBEDTLS_PARAM_FAILED() to indicate the error.
266 *
267 * \param TEST The test expression to be tested.
268 */
269#define TEST_INVALID_PARAM( TEST ) \
270 do { \
271 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
272 if( setjmp( param_fail_jmp ) == 0 ) \
273 { \
274 TEST; \
275 test_fail( #TEST, __LINE__, __FILE__ ); \
276 goto exit; \
277 } \
278 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
279 } while( 0 )
280#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
281
282/**
283 * \brief This macro tests the statement passed to it as a test step or
284 * individual test in a test case. The macro assumes the test will not fail.
285 *
286 * It assumes the library function under test cannot return a value and
287 * assumes errors can only be indicated by calls to
288 * MBEDTLS_PARAM_FAILED().
289 *
290 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
291 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
292 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
293 * can be made.
294 *
295 * This macro is intended to test that functions returning void
296 * accept all of the parameter values they're supposed to accept - eg
297 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
298 * that's allowed to be NULL happens to be NULL.
299 *
300 * Note: for functions that return something other that void,
301 * checking that they accept all the parameters they're supposed to
302 * accept is best done by using TEST_ASSERT() and checking the return
303 * value as well.
304 *
305 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
306 * disabled, as it makes sense to check that the functions accept all
307 * legal values even if this option is disabled - only in that case,
308 * the test is more about whether the function segfaults than about
309 * whether it invokes MBEDTLS_PARAM_FAILED().
310 *
311 * \param TEST The test expression to be tested.
312 */
313#define TEST_VALID_PARAM( TEST ) \
314 TEST_ASSERT( ( TEST, 1 ) );
315
Gilles Peskine28405302018-09-27 13:52:16 +0200316#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
Rich Evans4c091142015-02-02 12:04:10 +0000317{ \
Gilles Peskine28405302018-09-27 13:52:16 +0200318 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000319 __FILE__, __LINE__, #a ); \
Gilles Peskine28405302018-09-27 13:52:16 +0200320 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000321}
322
Gilles Peskinef055ad72018-12-17 23:18:00 +0100323#if defined(__GNUC__)
324/* Test if arg and &(arg)[0] have the same type. This is true if arg is
325 * an array but not if it's a pointer. */
326#define IS_ARRAY_NOT_POINTER( arg ) \
327 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
328 __typeof__( &( arg )[0] ) ) )
329#else
330/* On platforms where we don't know how to implement this check,
331 * omit it. Oh well, a non-portable check is better than nothing. */
332#define IS_ARRAY_NOT_POINTER( arg ) 1
333#endif
334
335/* A compile-time constant with the value 0. If `const_expr` is not a
336 * compile-time constant with a nonzero value, cause a compile-time error. */
337#define STATIC_ASSERT_EXPR( const_expr ) \
338 ( 0 && sizeof( struct { int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
339/* Return the scalar value `value` (possibly promoted). This is a compile-time
340 * constant if `value` is. `condition` must be a compile-time constant.
341 * If `condition` is false, arrange to cause a compile-time error. */
342#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
343 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
344
345#define ARRAY_LENGTH_UNSAFE( array ) \
Gilles Peskine3d2f9492018-12-17 23:17:17 +0100346 ( sizeof( array ) / sizeof( *( array ) ) )
Gilles Peskinef055ad72018-12-17 23:18:00 +0100347/** Return the number of elements of a static or stack array.
348 *
349 * \param array A value of array (not pointer) type.
350 *
351 * \return The number of elements of the array.
352 */
353#define ARRAY_LENGTH( array ) \
354 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
355 ARRAY_LENGTH_UNSAFE( array ) ) )
356
Gilles Peskinec08fc1d2018-12-18 08:47:00 +0100357/** Return the smaller of two values.
358 *
359 * \param x An integer-valued expression without side effects.
360 * \param y An integer-valued expression without side effects.
361 *
362 * \return The smaller of \p x and \p y.
363 */
364#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
365
366/** Return the larger of two values.
367 *
368 * \param x An integer-valued expression without side effects.
369 * \param y An integer-valued expression without side effects.
370 *
371 * \return The larger of \p x and \p y.
372 */
373#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
Gilles Peskinef055ad72018-12-17 23:18:00 +0100374
Gilles Peskine28405302018-09-27 13:52:16 +0200375/** Allocate memory dynamically and fail the test case if this fails.
376 *
377 * You must set \p pointer to \c NULL before calling this macro and
378 * put `mbedtls_free( pointer )` in the test's cleanup code.
379 *
Gilles Peskine6608e712018-11-30 18:51:45 +0100380 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskine28405302018-09-27 13:52:16 +0200381 * This is usually what we want in tests since API functions are
382 * supposed to accept null pointers when a buffer size is zero.
383 *
384 * This macro expands to an instruction, not an expression.
385 * It may jump to the \c exit label.
386 *
387 * \param pointer An lvalue where the address of the allocated buffer
388 * will be stored.
389 * This expression may be evaluated multiple times.
Gilles Peskine6608e712018-11-30 18:51:45 +0100390 * \param length Number of elements to allocate.
Gilles Peskine28405302018-09-27 13:52:16 +0200391 * This expression may be evaluated multiple times.
392 *
393 */
Gilles Peskine6608e712018-11-30 18:51:45 +0100394#define ASSERT_ALLOC( pointer, length ) \
395 do \
396 { \
397 TEST_ASSERT( ( pointer ) == NULL ); \
398 if( ( length ) != 0 ) \
399 { \
400 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
401 ( length ) ); \
402 TEST_ASSERT( ( pointer ) != NULL ); \
403 } \
404 } \
Gilles Peskine28405302018-09-27 13:52:16 +0200405 while( 0 )
406
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000407/*
408 * 32-bit integer manipulation macros (big endian)
409 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000410#ifndef GET_UINT32_BE
411#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000412{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000413 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
414 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
415 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
416 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000417}
418#endif
419
Paul Bakker5c2364c2012-10-01 14:41:15 +0000420#ifndef PUT_UINT32_BE
421#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000422{ \
423 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
424 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
425 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
426 (b)[(i) + 3] = (unsigned char) ( (n) ); \
427}
428#endif
429
SimonB0269dad2016-02-17 23:34:30 +0000430
431/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100432/* Global variables */
433
Hanno Beckere69d0152019-07-05 13:31:30 +0100434typedef enum
435{
436 TEST_RESULT_SUCCESS = 0,
437 TEST_RESULT_FAILED,
438 TEST_RESULT_SKIPPED
439} test_result_t;
440
Gilles Peskine47b75402019-09-16 15:12:51 +0200441typedef struct
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100442{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500443 paramfail_test_state_t paramfail_test_state;
Hanno Beckere69d0152019-07-05 13:31:30 +0100444 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100445 const char *test;
446 const char *filename;
447 int line_no;
Gilles Peskine56055912019-03-01 14:26:30 +0100448 unsigned long step;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100449}
Gilles Peskine47b75402019-09-16 15:12:51 +0200450test_info_t;
451static test_info_t test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100452
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400453#if defined(MBEDTLS_PLATFORM_C)
Ronald Cronf91c4952020-05-27 16:22:17 +0200454static mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400455#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100456
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500457#if defined(MBEDTLS_CHECK_PARAMS)
458jmp_buf param_fail_jmp;
459jmp_buf jmp_tmp;
460#endif
461
SimonB8ca7bc42016-04-17 23:24:50 +0100462/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100463/* Helper flags for complex dependencies */
464
465/* Indicates whether we expect mbedtls_entropy_init
466 * to initialize some strong entropy source. */
467#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
468 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
469 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
470 defined(MBEDTLS_HAVEGE_C) || \
471 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
472 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100473#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100474#endif
475
476
477/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000478/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500479
Gilles Peskine56055912019-03-01 14:26:30 +0100480/** Set the test step number for failure reports.
481 *
482 * Call this function to display "step NNN" in addition to the line number
483 * and file name if a test fails. Typically the "step number" is the index
484 * of a for loop but it can be whatever you want.
485 *
486 * \param step The step number to report.
487 */
488void test_set_step( unsigned long step )
489{
490 test_info.step = step;
491}
492
Simon Butcherecff2192018-10-03 16:17:41 +0100493void test_fail( const char *test, int line_no, const char* filename )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500494{
Hanno Beckere69d0152019-07-05 13:31:30 +0100495 test_info.result = TEST_RESULT_FAILED;
496 test_info.test = test;
497 test_info.line_no = line_no;
498 test_info.filename = filename;
499}
500
501void test_skip( const char *test, int line_no, const char* filename )
502{
503 test_info.result = TEST_RESULT_SKIPPED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500504 test_info.test = test;
505 test_info.line_no = line_no;
506 test_info.filename = filename;
507}
508
Ronald Cronf91c4952020-05-27 16:22:17 +0200509int platform_setup()
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400510{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400511 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400512#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400513 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400514#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400515 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400516}
517
Ronald Cronf91c4952020-05-27 16:22:17 +0200518void platform_teardown()
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400519{
520#if defined(MBEDTLS_PLATFORM_C)
521 mbedtls_platform_teardown( &platform_ctx );
522#endif /* MBEDTLS_PLATFORM_C */
523}
SimonB0269dad2016-02-17 23:34:30 +0000524
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500525#if defined(MBEDTLS_CHECK_PARAMS)
526void mbedtls_param_failed( const char *failure_condition,
527 const char *file,
528 int line )
529{
530 /* If we are testing the callback function... */
531 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
532 {
533 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
534 }
535 else
536 {
537 /* ...else we treat this as an error */
538
539 /* Record the location of the failure, but not as a failure yet, in case
540 * it was part of the test */
541 test_fail( failure_condition, line, file );
Hanno Beckere69d0152019-07-05 13:31:30 +0100542 test_info.result = TEST_RESULT_SUCCESS;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500543
544 longjmp( param_fail_jmp, 1 );
545 }
546}
547#endif
548
Janos Follath8ca53b52016-10-05 10:57:49 +0100549#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
550static int redirect_output( FILE** out_stream, const char* path )
551{
552 int stdout_fd = dup( fileno( *out_stream ) );
553
554 if( stdout_fd == -1 )
555 {
556 return -1;
557 }
558
559 fflush( *out_stream );
560 fclose( *out_stream );
561 *out_stream = fopen( path, "w" );
562
563 if( *out_stream == NULL )
564 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500565 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100566 return -1;
567 }
568
569 return stdout_fd;
570}
571
572static int restore_output( FILE** out_stream, int old_fd )
573{
574 fflush( *out_stream );
575 fclose( *out_stream );
576
577 *out_stream = fdopen( old_fd, "w" );
578 if( *out_stream == NULL )
579 {
580 return -1;
581 }
582
583 return 0;
584}
Simon Butchere0192962016-10-12 23:07:30 +0100585
Janos Follathe709f7c2016-10-13 11:26:29 +0100586static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100587{
Janos Follathe709f7c2016-10-13 11:26:29 +0100588 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100589}
Janos Follath8ca53b52016-10-05 10:57:49 +0100590#endif /* __unix__ || __APPLE__ __MACH__ */
591
Simon Butcherecff2192018-10-03 16:17:41 +0100592int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000593{
594 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000595 int len = strlen( ibuf ) / 2;
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200596 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000597
Rich Evans4c091142015-02-02 12:04:10 +0000598 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000599 {
600 c = *ibuf++;
601 if( c >= '0' && c <= '9' )
602 c -= '0';
603 else if( c >= 'a' && c <= 'f' )
604 c -= 'a' - 10;
605 else if( c >= 'A' && c <= 'F' )
606 c -= 'A' - 10;
607 else
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200608 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000609
610 c2 = *ibuf++;
611 if( c2 >= '0' && c2 <= '9' )
612 c2 -= '0';
613 else if( c2 >= 'a' && c2 <= 'f' )
614 c2 -= 'a' - 10;
615 else if( c2 >= 'A' && c2 <= 'F' )
616 c2 -= 'A' - 10;
617 else
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200618 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000619
620 *obuf++ = ( c << 4 ) | c2;
621 }
622
623 return len;
624}
625
Simon Butcherecff2192018-10-03 16:17:41 +0100626void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000627{
628 unsigned char l, h;
629
Rich Evans42914452015-02-02 12:09:25 +0000630 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000631 {
Rich Evans42914452015-02-02 12:09:25 +0000632 h = *ibuf / 16;
633 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000634
635 if( h < 10 )
636 *obuf++ = '0' + h;
637 else
638 *obuf++ = 'a' + h - 10;
639
640 if( l < 10 )
641 *obuf++ = '0' + l;
642 else
643 *obuf++ = 'a' + l - 10;
644
645 ++ibuf;
646 len--;
647 }
648}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000649
650/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200651 * Allocate and zeroize a buffer.
652 *
653 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
654 *
655 * For convenience, dies if allocation fails.
656 */
Ronald Cronf91c4952020-05-27 16:22:17 +0200657unsigned char *zero_alloc( size_t len )
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200658{
659 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000660 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200661
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200662 p = mbedtls_calloc( 1, actual_len );
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200663 TEST_HELPER_ASSERT( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200664
665 memset( p, 0x00, actual_len );
666
667 return( p );
668}
669
670/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200671 * Allocate and fill a buffer from hex data.
672 *
673 * The buffer is sized exactly as needed. This allows to detect buffer
674 * overruns (including overreads) when running the test suite under valgrind.
675 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200676 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
677 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200678 * For convenience, dies if allocation fails.
679 */
Simon Butcherecff2192018-10-03 16:17:41 +0100680unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200681{
682 unsigned char *obuf;
683
Rich Evans42914452015-02-02 12:09:25 +0000684 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200685
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200686 if( *olen == 0 )
687 return( zero_alloc( *olen ) );
688
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200689 obuf = mbedtls_calloc( 1, *olen );
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200690 TEST_HELPER_ASSERT( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200691
692 (void) unhexify( obuf, ibuf );
693
694 return( obuf );
695}
696
697/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000698 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000699 * Although predictable and often similar on multiple
700 * runs, this does not result in identical random on
701 * each run. So do not use this if the results of a
702 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000703 *
704 * rng_state shall be NULL.
705 */
Ronald Cronf91c4952020-05-27 16:22:17 +0200706int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000707{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200708#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000709 size_t i;
710
Paul Bakker9dcc3222011-03-08 14:16:06 +0000711 if( rng_state != NULL )
712 rng_state = NULL;
713
Paul Bakkera3d195c2011-11-27 21:07:34 +0000714 for( i = 0; i < len; ++i )
715 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200716#else
717 if( rng_state != NULL )
718 rng_state = NULL;
719
720 arc4random_buf( output, len );
721#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000722
723 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000724}
725
726/**
727 * This function only returns zeros
728 *
729 * rng_state shall be NULL.
730 */
Simon Butcherecff2192018-10-03 16:17:41 +0100731int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000732{
733 if( rng_state != NULL )
734 rng_state = NULL;
735
Paul Bakkera3d195c2011-11-27 21:07:34 +0000736 memset( output, 0, len );
737
Paul Bakker9dcc3222011-03-08 14:16:06 +0000738 return( 0 );
739}
740
741typedef struct
742{
743 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000744 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000745} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000746
747/**
748 * This function returns random based on a buffer it receives.
749 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000750 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100751 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000752 * The number of bytes released from the buffer on each call to
753 * the random function is specified by per_call. (Can be between
754 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000755 *
756 * After the buffer is empty it will return rand();
757 */
Simon Butcherecff2192018-10-03 16:17:41 +0100758int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000759{
Paul Bakker997bbd12011-03-13 15:45:42 +0000760 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000761 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000762
763 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000764 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000765
Paul Bakkera3d195c2011-11-27 21:07:34 +0000766 use_len = len;
767 if( len > info->length )
768 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000769
Paul Bakkera3d195c2011-11-27 21:07:34 +0000770 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000771 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000772 memcpy( output, info->buf, use_len );
773 info->buf += use_len;
774 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000775 }
776
Paul Bakkera3d195c2011-11-27 21:07:34 +0000777 if( len - use_len > 0 )
778 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
779
780 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000781}
Paul Bakker997bbd12011-03-13 15:45:42 +0000782
783/**
784 * Info structure for the pseudo random function
785 *
786 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000787 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000788 * State( v0, v1 ) should be set to zero.
789 */
790typedef struct
791{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000792 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000793 uint32_t v0, v1;
794} rnd_pseudo_info;
795
796/**
797 * This function returns random based on a pseudo random function.
798 * This means the results should be identical on all systems.
799 * Pseudo random is based on the XTEA encryption algorithm to
800 * generate pseudorandom.
801 *
802 * rng_state shall be a pointer to a rnd_pseudo_info structure.
803 */
Simon Butcherecff2192018-10-03 16:17:41 +0100804int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000805{
806 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000807 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100808 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000809
810 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000811 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000812
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000813 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000814
815 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000816 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000817 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000818 sum = 0;
819
Paul Bakkera3d195c2011-11-27 21:07:34 +0000820 for( i = 0; i < 32; i++ )
821 {
Rich Evans42914452015-02-02 12:09:25 +0000822 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
823 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000824 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000825 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
826 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000827 }
828
Paul Bakker5c2364c2012-10-01 14:41:15 +0000829 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100830 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000831 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100832 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000833 }
834
Paul Bakkera3d195c2011-11-27 21:07:34 +0000835 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000836}
SimonB0269dad2016-02-17 23:34:30 +0000837
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100838int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100839{
840 int ret = 0;
841 uint32_t i = 0;
842
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500843 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100844 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100845
846 for( i = 0; i < a_len; i++ )
847 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500848 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100849 {
850 ret = -1;
851 break;
852 }
853 }
854 return ret;
855}