From 4cfaae5b6b98c8f05071650a33f7b0098bf35b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 23 Jun 2022 09:43:39 +0200 Subject: [PATCH] Save code size by calling get_type only once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is an external function, so in the absence of link-time optimisation (LTO) the compiler can't know anything about it and has to call it the number of times it's called in the source code. This only matters for pk_ec, but change pk_rsa as well for the sake of uniformity. Signed-off-by: Manuel Pégourié-Gonnard --- include/mbedtls/pk.h | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/include/mbedtls/pk.h b/include/mbedtls/pk.h index 30f0492e9..867961d32 100644 --- a/include/mbedtls/pk.h +++ b/include/mbedtls/pk.h @@ -722,9 +722,13 @@ mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx ); */ static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk ) { - return( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA ? - (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx) : - NULL ); + switch( mbedtls_pk_get_type( &pk ) ) + { + case MBEDTLS_PK_RSA: + return( (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx) ); + default: + return( NULL ); + } } #endif /* MBEDTLS_RSA_C */ @@ -742,11 +746,15 @@ static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk ) */ static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk ) { - return( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY || - mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH || - mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECDSA ? - (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx) : - NULL ); + switch( mbedtls_pk_get_type( &pk ) ) + { + case MBEDTLS_PK_ECKEY: + case MBEDTLS_PK_ECKEY_DH: + case MBEDTLS_PK_ECDSA: + return( (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx) ); + default: + return( NULL ); + } } #endif /* MBEDTLS_ECP_C */