| M480 BSP V3.05.001 The Board Support Package for M480 Series

crypto.h
Go to the documentation of this file.
1 /**************************************************************************/
9 #ifndef __CRYPTO_H__
10 #define __CRYPTO_H__
11
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16
30 #define PRNG_KEY_SIZE_64 0UL
31 #define PRNG_KEY_SIZE_128 1UL
32 #define PRNG_KEY_SIZE_192 2UL
33 #define PRNG_KEY_SIZE_256 3UL
35 #define PRNG_SEED_CONT 0UL
36 #define PRNG_SEED_RELOAD 1UL
38 #define AES_KEY_SIZE_128 0UL
39 #define AES_KEY_SIZE_192 1UL
40 #define AES_KEY_SIZE_256 2UL
42 #define AES_MODE_ECB 0UL
43 #define AES_MODE_CBC 1UL
44 #define AES_MODE_CFB 2UL
45 #define AES_MODE_OFB 3UL
46 #define AES_MODE_CTR 4UL
47 #define AES_MODE_CBC_CS1 0x10UL
48 #define AES_MODE_CBC_CS2 0x11UL
49 #define AES_MODE_CBC_CS3 0x12UL
51 #define AES_NO_SWAP 0UL
52 #define AES_OUT_SWAP 1UL
53 #define AES_IN_SWAP 2UL
54 #define AES_IN_OUT_SWAP 3UL
56 #define DES_MODE_ECB 0x000UL
57 #define DES_MODE_CBC 0x100UL
58 #define DES_MODE_CFB 0x200UL
59 #define DES_MODE_OFB 0x300UL
60 #define DES_MODE_CTR 0x400UL
61 #define TDES_MODE_ECB 0x004UL
62 #define TDES_MODE_CBC 0x104UL
63 #define TDES_MODE_CFB 0x204UL
64 #define TDES_MODE_OFB 0x304UL
65 #define TDES_MODE_CTR 0x404UL
67 #define TDES_NO_SWAP 0UL
68 #define TDES_WHL_SWAP 1UL
69 #define TDES_OUT_SWAP 2UL
70 #define TDES_OUT_WHL_SWAP 3UL
71 #define TDES_IN_SWAP 4UL
72 #define TDES_IN_WHL_SWAP 5UL
73 #define TDES_IN_OUT_SWAP 6UL
74 #define TDES_IN_OUT_WHL_SWAP 7UL
76 #define SHA_MODE_SHA1 0UL
77 #define SHA_MODE_SHA224 5UL
78 #define SHA_MODE_SHA256 4UL
79 #define SHA_MODE_SHA384 7UL
80 #define SHA_MODE_SHA512 6UL
82 #define SHA_NO_SWAP 0UL
83 #define SHA_OUT_SWAP 1UL
84 #define SHA_IN_SWAP 2UL
85 #define SHA_IN_OUT_SWAP 3UL
87 #define CRYPTO_DMA_FIRST 0x4UL
88 #define CRYPTO_DMA_ONE_SHOT 0x5UL
89 #define CRYPTO_DMA_CONTINUE 0x6UL
90 #define CRYPTO_DMA_LAST 0x7UL
92 typedef enum
93 {
95 CURVE_P_192,
96 CURVE_P_224,
97 CURVE_P_256,
98 CURVE_P_384,
99 CURVE_P_521,
100 CURVE_K_163,
101 CURVE_K_233,
102 CURVE_K_283,
103 CURVE_K_409,
104 CURVE_K_571,
105 CURVE_B_163,
106 CURVE_B_233,
107 CURVE_B_283,
108 CURVE_B_409,
109 CURVE_B_571,
110 CURVE_KO_192,
111 CURVE_KO_224,
112 CURVE_KO_256,
113 CURVE_BP_256,
114 CURVE_BP_384,
115 CURVE_BP_512,
116 CURVE_UNDEF,
117 }
118 E_ECC_CURVE;
121 /* end of group CRYPTO_EXPORTED_CONSTANTS */
122
123
128 /*----------------------------------------------------------------------------------------------*/
129 /* Macros */
130 /*----------------------------------------------------------------------------------------------*/
131
138 #define PRNG_ENABLE_INT(crpt) ((crpt)->INTEN |= CRPT_INTEN_PRNGIEN_Msk)
139
146 #define PRNG_DISABLE_INT(crpt) ((crpt)->INTEN &= ~CRPT_INTEN_PRNGIEN_Msk)
147
154 #define PRNG_GET_INT_FLAG(crpt) ((crpt)->INTSTS & CRPT_INTSTS_PRNGIF_Msk)
155
162 #define PRNG_CLR_INT_FLAG(crpt) ((crpt)->INTSTS = CRPT_INTSTS_PRNGIF_Msk)
163
170 #define AES_ENABLE_INT(crpt) ((crpt)->INTEN |= (CRPT_INTEN_AESIEN_Msk|CRPT_INTEN_AESEIEN_Msk))
171
178 #define AES_DISABLE_INT(crpt) ((crpt)->INTEN &= ~(CRPT_INTEN_AESIEN_Msk|CRPT_INTEN_AESEIEN_Msk))
179
186 #define AES_GET_INT_FLAG(crpt) ((crpt)->INTSTS & (CRPT_INTSTS_AESIF_Msk|CRPT_INTSTS_AESEIF_Msk))
187
194 #define AES_CLR_INT_FLAG(crpt) ((crpt)->INTSTS = (CRPT_INTSTS_AESIF_Msk|CRPT_INTSTS_AESEIF_Msk))
195
202 #define AES_ENABLE_KEY_PROTECT(crpt) ((crpt)->AES_CTL |= CRPT_AES_CTL_KEYPRT_Msk)
203
210 #define AES_DISABLE_KEY_PROTECT(crpt) ((crpt)->AES_CTL = ((crpt)->AES_CTL & ~CRPT_AES_CTL_KEYPRT_Msk) | (0x16UL<<CRPT_AES_CTL_KEYUNPRT_Pos)); \
211 ((crpt)->AES_CTL &= ~CRPT_AES_CTL_KEYPRT_Msk)
212
219 #define TDES_ENABLE_INT(crpt) ((crpt)->INTEN |= (CRPT_INTEN_TDESIEN_Msk|CRPT_INTEN_TDESEIEN_Msk))
220
227 #define TDES_DISABLE_INT(crpt) ((crpt)->INTEN &= ~(CRPT_INTEN_TDESIEN_Msk|CRPT_INTEN_TDESEIEN_Msk))
228
235 #define TDES_GET_INT_FLAG(crpt) ((crpt)->INTSTS & (CRPT_INTSTS_TDESIF_Msk|CRPT_INTSTS_TDESEIF_Msk))
236
243 #define TDES_CLR_INT_FLAG(crpt) ((crpt)->INTSTS = (CRPT_INTSTS_TDESIF_Msk|CRPT_INTSTS_TDESEIF_Msk))
244
251 #define TDES_ENABLE_KEY_PROTECT(crpt) ((crpt)->TDES_CTL |= CRPT_TDES_CTL_KEYPRT_Msk)
252
259 #define TDES_DISABLE_KEY_PROTECT(crpt) ((crpt)->TDES_CTL = ((crpt)->TDES_CTL & ~CRPT_TDES_CTL_KEYPRT_Msk) | (0x16UL<<CRPT_TDES_CTL_KEYUNPRT_Pos)); \
260 ((crpt)->TDES_CTL &= ~CRPT_TDES_CTL_KEYPRT_Msk)
261
268 #define SHA_ENABLE_INT(crpt) ((crpt)->INTEN |= (CRPT_INTEN_HMACIEN_Msk|CRPT_INTEN_HMACEIEN_Msk))
269
276 #define SHA_DISABLE_INT(crpt) ((crpt)->INTEN &= ~(CRPT_INTEN_HMACIEN_Msk|CRPT_INTEN_HMACEIEN_Msk))
277
284 #define SHA_GET_INT_FLAG(crpt) ((crpt)->INTSTS & (CRPT_INTSTS_HMACIF_Msk|CRPT_INTSTS_HMACEIF_Msk))
285
292 #define SHA_CLR_INT_FLAG(crpt) ((crpt)->INTSTS = (CRPT_INTSTS_HMACIF_Msk|CRPT_INTSTS_HMACEIF_Msk))
293
300 #define ECC_ENABLE_INT(crpt) ((crpt)->INTEN |= (CRPT_INTEN_ECCIEN_Msk|CRPT_INTEN_ECCEIEN_Msk))
301
308 #define ECC_DISABLE_INT(crpt) ((crpt)->INTEN &= ~(CRPT_INTEN_ECCIEN_Msk|CRPT_INTEN_ECCEIEN_Msk))
309
316 #define ECC_GET_INT_FLAG(crpt) ((crpt)->INTSTS & (CRPT_INTSTS_ECCIF_Msk|CRPT_INTSTS_ECCEIF_Msk))
317
324 #define ECC_CLR_INT_FLAG(crpt) ((crpt)->INTSTS = (CRPT_INTSTS_ECCIF_Msk|CRPT_INTSTS_ECCEIF_Msk))
325
326 /* end of group M480_CRYPTO_EXPORTED_MACROS */
328
329
335 /*---------------------------------------------------------------------------------------------------------*/
336 /* Functions */
337 /*---------------------------------------------------------------------------------------------------------*/
338
339 void PRNG_Open(CRPT_T *crpt, uint32_t u32KeySize, uint32_t u32SeedReload, uint32_t u32Seed);
340 void PRNG_Start(CRPT_T *crpt);
341 void PRNG_Read(CRPT_T *crpt, uint32_t u32RandKey[]);
342 void AES_Open(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32EncDec, uint32_t u32OpMode, uint32_t u32KeySize, uint32_t u32SwapType);
343 void AES_Start(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32DMAMode);
344 void AES_SetKey(CRPT_T *crpt, uint32_t u32Channel, uint32_t au32Keys[], uint32_t u32KeySize);
345 void AES_SetInitVect(CRPT_T *crpt, uint32_t u32Channel, uint32_t au32IV[]);
346 void AES_SetDMATransfer(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt);
347 void TDES_Open(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32EncDec, int32_t Is3DES, int32_t Is3Key, uint32_t u32OpMode, uint32_t u32SwapType);
348 void TDES_Start(CRPT_T *crpt, int32_t u32Channel, uint32_t u32DMAMode);
349 void TDES_SetKey(CRPT_T *crpt, uint32_t u32Channel, uint32_t au32Keys[3][2]);
350 void TDES_SetInitVect(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32IVH, uint32_t u32IVL);
351 void TDES_SetDMATransfer(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt);
352 void SHA_Open(CRPT_T *crpt, uint32_t u32OpMode, uint32_t u32SwapType, uint32_t hmac_key_len);
353 void SHA_Start(CRPT_T *crpt, uint32_t u32DMAMode);
354 void SHA_SetDMATransfer(CRPT_T *crpt, uint32_t u32SrcAddr, uint32_t u32TransCnt);
355 void SHA_Read(CRPT_T *crpt, uint32_t u32Digest[]);
356 void ECC_Complete(CRPT_T *crpt);
357 int ECC_IsPrivateKeyValid(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char private_k[]);
358 int32_t ECC_GeneratePublicKey(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *private_k, char public_k1[], char public_k2[]);
359 int32_t ECC_Mutiply(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char x1[], char y1[], char *k, char x2[], char y2[]);
360 int32_t ECC_GenerateSecretZ(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *private_k, char public_k1[], char public_k2[], char secret_z[]);
361 int32_t ECC_GenerateSignature(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *message, char *d, char *k, char *R, char *S);
362 int32_t ECC_VerifySignature(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *message, char *public_k1, char *public_k2, char *R, char *S);
363
364 /* end of group CRYPTO_EXPORTED_FUNCTIONS */
366 /* end of group CRYPTO_Driver */
368 /* end of group Standard_Driver */
370
371 #ifdef __cplusplus
372 }
373 #endif
374
375 #endif /* __CRYPTO_H__ */
376
377 /*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/
378
CRPT_T
Definition: crypto_reg.h:26
AES_SetDMATransfer
void AES_SetDMATransfer(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt)
Set AES DMA transfer configuration.
Definition: crypto.c:219
CURVE_K_571
Definition: crypto.h:104
CURVE_BP_512
Definition: crypto.h:115
CURVE_KO_256
Definition: crypto.h:112
SHA_Open
void SHA_Open(CRPT_T *crpt, uint32_t u32OpMode, uint32_t u32SwapType, uint32_t hmac_key_len)
Open SHA encrypt function.
Definition: crypto.c:372
ECC_GenerateSecretZ
int32_t ECC_GenerateSecretZ(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *private_k, char public_k1[], char public_k2[], char secret_z[])
Given a curve parameter, the other party's public key, and one's own private key to generate the secr...
Definition: crypto.c:1325
CURVE_P_224
Definition: crypto.h:96
CURVE_K_283
Definition: crypto.h:102
PRNG_Read
void PRNG_Read(CRPT_T *crpt, uint32_t u32RandKey[])
Read the PRNG key.
Definition: crypto.c:98
AES_Open
void AES_Open(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32EncDec, uint32_t u32OpMode, uint32_t u32KeySize, uint32_t u32SwapType)
Open AES encrypt/decrypt function.
Definition: crypto.c:138
SHA_Start
void SHA_Start(CRPT_T *crpt, uint32_t u32DMAMode)
Start SHA encrypt.
Definition: crypto.c:397
CURVE_B_233
Definition: crypto.h:106
AES_SetInitVect
void AES_SetInitVect(CRPT_T *crpt, uint32_t u32Channel, uint32_t au32IV[])
Set AES initial vectors.
Definition: crypto.c:197
CURVE_K_409
Definition: crypto.h:103
CURVE_BP_384
Definition: crypto.h:114
E_ECC_CURVE
E_ECC_CURVE
Definition: crypto.h:92
CURVE_KO_224
Definition: crypto.h:111
CURVE_B_571
Definition: crypto.h:109
CURVE_K_233
Definition: crypto.h:101
SHA_SetDMATransfer
void SHA_SetDMATransfer(CRPT_T *crpt, uint32_t u32SrcAddr, uint32_t u32TransCnt)
Set SHA DMA transfer.
Definition: crypto.c:410
ECC_VerifySignature
int32_t ECC_VerifySignature(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *message, char *public_k1, char *public_k2, char *R, char *S)
ECDSA dogotal signature verification.
Definition: crypto.c:1665
AES_Start
void AES_Start(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32DMAMode)
Start AES encrypt/decrypt.
Definition: crypto.c:159
CURVE_B_409
Definition: crypto.h:108
CURVE_P_256
Definition: crypto.h:97
ECC_IsPrivateKeyValid
int ECC_IsPrivateKeyValid(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char private_k[])
Check if the private key is located in valid range of curve.
Definition: crypto.c:1165
PRNG_Open
void PRNG_Open(CRPT_T *crpt, uint32_t u32KeySize, uint32_t u32SeedReload, uint32_t u32Seed)
Open PRNG function.
Definition: crypto.c:71
TDES_SetInitVect
void TDES_SetInitVect(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32IVH, uint32_t u32IVL)
Set TDES initial vectors.
Definition: crypto.c:320
SHA_Read
void SHA_Read(CRPT_T *crpt, uint32_t u32Digest[])
Read the SHA digest.
Definition: crypto.c:422
ECC_GeneratePublicKey
int32_t ECC_GeneratePublicKey(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *private_k, char public_k1[], char public_k2[])
Given a private key and curve to generate the public key pair.
Definition: crypto.c:1212
ECC_Complete
void ECC_Complete(CRPT_T *crpt)
ECC interrupt service routine. User application must invoke this function in his CRYPTO_IRQHandler() ...
Definition: crypto.c:1139
TDES_Start
void TDES_Start(CRPT_T *crpt, int32_t u32Channel, uint32_t u32DMAMode)
Start TDES encrypt/decrypt.
Definition: crypto.c:284
TDES_Open
void TDES_Open(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32EncDec, int32_t Is3DES, int32_t Is3Key, uint32_t u32OpMode, uint32_t u32SwapType)
Open TDES encrypt/decrypt function.
Definition: crypto.c:258
ECC_GenerateSignature
int32_t ECC_GenerateSignature(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char *message, char *d, char *k, char *R, char *S)
ECDSA digital signature generation.
Definition: crypto.c:1431
PRNG_Start
void PRNG_Start(CRPT_T *crpt)
Start to generate one PRNG key.
Definition: crypto.c:87
AES_SetKey
void AES_SetKey(CRPT_T *crpt, uint32_t u32Channel, uint32_t au32Keys[], uint32_t u32KeySize)
Set AES keys.
Definition: crypto.c:176
CURVE_UNDEF
Definition: crypto.h:116
CURVE_B_163
Definition: crypto.h:105
CURVE_BP_256
Definition: crypto.h:113
ECC_Mutiply
int32_t ECC_Mutiply(CRPT_T *crpt, E_ECC_CURVE ecc_curve, char x1[], char y1[], char *k, char x2[], char y2[])
Given a private key and curve to generate the public key pair.
Definition: crypto.c:1267
TDES_SetDMATransfer
void TDES_SetDMATransfer(CRPT_T *crpt, uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt)
Set TDES DMA transfer configuration.
Definition: crypto.c:340
CURVE_P_521
Definition: crypto.h:99
CURVE_KO_192
Definition: crypto.h:110
CURVE_P_384
Definition: crypto.h:98
CURVE_K_163
Definition: crypto.h:100
TDES_SetKey
void TDES_SetKey(CRPT_T *crpt, uint32_t u32Channel, uint32_t au32Keys[3][2])
Set TDES keys.
Definition: crypto.c:297
CURVE_B_283
Definition: crypto.h:107
CURVE_P_192
Definition: crypto.h:95