1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_hal_cryp.c
4   * @author  MCD Application Team
5   * @brief   CRYP HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the Cryptography (CRYP) peripheral:
8   *           + Initialization and de-initialization functions
9   *           + AES processing functions
10   *           + DES processing functions
11   *           + TDES processing functions
12   *           + DMA callback functions
13   *           + CRYP IRQ handler management
14   *           + Peripheral State functions
15   *
16   ******************************************************************************
17   * @attention
18   *
19   * Copyright (c) 2016 STMicroelectronics.
20   * All rights reserved.
21   *
22   * This software is licensed under terms that can be found in the LICENSE file
23   * in the root directory of this software component.
24   * If no LICENSE file comes with this software, it is provided AS-IS.
25   *
26   ******************************************************************************
27   @verbatim
28   ==============================================================================
29                      ##### How to use this driver #####
30   ==============================================================================
31     [..]
32       The CRYP HAL driver can be used in CRYP IP as follows:
33 
34       (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
35          (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()
36          (##) In case of using interrupts (e.g. HAL_CRYP_Encrypt_IT())
37              (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
38              (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
39              (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
40          (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_Encrypt_DMA())
41              (+++) Enable the DMAx interface clock using __RCC_DMAx_CLK_ENABLE()
42              (+++) Configure and enable two DMA streams one for managing data transfer from
43                  memory to peripheral (input stream) and another stream for managing data
44                  transfer from peripheral to memory (output stream)
45              (+++) Associate the initialized DMA handle to the CRYP DMA handle
46                  using  __HAL_LINKDMA()
47              (+++) Configure the priority and enable the NVIC for the transfer complete
48                  interrupt on the two DMA Streams. The output stream should have higher
49                  priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
50 
51       (#)Initialize the CRYP according to the specified parameters :
52          (##) The data type: 1-bit, 8-bit, 16-bit or 32-bit.
53          (##) The key size: 128, 192 or 256.
54          (##) The AlgoMode DES/ TDES Algorithm ECB/CBC or AES Algorithm ECB/CBC/CTR.
55          (##) The initialization vector (counter). It is not used in ECB mode.
56          (##) The key buffer used for encryption/decryption.
57 
58       (#)Three processing (encryption/decryption) functions are available:
59          (##) Polling mode: encryption and decryption APIs are blocking functions
60               i.e. they process the data and wait till the processing is finished,
61               e.g. HAL_CRYP_Encrypt & HAL_CRYP_Decrypt
62          (##) Interrupt mode: encryption and decryption APIs are not blocking functions
63               i.e. they process the data under interrupt,
64               e.g. HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT
65          (##) DMA mode: encryption and decryption APIs are not blocking functions
66               i.e. the data transfer is ensured by DMA,
67               e.g. HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA
68 
69       (#)When the processing function is called at first time after HAL_CRYP_Init()
70          the CRYP peripheral is configured and processes the buffer in input.
71          At second call, no need to Initialize the CRYP, user have to get current configuration via
72          HAL_CRYP_GetConfig() API, then only  HAL_CRYP_SetConfig() is requested to set
73          new parameters, finally user can  start encryption/decryption.
74 
75        (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
76 
77     [..]
78       The cryptographic processor supports following standards:
79       (#) The data encryption standard (DES) and Triple-DES (TDES) supported only by CRYP1 IP:
80          (##)64-bit data block processing
81          (##) chaining modes supported :
82              (+++)  Electronic Code Book(ECB)
83              (+++)  Cipher Block Chaining (CBC)
84          (##) keys length supported :64-bit, 128-bit and 192-bit.
85       (#) The advanced encryption standard (AES) supported  by CRYP1:
86          (##)128-bit data block processing
87          (##) chaining modes supported :
88              (+++)  Electronic Code Book(ECB)
89              (+++)  Cipher Block Chaining (CBC)
90              (+++)  Counter mode (CTR)
91          (##) keys length Supported :
92              (+++) for CRYP1 IP: 128-bit, 192-bit and 256-bit.
93 
94   *** Callback registration ***
95   =============================================
96   [..]
97   The compilation define  USE_HAL_CRYP_REGISTER_CALLBACKS when set to 1
98   allows the user to configure dynamically the driver callbacks.
99   Use Functions @ref HAL_CRYP_RegisterCallback() or HAL_CRYP_RegisterXXXCallback()
100   to register an interrupt callback.
101   [..]
102   Function @ref HAL_CRYP_RegisterCallback() allows to register following callbacks:
103     (+) InCpltCallback     :  Input FIFO transfer completed callback.
104     (+) OutCpltCallback    : Output FIFO transfer completed callback.
105     (+) ErrorCallback      : callback for error detection.
106     (+) MspInitCallback    : CRYP MspInit.
107     (+) MspDeInitCallback  : CRYP MspDeInit.
108   This function takes as parameters the HAL peripheral handle, the Callback ID
109   and a pointer to the user callback function.
110   [..]
111   Use function @ref HAL_CRYP_UnRegisterCallback() to reset a callback to the default
112   weak function.
113   @ref HAL_CRYP_UnRegisterCallback() takes as parameters the HAL peripheral handle,
114   and the Callback ID.
115   This function allows to reset following callbacks:
116     (+) InCpltCallback     :  Input FIFO transfer completed callback.
117     (+) OutCpltCallback    : Output FIFO transfer completed callback.
118     (+) ErrorCallback      : callback for error detection.
119     (+) MspInitCallback    : CRYP MspInit.
120     (+) MspDeInitCallback  : CRYP MspDeInit.
121   [..]
122   By default, after the @ref HAL_CRYP_Init() and when the state is HAL_CRYP_STATE_RESET
123   all callbacks are set to the corresponding weak functions :
124   examples @ref HAL_CRYP_InCpltCallback() , @ref HAL_CRYP_OutCpltCallback().
125   Exception done for MspInit and MspDeInit functions that are
126   reset to the legacy weak function in the @ref HAL_CRYP_Init()/ @ref HAL_CRYP_DeInit() only when
127   these callbacks are null (not registered beforehand).
128   if not, MspInit or MspDeInit are not null, the @ref HAL_CRYP_Init() / @ref HAL_CRYP_DeInit()
129   keep and use the user MspInit/MspDeInit functions (registered beforehand)
130 
131   Callbacks can be registered/unregistered in HAL_CRYP_STATE_READY state only.
132   Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
133   in HAL_CRYP_STATE_READY or HAL_CRYP_STATE_RESET state,
134   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
135   In that case first register the MspInit/MspDeInit user callbacks
136   using @ref HAL_CRYP_RegisterCallback() before calling @ref HAL_CRYP_DeInit()
137   or @ref HAL_CRYP_Init() function.
138   [..]
139   When The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS is set to 0 or
140   not defined, the callback registration feature is not available and all callbacks
141   are set to the corresponding weak functions.
142 
143   @endverbatim
144   ******************************************************************************
145   */
146 
147 /* Includes ------------------------------------------------------------------*/
148 #include "stm32f2xx_hal.h"
149 
150 #if defined(CRYP)
151 #ifdef HAL_CRYP_MODULE_ENABLED
152 /** @addtogroup STM32F2xx_HAL_Driver
153   * @{
154   */
155 
156 /** @addtogroup CRYP
157   * @{
158   */
159 
160 /* Private typedef -----------------------------------------------------------*/
161 /* Private define ------------------------------------------------------------*/
162 /** @addtogroup CRYP_Private_Defines
163   * @{
164   */
165 
166 #define CRYP_TIMEOUT_KEYPREPARATION      82U         /*The latency of key preparation operation is 82 clock cycles.*/
167 
168 #define  CRYP_PHASE_READY                0x00000001U /*!< CRYP peripheral is ready for initialization. */
169 #define  CRYP_PHASE_PROCESS              0x00000002U /*!< CRYP peripheral is in processing phase */
170 
171 #define CRYP_OPERATINGMODE_ENCRYPT       0x00000000U             /*!< Encryption mode   */
172 #define CRYP_OPERATINGMODE_DECRYPT       CRYP_CR_ALGODIR         /*!< Decryption        */
173 
174 /**
175   * @}
176   */
177 
178 
179 /* Private macro -------------------------------------------------------------*/
180 /** @addtogroup CRYP_Private_Macros
181   * @{
182   */
183 
184 #define HAL_CRYP_FIFO_FLUSH(__HANDLE__) ((__HANDLE__)->Instance->CR |=  CRYP_CR_FFLUSH)
185 
186 
187 /**
188   * @}
189   */
190 
191 /* Private struct -------------------------------------------------------------*/
192 /* Private variables ---------------------------------------------------------*/
193 /* Private function prototypes -----------------------------------------------*/
194 /** @addtogroup CRYP_Private_Functions_prototypes
195   * @{
196   */
197 
198 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
199 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
200 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
201 static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
202 static void CRYP_SetKey( CRYP_HandleTypeDef *hcryp, uint32_t KeySize);
203 static void CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
204 static void CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
205 static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
206 static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
207 static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp);
208 static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp);
209 static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp);
210 static void CRYP_TDES_IT(CRYP_HandleTypeDef *hcryp);
211 static HAL_StatusTypeDef CRYP_WaitOnBUSYFlag(const CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
212 static HAL_StatusTypeDef CRYP_WaitOnOFNEFlag(const CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
213 static HAL_StatusTypeDef CRYP_TDES_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
214 
215 /**
216   * @}
217   */
218 
219 /* Exported functions ---------------------------------------------------------*/
220 
221 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
222   * @{
223   */
224 
225 
226 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
227  *  @brief    CRYP  Initialization and Configuration functions.
228  *
229 @verbatim
230   ========================================================================================
231      ##### Initialization, de-initialization and Set and Get configuration functions #####
232   ========================================================================================
233     [..]  This section provides functions allowing to:
234       (+) Initialize the CRYP
235       (+) DeInitialize the CRYP
236       (+) Initialize the CRYP MSP
237       (+) DeInitialize the CRYP MSP
238       (+) configure CRYP (HAL_CRYP_SetConfig) with the specified parameters in the CRYP_ConfigTypeDef
239           Parameters which are configured in This section are :
240           (++) Key size
241           (++) Data Type : 32,16, 8 or 1bit
242           (++) AlgoMode : for CRYP1 IP
243                  ECB and CBC in DES/TDES Standard
244                  ECB,CBC and CTR in AES Standard.
245       (+) Get CRYP configuration (HAL_CRYP_GetConfig) from the specified parameters in the CRYP_HandleTypeDef
246 
247 
248 @endverbatim
249   * @{
250   */
251 
252 
253 /**
254   * @brief  Initializes the CRYP according to the specified
255   *         parameters in the CRYP_ConfigTypeDef and creates the associated handle.
256   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
257   *         the configuration information for CRYP module
258   * @retval HAL status
259   */
HAL_CRYP_Init(CRYP_HandleTypeDef * hcryp)260 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
261 {
262   /* Check the CRYP handle allocation */
263   if(hcryp == NULL)
264   {
265     return HAL_ERROR;
266   }
267 
268   /* Check parameters */
269   assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
270   assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
271   assert_param(IS_CRYP_ALGORITHM(hcryp->Init.Algorithm));
272 
273 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
274   if(hcryp->State == HAL_CRYP_STATE_RESET)
275   {
276     /* Allocate lock resource and initialize it */
277     hcryp->Lock = HAL_UNLOCKED;
278 
279     hcryp->InCpltCallback  = HAL_CRYP_InCpltCallback;  /* Legacy weak  InCpltCallback  */
280     hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak OutCpltCallback  */
281     hcryp->ErrorCallback   = HAL_CRYP_ErrorCallback;   /* Legacy weak ErrorCallback    */
282 
283     if(hcryp->MspInitCallback == NULL)
284     {
285       hcryp->MspInitCallback = HAL_CRYP_MspInit; /* Legacy weak MspInit  */
286     }
287 
288     /* Init the low level hardware */
289     hcryp->MspInitCallback(hcryp);
290   }
291 #else
292   if(hcryp->State == HAL_CRYP_STATE_RESET)
293   {
294     /* Allocate lock resource and initialize it */
295     hcryp->Lock = HAL_UNLOCKED;
296 
297     /* Init the low level hardware */
298     HAL_CRYP_MspInit(hcryp);
299   }
300 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
301 
302   /* Set the key size(This bit field is "don't care" in the DES or TDES modes) data type and Algorithm */
303   MODIFY_REG(hcryp->Instance->CR, CRYP_CR_DATATYPE|CRYP_CR_KEYSIZE|CRYP_CR_ALGOMODE, hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
304 
305   /* Reset Error Code field */
306   hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
307 
308   /* Change the CRYP state */
309   hcryp->State = HAL_CRYP_STATE_READY;
310 
311   /* Set the default CRYP phase */
312   hcryp->Phase = CRYP_PHASE_READY;
313 
314   /* Return function status */
315   return HAL_OK;
316 }
317 
318 /**
319   * @brief  De-Initializes the CRYP peripheral.
320   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
321   *         the configuration information for CRYP module
322   * @retval HAL status
323 */
HAL_CRYP_DeInit(CRYP_HandleTypeDef * hcryp)324 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
325 {
326   /* Check the CRYP handle allocation */
327   if(hcryp == NULL)
328   {
329     return HAL_ERROR;
330   }
331 
332   /* Set the default CRYP phase */
333   hcryp->Phase = CRYP_PHASE_READY;
334 
335   /* Reset CrypInCount and CrypOutCount */
336   hcryp->CrypInCount = 0;
337   hcryp->CrypOutCount = 0;
338 
339   /* Disable the CRYP peripheral clock */
340   __HAL_CRYP_DISABLE(hcryp);
341 
342 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
343   if(hcryp->MspDeInitCallback == NULL)
344   {
345     hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit; /* Legacy weak MspDeInit  */
346   }
347   /* DeInit the low level hardware */
348   hcryp->MspDeInitCallback(hcryp);
349 
350 #else
351   /* DeInit the low level hardware: CLOCK, NVIC.*/
352   HAL_CRYP_MspDeInit(hcryp);
353 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
354 
355   /* Change the CRYP state */
356   hcryp->State = HAL_CRYP_STATE_RESET;
357 
358   /* Release Lock */
359   __HAL_UNLOCK(hcryp);
360 
361   /* Return function status */
362   return HAL_OK;
363 }
364 
365 /**
366   * @brief  Configure the CRYP according to the specified
367   *         parameters in the CRYP_ConfigTypeDef
368   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure
369   * @param  pConf: pointer to a CRYP_ConfigTypeDef structure that contains
370   *         the configuration information for CRYP module
371   * @retval HAL status
372   */
HAL_CRYP_SetConfig(CRYP_HandleTypeDef * hcryp,CRYP_ConfigTypeDef * pConf)373 HAL_StatusTypeDef HAL_CRYP_SetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf )
374 {
375   /* Check the CRYP handle allocation */
376   if((hcryp == NULL)|| (pConf == NULL) )
377   {
378     return HAL_ERROR;
379   }
380 
381   /* Check parameters */
382   assert_param(IS_CRYP_KEYSIZE(pConf->KeySize));
383   assert_param(IS_CRYP_DATATYPE(pConf->DataType));
384   assert_param(IS_CRYP_ALGORITHM(pConf->Algorithm));
385 
386   if(hcryp->State == HAL_CRYP_STATE_READY)
387   {
388     /* Change the CRYP state */
389     hcryp->State = HAL_CRYP_STATE_BUSY;
390 
391     /* Process locked */
392     __HAL_LOCK(hcryp);
393 
394     /* Set  CRYP parameters  */
395     hcryp->Init.DataType     = pConf->DataType;
396     hcryp->Init.pKey         = pConf->pKey;
397     hcryp->Init.Algorithm    = pConf->Algorithm;
398     hcryp->Init.KeySize      = pConf->KeySize;
399     hcryp->Init.pInitVect    = pConf->pInitVect;
400 
401     /* Set the key size(This bit field is "don't care" in the DES or TDES modes) data type, AlgoMode and operating mode*/
402     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_DATATYPE|CRYP_CR_KEYSIZE|CRYP_CR_ALGOMODE, hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
403 
404     /* Process Unlocked */
405     __HAL_UNLOCK(hcryp);
406 
407     /* Reset Error Code field */
408     hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
409 
410     /* Change the CRYP state */
411     hcryp->State = HAL_CRYP_STATE_READY;
412 
413     /* Set the default CRYP phase */
414     hcryp->Phase = CRYP_PHASE_READY;
415 
416     /* Return function status */
417     return HAL_OK;
418   }
419   else
420   {
421     /* Process Unlocked */
422     __HAL_UNLOCK(hcryp);
423 
424     /* Busy error code field */
425     hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
426     return HAL_ERROR;
427   }
428 }
429 
430 /**
431   * @brief  Get CRYP Configuration parameters in associated handle.
432   * @param  pConf: pointer to a CRYP_ConfigTypeDef structure
433   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
434   *         the configuration information for CRYP module
435   * @retval HAL status
436   */
HAL_CRYP_GetConfig(CRYP_HandleTypeDef * hcryp,CRYP_ConfigTypeDef * pConf)437 HAL_StatusTypeDef HAL_CRYP_GetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf )
438 {
439   /* Check the CRYP handle allocation */
440   if((hcryp == NULL)|| (pConf == NULL) )
441   {
442     return HAL_ERROR;
443   }
444 
445   if(hcryp->State == HAL_CRYP_STATE_READY)
446   {
447     /* Change the CRYP state */
448     hcryp->State = HAL_CRYP_STATE_BUSY;
449 
450     /* Process locked */
451     __HAL_LOCK(hcryp);
452 
453     /* Get  CRYP parameters  */
454     pConf->DataType        = hcryp->Init.DataType;
455     pConf->pKey            = hcryp->Init.pKey;
456     pConf->Algorithm       = hcryp->Init.Algorithm;
457     pConf->KeySize         = hcryp->Init.KeySize ;
458     pConf->pInitVect       = hcryp->Init.pInitVect;
459 
460     /* Process Unlocked */
461     __HAL_UNLOCK(hcryp);
462 
463     /* Change the CRYP state */
464     hcryp->State = HAL_CRYP_STATE_READY;
465 
466     /* Return function status */
467     return HAL_OK;
468   }
469   else
470   {
471     /* Process Unlocked */
472     __HAL_UNLOCK(hcryp);
473 
474     /* Busy error code field */
475     hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
476     return HAL_ERROR;
477   }
478 }
479 /**
480   * @brief  Initializes the CRYP MSP.
481   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
482   *         the configuration information for CRYP module
483   * @retval None
484   */
HAL_CRYP_MspInit(CRYP_HandleTypeDef * hcryp)485 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
486 {
487   /* Prevent unused argument(s) compilation warning */
488   UNUSED(hcryp);
489 
490   /* NOTE : This function Should not be modified, when the callback is needed,
491             the HAL_CRYP_MspInit could be implemented in the user file
492    */
493 }
494 
495 /**
496   * @brief  DeInitializes CRYP MSP.
497   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
498   *         the configuration information for CRYP module
499   * @retval None
500   */
HAL_CRYP_MspDeInit(CRYP_HandleTypeDef * hcryp)501 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
502 {
503   /* Prevent unused argument(s) compilation warning */
504   UNUSED(hcryp);
505 
506   /* NOTE : This function Should not be modified, when the callback is needed,
507             the HAL_CRYP_MspDeInit could be implemented in the user file
508    */
509 }
510 
511 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
512 /**
513   * @brief  Register a User CRYP Callback
514   *         To be used instead of the weak predefined callback
515   * @param hcryp cryp handle
516   * @param CallbackID ID of the callback to be registered
517   *        This parameter can be one of the following values:
518   *          @arg @ref HAL_CRYP_INPUT_COMPLETE_CB_ID Input FIFO transfer completed callback ID
519   *          @arg @ref HAL_CRYP_OUTPUT_COMPLETE_CB_ID Output FIFO transfer completed callback ID
520   *          @arg @ref HAL_CRYP_ERROR_CB_ID Rx Half Error callback ID
521   *          @arg @ref HAL_CRYP_MSPINIT_CB_ID MspInit callback ID
522   *          @arg @ref HAL_CRYP_MSPDEINIT_CB_ID MspDeInit callback ID
523   * @param pCallback pointer to the Callback function
524   * @retval status
525   */
HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef * hcryp,HAL_CRYP_CallbackIDTypeDef CallbackID,pCRYP_CallbackTypeDef pCallback)526 HAL_StatusTypeDef HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID, pCRYP_CallbackTypeDef pCallback)
527 {
528   HAL_StatusTypeDef status = HAL_OK;
529 
530   if(pCallback == NULL)
531   {
532     /* Update the error code */
533     hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
534 
535     return HAL_ERROR;
536   }
537   /* Process locked */
538   __HAL_LOCK(hcryp);
539 
540   if(hcryp->State == HAL_CRYP_STATE_READY)
541   {
542     switch (CallbackID)
543     {
544     case HAL_CRYP_INPUT_COMPLETE_CB_ID :
545       hcryp->InCpltCallback = pCallback;
546       break;
547 
548     case HAL_CRYP_OUTPUT_COMPLETE_CB_ID :
549       hcryp->OutCpltCallback = pCallback;
550       break;
551 
552     case HAL_CRYP_ERROR_CB_ID :
553       hcryp->ErrorCallback = pCallback;
554       break;
555 
556     case HAL_CRYP_MSPINIT_CB_ID :
557       hcryp->MspInitCallback = pCallback;
558       break;
559 
560     case HAL_CRYP_MSPDEINIT_CB_ID :
561       hcryp->MspDeInitCallback = pCallback;
562       break;
563 
564     default :
565       /* Update the error code */
566       hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
567       /* Return error status */
568       status =  HAL_ERROR;
569       break;
570     }
571   }
572   else if(hcryp->State == HAL_CRYP_STATE_RESET)
573   {
574     switch (CallbackID)
575     {
576     case HAL_CRYP_MSPINIT_CB_ID :
577       hcryp->MspInitCallback = pCallback;
578       break;
579 
580     case HAL_CRYP_MSPDEINIT_CB_ID :
581       hcryp->MspDeInitCallback = pCallback;
582       break;
583 
584     default :
585       /* Update the error code */
586       hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
587       /* Return error status */
588       status =  HAL_ERROR;
589       break;
590     }
591   }
592   else
593   {
594     /* Update the error code */
595     hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
596     /* Return error status */
597     status =  HAL_ERROR;
598   }
599 
600   /* Release Lock */
601   __HAL_UNLOCK(hcryp);
602 
603   return status;
604 }
605 
606 /**
607   * @brief  Unregister an CRYP Callback
608   *         CRYP callback is redirected to the weak predefined callback
609   * @param hcryp cryp handle
610   * @param CallbackID ID of the callback to be unregistered
611   *        This parameter can be one of the following values:
612   *          @arg @ref HAL_CRYP_INPUT_COMPLETE_CB_ID Input FIFO transfer completed callback ID
613   *          @arg @ref HAL_CRYP_OUTPUT_COMPLETE_CB_ID Output FIFO transfer completed callback ID
614   *          @arg @ref HAL_CRYP_ERROR_CB_ID Rx Half Error callback ID
615   *          @arg @ref HAL_CRYP_MSPINIT_CB_ID MspInit callback ID
616   *          @arg @ref HAL_CRYP_MSPDEINIT_CB_ID MspDeInit callback ID
617   * @retval status
618   */
HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef * hcryp,HAL_CRYP_CallbackIDTypeDef CallbackID)619 HAL_StatusTypeDef HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID)
620 {
621   HAL_StatusTypeDef status = HAL_OK;
622 
623   /* Process locked */
624   __HAL_LOCK(hcryp);
625 
626   if(hcryp->State == HAL_CRYP_STATE_READY)
627   {
628     switch (CallbackID)
629     {
630     case HAL_CRYP_INPUT_COMPLETE_CB_ID :
631       hcryp->InCpltCallback = HAL_CRYP_InCpltCallback;  /* Legacy weak  InCpltCallback  */
632       break;
633 
634     case HAL_CRYP_OUTPUT_COMPLETE_CB_ID :
635       hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback;         /* Legacy weak OutCpltCallback       */
636       break;
637 
638     case HAL_CRYP_ERROR_CB_ID :
639       hcryp->ErrorCallback = HAL_CRYP_ErrorCallback;           /* Legacy weak ErrorCallback        */
640       break;
641 
642     case HAL_CRYP_MSPINIT_CB_ID :
643       hcryp->MspInitCallback = HAL_CRYP_MspInit;
644       break;
645 
646     case HAL_CRYP_MSPDEINIT_CB_ID :
647       hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
648       break;
649 
650     default :
651       /* Update the error code */
652       hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
653       /* Return error status */
654       status =  HAL_ERROR;
655       break;
656     }
657   }
658   else if(hcryp->State == HAL_CRYP_STATE_RESET)
659   {
660     switch (CallbackID)
661     {
662     case HAL_CRYP_MSPINIT_CB_ID :
663       hcryp->MspInitCallback = HAL_CRYP_MspInit;
664       break;
665 
666     case HAL_CRYP_MSPDEINIT_CB_ID :
667       hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
668       break;
669 
670     default :
671       /* Update the error code */
672       hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
673       /* Return error status */
674       status =  HAL_ERROR;
675       break;
676     }
677   }
678   else
679   {
680     /* Update the error code */
681     hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
682     /* Return error status */
683     status =  HAL_ERROR;
684   }
685 
686   /* Release Lock */
687   __HAL_UNLOCK(hcryp);
688 
689   return status;
690 }
691 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
692 
693 /**
694   * @}
695   */
696 
697 /** @defgroup CRYP_Exported_Functions_Group2  Encrypt Decrypt functions
698  *  @brief   CRYP processing functions.
699  *
700 @verbatim
701   ==============================================================================
702                       ##### Encrypt Decrypt  functions #####
703   ==============================================================================
704     [..]  This section provides API allowing to Encrypt/Decrypt Data following
705           Standard DES/TDES or AES, and Algorithm configured by the user:
706       (+) Standard DES/TDES only supported by CRYP1 IP, below list of Algorithm supported :
707            (++)  Electronic Code Book(ECB)
708            (++) Cipher Block Chaining (CBC)
709       (+) Standard AES  supported by CRYP1 IP , list of Algorithm supported:
710            (++) Electronic Code Book(ECB)
711            (++) Cipher Block Chaining (CBC)
712            (++) Counter mode (CTR)
713            (++) Cipher Block Chaining (CBC)
714            (++) Counter mode (CTR)
715     [..]  Three processing functions are available:
716       (+) Polling mode : HAL_CRYP_Encrypt & HAL_CRYP_Decrypt
717       (+) Interrupt mode : HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT
718       (+) DMA mode : HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA
719 
720 @endverbatim
721   * @{
722   */
723 
724 
725 /**
726   * @brief  Encryption mode.
727   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
728   *         the configuration information for CRYP module
729   * @param  Input: Pointer to the input buffer (plaintext)
730   * @param  Size: Length of the plaintext buffer in word.
731   * @param  Output: Pointer to the output buffer(ciphertext)
732   * @param  Timeout: Specify Timeout value
733   * @retval HAL status
734   */
HAL_CRYP_Encrypt(CRYP_HandleTypeDef * hcryp,uint32_t * Input,uint16_t Size,uint32_t * Output,uint32_t Timeout)735 HAL_StatusTypeDef HAL_CRYP_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, uint32_t Timeout)
736 {
737   uint32_t algo;
738   HAL_StatusTypeDef status;
739 
740   if(hcryp->State == HAL_CRYP_STATE_READY)
741   {
742     /* Change state Busy */
743     hcryp->State = HAL_CRYP_STATE_BUSY;
744 
745     /* Process locked */
746     __HAL_LOCK(hcryp);
747 
748     /*  Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
749     hcryp->CrypInCount = 0U;
750     hcryp->CrypOutCount = 0U;
751     hcryp->pCrypInBuffPtr = Input;
752     hcryp->pCrypOutBuffPtr = Output;
753 
754     /*  Calculate Size parameter in Byte*/
755     if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
756     {
757       hcryp->Size = Size * 4U;
758     }
759     else
760     {
761       hcryp->Size = Size;
762     }
763 
764     /* Set Encryption operating mode*/
765     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
766 
767     /* algo get algorithm selected */
768     algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
769 
770     switch(algo)
771     {
772     case CRYP_DES_ECB:
773     case CRYP_DES_CBC:
774     case CRYP_TDES_ECB:
775     case CRYP_TDES_CBC:
776 
777       /*Set Key */
778       hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
779       hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
780       if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
781       {
782         hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
783         hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
784         hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
785         hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
786       }
787 
788       /*Set Initialization Vector (IV)*/
789       if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
790       {
791         hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
792         hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
793       }
794 
795       /* Flush FIFO */
796       HAL_CRYP_FIFO_FLUSH(hcryp);
797 
798       /* Set the phase */
799       hcryp->Phase = CRYP_PHASE_PROCESS;
800 
801       /* Start DES/TDES encryption process */
802       status = CRYP_TDES_Process(hcryp,Timeout);
803       break;
804 
805     case CRYP_AES_ECB:
806     case CRYP_AES_CBC:
807     case CRYP_AES_CTR:
808 
809       /* AES encryption */
810       status = CRYP_AES_Encrypt(hcryp, Timeout);
811       break;
812 
813     default:
814       hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
815       status = HAL_ERROR;
816       break;
817     }
818 
819     if (status == HAL_OK)
820     {
821       /* Change the CRYP peripheral state */
822       hcryp->State = HAL_CRYP_STATE_READY;
823 
824       /* Process unlocked */
825       __HAL_UNLOCK(hcryp);
826     }
827   }
828   else
829   {
830     /* Process unlocked */
831     __HAL_UNLOCK(hcryp);
832 
833     /* Busy error code field */
834     hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
835     status = HAL_ERROR;
836   }
837 
838   /* Return function status */
839   return status ;
840 }
841 
842 /**
843   * @brief  Decryption mode.
844   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
845   *         the configuration information for CRYP module
846   * @param  Input: Pointer to the input buffer (ciphertext )
847   * @param  Size: Length of the plaintext buffer in word.
848   * @param  Output: Pointer to the output buffer(plaintext)
849   * @param  Timeout: Specify Timeout value
850   * @retval HAL status
851   */
HAL_CRYP_Decrypt(CRYP_HandleTypeDef * hcryp,uint32_t * Input,uint16_t Size,uint32_t * Output,uint32_t Timeout)852 HAL_StatusTypeDef HAL_CRYP_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, uint32_t Timeout)
853 {
854   HAL_StatusTypeDef status;
855   uint32_t algo;
856 
857   if(hcryp->State == HAL_CRYP_STATE_READY)
858   {
859     /* Change state Busy */
860     hcryp->State = HAL_CRYP_STATE_BUSY;
861 
862     /* Process locked */
863     __HAL_LOCK(hcryp);
864 
865     /*  Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
866     hcryp->CrypInCount = 0U;
867     hcryp->CrypOutCount = 0U;
868     hcryp->pCrypInBuffPtr = Input;
869     hcryp->pCrypOutBuffPtr = Output;
870 
871     /*  Calculate Size parameter in Byte*/
872     if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
873     {
874       hcryp->Size = Size * 4U;
875     }
876     else
877     {
878       hcryp->Size = Size;
879     }
880 
881     /* Set Decryption operating mode*/
882     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT);
883 
884     /* algo get algorithm selected */
885     algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
886 
887     switch(algo)
888     {
889     case CRYP_DES_ECB:
890     case CRYP_DES_CBC:
891     case CRYP_TDES_ECB:
892     case CRYP_TDES_CBC:
893 
894       /*Set Key */
895       hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
896       hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
897       if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
898       {
899         hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
900         hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
901         hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
902         hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
903       }
904 
905       /*Set Initialization Vector (IV)*/
906       if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
907       {
908         hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
909         hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
910       }
911 
912       /* Flush FIFO */
913       HAL_CRYP_FIFO_FLUSH(hcryp);
914 
915       /* Set the phase */
916       hcryp->Phase = CRYP_PHASE_PROCESS;
917 
918       /* Start DES/TDES decryption process */
919       status = CRYP_TDES_Process(hcryp, Timeout);
920 
921       break;
922 
923     case CRYP_AES_ECB:
924     case CRYP_AES_CBC:
925     case CRYP_AES_CTR:
926 
927       /* AES decryption */
928       status = CRYP_AES_Decrypt(hcryp, Timeout);
929       break;
930 
931     default:
932       hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
933       status = HAL_ERROR;
934       break;
935     }
936 
937     if (status == HAL_OK)
938     {
939       /* Change the CRYP peripheral state */
940       hcryp->State = HAL_CRYP_STATE_READY;
941 
942       /* Process unlocked */
943       __HAL_UNLOCK(hcryp);
944     }
945   }
946   else
947   {
948     /* Process unlocked */
949     __HAL_UNLOCK(hcryp);
950 
951     /* Busy error code field */
952     hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
953     status = HAL_ERROR;
954   }
955 
956   /* Return function status */
957   return status;
958 }
959 
960 /**
961   * @brief  Encryption in interrupt mode.
962   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
963   *         the configuration information for CRYP module
964   * @param  Input: Pointer to the input buffer (plaintext)
965   * @param  Size: Length of the plaintext buffer in word
966   * @param  Output: Pointer to the output buffer(ciphertext)
967   * @retval HAL status
968   */
HAL_CRYP_Encrypt_IT(CRYP_HandleTypeDef * hcryp,uint32_t * Input,uint16_t Size,uint32_t * Output)969 HAL_StatusTypeDef HAL_CRYP_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
970 {
971   uint32_t algo;
972   HAL_StatusTypeDef status;
973 
974   if(hcryp->State == HAL_CRYP_STATE_READY)
975   {
976     /* Change state Busy */
977     hcryp->State = HAL_CRYP_STATE_BUSY;
978 
979     /* Process locked */
980     __HAL_LOCK(hcryp);
981 
982     /*  Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
983     hcryp->CrypInCount = 0U;
984     hcryp->CrypOutCount = 0U;
985     hcryp->pCrypInBuffPtr = Input;
986     hcryp->pCrypOutBuffPtr = Output;
987 
988     /*  Calculate Size parameter in Byte*/
989     if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
990     {
991       hcryp->Size = Size * 4U;
992     }
993     else
994     {
995       hcryp->Size = Size;
996     }
997 
998     /* Set encryption operating mode*/
999     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
1000 
1001     /* algo get algorithm selected */
1002     algo = (hcryp->Instance->CR & CRYP_CR_ALGOMODE);
1003 
1004     switch(algo)
1005     {
1006     case CRYP_DES_ECB:
1007     case CRYP_DES_CBC:
1008     case CRYP_TDES_ECB:
1009     case CRYP_TDES_CBC:
1010 
1011       /*Set Key */
1012       hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
1013       hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
1014       if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
1015       {
1016         hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
1017         hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
1018         hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
1019         hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
1020       }
1021       /* Set the Initialization Vector*/
1022       if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
1023       {
1024         hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
1025         hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
1026       }
1027 
1028       /* Flush FIFO */
1029       HAL_CRYP_FIFO_FLUSH(hcryp);
1030 
1031       /* Set the phase */
1032       hcryp->Phase = CRYP_PHASE_PROCESS;
1033 
1034       /* Enable interrupts */
1035       __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
1036 
1037       /* Enable CRYP to start DES/TDES process*/
1038       __HAL_CRYP_ENABLE(hcryp);
1039 
1040       status = HAL_OK;
1041       break;
1042 
1043     case CRYP_AES_ECB:
1044     case CRYP_AES_CBC:
1045     case CRYP_AES_CTR:
1046 
1047       status = CRYP_AES_Encrypt_IT(hcryp);
1048       break;
1049 
1050     default:
1051       hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
1052       status =  HAL_ERROR;
1053       break;
1054     }
1055   }
1056   else
1057   {
1058     /* Busy error code field */
1059     hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
1060     status =  HAL_ERROR;
1061   }
1062 
1063   /* Return function status */
1064   return status ;
1065 }
1066 
1067 /**
1068   * @brief  Decryption in itnterrupt mode.
1069   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1070   *         the configuration information for CRYP module
1071   * @param  Input: Pointer to the input buffer (ciphertext )
1072   * @param  Size: Length of the plaintext buffer in word.
1073   * @param  Output: Pointer to the output buffer(plaintext)
1074   * @retval HAL status
1075   */
HAL_CRYP_Decrypt_IT(CRYP_HandleTypeDef * hcryp,uint32_t * Input,uint16_t Size,uint32_t * Output)1076 HAL_StatusTypeDef HAL_CRYP_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
1077 {
1078   uint32_t algo;
1079   HAL_StatusTypeDef status = HAL_OK;
1080 
1081   if(hcryp->State == HAL_CRYP_STATE_READY)
1082   {
1083     /* Change state Busy */
1084     hcryp->State = HAL_CRYP_STATE_BUSY;
1085 
1086     /* Process locked */
1087     __HAL_LOCK(hcryp);
1088 
1089     /*  Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
1090     hcryp->CrypInCount = 0U;
1091     hcryp->CrypOutCount = 0U;
1092     hcryp->pCrypInBuffPtr = Input;
1093     hcryp->pCrypOutBuffPtr = Output;
1094 
1095     /*  Calculate Size parameter in Byte*/
1096     if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
1097     {
1098       hcryp->Size = Size * 4U;
1099     }
1100     else
1101     {
1102       hcryp->Size = Size;
1103     }
1104 
1105     /* Set decryption operating mode*/
1106     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR,CRYP_OPERATINGMODE_DECRYPT);
1107 
1108     /* algo get algorithm selected */
1109     algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
1110 
1111     switch(algo)
1112     {
1113     case CRYP_DES_ECB:
1114     case CRYP_DES_CBC:
1115     case CRYP_TDES_ECB:
1116     case CRYP_TDES_CBC:
1117 
1118       /*Set Key */
1119       hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
1120       hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
1121       if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
1122       {
1123         hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
1124         hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
1125         hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
1126         hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
1127       }
1128 
1129       /* Set the Initialization Vector*/
1130       if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
1131       {
1132         hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
1133         hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
1134       }
1135       /* Flush FIFO */
1136       HAL_CRYP_FIFO_FLUSH(hcryp);
1137 
1138       /* Set the phase */
1139       hcryp->Phase = CRYP_PHASE_PROCESS;
1140 
1141       /* Enable interrupts */
1142       __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
1143 
1144       /* Enable CRYP and start DES/TDES process*/
1145       __HAL_CRYP_ENABLE(hcryp);
1146 
1147       break;
1148 
1149     case CRYP_AES_ECB:
1150     case CRYP_AES_CBC:
1151     case CRYP_AES_CTR:
1152 
1153       /* AES decryption */
1154       status = CRYP_AES_Decrypt_IT(hcryp);
1155       break;
1156 
1157     default:
1158       hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
1159       status = HAL_ERROR;
1160       break;
1161     }
1162   }
1163   else
1164   {
1165     /* Busy error code field */
1166     hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
1167     status = HAL_ERROR;
1168   }
1169 
1170   /* Return function status */
1171   return status;
1172 }
1173 
1174 /**
1175   * @brief  Encryption in DMA mode.
1176   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1177   *         the configuration information for CRYP module
1178   * @param  Input: Pointer to the input buffer (plaintext)
1179   * @param  Size: Length of the plaintext buffer in word.
1180   * @param  Output: Pointer to the output buffer(ciphertext)
1181   * @retval HAL status
1182   */
HAL_CRYP_Encrypt_DMA(CRYP_HandleTypeDef * hcryp,uint32_t * Input,uint16_t Size,uint32_t * Output)1183 HAL_StatusTypeDef HAL_CRYP_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
1184 {
1185   uint32_t algo;
1186   HAL_StatusTypeDef status = HAL_OK;
1187 
1188   if(hcryp->State == HAL_CRYP_STATE_READY)
1189   {
1190     /* Change state Busy */
1191     hcryp->State = HAL_CRYP_STATE_BUSY;
1192 
1193     /* Process locked */
1194     __HAL_LOCK(hcryp);
1195 
1196     /*  Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
1197     hcryp->CrypInCount = 0U;
1198     hcryp->CrypOutCount = 0U;
1199     hcryp->pCrypInBuffPtr = Input;
1200     hcryp->pCrypOutBuffPtr = Output;
1201 
1202     /*  Calculate Size parameter in Byte*/
1203     if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
1204     {
1205       hcryp->Size = Size * 4U;
1206     }
1207     else
1208     {
1209       hcryp->Size = Size;
1210     }
1211 
1212     /* Set encryption operating mode*/
1213     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
1214 
1215     /* algo get algorithm selected */
1216     algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
1217 
1218     switch(algo)
1219     {
1220     case CRYP_DES_ECB:
1221     case CRYP_DES_CBC:
1222     case CRYP_TDES_ECB:
1223     case CRYP_TDES_CBC:
1224 
1225       /*Set Key */
1226       hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
1227       hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
1228       if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
1229       {
1230         hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
1231         hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
1232         hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
1233         hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
1234       }
1235 
1236       /* Set the Initialization Vector*/
1237       if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
1238       {
1239         hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
1240         hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
1241       }
1242 
1243       /* Flush FIFO */
1244       HAL_CRYP_FIFO_FLUSH(hcryp);
1245 
1246       /* Set the phase */
1247       hcryp->Phase = CRYP_PHASE_PROCESS;
1248 
1249       /* Start DMA process transfer for DES/TDES */
1250       CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), (hcryp->Size/4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
1251 
1252       break;
1253 
1254     case CRYP_AES_ECB:
1255     case CRYP_AES_CBC:
1256     case CRYP_AES_CTR:
1257 
1258       /*  Set the Key*/
1259       CRYP_SetKey(hcryp, hcryp->Init.KeySize);
1260 
1261       /* Set the Initialization Vector IV */
1262       if (hcryp->Init.Algorithm != CRYP_AES_ECB)
1263       {
1264         hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
1265         hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
1266         hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
1267         hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
1268       }
1269 
1270       /* Set the phase */
1271       hcryp->Phase = CRYP_PHASE_PROCESS;
1272 
1273       /* Start DMA process transfer for AES */
1274       CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), (hcryp->Size/4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
1275       break;
1276 
1277     default:
1278       hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
1279       status =  HAL_ERROR;
1280       break;
1281     }
1282   }
1283   else
1284   {
1285     /* Busy error code field */
1286     hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
1287     status =  HAL_ERROR;
1288   }
1289 
1290   /* Return function status */
1291   return status;
1292 }
1293 
1294 /**
1295   * @brief  Decryption in DMA mode.
1296   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1297   *         the configuration information for CRYP module
1298   * @param  Input: Pointer to the input buffer (ciphertext )
1299   * @param  Size: Length of the plaintext buffer in word
1300   * @param  Output: Pointer to the output buffer(plaintext)
1301   * @retval HAL status
1302   */
HAL_CRYP_Decrypt_DMA(CRYP_HandleTypeDef * hcryp,uint32_t * Input,uint16_t Size,uint32_t * Output)1303 HAL_StatusTypeDef HAL_CRYP_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
1304 {
1305   uint32_t algo;
1306   HAL_StatusTypeDef status = HAL_OK;
1307 
1308   if(hcryp->State == HAL_CRYP_STATE_READY)
1309   {
1310     /* Change state Busy */
1311     hcryp->State = HAL_CRYP_STATE_BUSY;
1312 
1313     /* Process locked */
1314     __HAL_LOCK(hcryp);
1315 
1316     /*  Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
1317     hcryp->CrypInCount = 0U;
1318     hcryp->CrypOutCount = 0U;
1319     hcryp->pCrypInBuffPtr = Input;
1320     hcryp->pCrypOutBuffPtr = Output;
1321 
1322     /*  Calculate Size parameter in Byte*/
1323     if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
1324     {
1325       hcryp->Size = Size * 4U;
1326     }
1327     else
1328     {
1329       hcryp->Size = Size;
1330     }
1331 
1332     /* Set decryption operating mode*/
1333     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT);
1334 
1335     /* algo get algorithm selected */
1336     algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
1337 
1338     switch(algo)
1339     {
1340     case CRYP_DES_ECB:
1341     case CRYP_DES_CBC:
1342     case CRYP_TDES_ECB:
1343     case CRYP_TDES_CBC:
1344 
1345       /*Set Key */
1346       hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
1347       hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
1348       if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
1349       {
1350         hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
1351         hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
1352         hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
1353         hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
1354       }
1355 
1356       /* Set the Initialization Vector*/
1357       if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
1358       {
1359         hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
1360         hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
1361       }
1362 
1363       /* Flush FIFO */
1364       HAL_CRYP_FIFO_FLUSH(hcryp);
1365 
1366       /* Set the phase */
1367       hcryp->Phase = CRYP_PHASE_PROCESS;
1368 
1369       /* Start DMA process transfer for DES/TDES */
1370       CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), (hcryp->Size/4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
1371       break;
1372 
1373     case CRYP_AES_ECB:
1374     case CRYP_AES_CBC:
1375     case CRYP_AES_CTR:
1376 
1377       /* AES decryption */
1378       status = CRYP_AES_Decrypt_DMA(hcryp);
1379       break;
1380 
1381     default:
1382       hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
1383       status =  HAL_ERROR;
1384       break;
1385     }
1386   }
1387   else
1388   {
1389     /* Busy error code field */
1390     hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
1391     status = HAL_ERROR;
1392   }
1393 
1394   /* Return function status */
1395   return status;
1396 }
1397 
1398 /**
1399   * @}
1400   */
1401 
1402 /** @defgroup CRYP_Exported_Functions_Group3 CRYP IRQ handler management
1403  *  @brief    CRYP IRQ handler.
1404  *
1405 @verbatim
1406   ==============================================================================
1407                 ##### CRYP IRQ handler management #####
1408   ==============================================================================
1409 [..]  This section provides CRYP IRQ handler and callback functions.
1410       (+) HAL_CRYP_IRQHandler CRYP interrupt request
1411       (+) HAL_CRYP_InCpltCallback input data transfer complete callback
1412       (+) HAL_CRYP_OutCpltCallback output data transfer complete callback
1413       (+) HAL_CRYP_ErrorCallback  CRYP error callback
1414       (+) HAL_CRYP_GetState return the CRYP state
1415       (+) HAL_CRYP_GetError return the CRYP error code
1416 @endverbatim
1417   * @{
1418   */
1419 
1420 /**
1421   * @brief  This function handles cryptographic interrupt request.
1422   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1423   *         the configuration information for CRYP module
1424   * @retval None
1425   */
HAL_CRYP_IRQHandler(CRYP_HandleTypeDef * hcryp)1426 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
1427 {
1428 
1429   if((__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI) != 0U) || (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI) != 0U))
1430   {
1431     if ((hcryp->Init.Algorithm == CRYP_DES_ECB)|| (hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
1432     {
1433       CRYP_TDES_IT(hcryp); /* DES or TDES*/
1434     }
1435     else if((hcryp->Init.Algorithm == CRYP_AES_ECB) || (hcryp->Init.Algorithm == CRYP_AES_CBC) || (hcryp->Init.Algorithm == CRYP_AES_CTR))
1436     {
1437       CRYP_AES_IT(hcryp); /*AES*/
1438     }
1439     else
1440     {
1441       /* Nothing to do */
1442     }
1443   }
1444 }
1445 
1446 /**
1447   * @brief  Return the CRYP error code.
1448   * @param  hcryp : pointer to a CRYP_HandleTypeDef structure that contains
1449   *                 the configuration information for the  CRYP IP
1450   * @retval CRYP error code
1451   */
HAL_CRYP_GetError(CRYP_HandleTypeDef * hcryp)1452 uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
1453 {
1454   return hcryp->ErrorCode;
1455 }
1456 
1457 /**
1458   * @brief  Returns the CRYP state.
1459   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1460   *         the configuration information for CRYP module.
1461   * @retval HAL state
1462   */
HAL_CRYP_GetState(CRYP_HandleTypeDef * hcryp)1463 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
1464 {
1465   return hcryp->State;
1466 }
1467 
1468 /**
1469   * @brief  Input FIFO transfer completed callback.
1470   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1471   *         the configuration information for CRYP module.
1472   * @retval None
1473   */
HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef * hcryp)1474 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
1475 {
1476   /* Prevent unused argument(s) compilation warning */
1477   UNUSED(hcryp);
1478 
1479   /* NOTE : This function Should not be modified, when the callback is needed,
1480             the HAL_CRYP_InCpltCallback could be implemented in the user file
1481    */
1482 }
1483 
1484 /**
1485   * @brief  Output FIFO transfer completed callback.
1486   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1487   *         the configuration information for CRYP module.
1488   * @retval None
1489   */
HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef * hcryp)1490 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
1491 {
1492   /* Prevent unused argument(s) compilation warning */
1493   UNUSED(hcryp);
1494 
1495   /* NOTE : This function Should not be modified, when the callback is needed,
1496             the HAL_CRYP_OutCpltCallback could be implemented in the user file
1497    */
1498 }
1499 
1500 /**
1501   * @brief  CRYP error callback.
1502   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1503   *         the configuration information for CRYP module.
1504   * @retval None
1505   */
HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef * hcryp)1506  __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
1507 {
1508   /* Prevent unused argument(s) compilation warning */
1509   UNUSED(hcryp);
1510 
1511   /* NOTE : This function Should not be modified, when the callback is needed,
1512             the HAL_CRYP_ErrorCallback could be implemented in the user file
1513    */
1514 }
1515 /**
1516   * @}
1517   */
1518 
1519 /* Private functions ---------------------------------------------------------*/
1520 /** @addtogroup CRYP_Private_Functions
1521   * @{
1522   */
1523 
1524 /**
1525   * @brief  Encryption in ECB/CBC Algorithm with DES/TDES standard.
1526   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1527   *         the configuration information for CRYP module
1528   * @param  Timeout: Timeout value
1529   * @retval HAL status
1530   */
CRYP_TDES_Process(CRYP_HandleTypeDef * hcryp,uint32_t Timeout)1531 static HAL_StatusTypeDef CRYP_TDES_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
1532 {
1533 
1534   uint32_t temp;  /* Temporary CrypOutBuff */
1535   uint16_t incount; /* Temporary CrypInCount Value */
1536   uint16_t outcount;  /* Temporary CrypOutCount Value */
1537 
1538   /* Enable CRYP */
1539   __HAL_CRYP_ENABLE(hcryp);
1540   /*Temporary CrypOutCount Value*/
1541   outcount = hcryp->CrypOutCount;
1542 
1543   /*Start processing*/
1544   while((hcryp->CrypInCount < (hcryp->Size/4U)) && (outcount < (hcryp->Size/4U)))
1545   {
1546     /* Temporary CrypInCount Value */
1547     incount = hcryp->CrypInCount;
1548     /* Write plain data and get cipher data */
1549     if(((hcryp->Instance->SR & CRYP_FLAG_IFNF ) != 0x0U) && (incount < (hcryp->Size/4U)))
1550     {
1551       /* Write the input block in the IN FIFO */
1552       hcryp->Instance->DIN  = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
1553       hcryp->CrypInCount++;
1554       hcryp->Instance->DIN  = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
1555       hcryp->CrypInCount++;
1556     }
1557 
1558     /* Wait for OFNE flag to be raised */
1559     if(CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
1560     {
1561       /* Disable the CRYP peripheral clock */
1562       __HAL_CRYP_DISABLE(hcryp);
1563 
1564       /* Change state & errorCode*/
1565       hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
1566       hcryp->State = HAL_CRYP_STATE_READY;
1567 
1568       /* Process unlocked */
1569       __HAL_UNLOCK(hcryp);
1570 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1571       /*Call registered error callback*/
1572       hcryp->ErrorCallback(hcryp);
1573 #else
1574       /*Call legacy weak error callback*/
1575       HAL_CRYP_ErrorCallback(hcryp);
1576 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1577     }
1578 
1579     /*Temporary CrypOutCount Value*/
1580     outcount = hcryp->CrypOutCount;
1581 
1582     if(((hcryp->Instance->SR & CRYP_FLAG_OFNE ) != 0x0U) && (outcount < (hcryp->Size/4U)))
1583     {
1584       /* Read the output block from the Output FIFO and put them in temporary Buffer then get CrypOutBuff from temporary buffer  */
1585       temp = hcryp->Instance->DOUT;
1586       *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = temp;
1587       hcryp->CrypOutCount++;
1588       temp = hcryp->Instance->DOUT;
1589       *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = temp;
1590       hcryp->CrypOutCount++;
1591     }
1592     /*Temporary CrypOutCount Value*/
1593     outcount = hcryp->CrypOutCount;
1594   }
1595   /* Disable CRYP */
1596   __HAL_CRYP_DISABLE(hcryp);
1597   /* Change the CRYP state */
1598   hcryp->State = HAL_CRYP_STATE_READY;
1599 
1600   /* Return function status */
1601   return HAL_OK;
1602 }
1603 
1604 /**
1605   * @brief  CRYP block input/output data handling under interruption with DES/TDES standard.
1606   * @note   The function is called under interruption only, once
1607   *         interruptions have been enabled by CRYP_Decrypt_IT() and CRYP_Encrypt_IT().
1608   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1609   *         the configuration information for CRYP module.
1610   * @retval HAL status
1611   */
CRYP_TDES_IT(CRYP_HandleTypeDef * hcryp)1612 static void CRYP_TDES_IT(CRYP_HandleTypeDef *hcryp)
1613 {
1614   uint32_t temp;  /* Temporary CrypOutBuff */
1615 
1616   if(hcryp->State == HAL_CRYP_STATE_BUSY)
1617   {
1618     if((__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI) != 0x0U) && (__HAL_CRYP_GET_FLAG(hcryp, CRYP_FLAG_INRIS) != 0x0U))
1619 
1620     {
1621       /* Write input block in the IN FIFO */
1622       hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
1623       hcryp->CrypInCount++;
1624       hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
1625       hcryp->CrypInCount++;
1626 
1627       if(hcryp->CrypInCount ==  (hcryp->Size/4U))
1628       {
1629         /* Disable interruption */
1630         __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
1631 
1632         /* Call the input data transfer complete callback */
1633 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1634         /*Call registered Input complete callback*/
1635         hcryp->InCpltCallback(hcryp);
1636 #else
1637         /*Call legacy weak Input complete callback*/
1638         HAL_CRYP_InCpltCallback(hcryp);
1639 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1640       }
1641     }
1642     if((__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI) != 0x0U)&& (__HAL_CRYP_GET_FLAG(hcryp, CRYP_FLAG_OUTRIS) != 0x0U))
1643     {
1644       /* Read the output block from the Output FIFO and put them in temporary Buffer then get CrypOutBuff from temporary buffer  */
1645       temp = hcryp->Instance->DOUT;
1646       *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = temp;
1647       hcryp->CrypOutCount++;
1648       temp = hcryp->Instance->DOUT;
1649       *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = temp;
1650       hcryp->CrypOutCount++;
1651       if(hcryp->CrypOutCount ==  (hcryp->Size/4U))
1652       {
1653         /* Disable interruption */
1654         __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
1655 
1656         /* Disable CRYP */
1657         __HAL_CRYP_DISABLE(hcryp);
1658 
1659         /* Process unlocked */
1660         __HAL_UNLOCK(hcryp);
1661 
1662         /* Change the CRYP state */
1663         hcryp->State = HAL_CRYP_STATE_READY;
1664 
1665         /* Call output transfer complete callback */
1666 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1667         /*Call registered Output complete callback*/
1668         hcryp->OutCpltCallback(hcryp);
1669 #else
1670         /*Call legacy weak Output complete callback*/
1671         HAL_CRYP_OutCpltCallback(hcryp);
1672 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1673 
1674       }
1675     }
1676   }
1677   else
1678   {
1679     /* Process unlocked */
1680     __HAL_UNLOCK(hcryp);
1681     /* Busy error code field */
1682     hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
1683 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1684     /*Call registered error callback*/
1685     hcryp->ErrorCallback(hcryp);
1686 #else
1687     /*Call legacy weak error callback*/
1688     HAL_CRYP_ErrorCallback(hcryp);
1689 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1690   }
1691 }
1692 
1693 /**
1694   * @brief  Encryption in ECB/CBC & CTR Algorithm with AES Standard
1695   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure
1696   * @param  Timeout: specify Timeout value
1697   * @retval HAL status
1698   */
CRYP_AES_Encrypt(CRYP_HandleTypeDef * hcryp,uint32_t Timeout)1699 static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
1700 {
1701   uint16_t outcount;  /* Temporary CrypOutCount Value */
1702 
1703   /*  Set the Key*/
1704   CRYP_SetKey(hcryp, hcryp->Init.KeySize);
1705 
1706   if (hcryp->Init.Algorithm != CRYP_AES_ECB)
1707   {
1708     /* Set the Initialization Vector*/
1709     hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
1710     hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
1711     hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
1712     hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
1713   }
1714 
1715   /* Set the phase */
1716   hcryp->Phase = CRYP_PHASE_PROCESS;
1717 
1718   /* Enable CRYP */
1719   __HAL_CRYP_ENABLE(hcryp);
1720    /*Temporary CrypOutCount Value*/
1721   outcount = hcryp->CrypOutCount;
1722 
1723   while((hcryp->CrypInCount < (hcryp->Size/4U)) && (outcount < (hcryp->Size/4U)))
1724   {
1725     /* Write plain Ddta and get cipher data */
1726     CRYP_AES_ProcessData(hcryp,Timeout);
1727     /*Temporary CrypOutCount Value*/
1728     outcount = hcryp->CrypOutCount;
1729   }
1730 
1731   /* Disable CRYP */
1732   __HAL_CRYP_DISABLE(hcryp);
1733 
1734   /* Change the CRYP state */
1735   hcryp->State = HAL_CRYP_STATE_READY;
1736 
1737   /* Return function status */
1738   return HAL_OK;
1739 }
1740 
1741 /**
1742   * @brief  Encryption in ECB/CBC & CTR mode with AES Standard using interrupt mode
1743   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1744   *         the configuration information for CRYP module
1745   * @retval HAL status
1746   */
CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef * hcryp)1747 static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp)
1748 {
1749 
1750   /*  Set the Key*/
1751   CRYP_SetKey(hcryp, hcryp->Init.KeySize);
1752 
1753   if (hcryp->Init.Algorithm != CRYP_AES_ECB)
1754   {
1755     /* Set the Initialization Vector*/
1756     hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
1757     hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
1758     hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
1759     hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
1760   }
1761   /* Set the phase */
1762   hcryp->Phase = CRYP_PHASE_PROCESS;
1763 
1764   if(hcryp->Size != 0U)
1765   {
1766     /* Enable interrupts */
1767     __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
1768 
1769     /* Enable CRYP */
1770     __HAL_CRYP_ENABLE(hcryp);
1771   }
1772   else
1773   {
1774     /* Change the CRYP state */
1775     hcryp->State = HAL_CRYP_STATE_READY;
1776 
1777     /* Process unlocked */
1778     __HAL_UNLOCK(hcryp);
1779   }
1780 
1781   /* Return function status */
1782   return HAL_OK;
1783 }
1784 
1785 /**
1786   * @brief  Decryption in ECB/CBC & CTR mode with AES Standard
1787   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure
1788   * @param  Timeout: Specify Timeout value
1789   * @retval HAL status
1790   */
CRYP_AES_Decrypt(CRYP_HandleTypeDef * hcryp,uint32_t Timeout)1791 static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout )
1792 {
1793   uint16_t outcount;  /* Temporary CrypOutCount Value */
1794 
1795   /*  Key preparation for ECB/CBC */
1796   if (hcryp->Init.Algorithm != CRYP_AES_CTR)   /*ECB or CBC*/
1797   {
1798     /* change ALGOMODE to key preparation for decryption*/
1799     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY );
1800 
1801     /*  Set the Key*/
1802     CRYP_SetKey(hcryp, hcryp->Init.KeySize);
1803 
1804     /* Enable CRYP */
1805     __HAL_CRYP_ENABLE(hcryp);
1806 
1807     /* Wait for BUSY flag to be raised */
1808     if(CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK)
1809     {
1810       /* Disable the CRYP peripheral clock */
1811       __HAL_CRYP_DISABLE(hcryp);
1812 
1813       /* Change state */
1814       hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
1815       hcryp->State = HAL_CRYP_STATE_READY;
1816 
1817       /* Process unlocked */
1818       __HAL_UNLOCK(hcryp);
1819       return HAL_ERROR;
1820     }
1821     /* Turn back to ALGOMODE of the configuration */
1822     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm );
1823   }
1824   else  /*Algorithm CTR */
1825   {
1826     /*  Set the Key*/
1827     CRYP_SetKey(hcryp, hcryp->Init.KeySize);
1828   }
1829 
1830   /* Set IV */
1831   if (hcryp->Init.Algorithm != CRYP_AES_ECB)
1832   {
1833     /* Set the Initialization Vector*/
1834     hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
1835     hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
1836     hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
1837     hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
1838   }
1839   /* Set the phase */
1840   hcryp->Phase = CRYP_PHASE_PROCESS;
1841 
1842   /* Enable CRYP */
1843   __HAL_CRYP_ENABLE(hcryp);
1844 
1845   /*Temporary CrypOutCount Value*/
1846   outcount = hcryp->CrypOutCount;
1847 
1848   while((hcryp->CrypInCount < (hcryp->Size/4U)) && (outcount < (hcryp->Size/4U)))
1849   {
1850     /* Write plain data and get cipher data */
1851     CRYP_AES_ProcessData(hcryp,Timeout);
1852     /*Temporary CrypOutCount Value*/
1853     outcount = hcryp->CrypOutCount;
1854   }
1855 
1856   /* Disable CRYP */
1857   __HAL_CRYP_DISABLE(hcryp);
1858 
1859   /* Change the CRYP state */
1860   hcryp->State = HAL_CRYP_STATE_READY;
1861 
1862   /* Return function status */
1863   return HAL_OK;
1864 }
1865 /**
1866   * @brief  Decryption in ECB/CBC & CTR mode with AES Standard using interrupt mode
1867   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1868   *         the configuration information for CRYP module
1869   * @retval HAL status
1870   */
CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef * hcryp)1871 static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp)
1872 {
1873   __IO uint32_t count = 0U;
1874 
1875   /*  Key preparation for ECB/CBC */
1876   if (hcryp->Init.Algorithm != CRYP_AES_CTR)
1877   {
1878     /* change ALGOMODE to key preparation for decryption*/
1879     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY );
1880 
1881     /*  Set the Key*/
1882     CRYP_SetKey(hcryp, hcryp->Init.KeySize);
1883 
1884     /* Enable CRYP */
1885     __HAL_CRYP_ENABLE(hcryp);
1886 
1887     /* Wait for BUSY flag to be raised */
1888     count = CRYP_TIMEOUT_KEYPREPARATION;
1889     do
1890     {
1891       count-- ;
1892       if(count == 0U)
1893       {
1894         /* Change state */
1895         hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
1896         hcryp->State = HAL_CRYP_STATE_READY;
1897 
1898         /* Process unlocked */
1899         __HAL_UNLOCK(hcryp);
1900         return HAL_ERROR;
1901       }
1902     }
1903     while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
1904 
1905     /* Turn back to ALGOMODE of the configuration */
1906     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm );
1907   }
1908   else  /*Algorithm CTR */
1909   {
1910     /*  Set the Key*/
1911     CRYP_SetKey(hcryp, hcryp->Init.KeySize);
1912   }
1913 
1914   /* Set IV */
1915   if (hcryp->Init.Algorithm != CRYP_AES_ECB)
1916   {
1917     /* Set the Initialization Vector*/
1918     hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
1919     hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
1920     hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
1921     hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
1922   }
1923   /* Set the phase */
1924   hcryp->Phase = CRYP_PHASE_PROCESS;
1925   if(hcryp->Size != 0U)
1926   {
1927     /* Enable interrupts */
1928     __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
1929 
1930     /* Enable CRYP */
1931     __HAL_CRYP_ENABLE(hcryp);
1932   }
1933   else
1934   {
1935     /* Process locked */
1936     __HAL_UNLOCK(hcryp);
1937 
1938     /* Change the CRYP state */
1939     hcryp->State = HAL_CRYP_STATE_READY;
1940   }
1941   /* Return function status */
1942   return HAL_OK;
1943 }
1944 /**
1945   * @brief  Decryption in ECB/CBC & CTR mode with AES Standard using DMA mode
1946   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1947   *         the configuration information for CRYP module
1948   * @retval HAL status
1949   */
CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef * hcryp)1950 static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp)
1951 {
1952   __IO uint32_t count = 0U;
1953 
1954   /*  Key preparation for ECB/CBC */
1955   if (hcryp->Init.Algorithm != CRYP_AES_CTR)
1956   {
1957     /* change ALGOMODE to key preparation for decryption*/
1958     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY );
1959 
1960     /*  Set the Key*/
1961     CRYP_SetKey(hcryp, hcryp->Init.KeySize);
1962 
1963     /* Enable CRYP */
1964     __HAL_CRYP_ENABLE(hcryp);
1965 
1966     /* Wait for BUSY flag to be raised */
1967     count = CRYP_TIMEOUT_KEYPREPARATION;
1968     do
1969     {
1970       count-- ;
1971       if(count == 0U)
1972       {
1973         /* Disable the CRYP peripheral clock */
1974         __HAL_CRYP_DISABLE(hcryp);
1975 
1976         /* Change state */
1977         hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
1978         hcryp->State = HAL_CRYP_STATE_READY;
1979 
1980         /* Process unlocked */
1981         __HAL_UNLOCK(hcryp);
1982         return HAL_ERROR;
1983       }
1984     }
1985     while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
1986 
1987     /* Turn back to ALGOMODE of the configuration */
1988     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm );
1989   }
1990   else  /*Algorithm CTR */
1991   {
1992     /*  Set the Key*/
1993     CRYP_SetKey(hcryp, hcryp->Init.KeySize);
1994   }
1995 
1996   if (hcryp->Init.Algorithm != CRYP_AES_ECB)
1997   {
1998     /* Set the Initialization Vector*/
1999     hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
2000     hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
2001     hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
2002     hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
2003   }
2004   /* Set the phase */
2005   hcryp->Phase = CRYP_PHASE_PROCESS;
2006 
2007   if(hcryp->Size != 0U)
2008   {
2009     /* Set the input and output addresses and start DMA transfer */
2010     CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), (hcryp->Size/4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
2011   }
2012   else
2013   {
2014     /* Process unlocked */
2015     __HAL_UNLOCK(hcryp);
2016 
2017     /* Change the CRYP state */
2018     hcryp->State = HAL_CRYP_STATE_READY;
2019   }
2020 
2021   /* Return function status */
2022   return HAL_OK;
2023 }
2024 
2025 
2026 /**
2027   * @brief  DMA CRYP input data process complete callback.
2028   * @param  hdma: DMA handle
2029   * @retval None
2030   */
CRYP_DMAInCplt(DMA_HandleTypeDef * hdma)2031 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
2032 {
2033   CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2034 
2035   /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit
2036   in the DMACR register */
2037   hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN);
2038 
2039   /* Call input data transfer complete callback */
2040 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
2041         /*Call registered Input complete callback*/
2042         hcryp->InCpltCallback(hcryp);
2043 #else
2044         /*Call legacy weak Input complete callback*/
2045         HAL_CRYP_InCpltCallback(hcryp);
2046 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
2047 }
2048 
2049 /**
2050   * @brief  DMA CRYP output data process complete callback.
2051   * @param  hdma: DMA handle
2052   * @retval None
2053   */
CRYP_DMAOutCplt(DMA_HandleTypeDef * hdma)2054 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
2055 {
2056   CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2057 
2058 
2059   /* Disable the DMA transfer for output FIFO */
2060   hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN);
2061 
2062   /* Change the CRYP state to ready */
2063   hcryp->State = HAL_CRYP_STATE_READY;
2064 
2065   /* Process unlocked */
2066   __HAL_UNLOCK(hcryp);
2067 
2068     /* Disable CRYP */
2069   __HAL_CRYP_DISABLE(hcryp);
2070 
2071 
2072   /* Call output data transfer complete callback */
2073 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
2074   /*Call registered Output complete callback*/
2075   hcryp->OutCpltCallback(hcryp);
2076 #else
2077   /*Call legacy weak Output complete callback*/
2078   HAL_CRYP_OutCpltCallback(hcryp);
2079 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
2080 }
2081 
2082 /**
2083   * @brief  DMA CRYP communication error callback.
2084   * @param  hdma: DMA handle
2085   * @retval None
2086   */
CRYP_DMAError(DMA_HandleTypeDef * hdma)2087 static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
2088 {
2089   CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2090 
2091   /* Change the CRYP peripheral state */
2092   hcryp->State= HAL_CRYP_STATE_READY;
2093 
2094   /* DMA error code field */
2095   hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
2096 
2097   /* Call error callback */
2098 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
2099   /*Call registered error callback*/
2100   hcryp->ErrorCallback(hcryp);
2101 #else
2102   /*Call legacy weak error callback*/
2103   HAL_CRYP_ErrorCallback(hcryp);
2104 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
2105 }
2106 
2107 /**
2108   * @brief  Set the DMA configuration and start the DMA transfer
2109   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2110   *         the configuration information for CRYP module
2111   * @param  inputaddr: address of the input buffer
2112   * @param  Size: size of the input buffer, must be a multiple of 16.
2113   * @param  outputaddr: address of the output buffer
2114   * @retval None
2115   */
CRYP_SetDMAConfig(CRYP_HandleTypeDef * hcryp,uint32_t inputaddr,uint16_t Size,uint32_t outputaddr)2116 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
2117 {
2118   /* Set the CRYP DMA transfer complete callback */
2119   hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
2120 
2121   /* Set the DMA input error callback */
2122   hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
2123 
2124   /* Set the CRYP DMA transfer complete callback */
2125   hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
2126 
2127   /* Set the DMA output error callback */
2128   hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
2129 
2130   /* Enable CRYP */
2131   __HAL_CRYP_ENABLE(hcryp);
2132 
2133   /* Enable the input DMA Stream */
2134   if (HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DIN, Size)!=HAL_OK)
2135   {
2136     /* DMA error code field */
2137     hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
2138 
2139     /* Call error callback */
2140 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
2141     /*Call registered error callback*/
2142     hcryp->ErrorCallback(hcryp);
2143 #else
2144     /*Call legacy weak error callback*/
2145     HAL_CRYP_ErrorCallback(hcryp);
2146 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
2147   }
2148 
2149   /* Enable the output DMA Stream */
2150   if (HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size)!=HAL_OK)
2151   {
2152     /* DMA error code field */
2153     hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
2154 
2155     /* Call error callback */
2156 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
2157     /*Call registered error callback*/
2158     hcryp->ErrorCallback(hcryp);
2159 #else
2160     /*Call legacy weak error callback*/
2161     HAL_CRYP_ErrorCallback(hcryp);
2162 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
2163   }
2164   /* Enable In/Out DMA request */
2165   hcryp->Instance->DMACR = CRYP_DMACR_DOEN | CRYP_DMACR_DIEN;
2166 }
2167 
2168 /**
2169   * @brief  Process Data: Write Input data in polling mode and used in AES functions.
2170   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2171   *         the configuration information for CRYP module
2172   * @param  Timeout: Specify Timeout value
2173   * @retval None
2174   */
CRYP_AES_ProcessData(CRYP_HandleTypeDef * hcryp,uint32_t Timeout)2175 static void CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
2176 {
2177 
2178   uint32_t temp;  /* Temporary CrypOutBuff */
2179   uint16_t incount;  /* Temporary CrypInCount Value */
2180   uint16_t outcount;  /* Temporary CrypOutCount Value */
2181 
2182   /*Temporary CrypOutCount Value*/
2183   incount = hcryp->CrypInCount;
2184 
2185   if(((hcryp->Instance->SR & CRYP_FLAG_IFNF ) != 0x0U) && (incount < ((hcryp->Size)/4U)))
2186   {
2187     /* Write the input block in the IN FIFO */
2188     hcryp->Instance->DIN  = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
2189     hcryp->CrypInCount++;
2190     hcryp->Instance->DIN  = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
2191     hcryp->CrypInCount++;
2192     hcryp->Instance->DIN  = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
2193     hcryp->CrypInCount++;
2194     hcryp->Instance->DIN  = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
2195     hcryp->CrypInCount++;
2196   }
2197 
2198   /* Wait for OFNE flag to be raised */
2199   if(CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
2200   {
2201     /* Disable the CRYP peripheral clock */
2202     __HAL_CRYP_DISABLE(hcryp);
2203 
2204     /* Change state & error code*/
2205     hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
2206     hcryp->State = HAL_CRYP_STATE_READY;
2207 
2208     /* Process unlocked */
2209     __HAL_UNLOCK(hcryp);
2210 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
2211     /*Call registered error callback*/
2212     hcryp->ErrorCallback(hcryp);
2213 #else
2214     /*Call legacy weak error callback*/
2215     HAL_CRYP_ErrorCallback(hcryp);
2216 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
2217   }
2218   /*Temporary CrypOutCount Value*/
2219   outcount = hcryp->CrypOutCount;
2220 
2221   if(((hcryp->Instance->SR & CRYP_FLAG_OFNE ) != 0x0U) && (outcount < ((hcryp->Size)/4U)))
2222   {
2223     /* Read the output block from the Output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer  */
2224     temp  = hcryp->Instance->DOUT;
2225     *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
2226     hcryp->CrypOutCount++;
2227     temp  = hcryp->Instance->DOUT;
2228     *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
2229     hcryp->CrypOutCount++;
2230     temp  = hcryp->Instance->DOUT;
2231     *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
2232     hcryp->CrypOutCount++;
2233     temp  = hcryp->Instance->DOUT;
2234     *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
2235     hcryp->CrypOutCount++;
2236   }
2237 }
2238 
2239 /**
2240   * @brief  Handle CRYP block input/output data handling under interruption.
2241   * @note   The function is called under interruption only, once
2242   *         interruptions have been enabled by HAL_CRYP_Encrypt_IT or HAL_CRYP_Decrypt_IT.
2243   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2244   *         the configuration information for CRYP module.
2245   * @retval HAL status
2246   */
CRYP_AES_IT(CRYP_HandleTypeDef * hcryp)2247 static void CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
2248 {
2249   uint32_t temp;  /* Temporary CrypOutBuff */
2250   uint16_t incount; /* Temporary CrypInCount Value */
2251   uint16_t outcount;  /* Temporary CrypOutCount Value */
2252 
2253   if(hcryp->State == HAL_CRYP_STATE_BUSY)
2254   {
2255     /*Temporary CrypOutCount Value*/
2256     incount = hcryp->CrypInCount;
2257 
2258     if(((hcryp->Instance->SR & CRYP_FLAG_IFNF ) != 0x0U) && (incount < (hcryp->Size/4U)))
2259     {
2260       /* Write the input block in the IN FIFO */
2261       hcryp->Instance->DIN  = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
2262       hcryp->CrypInCount++;
2263       hcryp->Instance->DIN  = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
2264       hcryp->CrypInCount++;
2265       hcryp->Instance->DIN  = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
2266       hcryp->CrypInCount++;
2267       hcryp->Instance->DIN  = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
2268       hcryp->CrypInCount++;
2269       if(hcryp->CrypInCount ==  (hcryp->Size/4U))
2270       {
2271         /* Disable interrupts */
2272         __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
2273 
2274         /* Call the input data transfer complete callback */
2275 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
2276         /*Call registered Input complete callback*/
2277         hcryp->InCpltCallback(hcryp);
2278 #else
2279         /*Call legacy weak Input complete callback*/
2280         HAL_CRYP_InCpltCallback(hcryp);
2281 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
2282       }
2283     }
2284 
2285     /*Temporary CrypOutCount Value*/
2286     outcount = hcryp->CrypOutCount;
2287 
2288     if(((hcryp->Instance->SR & CRYP_FLAG_OFNE ) != 0x0U) && (outcount < (hcryp->Size/4U)))
2289     {
2290       /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer  */
2291       temp  = hcryp->Instance->DOUT;
2292       *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
2293       hcryp->CrypOutCount++;
2294       temp  = hcryp->Instance->DOUT;
2295       *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
2296       hcryp->CrypOutCount++;
2297       temp  = hcryp->Instance->DOUT;
2298       *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
2299       hcryp->CrypOutCount++;
2300       temp  = hcryp->Instance->DOUT;
2301       *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
2302       hcryp->CrypOutCount++;
2303       if(hcryp->CrypOutCount ==  (hcryp->Size/4U))
2304       {
2305         /* Disable interrupts */
2306         __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
2307 
2308         /* Change the CRYP state */
2309         hcryp->State = HAL_CRYP_STATE_READY;
2310 
2311         /* Disable CRYP */
2312         __HAL_CRYP_DISABLE(hcryp);
2313 
2314         /* Process unlocked */
2315         __HAL_UNLOCK(hcryp);
2316 
2317         /* Call output transfer complete callback */
2318 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
2319         /*Call registered Output complete callback*/
2320         hcryp->OutCpltCallback(hcryp);
2321 #else
2322         /*Call legacy weak Output complete callback*/
2323         HAL_CRYP_OutCpltCallback(hcryp);
2324 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
2325       }
2326     }
2327   }
2328   else
2329   {
2330     /* Process unlocked */
2331     __HAL_UNLOCK(hcryp);
2332     /* Busy error code field */
2333     hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
2334 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
2335     /*Call registered error callback*/
2336     hcryp->ErrorCallback(hcryp);
2337 #else
2338     /*Call legacy weak error callback*/
2339     HAL_CRYP_ErrorCallback(hcryp);
2340 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
2341   }
2342 }
2343 
2344 /**
2345   * @brief  Writes Key in Key registers.
2346   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2347   *         the configuration information for CRYP module
2348   * @param  KeySize: Size of Key
2349   * @retval None
2350   */
CRYP_SetKey(CRYP_HandleTypeDef * hcryp,uint32_t KeySize)2351 static void CRYP_SetKey( CRYP_HandleTypeDef *hcryp, uint32_t KeySize)
2352 {
2353   switch(KeySize)
2354   {
2355   case CRYP_KEYSIZE_256B:
2356     hcryp->Instance->K0LR = *(uint32_t*)(hcryp->Init.pKey);
2357     hcryp->Instance->K0RR = *(uint32_t*)(hcryp->Init.pKey+1);
2358     hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey+2);
2359     hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+3);
2360     hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+4);
2361     hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+5);
2362     hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+6);
2363     hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+7);
2364     break;
2365   case CRYP_KEYSIZE_192B:
2366     hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
2367     hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
2368     hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
2369     hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
2370     hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
2371     hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
2372     break;
2373   case CRYP_KEYSIZE_128B:
2374     hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey);
2375     hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+1);
2376     hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+2);
2377     hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+3);
2378 
2379     break;
2380   default:
2381     break;
2382   }
2383 }
2384 
2385 /**
2386   * @brief  Handle CRYP hardware block Timeout when waiting for BUSY flag to be raised.
2387   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2388   *         the configuration information for CRYP module.
2389   * @param  Timeout: Timeout duration.
2390   * @retval HAL status
2391   */
CRYP_WaitOnBUSYFlag(const CRYP_HandleTypeDef * hcryp,uint32_t Timeout)2392 static HAL_StatusTypeDef CRYP_WaitOnBUSYFlag(const CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
2393 {
2394   uint32_t tickstart;
2395 
2396   /* Get timeout */
2397   tickstart = HAL_GetTick();
2398 
2399   while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
2400   {
2401     /* Check for the Timeout */
2402     if(Timeout != HAL_MAX_DELAY)
2403     {
2404       if(((HAL_GetTick() - tickstart ) > Timeout) || (Timeout == 0U))
2405       {
2406         return HAL_ERROR;
2407       }
2408     }
2409   }
2410   return HAL_OK;
2411 }
2412 
2413 
2414 /**
2415   * @brief  Handle CRYP hardware block Timeout when waiting for OFNE flag to be raised.
2416   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2417   *         the configuration information for CRYP module.
2418   * @param  Timeout: Timeout duration.
2419   * @retval HAL status
2420   */
CRYP_WaitOnOFNEFlag(const CRYP_HandleTypeDef * hcryp,uint32_t Timeout)2421 static HAL_StatusTypeDef CRYP_WaitOnOFNEFlag(const CRYP_HandleTypeDef  *hcryp, uint32_t Timeout)
2422 {
2423   uint32_t tickstart;
2424 
2425   /* Get timeout */
2426   tickstart = HAL_GetTick();
2427 
2428   while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
2429   {
2430     /* Check for the Timeout */
2431     if(Timeout != HAL_MAX_DELAY)
2432     {
2433       if(((HAL_GetTick() - tickstart ) > Timeout) || (Timeout == 0U))
2434       {
2435         return HAL_ERROR;
2436       }
2437     }
2438   }
2439   return HAL_OK;
2440 }
2441 
2442 
2443 /**
2444   * @}
2445   */
2446 
2447 
2448 
2449 /**
2450   * @}
2451   */
2452 
2453 /**
2454   * @}
2455   */
2456 
2457 #endif /* HAL_CRYP_MODULE_ENABLED */
2458 
2459 
2460 /**
2461   * @}
2462   */
2463 #endif /*  CRYP*/
2464 /**
2465   * @}
2466   */
2467