Save code size by calling get_type only once

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 <manuel.pegourie-gonnard@arm.com>
This commit is contained in:
Manuel Pégourié-Gonnard 2022-06-23 09:43:39 +02:00
parent a82a8b9f4b
commit 4cfaae5b6b

View File

@ -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 ) static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
{ {
return( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA ? switch( mbedtls_pk_get_type( &pk ) )
(mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx) : {
NULL ); case MBEDTLS_PK_RSA:
return( (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx) );
default:
return( NULL );
}
} }
#endif /* MBEDTLS_RSA_C */ #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 ) static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
{ {
return( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY || switch( mbedtls_pk_get_type( &pk ) )
mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH || {
mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECDSA ? case MBEDTLS_PK_ECKEY:
(mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx) : case MBEDTLS_PK_ECKEY_DH:
NULL ); case MBEDTLS_PK_ECDSA:
return( (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx) );
default:
return( NULL );
}
} }
#endif /* MBEDTLS_ECP_C */ #endif /* MBEDTLS_ECP_C */