From 073c1e139140d13be6b40380088ecb4f2893d60d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 19 Sep 2019 10:45:14 +0200 Subject: [PATCH] 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. --- include/mbedtls/pk.h | 2 ++ include/mbedtls/pk_internal.h | 4 ++++ library/pk.c | 16 ++++++++++------ tests/suites/test_suite_pk.function | 4 ++++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/mbedtls/pk.h b/include/mbedtls/pk.h index 64cd7b5e6..09a851391 100644 --- a/include/mbedtls/pk.h +++ b/include/mbedtls/pk.h @@ -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; diff --git a/include/mbedtls/pk_internal.h b/include/mbedtls/pk_internal.h index 94dd9aaa9..5258cb1da 100644 --- a/include/mbedtls/pk_internal.h +++ b/include/mbedtls/pk_internal.h @@ -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 ) diff --git a/library/pk.c b/library/pk.c index d6bf97650..dfa9a68ff 100644 --- a/library/pk.c +++ b/library/pk.c @@ -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 ); } diff --git a/tests/suites/test_suite_pk.function b/tests/suites/test_suite_pk.function index 163585584..b446f49b8 100644 --- a/tests/suites/test_suite_pk.function +++ b/tests/suites/test_suite_pk.function @@ -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 ) ==