blob: 5ed37da09bedb1bbd857ea2a6a03c7684ee282c6 [file] [log] [blame]
Azim Khanec024482017-05-09 17:20:21 +01001#line 2 "suites/helpers.function"
SimonB0269dad2016-02-17 23:34:30 +00002/*----------------------------------------------------------------------------*/
3/* Headers */
4
Simon Butcheredb7fd92016-05-17 13:35:51 +01005#include <stdlib.h>
6
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00008#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +02009#else
Rich Evans00ab4702015-02-06 13:43:58 +000010#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010012#define mbedtls_snprintf snprintf
13#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#define mbedtls_free free
15#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010016#define mbedtls_time time
17#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010018#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
19#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020020#endif
21
SimonB0269dad2016-02-17 23:34:30 +000022#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
23#include "mbedtls/memory_buffer_alloc.h"
24#endif
25
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050026#if defined(MBEDTLS_CHECK_PARAMS)
27#include "mbedtls/platform_util.h"
28#include <setjmp.h>
29#endif
30
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000031#ifdef _MSC_VER
32#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010033typedef UINT8 uint8_t;
34typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000035typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000036#define strncasecmp _strnicmp
37#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000038#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020039#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000040#endif
41
Paul Bakker19343182013-08-16 13:31:10 +020042#include <string.h>
43
Janos Follath8ca53b52016-10-05 10:57:49 +010044#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
45#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000046#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010047#endif
SimonB0269dad2016-02-17 23:34:30 +000048
Azim Khand30ca132017-06-09 04:32:58 +010049/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010050typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010051{
52 uint8_t * x;
53 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010054} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010055
SimonB0269dad2016-02-17 23:34:30 +000056/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010057/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000058
Azim Khan62a5d7d2018-06-29 10:02:54 +010059#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
60#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
61#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010062
Azim Khan62a5d7d2018-06-29 10:02:54 +010063#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
64#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
65#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
66#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
67 Only int, string, binary data
68 and integer expressions are
69 allowed */
70#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
71 build */
SimonB0269dad2016-02-17 23:34:30 +000072
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050073typedef enum
74{
75 PARAMFAIL_TESTSTATE_IDLE = 0, /* No parameter failure call test */
76 PARAMFAIL_TESTSTATE_PENDING, /* Test call to the parameter failure
77 * is pending */
78 PARAMFAIL_TESTSTATE_CALLED /* The test call to the parameter
79 * failure function has been made */
80} paramfail_test_state_t;
81
SimonB0269dad2016-02-17 23:34:30 +000082
83/*----------------------------------------------------------------------------*/
84/* Macros */
85
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086/**
87 * \brief This macro tests the expression passed to it as a test step or
88 * individual test in a test case.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020089 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050090 * It allows a library function to return a value and return an error
91 * code that can be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020092 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
94 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
95 * failure.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020096 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * This macro is not suitable for negative parameter validation tests,
98 * as it assumes the test step will not create an error.
99 *
Jaeden Amero67ea2c52019-02-11 12:05:54 +0000100 * Failing the test means:
101 * - Mark this test case as failed.
102 * - Print a message identifying the failure.
103 * - Jump to the \c exit label.
104 *
105 * This macro expands to an instruction, not an expression.
106 * It may jump to the \c exit label.
107 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 * \param TEST The test expression to be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +0200109 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110#define TEST_ASSERT( TEST ) \
111 do { \
112 if( ! (TEST) ) \
113 { \
114 test_fail( #TEST, __LINE__, __FILE__ ); \
115 goto exit; \
116 } \
SimonB0269dad2016-02-17 23:34:30 +0000117 } while( 0 )
118
Gilles Peskine5f7aeee2018-12-17 23:26:52 +0100119/** Evaluate two expressions and fail the test case if they have different
120 * values.
121 *
122 * \param expr1 An expression to evaluate.
123 * \param expr2 The expected value of \p expr1. This can be any
124 * expression, but it is typically a constant.
125 */
126#define TEST_EQUAL( expr1, expr2 ) \
127 TEST_ASSERT( ( expr1 ) == ( expr2 ) )
128
Gilles Peskineb75125c2018-09-27 13:52:16 +0200129/** Allocate memory dynamically and fail the test case if this fails.
130 *
131 * You must set \p pointer to \c NULL before calling this macro and
132 * put `mbedtls_free( pointer )` in the test's cleanup code.
133 *
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100134 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200135 * This is usually what we want in tests since API functions are
136 * supposed to accept null pointers when a buffer size is zero.
137 *
138 * This macro expands to an instruction, not an expression.
139 * It may jump to the \c exit label.
140 *
141 * \param pointer An lvalue where the address of the allocated buffer
142 * will be stored.
143 * This expression may be evaluated multiple times.
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100144 * \param length Number of elements to allocate.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200145 * This expression may be evaluated multiple times.
146 *
147 */
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100148#define ASSERT_ALLOC( pointer, length ) \
149 do \
150 { \
151 TEST_ASSERT( ( pointer ) == NULL ); \
152 if( ( length ) != 0 ) \
153 { \
154 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
155 ( length ) ); \
156 TEST_ASSERT( ( pointer ) != NULL ); \
157 } \
158 } \
Gilles Peskineb75125c2018-09-27 13:52:16 +0200159 while( 0 )
160
Gilles Peskine292672e2020-01-21 16:20:04 +0100161/** Allocate memory dynamically. If the allocation fails, skip the test case.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100162 *
163 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
Gilles Peskine292672e2020-01-21 16:20:04 +0100164 * fails, it marks the test as skipped rather than failed.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100165 */
166#define ASSERT_ALLOC_WEAK( pointer, length ) \
167 do \
168 { \
169 TEST_ASSERT( ( pointer ) == NULL ); \
170 if( ( length ) != 0 ) \
171 { \
172 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
173 ( length ) ); \
Gilles Peskine292672e2020-01-21 16:20:04 +0100174 TEST_ASSUME( ( pointer ) != NULL ); \
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100175 } \
176 } \
177 while( 0 )
178
Gilles Peskine3c225962018-09-27 13:56:31 +0200179/** Compare two buffers and fail the test case if they differ.
180 *
181 * This macro expands to an instruction, not an expression.
182 * It may jump to the \c exit label.
183 *
184 * \param p1 Pointer to the start of the first buffer.
185 * \param size1 Size of the first buffer in bytes.
186 * This expression may be evaluated multiple times.
187 * \param p2 Pointer to the start of the second buffer.
188 * \param size2 Size of the second buffer in bytes.
189 * This expression may be evaluated multiple times.
190 */
191#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
192 do \
193 { \
194 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
195 if( ( size1 ) != 0 ) \
196 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
197 } \
198 while( 0 )
199
Hanno Beckere69d0152019-07-05 13:31:30 +0100200/**
201 * \brief This macro tests the expression passed to it and skips the
202 * running test if it doesn't evaluate to 'true'.
203 *
204 * \param TEST The test expression to be tested.
205 */
206#define TEST_ASSUME( TEST ) \
207 do { \
208 if( ! (TEST) ) \
209 { \
210 test_skip( #TEST, __LINE__, __FILE__ ); \
211 goto exit; \
212 } \
213 } while( 0 )
214
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500215#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
216/**
217 * \brief This macro tests the statement passed to it as a test step or
218 * individual test in a test case. The macro assumes the test will fail
219 * and will generate an error.
220 *
221 * It allows a library function to return a value and tests the return
222 * code on return to confirm the given error code was returned.
223 *
224 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
225 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
226 * expected failure, and the test will pass.
227 *
228 * This macro is intended for negative parameter validation tests,
229 * where the failing function may return an error value or call
230 * MBEDTLS_PARAM_FAILED() to indicate the error.
231 *
232 * \param PARAM_ERROR_VALUE The expected error code.
233 *
234 * \param TEST The test expression to be tested.
235 */
236#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
237 do { \
238 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
239 if( (TEST) != (PARAM_ERR_VALUE) || \
240 test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
241 { \
242 test_fail( #TEST, __LINE__, __FILE__ ); \
243 goto exit; \
244 } \
245 } while( 0 )
246
247/**
248 * \brief This macro tests the statement passed to it as a test step or
249 * individual test in a test case. The macro assumes the test will fail
250 * and will generate an error.
251 *
252 * It assumes the library function under test cannot return a value and
253 * assumes errors can only be indicated byt calls to
254 * MBEDTLS_PARAM_FAILED().
255 *
256 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
257 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
258 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
259 * can be made.
260 *
261 * This macro is intended for negative parameter validation tests,
262 * where the failing function can only return an error by calling
263 * MBEDTLS_PARAM_FAILED() to indicate the error.
264 *
265 * \param TEST The test expression to be tested.
266 */
267#define TEST_INVALID_PARAM( TEST ) \
268 do { \
269 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
270 if( setjmp( param_fail_jmp ) == 0 ) \
271 { \
272 TEST; \
273 test_fail( #TEST, __LINE__, __FILE__ ); \
274 goto exit; \
275 } \
276 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
277 } while( 0 )
278#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
279
280/**
281 * \brief This macro tests the statement passed to it as a test step or
282 * individual test in a test case. The macro assumes the test will not fail.
283 *
284 * It assumes the library function under test cannot return a value and
285 * assumes errors can only be indicated by calls to
286 * MBEDTLS_PARAM_FAILED().
287 *
288 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
289 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
290 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
291 * can be made.
292 *
293 * This macro is intended to test that functions returning void
294 * accept all of the parameter values they're supposed to accept - eg
295 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
296 * that's allowed to be NULL happens to be NULL.
297 *
298 * Note: for functions that return something other that void,
299 * checking that they accept all the parameters they're supposed to
300 * accept is best done by using TEST_ASSERT() and checking the return
301 * value as well.
302 *
303 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
304 * disabled, as it makes sense to check that the functions accept all
305 * legal values even if this option is disabled - only in that case,
306 * the test is more about whether the function segfaults than about
307 * whether it invokes MBEDTLS_PARAM_FAILED().
308 *
309 * \param TEST The test expression to be tested.
310 */
311#define TEST_VALID_PARAM( TEST ) \
312 TEST_ASSERT( ( TEST, 1 ) );
313
Gilles Peskine28405302018-09-27 13:52:16 +0200314#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
Rich Evans4c091142015-02-02 12:04:10 +0000315{ \
Gilles Peskine28405302018-09-27 13:52:16 +0200316 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000317 __FILE__, __LINE__, #a ); \
Gilles Peskine28405302018-09-27 13:52:16 +0200318 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000319}
320
Gilles Peskinef055ad72018-12-17 23:18:00 +0100321#if defined(__GNUC__)
322/* Test if arg and &(arg)[0] have the same type. This is true if arg is
323 * an array but not if it's a pointer. */
324#define IS_ARRAY_NOT_POINTER( arg ) \
325 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
326 __typeof__( &( arg )[0] ) ) )
327#else
328/* On platforms where we don't know how to implement this check,
329 * omit it. Oh well, a non-portable check is better than nothing. */
330#define IS_ARRAY_NOT_POINTER( arg ) 1
331#endif
332
333/* A compile-time constant with the value 0. If `const_expr` is not a
334 * compile-time constant with a nonzero value, cause a compile-time error. */
335#define STATIC_ASSERT_EXPR( const_expr ) \
336 ( 0 && sizeof( struct { int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
337/* Return the scalar value `value` (possibly promoted). This is a compile-time
338 * constant if `value` is. `condition` must be a compile-time constant.
339 * If `condition` is false, arrange to cause a compile-time error. */
340#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
341 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
342
343#define ARRAY_LENGTH_UNSAFE( array ) \
Gilles Peskine3d2f9492018-12-17 23:17:17 +0100344 ( sizeof( array ) / sizeof( *( array ) ) )
Gilles Peskinef055ad72018-12-17 23:18:00 +0100345/** Return the number of elements of a static or stack array.
346 *
347 * \param array A value of array (not pointer) type.
348 *
349 * \return The number of elements of the array.
350 */
351#define ARRAY_LENGTH( array ) \
352 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
353 ARRAY_LENGTH_UNSAFE( array ) ) )
354
Gilles Peskinec08fc1d2018-12-18 08:47:00 +0100355/** Return the smaller of two values.
356 *
357 * \param x An integer-valued expression without side effects.
358 * \param y An integer-valued expression without side effects.
359 *
360 * \return The smaller of \p x and \p y.
361 */
362#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
363
364/** Return the larger of two values.
365 *
366 * \param x An integer-valued expression without side effects.
367 * \param y An integer-valued expression without side effects.
368 *
369 * \return The larger of \p x and \p y.
370 */
371#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
Gilles Peskinef055ad72018-12-17 23:18:00 +0100372
Gilles Peskine28405302018-09-27 13:52:16 +0200373/** Allocate memory dynamically and fail the test case if this fails.
374 *
375 * You must set \p pointer to \c NULL before calling this macro and
376 * put `mbedtls_free( pointer )` in the test's cleanup code.
377 *
Gilles Peskine6608e712018-11-30 18:51:45 +0100378 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskine28405302018-09-27 13:52:16 +0200379 * This is usually what we want in tests since API functions are
380 * supposed to accept null pointers when a buffer size is zero.
381 *
382 * This macro expands to an instruction, not an expression.
383 * It may jump to the \c exit label.
384 *
385 * \param pointer An lvalue where the address of the allocated buffer
386 * will be stored.
387 * This expression may be evaluated multiple times.
Gilles Peskine6608e712018-11-30 18:51:45 +0100388 * \param length Number of elements to allocate.
Gilles Peskine28405302018-09-27 13:52:16 +0200389 * This expression may be evaluated multiple times.
390 *
391 */
Gilles Peskine6608e712018-11-30 18:51:45 +0100392#define ASSERT_ALLOC( pointer, length ) \
393 do \
394 { \
395 TEST_ASSERT( ( pointer ) == NULL ); \
396 if( ( length ) != 0 ) \
397 { \
398 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
399 ( length ) ); \
400 TEST_ASSERT( ( pointer ) != NULL ); \
401 } \
402 } \
Gilles Peskine28405302018-09-27 13:52:16 +0200403 while( 0 )
404
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000405/*
406 * 32-bit integer manipulation macros (big endian)
407 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000408#ifndef GET_UINT32_BE
409#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000410{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000411 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
412 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
413 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
414 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000415}
416#endif
417
Paul Bakker5c2364c2012-10-01 14:41:15 +0000418#ifndef PUT_UINT32_BE
419#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000420{ \
421 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
422 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
423 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
424 (b)[(i) + 3] = (unsigned char) ( (n) ); \
425}
426#endif
427
SimonB0269dad2016-02-17 23:34:30 +0000428
429/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100430/* Global variables */
431
Hanno Beckere69d0152019-07-05 13:31:30 +0100432typedef enum
433{
434 TEST_RESULT_SUCCESS = 0,
435 TEST_RESULT_FAILED,
436 TEST_RESULT_SKIPPED
437} test_result_t;
438
Gilles Peskine47b75402019-09-16 15:12:51 +0200439typedef struct
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100440{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500441 paramfail_test_state_t paramfail_test_state;
Hanno Beckere69d0152019-07-05 13:31:30 +0100442 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100443 const char *test;
444 const char *filename;
445 int line_no;
Gilles Peskine56055912019-03-01 14:26:30 +0100446 unsigned long step;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100447}
Gilles Peskine47b75402019-09-16 15:12:51 +0200448test_info_t;
449static test_info_t test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100450
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400451#if defined(MBEDTLS_PLATFORM_C)
Ronald Cronf91c4952020-05-27 16:22:17 +0200452static mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400453#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100454
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500455#if defined(MBEDTLS_CHECK_PARAMS)
456jmp_buf param_fail_jmp;
457jmp_buf jmp_tmp;
458#endif
459
SimonB8ca7bc42016-04-17 23:24:50 +0100460/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100461/* Helper flags for complex dependencies */
462
463/* Indicates whether we expect mbedtls_entropy_init
464 * to initialize some strong entropy source. */
465#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
466 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
467 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
468 defined(MBEDTLS_HAVEGE_C) || \
469 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
470 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100471#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100472#endif
473
474
475/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000476/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500477
Gilles Peskine56055912019-03-01 14:26:30 +0100478/** Set the test step number for failure reports.
479 *
480 * Call this function to display "step NNN" in addition to the line number
481 * and file name if a test fails. Typically the "step number" is the index
482 * of a for loop but it can be whatever you want.
483 *
484 * \param step The step number to report.
485 */
486void test_set_step( unsigned long step )
487{
488 test_info.step = step;
489}
490
Simon Butcherecff2192018-10-03 16:17:41 +0100491void test_fail( const char *test, int line_no, const char* filename )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500492{
Hanno Beckere69d0152019-07-05 13:31:30 +0100493 test_info.result = TEST_RESULT_FAILED;
494 test_info.test = test;
495 test_info.line_no = line_no;
496 test_info.filename = filename;
497}
498
499void test_skip( const char *test, int line_no, const char* filename )
500{
501 test_info.result = TEST_RESULT_SKIPPED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500502 test_info.test = test;
503 test_info.line_no = line_no;
504 test_info.filename = filename;
505}
506
Ronald Cronf91c4952020-05-27 16:22:17 +0200507int platform_setup()
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400508{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400509 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400510#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400511 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400512#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400513 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400514}
515
Ronald Cronf91c4952020-05-27 16:22:17 +0200516void platform_teardown()
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400517{
518#if defined(MBEDTLS_PLATFORM_C)
519 mbedtls_platform_teardown( &platform_ctx );
520#endif /* MBEDTLS_PLATFORM_C */
521}
SimonB0269dad2016-02-17 23:34:30 +0000522
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500523#if defined(MBEDTLS_CHECK_PARAMS)
524void mbedtls_param_failed( const char *failure_condition,
525 const char *file,
526 int line )
527{
528 /* If we are testing the callback function... */
529 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
530 {
531 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
532 }
533 else
534 {
535 /* ...else we treat this as an error */
536
537 /* Record the location of the failure, but not as a failure yet, in case
538 * it was part of the test */
539 test_fail( failure_condition, line, file );
Hanno Beckere69d0152019-07-05 13:31:30 +0100540 test_info.result = TEST_RESULT_SUCCESS;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500541
542 longjmp( param_fail_jmp, 1 );
543 }
544}
545#endif
546
Janos Follath8ca53b52016-10-05 10:57:49 +0100547#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
548static int redirect_output( FILE** out_stream, const char* path )
549{
550 int stdout_fd = dup( fileno( *out_stream ) );
551
552 if( stdout_fd == -1 )
553 {
554 return -1;
555 }
556
557 fflush( *out_stream );
558 fclose( *out_stream );
559 *out_stream = fopen( path, "w" );
560
561 if( *out_stream == NULL )
562 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500563 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100564 return -1;
565 }
566
567 return stdout_fd;
568}
569
570static int restore_output( FILE** out_stream, int old_fd )
571{
572 fflush( *out_stream );
573 fclose( *out_stream );
574
575 *out_stream = fdopen( old_fd, "w" );
576 if( *out_stream == NULL )
577 {
578 return -1;
579 }
580
581 return 0;
582}
Simon Butchere0192962016-10-12 23:07:30 +0100583
Janos Follathe709f7c2016-10-13 11:26:29 +0100584static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100585{
Janos Follathe709f7c2016-10-13 11:26:29 +0100586 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100587}
Janos Follath8ca53b52016-10-05 10:57:49 +0100588#endif /* __unix__ || __APPLE__ __MACH__ */
589
Simon Butcherecff2192018-10-03 16:17:41 +0100590int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000591{
592 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000593 int len = strlen( ibuf ) / 2;
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200594 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000595
Rich Evans4c091142015-02-02 12:04:10 +0000596 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000597 {
598 c = *ibuf++;
599 if( c >= '0' && c <= '9' )
600 c -= '0';
601 else if( c >= 'a' && c <= 'f' )
602 c -= 'a' - 10;
603 else if( c >= 'A' && c <= 'F' )
604 c -= 'A' - 10;
605 else
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200606 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000607
608 c2 = *ibuf++;
609 if( c2 >= '0' && c2 <= '9' )
610 c2 -= '0';
611 else if( c2 >= 'a' && c2 <= 'f' )
612 c2 -= 'a' - 10;
613 else if( c2 >= 'A' && c2 <= 'F' )
614 c2 -= 'A' - 10;
615 else
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200616 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000617
618 *obuf++ = ( c << 4 ) | c2;
619 }
620
621 return len;
622}
623
Simon Butcherecff2192018-10-03 16:17:41 +0100624void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000625{
626 unsigned char l, h;
627
Rich Evans42914452015-02-02 12:09:25 +0000628 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000629 {
Rich Evans42914452015-02-02 12:09:25 +0000630 h = *ibuf / 16;
631 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000632
633 if( h < 10 )
634 *obuf++ = '0' + h;
635 else
636 *obuf++ = 'a' + h - 10;
637
638 if( l < 10 )
639 *obuf++ = '0' + l;
640 else
641 *obuf++ = 'a' + l - 10;
642
643 ++ibuf;
644 len--;
645 }
646}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000647
648/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200649 * Allocate and zeroize a buffer.
650 *
651 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
652 *
653 * For convenience, dies if allocation fails.
654 */
Ronald Cronf91c4952020-05-27 16:22:17 +0200655unsigned char *zero_alloc( size_t len )
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200656{
657 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000658 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200659
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200660 p = mbedtls_calloc( 1, actual_len );
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200661 TEST_HELPER_ASSERT( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200662
663 memset( p, 0x00, actual_len );
664
665 return( p );
666}
667
668/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200669 * Allocate and fill a buffer from hex data.
670 *
671 * The buffer is sized exactly as needed. This allows to detect buffer
672 * overruns (including overreads) when running the test suite under valgrind.
673 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200674 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
675 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200676 * For convenience, dies if allocation fails.
677 */
Simon Butcherecff2192018-10-03 16:17:41 +0100678unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200679{
680 unsigned char *obuf;
681
Rich Evans42914452015-02-02 12:09:25 +0000682 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200683
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200684 if( *olen == 0 )
685 return( zero_alloc( *olen ) );
686
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200687 obuf = mbedtls_calloc( 1, *olen );
Gilles Peskine9e23bea2019-06-07 14:52:07 +0200688 TEST_HELPER_ASSERT( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200689
690 (void) unhexify( obuf, ibuf );
691
692 return( obuf );
693}
694
695/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000696 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000697 * Although predictable and often similar on multiple
698 * runs, this does not result in identical random on
699 * each run. So do not use this if the results of a
700 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000701 *
702 * rng_state shall be NULL.
703 */
Ronald Cronf91c4952020-05-27 16:22:17 +0200704int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000705{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200706#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000707 size_t i;
708
Paul Bakker9dcc3222011-03-08 14:16:06 +0000709 if( rng_state != NULL )
710 rng_state = NULL;
711
Paul Bakkera3d195c2011-11-27 21:07:34 +0000712 for( i = 0; i < len; ++i )
713 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200714#else
715 if( rng_state != NULL )
716 rng_state = NULL;
717
718 arc4random_buf( output, len );
719#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000720
721 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000722}
723
724/**
725 * This function only returns zeros
726 *
727 * rng_state shall be NULL.
728 */
Simon Butcherecff2192018-10-03 16:17:41 +0100729int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000730{
731 if( rng_state != NULL )
732 rng_state = NULL;
733
Paul Bakkera3d195c2011-11-27 21:07:34 +0000734 memset( output, 0, len );
735
Paul Bakker9dcc3222011-03-08 14:16:06 +0000736 return( 0 );
737}
738
739typedef struct
740{
741 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000742 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000743} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000744
745/**
746 * This function returns random based on a buffer it receives.
747 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000748 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100749 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000750 * The number of bytes released from the buffer on each call to
751 * the random function is specified by per_call. (Can be between
752 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000753 *
754 * After the buffer is empty it will return rand();
755 */
Simon Butcherecff2192018-10-03 16:17:41 +0100756int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000757{
Paul Bakker997bbd12011-03-13 15:45:42 +0000758 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000759 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000760
761 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000762 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000763
Paul Bakkera3d195c2011-11-27 21:07:34 +0000764 use_len = len;
765 if( len > info->length )
766 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000767
Paul Bakkera3d195c2011-11-27 21:07:34 +0000768 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000769 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000770 memcpy( output, info->buf, use_len );
771 info->buf += use_len;
772 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000773 }
774
Paul Bakkera3d195c2011-11-27 21:07:34 +0000775 if( len - use_len > 0 )
776 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
777
778 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000779}
Paul Bakker997bbd12011-03-13 15:45:42 +0000780
781/**
782 * Info structure for the pseudo random function
783 *
784 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000785 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000786 * State( v0, v1 ) should be set to zero.
787 */
788typedef struct
789{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000790 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000791 uint32_t v0, v1;
792} rnd_pseudo_info;
793
794/**
795 * This function returns random based on a pseudo random function.
796 * This means the results should be identical on all systems.
797 * Pseudo random is based on the XTEA encryption algorithm to
798 * generate pseudorandom.
799 *
800 * rng_state shall be a pointer to a rnd_pseudo_info structure.
801 */
Simon Butcherecff2192018-10-03 16:17:41 +0100802int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000803{
804 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000805 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100806 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000807
808 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000809 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000810
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000811 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000812
813 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000814 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000815 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000816 sum = 0;
817
Paul Bakkera3d195c2011-11-27 21:07:34 +0000818 for( i = 0; i < 32; i++ )
819 {
Rich Evans42914452015-02-02 12:09:25 +0000820 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
821 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000822 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000823 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
824 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000825 }
826
Paul Bakker5c2364c2012-10-01 14:41:15 +0000827 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100828 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000829 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100830 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000831 }
832
Paul Bakkera3d195c2011-11-27 21:07:34 +0000833 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000834}
SimonB0269dad2016-02-17 23:34:30 +0000835
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100836int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100837{
838 int ret = 0;
839 uint32_t i = 0;
840
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500841 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100842 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100843
844 for( i = 0; i < a_len; i++ )
845 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500846 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100847 {
848 ret = -1;
849 break;
850 }
851 }
852 return ret;
853}