From 953cd5b372ffb0bbe15e52d5faed139d5252af0b Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Wed, 8 Nov 2023 15:19:43 +0000 Subject: [PATCH] Add implementation of psa_crypto_output_alloc() Signed-off-by: David Horstmann --- library/psa_crypto.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index d4c566dd47..669087cdb1 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -5591,4 +5591,28 @@ void psa_crypto_input_copy_free(psa_crypto_input_copy_t *input_copy) input_copy->len = 0; } +psa_status_t psa_crypto_output_copy_alloc(uint8_t *output, size_t output_len, + psa_crypto_output_copy_t *output_copy) +{ + output_copy->original = NULL; + output_copy->buffer = NULL; + output_copy->len = 0; + + /* Treat NULL and zero-length input the same. + * This is simpler than potentially calling calloc(0). */ + if (output == NULL || output_len == 0) { + return PSA_SUCCESS; + } + output_copy->buffer = mbedtls_calloc(output_len, 1); + if (output_copy->buffer == NULL) { + /* Since we dealt with the zero-length case above, we know that + * a NULL return value means a failure of allocation. */ + return PSA_ERROR_INSUFFICIENT_MEMORY; + } + output_copy->len = output_len; + output_copy->original = output; + + return PSA_SUCCESS; +} + #endif /* MBEDTLS_PSA_CRYPTO_C */