Skip to content

Commit

Permalink
Merge branch 'feature/locking_layer_for_ecdsa' into 'master'
Browse files Browse the repository at this point in the history
feat(esp_hw_support): Added locking mechanism for the ECDSA and ECC peripherals

Closes IDF-7990

See merge request espressif/esp-idf!26029
  • Loading branch information
mahavirj committed Sep 25, 2023
2 parents f0b4a69 + 6a7caa7 commit ab74fb4
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 12 deletions.
27 changes: 27 additions & 0 deletions components/esp_hw_support/include/soc/esp32c2/esp_crypto_lock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

/**
* @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
14 changes: 13 additions & 1 deletion components/esp_hw_support/include/soc/esp32c6/esp_crypto_lock.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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 changes: 28 additions & 0 deletions components/esp_hw_support/include/soc/esp32h2/esp_crypto_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 28 additions & 0 deletions components/esp_hw_support/include/soc/esp32p4/esp_crypto_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion components/esp_hw_support/port/esp32c2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ set(srcs "rtc_clk_init.c"

if(NOT BOOTLOADER_BUILD)

list(APPEND srcs "sar_periph_ctrl.c")
list(APPEND srcs "esp_crypto_lock.c"
"sar_periph_ctrl.c")

endif()

Expand Down
26 changes: 26 additions & 0 deletions components/esp_hw_support/port/esp32c2/esp_crypto_lock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <sys/lock.h>

#include "esp_crypto_lock.h"

/* Lock overview:
ECC: independent
*/

/* Lock for ECC peripheral */
static _lock_t s_crypto_ecc_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);
}
16 changes: 15 additions & 1 deletion components/esp_hw_support/port/esp32c6/esp_crypto_lock.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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);
Expand Down Expand Up @@ -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 changes: 32 additions & 0 deletions components/esp_hw_support/port/esp32h2/esp_crypto_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down Expand Up @@ -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 changes: 32 additions & 0 deletions components/esp_hw_support/port/esp32p4/esp_crypto_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
9 changes: 4 additions & 5 deletions components/mbedtls/port/ecc/esp_ecc.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#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);
}
Expand All @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions components/mbedtls/port/ecdsa/ecdsa_alt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
}
Expand All @@ -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)
Expand Down

0 comments on commit ab74fb4

Please sign in to comment.