Bladeren bron

feat(esp_hw_support): Added locking mechanism for the ECDSA and ECC peripheral

harshal.patil 2 jaren geleden
bovenliggende
commit
6a7caa7b8e

+ 13 - 1
components/esp_hw_support/include/soc/esp32c6/esp_crypto_lock.h

@@ -1,5 +1,5 @@
 /*
- * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  *
  * SPDX-License-Identifier: Apache-2.0
  */
@@ -63,6 +63,18 @@ void esp_crypto_mpi_lock_acquire(void);
  */
 void esp_crypto_mpi_lock_release(void);
 
+/**
+ * @brief Acquire lock for the ECC cryptography peripheral.
+ *
+ */
+void esp_crypto_ecc_lock_acquire(void);
+
+/**
+ * @brief Release lock for the ECC cryptography peripheral.
+ *
+ */
+void esp_crypto_ecc_lock_release(void);
+
 #ifdef __cplusplus
 }
 #endif

+ 28 - 0
components/esp_hw_support/include/soc/esp32h2/esp_crypto_lock.h

@@ -63,6 +63,34 @@ void esp_crypto_mpi_lock_acquire(void);
  */
 void esp_crypto_mpi_lock_release(void);
 
+
+/**
+ * @brief Acquire lock for the ECC cryptography peripheral.
+ *
+ */
+void esp_crypto_ecc_lock_acquire(void);
+
+/**
+ * @brief Release lock for the ECC cryptography peripheral.
+ *
+ */
+void esp_crypto_ecc_lock_release(void);
+
+
+/**
+ * @brief Acquire lock for ECDSA cryptography peripheral
+ *
+ * Internally also locks the ECC and MPI peripheral, as the ECDSA depends on these peripherals
+ */
+void esp_crypto_ecdsa_lock_acquire(void);
+
+/**
+ * @brief Release lock for ECDSA cryptography peripheral
+ *
+ * Internally also releases the ECC and MPI peripheral, as the ECDSA depends on these peripherals
+ */
+void esp_crypto_ecdsa_lock_release(void);
+
 #ifdef __cplusplus
 }
 #endif

+ 28 - 0
components/esp_hw_support/include/soc/esp32p4/esp_crypto_lock.h

@@ -63,6 +63,34 @@ void esp_crypto_mpi_lock_acquire(void);
  */
 void esp_crypto_mpi_lock_release(void);
 
+
+/**
+ * @brief Acquire lock for the ECC cryptography peripheral.
+ *
+ */
+void esp_crypto_ecc_lock_acquire(void);
+
+/**
+ * @brief Release lock for the ECC cryptography peripheral.
+ *
+ */
+void esp_crypto_ecc_lock_release(void);
+
+
+/**
+ * @brief Acquire lock for ECDSA cryptography peripheral
+ *
+ * Internally also locks the ECC and MPI peripheral, as the ECDSA depends on these peripherals
+ */
+void esp_crypto_ecdsa_lock_acquire(void);
+
+/**
+ * @brief Release lock for ECDSA cryptography peripheral
+ *
+ * Internally also releases the ECC and MPI peripheral, as the ECDSA depends on these peripherals
+ */
+void esp_crypto_ecdsa_lock_release(void);
+
 #ifdef __cplusplus
 }
 #endif

+ 15 - 1
components/esp_hw_support/port/esp32c6/esp_crypto_lock.c

@@ -1,5 +1,5 @@
 /*
- * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  *
  * SPDX-License-Identifier: Apache-2.0
  */
@@ -12,6 +12,7 @@
 SHA: peripheral independent, but DMA is shared with AES
 AES: peripheral independent, but DMA is shared with SHA
 MPI/RSA: independent
+ECC: independent
 HMAC: needs SHA
 DS: needs HMAC (which needs SHA), AES and MPI
 */
@@ -28,6 +29,9 @@ static _lock_t s_crypto_mpi_lock;
 /* Single lock for SHA and AES, sharing a reserved GDMA channel */
 static _lock_t s_crypto_sha_aes_lock;
 
+/* Lock for ECC peripheral */
+static _lock_t s_crypto_ecc_lock;
+
 void esp_crypto_hmac_lock_acquire(void)
 {
     _lock_acquire(&s_crypto_hmac_lock);
@@ -73,3 +77,13 @@ void esp_crypto_mpi_lock_release(void)
 {
     _lock_release(&s_crypto_mpi_lock);
 }
+
+void esp_crypto_ecc_lock_acquire(void)
+{
+    _lock_acquire(&s_crypto_ecc_lock);
+}
+
+void esp_crypto_ecc_lock_release(void)
+{
+    _lock_release(&s_crypto_ecc_lock);
+}

+ 32 - 0
components/esp_hw_support/port/esp32h2/esp_crypto_lock.c

@@ -12,8 +12,10 @@
 SHA: peripheral independent, but DMA is shared with AES
 AES: peripheral independent, but DMA is shared with SHA
 MPI/RSA: independent
+ECC: independent
 HMAC: needs SHA
 DS: needs HMAC (which needs SHA), AES and MPI
+ECDSA: needs ECC and MPI
 */
 
 /* Lock for DS peripheral */
@@ -28,6 +30,12 @@ static _lock_t s_crypto_mpi_lock;
 /* Single lock for SHA and AES, sharing a reserved GDMA channel */
 static _lock_t s_crypto_sha_aes_lock;
 
+/* Lock for ECC peripheral */
+static _lock_t s_crypto_ecc_lock;
+
+/* Lock for ECDSA peripheral */
+static _lock_t s_crypto_ecdsa_lock;
+
 void esp_crypto_hmac_lock_acquire(void)
 {
     _lock_acquire(&s_crypto_hmac_lock);
@@ -73,3 +81,27 @@ void esp_crypto_mpi_lock_release(void)
 {
     _lock_release(&s_crypto_mpi_lock);
 }
+
+void esp_crypto_ecc_lock_acquire(void)
+{
+    _lock_acquire(&s_crypto_ecc_lock);
+}
+
+void esp_crypto_ecc_lock_release(void)
+{
+    _lock_release(&s_crypto_ecc_lock);
+}
+
+void esp_crypto_ecdsa_lock_acquire(void)
+{
+    _lock_acquire(&s_crypto_ecdsa_lock);
+    esp_crypto_ecc_lock_acquire();
+    esp_crypto_mpi_lock_acquire();
+}
+
+void esp_crypto_ecdsa_lock_release(void)
+{
+    esp_crypto_mpi_lock_release();
+    esp_crypto_ecc_lock_release();
+    _lock_release(&s_crypto_ecdsa_lock);
+}

+ 32 - 0
components/esp_hw_support/port/esp32p4/esp_crypto_lock.c

@@ -12,8 +12,10 @@
 SHA: peripheral independent, but DMA is shared with AES
 AES: peripheral independent, but DMA is shared with SHA
 MPI/RSA: independent
+ECC: independent
 HMAC: needs SHA
 DS: needs HMAC (which needs SHA), AES and MPI
+ECDSA: needs ECC and MPI
 */
 
 /* Lock for DS peripheral */
@@ -28,6 +30,12 @@ static _lock_t s_crypto_mpi_lock;
 /* Single lock for SHA and AES, sharing a reserved GDMA channel */
 static _lock_t s_crypto_sha_aes_lock;
 
+/* Lock for ECC peripheral */
+static _lock_t s_crypto_ecc_lock;
+
+/* Lock for ECDSA peripheral */
+static _lock_t s_crypto_ecdsa_lock;
+
 void esp_crypto_hmac_lock_acquire(void)
 {
     _lock_acquire(&s_crypto_hmac_lock);
@@ -73,3 +81,27 @@ void esp_crypto_mpi_lock_release(void)
 {
     _lock_release(&s_crypto_mpi_lock);
 }
+
+void esp_crypto_ecc_lock_acquire(void)
+{
+    _lock_acquire(&s_crypto_ecc_lock);
+}
+
+void esp_crypto_ecc_lock_release(void)
+{
+    _lock_release(&s_crypto_ecc_lock);
+}
+
+void esp_crypto_ecdsa_lock_acquire(void)
+{
+    _lock_acquire(&s_crypto_ecdsa_lock);
+    esp_crypto_ecc_lock_acquire();
+    esp_crypto_mpi_lock_acquire();
+}
+
+void esp_crypto_ecdsa_lock_release(void)
+{
+    esp_crypto_mpi_lock_release();
+    esp_crypto_ecc_lock_release();
+    _lock_release(&s_crypto_ecdsa_lock);
+}

+ 4 - 5
components/mbedtls/port/ecc/esp_ecc.c

@@ -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
  */
@@ -7,15 +7,14 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "esp_crypto_lock.h"
 #include "esp_private/periph_ctrl.h"
 #include "ecc_impl.h"
 #include "hal/ecc_hal.h"
 
-static _lock_t s_crypto_ecc_lock;
-
 static void esp_ecc_acquire_hardware(void)
 {
-    _lock_acquire(&s_crypto_ecc_lock);
+    esp_crypto_ecc_lock_acquire();
 
     periph_module_enable(PERIPH_ECC_MODULE);
 }
@@ -24,7 +23,7 @@ static void esp_ecc_release_hardware(void)
 {
     periph_module_disable(PERIPH_ECC_MODULE);
 
-    _lock_release(&s_crypto_ecc_lock);
+    esp_crypto_ecc_lock_release();
 }
 
 int esp_ecc_point_multiply(const ecc_point_t *point, const uint8_t *scalar, ecc_point_t *result, bool verify_first)

+ 3 - 4
components/mbedtls/port/ecdsa/ecdsa_alt.c

@@ -5,6 +5,7 @@
  */
 #include <string.h>
 #include "hal/ecdsa_hal.h"
+#include "esp_crypto_lock.h"
 #include "esp_efuse.h"
 #include "mbedtls/error.h"
 #include "mbedtls/ecdsa.h"
@@ -19,11 +20,9 @@
 
 __attribute__((unused)) static const char *TAG = "ecdsa_alt";
 
-static _lock_t s_crypto_ecdsa_lock;
-
 static void esp_ecdsa_acquire_hardware(void)
 {
-    _lock_acquire(&s_crypto_ecdsa_lock);
+    esp_crypto_ecdsa_lock_acquire();
 
     periph_module_enable(PERIPH_ECDSA_MODULE);
 }
@@ -32,7 +31,7 @@ static void esp_ecdsa_release_hardware(void)
 {
     periph_module_disable(PERIPH_ECDSA_MODULE);
 
-    _lock_release(&s_crypto_ecdsa_lock);
+    esp_crypto_ecdsa_lock_release();
 }
 
 static void ecdsa_be_to_le(const uint8_t* be_point, uint8_t *le_point, uint8_t len)