1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_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 __STM32L0xx_HAL_CRYP_H
21 #define __STM32L0xx_HAL_CRYP_H
22 
23 #ifdef __cplusplus
24  extern "C" {
25 #endif
26 
27 #if defined (STM32L021xx) || defined (STM32L041xx) || defined (STM32L062xx) || defined (STM32L063xx) || defined (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx)
28 
29 /* Includes ------------------------------------------------------------------*/
30 #include "stm32l0xx_hal_def.h"
31 
32 /** @addtogroup STM32L0xx_HAL_Driver
33   * @{
34   */
35 
36 /** @defgroup CRYP CRYP
37   * @{
38   */
39 
40 /* Exported types ------------------------------------------------------------*/
41 
42 /** @defgroup CRYP_Exported_Types CRYP Exported Types
43   * @{
44   */
45 
46 /**
47   * @brief  CRYP Configuration Structure definition
48   */
49 typedef struct
50 {
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 
54   uint8_t* pKey;        /*!< The key used for encryption/decryption */
55 
56   uint8_t* pInitVect;   /*!< The initialization vector used also as initialization
57                              counter in CTR mode */
58 
59 }CRYP_InitTypeDef;
60 
61 /**
62   * @brief HAL CRYP State structures definition
63   */
64 typedef enum
65 {
66   HAL_CRYP_STATE_RESET             = 0x00U,  /*!< CRYP not yet initialized or disabled  */
67   HAL_CRYP_STATE_READY             = 0x01U,  /*!< CRYP initialized and ready for use    */
68   HAL_CRYP_STATE_BUSY              = 0x02U,  /*!< CRYP internal processing is ongoing   */
69   HAL_CRYP_STATE_TIMEOUT           = 0x03U,  /*!< CRYP timeout state                    */
70   HAL_CRYP_STATE_ERROR             = 0x04U   /*!< CRYP error state                      */
71 
72 }HAL_CRYP_STATETypeDef;
73 
74 /**
75   * @brief HAL CRYP phase structures definition
76   */
77 typedef enum
78 {
79   HAL_CRYP_PHASE_READY             = 0x01U,    /*!< CRYP peripheral is ready for initialization. */
80   HAL_CRYP_PHASE_PROCESS           = 0x02U,    /*!< CRYP peripheral is in processing phase */
81 }HAL_PhaseTypeDef;
82 
83 /**
84   * @brief  CRYP handle Structure definition
85   */
86 typedef struct
87 {
88   AES_TypeDef                 *Instance;        /*!< Register base address        */
89 
90   CRYP_InitTypeDef            Init;             /*!< CRYP required parameters */
91 
92   uint8_t                     *pCrypInBuffPtr;  /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
93 
94   uint8_t                     *pCrypOutBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */
95 
96   __IO uint16_t               CrypInCount;      /*!< Counter of inputted data */
97 
98   __IO uint16_t               CrypOutCount;     /*!< Counter of outputted data */
99 
100   HAL_StatusTypeDef           Status;           /*!< CRYP peripheral status */
101 
102   HAL_PhaseTypeDef            Phase;            /*!< CRYP peripheral phase */
103 
104   DMA_HandleTypeDef           *hdmain;          /*!< CRYP In DMA handle parameters */
105 
106   DMA_HandleTypeDef           *hdmaout;         /*!< CRYP Out DMA handle parameters */
107 
108   HAL_LockTypeDef             Lock;             /*!< CRYP locking object */
109 
110   __IO  HAL_CRYP_STATETypeDef State;            /*!< CRYP peripheral state */
111 
112 }CRYP_HandleTypeDef;
113 
114 /**
115   * @}
116   */
117 
118 /* Exported constants --------------------------------------------------------*/
119 
120 /** @defgroup CRYP_Exported_Constants CRYP Exported Constants
121   * @{
122   */
123 
124 /** @defgroup CRYP_Data_Type CRYP Data Type
125   * @{
126   */
127 #define CRYP_DATATYPE_32B         (0x00000000U)
128 #define CRYP_DATATYPE_16B         AES_CR_DATATYPE_0
129 #define CRYP_DATATYPE_8B          AES_CR_DATATYPE_1
130 #define CRYP_DATATYPE_1B          AES_CR_DATATYPE
131 
132 #define IS_CRYP_DATATYPE(DATATYPE) (((DATATYPE) == CRYP_DATATYPE_32B) || \
133                                     ((DATATYPE) == CRYP_DATATYPE_16B) || \
134                                     ((DATATYPE) == CRYP_DATATYPE_8B)  || \
135                                     ((DATATYPE) == CRYP_DATATYPE_1B))
136 /**
137   * @}
138   */
139 
140 /** @defgroup CRYP_AlgoModeDirection CRYP Algo Mode Direction
141   * @{
142   */
143 #define CRYP_CR_ALGOMODE_DIRECTION              (uint32_t)(AES_CR_MODE|AES_CR_CHMOD)
144 
145 #define CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT        (0x00000000U)
146 #define CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT  (AES_CR_MODE)
147 #define CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT        (AES_CR_CHMOD_0)
148 #define CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT  ((uint32_t)(AES_CR_CHMOD_0|AES_CR_MODE))
149 #define CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT        (AES_CR_CHMOD_1)
150 #define CRYP_CR_ALGOMODE_AES_CTR_DECRYPT        ((uint32_t)(AES_CR_CHMOD_1 | AES_CR_MODE_1))
151 /**
152   * @}
153   */
154 
155 /** @defgroup CRYP_AES_Interrupts AES Interrupts
156   * @{
157   */
158 #define CRYP_IT_CC                          AES_CR_CCIE  /*!< Computation Complete interrupt */
159 #define CRYP_IT_ERR                         AES_CR_ERRIE /*!< Error interrupt                */
160 
161 /**
162   * @}
163   */
164 
165 
166 /** @defgroup CRYP_AES_Flags AES Flags
167   * @{
168   */
169 #define CRYP_FLAG_CCF                       AES_SR_CCF    /*!< Computation Complete Flag */
170 #define CRYP_FLAG_RDERR                     AES_SR_RDERR  /*!< Read Error Flag           */
171 #define CRYP_FLAG_WRERR                     AES_SR_WRERR  /*!< Write Error Flag          */
172 
173 /**
174   * @}
175   */
176 
177 /** @defgroup CRYP_AES_Clear_Flags AES Clear Flags
178   * @{
179   */
180 #define CRYP_CLEARFLAG_CCF                       AES_CR_CCFC   /*!< Computation Complete Flag Clear */
181 #define CRYP_CLEARFLAG_RDERR                     AES_CR_ERRC   /*!< Read Error Clear           */
182 #define CRYP_CLEARFLAG_WRERR                     AES_CR_ERRC   /*!< Write Error Clear          */
183 
184 /**
185   * @}
186   */
187 
188 /**
189   * @}
190   */
191 
192 /* Exported macro ------------------------------------------------------------*/
193 
194 /** @defgroup CRYP_Exported_Macros CRYP Exported Macros
195   * @{
196   */
197 
198 /** @brief Reset CRYP handle state
199   * @param  __HANDLE__ specifies the CRYP handle.
200   * @retval None
201   */
202 #define __HAL_CRYP_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_CRYP_STATE_RESET)
203 
204 /**
205   * @brief  Enable/Disable the CRYP peripheral.
206   * @param  __HANDLE__ specifies the CRYP handle.
207   * @retval None
208   */
209 #define __HAL_CRYP_ENABLE(__HANDLE__)                   SET_BIT((__HANDLE__)->Instance->CR, AES_CR_EN)
210 #define __HAL_CRYP_DISABLE(__HANDLE__)                  CLEAR_BIT((__HANDLE__)->Instance->CR, AES_CR_EN)
211 
212 /**
213   * @brief  Set the algorithm mode: AES-ECB, AES-CBC, AES-CTR, DES-ECB, DES-CBC,...
214   * @param  __HANDLE__ specifies the CRYP handle.
215   * @param  __MODE__ The algorithm mode.
216   * @retval None
217   */
218 #define __HAL_CRYP_SET_MODE(__HANDLE__,__MODE__)             SET_BIT((__HANDLE__)->Instance->CR, (__MODE__))
219 
220 
221 /** @brief  Check whether the specified CRYP flag is set or not.
222   * @param  __HANDLE__ specifies the CRYP handle.
223   * @param  __FLAG__ specifies the flag to check.
224   *         This parameter can be one of the following values:
225   *            @arg CRYP_FLAG_CCF   : Computation Complete Flag
226   *            @arg CRYP_FLAG_RDERR : Read Error Flag
227   *            @arg CRYP_FLAG_WRERR : Write Error Flag
228   * @retval The new state of __FLAG__ (TRUE or FALSE).
229   */
230 #define __HAL_CRYP_GET_FLAG(__HANDLE__,__FLAG__)         (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
231 
232 /** @brief  Clear the CRYP pending flag.
233   * @param  __HANDLE__ specifies the CRYP handle.
234   * @param  __FLAG__ specifies the flag to clear.
235   *         This parameter can be one of the following values:
236   *            @arg CRYP_CLEARFLAG_CCF   : Computation Complete Clear Flag
237   *            @arg CRYP_CLEARFLAG_RDERR : Read Error Clear
238   *            @arg CRYP_CLEARFLAG_WRERR : Write Error Clear
239   * @retval None
240   */
241 #define __HAL_CRYP_CLEAR_FLAG(__HANDLE__, __FLAG__)   SET_BIT((__HANDLE__)->Instance->CR, (__FLAG__))
242 
243 /**
244   * @brief  Enable the CRYP interrupt.
245   * @param  __HANDLE__ specifies the CRYP handle.
246   * @param  __INTERRUPT__ CRYP Interrupt.
247   * @retval None
248   */
249 #define __HAL_CRYP_ENABLE_IT(__HANDLE__,__INTERRUPT__)   SET_BIT((__HANDLE__)->Instance->CR, (__INTERRUPT__))
250 
251 /**
252   * @brief  Disable the CRYP interrupt.
253   * @param  __HANDLE__ specifies the CRYP handle.
254   * @param  __INTERRUPT__ CRYP interrupt.
255   * @retval None
256   */
257 #define __HAL_CRYP_DISABLE_IT(__HANDLE__,__INTERRUPT__)  CLEAR_BIT((__HANDLE__)->Instance->CR, (__INTERRUPT__))
258 
259 /** @brief  Checks if the specified CRYP interrupt source is enabled or disabled.
260   * @param  __HANDLE__ specifies the CRYP handle.
261   * @param __INTERRUPT__ CRYP interrupt source to check
262   *         This parameter can be one of the following values:
263   *            @arg CRYP_IT_CC   : Computation Complete interrupt
264   *            @arg CRYP_IT_ERR : Error interrupt (used for RDERR and WRERR)
265   * @retval State of interruption (SET or RESET)
266   */
267 #define __HAL_CRYP_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) \
268     (( ((__HANDLE__)->Instance->CR & (__INTERRUPT__)) == (__INTERRUPT__)       \
269      )? SET : RESET                                         \
270     )
271 
272 /** @brief  Clear the CRYP pending IT.
273   * @param  __HANDLE__ specifies the CRYP handle.
274   * @param  __IT__ specifies the IT to clear.
275   *         This parameter can be one of the following values:
276   *            @arg CRYP_CLEARFLAG_CCF   : Computation Complete Clear Flag
277   *            @arg CRYP_CLEARFLAG_RDERR : Read Error Clear
278   *            @arg CRYP_CLEARFLAG_WRERR : Write Error Clear
279   * @retval None
280   */
281 #define __HAL_CRYP_CLEAR_IT(__HANDLE__, __IT__) SET_BIT((__HANDLE__)->Instance->CR, (__IT__))
282 
283 /**
284   * @}
285   */
286 
287 /* Include CRYP HAL Extension module */
288 #include "stm32l0xx_hal_cryp_ex.h"
289 
290 /* Exported functions --------------------------------------------------------*/
291 
292 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
293   * @{
294   */
295 
296 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
297   * @{
298   */
299 
300 /* Initialization/de-initialization functions *********************************/
301 HAL_StatusTypeDef     HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp);
302 HAL_StatusTypeDef     HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp);
303 
304 /* MSP functions  *************************************************************/
305 void                  HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp);
306 void                  HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp);
307 
308 /**
309   * @}
310   */
311 
312 /** @defgroup CRYP_Exported_Functions_Group2  AES processing functions
313   * @{
314   */
315 
316 /* AES encryption/decryption using polling  ***********************************/
317 HAL_StatusTypeDef     HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
318 HAL_StatusTypeDef     HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
319 HAL_StatusTypeDef     HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
320 HAL_StatusTypeDef     HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
321 HAL_StatusTypeDef     HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout);
322 HAL_StatusTypeDef     HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout);
323 
324 /* AES encryption/decryption using interrupt  *********************************/
325 HAL_StatusTypeDef     HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
326 HAL_StatusTypeDef     HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
327 HAL_StatusTypeDef     HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
328 HAL_StatusTypeDef     HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
329 HAL_StatusTypeDef     HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
330 HAL_StatusTypeDef     HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
331 
332 /* AES encryption/decryption using DMA  ***************************************/
333 HAL_StatusTypeDef     HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
334 HAL_StatusTypeDef     HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
335 HAL_StatusTypeDef     HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
336 HAL_StatusTypeDef     HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
337 HAL_StatusTypeDef     HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData);
338 HAL_StatusTypeDef     HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData);
339 
340 /**
341   * @}
342   */
343 
344 /** @addtogroup CRYP_Exported_Functions_Group3 DMA callback functions
345   * @{
346   */
347 
348 /* CallBack functions  ********************************************************/
349 void                  HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp);
350 void                  HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp);
351 void                  HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp);
352 
353 /**
354   * @}
355   */
356 
357 /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
358   * @{
359   */
360 
361 /* Processing functions  ********************************************************/
362 void                  HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp);
363 
364 /**
365   * @}
366   */
367 
368 /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
369   * @{
370   */
371 
372 /* Peripheral State functions  **************************************************/
373 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp);
374 
375 /**
376   * @}
377   */
378 
379 /**
380   * @}
381   */
382 
383 /* Define the private group ***********************************/
384 /**************************************************************/
385 /** @defgroup CRYP_Private CRYP Private
386   * @{
387   */
388 /**
389   * @}
390   */
391 /**************************************************************/
392 
393 
394 /**
395   * @}
396   */
397 
398 /**
399   * @}
400   */
401 
402 #endif /* STM32L021xx || STM32L041xx || STM32L062xx || STM32L063xx || STM32L081xx || STM32L082xx || STM32L083xx */
403 #ifdef __cplusplus
404 }
405 #endif
406 
407 #endif /* __STM32L0xx_HAL_CRYP_H */
408