diff --git a/library/psa_crypto_core.h b/library/psa_crypto_core.h index 4c38a7566f..78550f8ee5 100644 --- a/library/psa_crypto_core.h +++ b/library/psa_crypto_core.h @@ -884,4 +884,57 @@ psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len, psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len, uint8_t *output, size_t output_len); +/** + * \brief Structure to store a pair of copied buffers (input, output) with a + * reference to the original output to be used during copy-back. + */ +struct psa_crypto_buffer_copy_s { + uint8_t *input; + size_t input_len; + + uint8_t *output_original; + uint8_t *output; + size_t output_len; +}; +typedef struct psa_crypto_buffer_copy_s psa_crypto_buffer_copy_t; + +/** + * \brief Allocate copies of provided input and output + * buffers and store references to them along with + * the original output buffer in the provided + * psa_crypto_buffer_copy_t struct. + * Either or both buffers may be NULL, in which case + * they are not copied. + * + * \note The input and output buffers may overlap. + * + * \param[in] input Pointer to the input buffer. + * \param[in] input_len Length of the input buffer. + * \param[in] output Pointer to the output buffer. + * \param[in] output_len Length of the output buffer. + * \param[out] buffers Struct containing pointers to the allocated + * copies and the original output buffer. + * \retval #PSA_SUCCESS + * The buffers were successfully allocated and copied. + * \retval #PSA_ERROR_INSUFFICIENT_MEMORY + * Failed to allocate buffers. + */ +psa_status_t psa_crypto_alloc_and_copy(const uint8_t *input, size_t input_len, + uint8_t *output, size_t output_len, + psa_crypto_buffer_copy_t *buffers); + +/** + * \brief Free an allocated pair of buffers after first + * copying the output buffer back to its original. + * + * \param[in] buffers psa_crypto_buffer_copy_t created by a previous + * call to psa_crypto_alloc_and_copy(). + * \retval #PSA_SUCCESS + * The buffers were successfully copied-back and the + * copies freed. + * \retval #PSA_ERROR_INVALID_ARGUMENT + * Could not copy-back as \p buffers is invalid. + */ +psa_status_t psa_crypto_copy_and_free(psa_crypto_buffer_copy_t *buffers); + #endif /* PSA_CRYPTO_CORE_H */