From 92849e660e6ef1ef9d35d58f08dcdc0acc5c2ca2 Mon Sep 17 00:00:00 2001 From: wuzhenghui Date: Wed, 28 Feb 2024 14:29:22 +0800 Subject: [PATCH] fix(mbedtls): fixing ecdsa's dependence on ecc_mult clock --- .../test_apps/crypto/main/ecdsa/test_ecdsa.c | 17 ++++++++++++++++- components/mbedtls/port/ecdsa/ecdsa_alt.c | 12 +++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/components/hal/test_apps/crypto/main/ecdsa/test_ecdsa.c b/components/hal/test_apps/crypto/main/ecdsa/test_ecdsa.c index 64c4c2aa0d..4564581967 100644 --- a/components/hal/test_apps/crypto/main/ecdsa/test_ecdsa.c +++ b/components/hal/test_apps/crypto/main/ecdsa/test_ecdsa.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: CC0-1.0 */ @@ -8,10 +8,12 @@ #include #include +#include "esp_crypto_lock.h" #include "esp_efuse_chip.h" #include "esp_private/esp_crypto_lock_internal.h" #include "esp_random.h" #include "hal/clk_gate_ll.h" +#include "hal/ecc_ll.h" #include "hal/ecdsa_hal.h" #include "hal/ecdsa_ll.h" #include "hal/ecdsa_types.h" @@ -24,6 +26,13 @@ static void ecdsa_enable_and_reset(void) { + esp_crypto_ecdsa_lock_acquire(); + + ECC_RCC_ATOMIC() { + ecc_ll_enable_bus_clock(true); + ecc_ll_reset_register(); + } + ECDSA_RCC_ATOMIC() { ecdsa_ll_enable_bus_clock(true); ecdsa_ll_reset_register(); @@ -32,9 +41,15 @@ static void ecdsa_enable_and_reset(void) static void ecdsa_disable(void) { + ECC_RCC_ATOMIC() { + ecc_ll_enable_bus_clock(false); + } + ECDSA_RCC_ATOMIC() { ecdsa_ll_enable_bus_clock(false); } + + esp_crypto_ecdsa_lock_release(); } static void ecc_be_to_le(const uint8_t* be_point, uint8_t *le_point, uint8_t len) diff --git a/components/mbedtls/port/ecdsa/ecdsa_alt.c b/components/mbedtls/port/ecdsa/ecdsa_alt.c index c0fc7c793b..8cce2663f7 100644 --- a/components/mbedtls/port/ecdsa/ecdsa_alt.c +++ b/components/mbedtls/port/ecdsa/ecdsa_alt.c @@ -1,9 +1,10 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include +#include "hal/ecc_ll.h" #include "hal/ecdsa_ll.h" #include "hal/ecdsa_hal.h" #include "esp_crypto_lock.h" @@ -25,6 +26,11 @@ static void esp_ecdsa_acquire_hardware(void) { esp_crypto_ecdsa_lock_acquire(); + ECC_RCC_ATOMIC() { + ecc_ll_enable_bus_clock(true); + ecc_ll_reset_register(); + } + ECDSA_RCC_ATOMIC() { ecdsa_ll_enable_bus_clock(true); ecdsa_ll_reset_register(); @@ -33,6 +39,10 @@ static void esp_ecdsa_acquire_hardware(void) static void esp_ecdsa_release_hardware(void) { + ECC_RCC_ATOMIC() { + ecc_ll_enable_bus_clock(false); + } + ECDSA_RCC_ATOMIC() { ecdsa_ll_enable_bus_clock(false); }