InPlay API
hal_ecc.h
1 
13 #ifndef HAL_ECC_H
14 #define HAL_ECC_H
15 
25 #include "stdint.h"
26 #include "in_mmap.h"
27 #include "in_compile.h"
28 
29 /*
30  ****************************************************************************************
31  * DEFINES
32  ****************************************************************************************
33  */
34 
35 #define ECC_CTRL 0x00000000UL
36 #define ECC_ENTRY_PNT 0x00000004UL
37 #define ECC_RTN_CODE 0x00000008UL
38 #define ECC_BUILD_CONFIG 0x0000000CUL
39 #define ECC_STACK_PNTR 0x00000010UL
40 #define ECC_INSTR_SINCE_GO 0x00000014UL
41 #define ECC_CONFIG 0x0000001CUL
42 #define ECC_STAT 0x00000020UL
43 #define ECC_FLAGS 0x00000024UL
44 #define ECC_WATCHDOG 0x00000028UL
45 #define ECC_CYCLES_SINCE_GO 0x0000002CUL
46 #define ECC_INDEX_I 0x00000030UL
47 #define ECC_INDEX_J 0x00000034UL
48 #define ECC_INDEX_K 0x00000038UL
49 #define ECC_INDEX_L 0x0000003CUL
50 #define ECC_IRQ_EN 0x00000040UL
51 #define ECC_JMP_PROB 0x00000044UL
52 #define ECC_JMP_PROB_LFSR 0x00000048UL
53 #define ECC_BANK_SW_A 0x00000050UL
54 #define ECC_BANK_SW_B 0x00000054UL
55 #define ECC_BANK_SW_C 0x00000058UL
56 #define ECC_BANK_SW_D 0x0000005CUL
57 #define ECC_Reserve1 0x00000000UL
58 #define ECC_Reserve2 0x00000000UL
59 
60 #define ECC_CTRL_GO 0x80000000UL
61 #define ECC_CTRL_STOP 0x08000000UL
62 #define ECC_CTRL_BASE_RADIX 0x00000700UL
63 #define ECC_CTRL_PARTIAL_RADIX 0x000000FFUL
64 
65 static INLINE void ecc_go(void)
66 {
67  uint32_t reg = RD_WORD(ECC_BASE + ECC_CTRL);
68  reg |= ECC_CTRL_GO;
69  WR_WORD(ECC_BASE + ECC_CTRL, reg);
70 }
71 
72 static INLINE void ecc_halt(void)
73 {
74  uint32_t reg = RD_WORD(ECC_BASE + ECC_CTRL);
75  reg |= ECC_CTRL_STOP;
76  WR_WORD(ECC_BASE + ECC_CTRL, reg);
77 }
78 
79 static INLINE void ecc_base_radix(int radix)
80 {
81  uint32_t reg = RD_WORD(ECC_BASE + ECC_CTRL);
82  reg &= ~ECC_CTRL_BASE_RADIX;
83  reg |= (radix << 8);
84  WR_WORD(ECC_BASE + ECC_CTRL, reg);
85 }
86 
87 static INLINE void ecc_partial_radix(int radix)
88 {
89  uint32_t reg = RD_WORD(ECC_BASE + ECC_CTRL);
90  reg &= ~ECC_CTRL_PARTIAL_RADIX;
91  reg |= radix;
92  WR_WORD(ECC_BASE + ECC_CTRL, reg);
93 }
94 
95 #define ECC_ENTRY_PNT_ADDR 0x00000FFFUL
96 
97 static INLINE void ecc_entry_addr(uint32_t addr)
98 {
99  uint32_t reg = RD_WORD(ECC_BASE + ECC_ENTRY_PNT);
100  reg &= ~ECC_ENTRY_PNT_ADDR;
101  reg |= addr;
102  WR_WORD(ECC_BASE + ECC_ENTRY_PNT, reg);
103 }
104 
105 static INLINE int ecc_rtn_busy(void)
106 {
107  return ((RD_WORD(ECC_BASE + ECC_RTN_CODE) >> 31) & 1);
108 }
109 
110 static INLINE int ecc_rtn_irq(void)
111 {
112  return ((RD_WORD(ECC_BASE + ECC_RTN_CODE) >> 30) & 1);
113 }
114 
115 static INLINE int ecc_rtn_zero(void)
116 {
117  return ((RD_WORD(ECC_BASE + ECC_RTN_CODE) >> 28) & 1);
118 }
119 
120 #define ECC_RTN_REASON_NORMAL_STOP 0
121 #define ECC_RTN_REASON_INVALID_OP_CODE 1
122 #define ECC_RTN_REASON_STACK_OVF 2
123 #define ECC_RTN_REASON_STACK_UNF 3
124 #define ECC_RTN_REASON_WDT 4
125 #define ECC_RTN_REASON_HOST_REQ 5
126 
127 static INLINE int ecc_rtn_reason(void)
128 {
129  return ((RD_WORD(ECC_BASE + ECC_RTN_CODE) >> 16) & 0xFF);
130 }
131 
132 static INLINE void ecc_stack_pntr_clear(void)
133 {
134  WR_WORD(ECC_BASE + ECC_STACK_PNTR, 0);
135 }
136 
137 #define ECC_CONFIG_ENDIAN_SWAP (1 << 26)
138 
139 static INLINE void ecc_config_endian_swap(void)
140 {
141  uint32_t reg = RD_WORD(ECC_BASE + ECC_CONFIG);
142  reg ^= ECC_CONFIG_ENDIAN_SWAP;
143  WR_WORD(ECC_BASE + ECC_CONFIG, reg);
144 }
145 
146 #define ECC_STAT_DONE 0x40000000UL
147 
148 static INLINE uint32_t ecc_stat(void)
149 {
150  return RD_WORD(ECC_BASE + ECC_STAT);
151 }
152 
153 static INLINE void ecc_stat_ack_irq(void)
154 {
155  uint32_t reg = RD_WORD(ECC_BASE + ECC_STAT);
156  reg |= (1<<30);
157  WR_WORD(ECC_BASE + ECC_STAT, reg);
158 }
159 
160 static INLINE void ecc_irq_enable(void)
161 {
162  uint32_t reg = RD_WORD(ECC_BASE + ECC_IRQ_EN);
163  reg |= (1<<30);
164  WR_WORD(ECC_BASE + ECC_IRQ_EN, reg);
165 }
166 
167 static INLINE void ecc_irq_disable(void)
168 {
169  uint32_t reg = RD_WORD( + ECC_IRQ_EN);
170  reg &= ~(1 << 30);
171  WR_WORD(ECC_BASE + ECC_IRQ_EN, reg);
172 }
173 
174 #define ECC_FLAG_ZERO 0x1
175 #define ECC_FLAG_MEM_TEST 0x2
176 #define ECC_FLAG_BORROW 0x4
177 #define ECC_FLAG_CARRY 0x8
178 
179 static INLINE uint32_t ecc_flag(void)
180 {
181  return RD_WORD(ECC_BASE + ECC_FLAGS);
182 }
183 
184 static INLINE void ecc_flag_clear(void)
185 {
186  WR_WORD(ECC_BASE + ECC_FLAGS, 0);
187 }
188 
189 static INLINE void ecc_stack_clear(void)
190 {
191  WR_WORD(ECC_BASE + ECC_STACK_PNTR, 0);
192 }
193 
194 static INLINE void ecc_mem_en(void)
195 {
196  WR_WORD(GLOBAL_REG_AES_ECC_MEM_SEL, GLOBAL_REG_AES_ECC_MEM_SEL_DEFAULT);
197 }
198 
199 static INLINE int ecc_mem_aes(void)
200 {
201  return ((RD_WORD(GLOBAL_REG_AES_ECC_MEM_SEL) & GLOBAL_REG_AES_ECC_MEM_SEL_CTL_AES_ECC_MEM_SEL) ? 1 : 0);
202 }
203 
204 static INLINE int ecc_reset_get(void)
205 {
206  return ((RD_WORD(GLOBAL_REG_RESET_CTRL_6) & GLOBAL_REG_RESET_CTRL_6_CTL_RESET_6_D0_SECURITY_CORE_ECC_PCLK_RSTN_REG) ? 1 : 0);
207 }
208 
209 static INLINE void ecc_reset_set_0(void)
210 {
211  WR_WORD(GLOBAL_REG_RESET_CTRL_6, (RD_WORD(GLOBAL_REG_RESET_CTRL_6) & ~GLOBAL_REG_RESET_CTRL_6_CTL_RESET_6_D0_SECURITY_CORE_ECC_PCLK_RSTN_REG));
212 }
213 
214 static INLINE void ecc_reset_set_1(void)
215 {
216  WR_WORD(GLOBAL_REG_RESET_CTRL_6, (RD_WORD(GLOBAL_REG_RESET_CTRL_6) | GLOBAL_REG_RESET_CTRL_6_CTL_RESET_6_D0_SECURITY_CORE_ECC_PCLK_RSTN_REG));
217 }
218 
219 
220 #define ECC_A0_BASE (ECC_BASE + 0x400)
221 #define ECC_B0_BASE (ECC_BASE + 0x800)
222 #define ECC_C0_BASE (ECC_BASE + 0xC00)
223 #define ECC_D0_BASE (ECC_BASE + 0x1000)
224 
225 #define ECC_FW_BASE (ECC_BASE + 0x4000)
226 
227 #define ECC_FW_ENTRY_CALC_MP 0x10
228 #define ECC_FW_ENTRY_CALC_R_INV 0x11
229 #define ECC_FW_ENTRY_CALC_R_SQR 0x12
230 #define ECC_FW_ENTRY_IS_A_M3 0x1d
231 #define ECC_FW_ENTRY_IS_P_EQUAL_Q 0x1b
232 #define ECC_FW_ENTRY_IS_P_REFLECT_Q 0x1c
233 #define ECC_FW_ENTRY_MODADD 0xb
234 #define ECC_FW_ENTRY_MODDIV 0xd
235 #define ECC_FW_ENTRY_MODINV 0xe
236 #define ECC_FW_ENTRY_MODMULT 0xa
237 #define ECC_FW_ENTRY_MODSUB 0xc
238 #define ECC_FW_ENTRY_MULT 0x13
239 #define ECC_FW_ENTRY_PADD 0x17
240 #define ECC_FW_ENTRY_PADD_STD_PRJ 0x18
241 #define ECC_FW_ENTRY_PDBL 0x15
242 #define ECC_FW_ENTRY_PDBL_STD_PRJ 0x16
243 #define ECC_FW_ENTRY_PMULT 0x14
244 #define ECC_FW_ENTRY_PVER 0x19
245 #define ECC_FW_ENTRY_REDUCE 0xf
246 #define ECC_FW_ENTRY_SHAMIR 0x1e
247 #define ECC_FW_ENTRY_STD_PRJ_TO_AFFINE 0x1a
248 
250 typedef enum {
273  ECC_ERR_INVALID_OP_CODE = (ECC_ERR_HW_BASE + 1),
274  ECC_ERR_STACK_OVF = (ECC_ERR_HW_BASE + 2),
275  ECC_ERR_STACK_UNF = (ECC_ERR_HW_BASE + 3),
276  ECC_ERR_WDT = (ECC_ERR_HW_BASE + 4),
277  ECC_ERR_HOST_REQ = (ECC_ERR_HW_BASE + 5),
279 
292 int hal_ecc_open(int int_priority) ;
293 
301 int hal_ecc_close(void);
302 
311 int hal_ecc_is_open(void);
312 
327 int hal_ecc_r_inv_mod_m_block(uint16_t size, const uint8_t *m, uint8_t *r_inv, uint32_t tmo);
328 int hal_ecc_r_inv_mod_m_non_block(uint16_t size, const uint8_t *m, uint8_t *r_inv, void (*callback)(int status));
329 
345 int hal_ecc_m_prime_block(uint16_t size, const uint8_t *r_inv, const uint8_t *m, uint8_t *mp, uint32_t tmo);
346 int hal_ecc_m_prime_non_block(uint16_t size, const uint8_t *r_inv, const uint8_t *m, uint8_t *mp, void (*callback)(int status));
347 
348 
364 int hal_ecc_r_sqr_mod_m_block(uint16_t size, const uint8_t *r_inv, const uint8_t *m, uint8_t *r_sqr, uint32_t tmo);
365 int hal_ecc_r_sqr_mod_m_non_block(uint16_t size, const uint8_t *r_inv, const uint8_t *m, uint8_t *r_sqr, void (*callback)(int status));
366 
367 
386 int hal_ecc_mod_mult_block(uint16_t size, const uint8_t *x, const uint8_t *y, const uint8_t *m, const uint8_t *mp, const uint8_t *r_sqr, uint8_t *c, uint32_t tmo);
387 int hal_ecc_mod_mult_non_block(uint16_t size, const uint8_t *x, const uint8_t *y, const uint8_t *m, const uint8_t *mp, const uint8_t *r_sqr, uint8_t *c, void (*callback)(int status));
388 
405 int hal_ecc_mod_add_block(uint16_t size, const uint8_t *x, const uint8_t *y, const uint8_t *m, uint8_t *c, uint32_t tmo);
406 int hal_ecc_mod_add_non_block(uint16_t size, const uint8_t *x, const uint8_t *y, const uint8_t *m, uint8_t *c, void (*callback)(int status));
407 
424 int hal_ecc_mod_sub_block(uint16_t size, const uint8_t *x, const uint8_t *y, const uint8_t *m, uint8_t *c, uint32_t tmo);
425 int hal_ecc_mod_sub_non_block(uint16_t size, const uint8_t *x, const uint8_t *y, const uint8_t *m, uint8_t *c, void (*callback)(int status));
426 
443 int hal_ecc_mod_div_block(uint16_t size, const uint8_t *y, const uint8_t *x, const uint8_t *m, uint8_t *c, uint32_t tmo);
444 int hal_ecc_mod_div_non_block(uint16_t size, const uint8_t *y, const uint8_t *x, const uint8_t *m, uint8_t *c, void (*callback)(int status));
445 
446 
462 int hal_ecc_mod_inv_block(uint16_t size, const uint8_t *x, const uint8_t *m, uint8_t *c, uint32_t tmo);
463 int hal_ecc_mod_inv_non_block(uint16_t size, const uint8_t *x, const uint8_t *m, uint8_t *c, void (*callback)(int status));
464 
465 
488 int hal_ecc_pmult_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *a, const uint8_t *w,
489  const uint8_t *p, const uint8_t *mp, const uint8_t *r_sqr, const uint8_t *k, uint8_t *Rx, uint8_t *Ry, uint32_t tmo);
490 int hal_ecc_pmult_non_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *a, const uint8_t *w,
491  const uint8_t *p, const uint8_t *mp, const uint8_t *r_sqr, const uint8_t *k, uint8_t *Rx, uint8_t *Ry, void (*callback)(int status));
492 
493 
516 int hal_ecc_padd_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *Qx, const uint8_t *Qy, const uint8_t *a, const uint8_t *p,
517  const uint8_t *mp, const uint8_t *r_sqr, uint8_t *Rx, uint8_t *Ry, uint32_t tmo);
518 int hal_ecc_padd_non_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *Qx, const uint8_t *Qy, const uint8_t *a, const uint8_t *p,
519  const uint8_t *mp, const uint8_t *r_sqr, uint8_t *Rx, uint8_t *Ry, void (*callback)(int status));
520 
541 int hal_ecc_pdbl_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *a, const uint8_t *p, const uint8_t *mp,
542  const uint8_t *r_sqr, uint8_t *Rx, uint8_t *Ry, uint32_t tmo);
543 int hal_ecc_pdbl_non_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *a, const uint8_t *p, const uint8_t *mp,
544  const uint8_t *r_sqr, uint8_t *Rx, uint8_t *Ry, void (*callback)(int status));
545 
564 int hal_ecc_pver_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *a, const uint8_t *b, const uint8_t *p, const uint8_t *mp,
565  const uint8_t *r_sqr, uint32_t tmo);
566 int hal_ecc_pver_non_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *a, const uint8_t *b, const uint8_t *p, const uint8_t *mp,
567  const uint8_t *r_sqr, void (*callback)(int status));
568 
592 int hal_ecc_shamir_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *Qx, const uint8_t *Qy, const uint8_t *a, const uint8_t *k,
593  const uint8_t *l, const uint8_t *p, const uint8_t *mp, const uint8_t *r_sqr, uint8_t *Rx, uint8_t *Ry, uint32_t tmo);
594 int hal_ecc_shamir_non_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *Qx, const uint8_t *Qy, const uint8_t *a, const uint8_t *k,
595  const uint8_t *l, const uint8_t *p, const uint8_t *mp, const uint8_t *r_sqr, uint8_t *Rx, uint8_t *Ry, void (*callback)(int status));
596 
598 #endif
599 
int hal_ecc_is_open(void)
Ecc and Aes shared the same HW memory. This is to inform AES that Ecc currently is using the HW memor...
int hal_ecc_open(int int_priority)
Initialize ECC driver. : All the Ecc APIs will be blocked call if "callback" function is not specifie...
ecc_result_status
ECC function status return.
Definition: hal_ecc.h:250
int hal_ecc_mod_sub_block(uint16_t size, const uint8_t *x, const uint8_t *y, const uint8_t *m, uint8_t *c, uint32_t tmo)
Subtracts two integers, each of a size contained in the operand size set, over the modular field m...
Error, ECC driver don&#39;t have enough resource.
Definition: hal_ecc.h:256
Error, ECC bad state.
Definition: hal_ecc.h:258
Error, ECC driver not initialized.
Definition: hal_ecc.h:254
Error, invalid elliptic curve (EC) point. Point is not on elliptic curve.
Definition: hal_ecc.h:262
int hal_ecc_mod_inv_block(uint16_t size, const uint8_t *x, const uint8_t *m, uint8_t *c, uint32_t tmo)
Calculates the inversion of an integer, of a size contained in the operand size set, over the modular field m. (1 / x) mod m.
Error, ECC operation returned abnormal stop condition.
Definition: hal_ecc.h:260
int hal_ecc_pver_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *a, const uint8_t *b, const uint8_t *p, const uint8_t *mp, const uint8_t *r_sqr, uint32_t tmo)
Computes y2 == x3 + ax + b mod p and verified LHS == RHS.
int hal_ecc_pdbl_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *a, const uint8_t *p, const uint8_t *mp, const uint8_t *r_sqr, uint8_t *Rx, uint8_t *Ry, uint32_t tmo)
Computes the EC Point Doubling over the curve y2 = x3 + ax + b mod p. R = 2P.
int hal_ecc_r_inv_mod_m_block(uint16_t size, const uint8_t *m, uint8_t *r_inv, uint32_t tmo)
Computes the modular inverse of r. (1/(r mod m))
No errors, good status.
Definition: hal_ecc.h:252
int hal_ecc_shamir_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *Qx, const uint8_t *Qy, const uint8_t *a, const uint8_t *k, const uint8_t *l, const uint8_t *p, const uint8_t *mp, const uint8_t *r_sqr, uint8_t *Rx, uint8_t *Ry, uint32_t tmo)
Calculate R = kP + lQ using Shamir&#39;s Trick.
int hal_ecc_mod_div_block(uint16_t size, const uint8_t *y, const uint8_t *x, const uint8_t *m, uint8_t *c, uint32_t tmo)
Divides one integer by another, each of a size contained in the operand size set, over the modular fi...
int hal_ecc_mod_mult_block(uint16_t size, const uint8_t *x, const uint8_t *y, const uint8_t *m, const uint8_t *mp, const uint8_t *r_sqr, uint8_t *c, uint32_t tmo)
Multiplies two integers, each of a size contained in the operand size set, over the modular field m...
int hal_ecc_pmult_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *a, const uint8_t *w, const uint8_t *p, const uint8_t *mp, const uint8_t *r_sqr, const uint8_t *k, uint8_t *Rx, uint8_t *Ry, uint32_t tmo)
Computes the ECC Point Multiplication over the curve y^2 = x^3 + ax + b mod p. R = 2P...
int hal_ecc_padd_block(uint16_t size, const uint8_t *Px, const uint8_t *Py, const uint8_t *Qx, const uint8_t *Qy, const uint8_t *a, const uint8_t *p, const uint8_t *mp, const uint8_t *r_sqr, uint8_t *Rx, uint8_t *Ry, uint32_t tmo)
Computes the ECC Point Addition over the curve y^2 = x^3 + ax + b mod p. R = P + Q.
Error, ECC return pending.
Definition: hal_ecc.h:268
int hal_ecc_mod_add_block(uint16_t size, const uint8_t *x, const uint8_t *y, const uint8_t *m, uint8_t *c, uint32_t tmo)
Adds two integers, each of a size contained in the operand size set, over the modular (x + y) mod m...
Error, invalid ECC size parameter.
Definition: hal_ecc.h:264
Error, return from HW.
Definition: hal_ecc.h:272
Error, ECC return pending.
Definition: hal_ecc.h:270
int hal_ecc_m_prime_block(uint16_t size, const uint8_t *r_inv, const uint8_t *m, uint8_t *mp, uint32_t tmo)
Computes the Montgomery residue constant. (m&#39;)
int hal_ecc_close(void)
De-Initialize ECC driver.
int hal_ecc_r_sqr_mod_m_block(uint16_t size, const uint8_t *r_inv, const uint8_t *m, uint8_t *r_sqr, uint32_t tmo)
Computes the Montgomery residue conversion constant. (r^2) mod m.
Error, AES using the HW memory.
Definition: hal_ecc.h:266