nu_crypto.h 27 KB

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