blob: 4c105ed3cedaec1997d7adf32a46c44bead530b6 [file] [log] [blame]
Azim Khanec024482017-05-09 17:20:21 +01001#line 2 "suites/helpers.function"
SimonB0269dad2016-02-17 23:34:30 +00002/*----------------------------------------------------------------------------*/
3/* Headers */
4
Simon Butcheredb7fd92016-05-17 13:35:51 +01005#include <stdlib.h>
6
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00008#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +02009#else
Rich Evans00ab4702015-02-06 13:43:58 +000010#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010012#define mbedtls_snprintf snprintf
13#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#define mbedtls_free free
15#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010016#define mbedtls_time time
17#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010018#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
19#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020020#endif
21
SimonB0269dad2016-02-17 23:34:30 +000022#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
23#include "mbedtls/memory_buffer_alloc.h"
24#endif
25
Simon Butchera6463452018-12-06 17:41:56 +000026#if defined(MBEDTLS_CHECK_PARAMS)
27#include <setjmp.h>
28#define MBEDTLS_PARAM_FAILED(x) mbedtls_param_failed( #x )
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
73
74/*----------------------------------------------------------------------------*/
75/* Macros */
76
Simon Butchera6463452018-12-06 17:41:56 +000077#if defined(MBEDTLS_CHECK_PARAMS)
78
79/**
80 * \brief This macro tests the expression passed to it as a test step or
81 * individual test in a test case.
82 *
83 * It allows a library function to return a value and return an error
84 * code that can be tested.
85 *
86 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
87 * callback, MBEDTLS_PARAM_FAIL, will be assumed to be a test failure.
88 *
89 * This macro is not suitable for negative parameter validation tests,
90 * as it assumes the test step will not create an error.
91 *
92 * \param TEST The test expression to be tested.
93 */
94#define TEST_ASSERT( TEST ) \
95 do { \
96 if ( setjmp( param_fail_jmp ) == 0 ) \
97 { \
98 if( ! (TEST) ) \
99 { \
100 test_fail( #TEST, __LINE__, __FILE__ ); \
101 goto exit; \
102 } \
103 } \
104 else \
105 { \
106 test_fail( #TEST, __LINE__, __FILE__ ); \
107 goto exit; \
108 } \
109 memset( param_fail_jmp, 0, sizeof(jmp_buf) ); \
SimonB0269dad2016-02-17 23:34:30 +0000110 } while( 0 )
111
Simon Butchera6463452018-12-06 17:41:56 +0000112/**
113 * \brief This macro tests and individual function call as a test step or
114 * individual test in a test case.
115 *
116 * It does not require a library function to return a value, and cannot
117 tets a return error code that can be tested.
118 *
119 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
120 * callback, MBEDTLS_PARAM_FAIL, will be assumed to be a test failure.
121 *
122 * This macro is not suitable for negative parameter validation tests
123 * as it assumes the test step will not create an error.
124 *
125 * \param TEST The test statement to be executed.
126 */
127#define TEST_FN( TEST ) \
128 do { \
129 if ( setjmp( param_fail_jmp ) == 0 ) \
130 { \
131 TEST; \
132 } \
133 else \
134 { \
135 test_fail( #TEST, __LINE__, __FILE__ ); \
136 goto exit; \
137 } \
138 memset( param_fail_jmp, 0, sizeof(jmp_buf) ); \
139 } while( 0 )
140
141/**
142 * \brief This macro tests the statement passed to it as a test step or
143 * individual test in a test case. The macro assumes the test will fail
144 * and will generate an error.
145 *
146 * It allows a library function to return a value and tests the return
147 * code on return to confirm the given error code was returned.
148 *
149 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
150 * callback, MBEDTLS_PARAM_FAIL, are assumed to indicate the
151 * expected failure, and the test will pass.
152 *
153 * This macro is intended for negative parameter validation tests,
154 * where the failing function may return an error value or call
155 * MBEDTLS_PARAM_FAIL to indicate the error.
156 *
157 * \param PARAM_ERROR_VALUE The expected error code.
158 *
159 * \param TEST The test expression to be tested.
160 */
161#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
162 do { \
163 if ( setjmp( param_fail_jmp ) == 0 ) \
164 { \
165 if( (TEST) != PARAM_ERR_VALUE) \
166 { \
167 test_fail( #TEST, __LINE__, __FILE__ ); \
168 goto exit; \
169 } \
170 } \
171 memset( param_fail_jmp, 0, sizeof(jmp_buf) ); \
172 } while( 0 )
173
174/**
175 * \brief This macro tests the statement passed to it as a test step or
176 * individual test in a test case. The macro assumes the test will fail
177 * and will generate an error.
178 *
179 * It assumes the library function under test cannot return a value and
180 * assumes errors can only be indicated byt calls to
181 * MBEDTLS_PARAM_FAIL.
182 *
183 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
184 * callback, MBEDTLS_PARAM_FAIL, are assumed to indicate the
185 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
186 * can be made.
187 *
188 * This macro is intended for negative parameter validation tests,
189 * where the failing function can only return an error by calling
190 * MBEDTLS_PARAM_FAIL to indicate the error.
191 *
192 * \param TEST The test expression to be tested.
193 */
194#define TEST_INVALID_PARAM( TEST ) \
195 do { \
196 if ( setjmp( param_fail_jmp ) == 0 ) \
197 { \
198 TEST; \
199 test_fail( #TEST, __LINE__, __FILE__ ); \
200 goto exit; \
201 } \
202 memset( param_fail_jmp, 0, sizeof(jmp_buf) ); \
203 } while( 0 )
204
205#else
206
207#define TEST_ASSERT( TEST ) \
208 do { \
209 if( ! (TEST) ) \
210 { \
211 test_fail( #TEST, __LINE__, __FILE__ ); \
212 goto exit; \
213 } \
214 } while( 0 )
215
216#define TEST_FN( TEST ) \
217 do { \
218 TEST; \
219 } while( 0 )
220
221#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
222 do { \
223 if( (TEST) != (PARAM_ERR_VALUE) ) \
224 { \
225 test_fail( #TEST, __LINE__, __FILE__ ); \
226 goto exit; \
227 } \
228 } while( 0 )
229
230#define TEST_INVALID_PARAM( TEST ) \
231 do { \
232 TEST; \
233 } while( 0 )
234
235#endif /* !defined( MBEDTLS_CHECK_PARAMS ) */
236
Rich Evans4c091142015-02-02 12:04:10 +0000237#define assert(a) if( !( a ) ) \
238{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000240 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000242}
243
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000244/*
245 * 32-bit integer manipulation macros (big endian)
246 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000247#ifndef GET_UINT32_BE
248#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000249{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000250 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
251 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
252 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
253 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000254}
255#endif
256
Paul Bakker5c2364c2012-10-01 14:41:15 +0000257#ifndef PUT_UINT32_BE
258#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000259{ \
260 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
261 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
262 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
263 (b)[(i) + 3] = (unsigned char) ( (n) ); \
264}
265#endif
266
SimonB0269dad2016-02-17 23:34:30 +0000267
268/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100269/* Global variables */
270
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100271
272static struct
273{
274 int failed;
275 const char *test;
276 const char *filename;
277 int line_no;
278}
279test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100280
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400281#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurek1152fa82018-04-13 05:15:17 -0400282mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400283#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100284
Simon Butchera6463452018-12-06 17:41:56 +0000285#if defined(MBEDTLS_CHECK_PARAMS)
286jmp_buf param_fail_jmp;
287#endif
288
SimonB8ca7bc42016-04-17 23:24:50 +0100289/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100290/* Helper flags for complex dependencies */
291
292/* Indicates whether we expect mbedtls_entropy_init
293 * to initialize some strong entropy source. */
294#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
295 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
296 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
297 defined(MBEDTLS_HAVEGE_C) || \
298 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
299 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100300#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100301#endif
302
303
304/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000305/* Helper Functions */
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400306static int platform_setup()
307{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400308 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400309#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400310 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400311#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400312 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400313}
314
315static void platform_teardown()
316{
317#if defined(MBEDTLS_PLATFORM_C)
318 mbedtls_platform_teardown( &platform_ctx );
319#endif /* MBEDTLS_PLATFORM_C */
320}
SimonB0269dad2016-02-17 23:34:30 +0000321
Simon Butchera6463452018-12-06 17:41:56 +0000322#if defined(MBEDTLS_CHECK_PARAMS)
323void mbedtls_param_failed( char* failure_condition, char* file, int line )
324{
325 (void)failure_condition;
326 (void)file;
327 (void)line;
328
329 longjmp( param_fail_jmp, 1 );
330}
331#endif
332
Janos Follath8ca53b52016-10-05 10:57:49 +0100333#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
334static int redirect_output( FILE** out_stream, const char* path )
335{
336 int stdout_fd = dup( fileno( *out_stream ) );
337
338 if( stdout_fd == -1 )
339 {
340 return -1;
341 }
342
343 fflush( *out_stream );
344 fclose( *out_stream );
345 *out_stream = fopen( path, "w" );
346
347 if( *out_stream == NULL )
348 {
349 return -1;
350 }
351
352 return stdout_fd;
353}
354
355static int restore_output( FILE** out_stream, int old_fd )
356{
357 fflush( *out_stream );
358 fclose( *out_stream );
359
360 *out_stream = fdopen( old_fd, "w" );
361 if( *out_stream == NULL )
362 {
363 return -1;
364 }
365
366 return 0;
367}
Simon Butchere0192962016-10-12 23:07:30 +0100368
Janos Follathe709f7c2016-10-13 11:26:29 +0100369static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100370{
Janos Follathe709f7c2016-10-13 11:26:29 +0100371 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100372}
Janos Follath8ca53b52016-10-05 10:57:49 +0100373#endif /* __unix__ || __APPLE__ __MACH__ */
374
Rich Evans4c091142015-02-02 12:04:10 +0000375static int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000376{
377 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000378 int len = strlen( ibuf ) / 2;
SimonB0269dad2016-02-17 23:34:30 +0000379 assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000380
Rich Evans4c091142015-02-02 12:04:10 +0000381 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000382 {
383 c = *ibuf++;
384 if( c >= '0' && c <= '9' )
385 c -= '0';
386 else if( c >= 'a' && c <= 'f' )
387 c -= 'a' - 10;
388 else if( c >= 'A' && c <= 'F' )
389 c -= 'A' - 10;
390 else
391 assert( 0 );
392
393 c2 = *ibuf++;
394 if( c2 >= '0' && c2 <= '9' )
395 c2 -= '0';
396 else if( c2 >= 'a' && c2 <= 'f' )
397 c2 -= 'a' - 10;
398 else if( c2 >= 'A' && c2 <= 'F' )
399 c2 -= 'A' - 10;
400 else
401 assert( 0 );
402
403 *obuf++ = ( c << 4 ) | c2;
404 }
405
406 return len;
407}
408
Rich Evans42914452015-02-02 12:09:25 +0000409static void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000410{
411 unsigned char l, h;
412
Rich Evans42914452015-02-02 12:09:25 +0000413 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000414 {
Rich Evans42914452015-02-02 12:09:25 +0000415 h = *ibuf / 16;
416 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000417
418 if( h < 10 )
419 *obuf++ = '0' + h;
420 else
421 *obuf++ = 'a' + h - 10;
422
423 if( l < 10 )
424 *obuf++ = '0' + l;
425 else
426 *obuf++ = 'a' + l - 10;
427
428 ++ibuf;
429 len--;
430 }
431}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000432
433/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200434 * Allocate and zeroize a buffer.
435 *
436 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
437 *
438 * For convenience, dies if allocation fails.
439 */
440static unsigned char *zero_alloc( size_t len )
441{
442 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000443 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200444
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200445 p = mbedtls_calloc( 1, actual_len );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200446 assert( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200447
448 memset( p, 0x00, actual_len );
449
450 return( p );
451}
452
453/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200454 * Allocate and fill a buffer from hex data.
455 *
456 * The buffer is sized exactly as needed. This allows to detect buffer
457 * overruns (including overreads) when running the test suite under valgrind.
458 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200459 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
460 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200461 * For convenience, dies if allocation fails.
462 */
463static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
464{
465 unsigned char *obuf;
466
Rich Evans42914452015-02-02 12:09:25 +0000467 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200468
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200469 if( *olen == 0 )
470 return( zero_alloc( *olen ) );
471
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200472 obuf = mbedtls_calloc( 1, *olen );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200473 assert( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200474
475 (void) unhexify( obuf, ibuf );
476
477 return( obuf );
478}
479
480/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000481 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000482 * Although predictable and often similar on multiple
483 * runs, this does not result in identical random on
484 * each run. So do not use this if the results of a
485 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000486 *
487 * rng_state shall be NULL.
488 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000489static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000490{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200491#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000492 size_t i;
493
Paul Bakker9dcc3222011-03-08 14:16:06 +0000494 if( rng_state != NULL )
495 rng_state = NULL;
496
Paul Bakkera3d195c2011-11-27 21:07:34 +0000497 for( i = 0; i < len; ++i )
498 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200499#else
500 if( rng_state != NULL )
501 rng_state = NULL;
502
503 arc4random_buf( output, len );
504#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000505
506 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000507}
508
509/**
510 * This function only returns zeros
511 *
512 * rng_state shall be NULL.
513 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000514static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000515{
516 if( rng_state != NULL )
517 rng_state = NULL;
518
Paul Bakkera3d195c2011-11-27 21:07:34 +0000519 memset( output, 0, len );
520
Paul Bakker9dcc3222011-03-08 14:16:06 +0000521 return( 0 );
522}
523
524typedef struct
525{
526 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000527 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000528} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000529
530/**
531 * This function returns random based on a buffer it receives.
532 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000533 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100534 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000535 * The number of bytes released from the buffer on each call to
536 * the random function is specified by per_call. (Can be between
537 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000538 *
539 * After the buffer is empty it will return rand();
540 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000541static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000542{
Paul Bakker997bbd12011-03-13 15:45:42 +0000543 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000544 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000545
546 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000547 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000548
Paul Bakkera3d195c2011-11-27 21:07:34 +0000549 use_len = len;
550 if( len > info->length )
551 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000552
Paul Bakkera3d195c2011-11-27 21:07:34 +0000553 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000554 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000555 memcpy( output, info->buf, use_len );
556 info->buf += use_len;
557 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000558 }
559
Paul Bakkera3d195c2011-11-27 21:07:34 +0000560 if( len - use_len > 0 )
561 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
562
563 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000564}
Paul Bakker997bbd12011-03-13 15:45:42 +0000565
566/**
567 * Info structure for the pseudo random function
568 *
569 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000570 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000571 * State( v0, v1 ) should be set to zero.
572 */
573typedef struct
574{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000575 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000576 uint32_t v0, v1;
577} rnd_pseudo_info;
578
579/**
580 * This function returns random based on a pseudo random function.
581 * This means the results should be identical on all systems.
582 * Pseudo random is based on the XTEA encryption algorithm to
583 * generate pseudorandom.
584 *
585 * rng_state shall be a pointer to a rnd_pseudo_info structure.
586 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000587static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000588{
589 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000590 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100591 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000592
593 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000594 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000595
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000596 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000597
598 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000599 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000600 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000601 sum = 0;
602
Paul Bakkera3d195c2011-11-27 21:07:34 +0000603 for( i = 0; i < 32; i++ )
604 {
Rich Evans42914452015-02-02 12:09:25 +0000605 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
606 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000607 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000608 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
609 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000610 }
611
Paul Bakker5c2364c2012-10-01 14:41:15 +0000612 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100613 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000614 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100615 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000616 }
617
Paul Bakkera3d195c2011-11-27 21:07:34 +0000618 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000619}
SimonB0269dad2016-02-17 23:34:30 +0000620
Simon Butcherd96924d2016-05-06 00:22:18 +0100621static void test_fail( const char *test, int line_no, const char* filename )
SimonB0269dad2016-02-17 23:34:30 +0000622{
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100623 test_info.failed = 1;
624 test_info.test = test;
625 test_info.line_no = line_no;
626 test_info.filename = filename;
SimonB0269dad2016-02-17 23:34:30 +0000627}
Azim Khan3499a9e2017-05-30 00:06:49 +0100628
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100629int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100630{
631 int ret = 0;
632 uint32_t i = 0;
633
634 if ( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100635 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100636
637 for( i = 0; i < a_len; i++ )
638 {
639 if ( a[i] != b[i] )
640 {
641 ret = -1;
642 break;
643 }
644 }
645 return ret;
646}
647