blob: 2d1f6922e85d9e51625e4f842182728e2da0ff8c [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 )
Simon Butcher747f5fe2018-12-07 16:53:57 +000029
30#if defined(__GNUC__) && !defined(__clang__)
31#pragma GCC diagnostic ignored "-Wno-uninitialized"
Simon Butchera6463452018-12-06 17:41:56 +000032#endif
33
Simon Butcher747f5fe2018-12-07 16:53:57 +000034#endif /* MBEDTLS_CHECK_PARAMS */
35
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000036#ifdef _MSC_VER
37#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010038typedef UINT8 uint8_t;
39typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000040typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000041#define strncasecmp _strnicmp
42#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000043#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020044#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000045#endif
46
Paul Bakker19343182013-08-16 13:31:10 +020047#include <string.h>
48
Janos Follath8ca53b52016-10-05 10:57:49 +010049#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
50#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000051#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010052#endif
SimonB0269dad2016-02-17 23:34:30 +000053
Azim Khand30ca132017-06-09 04:32:58 +010054/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010055typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010056{
57 uint8_t * x;
58 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010059} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010060
SimonB0269dad2016-02-17 23:34:30 +000061/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010062/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000063
Azim Khan62a5d7d2018-06-29 10:02:54 +010064#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
65#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
66#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010067
Azim Khan62a5d7d2018-06-29 10:02:54 +010068#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
69#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
70#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
71#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
72 Only int, string, binary data
73 and integer expressions are
74 allowed */
75#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
76 build */
SimonB0269dad2016-02-17 23:34:30 +000077
78
79/*----------------------------------------------------------------------------*/
80/* Macros */
81
Simon Butchera6463452018-12-06 17:41:56 +000082#if defined(MBEDTLS_CHECK_PARAMS)
83
84/**
85 * \brief This macro tests the expression passed to it as a test step or
86 * individual test in a test case.
87 *
88 * It allows a library function to return a value and return an error
89 * code that can be tested.
90 *
91 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
92 * callback, MBEDTLS_PARAM_FAIL, will be assumed to be a test failure.
93 *
94 * This macro is not suitable for negative parameter validation tests,
95 * as it assumes the test step will not create an error.
96 *
97 * \param TEST The test expression to be tested.
98 */
99#define TEST_ASSERT( TEST ) \
100 do { \
101 if ( setjmp( param_fail_jmp ) == 0 ) \
102 { \
103 if( ! (TEST) ) \
104 { \
105 test_fail( #TEST, __LINE__, __FILE__ ); \
106 goto exit; \
107 } \
108 } \
109 else \
110 { \
111 test_fail( #TEST, __LINE__, __FILE__ ); \
112 goto exit; \
113 } \
114 memset( param_fail_jmp, 0, sizeof(jmp_buf) ); \
SimonB0269dad2016-02-17 23:34:30 +0000115 } while( 0 )
116
Simon Butchera6463452018-12-06 17:41:56 +0000117/**
118 * \brief This macro tests and individual function call as a test step or
119 * individual test in a test case.
120 *
121 * It does not require a library function to return a value, and cannot
122 tets a return error code that can be tested.
123 *
124 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
125 * callback, MBEDTLS_PARAM_FAIL, will be assumed to be a test failure.
126 *
127 * This macro is not suitable for negative parameter validation tests
128 * as it assumes the test step will not create an error.
129 *
130 * \param TEST The test statement to be executed.
131 */
132#define TEST_FN( TEST ) \
133 do { \
134 if ( setjmp( param_fail_jmp ) == 0 ) \
135 { \
136 TEST; \
137 } \
138 else \
139 { \
140 test_fail( #TEST, __LINE__, __FILE__ ); \
141 goto exit; \
142 } \
143 memset( param_fail_jmp, 0, sizeof(jmp_buf) ); \
144 } while( 0 )
145
146/**
147 * \brief This macro tests the statement passed to it as a test step or
148 * individual test in a test case. The macro assumes the test will fail
149 * and will generate an error.
150 *
151 * It allows a library function to return a value and tests the return
152 * code on return to confirm the given error code was returned.
153 *
154 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
155 * callback, MBEDTLS_PARAM_FAIL, are assumed to indicate the
156 * expected failure, and the test will pass.
157 *
158 * This macro is intended for negative parameter validation tests,
159 * where the failing function may return an error value or call
160 * MBEDTLS_PARAM_FAIL to indicate the error.
161 *
162 * \param PARAM_ERROR_VALUE The expected error code.
163 *
164 * \param TEST The test expression to be tested.
165 */
166#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
167 do { \
168 if ( setjmp( param_fail_jmp ) == 0 ) \
169 { \
170 if( (TEST) != PARAM_ERR_VALUE) \
171 { \
172 test_fail( #TEST, __LINE__, __FILE__ ); \
173 goto exit; \
174 } \
175 } \
176 memset( param_fail_jmp, 0, sizeof(jmp_buf) ); \
177 } while( 0 )
178
179/**
180 * \brief This macro tests the statement passed to it as a test step or
181 * individual test in a test case. The macro assumes the test will fail
182 * and will generate an error.
183 *
184 * It assumes the library function under test cannot return a value and
185 * assumes errors can only be indicated byt calls to
186 * MBEDTLS_PARAM_FAIL.
187 *
188 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
189 * callback, MBEDTLS_PARAM_FAIL, are assumed to indicate the
190 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
191 * can be made.
192 *
193 * This macro is intended for negative parameter validation tests,
194 * where the failing function can only return an error by calling
195 * MBEDTLS_PARAM_FAIL to indicate the error.
196 *
197 * \param TEST The test expression to be tested.
198 */
199#define TEST_INVALID_PARAM( TEST ) \
200 do { \
201 if ( setjmp( param_fail_jmp ) == 0 ) \
202 { \
203 TEST; \
204 test_fail( #TEST, __LINE__, __FILE__ ); \
205 goto exit; \
206 } \
207 memset( param_fail_jmp, 0, sizeof(jmp_buf) ); \
208 } while( 0 )
209
210#else
211
212#define TEST_ASSERT( TEST ) \
213 do { \
214 if( ! (TEST) ) \
215 { \
216 test_fail( #TEST, __LINE__, __FILE__ ); \
217 goto exit; \
218 } \
219 } while( 0 )
220
221#define TEST_FN( TEST ) \
222 do { \
223 TEST; \
224 } while( 0 )
225
226#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
227 do { \
228 if( (TEST) != (PARAM_ERR_VALUE) ) \
229 { \
230 test_fail( #TEST, __LINE__, __FILE__ ); \
231 goto exit; \
232 } \
233 } while( 0 )
234
235#define TEST_INVALID_PARAM( TEST ) \
236 do { \
237 TEST; \
238 } while( 0 )
239
240#endif /* !defined( MBEDTLS_CHECK_PARAMS ) */
241
Rich Evans4c091142015-02-02 12:04:10 +0000242#define assert(a) if( !( a ) ) \
243{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000245 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000247}
248
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000249/*
250 * 32-bit integer manipulation macros (big endian)
251 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000252#ifndef GET_UINT32_BE
253#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000254{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000255 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
256 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
257 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
258 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000259}
260#endif
261
Paul Bakker5c2364c2012-10-01 14:41:15 +0000262#ifndef PUT_UINT32_BE
263#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000264{ \
265 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
266 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
267 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
268 (b)[(i) + 3] = (unsigned char) ( (n) ); \
269}
270#endif
271
SimonB0269dad2016-02-17 23:34:30 +0000272
273/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100274/* Global variables */
275
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100276
277static struct
278{
279 int failed;
280 const char *test;
281 const char *filename;
282 int line_no;
283}
284test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100285
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400286#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurek1152fa82018-04-13 05:15:17 -0400287mbedtls_platform_context platform_ctx;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400288#endif
SimonB8ca7bc42016-04-17 23:24:50 +0100289
Simon Butchera6463452018-12-06 17:41:56 +0000290#if defined(MBEDTLS_CHECK_PARAMS)
291jmp_buf param_fail_jmp;
292#endif
293
SimonB8ca7bc42016-04-17 23:24:50 +0100294/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100295/* Helper flags for complex dependencies */
296
297/* Indicates whether we expect mbedtls_entropy_init
298 * to initialize some strong entropy source. */
299#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
300 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
301 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
302 defined(MBEDTLS_HAVEGE_C) || \
303 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
304 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100305#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100306#endif
307
308
309/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000310/* Helper Functions */
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400311static int platform_setup()
312{
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400313 int ret = 0;
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400314#if defined(MBEDTLS_PLATFORM_C)
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400315 ret = mbedtls_platform_setup( &platform_ctx );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400316#endif /* MBEDTLS_PLATFORM_C */
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400317 return( ret );
Andrzej Kurek32a675f2018-04-13 06:16:04 -0400318}
319
320static void platform_teardown()
321{
322#if defined(MBEDTLS_PLATFORM_C)
323 mbedtls_platform_teardown( &platform_ctx );
324#endif /* MBEDTLS_PLATFORM_C */
325}
SimonB0269dad2016-02-17 23:34:30 +0000326
Simon Butchera6463452018-12-06 17:41:56 +0000327#if defined(MBEDTLS_CHECK_PARAMS)
328void mbedtls_param_failed( char* failure_condition, char* file, int line )
329{
330 (void)failure_condition;
331 (void)file;
332 (void)line;
333
334 longjmp( param_fail_jmp, 1 );
335}
336#endif
337
Janos Follath8ca53b52016-10-05 10:57:49 +0100338#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
339static int redirect_output( FILE** out_stream, const char* path )
340{
341 int stdout_fd = dup( fileno( *out_stream ) );
342
343 if( stdout_fd == -1 )
344 {
345 return -1;
346 }
347
348 fflush( *out_stream );
349 fclose( *out_stream );
350 *out_stream = fopen( path, "w" );
351
352 if( *out_stream == NULL )
353 {
354 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
Rich Evans4c091142015-02-02 12:04:10 +0000380static int 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;
SimonB0269dad2016-02-17 23:34:30 +0000384 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
396 assert( 0 );
397
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
406 assert( 0 );
407
408 *obuf++ = ( c << 4 ) | c2;
409 }
410
411 return len;
412}
413
Rich Evans42914452015-02-02 12:09:25 +0000414static void 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 );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200451 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 */
468static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
469{
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 );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200478 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 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000519static int 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 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000546static int 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 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000592static int 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
Simon Butcherd96924d2016-05-06 00:22:18 +0100626static void test_fail( const char *test, int line_no, const char* filename )
SimonB0269dad2016-02-17 23:34:30 +0000627{
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100628 test_info.failed = 1;
629 test_info.test = test;
630 test_info.line_no = line_no;
631 test_info.filename = filename;
SimonB0269dad2016-02-17 23:34:30 +0000632}
Azim Khan3499a9e2017-05-30 00:06:49 +0100633
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100634int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
Azim Khan3499a9e2017-05-30 00:06:49 +0100635{
636 int ret = 0;
637 uint32_t i = 0;
638
639 if ( a_len != b_len )
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100640 return( -1 );
Azim Khan3499a9e2017-05-30 00:06:49 +0100641
642 for( i = 0; i < a_len; i++ )
643 {
644 if ( a[i] != b[i] )
645 {
646 ret = -1;
647 break;
648 }
649 }
650 return ret;
651}
652