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>
diff --git a/include/mbedtls/pk.h b/include/mbedtls/pk.h
index 017557a..c9a13f4 100644
--- a/include/mbedtls/pk.h
+++ b/include/mbedtls/pk.h
@@ -643,9 +643,13 @@
*/
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).pk_ctx :
- NULL );
+ switch( mbedtls_pk_get_type( &pk ) )
+ {
+ case MBEDTLS_PK_RSA:
+ return( (mbedtls_rsa_context *) (pk).pk_ctx );
+ default:
+ return( NULL );
+ }
}
#endif /* MBEDTLS_RSA_C */
@@ -663,11 +667,15 @@
*/
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).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).pk_ctx );
+ default:
+ return( NULL );
+ }
}
#endif /* MBEDTLS_ECP_C */