Improve error codes in p256-m driver

Fix 19 out of 20 errors in test_suite_psa_crypto mentioned in the
previous commit. The remaining error will be fix in the next commit.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
This commit is contained in:
Manuel Pégourié-Gonnard 2023-08-08 12:23:42 +02:00
parent 25b45db3d8
commit f0251e0824

View File

@ -118,9 +118,9 @@ psa_status_t p256_transparent_export_public_key(const psa_key_attributes_t *attr
return PSA_ERROR_NOT_SUPPORTED; return PSA_ERROR_NOT_SUPPORTED;
} }
/* Validate input and output sizes */ /* Validate sizes, as p256-m expects fixed-size buffers */
if (key_buffer_size != 32) { if (key_buffer_size != 32) {
return PSA_ERROR_INVALID_ARGUMENT; return PSA_ERROR_CORRUPTION_DETECTED;
} }
if (data_size < 65) { if (data_size < 65) {
return PSA_ERROR_BUFFER_TOO_SMALL; return PSA_ERROR_BUFFER_TOO_SMALL;
@ -129,13 +129,11 @@ psa_status_t p256_transparent_export_public_key(const psa_key_attributes_t *attr
/* Output public key in the PSA export format */ /* Output public key in the PSA export format */
data[0] = 0x04; data[0] = 0x04;
int ret = p256_public_from_private(data + 1, key_buffer); int ret = p256_public_from_private(data + 1, key_buffer);
if (ret != P256_SUCCESS) { if (ret == P256_SUCCESS) {
/* The only possible error is the private key was invalid */ *data_length = 65;
return PSA_ERROR_INVALID_ARGUMENT;
} }
*data_length = 65;
return PSA_SUCCESS; return p256_to_psa_error(ret);
} }
psa_status_t p256_transparent_generate_key( psa_status_t p256_transparent_generate_key(
@ -148,13 +146,9 @@ psa_status_t p256_transparent_generate_key(
* of driver entry-points. (void) used to avoid compiler warning. */ * of driver entry-points. (void) used to avoid compiler warning. */
(void) attributes; (void) attributes;
psa_status_t status = PSA_ERROR_NOT_SUPPORTED; /* Validate sizes, as p256-m expects fixed-size buffers */
/*
* p256-m generates a 32 byte private key, and expects to write to a buffer
* that is of that size. */
if (key_buffer_size != 32) { if (key_buffer_size != 32) {
return status; return PSA_ERROR_BUFFER_TOO_SMALL;
} }
/* /*
@ -164,13 +158,12 @@ psa_status_t p256_transparent_generate_key(
* function as an argument. */ * function as an argument. */
uint8_t public_key_buffer[64]; uint8_t public_key_buffer[64];
status = p256_to_psa_error( int ret = p256_gen_keypair(key_buffer, public_key_buffer);
p256_gen_keypair(key_buffer, public_key_buffer)); if (ret == P256_SUCCESS) {
if (status == PSA_SUCCESS) {
*key_buffer_length = 32; *key_buffer_length = 32;
} }
return status; return p256_to_psa_error(ret);
} }
psa_status_t p256_transparent_key_agreement( psa_status_t p256_transparent_key_agreement(
@ -190,25 +183,23 @@ psa_status_t p256_transparent_key_agreement(
(void) attributes; (void) attributes;
(void) alg; (void) alg;
/* /* Validate sizes, as p256-m expects fixed-size buffers */
* Check that private key = 32 bytes, peer public key = 65 bytes, if (key_buffer_size != 32 || peer_key_length != 65) {
* and that the shared secret buffer is big enough. */ return PSA_ERROR_INVALID_ARGUMENT;
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; }
if (key_buffer_size != 32 || shared_secret_size < 32 || if (shared_secret_size < 32) {
peer_key_length != 65) { return PSA_ERROR_BUFFER_TOO_SMALL;
return status;
} }
/* We add 1 to peer_key pointer to omit the leading byte of the public key /* We add 1 to peer_key pointer to omit the leading byte of the public key
* representation (0x04). See information about PSA key formats at the top * representation (0x04). See information about PSA key formats at the top
* of the file. */ * of the file. */
status = p256_to_psa_error( int ret = p256_ecdh_shared_secret(shared_secret, key_buffer, peer_key + 1);
p256_ecdh_shared_secret(shared_secret, key_buffer, peer_key+1)); if (ret == P256_SUCCESS) {
if (status == PSA_SUCCESS) {
*shared_secret_length = 32; *shared_secret_length = 32;
} }
return status; return p256_to_psa_error(ret);
} }
psa_status_t p256_transparent_sign_hash( psa_status_t p256_transparent_sign_hash(
@ -228,18 +219,20 @@ psa_status_t p256_transparent_sign_hash(
(void) attributes; (void) attributes;
(void) alg; (void) alg;
psa_status_t status = PSA_ERROR_NOT_SUPPORTED; /* Validate sizes, as p256-m expects fixed-size buffers */
if (key_buffer_size != 32 || signature_size < 64) { if (key_buffer_size != 32) {
return status; return PSA_ERROR_CORRUPTION_DETECTED;
}
if (signature_size < 64) {
return PSA_ERROR_BUFFER_TOO_SMALL;
} }
status = p256_to_psa_error( int ret = p256_ecdsa_sign(signature, key_buffer, hash, hash_length);
p256_ecdsa_sign(signature, key_buffer, hash, hash_length)); if (ret == P256_SUCCESS) {
if (status == PSA_SUCCESS) {
*signature_length = 64; *signature_length = 64;
} }
return status; return p256_to_psa_error(ret);
} }
/* This function expects the key buffer to contain a 65 byte public key, /* This function expects the key buffer to contain a 65 byte public key,
@ -252,19 +245,21 @@ static psa_status_t p256_verify_hash_with_public_key(
const uint8_t *signature, const uint8_t *signature,
size_t signature_length) size_t signature_length)
{ {
psa_status_t status = PSA_ERROR_NOT_SUPPORTED; /* Validate sizes, as p256-m expects fixed-size buffers */
if (key_buffer_size != 65 || signature_length != 64 || *key_buffer != 0x04) { if (key_buffer_size != 65 || *key_buffer != 0x04) {
return status; return PSA_ERROR_CORRUPTION_DETECTED;
}
if (signature_length != 64) {
return PSA_ERROR_INVALID_SIGNATURE;
} }
/* We add 1 to public_key_buffer pointer to omit the leading byte of the /* We add 1 to public_key_buffer pointer to omit the leading byte of the
* public key representation (0x04). See information about PSA key formats * public key representation (0x04). See information about PSA key formats
* at the top of the file. */ * at the top of the file. */
const uint8_t *public_key_buffer = key_buffer + 1; const uint8_t *public_key_buffer = key_buffer + 1;
status = p256_to_psa_error( int ret = p256_ecdsa_verify(signature, public_key_buffer, hash, hash_length);
p256_ecdsa_verify(signature, public_key_buffer, hash, hash_length));
return status; return p256_to_psa_error(ret);
} }
psa_status_t p256_transparent_verify_hash( psa_status_t p256_transparent_verify_hash(