crypto.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**************************************************************************//**
  2. * @file crypto.h
  3. * @version V1.10
  4. * $Revision: 14 $
  5. * $Date: 14/10/06 1:49p $
  6. * @brief Cryptographic Accelerator driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #ifndef __CRYPTO_H__
  12. #define __CRYPTO_H__
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver
  18. @{
  19. */
  20. /** @addtogroup NUC472_442_CRYPTO_Driver CRYPTO Driver
  21. @{
  22. */
  23. /** @addtogroup NUC472_442_CRYPTO_EXPORTED_CONSTANTS CRYPTO Exported Constants
  24. @{
  25. */
  26. #define PRNG_KEY_SIZE_64 0 /*!< Select to generate 64-bit random key \hideinitializer */
  27. #define PRNG_KEY_SIZE_128 1 /*!< Select to generate 128-bit random key \hideinitializer */
  28. #define PRNG_KEY_SIZE_192 2 /*!< Select to generate 192-bit random key \hideinitializer */
  29. #define PRNG_KEY_SIZE_256 3 /*!< Select to generate 256-bit random key \hideinitializer */
  30. #define PRNG_SEED_CONT 0 /*!< PRNG using current seed \hideinitializer */
  31. #define PRNG_SEED_RELOAD 1 /*!< PRNG reload new seed \hideinitializer */
  32. #define AES_KEY_SIZE_128 0 /*!< AES select 128-bit key length \hideinitializer */
  33. #define AES_KEY_SIZE_192 1 /*!< AES select 192-bit key length \hideinitializer */
  34. #define AES_KEY_SIZE_256 2 /*!< AES select 256-bit key length \hideinitializer */
  35. #define AES_MODE_ECB 0 /*!< AES select ECB mode \hideinitializer */
  36. #define AES_MODE_CBC 1 /*!< AES select CBC mode \hideinitializer */
  37. #define AES_MODE_CFB 2 /*!< AES select CFB mode \hideinitializer */
  38. #define AES_MODE_OFB 3 /*!< AES select OFB mode \hideinitializer */
  39. #define AES_MODE_CTR 4 /*!< AES select CTR mode \hideinitializer */
  40. #define AES_MODE_CBC_CS1 0x10 /*!< AES select CBC CS1 mode \hideinitializer */
  41. #define AES_MODE_CBC_CS2 0x11 /*!< AES select CBC CS2 mode \hideinitializer */
  42. #define AES_MODE_CBC_CS3 0x12 /*!< AES select CBC CS3 mode \hideinitializer */
  43. #define AES_NO_SWAP 0 /*!< AES do not swap input and output data \hideinitializer */
  44. #define AES_OUT_SWAP 1 /*!< AES swap output data \hideinitializer */
  45. #define AES_IN_SWAP 2 /*!< AES swap input data \hideinitializer */
  46. #define AES_IN_OUT_SWAP 3 /*!< AES swap both input and output data \hideinitializer */
  47. #define DES_MODE_ECB 0x000 /*!< DES select ECB mode \hideinitializer */
  48. #define DES_MODE_CBC 0x100 /*!< DES select CBC mode \hideinitializer */
  49. #define DES_MODE_CFB 0x200 /*!< DES select CFB mode \hideinitializer */
  50. #define DES_MODE_OFB 0x300 /*!< DES select OFB mode \hideinitializer */
  51. #define DES_MODE_CTR 0x400 /*!< DES select CTR mode \hideinitializer */
  52. #define TDES_MODE_ECB 0x004 /*!< TDES select ECB mode \hideinitializer */
  53. #define TDES_MODE_CBC 0x104 /*!< TDES select CBC mode \hideinitializer */
  54. #define TDES_MODE_CFB 0x204 /*!< TDES select CFB mode \hideinitializer */
  55. #define TDES_MODE_OFB 0x304 /*!< TDES select OFB mode \hideinitializer */
  56. #define TDES_MODE_CTR 0x404 /*!< TDES select CTR mode \hideinitializer */
  57. #define TDES_NO_SWAP 0 /*!< TDES do not swap data \hideinitializer */
  58. #define TDES_WHL_SWAP 1 /*!< TDES swap high-low word \hideinitializer */
  59. #define TDES_OUT_SWAP 2 /*!< TDES swap output data \hideinitializer */
  60. #define TDES_OUT_WHL_SWAP 3 /*!< TDES swap output data and high-low word \hideinitializer */
  61. #define TDES_IN_SWAP 4 /*!< TDES swap input data \hideinitializer */
  62. #define TDES_IN_WHL_SWAP 5 /*!< TDES swap input data and high-low word \hideinitializer */
  63. #define TDES_IN_OUT_SWAP 6 /*!< TDES swap both input and output data \hideinitializer */
  64. #define TDES_IN_OUT_WHL_SWAP 7 /*!< TDES swap input, output and high-low word \hideinitializer */
  65. #define SHA_MODE_SHA1 0 /*!< SHA select SHA-1 160-bit \hideinitializer */
  66. #define SHA_MODE_SHA224 5 /*!< SHA select SHA-224 224-bit \hideinitializer */
  67. #define SHA_MODE_SHA256 4 /*!< SHA select SHA-256 256-bit \hideinitializer */
  68. #define SHA_NO_SWAP 0 /*!< SHA do not swap input and output data \hideinitializer */
  69. #define SHA_OUT_SWAP 1 /*!< SHA swap output data \hideinitializer */
  70. #define SHA_IN_SWAP 2 /*!< SHA swap input data \hideinitializer */
  71. #define SHA_IN_OUT_SWAP 3 /*!< SHA swap both input and output data \hideinitializer */
  72. #define CRYPTO_DMA_ONE_SHOT 0x5 /*!< Do one shot encrypt/decrypt with DMA \hideinitializer */
  73. #define CRYPTO_DMA_CONTINUE 0x6 /*!< Do one continuous encrypt/decrypt with DMA \hideinitializer */
  74. #define CRYPTO_DMA_LAST 0x6 /*!< Do last encrypt/decrypt with DMA \hideinitializer */
  75. /*@}*/ /* end of group NUC472_442_CRYPTO_EXPORTED_CONSTANTS */
  76. /** @addtogroup NUC472_442_CRYPTO_EXPORTED_FUNCTIONS CRYPTO Exported Functions
  77. @{
  78. */
  79. /*----------------------------------------------------------------------------------------------*/
  80. /* Macros */
  81. /*----------------------------------------------------------------------------------------------*/
  82. /**
  83. * @brief This macro enables PRNG interrupt.
  84. * @param None
  85. * @return None
  86. * \hideinitializer
  87. */
  88. #define PRNG_ENABLE_INT() (CRPT->INTEN |= CRPT_INTEN_PRNGIEN_Msk)
  89. /**
  90. * @brief This macro disables PRNG interrupt.
  91. * @param None
  92. * @return None
  93. * \hideinitializer
  94. */
  95. #define PRNG_DISABLE_INT() (CRPT->INTEN &= ~CRPT_INTEN_PRNGIEN_Msk)
  96. /**
  97. * @brief This macro gets PRNG interrupt flag.
  98. * @param None
  99. * @return PRNG interrupt flag.
  100. * \hideinitializer
  101. */
  102. #define PRNG_GET_INT_FLAG() (CRPT->INTSTS & CRPT_INTSTS_PRNGIF_Msk)
  103. /**
  104. * @brief This macro clears PRNG interrupt flag.
  105. * @param None
  106. * @return None
  107. * \hideinitializer
  108. */
  109. #define PRNG_CLR_INT_FLAG() (CRPT->INTSTS = CRPT_INTSTS_PRNGIF_Msk)
  110. /**
  111. * @brief This macro enables AES interrupt.
  112. * @param None
  113. * @return None
  114. * \hideinitializer
  115. */
  116. #define AES_ENABLE_INT() (CRPT->INTEN |= (CRPT_INTEN_AESIEN_Msk|CRPT_INTEN_AESERRIEN_Msk))
  117. /**
  118. * @brief This macro disables AES interrupt.
  119. * @param None
  120. * @return None
  121. * \hideinitializer
  122. */
  123. #define AES_DISABLE_INT() (CRPT->INTEN &= ~(CRPT_INTEN_AESIEN_Msk|CRPT_INTEN_AESERRIEN_Msk))
  124. /**
  125. * @brief This macro gets AES interrupt flag.
  126. * @param None
  127. * @return AES interrupt flag.
  128. * \hideinitializer
  129. */
  130. #define AES_GET_INT_FLAG() (CRPT->INTSTS & (CRPT_INTSTS_AESIF_Msk|CRPT_INTSTS_AESERRIF_Msk))
  131. /**
  132. * @brief This macro clears AES interrupt flag.
  133. * @param None
  134. * @return None
  135. * \hideinitializer
  136. */
  137. #define AES_CLR_INT_FLAG() (CRPT->INTSTS = (CRPT_INTSTS_AESIF_Msk|CRPT_INTSTS_AESERRIF_Msk))
  138. /**
  139. * @brief This macro enables AES key protection.
  140. * @param None
  141. * @return None
  142. * \hideinitializer
  143. */
  144. #define AES_ENABLE_KEY_PROTECT() (CRPT->AES_CTL |= CRPT_AES_CTL_KEYPRT_Msk)
  145. /**
  146. * @brief This macro disables AES key protection.
  147. * @param None
  148. * @return None
  149. * \hideinitializer
  150. */
  151. #define AES_DISABLE_KEY_PROTECT() (CRPT->AES_CTL = (CRPT->AES_CTL & ~CRPT_AES_CTL_KEYPRT_Msk) | (0x16<<CRPT_AES_CTL_KEYUNPRT_Pos))
  152. /**
  153. * @brief This macro enables TDES interrupt.
  154. * @param None
  155. * @return None
  156. * \hideinitializer
  157. */
  158. #define TDES_ENABLE_INT() (CRPT->INTEN |= (CRPT_INTEN_TDESIEN_Msk|CRPT_INTEN_TDESERRIEN_Msk))
  159. /**
  160. * @brief This macro disables TDES interrupt.
  161. * @param None
  162. * @return None
  163. * \hideinitializer
  164. */
  165. #define TDES_DISABLE_INT() (CRPT->INTEN &= ~(CRPT_INTEN_TDESIEN_Msk|CRPT_INTEN_TDESERRIEN_Msk))
  166. /**
  167. * @brief This macro gets TDES interrupt flag.
  168. * @param None
  169. * @return TDES interrupt flag.
  170. * \hideinitializer
  171. */
  172. #define TDES_GET_INT_FLAG() (CRPT->INTSTS & (CRPT_INTSTS_TDESIF_Msk|CRPT_INTSTS_TDESERRIF_Msk))
  173. /**
  174. * @brief This macro clears TDES interrupt flag.
  175. * @param None
  176. * @return None
  177. * \hideinitializer
  178. */
  179. #define TDES_CLR_INT_FLAG() (CRPT->INTSTS = (CRPT_INTSTS_TDESIF_Msk|CRPT_INTSTS_TDESERRIF_Msk))
  180. /**
  181. * @brief This macro enables TDES key protection.
  182. * @param None
  183. * @return None
  184. * \hideinitializer
  185. */
  186. #define TDES_ENABLE_KEY_PROTECT() (CRPT->TDES_CTL |= CRPT_TDES_CTL_KEYPRT_Msk)
  187. /**
  188. * @brief This macro disables TDES key protection.
  189. * @param None
  190. * @return None
  191. * \hideinitializer
  192. */
  193. #define TDES_DISABLE_KEY_PROTECT() (CRPT->TDES_CTL = (CRPT->TDES_CTL & ~CRPT_TDES_CTL_KEYPRT_Msk) | (0x16<<CRPT_TDES_CTL_KEYUNPRT_Pos))
  194. /**
  195. * @brief This macro enables SHA interrupt.
  196. * @param None
  197. * @return None
  198. * \hideinitializer
  199. */
  200. #define SHA_ENABLE_INT() (CRPT->INTEN |= (CRPT_INTEN_SHAIEN_Msk|CRPT_INTEN_SHAERRIEN_Msk))
  201. /**
  202. * @brief This macro disables SHA interrupt.
  203. * @param None
  204. * @return None
  205. * \hideinitializer
  206. */
  207. #define SHA_DISABLE_INT() (CRPT->INTEN &= ~(CRPT_INTEN_SHAIEN_Msk|CRPT_INTEN_SHAERRIEN_Msk))
  208. /**
  209. * @brief This macro gets SHA interrupt flag.
  210. * @param None
  211. * @return SHA interrupt flag.
  212. * \hideinitializer
  213. */
  214. #define SHA_GET_INT_FLAG() (CRPT->INTSTS & (CRPT_INTSTS_SHAIF_Msk|CRPT_INTSTS_SHAERRIF_Msk))
  215. /**
  216. * @brief This macro clears SHA interrupt flag.
  217. * @param None
  218. * @return None
  219. * \hideinitializer
  220. */
  221. #define SHA_CLR_INT_FLAG() (CRPT->INTSTS = (CRPT_INTSTS_SHAIF_Msk|CRPT_INTSTS_SHAERRIF_Msk))
  222. /*---------------------------------------------------------------------------------------------------------*/
  223. /* Functions */
  224. /*---------------------------------------------------------------------------------------------------------*/
  225. void PRNG_Open(uint32_t u32KeySize, uint32_t u32SeedReload, uint32_t u32Seed);
  226. void PRNG_Start(void);
  227. void PRNG_Read(uint32_t u32RandKey[]);
  228. void AES_Open(uint32_t u32Channel, uint32_t u32EncDec, uint32_t u32OpMode, uint32_t u32KeySize, uint32_t u32SwapType);
  229. void AES_Start(int32_t u32Channel, uint32_t u32DMAMode);
  230. void AES_SetKey(uint32_t u32Channel, uint32_t au32Keys[], uint32_t u32KeySize);
  231. void AES_SetInitVect(uint32_t u32Channel, uint32_t au32IV[]);
  232. void AES_SetDMATransfer(uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt);
  233. void TDES_Open(uint32_t u32Channel, uint32_t u32EncDec, uint32_t u32OpMode, uint32_t u32SwapType);
  234. void TDES_Start(int32_t u32Channel, uint32_t u32DMAMode);
  235. void TDES_SetKey(uint32_t u32Channel, uint8_t au8Keys[3][8]);
  236. void TDES_SetInitVect(uint32_t u32Channel, uint32_t u32IVH, uint32_t u32IVL);
  237. void TDES_SetDMATransfer(uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt);
  238. void SHA_Open(uint32_t u32OpMode, uint32_t u32SwapType);
  239. void SHA_Start(uint32_t u32DMAMode);
  240. void SHA_SetDMATransfer(uint32_t u32SrcAddr, uint32_t u32TransCnt);
  241. void SHA_Read(uint32_t u32Digest[]);
  242. /*@}*/ /* end of group NUC472_442_CRYPTO_EXPORTED_FUNCTIONS */
  243. /*@}*/ /* end of group NUC472_442_CRYPTO_Driver */
  244. /*@}*/ /* end of group NUC472_442_Device_Driver */
  245. #ifdef __cplusplus
  246. }
  247. #endif
  248. #endif // __CRYPTO_H__
  249. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/