2022-01-05 12:25:28 -05:00
|
|
|
/*
|
2023-09-18 07:00:42 -04:00
|
|
|
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
|
2022-01-05 12:25:28 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2023-09-18 07:00:42 -04:00
|
|
|
#include "esp_crypto_lock.h"
|
2023-09-06 07:33:55 -04:00
|
|
|
#include "esp_private/esp_crypto_lock_internal.h"
|
2022-01-05 12:25:28 -05:00
|
|
|
#include "ecc_impl.h"
|
|
|
|
#include "hal/ecc_hal.h"
|
2023-09-06 07:33:55 -04:00
|
|
|
#include "hal/ecc_ll.h"
|
2022-01-05 12:25:28 -05:00
|
|
|
|
|
|
|
static void esp_ecc_acquire_hardware(void)
|
|
|
|
{
|
2023-09-18 07:00:42 -04:00
|
|
|
esp_crypto_ecc_lock_acquire();
|
2022-01-05 12:25:28 -05:00
|
|
|
|
2023-09-06 07:33:55 -04:00
|
|
|
ECC_RCC_ATOMIC() {
|
|
|
|
ecc_ll_enable_bus_clock(true);
|
2024-06-19 22:41:20 -04:00
|
|
|
ecc_ll_power_up();
|
2023-09-06 07:33:55 -04:00
|
|
|
ecc_ll_reset_register();
|
|
|
|
}
|
2022-01-05 12:25:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void esp_ecc_release_hardware(void)
|
|
|
|
{
|
2023-09-06 07:33:55 -04:00
|
|
|
ECC_RCC_ATOMIC() {
|
|
|
|
ecc_ll_enable_bus_clock(false);
|
2024-06-19 22:41:20 -04:00
|
|
|
ecc_ll_power_down();
|
2023-09-06 07:33:55 -04:00
|
|
|
}
|
2022-01-05 12:25:28 -05:00
|
|
|
|
2023-09-18 07:00:42 -04:00
|
|
|
esp_crypto_ecc_lock_release();
|
2022-01-05 12:25:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int esp_ecc_point_multiply(const ecc_point_t *point, const uint8_t *scalar, ecc_point_t *result, bool verify_first)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
uint16_t len = point->len;
|
|
|
|
ecc_mode_t work_mode = verify_first ? ECC_MODE_VERIFY_THEN_POINT_MUL : ECC_MODE_POINT_MUL;
|
|
|
|
|
|
|
|
esp_ecc_acquire_hardware();
|
|
|
|
|
|
|
|
ecc_hal_write_mul_param(scalar, point->x, point->y, len);
|
|
|
|
ecc_hal_set_mode(work_mode);
|
|
|
|
ecc_hal_start_calc();
|
|
|
|
|
|
|
|
memset(result, 0, sizeof(ecc_point_t));
|
|
|
|
|
|
|
|
result->len = len;
|
|
|
|
|
|
|
|
while (!ecc_hal_is_calc_finished()) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ecc_hal_read_mul_result(result->x, result->y, len);
|
|
|
|
|
|
|
|
esp_ecc_release_hardware();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int esp_ecc_point_verify(const ecc_point_t *point)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
esp_ecc_acquire_hardware();
|
|
|
|
ecc_hal_write_verify_param(point->x, point->y, point->len);
|
|
|
|
ecc_hal_set_mode(ECC_MODE_VERIFY);
|
|
|
|
ecc_hal_start_calc();
|
|
|
|
|
|
|
|
while (!ecc_hal_is_calc_finished()) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = ecc_hal_read_verify_result();
|
|
|
|
|
|
|
|
esp_ecc_release_hardware();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|