mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-30 22:43:08 +03:00
Add buffer protection for cipher functions
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
This commit is contained in:
@ -4405,14 +4405,20 @@ exit:
|
|||||||
}
|
}
|
||||||
|
|
||||||
psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
|
psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
|
||||||
const uint8_t *input,
|
const uint8_t *input_external,
|
||||||
size_t input_length,
|
size_t input_length,
|
||||||
uint8_t *output,
|
uint8_t *output_external,
|
||||||
size_t output_size,
|
size_t output_size,
|
||||||
size_t *output_length)
|
size_t *output_length)
|
||||||
{
|
{
|
||||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||||
|
|
||||||
|
LOCAL_INPUT_DECLARE(input_external, input);
|
||||||
|
LOCAL_OUTPUT_DECLARE(output_external, output);
|
||||||
|
|
||||||
|
LOCAL_INPUT_ALLOC(input_external, input_length, input);
|
||||||
|
LOCAL_OUTPUT_ALLOC_WITH_COPY(output_external, output_size, output);
|
||||||
|
|
||||||
if (operation->id == 0) {
|
if (operation->id == 0) {
|
||||||
status = PSA_ERROR_BAD_STATE;
|
status = PSA_ERROR_BAD_STATE;
|
||||||
goto exit;
|
goto exit;
|
||||||
@ -4435,16 +4441,23 @@ exit:
|
|||||||
psa_cipher_abort(operation);
|
psa_cipher_abort(operation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOCAL_INPUT_FREE(input_external, input);
|
||||||
|
LOCAL_OUTPUT_FREE(output_external, output);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
|
psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
|
||||||
uint8_t *output,
|
uint8_t *output_external,
|
||||||
size_t output_size,
|
size_t output_size,
|
||||||
size_t *output_length)
|
size_t *output_length)
|
||||||
{
|
{
|
||||||
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
||||||
|
|
||||||
|
LOCAL_OUTPUT_DECLARE(output_external, output);
|
||||||
|
|
||||||
|
LOCAL_OUTPUT_ALLOC_WITH_COPY(output_external, output_size, output);
|
||||||
|
|
||||||
if (operation->id == 0) {
|
if (operation->id == 0) {
|
||||||
status = PSA_ERROR_BAD_STATE;
|
status = PSA_ERROR_BAD_STATE;
|
||||||
goto exit;
|
goto exit;
|
||||||
@ -4462,13 +4475,15 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
|
|||||||
|
|
||||||
exit:
|
exit:
|
||||||
if (status == PSA_SUCCESS) {
|
if (status == PSA_SUCCESS) {
|
||||||
return psa_cipher_abort(operation);
|
status = psa_cipher_abort(operation);
|
||||||
} else {
|
} else {
|
||||||
*output_length = 0;
|
*output_length = 0;
|
||||||
(void) psa_cipher_abort(operation);
|
(void) psa_cipher_abort(operation);
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOCAL_OUTPUT_FREE(output_external, output);
|
||||||
|
|
||||||
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
|
psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
|
||||||
@ -4573,9 +4588,9 @@ exit:
|
|||||||
|
|
||||||
psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
|
psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
|
||||||
psa_algorithm_t alg,
|
psa_algorithm_t alg,
|
||||||
const uint8_t *input,
|
const uint8_t *input_external,
|
||||||
size_t input_length,
|
size_t input_length,
|
||||||
uint8_t *output,
|
uint8_t *output_external,
|
||||||
size_t output_size,
|
size_t output_size,
|
||||||
size_t *output_length)
|
size_t *output_length)
|
||||||
{
|
{
|
||||||
@ -4584,6 +4599,12 @@ psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
|
|||||||
psa_key_slot_t *slot = NULL;
|
psa_key_slot_t *slot = NULL;
|
||||||
psa_key_attributes_t attributes;
|
psa_key_attributes_t attributes;
|
||||||
|
|
||||||
|
LOCAL_INPUT_DECLARE(input_external, input);
|
||||||
|
LOCAL_OUTPUT_DECLARE(output_external, output);
|
||||||
|
|
||||||
|
LOCAL_INPUT_ALLOC(input_external, input_length, input);
|
||||||
|
LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
|
||||||
|
|
||||||
if (!PSA_ALG_IS_CIPHER(alg)) {
|
if (!PSA_ALG_IS_CIPHER(alg)) {
|
||||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||||
goto exit;
|
goto exit;
|
||||||
@ -4624,6 +4645,9 @@ exit:
|
|||||||
*output_length = 0;
|
*output_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOCAL_INPUT_FREE(input_external, input);
|
||||||
|
LOCAL_OUTPUT_FREE(output_external, output);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user