mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-29 11:41:15 +03:00
Remove psa_crypto_alloc_and_copy() API
This tied input and output buffers together in awkward pairs, which made the API more difficult to use. Signed-off-by: David Horstmann <david.horstmann@arm.com>
This commit is contained in:
@ -8464,89 +8464,4 @@ psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_co
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
psa_status_t ret;
|
||||
/* Zeroize the buffers struct to ensure we can call free()
|
||||
* on any pointers safely. */
|
||||
memset(buffers, 0, sizeof(*buffers));
|
||||
|
||||
/* Since calloc() may return NULL if we try to allocate zero-length
|
||||
* buffers anyway, deal with this corner case explicitly to ensure
|
||||
* predictable behaviour. Represent zero-length buffers as NULL. */
|
||||
if (input_len == 0) {
|
||||
input = NULL;
|
||||
}
|
||||
if (output_len == 0) {
|
||||
output = NULL;
|
||||
}
|
||||
|
||||
if (output != NULL) {
|
||||
buffers->output = mbedtls_calloc(output_len, 1);
|
||||
if (buffers->output == NULL) {
|
||||
ret = PSA_ERROR_INSUFFICIENT_MEMORY;
|
||||
goto error;
|
||||
}
|
||||
buffers->output_len = output_len;
|
||||
|
||||
buffers->output_original = output;
|
||||
}
|
||||
|
||||
if (input != NULL) {
|
||||
buffers->input = mbedtls_calloc(input_len, 1);
|
||||
if (buffers->input == NULL) {
|
||||
ret = PSA_ERROR_INSUFFICIENT_MEMORY;
|
||||
goto error;
|
||||
}
|
||||
buffers->input_len = input_len;
|
||||
|
||||
if (psa_crypto_copy_input(input, input_len,
|
||||
buffers->input, buffers->input_len)
|
||||
!= PSA_SUCCESS) {
|
||||
ret = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
return PSA_SUCCESS;
|
||||
|
||||
error:
|
||||
mbedtls_free(buffers->input);
|
||||
mbedtls_free(buffers->output);
|
||||
memset(buffers, 0, sizeof(*buffers));
|
||||
return ret;
|
||||
}
|
||||
|
||||
psa_status_t psa_crypto_copy_and_free(psa_crypto_buffer_copy_t *buffers)
|
||||
{
|
||||
if ((buffers->input != NULL) && (buffers->input_len == 0)) {
|
||||
/* Reject zero-length buffers, these should have been represented by
|
||||
* NULL in psa_crypto_alloc_and_copy() */
|
||||
return PSA_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (buffers->output != NULL) {
|
||||
if (buffers->output_len == 0) {
|
||||
/* Reject zero-length buffers, these should have been represented
|
||||
* by NULL in psa_crypto_alloc_and_copy() */
|
||||
return PSA_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (buffers->output_original == NULL) {
|
||||
/* Output is non-NULL but original output is NULL. The argument
|
||||
* buffers is invalid. Return an error as we have no original to
|
||||
* copy back to. */
|
||||
return PSA_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
memcpy(buffers->output_original, buffers->output, buffers->output_len);
|
||||
}
|
||||
|
||||
mbedtls_free(buffers->input);
|
||||
buffers->input = NULL;
|
||||
mbedtls_free(buffers->output);
|
||||
buffers->output = NULL;
|
||||
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
@ -884,57 +884,4 @@ 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 */
|
||||
|
Reference in New Issue
Block a user