Remove pk_info from pk_context_t with SINGLE_TYPE

In very reduced configurations, we don't want the overhead of maintaining a
bool just to remember if the context is valid and checking that bit at every
point of entry.

Note: so far this validity bit also served as a proxy to ensure that pk_ctx
was valid (currently this is a pointer to a dynamically-allocated buffer). In
the next series of commits, this will be changed to a statically-allocated
buffer, so there will be no question about its validity.

In the end (after this commit and the next series), a pk_context_t will be
(memory-wise) just the same as a mbedtls_uecc_keypair when SINGLE_TYPE is
enabled - meaning the PK layer will have zero memory overhead in that case.
This commit is contained in:
Manuel Pégourié-Gonnard 2019-09-19 10:45:14 +02:00
parent 2829bbf59b
commit 073c1e1391
4 changed files with 20 additions and 6 deletions

View File

@ -142,7 +142,9 @@ typedef const mbedtls_pk_info_t *mbedtls_pk_handle_t;
*/
typedef struct mbedtls_pk_context
{
#if !defined(MBEDTLS_PK_SINGLE_TYPE)
mbedtls_pk_handle_t pk_info; /**< Public key information */
#endif
void * pk_ctx; /**< Underlying public key context */
} mbedtls_pk_context;

View File

@ -234,7 +234,11 @@ struct mbedtls_pk_info_t
/*
* Macros to access pk_info
*/
#if defined(MBEDTLS_PK_SINGLE_TYPE)
#define MBEDTLS_PK_CTX_INFO( ctx ) MBEDTLS_PK_UNIQUE_VALID_HANDLE
#else
#define MBEDTLS_PK_CTX_INFO( ctx ) ( (ctx)->pk_info )
#endif
#define MBEDTLS_PK_CTX_IS_VALID( ctx ) \
( MBEDTLS_PK_CTX_INFO( (ctx) ) != MBEDTLS_PK_INVALID_HANDLE )

View File

@ -1299,7 +1299,9 @@ void mbedtls_pk_init( mbedtls_pk_context *ctx )
{
PK_VALIDATE( ctx != NULL );
#if !defined(MBEDTLS_PK_SINGLE_TYPE)
ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
#endif
ctx->pk_ctx = NULL;
}
@ -1394,17 +1396,19 @@ mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info )
{
PK_VALIDATE_RET( ctx != NULL );
if( info == MBEDTLS_PK_INVALID_HANDLE ||
MBEDTLS_PK_CTX_IS_VALID( ctx ) )
{
if( info == MBEDTLS_PK_INVALID_HANDLE )
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
}
#if !defined(MBEDTLS_PK_SINGLE_TYPE)
if( ctx->pk_info != NULL )
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
ctx->pk_info = info;
#endif
if( ( ctx->pk_ctx = pk_info_ctx_alloc_func( info ) ) == NULL )
return( MBEDTLS_ERR_PK_ALLOC_FAILED );
ctx->pk_info = info;
return( 0 );
}

View File

@ -95,9 +95,11 @@ size_t mbedtls_rsa_key_len_func( void *ctx )
void valid_parameters( )
{
mbedtls_pk_context pk;
#if !defined(MBEDTLS_PK_SINGLE_TYPE)
unsigned char buf[1];
size_t len;
void *options = NULL;
#endif
mbedtls_pk_init( &pk );
@ -118,6 +120,7 @@ void valid_parameters( )
TEST_ASSERT( mbedtls_pk_get_len( NULL ) == 0 );
TEST_ASSERT( mbedtls_pk_can_do( NULL, MBEDTLS_PK_NONE ) == 0 );
#if !defined(MBEDTLS_PK_SINGLE_TYPE)
TEST_ASSERT( mbedtls_pk_sign_restartable( &pk,
MBEDTLS_MD_NONE,
NULL, 0,
@ -172,6 +175,7 @@ void valid_parameters( )
NULL, &len, 0,
rnd_std_rand, NULL ) ==
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
#endif /* MBEDTLS_PK_SINGLE_TYPE */
#if defined(MBEDTLS_PK_PARSE_C)
TEST_ASSERT( mbedtls_pk_parse_key( &pk, NULL, 0, NULL, 1 ) ==