blob: cf7a1c51bfb1b43dce0ee73300cb86d152b18a47 [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.
89 *
90 * It allows a library function to return a value and return an error
91 * code that can be tested.
92 *
93 * 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.
96 *
97 * This macro is not suitable for negative parameter validation tests,
98 * as it assumes the test step will not create an error.
99 *
Andrzej Kurek1b20be52019-02-15 10:13:35 -0500100 * 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.
109 */
110#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
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500119#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
120/**
121 * \brief This macro tests the statement passed to it as a test step or
122 * individual test in a test case. The macro assumes the test will fail
123 * and will generate an error.
124 *
125 * It allows a library function to return a value and tests the return
126 * code on return to confirm the given error code was returned.
127 *
128 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
129 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
130 * expected failure, and the test will pass.
131 *
132 * This macro is intended for negative parameter validation tests,
133 * where the failing function may return an error value or call
134 * MBEDTLS_PARAM_FAILED() to indicate the error.
135 *
136 * \param PARAM_ERROR_VALUE The expected error code.
137 *
138 * \param TEST The test expression to be tested.
139 */
140#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
141 do { \
142 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
143 if( (TEST) != (PARAM_ERR_VALUE) || \
144 test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
145 { \
146 test_fail( #TEST, __LINE__, __FILE__ ); \
147 goto exit; \
148 } \
149 } while( 0 )
150
151/**
152 * \brief This macro tests the statement passed to it as a test step or
153 * individual test in a test case. The macro assumes the test will fail
154 * and will generate an error.
155 *
156 * It assumes the library function under test cannot return a value and
157 * assumes errors can only be indicated byt calls to
158 * MBEDTLS_PARAM_FAILED().
159 *
160 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
161 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
162 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
163 * can be made.
164 *
165 * This macro is intended for negative parameter validation tests,
166 * where the failing function can only return an error by calling
167 * MBEDTLS_PARAM_FAILED() to indicate the error.
168 *
169 * \param TEST The test expression to be tested.
170 */
171#define TEST_INVALID_PARAM( TEST ) \
172 do { \
173 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
174 if( setjmp( param_fail_jmp ) == 0 ) \
175 { \
176 TEST; \
177 test_fail( #TEST, __LINE__, __FILE__ ); \
178 goto exit; \
179 } \
180 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
181 } while( 0 )
182#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
183
184/**
185 * \brief This macro tests the statement passed to it as a test step or
186 * individual test in a test case. The macro assumes the test will not fail.
187 *
188 * It assumes the library function under test cannot return a value and
189 * assumes errors can only be indicated by calls to
190 * MBEDTLS_PARAM_FAILED().
191 *
192 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
193 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
194 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
195 * can be made.
196 *
197 * This macro is intended to test that functions returning void
198 * accept all of the parameter values they're supposed to accept - eg
199 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
200 * that's allowed to be NULL happens to be NULL.
201 *
202 * Note: for functions that return something other that void,
203 * checking that they accept all the parameters they're supposed to
204 * accept is best done by using TEST_ASSERT() and checking the return
205 * value as well.
206 *
207 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
208 * disabled, as it makes sense to check that the functions accept all
209 * legal values even if this option is disabled - only in that case,
210 * the test is more about whether the function segfaults than about
211 * whether it invokes MBEDTLS_PARAM_FAILED().
212 *
213 * \param TEST The test expression to be tested.
214 */
215#define TEST_VALID_PARAM( TEST ) \
216 TEST_ASSERT( ( TEST, 1 ) );
217
Gilles Peskinee7655df2019-06-07 14:52:07 +0200218#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
Rich Evans4c091142015-02-02 12:04:10 +0000219{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000221 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000223}
224
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000225/*
226 * 32-bit integer manipulation macros (big endian)
227 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000228#ifndef GET_UINT32_BE
229#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000230{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000231 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
232 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
233 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
234 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000235}
236#endif
237
Paul Bakker5c2364c2012-10-01 14:41:15 +0000238#ifndef PUT_UINT32_BE
239#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000240{ \
241 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
242 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
243 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
244 (b)[(i) + 3] = (unsigned char) ( (n) ); \
245}
246#endif
247
SimonB0269dad2016-02-17 23:34:30 +0000248
249/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100250/* Global variables */
251
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100252static struct
253{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500254 paramfail_test_state_t paramfail_test_state;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100255 int failed;
256 const char *test;
257 const char *filename;
258 int line_no;
259}
260test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100261
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400262#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurek1152fa82018-04-13 05:15:17 -0400263mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400264#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100265
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500266#if defined(MBEDTLS_CHECK_PARAMS)
267jmp_buf param_fail_jmp;
268jmp_buf jmp_tmp;
269#endif
270
SimonB8ca7bc42016-04-17 23:24:50 +0100271/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100272/* Helper flags for complex dependencies */
273
274/* Indicates whether we expect mbedtls_entropy_init
275 * to initialize some strong entropy source. */
276#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
277 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
278 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
279 defined(MBEDTLS_HAVEGE_C) || \
280 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
281 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100282#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100283#endif
284
285
286/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000287/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288
Simon Butcherecff2192018-10-03 16:17:41 +0100289void test_fail( const char *test, int line_no, const char* filename )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500290{
291 test_info.failed = 1;
292 test_info.test = test;
293 test_info.line_no = line_no;
294 test_info.filename = filename;
295}
296
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400297static int platform_setup()
298{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400299 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400300#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400301 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400302#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400303 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400304}
305
306static void platform_teardown()
307{
308#if defined(MBEDTLS_PLATFORM_C)
309 mbedtls_platform_teardown( &platform_ctx );
310#endif /* MBEDTLS_PLATFORM_C */
311}
SimonB0269dad2016-02-17 23:34:30 +0000312
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500313#if defined(MBEDTLS_CHECK_PARAMS)
314void mbedtls_param_failed( const char *failure_condition,
315 const char *file,
316 int line )
317{
318 /* If we are testing the callback function... */
319 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
320 {
321 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
322 }
323 else
324 {
325 /* ...else we treat this as an error */
326
327 /* Record the location of the failure, but not as a failure yet, in case
328 * it was part of the test */
329 test_fail( failure_condition, line, file );
330 test_info.failed = 0;
331
332 longjmp( param_fail_jmp, 1 );
333 }
334}
335#endif
336
Janos Follath8ca53b52016-10-05 10:57:49 +0100337#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
338static int redirect_output( FILE** out_stream, const char* path )
339{
340 int stdout_fd = dup( fileno( *out_stream ) );
341
342 if( stdout_fd == -1 )
343 {
344 return -1;
345 }
346
347 fflush( *out_stream );
348 fclose( *out_stream );
349 *out_stream = fopen( path, "w" );
350
351 if( *out_stream == NULL )
352 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500353 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100354 return -1;
355 }
356
357 return stdout_fd;
358}
359
360static int restore_output( FILE** out_stream, int old_fd )
361{
362 fflush( *out_stream );
363 fclose( *out_stream );
364
365 *out_stream = fdopen( old_fd, "w" );
366 if( *out_stream == NULL )
367 {
368 return -1;
369 }
370
371 return 0;
372}
Simon Butchere0192962016-10-12 23:07:30 +0100373
Janos Follathe709f7c2016-10-13 11:26:29 +0100374static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100375{
Janos Follathe709f7c2016-10-13 11:26:29 +0100376 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100377}
Janos Follath8ca53b52016-10-05 10:57:49 +0100378#endif /* __unix__ || __APPLE__ __MACH__ */
379
Simon Butcherecff2192018-10-03 16:17:41 +0100380int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000381{
382 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000383 int len = strlen( ibuf ) / 2;
Gilles Peskinee7655df2019-06-07 14:52:07 +0200384 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000385
Rich Evans4c091142015-02-02 12:04:10 +0000386 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000387 {
388 c = *ibuf++;
389 if( c >= '0' && c <= '9' )
390 c -= '0';
391 else if( c >= 'a' && c <= 'f' )
392 c -= 'a' - 10;
393 else if( c >= 'A' && c <= 'F' )
394 c -= 'A' - 10;
395 else
Gilles Peskinee7655df2019-06-07 14:52:07 +0200396 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000397
398 c2 = *ibuf++;
399 if( c2 >= '0' && c2 <= '9' )
400 c2 -= '0';
401 else if( c2 >= 'a' && c2 <= 'f' )
402 c2 -= 'a' - 10;
403 else if( c2 >= 'A' && c2 <= 'F' )
404 c2 -= 'A' - 10;
405 else
Gilles Peskinee7655df2019-06-07 14:52:07 +0200406 TEST_HELPER_ASSERT( 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000407
408 *obuf++ = ( c << 4 ) | c2;
409 }
410
411 return len;
412}
413
Simon Butcherecff2192018-10-03 16:17:41 +0100414void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000415{
416 unsigned char l, h;
417
Rich Evans42914452015-02-02 12:09:25 +0000418 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000419 {
Rich Evans42914452015-02-02 12:09:25 +0000420 h = *ibuf / 16;
421 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000422
423 if( h < 10 )
424 *obuf++ = '0' + h;
425 else
426 *obuf++ = 'a' + h - 10;
427
428 if( l < 10 )
429 *obuf++ = '0' + l;
430 else
431 *obuf++ = 'a' + l - 10;
432
433 ++ibuf;
434 len--;
435 }
436}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000437
438/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200439 * Allocate and zeroize a buffer.
440 *
441 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
442 *
443 * For convenience, dies if allocation fails.
444 */
445static unsigned char *zero_alloc( size_t len )
446{
447 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000448 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200449
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200450 p = mbedtls_calloc( 1, actual_len );
Gilles Peskinee7655df2019-06-07 14:52:07 +0200451 TEST_HELPER_ASSERT( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200452
453 memset( p, 0x00, actual_len );
454
455 return( p );
456}
457
458/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200459 * Allocate and fill a buffer from hex data.
460 *
461 * The buffer is sized exactly as needed. This allows to detect buffer
462 * overruns (including overreads) when running the test suite under valgrind.
463 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200464 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
465 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200466 * For convenience, dies if allocation fails.
467 */
Simon Butcherecff2192018-10-03 16:17:41 +0100468unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200469{
470 unsigned char *obuf;
471
Rich Evans42914452015-02-02 12:09:25 +0000472 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200473
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200474 if( *olen == 0 )
475 return( zero_alloc( *olen ) );
476
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200477 obuf = mbedtls_calloc( 1, *olen );
Gilles Peskinee7655df2019-06-07 14:52:07 +0200478 TEST_HELPER_ASSERT( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200479
480 (void) unhexify( obuf, ibuf );
481
482 return( obuf );
483}
484
485/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000486 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000487 * Although predictable and often similar on multiple
488 * runs, this does not result in identical random on
489 * each run. So do not use this if the results of a
490 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000491 *
492 * rng_state shall be NULL.
493 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000494static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000495{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200496#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000497 size_t i;
498
Paul Bakker9dcc3222011-03-08 14:16:06 +0000499 if( rng_state != NULL )
500 rng_state = NULL;
501
Paul Bakkera3d195c2011-11-27 21:07:34 +0000502 for( i = 0; i < len; ++i )
503 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200504#else
505 if( rng_state != NULL )
506 rng_state = NULL;
507
508 arc4random_buf( output, len );
509#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000510
511 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000512}
513
514/**
515 * This function only returns zeros
516 *
517 * rng_state shall be NULL.
518 */
Simon Butcherecff2192018-10-03 16:17:41 +0100519int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000520{
521 if( rng_state != NULL )
522 rng_state = NULL;
523
Paul Bakkera3d195c2011-11-27 21:07:34 +0000524 memset( output, 0, len );
525
Paul Bakker9dcc3222011-03-08 14:16:06 +0000526 return( 0 );
527}
528
529typedef struct
530{
531 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000532 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000533} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000534
535/**
536 * This function returns random based on a buffer it receives.
537 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000538 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100539 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000540 * The number of bytes released from the buffer on each call to
541 * the random function is specified by per_call. (Can be between
542 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000543 *
544 * After the buffer is empty it will return rand();
545 */
Simon Butcherecff2192018-10-03 16:17:41 +0100546int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000547{
Paul Bakker997bbd12011-03-13 15:45:42 +0000548 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000549 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000550
551 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000552 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000553
Paul Bakkera3d195c2011-11-27 21:07:34 +0000554 use_len = len;
555 if( len > info->length )
556 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000557
Paul Bakkera3d195c2011-11-27 21:07:34 +0000558 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000559 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000560 memcpy( output, info->buf, use_len );
561 info->buf += use_len;
562 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000563 }
564
Paul Bakkera3d195c2011-11-27 21:07:34 +0000565 if( len - use_len > 0 )
566 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
567
568 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000569}
Paul Bakker997bbd12011-03-13 15:45:42 +0000570
571/**
572 * Info structure for the pseudo random function
573 *
574 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000575 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000576 * State( v0, v1 ) should be set to zero.
577 */
578typedef struct
579{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000580 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000581 uint32_t v0, v1;
582} rnd_pseudo_info;
583
584/**
585 * This function returns random based on a pseudo random function.
586 * This means the results should be identical on all systems.
587 * Pseudo random is based on the XTEA encryption algorithm to
588 * generate pseudorandom.
589 *
590 * rng_state shall be a pointer to a rnd_pseudo_info structure.
591 */
Simon Butcherecff2192018-10-03 16:17:41 +0100592int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000593{
594 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000595 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100596 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000597
598 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000599 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000600
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000601 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000602
603 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000604 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000605 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000606 sum = 0;
607
Paul Bakkera3d195c2011-11-27 21:07:34 +0000608 for( i = 0; i < 32; i++ )
609 {
Rich Evans42914452015-02-02 12:09:25 +0000610 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
611 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000612 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000613 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
614 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000615 }
616
Paul Bakker5c2364c2012-10-01 14:41:15 +0000617 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100618 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000619 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100620 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000621 }
622
Paul Bakkera3d195c2011-11-27 21:07:34 +0000623 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000624}
SimonB0269dad2016-02-17 23:34:30 +0000625
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100626int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100627{
628 int ret = 0;
629 uint32_t i = 0;
630
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500631 if( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100632 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100633
634 for( i = 0; i < a_len; i++ )
635 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500636 if( a[i] != b[i] )
Azim Khan3499a9e2017-05-30 00:06:49 +0100637 {
638 ret = -1;
639 break;
640 }
641 }
642 return ret;
643}