nu_crypto.h 27 KB

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