exported_key_sanity_check: simplify the logic for public keys

Remove a conditional imbrication level. Get rid of some minor overhead
for ECC public keys dating back from when they had ASN.1 wrapping.

No behavior change.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-02-14 01:19:21 +01:00
parent e50b578218
commit ad557e58bf

View File

@ -659,13 +659,11 @@ int mbedtls_test_psa_exported_key_sanity_check(
else else
#endif /* MBEDTLS_ECP_C */ #endif /* MBEDTLS_ECP_C */
if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
{
uint8_t *p = exported;
uint8_t *end = exported + exported_length;
#if defined(MBEDTLS_RSA_C) #if defined(MBEDTLS_RSA_C)
if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
{ {
uint8_t *p = exported;
uint8_t *end = exported + exported_length;
size_t len; size_t len;
/* RSAPublicKey ::= SEQUENCE { /* RSAPublicKey ::= SEQUENCE {
* modulus INTEGER, -- n * modulus INTEGER, -- n
@ -684,6 +682,7 @@ int mbedtls_test_psa_exported_key_sanity_check(
} }
else else
#endif /* MBEDTLS_RSA_C */ #endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_ECP_C) #if defined(MBEDTLS_ECP_C)
if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
{ {
@ -691,7 +690,7 @@ int mbedtls_test_psa_exported_key_sanity_check(
{ {
/* The representation of an ECC Montgomery public key is /* The representation of an ECC Montgomery public key is
* the raw compressed point */ * the raw compressed point */
TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end ); TEST_EQUAL( PSA_BITS_TO_BYTES( bits ), exported_length );
} }
else else
{ {
@ -701,23 +700,22 @@ int mbedtls_test_psa_exported_key_sanity_check(
* - `y_P` as a `ceiling(m/8)`-byte string, big-endian; * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
* - where m is the bit size associated with the curve. * - where m is the bit size associated with the curve.
*/ */
TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end ); TEST_EQUAL( 1 + 2 * PSA_BITS_TO_BYTES( bits ), exported_length );
TEST_EQUAL( p[0], 4 ); TEST_EQUAL( exported[0], 4 );
} }
} }
else else
#endif /* MBEDTLS_ECP_C */ #endif /* MBEDTLS_ECP_C */
if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
{ {
char message[47]; char message[47];
mbedtls_snprintf( message, sizeof( message ), mbedtls_snprintf( message, sizeof( message ),
"No sanity check for public key type=0x%08lx", "No sanity check for public key type=0x%08lx",
(unsigned long) type ); (unsigned long) type );
mbedtls_test_fail( message, __LINE__, __FILE__ ); mbedtls_test_fail( message, __LINE__, __FILE__ );
(void) p;
(void) end;
return( 0 ); return( 0 );
} }
}
else else
{ {