stm32l1xx_hal_cryp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_cryp.h
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  7. * @brief Header file of CRYP HAL module.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __STM32L1xx_HAL_CRYP_H
  39. #define __STM32L1xx_HAL_CRYP_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. #if defined(STM32L162xC) || defined(STM32L162xCA) || defined(STM32L162xD) || defined(STM32L162xE)
  44. /* Includes ------------------------------------------------------------------*/
  45. #include "stm32l1xx_hal_def.h"
  46. /** @addtogroup STM32L1xx_HAL_Driver
  47. * @{
  48. */
  49. /** @addtogroup CRYP
  50. * @{
  51. */
  52. /* Exported types ------------------------------------------------------------*/
  53. /** @defgroup CRYP_Exported_Types CRYP Exported Types
  54. * @{
  55. */
  56. /**
  57. * @brief CRYP Configuration Structure definition
  58. */
  59. typedef struct
  60. {
  61. uint32_t DataType; /*!< 32-bit data, 16-bit data, 8-bit data or 1-bit string.
  62. This parameter can be a value of @ref CRYP_Data_Type */
  63. uint8_t* pKey; /*!< The key used for encryption/decryption */
  64. uint8_t* pInitVect; /*!< The initialization vector used also as initialization
  65. counter in CTR mode */
  66. }CRYP_InitTypeDef;
  67. /**
  68. * @brief HAL CRYP State structures definition
  69. */
  70. typedef enum
  71. {
  72. HAL_CRYP_STATE_RESET = 0x00, /*!< CRYP not yet initialized or disabled */
  73. HAL_CRYP_STATE_READY = 0x01, /*!< CRYP initialized and ready for use */
  74. HAL_CRYP_STATE_BUSY = 0x02, /*!< CRYP internal processing is ongoing */
  75. HAL_CRYP_STATE_TIMEOUT = 0x03, /*!< CRYP timeout state */
  76. HAL_CRYP_STATE_ERROR = 0x04 /*!< CRYP error state */
  77. }HAL_CRYP_STATETypeDef;
  78. /**
  79. * @brief HAL CRYP phase structures definition
  80. */
  81. typedef enum
  82. {
  83. HAL_CRYP_PHASE_READY = 0x01, /*!< CRYP peripheral is ready for initialization. */
  84. HAL_CRYP_PHASE_PROCESS = 0x02, /*!< CRYP peripheral is in processing phase */
  85. }HAL_PhaseTypeDef;
  86. /**
  87. * @brief CRYP handle Structure definition
  88. */
  89. typedef struct
  90. {
  91. CRYP_InitTypeDef Init; /*!< CRYP required parameters */
  92. uint8_t *pCrypInBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
  93. uint8_t *pCrypOutBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
  94. __IO uint16_t CrypInCount; /*!< Counter of inputed data */
  95. __IO uint16_t CrypOutCount; /*!< Counter of outputed data */
  96. HAL_StatusTypeDef Status; /*!< CRYP peripheral status */
  97. HAL_PhaseTypeDef Phase; /*!< CRYP peripheral phase */
  98. DMA_HandleTypeDef *hdmain; /*!< CRYP In DMA handle parameters */
  99. DMA_HandleTypeDef *hdmaout; /*!< CRYP Out DMA handle parameters */
  100. HAL_LockTypeDef Lock; /*!< CRYP locking object */
  101. __IO HAL_CRYP_STATETypeDef State; /*!< CRYP peripheral state */
  102. }CRYP_HandleTypeDef;
  103. /**
  104. * @}
  105. */
  106. /* Exported constants --------------------------------------------------------*/
  107. /** @defgroup CRYP_Exported_Constants CRYP Exported Constants
  108. * @{
  109. */
  110. /** @defgroup CRYP_Data_Type CRYP Data Type
  111. * @{
  112. */
  113. #define CRYP_DATATYPE_32B ((uint32_t)0x00000000)
  114. #define CRYP_DATATYPE_16B AES_CR_DATATYPE_0
  115. #define CRYP_DATATYPE_8B AES_CR_DATATYPE_1
  116. #define CRYP_DATATYPE_1B AES_CR_DATATYPE
  117. #define IS_CRYP_DATATYPE(DATATYPE) (((DATATYPE) == CRYP_DATATYPE_32B) || \
  118. ((DATATYPE) == CRYP_DATATYPE_16B) || \
  119. ((DATATYPE) == CRYP_DATATYPE_8B) || \
  120. ((DATATYPE) == CRYP_DATATYPE_1B))
  121. /**
  122. * @}
  123. */
  124. /** @defgroup CRYP_AlgoModeDirection CRYP Algo Mode Direction
  125. * @{
  126. */
  127. #define CRYP_CR_ALGOMODE_DIRECTION (uint32_t)(AES_CR_MODE|AES_CR_CHMOD)
  128. #define CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT ((uint32_t)0x00000000)
  129. #define CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT (AES_CR_MODE)
  130. #define CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT (AES_CR_CHMOD_0)
  131. #define CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT ((uint32_t)(AES_CR_CHMOD_0|AES_CR_MODE))
  132. #define CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT (AES_CR_CHMOD_1)
  133. #define CRYP_CR_ALGOMODE_AES_CTR_DECRYPT ((uint32_t)(AES_CR_CHMOD_1 | AES_CR_MODE_1))
  134. /**
  135. * @}
  136. */
  137. /** @defgroup CRYP_AES_Interrupts AES Interrupts
  138. * @{
  139. */
  140. #define AES_IT_CC AES_CR_CCIE /*!< Computation Complete interrupt */
  141. #define AES_IT_ERR AES_CR_ERRIE /*!< Error interrupt */
  142. /**
  143. * @}
  144. */
  145. /** @defgroup CRYP_AES_Flags AES Flags
  146. * @{
  147. */
  148. #define AES_FLAG_CCF AES_SR_CCF /*!< Computation Complete Flag */
  149. #define AES_FLAG_RDERR AES_SR_RDERR /*!< Read Error Flag */
  150. #define AES_FLAG_WRERR AES_SR_WRERR /*!< Write Error Flag */
  151. /**
  152. * @}
  153. */
  154. /** @defgroup CRYP_AES_Clear_Flags AES Clear Flags
  155. * @{
  156. */
  157. #define AES_CLEARFLAG_CCF AES_CR_CCFC /*!< Computation Complete Flag Clear */
  158. #define AES_CLEARFLAG_RDERR AES_CR_ERRC /*!< Read Error Clear */
  159. #define AES_CLEARFLAG_WRERR AES_CR_ERRC /*!< Write Error Clear */
  160. /**
  161. * @}
  162. */
  163. /**
  164. * @}
  165. */
  166. /* Exported macro ------------------------------------------------------------*/
  167. /** @defgroup CRYP_Exported_Macros CRYP Exported Macros
  168. * @{
  169. */
  170. /** @brief Reset CRYP handle state
  171. * @param __HANDLE__: specifies the CRYP Handle.
  172. * @retval None
  173. */
  174. #define __HAL_CRYP_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_CRYP_STATE_RESET)
  175. /**
  176. * @brief Enable/Disable the CRYP peripheral.
  177. * @retval None
  178. */
  179. #define __HAL_CRYP_ENABLE() SET_BIT(AES->CR, AES_CR_EN)
  180. #define __HAL_CRYP_DISABLE() CLEAR_BIT(AES->CR, AES_CR_EN)
  181. /**
  182. * @brief Set the algorithm mode: AES-ECB, AES-CBC, AES-CTR, DES-ECB, DES-CBC,...
  183. * @param __MODE__: The algorithm mode.
  184. * @retval None
  185. */
  186. #define __HAL_CRYP_SET_MODE(__MODE__) SET_BIT(AES->CR, (__MODE__))
  187. /** @brief Check whether the specified CRYP flag is set or not.
  188. * @param __FLAG__: specifies the flag to check.
  189. * This parameter can be one of the following values:
  190. * @arg AES_FLAG_CCF : Computation Complete Flag
  191. * @arg AES_FLAG_RDERR : Read Error Flag
  192. * @arg AES_FLAG_WRERR : Write Error Flag
  193. * @retval The new state of __FLAG__ (TRUE or FALSE).
  194. */
  195. #define __HAL_CRYP_GET_FLAG(__FLAG__) ((AES->SR & (__FLAG__)) == (__FLAG__))
  196. /** @brief Clear the CRYP pending flag.
  197. * @param __HANDLE__: specifies the CRYP handle.
  198. * @param __FLAG__: specifies the flag to clear.
  199. * This parameter can be one of the following values:
  200. * @arg AES_CLEARFLAG_CCF : Computation Complete Clear Flag
  201. * @arg AES_CLEARFLAG_RDERR : Read Error Clear
  202. * @arg AES_CLEARFLAG_WRERR : Write Error Clear
  203. * @retval None
  204. */
  205. #define __HAL_CRYP_CLEAR_FLAG(__HANDLE__, __FLAG__) SET_BIT(AES->CR, (__FLAG__))
  206. /**
  207. * @brief Enable the CRYP interrupt.
  208. * @param __INTERRUPT__: CRYP Interrupt.
  209. * @retval None
  210. */
  211. #define __HAL_CRYP_ENABLE_IT(__INTERRUPT__) SET_BIT(AES->CR, (__INTERRUPT__))
  212. /**
  213. * @brief Disable the CRYP interrupt.
  214. * @param __INTERRUPT__: CRYP interrupt.
  215. * @retval None
  216. */
  217. #define __HAL_CRYP_DISABLE_IT(__INTERRUPT__) CLEAR_BIT(AES->CR, (__INTERRUPT__))
  218. /** @brief Checks if the specified CRYP interrupt source is enabled or disabled.
  219. * @param __HANDLE__: CRYP handle
  220. * @param __INTERRUPT__: CRYP interrupt source to check
  221. * This parameter can be one of the following values:
  222. * @arg AES_IT_CC : Computation Complete interrupt
  223. * @arg AES_IT_ERR : Error interrupt (used for RDERR and WRERR)
  224. * @retval State of interruption (SET or RESET)
  225. */
  226. #define __HAL_CRYP_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) \
  227. (( (AES->CR & (__INTERRUPT__)) == (__INTERRUPT__) \
  228. )? SET : RESET \
  229. )
  230. /** @brief Clear the CRYP pending IT.
  231. * @param __HANDLE__: specifies the CRYP handle.
  232. * @param __IT__: specifies the IT to clear.
  233. * This parameter can be one of the following values:
  234. * @arg AES_CLEARFLAG_CCF : Computation Complete Clear Flag
  235. * @arg AES_CLEARFLAG_RDERR : Read Error Clear
  236. * @arg AES_CLEARFLAG_WRERR : Write Error Clear
  237. * @retval None
  238. */
  239. #define __HAL_CRYP_CLEAR_IT(__HANDLE__, __IT__) SET_BIT(AES->CR, (__IT__))
  240. /**
  241. * @}
  242. */
  243. /* Include CRYP HAL Extension module */
  244. #include "stm32l1xx_hal_cryp_ex.h"
  245. /* Exported functions --------------------------------------------------------*/
  246. /** @addtogroup CRYP_Exported_Functions
  247. * @{
  248. */
  249. /** @addtogroup CRYP_Exported_Functions_Group1
  250. * @{
  251. */
  252. /* Initialization/de-initialization functions *********************************/
  253. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp);
  254. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp);
  255. /* MSP functions *************************************************************/
  256. void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp);
  257. void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp);
  258. /**
  259. * @}
  260. */
  261. /** @addtogroup CRYP_Exported_Functions_Group2
  262. * @{
  263. */
  264. /* AES encryption/decryption using polling ***********************************/
  265. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  266. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  267. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  268. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  269. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  270. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  271. /* AES encryption/decryption using interrupt *********************************/
  272. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  273. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  274. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  275. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  276. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  277. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  278. /* AES encryption/decryption using DMA ***************************************/
  279. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  280. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  281. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  282. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  283. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  284. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  285. /**
  286. * @}
  287. */
  288. /** @addtogroup CRYP_Exported_Functions_Group3
  289. * @{
  290. */
  291. /* CallBack functions ********************************************************/
  292. void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp);
  293. void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp);
  294. void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp);
  295. /**
  296. * @}
  297. */
  298. /** @addtogroup CRYP_Exported_Functions_Group4
  299. * @{
  300. */
  301. /* Processing functions ********************************************************/
  302. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp);
  303. /**
  304. * @}
  305. */
  306. /** @addtogroup CRYP_Exported_Functions_Group5
  307. * @{
  308. */
  309. /* Peripheral State functions **************************************************/
  310. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp);
  311. /**
  312. * @}
  313. */
  314. /**
  315. * @}
  316. */
  317. /**
  318. * @}
  319. */
  320. /**
  321. * @}
  322. */
  323. #endif /* STM32L162xC || STM32L162xCA || STM32L162xD || STM32L162xE*/
  324. #ifdef __cplusplus
  325. }
  326. #endif
  327. #endif /* __STM32L1xx_HAL_CRYP_H */
  328. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/