Merge branch 'fix/aes_mpi_interrupt_allocation_workflow_v5.1' into 'release/v5.1'

fix(mbedtls): move interrupt allocation during initialization phase (v5.1)

See merge request espressif/esp-idf!27204
This commit is contained in:
Mahavir Jain 2023-12-12 11:44:42 +08:00
commit 4aa464a4ea
8 changed files with 90 additions and 48 deletions

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -7,6 +7,7 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "unity.h" #include "unity.h"
#include "mbedtls/aes.h"
#include "memory_checks.h" #include "memory_checks.h"
#include "soc/soc_caps.h" #include "soc/soc_caps.h"
#if SOC_SHA_SUPPORT_PARALLEL_ENG #if SOC_SHA_SUPPORT_PARALLEL_ENG
@ -26,13 +27,21 @@
/* setUp runs before every test */ /* setUp runs before every test */
void setUp(void) void setUp(void)
{ {
// Execute esp_sha operation to allocate internal SHA semaphore memory // Execute esp_sha operation to allocate internal SHA semaphore (in case of ESP32)
// which is considered as leaked otherwise // and initial DMA setup memory which is considered as leaked otherwise
#if SOC_SHA_SUPPORTED #if SOC_SHA_SUPPORTED
const uint8_t input_buffer[64] = {0}; const uint8_t input_buffer[64] = {0};
uint8_t output_buffer[64]; uint8_t output_buffer[64];
esp_sha(SHA_TYPE, input_buffer, sizeof(input_buffer), output_buffer); esp_sha(SHA_TYPE, input_buffer, sizeof(input_buffer), output_buffer);
#endif // SOC_SHA_SUPPORTED #endif // SOC_SHA_SUPPORTED
// Execute mbedtls_aes_init operation to allocate AES interrupt
// allocation memory which is considered as leak otherwise
#if SOC_AES_SUPPORTED
mbedtls_aes_context ctx;
mbedtls_aes_init(&ctx);
#endif // SOC_AES_SUPPORTED
test_utils_record_free_mem(); test_utils_record_free_mem();
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL)); TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_GENERAL)); TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_GENERAL));

View File

@ -177,23 +177,30 @@ static IRAM_ATTR void esp_aes_complete_isr(void *arg)
} }
} }
void esp_aes_intr_alloc(void)
{
if (op_complete_sem == NULL) {
esp_err_t ret = esp_intr_alloc(ETS_AES_INTR_SOURCE, 0, esp_aes_complete_isr, NULL, NULL);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to allocate AES interrupt %d", ret);
// This should be treated as fatal error as this API would mostly
// be invoked within mbedTLS interface. There is no way for the system
// to proceed if the AES interrupt allocation fails here.
abort();
}
static StaticSemaphore_t op_sem_buf;
op_complete_sem = xSemaphoreCreateBinaryStatic(&op_sem_buf);
// Static semaphore creation is unlikley to fail but still basic sanity
assert(op_complete_sem != NULL);
}
}
static esp_err_t esp_aes_isr_initialise( void ) static esp_err_t esp_aes_isr_initialise( void )
{ {
aes_hal_interrupt_clear(); aes_hal_interrupt_clear();
aes_hal_interrupt_enable(true); aes_hal_interrupt_enable(true);
if (op_complete_sem == NULL) {
op_complete_sem = xSemaphoreCreateBinary();
if (op_complete_sem == NULL) {
ESP_LOGE(TAG, "Failed to create intr semaphore");
return ESP_FAIL;
}
esp_err_t ret = esp_intr_alloc(ETS_AES_INTR_SOURCE, 0, esp_aes_complete_isr, NULL, NULL);
if (ret != ESP_OK) {
return ret;
}
}
/* AES is clocked proportionally to CPU clock, take power management lock */ /* AES is clocked proportionally to CPU clock, take power management lock */
#ifdef CONFIG_PM_ENABLE #ifdef CONFIG_PM_ENABLE
@ -432,7 +439,7 @@ static int esp_aes_process_dma(esp_aes_context *ctx, const unsigned char *input,
/* Only use interrupt for long AES operations */ /* Only use interrupt for long AES operations */
if (len > AES_DMA_INTR_TRIG_LEN) { if (len > AES_DMA_INTR_TRIG_LEN) {
use_intr = true; use_intr = true;
if (esp_aes_isr_initialise() == ESP_FAIL) { if (esp_aes_isr_initialise() != ESP_OK) {
ESP_LOGE(TAG, "ESP-AES ISR initialisation failed"); ESP_LOGE(TAG, "ESP-AES ISR initialisation failed");
ret = -1; ret = -1;
goto cleanup; goto cleanup;
@ -575,7 +582,7 @@ int esp_aes_process_dma_gcm(esp_aes_context *ctx, const unsigned char *input, un
/* Only use interrupt for long AES operations */ /* Only use interrupt for long AES operations */
if (len > AES_DMA_INTR_TRIG_LEN) { if (len > AES_DMA_INTR_TRIG_LEN) {
use_intr = true; use_intr = true;
if (esp_aes_isr_initialise() == ESP_FAIL) { if (esp_aes_isr_initialise() != ESP_OK) {
ESP_LOGE(TAG, "ESP-AES ISR initialisation failed"); ESP_LOGE(TAG, "ESP-AES ISR initialisation failed");
ret = -1; ret = -1;
goto cleanup; goto cleanup;

View File

@ -1,16 +1,8 @@
// Copyright 2020 Espressif Systems (Shanghai) PTE LTD /*
// * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
// Licensed under the Apache License, Version 2.0 (the "License"); *
// you may not use this file except in compliance with the License. * SPDX-License-Identifier: Apache-2.0
// You may obtain a copy of the License at */
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once #pragma once
@ -43,6 +35,11 @@ esp_err_t esp_aes_dma_start(const lldesc_t *input, const lldesc_t *output);
*/ */
bool esp_aes_dma_done(const lldesc_t *output); bool esp_aes_dma_done(const lldesc_t *output);
/**
* @brief Allocate AES peripheral interrupt handler
*/
void esp_aes_intr_alloc(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -6,7 +6,7 @@
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
*/ */
/* /*
* The AES block cipher was designed by Vincent Rijmen and Joan Daemen. * The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
@ -14,6 +14,7 @@
* http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf * http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf
* http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
*/ */
#include "sdkconfig.h"
#include "aes/esp_aes_internal.h" #include "aes/esp_aes_internal.h"
#include "mbedtls/aes.h" #include "mbedtls/aes.h"
#include "hal/aes_hal.h" #include "hal/aes_hal.h"
@ -24,7 +25,7 @@
#include <string.h> #include <string.h>
#include "mbedtls/platform.h" #include "mbedtls/platform.h"
#if SOC_AES_GDMA #if SOC_AES_SUPPORT_DMA
#include "esp_aes_dma_priv.h" #include "esp_aes_dma_priv.h"
#endif #endif
@ -39,10 +40,12 @@ bool valid_key_length(const esp_aes_context *ctx)
return valid_len; return valid_len;
} }
void esp_aes_init(esp_aes_context *ctx) void esp_aes_init(esp_aes_context *ctx)
{ {
bzero(ctx, sizeof(esp_aes_context)); bzero(ctx, sizeof(esp_aes_context));
#if SOC_AES_SUPPORT_DMA && CONFIG_MBEDTLS_AES_USE_INTERRUPT
esp_aes_intr_alloc();
#endif
} }
void esp_aes_free( esp_aes_context *ctx ) void esp_aes_free( esp_aes_context *ctx )

View File

@ -14,22 +14,27 @@
* http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf * http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf
* http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
*/ */
#include "soc/soc_caps.h" #include <string.h>
#include "aes/esp_aes.h" #include "aes/esp_aes.h"
#include "aes/esp_aes_gcm.h" #include "aes/esp_aes_gcm.h"
#include "aes/esp_aes_internal.h" #include "aes/esp_aes_internal.h"
#include "hal/aes_hal.h" #include "hal/aes_hal.h"
#include "esp_log.h"
#include "mbedtls/aes.h" #include "mbedtls/aes.h"
#include "mbedtls/error.h"
#include "mbedtls/gcm.h" #include "mbedtls/gcm.h"
#include "esp_heap_caps.h" #include "esp_heap_caps.h"
#include "esp_log.h"
#include "soc/soc_caps.h"
#include "soc/soc_memory_layout.h" #include "soc/soc_memory_layout.h"
#include "mbedtls/error.h" #include "sdkconfig.h"
#include <string.h>
#if SOC_AES_SUPPORT_DMA
#include "esp_aes_dma_priv.h"
#endif
#define ESP_PUT_BE64(a, val) \ #define ESP_PUT_BE64(a, val) \
do { \ do { \
@ -314,6 +319,10 @@ void esp_aes_gcm_init( esp_gcm_context *ctx)
bzero(ctx, sizeof(esp_gcm_context)); bzero(ctx, sizeof(esp_gcm_context));
#if SOC_AES_SUPPORT_DMA && CONFIG_MBEDTLS_AES_USE_INTERRUPT
esp_aes_intr_alloc();
#endif
ctx->gcm_state = ESP_AES_GCM_STATE_INIT; ctx->gcm_state = ESP_AES_GCM_STATE_INIT;
} }

View File

@ -6,7 +6,7 @@
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
*/ */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -79,14 +79,23 @@ static esp_err_t esp_mpi_isr_initialise(void)
esp_mpi_interrupt_clear(); esp_mpi_interrupt_clear();
esp_mpi_interrupt_enable(true); esp_mpi_interrupt_enable(true);
if (op_complete_sem == NULL) { if (op_complete_sem == NULL) {
op_complete_sem = xSemaphoreCreateBinary(); static StaticSemaphore_t op_sem_buf;
op_complete_sem = xSemaphoreCreateBinaryStatic(&op_sem_buf);
if (op_complete_sem == NULL) { if (op_complete_sem == NULL) {
ESP_LOGE(TAG, "Failed to create intr semaphore"); ESP_LOGE(TAG, "Failed to create intr semaphore");
return ESP_FAIL; return ESP_FAIL;
} }
esp_intr_alloc(ETS_RSA_INTR_SOURCE, 0, esp_mpi_complete_isr, NULL, NULL); esp_err_t ret;
ret = esp_intr_alloc(ETS_RSA_INTR_SOURCE, 0, esp_mpi_complete_isr, NULL, NULL);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to allocate RSA interrupt %d", ret);
// This should be treated as fatal error as this API would mostly
// be invoked within mbedTLS interface. There is no way for the system
// to proceed if the MPI interrupt allocation fails here.
abort();
}
} }
/* MPI is clocked proportionally to CPU clock, take power management lock */ /* MPI is clocked proportionally to CPU clock, take power management lock */
@ -398,7 +407,7 @@ static int esp_mpi_exp_mod( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_
esp_mpi_enable_hardware_hw_op(); esp_mpi_enable_hardware_hw_op();
#if defined (CONFIG_MBEDTLS_MPI_USE_INTERRUPT) #if defined (CONFIG_MBEDTLS_MPI_USE_INTERRUPT)
if (esp_mpi_isr_initialise() == ESP_FAIL) { if (esp_mpi_isr_initialise() != ESP_OK) {
ret = -1; ret = -1;
esp_mpi_disable_hardware_hw_op(); esp_mpi_disable_hardware_hw_op();
goto cleanup; goto cleanup;

View File

@ -1,16 +1,25 @@
/* /*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "unity.h" #include "unity.h"
#include "mbedtls/aes.h"
#include "memory_checks.h" #include "memory_checks.h"
#include "soc/soc_caps.h"
/* setUp runs before every test */ /* setUp runs before every test */
void setUp(void) void setUp(void)
{ {
// Execute mbedtls_aes_init operation to allocate AES interrupt
// allocation memory which is considered as leak otherwise
#if SOC_AES_SUPPORTED
mbedtls_aes_context ctx;
mbedtls_aes_init(&ctx);
#endif // SOC_AES_SUPPORTED
test_utils_record_free_mem(); test_utils_record_free_mem();
test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL); test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL);
test_utils_set_leak_level(CONFIG_UNITY_WARN_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_GENERAL); test_utils_set_leak_level(CONFIG_UNITY_WARN_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_GENERAL);

View File

@ -681,7 +681,6 @@ components/mbedtls/port/aes/block/esp_aes.c
components/mbedtls/port/aes/dma/esp_aes.c components/mbedtls/port/aes/dma/esp_aes.c
components/mbedtls/port/aes/dma/esp_aes_crypto_dma_impl.c components/mbedtls/port/aes/dma/esp_aes_crypto_dma_impl.c
components/mbedtls/port/aes/dma/esp_aes_gdma_impl.c components/mbedtls/port/aes/dma/esp_aes_gdma_impl.c
components/mbedtls/port/aes/dma/include/esp_aes_dma_priv.h
components/mbedtls/port/aes/esp_aes_xts.c components/mbedtls/port/aes/esp_aes_xts.c
components/mbedtls/port/include/aes/esp_aes.h components/mbedtls/port/include/aes/esp_aes.h
components/mbedtls/port/include/aes/esp_aes_internal.h components/mbedtls/port/include/aes/esp_aes_internal.h