New macro PSA_KEY_TYPE_IS_RSA

This commit is contained in:
Gilles Peskine 2018-06-29 19:51:51 +02:00 committed by itayzafrir
parent 365b984e38
commit d8008d6dfe
2 changed files with 13 additions and 18 deletions

View File

@ -433,7 +433,11 @@ typedef uint32_t psa_key_type_t;
/** Whether a key type is an RSA key pair or public key. */ /** Whether a key type is an RSA key pair or public key. */
#define PSA_KEY_TYPE_IS_RSA(type) \ #define PSA_KEY_TYPE_IS_RSA(type) \
(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY) (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
/** Whether a key type is an elliptic curve key pair or public key. */ /** Whether a key type is an RSA key (pair or public-only). */
#define PSA_KEY_TYPE_IS_RSA(type) \
(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == \
PSA_KEY_TYPE_RSA_PUBLIC_KEY)
/** Whether a key type is an elliptic curve key (pair or public-only). */
#define PSA_KEY_TYPE_IS_ECC(type) \ #define PSA_KEY_TYPE_IS_ECC(type) \
((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \ ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE) ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)

View File

@ -567,9 +567,7 @@ psa_status_t psa_import_key( psa_key_slot_t key,
} }
else else
#if defined(MBEDTLS_PK_PARSE_C) #if defined(MBEDTLS_PK_PARSE_C)
if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
type == PSA_KEY_TYPE_RSA_KEYPAIR ||
PSA_KEY_TYPE_IS_ECC( type ) )
{ {
int ret; int ret;
mbedtls_pk_context pk; mbedtls_pk_context pk;
@ -584,8 +582,7 @@ psa_status_t psa_import_key( psa_key_slot_t key,
{ {
#if defined(MBEDTLS_RSA_C) #if defined(MBEDTLS_RSA_C)
case MBEDTLS_PK_RSA: case MBEDTLS_PK_RSA:
if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || if( PSA_KEY_TYPE_IS_RSA( type ) )
type == PSA_KEY_TYPE_RSA_KEYPAIR )
{ {
mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk ); mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
size_t bits = mbedtls_rsa_get_bitlen( rsa ); size_t bits = mbedtls_rsa_get_bitlen( rsa );
@ -662,8 +659,7 @@ psa_status_t psa_destroy_key( psa_key_slot_t key )
} }
else else
#if defined(MBEDTLS_RSA_C) #if defined(MBEDTLS_RSA_C)
if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
{ {
mbedtls_rsa_free( slot->data.rsa ); mbedtls_rsa_free( slot->data.rsa );
mbedtls_free( slot->data.rsa ); mbedtls_free( slot->data.rsa );
@ -694,8 +690,7 @@ static size_t psa_get_key_bits( const key_slot_t *slot )
if( key_type_is_raw_bytes( slot->type ) ) if( key_type_is_raw_bytes( slot->type ) )
return( slot->data.raw.bytes * 8 ); return( slot->data.raw.bytes * 8 );
#if defined(MBEDTLS_RSA_C) #if defined(MBEDTLS_RSA_C)
if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
return( mbedtls_rsa_get_bitlen( slot->data.rsa ) ); return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
#endif /* defined(MBEDTLS_RSA_C) */ #endif /* defined(MBEDTLS_RSA_C) */
#if defined(MBEDTLS_ECP_C) #if defined(MBEDTLS_ECP_C)
@ -769,15 +764,13 @@ static psa_status_t psa_internal_export_key( psa_key_slot_t key,
else else
{ {
#if defined(MBEDTLS_PK_WRITE_C) #if defined(MBEDTLS_PK_WRITE_C)
if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
PSA_KEY_TYPE_IS_ECC( slot->type ) ) PSA_KEY_TYPE_IS_ECC( slot->type ) )
{ {
mbedtls_pk_context pk; mbedtls_pk_context pk;
int ret; int ret;
mbedtls_pk_init( &pk ); mbedtls_pk_init( &pk );
if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
{ {
pk.pk_info = &mbedtls_rsa_info; pk.pk_info = &mbedtls_rsa_info;
pk.pk_ctx = slot->data.rsa; pk.pk_ctx = slot->data.rsa;
@ -2064,8 +2057,7 @@ psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
return( status ); return( status );
#if defined(MBEDTLS_RSA_C) #if defined(MBEDTLS_RSA_C)
if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR || if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
{ {
return( psa_rsa_verify( slot->data.rsa, return( psa_rsa_verify( slot->data.rsa,
alg, alg,
@ -2120,8 +2112,7 @@ psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
return( PSA_ERROR_INVALID_ARGUMENT ); return( PSA_ERROR_INVALID_ARGUMENT );
#if defined(MBEDTLS_RSA_C) #if defined(MBEDTLS_RSA_C)
if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR || if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
{ {
mbedtls_rsa_context *rsa = slot->data.rsa; mbedtls_rsa_context *rsa = slot->data.rsa;
int ret; int ret;