From 0760b15d4564a2925f9a88272ec3a75a6792986d Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Fri, 24 Nov 2023 16:21:04 +0000 Subject: [PATCH] Add memory poisoning hooks Signed-off-by: David Horstmann --- library/CMakeLists.txt | 5 ++++- library/psa_crypto.c | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index c7105a1fdf..80a323702c 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -226,7 +226,10 @@ foreach(target IN LISTS target_libraries) PUBLIC ${MBEDTLS_DIR}/include/ PUBLIC ${thirdparty_inc_public} PRIVATE ${MBEDTLS_DIR}/library/ - PRIVATE ${thirdparty_inc}) + PRIVATE ${thirdparty_inc} + # Needed to include psa_crypto_driver_wrappers.h + ${CMAKE_CURRENT_BINARY_DIR} + ${MBEDTLS_DIR}/tests/include/) target_compile_definitions(${target} PRIVATE ${thirdparty_def}) # Pass-through MBEDTLS_CONFIG_FILE and MBEDTLS_USER_CONFIG_FILE diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 094b526311..60c81a572d 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -73,6 +73,10 @@ #include "mbedtls/sha512.h" #include "mbedtls/xtea.h" +#if defined(MBEDTLS_TEST_HOOKS) +#include "test/memory.h" +#endif + #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array))) /****************************************************************/ @@ -5531,10 +5535,18 @@ psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len, return PSA_ERROR_CORRUPTION_DETECTED; } +#if defined(MBEDTLS_TEST_HOOKS) + MBEDTLS_TEST_MEMORY_UNPOISON(input, input_len); +#endif + if (input_len > 0) { memcpy(input_copy, input, input_len); } +#if defined(MBEDTLS_TEST_HOOKS) + MBEDTLS_TEST_MEMORY_POISON(input, input_len); +#endif + return PSA_SUCCESS; } @@ -5558,10 +5570,18 @@ psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_co return PSA_ERROR_BUFFER_TOO_SMALL; } +#if defined(MBEDTLS_TEST_HOOKS) + MBEDTLS_TEST_MEMORY_UNPOISON(output, output_len); +#endif + if (output_copy_len > 0) { memcpy(output, output_copy, output_copy_len); } +#if defined(MBEDTLS_TEST_HOOKS) + MBEDTLS_TEST_MEMORY_POISON(output, output_len); +#endif + return PSA_SUCCESS; }