Skip to content

Commit 8908c5e

Browse files
Make psa_calculate_key_bits return psa_key_bits_t
This is cleaner and solves a complaint from MSVC about truncation from size_t to psa_key_bits_t.
1 parent 1b8594a commit 8908c5e

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

library/psa_crypto.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -714,20 +714,24 @@ static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
714714
*
715715
* \return The key size in bits, calculated from the key data.
716716
*/
717-
static size_t psa_calculate_key_bits( const psa_key_slot_t *slot )
717+
static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
718718
{
719+
size_t bits = 0; /* return 0 on an empty slot */
720+
719721
if( key_type_is_raw_bytes( slot->attr.type ) )
720-
return( slot->data.raw.bytes * 8 );
722+
bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
721723
#if defined(MBEDTLS_RSA_C)
722-
if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
723-
return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
724+
else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
725+
bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
724726
#endif /* defined(MBEDTLS_RSA_C) */
725727
#if defined(MBEDTLS_ECP_C)
726-
if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
727-
return( slot->data.ecp->grp.pbits );
728+
else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
729+
bits = slot->data.ecp->grp.pbits;
728730
#endif /* defined(MBEDTLS_ECP_C) */
729-
/* Shouldn't happen except on an empty slot. */
730-
return( 0 );
731+
732+
/* We know that the size fits in psa_key_bits_t thanks to checks
733+
* when the key was created. */
734+
return( (psa_key_bits_t) bits );
731735
}
732736

733737
/** Import key data into a slot. `slot->attr.type` must have been set

0 commit comments

Comments
 (0)