2017-05-09 18:20:21 +02:00
|
|
|
#line 2 "suites/helpers.function"
|
2016-02-18 00:34:30 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Headers */
|
|
|
|
|
2016-05-17 14:35:51 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2014-06-06 14:48:09 +02:00
|
|
|
#else
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdio.h>
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_fprintf fprintf
|
2016-09-30 14:11:29 +02:00
|
|
|
#define mbedtls_snprintf snprintf
|
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_free free
|
|
|
|
#define mbedtls_exit exit
|
2016-04-27 14:35:37 +02:00
|
|
|
#define mbedtls_time time
|
|
|
|
#define mbedtls_time_t time_t
|
2016-04-18 19:18:48 +02:00
|
|
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
|
|
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
2014-06-06 14:48:09 +02:00
|
|
|
#endif
|
|
|
|
|
2016-02-18 00:34:30 +01:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
|
|
|
#include "mbedtls/memory_buffer_alloc.h"
|
|
|
|
#endif
|
|
|
|
|
2011-03-13 17:57:25 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <basetsd.h>
|
2018-06-22 12:34:33 +02:00
|
|
|
typedef UINT8 uint8_t;
|
|
|
|
typedef INT32 int32_t;
|
2011-03-13 17:57:25 +01:00
|
|
|
typedef UINT32 uint32_t;
|
2015-11-14 14:09:01 +01:00
|
|
|
#define strncasecmp _strnicmp
|
|
|
|
#define strcasecmp _stricmp
|
2011-03-13 17:57:25 +01:00
|
|
|
#else
|
2015-06-22 19:21:23 +02:00
|
|
|
#include <stdint.h>
|
2011-03-13 17:57:25 +01:00
|
|
|
#endif
|
|
|
|
|
2013-08-16 13:31:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-10-05 11:57:49 +02:00
|
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
#include <unistd.h>
|
2017-12-05 13:08:15 +01:00
|
|
|
#include <strings.h>
|
2016-10-05 11:57:49 +02:00
|
|
|
#endif
|
2016-02-18 00:34:30 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
/* Type for Hex parameters */
|
2018-06-29 12:05:32 +02:00
|
|
|
typedef struct data_tag
|
2017-06-09 05:32:58 +02:00
|
|
|
{
|
|
|
|
uint8_t * x;
|
|
|
|
uint32_t len;
|
2018-06-29 12:05:32 +02:00
|
|
|
} data_t;
|
2017-06-09 05:32:58 +02:00
|
|
|
|
2016-02-18 00:34:30 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-06-29 11:02:54 +02:00
|
|
|
/* Status and error constants */
|
2016-02-18 00:34:30 +01:00
|
|
|
|
2018-06-29 11:02:54 +02:00
|
|
|
#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
|
|
|
|
#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
|
|
|
|
#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
|
2016-04-18 00:24:50 +02:00
|
|
|
|
2018-06-29 11:02:54 +02:00
|
|
|
#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
|
|
|
|
#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
|
|
|
|
#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
|
|
|
|
#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
|
|
|
|
Only int, string, binary data
|
|
|
|
and integer expressions are
|
|
|
|
allowed */
|
|
|
|
#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
|
|
|
|
build */
|
2016-02-18 00:34:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Macros */
|
|
|
|
|
2018-09-27 13:51:25 +02:00
|
|
|
/** Evaluate an expression and fail the test case if it is false.
|
|
|
|
*
|
|
|
|
* Failing the test means:
|
|
|
|
* - Mark this test case as failed.
|
|
|
|
* - Print a message identifying the failure.
|
|
|
|
* - Jump to the \c exit label.
|
|
|
|
*
|
|
|
|
* This macro expands to an instruction, not an expression.
|
|
|
|
* It may jump to the \c exit label.
|
|
|
|
*
|
|
|
|
* \param TEST The expression to evaluate.
|
|
|
|
*/
|
2016-02-18 00:34:30 +01:00
|
|
|
#define TEST_ASSERT( TEST ) \
|
|
|
|
do { \
|
|
|
|
if( ! (TEST) ) \
|
|
|
|
{ \
|
2016-05-02 22:32:44 +02:00
|
|
|
test_fail( #TEST, __LINE__, __FILE__ ); \
|
2016-02-18 00:34:30 +01:00
|
|
|
goto exit; \
|
|
|
|
} \
|
|
|
|
} while( 0 )
|
|
|
|
|
2018-12-17 23:26:52 +01:00
|
|
|
/** Evaluate two expressions and fail the test case if they have different
|
|
|
|
* values.
|
|
|
|
*
|
|
|
|
* \param expr1 An expression to evaluate.
|
|
|
|
* \param expr2 The expected value of \p expr1. This can be any
|
|
|
|
* expression, but it is typically a constant.
|
|
|
|
*/
|
|
|
|
#define TEST_EQUAL( expr1, expr2 ) \
|
|
|
|
TEST_ASSERT( ( expr1 ) == ( expr2 ) )
|
|
|
|
|
2018-12-17 23:26:01 +01:00
|
|
|
/** Evaluate an expression and fail the test case if it returns an error.
|
|
|
|
*
|
|
|
|
* \param expr The expression to evaluate. This is typically a call
|
|
|
|
* to a \c psa_xxx function that returns a value of type
|
|
|
|
* #psa_status_t.
|
|
|
|
*/
|
2018-12-17 23:26:52 +01:00
|
|
|
#define PSA_ASSERT( expr ) TEST_EQUAL( ( expr ), PSA_SUCCESS )
|
2018-12-17 23:26:01 +01:00
|
|
|
|
2018-09-27 13:52:16 +02:00
|
|
|
/** Allocate memory dynamically and fail the test case if this fails.
|
|
|
|
*
|
|
|
|
* You must set \p pointer to \c NULL before calling this macro and
|
|
|
|
* put `mbedtls_free( pointer )` in the test's cleanup code.
|
|
|
|
*
|
2018-11-30 18:51:45 +01:00
|
|
|
* If \p length is zero, the resulting \p pointer will be \c NULL.
|
2018-09-27 13:52:16 +02:00
|
|
|
* This is usually what we want in tests since API functions are
|
|
|
|
* supposed to accept null pointers when a buffer size is zero.
|
|
|
|
*
|
|
|
|
* This macro expands to an instruction, not an expression.
|
|
|
|
* It may jump to the \c exit label.
|
|
|
|
*
|
|
|
|
* \param pointer An lvalue where the address of the allocated buffer
|
|
|
|
* will be stored.
|
|
|
|
* This expression may be evaluated multiple times.
|
2018-11-30 18:51:45 +01:00
|
|
|
* \param length Number of elements to allocate.
|
2018-09-27 13:52:16 +02:00
|
|
|
* This expression may be evaluated multiple times.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-30 18:51:45 +01:00
|
|
|
#define ASSERT_ALLOC( pointer, length ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
TEST_ASSERT( ( pointer ) == NULL ); \
|
|
|
|
if( ( length ) != 0 ) \
|
|
|
|
{ \
|
|
|
|
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
|
|
|
|
( length ) ); \
|
|
|
|
TEST_ASSERT( ( pointer ) != NULL ); \
|
|
|
|
} \
|
|
|
|
} \
|
2018-09-27 13:52:16 +02:00
|
|
|
while( 0 )
|
|
|
|
|
2018-09-27 13:56:31 +02:00
|
|
|
/** Compare two buffers and fail the test case if they differ.
|
|
|
|
*
|
|
|
|
* This macro expands to an instruction, not an expression.
|
|
|
|
* It may jump to the \c exit label.
|
|
|
|
*
|
|
|
|
* \param p1 Pointer to the start of the first buffer.
|
|
|
|
* \param size1 Size of the first buffer in bytes.
|
|
|
|
* This expression may be evaluated multiple times.
|
|
|
|
* \param p2 Pointer to the start of the second buffer.
|
|
|
|
* \param size2 Size of the second buffer in bytes.
|
|
|
|
* This expression may be evaluated multiple times.
|
|
|
|
*/
|
|
|
|
#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
TEST_ASSERT( ( size1 ) == ( size2 ) ); \
|
|
|
|
if( ( size1 ) != 0 ) \
|
|
|
|
TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
|
|
|
|
} \
|
|
|
|
while( 0 )
|
|
|
|
|
2015-02-02 13:04:10 +01:00
|
|
|
#define assert(a) if( !( a ) ) \
|
|
|
|
{ \
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
|
2015-02-02 13:04:10 +01:00
|
|
|
__FILE__, __LINE__, #a ); \
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_exit( 1 ); \
|
2015-02-02 13:04:10 +01:00
|
|
|
}
|
|
|
|
|
2018-12-17 23:18:00 +01:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
/* Test if arg and &(arg)[0] have the same type. This is true if arg is
|
|
|
|
* an array but not if it's a pointer. */
|
|
|
|
#define IS_ARRAY_NOT_POINTER( arg ) \
|
|
|
|
( ! __builtin_types_compatible_p( __typeof__( arg ), \
|
|
|
|
__typeof__( &( arg )[0] ) ) )
|
|
|
|
#else
|
|
|
|
/* On platforms where we don't know how to implement this check,
|
|
|
|
* omit it. Oh well, a non-portable check is better than nothing. */
|
|
|
|
#define IS_ARRAY_NOT_POINTER( arg ) 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* A compile-time constant with the value 0. If `const_expr` is not a
|
|
|
|
* compile-time constant with a nonzero value, cause a compile-time error. */
|
|
|
|
#define STATIC_ASSERT_EXPR( const_expr ) \
|
|
|
|
( 0 && sizeof( struct { int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
|
|
|
|
/* Return the scalar value `value` (possibly promoted). This is a compile-time
|
|
|
|
* constant if `value` is. `condition` must be a compile-time constant.
|
|
|
|
* If `condition` is false, arrange to cause a compile-time error. */
|
|
|
|
#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
|
|
|
|
( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
|
|
|
|
|
|
|
|
#define ARRAY_LENGTH_UNSAFE( array ) \
|
2018-12-17 23:17:17 +01:00
|
|
|
( sizeof( array ) / sizeof( *( array ) ) )
|
2018-12-17 23:18:00 +01:00
|
|
|
/** Return the number of elements of a static or stack array.
|
|
|
|
*
|
|
|
|
* \param array A value of array (not pointer) type.
|
|
|
|
*
|
|
|
|
* \return The number of elements of the array.
|
|
|
|
*/
|
|
|
|
#define ARRAY_LENGTH( array ) \
|
|
|
|
( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
|
|
|
|
ARRAY_LENGTH_UNSAFE( array ) ) )
|
|
|
|
|
|
|
|
|
2011-03-13 17:57:25 +01:00
|
|
|
/*
|
|
|
|
* 32-bit integer manipulation macros (big endian)
|
|
|
|
*/
|
2012-10-01 16:41:15 +02:00
|
|
|
#ifndef GET_UINT32_BE
|
|
|
|
#define GET_UINT32_BE(n,b,i) \
|
2011-03-13 17:57:25 +01:00
|
|
|
{ \
|
2012-10-01 16:41:15 +02:00
|
|
|
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 3] ); \
|
2011-03-13 17:57:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
#ifndef PUT_UINT32_BE
|
|
|
|
#define PUT_UINT32_BE(n,b,i) \
|
2011-03-13 17:57:25 +01:00
|
|
|
{ \
|
|
|
|
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
|
|
|
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
|
|
|
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
|
|
|
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-18 00:34:30 +01:00
|
|
|
|
2016-04-18 00:24:50 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Global variables */
|
|
|
|
|
2017-10-01 17:42:29 +02:00
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
int failed;
|
|
|
|
const char *test;
|
|
|
|
const char *filename;
|
|
|
|
int line_no;
|
|
|
|
}
|
|
|
|
test_info;
|
2016-04-18 00:24:50 +02:00
|
|
|
|
2018-04-13 12:16:04 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2018-04-13 11:15:17 +02:00
|
|
|
mbedtls_platform_context platform_ctx;
|
2018-04-13 12:16:04 +02:00
|
|
|
#endif
|
2016-04-18 00:24:50 +02:00
|
|
|
|
2017-07-24 13:27:09 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Helper flags for complex dependencies */
|
|
|
|
|
|
|
|
/* Indicates whether we expect mbedtls_entropy_init
|
|
|
|
* to initialize some strong entropy source. */
|
|
|
|
#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
|
|
|
|
( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
|
|
|
|
( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
|
|
|
|
defined(MBEDTLS_HAVEGE_C) || \
|
|
|
|
defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
|
|
|
|
defined(ENTROPY_NV_SEED) ) )
|
2017-09-07 09:09:33 +02:00
|
|
|
#define ENTROPY_HAVE_STRONG
|
2017-07-24 13:27:09 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-02-18 00:34:30 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Helper Functions */
|
2018-04-13 12:16:04 +02:00
|
|
|
static int platform_setup()
|
|
|
|
{
|
2018-04-18 10:14:31 +02:00
|
|
|
int ret = 0;
|
2018-04-13 12:16:04 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2018-04-18 10:14:31 +02:00
|
|
|
ret = mbedtls_platform_setup( &platform_ctx );
|
2018-04-13 12:16:04 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
2018-04-18 10:14:31 +02:00
|
|
|
return( ret );
|
2018-04-13 12:16:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void platform_teardown()
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
mbedtls_platform_teardown( &platform_ctx );
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
|
|
|
}
|
2016-02-18 00:34:30 +01:00
|
|
|
|
2016-10-05 11:57:49 +02:00
|
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
static int redirect_output( FILE** out_stream, const char* path )
|
|
|
|
{
|
|
|
|
int stdout_fd = dup( fileno( *out_stream ) );
|
|
|
|
|
|
|
|
if( stdout_fd == -1 )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush( *out_stream );
|
|
|
|
fclose( *out_stream );
|
|
|
|
*out_stream = fopen( path, "w" );
|
|
|
|
|
|
|
|
if( *out_stream == NULL )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int restore_output( FILE** out_stream, int old_fd )
|
|
|
|
{
|
|
|
|
fflush( *out_stream );
|
|
|
|
fclose( *out_stream );
|
|
|
|
|
|
|
|
*out_stream = fdopen( old_fd, "w" );
|
|
|
|
if( *out_stream == NULL )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-13 00:07:30 +02:00
|
|
|
|
2016-10-13 12:26:29 +02:00
|
|
|
static void close_output( FILE* out_stream )
|
2016-10-13 00:07:30 +02:00
|
|
|
{
|
2016-10-13 12:26:29 +02:00
|
|
|
fclose( out_stream );
|
2016-10-13 00:07:30 +02:00
|
|
|
}
|
2016-10-05 11:57:49 +02:00
|
|
|
#endif /* __unix__ || __APPLE__ __MACH__ */
|
|
|
|
|
2015-02-02 13:04:10 +01:00
|
|
|
static int unhexify( unsigned char *obuf, const char *ibuf )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
|
|
|
unsigned char c, c2;
|
2015-02-02 13:04:10 +01:00
|
|
|
int len = strlen( ibuf ) / 2;
|
2016-02-18 00:34:30 +01:00
|
|
|
assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-02-02 13:04:10 +01:00
|
|
|
while( *ibuf != 0 )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
|
|
|
c = *ibuf++;
|
|
|
|
if( c >= '0' && c <= '9' )
|
|
|
|
c -= '0';
|
|
|
|
else if( c >= 'a' && c <= 'f' )
|
|
|
|
c -= 'a' - 10;
|
|
|
|
else if( c >= 'A' && c <= 'F' )
|
|
|
|
c -= 'A' - 10;
|
|
|
|
else
|
|
|
|
assert( 0 );
|
|
|
|
|
|
|
|
c2 = *ibuf++;
|
|
|
|
if( c2 >= '0' && c2 <= '9' )
|
|
|
|
c2 -= '0';
|
|
|
|
else if( c2 >= 'a' && c2 <= 'f' )
|
|
|
|
c2 -= 'a' - 10;
|
|
|
|
else if( c2 >= 'A' && c2 <= 'F' )
|
|
|
|
c2 -= 'A' - 10;
|
|
|
|
else
|
|
|
|
assert( 0 );
|
|
|
|
|
|
|
|
*obuf++ = ( c << 4 ) | c2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-02-02 13:09:25 +01:00
|
|
|
static void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
|
|
|
unsigned char l, h;
|
|
|
|
|
2015-02-02 13:09:25 +01:00
|
|
|
while( len != 0 )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-02-02 13:09:25 +01:00
|
|
|
h = *ibuf / 16;
|
|
|
|
l = *ibuf % 16;
|
2009-06-28 23:50:27 +02:00
|
|
|
|
|
|
|
if( h < 10 )
|
|
|
|
*obuf++ = '0' + h;
|
|
|
|
else
|
|
|
|
*obuf++ = 'a' + h - 10;
|
|
|
|
|
|
|
|
if( l < 10 )
|
|
|
|
*obuf++ = '0' + l;
|
|
|
|
else
|
|
|
|
*obuf++ = 'a' + l - 10;
|
|
|
|
|
|
|
|
++ibuf;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
}
|
2011-03-08 15:16:06 +01:00
|
|
|
|
2014-06-13 21:09:26 +02:00
|
|
|
/**
|
|
|
|
* Allocate and zeroize a buffer.
|
|
|
|
*
|
|
|
|
* If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
|
|
|
|
*
|
|
|
|
* For convenience, dies if allocation fails.
|
|
|
|
*/
|
|
|
|
static unsigned char *zero_alloc( size_t len )
|
|
|
|
{
|
|
|
|
void *p;
|
2015-02-02 13:09:25 +01:00
|
|
|
size_t actual_len = ( len != 0 ) ? len : 1;
|
2014-06-13 21:09:26 +02:00
|
|
|
|
2015-05-26 16:04:06 +02:00
|
|
|
p = mbedtls_calloc( 1, actual_len );
|
2014-07-10 14:37:36 +02:00
|
|
|
assert( p != NULL );
|
2014-06-13 21:09:26 +02:00
|
|
|
|
|
|
|
memset( p, 0x00, actual_len );
|
|
|
|
|
|
|
|
return( p );
|
|
|
|
}
|
|
|
|
|
2014-06-06 14:48:09 +02:00
|
|
|
/**
|
|
|
|
* Allocate and fill a buffer from hex data.
|
|
|
|
*
|
|
|
|
* The buffer is sized exactly as needed. This allows to detect buffer
|
|
|
|
* overruns (including overreads) when running the test suite under valgrind.
|
|
|
|
*
|
2014-06-13 21:09:26 +02:00
|
|
|
* If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
|
|
|
|
*
|
2014-06-06 14:48:09 +02:00
|
|
|
* For convenience, dies if allocation fails.
|
|
|
|
*/
|
|
|
|
static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
|
|
|
|
{
|
|
|
|
unsigned char *obuf;
|
|
|
|
|
2015-02-02 13:09:25 +01:00
|
|
|
*olen = strlen( ibuf ) / 2;
|
2014-06-06 14:48:09 +02:00
|
|
|
|
2014-06-13 21:09:26 +02:00
|
|
|
if( *olen == 0 )
|
|
|
|
return( zero_alloc( *olen ) );
|
|
|
|
|
2015-05-26 16:04:06 +02:00
|
|
|
obuf = mbedtls_calloc( 1, *olen );
|
2014-07-10 14:37:36 +02:00
|
|
|
assert( obuf != NULL );
|
2014-06-06 14:48:09 +02:00
|
|
|
|
|
|
|
(void) unhexify( obuf, ibuf );
|
|
|
|
|
|
|
|
return( obuf );
|
|
|
|
}
|
|
|
|
|
2011-03-08 15:16:06 +01:00
|
|
|
/**
|
|
|
|
* This function just returns data from rand().
|
2011-03-13 16:45:42 +01:00
|
|
|
* Although predictable and often similar on multiple
|
|
|
|
* runs, this does not result in identical random on
|
|
|
|
* each run. So do not use this if the results of a
|
|
|
|
* test depend on the random data that is generated.
|
2011-03-08 15:16:06 +01:00
|
|
|
*
|
|
|
|
* rng_state shall be NULL.
|
|
|
|
*/
|
2011-11-27 22:07:34 +01:00
|
|
|
static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
|
2011-03-08 15:16:06 +01:00
|
|
|
{
|
2014-04-30 16:02:38 +02:00
|
|
|
#if !defined(__OpenBSD__)
|
2011-11-27 22:07:34 +01:00
|
|
|
size_t i;
|
|
|
|
|
2011-03-08 15:16:06 +01:00
|
|
|
if( rng_state != NULL )
|
|
|
|
rng_state = NULL;
|
|
|
|
|
2011-11-27 22:07:34 +01:00
|
|
|
for( i = 0; i < len; ++i )
|
|
|
|
output[i] = rand();
|
2014-04-30 16:02:38 +02:00
|
|
|
#else
|
|
|
|
if( rng_state != NULL )
|
|
|
|
rng_state = NULL;
|
|
|
|
|
|
|
|
arc4random_buf( output, len );
|
|
|
|
#endif /* !OpenBSD */
|
2011-11-27 22:07:34 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2011-03-08 15:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function only returns zeros
|
|
|
|
*
|
|
|
|
* rng_state shall be NULL.
|
|
|
|
*/
|
2011-11-27 22:07:34 +01:00
|
|
|
static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
|
2011-03-08 15:16:06 +01:00
|
|
|
{
|
|
|
|
if( rng_state != NULL )
|
|
|
|
rng_state = NULL;
|
|
|
|
|
2011-11-27 22:07:34 +01:00
|
|
|
memset( output, 0, len );
|
|
|
|
|
2011-03-08 15:16:06 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned char *buf;
|
2011-11-27 22:07:34 +01:00
|
|
|
size_t length;
|
2011-03-13 16:45:42 +01:00
|
|
|
} rnd_buf_info;
|
2011-03-08 15:16:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns random based on a buffer it receives.
|
|
|
|
*
|
2011-03-13 16:45:42 +01:00
|
|
|
* rng_state shall be a pointer to a rnd_buf_info structure.
|
2015-10-30 09:23:19 +01:00
|
|
|
*
|
2011-03-13 16:45:42 +01:00
|
|
|
* The number of bytes released from the buffer on each call to
|
|
|
|
* the random function is specified by per_call. (Can be between
|
|
|
|
* 1 and 4)
|
2011-03-08 15:16:06 +01:00
|
|
|
*
|
|
|
|
* After the buffer is empty it will return rand();
|
|
|
|
*/
|
2011-11-27 22:07:34 +01:00
|
|
|
static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
|
2011-03-08 15:16:06 +01:00
|
|
|
{
|
2011-03-13 16:45:42 +01:00
|
|
|
rnd_buf_info *info = (rnd_buf_info *) rng_state;
|
2011-11-27 22:07:34 +01:00
|
|
|
size_t use_len;
|
2011-03-08 15:16:06 +01:00
|
|
|
|
|
|
|
if( rng_state == NULL )
|
2011-11-27 22:07:34 +01:00
|
|
|
return( rnd_std_rand( NULL, output, len ) );
|
2011-03-08 15:16:06 +01:00
|
|
|
|
2011-11-27 22:07:34 +01:00
|
|
|
use_len = len;
|
|
|
|
if( len > info->length )
|
|
|
|
use_len = info->length;
|
2011-03-13 16:45:42 +01:00
|
|
|
|
2011-11-27 22:07:34 +01:00
|
|
|
if( use_len )
|
2011-03-08 15:16:06 +01:00
|
|
|
{
|
2011-11-27 22:07:34 +01:00
|
|
|
memcpy( output, info->buf, use_len );
|
|
|
|
info->buf += use_len;
|
|
|
|
info->length -= use_len;
|
2011-03-08 15:16:06 +01:00
|
|
|
}
|
|
|
|
|
2011-11-27 22:07:34 +01:00
|
|
|
if( len - use_len > 0 )
|
|
|
|
return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
|
|
|
|
|
|
|
|
return( 0 );
|
2011-03-08 15:16:06 +01:00
|
|
|
}
|
2011-03-13 16:45:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Info structure for the pseudo random function
|
|
|
|
*
|
|
|
|
* Key should be set at the start to a test-unique value.
|
2011-03-13 17:57:25 +01:00
|
|
|
* Do not forget endianness!
|
2011-03-13 16:45:42 +01:00
|
|
|
* State( v0, v1 ) should be set to zero.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2011-03-13 17:57:25 +01:00
|
|
|
uint32_t key[16];
|
2011-03-13 16:45:42 +01:00
|
|
|
uint32_t v0, v1;
|
|
|
|
} rnd_pseudo_info;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns random based on a pseudo random function.
|
|
|
|
* This means the results should be identical on all systems.
|
|
|
|
* Pseudo random is based on the XTEA encryption algorithm to
|
|
|
|
* generate pseudorandom.
|
|
|
|
*
|
|
|
|
* rng_state shall be a pointer to a rnd_pseudo_info structure.
|
|
|
|
*/
|
2011-11-27 22:07:34 +01:00
|
|
|
static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
|
2011-03-13 16:45:42 +01:00
|
|
|
{
|
|
|
|
rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
|
2011-11-27 22:07:34 +01:00
|
|
|
uint32_t i, *k, sum, delta=0x9E3779B9;
|
2014-01-03 11:59:09 +01:00
|
|
|
unsigned char result[4], *out = output;
|
2011-03-13 16:45:42 +01:00
|
|
|
|
|
|
|
if( rng_state == NULL )
|
2011-11-27 22:07:34 +01:00
|
|
|
return( rnd_std_rand( NULL, output, len ) );
|
2011-03-13 16:45:42 +01:00
|
|
|
|
2011-03-13 17:57:25 +01:00
|
|
|
k = info->key;
|
2011-11-27 22:07:34 +01:00
|
|
|
|
|
|
|
while( len > 0 )
|
2011-03-13 16:45:42 +01:00
|
|
|
{
|
2012-05-15 17:02:38 +02:00
|
|
|
size_t use_len = ( len > 4 ) ? 4 : len;
|
2011-11-27 22:07:34 +01:00
|
|
|
sum = 0;
|
|
|
|
|
|
|
|
for( i = 0; i < 32; i++ )
|
|
|
|
{
|
2015-02-02 13:09:25 +01:00
|
|
|
info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
|
|
|
|
+ info->v1 ) ^ ( sum + k[sum & 3] );
|
2011-11-27 22:07:34 +01:00
|
|
|
sum += delta;
|
2015-02-02 13:09:25 +01:00
|
|
|
info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
|
|
|
|
+ info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
|
2011-11-27 22:07:34 +01:00
|
|
|
}
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
PUT_UINT32_BE( info->v0, result, 0 );
|
2014-01-03 11:59:09 +01:00
|
|
|
memcpy( out, result, use_len );
|
2011-11-27 22:07:34 +01:00
|
|
|
len -= use_len;
|
2014-01-03 11:59:09 +01:00
|
|
|
out += 4;
|
2011-03-13 16:45:42 +01:00
|
|
|
}
|
|
|
|
|
2011-11-27 22:07:34 +01:00
|
|
|
return( 0 );
|
2011-03-13 16:45:42 +01:00
|
|
|
}
|
2016-02-18 00:34:30 +01:00
|
|
|
|
2016-05-06 01:22:18 +02:00
|
|
|
static void test_fail( const char *test, int line_no, const char* filename )
|
2016-02-18 00:34:30 +01:00
|
|
|
{
|
2017-10-01 17:42:29 +02:00
|
|
|
test_info.failed = 1;
|
|
|
|
test_info.test = test;
|
|
|
|
test_info.line_no = line_no;
|
|
|
|
test_info.filename = filename;
|
2016-02-18 00:34:30 +01:00
|
|
|
}
|
2017-05-30 01:06:49 +02:00
|
|
|
|
2018-07-18 18:48:37 +02:00
|
|
|
int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
|
2017-05-30 01:06:49 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
|
|
|
if ( a_len != b_len )
|
2018-07-18 18:48:37 +02:00
|
|
|
return( -1 );
|
2017-05-30 01:06:49 +02:00
|
|
|
|
|
|
|
for( i = 0; i < a_len; i++ )
|
|
|
|
{
|
|
|
|
if ( a[i] != b[i] )
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|