1 /**************************************************************************//**
2  * @file     crypto.h
3  * @version  V1.10
4  * @brief    Cryptographic Accelerator driver header file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2017-2020 Nuvoton Technology Corp. All rights reserved.
8  ******************************************************************************/
9 #ifndef __CRYPTO_H__
10 #define __CRYPTO_H__
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 /** @addtogroup Standard_Driver Standard Driver
18   @{
19 */
20 
21 /** @addtogroup CRYPTO_Driver CRYPTO Driver
22   @{
23 */
24 
25 
26 /** @addtogroup CRYPTO_EXPORTED_CONSTANTS CRYPTO Exported Constants
27   @{
28 */
29 
30 #define PRNG_KEY_SIZE_64        0UL     /*!< Select to generate 64-bit random key    \hideinitializer */
31 #define PRNG_KEY_SIZE_128       1UL     /*!< Select to generate 128-bit random key   \hideinitializer */
32 #define PRNG_KEY_SIZE_192       2UL     /*!< Select to generate 192-bit random key   \hideinitializer */
33 #define PRNG_KEY_SIZE_256       3UL     /*!< Select to generate 256-bit random key   \hideinitializer */
34 
35 #define PRNG_SEED_CONT          0UL     /*!< PRNG using current seed                 \hideinitializer */
36 #define PRNG_SEED_RELOAD        1UL     /*!< PRNG reload new seed                    \hideinitializer */
37 
38 #define AES_KEY_SIZE_128        0UL     /*!< AES select 128-bit key length           \hideinitializer */
39 #define AES_KEY_SIZE_192        1UL     /*!< AES select 192-bit key length           \hideinitializer */
40 #define AES_KEY_SIZE_256        2UL     /*!< AES select 256-bit key length           \hideinitializer */
41 
42 #define AES_MODE_ECB            0UL     /*!< AES select ECB mode                     \hideinitializer */
43 #define AES_MODE_CBC            1UL     /*!< AES select CBC mode                     \hideinitializer */
44 #define AES_MODE_CFB            2UL     /*!< AES select CFB mode                     \hideinitializer */
45 #define AES_MODE_OFB            3UL     /*!< AES select OFB mode                     \hideinitializer */
46 #define AES_MODE_CTR            4UL     /*!< AES select CTR mode                     \hideinitializer */
47 #define AES_MODE_CBC_CS1        0x10UL  /*!< AES select CBC CS1 mode                 \hideinitializer */
48 #define AES_MODE_CBC_CS2        0x11UL  /*!< AES select CBC CS2 mode                 \hideinitializer */
49 #define AES_MODE_CBC_CS3        0x12UL  /*!< AES select CBC CS3 mode                 \hideinitializer */
50 
51 #define AES_NO_SWAP             0UL     /*!< AES do not swap input and output data   \hideinitializer */
52 #define AES_OUT_SWAP            1UL     /*!< AES swap output data                    \hideinitializer */
53 #define AES_IN_SWAP             2UL     /*!< AES swap input data                     \hideinitializer */
54 #define AES_IN_OUT_SWAP         3UL     /*!< AES swap both input and output data     \hideinitializer */
55 
56 #define DES_MODE_ECB            0x000UL /*!< DES select ECB mode                     \hideinitializer */
57 #define DES_MODE_CBC            0x100UL /*!< DES select CBC mode                     \hideinitializer */
58 #define DES_MODE_CFB            0x200UL /*!< DES select CFB mode                     \hideinitializer */
59 #define DES_MODE_OFB            0x300UL /*!< DES select OFB mode                     \hideinitializer */
60 #define DES_MODE_CTR            0x400UL /*!< DES select CTR mode                     \hideinitializer */
61 #define TDES_MODE_ECB           0x004UL /*!< TDES select ECB mode                    \hideinitializer */
62 #define TDES_MODE_CBC           0x104UL /*!< TDES select CBC mode                    \hideinitializer */
63 #define TDES_MODE_CFB           0x204UL /*!< TDES select CFB mode                    \hideinitializer */
64 #define TDES_MODE_OFB           0x304UL /*!< TDES select OFB mode                    \hideinitializer */
65 #define TDES_MODE_CTR           0x404UL /*!< TDES select CTR mode                    \hideinitializer */
66 
67 #define TDES_NO_SWAP            0UL     /*!< TDES do not swap data                       \hideinitializer */
68 #define TDES_WHL_SWAP           1UL     /*!< TDES swap high-low word                     \hideinitializer */
69 #define TDES_OUT_SWAP           2UL     /*!< TDES swap output data                       \hideinitializer */
70 #define TDES_OUT_WHL_SWAP       3UL     /*!< TDES swap output data and high-low word     \hideinitializer */
71 #define TDES_IN_SWAP            4UL     /*!< TDES swap input data                        \hideinitializer */
72 #define TDES_IN_WHL_SWAP        5UL     /*!< TDES swap input data and high-low word      \hideinitializer */
73 #define TDES_IN_OUT_SWAP        6UL     /*!< TDES swap both input and output data        \hideinitializer */
74 #define TDES_IN_OUT_WHL_SWAP    7UL     /*!< TDES swap input, output and high-low word   \hideinitializer */
75 
76 #define SHA_MODE_SHA1           0UL     /*!< SHA select SHA-1 160-bit                \hideinitializer */
77 #define SHA_MODE_SHA224         5UL     /*!< SHA select SHA-224 224-bit              \hideinitializer */
78 #define SHA_MODE_SHA256         4UL     /*!< SHA select SHA-256 256-bit              \hideinitializer */
79 #define SHA_MODE_SHA384         7UL     /*!< SHA select SHA-384 384-bit              \hideinitializer */
80 #define SHA_MODE_SHA512         6UL     /*!< SHA select SHA-512 512-bit              \hideinitializer */
81 
82 #define SHA_NO_SWAP             0UL     /*!< SHA do not swap input and output data   \hideinitializer */
83 #define SHA_OUT_SWAP            1UL     /*!< SHA swap output data                    \hideinitializer */
84 #define SHA_IN_SWAP             2UL     /*!< SHA swap input data                     \hideinitializer */
85 #define SHA_IN_OUT_SWAP         3UL     /*!< SHA swap both input and output data     \hideinitializer */
86 
87 #define CRYPTO_DMA_FIRST        0x4UL   /*!< Do first encrypt/decrypt in DMA cascade \hideinitializer */
88 #define CRYPTO_DMA_ONE_SHOT     0x5UL   /*!< Do one shot encrypt/decrypt with DMA      \hideinitializer */
89 #define CRYPTO_DMA_CONTINUE     0x6UL   /*!< Do continuous encrypt/decrypt in DMA cascade \hideinitializer */
90 #define CRYPTO_DMA_LAST         0x7UL   /*!< Do last encrypt/decrypt in DMA cascade          \hideinitializer */
91 
92 
93 
94 typedef enum
95 {
96     CURVE_P_192,
97     CURVE_P_224,
98     CURVE_P_256,
99     CURVE_P_384,
100     CURVE_P_521,
101     CURVE_K_163,
102     CURVE_K_233,
103     CURVE_K_283,
104     CURVE_K_409,
105     CURVE_K_571,
106     CURVE_B_163,
107     CURVE_B_233,
108     CURVE_B_283,
109     CURVE_B_409,
110     CURVE_B_571
111 }
112 E_ECC_CURVE;
113 
114 
115 
116 typedef struct e_curve_t
117 {
118     E_ECC_CURVE  curve_id;
119     int32_t   Echar;
120     char  Ea[144];
121     char  Eb[144];
122     char  Px[144];
123     char  Py[144];
124     int32_t   Epl;
125     char  Pp[176];
126     int32_t   Eol;
127     char  Eorder[176];
128     int32_t   key_len;
129     int32_t   irreducible_k1;
130     int32_t   irreducible_k2;
131     int32_t   irreducible_k3;
132     int32_t   GF;
133 }  ECC_CURVE;
134 
135 
136 
137 /*@}*/ /* end of group CRYPTO_EXPORTED_CONSTANTS */
138 
139 
140 /** @addtogroup CRYPTO_EXPORTED_MACROS CRYPTO Exported Macros
141   @{
142 */
143 
144 /*----------------------------------------------------------------------------------------------*/
145 /*  Macros                                                                                      */
146 /*----------------------------------------------------------------------------------------------*/
147 
148 /**
149   * @brief This macro enables PRNG interrupt.
150   * @param crpt     Specified crypto module
151   * @return None
152   * \hideinitializer
153   */
154 #define PRNG_ENABLE_INT(crpt)       ((crpt)->INTEN |= CRPT_INTEN_PRNGIEN_Msk)
155 
156 /**
157   * @brief This macro disables PRNG interrupt.
158   * @param crpt     Specified crypto module
159   * @return None
160   * \hideinitializer
161   */
162 #define PRNG_DISABLE_INT(crpt)      ((crpt)->INTEN &= ~CRPT_INTEN_PRNGIEN_Msk)
163 
164 /**
165   * @brief This macro gets PRNG interrupt flag.
166   * @param crpt     Specified crypto module
167   * @return PRNG interrupt flag.
168   * \hideinitializer
169   */
170 #define PRNG_GET_INT_FLAG(crpt)     ((crpt)->INTSTS & CRPT_INTSTS_PRNGIF_Msk)
171 
172 /**
173   * @brief This macro clears PRNG interrupt flag.
174   * @param crpt     Specified crypto module
175   * @return None
176   * \hideinitializer
177   */
178 #define PRNG_CLR_INT_FLAG(crpt)     ((crpt)->INTSTS = CRPT_INTSTS_PRNGIF_Msk)
179 
180 /**
181   * @brief This macro enables AES interrupt.
182   * @param crpt     Specified crypto module
183   * @return None
184   * \hideinitializer
185   */
186 #define AES_ENABLE_INT(crpt)        ((crpt)->INTEN |= (CRPT_INTEN_AESIEN_Msk|CRPT_INTEN_AESEIEN_Msk))
187 
188 /**
189   * @brief This macro disables AES interrupt.
190   * @param crpt     Specified crypto module
191   * @return None
192   * \hideinitializer
193   */
194 #define AES_DISABLE_INT(crpt)       ((crpt)->INTEN &= ~(CRPT_INTEN_AESIEN_Msk|CRPT_INTEN_AESEIEN_Msk))
195 
196 /**
197   * @brief This macro gets AES interrupt flag.
198   * @param crpt     Specified crypto module
199   * @return AES interrupt flag.
200   * \hideinitializer
201   */
202 #define AES_GET_INT_FLAG(crpt)      ((crpt)->INTSTS & (CRPT_INTSTS_AESIF_Msk|CRPT_INTSTS_AESEIF_Msk))
203 
204 /**
205   * @brief This macro clears AES interrupt flag.
206   * @param crpt     Specified crypto module
207   * @return None
208   * \hideinitializer
209   */
210 #define AES_CLR_INT_FLAG(crpt)      ((crpt)->INTSTS = (CRPT_INTSTS_AESIF_Msk|CRPT_INTSTS_AESEIF_Msk))
211 
212 /**
213   * @brief This macro enables AES key protection.
214   * @param crpt     Specified crypto module
215   * @return None
216   * \hideinitializer
217   */
218 #define AES_ENABLE_KEY_PROTECT(crpt)  ((crpt)->AES_CTL |= CRPT_AES_CTL_KEYPRT_Msk)
219 
220 /**
221   * @brief This macro disables AES key protection.
222   * @param crpt     Specified crypto module
223   * @return None
224   * \hideinitializer
225   */
226 #define AES_DISABLE_KEY_PROTECT(crpt) ((crpt)->AES_CTL = ((crpt)->AES_CTL & ~CRPT_AES_CTL_KEYPRT_Msk) | (0x16UL<<CRPT_AES_CTL_KEYUNPRT_Pos)); \
227                                       ((crpt)->AES_CTL &= ~CRPT_AES_CTL_KEYPRT_Msk)
228 
229 /**
230   * @brief This macro enables TDES interrupt.
231   * @param crpt     Specified crypto module
232   * @return None
233   * \hideinitializer
234   */
235 #define TDES_ENABLE_INT(crpt)       ((crpt)->INTEN |= (CRPT_INTEN_TDESIEN_Msk|CRPT_INTEN_TDESEIEN_Msk))
236 
237 /**
238   * @brief This macro disables TDES interrupt.
239   * @param crpt     Specified crypto module
240   * @return None
241   * \hideinitializer
242   */
243 #define TDES_DISABLE_INT(crpt)      ((crpt)->INTEN &= ~(CRPT_INTEN_TDESIEN_Msk|CRPT_INTEN_TDESEIEN_Msk))
244 
245 /**
246   * @brief This macro gets TDES interrupt flag.
247   * @param crpt     Specified crypto module
248   * @return TDES interrupt flag.
249   * \hideinitializer
250   */
251 #define TDES_GET_INT_FLAG(crpt)     ((crpt)->INTSTS & (CRPT_INTSTS_TDESIF_Msk|CRPT_INTSTS_TDESEIF_Msk))
252 
253 /**
254   * @brief This macro clears TDES interrupt flag.
255   * @param crpt     Specified crypto module
256   * @return None
257   * \hideinitializer
258   */
259 #define TDES_CLR_INT_FLAG(crpt)     ((crpt)->INTSTS = (CRPT_INTSTS_TDESIF_Msk|CRPT_INTSTS_TDESEIF_Msk))
260 
261 /**
262   * @brief This macro enables TDES key protection.
263   * @param crpt     Specified crypto module
264   * @return None
265   * \hideinitializer
266   */
267 #define TDES_ENABLE_KEY_PROTECT(crpt)  ((crpt)->TDES_CTL |= CRPT_TDES_CTL_KEYPRT_Msk)
268 
269 /**
270   * @brief This macro disables TDES key protection.
271   * @param crpt     Specified crypto module
272   * @return None
273   * \hideinitializer
274   */
275 #define TDES_DISABLE_KEY_PROTECT(crpt) ((crpt)->TDES_CTL = ((crpt)->TDES_CTL & ~CRPT_TDES_CTL_KEYPRT_Msk) | (0x16UL<<CRPT_TDES_CTL_KEYUNPRT_Pos)); \
276                                        ((crpt)->TDES_CTL &= ~CRPT_TDES_CTL_KEYPRT_Msk)
277 
278 /**
279   * @brief This macro enables SHA interrupt.
280   * @param crpt     Specified crypto module
281   * @return None
282   * \hideinitializer
283   */
284 #define SHA_ENABLE_INT(crpt)        ((crpt)->INTEN |= (CRPT_INTEN_HMACIEN_Msk|CRPT_INTEN_HMACEIEN_Msk))
285 
286 /**
287   * @brief This macro disables SHA interrupt.
288   * @param crpt     Specified crypto module
289   * @return None
290   * \hideinitializer
291   */
292 #define SHA_DISABLE_INT(crpt)       ((crpt)->INTEN &= ~(CRPT_INTEN_HMACIEN_Msk|CRPT_INTEN_HMACEIEN_Msk))
293 
294 /**
295   * @brief This macro gets SHA interrupt flag.
296   * @param crpt     Specified crypto module
297   * @return SHA interrupt flag.
298   * \hideinitializer
299   */
300 #define SHA_GET_INT_FLAG(crpt)      ((crpt)->INTSTS & (CRPT_INTSTS_HMACIF_Msk|CRPT_INTSTS_HMACEIF_Msk))
301 
302 /**
303   * @brief This macro clears SHA interrupt flag.
304   * @param crpt     Specified crypto module
305   * @return None
306   * \hideinitializer
307   */
308 #define SHA_CLR_INT_FLAG(crpt)      ((crpt)->INTSTS = (CRPT_INTSTS_HMACIF_Msk|CRPT_INTSTS_HMACEIF_Msk))
309 
310 /**
311   * @brief This macro enables ECC interrupt.
312   * @param crpt     Specified crypto module
313   * @return None
314   * \hideinitializer
315   */
316 #define ECC_ENABLE_INT(crpt)        ((crpt)->INTEN |= (CRPT_INTEN_ECCIEN_Msk|CRPT_INTEN_ECCEIEN_Msk))
317 
318 /**
319   * @brief This macro disables ECC interrupt.
320   * @param crpt     Specified crypto module
321   * @return None
322   * \hideinitializer
323   */
324 #define ECC_DISABLE_INT(crpt)       ((crpt)->INTEN &= ~(CRPT_INTEN_ECCIEN_Msk|CRPT_INTEN_ECCEIEN_Msk))
325 
326 /**
327   * @brief This macro gets ECC interrupt flag.
328   * @param crpt     Specified crypto module
329   * @return ECC interrupt flag.
330   * \hideinitializer
331   */
332 #define ECC_GET_INT_FLAG(crpt)      ((crpt)->INTSTS & (CRPT_INTSTS_ECCIF_Msk|CRPT_INTSTS_ECCEIF_Msk))
333 
334 /**
335   * @brief This macro clears ECC interrupt flag.
336   * @param crpt     Specified crypto module
337   * @return None
338   * \hideinitializer
339   */
340 #define ECC_CLR_INT_FLAG(crpt)      ((crpt)->INTSTS = (CRPT_INTSTS_ECCIF_Msk|CRPT_INTSTS_ECCEIF_Msk))
341 
342 
343 /*@}*/ /* end of group CRYPTO_EXPORTED_MACROS */
344 
345 
346 
347 /** @addtogroup CRYPTO_EXPORTED_FUNCTIONS CRYPTO Exported Functions
348   @{
349 */
350 
351 /*---------------------------------------------------------------------------------------------------------*/
352 /*  Functions                                                                                      */
353 /*---------------------------------------------------------------------------------------------------------*/
354 
355 void PRNG_Open(CRPT_T *crpt, uint32_t u32KeySize, uint32_t u32SeedReload, uint32_t u32Seed);
356 void PRNG_Start(CRPT_T *crpt);
357 void PRNG_Read(CRPT_T *crpt, uint32_t u32RandKey[]);
358 void AES_Open(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32EncDec, uint32_t u32OpMode, uint32_t u32KeySize, uint32_t u32SwapType);
359 void AES_Start(CRPT_T *crpt, int32_t u32Channel, uint32_t u32DMAMode);
360 void AES_SetKey(CRPT_T *crpt, uint32_t u32Channel, uint32_t au32Keys[], uint32_t u32KeySize);
361 void AES_SetInitVect(CRPT_T *crpt, uint32_t u32Channel, uint32_t au32IV[]);
362 void AES_SetDMATransfer(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt);
363 void TDES_Open(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32EncDec, int32_t Is3DES, int32_t Is3Key, uint32_t u32OpMode, uint32_t u32SwapType);
364 void TDES_Start(CRPT_T *crpt, int32_t u32Channel, uint32_t u32DMAMode);
365 void TDES_SetKey(CRPT_T *crpt, uint32_t u32Channel, uint32_t au32Keys[3][2]);
366 void TDES_SetInitVect(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32IVH, uint32_t u32IVL);
367 void TDES_SetDMATransfer(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt);
368 void SHA_Open(CRPT_T *crpt, uint32_t u32OpMode, uint32_t u32SwapType, uint32_t hmac_key_len);
369 void SHA_Start(CRPT_T *crpt, uint32_t u32DMAMode);
370 void SHA_SetDMATransfer(CRPT_T *crpt, uint32_t u32SrcAddr, uint32_t u32TransCnt);
371 void SHA_Read(CRPT_T *crpt, uint32_t u32Digest[]);
372 void ECC_DriverISR(CRPT_T *crpt);
373 int  ECC_IsPrivateKeyValid(CRPT_T *crpt, E_ECC_CURVE ecc_curve,  char private_k[]);
374 int32_t  ECC_GenerateSecretZ(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *private_k, char public_k1[], char public_k2[], char secret_z[]);
375 int32_t  ECC_GeneratePublicKey(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *private_k, char public_k1[], char public_k2[]);
376 int32_t  ECC_GenerateSignature(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *message, char *d, char *k, char *R, char *S);
377 int32_t  ECC_VerifySignature(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *message, char *public_k1, char *public_k2, char *R, char *S);
378 
379 
380 /*@}*/ /* end of group CRYPTO_EXPORTED_FUNCTIONS */
381 
382 /*@}*/ /* end of group CRYPTO_Driver */
383 
384 /*@}*/ /* end of group Standard_Driver */
385 
386 #ifdef __cplusplus
387 }
388 #endif
389 
390 #endif  /* __CRYPTO_H__ */
391 
392