chore: import local project into Gitea
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* \file helpers.h
|
||||
*
|
||||
* \brief This file contains the prototypes of helper functions for the
|
||||
* purpose of testing.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2020, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#ifndef TEST_HELPERS_H
|
||||
#define TEST_HELPERS_H
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_calloc calloc
|
||||
#define mbedtls_free free
|
||||
#define mbedtls_exit exit
|
||||
#define mbedtls_time time
|
||||
#define mbedtls_time_t time_t
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int mbedtls_test_platform_setup( void );
|
||||
void mbedtls_test_platform_teardown( void );
|
||||
|
||||
int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf );
|
||||
void mbedtls_test_hexify( unsigned char *obuf,
|
||||
const unsigned char *ibuf,
|
||||
int len );
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
unsigned char *mbedtls_test_zero_alloc( size_t len );
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
|
||||
*
|
||||
* For convenience, dies if allocation fails.
|
||||
*/
|
||||
unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen );
|
||||
|
||||
int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
|
||||
uint32_t a_len, uint32_t b_len );
|
||||
|
||||
#endif /* TEST_HELPERS_H */
|
||||
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* \file macros.h
|
||||
*
|
||||
* \brief This file contains generic macros for the purpose of testing.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2020, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#ifndef TEST_MACROS_H
|
||||
#define TEST_MACROS_H
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_calloc calloc
|
||||
#define mbedtls_free free
|
||||
#define mbedtls_exit exit
|
||||
#define mbedtls_time time
|
||||
#define mbedtls_time_t time_t
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
||||
#include "mbedtls/memory_buffer_alloc.h"
|
||||
#endif
|
||||
|
||||
#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
|
||||
{ \
|
||||
mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
|
||||
__FILE__, __LINE__, #a ); \
|
||||
mbedtls_exit( 1 ); \
|
||||
}
|
||||
|
||||
#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 ) \
|
||||
( sizeof( array ) / sizeof( *( array ) ) )
|
||||
/** 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 ) ) )
|
||||
|
||||
/** Return the smaller of two values.
|
||||
*
|
||||
* \param x An integer-valued expression without side effects.
|
||||
* \param y An integer-valued expression without side effects.
|
||||
*
|
||||
* \return The smaller of \p x and \p y.
|
||||
*/
|
||||
#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
|
||||
|
||||
/** Return the larger of two values.
|
||||
*
|
||||
* \param x An integer-valued expression without side effects.
|
||||
* \param y An integer-valued expression without side effects.
|
||||
*
|
||||
* \return The larger of \p x and \p y.
|
||||
*/
|
||||
#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (big endian)
|
||||
*/
|
||||
#ifndef GET_UINT32_BE
|
||||
#define GET_UINT32_BE(n,b,i) \
|
||||
{ \
|
||||
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
||||
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
||||
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
||||
| ( (uint32_t) (b)[(i) + 3] ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef PUT_UINT32_BE
|
||||
#define PUT_UINT32_BE(n,b,i) \
|
||||
{ \
|
||||
(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
|
||||
|
||||
#endif /* TEST_MACROS_H */
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Helper functions for tests that use the PSA Crypto API.
|
||||
*/
|
||||
/*
|
||||
* Copyright (C) 2019, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#ifndef PSA_CRYPTO_HELPERS_H
|
||||
#define PSA_CRYPTO_HELPERS_H
|
||||
|
||||
#include "test/psa_helpers.h"
|
||||
|
||||
#include <psa/crypto.h>
|
||||
|
||||
static int test_helper_is_psa_pristine( int line, const char *file )
|
||||
{
|
||||
mbedtls_psa_stats_t stats;
|
||||
const char *msg = NULL;
|
||||
|
||||
mbedtls_psa_get_stats( &stats );
|
||||
|
||||
if( stats.volatile_slots != 0 )
|
||||
msg = "A volatile slot has not been closed properly.";
|
||||
else if( stats.persistent_slots != 0 )
|
||||
msg = "A persistent slot has not been closed properly.";
|
||||
else if( stats.external_slots != 0 )
|
||||
msg = "An external slot has not been closed properly.";
|
||||
else if( stats.half_filled_slots != 0 )
|
||||
msg = "A half-filled slot has not been cleared properly.";
|
||||
|
||||
/* If the test has already failed, don't overwrite the failure
|
||||
* information. Do keep the stats lookup above, because it can be
|
||||
* convenient to break on it when debugging a failure. */
|
||||
if( msg != NULL && test_info.result == TEST_RESULT_SUCCESS )
|
||||
test_fail( msg, line, file );
|
||||
|
||||
return( msg == NULL );
|
||||
}
|
||||
|
||||
/** Check that no PSA Crypto key slots are in use.
|
||||
*/
|
||||
#define ASSERT_PSA_PRISTINE( ) \
|
||||
do \
|
||||
{ \
|
||||
if( ! test_helper_is_psa_pristine( __LINE__, __FILE__ ) ) \
|
||||
goto exit; \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
static void test_helper_psa_done( int line, const char *file )
|
||||
{
|
||||
(void) test_helper_is_psa_pristine( line, file );
|
||||
mbedtls_psa_crypto_free( );
|
||||
}
|
||||
|
||||
/** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots
|
||||
* in use.
|
||||
*/
|
||||
#define PSA_DONE( ) test_helper_psa_done( __LINE__, __FILE__ )
|
||||
|
||||
|
||||
|
||||
#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
|
||||
#include <psa/crypto.h>
|
||||
|
||||
/** Name of the file where return statuses are logged by #RECORD_STATUS. */
|
||||
#define STATUS_LOG_FILE_NAME "statuses.log"
|
||||
|
||||
static psa_status_t record_status( psa_status_t status,
|
||||
const char *func,
|
||||
const char *file, int line,
|
||||
const char *expr )
|
||||
{
|
||||
/* We open the log file on first use.
|
||||
* We never close the log file, so the record_status feature is not
|
||||
* compatible with resource leak detectors such as Asan.
|
||||
*/
|
||||
static FILE *log;
|
||||
if( log == NULL )
|
||||
log = fopen( STATUS_LOG_FILE_NAME, "a" );
|
||||
fprintf( log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr );
|
||||
return( status );
|
||||
}
|
||||
|
||||
/** Return value logging wrapper macro.
|
||||
*
|
||||
* Evaluate \p expr. Write a line recording its value to the log file
|
||||
* #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated
|
||||
* list of fields:
|
||||
* ```
|
||||
* value of expr:string:__FILE__:__LINE__:expr
|
||||
* ```
|
||||
*
|
||||
* The test code does not call this macro explicitly because that would
|
||||
* be very invasive. Instead, we instrument the source code by defining
|
||||
* a bunch of wrapper macros like
|
||||
* ```
|
||||
* #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init())
|
||||
* ```
|
||||
* These macro definitions must be present in `instrument_record_status.h`
|
||||
* when building the test suites.
|
||||
*
|
||||
* \param string A string, normally a function name.
|
||||
* \param expr An expression to evaluate, normally a call of the function
|
||||
* whose name is in \p string. This expression must return
|
||||
* a value of type #psa_status_t.
|
||||
* \return The value of \p expr.
|
||||
*/
|
||||
#define RECORD_STATUS( string, expr ) \
|
||||
record_status( ( expr ), string, __FILE__, __LINE__, #expr )
|
||||
|
||||
#include "instrument_record_status.h"
|
||||
|
||||
#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
|
||||
|
||||
#endif /* PSA_CRYPTO_HELPERS_H */
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Helper functions for tests that use any PSA API.
|
||||
*/
|
||||
/*
|
||||
* Copyright (C) 2019, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#ifndef PSA_HELPERS_H
|
||||
#define PSA_HELPERS_H
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_SPM)
|
||||
#include "spm/psa_defs.h"
|
||||
#endif
|
||||
|
||||
/** 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.
|
||||
*/
|
||||
#define PSA_ASSERT( expr ) TEST_EQUAL( ( expr ), PSA_SUCCESS )
|
||||
|
||||
#endif /* PSA_HELPERS_H */
|
||||
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* \file random.h
|
||||
*
|
||||
* \brief This file contains the prototypes of helper functions to generate
|
||||
* random numbers for the purpose of testing.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2020, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#ifndef TEST_RANDOM_H
|
||||
#define TEST_RANDOM_H
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char *buf;
|
||||
size_t length;
|
||||
} mbedtls_test_rnd_buf_info;
|
||||
|
||||
/**
|
||||
* Info structure for the pseudo random function
|
||||
*
|
||||
* Key should be set at the start to a test-unique value.
|
||||
* Do not forget endianness!
|
||||
* State( v0, v1 ) should be set to zero.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint32_t key[16];
|
||||
uint32_t v0, v1;
|
||||
} mbedtls_test_rnd_pseudo_info;
|
||||
|
||||
/**
|
||||
* This function just returns data from rand().
|
||||
* 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.
|
||||
*
|
||||
* rng_state shall be NULL.
|
||||
*/
|
||||
int mbedtls_test_rnd_std_rand( void *rng_state,
|
||||
unsigned char *output,
|
||||
size_t len );
|
||||
|
||||
/**
|
||||
* This function only returns zeros
|
||||
*
|
||||
* rng_state shall be NULL.
|
||||
*/
|
||||
int mbedtls_test_rnd_zero_rand( void *rng_state,
|
||||
unsigned char *output,
|
||||
size_t len );
|
||||
|
||||
/**
|
||||
* This function returns random based on a buffer it receives.
|
||||
*
|
||||
* rng_state shall be a pointer to a rnd_buf_info structure.
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* After the buffer is empty it will return rand();
|
||||
*/
|
||||
int mbedtls_test_rnd_buffer_rand( void *rng_state,
|
||||
unsigned char *output,
|
||||
size_t len );
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
int mbedtls_test_rnd_pseudo_rand( void *rng_state,
|
||||
unsigned char *output,
|
||||
size_t len );
|
||||
|
||||
#endif /* TEST_RANDOM_H */
|
||||
Reference in New Issue
Block a user