@@ -714,20 +714,24 @@ static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
714
714
*
715
715
* \return The key size in bits, calculated from the key data.
716
716
*/
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 )
718
718
{
719
+ size_t bits = 0 ; /* return 0 on an empty slot */
720
+
719
721
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 );
721
723
#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 ) );
724
726
#endif /* defined(MBEDTLS_RSA_C) */
725
727
#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 ;
728
730
#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 );
731
735
}
732
736
733
737
/** Import key data into a slot. `slot->attr.type` must have been set
0 commit comments