1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-07-30 22:43:08 +03:00

Implement safe buffer copying in MAC API

Use buffer local copy macros to implement safe
copy mechanism in MAC API.

Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
This commit is contained in:
Thomas Daubney
2024-01-18 13:18:58 +00:00
parent ad736991bb
commit 8db8d1a83e

View File

@ -2652,35 +2652,45 @@ psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
} }
psa_status_t psa_mac_update(psa_mac_operation_t *operation, psa_status_t psa_mac_update(psa_mac_operation_t *operation,
const uint8_t *input, const uint8_t *input_external,
size_t input_length) size_t input_length)
{ {
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
LOCAL_INPUT_DECLARE(input_external, input);
if (operation->id == 0) { if (operation->id == 0) {
return PSA_ERROR_BAD_STATE; status = PSA_ERROR_BAD_STATE;
return status;
} }
/* Don't require hash implementations to behave correctly on a /* Don't require hash implementations to behave correctly on a
* zero-length input, which may have an invalid pointer. */ * zero-length input, which may have an invalid pointer. */
if (input_length == 0) { if (input_length == 0) {
return PSA_SUCCESS; status = PSA_SUCCESS;
return status;
} }
psa_status_t status = psa_driver_wrapper_mac_update(operation, LOCAL_INPUT_ALLOC(input_external, input_length, input);
input, input_length); status = psa_driver_wrapper_mac_update(operation, input, input_length);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
psa_mac_abort(operation); psa_mac_abort(operation);
} }
exit:
LOCAL_INPUT_FREE(input_external, input);
return status; return status;
} }
psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
uint8_t *mac, uint8_t *mac_external,
size_t mac_size, size_t mac_size,
size_t *mac_length) size_t *mac_length)
{ {
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
LOCAL_OUTPUT_DECLARE(mac_external, mac);
if (operation->id == 0) { if (operation->id == 0) {
status = PSA_ERROR_BAD_STATE; status = PSA_ERROR_BAD_STATE;
@ -2704,6 +2714,7 @@ psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
goto exit; goto exit;
} }
LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
status = psa_driver_wrapper_mac_sign_finish(operation, status = psa_driver_wrapper_mac_sign_finish(operation,
mac, operation->mac_size, mac, operation->mac_size,
mac_length); mac_length);
@ -2723,16 +2734,18 @@ exit:
psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
abort_status = psa_mac_abort(operation); abort_status = psa_mac_abort(operation);
LOCAL_OUTPUT_FREE(mac_external, mac);
return status == PSA_SUCCESS ? abort_status : status; return status == PSA_SUCCESS ? abort_status : status;
} }
psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
const uint8_t *mac, const uint8_t *mac_external,
size_t mac_length) size_t mac_length)
{ {
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
LOCAL_INPUT_DECLARE(mac_external, mac);
if (operation->id == 0) { if (operation->id == 0) {
status = PSA_ERROR_BAD_STATE; status = PSA_ERROR_BAD_STATE;
@ -2749,11 +2762,13 @@ psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
goto exit; goto exit;
} }
LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
status = psa_driver_wrapper_mac_verify_finish(operation, status = psa_driver_wrapper_mac_verify_finish(operation,
mac, mac_length); mac, mac_length);
exit: exit:
abort_status = psa_mac_abort(operation); abort_status = psa_mac_abort(operation);
LOCAL_INPUT_FREE(mac_external, mac);
return status == PSA_SUCCESS ? abort_status : status; return status == PSA_SUCCESS ? abort_status : status;
} }
@ -2825,28 +2840,42 @@ exit:
psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key, psa_status_t psa_mac_compute(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 *mac, uint8_t *mac_external,
size_t mac_size, size_t mac_size,
size_t *mac_length) size_t *mac_length)
{ {
return psa_mac_compute_internal(key, alg, psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
LOCAL_INPUT_DECLARE(input_external, input);
LOCAL_OUTPUT_DECLARE(mac_external, mac);
LOCAL_INPUT_ALLOC(input_external, input_length, input);
LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
status = psa_mac_compute_internal(key, alg,
input, input_length, input, input_length,
mac, mac_size, mac_length, 1); mac, mac_size, mac_length, 1);
exit:
LOCAL_INPUT_FREE(input_external, input);
LOCAL_OUTPUT_FREE(mac_external, mac);
return status;
} }
psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, psa_status_t psa_mac_verify(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,
const uint8_t *mac, const uint8_t *mac_external,
size_t mac_length) size_t mac_length)
{ {
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
uint8_t actual_mac[PSA_MAC_MAX_SIZE]; uint8_t actual_mac[PSA_MAC_MAX_SIZE];
size_t actual_mac_length; size_t actual_mac_length;
LOCAL_INPUT_DECLARE(input_external, input);
LOCAL_INPUT_DECLARE(mac_external, mac);
LOCAL_INPUT_ALLOC(input_external, input_length, input);
status = psa_mac_compute_internal(key, alg, status = psa_mac_compute_internal(key, alg,
input, input_length, input, input_length,
actual_mac, sizeof(actual_mac), actual_mac, sizeof(actual_mac),
@ -2859,6 +2888,8 @@ psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
status = PSA_ERROR_INVALID_SIGNATURE; status = PSA_ERROR_INVALID_SIGNATURE;
goto exit; goto exit;
} }
LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) { if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
status = PSA_ERROR_INVALID_SIGNATURE; status = PSA_ERROR_INVALID_SIGNATURE;
goto exit; goto exit;
@ -2866,6 +2897,8 @@ psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
exit: exit:
mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
LOCAL_INPUT_FREE(input_external, input);
LOCAL_INPUT_FREE(mac_external, mac);
return status; return status;
} }