stm32l4xx_hal_cryp.h 31 KB

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