mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-08 17:42:09 +03:00
Merge remote-tracking branch 'origin/development' into development_new
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com> Conflicts: include/mbedtls/check_config.h: nearby edits library/entropy.c: nearby edits programs/random/gen_random_havege.c: modification vs. removal programs/ssl/ssl_test_lib.h: nearby edits programs/test/cpp_dummy_build.cpp: nearby edits visualc/VS2010/mbedTLS.vcxproj: automatically generated file, regenerated with scripts/generate_visualc_files.pl
This commit is contained in:
50
tests/include/test/asn1_helpers.h
Normal file
50
tests/include/test/asn1_helpers.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/** Helper functions for tests that manipulate ASN.1 data.
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef ASN1_HELPERS_H
|
||||
#define ASN1_HELPERS_H
|
||||
|
||||
#include "test/helpers.h"
|
||||
|
||||
/** Skip past an INTEGER in an ASN.1 buffer.
|
||||
*
|
||||
* Mark the current test case as failed in any of the following conditions:
|
||||
* - The buffer does not start with an ASN.1 INTEGER.
|
||||
* - The integer's size or parity does not match the constraints expressed
|
||||
* through \p min_bits, \p max_bits and \p must_be_odd.
|
||||
*
|
||||
* \param p Upon entry, `*p` points to the first byte of the
|
||||
* buffer to parse.
|
||||
* On successful return, `*p` points to the first byte
|
||||
* after the parsed INTEGER.
|
||||
* On failure, `*p` is unspecified.
|
||||
* \param end The end of the ASN.1 buffer.
|
||||
* \param min_bits Fail the test case if the integer does not have at
|
||||
* least this many significant bits.
|
||||
* \param max_bits Fail the test case if the integer has more than
|
||||
* this many significant bits.
|
||||
* \param must_be_odd Fail the test case if the integer is even.
|
||||
*
|
||||
* \return \c 0 if the test failed, otherwise 1.
|
||||
*/
|
||||
int mbedtls_test_asn1_skip_integer( unsigned char **p, const unsigned char *end,
|
||||
size_t min_bits, size_t max_bits,
|
||||
int must_be_odd );
|
||||
|
||||
#endif /* ASN1_HELPERS_H */
|
@@ -28,22 +28,9 @@
|
||||
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
#include <psa/crypto_driver_common.h>
|
||||
#include <psa/crypto.h>
|
||||
|
||||
#include "mbedtls/cipher.h"
|
||||
typedef struct {
|
||||
psa_algorithm_t alg;
|
||||
unsigned int key_set : 1;
|
||||
unsigned int iv_required : 1;
|
||||
unsigned int iv_set : 1;
|
||||
uint8_t iv_size;
|
||||
uint8_t block_size;
|
||||
mbedtls_cipher_context_t cipher;
|
||||
} test_transparent_cipher_operation_t;
|
||||
|
||||
typedef struct{
|
||||
unsigned int initialised : 1;
|
||||
test_transparent_cipher_operation_t ctx;
|
||||
} test_opaque_cipher_operation_t;
|
||||
|
||||
typedef struct {
|
||||
/* If non-null, on success, copy this to the output. */
|
||||
@@ -80,44 +67,32 @@ psa_status_t test_transparent_cipher_decrypt(
|
||||
uint8_t *output, size_t output_size, size_t *output_length);
|
||||
|
||||
psa_status_t test_transparent_cipher_encrypt_setup(
|
||||
test_transparent_cipher_operation_t *operation,
|
||||
mbedtls_transparent_test_driver_cipher_operation_t *operation,
|
||||
const psa_key_attributes_t *attributes,
|
||||
const uint8_t *key, size_t key_length,
|
||||
psa_algorithm_t alg);
|
||||
|
||||
psa_status_t test_transparent_cipher_decrypt_setup(
|
||||
test_transparent_cipher_operation_t *operation,
|
||||
mbedtls_transparent_test_driver_cipher_operation_t *operation,
|
||||
const psa_key_attributes_t *attributes,
|
||||
const uint8_t *key, size_t key_length,
|
||||
psa_algorithm_t alg);
|
||||
|
||||
psa_status_t test_transparent_cipher_abort(
|
||||
test_transparent_cipher_operation_t *operation);
|
||||
|
||||
psa_status_t test_transparent_cipher_generate_iv(
|
||||
test_transparent_cipher_operation_t *operation,
|
||||
uint8_t *iv,
|
||||
size_t iv_size,
|
||||
size_t *iv_length);
|
||||
mbedtls_transparent_test_driver_cipher_operation_t *operation );
|
||||
|
||||
psa_status_t test_transparent_cipher_set_iv(
|
||||
test_transparent_cipher_operation_t *operation,
|
||||
const uint8_t *iv,
|
||||
size_t iv_length);
|
||||
mbedtls_transparent_test_driver_cipher_operation_t *operation,
|
||||
const uint8_t *iv, size_t iv_length);
|
||||
|
||||
psa_status_t test_transparent_cipher_update(
|
||||
test_transparent_cipher_operation_t *operation,
|
||||
const uint8_t *input,
|
||||
size_t input_length,
|
||||
uint8_t *output,
|
||||
size_t output_size,
|
||||
size_t *output_length);
|
||||
mbedtls_transparent_test_driver_cipher_operation_t *operation,
|
||||
const uint8_t *input, size_t input_length,
|
||||
uint8_t *output, size_t output_size, size_t *output_length);
|
||||
|
||||
psa_status_t test_transparent_cipher_finish(
|
||||
test_transparent_cipher_operation_t *operation,
|
||||
uint8_t *output,
|
||||
size_t output_size,
|
||||
size_t *output_length);
|
||||
mbedtls_transparent_test_driver_cipher_operation_t *operation,
|
||||
uint8_t *output, size_t output_size, size_t *output_length);
|
||||
|
||||
/*
|
||||
* opaque versions
|
||||
@@ -137,44 +112,32 @@ psa_status_t test_opaque_cipher_decrypt(
|
||||
uint8_t *output, size_t output_size, size_t *output_length);
|
||||
|
||||
psa_status_t test_opaque_cipher_encrypt_setup(
|
||||
test_opaque_cipher_operation_t *operation,
|
||||
mbedtls_opaque_test_driver_cipher_operation_t *operation,
|
||||
const psa_key_attributes_t *attributes,
|
||||
const uint8_t *key, size_t key_length,
|
||||
psa_algorithm_t alg);
|
||||
|
||||
psa_status_t test_opaque_cipher_decrypt_setup(
|
||||
test_opaque_cipher_operation_t *operation,
|
||||
mbedtls_opaque_test_driver_cipher_operation_t *operation,
|
||||
const psa_key_attributes_t *attributes,
|
||||
const uint8_t *key, size_t key_length,
|
||||
psa_algorithm_t alg);
|
||||
|
||||
psa_status_t test_opaque_cipher_abort(
|
||||
test_opaque_cipher_operation_t *operation);
|
||||
|
||||
psa_status_t test_opaque_cipher_generate_iv(
|
||||
test_opaque_cipher_operation_t *operation,
|
||||
uint8_t *iv,
|
||||
size_t iv_size,
|
||||
size_t *iv_length);
|
||||
mbedtls_opaque_test_driver_cipher_operation_t *operation);
|
||||
|
||||
psa_status_t test_opaque_cipher_set_iv(
|
||||
test_opaque_cipher_operation_t *operation,
|
||||
const uint8_t *iv,
|
||||
size_t iv_length);
|
||||
mbedtls_opaque_test_driver_cipher_operation_t *operation,
|
||||
const uint8_t *iv, size_t iv_length);
|
||||
|
||||
psa_status_t test_opaque_cipher_update(
|
||||
test_opaque_cipher_operation_t *operation,
|
||||
const uint8_t *input,
|
||||
size_t input_length,
|
||||
uint8_t *output,
|
||||
size_t output_size,
|
||||
size_t *output_length);
|
||||
mbedtls_opaque_test_driver_cipher_operation_t *operation,
|
||||
const uint8_t *input, size_t input_length,
|
||||
uint8_t *output, size_t output_size, size_t *output_length);
|
||||
|
||||
psa_status_t test_opaque_cipher_finish(
|
||||
test_opaque_cipher_operation_t *operation,
|
||||
uint8_t *output,
|
||||
size_t output_size,
|
||||
size_t *output_length);
|
||||
mbedtls_opaque_test_driver_cipher_operation_t *operation,
|
||||
uint8_t *output, size_t output_size, size_t *output_length);
|
||||
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
#endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */
|
||||
|
@@ -41,7 +41,7 @@ typedef struct {
|
||||
unsigned long hits;
|
||||
} test_driver_key_management_hooks_t;
|
||||
|
||||
#define TEST_DRIVER_KEY_MANAGEMENT_INIT { NULL, 0, PSA_ERROR_NOT_SUPPORTED, 0 }
|
||||
#define TEST_DRIVER_KEY_MANAGEMENT_INIT { NULL, 0, PSA_SUCCESS, 0 }
|
||||
static inline test_driver_key_management_hooks_t test_driver_key_management_hooks_init( void )
|
||||
{
|
||||
const test_driver_key_management_hooks_t v = TEST_DRIVER_KEY_MANAGEMENT_INIT;
|
||||
@@ -58,11 +58,10 @@ psa_status_t test_opaque_generate_key(
|
||||
const psa_key_attributes_t *attributes,
|
||||
uint8_t *key, size_t key_size, size_t *key_length );
|
||||
|
||||
psa_status_t test_transparent_validate_key(
|
||||
psa_status_t test_opaque_export_key(
|
||||
const psa_key_attributes_t *attributes,
|
||||
const uint8_t *data,
|
||||
size_t data_length,
|
||||
size_t *bits);
|
||||
const uint8_t *key, size_t key_length,
|
||||
uint8_t *data, size_t data_size, size_t *data_length );
|
||||
|
||||
psa_status_t test_transparent_export_public_key(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@@ -74,5 +73,14 @@ psa_status_t test_opaque_export_public_key(
|
||||
const uint8_t *key, size_t key_length,
|
||||
uint8_t *data, size_t data_size, size_t *data_length );
|
||||
|
||||
psa_status_t test_transparent_import_key(
|
||||
const psa_key_attributes_t *attributes,
|
||||
const uint8_t *data,
|
||||
size_t data_length,
|
||||
uint8_t *key_buffer,
|
||||
size_t key_buffer_size,
|
||||
size_t *key_buffer_length,
|
||||
size_t *bits);
|
||||
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
#endif /* PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H */
|
||||
|
@@ -40,7 +40,7 @@ typedef struct {
|
||||
unsigned long hits;
|
||||
} test_driver_signature_hooks_t;
|
||||
|
||||
#define TEST_DRIVER_SIGNATURE_INIT { NULL, 0, PSA_ERROR_NOT_SUPPORTED, 0 }
|
||||
#define TEST_DRIVER_SIGNATURE_INIT { NULL, 0, PSA_SUCCESS, 0 }
|
||||
static inline test_driver_signature_hooks_t test_driver_signature_hooks_init( void )
|
||||
{
|
||||
const test_driver_signature_hooks_t v = TEST_DRIVER_SIGNATURE_INIT;
|
||||
|
56
tests/include/test/fake_external_rng_for_test.h
Normal file
56
tests/include/test/fake_external_rng_for_test.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Insecure but standalone implementation of mbedtls_psa_external_get_random().
|
||||
* Only for use in tests!
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef FAKE_EXTERNAL_RNG_FOR_TEST_H
|
||||
#define FAKE_EXTERNAL_RNG_FOR_TEST_H
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
||||
/** Enable the insecure implementation of mbedtls_psa_external_get_random().
|
||||
*
|
||||
* The insecure implementation of mbedtls_psa_external_get_random() is
|
||||
* disabled by default.
|
||||
*
|
||||
* When MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled and the test
|
||||
* helpers are linked into a program, you must enable this before running any
|
||||
* code that uses the PSA subsystem to generate random data (including internal
|
||||
* random generation for purposes such as blinding when the random generation
|
||||
* is routed through PSA).
|
||||
*
|
||||
* You can enable and disable it at any time, regardless of the state
|
||||
* of the PSA subsystem. You may disable it temporarily to simulate a
|
||||
* depleted entropy source.
|
||||
*/
|
||||
void mbedtls_test_enable_insecure_external_rng( void );
|
||||
|
||||
/** Disable the insecure implementation of mbedtls_psa_external_get_random().
|
||||
*
|
||||
* See mbedtls_test_enable_insecure_external_rng().
|
||||
*/
|
||||
void mbedtls_test_disable_insecure_external_rng( void );
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
||||
|
||||
#endif /* FAKE_EXTERNAL_RNG_FOR_TEST_H */
|
@@ -31,6 +31,11 @@
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
|
||||
defined(MBEDTLS_TEST_HOOKS)
|
||||
#define MBEDTLS_TEST_MUTEX_USAGE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
@@ -49,9 +54,78 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MBEDTLS_TEST_RESULT_SUCCESS = 0,
|
||||
MBEDTLS_TEST_RESULT_FAILED,
|
||||
MBEDTLS_TEST_RESULT_SKIPPED
|
||||
} mbedtls_test_result_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
mbedtls_test_result_t result;
|
||||
const char *test;
|
||||
const char *filename;
|
||||
int line_no;
|
||||
unsigned long step;
|
||||
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
|
||||
const char *mutex_usage_error;
|
||||
#endif
|
||||
}
|
||||
mbedtls_test_info_t;
|
||||
extern mbedtls_test_info_t mbedtls_test_info;
|
||||
|
||||
int mbedtls_test_platform_setup( void );
|
||||
void mbedtls_test_platform_teardown( void );
|
||||
|
||||
/**
|
||||
* \brief Record the current test case as a failure.
|
||||
*
|
||||
* This function can be called directly however it is usually
|
||||
* called via macros such as TEST_ASSERT, TEST_EQUAL,
|
||||
* PSA_ASSERT, etc...
|
||||
*
|
||||
* \note If the test case was already marked as failed, calling
|
||||
* `mbedtls_test_fail( )` again will not overwrite any
|
||||
* previous information about the failure.
|
||||
*
|
||||
* \param test Description of the failure or assertion that failed. This
|
||||
* MUST be a string literal.
|
||||
* \param line_no Line number where the failure originated.
|
||||
* \param filename Filename where the failure originated.
|
||||
*/
|
||||
void mbedtls_test_fail( const char *test, int line_no, const char* filename );
|
||||
|
||||
/**
|
||||
* \brief Record the current test case as skipped.
|
||||
*
|
||||
* This function can be called directly however it is usually
|
||||
* called via the TEST_ASSUME macro.
|
||||
*
|
||||
* \param test Description of the assumption that caused the test case to
|
||||
* be skipped. This MUST be a string literal.
|
||||
* \param line_no Line number where the test case was skipped.
|
||||
* \param filename Filename where the test case was skipped.
|
||||
*/
|
||||
void mbedtls_test_skip( const char *test, int line_no, const char* filename );
|
||||
|
||||
/**
|
||||
* \brief Set the test step number for failure reports.
|
||||
*
|
||||
* Call this function to display "step NNN" in addition to the
|
||||
* line number and file name if a test fails. Typically the "step
|
||||
* number" is the index of a for loop but it can be whatever you
|
||||
* want.
|
||||
*
|
||||
* \param step The step number to report.
|
||||
*/
|
||||
void mbedtls_test_set_step( unsigned long step );
|
||||
|
||||
/**
|
||||
* \brief Reset mbedtls_test_info to a ready/starting state.
|
||||
*/
|
||||
void mbedtls_test_info_reset( void );
|
||||
|
||||
/**
|
||||
* \brief This function decodes the hexadecimal representation of
|
||||
* data.
|
||||
@@ -190,4 +264,18 @@ void* mbedtls_test_param_failed_get_state_buf( void );
|
||||
void mbedtls_test_param_failed_reset_state( void );
|
||||
#endif /* MBEDTLS_CHECK_PARAMS */
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
||||
#include "test/fake_external_rng_for_test.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
|
||||
/** Permanently activate the mutex usage verification framework. See
|
||||
* threading_helpers.c for information. */
|
||||
void mbedtls_test_mutex_usage_init( void );
|
||||
|
||||
/** Call this function after executing a test case to check for mutex usage
|
||||
* errors. */
|
||||
void mbedtls_test_mutex_usage_check( void );
|
||||
#endif /* MBEDTLS_TEST_MUTEX_USAGE */
|
||||
|
||||
#endif /* TEST_HELPERS_H */
|
||||
|
@@ -51,6 +51,269 @@
|
||||
#include "mbedtls/memory_buffer_alloc.h"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief This macro tests the expression passed to it as a test step or
|
||||
* individual test in a test case.
|
||||
*
|
||||
* It allows a library function to return a value and return an error
|
||||
* code that can be tested.
|
||||
*
|
||||
* When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
|
||||
* callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
|
||||
* failure.
|
||||
*
|
||||
* This macro is not suitable for negative parameter validation tests,
|
||||
* as it assumes the test step will not create an error.
|
||||
*
|
||||
* 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 test expression to be tested.
|
||||
*/
|
||||
#define TEST_ASSERT( TEST ) \
|
||||
do { \
|
||||
if( ! (TEST) ) \
|
||||
{ \
|
||||
mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
|
||||
goto exit; \
|
||||
} \
|
||||
} while( 0 )
|
||||
|
||||
/** 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 ) )
|
||||
|
||||
/** Allocate memory dynamically and fail the test case if this fails.
|
||||
* The allocated memory will be filled with zeros.
|
||||
*
|
||||
* You must set \p pointer to \c NULL before calling this macro and
|
||||
* put `mbedtls_free( pointer )` in the test's cleanup code.
|
||||
*
|
||||
* If \p length is zero, the resulting \p pointer will be \c NULL.
|
||||
* 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.
|
||||
* \param length Number of elements to allocate.
|
||||
* This expression may be evaluated multiple times.
|
||||
*
|
||||
*/
|
||||
#define ASSERT_ALLOC( pointer, length ) \
|
||||
do \
|
||||
{ \
|
||||
TEST_ASSERT( ( pointer ) == NULL ); \
|
||||
if( ( length ) != 0 ) \
|
||||
{ \
|
||||
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
|
||||
( length ) ); \
|
||||
TEST_ASSERT( ( pointer ) != NULL ); \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
/** Allocate memory dynamically. If the allocation fails, skip the test case.
|
||||
*
|
||||
* This macro behaves like #ASSERT_ALLOC, except that if the allocation
|
||||
* fails, it marks the test as skipped rather than failed.
|
||||
*/
|
||||
#define ASSERT_ALLOC_WEAK( pointer, length ) \
|
||||
do \
|
||||
{ \
|
||||
TEST_ASSERT( ( pointer ) == NULL ); \
|
||||
if( ( length ) != 0 ) \
|
||||
{ \
|
||||
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
|
||||
( length ) ); \
|
||||
TEST_ASSUME( ( pointer ) != NULL ); \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
/** 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 )
|
||||
|
||||
/**
|
||||
* \brief This macro tests the expression passed to it and skips the
|
||||
* running test if it doesn't evaluate to 'true'.
|
||||
*
|
||||
* \param TEST The test expression to be tested.
|
||||
*/
|
||||
#define TEST_ASSUME( TEST ) \
|
||||
do { \
|
||||
if( ! (TEST) ) \
|
||||
{ \
|
||||
mbedtls_test_skip( #TEST, __LINE__, __FILE__ ); \
|
||||
goto exit; \
|
||||
} \
|
||||
} while( 0 )
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
|
||||
/**
|
||||
* \brief This macro tests the statement passed to it as a test step or
|
||||
* individual test in a test case. The macro assumes the test will fail
|
||||
* and will generate an error.
|
||||
*
|
||||
* It allows a library function to return a value and tests the return
|
||||
* code on return to confirm the given error code was returned.
|
||||
*
|
||||
* When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
|
||||
* callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
|
||||
* expected failure, and the test will pass.
|
||||
*
|
||||
* This macro is intended for negative parameter validation tests,
|
||||
* where the failing function may return an error value or call
|
||||
* MBEDTLS_PARAM_FAILED() to indicate the error.
|
||||
*
|
||||
* \param PARAM_ERROR_VALUE The expected error code.
|
||||
*
|
||||
* \param TEST The test expression to be tested.
|
||||
*/
|
||||
#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
|
||||
do { \
|
||||
mbedtls_test_param_failed_expect_call( ); \
|
||||
if( ( ( TEST ) != ( PARAM_ERR_VALUE ) ) || \
|
||||
( mbedtls_test_param_failed_check_expected_call( ) != 0 ) ) \
|
||||
{ \
|
||||
mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
|
||||
goto exit; \
|
||||
} \
|
||||
mbedtls_test_param_failed_check_expected_call( ); \
|
||||
} while( 0 )
|
||||
|
||||
/**
|
||||
* \brief This macro tests the statement passed to it as a test step or
|
||||
* individual test in a test case. The macro assumes the test will fail
|
||||
* and will generate an error.
|
||||
*
|
||||
* It assumes the library function under test cannot return a value and
|
||||
* assumes errors can only be indicated byt calls to
|
||||
* MBEDTLS_PARAM_FAILED().
|
||||
*
|
||||
* When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
|
||||
* callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
|
||||
* expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
|
||||
* can be made.
|
||||
*
|
||||
* This macro is intended for negative parameter validation tests,
|
||||
* where the failing function can only return an error by calling
|
||||
* MBEDTLS_PARAM_FAILED() to indicate the error.
|
||||
*
|
||||
* \param TEST The test expression to be tested.
|
||||
*/
|
||||
#define TEST_INVALID_PARAM( TEST ) \
|
||||
do { \
|
||||
memcpy( jmp_tmp, mbedtls_test_param_failed_get_state_buf( ), \
|
||||
sizeof( jmp_tmp ) ); \
|
||||
if( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 ) \
|
||||
{ \
|
||||
TEST; \
|
||||
mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
|
||||
goto exit; \
|
||||
} \
|
||||
mbedtls_test_param_failed_reset_state( ); \
|
||||
} while( 0 )
|
||||
#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
|
||||
|
||||
/**
|
||||
* \brief This macro tests the statement passed to it as a test step or
|
||||
* individual test in a test case. The macro assumes the test will not fail.
|
||||
*
|
||||
* It assumes the library function under test cannot return a value and
|
||||
* assumes errors can only be indicated by calls to
|
||||
* MBEDTLS_PARAM_FAILED().
|
||||
*
|
||||
* When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
|
||||
* callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
|
||||
* expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
|
||||
* can be made.
|
||||
*
|
||||
* This macro is intended to test that functions returning void
|
||||
* accept all of the parameter values they're supposed to accept - eg
|
||||
* that they don't call MBEDTLS_PARAM_FAILED() when a parameter
|
||||
* that's allowed to be NULL happens to be NULL.
|
||||
*
|
||||
* Note: for functions that return something other that void,
|
||||
* checking that they accept all the parameters they're supposed to
|
||||
* accept is best done by using TEST_ASSERT() and checking the return
|
||||
* value as well.
|
||||
*
|
||||
* Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
|
||||
* disabled, as it makes sense to check that the functions accept all
|
||||
* legal values even if this option is disabled - only in that case,
|
||||
* the test is more about whether the function segfaults than about
|
||||
* whether it invokes MBEDTLS_PARAM_FAILED().
|
||||
*
|
||||
* \param TEST The test expression to be tested.
|
||||
*/
|
||||
#define TEST_VALID_PARAM( TEST ) \
|
||||
TEST_ASSERT( ( TEST, 1 ) );
|
||||
|
||||
/** 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.
|
||||
*
|
||||
* If \p length is zero, the resulting \p pointer will be \c NULL.
|
||||
* 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.
|
||||
* \param length Number of elements to allocate.
|
||||
* This expression may be evaluated multiple times.
|
||||
*
|
||||
*/
|
||||
#define ASSERT_ALLOC( pointer, length ) \
|
||||
do \
|
||||
{ \
|
||||
TEST_ASSERT( ( pointer ) == NULL ); \
|
||||
if( ( length ) != 0 ) \
|
||||
{ \
|
||||
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
|
||||
( length ) ); \
|
||||
TEST_ASSERT( ( pointer ) != NULL ); \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
|
||||
{ \
|
||||
mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
|
||||
@@ -58,40 +321,45 @@
|
||||
mbedtls_exit( 1 ); \
|
||||
}
|
||||
|
||||
/** \def ARRAY_LENGTH
|
||||
* 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.
|
||||
*/
|
||||
/* A correct implementation of ARRAY_LENGTH, but which silently gives
|
||||
* a nonsensical result if called with a pointer rather than an array. */
|
||||
#define ARRAY_LENGTH_UNSAFE( array ) \
|
||||
( sizeof( array ) / sizeof( *( array ) ) )
|
||||
|
||||
#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 { unsigned 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 ) ) )
|
||||
|
||||
#else
|
||||
/* If we aren't sure the compiler supports our non-standard tricks,
|
||||
* fall back to the unsafe implementation. */
|
||||
#define ARRAY_LENGTH( array ) ARRAY_LENGTH_UNSAFE( array )
|
||||
#endif
|
||||
|
||||
/** Return the smaller of two values.
|
||||
*
|
||||
* \param x An integer-valued expression without side effects.
|
||||
|
@@ -21,11 +21,74 @@
|
||||
#ifndef PSA_CRYPTO_HELPERS_H
|
||||
#define PSA_CRYPTO_HELPERS_H
|
||||
|
||||
#include "test/helpers.h"
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
|
||||
#include "test/psa_helpers.h"
|
||||
|
||||
#include <psa/crypto.h>
|
||||
#include <psa_crypto_slot_management.h>
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "mbedtls/psa_util.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
||||
|
||||
/* Internal function for #TEST_USES_KEY_ID. Return 1 on success, 0 on failure. */
|
||||
int mbedtls_test_uses_key_id( mbedtls_svc_key_id_t key_id );
|
||||
|
||||
/** Destroy persistent keys recorded with #TEST_USES_KEY_ID.
|
||||
*/
|
||||
void mbedtls_test_psa_purge_key_storage( void );
|
||||
|
||||
/** Purge the in-memory cache of persistent keys recorded with
|
||||
* #TEST_USES_KEY_ID.
|
||||
*
|
||||
* Call this function before calling PSA_DONE() if it's ok for
|
||||
* persistent keys to still exist at this point.
|
||||
*/
|
||||
void mbedtls_test_psa_purge_key_cache( void );
|
||||
|
||||
/** \def TEST_USES_KEY_ID
|
||||
*
|
||||
* Call this macro in a test function before potentially creating a
|
||||
* persistent key. Test functions that use this mechanism must call
|
||||
* mbedtls_test_psa_purge_key_storage() in their cleanup code.
|
||||
*
|
||||
* This macro records a persistent key identifier as potentially used in the
|
||||
* current test case. Recorded key identifiers will be cleaned up at the end
|
||||
* of the test case, even on failure.
|
||||
*
|
||||
* This macro has no effect on volatile keys. Therefore, it is safe to call
|
||||
* this macro in a test function that creates either volatile or persistent
|
||||
* keys depending on the test data.
|
||||
*
|
||||
* This macro currently has no effect on special identifiers
|
||||
* used to store implementation-specific files.
|
||||
*
|
||||
* Calling this macro multiple times on the same key identifier in the same
|
||||
* test case has no effect.
|
||||
*
|
||||
* This macro can fail the test case if there isn't enough memory to
|
||||
* record the key id.
|
||||
*
|
||||
* \param key_id The PSA key identifier to record.
|
||||
*/
|
||||
#define TEST_USES_KEY_ID( key_id ) \
|
||||
TEST_ASSERT( mbedtls_test_uses_key_id( key_id ) )
|
||||
|
||||
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
||||
|
||||
#define TEST_USES_KEY_ID( key_id ) ( (void) ( key_id ) )
|
||||
#define mbedtls_test_psa_purge_key_storage( ) ( (void) 0 )
|
||||
#define mbedtls_test_psa_purge_key_cache( ) ( (void) 0 )
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
||||
|
||||
#define PSA_INIT( ) PSA_ASSERT( psa_crypto_init( ) )
|
||||
|
||||
/** Check for things that have not been cleaned up properly in the
|
||||
* PSA subsystem.
|
||||
*
|
||||
@@ -50,43 +113,40 @@ const char *mbedtls_test_helper_is_psa_leaking( void );
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
/** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots
|
||||
* in use.
|
||||
/** Shut down the PSA Crypto subsystem and destroy persistent keys.
|
||||
* Expect a clean shutdown, with no slots in use.
|
||||
*
|
||||
* If some key slots are still in use, record the test case as failed,
|
||||
* but continue executing. This macro is suitable (and primarily intended)
|
||||
* for use in the cleanup section of test functions.
|
||||
*
|
||||
* \note Persistent keys must be recorded with #TEST_USES_KEY_ID before
|
||||
* creating them.
|
||||
*/
|
||||
#define PSA_DONE( ) \
|
||||
do \
|
||||
{ \
|
||||
test_fail_if_psa_leaking( __LINE__, __FILE__ ); \
|
||||
mbedtls_test_psa_purge_key_storage( ); \
|
||||
mbedtls_psa_crypto_free( ); \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
||||
/** Enable the insecure implementation of mbedtls_psa_external_get_random().
|
||||
/** Shut down the PSA Crypto subsystem, allowing persistent keys to survive.
|
||||
* Expect a clean shutdown, with no slots in use.
|
||||
*
|
||||
* The insecure implementation of mbedtls_psa_external_get_random() is
|
||||
* disabled by default.
|
||||
*
|
||||
* When MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled and the test
|
||||
* helpers are linked into a program, you must enable this before running any
|
||||
* code that uses the PSA subsystem to generate random data (including internal
|
||||
* random generation for purposes such as blinding when the random generation
|
||||
* is routed through PSA).
|
||||
*
|
||||
* You can enable and disable it at any time, regardless of the state
|
||||
* of the PSA subsystem. You may disable it temporarily to simulate a
|
||||
* depleted entropy source.
|
||||
* If some key slots are still in use, record the test case as failed and
|
||||
* jump to the `exit` label.
|
||||
*/
|
||||
void mbedtls_test_enable_insecure_external_rng( void );
|
||||
#define PSA_SESSION_DONE( ) \
|
||||
do \
|
||||
{ \
|
||||
mbedtls_test_psa_purge_key_cache( ); \
|
||||
ASSERT_PSA_PRISTINE( ); \
|
||||
mbedtls_psa_crypto_free( ); \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
/** Disable the insecure implementation of mbedtls_psa_external_get_random().
|
||||
*
|
||||
* See mbedtls_test_enable_insecure_external_rng().
|
||||
*/
|
||||
void mbedtls_test_disable_insecure_external_rng( void );
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
||||
|
||||
|
||||
#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
|
||||
@@ -126,4 +186,113 @@ psa_status_t mbedtls_test_record_status( psa_status_t status,
|
||||
|
||||
#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
|
||||
|
||||
/** Skip a test case if the given key is a 192 bits AES key and the AES
|
||||
* implementation is at least partially provided by an accelerator or
|
||||
* alternative implementation.
|
||||
*
|
||||
* Call this macro in a test case when a cryptographic operation that may
|
||||
* involve an AES operation returns a #PSA_ERROR_NOT_SUPPORTED error code.
|
||||
* The macro call will skip and not fail the test case in case the operation
|
||||
* involves a 192 bits AES key and the AES implementation is at least
|
||||
* partially provided by an accelerator or alternative implementation.
|
||||
*
|
||||
* Hardware AES implementations not supporting 192 bits keys commonly exist.
|
||||
* Consequently, PSA test cases aim at not failing when an AES operation with
|
||||
* a 192 bits key performed by an alternative AES implementation returns
|
||||
* with the #PSA_ERROR_NOT_SUPPORTED error code. The purpose of this macro
|
||||
* is to facilitate this and make the test case code more readable.
|
||||
*
|
||||
* \param key_type Key type
|
||||
* \param key_bits Key length in number of bits.
|
||||
*/
|
||||
#if defined(MBEDTLS_AES_ALT) || \
|
||||
defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
|
||||
defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
|
||||
#define MBEDTLS_TEST_HAVE_ALT_AES 1
|
||||
#else
|
||||
#define MBEDTLS_TEST_HAVE_ALT_AES 0
|
||||
#endif
|
||||
|
||||
#define MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_bits ) \
|
||||
do \
|
||||
{ \
|
||||
if( ( MBEDTLS_TEST_HAVE_ALT_AES ) && \
|
||||
( ( key_type ) == PSA_KEY_TYPE_AES ) && \
|
||||
( key_bits == 192 ) ) \
|
||||
{ \
|
||||
mbedtls_test_skip( "AES-192 not supported", __LINE__, __FILE__ ); \
|
||||
goto exit; \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
/** Skip a test case if a GCM operation with a nonce length different from
|
||||
* 12 bytes fails and was performed by an accelerator or alternative
|
||||
* implementation.
|
||||
*
|
||||
* Call this macro in a test case when an AEAD cryptography operation that
|
||||
* may involve the GCM mode returns with a #PSA_ERROR_NOT_SUPPORTED error
|
||||
* code. The macro call will skip and not fail the test case in case the
|
||||
* operation involves the GCM mode, a nonce with a length different from
|
||||
* 12 bytes and the GCM mode implementation is an alternative one.
|
||||
*
|
||||
* Hardware GCM implementations not supporting nonce lengths different from
|
||||
* 12 bytes commonly exist, as supporting a non-12-byte nonce requires
|
||||
* additional computations involving the GHASH function.
|
||||
* Consequently, PSA test cases aim at not failing when an AEAD operation in
|
||||
* GCM mode with a nonce length different from 12 bytes is performed by an
|
||||
* alternative GCM implementation and returns with a #PSA_ERROR_NOT_SUPPORTED
|
||||
* error code. The purpose of this macro is to facilitate this check and make
|
||||
* the test case code more readable.
|
||||
*
|
||||
* \param alg The AEAD algorithm.
|
||||
* \param nonce_length The nonce length in number of bytes.
|
||||
*/
|
||||
#if defined(MBEDTLS_GCM_ALT) || \
|
||||
defined(MBEDTLS_PSA_ACCEL_ALG_GCM)
|
||||
#define MBEDTLS_TEST_HAVE_ALT_GCM 1
|
||||
#else
|
||||
#define MBEDTLS_TEST_HAVE_ALT_GCM 0
|
||||
#endif
|
||||
|
||||
#define MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, \
|
||||
nonce_length ) \
|
||||
do \
|
||||
{ \
|
||||
if( ( MBEDTLS_TEST_HAVE_ALT_GCM ) && \
|
||||
( PSA_ALG_AEAD_WITH_SHORTENED_TAG( ( alg ) , 0 ) == \
|
||||
PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ) ) && \
|
||||
( ( nonce_length ) != 12 ) ) \
|
||||
{ \
|
||||
mbedtls_test_skip( "GCM with non-12-byte IV is not supported", __LINE__, __FILE__ ); \
|
||||
goto exit; \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
/** \def USE_PSA_INIT
|
||||
*
|
||||
* Call this macro to initialize the PSA subsystem if #MBEDTLS_USE_PSA_CRYPTO
|
||||
* is enabled and do nothing otherwise. If the initialization fails, mark
|
||||
* the test case as failed and jump to the \p exit label.
|
||||
*/
|
||||
/** \def USE_PSA_DONE
|
||||
*
|
||||
* Call this macro at the end of a test case if you called #USE_PSA_INIT.
|
||||
* This is like #PSA_DONE, except that it does nothing if
|
||||
* #MBEDTLS_USE_PSA_CRYPTO is disabled.
|
||||
*/
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#define USE_PSA_INIT( ) PSA_INIT( )
|
||||
#define USE_PSA_DONE( ) PSA_DONE( )
|
||||
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
/* Define empty macros so that we can use them in the preamble and teardown
|
||||
* of every test function that uses PSA conditionally based on
|
||||
* MBEDTLS_USE_PSA_CRYPTO. */
|
||||
#define USE_PSA_INIT( ) ( (void) 0 )
|
||||
#define USE_PSA_DONE( ) ( (void) 0 )
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#endif /* PSA_CRYPTO_HELPERS_H */
|
||||
|
241
tests/include/test/psa_exercise_key.h
Normal file
241
tests/include/test/psa_exercise_key.h
Normal file
@@ -0,0 +1,241 @@
|
||||
/** Code to exercise a PSA key object, i.e. validate that it seems well-formed
|
||||
* and can do what it is supposed to do.
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef PSA_EXERCISE_KEY_H
|
||||
#define PSA_EXERCISE_KEY_H
|
||||
|
||||
#include "test/helpers.h"
|
||||
#include "test/psa_crypto_helpers.h"
|
||||
|
||||
#include <psa/crypto.h>
|
||||
|
||||
/** \def KNOWN_SUPPORTED_HASH_ALG
|
||||
*
|
||||
* A hash algorithm that is known to be supported.
|
||||
*
|
||||
* This is used in some smoke tests.
|
||||
*/
|
||||
#if defined(PSA_WANT_ALG_MD2)
|
||||
#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
|
||||
#elif defined(PSA_WANT_ALG_MD4)
|
||||
#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
|
||||
#elif defined(PSA_WANT_ALG_MD5)
|
||||
#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
|
||||
/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
|
||||
* exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
|
||||
* in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
|
||||
* implausible anyway. */
|
||||
#elif defined(PSA_WANT_ALG_SHA_1)
|
||||
#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
|
||||
#elif defined(PSA_WANT_ALG_SHA_256)
|
||||
#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
|
||||
#elif defined(PSA_WANT_ALG_SHA_384)
|
||||
#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
|
||||
#elif defined(PSA_WANT_ALG_SHA_512)
|
||||
#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
|
||||
#elif defined(PSA_WANT_ALG_SHA3_256)
|
||||
#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
|
||||
#else
|
||||
#undef KNOWN_SUPPORTED_HASH_ALG
|
||||
#endif
|
||||
|
||||
/** \def KNOWN_SUPPORTED_BLOCK_CIPHER
|
||||
*
|
||||
* A block cipher that is known to be supported.
|
||||
*
|
||||
* For simplicity's sake, stick to block ciphers with 16-byte blocks.
|
||||
*/
|
||||
#if defined(MBEDTLS_AES_C)
|
||||
#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
|
||||
#elif defined(MBEDTLS_ARIA_C)
|
||||
#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
|
||||
#elif defined(MBEDTLS_CAMELLIA_C)
|
||||
#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
|
||||
#undef KNOWN_SUPPORTED_BLOCK_CIPHER
|
||||
#endif
|
||||
|
||||
/** \def KNOWN_SUPPORTED_MAC_ALG
|
||||
*
|
||||
* A MAC mode that is known to be supported.
|
||||
*
|
||||
* It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
|
||||
* a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
|
||||
*
|
||||
* This is used in some smoke tests.
|
||||
*/
|
||||
#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
|
||||
#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
|
||||
#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
|
||||
#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
|
||||
#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
|
||||
#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
|
||||
#else
|
||||
#undef KNOWN_SUPPORTED_MAC_ALG
|
||||
#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
|
||||
#endif
|
||||
|
||||
/** \def KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
|
||||
*
|
||||
* A cipher algorithm and key type that are known to be supported.
|
||||
*
|
||||
* This is used in some smoke tests.
|
||||
*/
|
||||
#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
|
||||
#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
|
||||
#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
|
||||
#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
|
||||
#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
|
||||
#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
|
||||
#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
|
||||
#else
|
||||
#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
|
||||
#endif
|
||||
#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
|
||||
#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
|
||||
#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
|
||||
#elif defined(MBEDTLS_RC4_C)
|
||||
#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
|
||||
#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
|
||||
#else
|
||||
#undef KNOWN_SUPPORTED_CIPHER_ALG
|
||||
#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
|
||||
#endif
|
||||
|
||||
/** Convenience function to set up a key derivation.
|
||||
*
|
||||
* In case of failure, mark the current test case as failed.
|
||||
*
|
||||
* The inputs \p input1 and \p input2 are, in order:
|
||||
* - HKDF: salt, info.
|
||||
* - TKS 1.2 PRF, TLS 1.2 PSK-to-MS: seed, label.
|
||||
*
|
||||
* \param operation The operation object to use.
|
||||
* It must be in the initialized state.
|
||||
* \param key The key to use.
|
||||
* \param alg The algorithm to use.
|
||||
* \param input1 The first input to pass.
|
||||
* \param input1_length The length of \p input1 in bytes.
|
||||
* \param input2 The first input to pass.
|
||||
* \param input2_length The length of \p input2 in bytes.
|
||||
* \param capacity The capacity to set.
|
||||
*
|
||||
* \return \c 1 on success, \c 0 on failure.
|
||||
*/
|
||||
int mbedtls_test_psa_setup_key_derivation_wrap(
|
||||
psa_key_derivation_operation_t* operation,
|
||||
mbedtls_svc_key_id_t key,
|
||||
psa_algorithm_t alg,
|
||||
const unsigned char* input1, size_t input1_length,
|
||||
const unsigned char* input2, size_t input2_length,
|
||||
size_t capacity );
|
||||
|
||||
/** Perform a key agreement using the given key pair against its public key
|
||||
* using psa_raw_key_agreement().
|
||||
*
|
||||
* The result is discarded. The purpose of this function is to smoke-test a key.
|
||||
*
|
||||
* In case of failure, mark the current test case as failed.
|
||||
*
|
||||
* \param alg A key agreement algorithm compatible with \p key.
|
||||
* \param key A key that allows key agreement with \p alg.
|
||||
*
|
||||
* \return \c 1 on success, \c 0 on failure.
|
||||
*/
|
||||
psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
|
||||
psa_algorithm_t alg,
|
||||
mbedtls_svc_key_id_t key );
|
||||
|
||||
/** Perform a key agreement using the given key pair against its public key
|
||||
* using psa_key_derivation_raw_key().
|
||||
*
|
||||
* The result is discarded. The purpose of this function is to smoke-test a key.
|
||||
*
|
||||
* In case of failure, mark the current test case as failed.
|
||||
*
|
||||
* \param operation An operation that has been set up for a key
|
||||
* agreement algorithm that is compatible with
|
||||
* \p key.
|
||||
* \param key A key pair object that is suitable for a key
|
||||
* agreement with \p operation.
|
||||
*
|
||||
* \return \c 1 on success, \c 0 on failure.
|
||||
*/
|
||||
psa_status_t mbedtls_test_psa_key_agreement_with_self(
|
||||
psa_key_derivation_operation_t *operation,
|
||||
mbedtls_svc_key_id_t key );
|
||||
|
||||
/** Perform sanity checks on the given key representation.
|
||||
*
|
||||
* If any of the checks fail, mark the current test case as failed.
|
||||
*
|
||||
* The checks depend on the key type.
|
||||
* - All types: check the export size against maximum-size macros.
|
||||
* - DES: parity bits.
|
||||
* - RSA: check the ASN.1 structure and the size and parity of the integers.
|
||||
* - ECC private or public key: exact representation length.
|
||||
* - Montgomery public key: first byte.
|
||||
*
|
||||
* \param type The key type.
|
||||
* \param bits The key size in bits.
|
||||
* \param exported A buffer containing the key representation.
|
||||
* \param exported_length The length of \p exported in bytes.
|
||||
*
|
||||
* \return \c 1 if all checks passed, \c 0 on failure.
|
||||
*/
|
||||
int mbedtls_test_psa_exported_key_sanity_check(
|
||||
psa_key_type_t type, size_t bits,
|
||||
const uint8_t *exported, size_t exported_length );
|
||||
|
||||
/** Do smoke tests on a key.
|
||||
*
|
||||
* Perform one of each operation indicated by \p alg (decrypt/encrypt,
|
||||
* sign/verify, or derivation) that is permitted according to \p usage.
|
||||
* \p usage and \p alg should correspond to the expected policy on the
|
||||
* key.
|
||||
*
|
||||
* Export the key if permitted by \p usage, and check that the output
|
||||
* looks sensible. If \p usage forbids export, check that
|
||||
* \p psa_export_key correctly rejects the attempt. If the key is
|
||||
* asymmetric, also check \p psa_export_public_key.
|
||||
*
|
||||
* If the key fails the tests, this function calls the test framework's
|
||||
* `mbedtls_test_fail` function and returns false. Otherwise this function
|
||||
* returns true. Therefore it should be used as follows:
|
||||
* ```
|
||||
* if( ! exercise_key( ... ) ) goto exit;
|
||||
* ```
|
||||
*
|
||||
* \param key The key to exercise. It should be capable of performing
|
||||
* \p alg.
|
||||
* \param usage The usage flags to assume.
|
||||
* \param alg The algorithm to exercise.
|
||||
*
|
||||
* \retval 0 The key failed the smoke tests.
|
||||
* \retval 1 The key passed the smoke tests.
|
||||
*/
|
||||
int mbedtls_test_psa_exercise_key( mbedtls_svc_key_id_t key,
|
||||
psa_key_usage_t usage,
|
||||
psa_algorithm_t alg );
|
||||
|
||||
psa_key_usage_t mbedtls_test_psa_usage_to_exercise( psa_key_type_t type,
|
||||
psa_algorithm_t alg );
|
||||
|
||||
#endif /* PSA_EXERCISE_KEY_H */
|
Reference in New Issue
Block a user