stm32l0xx_hal_cryp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_cryp.h
  4. * @author MCD Application Team
  5. * @brief Header file of CRYP HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* Define to prevent recursive inclusion -------------------------------------*/
  19. #ifndef __STM32L0xx_HAL_CRYP_H
  20. #define __STM32L0xx_HAL_CRYP_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #if defined (STM32L021xx) || defined (STM32L041xx) || defined (STM32L062xx) || defined (STM32L063xx) || defined (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx)
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32l0xx_hal_def.h"
  27. /** @addtogroup STM32L0xx_HAL_Driver
  28. * @{
  29. */
  30. /** @defgroup CRYP CRYP
  31. * @{
  32. */
  33. /* Exported types ------------------------------------------------------------*/
  34. /** @defgroup CRYP_Exported_Types CRYP Exported Types
  35. * @{
  36. */
  37. /**
  38. * @brief CRYP Configuration Structure definition
  39. */
  40. typedef struct
  41. {
  42. uint32_t DataType; /*!< 32-bit data, 16-bit data, 8-bit data or 1-bit string.
  43. This parameter can be a value of @ref CRYP_Data_Type */
  44. uint8_t* pKey; /*!< The key used for encryption/decryption */
  45. uint8_t* pInitVect; /*!< The initialization vector used also as initialization
  46. counter in CTR mode */
  47. }CRYP_InitTypeDef;
  48. /**
  49. * @brief HAL CRYP State structures definition
  50. */
  51. typedef enum
  52. {
  53. HAL_CRYP_STATE_RESET = 0x00U, /*!< CRYP not yet initialized or disabled */
  54. HAL_CRYP_STATE_READY = 0x01U, /*!< CRYP initialized and ready for use */
  55. HAL_CRYP_STATE_BUSY = 0x02U, /*!< CRYP internal processing is ongoing */
  56. HAL_CRYP_STATE_TIMEOUT = 0x03U, /*!< CRYP timeout state */
  57. HAL_CRYP_STATE_ERROR = 0x04U /*!< CRYP error state */
  58. }HAL_CRYP_STATETypeDef;
  59. /**
  60. * @brief HAL CRYP phase structures definition
  61. */
  62. typedef enum
  63. {
  64. HAL_CRYP_PHASE_READY = 0x01U, /*!< CRYP peripheral is ready for initialization. */
  65. HAL_CRYP_PHASE_PROCESS = 0x02U, /*!< CRYP peripheral is in processing phase */
  66. }HAL_PhaseTypeDef;
  67. /**
  68. * @brief CRYP handle Structure definition
  69. */
  70. typedef struct
  71. {
  72. AES_TypeDef *Instance; /*!< Register base address */
  73. CRYP_InitTypeDef Init; /*!< CRYP required parameters */
  74. uint8_t *pCrypInBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
  75. uint8_t *pCrypOutBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
  76. __IO uint16_t CrypInCount; /*!< Counter of inputted data */
  77. __IO uint16_t CrypOutCount; /*!< Counter of outputted data */
  78. HAL_StatusTypeDef Status; /*!< CRYP peripheral status */
  79. HAL_PhaseTypeDef Phase; /*!< CRYP peripheral phase */
  80. DMA_HandleTypeDef *hdmain; /*!< CRYP In DMA handle parameters */
  81. DMA_HandleTypeDef *hdmaout; /*!< CRYP Out DMA handle parameters */
  82. HAL_LockTypeDef Lock; /*!< CRYP locking object */
  83. __IO HAL_CRYP_STATETypeDef State; /*!< CRYP peripheral state */
  84. }CRYP_HandleTypeDef;
  85. /**
  86. * @}
  87. */
  88. /* Exported constants --------------------------------------------------------*/
  89. /** @defgroup CRYP_Exported_Constants CRYP Exported Constants
  90. * @{
  91. */
  92. /** @defgroup CRYP_Data_Type CRYP Data Type
  93. * @{
  94. */
  95. #define CRYP_DATATYPE_32B (0x00000000U)
  96. #define CRYP_DATATYPE_16B AES_CR_DATATYPE_0
  97. #define CRYP_DATATYPE_8B AES_CR_DATATYPE_1
  98. #define CRYP_DATATYPE_1B AES_CR_DATATYPE
  99. #define IS_CRYP_DATATYPE(DATATYPE) (((DATATYPE) == CRYP_DATATYPE_32B) || \
  100. ((DATATYPE) == CRYP_DATATYPE_16B) || \
  101. ((DATATYPE) == CRYP_DATATYPE_8B) || \
  102. ((DATATYPE) == CRYP_DATATYPE_1B))
  103. /**
  104. * @}
  105. */
  106. /** @defgroup CRYP_AlgoModeDirection CRYP Algo Mode Direction
  107. * @{
  108. */
  109. #define CRYP_CR_ALGOMODE_DIRECTION (uint32_t)(AES_CR_MODE|AES_CR_CHMOD)
  110. #define CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT (0x00000000U)
  111. #define CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT (AES_CR_MODE)
  112. #define CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT (AES_CR_CHMOD_0)
  113. #define CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT ((uint32_t)(AES_CR_CHMOD_0|AES_CR_MODE))
  114. #define CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT (AES_CR_CHMOD_1)
  115. #define CRYP_CR_ALGOMODE_AES_CTR_DECRYPT ((uint32_t)(AES_CR_CHMOD_1 | AES_CR_MODE_1))
  116. /**
  117. * @}
  118. */
  119. /** @defgroup CRYP_AES_Interrupts AES Interrupts
  120. * @{
  121. */
  122. #define CRYP_IT_CC AES_CR_CCIE /*!< Computation Complete interrupt */
  123. #define CRYP_IT_ERR AES_CR_ERRIE /*!< Error interrupt */
  124. /**
  125. * @}
  126. */
  127. /** @defgroup CRYP_AES_Flags AES Flags
  128. * @{
  129. */
  130. #define CRYP_FLAG_CCF AES_SR_CCF /*!< Computation Complete Flag */
  131. #define CRYP_FLAG_RDERR AES_SR_RDERR /*!< Read Error Flag */
  132. #define CRYP_FLAG_WRERR AES_SR_WRERR /*!< Write Error Flag */
  133. /**
  134. * @}
  135. */
  136. /** @defgroup CRYP_AES_Clear_Flags AES Clear Flags
  137. * @{
  138. */
  139. #define CRYP_CLEARFLAG_CCF AES_CR_CCFC /*!< Computation Complete Flag Clear */
  140. #define CRYP_CLEARFLAG_RDERR AES_CR_ERRC /*!< Read Error Clear */
  141. #define CRYP_CLEARFLAG_WRERR AES_CR_ERRC /*!< Write Error Clear */
  142. /**
  143. * @}
  144. */
  145. /**
  146. * @}
  147. */
  148. /* Exported macro ------------------------------------------------------------*/
  149. /** @defgroup CRYP_Exported_Macros CRYP Exported Macros
  150. * @{
  151. */
  152. /** @brief Reset CRYP handle state
  153. * @param __HANDLE__ specifies the CRYP handle.
  154. * @retval None
  155. */
  156. #define __HAL_CRYP_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_CRYP_STATE_RESET)
  157. /**
  158. * @brief Enable/Disable the CRYP peripheral.
  159. * @param __HANDLE__ specifies the CRYP handle.
  160. * @retval None
  161. */
  162. #define __HAL_CRYP_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR, AES_CR_EN)
  163. #define __HAL_CRYP_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR, AES_CR_EN)
  164. /**
  165. * @brief Set the algorithm mode: AES-ECB, AES-CBC, AES-CTR, DES-ECB, DES-CBC,...
  166. * @param __HANDLE__ specifies the CRYP handle.
  167. * @param __MODE__ The algorithm mode.
  168. * @retval None
  169. */
  170. #define __HAL_CRYP_SET_MODE(__HANDLE__,__MODE__) SET_BIT((__HANDLE__)->Instance->CR, (__MODE__))
  171. /** @brief Check whether the specified CRYP flag is set or not.
  172. * @param __HANDLE__ specifies the CRYP handle.
  173. * @param __FLAG__ specifies the flag to check.
  174. * This parameter can be one of the following values:
  175. * @arg CRYP_FLAG_CCF : Computation Complete Flag
  176. * @arg CRYP_FLAG_RDERR : Read Error Flag
  177. * @arg CRYP_FLAG_WRERR : Write Error Flag
  178. * @retval The new state of __FLAG__ (TRUE or FALSE).
  179. */
  180. #define __HAL_CRYP_GET_FLAG(__HANDLE__,__FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
  181. /** @brief Clear the CRYP pending flag.
  182. * @param __HANDLE__ specifies the CRYP handle.
  183. * @param __FLAG__ specifies the flag to clear.
  184. * This parameter can be one of the following values:
  185. * @arg CRYP_CLEARFLAG_CCF : Computation Complete Clear Flag
  186. * @arg CRYP_CLEARFLAG_RDERR : Read Error Clear
  187. * @arg CRYP_CLEARFLAG_WRERR : Write Error Clear
  188. * @retval None
  189. */
  190. #define __HAL_CRYP_CLEAR_FLAG(__HANDLE__, __FLAG__) SET_BIT((__HANDLE__)->Instance->CR, (__FLAG__))
  191. /**
  192. * @brief Enable the CRYP interrupt.
  193. * @param __HANDLE__ specifies the CRYP handle.
  194. * @param __INTERRUPT__ CRYP Interrupt.
  195. * @retval None
  196. */
  197. #define __HAL_CRYP_ENABLE_IT(__HANDLE__,__INTERRUPT__) SET_BIT((__HANDLE__)->Instance->CR, (__INTERRUPT__))
  198. /**
  199. * @brief Disable the CRYP interrupt.
  200. * @param __HANDLE__ specifies the CRYP handle.
  201. * @param __INTERRUPT__ CRYP interrupt.
  202. * @retval None
  203. */
  204. #define __HAL_CRYP_DISABLE_IT(__HANDLE__,__INTERRUPT__) CLEAR_BIT((__HANDLE__)->Instance->CR, (__INTERRUPT__))
  205. /** @brief Checks if the specified CRYP interrupt source is enabled or disabled.
  206. * @param __HANDLE__ specifies the CRYP handle.
  207. * @param __INTERRUPT__ CRYP interrupt source to check
  208. * This parameter can be one of the following values:
  209. * @arg CRYP_IT_CC : Computation Complete interrupt
  210. * @arg CRYP_IT_ERR : Error interrupt (used for RDERR and WRERR)
  211. * @retval State of interruption (SET or RESET)
  212. */
  213. #define __HAL_CRYP_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) \
  214. (( ((__HANDLE__)->Instance->CR & (__INTERRUPT__)) == (__INTERRUPT__) \
  215. )? SET : RESET \
  216. )
  217. /** @brief Clear the CRYP pending IT.
  218. * @param __HANDLE__ specifies the CRYP handle.
  219. * @param __IT__ specifies the IT to clear.
  220. * This parameter can be one of the following values:
  221. * @arg CRYP_CLEARFLAG_CCF : Computation Complete Clear Flag
  222. * @arg CRYP_CLEARFLAG_RDERR : Read Error Clear
  223. * @arg CRYP_CLEARFLAG_WRERR : Write Error Clear
  224. * @retval None
  225. */
  226. #define __HAL_CRYP_CLEAR_IT(__HANDLE__, __IT__) SET_BIT((__HANDLE__)->Instance->CR, (__IT__))
  227. /**
  228. * @}
  229. */
  230. /* Include CRYP HAL Extension module */
  231. #include "stm32l0xx_hal_cryp_ex.h"
  232. /* Exported functions --------------------------------------------------------*/
  233. /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
  234. * @{
  235. */
  236. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
  237. * @{
  238. */
  239. /* Initialization/de-initialization functions *********************************/
  240. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp);
  241. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp);
  242. /* MSP functions *************************************************************/
  243. void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp);
  244. void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp);
  245. /**
  246. * @}
  247. */
  248. /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
  249. * @{
  250. */
  251. /* AES encryption/decryption using polling ***********************************/
  252. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  253. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  254. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  255. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  256. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  257. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  258. /* AES encryption/decryption using interrupt *********************************/
  259. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  260. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  261. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  262. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  263. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  264. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  265. /* AES encryption/decryption using DMA ***************************************/
  266. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  267. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  268. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  269. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  270. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  271. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  272. /**
  273. * @}
  274. */
  275. /** @addtogroup CRYP_Exported_Functions_Group3 DMA callback functions
  276. * @{
  277. */
  278. /* CallBack functions ********************************************************/
  279. void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp);
  280. void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp);
  281. void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp);
  282. /**
  283. * @}
  284. */
  285. /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
  286. * @{
  287. */
  288. /* Processing functions ********************************************************/
  289. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp);
  290. /**
  291. * @}
  292. */
  293. /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
  294. * @{
  295. */
  296. /* Peripheral State functions **************************************************/
  297. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp);
  298. /**
  299. * @}
  300. */
  301. /**
  302. * @}
  303. */
  304. /* Define the private group ***********************************/
  305. /**************************************************************/
  306. /** @defgroup CRYP_Private CRYP Private
  307. * @{
  308. */
  309. /**
  310. * @}
  311. */
  312. /**************************************************************/
  313. /**
  314. * @}
  315. */
  316. /**
  317. * @}
  318. */
  319. #endif /* STM32L021xx || STM32L041xx || STM32L062xx || STM32L063xx || STM32L081xx || STM32L082xx || STM32L083xx */
  320. #ifdef __cplusplus
  321. }
  322. #endif
  323. #endif /* __STM32L0xx_HAL_CRYP_H */