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,66 +659,64 @@ 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 ) ) #if defined(MBEDTLS_RSA_C)
if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
{ {
uint8_t *p = exported; uint8_t *p = exported;
uint8_t *end = exported + exported_length; uint8_t *end = exported + exported_length;
#if defined(MBEDTLS_RSA_C) size_t len;
if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) /* RSAPublicKey ::= SEQUENCE {
{ * modulus INTEGER, -- n
size_t len; * publicExponent INTEGER } -- e
/* RSAPublicKey ::= SEQUENCE { */
* modulus INTEGER, -- n TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
* publicExponent INTEGER } -- e MBEDTLS_ASN1_SEQUENCE |
*/ MBEDTLS_ASN1_CONSTRUCTED ),
TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, 0 );
MBEDTLS_ASN1_SEQUENCE | TEST_EQUAL( p + len, end );
MBEDTLS_ASN1_CONSTRUCTED ), if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) )
0 ); goto exit;
TEST_EQUAL( p + len, end ); if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) )
if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ) goto exit;
goto exit; TEST_EQUAL( p, end );
if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) ) }
goto exit; else
TEST_EQUAL( p, end );
}
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 ) )
{
if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
{ {
if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY ) /* The representation of an ECC Montgomery public key is
{ * the raw compressed point */
/* The representation of an ECC Montgomery public key is TEST_EQUAL( PSA_BITS_TO_BYTES( bits ), exported_length );
* the raw compressed point */
TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
}
else
{
/* The representation of an ECC Weierstrass public key is:
* - The byte 0x04;
* - `x_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.
*/
TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
TEST_EQUAL( p[0], 4 );
}
} }
else else
#endif /* MBEDTLS_ECP_C */
{ {
char message[47]; /* The representation of an ECC Weierstrass public key is:
mbedtls_snprintf( message, sizeof( message ), * - The byte 0x04;
"No sanity check for public key type=0x%08lx", * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
(unsigned long) type ); * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
mbedtls_test_fail( message, __LINE__, __FILE__ ); * - where m is the bit size associated with the curve.
(void) p; */
(void) end; TEST_EQUAL( 1 + 2 * PSA_BITS_TO_BYTES( bits ), exported_length );
return( 0 ); TEST_EQUAL( exported[0], 4 );
} }
} }
else else
#endif /* MBEDTLS_ECP_C */
if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
{
char message[47];
mbedtls_snprintf( message, sizeof( message ),
"No sanity check for public key type=0x%08lx",
(unsigned long) type );
mbedtls_test_fail( message, __LINE__, __FILE__ );
return( 0 );
}
else
{ {
/* No sanity checks for other types */ /* No sanity checks for other types */