1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-07-28 00:21:48 +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:
David Horstmann
2023-11-06 14:22:28 +00:00
parent 7dd8205423
commit b3de69493c
4 changed files with 0 additions and 483 deletions

View File

@ -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 */