From 021e72493610a43a486cb94bf574c2d8587f13b9 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Tue, 16 Nov 2021 10:32:48 +0000 Subject: [PATCH] Improve PSA error return code psa_key_derivation_output_key: prioritize BAD_STATE over NOT_PERMITTED If psa_key_derivation_output_key() is called on an operation which hasn't been set up or which has been aborted, return PSA_ERROR_BAD_STATE. Only return PSA_ERROR_NOT_PERMITTED if the operation state is ok for psa_key_derivation_input_bytes() or psa_key_derivation_output_bytes() but not ok to output a key. Ideally psa_key_derivation_output_key() would return PSA_ERROR_NOT_PERMITTED only when psa_key_derivation_output_bytes() is possible, but this is clumsier to implement. Signed-off-by: Dave Rodgman --- library/psa_crypto.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 406e6c4cfb..3c75989551 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -4282,6 +4282,9 @@ psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attribut if( psa_get_key_bits( attributes ) == 0 ) return( PSA_ERROR_INVALID_ARGUMENT ); + if( operation->alg == PSA_ALG_NONE ) + return( PSA_ERROR_BAD_STATE ); + if( ! operation->can_output_key ) return( PSA_ERROR_NOT_PERMITTED );