mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-10 05:03:02 +03:00
Merge pull request #1148 from tom-daubney-arm/backport_hash_buffer_protection
[Backport] - Hash buffer protection
This commit is contained in:
@@ -2320,10 +2320,11 @@ exit:
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_update(psa_hash_operation_t *operation,
|
||||
const uint8_t *input,
|
||||
const uint8_t *input_external,
|
||||
size_t input_length)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
LOCAL_INPUT_DECLARE(input_external, input);
|
||||
|
||||
if (operation->id == 0) {
|
||||
status = PSA_ERROR_BAD_STATE;
|
||||
@@ -2336,6 +2337,7 @@ psa_status_t psa_hash_update(psa_hash_operation_t *operation,
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
|
||||
LOCAL_INPUT_ALLOC(input_external, input_length, input);
|
||||
status = psa_driver_wrapper_hash_update(operation, input, input_length);
|
||||
|
||||
exit:
|
||||
@@ -2343,32 +2345,57 @@ exit:
|
||||
psa_hash_abort(operation);
|
||||
}
|
||||
|
||||
LOCAL_INPUT_FREE(input_external, input);
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
|
||||
uint8_t *hash,
|
||||
size_t hash_size,
|
||||
size_t *hash_length)
|
||||
static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
|
||||
uint8_t *hash,
|
||||
size_t hash_size,
|
||||
size_t *hash_length)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
*hash_length = 0;
|
||||
if (operation->id == 0) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
psa_status_t status = psa_driver_wrapper_hash_finish(
|
||||
status = psa_driver_wrapper_hash_finish(
|
||||
operation, hash, hash_size, hash_length);
|
||||
psa_hash_abort(operation);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
|
||||
uint8_t *hash_external,
|
||||
size_t hash_size,
|
||||
size_t *hash_length)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
LOCAL_OUTPUT_DECLARE(hash_external, hash);
|
||||
|
||||
LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
|
||||
status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
|
||||
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
exit:
|
||||
#endif
|
||||
LOCAL_OUTPUT_FREE(hash_external, hash);
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
|
||||
const uint8_t *hash,
|
||||
const uint8_t *hash_external,
|
||||
size_t hash_length)
|
||||
{
|
||||
uint8_t actual_hash[PSA_HASH_MAX_SIZE];
|
||||
size_t actual_hash_length;
|
||||
psa_status_t status = psa_hash_finish(
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
LOCAL_INPUT_DECLARE(hash_external, hash);
|
||||
|
||||
status = psa_hash_finish_internal(
|
||||
operation,
|
||||
actual_hash, sizeof(actual_hash),
|
||||
&actual_hash_length);
|
||||
@@ -2382,6 +2409,7 @@ psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
|
||||
goto exit;
|
||||
}
|
||||
|
||||
LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
|
||||
if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
|
||||
status = PSA_ERROR_INVALID_SIGNATURE;
|
||||
}
|
||||
@@ -2391,36 +2419,55 @@ exit:
|
||||
if (status != PSA_SUCCESS) {
|
||||
psa_hash_abort(operation);
|
||||
}
|
||||
|
||||
LOCAL_INPUT_FREE(hash_external, hash);
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_compute(psa_algorithm_t alg,
|
||||
const uint8_t *input, size_t input_length,
|
||||
uint8_t *hash, size_t hash_size,
|
||||
const uint8_t *input_external, size_t input_length,
|
||||
uint8_t *hash_external, size_t hash_size,
|
||||
size_t *hash_length)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
LOCAL_INPUT_DECLARE(input_external, input);
|
||||
LOCAL_OUTPUT_DECLARE(hash_external, hash);
|
||||
|
||||
*hash_length = 0;
|
||||
if (!PSA_ALG_IS_HASH(alg)) {
|
||||
return PSA_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
return psa_driver_wrapper_hash_compute(alg, input, input_length,
|
||||
hash, hash_size, hash_length);
|
||||
LOCAL_INPUT_ALLOC(input_external, input_length, input);
|
||||
LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
|
||||
status = psa_driver_wrapper_hash_compute(alg, input, input_length,
|
||||
hash, hash_size, hash_length);
|
||||
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
exit:
|
||||
#endif
|
||||
LOCAL_INPUT_FREE(input_external, input);
|
||||
LOCAL_OUTPUT_FREE(hash_external, hash);
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_compare(psa_algorithm_t alg,
|
||||
const uint8_t *input, size_t input_length,
|
||||
const uint8_t *hash, size_t hash_length)
|
||||
const uint8_t *input_external, size_t input_length,
|
||||
const uint8_t *hash_external, size_t hash_length)
|
||||
{
|
||||
uint8_t actual_hash[PSA_HASH_MAX_SIZE];
|
||||
size_t actual_hash_length;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
LOCAL_INPUT_DECLARE(input_external, input);
|
||||
LOCAL_INPUT_DECLARE(hash_external, hash);
|
||||
|
||||
if (!PSA_ALG_IS_HASH(alg)) {
|
||||
return PSA_ERROR_INVALID_ARGUMENT;
|
||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t status = psa_driver_wrapper_hash_compute(
|
||||
LOCAL_INPUT_ALLOC(input_external, input_length, input);
|
||||
status = psa_driver_wrapper_hash_compute(
|
||||
alg, input, input_length,
|
||||
actual_hash, sizeof(actual_hash),
|
||||
&actual_hash_length);
|
||||
@@ -2431,12 +2478,18 @@ psa_status_t psa_hash_compare(psa_algorithm_t alg,
|
||||
status = PSA_ERROR_INVALID_SIGNATURE;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
|
||||
if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
|
||||
status = PSA_ERROR_INVALID_SIGNATURE;
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
|
||||
|
||||
LOCAL_INPUT_FREE(input_external, input);
|
||||
LOCAL_INPUT_FREE(hash_external, hash);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
@@ -162,6 +162,12 @@ class PSAWrapperGenerator(c_wrapper_generator.Base):
|
||||
'psa_sign_hash',
|
||||
'psa_verify_hash'):
|
||||
return True
|
||||
if function_name in ('psa_hash_update',
|
||||
'psa_hash_finish',
|
||||
'psa_hash_verify',
|
||||
'psa_hash_compute',
|
||||
'psa_hash_compare'):
|
||||
return True
|
||||
return False
|
||||
|
||||
def _write_function_call(self, out: typing_util.Writable,
|
||||
|
@@ -340,7 +340,15 @@ psa_status_t mbedtls_test_wrap_psa_hash_compare(
|
||||
const uint8_t *arg3_hash,
|
||||
size_t arg4_hash_length)
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_POISON(arg1_input, arg2_input_length);
|
||||
MBEDTLS_TEST_MEMORY_POISON(arg3_hash, arg4_hash_length);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
psa_status_t status = (psa_hash_compare)(arg0_alg, arg1_input, arg2_input_length, arg3_hash, arg4_hash_length);
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(arg1_input, arg2_input_length);
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(arg3_hash, arg4_hash_length);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -353,7 +361,15 @@ psa_status_t mbedtls_test_wrap_psa_hash_compute(
|
||||
size_t arg4_hash_size,
|
||||
size_t *arg5_hash_length)
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_POISON(arg1_input, arg2_input_length);
|
||||
MBEDTLS_TEST_MEMORY_POISON(arg3_hash, arg4_hash_size);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
psa_status_t status = (psa_hash_compute)(arg0_alg, arg1_input, arg2_input_length, arg3_hash, arg4_hash_size, arg5_hash_length);
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(arg1_input, arg2_input_length);
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(arg3_hash, arg4_hash_size);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -364,7 +380,13 @@ psa_status_t mbedtls_test_wrap_psa_hash_finish(
|
||||
size_t arg2_hash_size,
|
||||
size_t *arg3_hash_length)
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_POISON(arg1_hash, arg2_hash_size);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
psa_status_t status = (psa_hash_finish)(arg0_operation, arg1_hash, arg2_hash_size, arg3_hash_length);
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(arg1_hash, arg2_hash_size);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -383,7 +405,13 @@ psa_status_t mbedtls_test_wrap_psa_hash_update(
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length)
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_POISON(arg1_input, arg2_input_length);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
psa_status_t status = (psa_hash_update)(arg0_operation, arg1_input, arg2_input_length);
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(arg1_input, arg2_input_length);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -393,7 +421,13 @@ psa_status_t mbedtls_test_wrap_psa_hash_verify(
|
||||
const uint8_t *arg1_hash,
|
||||
size_t arg2_hash_length)
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_POISON(arg1_hash, arg2_hash_length);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
psa_status_t status = (psa_hash_verify)(arg0_operation, arg1_hash, arg2_hash_length);
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(arg1_hash, arg2_hash_length);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user