Check for output allocation before randomising

Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
This commit is contained in:
Thomas Daubney 2024-02-21 12:28:20 +00:00
parent 26d1c43821
commit 0736df33ac

View File

@ -5298,6 +5298,7 @@ psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
size_t expected_length; size_t expected_length;
LOCAL_INPUT_DECLARE(peer_key_external, peer_key); LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
LOCAL_OUTPUT_DECLARE(output_external, output); LOCAL_OUTPUT_DECLARE(output_external, output);
LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
status = PSA_ERROR_INVALID_ARGUMENT; status = PSA_ERROR_INVALID_ARGUMENT;
@ -5325,14 +5326,16 @@ psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
} }
LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key); LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
status = psa_key_agreement_raw_internal(alg, slot, status = psa_key_agreement_raw_internal(alg, slot,
peer_key, peer_key_length, peer_key, peer_key_length,
output, output_size, output, output_size,
output_length); output_length);
exit: exit:
if (status != PSA_SUCCESS && output != NULL) { /* Check for successful allocation of output. */
if (output != NULL && status != PSA_ERROR_INSUFFICIENT_MEMORY) {
/* output allocated. */
if (status != PSA_SUCCESS) {
/* If an error happens and is not handled properly, the output /* If an error happens and is not handled properly, the output
* may be used as a key to protect sensitive data. Arrange for such * may be used as a key to protect sensitive data. Arrange for such
* a key to be random, which is likely to result in decryption or * a key to be random, which is likely to result in decryption or
@ -5343,6 +5346,10 @@ exit:
psa_generate_random(output, output_size); psa_generate_random(output, output_size);
*output_length = output_size; *output_length = output_size;
} }
} else {
/* output allocation failed. */
*output_length = 0;
}
unlock_status = psa_unlock_key_slot(slot); unlock_status = psa_unlock_key_slot(slot);