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