1 /*
2  * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "hal/ecc_hal.h"
7 #include "hal/ecc_ll.h"
8 #include "soc/soc_caps.h"
9 
ecc_hal_set_mode(ecc_mode_t mode)10 void ecc_hal_set_mode(ecc_mode_t mode)
11 {
12     ecc_ll_set_mode(mode);
13 }
14 
ecc_hal_set_curve(ecc_curve_t curve)15 void ecc_hal_set_curve(ecc_curve_t curve)
16 {
17     ecc_ll_set_curve(curve);
18 }
19 
ecc_hal_start_calc(void)20 void ecc_hal_start_calc(void)
21 {
22     ecc_ll_clear_interrupt();
23     ecc_ll_start_calc();
24 }
25 
ecc_hal_is_calc_finished(void)26 int ecc_hal_is_calc_finished(void)
27 {
28     return ecc_ll_is_calc_finished();
29 }
30 
clear_param_registers(void)31 static void clear_param_registers(void)
32 {
33     uint8_t buf[32] = {0};
34 
35     ecc_ll_write_param(ECC_PARAM_PX, buf, sizeof(buf));
36     ecc_ll_write_param(ECC_PARAM_PY, buf, sizeof(buf));
37     ecc_ll_write_param(ECC_PARAM_K, buf, sizeof(buf));
38 #if SOC_ECC_EXTENDED_MODES_SUPPORTED
39     ecc_ll_write_param(ECC_PARAM_QX, buf, sizeof(buf));
40     ecc_ll_write_param(ECC_PARAM_QY, buf, sizeof(buf));
41     ecc_ll_write_param(ECC_PARAM_QZ, buf, sizeof(buf));
42 #endif
43 }
44 
ecc_hal_write_mul_param(const uint8_t * k,const uint8_t * px,const uint8_t * py,uint16_t len)45 void ecc_hal_write_mul_param(const uint8_t *k, const uint8_t *px, const uint8_t *py, uint16_t len)
46 {
47     ecc_curve_t curve = len == 32 ? ECC_CURVE_SECP256R1 : ECC_CURVE_SECP192R1;
48     ecc_ll_set_curve(curve);
49 
50     clear_param_registers();
51 
52     ecc_ll_write_param(ECC_PARAM_K, k, len);
53     ecc_ll_write_param(ECC_PARAM_PX, px, len);
54     ecc_ll_write_param(ECC_PARAM_PY, py, len);
55 }
56 
ecc_hal_write_verify_param(const uint8_t * px,const uint8_t * py,uint16_t len)57 void ecc_hal_write_verify_param(const uint8_t *px, const uint8_t *py, uint16_t len)
58 {
59     ecc_curve_t curve = len == 32 ? ECC_CURVE_SECP256R1 : ECC_CURVE_SECP192R1;
60     ecc_ll_set_curve(curve);
61 
62     clear_param_registers();
63 
64     ecc_ll_write_param(ECC_PARAM_PX, px, len);
65     ecc_ll_write_param(ECC_PARAM_PY, py, len);
66 }
67 
ecc_hal_read_mul_result(uint8_t * rx,uint8_t * ry,uint16_t len)68 int ecc_hal_read_mul_result(uint8_t *rx, uint8_t *ry, uint16_t len)
69 {
70     ecc_mode_t mode = ecc_ll_get_mode();
71 
72     if (mode == ECC_MODE_VERIFY_THEN_POINT_MUL) {
73         if (!ecc_ll_get_verification_result()) {
74             memset(rx, 0x0, len);
75             memset(ry, 0x0, len);
76             return -1;
77         }
78     }
79 
80     ecc_ll_read_param(ECC_PARAM_PX, rx, len);
81     ecc_ll_read_param(ECC_PARAM_PY, ry, len);
82     return 0;
83 }
84 
ecc_hal_read_verify_result(void)85 int ecc_hal_read_verify_result(void)
86 {
87     return ecc_ll_get_verification_result();
88 }
89 
90 #if SOC_ECC_EXTENDED_MODES_SUPPORTED
91 
ecc_hal_set_mod_base(ecc_mod_base_t base)92 void ecc_hal_set_mod_base(ecc_mod_base_t base)
93 {
94     ecc_ll_set_mod_base(base);
95 }
96 
ecc_hal_write_jacob_verify_param(const uint8_t * qx,const uint8_t * qy,const uint8_t * qz,uint16_t len)97 void ecc_hal_write_jacob_verify_param(const uint8_t *qx, const uint8_t *qy, const uint8_t *qz, uint16_t len)
98 {
99     ecc_curve_t curve = len == 32 ? ECC_CURVE_SECP256R1 : ECC_CURVE_SECP192R1;
100     ecc_ll_set_curve(curve);
101 
102     clear_param_registers();
103 
104     ecc_ll_write_param(ECC_PARAM_QX, qx, len);
105     ecc_ll_write_param(ECC_PARAM_QY, qy, len);
106     ecc_ll_write_param(ECC_PARAM_QZ, qz, len);
107 }
108 
ecc_hal_read_jacob_mul_result(uint8_t * rx,uint8_t * ry,uint8_t * rz,uint16_t len)109 int ecc_hal_read_jacob_mul_result(uint8_t *rx, uint8_t *ry, uint8_t *rz, uint16_t len)
110 {
111     ecc_mode_t mode = ecc_ll_get_mode();
112 
113     if (mode == ECC_MODE_POINT_VERIFY_JACOBIAN_MUL) {
114         if (!ecc_ll_get_verification_result()) {
115             memset(rx, 0x0, len);
116             memset(ry, 0x0, len);
117             memset(rz, 0x0, len);
118             return -1;
119         }
120     }
121 
122     ecc_ll_read_param(ECC_PARAM_QX, rx, len);
123     ecc_ll_read_param(ECC_PARAM_QY, ry, len);
124     ecc_ll_read_param(ECC_PARAM_QZ, rz, len);
125     return 0;
126 }
127 
ecc_hal_write_point_add_param(const uint8_t * px,const uint8_t * py,const uint8_t * qx,const uint8_t * qy,const uint8_t * qz,uint16_t len)128 void ecc_hal_write_point_add_param(const uint8_t *px, const uint8_t *py, const uint8_t *qx, const uint8_t *qy, const uint8_t *qz, uint16_t len)
129 {
130     ecc_curve_t curve = len == 32 ? ECC_CURVE_SECP256R1 : ECC_CURVE_SECP192R1;
131     ecc_ll_set_curve(curve);
132 
133     clear_param_registers();
134 
135     ecc_ll_write_param(ECC_PARAM_PX, px, len);
136     ecc_ll_write_param(ECC_PARAM_PY, py, len);
137     ecc_ll_write_param(ECC_PARAM_QX, qx, len);
138     ecc_ll_write_param(ECC_PARAM_QY, qy, len);
139     ecc_ll_write_param(ECC_PARAM_QZ, qz, len);
140 }
141 
ecc_hal_read_point_add_result(uint8_t * rx,uint8_t * ry,uint8_t * rz,uint16_t len,bool read_jacob)142 int ecc_hal_read_point_add_result(uint8_t *rx, uint8_t *ry, uint8_t *rz, uint16_t len, bool read_jacob)
143 {
144     if (read_jacob) {
145         ecc_ll_read_param(ECC_PARAM_QX, rx, len);
146         ecc_ll_read_param(ECC_PARAM_QY, ry, len);
147         ecc_ll_read_param(ECC_PARAM_QZ, rz, len);
148     } else {
149         ecc_ll_read_param(ECC_PARAM_PX, rx, len);
150         ecc_ll_read_param(ECC_PARAM_PY, ry, len);
151     }
152     return 0;
153 }
154 
ecc_hal_write_mod_op_param(const uint8_t * a,const uint8_t * b,uint16_t len)155 void ecc_hal_write_mod_op_param(const uint8_t *a, const uint8_t *b, uint16_t len)
156 {
157     ecc_curve_t curve = len == 32 ? ECC_CURVE_SECP256R1 : ECC_CURVE_SECP192R1;
158     ecc_ll_set_curve(curve);
159 
160     clear_param_registers();
161 
162     ecc_ll_write_param(ECC_PARAM_PX, a, len);
163     ecc_ll_write_param(ECC_PARAM_PY, b, len);
164 }
165 
ecc_hal_read_mod_op_result(uint8_t * r,uint16_t len)166 int ecc_hal_read_mod_op_result(uint8_t *r, uint16_t len)
167 {
168     ecc_mode_t mode = ecc_ll_get_mode();
169     if (mode == ECC_MODE_MOD_ADD || mode == ECC_MODE_MOD_SUB) {
170         ecc_ll_read_param(ECC_PARAM_PX, r, len);
171     } else if (mode == ECC_MODE_MOD_MUL || mode == ECC_MODE_INVERSE_MUL) {
172         ecc_ll_read_param(ECC_PARAM_PY, r, len);
173     } else {
174         return -1;
175     }
176     return 0;
177 }
178 
179 #endif /* SOC_ECC_EXTENDED_MODES_SUPPORTED */
180