From 021e72493610a43a486cb94bf574c2d8587f13b9 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Tue, 16 Nov 2021 10:32:48 +0000 Subject: [PATCH 1/3] 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 406e6c4cf..3c7598955 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 ); From bc92abed8c56dcd6cc20bbdfc1f60239def43d51 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Tue, 16 Nov 2021 12:05:38 +0000 Subject: [PATCH 2/3] Update test to handle changed error code Update test to handle changed error code from psa_key_derivation_output_key Signed-off-by: Dave Rodgman --- tests/suites/test_suite_psa_crypto.data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index dc2479ae0..332845b12 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -3246,7 +3246,7 @@ PSA key derivation: HKDF-SHA-256, bad key type, key output depends_on:PSA_WANT_ALG_HKDF:PSA_WANT_ALG_SHA_256 # Whether we get NOT_PERMITTED or BAD_STATE for the output is an implementation # detail. -derive_input:PSA_ALG_HKDF(PSA_ALG_SHA_256):PSA_KEY_DERIVATION_INPUT_SALT:PSA_KEY_TYPE_NONE:"":PSA_SUCCESS:PSA_KEY_DERIVATION_INPUT_SECRET:PSA_KEY_TYPE_RAW_DATA:"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b":PSA_ERROR_INVALID_ARGUMENT:PSA_KEY_DERIVATION_INPUT_INFO:PSA_KEY_TYPE_NONE:"":PSA_ERROR_BAD_STATE:PSA_KEY_TYPE_RAW_DATA:PSA_ERROR_NOT_PERMITTED +derive_input:PSA_ALG_HKDF(PSA_ALG_SHA_256):PSA_KEY_DERIVATION_INPUT_SALT:PSA_KEY_TYPE_NONE:"":PSA_SUCCESS:PSA_KEY_DERIVATION_INPUT_SECRET:PSA_KEY_TYPE_RAW_DATA:"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b":PSA_ERROR_INVALID_ARGUMENT:PSA_KEY_DERIVATION_INPUT_INFO:PSA_KEY_TYPE_NONE:"":PSA_ERROR_BAD_STATE:PSA_KEY_TYPE_RAW_DATA:PSA_ERROR_BAD_STATE PSA key derivation: HKDF-SHA-256, direct secret, direct output depends_on:PSA_WANT_ALG_HKDF:PSA_WANT_ALG_SHA_256 From dc4e4b72c08bec44774447635d30d459765fe453 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Tue, 16 Nov 2021 12:12:49 +0000 Subject: [PATCH 3/3] Fix derive_input test ignoring parameter Fix derive_input test hardcoding key type instead of using test argument. Signed-off-by: Dave Rodgman --- tests/suites/test_suite_psa_crypto.function | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 2eab8e50a..9ed142423 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -4464,7 +4464,7 @@ void derive_input( int alg_arg, if( output_key_type != PSA_KEY_TYPE_NONE ) { psa_reset_key_attributes( &attributes ); - psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); + psa_set_key_type( &attributes, output_key_type ); psa_set_key_bits( &attributes, 8 ); actual_output_status = psa_key_derivation_output_key( &attributes, &operation,