stm32f4xx_hal_cryp.h 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 __STM32F4xx_HAL_CRYP_H
  37. #define __STM32F4xx_HAL_CRYP_H
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32f4xx_hal_def.h"
  43. #if defined(CRYP)
  44. /** @addtogroup STM32F4xx_HAL_Driver
  45. * @{
  46. */
  47. /** @addtogroup CRYP
  48. * @{
  49. */
  50. /* Exported types ------------------------------------------------------------*/
  51. /** @defgroup CRYP_Exported_Types CRYP Exported Types
  52. * @{
  53. */
  54. /** @defgroup CRYP_Exported_Types_Group1 CRYP Configuration Structure definition
  55. * @{
  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; /*!< Used only in AES mode only : 128, 192 or 256 bit key length.
  62. This parameter can be a value of @ref CRYP_Key_Size */
  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. uint8_t IVSize; /*!< The size of initialization vector.
  67. This parameter (called nonce size in CCM) is used only
  68. in AES-128/192/256 encryption/decryption CCM mode */
  69. uint8_t TagSize; /*!< The size of returned authentication TAG.
  70. This parameter is used only in AES-128/192/256
  71. encryption/decryption CCM mode */
  72. uint8_t* Header; /*!< The header used in GCM and CCM modes */
  73. uint32_t HeaderSize; /*!< The size of header buffer in bytes */
  74. uint8_t* pScratch; /*!< Scratch buffer used to append the header. It's size must be equal to header size + 21 bytes.
  75. This parameter is used only in AES-128/192/256 encryption/decryption CCM mode */
  76. }CRYP_InitTypeDef;
  77. /**
  78. * @}
  79. */
  80. /** @defgroup CRYP_Exported_Types_Group2 CRYP State structures definition
  81. * @{
  82. */
  83. typedef enum
  84. {
  85. HAL_CRYP_STATE_RESET = 0x00U, /*!< CRYP not yet initialized or disabled */
  86. HAL_CRYP_STATE_READY = 0x01U, /*!< CRYP initialized and ready for use */
  87. HAL_CRYP_STATE_BUSY = 0x02U, /*!< CRYP internal processing is ongoing */
  88. HAL_CRYP_STATE_TIMEOUT = 0x03U, /*!< CRYP timeout state */
  89. HAL_CRYP_STATE_ERROR = 0x04U /*!< CRYP error state */
  90. }HAL_CRYP_STATETypeDef;
  91. /**
  92. * @}
  93. */
  94. /** @defgroup CRYP_Exported_Types_Group3 CRYP phase structures definition
  95. * @{
  96. */
  97. typedef enum
  98. {
  99. HAL_CRYP_PHASE_READY = 0x01U, /*!< CRYP peripheral is ready for initialization. */
  100. HAL_CRYP_PHASE_PROCESS = 0x02U, /*!< CRYP peripheral is in processing phase */
  101. HAL_CRYP_PHASE_FINAL = 0x03U /*!< CRYP peripheral is in final phase
  102. This is relevant only with CCM and GCM modes */
  103. }HAL_PhaseTypeDef;
  104. /**
  105. * @}
  106. */
  107. /** @defgroup CRYP_Exported_Types_Group4 CRYP handle Structure definition
  108. * @{
  109. */
  110. typedef struct
  111. {
  112. CRYP_TypeDef *Instance; /*!< CRYP registers base address */
  113. CRYP_InitTypeDef Init; /*!< CRYP required parameters */
  114. uint8_t *pCrypInBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
  115. uint8_t *pCrypOutBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
  116. __IO uint16_t CrypInCount; /*!< Counter of inputed data */
  117. __IO uint16_t CrypOutCount; /*!< Counter of output data */
  118. HAL_StatusTypeDef Status; /*!< CRYP peripheral status */
  119. HAL_PhaseTypeDef Phase; /*!< CRYP peripheral phase */
  120. DMA_HandleTypeDef *hdmain; /*!< CRYP In DMA handle parameters */
  121. DMA_HandleTypeDef *hdmaout; /*!< CRYP Out DMA handle parameters */
  122. HAL_LockTypeDef Lock; /*!< CRYP locking object */
  123. __IO HAL_CRYP_STATETypeDef State; /*!< CRYP peripheral state */
  124. }CRYP_HandleTypeDef;
  125. /**
  126. * @}
  127. */
  128. /**
  129. * @}
  130. */
  131. /* Exported constants --------------------------------------------------------*/
  132. /** @defgroup CRYP_Exported_Constants CRYP Exported Constants
  133. * @{
  134. */
  135. /** @defgroup CRYP_Key_Size CRYP Key Size
  136. * @{
  137. */
  138. #define CRYP_KEYSIZE_128B 0x00000000U
  139. #define CRYP_KEYSIZE_192B CRYP_CR_KEYSIZE_0
  140. #define CRYP_KEYSIZE_256B CRYP_CR_KEYSIZE_1
  141. /**
  142. * @}
  143. */
  144. /** @defgroup CRYP_Data_Type CRYP Data Type
  145. * @{
  146. */
  147. #define CRYP_DATATYPE_32B 0x00000000U
  148. #define CRYP_DATATYPE_16B CRYP_CR_DATATYPE_0
  149. #define CRYP_DATATYPE_8B CRYP_CR_DATATYPE_1
  150. #define CRYP_DATATYPE_1B CRYP_CR_DATATYPE
  151. /**
  152. * @}
  153. */
  154. /** @defgroup CRYP_Exported_Constants_Group3 CRYP CRYP_AlgoModeDirection
  155. * @{
  156. */
  157. #define CRYP_CR_ALGOMODE_DIRECTION 0x0008003CU
  158. #define CRYP_CR_ALGOMODE_TDES_ECB_ENCRYPT 0x00000000U
  159. #define CRYP_CR_ALGOMODE_TDES_ECB_DECRYPT 0x00000004U
  160. #define CRYP_CR_ALGOMODE_TDES_CBC_ENCRYPT 0x00000008U
  161. #define CRYP_CR_ALGOMODE_TDES_CBC_DECRYPT 0x0000000CU
  162. #define CRYP_CR_ALGOMODE_DES_ECB_ENCRYPT 0x00000010U
  163. #define CRYP_CR_ALGOMODE_DES_ECB_DECRYPT 0x00000014U
  164. #define CRYP_CR_ALGOMODE_DES_CBC_ENCRYPT 0x00000018U
  165. #define CRYP_CR_ALGOMODE_DES_CBC_DECRYPT 0x0000001CU
  166. #define CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT 0x00000020U
  167. #define CRYP_CR_ALGOMODE_AES_ECB_DECRYPT 0x00000024U
  168. #define CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT 0x00000028U
  169. #define CRYP_CR_ALGOMODE_AES_CBC_DECRYPT 0x0000002CU
  170. #define CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT 0x00000030U
  171. #define CRYP_CR_ALGOMODE_AES_CTR_DECRYPT 0x00000034U
  172. /**
  173. * @}
  174. */
  175. /** @defgroup CRYP_Exported_Constants_Group4 CRYP CRYP_Interrupt
  176. * @{
  177. */
  178. #define CRYP_IT_INI ((uint32_t)CRYP_IMSCR_INIM) /*!< Input FIFO Interrupt */
  179. #define CRYP_IT_OUTI ((uint32_t)CRYP_IMSCR_OUTIM) /*!< Output FIFO Interrupt */
  180. /**
  181. * @}
  182. */
  183. /** @defgroup CRYP_Exported_Constants_Group5 CRYP CRYP_Flags
  184. * @{
  185. */
  186. #define CRYP_FLAG_BUSY 0x00000010U /*!< The CRYP core is currently
  187. processing a block of data
  188. or a key preparation (for
  189. AES decryption). */
  190. #define CRYP_FLAG_IFEM 0x00000001U /*!< Input FIFO is empty */
  191. #define CRYP_FLAG_IFNF 0x00000002U /*!< Input FIFO is not Full */
  192. #define CRYP_FLAG_OFNE 0x00000004U /*!< Output FIFO is not empty */
  193. #define CRYP_FLAG_OFFU 0x00000008U /*!< Output FIFO is Full */
  194. #define CRYP_FLAG_OUTRIS 0x01000002U /*!< Output FIFO service raw
  195. interrupt status */
  196. #define CRYP_FLAG_INRIS 0x01000001U /*!< Input FIFO service raw
  197. interrupt status */
  198. /**
  199. * @}
  200. */
  201. /**
  202. * @}
  203. */
  204. /* Exported macro ------------------------------------------------------------*/
  205. /** @defgroup CRYP_Exported_Macros CRYP Exported Macros
  206. * @{
  207. */
  208. /** @brief Reset CRYP handle state
  209. * @param __HANDLE__ specifies the CRYP handle.
  210. * @retval None
  211. */
  212. #define __HAL_CRYP_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_CRYP_STATE_RESET)
  213. /**
  214. * @brief Enable/Disable the CRYP peripheral.
  215. * @param __HANDLE__ specifies the CRYP handle.
  216. * @retval None
  217. */
  218. #define __HAL_CRYP_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR |= CRYP_CR_CRYPEN)
  219. #define __HAL_CRYP_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~CRYP_CR_CRYPEN)
  220. /**
  221. * @brief Flush the data FIFO.
  222. * @param __HANDLE__ specifies the CRYP handle.
  223. * @retval None
  224. */
  225. #define __HAL_CRYP_FIFO_FLUSH(__HANDLE__) ((__HANDLE__)->Instance->CR |= CRYP_CR_FFLUSH)
  226. /**
  227. * @brief Set the algorithm mode: AES-ECB, AES-CBC, AES-CTR, DES-ECB, DES-CBC.
  228. * @param __HANDLE__ specifies the CRYP handle.
  229. * @param MODE The algorithm mode.
  230. * @retval None
  231. */
  232. #define __HAL_CRYP_SET_MODE(__HANDLE__, MODE) ((__HANDLE__)->Instance->CR |= (uint32_t)(MODE))
  233. /** @brief Check whether the specified CRYP flag is set or not.
  234. * @param __HANDLE__ specifies the CRYP handle.
  235. * @param __FLAG__ specifies the flag to check.
  236. * This parameter can be one of the following values:
  237. * @arg CRYP_FLAG_BUSY: The CRYP core is currently processing a block of data
  238. * or a key preparation (for AES decryption).
  239. * @arg CRYP_FLAG_IFEM: Input FIFO is empty
  240. * @arg CRYP_FLAG_IFNF: Input FIFO is not full
  241. * @arg CRYP_FLAG_INRIS: Input FIFO service raw interrupt is pending
  242. * @arg CRYP_FLAG_OFNE: Output FIFO is not empty
  243. * @arg CRYP_FLAG_OFFU: Output FIFO is full
  244. * @arg CRYP_FLAG_OUTRIS: Input FIFO service raw interrupt is pending
  245. * @retval The new state of __FLAG__ (TRUE or FALSE).
  246. */
  247. #define __HAL_CRYP_GET_FLAG(__HANDLE__, __FLAG__) ((((uint8_t)((__FLAG__) >> 24U)) == 0x01U)?((((__HANDLE__)->Instance->RISR) & ((__FLAG__) & CRYP_FLAG_MASK)) == ((__FLAG__) & CRYP_FLAG_MASK)): \
  248. ((((__HANDLE__)->Instance->RISR) & ((__FLAG__) & CRYP_FLAG_MASK)) == ((__FLAG__) & CRYP_FLAG_MASK)))
  249. /** @brief Check whether the specified CRYP interrupt is set or not.
  250. * @param __HANDLE__ specifies the CRYP handle.
  251. * @param __INTERRUPT__ specifies the interrupt to check.
  252. * This parameter can be one of the following values:
  253. * @arg CRYP_IT_INRIS: Input FIFO service raw interrupt is pending
  254. * @arg CRYP_IT_OUTRIS: Output FIFO service raw interrupt is pending
  255. * @retval The new state of __INTERRUPT__ (TRUE or FALSE).
  256. */
  257. #define __HAL_CRYP_GET_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->MISR & (__INTERRUPT__)) == (__INTERRUPT__))
  258. /**
  259. * @brief Enable the CRYP interrupt.
  260. * @param __HANDLE__ specifies the CRYP handle.
  261. * @param __INTERRUPT__ CRYP Interrupt.
  262. * @retval None
  263. */
  264. #define __HAL_CRYP_ENABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IMSCR) |= (__INTERRUPT__))
  265. /**
  266. * @brief Disable the CRYP interrupt.
  267. * @param __HANDLE__ specifies the CRYP handle.
  268. * @param __INTERRUPT__ CRYP interrupt.
  269. * @retval None
  270. */
  271. #define __HAL_CRYP_DISABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IMSCR) &= ~(__INTERRUPT__))
  272. /**
  273. * @}
  274. */
  275. /* Include CRYP HAL Extension module */
  276. #include "stm32f4xx_hal_cryp_ex.h"
  277. /* Exported functions --------------------------------------------------------*/
  278. /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
  279. * @{
  280. */
  281. /** @addtogroup CRYP_Exported_Functions_Group1
  282. * @{
  283. */
  284. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp);
  285. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp);
  286. void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp);
  287. void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp);
  288. /**
  289. * @}
  290. */
  291. /** @addtogroup CRYP_Exported_Functions_Group2
  292. * @{
  293. */
  294. /* AES encryption/decryption using polling ***********************************/
  295. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  296. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  297. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  298. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  299. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  300. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  301. /* AES encryption/decryption using interrupt *********************************/
  302. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  303. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  304. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  305. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  306. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  307. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  308. /* AES encryption/decryption using DMA ***************************************/
  309. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  310. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  311. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  312. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  313. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  314. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  315. /**
  316. * @}
  317. */
  318. /** @addtogroup CRYP_Exported_Functions_Group3
  319. * @{
  320. */
  321. /* DES encryption/decryption using polling ***********************************/
  322. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  323. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  324. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  325. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  326. /* DES encryption/decryption using interrupt *********************************/
  327. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  328. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  329. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  330. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  331. /* DES encryption/decryption using DMA ***************************************/
  332. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  333. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  334. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  335. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  336. /**
  337. * @}
  338. */
  339. /** @addtogroup CRYP_Exported_Functions_Group4
  340. * @{
  341. */
  342. /* TDES encryption/decryption using polling **********************************/
  343. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  344. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  345. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  346. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  347. /* TDES encryption/decryption using interrupt ********************************/
  348. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  349. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  350. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  351. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  352. /* TDES encryption/decryption using DMA **************************************/
  353. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  354. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  355. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  356. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  357. /**
  358. * @}
  359. */
  360. /** @addtogroup CRYP_Exported_Functions_Group5
  361. * @{
  362. */
  363. void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp);
  364. void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp);
  365. void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp);
  366. /**
  367. * @}
  368. */
  369. /** @addtogroup CRYP_Exported_Functions_Group6
  370. * @{
  371. */
  372. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp);
  373. /**
  374. * @}
  375. */
  376. /** @addtogroup CRYP_Exported_Functions_Group7
  377. * @{
  378. */
  379. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp);
  380. /**
  381. * @}
  382. */
  383. /**
  384. * @}
  385. */
  386. /* Private types -------------------------------------------------------------*/
  387. /** @defgroup CRYP_Private_Types CRYP Private Types
  388. * @{
  389. */
  390. /**
  391. * @}
  392. */
  393. /* Private variables ---------------------------------------------------------*/
  394. /** @defgroup CRYP_Private_Variables CRYP Private Variables
  395. * @{
  396. */
  397. /**
  398. * @}
  399. */
  400. /* Private constants ---------------------------------------------------------*/
  401. /** @defgroup CRYP_Private_Constants CRYP Private Constants
  402. * @{
  403. */
  404. #define CRYP_FLAG_MASK 0x0000001FU
  405. /**
  406. * @}
  407. */
  408. /* Private macros ------------------------------------------------------------*/
  409. /** @defgroup CRYP_Private_Macros CRYP Private Macros
  410. * @{
  411. */
  412. #define IS_CRYP_KEYSIZE(__KEYSIZE__) (((__KEYSIZE__) == CRYP_KEYSIZE_128B) || \
  413. ((__KEYSIZE__) == CRYP_KEYSIZE_192B) || \
  414. ((__KEYSIZE__) == CRYP_KEYSIZE_256B))
  415. #define IS_CRYP_DATATYPE(__DATATYPE__) (((__DATATYPE__) == CRYP_DATATYPE_32B) || \
  416. ((__DATATYPE__) == CRYP_DATATYPE_16B) || \
  417. ((__DATATYPE__) == CRYP_DATATYPE_8B) || \
  418. ((__DATATYPE__) == CRYP_DATATYPE_1B))
  419. /**
  420. * @}
  421. */
  422. /* Private functions ---------------------------------------------------------*/
  423. /** @defgroup CRYP_Private_Functions CRYP Private Functions
  424. * @{
  425. */
  426. /**
  427. * @}
  428. */
  429. #endif /* CRYP */
  430. #if defined (AES)
  431. /** @addtogroup STM32F4xx_HAL_Driver
  432. * @{
  433. */
  434. /** @addtogroup CRYP
  435. * @{
  436. */
  437. /* Exported types ------------------------------------------------------------*/
  438. /** @defgroup CRYP_Exported_Types CRYP Exported Types
  439. * @{
  440. */
  441. /**
  442. * @brief CRYP Configuration Structure definition
  443. */
  444. typedef struct
  445. {
  446. uint32_t DataType; /*!< 32-bit data, 16-bit data, 8-bit data or 1-bit string.
  447. This parameter can be a value of @ref CRYP_Data_Type */
  448. uint32_t KeySize; /*!< 128 or 256-bit key length.
  449. This parameter can be a value of @ref CRYP_Key_Size */
  450. uint32_t OperatingMode; /*!< AES operating mode.
  451. This parameter can be a value of @ref CRYP_AES_OperatingMode */
  452. uint32_t ChainingMode; /*!< AES chaining mode.
  453. This parameter can be a value of @ref CRYP_AES_ChainingMode */
  454. uint32_t KeyWriteFlag; /*!< Allows to bypass or not key write-up before decryption.
  455. This parameter can be a value of @ref CRYP_Key_Write */
  456. uint32_t GCMCMACPhase; /*!< Indicates the processing phase of the Galois Counter Mode (GCM),
  457. Galois Message Authentication Code (GMAC) or Cipher Message
  458. Authentication Code (CMAC) or Counter with Cipher Mode (CCM) when
  459. the latter is applicable.
  460. This parameter can be a value of @ref CRYP_GCM_CMAC_Phase */
  461. uint8_t* pKey; /*!< Encryption/Decryption Key */
  462. uint8_t* pInitVect; /*!< Initialization Vector used for CTR, CBC, GCM/GMAC, CMAC,
  463. (and CCM when applicable) modes */
  464. uint8_t* Header; /*!< Header used in GCM/GMAC, CMAC (and CCM when applicable) modes */
  465. uint64_t HeaderSize; /*!< Header size in bytes */
  466. }CRYP_InitTypeDef;
  467. /**
  468. * @brief HAL CRYP State structures definition
  469. */
  470. typedef enum
  471. {
  472. HAL_CRYP_STATE_RESET = 0x00U, /*!< CRYP not yet initialized or disabled */
  473. HAL_CRYP_STATE_READY = 0x01U, /*!< CRYP initialized and ready for use */
  474. HAL_CRYP_STATE_BUSY = 0x02U, /*!< CRYP internal processing is ongoing */
  475. HAL_CRYP_STATE_TIMEOUT = 0x03U, /*!< CRYP timeout state */
  476. HAL_CRYP_STATE_ERROR = 0x04U, /*!< CRYP error state */
  477. HAL_CRYP_STATE_SUSPENDED = 0x05U /*!< CRYP suspended */
  478. }HAL_CRYP_STATETypeDef;
  479. /**
  480. * @brief HAL CRYP phase structures definition
  481. */
  482. typedef enum
  483. {
  484. HAL_CRYP_PHASE_READY = 0x01U, /*!< CRYP peripheral is ready for initialization. */
  485. HAL_CRYP_PHASE_PROCESS = 0x02U, /*!< CRYP peripheral is in processing phase */
  486. HAL_CRYP_PHASE_START = 0x03U, /*!< CRYP peripheral has been initialized but
  487. GCM/GMAC/CMAC(/CCM) initialization phase has not started */
  488. HAL_CRYP_PHASE_INIT_OVER = 0x04U, /*!< GCM/GMAC/CMAC(/CCM) init phase has been carried out */
  489. HAL_CRYP_PHASE_HEADER_OVER = 0x05U, /*!< GCM/GMAC/CMAC(/CCM) header phase has been carried out */
  490. HAL_CRYP_PHASE_PAYLOAD_OVER = 0x06U, /*!< GCM(/CCM) payload phase has been carried out */
  491. HAL_CRYP_PHASE_FINAL_OVER = 0x07U, /*!< GCM/GMAC/CMAC(/CCM) final phase has been carried out */
  492. HAL_CRYP_PHASE_HEADER_SUSPENDED = 0x08U, /*!< GCM/GMAC/CMAC(/CCM) header phase has been suspended */
  493. HAL_CRYP_PHASE_PAYLOAD_SUSPENDED = 0x09U, /*!< GCM(/CCM) payload phase has been suspended */
  494. HAL_CRYP_PHASE_NOT_USED = 0x0AU /*!< Phase is irrelevant to the current chaining mode */
  495. }HAL_PhaseTypeDef;
  496. /**
  497. * @brief HAL CRYP mode suspend definitions
  498. */
  499. typedef enum
  500. {
  501. HAL_CRYP_SUSPEND_NONE = 0x00U, /*!< CRYP peripheral suspension not requested */
  502. HAL_CRYP_SUSPEND = 0x01U /*!< CRYP peripheral suspension requested */
  503. }HAL_SuspendTypeDef;
  504. /**
  505. * @brief HAL CRYP Error Codes definition
  506. */
  507. #define HAL_CRYP_ERROR_NONE 0x00000000U /*!< No error */
  508. #define HAL_CRYP_WRITE_ERROR 0x00000001U /*!< Write error */
  509. #define HAL_CRYP_READ_ERROR 0x00000002U /*!< Read error */
  510. #define HAL_CRYP_DMA_ERROR 0x00000004U /*!< DMA error */
  511. #define HAL_CRYP_BUSY_ERROR 0x00000008U /*!< Busy flag error */
  512. /**
  513. * @brief CRYP handle Structure definition
  514. */
  515. typedef struct
  516. {
  517. AES_TypeDef *Instance; /*!< Register base address */
  518. CRYP_InitTypeDef Init; /*!< CRYP initialization parameters */
  519. uint8_t *pCrypInBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) input buffer */
  520. uint8_t *pCrypOutBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) output buffer */
  521. uint32_t CrypInCount; /*!< Input data size in bytes or, after suspension, the remaining
  522. number of bytes to process */
  523. uint32_t CrypOutCount; /*!< Output data size in bytes */
  524. HAL_PhaseTypeDef Phase; /*!< CRYP peripheral processing phase for GCM, GMAC, CMAC
  525. (or CCM when applicable) modes.
  526. Indicates the last phase carried out to ease
  527. phase transitions */
  528. DMA_HandleTypeDef *hdmain; /*!< CRYP peripheral Input DMA handle parameters */
  529. DMA_HandleTypeDef *hdmaout; /*!< CRYP peripheral Output DMA handle parameters */
  530. HAL_LockTypeDef Lock; /*!< CRYP locking object */
  531. __IO HAL_CRYP_STATETypeDef State; /*!< CRYP peripheral state */
  532. __IO uint32_t ErrorCode; /*!< CRYP peripheral error code */
  533. HAL_SuspendTypeDef SuspendRequest; /*!< CRYP peripheral suspension request flag */
  534. }CRYP_HandleTypeDef;
  535. /**
  536. * @}
  537. */
  538. /* Exported constants --------------------------------------------------------*/
  539. /** @defgroup CRYP_Exported_Constants CRYP Exported Constants
  540. * @{
  541. */
  542. /** @defgroup CRYP_Key_Size Key size selection
  543. * @{
  544. */
  545. #define CRYP_KEYSIZE_128B 0x00000000U /*!< 128-bit long key */
  546. #define CRYP_KEYSIZE_256B AES_CR_KEYSIZE /*!< 256-bit long key */
  547. /**
  548. * @}
  549. */
  550. /** @defgroup CRYP_Data_Type AES Data Type selection
  551. * @{
  552. */
  553. #define CRYP_DATATYPE_32B 0x00000000U /*!< 32-bit data type (no swapping) */
  554. #define CRYP_DATATYPE_16B AES_CR_DATATYPE_0 /*!< 16-bit data type (half-word swapping) */
  555. #define CRYP_DATATYPE_8B AES_CR_DATATYPE_1 /*!< 8-bit data type (byte swapping) */
  556. #define CRYP_DATATYPE_1B AES_CR_DATATYPE /*!< 1-bit data type (bit swapping) */
  557. /**
  558. * @}
  559. */
  560. /** @defgroup CRYP_AES_State AES Enable state
  561. * @{
  562. */
  563. #define CRYP_AES_DISABLE 0x00000000U /*!< Disable AES */
  564. #define CRYP_AES_ENABLE AES_CR_EN /*!< Enable AES */
  565. /**
  566. * @}
  567. */
  568. /** @defgroup CRYP_AES_OperatingMode AES operating mode
  569. * @{
  570. */
  571. #define CRYP_ALGOMODE_ENCRYPT 0x00000000U /*!< Encryption mode */
  572. #define CRYP_ALGOMODE_KEYDERIVATION AES_CR_MODE_0 /*!< Key derivation mode */
  573. #define CRYP_ALGOMODE_DECRYPT AES_CR_MODE_1 /*!< Decryption */
  574. #define CRYP_ALGOMODE_KEYDERIVATION_DECRYPT AES_CR_MODE /*!< Key derivation and decryption */
  575. #define CRYP_ALGOMODE_TAG_GENERATION 0x00000000U /*!< GMAC or CMAC authentication tag generation */
  576. /**
  577. * @}
  578. */
  579. /** @defgroup CRYP_AES_ChainingMode AES chaining mode
  580. * @{
  581. */
  582. #define CRYP_CHAINMODE_AES_ECB 0x00000000U /*!< Electronic codebook chaining algorithm */
  583. #define CRYP_CHAINMODE_AES_CBC AES_CR_CHMOD_0 /*!< Cipher block chaining algorithm */
  584. #define CRYP_CHAINMODE_AES_CTR AES_CR_CHMOD_1 /*!< Counter mode chaining algorithm */
  585. #define CRYP_CHAINMODE_AES_GCM_GMAC (AES_CR_CHMOD_0 | AES_CR_CHMOD_1) /*!< Galois counter mode - Galois message authentication code */
  586. #define CRYP_CHAINMODE_AES_CMAC AES_CR_CHMOD_2 /*!< Cipher message authentication code */
  587. #if defined(AES_CR_NPBLB)
  588. #define CRYP_CHAINMODE_AES_CCM_CMAC AES_CR_CHMOD_2 /*!< Counter with Cipher Mode - Cipher message authentication code */
  589. #endif
  590. /**
  591. * @}
  592. */
  593. /** @defgroup CRYP_Key_Write AES decryption key write-up flag
  594. * @{
  595. */
  596. #define CRYP_KEY_WRITE_ENABLE 0x00000000U /*!< Enable decryption key writing */
  597. #define CRYP_KEY_WRITE_DISABLE 0x00000001U /*!< Disable decryption key writing */
  598. /**
  599. * @}
  600. */
  601. /** @defgroup CRYP_DMAIN DMA Input phase management enable state
  602. * @{
  603. */
  604. #define CRYP_DMAIN_DISABLE 0x00000000U /*!< Disable DMA Input phase management */
  605. #define CRYP_DMAIN_ENABLE AES_CR_DMAINEN /*!< Enable DMA Input phase management */
  606. /**
  607. * @}
  608. */
  609. /** @defgroup CRYP_DMAOUT DMA Output phase management enable state
  610. * @{
  611. */
  612. #define CRYP_DMAOUT_DISABLE 0x00000000U /*!< Disable DMA Output phase management */
  613. #define CRYP_DMAOUT_ENABLE AES_CR_DMAOUTEN /*!< Enable DMA Output phase management */
  614. /**
  615. * @}
  616. */
  617. /** @defgroup CRYP_GCM_CMAC_Phase GCM/GMAC and CMAC processing phase selection
  618. * @{
  619. */
  620. #define CRYP_GCM_INIT_PHASE 0x00000000U /*!< GCM/GMAC (or CCM) init phase */
  621. #define CRYP_GCMCMAC_HEADER_PHASE AES_CR_GCMPH_0 /*!< GCM/GMAC or (CCM/)CMAC header phase */
  622. #define CRYP_GCM_PAYLOAD_PHASE AES_CR_GCMPH_1 /*!< GCM(/CCM) payload phase */
  623. #define CRYP_GCMCMAC_FINAL_PHASE AES_CR_GCMPH /*!< GCM/GMAC or (CCM/)CMAC final phase */
  624. /* Definitions duplication for code readibility's sake:
  625. supported or not supported chain modes are not specified for each phase */
  626. #define CRYP_INIT_PHASE 0x00000000U /*!< Init phase */
  627. #define CRYP_HEADER_PHASE AES_CR_GCMPH_0 /*!< Header phase */
  628. #define CRYP_PAYLOAD_PHASE AES_CR_GCMPH_1 /*!< Payload phase */
  629. #define CRYP_FINAL_PHASE AES_CR_GCMPH /*!< Final phase */
  630. /**
  631. * @}
  632. */
  633. /** @defgroup CRYP_Flags AES status flags
  634. * @{
  635. */
  636. #define CRYP_FLAG_BUSY AES_SR_BUSY /*!< GCM process suspension forbidden */
  637. #define CRYP_FLAG_WRERR AES_SR_WRERR /*!< Write Error */
  638. #define CRYP_FLAG_RDERR AES_SR_RDERR /*!< Read error */
  639. #define CRYP_FLAG_CCF AES_SR_CCF /*!< Computation completed */
  640. /**
  641. * @}
  642. */
  643. /** @defgroup CRYP_Clear_Flags AES clearing flags
  644. * @{
  645. */
  646. #define CRYP_CCF_CLEAR AES_CR_CCFC /*!< Computation Complete Flag Clear */
  647. #define CRYP_ERR_CLEAR AES_CR_ERRC /*!< Error Flag Clear */
  648. /**
  649. * @}
  650. */
  651. /** @defgroup AES_Interrupts_Enable AES Interrupts Enable bits
  652. * @{
  653. */
  654. #define CRYP_IT_CCFIE AES_CR_CCFIE /*!< Computation Complete interrupt enable */
  655. #define CRYP_IT_ERRIE AES_CR_ERRIE /*!< Error interrupt enable */
  656. /**
  657. * @}
  658. */
  659. /** @defgroup CRYP_Interrupts_Flags AES Interrupts flags
  660. * @{
  661. */
  662. #define CRYP_IT_WRERR AES_SR_WRERR /*!< Write Error */
  663. #define CRYP_IT_RDERR AES_SR_RDERR /*!< Read Error */
  664. #define CRYP_IT_CCF AES_SR_CCF /*!< Computation completed */
  665. /**
  666. * @}
  667. */
  668. /**
  669. * @}
  670. */
  671. /* Exported macros -----------------------------------------------------------*/
  672. /** @defgroup CRYP_Exported_Macros CRYP Exported Macros
  673. * @{
  674. */
  675. /** @brief Reset CRYP handle state.
  676. * @param __HANDLE__ specifies the CRYP handle.
  677. * @retval None
  678. */
  679. #define __HAL_CRYP_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_CRYP_STATE_RESET)
  680. /**
  681. * @brief Enable the CRYP AES peripheral.
  682. * @retval None
  683. */
  684. #define __HAL_CRYP_ENABLE() (AES->CR |= AES_CR_EN)
  685. /**
  686. * @brief Disable the CRYP AES peripheral.
  687. * @retval None
  688. */
  689. #define __HAL_CRYP_DISABLE() (AES->CR &= ~AES_CR_EN)
  690. /**
  691. * @brief Set the algorithm operating mode.
  692. * @param __OPERATING_MODE__ specifies the operating mode
  693. * This parameter can be one of the following values:
  694. * @arg @ref CRYP_ALGOMODE_ENCRYPT encryption
  695. * @arg @ref CRYP_ALGOMODE_KEYDERIVATION key derivation
  696. * @arg @ref CRYP_ALGOMODE_DECRYPT decryption
  697. * @arg @ref CRYP_ALGOMODE_KEYDERIVATION_DECRYPT key derivation and decryption
  698. * @retval None
  699. */
  700. #define __HAL_CRYP_SET_OPERATINGMODE(__OPERATING_MODE__) MODIFY_REG(AES->CR, AES_CR_MODE, (__OPERATING_MODE__))
  701. /**
  702. * @brief Set the algorithm chaining mode.
  703. * @param __CHAINING_MODE__ specifies the chaining mode
  704. * This parameter can be one of the following values:
  705. * @arg @ref CRYP_CHAINMODE_AES_ECB Electronic CodeBook
  706. * @arg @ref CRYP_CHAINMODE_AES_CBC Cipher Block Chaining
  707. * @arg @ref CRYP_CHAINMODE_AES_CTR CounTeR mode
  708. * @arg @ref CRYP_CHAINMODE_AES_GCM_GMAC Galois Counter Mode or Galois Message Authentication Code
  709. * @arg @ref CRYP_CHAINMODE_AES_CMAC Cipher Message Authentication Code (or Counter with Cipher Mode when applicable)
  710. * @retval None
  711. */
  712. #define __HAL_CRYP_SET_CHAININGMODE(__CHAINING_MODE__) MODIFY_REG(AES->CR, AES_CR_CHMOD, (__CHAINING_MODE__))
  713. /** @brief Check whether the specified CRYP status flag is set or not.
  714. * @param __FLAG__ specifies the flag to check.
  715. * This parameter can be one of the following values:
  716. * @arg @ref CRYP_FLAG_BUSY GCM process suspension forbidden
  717. * @arg @ref CRYP_IT_WRERR Write Error
  718. * @arg @ref CRYP_IT_RDERR Read Error
  719. * @arg @ref CRYP_IT_CCF Computation Complete
  720. * @retval The state of __FLAG__ (TRUE or FALSE).
  721. */
  722. #define __HAL_CRYP_GET_FLAG(__FLAG__) ((AES->SR & (__FLAG__)) == (__FLAG__))
  723. /** @brief Clear the CRYP pending status flag.
  724. * @param __FLAG__ specifies the flag to clear.
  725. * This parameter can be one of the following values:
  726. * @arg @ref CRYP_ERR_CLEAR Read (RDERR) or Write Error (WRERR) Flag Clear
  727. * @arg @ref CRYP_CCF_CLEAR Computation Complete Flag (CCF) Clear
  728. * @retval None
  729. */
  730. #define __HAL_CRYP_CLEAR_FLAG(__FLAG__) SET_BIT(AES->CR, (__FLAG__))
  731. /** @brief Check whether the specified CRYP interrupt source is enabled or not.
  732. * @param __INTERRUPT__ CRYP interrupt source to check
  733. * This parameter can be one of the following values:
  734. * @arg @ref CRYP_IT_ERRIE Error interrupt (used for RDERR and WRERR)
  735. * @arg @ref CRYP_IT_CCFIE Computation Complete interrupt
  736. * @retval State of interruption (TRUE or FALSE).
  737. */
  738. #define __HAL_CRYP_GET_IT_SOURCE(__INTERRUPT__) ((AES->CR & (__INTERRUPT__)) == (__INTERRUPT__))
  739. /** @brief Check whether the specified CRYP interrupt is set or not.
  740. * @param __INTERRUPT__ specifies the interrupt to check.
  741. * This parameter can be one of the following values:
  742. * @arg @ref CRYP_IT_WRERR Write Error
  743. * @arg @ref CRYP_IT_RDERR Read Error
  744. * @arg @ref CRYP_IT_CCF Computation Complete
  745. * @retval The state of __INTERRUPT__ (TRUE or FALSE).
  746. */
  747. #define __HAL_CRYP_GET_IT(__INTERRUPT__) ((AES->SR & (__INTERRUPT__)) == (__INTERRUPT__))
  748. /** @brief Clear the CRYP pending interrupt.
  749. * @param __INTERRUPT__ specifies the IT to clear.
  750. * This parameter can be one of the following values:
  751. * @arg @ref CRYP_ERR_CLEAR Read (RDERR) or Write Error (WRERR) Flag Clear
  752. * @arg @ref CRYP_CCF_CLEAR Computation Complete Flag (CCF) Clear
  753. * @retval None
  754. */
  755. #define __HAL_CRYP_CLEAR_IT(__INTERRUPT__) SET_BIT(AES->CR, (__INTERRUPT__))
  756. /**
  757. * @brief Enable the CRYP interrupt.
  758. * @param __INTERRUPT__ CRYP Interrupt.
  759. * This parameter can be one of the following values:
  760. * @arg @ref CRYP_IT_ERRIE Error interrupt (used for RDERR and WRERR)
  761. * @arg @ref CRYP_IT_CCFIE Computation Complete interrupt
  762. * @retval None
  763. */
  764. #define __HAL_CRYP_ENABLE_IT(__INTERRUPT__) ((AES->CR) |= (__INTERRUPT__))
  765. /**
  766. * @brief Disable the CRYP interrupt.
  767. * @param __INTERRUPT__ CRYP Interrupt.
  768. * This parameter can be one of the following values:
  769. * @arg @ref CRYP_IT_ERRIE Error interrupt (used for RDERR and WRERR)
  770. * @arg @ref CRYP_IT_CCFIE Computation Complete interrupt
  771. * @retval None
  772. */
  773. #define __HAL_CRYP_DISABLE_IT(__INTERRUPT__) ((AES->CR) &= ~(__INTERRUPT__))
  774. /**
  775. * @}
  776. */
  777. /* Private macros --------------------------------------------------------*/
  778. /** @addtogroup CRYP_Private_Macros CRYP Private Macros
  779. * @{
  780. */
  781. /**
  782. * @brief Verify the key size length.
  783. * @param __KEYSIZE__ Ciphering/deciphering algorithm key size.
  784. * @retval SET (__KEYSIZE__ is a valid value) or RESET (__KEYSIZE__ is invalid)
  785. */
  786. #define IS_CRYP_KEYSIZE(__KEYSIZE__) (((__KEYSIZE__) == CRYP_KEYSIZE_128B) || \
  787. ((__KEYSIZE__) == CRYP_KEYSIZE_256B))
  788. /**
  789. * @brief Verify the input data type.
  790. * @param __DATATYPE__ Ciphering/deciphering algorithm input data type.
  791. * @retval SET (__DATATYPE__ is valid) or RESET (__DATATYPE__ is invalid)
  792. */
  793. #define IS_CRYP_DATATYPE(__DATATYPE__) (((__DATATYPE__) == CRYP_DATATYPE_32B) || \
  794. ((__DATATYPE__) == CRYP_DATATYPE_16B) || \
  795. ((__DATATYPE__) == CRYP_DATATYPE_8B) || \
  796. ((__DATATYPE__) == CRYP_DATATYPE_1B))
  797. /**
  798. * @brief Verify the CRYP AES IP running mode.
  799. * @param __MODE__ CRYP AES IP running mode.
  800. * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
  801. */
  802. #define IS_CRYP_AES(__MODE__) (((__MODE__) == CRYP_AES_DISABLE) || \
  803. ((__MODE__) == CRYP_AES_ENABLE))
  804. /**
  805. * @brief Verify the selected CRYP algorithm.
  806. * @param __ALGOMODE__ Selected CRYP algorithm (ciphering, deciphering, key derivation or a combination of the latter).
  807. * @retval SET (__ALGOMODE__ is valid) or RESET (__ALGOMODE__ is invalid)
  808. */
  809. #define IS_CRYP_ALGOMODE(__ALGOMODE__) (((__ALGOMODE__) == CRYP_ALGOMODE_ENCRYPT) || \
  810. ((__ALGOMODE__) == CRYP_ALGOMODE_KEYDERIVATION) || \
  811. ((__ALGOMODE__) == CRYP_ALGOMODE_DECRYPT) || \
  812. ((__ALGOMODE__) == CRYP_ALGOMODE_TAG_GENERATION) || \
  813. ((__ALGOMODE__) == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT))
  814. /**
  815. * @brief Verify the selected CRYP chaining algorithm.
  816. * @param __CHAINMODE__ Selected CRYP chaining algorithm.
  817. * @retval SET (__CHAINMODE__ is valid) or RESET (__CHAINMODE__ is invalid)
  818. */
  819. #if defined(AES_CR_NPBLB)
  820. #define IS_CRYP_CHAINMODE(__CHAINMODE__) (((__CHAINMODE__) == CRYP_CHAINMODE_AES_ECB) || \
  821. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CBC) || \
  822. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CTR) || \
  823. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_GCM_GMAC) || \
  824. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CCM_CMAC))
  825. #else
  826. #define IS_CRYP_CHAINMODE(__CHAINMODE__) (((__CHAINMODE__) == CRYP_CHAINMODE_AES_ECB) || \
  827. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CBC) || \
  828. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CTR) || \
  829. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_GCM_GMAC) || \
  830. ((__CHAINMODE__) == CRYP_CHAINMODE_AES_CMAC))
  831. #endif
  832. /**
  833. * @brief Verify the deciphering key write option.
  834. * @param __WRITE__ deciphering key write option.
  835. * @retval SET (__WRITE__ is valid) or RESET (__WRITE__ is invalid)
  836. */
  837. #define IS_CRYP_WRITE(__WRITE__) (((__WRITE__) == CRYP_KEY_WRITE_ENABLE) || \
  838. ((__WRITE__) == CRYP_KEY_WRITE_DISABLE))
  839. /**
  840. * @brief Verify the CRYP input data DMA mode.
  841. * @param __MODE__ CRYP input data DMA mode.
  842. * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
  843. */
  844. #define IS_CRYP_DMAIN(__MODE__) (((__MODE__) == CRYP_DMAIN_DISABLE) || \
  845. ((__MODE__) == CRYP_DMAIN_ENABLE))
  846. /**
  847. * @brief Verify the CRYP output data DMA mode.
  848. * @param __MODE__ CRYP output data DMA mode.
  849. * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
  850. */
  851. #define IS_CRYP_DMAOUT(__MODE__) (((__MODE__) == CRYP_DMAOUT_DISABLE) || \
  852. ((__MODE__) == CRYP_DMAOUT_ENABLE))
  853. /**
  854. * @brief Verify the CRYP AES ciphering/deciphering/authentication algorithm phase.
  855. * @param __PHASE__ CRYP AES ciphering/deciphering/authentication algorithm phase.
  856. * @retval SET (__PHASE__ is valid) or RESET (__PHASE__ is invalid)
  857. */
  858. #define IS_CRYP_GCMCMAC_PHASE(__PHASE__) (((__PHASE__) == CRYP_GCM_INIT_PHASE) || \
  859. ((__PHASE__) == CRYP_GCMCMAC_HEADER_PHASE) || \
  860. ((__PHASE__) == CRYP_GCM_PAYLOAD_PHASE) || \
  861. ((__PHASE__) == CRYP_GCMCMAC_FINAL_PHASE))
  862. /**
  863. * @}
  864. */
  865. /* Include CRYP HAL Extended module */
  866. #include "stm32f4xx_hal_cryp_ex.h"
  867. /* Exported functions --------------------------------------------------------*/
  868. /** @addtogroup CRYP_Exported_Functions CRYP Exported Functions
  869. * @{
  870. */
  871. /** @addtogroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions
  872. * @{
  873. */
  874. /* Initialization/de-initialization functions ********************************/
  875. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp);
  876. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp);
  877. /* MSP initialization/de-initialization functions ****************************/
  878. void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp);
  879. void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp);
  880. /**
  881. * @}
  882. */
  883. /** @addtogroup CRYP_Exported_Functions_Group2 AES processing functions
  884. * @{
  885. */
  886. /* AES encryption/decryption processing functions ****************************/
  887. /* AES encryption/decryption using polling ***********************************/
  888. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  889. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  890. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  891. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  892. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
  893. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
  894. /* AES encryption/decryption using interrupt *********************************/
  895. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  896. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  897. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  898. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  899. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  900. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  901. /* AES encryption/decryption using DMA ***************************************/
  902. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  903. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  904. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  905. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  906. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
  907. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
  908. /**
  909. * @}
  910. */
  911. /** @addtogroup CRYP_Exported_Functions_Group3 Callback functions
  912. * @{
  913. */
  914. /* CallBack functions ********************************************************/
  915. void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp);
  916. void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp);
  917. void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp);
  918. /**
  919. * @}
  920. */
  921. /** @addtogroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
  922. * @{
  923. */
  924. /* AES interrupt handling function *******************************************/
  925. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp);
  926. /**
  927. * @}
  928. */
  929. /** @addtogroup CRYP_Exported_Functions_Group5 Peripheral State functions
  930. * @{
  931. */
  932. /* Peripheral State functions ************************************************/
  933. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp);
  934. uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp);
  935. /**
  936. * @}
  937. */
  938. /**
  939. * @}
  940. */
  941. #endif /* AES */
  942. /**
  943. * @}
  944. */
  945. /**
  946. * @}
  947. */
  948. #ifdef __cplusplus
  949. }
  950. #endif
  951. #endif /* __STM32F4xx_HAL_CRYP_H */
  952. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/