summaryrefslogtreecommitdiff
path: root/lib/bt/common/tinycrypt/port
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2025-07-25 13:33:07 +1000
committerjacqueline <me@jacqueline.id.au>2025-07-25 13:33:07 +1000
commitc8e79a926620e48830778714cfe4b2ea2453fcaf (patch)
tree8c756e08e01b8e147cf72bec128026f46bd854c5 /lib/bt/common/tinycrypt/port
parent237136f3e93cb6b5be24670d7520adb17cc0fa36 (diff)
downloadtangara-fw-c8e79a926620e48830778714cfe4b2ea2453fcaf.tar.gz
Update forked idf components
Diffstat (limited to 'lib/bt/common/tinycrypt/port')
-rw-r--r--lib/bt/common/tinycrypt/port/esp_tinycrypt_port.c87
-rw-r--r--lib/bt/common/tinycrypt/port/esp_tinycrypt_port.h15
2 files changed, 102 insertions, 0 deletions
diff --git a/lib/bt/common/tinycrypt/port/esp_tinycrypt_port.c b/lib/bt/common/tinycrypt/port/esp_tinycrypt_port.c
new file mode 100644
index 00000000..c4424c56
--- /dev/null
+++ b/lib/bt/common/tinycrypt/port/esp_tinycrypt_port.c
@@ -0,0 +1,87 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#include "esp_tinycrypt_port.h"
+
+#include "esp_crypto_lock.h"
+#include "esp_private/esp_crypto_lock_internal.h"
+
+#if SOC_ECC_SUPPORTED
+#include "hal/ecc_hal.h"
+#include "hal/ecc_ll.h"
+#endif /* SOC_ECC_SUPPORTED */
+
+
+#if SOC_ECC_SUPPORTED
+static void esp_tinycrypt_acquire_ecc_hardware(void)
+{
+ esp_crypto_ecc_lock_acquire();
+
+ ECC_RCC_ATOMIC() {
+ ecc_ll_enable_bus_clock(true);
+ ecc_ll_power_up();
+ ecc_ll_reset_register();
+ }
+}
+
+static void esp_tinycrypt_release_ecc_hardware(void)
+{
+ ECC_RCC_ATOMIC() {
+ ecc_ll_enable_bus_clock(false);
+ ecc_ll_power_down();
+ }
+
+ esp_crypto_ecc_lock_release();
+}
+
+int esp_tinycrypt_verify_ecc_point(const uint8_t *pk_x, const uint8_t *pk_y, uint8_t length)
+{
+ int result;
+
+ esp_tinycrypt_acquire_ecc_hardware();
+
+ ecc_hal_write_verify_param(pk_x, pk_y, length);
+ ecc_hal_set_mode(ECC_MODE_VERIFY);
+ ecc_hal_start_calc();
+ while (!ecc_hal_is_calc_finished());
+ result = ecc_hal_read_verify_result();
+
+ esp_tinycrypt_release_ecc_hardware();
+
+ if (result == 1) {
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+int esp_tinycrypt_calc_ecc_mult(const uint8_t *p_x, const uint8_t *p_y, const uint8_t *scalar,
+ uint8_t *r_x, uint8_t *r_y, uint8_t num_bytes, bool verify_first)
+{
+ int ret = -1;
+ ecc_mode_t work_mode = verify_first ? ECC_MODE_VERIFY_THEN_POINT_MUL : ECC_MODE_POINT_MUL;
+
+ esp_tinycrypt_acquire_ecc_hardware();
+
+ ecc_hal_write_mul_param(scalar, p_x, p_y, num_bytes);
+ ecc_hal_set_mode(work_mode);
+ /*
+ * Enable constant-time point multiplication operations for the ECC hardware accelerator,
+ * if supported for the given target. This protects the ECC multiplication operation from
+ * timing attacks. This increases the time taken (by almost 50%) for some point
+ * multiplication operations performed by the ECC hardware accelerator.
+ */
+ ecc_hal_enable_constant_time_point_mul(true);
+ ecc_hal_start_calc();
+
+ while (!ecc_hal_is_calc_finished());
+
+ ret = ecc_hal_read_mul_result(r_x, r_y, num_bytes);
+
+ esp_tinycrypt_release_ecc_hardware();
+
+ return ret;
+}
+#endif /* SOC_ECC_SUPPORTED */
diff --git a/lib/bt/common/tinycrypt/port/esp_tinycrypt_port.h b/lib/bt/common/tinycrypt/port/esp_tinycrypt_port.h
new file mode 100644
index 00000000..4fdf82c2
--- /dev/null
+++ b/lib/bt/common/tinycrypt/port/esp_tinycrypt_port.h
@@ -0,0 +1,15 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#include <stdint.h>
+#include <stdbool.h>
+#include "soc/soc_caps.h"
+
+#if SOC_ECC_SUPPORTED
+int esp_tinycrypt_verify_ecc_point(const uint8_t *pk_x, const uint8_t *pk_y, uint8_t length);
+
+int esp_tinycrypt_calc_ecc_mult(const uint8_t *p_x, const uint8_t *p_y, const uint8_t *scalar,
+ uint8_t *r_x, uint8_t *r_y, uint8_t num_bytes, bool verify_first);
+#endif /* SOC_ECC_SUPPORTED */