1 /**************************************************************************//**
2  * @file     crypto.h
3  * @version  V3.00
4  * @brief    Cryptographic Accelerator driver header file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 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 
31 #define PRNG_KEY_SIZE_128       ( 0UL)     /*!< Select to generate 128-bit random key   \hideinitializer */
32 #define PRNG_KEY_SIZE_163       ( 1UL)     /*!< Select to generate 163-bit random key   \hideinitializer */
33 #define PRNG_KEY_SIZE_192       ( 2UL)     /*!< Select to generate 192-bit random key   \hideinitializer */
34 #define PRNG_KEY_SIZE_224       ( 3UL)     /*!< Select to generate 224-bit random key   \hideinitializer */
35 #define PRNG_KEY_SIZE_255       ( 4UL)     /*!< Select to generate 255-bit random key   \hideinitializer */
36 #define PRNG_KEY_SIZE_256       ( 6UL)     /*!< Select to generate 256-bit random key   \hideinitializer */
37 #define PRNG_KEY_SIZE_283       ( 7UL)     /*!< Select to generate 283-bit random key (Key Store Only)  \hideinitializer */
38 #define PRNG_KEY_SIZE_384       ( 8UL)     /*!< Select to generate 384-bit random key (Key Store Only)  \hideinitializer */
39 #define PRNG_KEY_SIZE_409       ( 9UL)     /*!< Select to generate 409-bit random key (Key Store Only)  \hideinitializer */
40 #define PRNG_KEY_SIZE_512       (10UL)     /*!< Select to generate 512-bit random key (Key Store Only)  \hideinitializer */
41 #define PRNG_KEY_SIZE_521       (11UL)     /*!< Select to generate 521-bit random key (Key Store Only)  \hideinitializer */
42 #define PRNG_KEY_SIZE_571       (12UL)     /*!< Select to generate 571-bit random key (Key Store Only)  \hideinitializer */
43 
44 #define PRNG_SEED_CONT          (0UL)     /*!< PRNG using current seed                 \hideinitializer */
45 #define PRNG_SEED_RELOAD        (1UL)     /*!< PRNG reload new seed                    \hideinitializer */
46 
47 #define AES_KEY_SIZE_128        (0UL)     /*!< AES select 128-bit key length           \hideinitializer */
48 #define AES_KEY_SIZE_192        (1UL)     /*!< AES select 192-bit key length           \hideinitializer */
49 #define AES_KEY_SIZE_256        (2UL)     /*!< AES select 256-bit key length           \hideinitializer */
50 
51 #define AES_MODE_ECB            (0UL)     /*!< AES select ECB mode                     \hideinitializer */
52 #define AES_MODE_CBC            (1UL)     /*!< AES select CBC mode                     \hideinitializer */
53 #define AES_MODE_CFB            (2UL)     /*!< AES select CFB mode                     \hideinitializer */
54 #define AES_MODE_OFB            (3UL)     /*!< AES select OFB mode                     \hideinitializer */
55 #define AES_MODE_CTR            (4UL)     /*!< AES select CTR mode                     \hideinitializer */
56 #define AES_MODE_CBC_CS1        (0x10UL)  /*!< AES select CBC CS1 mode                 \hideinitializer */
57 #define AES_MODE_CBC_CS2        (0x11UL)  /*!< AES select CBC CS2 mode                 \hideinitializer */
58 #define AES_MODE_CBC_CS3        (0x12UL)  /*!< AES select CBC CS3 mode                 \hideinitializer */
59 #define AES_MODE_GCM            (0x20UL)
60 #define AES_MODE_GHASH          (0x21UL)
61 #define AES_MODE_CCM            (0x22UL)
62 
63 #define SM4_MODE_ECB            (0x200UL)     /*!< SM4 select ECB mode                     \hideinitializer */
64 #define SM4_MODE_CBC            (0x201UL)     /*!< SM4 select CBC mode                     \hideinitializer */
65 #define SM4_MODE_CFB            (0x202UL)     /*!< SM4 select CFB mode                     \hideinitializer */
66 #define SM4_MODE_OFB            (0x203UL)     /*!< SM4 select OFB mode                     \hideinitializer */
67 #define SM4_MODE_CTR            (0x204UL)     /*!< SM4 select CTR mode                     \hideinitializer */
68 #define SM4_MODE_CBC_CS1        (0x210UL)     /*!< SM4 select CBC CS1 mode                 \hideinitializer */
69 #define SM4_MODE_CBC_CS2        (0x211UL)     /*!< SM4 select CBC CS2 mode                 \hideinitializer */
70 #define SM4_MODE_CBC_CS3        (0x212UL)     /*!< SM4 select CBC CS3 mode                 \hideinitializer */
71 #define SM4_MODE_GCM            (0x220UL)
72 #define SM4_MODE_GHASH          (0x221UL)
73 #define SM4_MODE_CCM            (0x222UL)
74 
75 
76 #define AES_NO_SWAP             (0UL)     /*!< AES do not swap input and output data   \hideinitializer */
77 #define AES_OUT_SWAP            (1UL)     /*!< AES swap output data                    \hideinitializer */
78 #define AES_IN_SWAP             (2UL)     /*!< AES swap input data                     \hideinitializer */
79 #define AES_IN_OUT_SWAP         (3UL)     /*!< AES swap both input and output data     \hideinitializer */
80 
81 #define DES_MODE_ECB            (0x000UL) /*!< DES select ECB mode                     \hideinitializer */
82 #define DES_MODE_CBC            (0x100UL) /*!< DES select CBC mode                     \hideinitializer */
83 #define DES_MODE_CFB            (0x200UL) /*!< DES select CFB mode                     \hideinitializer */
84 #define DES_MODE_OFB            (0x300UL) /*!< DES select OFB mode                     \hideinitializer */
85 #define DES_MODE_CTR            (0x400UL) /*!< DES select CTR mode                     \hideinitializer */
86 #define TDES_MODE_ECB           (0x004UL) /*!< TDES select ECB mode                    \hideinitializer */
87 #define TDES_MODE_CBC           (0x104UL) /*!< TDES select CBC mode                    \hideinitializer */
88 #define TDES_MODE_CFB           (0x204UL) /*!< TDES select CFB mode                    \hideinitializer */
89 #define TDES_MODE_OFB           (0x304UL) /*!< TDES select OFB mode                    \hideinitializer */
90 #define TDES_MODE_CTR           (0x404UL) /*!< TDES select CTR mode                    \hideinitializer */
91 
92 #define TDES_NO_SWAP            (0UL)     /*!< TDES do not swap data                       \hideinitializer */
93 #define TDES_WHL_SWAP           (1UL)     /*!< TDES swap high-low word                     \hideinitializer */
94 #define TDES_OUT_SWAP           (2UL)     /*!< TDES swap output data                       \hideinitializer */
95 #define TDES_OUT_WHL_SWAP       (3UL)     /*!< TDES swap output data and high-low word     \hideinitializer */
96 #define TDES_IN_SWAP            (4UL)     /*!< TDES swap input data                        \hideinitializer */
97 #define TDES_IN_WHL_SWAP        (5UL)     /*!< TDES swap input data and high-low word      \hideinitializer */
98 #define TDES_IN_OUT_SWAP        (6UL)     /*!< TDES swap both input and output data        \hideinitializer */
99 #define TDES_IN_OUT_WHL_SWAP    (7UL)     /*!< TDES swap input, output and high-low word   \hideinitializer */
100 
101 #define SHA_MODE_SHA1           (0UL)     /*!< SHA select SHA-1 160-bit                \hideinitializer */
102 #define SHA_MODE_SHA224         (5UL)     /*!< SHA select SHA-224 224-bit              \hideinitializer */
103 #define SHA_MODE_SHA256         (4UL)     /*!< SHA select SHA-256 256-bit              \hideinitializer */
104 #define SHA_MODE_SHA384         (7UL)     /*!< SHA select SHA-384 384-bit              \hideinitializer */
105 #define SHA_MODE_SHA512         (6UL)     /*!< SHA select SHA-512 512-bit              \hideinitializer */
106 
107 #define HMAC_MODE_SHA1          (8UL)     /*!< HMAC select SHA-1 160-bit                \hideinitializer */
108 #define HMAC_MODE_SHA224        (13UL)    /*!< HMAC select SHA-224 224-bit              \hideinitializer */
109 #define HMAC_MODE_SHA256        (12UL)    /*!< HMAC select SHA-256 256-bit              \hideinitializer */
110 #define HMAC_MODE_SHA384        (15UL)    /*!< HMAC select SHA-384 384-bit              \hideinitializer */
111 #define HMAC_MODE_SHA512        (14UL)    /*!< HMAC select SHA-512 512-bit              \hideinitializer */
112 
113 
114 #define SHA_NO_SWAP             (0UL)     /*!< SHA do not swap input and output data   \hideinitializer */
115 #define SHA_OUT_SWAP            (1UL)     /*!< SHA swap output data                    \hideinitializer */
116 #define SHA_IN_SWAP             (2UL)     /*!< SHA swap input data                     \hideinitializer */
117 #define SHA_IN_OUT_SWAP         (3UL)     /*!< SHA swap both input and output data     \hideinitializer */
118 
119 #define CRYPTO_DMA_FIRST        (0x4UL)   /*!< Do first encrypt/decrypt in DMA cascade \hideinitializer */
120 #define CRYPTO_DMA_ONE_SHOT     (0x5UL)   /*!< Do one shot encrypt/decrypt with DMA      \hideinitializer */
121 #define CRYPTO_DMA_CONTINUE     (0x6UL)   /*!< Do continuous encrypt/decrypt in DMA cascade \hideinitializer */
122 #define CRYPTO_DMA_LAST         (0x7UL)   /*!< Do last encrypt/decrypt in DMA cascade          \hideinitializer */
123 
124 //---------------------------------------------------
125 
126 #define RSA_MAX_KLEN            (4096)
127 #define RSA_KBUF_HLEN           (RSA_MAX_KLEN/4 + 8)
128 #define RSA_KBUF_BLEN           (RSA_MAX_KLEN + 32)
129 
130 #define RSA_KEY_SIZE_1024       (0UL)     /*!< RSA select 1024-bit key length           \hideinitializer */
131 #define RSA_KEY_SIZE_2048       (1UL)     /*!< RSA select 2048-bit key length           \hideinitializer */
132 #define RSA_KEY_SIZE_3072       (2UL)     /*!< RSA select 3072-bit key length           \hideinitializer */
133 #define RSA_KEY_SIZE_4096       (3UL)     /*!< RSA select 4096-bit key length           \hideinitializer */
134 
135 #define RSA_MODE_NORMAL         (0x000UL)     /*!< RSA select normal mode                \hideinitializer */
136 #define RSA_MODE_CRT            (0x004UL)     /*!< RSA select CRT mode                   \hideinitializer */
137 #define RSA_MODE_CRTBYPASS      (0x00CUL)     /*!< RSA select CRT bypass mode            \hideinitializer */
138 #define RSA_MODE_SCAP           (0x100UL)     /*!< RSA select SCAP mode                  \hideinitializer */
139 #define RSA_MODE_CRT_SCAP       (0x104UL)     /*!< RSA select CRT SCAP mode              \hideinitializer */
140 #define RSA_MODE_CRTBYPASS_SCAP (0x10CUL)     /*!< RSA select CRT bypass SCAP mode       \hideinitializer */
141 
142 
143 typedef enum
144 {
145     /*!< ECC curve                \hideinitializer */
146     CURVE_P_192,                        /*!< ECC curve P-192          \hideinitializer */
147     CURVE_P_224,                        /*!< ECC curve P-224          \hideinitializer */
148     CURVE_P_256,                        /*!< ECC curve P-256          \hideinitializer */
149     CURVE_P_384,                        /*!< ECC curve P-384          \hideinitializer */
150     CURVE_P_521,                        /*!< ECC curve P-521          \hideinitializer */
151     CURVE_K_163,                        /*!< ECC curve K-163          \hideinitializer */
152     CURVE_K_233,                        /*!< ECC curve K-233          \hideinitializer */
153     CURVE_K_283,                        /*!< ECC curve K-283          \hideinitializer */
154     CURVE_K_409,                        /*!< ECC curve K-409          \hideinitializer */
155     CURVE_K_571,                        /*!< ECC curve K-571          \hideinitializer */
156     CURVE_B_163,                        /*!< ECC curve B-163          \hideinitializer */
157     CURVE_B_233,                        /*!< ECC curve B-233          \hideinitializer */
158     CURVE_B_283,                        /*!< ECC curve B-283          \hideinitializer */
159     CURVE_B_409,                        /*!< ECC curve B-409          \hideinitializer */
160     CURVE_B_571,                        /*!< ECC curve K-571          \hideinitializer */
161     CURVE_KO_192,                       /*!< ECC 192-bits "Koblitz" curve   \hideinitializer */
162     CURVE_KO_224,                       /*!< ECC 224-bits "Koblitz" curve   \hideinitializer */
163     CURVE_KO_256,                       /*!< ECC 256-bits "Koblitz" curve   \hideinitializer */
164     CURVE_BP_256,                       /*!< ECC Brainpool 256-bits curve   \hideinitializer */
165     CURVE_BP_384,                       /*!< ECC Brainpool 256-bits curve   \hideinitializer */
166     CURVE_BP_512,                       /*!< ECC Brainpool 256-bits curve   \hideinitializer */
167     CURVE_25519,                        /*!< ECC curve-25519          \hideinitializer */
168     CURVE_SM2_256,                      /*!< SM2                      \hideinitializer */
169     CURVE_UNDEF = -0x7fffffff,                        /*!< Invalid curve            \hideinitializer */
170 }
171 E_ECC_CURVE;
172 
173 
174 
175 typedef struct e_curve_t
176 {
177     E_ECC_CURVE curve_id;
178     int32_t     Echar;
179     char        Ea[144];
180     char        Eb[144];
181     char        Px[144];
182     char        Py[144];
183     int32_t     Epl;
184     char        Pp[176];
185     int32_t     Eol;
186     char        Eorder[176];
187     int32_t     key_len;
188     int32_t     irreducible_k1;
189     int32_t     irreducible_k2;
190     int32_t     irreducible_k3;
191     int32_t     GF;
192 }  ECC_CURVE;
193 
194 
195 /* RSA working buffer for normal mode */
196 typedef struct
197 {
198     uint32_t au32RsaOutput[128]; /* The RSA answer. */
199     uint32_t au32RsaN[128]; /* The base of modulus operation word. */
200     uint32_t au32RsaM[128]; /* The base of exponentiation words. */
201     uint32_t au32RsaE[128]; /* The exponent of exponentiation words. */
202 } RSA_BUF_NORMAL_T;
203 
204 /* RSA working buffer for CRT ( + CRT bypass) mode */
205 typedef struct
206 {
207     uint32_t au32RsaOutput[128]; /* The RSA answer. */
208     uint32_t au32RsaN[128]; /* The base of modulus operation word. */
209     uint32_t au32RsaM[128]; /* The base of exponentiation words. */
210     uint32_t au32RsaE[128]; /* The exponent of exponentiation words. */
211     uint32_t au32RsaP[128]; /* The Factor of Modulus Operation. */
212     uint32_t au32RsaQ[128]; /* The Factor of Modulus Operation. */
213     uint32_t au32RsaTmpCp[128]; /* The Temporary Value(Cp) of RSA CRT. */
214     uint32_t au32RsaTmpCq[128]; /* The Temporary Value(Cq) of RSA CRT. */
215     uint32_t au32RsaTmpDp[128]; /* The Temporary Value(Dp) of RSA CRT. */
216     uint32_t au32RsaTmpDq[128]; /* The Temporary Value(Dq) of RSA CRT. */
217     uint32_t au32RsaTmpRp[128]; /* The Temporary Value(Rp) of RSA CRT. */
218     uint32_t au32RsaTmpRq[128]; /* The Temporary Value(Rq) of RSA CRT. */
219 } RSA_BUF_CRT_T;
220 
221 /* RSA working buffer for SCAP mode */
222 typedef struct
223 {
224     uint32_t au32RsaOutput[128]; /* The RSA answer. */
225     uint32_t au32RsaN[128]; /* The base of modulus operation word. */
226     uint32_t au32RsaM[128]; /* The base of exponentiation words. */
227     uint32_t au32RsaE[128]; /* The exponent of exponentiation words. */
228     uint32_t au32RsaP[128]; /* The Factor of Modulus Operation. */
229     uint32_t au32RsaQ[128]; /* The Factor of Modulus Operation. */
230     uint32_t au32RsaTmpBlindKey[128]; /* The Temporary Value(blind key) of RSA SCAP. */
231 } RSA_BUF_SCAP_T;
232 
233 /* RSA working buffer for CRT ( + CRT bypass ) + SCAP mode */
234 typedef struct
235 {
236     uint32_t au32RsaOutput[128]; /* The RSA answer. */
237     uint32_t au32RsaN[128]; /* The base of modulus operation word. */
238     uint32_t au32RsaM[128]; /* The base of exponentiation words. */
239     uint32_t au32RsaE[128]; /* The exponent of exponentiation words. */
240     uint32_t au32RsaP[128]; /* The Factor of Modulus Operation. */
241     uint32_t au32RsaQ[128]; /* The Factor of Modulus Operation. */
242     uint32_t au32RsaTmpCp[128]; /* The Temporary Value(Cp) of RSA CRT. */
243     uint32_t au32RsaTmpCq[128]; /* The Temporary Value(Cq) of RSA CRT. */
244     uint32_t au32RsaTmpDp[128]; /* The Temporary Value(Dp) of RSA CRT. */
245     uint32_t au32RsaTmpDq[128]; /* The Temporary Value(Dq) of RSA CRT. */
246     uint32_t au32RsaTmpRp[128]; /* The Temporary Value(Rp) of RSA CRT. */
247     uint32_t au32RsaTmpRq[128]; /* The Temporary Value(Rq) of RSA CRT. */
248     uint32_t au32RsaTmpBlindKey[128]; /* The Temporary Value(blind key) of RSA SCAP. */
249 } RSA_BUF_CRT_SCAP_T;
250 
251 /* RSA working buffer for using key store */
252 typedef struct
253 {
254     uint32_t au32RsaOutput[128]; /* The RSA answer. */
255     uint32_t au32RsaN[128]; /* The base of modulus operation word. */
256     uint32_t au32RsaM[128]; /* The base of exponentiation words. */
257 } RSA_BUF_KS_T;
258 
259 /**@}*/ /* end of group CRYPTO_EXPORTED_CONSTANTS */
260 
261 
262 /** @addtogroup CRYPTO_EXPORTED_MACROS CRYPTO Exported Macros
263   @{
264 */
265 
266 /*----------------------------------------------------------------------------------------------*/
267 /*  Macros                                                                                      */
268 /*----------------------------------------------------------------------------------------------*/
269 
270 /**
271   * @brief This macro enables PRNG interrupt.
272   * @param crpt     Specified crypto module
273   * @return None
274   * \hideinitializer
275   */
276 #define PRNG_ENABLE_INT(crpt)       ((crpt)->INTEN |= CRPT_INTEN_PRNGIEN_Msk)
277 
278 /**
279   * @brief This macro disables PRNG interrupt.
280   * @param crpt     Specified crypto module
281   * @return None
282   * \hideinitializer
283   */
284 #define PRNG_DISABLE_INT(crpt)      ((crpt)->INTEN &= ~CRPT_INTEN_PRNGIEN_Msk)
285 
286 /**
287   * @brief This macro gets PRNG interrupt flag.
288   * @param crpt     Specified crypto module
289   * @return PRNG interrupt flag.
290   * \hideinitializer
291   */
292 #define PRNG_GET_INT_FLAG(crpt)     ((crpt)->INTSTS & CRPT_INTSTS_PRNGIF_Msk)
293 
294 /**
295   * @brief This macro clears PRNG interrupt flag.
296   * @param crpt     Specified crypto module
297   * @return None
298   * \hideinitializer
299   */
300 #define PRNG_CLR_INT_FLAG(crpt)     ((crpt)->INTSTS = CRPT_INTSTS_PRNGIF_Msk)
301 
302 /**
303   * @brief This macro enables AES interrupt.
304   * @param crpt     Specified crypto module
305   * @return None
306   * \hideinitializer
307   */
308 #define AES_ENABLE_INT(crpt)        ((crpt)->INTEN |= (CRPT_INTEN_AESIEN_Msk|CRPT_INTEN_AESEIEN_Msk))
309 
310 /**
311   * @brief This macro disables AES interrupt.
312   * @param crpt     Specified crypto module
313   * @return None
314   * \hideinitializer
315   */
316 #define AES_DISABLE_INT(crpt)       ((crpt)->INTEN &= ~(CRPT_INTEN_AESIEN_Msk|CRPT_INTEN_AESEIEN_Msk))
317 
318 /**
319   * @brief This macro gets AES interrupt flag.
320   * @param crpt     Specified crypto module
321   * @return AES interrupt flag.
322   * \hideinitializer
323   */
324 #define AES_GET_INT_FLAG(crpt)      ((crpt)->INTSTS & (CRPT_INTSTS_AESIF_Msk|CRPT_INTSTS_AESEIF_Msk))
325 
326 /**
327   * @brief This macro clears AES interrupt flag.
328   * @param crpt     Specified crypto module
329   * @return None
330   * \hideinitializer
331   */
332 #define AES_CLR_INT_FLAG(crpt)      ((crpt)->INTSTS = (CRPT_INTSTS_AESIF_Msk|CRPT_INTSTS_AESEIF_Msk))
333 
334 /**
335   * @brief This macro enables AES key protection.
336   * @param crpt     Specified crypto module
337   * @return None
338   * \hideinitializer
339   */
340 #define AES_ENABLE_KEY_PROTECT(crpt)  ((crpt)->AES_CTL |= CRPT_AES_CTL_KEYPRT_Msk)
341 
342 /**
343   * @brief This macro disables AES key protection.
344   * @param crpt     Specified crypto module
345   * @return None
346   * \hideinitializer
347   */
348 #define AES_DISABLE_KEY_PROTECT(crpt) ((crpt)->AES_CTL = ((crpt)->AES_CTL & ~CRPT_AES_CTL_KEYPRT_Msk) | (0x16UL<<CRPT_AES_CTL_KEYUNPRT_Pos)); \
349                                       ((crpt)->AES_CTL &= ~CRPT_AES_CTL_KEYPRT_Msk)
350 
351 /**
352   * @brief This macro enables TDES interrupt.
353   * @param crpt     Specified crypto module
354   * @return None
355   * \hideinitializer
356   */
357 #define TDES_ENABLE_INT(crpt)       ((crpt)->INTEN |= (CRPT_INTEN_TDESIEN_Msk|CRPT_INTEN_TDESEIEN_Msk))
358 
359 /**
360   * @brief This macro disables TDES interrupt.
361   * @param crpt     Specified crypto module
362   * @return None
363   * \hideinitializer
364   */
365 #define TDES_DISABLE_INT(crpt)      ((crpt)->INTEN &= ~(CRPT_INTEN_TDESIEN_Msk|CRPT_INTEN_TDESEIEN_Msk))
366 
367 /**
368   * @brief This macro gets TDES interrupt flag.
369   * @param crpt     Specified crypto module
370   * @return TDES interrupt flag.
371   * \hideinitializer
372   */
373 #define TDES_GET_INT_FLAG(crpt)     ((crpt)->INTSTS & (CRPT_INTSTS_TDESIF_Msk|CRPT_INTSTS_TDESEIF_Msk))
374 
375 /**
376   * @brief This macro clears TDES interrupt flag.
377   * @param crpt     Specified crypto module
378   * @return None
379   * \hideinitializer
380   */
381 #define TDES_CLR_INT_FLAG(crpt)     ((crpt)->INTSTS = (CRPT_INTSTS_TDESIF_Msk|CRPT_INTSTS_TDESEIF_Msk))
382 
383 /**
384   * @brief This macro enables TDES key protection.
385   * @param crpt     Specified crypto module
386   * @return None
387   * \hideinitializer
388   */
389 #define TDES_ENABLE_KEY_PROTECT(crpt)  ((crpt)->TDES_CTL |= CRPT_TDES_CTL_KEYPRT_Msk)
390 
391 /**
392   * @brief This macro disables TDES key protection.
393   * @param crpt     Specified crypto module
394   * @return None
395   * \hideinitializer
396   */
397 #define TDES_DISABLE_KEY_PROTECT(crpt) ((crpt)->TDES_CTL = ((crpt)->TDES_CTL & ~CRPT_TDES_CTL_KEYPRT_Msk) | (0x16UL<<CRPT_TDES_CTL_KEYUNPRT_Pos)); \
398                                        ((crpt)->TDES_CTL &= ~CRPT_TDES_CTL_KEYPRT_Msk)
399 
400 /**
401   * @brief This macro enables SHA interrupt.
402   * @param crpt     Specified crypto module
403   * @return None
404   * \hideinitializer
405   */
406 #define SHA_ENABLE_INT(crpt)        ((crpt)->INTEN |= (CRPT_INTEN_HMACIEN_Msk|CRPT_INTEN_HMACEIEN_Msk))
407 
408 /**
409   * @brief This macro disables SHA interrupt.
410   * @param crpt     Specified crypto module
411   * @return None
412   * \hideinitializer
413   */
414 #define SHA_DISABLE_INT(crpt)       ((crpt)->INTEN &= ~(CRPT_INTEN_HMACIEN_Msk|CRPT_INTEN_HMACEIEN_Msk))
415 
416 /**
417   * @brief This macro gets SHA interrupt flag.
418   * @param crpt     Specified crypto module
419   * @return SHA interrupt flag.
420   * \hideinitializer
421   */
422 #define SHA_GET_INT_FLAG(crpt)      ((crpt)->INTSTS & (CRPT_INTSTS_HMACIF_Msk|CRPT_INTSTS_HMACEIF_Msk))
423 
424 /**
425   * @brief This macro clears SHA interrupt flag.
426   * @param crpt     Specified crypto module
427   * @return None
428   * \hideinitializer
429   */
430 #define SHA_CLR_INT_FLAG(crpt)      ((crpt)->INTSTS = (CRPT_INTSTS_HMACIF_Msk|CRPT_INTSTS_HMACEIF_Msk))
431 
432 /**
433   * @brief This macro enables ECC interrupt.
434   * @param crpt     Specified crypto module
435   * @return None
436   * \hideinitializer
437   */
438 #define ECC_ENABLE_INT(crpt)        ((crpt)->INTEN |= (CRPT_INTEN_ECCIEN_Msk|CRPT_INTEN_ECCEIEN_Msk))
439 
440 /**
441   * @brief This macro disables ECC interrupt.
442   * @param crpt     Specified crypto module
443   * @return None
444   * \hideinitializer
445   */
446 #define ECC_DISABLE_INT(crpt)       ((crpt)->INTEN &= ~(CRPT_INTEN_ECCIEN_Msk|CRPT_INTEN_ECCEIEN_Msk))
447 
448 /**
449   * @brief This macro gets ECC interrupt flag.
450   * @param crpt     Specified crypto module
451   * @return ECC interrupt flag.
452   * \hideinitializer
453   */
454 #define ECC_GET_INT_FLAG(crpt)      ((crpt)->INTSTS & (CRPT_INTSTS_ECCIF_Msk|CRPT_INTSTS_ECCEIF_Msk))
455 
456 /**
457   * @brief This macro clears ECC interrupt flag.
458   * @param crpt     Specified crypto module
459   * @return None
460   * \hideinitializer
461   */
462 #define ECC_CLR_INT_FLAG(crpt)      ((crpt)->INTSTS = (CRPT_INTSTS_ECCIF_Msk|CRPT_INTSTS_ECCEIF_Msk))
463 
464 /**
465   * @brief This macro enables RSA interrupt.
466   * @param crpt     Specified crypto module
467   * @return None
468   * \hideinitializer
469   */
470 #define RSA_ENABLE_INT(crpt)        ((crpt)->INTEN |= (CRPT_INTEN_RSAIEN_Msk|CRPT_INTEN_RSAEIEN_Msk))
471 
472 /**
473   * @brief This macro disables RSA interrupt.
474   * @param crpt     Specified crypto module
475   * @return None
476   * \hideinitializer
477   */
478 #define RSA_DISABLE_INT(crpt)       ((crpt)->INTEN &= ~(CRPT_INTEN_RSAIEN_Msk|CRPT_INTEN_RSAEIEN_Msk))
479 
480 /**
481   * @brief This macro gets RSA interrupt flag.
482   * @param crpt     Specified crypto module
483   * @return ECC interrupt flag.
484   * \hideinitializer
485   */
486 #define RSA_GET_INT_FLAG(crpt)      ((crpt)->INTSTS & (CRPT_INTSTS_RSAIF_Msk|CRPT_INTSTS_RSAEIF_Msk))
487 
488 /**
489   * @brief This macro clears RSA interrupt flag.
490   * @param crpt     Specified crypto module
491   * @return None
492   * \hideinitializer
493   */
494 #define RSA_CLR_INT_FLAG(crpt)      ((crpt)->INTSTS = (CRPT_INTSTS_RSAIF_Msk|CRPT_INTSTS_RSAEIF_Msk))
495 
496 
497 /**@}*/ /* end of group CRYPTO_EXPORTED_MACROS */
498 
499 
500 
501 /** @addtogroup CRYPTO_EXPORTED_FUNCTIONS CRYPTO Exported Functions
502   @{
503 */
504 
505 /*---------------------------------------------------------------------------------------------------------*/
506 /*  Functions                                                                                      */
507 /*---------------------------------------------------------------------------------------------------------*/
508 
509 void PRNG_Open(CRPT_T *crpt, uint32_t u32KeySize, uint32_t u32SeedReload, uint32_t u32Seed);
510 void PRNG_Start(CRPT_T *crpt);
511 void PRNG_Read(CRPT_T *crpt, uint32_t u32RandKey[]);
512 void AES_Open(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32EncDec, uint32_t u32OpMode, uint32_t u32KeySize, uint32_t u32SwapType);
513 void AES_Start(CRPT_T *crpt, int32_t u32Channel, uint32_t u32DMAMode);
514 void AES_SetKey(CRPT_T *crpt, uint32_t u32Channel, uint32_t au32Keys[], uint32_t u32KeySize);
515 void AES_SetKey_KS(CRPT_T *crpt, KS_MEM_Type mem, int32_t i32KeyIdx);
516 void AES_SetInitVect(CRPT_T *crpt, uint32_t u32Channel, uint32_t au32IV[]);
517 void AES_SetDMATransfer(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt);
518 void SHA_Open(CRPT_T *crpt, uint32_t u32OpMode, uint32_t u32SwapType, uint32_t hmac_key_len);
519 void SHA_Start(CRPT_T *crpt, uint32_t u32DMAMode);
520 void SHA_SetDMATransfer(CRPT_T *crpt, uint32_t u32SrcAddr, uint32_t u32TransCnt);
521 void SHA_Read(CRPT_T *crpt, uint32_t u32Digest[]);
522 void ECC_DriverISR(CRPT_T *crpt);
523 int  ECC_IsPrivateKeyValid(CRPT_T *crpt, E_ECC_CURVE ecc_curve,  char private_k[]);
524 int32_t  ECC_GenerateSecretZ(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *private_k, char public_k1[], char public_k2[], char secret_z[]);
525 int32_t  ECC_GeneratePublicKey(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *private_k, char public_k1[], char public_k2[]);
526 int32_t  ECC_GenerateSignature(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *message, char *d, char *k, char *R, char *S);
527 int32_t  ECC_VerifySignature(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *message, char *public_k1, char *public_k2, char *R, char *S);
528 
529 
530 int32_t RSA_Open(CRPT_T *crpt, uint32_t u32OpMode, uint32_t u32KeySize, void *psRSA_Buf, uint32_t u32BufSize, uint32_t u32UseKS);
531 int32_t RSA_SetKey(CRPT_T *crpt, char *Key);
532 int32_t RSA_SetDMATransfer(CRPT_T *crpt, char *Src, char *n, char *P, char *Q);
533 void RSA_Start(CRPT_T *crpt);
534 int32_t RSA_Read(CRPT_T *crpt, char * Output);
535 int32_t RSA_SetKey_KS(CRPT_T *crpt, uint32_t u32KeyNum, uint32_t u32KSMemType, uint32_t u32BlindKeyNum);
536 int32_t RSA_SetDMATransfer_KS(CRPT_T *crpt, char *Src, char *n, uint32_t u32PNum,
537                               uint32_t u32QNum, uint32_t u32CpNum, uint32_t u32CqNum, uint32_t u32DpNum,
538                               uint32_t u32DqNum, uint32_t u32RpNum, uint32_t u32RqNum);
539 int32_t  ECC_GeneratePublicKey_KS(CRPT_T *crpt, E_ECC_CURVE ecc_curve, KS_MEM_Type mem, int32_t i32KeyIdx, char public_k1[], char public_k2[], uint32_t u32ExtraOp);
540 int32_t  ECC_GenerateSignature_KS(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *message, KS_MEM_Type mem_d, int32_t i32KeyIdx_d, KS_MEM_Type mem_k, int32_t i32KeyIdx_k, char *R, char *S);
541 int32_t  ECC_VerifySignature_KS(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *message, KS_MEM_Type mem_pk1, int32_t i32KeyIdx_pk1, KS_MEM_Type mem_pk2, int32_t i32KeyIdx_pk2, char *R, char *S);
542 int32_t  ECC_GenerateSecretZ_KS(CRPT_T *crpt, E_ECC_CURVE ecc_curve, KS_MEM_Type mem, int32_t i32KeyIdx, char public_k1[], char public_k2[]);
543 
544 void CRPT_Reg2Hex(int32_t count, uint32_t volatile reg[], char output[]);
545 void CRPT_Hex2Reg(char input[], uint32_t volatile reg[]);
546 int32_t ECC_GetCurve(CRPT_T *crpt, E_ECC_CURVE ecc_curve, ECC_CURVE *curve);
547 
548 /**@}*/ /* end of group CRYPTO_EXPORTED_FUNCTIONS */
549 
550 /**@}*/ /* end of group CRYPTO_Driver */
551 
552 /**@}*/ /* end of group Standard_Driver */
553 
554 #ifdef __cplusplus
555 }
556 #endif
557 
558 #endif  /* __CRYPTO_H__ */
559