stm32h7xx_hal_cryp.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_cryp.h
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 21-April-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 __STM32H7xx_HAL_CRYP_H
  39. #define __STM32H7xx_HAL_CRYP_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32h7xx_hal_def.h"
  45. /** @addtogroup STM32H7xx_HAL_Driver
  46. * @{
  47. */
  48. #if defined (CRYP)
  49. /** @addtogroup CRYP
  50. * @{
  51. */
  52. /* Exported types ------------------------------------------------------------*/
  53. /** @defgroup CRYP_Exported_Types CRYP Exported Types
  54. * @{
  55. */
  56. /**
  57. * @brief CRYP Init 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; /*!< Used only in AES mode : 128, 192 or 256 bit key length in CRYP1.
  64. This parameter can be a value of @ref CRYP_Key_Size */
  65. uint32_t* pKey; /*!< The key used for encryption/decryption */
  66. uint32_t* pInitVect; /*!< The initialization vector used also as initialization
  67. counter in CTR mode */
  68. uint32_t Algorithm; /*!< DES/ TDES Algorithm ECB/CBC
  69. AES Algorithm ECB/CBC/CTR/GCM or CCM
  70. This parameter can be a value of @ref CRYP_Algorithm_Mode */
  71. uint32_t* Header; /*!< used only in AES GCM and CCM Algorithm for authentication,
  72. GCM : also known as Additional Authentication Data
  73. CCM : named B1 composed of the associated data length and Associated Data. */
  74. uint32_t HeaderSize; /*!< The size of header buffer in word */
  75. uint32_t* B0; /*!< B0 is first authentication block used only in AES CCM mode */
  76. }CRYP_ConfigTypeDef;
  77. /**
  78. * @brief CRYP State Structure definition
  79. */
  80. typedef enum
  81. {
  82. HAL_CRYP_STATE_RESET = 0x00U, /*!< CRYP not yet initialized or disabled */
  83. HAL_CRYP_STATE_READY = 0x01U, /*!< CRYP initialized and ready for use */
  84. HAL_CRYP_STATE_BUSY = 0x02U /*!< CRYP BUSY, internal processing is ongoing */
  85. }HAL_CRYP_STATETypeDef;
  86. /**
  87. * @brief CRYP handle Structure definition
  88. */
  89. typedef struct
  90. {
  91. CRYP_TypeDef *Instance; /*!< CRYP registers base address */
  92. CRYP_ConfigTypeDef Init; /*!< CRYP required parameters */
  93. uint32_t *pCrypInBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
  94. uint32_t *pCrypOutBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
  95. __IO uint16_t CrypHeaderCount; /*!< Counter of header data */
  96. __IO uint16_t CrypInCount; /*!< Counter of input data */
  97. __IO uint16_t CrypOutCount; /*!< Counter of output data */
  98. uint16_t Size; /*!< length of input data in word */
  99. uint32_t Phase; /*!< CRYP peripheral phase */
  100. DMA_HandleTypeDef *hdmain; /*!< CRYP In DMA handle parameters */
  101. DMA_HandleTypeDef *hdmaout; /*!< CRYP Out DMA handle parameters */
  102. HAL_LockTypeDef Lock; /*!< CRYP locking object */
  103. __IO HAL_CRYP_STATETypeDef State; /*!< CRYP peripheral state */
  104. __IO uint32_t ErrorCode; /*!< CRYP peripheral error code */
  105. }CRYP_HandleTypeDef;
  106. /**
  107. * @}
  108. */
  109. /* Exported constants --------------------------------------------------------*/
  110. /** @defgroup CRYP_Exported_Constants CRYP Exported Constants
  111. * @{
  112. */
  113. /** @defgroup CRYP_Error_Definition CRYP Error Definition
  114. * @{
  115. */
  116. #define HAL_CRYP_ERROR_NONE 0x00000000U /*!< No error */
  117. #define HAL_CRYP_ERROR_WRITE 0x00000001U /*!< Write error */
  118. #define HAL_CRYP_ERROR_READ 0x00000002U /*!< Read error */
  119. #define HAL_CRYP_ERROR_DMA 0x00000004U /*!< DMA error */
  120. #define HAL_CRYP_ERROR_BUSY 0x00000008U /*!< Busy flag error */
  121. #define HAL_CRYP_ERROR_TIMEOUT 0x00000010U /*!< Timeout error */
  122. #define HAL_CRYP_ERROR_NOT_SUPPORTED 0x00000020U /*!< Not supported mode */
  123. #define HAL_CRYP_ERROR_AUTH_TAG_SEQUENCE 0x00000040U /*!< Sequence are not respected only for GCM or CCM */
  124. /**
  125. * @}
  126. */
  127. /** @defgroup CRYP_Algorithm_Mode CRYP Algorithm Mode
  128. * @{
  129. */
  130. #define CRYP_DES_ECB CRYP_CR_ALGOMODE_DES_ECB
  131. #define CRYP_DES_CBC CRYP_CR_ALGOMODE_DES_CBC
  132. #define CRYP_TDES_ECB CRYP_CR_ALGOMODE_TDES_ECB
  133. #define CRYP_TDES_CBC CRYP_CR_ALGOMODE_TDES_CBC
  134. #define CRYP_AES_ECB CRYP_CR_ALGOMODE_AES_ECB
  135. #define CRYP_AES_CBC CRYP_CR_ALGOMODE_AES_CBC
  136. #define CRYP_AES_CTR CRYP_CR_ALGOMODE_AES_CTR
  137. #define CRYP_AES_GCM CRYP_CR_ALGOMODE_AES_GCM
  138. #define CRYP_AES_CCM CRYP_CR_ALGOMODE_AES_CCM
  139. /**
  140. * @}
  141. */
  142. /** @defgroup CRYP_Key_Size CRYP Key Size
  143. * @{
  144. */
  145. #define CRYP_KEYSIZE_128B 0x00000000U
  146. #define CRYP_KEYSIZE_192B CRYP_CR_KEYSIZE_0
  147. #define CRYP_KEYSIZE_256B CRYP_CR_KEYSIZE_1
  148. /**
  149. * @}
  150. */
  151. /** @defgroup CRYP_Data_Type CRYP Data Type
  152. * @{
  153. */
  154. #define CRYP_DATATYPE_32B 0x00000000U
  155. #define CRYP_DATATYPE_16B CRYP_CR_DATATYPE_0
  156. #define CRYP_DATATYPE_8B CRYP_CR_DATATYPE_1
  157. #define CRYP_DATATYPE_1B CRYP_CR_DATATYPE
  158. /**
  159. * @}
  160. */
  161. /** @defgroup CRYP_Interrupt CRYP Interrupt
  162. * @{
  163. */
  164. #define CRYP_IT_INI CRYP_IMSCR_INIM /*!< Input FIFO Interrupt */
  165. #define CRYP_IT_OUTI CRYP_IMSCR_OUTIM /*!< Output FIFO Interrupt */
  166. /**
  167. * @}
  168. */
  169. /** @defgroup CRYP_Flags CRYP Flags
  170. * @{
  171. */
  172. /* Flags in the SR register */
  173. #define CRYP_FLAG_IFEM CRYP_SR_IFEM /*!< Input FIFO is empty */
  174. #define CRYP_FLAG_IFNF CRYP_SR_IFNF /*!< Input FIFO is not Full */
  175. #define CRYP_FLAG_OFNE CRYP_SR_OFNE /*!< Output FIFO is not empty */
  176. #define CRYP_FLAG_OFFU CRYP_SR_OFFU /*!< Output FIFO is Full */
  177. #define CRYP_FLAG_BUSY CRYP_SR_BUSY /*!< The CRYP core is currently processing a block of data
  178. or a key preparation (for AES decryption). */
  179. /* Flags in the RISR register */
  180. #define CRYP_FLAG_OUTRIS 0x01000002U /*!< Output FIFO service raw interrupt status */
  181. #define CRYP_FLAG_INRIS 0x01000001U /*!< Input FIFO service raw interrupt status*/
  182. /**
  183. * @}
  184. */
  185. /**
  186. * @}
  187. */
  188. /* Exported macros -----------------------------------------------------------*/
  189. /** @defgroup CRYP_Exported_Macros CRYP Exported Macros
  190. * @{
  191. */
  192. /**
  193. * @brief Enable/Disable the CRYP peripheral.
  194. * @param __HANDLE__: specifies the CRYP handle.
  195. * @retval None
  196. */
  197. #define __HAL_CRYP_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR |= CRYP_CR_CRYPEN)
  198. #define __HAL_CRYP_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~CRYP_CR_CRYPEN)
  199. /** @brief Check whether the specified CRYP status flag is set or not.
  200. * @param __FLAG__: specifies the flag to check.
  201. * This parameter can be one of the following values for CRYP:
  202. * @arg CRYP_FLAG_BUSY: The CRYP core is currently processing a block of data
  203. * or a key preparation (for AES decryption).
  204. * @arg CRYP_FLAG_IFEM: Input FIFO is empty
  205. * @arg CRYP_FLAG_IFNF: Input FIFO is not full
  206. * @arg CRYP_FLAG_INRIS: Input FIFO service raw interrupt is pending
  207. * @arg CRYP_FLAG_OFNE: Output FIFO is not empty
  208. * @arg CRYP_FLAG_OFFU: Output FIFO is full
  209. * @arg CRYP_FLAG_OUTRIS: Input FIFO service raw interrupt is pending
  210. * @retval The state of __FLAG__ (TRUE or FALSE).
  211. */
  212. #define CRYP_FLAG_MASK 0x0000001FU
  213. #define __HAL_CRYP_GET_FLAG(__HANDLE__, __FLAG__) ((((uint8_t)((__FLAG__) >> 24)) == 0x01U)?((((__HANDLE__)->Instance->RISR) & ((__FLAG__) & CRYP_FLAG_MASK)) == ((__FLAG__) & CRYP_FLAG_MASK)): \
  214. ((((__HANDLE__)->Instance->RISR) & ((__FLAG__) & CRYP_FLAG_MASK)) == ((__FLAG__) & CRYP_FLAG_MASK)))
  215. /** @brief Check whether the specified CRYP interrupt is set or not.
  216. * @param __HANDLE__: specifies the CRYP handle.
  217. * @param __INTERRUPT__: specifies the interrupt to check.
  218. * This parameter can be one of the following values for CRYP:
  219. * @arg CRYP_IT_INI: Input FIFO service masked interrupt status
  220. * @arg CRYP_IT_OUTI: Output FIFO service masked interrupt status
  221. * @retval The state of __INTERRUPT__ (TRUE or FALSE).
  222. */
  223. #define __HAL_CRYP_GET_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->MISR & (__INTERRUPT__)) == (__INTERRUPT__))
  224. /**
  225. * @brief Enable the CRYP interrupt.
  226. * @param __HANDLE__: specifies the CRYP handle.
  227. * @param __INTERRUPT__: CRYP Interrupt.
  228. * This parameter can be one of the following values for CRYP:
  229. * @ CRYP_IT_INI : Input FIFO service interrupt mask.
  230. * @ CRYP_IT_OUTI : Output FIFO service interrupt mask.CRYP interrupt.
  231. * @retval None
  232. */
  233. #define __HAL_CRYP_ENABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IMSCR) |= (__INTERRUPT__))
  234. /**
  235. * @brief Disable the CRYP interrupt.
  236. * @param __HANDLE__: specifies the CRYP handle.
  237. * @param __INTERRUPT__: CRYP Interrupt.
  238. * This parameter can be one of the following values for CRYP:
  239. * @ CRYP_IT_INI : Input FIFO service interrupt mask.
  240. * @ CRYP_IT_OUTI : Output FIFO service interrupt mask.CRYP interrupt.
  241. * @retval None
  242. */
  243. #define __HAL_CRYP_DISABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IMSCR) &= ~(__INTERRUPT__))
  244. /**
  245. * @}
  246. */
  247. /* Include CRYP HAL Extended module */
  248. #include "stm32h7xx_hal_cryp_ex.h"
  249. /* Exported functions --------------------------------------------------------*/
  250. /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
  251. * @{
  252. */
  253. /** @addtogroup CRYP_Exported_Functions_Group1
  254. * @{
  255. */
  256. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp);
  257. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp);
  258. void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp);
  259. void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp);
  260. HAL_StatusTypeDef HAL_CRYP_SetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf );
  261. HAL_StatusTypeDef HAL_CRYP_GetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf );
  262. /**
  263. * @}
  264. */
  265. /** @addtogroup CRYP_Exported_Functions_Group2
  266. * @{
  267. */
  268. /* encryption/decryption ***********************************/
  269. HAL_StatusTypeDef HAL_CRYP_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, uint32_t Timeout);
  270. HAL_StatusTypeDef HAL_CRYP_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, uint32_t Timeout);
  271. HAL_StatusTypeDef HAL_CRYP_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output);
  272. HAL_StatusTypeDef HAL_CRYP_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output);
  273. HAL_StatusTypeDef HAL_CRYP_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output);
  274. HAL_StatusTypeDef HAL_CRYP_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output);
  275. /**
  276. * @}
  277. */
  278. /** @addtogroup CRYP_Exported_Functions_Group3
  279. * @{
  280. */
  281. /* Interrupt Handler functions **********************************************/
  282. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp);
  283. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp);
  284. void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp);
  285. void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp);
  286. void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp);
  287. uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp);
  288. /**
  289. * @}
  290. */
  291. /**
  292. * @}
  293. */
  294. /* Private macros --------------------------------------------------------*/
  295. /** @defgroup CRYP_Private_Macros CRYP Private Macros
  296. * @{
  297. */
  298. /** @defgroup CRYP_IS_CRYP_Definitions CRYP Private macros to check input parameters
  299. * @{
  300. */
  301. #define IS_CRYP_ALGORITHM(ALGORITHM) (((ALGORITHM) == CRYP_DES_ECB) || \
  302. ((ALGORITHM) == CRYP_DES_CBC) || \
  303. ((ALGORITHM) == CRYP_TDES_ECB) || \
  304. ((ALGORITHM) == CRYP_TDES_CBC) || \
  305. ((ALGORITHM) == CRYP_AES_ECB) || \
  306. ((ALGORITHM) == CRYP_AES_CBC) || \
  307. ((ALGORITHM) == CRYP_AES_CTR) || \
  308. ((ALGORITHM) == CRYP_AES_GCM) || \
  309. ((ALGORITHM) == CRYP_AES_CCM))
  310. #define IS_CRYP_KEYSIZE(KEYSIZE)(((KEYSIZE) == CRYP_KEYSIZE_128B) || \
  311. ((KEYSIZE) == CRYP_KEYSIZE_192B) || \
  312. ((KEYSIZE) == CRYP_KEYSIZE_256B))
  313. #define IS_CRYP_DATATYPE(DATATYPE)(((DATATYPE) == CRYP_DATATYPE_32B) || \
  314. ((DATATYPE) == CRYP_DATATYPE_16B) || \
  315. ((DATATYPE) == CRYP_DATATYPE_8B) || \
  316. ((DATATYPE) == CRYP_DATATYPE_1B))
  317. /**
  318. * @}
  319. */
  320. /**
  321. * @}
  322. */
  323. /* Private constants ---------------------------------------------------------*/
  324. /** @defgroup CRYP_Private_Constants CRYP Private Constants
  325. * @{
  326. */
  327. /**
  328. * @}
  329. */
  330. /* Private defines -----------------------------------------------------------*/
  331. /** @defgroup CRYP_Private_Defines CRYP Private Defines
  332. * @{
  333. */
  334. /**
  335. * @}
  336. */
  337. /* Private variables ---------------------------------------------------------*/
  338. /** @defgroup CRYP_Private_Variables CRYP Private Variables
  339. * @{
  340. */
  341. /**
  342. * @}
  343. */
  344. /* Private functions prototypes ----------------------------------------------*/
  345. /** @defgroup CRYP_Private_Functions_Prototypes CRYP Private Functions Prototypes
  346. * @{
  347. */
  348. /**
  349. * @}
  350. */
  351. /* Private functions ---------------------------------------------------------*/
  352. /** @defgroup CRYP_Private_Functions CRYP Private Functions
  353. * @{
  354. */
  355. /**
  356. * @}
  357. */
  358. /**
  359. * @}
  360. */
  361. #endif /* CRYP */
  362. /**
  363. * @}
  364. */
  365. #ifdef __cplusplus
  366. }
  367. #endif
  368. #endif /* __STM32H7xx_HAL_CRYP_H */
  369. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/