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