From 9467ea343b182e38449f787c24852a805e5c67d6 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Wed, 8 Nov 2023 17:44:18 +0000 Subject: [PATCH] Add psa_crypto_output_copy_free() implementation Signed-off-by: David Horstmann --- library/psa_crypto.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index c733b28ff8..0e2fb3f946 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -8534,4 +8534,24 @@ psa_status_t psa_crypto_output_copy_alloc(uint8_t *output, size_t output_len, return PSA_SUCCESS; } +psa_status_t psa_crypto_output_copy_free(psa_crypto_output_copy_t *output_copy) +{ + if (output_copy->buffer == NULL) { + output_copy->len = 0; + return PSA_SUCCESS; + } + if (output_copy->original == NULL) { + /* We have an internal copy but nothing to copy back to. */ + return PSA_ERROR_CORRUPTION_DETECTED; + } + + memcpy(output_copy->original, output_copy->buffer, output_copy->len); + + mbedtls_free(output_copy->buffer); + output_copy->buffer = NULL; + output_copy->len = 0; + + return PSA_SUCCESS; +} + #endif /* MBEDTLS_PSA_CRYPTO_C */