stm32l4xx_hal_cryp.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_cryp.h
  4. * @author MCD Application Team
  5. * @brief Header file of CRYP HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. /* Define to prevent recursive inclusion -------------------------------------*/
  36. #ifndef __STM32L4xx_HAL_CRYP_H
  37. #define __STM32L4xx_HAL_CRYP_H
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. #if defined (STM32L442xx) || defined (STM32L443xx) || defined (STM32L462xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L4A6xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  42. /* Includes ------------------------------------------------------------------*/
  43. #include "stm32l4xx_hal_def.h"
  44. /** @addtogroup STM32L4xx_HAL_Driver
  45. * @{
  46. */
  47. /** @addtogroup CRYP
  48. * @{
  49. */
  50. /* Exported types ------------------------------------------------------------*/
  51. /** @defgroup CRYP_Exported_Types CRYP Exported Types
  52. * @{
  53. */
  54. /**
  55. * @brief CRYP Configuration Structure definition
  56. */
  57. typedef struct
  58. {
  59. uint32_t DataType; /*!< 32-bit data, 16-bit data, 8-bit data or 1-bit string.
  60. This parameter can be a value of @ref CRYP_Data_Type */
  61. uint32_t KeySize; /*!< 128 or 256-bit key length.
  62. This parameter can be a value of @ref CRYP_Key_Size */
  63. uint32_t OperatingMode; /*!< AES operating mode.
  64. This parameter can be a value of @ref CRYP_AES_OperatingMode */
  65. uint32_t ChainingMode; /*!< AES chaining mode.
  66. This parameter can be a value of @ref CRYP_AES_ChainingMode */
  67. uint32_t KeyWriteFlag; /*!< Allows to bypass or not key write-up before decryption.
  68. This parameter can be a value of @ref CRYP_Key_Write */
  69. uint32_t GCMCMACPhase; /*!< Indicates the processing phase of the Galois Counter Mode (GCM),
  70. Galois Message Authentication Code (GMAC), Cipher Message
  71. Authentication Code (CMAC) (when applicable) or Counter with Cipher
  72. Mode (CCM) (when applicable).
  73. This parameter can be a value of @ref CRYP_GCM_CMAC_Phase */
  74. uint8_t* pKey; /*!< Encryption/Decryption Key */
  75. uint8_t* pInitVect; /*!< Initialization Vector used for CTR, CBC, GCM/GMAC, CMAC (when applicable)
  76. and CCM (when applicable) modes */
  77. uint8_t* Header; /*!< Header used in GCM/GMAC, CMAC (when applicable) and CCM (when applicable) modes */
  78. uint64_t HeaderSize; /*!< Header size in bytes */
  79. }CRYP_InitTypeDef;
  80. /**
  81. * @brief HAL CRYP State structures definition
  82. */
  83. typedef enum
  84. {
  85. HAL_CRYP_STATE_RESET = 0x00, /*!< CRYP not yet initialized or disabled */
  86. HAL_CRYP_STATE_READY = 0x01, /*!< CRYP initialized and ready for use */
  87. HAL_CRYP_STATE_BUSY = 0x02, /*!< CRYP internal processing is ongoing */
  88. HAL_CRYP_STATE_TIMEOUT = 0x03, /*!< CRYP timeout state */
  89. HAL_CRYP_STATE_ERROR = 0x04, /*!< CRYP error state */
  90. HAL_CRYP_STATE_SUSPENDED = 0x05 /*!< CRYP suspended */
  91. }HAL_CRYP_STATETypeDef;
  92. /**
  93. * @brief HAL CRYP phase structures definition
  94. */
  95. typedef enum
  96. {
  97. HAL_CRYP_PHASE_READY = 0x01, /*!< CRYP peripheral is ready for initialization. */
  98. HAL_CRYP_PHASE_PROCESS = 0x02, /*!< CRYP peripheral is in processing phase */
  99. HAL_CRYP_PHASE_START = 0x03, /*!< CRYP peripheral has been initialized but
  100. GCM/GMAC(/CMAC)(/CCM) initialization phase has not started */
  101. HAL_CRYP_PHASE_INIT_OVER = 0x04, /*!< GCM/GMAC(/CMAC)(/CCM) init phase has been carried out */
  102. HAL_CRYP_PHASE_HEADER_OVER = 0x05, /*!< GCM/GMAC(/CMAC)(/CCM) header phase has been carried out */
  103. HAL_CRYP_PHASE_PAYLOAD_OVER = 0x06, /*!< GCM(/CCM) payload phase has been carried out */
  104. HAL_CRYP_PHASE_FINAL_OVER = 0x07, /*!< GCM/GMAC(/CMAC)(/CCM) final phase has been carried out */
  105. HAL_CRYP_PHASE_HEADER_SUSPENDED = 0x08, /*!< GCM/GMAC(/CMAC)(/CCM) header phase has been suspended */
  106. HAL_CRYP_PHASE_PAYLOAD_SUSPENDED = 0x09, /*!< GCM(/CCM) payload phase has been suspended */
  107. HAL_CRYP_PHASE_NOT_USED = 0x0a /*!< Phase is irrelevant to the current chaining mode */
  108. }HAL_PhaseTypeDef;
  109. /**
  110. * @brief HAL CRYP mode suspend definitions
  111. */
  112. typedef enum
  113. {
  114. HAL_CRYP_SUSPEND_NONE = 0x00, /*!< CRYP peripheral suspension not requested */
  115. HAL_CRYP_SUSPEND = 0x01 /*!< CRYP peripheral suspension requested */
  116. }HAL_SuspendTypeDef;
  117. /**
  118. * @brief HAL CRYP Error Codes definition
  119. */
  120. #define HAL_CRYP_ERROR_NONE ((uint32_t)0x00000000) /*!< No error */
  121. #define HAL_CRYP_WRITE_ERROR ((uint32_t)0x00000001) /*!< Write error */
  122. #define HAL_CRYP_READ_ERROR ((uint32_t)0x00000002) /*!< Read error */
  123. #define HAL_CRYP_DMA_ERROR ((uint32_t)0x00000004) /*!< DMA error */
  124. #define HAL_CRYP_BUSY_ERROR ((uint32_t)0x00000008) /*!< Busy flag error */
  125. /**
  126. * @brief CRYP handle Structure definition
  127. */
  128. typedef struct
  129. {
  130. AES_TypeDef *Instance; /*!< Register base address */
  131. CRYP_InitTypeDef Init; /*!< CRYP initialization parameters */
  132. uint8_t *pCrypInBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) input buffer */
  133. uint8_t *pCrypOutBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) output buffer */
  134. uint32_t CrypInCount; /*!< Input data size in bytes or, after suspension, the remaining
  135. number of bytes to process */
  136. uint32_t CrypOutCount; /*!< Output data size in bytes */
  137. HAL_PhaseTypeDef Phase; /*!< CRYP peripheral processing phase for GCM, GMAC, CMAC (when applicable)
  138. or CCM (when applicable) modes.
  139. Indicates the last phase carried out to ease
  140. phase transitions */
  141. DMA_HandleTypeDef *hdmain; /*!< CRYP peripheral Input DMA handle parameters */
  142. DMA_HandleTypeDef *hdmaout; /*!< CRYP peripheral Output DMA handle parameters */
  143. HAL_LockTypeDef Lock; /*!< CRYP locking object */
  144. __IO HAL_CRYP_STATETypeDef State; /*!< CRYP peripheral state */
  145. __IO uint32_t ErrorCode; /*!< CRYP peripheral error code */
  146. HAL_SuspendTypeDef SuspendRequest; /*!< CRYP peripheral suspension request flag */
  147. }CRYP_HandleTypeDef;
  148. /**
  149. * @}
  150. */
  151. /* Exported constants --------------------------------------------------------*/
  152. /** @defgroup CRYP_Exported_Constants CRYP Exported Constants
  153. * @{
  154. */
  155. /** @defgroup CRYP_Key_Size Key size selection
  156. * @{
  157. */
  158. #define CRYP_KEYSIZE_128B ((uint32_t)0x00000000) /*!< 128-bit long key */
  159. #define CRYP_KEYSIZE_256B AES_CR_KEYSIZE /*!< 256-bit long key */
  160. /**
  161. * @}
  162. */
  163. /** @defgroup CRYP_Data_Type AES Data Type selection
  164. * @{
  165. */
  166. #define CRYP_DATATYPE_32B ((uint32_t)0x00000000) /*!< 32-bit data type (no swapping) */
  167. #define CRYP_DATATYPE_16B AES_CR_DATATYPE_0 /*!< 16-bit data type (half-word swapping) */
  168. #define CRYP_DATATYPE_8B AES_CR_DATATYPE_1 /*!< 8-bit data type (byte swapping) */
  169. #define CRYP_DATATYPE_1B AES_CR_DATATYPE /*!< 1-bit data type (bit swapping) */
  170. /**
  171. * @}
  172. */
  173. /** @defgroup CRYP_AES_State AES Enable state
  174. * @{
  175. */
  176. #define CRYP_AES_DISABLE ((uint32_t)0x00000000) /*!< Disable AES */
  177. #define CRYP_AES_ENABLE AES_CR_EN /*!< Enable AES */
  178. /**
  179. * @}
  180. */
  181. /** @defgroup CRYP_AES_OperatingMode AES operating mode
  182. * @{
  183. */
  184. #define CRYP_ALGOMODE_ENCRYPT ((uint32_t)0x00000000) /*!< Encryption mode */
  185. #define CRYP_ALGOMODE_KEYDERIVATION AES_CR_MODE_0 /*!< Key derivation mode */
  186. #define CRYP_ALGOMODE_DECRYPT AES_CR_MODE_1 /*!< Decryption */
  187. #define CRYP_ALGOMODE_KEYDERIVATION_DECRYPT AES_CR_MODE /*!< Key derivation and decryption */
  188. #define CRYP_ALGOMODE_TAG_GENERATION ((uint32_t)0x00000000) /*!< GMAC or CMAC (when applicable) authentication tag generation */
  189. /**
  190. * @}
  191. */
  192. /** @defgroup CRYP_AES_ChainingMode AES chaining mode
  193. * @{
  194. */
  195. #define CRYP_CHAINMODE_AES_ECB ((uint32_t)0x00000000) /*!< Electronic codebook chaining algorithm */
  196. #define CRYP_CHAINMODE_AES_CBC AES_CR_CHMOD_0 /*!< Cipher block chaining algorithm */
  197. #define CRYP_CHAINMODE_AES_CTR AES_CR_CHMOD_1 /*!< Counter mode chaining algorithm */
  198. #define CRYP_CHAINMODE_AES_GCM_GMAC (AES_CR_CHMOD_0 | AES_CR_CHMOD_1) /*!< Galois counter mode - Galois message authentication code */
  199. #if defined(AES_CR_NPBLB)
  200. #define CRYP_CHAINMODE_AES_CCM AES_CR_CHMOD_2 /*!< Counter with Cipher Mode */
  201. #else
  202. #define CRYP_CHAINMODE_AES_CMAC AES_CR_CHMOD_2 /*!< Cipher message authentication code */
  203. #endif
  204. /**
  205. * @}
  206. */
  207. /** @defgroup CRYP_Key_Write AES decryption key write-up flag
  208. * @{
  209. */
  210. #define CRYP_KEY_WRITE_ENABLE ((uint32_t)0x00000000) /*!< Enable decryption key writing */
  211. #define CRYP_KEY_WRITE_DISABLE ((uint32_t)0x00000001) /*!< Disable decryption key writing */
  212. /**
  213. * @}
  214. */
  215. /** @defgroup CRYP_DMAIN DMA Input phase management enable state
  216. * @{
  217. */
  218. #define CRYP_DMAIN_DISABLE ((uint32_t)0x00000000) /*!< Disable DMA Input phase management */
  219. #define CRYP_DMAIN_ENABLE AES_CR_DMAINEN /*!< Enable DMA Input phase management */
  220. /**
  221. * @}
  222. */
  223. /** @defgroup CRYP_DMAOUT DMA Output phase management enable state
  224. * @{
  225. */
  226. #define CRYP_DMAOUT_DISABLE ((uint32_t)0x00000000) /*!< Disable DMA Output phase management */
  227. #define CRYP_DMAOUT_ENABLE AES_CR_DMAOUTEN /*!< Enable DMA Output phase management */
  228. /**
  229. * @}
  230. */
  231. /** @defgroup CRYP_GCM_CMAC_Phase GCM/GMAC and CCM/CMAC (when applicable) processing phase selection
  232. * @{
  233. */
  234. #define CRYP_GCM_INIT_PHASE ((uint32_t)0x00000000) /*!< GCM/GMAC (or CCM) init phase */
  235. #define CRYP_GCMCMAC_HEADER_PHASE AES_CR_GCMPH_0 /*!< GCM/GMAC/CCM/CMAC header phase */
  236. #define CRYP_GCM_PAYLOAD_PHASE AES_CR_GCMPH_1 /*!< GCM/CCM payload phase */
  237. #define CRYP_GCMCMAC_FINAL_PHASE AES_CR_GCMPH /*!< GCM/GMAC/CCM/CMAC final phase */
  238. /* Definitions duplication for code readibility's sake:
  239. supported or not supported chain modes are not specified for each phase */
  240. #define CRYP_INIT_PHASE ((uint32_t)0x00000000) /*!< Init phase */
  241. #define CRYP_HEADER_PHASE AES_CR_GCMPH_0 /*!< Header phase */
  242. #define CRYP_PAYLOAD_PHASE AES_CR_GCMPH_1 /*!< Payload phase */
  243. #define CRYP_FINAL_PHASE AES_CR_GCMPH /*!< Final phase */
  244. /**
  245. * @}
  246. */
  247. /** @defgroup CRYP_Flags AES status flags
  248. * @{
  249. */
  250. #define CRYP_FLAG_BUSY AES_SR_BUSY /*!< GCM process suspension forbidden */
  251. #define CRYP_FLAG_WRERR AES_SR_WRERR /*!< Write Error */
  252. #define CRYP_FLAG_RDERR AES_SR_RDERR /*!< Read error */
  253. #define CRYP_FLAG_CCF AES_SR_CCF /*!< Computation completed */
  254. /**
  255. * @}
  256. */
  257. /** @defgroup CRYP_Clear_Flags AES clearing flags
  258. * @{
  259. */
  260. #define CRYP_CCF_CLEAR AES_CR_CCFC /*!< Computation Complete Flag Clear */
  261. #define CRYP_ERR_CLEAR AES_CR_ERRC /*!< Error Flag Clear */
  262. /**
  263. * @}
  264. */
  265. /** @defgroup AES_Interrupts_Enable AES Interrupts Enable bits
  266. * @{
  267. */
  268. #define CRYP_IT_CCFIE AES_CR_CCFIE /*!< Computation Complete interrupt enable */
  269. #define CRYP_IT_ERRIE AES_CR_ERRIE /*!< Error interrupt enable */
  270. /**
  271. * @}
  272. */
  273. /** @defgroup CRYP_Interrupts_Flags AES Interrupts flags
  274. * @{
  275. */
  276. #define CRYP_IT_WRERR AES_SR_WRERR /*!< Write Error */
  277. #define CRYP_IT_RDERR AES_SR_RDERR /*!< Read Error */
  278. #define CRYP_IT_CCF AES_SR_CCF /*!< Computation completed */
  279. /**
  280. * @}
  281. */
  282. /**
  283. * @}
  284. */
  285. /* Exported macros -----------------------------------------------------------*/
  286. /** @defgroup CRYP_Exported_Macros CRYP Exported Macros
  287. * @{
  288. */
  289. /** @brief Reset CRYP handle state.
  290. * @param __HANDLE__: specifies the CRYP handle.
  291. * @retval None
  292. */
  293. #define __HAL_CRYP_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_CRYP_STATE_RESET)
  294. /**
  295. * @brief Enable the CRYP AES peripheral.
  296. * @param __HANDLE__: specifies the CRYP handle.
  297. * @retval None
  298. */
  299. #define __HAL_CRYP_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR |= AES_CR_EN)
  300. /**
  301. * @brief Disable the CRYP AES peripheral.
  302. * @param __HANDLE__: specifies the CRYP handle.
  303. * @retval None
  304. */
  305. #define __HAL_CRYP_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~AES_CR_EN)
  306. /**
  307. * @brief Set the algorithm operating mode.
  308. * @param __HANDLE__: specifies the CRYP handle.
  309. * @param __OPERATING_MODE__: specifies the operating mode
  310. * This parameter can be one of the following values:
  311. * @arg @ref CRYP_ALGOMODE_ENCRYPT encryption
  312. * @arg @ref CRYP_ALGOMODE_KEYDERIVATION key derivation
  313. * @arg @ref CRYP_ALGOMODE_DECRYPT decryption
  314. * @arg @ref CRYP_ALGOMODE_KEYDERIVATION_DECRYPT key derivation and decryption
  315. * @retval None
  316. */
  317. #define __HAL_CRYP_SET_OPERATINGMODE(__HANDLE__, __OPERATING_MODE__) MODIFY_REG((__HANDLE__)->Instance->CR, AES_CR_MODE, (__OPERATING_MODE__))
  318. /**
  319. * @brief Set the algorithm chaining mode.
  320. * @param __HANDLE__: specifies the CRYP handle.
  321. * @param __CHAINING_MODE__: specifies the chaining mode
  322. * This parameter can be one of the following values:
  323. * @arg @ref CRYP_CHAINMODE_AES_ECB Electronic CodeBook
  324. * @arg @ref CRYP_CHAINMODE_AES_CBC Cipher Block Chaining
  325. * @arg @ref CRYP_CHAINMODE_AES_CTR CounTeR mode
  326. * @arg @ref CRYP_CHAINMODE_AES_GCM_GMAC Galois Counter Mode or Galois Message Authentication Code
  327. * @arg @ref CRYP_CHAINMODE_AES_CMAC Cipher Message Authentication Code (or Counter with Cipher Mode when applicable)
  328. * @retval None
  329. */
  330. #define __HAL_CRYP_SET_CHAININGMODE(__HANDLE__, __CHAINING_MODE__) MODIFY_REG((__HANDLE__)->Instance->CR, AES_CR_CHMOD, (__CHAINING_MODE__))
  331. /** @brief Check whether the specified CRYP status flag is set or not.
  332. * @param __HANDLE__: specifies the CRYP handle.
  333. * @param __FLAG__: specifies the flag to check.
  334. * This parameter can be one of the following values:
  335. * @arg @ref CRYP_FLAG_BUSY GCM process suspension forbidden
  336. * @arg @ref CRYP_IT_WRERR Write Error
  337. * @arg @ref CRYP_IT_RDERR Read Error
  338. * @arg @ref CRYP_IT_CCF Computation Complete
  339. * @retval The state of __FLAG__ (TRUE or FALSE).
  340. */
  341. #define __HAL_CRYP_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
  342. /** @brief Clear the CRYP pending status flag.
  343. * @param __HANDLE__: specifies the CRYP handle.
  344. * @param __FLAG__: specifies the flag to clear.
  345. * This parameter can be one of the following values:
  346. * @arg @ref CRYP_ERR_CLEAR Read (RDERR) or Write Error (WRERR) Flag Clear
  347. * @arg @ref CRYP_CCF_CLEAR Computation Complete Flag (CCF) Clear
  348. * @retval None
  349. */
  350. #define __HAL_CRYP_CLEAR_FLAG(__HANDLE__, __FLAG__) SET_BIT((__HANDLE__)->Instance->CR, (__FLAG__))
  351. /** @brief Check whether the specified CRYP interrupt source is enabled or not.
  352. * @param __HANDLE__: specifies the CRYP handle.
  353. * @param __INTERRUPT__: CRYP interrupt source to check
  354. * This parameter can be one of the following values:
  355. * @arg @ref CRYP_IT_ERRIE Error interrupt (used for RDERR and WRERR)
  356. * @arg @ref CRYP_IT_CCFIE Computation Complete interrupt
  357. * @retval State of interruption (TRUE or FALSE).
  358. */
  359. #define __HAL_CRYP_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->CR & (__INTERRUPT__)) == (__INTERRUPT__))
  360. /** @brief Check whether the specified CRYP interrupt is set or not.
  361. * @param __HANDLE__: specifies the CRYP handle.
  362. * @param __INTERRUPT__: specifies the interrupt to check.
  363. * This parameter can be one of the following values:
  364. * @arg @ref CRYP_IT_WRERR Write Error
  365. * @arg @ref CRYP_IT_RDERR Read Error
  366. * @arg @ref CRYP_IT_CCF Computation Complete
  367. * @retval The state of __INTERRUPT__ (TRUE or FALSE).
  368. */
  369. #define __HAL_CRYP_GET_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->SR & (__INTERRUPT__)) == (__INTERRUPT__))
  370. /** @brief Clear the CRYP pending interrupt.
  371. * @param __HANDLE__: specifies the CRYP handle.
  372. * @param __INTERRUPT__: specifies the IT to clear.
  373. * This parameter can be one of the following values:
  374. * @arg @ref CRYP_ERR_CLEAR Read (RDERR) or Write Error (WRERR) Flag Clear
  375. * @arg @ref CRYP_CCF_CLEAR Computation Complete Flag (CCF) Clear
  376. * @retval None
  377. */
  378. #define __HAL_CRYP_CLEAR_IT(__HANDLE__, __INTERRUPT__) SET_BIT((__HANDLE__)->Instance->CR, (__INTERRUPT__))
  379. /**
  380. * @brief Enable the CRYP interrupt.
  381. * @param __HANDLE__: specifies the CRYP handle.
  382. * @param __INTERRUPT__: CRYP Interrupt.
  383. * This parameter can be one of the following values:
  384. * @arg @ref CRYP_IT_ERRIE Error interrupt (used for RDERR and WRERR)
  385. * @arg @ref CRYP_IT_CCFIE Computation Complete interrupt
  386. * @retval None
  387. */
  388. #define __HAL_CRYP_ENABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->CR) |= (__INTERRUPT__))
  389. /**
  390. * @brief Disable the CRYP interrupt.
  391. * @param __HANDLE__: specifies the CRYP handle.
  392. * @param __INTERRUPT__: CRYP Interrupt.
  393. * This parameter can be one of the following values:
  394. * @arg @ref CRYP_IT_ERRIE Error interrupt (used for RDERR and WRERR)
  395. * @arg @ref CRYP_IT_CCFIE Computation Complete interrupt
  396. * @retval None
  397. */
  398. #define __HAL_CRYP_DISABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->CR) &= ~(__INTERRUPT__))
  399. /**
  400. * @}
  401. */
  402. /* Private macros --------------------------------------------------------*/
  403. /** @addtogroup CRYP_Private_Macros CRYP Private Macros
  404. * @{
  405. */
  406. /**
  407. * @brief Verify the key size length.
  408. * @param __KEYSIZE__: Ciphering/deciphering algorithm key size.
  409. * @retval SET (__KEYSIZE__ is a valid value) or RESET (__KEYSIZE__ is invalid)
  410. */
  411. #define IS_CRYP_KEYSIZE(__KEYSIZE__) (((__KEYSIZE__) == CRYP_KEYSIZE_128B) || \
  412. ((__KEYSIZE__) == CRYP_KEYSIZE_256B))
  413. /**
  414. * @brief Verify the input data type.
  415. * @param __DATATYPE__: Ciphering/deciphering algorithm input data type.
  416. * @retval SET (__DATATYPE__ is valid) or RESET (__DATATYPE__ is invalid)
  417. */
  418. #define IS_CRYP_DATATYPE(__DATATYPE__) (((__DATATYPE__) == CRYP_DATATYPE_32B) || \
  419. ((__DATATYPE__) == CRYP_DATATYPE_16B) || \
  420. ((__DATATYPE__) == CRYP_DATATYPE_8B) || \
  421. ((__DATATYPE__) == CRYP_DATATYPE_1B))
  422. /**
  423. * @brief Verify the CRYP AES IP running mode.
  424. * @param __MODE__: CRYP AES IP running mode.
  425. * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
  426. */
  427. #define IS_CRYP_AES(__MODE__) (((__MODE__) == CRYP_AES_DISABLE) || \
  428. ((__MODE__) == CRYP_AES_ENABLE))
  429. /**
  430. * @brief Verify the selected CRYP algorithm.
  431. * @param __ALGOMODE__: Selected CRYP algorithm (ciphering, deciphering, key derivation or a combination of the latter).
  432. * @retval SET (__ALGOMODE__ is valid) or RESET (__ALGOMODE__ is invalid)
  433. */
  434. #define IS_CRYP_ALGOMODE(__ALGOMODE__) (((__ALGOMODE__) == CRYP_ALGOMODE_ENCRYPT) || \
  435. ((__ALGOMODE__) == CRYP_ALGOMODE_KEYDERIVATION) || \
  436. ((__ALGOMODE__) == CRYP_ALGOMODE_DECRYPT) || \
  437. ((__ALGOMODE__) == CRYP_ALGOMODE_TAG_GENERATION) || \
  438. ((__ALGOMODE__) == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT))
  439. /**
  440. * @brief Verify the selected CRYP chaining algorithm.
  441. * @param __CHAINMODE__: Selected CRYP chaining algorithm.
  442. * @retval SET (__CHAINMODE__ is valid) or RESET (__CHAINMODE__ is invalid)
  443. */
  444. #if defined(AES_CR_NPBLB)
  445. #define IS_CRYP_CHAINMODE(__CHAINMODE__) (((__CHAINMODE__) == CRYP_CHAINMODE_AES_ECB) || \
  446. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CBC) || \
  447. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CTR) || \
  448. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_GCM_GMAC) || \
  449. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CCM))
  450. #else
  451. #define IS_CRYP_CHAINMODE(__CHAINMODE__) (((__CHAINMODE__) == CRYP_CHAINMODE_AES_ECB) || \
  452. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CBC) || \
  453. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CTR) || \
  454. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_GCM_GMAC) || \
  455. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CMAC))
  456. #endif
  457. /**
  458. * @brief Verify the deciphering key write option.
  459. * @param __WRITE__: deciphering key write option.
  460. * @retval SET (__WRITE__ is valid) or RESET (__WRITE__ is invalid)
  461. */
  462. #define IS_CRYP_WRITE(__WRITE__) (((__WRITE__) == CRYP_KEY_WRITE_ENABLE) || \
  463. ((__WRITE__) == CRYP_KEY_WRITE_DISABLE))
  464. /**
  465. * @brief Verify the CRYP input data DMA mode.
  466. * @param __MODE__: CRYP input data DMA mode.
  467. * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
  468. */
  469. #define IS_CRYP_DMAIN(__MODE__) (((__MODE__) == CRYP_DMAIN_DISABLE) || \
  470. ((__MODE__) == CRYP_DMAIN_ENABLE))
  471. /**
  472. * @brief Verify the CRYP output data DMA mode.
  473. * @param __MODE__: CRYP output data DMA mode.
  474. * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
  475. */
  476. #define IS_CRYP_DMAOUT(__MODE__) (((__MODE__) == CRYP_DMAOUT_DISABLE) || \
  477. ((__MODE__) == CRYP_DMAOUT_ENABLE))
  478. /**
  479. * @brief Verify the CRYP AES ciphering/deciphering/authentication algorithm phase.
  480. * @param __PHASE__: CRYP AES ciphering/deciphering/authentication algorithm phase.
  481. * @retval SET (__PHASE__ is valid) or RESET (__PHASE__ is invalid)
  482. */
  483. #define IS_CRYP_GCMCMAC_PHASE(__PHASE__) (((__PHASE__) == CRYP_INIT_PHASE) || \
  484. ((__PHASE__) == CRYP_HEADER_PHASE) || \
  485. ((__PHASE__) == CRYP_PAYLOAD_PHASE) || \
  486. ((__PHASE__) == CRYP_FINAL_PHASE))
  487. /**
  488. * @}
  489. */
  490. /* Include CRYP HAL Extended module */
  491. #include "stm32l4xx_hal_cryp_ex.h"
  492. /* Exported functions --------------------------------------------------------*/
  493. /** @addtogroup CRYP_Exported_Functions CRYP Exported Functions
  494. * @{
  495. */
  496. /** @addtogroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions
  497. * @{
  498. */
  499. /* Initialization/de-initialization functions ********************************/
  500. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp);
  501. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp);
  502. /* MSP initialization/de-initialization functions ****************************/
  503. void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp);
  504. void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp);
  505. /**
  506. * @}
  507. */
  508. /** @addtogroup CRYP_Exported_Functions_Group2 AES processing functions
  509. * @{
  510. */
  511. /* AES encryption/decryption processing functions ****************************/
  512. /* AES encryption/decryption using polling ***********************************/
  513. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  514. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  515. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  516. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  517. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  518. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  519. /* AES encryption/decryption using interrupt *********************************/
  520. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  521. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  522. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  523. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  524. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  525. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  526. /* AES encryption/decryption using DMA ***************************************/
  527. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  528. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  529. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  530. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  531. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  532. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  533. /**
  534. * @}
  535. */
  536. /** @addtogroup CRYP_Exported_Functions_Group3 Callback functions
  537. * @{
  538. */
  539. /* CallBack functions ********************************************************/
  540. void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp);
  541. void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp);
  542. void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp);
  543. /**
  544. * @}
  545. */
  546. /** @addtogroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
  547. * @{
  548. */
  549. /* AES interrupt handling function *******************************************/
  550. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp);
  551. /**
  552. * @}
  553. */
  554. /** @addtogroup CRYP_Exported_Functions_Group5 Peripheral State functions
  555. * @{
  556. */
  557. /* Peripheral State functions ************************************************/
  558. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp);
  559. uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp);
  560. /**
  561. * @}
  562. */
  563. /**
  564. * @}
  565. */
  566. /**
  567. * @}
  568. */
  569. /**
  570. * @}
  571. */
  572. #endif /* defined (STM32L442xx) || defined (STM32L443xx) || defined (STM32L462xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L4A6xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) */
  573. #ifdef __cplusplus
  574. }
  575. #endif
  576. #endif /* __STM32L4xx_HAL_CRYP_H */
  577. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/