Test attempts to use a public key for a private-key operation

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-04-29 21:56:59 +02:00
parent 0c3a071300
commit e6300959df
4 changed files with 48 additions and 16 deletions

View file

@ -104,6 +104,10 @@ class KeyType:
`self.name`. `self.name`.
""" """
def is_public(self) -> bool:
"""Whether the key type is for public keys."""
return self.name.endswith('_PUBLIC_KEY')
ECC_KEY_SIZES = { ECC_KEY_SIZES = {
'PSA_ECC_FAMILY_SECP_K1': (192, 224, 256), 'PSA_ECC_FAMILY_SECP_K1': (192, 224, 256),
'PSA_ECC_FAMILY_SECP_R1': (225, 256, 384, 521), 'PSA_ECC_FAMILY_SECP_R1': (225, 256, 384, 521),
@ -242,8 +246,17 @@ class AlgorithmCategory(enum.Enum):
PAKE = 10 PAKE = 10
def requires_key(self) -> bool: def requires_key(self) -> bool:
"""Whether operations in this category are set up with a key."""
return self not in {self.HASH, self.KEY_DERIVATION} return self not in {self.HASH, self.KEY_DERIVATION}
def is_asymmetric(self) -> bool:
"""Whether operations in this category involve asymmetric keys."""
return self in {
self.SIGN,
self.ASYMMETRIC_ENCRYPTION,
self.KEY_AGREEMENT
}
class AlgorithmNotRecognized(Exception): class AlgorithmNotRecognized(Exception):
def __init__(self, expr: str) -> None: def __init__(self, expr: str) -> None:

View file

@ -314,6 +314,7 @@ class OpFail:
NOT_SUPPORTED = 0 NOT_SUPPORTED = 0
INVALID = 1 INVALID = 1
INCOMPATIBLE = 2 INCOMPATIBLE = 2
PUBLIC = 3
def __init__(self, info: Information) -> None: def __init__(self, info: Information) -> None:
self.constructors = info.constructors self.constructors = info.constructors
@ -363,6 +364,8 @@ class OpFail:
key_material = kt.key_material(kt.sizes_to_test()[0]) key_material = kt.key_material(kt.sizes_to_test()[0])
arguments += [key_type, test_case.hex_string(key_material)] arguments += [key_type, test_case.hex_string(key_material)]
arguments.append(alg.expression) arguments.append(alg.expression)
if category.is_asymmetric():
arguments.append('1' if reason == self.Reason.PUBLIC else '0')
error = ('NOT_SUPPORTED' if reason == self.Reason.NOT_SUPPORTED else error = ('NOT_SUPPORTED' if reason == self.Reason.NOT_SUPPORTED else
'INVALID_ARGUMENT') 'INVALID_ARGUMENT')
arguments.append('PSA_ERROR_' + error) arguments.append('PSA_ERROR_' + error)
@ -393,13 +396,17 @@ class OpFail:
"""Generate failure test cases for one-key operations with the specified algorithm.""" """Generate failure test cases for one-key operations with the specified algorithm."""
for kt in self.key_types: for kt in self.key_types:
key_is_compatible = kt.can_do(alg) key_is_compatible = kt.can_do(alg)
# To do: public key for a private key operation
if key_is_compatible and alg.can_do(category): if key_is_compatible and alg.can_do(category):
# Compatible key and operation, unsupported algorithm # Compatible key and operation, unsupported algorithm
for dep in automatic_dependencies(alg.base_expression): for dep in automatic_dependencies(alg.base_expression):
yield self.make_test_case(alg, category, yield self.make_test_case(alg, category,
self.Reason.NOT_SUPPORTED, self.Reason.NOT_SUPPORTED,
kt=kt, not_deps=frozenset([dep])) kt=kt, not_deps=frozenset([dep]))
# Public key for a private-key operation
if category.is_asymmetric() and kt.is_public():
yield self.make_test_case(alg, category,
self.Reason.PUBLIC,
kt=kt)
elif key_is_compatible: elif key_is_compatible:
# Compatible key, incompatible operation, supported algorithm # Compatible key, incompatible operation, supported algorithm
yield self.make_test_case(alg, category, yield self.make_test_case(alg, category,

View file

@ -211,7 +211,8 @@ exit:
/* BEGIN_CASE */ /* BEGIN_CASE */
void sign_fail( int key_type_arg, data_t *key_data, void sign_fail( int key_type_arg, data_t *key_data,
int alg_arg, int expected_status_arg ) int alg_arg, int private_only,
int expected_status_arg )
{ {
psa_status_t expected_status = expected_status_arg; psa_status_t expected_status = expected_status_arg;
psa_key_type_t key_type = key_type_arg; psa_key_type_t key_type = key_type_arg;
@ -237,10 +238,13 @@ void sign_fail( int key_type_arg, data_t *key_data,
psa_sign_hash( key_id, alg, psa_sign_hash( key_id, alg,
input, sizeof( input ), input, sizeof( input ),
output, sizeof( output ), &length ) ); output, sizeof( output ), &length ) );
TEST_STATUS( expected_status, if( ! private_only )
psa_verify_hash( key_id, alg, {
input, sizeof( input ), TEST_STATUS( expected_status,
output, sizeof( output ) ) ); psa_verify_hash( key_id, alg,
input, sizeof( input ),
output, sizeof( output ) ) );
}
exit: exit:
psa_destroy_key( key_id ); psa_destroy_key( key_id );
@ -251,7 +255,8 @@ exit:
/* BEGIN_CASE */ /* BEGIN_CASE */
void asymmetric_encryption_fail( int key_type_arg, data_t *key_data, void asymmetric_encryption_fail( int key_type_arg, data_t *key_data,
int alg_arg, int expected_status_arg ) int alg_arg, int private_only,
int expected_status_arg )
{ {
psa_status_t expected_status = expected_status_arg; psa_status_t expected_status = expected_status_arg;
psa_key_type_t key_type = key_type_arg; psa_key_type_t key_type = key_type_arg;
@ -273,12 +278,15 @@ void asymmetric_encryption_fail( int key_type_arg, data_t *key_data,
key_data->x, key_data->len, key_data->x, key_data->len,
&key_id ) ); &key_id ) );
TEST_STATUS( expected_status, if( ! private_only )
psa_asymmetric_encrypt( key_id, alg, {
plaintext, 1, TEST_STATUS( expected_status,
NULL, 0, psa_asymmetric_encrypt( key_id, alg,
ciphertext, sizeof( ciphertext ), plaintext, 1,
&length ) ); NULL, 0,
ciphertext, sizeof( ciphertext ),
&length ) );
}
TEST_STATUS( expected_status, TEST_STATUS( expected_status,
psa_asymmetric_decrypt( key_id, alg, psa_asymmetric_decrypt( key_id, alg,
ciphertext, sizeof( ciphertext ), ciphertext, sizeof( ciphertext ),
@ -313,7 +321,8 @@ exit:
/* BEGIN_CASE */ /* BEGIN_CASE */
void key_agreement_fail( int key_type_arg, data_t *key_data, void key_agreement_fail( int key_type_arg, data_t *key_data,
int alg_arg, int expected_status_arg ) int alg_arg, int private_only,
int expected_status_arg )
{ {
psa_status_t expected_status = expected_status_arg; psa_status_t expected_status = expected_status_arg;
psa_key_type_t key_type = key_type_arg; psa_key_type_t key_type = key_type_arg;
@ -359,6 +368,9 @@ void key_agreement_fail( int key_type_arg, data_t *key_data,
public_key, public_key_length ) ); public_key, public_key_length ) );
#endif #endif
/* There are no public-key operations. */
(void) private_only;
exit: exit:
psa_key_derivation_abort( &operation ); psa_key_derivation_abort( &operation );
psa_destroy_key( key_id ); psa_destroy_key( key_id );

View file

@ -8,8 +8,8 @@ hash_fail:PSA_ALG_ECDSA_ANY:PSA_ERROR_INVALID_ARGUMENT
PSA sign RSA_PSS(SHA_256): incompatible key type PSA sign RSA_PSS(SHA_256): incompatible key type
depends_on:PSA_WANT_ALG_RSA_PSS:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_AES depends_on:PSA_WANT_ALG_RSA_PSS:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_AES
sign_fail:PSA_KEY_TYPE_AES:"48657265006973206b6579a064617461":PSA_ALG_RSA_PSS(PSA_ALG_SHA_256):PSA_ERROR_INVALID_ARGUMENT sign_fail:PSA_KEY_TYPE_AES:"48657265006973206b6579a064617461":PSA_ALG_RSA_PSS(PSA_ALG_SHA_256):0:PSA_ERROR_INVALID_ARGUMENT
PSA sign RSA_PSS(SHA_256): RSA_PSS not enabled, key pair PSA sign RSA_PSS(SHA_256): RSA_PSS not enabled, key pair
depends_on:!PSA_WANT_ALG_RSA_PSS:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR depends_on:!PSA_WANT_ALG_RSA_PSS:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR
sign_fail:PSA_KEY_TYPE_RSA_KEY_PAIR:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24":PSA_ALG_RSA_PSS(PSA_ALG_SHA_256):PSA_ERROR_NOT_SUPPORTED sign_fail:PSA_KEY_TYPE_RSA_KEY_PAIR:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24":PSA_ALG_RSA_PSS(PSA_ALG_SHA_256):0:PSA_ERROR_NOT_SUPPORTED