1
0
mirror of https://github.com/ARMmbed/mbedtls.git synced 2025-07-24 01:51:03 +08:00

Protect key_derivation_output_bytes

If the alloc fails I belive it is okay to preserve the algorithm.
The alloc cannot fail with BAD_STATE, and this setting is only used
to differentiate between a exhausted and blank.

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
This commit is contained in:
Ryan Everett 2024-01-19 14:46:39 +00:00
parent d1e398c374
commit f943e22bb9

View File

@ -5801,10 +5801,12 @@ static psa_status_t psa_key_derivation_pbkdf2_read(
psa_status_t psa_key_derivation_output_bytes( psa_status_t psa_key_derivation_output_bytes(
psa_key_derivation_operation_t *operation, psa_key_derivation_operation_t *operation,
uint8_t *output, uint8_t *output_external,
size_t output_length) size_t output_length)
{ {
psa_status_t status; psa_status_t status;
LOCAL_OUTPUT_DECLARE(output_external, output);
psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
if (operation->alg == 0) { if (operation->alg == 0) {
@ -5828,6 +5830,8 @@ psa_status_t psa_key_derivation_output_bytes(
* output_length > 0. */ * output_length > 0. */
return PSA_ERROR_INSUFFICIENT_DATA; return PSA_ERROR_INSUFFICIENT_DATA;
} }
LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
operation->capacity -= output_length; operation->capacity -= output_length;
#if defined(BUILTIN_ALG_ANY_HKDF) #if defined(BUILTIN_ALG_ANY_HKDF)
@ -5861,10 +5865,15 @@ psa_status_t psa_key_derivation_output_bytes(
{ {
(void) kdf_alg; (void) kdf_alg;
return PSA_ERROR_BAD_STATE; status = PSA_ERROR_BAD_STATE;
LOCAL_OUTPUT_FREE(output_external, output);
return status;
} }
exit: exit:
LOCAL_OUTPUT_FREE(output_external, output);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
/* Preserve the algorithm upon errors, but clear all sensitive state. /* Preserve the algorithm upon errors, but clear all sensitive state.
* This allows us to differentiate between exhausted operations and * This allows us to differentiate between exhausted operations and