1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_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) 2016 STMicroelectronics.
10   * All rights reserved.</center></h2>
11   *
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
16   *
17   ******************************************************************************
18   */
19 
20 /* Define to prevent recursive inclusion -------------------------------------*/
21 #ifndef __STM32F2xx_HAL_CRYP_H
22 #define __STM32F2xx_HAL_CRYP_H
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #if defined(CRYP)
29 
30 /* Includes ------------------------------------------------------------------*/
31 #include "stm32f2xx_hal_def.h"
32 
33 /** @addtogroup STM32F2xx_HAL_Driver
34   * @{
35   */
36 
37 /** @defgroup CRYP CRYP
38   * @brief CRYP HAL module driver.
39   * @{
40   */
41 
42 /* Exported types ------------------------------------------------------------*/
43 
44 /** @defgroup CRYP_Exported_Types CRYP Exported Types
45   * @{
46   */
47 
48 /**
49   * @brief CRYP Init Structure definition
50   */
51 
52 typedef struct
53 {
54   uint32_t DataType;                   /*!< 32-bit data, 16-bit data, 8-bit data or 1-bit string.
55                                         This parameter can be a value of @ref CRYP_Data_Type */
56   uint32_t KeySize;                    /*!< Used only in AES mode : 128, 192 or 256 bit key length in CRYP1.
57                                             This parameter can be a value of @ref CRYP_Key_Size */
58   uint32_t* pKey;                      /*!< The key used for encryption/decryption */
59   uint32_t* pInitVect;                 /*!< The initialization vector used also as initialization
60                                          counter in CTR mode */
61   uint32_t Algorithm;                  /*!<  DES/ TDES Algorithm ECB/CBC*/
62 
63   uint32_t DataWidthUnit;             /*!< Data With Unit, this parameter can be value of @ref CRYP_Data_Width_Unit*/
64 }CRYP_ConfigTypeDef;
65 
66 
67 /**
68   * @brief  CRYP State Structure definition
69   */
70 
71 typedef enum
72 {
73   HAL_CRYP_STATE_RESET             = 0x00U,  /*!< CRYP not yet initialized or disabled  */
74   HAL_CRYP_STATE_READY             = 0x01U,  /*!< CRYP initialized and ready for use    */
75   HAL_CRYP_STATE_BUSY              = 0x02U  /*!< CRYP BUSY, internal processing is ongoing  */
76 }HAL_CRYP_STATETypeDef;
77 
78 
79 /**
80   * @brief  CRYP handle Structure definition
81   */
82 
83 typedef struct __CRYP_HandleTypeDef
84 {
85 
86       CRYP_TypeDef                      *Instance;            /*!< CRYP registers base address */
87 
88       CRYP_ConfigTypeDef                Init;             /*!< CRYP required parameters */
89 
90       uint32_t                          *pCrypInBuffPtr;  /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
91 
92       uint32_t                          *pCrypOutBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
93 
94       __IO uint16_t                     CrypInCount;      /*!< Counter of input data */
95 
96       __IO uint16_t                     CrypOutCount;     /*!< Counter of output data */
97 
98       uint16_t                          Size;           /*!< length of input data in word */
99 
100       uint32_t                          Phase;            /*!< CRYP peripheral phase */
101 
102       DMA_HandleTypeDef                 *hdmain;          /*!< CRYP In DMA handle parameters */
103 
104       DMA_HandleTypeDef                 *hdmaout;         /*!< CRYP Out DMA handle parameters */
105 
106       HAL_LockTypeDef                   Lock;             /*!< CRYP locking object */
107 
108       __IO  HAL_CRYP_STATETypeDef       State;            /*!< CRYP peripheral state */
109 
110       __IO uint32_t                     ErrorCode;        /*!< CRYP peripheral error code */
111 
112 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
113  void (*InCpltCallback)    (struct __CRYP_HandleTypeDef * hcryp); /*!< CRYP Input FIFO transfer completed callback  */
114  void (*OutCpltCallback)   (struct __CRYP_HandleTypeDef * hcryp); /*!< CRYP Output FIFO transfer completed callback */
115  void (*ErrorCallback)     (struct __CRYP_HandleTypeDef * hcryp); /*!< CRYP Error callback */
116 
117  void (* MspInitCallback)  (struct __CRYP_HandleTypeDef * hcryp); /*!< CRYP Msp Init callback  */
118  void (* MspDeInitCallback)(struct __CRYP_HandleTypeDef * hcryp); /*!< CRYP Msp DeInit callback  */
119 
120 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
121 
122 }CRYP_HandleTypeDef;
123 
124 
125 /**
126   * @}
127   */
128 
129 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
130 /** @defgroup HAL_CRYP_Callback_ID_enumeration_definition HAL CRYP Callback ID enumeration definition
131   * @brief  HAL CRYP Callback ID enumeration definition
132   * @{
133   */
134 typedef enum
135 {
136   HAL_CRYP_INPUT_COMPLETE_CB_ID    = 0x01U,    /*!< CRYP Input FIFO transfer completed callback ID */
137   HAL_CRYP_OUTPUT_COMPLETE_CB_ID   = 0x02U,    /*!< CRYP Output FIFO transfer completed callback ID */
138   HAL_CRYP_ERROR_CB_ID             = 0x03U,    /*!< CRYP Error callback ID           */
139 
140   HAL_CRYP_MSPINIT_CB_ID        = 0x04U,    /*!< CRYP MspInit callback ID         */
141   HAL_CRYP_MSPDEINIT_CB_ID      = 0x05U     /*!< CRYP MspDeInit callback ID       */
142 
143 }HAL_CRYP_CallbackIDTypeDef;
144 /**
145   * @}
146   */
147 
148 /** @defgroup HAL_CRYP_Callback_pointer_definition HAL CRYP Callback pointer definition
149   * @brief  HAL CRYP Callback pointer definition
150   * @{
151   */
152 
153 typedef  void (*pCRYP_CallbackTypeDef)(CRYP_HandleTypeDef * hcryp);   /*!< pointer to a common CRYP callback function */
154 
155 /**
156   * @}
157   */
158 
159 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
160 
161 /* Exported constants --------------------------------------------------------*/
162 /** @defgroup CRYP_Exported_Constants CRYP Exported Constants
163   * @{
164   */
165 
166 /** @defgroup CRYP_Error_Definition   CRYP Error Definition
167   * @{
168   */
169 #define HAL_CRYP_ERROR_NONE              0x00000000U  /*!< No error        */
170 #define HAL_CRYP_ERROR_WRITE             0x00000001U  /*!< Write error     */
171 #define HAL_CRYP_ERROR_READ              0x00000002U  /*!< Read error      */
172 #define HAL_CRYP_ERROR_DMA               0x00000004U  /*!< DMA error       */
173 #define HAL_CRYP_ERROR_BUSY              0x00000008U  /*!< Busy flag error */
174 #define HAL_CRYP_ERROR_TIMEOUT           0x00000010U  /*!< Timeout error */
175 #define HAL_CRYP_ERROR_NOT_SUPPORTED     0x00000020U  /*!< Not supported mode */
176 #define HAL_CRYP_ERROR_AUTH_TAG_SEQUENCE 0x00000040U  /*!< Sequence are not respected only for GCM or CCM */
177 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
178 #define  HAL_CRYP_ERROR_INVALID_CALLBACK ((uint32_t)0x00000080U)    /*!< Invalid Callback error  */
179 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
180 
181 /**
182   * @}
183   */
184 
185 
186 /** @defgroup CRYP_Data_Width_Unit CRYP Data Width Unit
187   * @{
188   */
189 
190 #define CRYP_DATAWIDTHUNIT_WORD   0x00000000U  /*!< By default, size unit is word */
191 #define CRYP_DATAWIDTHUNIT_BYTE   0x00000001U  /*!< By default, size unit is word */
192 
193 /**
194   * @}
195   */
196 
197 /** @defgroup CRYP_Algorithm_Mode CRYP Algorithm Mode
198   * @{
199   */
200 
201 #define CRYP_DES_ECB     CRYP_CR_ALGOMODE_DES_ECB
202 #define CRYP_DES_CBC     CRYP_CR_ALGOMODE_DES_CBC
203 #define CRYP_TDES_ECB    CRYP_CR_ALGOMODE_TDES_ECB
204 #define CRYP_TDES_CBC    CRYP_CR_ALGOMODE_TDES_CBC
205 #define CRYP_AES_ECB     CRYP_CR_ALGOMODE_AES_ECB
206 #define CRYP_AES_CBC     CRYP_CR_ALGOMODE_AES_CBC
207 #define CRYP_AES_CTR     CRYP_CR_ALGOMODE_AES_CTR
208 
209 /**
210   * @}
211   */
212 
213 /** @defgroup CRYP_Key_Size CRYP Key Size
214   * @{
215   */
216 
217 #define CRYP_KEYSIZE_128B         0x00000000U
218 #define CRYP_KEYSIZE_192B         CRYP_CR_KEYSIZE_0
219 #define CRYP_KEYSIZE_256B         CRYP_CR_KEYSIZE_1
220 
221 /**
222   * @}
223   */
224 
225 /** @defgroup CRYP_Data_Type CRYP Data Type
226   * @{
227   */
228 
229 #define CRYP_DATATYPE_32B         0x00000000U
230 #define CRYP_DATATYPE_16B         CRYP_CR_DATATYPE_0
231 #define CRYP_DATATYPE_8B          CRYP_CR_DATATYPE_1
232 #define CRYP_DATATYPE_1B          CRYP_CR_DATATYPE
233 
234 /**
235   * @}
236   */
237 
238 /** @defgroup CRYP_Interrupt  CRYP Interrupt
239   * @{
240   */
241 
242 #define CRYP_IT_INI       CRYP_IMSCR_INIM   /*!< Input FIFO Interrupt */
243 #define CRYP_IT_OUTI      CRYP_IMSCR_OUTIM  /*!< Output FIFO Interrupt */
244 
245 /**
246   * @}
247   */
248 
249 /** @defgroup CRYP_Flags CRYP Flags
250   * @{
251   */
252 
253 /* Flags in the SR register */
254 #define CRYP_FLAG_IFEM    CRYP_SR_IFEM  /*!< Input FIFO is empty */
255 #define CRYP_FLAG_IFNF    CRYP_SR_IFNF  /*!< Input FIFO is not Full */
256 #define CRYP_FLAG_OFNE    CRYP_SR_OFNE  /*!< Output FIFO is not empty */
257 #define CRYP_FLAG_OFFU    CRYP_SR_OFFU  /*!< Output FIFO is Full */
258 #define CRYP_FLAG_BUSY    CRYP_SR_BUSY  /*!< The CRYP core is currently processing a block of data
259                                              or a key preparation (for AES decryption). */
260 /* Flags in the RISR register */
261 #define CRYP_FLAG_OUTRIS  0x01000002U  /*!< Output FIFO service raw interrupt status */
262 #define CRYP_FLAG_INRIS   0x01000001U  /*!< Input FIFO service raw interrupt status*/
263 
264 /**
265   * @}
266   */
267 
268 
269 /**
270   * @}
271   */
272 
273 /* Exported macros -----------------------------------------------------------*/
274 /** @defgroup CRYP_Exported_Macros CRYP Exported Macros
275   * @{
276   */
277 
278 /** @brief Reset CRYP handle state
279   * @param  __HANDLE__ specifies the CRYP handle.
280   * @retval None
281   */
282 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
283 #define __HAL_CRYP_RESET_HANDLE_STATE(__HANDLE__) do{\
284                                                       (__HANDLE__)->State = HAL_CRYP_STATE_RESET;\
285                                                       (__HANDLE__)->MspInitCallback = NULL;\
286                                                       (__HANDLE__)->MspDeInitCallback = NULL;\
287                                                      }while(0)
288 #else
289 #define __HAL_CRYP_RESET_HANDLE_STATE(__HANDLE__) ( (__HANDLE__)->State = HAL_CRYP_STATE_RESET)
290 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
291 
292 /**
293   * @brief  Enable/Disable the CRYP peripheral.
294   * @param  __HANDLE__: specifies the CRYP handle.
295   * @retval None
296   */
297 
298 #define __HAL_CRYP_ENABLE(__HANDLE__)  ((__HANDLE__)->Instance->CR |=  CRYP_CR_CRYPEN)
299 #define __HAL_CRYP_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR &=  ~CRYP_CR_CRYPEN)
300 
301 /** @brief  Check whether the specified CRYP status flag is set or not.
302   * @param  __FLAG__: specifies the flag to check.
303   *         This parameter can be one of the following values for CRYP:
304   *            @arg CRYP_FLAG_BUSY: The CRYP core is currently processing a block of data
305   *                                 or a key preparation (for AES decryption).
306   *            @arg CRYP_FLAG_IFEM: Input FIFO is empty
307   *            @arg CRYP_FLAG_IFNF: Input FIFO is not full
308   *            @arg CRYP_FLAG_INRIS: Input FIFO service raw interrupt is pending
309   *            @arg CRYP_FLAG_OFNE: Output FIFO is not empty
310   *            @arg CRYP_FLAG_OFFU: Output FIFO is full
311   *            @arg CRYP_FLAG_OUTRIS: Input FIFO service raw interrupt is pending
312  * @retval The state of __FLAG__ (TRUE or FALSE).
313   */
314 #define CRYP_FLAG_MASK  0x0000001FU
315 
316 #define __HAL_CRYP_GET_FLAG(__HANDLE__, __FLAG__) ((((uint8_t)((__FLAG__) >> 24)) == 0x01U)?((((__HANDLE__)->Instance->RISR) & ((__FLAG__) & CRYP_FLAG_MASK)) == ((__FLAG__) & CRYP_FLAG_MASK)): \
317                                                  ((((__HANDLE__)->Instance->RISR) & ((__FLAG__) & CRYP_FLAG_MASK)) == ((__FLAG__) & CRYP_FLAG_MASK)))
318 
319 /** @brief  Check whether the specified CRYP interrupt is set or not.
320   * @param  __HANDLE__: specifies the CRYP handle.
321   * @param  __INTERRUPT__: specifies the interrupt to check.
322   *         This parameter can be one of the following values for CRYP:
323   *            @arg CRYP_IT_INI: Input FIFO service masked interrupt status
324   *            @arg CRYP_IT_OUTI: Output FIFO service masked interrupt status
325   * @retval The state of __INTERRUPT__ (TRUE or FALSE).
326   */
327 
328 #define __HAL_CRYP_GET_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->MISR & (__INTERRUPT__)) == (__INTERRUPT__))
329 
330 /**
331   * @brief  Enable the CRYP interrupt.
332   * @param  __HANDLE__: specifies the CRYP handle.
333   * @param  __INTERRUPT__: CRYP Interrupt.
334   *         This parameter can be one of the following values for CRYP:
335   *            @ CRYP_IT_INI : Input FIFO service interrupt mask.
336   *            @ CRYP_IT_OUTI : Output FIFO service interrupt mask.CRYP interrupt.
337   * @retval None
338   */
339 
340 #define __HAL_CRYP_ENABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IMSCR) |= (__INTERRUPT__))
341 
342 /**
343   * @brief  Disable the CRYP interrupt.
344   * @param  __HANDLE__: specifies the CRYP handle.
345   * @param  __INTERRUPT__: CRYP Interrupt.
346   *         This parameter can be one of the following values for CRYP:
347   *            @ CRYP_IT_INI : Input FIFO service interrupt mask.
348   *            @ CRYP_IT_OUTI : Output FIFO service interrupt mask.CRYP interrupt.
349   * @retval None
350   */
351 
352 #define __HAL_CRYP_DISABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IMSCR) &= ~(__INTERRUPT__))
353 
354 /**
355   * @}
356   */
357 
358 /* Exported functions --------------------------------------------------------*/
359 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
360   * @{
361   */
362 
363 /** @addtogroup CRYP_Exported_Functions_Group1
364   * @{
365   */
366 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp);
367 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp);
368 void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp);
369 void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp);
370 HAL_StatusTypeDef HAL_CRYP_SetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf );
371 HAL_StatusTypeDef HAL_CRYP_GetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf );
372 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
373 HAL_StatusTypeDef HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID, pCRYP_CallbackTypeDef pCallback);
374 HAL_StatusTypeDef HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID);
375 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
376 /**
377   * @}
378   */
379 
380 /** @addtogroup CRYP_Exported_Functions_Group2
381   * @{
382   */
383 
384 /* encryption/decryption ***********************************/
385 HAL_StatusTypeDef HAL_CRYP_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, uint32_t Timeout);
386 HAL_StatusTypeDef HAL_CRYP_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, uint32_t Timeout);
387 HAL_StatusTypeDef HAL_CRYP_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output);
388 HAL_StatusTypeDef HAL_CRYP_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output);
389 HAL_StatusTypeDef HAL_CRYP_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output);
390 HAL_StatusTypeDef HAL_CRYP_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output);
391 
392 /**
393   * @}
394   */
395 
396 
397 /** @addtogroup CRYP_Exported_Functions_Group3
398   * @{
399   */
400 /* Interrupt Handler functions  **********************************************/
401 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp);
402 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp);
403 void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp);
404 void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp);
405 void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp);
406 uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp);
407 
408 /**
409   * @}
410   */
411 
412 /**
413   * @}
414   */
415 
416 /* Private macros --------------------------------------------------------*/
417 /** @defgroup CRYP_Private_Macros   CRYP Private Macros
418   * @{
419   */
420 
421 /** @defgroup CRYP_IS_CRYP_Definitions CRYP Private macros to check input parameters
422   * @{
423   */
424 
425 #define IS_CRYP_ALGORITHM(ALGORITHM) (((ALGORITHM) == CRYP_DES_ECB)   || \
426                                    ((ALGORITHM)  == CRYP_DES_CBC)   || \
427                                    ((ALGORITHM)  == CRYP_TDES_ECB)  || \
428                                    ((ALGORITHM)  == CRYP_TDES_CBC)  || \
429                                    ((ALGORITHM)  == CRYP_AES_ECB)   || \
430                                    ((ALGORITHM)  == CRYP_AES_CBC)   || \
431                                    ((ALGORITHM)  == CRYP_AES_CTR))
432 
433 #define IS_CRYP_KEYSIZE(KEYSIZE)(((KEYSIZE) == CRYP_KEYSIZE_128B)   || \
434                                  ((KEYSIZE) == CRYP_KEYSIZE_192B)   || \
435                                  ((KEYSIZE) == CRYP_KEYSIZE_256B))
436 
437 #define IS_CRYP_DATATYPE(DATATYPE)(((DATATYPE) == CRYP_DATATYPE_32B)   || \
438                                    ((DATATYPE) == CRYP_DATATYPE_16B) || \
439                                    ((DATATYPE) == CRYP_DATATYPE_8B) || \
440                                    ((DATATYPE) == CRYP_DATATYPE_1B))
441 
442 /**
443   * @}
444   */
445 
446 /**
447   * @}
448   */
449 
450 
451 /* Private constants ---------------------------------------------------------*/
452 /** @defgroup CRYP_Private_Constants CRYP Private Constants
453   * @{
454   */
455 
456 /**
457   * @}
458   */
459 /* Private defines -----------------------------------------------------------*/
460 /** @defgroup CRYP_Private_Defines CRYP Private Defines
461   * @{
462   */
463 
464 /**
465   * @}
466   */
467 
468 /* Private variables ---------------------------------------------------------*/
469 /** @defgroup CRYP_Private_Variables CRYP Private Variables
470   * @{
471   */
472 
473 /**
474   * @}
475   */
476 /* Private functions prototypes ----------------------------------------------*/
477 /** @defgroup CRYP_Private_Functions_Prototypes CRYP Private Functions Prototypes
478   * @{
479   */
480 
481 /**
482   * @}
483   */
484 
485 /* Private functions ---------------------------------------------------------*/
486 /** @defgroup CRYP_Private_Functions CRYP Private Functions
487   * @{
488   */
489 
490 /**
491   * @}
492   */
493 
494 
495 /**
496   * @}
497   */
498 
499 
500 #endif /* CRYP */
501 /**
502   * @}
503   */
504 
505 #ifdef __cplusplus
506 }
507 #endif
508 
509 #endif /* __STM32F2xx_HAL_CRYP_H */
510 
511 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
512