tests: psa: Simplify key buffer size calculation

Move the key buffer size calculation code under
tests to avoid check-names.sh to complain about
"likely macros with typos".

This removes the calculation of key buffer
sizes for the test driver from the wrapper based on
static size data. But the code is still there in test
code to be used when we go back to work on the
generation of the driver wrapper.

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
This commit is contained in:
Ronald Cron 2021-04-14 10:55:34 +02:00
parent 7f13fa2454
commit 4607c829d0
3 changed files with 67 additions and 95 deletions

View file

@ -268,37 +268,9 @@ psa_status_t psa_driver_wrapper_get_key_buffer_size(
return( PSA_SUCCESS ); return( PSA_SUCCESS );
} }
#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
#ifdef MBEDTLS_TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
*key_buffer_size = mbedtls_test_size_function( key_type, key_bits ); *key_buffer_size = mbedtls_test_size_function( key_type, key_bits );
return( PSA_SUCCESS ); return( ( *key_buffer_size != 0 ) ?
#else /* MBEDTLS_TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */ PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
{
int public_key_overhead =
( ( MBEDTLS_TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 )
? PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
*key_buffer_size = MBEDTLS_TEST_DRIVER_KEY_CONTEXT_BASE_SIZE +
MBEDTLS_TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE +
public_key_overhead;
}
else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
{
*key_buffer_size = MBEDTLS_TEST_DRIVER_KEY_CONTEXT_BASE_SIZE +
MBEDTLS_TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
}
else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
!PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
{
*key_buffer_size = MBEDTLS_TEST_DRIVER_KEY_CONTEXT_BASE_SIZE +
( MBEDTLS_TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR *
( ( key_bits + 7 ) / 8 ) );
}
else
{
return( PSA_ERROR_NOT_SUPPORTED );
}
return( PSA_SUCCESS );
#endif /* MBEDTLS_TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
#endif /* PSA_CRYPTO_DRIVER_TEST */ #endif /* PSA_CRYPTO_DRIVER_TEST */
default: default:

View file

@ -29,68 +29,9 @@
#if defined(PSA_CRYPTO_DRIVER_TEST) #if defined(PSA_CRYPTO_DRIVER_TEST)
#include <psa/crypto_driver_common.h> #include <psa/crypto_driver_common.h>
typedef struct {
unsigned int context;
} mbedtls_test_driver_key_context_t;
/** \def MBEDTLS_TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
*
* This macro returns the base size for the key context. It is the size of the
* driver specific information stored in each key context.
*/
#define MBEDTLS_TEST_DRIVER_KEY_CONTEXT_BASE_SIZE \
sizeof( mbedtls_test_driver_key_context_t )
/** \def MBEDTLS_TEST_DRIVER_KEY_CONTEXT_KEY_PAIR_SIZE
*
* Number of bytes included in every key context for a key pair.
*
* This pair size is for an ECC 256-bit private/public key pair.
* Based on this value, the size of the private key can be derived by
* subtracting the public key size below from this one.
*/
#define MBEDTLS_TEST_DRIVER_KEY_CONTEXT_KEY_PAIR_SIZE 65
/** \def MBEDTLS_TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
*
* Number of bytes included in every key context for a public key.
*
* For ECC public keys, it needs 257 bits so 33 bytes.
*/
#define MBEDTLS_TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE 33
/** \def MBEDTLS_TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
*
* Every key context for a symmetric key includes this many times the key size.
*/
#define MBEDTLS_TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR 0
/** \def MBEDTLS_TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY
*
* If this is true for a key pair, the key context includes space for the public key.
* If this is false, no additional space is added for the public key.
*
* For this instance, store the public key with the private one.
*/
#define MBEDTLS_TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY 1
/** \def MBEDTLS_TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
*
* If MBEDTLS_TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION is defined, the test driver
* provides a size_function entry point, otherwise, it does not.
*
* Some opaque drivers have the need to support a custom size for the storage
* of key and context information. The size_function provides the ability to
* provide that customization.
*/
//#define MBEDTLS_TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
#ifdef MBEDTLS_TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
size_t mbedtls_test_size_function( size_t mbedtls_test_size_function(
const psa_key_type_t key_type, const psa_key_type_t key_type,
const size_t key_bits ); const size_t key_bits );
#endif /* MBEDTLS_TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
#endif /* PSA_CRYPTO_DRIVER_TEST */ #endif /* PSA_CRYPTO_DRIVER_TEST */
#endif /* PSA_CRYPTO_TEST_DRIVERS_SIZE_H */ #endif /* PSA_CRYPTO_TEST_DRIVERS_SIZE_H */

View file

@ -27,16 +27,75 @@
#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST) #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
#include "test/drivers/size.h" #include "test/drivers/size.h"
#include "psa/crypto.h"
typedef struct {
unsigned int context;
} test_driver_key_context_t;
/*
* This macro returns the base size for the key context. It is the size of the
* driver specific information stored in each key context.
*/
#define TEST_DRIVER_KEY_CONTEXT_BASE_SIZE sizeof( test_driver_key_context_t )
/*
* Number of bytes included in every key context for a key pair.
*
* This pair size is for an ECC 256-bit private/public key pair.
* Based on this value, the size of the private key can be derived by
* subtracting the public key size below from this one.
*/
#define TEST_DRIVER_KEY_CONTEXT_KEY_PAIR_SIZE 65
/*
* Number of bytes included in every key context for a public key.
*
* For ECC public keys, it needs 257 bits so 33 bytes.
*/
#define TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE 33
/*
* Every key context for a symmetric key includes this many times the key size.
*/
#define TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR 0
/*
* If this is true for a key pair, the key context includes space for the public key.
* If this is false, no additional space is added for the public key.
*
* For this instance, store the public key with the private one.
*/
#define TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY 1
#ifdef MBEDTLS_TEST_KEY_CONTEXT_SIZE_FUNCTION
size_t mbedtls_test_size_function( size_t mbedtls_test_size_function(
const psa_key_type_t key_type, const psa_key_type_t key_type,
const size_t key_bits ) const size_t key_bits )
{ {
(void) key_type; size_t key_buffer_size = 0;
(void) key_bits;
return 0;
}
#endif /*MBEDTLS_TEST_KEY_CONTEXT_SIZE_FUNCTION */
if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
{
int public_key_overhead =
( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 )
? PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE +
TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE +
public_key_overhead;
}
else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
{
key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE +
TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
}
else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
!PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
{
key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE +
( TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR *
( ( key_bits + 7 ) / 8 ) );
}
return( key_buffer_size );
}
#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */