11/*
2- * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+ * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 */
66
77#include "unity.h"
88#include "unity_test_runner.h"
99#include "esp_heap_caps.h"
10+ #include "mbedtls/aes.h"
11+ #include "sdkconfig.h"
12+ #include "soc/soc_caps.h"
13+ #if SOC_SHA_SUPPORT_PARALLEL_ENG
14+ #include "sha/sha_parallel_engine.h"
15+ #elif SOC_SHA_SUPPORT_DMA
16+ #include "sha/sha_dma.h"
17+ #else
18+ #include "sha/sha_block.h"
19+ #endif
1020
1121#define TEST_MEMORY_LEAK_THRESHOLD_DEFAULT 0
1222static int leak_threshold = TEST_MEMORY_LEAK_THRESHOLD_DEFAULT ;
@@ -25,8 +35,35 @@ static void check_leak(size_t before_free, size_t after_free, const char *type)
2535 TEST_ASSERT_MESSAGE (delta > leak_threshold , "memory leak" );
2636}
2737
38+ #if SOC_SHA_SUPPORT_SHA512
39+ #define SHA_TYPE SHA2_512
40+ #else
41+ #define SHA_TYPE SHA2_256
42+ #endif //SOC_SHA_SUPPORT_SHA512
43+
2844void setUp (void )
2945{
46+ #if CONFIG_MBEDTLS_HARDWARE_SHA
47+ // Execute esp_sha operation to allocate internal SHA semaphore (in case of ESP32)
48+ // and initial DMA setup memory which is considered as leaked otherwise
49+ const uint8_t input_buffer [64 ] = {0 };
50+ uint8_t output_buffer [64 ];
51+ esp_sha (SHA_TYPE , input_buffer , sizeof (input_buffer ), output_buffer );
52+ #endif // SOC_SHA_SUPPORTED
53+
54+ #if CONFIG_MBEDTLS_HARDWARE_AES
55+ // Execute mbedtls_aes_init operation to allocate AES interrupt
56+ // allocation memory which is considered as leak otherwise
57+ const uint8_t plaintext [16 ] = {0 };
58+ uint8_t ciphertext [16 ];
59+ const uint8_t key [16 ] = { 0 };
60+ mbedtls_aes_context ctx ;
61+ mbedtls_aes_init (& ctx );
62+ mbedtls_aes_setkey_enc (& ctx , key , 128 );
63+ mbedtls_aes_crypt_ecb (& ctx , MBEDTLS_AES_ENCRYPT , plaintext , ciphertext );
64+ mbedtls_aes_free (& ctx );
65+ #endif // SOC_AES_SUPPORTED
66+
3067 before_free_8bit = heap_caps_get_free_size (MALLOC_CAP_8BIT );
3168 before_free_32bit = heap_caps_get_free_size (MALLOC_CAP_32BIT );
3269}
0 commit comments