2 ******************************************************************************
3 * @file stm32h7xx_hal_cryp.h
4 * @author MCD Application Team
5 * @brief Header file of CRYP HAL module.
6 ******************************************************************************
9 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
10 * All rights reserved.</center></h2>
12 * This software component is licensed by ST under BSD 3-Clause license,
13 * the "License"; You may not use this file except in compliance with the
14 * License. You may obtain a copy of the License at:
15 * opensource.org/licenses/BSD-3-Clause
17 ******************************************************************************
20 /* Define to prevent recursive inclusion -------------------------------------*/
21 #ifndef STM32H7xx_HAL_CRYP_H
22 #define STM32H7xx_HAL_CRYP_H
29 /* Includes ------------------------------------------------------------------*/
30 #include "stm32h7xx_hal_def.h"
32 /** @addtogroup STM32H7xx_HAL_Driver
40 /* Exported types ------------------------------------------------------------*/
41 /** @defgroup CRYP_Exported_Types CRYP Exported Types
46 * @brief CRYP Init Structure definition
51 uint32_t DataType
; /*!< 32-bit data, 16-bit data, 8-bit data or 1-bit string.
52 This parameter can be a value of @ref CRYP_Data_Type */
53 uint32_t KeySize
; /*!< Used only in AES mode : 128, 192 or 256 bit key length in CRYP1.
54 This parameter can be a value of @ref CRYP_Key_Size */
55 uint32_t *pKey
; /*!< The key used for encryption/decryption */
56 uint32_t *pInitVect
; /*!< The initialization vector used also as initialization
57 counter in CTR mode */
58 uint32_t Algorithm
; /*!< DES/ TDES Algorithm ECB/CBC
59 AES Algorithm ECB/CBC/CTR/GCM or CCM
60 This parameter can be a value of @ref CRYP_Algorithm_Mode */
61 uint32_t *Header
; /*!< used only in AES GCM and CCM Algorithm for authentication,
62 GCM : also known as Additional Authentication Data
63 CCM : named B1 composed of the associated data length and Associated Data. */
64 uint32_t HeaderSize
; /*!< The size of header buffer in word */
65 uint32_t *B0
; /*!< B0 is first authentication block used only in AES CCM mode */
66 uint32_t DataWidthUnit
; /*!< Data With Unit, this parameter can be value of @ref CRYP_Data_Width_Unit*/
67 uint32_t KeyIVConfigSkip
; /*!< CRYP peripheral Key and IV configuration skip, to configure Key and Initialization
68 Vector only once and to skip configuration for consecutive processing.
69 This parameter can be a value of @ref CRYP_Configuration_Skip */
75 * @brief CRYP State Structure definition
80 HAL_CRYP_STATE_RESET
= 0x00U
, /*!< CRYP not yet initialized or disabled */
81 HAL_CRYP_STATE_READY
= 0x01U
, /*!< CRYP initialized and ready for use */
82 HAL_CRYP_STATE_BUSY
= 0x02U
/*!< CRYP BUSY, internal processing is ongoing */
83 } HAL_CRYP_STATETypeDef
;
87 * @brief CRYP handle Structure definition
90 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
91 typedef struct __CRYP_HandleTypeDef
94 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
97 CRYP_TypeDef
*Instance
; /*!< CRYP registers base address */
99 CRYP_ConfigTypeDef Init
; /*!< CRYP required parameters */
101 uint32_t *pCrypInBuffPtr
; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
103 uint32_t *pCrypOutBuffPtr
; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
105 __IO
uint16_t CrypHeaderCount
; /*!< Counter of header data */
107 __IO
uint16_t CrypInCount
; /*!< Counter of input data */
109 __IO
uint16_t CrypOutCount
; /*!< Counter of output data */
111 uint16_t Size
; /*!< length of input data in word or in byte, according to DataWidthUnit */
113 uint32_t Phase
; /*!< CRYP peripheral phase */
115 DMA_HandleTypeDef
*hdmain
; /*!< CRYP In DMA handle parameters */
117 DMA_HandleTypeDef
*hdmaout
; /*!< CRYP Out DMA handle parameters */
119 HAL_LockTypeDef Lock
; /*!< CRYP locking object */
121 __IO HAL_CRYP_STATETypeDef State
; /*!< CRYP peripheral state */
123 __IO
uint32_t ErrorCode
; /*!< CRYP peripheral error code */
125 uint32_t Version
; /*!< CRYP1 IP version*/
127 uint32_t KeyIVConfig
; /*!< CRYP peripheral Key and IV configuration flag, used when
128 configuration can be skipped */
130 uint32_t SizesSum
; /*!< Sum of successive payloads lengths (in bytes), stored
131 for a single signature computation after several
132 messages processing */
134 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
135 void (*InCpltCallback
)(struct __CRYP_HandleTypeDef
*hcryp
); /*!< CRYP Input FIFO transfer completed callback */
136 void (*OutCpltCallback
)(struct __CRYP_HandleTypeDef
*hcryp
); /*!< CRYP Output FIFO transfer completed callback */
137 void (*ErrorCallback
)(struct __CRYP_HandleTypeDef
*hcryp
); /*!< CRYP Error callback */
139 void (* MspInitCallback
)(struct __CRYP_HandleTypeDef
*hcryp
); /*!< CRYP Msp Init callback */
140 void (* MspDeInitCallback
)(struct __CRYP_HandleTypeDef
*hcryp
); /*!< CRYP Msp DeInit callback */
142 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
144 } CRYP_HandleTypeDef
;
151 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
152 /** @defgroup HAL_CRYP_Callback_ID_enumeration_definition HAL CRYP Callback ID enumeration definition
153 * @brief HAL CRYP Callback ID enumeration definition
158 HAL_CRYP_INPUT_COMPLETE_CB_ID
= 0x01U
, /*!< CRYP Input FIFO transfer completed callback ID */
159 HAL_CRYP_OUTPUT_COMPLETE_CB_ID
= 0x02U
, /*!< CRYP Output FIFO transfer completed callback ID */
160 HAL_CRYP_ERROR_CB_ID
= 0x03U
, /*!< CRYP Error callback ID */
162 HAL_CRYP_MSPINIT_CB_ID
= 0x04U
, /*!< CRYP MspInit callback ID */
163 HAL_CRYP_MSPDEINIT_CB_ID
= 0x05U
/*!< CRYP MspDeInit callback ID */
165 } HAL_CRYP_CallbackIDTypeDef
;
170 /** @defgroup HAL_CRYP_Callback_pointer_definition HAL CRYP Callback pointer definition
171 * @brief HAL CRYP Callback pointer definition
175 typedef void (*pCRYP_CallbackTypeDef
)(CRYP_HandleTypeDef
*hcryp
); /*!< pointer to a common CRYP callback function */
181 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
183 /* Exported constants --------------------------------------------------------*/
184 /** @defgroup CRYP_Exported_Constants CRYP Exported Constants
188 /** @defgroup CRYP_Error_Definition CRYP Error Definition
191 #define HAL_CRYP_ERROR_NONE 0x00000000U /*!< No error */
192 #define HAL_CRYP_ERROR_WRITE 0x00000001U /*!< Write error */
193 #define HAL_CRYP_ERROR_READ 0x00000002U /*!< Read error */
194 #define HAL_CRYP_ERROR_DMA 0x00000004U /*!< DMA error */
195 #define HAL_CRYP_ERROR_BUSY 0x00000008U /*!< Busy flag error */
196 #define HAL_CRYP_ERROR_TIMEOUT 0x00000010U /*!< Timeout error */
197 #define HAL_CRYP_ERROR_NOT_SUPPORTED 0x00000020U /*!< Not supported mode */
198 #define HAL_CRYP_ERROR_AUTH_TAG_SEQUENCE 0x00000040U /*!< Sequence are not respected only for GCM or CCM */
199 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
200 #define HAL_CRYP_ERROR_INVALID_CALLBACK ((uint32_t)0x00000080U) /*!< Invalid Callback error */
201 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
208 /** @defgroup CRYP_Data_Width_Unit CRYP Data Width Unit
212 #define CRYP_DATAWIDTHUNIT_WORD 0x00000000U /*!< By default, size unit is word */
213 #define CRYP_DATAWIDTHUNIT_BYTE 0x00000001U /*!< By default, size unit is word */
219 /** @defgroup CRYP_Algorithm_Mode CRYP Algorithm Mode
223 #define CRYP_DES_ECB CRYP_CR_ALGOMODE_DES_ECB
224 #define CRYP_DES_CBC CRYP_CR_ALGOMODE_DES_CBC
225 #define CRYP_TDES_ECB CRYP_CR_ALGOMODE_TDES_ECB
226 #define CRYP_TDES_CBC CRYP_CR_ALGOMODE_TDES_CBC
227 #define CRYP_AES_ECB CRYP_CR_ALGOMODE_AES_ECB
228 #define CRYP_AES_CBC CRYP_CR_ALGOMODE_AES_CBC
229 #define CRYP_AES_CTR CRYP_CR_ALGOMODE_AES_CTR
230 #define CRYP_AES_GCM CRYP_CR_ALGOMODE_AES_GCM
231 #define CRYP_AES_CCM CRYP_CR_ALGOMODE_AES_CCM
237 /** @defgroup CRYP_Key_Size CRYP Key Size
241 #define CRYP_KEYSIZE_128B 0x00000000U
242 #define CRYP_KEYSIZE_192B CRYP_CR_KEYSIZE_0
243 #define CRYP_KEYSIZE_256B CRYP_CR_KEYSIZE_1
249 /** @defgroup CRYP_Data_Type CRYP Data Type
253 #define CRYP_DATATYPE_32B 0x00000000U
254 #define CRYP_DATATYPE_16B CRYP_CR_DATATYPE_0
255 #define CRYP_DATATYPE_8B CRYP_CR_DATATYPE_1
256 #define CRYP_DATATYPE_1B CRYP_CR_DATATYPE
262 /** @defgroup CRYP_Interrupt CRYP Interrupt
266 #define CRYP_IT_INI CRYP_IMSCR_INIM /*!< Input FIFO Interrupt */
267 #define CRYP_IT_OUTI CRYP_IMSCR_OUTIM /*!< Output FIFO Interrupt */
273 /** @defgroup CRYP_Flags CRYP Flags
277 /* Flags in the SR register */
278 #define CRYP_FLAG_IFEM CRYP_SR_IFEM /*!< Input FIFO is empty */
279 #define CRYP_FLAG_IFNF CRYP_SR_IFNF /*!< Input FIFO is not Full */
280 #define CRYP_FLAG_OFNE CRYP_SR_OFNE /*!< Output FIFO is not empty */
281 #define CRYP_FLAG_OFFU CRYP_SR_OFFU /*!< Output FIFO is Full */
282 #define CRYP_FLAG_BUSY CRYP_SR_BUSY /*!< The CRYP core is currently processing a block of data
283 or a key preparation (for AES decryption). */
284 /* Flags in the RISR register */
285 #define CRYP_FLAG_OUTRIS 0x01000002U /*!< Output FIFO service raw interrupt status */
286 #define CRYP_FLAG_INRIS 0x01000001U /*!< Input FIFO service raw interrupt status*/
292 /** @defgroup CRYP_Configuration_Skip CRYP Key and IV Configuration Skip Mode
296 #define CRYP_KEYIVCONFIG_ALWAYS 0x00000000U /*!< Peripheral Key and IV configuration to do systematically */
297 #define CRYP_KEYIVCONFIG_ONCE 0x00000001U /*!< Peripheral Key and IV configuration to do only once */
307 /* Exported macros -----------------------------------------------------------*/
308 /** @defgroup CRYP_Exported_Macros CRYP Exported Macros
312 /** @brief Reset CRYP handle state
313 * @param __HANDLE__ specifies the CRYP handle.
316 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
317 #define __HAL_CRYP_RESET_HANDLE_STATE(__HANDLE__) do{\
318 (__HANDLE__)->State = HAL_CRYP_STATE_RESET;\
319 (__HANDLE__)->MspInitCallback = NULL;\
320 (__HANDLE__)->MspDeInitCallback = NULL;\
323 #define __HAL_CRYP_RESET_HANDLE_STATE(__HANDLE__) ( (__HANDLE__)->State = HAL_CRYP_STATE_RESET)
324 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
327 * @brief Enable/Disable the CRYP peripheral.
328 * @param __HANDLE__: specifies the CRYP handle.
332 #define __HAL_CRYP_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR |= CRYP_CR_CRYPEN)
333 #define __HAL_CRYP_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~CRYP_CR_CRYPEN)
335 /** @brief Check whether the specified CRYP status flag is set or not.
336 * @param __FLAG__: specifies the flag to check.
337 * This parameter can be one of the following values for CRYP:
338 * @arg CRYP_FLAG_BUSY: The CRYP core is currently processing a block of data
339 * or a key preparation (for AES decryption).
340 * @arg CRYP_FLAG_IFEM: Input FIFO is empty
341 * @arg CRYP_FLAG_IFNF: Input FIFO is not full
342 * @arg CRYP_FLAG_INRIS: Input FIFO service raw interrupt is pending
343 * @arg CRYP_FLAG_OFNE: Output FIFO is not empty
344 * @arg CRYP_FLAG_OFFU: Output FIFO is full
345 * @arg CRYP_FLAG_OUTRIS: Input FIFO service raw interrupt is pending
346 * @retval The state of __FLAG__ (TRUE or FALSE).
348 #define CRYP_FLAG_MASK 0x0000001FU
350 #define __HAL_CRYP_GET_FLAG(__HANDLE__, __FLAG__) ((((uint8_t)((__FLAG__) >> 24)) == 0x01U)?((((__HANDLE__)->Instance->RISR) & ((__FLAG__) & CRYP_FLAG_MASK)) == ((__FLAG__) & CRYP_FLAG_MASK)): \
351 ((((__HANDLE__)->Instance->RISR) & ((__FLAG__) & CRYP_FLAG_MASK)) == ((__FLAG__) & CRYP_FLAG_MASK)))
353 /** @brief Check whether the specified CRYP interrupt is set or not.
354 * @param __HANDLE__: specifies the CRYP handle.
355 * @param __INTERRUPT__: specifies the interrupt to check.
356 * This parameter can be one of the following values for CRYP:
357 * @arg CRYP_IT_INI: Input FIFO service masked interrupt status
358 * @arg CRYP_IT_OUTI: Output FIFO service masked interrupt status
359 * @retval The state of __INTERRUPT__ (TRUE or FALSE).
362 #define __HAL_CRYP_GET_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->MISR & (__INTERRUPT__)) == (__INTERRUPT__))
365 * @brief Enable the CRYP interrupt.
366 * @param __HANDLE__: specifies the CRYP handle.
367 * @param __INTERRUPT__: CRYP Interrupt.
368 * This parameter can be one of the following values for CRYP:
369 * @ CRYP_IT_INI : Input FIFO service interrupt mask.
370 * @ CRYP_IT_OUTI : Output FIFO service interrupt mask.CRYP interrupt.
374 #define __HAL_CRYP_ENABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IMSCR) |= (__INTERRUPT__))
377 * @brief Disable the CRYP interrupt.
378 * @param __HANDLE__: specifies the CRYP handle.
379 * @param __INTERRUPT__: CRYP Interrupt.
380 * This parameter can be one of the following values for CRYP:
381 * @ CRYP_IT_INI : Input FIFO service interrupt mask.
382 * @ CRYP_IT_OUTI : Output FIFO service interrupt mask.CRYP interrupt.
386 #define __HAL_CRYP_DISABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IMSCR) &= ~(__INTERRUPT__))
392 /* Include CRYP HAL Extended module */
393 #include "stm32h7xx_hal_cryp_ex.h"
395 /* Exported functions --------------------------------------------------------*/
396 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
400 /** @addtogroup CRYP_Exported_Functions_Group1
403 HAL_StatusTypeDef
HAL_CRYP_Init(CRYP_HandleTypeDef
*hcryp
);
404 HAL_StatusTypeDef
HAL_CRYP_DeInit(CRYP_HandleTypeDef
*hcryp
);
405 void HAL_CRYP_MspInit(CRYP_HandleTypeDef
*hcryp
);
406 void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef
*hcryp
);
407 HAL_StatusTypeDef
HAL_CRYP_SetConfig(CRYP_HandleTypeDef
*hcryp
, CRYP_ConfigTypeDef
*pConf
);
408 HAL_StatusTypeDef
HAL_CRYP_GetConfig(CRYP_HandleTypeDef
*hcryp
, CRYP_ConfigTypeDef
*pConf
);
409 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
410 HAL_StatusTypeDef
HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef
*hcryp
, HAL_CRYP_CallbackIDTypeDef CallbackID
,
411 pCRYP_CallbackTypeDef pCallback
);
412 HAL_StatusTypeDef
HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef
*hcryp
, HAL_CRYP_CallbackIDTypeDef CallbackID
);
413 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
418 /** @addtogroup CRYP_Exported_Functions_Group2
422 /* encryption/decryption ***********************************/
423 HAL_StatusTypeDef
HAL_CRYP_Encrypt(CRYP_HandleTypeDef
*hcryp
, uint32_t *Input
, uint16_t Size
, uint32_t *Output
,
425 HAL_StatusTypeDef
HAL_CRYP_Decrypt(CRYP_HandleTypeDef
*hcryp
, uint32_t *Input
, uint16_t Size
, uint32_t *Output
,
427 HAL_StatusTypeDef
HAL_CRYP_Encrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint32_t *Input
, uint16_t Size
, uint32_t *Output
);
428 HAL_StatusTypeDef
HAL_CRYP_Decrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint32_t *Input
, uint16_t Size
, uint32_t *Output
);
429 HAL_StatusTypeDef
HAL_CRYP_Encrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint32_t *Input
, uint16_t Size
, uint32_t *Output
);
430 HAL_StatusTypeDef
HAL_CRYP_Decrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint32_t *Input
, uint16_t Size
, uint32_t *Output
);
437 /** @addtogroup CRYP_Exported_Functions_Group3
440 /* Interrupt Handler functions **********************************************/
441 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef
*hcryp
);
442 HAL_CRYP_STATETypeDef
HAL_CRYP_GetState(CRYP_HandleTypeDef
*hcryp
);
443 void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef
*hcryp
);
444 void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef
*hcryp
);
445 void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef
*hcryp
);
446 uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef
*hcryp
);
456 /* Private macros --------------------------------------------------------*/
457 /** @defgroup CRYP_Private_Macros CRYP Private Macros
461 /** @defgroup CRYP_IS_CRYP_Definitions CRYP Private macros to check input parameters
465 #define IS_CRYP_ALGORITHM(ALGORITHM) (((ALGORITHM) == CRYP_DES_ECB) || \
466 ((ALGORITHM) == CRYP_DES_CBC) || \
467 ((ALGORITHM) == CRYP_TDES_ECB) || \
468 ((ALGORITHM) == CRYP_TDES_CBC) || \
469 ((ALGORITHM) == CRYP_AES_ECB) || \
470 ((ALGORITHM) == CRYP_AES_CBC) || \
471 ((ALGORITHM) == CRYP_AES_CTR) || \
472 ((ALGORITHM) == CRYP_AES_GCM) || \
473 ((ALGORITHM) == CRYP_AES_CCM))
475 #define IS_CRYP_KEYSIZE(KEYSIZE)(((KEYSIZE) == CRYP_KEYSIZE_128B) || \
476 ((KEYSIZE) == CRYP_KEYSIZE_192B) || \
477 ((KEYSIZE) == CRYP_KEYSIZE_256B))
479 #define IS_CRYP_DATATYPE(DATATYPE)(((DATATYPE) == CRYP_DATATYPE_32B) || \
480 ((DATATYPE) == CRYP_DATATYPE_16B) || \
481 ((DATATYPE) == CRYP_DATATYPE_8B) || \
482 ((DATATYPE) == CRYP_DATATYPE_1B))
484 #define IS_CRYP_INIT(CONFIG)(((CONFIG) == CRYP_KEYIVCONFIG_ALWAYS) || \
485 ((CONFIG) == CRYP_KEYIVCONFIG_ONCE))
496 /* Private constants ---------------------------------------------------------*/
497 /** @defgroup CRYP_Private_Constants CRYP Private Constants
504 /* Private defines -----------------------------------------------------------*/
505 /** @defgroup CRYP_Private_Defines CRYP Private Defines
513 /* Private variables ---------------------------------------------------------*/
514 /** @defgroup CRYP_Private_Variables CRYP Private Variables
521 /* Private functions prototypes ----------------------------------------------*/
522 /** @defgroup CRYP_Private_Functions_Prototypes CRYP Private Functions Prototypes
530 /* Private functions ---------------------------------------------------------*/
531 /** @defgroup CRYP_Private_Functions CRYP Private Functions
554 #endif /* STM32H7xx_HAL_CRYP_H */
556 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/