]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL/ST_Library/stm32f7xx_hal_cryp.c
Update version number ready for V8.2.1 release.
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL / ST_Library / stm32f7xx_hal_cryp.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_cryp.c\r
4   * @author  MCD Application Team\r
5   * @version V0.3.0\r
6   * @date    06-March-2015\r
7   * @brief   CRYP HAL module driver.\r
8   *          This file provides firmware functions to manage the following \r
9   *          functionalities of the Cryptography (CRYP) peripheral:\r
10   *           + Initialization and de-initialization functions\r
11   *           + AES processing functions\r
12   *           + DES processing functions\r
13   *           + TDES processing functions\r
14   *           + DMA callback functions\r
15   *           + CRYP IRQ handler management\r
16   *           + Peripheral State functions\r
17   *\r
18   @verbatim\r
19   ==============================================================================\r
20                      ##### How to use this driver #####\r
21   ==============================================================================\r
22     [..]\r
23       The CRYP HAL driver can be used as follows:\r
24 \r
25       (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():\r
26          (##) Enable the CRYP interface clock using __CRYP_CLK_ENABLE()\r
27          (##) In case of using interrupts (e.g. HAL_CRYP_AESECB_Encrypt_IT())\r
28              (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()\r
29              (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()\r
30              (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()\r
31          (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_AESECB_Encrypt_DMA())\r
32              (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE()\r
33              (+++) Configure and enable two DMA streams one for managing data transfer from\r
34                  memory to peripheral (input stream) and another stream for managing data\r
35                  transfer from peripheral to memory (output stream)\r
36              (+++) Associate the initialized DMA handle to the CRYP DMA handle\r
37                  using  __HAL_LINKDMA()\r
38              (+++) Configure the priority and enable the NVIC for the transfer complete\r
39                  interrupt on the two DMA Streams. The output stream should have higher\r
40                  priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()\r
41     \r
42       (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly:\r
43          (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit\r
44          (##) The key size: 128, 192 and 256. This parameter is relevant only for AES\r
45          (##) The encryption/decryption key. It's size depends on the algorithm\r
46               used for encryption/decryption\r
47          (##) The initialization vector (counter). It is not used ECB mode.\r
48     \r
49       (#)Three processing (encryption/decryption) functions are available:\r
50          (##) Polling mode: encryption and decryption APIs are blocking functions\r
51               i.e. they process the data and wait till the processing is finished,\r
52               e.g. HAL_CRYP_AESCBC_Encrypt()\r
53          (##) Interrupt mode: encryption and decryption APIs are not blocking functions\r
54               i.e. they process the data under interrupt,\r
55               e.g. HAL_CRYP_AESCBC_Encrypt_IT()\r
56          (##) DMA mode: encryption and decryption APIs are not blocking functions\r
57               i.e. the data transfer is ensured by DMA,\r
58               e.g. HAL_CRYP_AESCBC_Encrypt_DMA()\r
59     \r
60       (#)When the processing function is called at first time after HAL_CRYP_Init()\r
61          the CRYP peripheral is initialized and processes the buffer in input.\r
62          At second call, the processing function performs an append of the already\r
63          processed buffer.\r
64          When a new data block is to be processed, call HAL_CRYP_Init() then the\r
65          processing function.\r
66     \r
67        (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.\r
68 \r
69   @endverbatim\r
70   ******************************************************************************\r
71   * @attention\r
72   *\r
73   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
74   *\r
75   * Redistribution and use in source and binary forms, with or without modification,\r
76   * are permitted provided that the following conditions are met:\r
77   *   1. Redistributions of source code must retain the above copyright notice,\r
78   *      this list of conditions and the following disclaimer.\r
79   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
80   *      this list of conditions and the following disclaimer in the documentation\r
81   *      and/or other materials provided with the distribution.\r
82   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
83   *      may be used to endorse or promote products derived from this software\r
84   *      without specific prior written permission.\r
85   *\r
86   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
87   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
88   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
89   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
90   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
91   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
92   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
93   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
94   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
95   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
96   *\r
97   ******************************************************************************\r
98   */ \r
99 \r
100 /* Includes ------------------------------------------------------------------*/\r
101 #include "stm32f7xx_hal.h"\r
102 \r
103 /** @addtogroup STM32F7xx_HAL_Driver\r
104   * @{\r
105   */\r
106 \r
107 /** @defgroup CRYP CRYP\r
108   * @brief CRYP HAL module driver.\r
109   * @{\r
110   */\r
111 \r
112 #ifdef HAL_CRYP_MODULE_ENABLED\r
113 \r
114 #if defined(STM32F756xx)\r
115 \r
116 /* Private typedef -----------------------------------------------------------*/\r
117 /* Private define ------------------------------------------------------------*/\r
118 /** @addtogroup CRYP_Private_define\r
119   * @{\r
120   */\r
121 #define CRYP_TIMEOUT_VALUE  1\r
122 /**\r
123   * @}\r
124   */ \r
125   \r
126 /* Private macro -------------------------------------------------------------*/\r
127 /* Private variables ---------------------------------------------------------*/\r
128 /* Private function prototypes -----------------------------------------------*/\r
129 /** @addtogroup CRYP_Private_Functions_prototypes\r
130   * @{\r
131   */  \r
132 static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize);\r
133 static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize);\r
134 static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);\r
135 static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);\r
136 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);\r
137 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);\r
138 static void CRYP_DMAError(DMA_HandleTypeDef *hdma);\r
139 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);\r
140 static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);\r
141 static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);\r
142 static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);\r
143 static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);\r
144 /**\r
145   * @}\r
146   */ \r
147 /* Private functions ---------------------------------------------------------*/\r
148 \r
149 /** @addtogroup CRYP_Private_Functions\r
150   * @{\r
151   */\r
152 \r
153 /**\r
154   * @brief  DMA CRYP Input Data process complete callback.\r
155   * @param  hdma: DMA handle\r
156   * @retval None\r
157   */\r
158 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)  \r
159 {\r
160   CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;\r
161   \r
162   /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit\r
163      in the DMACR register */\r
164   CRYP->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN);\r
165   \r
166   /* Call input data transfer complete callback */\r
167   HAL_CRYP_InCpltCallback(hcryp);\r
168 }\r
169 \r
170 /**\r
171   * @brief  DMA CRYP Output Data process complete callback.\r
172   * @param  hdma: DMA handle\r
173   * @retval None\r
174   */\r
175 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)\r
176 {\r
177   CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;\r
178   \r
179   /* Disable the DMA transfer for output FIFO request by resetting the DOEN bit\r
180      in the DMACR register */\r
181   CRYP->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN);\r
182   \r
183   /* Disable CRYP */\r
184   __HAL_CRYP_DISABLE();\r
185   \r
186   /* Change the CRYP state to ready */\r
187   hcryp->State = HAL_CRYP_STATE_READY;\r
188   \r
189   /* Call output data transfer complete callback */\r
190   HAL_CRYP_OutCpltCallback(hcryp);\r
191 }\r
192 \r
193 /**\r
194   * @brief  DMA CRYP communication error callback. \r
195   * @param  hdma: DMA handle\r
196   * @retval None\r
197   */\r
198 static void CRYP_DMAError(DMA_HandleTypeDef *hdma)\r
199 {\r
200   CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;\r
201   hcryp->State= HAL_CRYP_STATE_READY;\r
202   HAL_CRYP_ErrorCallback(hcryp);\r
203 }\r
204 \r
205 /**\r
206   * @brief  Writes the Key in Key registers. \r
207   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
208   *         the configuration information for CRYP module\r
209   * @param  Key: Pointer to Key buffer\r
210   * @param  KeySize: Size of Key\r
211   * @retval None\r
212   */\r
213 static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize)\r
214 {\r
215   uint32_t keyaddr = (uint32_t)Key;\r
216   \r
217   switch(KeySize)\r
218   {\r
219   case CRYP_KEYSIZE_256B:\r
220     /* Key Initialisation */\r
221     CRYP->K0LR = __REV(*(uint32_t*)(keyaddr));\r
222     keyaddr+=4;\r
223     CRYP->K0RR = __REV(*(uint32_t*)(keyaddr));\r
224     keyaddr+=4;\r
225     CRYP->K1LR = __REV(*(uint32_t*)(keyaddr));\r
226     keyaddr+=4;\r
227     CRYP->K1RR = __REV(*(uint32_t*)(keyaddr));\r
228     keyaddr+=4;\r
229     CRYP->K2LR = __REV(*(uint32_t*)(keyaddr));\r
230     keyaddr+=4;\r
231     CRYP->K2RR = __REV(*(uint32_t*)(keyaddr));\r
232     keyaddr+=4;\r
233     CRYP->K3LR = __REV(*(uint32_t*)(keyaddr));\r
234     keyaddr+=4;\r
235     CRYP->K3RR = __REV(*(uint32_t*)(keyaddr));\r
236     break;\r
237   case CRYP_KEYSIZE_192B:\r
238     CRYP->K1LR = __REV(*(uint32_t*)(keyaddr));\r
239     keyaddr+=4;\r
240     CRYP->K1RR = __REV(*(uint32_t*)(keyaddr));\r
241     keyaddr+=4;\r
242     CRYP->K2LR = __REV(*(uint32_t*)(keyaddr));\r
243     keyaddr+=4;\r
244     CRYP->K2RR = __REV(*(uint32_t*)(keyaddr));\r
245     keyaddr+=4;\r
246     CRYP->K3LR = __REV(*(uint32_t*)(keyaddr));\r
247     keyaddr+=4;\r
248     CRYP->K3RR = __REV(*(uint32_t*)(keyaddr));\r
249     break;\r
250   case CRYP_KEYSIZE_128B:       \r
251     CRYP->K2LR = __REV(*(uint32_t*)(keyaddr));\r
252     keyaddr+=4;\r
253     CRYP->K2RR = __REV(*(uint32_t*)(keyaddr));\r
254     keyaddr+=4;\r
255     CRYP->K3LR = __REV(*(uint32_t*)(keyaddr));\r
256     keyaddr+=4;\r
257     CRYP->K3RR = __REV(*(uint32_t*)(keyaddr));\r
258     break;\r
259   default:\r
260     break;\r
261   }\r
262 }\r
263 \r
264 /**\r
265   * @brief  Writes the InitVector/InitCounter in IV registers. \r
266   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
267   *         the configuration information for CRYP module\r
268   * @param  InitVector: Pointer to InitVector/InitCounter buffer\r
269   * @param  IVSize: Size of the InitVector/InitCounter\r
270   * @retval None\r
271   */\r
272 static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize)\r
273 {\r
274   uint32_t ivaddr = (uint32_t)InitVector;\r
275   \r
276   switch(IVSize)\r
277   {\r
278   case CRYP_KEYSIZE_128B:\r
279     CRYP->IV0LR = __REV(*(uint32_t*)(ivaddr));\r
280     ivaddr+=4;\r
281     CRYP->IV0RR = __REV(*(uint32_t*)(ivaddr));\r
282     ivaddr+=4;\r
283     CRYP->IV1LR = __REV(*(uint32_t*)(ivaddr));\r
284     ivaddr+=4;\r
285     CRYP->IV1RR = __REV(*(uint32_t*)(ivaddr));\r
286     break;\r
287     /* Whatever key size 192 or 256, Init vector is written in IV0LR and IV0RR */\r
288   case CRYP_KEYSIZE_192B:\r
289     CRYP->IV0LR = __REV(*(uint32_t*)(ivaddr));\r
290     ivaddr+=4;\r
291     CRYP->IV0RR = __REV(*(uint32_t*)(ivaddr));\r
292     break;\r
293   case CRYP_KEYSIZE_256B:\r
294     CRYP->IV0LR = __REV(*(uint32_t*)(ivaddr));\r
295     ivaddr+=4;\r
296     CRYP->IV0RR = __REV(*(uint32_t*)(ivaddr));\r
297     break;\r
298   default:\r
299     break;\r
300   }\r
301 }\r
302 \r
303 /**\r
304   * @brief  Process Data: Writes Input data in polling mode and read the output data\r
305   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
306   *         the configuration information for CRYP module\r
307   * @param  Input: Pointer to the Input buffer\r
308   * @param  Ilength: Length of the Input buffer, must be a multiple of 16.\r
309   * @param  Output: Pointer to the returned buffer\r
310   * @retval None\r
311   */\r
312 static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)\r
313 {\r
314   uint32_t tickstart = 0;\r
315   \r
316   uint32_t i = 0;\r
317   uint32_t inputaddr  = (uint32_t)Input;\r
318   uint32_t outputaddr = (uint32_t)Output;\r
319   \r
320   for(i=0; (i < Ilength); i+=16)\r
321   {\r
322     /* Write the Input block in the IN FIFO */\r
323     CRYP->DR = *(uint32_t*)(inputaddr);\r
324     inputaddr+=4;\r
325     CRYP->DR = *(uint32_t*)(inputaddr);\r
326     inputaddr+=4;\r
327     CRYP->DR  = *(uint32_t*)(inputaddr);\r
328     inputaddr+=4;\r
329     CRYP->DR = *(uint32_t*)(inputaddr);\r
330     inputaddr+=4;\r
331     \r
332     /* Get tick */\r
333     tickstart = HAL_GetTick();\r
334 \r
335     while(HAL_IS_BIT_CLR(CRYP->SR, CRYP_FLAG_OFNE))\r
336     {    \r
337       /* Check for the Timeout */\r
338       if(Timeout != HAL_MAX_DELAY)\r
339       {\r
340         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
341         {\r
342           /* Change state */\r
343           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
344           \r
345           /* Process Unlocked */\r
346           __HAL_UNLOCK(hcryp);\r
347         \r
348           return HAL_TIMEOUT;\r
349         }\r
350       }\r
351     }\r
352     /* Read the Output block from the Output FIFO */\r
353     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
354     outputaddr+=4;\r
355     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
356     outputaddr+=4;\r
357     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
358     outputaddr+=4;\r
359     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
360     outputaddr+=4;\r
361   }\r
362   /* Return function status */\r
363   return HAL_OK;\r
364 }\r
365 \r
366 /**\r
367   * @brief  Process Data: Write Input data in polling mode. \r
368   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
369   *         the configuration information for CRYP module\r
370   * @param  Input: Pointer to the Input buffer\r
371   * @param  Ilength: Length of the Input buffer, must be a multiple of 8\r
372   * @param  Output: Pointer to the returned buffer\r
373   * @param  Timeout: Specify Timeout value  \r
374   * @retval None\r
375   */\r
376 static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)\r
377 {\r
378   uint32_t tickstart = 0;   \r
379   \r
380   uint32_t i = 0;\r
381   uint32_t inputaddr  = (uint32_t)Input;\r
382   uint32_t outputaddr = (uint32_t)Output;\r
383   \r
384   for(i=0; (i < Ilength); i+=8)\r
385   {\r
386     /* Write the Input block in the IN FIFO */\r
387     CRYP->DR  = *(uint32_t*)(inputaddr);\r
388     inputaddr+=4;\r
389     CRYP->DR = *(uint32_t*)(inputaddr);\r
390     inputaddr+=4;\r
391     \r
392     /* Get tick */\r
393     tickstart = HAL_GetTick();\r
394     \r
395     while(HAL_IS_BIT_CLR(CRYP->SR, CRYP_FLAG_OFNE))\r
396     {\r
397       /* Check for the Timeout */\r
398       if(Timeout != HAL_MAX_DELAY)\r
399       {\r
400         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
401         {\r
402           /* Change state */\r
403           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
404           \r
405           /* Process Unlocked */          \r
406           __HAL_UNLOCK(hcryp);\r
407           \r
408           return HAL_TIMEOUT;\r
409         }\r
410       }\r
411     }\r
412     /* Read the Output block from the Output FIFO */\r
413     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
414     outputaddr+=4;\r
415     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
416     outputaddr+=4;\r
417   }\r
418   /* Return function status */\r
419   return HAL_OK;\r
420 }\r
421 \r
422 /**\r
423   * @brief  Set the DMA configuration and start the DMA transfer\r
424   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
425   *         the configuration information for CRYP module\r
426   * @param  inputaddr: address of the Input buffer\r
427   * @param  Size: Size of the Input buffer, must be a multiple of 16.\r
428   * @param  outputaddr: address of the Output buffer\r
429   * @retval None\r
430   */\r
431 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)\r
432 {\r
433   /* Set the CRYP DMA transfer complete callback */\r
434   hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;\r
435   /* Set the DMA error callback */\r
436   hcryp->hdmain->XferErrorCallback = CRYP_DMAError;\r
437   \r
438   /* Set the CRYP DMA transfer complete callback */\r
439   hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;\r
440   /* Set the DMA error callback */\r
441   hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;\r
442   \r
443   /* Enable CRYP */\r
444   __HAL_CRYP_ENABLE();\r
445   \r
446   /* Enable the DMA In DMA Stream */\r
447   HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&CRYP->DR, Size/4);\r
448   \r
449   /* Enable In DMA request */\r
450   CRYP->DMACR = (CRYP_DMACR_DIEN);\r
451   \r
452   /* Enable the DMA Out DMA Stream */\r
453   HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&CRYP->DOUT, outputaddr, Size/4);\r
454   \r
455   /* Enable Out DMA request */\r
456   CRYP->DMACR |= CRYP_DMACR_DOEN;\r
457  \r
458 }\r
459 \r
460 /**\r
461   * @brief  Sets the CRYP peripheral in DES ECB mode.\r
462   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
463   *         the configuration information for CRYP module\r
464   * @param  Direction: Encryption or decryption\r
465   * @retval None\r
466   */\r
467 static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)\r
468 {\r
469   /* Check if initialization phase has already been performed */\r
470   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
471   {\r
472     /* Set the CRYP peripheral in AES ECB mode */\r
473     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_DES_ECB | Direction);\r
474     \r
475     /* Set the key */\r
476     CRYP->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey));\r
477     CRYP->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4));\r
478     \r
479     /* Flush FIFO */\r
480     __HAL_CRYP_FIFO_FLUSH();\r
481     \r
482     /* Set the phase */\r
483     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
484   }\r
485 }\r
486 \r
487 /**\r
488   * @brief  Sets the CRYP peripheral in DES CBC mode.\r
489   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
490   *         the configuration information for CRYP module\r
491   * @param  Direction: Encryption or decryption\r
492   * @retval None\r
493   */\r
494 static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)\r
495 {\r
496   /* Check if initialization phase has already been performed */\r
497   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
498   {\r
499     /* Set the CRYP peripheral in AES ECB mode */\r
500     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_DES_CBC | Direction);\r
501     \r
502     /* Set the key */\r
503     CRYP->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey));\r
504     CRYP->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4));\r
505     \r
506     /* Set the Initialization Vector */\r
507     CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B);\r
508     \r
509     /* Flush FIFO */\r
510     __HAL_CRYP_FIFO_FLUSH();\r
511     \r
512     /* Set the phase */\r
513     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
514   }\r
515 }\r
516 \r
517 /**\r
518   * @brief  Sets the CRYP peripheral in TDES ECB mode.\r
519   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
520   *         the configuration information for CRYP module\r
521   * @param  Direction: Encryption or decryption\r
522   * @retval None\r
523   */\r
524 static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)\r
525 {\r
526   /* Check if initialization phase has already been performed */\r
527   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
528   {\r
529     /* Set the CRYP peripheral in AES ECB mode */\r
530     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_TDES_ECB | Direction);\r
531     \r
532     /* Set the key */\r
533     CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B);\r
534     \r
535     /* Flush FIFO */\r
536     __HAL_CRYP_FIFO_FLUSH();\r
537     \r
538     /* Set the phase */\r
539     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
540   }\r
541 }\r
542 \r
543 /**\r
544   * @brief  Sets the CRYP peripheral in TDES CBC mode\r
545   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
546   *         the configuration information for CRYP module\r
547   * @param  Direction: Encryption or decryption\r
548   * @retval None\r
549   */\r
550 static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)\r
551 {\r
552   /* Check if initialization phase has already been performed */\r
553   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
554   {\r
555     /* Set the CRYP peripheral in AES CBC mode */\r
556     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_TDES_CBC | Direction);\r
557     \r
558     /* Set the key */\r
559     CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B);\r
560     \r
561     /* Set the Initialization Vector */\r
562     CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B);\r
563     \r
564     /* Flush FIFO */\r
565     __HAL_CRYP_FIFO_FLUSH();\r
566     \r
567     /* Set the phase */\r
568     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
569   }\r
570 }\r
571 \r
572 /**\r
573   * @}\r
574   */ \r
575   \r
576  /* Exported functions --------------------------------------------------------*/\r
577 /** @addtogroup CRYP_Exported_Functions\r
578   * @{\r
579   */ \r
580   \r
581 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions \r
582  *  @brief    Initialization and Configuration functions. \r
583  *\r
584 @verbatim    \r
585   ==============================================================================\r
586               ##### Initialization and de-initialization functions #####\r
587   ==============================================================================\r
588     [..]  This section provides functions allowing to:\r
589       (+) Initialize the CRYP according to the specified parameters \r
590           in the CRYP_InitTypeDef and creates the associated handle\r
591       (+) DeInitialize the CRYP peripheral\r
592       (+) Initialize the CRYP MSP\r
593       (+) DeInitialize CRYP MSP \r
594  \r
595 @endverbatim\r
596   * @{\r
597   */\r
598 \r
599 /**\r
600   * @brief  Initializes the CRYP according to the specified\r
601   *         parameters in the CRYP_InitTypeDef and creates the associated handle.\r
602   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
603   *         the configuration information for CRYP module\r
604   * @retval HAL status\r
605   */\r
606 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)\r
607\r
608   /* Check the CRYP handle allocation */\r
609   if(hcryp == NULL)\r
610   {\r
611     return HAL_ERROR;\r
612   }\r
613 \r
614   /* Check the parameters */\r
615   assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));\r
616   assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));\r
617     \r
618   if(hcryp->State == HAL_CRYP_STATE_RESET)\r
619   {\r
620     /* Init the low level hardware */\r
621     HAL_CRYP_MspInit(hcryp);\r
622   }\r
623   \r
624   /* Change the CRYP state */\r
625   hcryp->State = HAL_CRYP_STATE_BUSY;\r
626   \r
627   /* Set the key size and data type*/\r
628   CRYP->CR = (uint32_t) (hcryp->Init.KeySize | hcryp->Init.DataType);\r
629   \r
630   /* Reset CrypInCount and CrypOutCount */\r
631   hcryp->CrypInCount = 0;\r
632   hcryp->CrypOutCount = 0;\r
633   \r
634   /* Change the CRYP state */\r
635   hcryp->State = HAL_CRYP_STATE_READY;\r
636   \r
637   /* Set the default CRYP phase */\r
638   hcryp->Phase = HAL_CRYP_PHASE_READY;\r
639   \r
640   /* Return function status */\r
641   return HAL_OK;\r
642 }\r
643 \r
644 /**\r
645   * @brief  DeInitializes the CRYP peripheral. \r
646   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
647   *         the configuration information for CRYP module\r
648   * @retval HAL status\r
649   */\r
650 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)\r
651 {\r
652   /* Check the CRYP handle allocation */\r
653   if(hcryp == NULL)\r
654   {\r
655     return HAL_ERROR;\r
656   }\r
657   \r
658   /* Change the CRYP state */\r
659   hcryp->State = HAL_CRYP_STATE_BUSY;\r
660   \r
661   /* Set the default CRYP phase */\r
662   hcryp->Phase = HAL_CRYP_PHASE_READY;\r
663   \r
664   /* Reset CrypInCount and CrypOutCount */\r
665   hcryp->CrypInCount = 0;\r
666   hcryp->CrypOutCount = 0;\r
667   \r
668   /* Disable the CRYP Peripheral Clock */\r
669   __HAL_CRYP_DISABLE();\r
670   \r
671   /* DeInit the low level hardware: CLOCK, NVIC.*/\r
672   HAL_CRYP_MspDeInit(hcryp);\r
673   \r
674   /* Change the CRYP state */\r
675   hcryp->State = HAL_CRYP_STATE_RESET;\r
676 \r
677   /* Release Lock */\r
678   __HAL_UNLOCK(hcryp);\r
679 \r
680   /* Return function status */\r
681   return HAL_OK;\r
682 }\r
683 \r
684 /**\r
685   * @brief  Initializes the CRYP MSP.\r
686   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
687   *         the configuration information for CRYP module\r
688   * @retval None\r
689   */\r
690 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)\r
691 {\r
692   /* NOTE : This function Should not be modified, when the callback is needed,\r
693             the HAL_CRYP_MspInit could be implemented in the user file\r
694    */\r
695 }\r
696 \r
697 /**\r
698   * @brief  DeInitializes CRYP MSP.\r
699   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
700   *         the configuration information for CRYP module\r
701   * @retval None\r
702   */\r
703 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)\r
704 {\r
705   /* NOTE : This function Should not be modified, when the callback is needed,\r
706             the HAL_CRYP_MspDeInit could be implemented in the user file\r
707    */\r
708 }\r
709 \r
710 /**\r
711   * @}\r
712   */\r
713 \r
714 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions \r
715  *  @brief   processing functions. \r
716  *\r
717 @verbatim   \r
718   ==============================================================================\r
719                       ##### AES processing functions #####\r
720   ==============================================================================  \r
721     [..]  This section provides functions allowing to:\r
722       (+) Encrypt plaintext using AES-128/192/256 using chaining modes\r
723       (+) Decrypt cyphertext using AES-128/192/256 using chaining modes\r
724     [..]  Three processing functions are available:\r
725       (+) Polling mode\r
726       (+) Interrupt mode\r
727       (+) DMA mode\r
728 \r
729 @endverbatim\r
730   * @{\r
731   */\r
732 \r
733 /**\r
734   * @brief  Initializes the CRYP peripheral in AES ECB encryption mode\r
735   *         then encrypt pPlainData. The cypher data are available in pCypherData\r
736   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
737   *         the configuration information for CRYP module\r
738   * @param  pPlainData: Pointer to the plaintext buffer\r
739   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.\r
740   * @param  pCypherData: Pointer to the cyphertext buffer\r
741   * @param  Timeout: Specify Timeout value \r
742   * @retval HAL status\r
743   */\r
744 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)\r
745 {\r
746   /* Process Locked */\r
747   __HAL_LOCK(hcryp);\r
748   \r
749   /* Change the CRYP state */\r
750   hcryp->State = HAL_CRYP_STATE_BUSY;\r
751   \r
752   /* Check if initialization phase has already been performed */\r
753   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
754   {\r
755     /* Set the key */\r
756     CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
757     \r
758     /* Set the CRYP peripheral in AES ECB mode */\r
759     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB);\r
760     \r
761     /* Flush FIFO */\r
762     __HAL_CRYP_FIFO_FLUSH();\r
763     \r
764     /* Enable CRYP */\r
765     __HAL_CRYP_ENABLE();\r
766     \r
767     /* Set the phase */\r
768     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
769   }\r
770   \r
771     /* Write Plain Data and Get Cypher Data */\r
772     if(CRYP_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK)\r
773     {\r
774       return HAL_TIMEOUT;\r
775     }\r
776   \r
777   /* Change the CRYP state */\r
778   hcryp->State = HAL_CRYP_STATE_READY;\r
779   \r
780   /* Process Unlocked */\r
781   __HAL_UNLOCK(hcryp);\r
782   \r
783   /* Return function status */\r
784   return HAL_OK;\r
785 }\r
786 \r
787 /**\r
788   * @brief  Initializes the CRYP peripheral in AES CBC encryption mode\r
789   *         then encrypt pPlainData. The cypher data are available in pCypherData\r
790   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
791   *         the configuration information for CRYP module\r
792   * @param  pPlainData: Pointer to the plaintext buffer\r
793   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.\r
794   * @param  pCypherData: Pointer to the cyphertext buffer\r
795   * @param  Timeout: Specify Timeout value  \r
796   * @retval HAL status\r
797   */\r
798 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)\r
799 {\r
800   /* Process Locked */\r
801   __HAL_LOCK(hcryp);\r
802   \r
803   /* Change the CRYP state */\r
804   hcryp->State = HAL_CRYP_STATE_BUSY;\r
805   \r
806   /* Check if initialization phase has already been performed */\r
807   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
808   {\r
809     /* Set the key */\r
810     CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
811     \r
812     /* Set the CRYP peripheral in AES ECB mode */\r
813     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC);\r
814     \r
815     /* Set the Initialization Vector */\r
816     CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
817     \r
818     /* Flush FIFO */\r
819     __HAL_CRYP_FIFO_FLUSH();\r
820     \r
821     /* Enable CRYP */\r
822     __HAL_CRYP_ENABLE();\r
823     \r
824     /* Set the phase */\r
825     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
826   }\r
827   \r
828     /* Write Plain Data and Get Cypher Data */\r
829     if(CRYP_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK)\r
830     {\r
831       return HAL_TIMEOUT;\r
832     }\r
833   \r
834   /* Change the CRYP state */\r
835   hcryp->State = HAL_CRYP_STATE_READY;\r
836   \r
837   /* Process Unlocked */\r
838   __HAL_UNLOCK(hcryp);\r
839   \r
840   /* Return function status */\r
841   return HAL_OK;\r
842 }\r
843 \r
844 /**\r
845   * @brief  Initializes the CRYP peripheral in AES CTR encryption mode\r
846   *         then encrypt pPlainData. The cypher data are available in pCypherData\r
847   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
848   *         the configuration information for CRYP module\r
849   * @param  pPlainData: Pointer to the plaintext buffer\r
850   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.\r
851   * @param  pCypherData: Pointer to the cyphertext buffer\r
852   * @param  Timeout: Specify Timeout value  \r
853   * @retval HAL status\r
854   */\r
855 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)\r
856 {  \r
857   /* Process Locked */\r
858   __HAL_LOCK(hcryp);\r
859   \r
860   /* Change the CRYP state */\r
861   hcryp->State = HAL_CRYP_STATE_BUSY;\r
862   \r
863   /* Check if initialization phase has already been performed */\r
864   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
865   {\r
866     /* Set the key */\r
867     CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
868     \r
869     /* Set the CRYP peripheral in AES ECB mode */\r
870     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR);\r
871     \r
872     /* Set the Initialization Vector */\r
873     CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
874     \r
875     /* Flush FIFO */\r
876     __HAL_CRYP_FIFO_FLUSH();\r
877     \r
878     /* Enable CRYP */\r
879     __HAL_CRYP_ENABLE();\r
880     \r
881     /* Set the phase */\r
882     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
883   }\r
884   \r
885     /* Write Plain Data and Get Cypher Data */\r
886     if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)\r
887     {\r
888       return HAL_TIMEOUT;\r
889     }\r
890   \r
891   /* Change the CRYP state */\r
892   hcryp->State = HAL_CRYP_STATE_READY;\r
893   \r
894   /* Process Unlocked */\r
895   __HAL_UNLOCK(hcryp);\r
896   \r
897   /* Return function status */\r
898   return HAL_OK;\r
899 }\r
900 \r
901 \r
902 \r
903 /**\r
904   * @brief  Initializes the CRYP peripheral in AES ECB decryption mode\r
905   *         then decrypted pCypherData. The cypher data are available in pPlainData\r
906   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
907   *         the configuration information for CRYP module\r
908   * @param  pCypherData: Pointer to the cyphertext buffer\r
909   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.\r
910   * @param  pPlainData: Pointer to the plaintext buffer\r
911   * @param  Timeout: Specify Timeout value  \r
912   * @retval HAL status\r
913   */\r
914 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)\r
915 {\r
916    uint32_t tickstart = 0;\r
917   \r
918   /* Process Locked */\r
919   __HAL_LOCK(hcryp);\r
920   \r
921   /* Change the CRYP state */\r
922   hcryp->State = HAL_CRYP_STATE_BUSY;\r
923   \r
924   /* Check if initialization phase has already been performed */\r
925   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
926   {\r
927     /* Set the key */\r
928     CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
929     \r
930     /* Set the CRYP peripheral in AES Key mode */\r
931     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);\r
932     \r
933     /* Enable CRYP */\r
934     __HAL_CRYP_ENABLE();\r
935     \r
936     /* Get tick */ \r
937     tickstart = HAL_GetTick();\r
938 \r
939     while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))\r
940     {\r
941       /* Check for the Timeout */\r
942       if(Timeout != HAL_MAX_DELAY)\r
943       {\r
944         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
945         {\r
946           /* Change state */\r
947           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
948           \r
949           /* Process Unlocked */          \r
950           __HAL_UNLOCK(hcryp);\r
951         \r
952           return HAL_TIMEOUT;\r
953         }\r
954       }\r
955     }\r
956     \r
957     /* Disable CRYP */\r
958     __HAL_CRYP_DISABLE();\r
959     \r
960     /* Reset the ALGOMODE bits*/\r
961     CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);\r
962     \r
963     /* Set the CRYP peripheral in AES ECB decryption mode */\r
964     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);\r
965     /* Flush FIFO */\r
966     __HAL_CRYP_FIFO_FLUSH();\r
967     \r
968     /* Enable CRYP */\r
969     __HAL_CRYP_ENABLE();\r
970     \r
971     /* Set the phase */\r
972     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
973   }\r
974     \r
975     /* Write Plain Data and Get Cypher Data */\r
976     if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)\r
977     {\r
978       return HAL_TIMEOUT;\r
979     }\r
980   \r
981   /* Change the CRYP state */\r
982   hcryp->State = HAL_CRYP_STATE_READY;\r
983   \r
984   /* Process Unlocked */\r
985   __HAL_UNLOCK(hcryp);\r
986   \r
987   /* Return function status */\r
988   return HAL_OK;\r
989 }\r
990 \r
991 /**\r
992   * @brief  Initializes the CRYP peripheral in AES ECB decryption mode\r
993   *         then decrypted pCypherData. The cypher data are available in pPlainData\r
994   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
995   *         the configuration information for CRYP module\r
996   * @param  pCypherData: Pointer to the cyphertext buffer\r
997   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.\r
998   * @param  pPlainData: Pointer to the plaintext buffer\r
999   * @param  Timeout: Specify Timeout value  \r
1000   * @retval HAL status\r
1001   */\r
1002 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)\r
1003 {\r
1004   uint32_t tickstart = 0;\r
1005   \r
1006   /* Process Locked */\r
1007   __HAL_LOCK(hcryp);\r
1008   \r
1009   /* Change the CRYP state */\r
1010   hcryp->State = HAL_CRYP_STATE_BUSY;\r
1011   \r
1012   /* Check if initialization phase has already been performed */\r
1013   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1014   {\r
1015     /* Set the key */\r
1016     CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1017     \r
1018     /* Set the CRYP peripheral in AES Key mode */\r
1019     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);\r
1020     \r
1021     /* Enable CRYP */\r
1022     __HAL_CRYP_ENABLE();\r
1023     \r
1024     /* Get tick */ \r
1025     tickstart = HAL_GetTick();\r
1026 \r
1027     while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))\r
1028     {\r
1029       /* Check for the Timeout */\r
1030       if(Timeout != HAL_MAX_DELAY)\r
1031       {\r
1032         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
1033         {\r
1034           /* Change state */\r
1035           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1036           \r
1037           /* Process Unlocked */\r
1038           __HAL_UNLOCK(hcryp);\r
1039           \r
1040           return HAL_TIMEOUT;\r
1041         }\r
1042       }\r
1043     }\r
1044     \r
1045     /* Reset the ALGOMODE bits*/\r
1046     CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);\r
1047     \r
1048     /* Set the CRYP peripheral in AES CBC decryption mode */\r
1049     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);\r
1050     \r
1051     /* Set the Initialization Vector */\r
1052     CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
1053     \r
1054     /* Flush FIFO */\r
1055     __HAL_CRYP_FIFO_FLUSH();\r
1056     \r
1057     /* Enable CRYP */\r
1058     __HAL_CRYP_ENABLE();\r
1059     \r
1060     /* Set the phase */\r
1061     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1062   }\r
1063   \r
1064     /* Write Plain Data and Get Cypher Data */\r
1065     if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)\r
1066     {\r
1067       return HAL_TIMEOUT;\r
1068     }\r
1069   \r
1070   /* Change the CRYP state */\r
1071   hcryp->State = HAL_CRYP_STATE_READY;\r
1072   \r
1073   /* Process Unlocked */\r
1074   __HAL_UNLOCK(hcryp);\r
1075   \r
1076   /* Return function status */\r
1077   return HAL_OK;\r
1078 }\r
1079 \r
1080 /**\r
1081   * @brief  Initializes the CRYP peripheral in AES CTR decryption mode\r
1082   *         then decrypted pCypherData. The cypher data are available in pPlainData\r
1083   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1084   *         the configuration information for CRYP module\r
1085   * @param  pCypherData: Pointer to the cyphertext buffer\r
1086   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.\r
1087   * @param  pPlainData: Pointer to the plaintext buffer\r
1088   * @param  Timeout: Specify Timeout value  \r
1089   * @retval HAL status\r
1090   */\r
1091 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)\r
1092 {  \r
1093   /* Process Locked */\r
1094   __HAL_LOCK(hcryp);\r
1095   \r
1096   /* Check if initialization phase has already been performed */\r
1097   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1098   {\r
1099     /* Change the CRYP state */\r
1100     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1101     \r
1102     /* Set the key */\r
1103     CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1104     \r
1105     /* Set the CRYP peripheral in AES CTR mode */\r
1106     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);\r
1107     \r
1108     /* Set the Initialization Vector */\r
1109     CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
1110     \r
1111     /* Flush FIFO */\r
1112     __HAL_CRYP_FIFO_FLUSH();\r
1113     \r
1114     /* Enable CRYP */\r
1115     __HAL_CRYP_ENABLE();\r
1116     \r
1117     /* Set the phase */\r
1118     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1119   }\r
1120   \r
1121     /* Write Plain Data and Get Cypher Data */\r
1122     if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)\r
1123     {\r
1124       return HAL_TIMEOUT;\r
1125     }\r
1126   \r
1127   /* Change the CRYP state */\r
1128   hcryp->State = HAL_CRYP_STATE_READY;\r
1129   \r
1130   /* Process Unlocked */\r
1131   __HAL_UNLOCK(hcryp);\r
1132   \r
1133   /* Return function status */\r
1134   return HAL_OK;\r
1135 }\r
1136 \r
1137 /**\r
1138   * @brief  Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt.\r
1139   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1140   *         the configuration information for CRYP module\r
1141   * @param  pPlainData: Pointer to the plaintext buffer\r
1142   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes\r
1143   * @param  pCypherData: Pointer to the cyphertext buffer\r
1144   * @retval HAL status\r
1145   */\r
1146 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
1147 {\r
1148   uint32_t inputaddr;\r
1149   uint32_t outputaddr;\r
1150   \r
1151   if(hcryp->State == HAL_CRYP_STATE_READY)\r
1152   {\r
1153     /* Process Locked */\r
1154     __HAL_LOCK(hcryp);\r
1155     \r
1156     hcryp->CrypInCount = Size;\r
1157     hcryp->pCrypInBuffPtr = pPlainData;\r
1158     hcryp->pCrypOutBuffPtr = pCypherData;\r
1159     hcryp->CrypOutCount = Size;\r
1160     \r
1161     /* Change the CRYP state */\r
1162     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1163     \r
1164     /* Check if initialization phase has already been performed */\r
1165     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1166     {\r
1167       /* Set the key */\r
1168       CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1169       \r
1170       /* Set the CRYP peripheral in AES ECB mode */\r
1171       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB);\r
1172       \r
1173       /* Flush FIFO */\r
1174       __HAL_CRYP_FIFO_FLUSH();\r
1175       \r
1176      /* Set the phase */\r
1177      hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1178     }\r
1179     \r
1180     /* Enable Interrupts */\r
1181     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
1182     \r
1183     /* Enable CRYP */\r
1184     __HAL_CRYP_ENABLE();\r
1185     \r
1186     /* Return function status */\r
1187     return HAL_OK;\r
1188   }\r
1189   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
1190   {\r
1191     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
1192     /* Write the Input block in the IN FIFO */\r
1193     CRYP->DR = *(uint32_t*)(inputaddr);\r
1194     inputaddr+=4;\r
1195     CRYP->DR = *(uint32_t*)(inputaddr);\r
1196     inputaddr+=4;\r
1197     CRYP->DR  = *(uint32_t*)(inputaddr);\r
1198     inputaddr+=4;\r
1199     CRYP->DR = *(uint32_t*)(inputaddr);\r
1200     hcryp->pCrypInBuffPtr += 16;\r
1201     hcryp->CrypInCount -= 16;\r
1202     if(hcryp->CrypInCount == 0)\r
1203     {\r
1204       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
1205       /* Call the Input data transfer complete callback */\r
1206       HAL_CRYP_InCpltCallback(hcryp);\r
1207     }\r
1208   }\r
1209   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
1210   {\r
1211     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
1212     /* Read the Output block from the Output FIFO */\r
1213     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1214     outputaddr+=4;\r
1215     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1216     outputaddr+=4;\r
1217     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1218     outputaddr+=4;\r
1219     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1220     hcryp->pCrypOutBuffPtr += 16;\r
1221     hcryp->CrypOutCount -= 16;\r
1222     if(hcryp->CrypOutCount == 0)\r
1223     {\r
1224       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
1225       /* Process Locked */\r
1226       __HAL_UNLOCK(hcryp);\r
1227       /* Change the CRYP state */\r
1228       hcryp->State = HAL_CRYP_STATE_READY;\r
1229       /* Call Input transfer complete callback */\r
1230       HAL_CRYP_OutCpltCallback(hcryp);\r
1231     }\r
1232   }\r
1233   \r
1234   /* Return function status */\r
1235   return HAL_OK;\r
1236 }\r
1237 \r
1238 /**\r
1239   * @brief  Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt.\r
1240   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1241   *         the configuration information for CRYP module\r
1242   * @param  pPlainData: Pointer to the plaintext buffer\r
1243   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes\r
1244   * @param  pCypherData: Pointer to the cyphertext buffer\r
1245   * @retval HAL status\r
1246   */\r
1247 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
1248 {\r
1249   uint32_t inputaddr;\r
1250   uint32_t outputaddr;\r
1251   \r
1252   if(hcryp->State == HAL_CRYP_STATE_READY)\r
1253   {\r
1254     /* Process Locked */\r
1255     __HAL_LOCK(hcryp);\r
1256     \r
1257     hcryp->CrypInCount = Size;\r
1258     hcryp->pCrypInBuffPtr = pPlainData;\r
1259     hcryp->pCrypOutBuffPtr = pCypherData;\r
1260     hcryp->CrypOutCount = Size;\r
1261     \r
1262     /* Change the CRYP state */\r
1263     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1264     \r
1265     /* Check if initialization phase has already been performed */\r
1266     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1267     {      \r
1268       /* Set the key */\r
1269       CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1270       \r
1271       /* Set the CRYP peripheral in AES CBC mode */\r
1272       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC);\r
1273       \r
1274       /* Set the Initialization Vector */\r
1275       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
1276       \r
1277       /* Flush FIFO */\r
1278       __HAL_CRYP_FIFO_FLUSH();\r
1279       \r
1280      /* Set the phase */\r
1281      hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1282     }\r
1283     /* Enable Interrupts */\r
1284     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
1285     \r
1286     /* Enable CRYP */\r
1287     __HAL_CRYP_ENABLE();\r
1288     \r
1289     /* Return function status */\r
1290     return HAL_OK;\r
1291   }\r
1292   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
1293   {\r
1294     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
1295     /* Write the Input block in the IN FIFO */\r
1296     CRYP->DR = *(uint32_t*)(inputaddr);\r
1297     inputaddr+=4;\r
1298     CRYP->DR = *(uint32_t*)(inputaddr);\r
1299     inputaddr+=4;\r
1300     CRYP->DR  = *(uint32_t*)(inputaddr);\r
1301     inputaddr+=4;\r
1302     CRYP->DR = *(uint32_t*)(inputaddr);\r
1303     hcryp->pCrypInBuffPtr += 16;\r
1304     hcryp->CrypInCount -= 16;\r
1305     if(hcryp->CrypInCount == 0)\r
1306     {\r
1307       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
1308       /* Call the Input data transfer complete callback */\r
1309       HAL_CRYP_InCpltCallback(hcryp);\r
1310     }\r
1311   }\r
1312   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
1313   {\r
1314     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
1315     /* Read the Output block from the Output FIFO */\r
1316     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1317     outputaddr+=4;\r
1318     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1319     outputaddr+=4;\r
1320     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1321     outputaddr+=4;\r
1322     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1323     hcryp->pCrypOutBuffPtr += 16;\r
1324     hcryp->CrypOutCount -= 16;\r
1325     if(hcryp->CrypOutCount == 0)\r
1326     {\r
1327       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
1328       /* Process Locked */\r
1329       __HAL_UNLOCK(hcryp);\r
1330       /* Change the CRYP state */\r
1331       hcryp->State = HAL_CRYP_STATE_READY;\r
1332       /* Call Input transfer complete callback */\r
1333       HAL_CRYP_OutCpltCallback(hcryp);\r
1334     }\r
1335   }\r
1336   \r
1337   /* Return function status */\r
1338   return HAL_OK;\r
1339 }\r
1340 \r
1341 /**\r
1342   * @brief  Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt.\r
1343   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1344   *         the configuration information for CRYP module\r
1345   * @param  pPlainData: Pointer to the plaintext buffer\r
1346   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes\r
1347   * @param  pCypherData: Pointer to the cyphertext buffer\r
1348   * @retval HAL status\r
1349   */\r
1350 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
1351 {\r
1352   uint32_t inputaddr;\r
1353   uint32_t outputaddr;\r
1354   \r
1355   if(hcryp->State == HAL_CRYP_STATE_READY)\r
1356   {\r
1357     /* Process Locked */\r
1358     __HAL_LOCK(hcryp);\r
1359     \r
1360     hcryp->CrypInCount = Size;\r
1361     hcryp->pCrypInBuffPtr = pPlainData;\r
1362     hcryp->pCrypOutBuffPtr = pCypherData;\r
1363     hcryp->CrypOutCount = Size;\r
1364     \r
1365     /* Change the CRYP state */\r
1366     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1367     \r
1368     /* Check if initialization phase has already been performed */\r
1369     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1370     {\r
1371       /* Set the key */\r
1372       CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1373       \r
1374       /* Set the CRYP peripheral in AES CTR mode */\r
1375       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR);\r
1376       \r
1377       /* Set the Initialization Vector */\r
1378       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
1379       \r
1380       /* Flush FIFO */\r
1381       __HAL_CRYP_FIFO_FLUSH();\r
1382       \r
1383      /* Set the phase */\r
1384      hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1385     }\r
1386     /* Enable Interrupts */\r
1387     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
1388     \r
1389     /* Enable CRYP */\r
1390     __HAL_CRYP_ENABLE();\r
1391     \r
1392     /* Return function status */\r
1393     return HAL_OK;\r
1394   }\r
1395   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
1396   {\r
1397     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
1398     /* Write the Input block in the IN FIFO */\r
1399     CRYP->DR = *(uint32_t*)(inputaddr);\r
1400     inputaddr+=4;\r
1401     CRYP->DR = *(uint32_t*)(inputaddr);\r
1402     inputaddr+=4;\r
1403     CRYP->DR  = *(uint32_t*)(inputaddr);\r
1404     inputaddr+=4;\r
1405     CRYP->DR = *(uint32_t*)(inputaddr);\r
1406     hcryp->pCrypInBuffPtr += 16;\r
1407     hcryp->CrypInCount -= 16;\r
1408     if(hcryp->CrypInCount == 0)\r
1409     {\r
1410       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
1411       /* Call the Input data transfer complete callback */\r
1412       HAL_CRYP_InCpltCallback(hcryp);\r
1413     }\r
1414   }\r
1415   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
1416   {\r
1417     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
1418     /* Read the Output block from the Output FIFO */\r
1419     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1420     outputaddr+=4;\r
1421     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1422     outputaddr+=4;\r
1423     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1424     outputaddr+=4;\r
1425     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1426     hcryp->pCrypOutBuffPtr += 16;\r
1427     hcryp->CrypOutCount -= 16;\r
1428     if(hcryp->CrypOutCount == 0)\r
1429     {\r
1430       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
1431       /* Process Unlocked */\r
1432       __HAL_UNLOCK(hcryp);\r
1433       /* Change the CRYP state */\r
1434       hcryp->State = HAL_CRYP_STATE_READY;\r
1435       /* Call Input transfer complete callback */\r
1436       HAL_CRYP_OutCpltCallback(hcryp);\r
1437     }\r
1438   }\r
1439   \r
1440   /* Return function status */\r
1441   return HAL_OK;\r
1442 }\r
1443 \r
1444 \r
1445 /**\r
1446   * @brief  Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt.\r
1447   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1448   *         the configuration information for CRYP module\r
1449   * @param  pCypherData: Pointer to the cyphertext buffer\r
1450   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.\r
1451   * @param  pPlainData: Pointer to the plaintext buffer\r
1452   * @retval HAL status\r
1453   */\r
1454 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
1455 {\r
1456   uint32_t tickstart = 0;\r
1457 \r
1458   uint32_t inputaddr;\r
1459   uint32_t outputaddr;\r
1460   \r
1461   if(hcryp->State == HAL_CRYP_STATE_READY)\r
1462   {\r
1463     /* Process Locked */\r
1464     __HAL_LOCK(hcryp);\r
1465     \r
1466     hcryp->CrypInCount = Size;\r
1467     hcryp->pCrypInBuffPtr = pCypherData;\r
1468     hcryp->pCrypOutBuffPtr = pPlainData;\r
1469     hcryp->CrypOutCount = Size;\r
1470     \r
1471     /* Change the CRYP state */\r
1472     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1473     \r
1474   /* Check if initialization phase has already been performed */\r
1475   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1476   {\r
1477     /* Set the key */\r
1478     CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1479     \r
1480     /* Set the CRYP peripheral in AES Key mode */\r
1481     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);\r
1482     /* Enable CRYP */\r
1483     __HAL_CRYP_ENABLE();\r
1484     \r
1485     /* Get tick */ \r
1486     tickstart = HAL_GetTick();\r
1487 \r
1488     while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))\r
1489     {\r
1490       /* Check for the Timeout */\r
1491       if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)\r
1492       {\r
1493         /* Change state */\r
1494         hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1495         \r
1496         /* Process Unlocked */\r
1497         __HAL_UNLOCK(hcryp);\r
1498         \r
1499         return HAL_TIMEOUT;\r
1500       }\r
1501     }\r
1502     \r
1503     /* Reset the ALGOMODE bits*/\r
1504     CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);\r
1505     \r
1506     /* Set the CRYP peripheral in AES ECB decryption mode */\r
1507     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);\r
1508     \r
1509     /* Flush FIFO */\r
1510     __HAL_CRYP_FIFO_FLUSH();\r
1511     \r
1512      /* Set the phase */\r
1513      hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1514   }\r
1515      \r
1516     /* Enable Interrupts */\r
1517     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
1518     \r
1519     /* Enable CRYP */\r
1520     __HAL_CRYP_ENABLE();\r
1521     \r
1522     /* Return function status */\r
1523     return HAL_OK;\r
1524   }\r
1525   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
1526   {\r
1527     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
1528     /* Write the Input block in the IN FIFO */\r
1529     CRYP->DR = *(uint32_t*)(inputaddr);\r
1530     inputaddr+=4;\r
1531     CRYP->DR = *(uint32_t*)(inputaddr);\r
1532     inputaddr+=4;\r
1533     CRYP->DR  = *(uint32_t*)(inputaddr);\r
1534     inputaddr+=4;\r
1535     CRYP->DR = *(uint32_t*)(inputaddr);\r
1536     hcryp->pCrypInBuffPtr += 16;\r
1537     hcryp->CrypInCount -= 16;\r
1538     if(hcryp->CrypInCount == 0)\r
1539     {\r
1540       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
1541       /* Call the Input data transfer complete callback */\r
1542       HAL_CRYP_InCpltCallback(hcryp);\r
1543     }\r
1544   }\r
1545   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
1546   {\r
1547     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
1548     /* Read the Output block from the Output FIFO */\r
1549     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1550     outputaddr+=4;\r
1551     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1552     outputaddr+=4;\r
1553     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1554     outputaddr+=4;\r
1555     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1556     hcryp->pCrypOutBuffPtr += 16;\r
1557     hcryp->CrypOutCount -= 16;\r
1558     if(hcryp->CrypOutCount == 0)\r
1559     {\r
1560       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
1561       /* Process Unlocked */\r
1562       __HAL_UNLOCK(hcryp);\r
1563       /* Change the CRYP state */\r
1564       hcryp->State = HAL_CRYP_STATE_READY;\r
1565       /* Call Input transfer complete callback */\r
1566       HAL_CRYP_OutCpltCallback(hcryp);\r
1567     }\r
1568   }\r
1569   \r
1570   /* Return function status */\r
1571   return HAL_OK;\r
1572 }\r
1573 \r
1574 /**\r
1575   * @brief  Initializes the CRYP peripheral in AES CBC decryption mode using IT.\r
1576   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1577   *         the configuration information for CRYP module\r
1578   * @param  pCypherData: Pointer to the cyphertext buffer\r
1579   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
1580   * @param  pPlainData: Pointer to the plaintext buffer\r
1581   * @retval HAL status\r
1582   */\r
1583 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
1584 {\r
1585 \r
1586   uint32_t tickstart = 0;   \r
1587   uint32_t inputaddr;\r
1588   uint32_t outputaddr;\r
1589   \r
1590   if(hcryp->State == HAL_CRYP_STATE_READY)\r
1591   {\r
1592     /* Process Locked */\r
1593     __HAL_LOCK(hcryp);\r
1594     \r
1595     /* Get the buffer addresses and sizes */    \r
1596     hcryp->CrypInCount = Size;\r
1597     hcryp->pCrypInBuffPtr = pCypherData;\r
1598     hcryp->pCrypOutBuffPtr = pPlainData;\r
1599     hcryp->CrypOutCount = Size;\r
1600     \r
1601     /* Change the CRYP state */\r
1602     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1603     \r
1604     /* Check if initialization phase has already been performed */\r
1605     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1606     {\r
1607       /* Set the key */\r
1608       CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1609       \r
1610       /* Set the CRYP peripheral in AES Key mode */\r
1611       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);\r
1612       \r
1613       /* Enable CRYP */\r
1614       __HAL_CRYP_ENABLE();\r
1615       \r
1616     /* Get tick */\r
1617     tickstart = HAL_GetTick();\r
1618 \r
1619     while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))\r
1620     {\r
1621       /* Check for the Timeout */\r
1622       if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)\r
1623       {\r
1624         /* Change state */\r
1625         hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1626         \r
1627         /* Process Unlocked */\r
1628         __HAL_UNLOCK(hcryp);\r
1629         \r
1630         return HAL_TIMEOUT;\r
1631       }\r
1632     }\r
1633     \r
1634       /* Reset the ALGOMODE bits*/\r
1635       CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);\r
1636     \r
1637       /* Set the CRYP peripheral in AES CBC decryption mode */\r
1638       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);\r
1639     \r
1640       /* Set the Initialization Vector */\r
1641       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
1642     \r
1643       /* Flush FIFO */\r
1644       __HAL_CRYP_FIFO_FLUSH();\r
1645     \r
1646       /* Enable CRYP */\r
1647       __HAL_CRYP_ENABLE();\r
1648       \r
1649       /* Set the phase */\r
1650       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1651     }\r
1652     \r
1653     /* Enable Interrupts */\r
1654     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
1655     \r
1656     /* Enable CRYP */\r
1657     __HAL_CRYP_ENABLE();\r
1658     \r
1659     /* Return function status */\r
1660     return HAL_OK;\r
1661   }\r
1662   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
1663   {\r
1664     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
1665     /* Write the Input block in the IN FIFO */\r
1666     CRYP->DR = *(uint32_t*)(inputaddr);\r
1667     inputaddr+=4;\r
1668     CRYP->DR = *(uint32_t*)(inputaddr);\r
1669     inputaddr+=4;\r
1670     CRYP->DR  = *(uint32_t*)(inputaddr);\r
1671     inputaddr+=4;\r
1672     CRYP->DR = *(uint32_t*)(inputaddr);\r
1673     hcryp->pCrypInBuffPtr += 16;\r
1674     hcryp->CrypInCount -= 16;\r
1675     if(hcryp->CrypInCount == 0)\r
1676     {\r
1677       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
1678       /* Call the Input data transfer complete callback */\r
1679       HAL_CRYP_InCpltCallback(hcryp);\r
1680     }\r
1681   }\r
1682   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
1683   {\r
1684     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
1685     /* Read the Output block from the Output FIFO */\r
1686     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1687     outputaddr+=4;\r
1688     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1689     outputaddr+=4;\r
1690     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1691     outputaddr+=4;\r
1692     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1693     hcryp->pCrypOutBuffPtr += 16;\r
1694     hcryp->CrypOutCount -= 16;\r
1695     if(hcryp->CrypOutCount == 0)\r
1696     {\r
1697       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
1698       /* Process Unlocked */\r
1699       __HAL_UNLOCK(hcryp);\r
1700       /* Change the CRYP state */\r
1701       hcryp->State = HAL_CRYP_STATE_READY;\r
1702       /* Call Input transfer complete callback */\r
1703       HAL_CRYP_OutCpltCallback(hcryp);\r
1704     }\r
1705   }\r
1706   \r
1707   /* Return function status */\r
1708   return HAL_OK;\r
1709 }\r
1710 \r
1711 /**\r
1712   * @brief  Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt.\r
1713   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1714   *         the configuration information for CRYP module\r
1715   * @param  pCypherData: Pointer to the cyphertext buffer\r
1716   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
1717   * @param  pPlainData: Pointer to the plaintext buffer\r
1718   * @retval HAL status\r
1719   */\r
1720 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
1721 {\r
1722   uint32_t inputaddr;\r
1723   uint32_t outputaddr;\r
1724   \r
1725   if(hcryp->State == HAL_CRYP_STATE_READY)\r
1726   {\r
1727     /* Process Locked */\r
1728     __HAL_LOCK(hcryp);\r
1729     \r
1730     /* Get the buffer addresses and sizes */    \r
1731     hcryp->CrypInCount = Size;\r
1732     hcryp->pCrypInBuffPtr = pCypherData;\r
1733     hcryp->pCrypOutBuffPtr = pPlainData;\r
1734     hcryp->CrypOutCount = Size;\r
1735     \r
1736     /* Change the CRYP state */\r
1737     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1738     \r
1739     /* Check if initialization phase has already been performed */\r
1740     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1741     {\r
1742       /* Set the key */\r
1743       CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1744       \r
1745       /* Set the CRYP peripheral in AES CTR mode */\r
1746       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);\r
1747       \r
1748       /* Set the Initialization Vector */\r
1749       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
1750       \r
1751       /* Flush FIFO */\r
1752       __HAL_CRYP_FIFO_FLUSH();\r
1753       \r
1754       /* Set the phase */\r
1755       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1756     }\r
1757     \r
1758     /* Enable Interrupts */\r
1759     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
1760     \r
1761     /* Enable CRYP */\r
1762     __HAL_CRYP_ENABLE();\r
1763     \r
1764     /* Return function status */\r
1765     return HAL_OK;\r
1766   }\r
1767   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
1768   {\r
1769     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
1770     /* Write the Input block in the IN FIFO */\r
1771     CRYP->DR = *(uint32_t*)(inputaddr);\r
1772     inputaddr+=4;\r
1773     CRYP->DR = *(uint32_t*)(inputaddr);\r
1774     inputaddr+=4;\r
1775     CRYP->DR  = *(uint32_t*)(inputaddr);\r
1776     inputaddr+=4;\r
1777     CRYP->DR = *(uint32_t*)(inputaddr);\r
1778     hcryp->pCrypInBuffPtr += 16;\r
1779     hcryp->CrypInCount -= 16;\r
1780     if(hcryp->CrypInCount == 0)\r
1781     {\r
1782       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
1783       /* Call the Input data transfer complete callback */\r
1784       HAL_CRYP_InCpltCallback(hcryp);\r
1785     }\r
1786   }\r
1787   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
1788   {\r
1789     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
1790     /* Read the Output block from the Output FIFO */\r
1791     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1792     outputaddr+=4;\r
1793     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1794     outputaddr+=4;\r
1795     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1796     outputaddr+=4;\r
1797     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
1798     hcryp->pCrypOutBuffPtr += 16;\r
1799     hcryp->CrypOutCount -= 16;\r
1800     if(hcryp->CrypOutCount == 0)\r
1801     {\r
1802       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
1803       /* Process Unlocked */\r
1804       __HAL_UNLOCK(hcryp);\r
1805       /* Change the CRYP state */\r
1806       hcryp->State = HAL_CRYP_STATE_READY;\r
1807       /* Call Input transfer complete callback */\r
1808       HAL_CRYP_OutCpltCallback(hcryp);\r
1809     }\r
1810   }\r
1811   \r
1812   /* Return function status */\r
1813   return HAL_OK;\r
1814 }\r
1815 \r
1816 /**\r
1817   * @brief  Initializes the CRYP peripheral in AES ECB encryption mode using DMA.\r
1818   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1819   *         the configuration information for CRYP module\r
1820   * @param  pPlainData: Pointer to the plaintext buffer\r
1821   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes\r
1822   * @param  pCypherData: Pointer to the cyphertext buffer\r
1823   * @retval HAL status\r
1824   */\r
1825 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
1826 {\r
1827   uint32_t inputaddr;\r
1828   uint32_t outputaddr;\r
1829   \r
1830   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
1831   {\r
1832     /* Process Locked */\r
1833     __HAL_LOCK(hcryp);\r
1834     \r
1835     inputaddr  = (uint32_t)pPlainData;\r
1836     outputaddr = (uint32_t)pCypherData;\r
1837     \r
1838     /* Change the CRYP state */\r
1839     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1840     \r
1841     /* Check if initialization phase has already been performed */\r
1842     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1843     {\r
1844       /* Set the key */\r
1845       CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1846       \r
1847       /* Set the CRYP peripheral in AES ECB mode */\r
1848       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB);\r
1849       \r
1850       /* Flush FIFO */\r
1851       __HAL_CRYP_FIFO_FLUSH();\r
1852       \r
1853      /* Set the phase */\r
1854      hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1855     }\r
1856     /* Set the input and output addresses and start DMA transfer */ \r
1857     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
1858     \r
1859     /* Process Unlocked */\r
1860     __HAL_UNLOCK(hcryp);\r
1861      \r
1862     /* Return function status */\r
1863     return HAL_OK;\r
1864   }\r
1865   else\r
1866   {\r
1867     return HAL_ERROR;   \r
1868   }\r
1869 }\r
1870 \r
1871 /**\r
1872   * @brief  Initializes the CRYP peripheral in AES CBC encryption mode using DMA.\r
1873   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1874   *         the configuration information for CRYP module\r
1875   * @param  pPlainData: Pointer to the plaintext buffer\r
1876   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.\r
1877   * @param  pCypherData: Pointer to the cyphertext buffer\r
1878   * @retval HAL status\r
1879   */\r
1880 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
1881 {\r
1882   uint32_t inputaddr;\r
1883   uint32_t outputaddr;\r
1884   \r
1885   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
1886   {\r
1887     /* Process Locked */\r
1888     __HAL_LOCK(hcryp);\r
1889     \r
1890     inputaddr  = (uint32_t)pPlainData;\r
1891     outputaddr = (uint32_t)pCypherData;\r
1892     \r
1893     /* Change the CRYP state */\r
1894     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1895   \r
1896     /* Check if initialization phase has already been performed */\r
1897     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1898     {\r
1899       /* Set the key */\r
1900       CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1901       \r
1902       /* Set the CRYP peripheral in AES ECB mode */\r
1903       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC);\r
1904       \r
1905       /* Set the Initialization Vector */\r
1906       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
1907       \r
1908       /* Flush FIFO */\r
1909       __HAL_CRYP_FIFO_FLUSH();\r
1910       \r
1911        /* Set the phase */\r
1912        hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1913      }\r
1914      /* Set the input and output addresses and start DMA transfer */ \r
1915      CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
1916      \r
1917      /* Process Unlocked */\r
1918      __HAL_UNLOCK(hcryp);\r
1919      \r
1920      /* Return function status */\r
1921      return HAL_OK;\r
1922   }\r
1923   else\r
1924   {\r
1925     return HAL_ERROR;   \r
1926   }\r
1927 }\r
1928 \r
1929 /**\r
1930   * @brief  Initializes the CRYP peripheral in AES CTR encryption mode using DMA.\r
1931   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1932   *         the configuration information for CRYP module\r
1933   * @param  pPlainData: Pointer to the plaintext buffer\r
1934   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.\r
1935   * @param  pCypherData: Pointer to the cyphertext buffer\r
1936   * @retval HAL status\r
1937   */\r
1938 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
1939 {\r
1940   uint32_t inputaddr;\r
1941   uint32_t outputaddr;\r
1942   \r
1943   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
1944   {\r
1945     /* Process Locked */\r
1946     __HAL_LOCK(hcryp);\r
1947     \r
1948     inputaddr  = (uint32_t)pPlainData;\r
1949     outputaddr = (uint32_t)pCypherData;\r
1950     \r
1951   /* Change the CRYP state */\r
1952   hcryp->State = HAL_CRYP_STATE_BUSY;\r
1953   \r
1954     /* Check if initialization phase has already been performed */\r
1955     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1956     {\r
1957       /* Set the key */\r
1958       CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1959       \r
1960       /* Set the CRYP peripheral in AES ECB mode */\r
1961       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR);\r
1962       \r
1963       /* Set the Initialization Vector */\r
1964       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
1965       \r
1966       /* Flush FIFO */\r
1967       __HAL_CRYP_FIFO_FLUSH();\r
1968       \r
1969        /* Set the phase */\r
1970        hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1971     }\r
1972     \r
1973     /* Set the input and output addresses and start DMA transfer */ \r
1974     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
1975     \r
1976     /* Process Unlocked */\r
1977     __HAL_UNLOCK(hcryp);\r
1978     \r
1979     /* Return function status */\r
1980     return HAL_OK;\r
1981   }\r
1982   else\r
1983   {\r
1984     return HAL_ERROR;   \r
1985   }\r
1986 }\r
1987 \r
1988 /**\r
1989   * @brief  Initializes the CRYP peripheral in AES ECB decryption mode using DMA.\r
1990   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1991   *         the configuration information for CRYP module\r
1992   * @param  pCypherData: Pointer to the cyphertext buffer\r
1993   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes\r
1994   * @param  pPlainData: Pointer to the plaintext buffer\r
1995   * @retval HAL status\r
1996   */\r
1997 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
1998 {\r
1999   uint32_t tickstart = 0;   \r
2000   uint32_t inputaddr;\r
2001   uint32_t outputaddr;\r
2002   \r
2003   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
2004   {\r
2005     /* Process Locked */\r
2006     __HAL_LOCK(hcryp);\r
2007     \r
2008     inputaddr  = (uint32_t)pCypherData;\r
2009     outputaddr = (uint32_t)pPlainData;\r
2010     \r
2011     /* Change the CRYP state */\r
2012     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2013     \r
2014     /* Check if initialization phase has already been performed */\r
2015     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
2016     {\r
2017     /* Set the key */\r
2018     CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
2019     \r
2020     /* Set the CRYP peripheral in AES Key mode */\r
2021     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);\r
2022     \r
2023     /* Enable CRYP */\r
2024     __HAL_CRYP_ENABLE();\r
2025     \r
2026     /* Get tick */\r
2027     tickstart = HAL_GetTick();\r
2028     \r
2029     while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))\r
2030     {\r
2031       /* Check for the Timeout */\r
2032       if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)\r
2033       {\r
2034         /* Change state */\r
2035         hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2036         \r
2037         /* Process Unlocked */\r
2038         __HAL_UNLOCK(hcryp);\r
2039         \r
2040         return HAL_TIMEOUT;\r
2041       }\r
2042     }\r
2043     \r
2044     /* Reset the ALGOMODE bits*/\r
2045     CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);\r
2046     \r
2047     /* Set the CRYP peripheral in AES ECB decryption mode */\r
2048     __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);\r
2049     \r
2050     /* Flush FIFO */\r
2051     __HAL_CRYP_FIFO_FLUSH();\r
2052     \r
2053      /* Set the phase */\r
2054      hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
2055     }\r
2056      \r
2057     /* Set the input and output addresses and start DMA transfer */ \r
2058     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
2059     \r
2060      /* Process Unlocked */\r
2061      __HAL_UNLOCK(hcryp);\r
2062     \r
2063     /* Return function status */\r
2064     return HAL_OK;\r
2065   }\r
2066   else\r
2067   {\r
2068     return HAL_ERROR;   \r
2069   }\r
2070 }\r
2071 \r
2072 /**\r
2073   * @brief  Initializes the CRYP peripheral in AES CBC encryption mode using DMA.\r
2074   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2075   *         the configuration information for CRYP module\r
2076   * @param  pCypherData: Pointer to the cyphertext buffer\r
2077   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes\r
2078   * @param  pPlainData: Pointer to the plaintext buffer\r
2079   * @retval HAL status\r
2080   */\r
2081 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
2082 {\r
2083   uint32_t tickstart = 0;   \r
2084   uint32_t inputaddr;\r
2085   uint32_t outputaddr;\r
2086   \r
2087   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
2088   {\r
2089     /* Process Locked */\r
2090     __HAL_LOCK(hcryp);\r
2091     \r
2092     inputaddr  = (uint32_t)pCypherData;\r
2093     outputaddr = (uint32_t)pPlainData;\r
2094     \r
2095     /* Change the CRYP state */\r
2096     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2097     \r
2098     /* Check if initialization phase has already been performed */\r
2099     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
2100     {\r
2101       /* Set the key */\r
2102       CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
2103       \r
2104       /* Set the CRYP peripheral in AES Key mode */\r
2105       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);\r
2106       \r
2107       /* Enable CRYP */\r
2108       __HAL_CRYP_ENABLE();\r
2109       \r
2110       /* Get tick */\r
2111       tickstart = HAL_GetTick();\r
2112 \r
2113       while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))\r
2114       {\r
2115         /* Check for the Timeout */\r
2116         if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)\r
2117         {\r
2118           /* Change state */\r
2119           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2120           \r
2121           /* Process Unlocked */\r
2122           __HAL_UNLOCK(hcryp);\r
2123           \r
2124           return HAL_TIMEOUT;\r
2125         }\r
2126       }\r
2127       \r
2128       /* Reset the ALGOMODE bits*/\r
2129       CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);\r
2130       \r
2131       /* Set the CRYP peripheral in AES CBC decryption mode */\r
2132       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);\r
2133       \r
2134       /* Set the Initialization Vector */\r
2135       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
2136       \r
2137       /* Flush FIFO */\r
2138       __HAL_CRYP_FIFO_FLUSH();\r
2139       \r
2140       /* Set the phase */\r
2141       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
2142     }\r
2143     \r
2144     /* Set the input and output addresses and start DMA transfer */ \r
2145     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
2146     \r
2147     /* Process Unlocked */\r
2148     __HAL_UNLOCK(hcryp);\r
2149     \r
2150     /* Return function status */\r
2151     return HAL_OK;\r
2152   }\r
2153   else\r
2154   {\r
2155     return HAL_ERROR;   \r
2156   }\r
2157 }\r
2158 \r
2159 /**\r
2160   * @brief  Initializes the CRYP peripheral in AES CTR decryption mode using DMA.\r
2161   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2162   *         the configuration information for CRYP module\r
2163   * @param  pCypherData: Pointer to the cyphertext buffer\r
2164   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
2165   * @param  pPlainData: Pointer to the plaintext buffer\r
2166   * @retval HAL status\r
2167   */\r
2168 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
2169 {  \r
2170   uint32_t inputaddr;\r
2171   uint32_t outputaddr;\r
2172   \r
2173   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
2174   {\r
2175     /* Process Locked */\r
2176     __HAL_LOCK(hcryp);\r
2177     \r
2178     inputaddr  = (uint32_t)pCypherData;\r
2179     outputaddr = (uint32_t)pPlainData;\r
2180     \r
2181     /* Change the CRYP state */\r
2182     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2183     \r
2184     /* Check if initialization phase has already been performed */\r
2185     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
2186     {\r
2187       /* Set the key */\r
2188       CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
2189       \r
2190       /* Set the CRYP peripheral in AES CTR mode */\r
2191       __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);\r
2192       \r
2193       /* Set the Initialization Vector */\r
2194       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);\r
2195       \r
2196       /* Flush FIFO */\r
2197       __HAL_CRYP_FIFO_FLUSH();\r
2198       \r
2199       /* Set the phase */\r
2200       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
2201     }\r
2202     \r
2203     /* Set the input and output addresses and start DMA transfer */ \r
2204     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
2205     \r
2206     /* Process Unlocked */\r
2207     __HAL_UNLOCK(hcryp);\r
2208     \r
2209     /* Return function status */\r
2210     return HAL_OK;\r
2211   }\r
2212   else\r
2213   {\r
2214     return HAL_ERROR;   \r
2215   }\r
2216 }\r
2217 \r
2218 \r
2219 /**\r
2220   * @}\r
2221   */\r
2222   \r
2223 /** @defgroup CRYP_Exported_Functions_Group3 DES processing functions \r
2224  *  @brief   processing functions. \r
2225  *\r
2226 @verbatim   \r
2227   ==============================================================================\r
2228                       ##### DES processing functions #####\r
2229   ==============================================================================  \r
2230     [..]  This section provides functions allowing to:\r
2231       (+) Encrypt plaintext using DES using ECB or CBC chaining modes\r
2232       (+) Decrypt cyphertext using ECB or CBC chaining modes\r
2233     [..]  Three processing functions are available:\r
2234       (+) Polling mode\r
2235       (+) Interrupt mode\r
2236       (+) DMA mode\r
2237 \r
2238 @endverbatim\r
2239   * @{\r
2240   */\r
2241 \r
2242 /**\r
2243   * @brief  Initializes the CRYP peripheral in DES ECB encryption mode.\r
2244   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2245   *         the configuration information for CRYP module\r
2246   * @param  pPlainData: Pointer to the plaintext buffer\r
2247   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2248   * @param  pCypherData: Pointer to the cyphertext buffer\r
2249   * @param  Timeout: Specify Timeout value  \r
2250   * @retval HAL status\r
2251   */\r
2252 HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)\r
2253 {\r
2254   /* Process Locked */\r
2255   __HAL_LOCK(hcryp);\r
2256   \r
2257   /* Change the CRYP state */\r
2258   hcryp->State = HAL_CRYP_STATE_BUSY;\r
2259   \r
2260   /* Set CRYP peripheral in DES ECB encryption mode */\r
2261   CRYP_SetDESECBMode(hcryp, 0);\r
2262   \r
2263   /* Enable CRYP */\r
2264   __HAL_CRYP_ENABLE();\r
2265   \r
2266   /* Write Plain Data and Get Cypher Data */\r
2267   if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)\r
2268   {\r
2269     return HAL_TIMEOUT;\r
2270   }\r
2271   \r
2272   /* Change the CRYP state */\r
2273   hcryp->State = HAL_CRYP_STATE_READY;\r
2274   \r
2275   /* Process Unlocked */\r
2276   __HAL_UNLOCK(hcryp);\r
2277   \r
2278   /* Return function status */\r
2279   return HAL_OK;\r
2280 }\r
2281 \r
2282 /**\r
2283   * @brief  Initializes the CRYP peripheral in DES ECB decryption mode.\r
2284   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2285   *         the configuration information for CRYP module\r
2286   * @param  pPlainData: Pointer to the plaintext buffer\r
2287   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2288   * @param  pCypherData: Pointer to the cyphertext buffer\r
2289   * @param  Timeout: Specify Timeout value  \r
2290   * @retval HAL status\r
2291   */\r
2292 HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)\r
2293 {\r
2294   /* Process Locked */\r
2295   __HAL_LOCK(hcryp);\r
2296   \r
2297   /* Change the CRYP state */\r
2298   hcryp->State = HAL_CRYP_STATE_BUSY;\r
2299   \r
2300   /* Set CRYP peripheral in DES ECB decryption mode */\r
2301   CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);\r
2302   \r
2303   /* Enable CRYP */\r
2304   __HAL_CRYP_ENABLE();\r
2305   \r
2306   /* Write Plain Data and Get Cypher Data */\r
2307   if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)\r
2308   {\r
2309     return HAL_TIMEOUT;\r
2310   }\r
2311   \r
2312   /* Change the CRYP state */\r
2313   hcryp->State = HAL_CRYP_STATE_READY;\r
2314   \r
2315   /* Process Unlocked */\r
2316   __HAL_UNLOCK(hcryp);\r
2317   \r
2318   /* Return function status */\r
2319   return HAL_OK;\r
2320 }\r
2321 \r
2322 /**\r
2323   * @brief  Initializes the CRYP peripheral in DES CBC encryption mode.\r
2324   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2325   *         the configuration information for CRYP module\r
2326   * @param  pPlainData: Pointer to the plaintext buffer\r
2327   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2328   * @param  pCypherData: Pointer to the cyphertext buffer\r
2329   * @param  Timeout: Specify Timeout value  \r
2330   * @retval HAL status\r
2331   */\r
2332 HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)\r
2333 {\r
2334   /* Process Locked */\r
2335   __HAL_LOCK(hcryp);\r
2336   \r
2337   /* Change the CRYP state */\r
2338   hcryp->State = HAL_CRYP_STATE_BUSY;\r
2339   \r
2340   /* Set CRYP peripheral in DES CBC encryption mode */\r
2341   CRYP_SetDESCBCMode(hcryp, 0);\r
2342   \r
2343   /* Enable CRYP */\r
2344   __HAL_CRYP_ENABLE();\r
2345   \r
2346   /* Write Plain Data and Get Cypher Data */\r
2347   if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)\r
2348   {\r
2349     return HAL_TIMEOUT;\r
2350   }\r
2351   \r
2352   /* Change the CRYP state */\r
2353   hcryp->State = HAL_CRYP_STATE_READY;\r
2354   \r
2355   /* Process Unlocked */\r
2356   __HAL_UNLOCK(hcryp);\r
2357   \r
2358   /* Return function status */\r
2359   return HAL_OK;\r
2360 }\r
2361 \r
2362 /**\r
2363   * @brief  Initializes the CRYP peripheral in DES ECB decryption mode.\r
2364   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2365   *         the configuration information for CRYP module\r
2366   * @param  pPlainData: Pointer to the plaintext buffer\r
2367   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2368   * @param  pCypherData: Pointer to the cyphertext buffer\r
2369   * @param  Timeout: Specify Timeout value  \r
2370   * @retval HAL status\r
2371   */\r
2372 HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)\r
2373 {\r
2374   /* Process Locked */\r
2375   __HAL_LOCK(hcryp);\r
2376   \r
2377   /* Change the CRYP state */\r
2378   hcryp->State = HAL_CRYP_STATE_BUSY;\r
2379   \r
2380   /* Set CRYP peripheral in DES CBC decryption mode */\r
2381   CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);\r
2382   \r
2383   /* Enable CRYP */\r
2384   __HAL_CRYP_ENABLE();\r
2385   \r
2386   /* Write Plain Data and Get Cypher Data */\r
2387   if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)\r
2388   {\r
2389     return HAL_TIMEOUT;\r
2390   }\r
2391   \r
2392   /* Change the CRYP state */\r
2393   hcryp->State = HAL_CRYP_STATE_READY;\r
2394   \r
2395   /* Process Unlocked */\r
2396   __HAL_UNLOCK(hcryp);\r
2397   \r
2398   /* Return function status */\r
2399   return HAL_OK;\r
2400 }\r
2401 \r
2402 /**\r
2403   * @brief  Initializes the CRYP peripheral in DES ECB encryption mode using IT.\r
2404   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2405   *         the configuration information for CRYP module\r
2406   * @param  pPlainData: Pointer to the plaintext buffer\r
2407   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2408   * @param  pCypherData: Pointer to the cyphertext buffer\r
2409   * @retval HAL status\r
2410   */\r
2411 HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
2412 {\r
2413   uint32_t inputaddr;\r
2414   uint32_t outputaddr;\r
2415   \r
2416   if(hcryp->State == HAL_CRYP_STATE_READY)\r
2417   {\r
2418     /* Process Locked */\r
2419     __HAL_LOCK(hcryp);\r
2420     \r
2421     hcryp->CrypInCount = Size;\r
2422     hcryp->pCrypInBuffPtr = pPlainData;\r
2423     hcryp->pCrypOutBuffPtr = pCypherData;\r
2424     hcryp->CrypOutCount = Size;\r
2425     \r
2426     /* Change the CRYP state */\r
2427     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2428     \r
2429     /* Set CRYP peripheral in DES ECB encryption mode */\r
2430     CRYP_SetDESECBMode(hcryp, 0);\r
2431     \r
2432     /* Enable Interrupts */\r
2433     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
2434     \r
2435     /* Enable CRYP */\r
2436     __HAL_CRYP_ENABLE();\r
2437     \r
2438     /* Return function status */\r
2439     return HAL_OK;\r
2440   }\r
2441   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
2442   {\r
2443     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
2444     /* Write the Input block in the IN FIFO */\r
2445     CRYP->DR = *(uint32_t*)(inputaddr);\r
2446     inputaddr+=4;\r
2447     CRYP->DR = *(uint32_t*)(inputaddr);\r
2448     \r
2449     hcryp->pCrypInBuffPtr += 8;\r
2450     hcryp->CrypInCount -= 8;\r
2451     if(hcryp->CrypInCount == 0)\r
2452     {\r
2453       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
2454       /* Call the Input data transfer complete callback */\r
2455       HAL_CRYP_InCpltCallback(hcryp);\r
2456     }\r
2457   }\r
2458   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
2459   {\r
2460     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
2461     /* Read the Output block from the Output FIFO */\r
2462     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
2463     outputaddr+=4;\r
2464     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
2465     \r
2466     hcryp->pCrypOutBuffPtr += 8;\r
2467     hcryp->CrypOutCount -= 8;\r
2468     if(hcryp->CrypOutCount == 0)\r
2469     {\r
2470       /* Disable IT */\r
2471       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
2472       /* Disable CRYP */\r
2473       __HAL_CRYP_DISABLE();\r
2474       /* Process Unlocked */\r
2475       __HAL_UNLOCK(hcryp);\r
2476       /* Change the CRYP state */\r
2477       hcryp->State = HAL_CRYP_STATE_READY;\r
2478       /* Call Input transfer complete callback */\r
2479       HAL_CRYP_OutCpltCallback(hcryp);\r
2480     }\r
2481   }\r
2482   \r
2483   /* Return function status */\r
2484   return HAL_OK;\r
2485 }\r
2486 \r
2487 /**\r
2488   * @brief  Initializes the CRYP peripheral in DES CBC encryption mode using interrupt.\r
2489   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2490   *         the configuration information for CRYP module\r
2491   * @param  pPlainData: Pointer to the plaintext buffer\r
2492   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2493   * @param  pCypherData: Pointer to the cyphertext buffer\r
2494   * @retval HAL status\r
2495   */\r
2496 HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
2497 {\r
2498   uint32_t inputaddr;\r
2499   uint32_t outputaddr;\r
2500   \r
2501   if(hcryp->State == HAL_CRYP_STATE_READY)\r
2502   {\r
2503     /* Process Locked */\r
2504     __HAL_LOCK(hcryp);\r
2505     \r
2506     hcryp->CrypInCount = Size;\r
2507     hcryp->pCrypInBuffPtr = pPlainData;\r
2508     hcryp->pCrypOutBuffPtr = pCypherData;\r
2509     hcryp->CrypOutCount = Size;\r
2510     \r
2511     /* Change the CRYP state */\r
2512     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2513     \r
2514     /* Set CRYP peripheral in DES CBC encryption mode */\r
2515     CRYP_SetDESCBCMode(hcryp, 0);\r
2516     \r
2517     /* Enable Interrupts */\r
2518     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
2519     \r
2520     /* Enable CRYP */\r
2521     __HAL_CRYP_ENABLE();\r
2522     \r
2523     /* Return function status */\r
2524     return HAL_OK;\r
2525   }\r
2526   \r
2527   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
2528   {\r
2529     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
2530     /* Write the Input block in the IN FIFO */\r
2531     CRYP->DR = *(uint32_t*)(inputaddr);\r
2532     inputaddr+=4;\r
2533     CRYP->DR = *(uint32_t*)(inputaddr);\r
2534 \r
2535     hcryp->pCrypInBuffPtr += 8;\r
2536     hcryp->CrypInCount -= 8;\r
2537     if(hcryp->CrypInCount == 0)\r
2538     {\r
2539       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
2540       /* Call the Input data transfer complete callback */\r
2541       HAL_CRYP_InCpltCallback(hcryp);\r
2542     }\r
2543   }\r
2544   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
2545   {\r
2546     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
2547     /* Read the Output block from the Output FIFO */\r
2548     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
2549     outputaddr+=4;\r
2550     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
2551 \r
2552     hcryp->pCrypOutBuffPtr += 8;\r
2553     hcryp->CrypOutCount -= 8;\r
2554     if(hcryp->CrypOutCount == 0)\r
2555     {\r
2556       /* Disable IT */\r
2557       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
2558       /* Disable CRYP */\r
2559       __HAL_CRYP_DISABLE();\r
2560       /* Process Unlocked */\r
2561       __HAL_UNLOCK(hcryp);\r
2562       /* Change the CRYP state */\r
2563       hcryp->State = HAL_CRYP_STATE_READY;\r
2564       /* Call Input transfer complete callback */\r
2565       HAL_CRYP_OutCpltCallback(hcryp);\r
2566     }\r
2567   }\r
2568   \r
2569   /* Return function status */\r
2570   return HAL_OK;\r
2571 }\r
2572 \r
2573 /**\r
2574   * @brief  Initializes the CRYP peripheral in DES ECB decryption mode using IT.\r
2575   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2576   *         the configuration information for CRYP module\r
2577   * @param  pPlainData: Pointer to the plaintext buffer\r
2578   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2579   * @param  pCypherData: Pointer to the cyphertext buffer\r
2580   * @retval HAL status\r
2581   */\r
2582 HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
2583 {\r
2584   uint32_t inputaddr;\r
2585   uint32_t outputaddr;\r
2586   \r
2587   if(hcryp->State == HAL_CRYP_STATE_READY)\r
2588   {\r
2589     /* Process Locked */\r
2590     __HAL_LOCK(hcryp);\r
2591     \r
2592     hcryp->CrypInCount = Size;\r
2593     hcryp->pCrypInBuffPtr = pCypherData;\r
2594     hcryp->pCrypOutBuffPtr = pPlainData;\r
2595     hcryp->CrypOutCount = Size;\r
2596     \r
2597     /* Change the CRYP state */\r
2598     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2599     \r
2600     /* Set CRYP peripheral in DES ECB decryption mode */\r
2601     CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);\r
2602     \r
2603     /* Enable Interrupts */\r
2604     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
2605     \r
2606     /* Enable CRYP */\r
2607     __HAL_CRYP_ENABLE();\r
2608     \r
2609     /* Return function status */\r
2610     return HAL_OK;\r
2611   }\r
2612   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
2613   {\r
2614     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
2615     /* Write the Input block in the IN FIFO */\r
2616     CRYP->DR = *(uint32_t*)(inputaddr);\r
2617     inputaddr+=4;\r
2618     CRYP->DR = *(uint32_t*)(inputaddr);\r
2619     \r
2620     hcryp->pCrypInBuffPtr += 8;\r
2621     hcryp->CrypInCount -= 8;\r
2622     if(hcryp->CrypInCount == 0)\r
2623     {\r
2624       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
2625       /* Call the Input data transfer complete callback */\r
2626       HAL_CRYP_InCpltCallback(hcryp);\r
2627     }\r
2628   }\r
2629   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
2630   {\r
2631     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
2632     /* Read the Output block from the Output FIFO */\r
2633     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
2634     outputaddr+=4;\r
2635     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
2636 \r
2637     hcryp->pCrypOutBuffPtr += 8;\r
2638     hcryp->CrypOutCount -= 8;\r
2639     if(hcryp->CrypOutCount == 0)\r
2640     {\r
2641       /* Disable IT */\r
2642       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
2643       /* Disable CRYP */\r
2644       __HAL_CRYP_DISABLE();\r
2645       /* Process Unlocked */\r
2646       __HAL_UNLOCK(hcryp);\r
2647       /* Change the CRYP state */\r
2648       hcryp->State = HAL_CRYP_STATE_READY;\r
2649       /* Call Input transfer complete callback */\r
2650       HAL_CRYP_OutCpltCallback(hcryp);\r
2651     }\r
2652   }\r
2653   \r
2654   /* Return function status */\r
2655   return HAL_OK;\r
2656 }\r
2657 \r
2658 /**\r
2659   * @brief  Initializes the CRYP peripheral in DES ECB decryption mode using interrupt.\r
2660   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2661   *         the configuration information for CRYP module\r
2662   * @param  pPlainData: Pointer to the plaintext buffer\r
2663   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2664   * @param  pCypherData: Pointer to the cyphertext buffer\r
2665   * @retval HAL status\r
2666   */\r
2667 HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
2668 {\r
2669   uint32_t inputaddr;\r
2670   uint32_t outputaddr;\r
2671   \r
2672   if(hcryp->State == HAL_CRYP_STATE_READY)\r
2673   {\r
2674     /* Process Locked */\r
2675     __HAL_LOCK(hcryp);\r
2676     \r
2677     hcryp->CrypInCount = Size;\r
2678     hcryp->pCrypInBuffPtr = pCypherData;\r
2679     hcryp->pCrypOutBuffPtr = pPlainData;\r
2680     hcryp->CrypOutCount = Size;\r
2681     \r
2682     /* Change the CRYP state */\r
2683     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2684     \r
2685     /* Set CRYP peripheral in DES CBC decryption mode */\r
2686     CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);\r
2687     \r
2688     /* Enable Interrupts */\r
2689     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
2690     \r
2691     /* Enable CRYP */\r
2692     __HAL_CRYP_ENABLE();\r
2693     \r
2694     /* Return function status */\r
2695     return HAL_OK;\r
2696   }\r
2697   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
2698   {\r
2699     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
2700     /* Write the Input block in the IN FIFO */\r
2701     CRYP->DR = *(uint32_t*)(inputaddr);\r
2702     inputaddr+=4;\r
2703     CRYP->DR = *(uint32_t*)(inputaddr);\r
2704 \r
2705     hcryp->pCrypInBuffPtr += 8;\r
2706     hcryp->CrypInCount -= 8;\r
2707     if(hcryp->CrypInCount == 0)\r
2708     {\r
2709       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
2710       /* Call the Input data transfer complete callback */\r
2711       HAL_CRYP_InCpltCallback(hcryp);\r
2712     }\r
2713   }\r
2714   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
2715   {\r
2716     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
2717     /* Read the Output block from the Output FIFO */\r
2718     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
2719     outputaddr+=4;\r
2720     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
2721 \r
2722     hcryp->pCrypOutBuffPtr += 8;\r
2723     hcryp->CrypOutCount -= 8;\r
2724     if(hcryp->CrypOutCount == 0)\r
2725     {\r
2726       /* Disable IT */\r
2727       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
2728       /* Disable CRYP */\r
2729       __HAL_CRYP_DISABLE();\r
2730       /* Process Unlocked */\r
2731       __HAL_UNLOCK(hcryp);\r
2732       /* Change the CRYP state */\r
2733       hcryp->State = HAL_CRYP_STATE_READY;\r
2734       /* Call Input transfer complete callback */\r
2735       HAL_CRYP_OutCpltCallback(hcryp);\r
2736     }\r
2737   }\r
2738   \r
2739   /* Return function status */\r
2740   return HAL_OK;\r
2741 }\r
2742 \r
2743 /**\r
2744   * @brief  Initializes the CRYP peripheral in DES ECB encryption mode using DMA.\r
2745   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2746   *         the configuration information for CRYP module\r
2747   * @param  pPlainData: Pointer to the plaintext buffer\r
2748   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2749   * @param  pCypherData: Pointer to the cyphertext buffer\r
2750   * @retval HAL status\r
2751   */\r
2752 HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
2753 {\r
2754   uint32_t inputaddr;\r
2755   uint32_t outputaddr;\r
2756   \r
2757   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
2758   {\r
2759     /* Process Locked */\r
2760     __HAL_LOCK(hcryp);\r
2761     \r
2762     inputaddr  = (uint32_t)pPlainData;\r
2763     outputaddr = (uint32_t)pCypherData;\r
2764     \r
2765     /* Change the CRYP state */\r
2766     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2767     \r
2768     /* Set CRYP peripheral in DES ECB encryption mode */\r
2769     CRYP_SetDESECBMode(hcryp, 0);\r
2770     \r
2771     /* Set the input and output addresses and start DMA transfer */ \r
2772     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
2773     \r
2774     /* Process Unlocked */\r
2775     __HAL_UNLOCK(hcryp);\r
2776     \r
2777     /* Return function status */\r
2778     return HAL_OK;\r
2779   }\r
2780   else\r
2781   {\r
2782     return HAL_ERROR;   \r
2783   }\r
2784 }\r
2785 \r
2786 /**\r
2787   * @brief  Initializes the CRYP peripheral in DES CBC encryption mode using DMA.\r
2788   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2789   *         the configuration information for CRYP module\r
2790   * @param  pPlainData: Pointer to the plaintext buffer\r
2791   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2792   * @param  pCypherData: Pointer to the cyphertext buffer\r
2793   * @retval HAL status\r
2794   */\r
2795 HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
2796 {\r
2797   uint32_t inputaddr;\r
2798   uint32_t outputaddr;\r
2799   \r
2800   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
2801   {\r
2802     /* Process Locked */\r
2803     __HAL_LOCK(hcryp);\r
2804     \r
2805     inputaddr  = (uint32_t)pPlainData;\r
2806     outputaddr = (uint32_t)pCypherData;\r
2807     \r
2808     /* Change the CRYP state */\r
2809     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2810     \r
2811     /* Set CRYP peripheral in DES CBC encryption mode */\r
2812     CRYP_SetDESCBCMode(hcryp, 0);\r
2813     \r
2814     /* Set the input and output addresses and start DMA transfer */ \r
2815     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
2816     \r
2817     /* Process Unlocked */\r
2818     __HAL_UNLOCK(hcryp);\r
2819     \r
2820     /* Return function status */\r
2821     return HAL_OK;\r
2822   }\r
2823   else\r
2824   {\r
2825     return HAL_ERROR;   \r
2826   }\r
2827 }\r
2828 \r
2829 /**\r
2830   * @brief  Initializes the CRYP peripheral in DES ECB decryption mode using DMA.\r
2831   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2832   *         the configuration information for CRYP module\r
2833   * @param  pPlainData: Pointer to the plaintext buffer\r
2834   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2835   * @param  pCypherData: Pointer to the cyphertext buffer\r
2836   * @retval HAL status\r
2837   */\r
2838 HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
2839 {\r
2840   uint32_t inputaddr;\r
2841   uint32_t outputaddr;\r
2842   \r
2843   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
2844   {\r
2845     /* Process Locked */\r
2846     __HAL_LOCK(hcryp);\r
2847     \r
2848     inputaddr  = (uint32_t)pCypherData;\r
2849     outputaddr = (uint32_t)pPlainData;\r
2850     \r
2851     /* Change the CRYP state */\r
2852     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2853     \r
2854     /* Set CRYP peripheral in DES ECB decryption mode */\r
2855     CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);\r
2856     \r
2857     /* Set the input and output addresses and start DMA transfer */ \r
2858     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
2859     \r
2860     /* Process Unlocked */\r
2861     __HAL_UNLOCK(hcryp);\r
2862     \r
2863     /* Return function status */\r
2864     return HAL_OK;\r
2865   }\r
2866   else\r
2867   {\r
2868     return HAL_ERROR;   \r
2869   }\r
2870 }\r
2871 \r
2872 /**\r
2873   * @brief  Initializes the CRYP peripheral in DES ECB decryption mode using DMA.\r
2874   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2875   *         the configuration information for CRYP module\r
2876   * @param  pPlainData: Pointer to the plaintext buffer\r
2877   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2878   * @param  pCypherData: Pointer to the cyphertext buffer\r
2879   * @retval HAL status\r
2880   */\r
2881 HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
2882 {\r
2883   uint32_t inputaddr;\r
2884   uint32_t outputaddr;\r
2885   \r
2886   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
2887   {\r
2888     /* Process Locked */\r
2889     __HAL_LOCK(hcryp);\r
2890     \r
2891     inputaddr  = (uint32_t)pCypherData;\r
2892     outputaddr = (uint32_t)pPlainData;\r
2893     \r
2894     /* Change the CRYP state */\r
2895     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2896     \r
2897     /* Set CRYP peripheral in DES CBC decryption mode */\r
2898     CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);\r
2899     \r
2900     /* Set the input and output addresses and start DMA transfer */ \r
2901     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
2902     \r
2903     /* Process Unlocked */\r
2904     __HAL_UNLOCK(hcryp);\r
2905     \r
2906     /* Return function status */\r
2907     return HAL_OK;\r
2908   }\r
2909   else\r
2910   {\r
2911     return HAL_ERROR;   \r
2912   }\r
2913 }\r
2914 \r
2915 /**\r
2916   * @}\r
2917   */\r
2918 \r
2919 /** @defgroup CRYP_Exported_Functions_Group4 TDES processing functions \r
2920  *  @brief   processing functions. \r
2921  *\r
2922 @verbatim   \r
2923   ==============================================================================\r
2924                       ##### TDES processing functions #####\r
2925   ==============================================================================  \r
2926     [..]  This section provides functions allowing to:\r
2927       (+) Encrypt plaintext using TDES based on ECB or CBC chaining modes\r
2928       (+) Decrypt cyphertext using TDES based on ECB or CBC chaining modes\r
2929     [..]  Three processing functions are available:\r
2930       (+) Polling mode\r
2931       (+) Interrupt mode\r
2932       (+) DMA mode\r
2933 \r
2934 @endverbatim\r
2935   * @{\r
2936   */\r
2937 \r
2938 /**\r
2939   * @brief  Initializes the CRYP peripheral in TDES ECB encryption mode\r
2940   *         then encrypt pPlainData. The cypher data are available in pCypherData\r
2941   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2942   *         the configuration information for CRYP module\r
2943   * @param  pPlainData: Pointer to the plaintext buffer\r
2944   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2945   * @param  pCypherData: Pointer to the cyphertext buffer\r
2946   * @param  Timeout: Specify Timeout value  \r
2947   * @retval HAL status\r
2948   */\r
2949 HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)\r
2950 {\r
2951   /* Process Locked */\r
2952   __HAL_LOCK(hcryp);\r
2953   \r
2954   /* Change the CRYP state */\r
2955   hcryp->State = HAL_CRYP_STATE_BUSY;\r
2956   \r
2957   /* Set CRYP peripheral in TDES ECB encryption mode */\r
2958   CRYP_SetTDESECBMode(hcryp, 0);\r
2959   \r
2960   /* Enable CRYP */\r
2961   __HAL_CRYP_ENABLE();\r
2962   \r
2963   /* Write Plain Data and Get Cypher Data */\r
2964   if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)\r
2965   {\r
2966     return HAL_TIMEOUT;\r
2967   }\r
2968   \r
2969   /* Change the CRYP state */\r
2970   hcryp->State = HAL_CRYP_STATE_READY;\r
2971   \r
2972   /* Process Unlocked */\r
2973   __HAL_UNLOCK(hcryp);\r
2974   \r
2975   /* Return function status */\r
2976   return HAL_OK;\r
2977 }\r
2978 \r
2979 /**\r
2980   * @brief  Initializes the CRYP peripheral in TDES ECB decryption mode\r
2981   *         then decrypted pCypherData. The cypher data are available in pPlainData\r
2982   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2983   *         the configuration information for CRYP module\r
2984   * @param  pPlainData: Pointer to the plaintext buffer\r
2985   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
2986   * @param  pCypherData: Pointer to the cyphertext buffer\r
2987   * @param  Timeout: Specify Timeout value  \r
2988   * @retval HAL status\r
2989   */\r
2990 HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)\r
2991 {  \r
2992   /* Process Locked */\r
2993   __HAL_LOCK(hcryp);\r
2994   \r
2995   /* Change the CRYP state */\r
2996   hcryp->State = HAL_CRYP_STATE_BUSY;\r
2997   \r
2998   /* Set CRYP peripheral in TDES ECB decryption mode */\r
2999   CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);\r
3000   \r
3001   /* Enable CRYP */\r
3002   __HAL_CRYP_ENABLE();\r
3003   \r
3004   /* Write Cypher Data and Get Plain Data */\r
3005   if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)\r
3006   {\r
3007     return HAL_TIMEOUT;\r
3008   }\r
3009   \r
3010   /* Change the CRYP state */\r
3011   hcryp->State = HAL_CRYP_STATE_READY;\r
3012   \r
3013   /* Process Unlocked */\r
3014   __HAL_UNLOCK(hcryp);\r
3015   \r
3016   /* Return function status */\r
3017   return HAL_OK;\r
3018 }\r
3019 \r
3020 /**\r
3021   * @brief  Initializes the CRYP peripheral in TDES CBC encryption mode\r
3022   *         then encrypt pPlainData. The cypher data are available in pCypherData\r
3023   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3024   *         the configuration information for CRYP module\r
3025   * @param  pPlainData: Pointer to the plaintext buffer\r
3026   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
3027   * @param  pCypherData: Pointer to the cyphertext buffer\r
3028   * @param  Timeout: Specify Timeout value  \r
3029   * @retval HAL status\r
3030   */\r
3031 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)\r
3032 {\r
3033   /* Process Locked */\r
3034   __HAL_LOCK(hcryp);\r
3035   \r
3036   /* Change the CRYP state */\r
3037   hcryp->State = HAL_CRYP_STATE_BUSY;\r
3038   \r
3039   /* Set CRYP peripheral in TDES CBC encryption mode */\r
3040   CRYP_SetTDESCBCMode(hcryp, 0);\r
3041   \r
3042   /* Enable CRYP */\r
3043   __HAL_CRYP_ENABLE();\r
3044   \r
3045   /* Write Plain Data and Get Cypher Data */\r
3046   if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)\r
3047   {\r
3048     return HAL_TIMEOUT;\r
3049   }\r
3050   \r
3051   /* Change the CRYP state */\r
3052   hcryp->State = HAL_CRYP_STATE_READY;\r
3053   \r
3054   /* Process Unlocked */\r
3055   __HAL_UNLOCK(hcryp);\r
3056   \r
3057   /* Return function status */\r
3058   return HAL_OK;\r
3059 }\r
3060 \r
3061 /**\r
3062   * @brief  Initializes the CRYP peripheral in TDES CBC decryption mode\r
3063   *         then decrypted pCypherData. The cypher data are available in pPlainData\r
3064   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3065   *         the configuration information for CRYP module\r
3066   * @param  pCypherData: Pointer to the cyphertext buffer\r
3067   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
3068   * @param  pPlainData: Pointer to the plaintext buffer\r
3069   * @param  Timeout: Specify Timeout value  \r
3070   * @retval HAL status\r
3071   */\r
3072 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)\r
3073 {\r
3074   /* Process Locked */\r
3075   __HAL_LOCK(hcryp);\r
3076   \r
3077   /* Change the CRYP state */\r
3078   hcryp->State = HAL_CRYP_STATE_BUSY;\r
3079   \r
3080   /* Set CRYP peripheral in TDES CBC decryption mode */\r
3081   CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);\r
3082   \r
3083   /* Enable CRYP */\r
3084   __HAL_CRYP_ENABLE();\r
3085   \r
3086   /* Write Cypher Data and Get Plain Data */\r
3087   if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)\r
3088   {\r
3089     return HAL_TIMEOUT;\r
3090   }\r
3091   \r
3092   /* Change the CRYP state */\r
3093   hcryp->State = HAL_CRYP_STATE_READY;\r
3094   \r
3095   /* Process Unlocked */\r
3096   __HAL_UNLOCK(hcryp);\r
3097   \r
3098   /* Return function status */\r
3099   return HAL_OK;\r
3100 }\r
3101 \r
3102 /**\r
3103   * @brief  Initializes the CRYP peripheral in TDES ECB encryption mode using interrupt.\r
3104   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3105   *         the configuration information for CRYP module\r
3106   * @param  pPlainData: Pointer to the plaintext buffer\r
3107   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
3108   * @param  pCypherData: Pointer to the cyphertext buffer\r
3109   * @retval HAL status\r
3110   */\r
3111 HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
3112 {\r
3113   uint32_t inputaddr;\r
3114   uint32_t outputaddr;\r
3115   \r
3116   if(hcryp->State == HAL_CRYP_STATE_READY)\r
3117   {\r
3118     /* Process Locked */\r
3119     __HAL_LOCK(hcryp);\r
3120     \r
3121     hcryp->CrypInCount = Size;\r
3122     hcryp->pCrypInBuffPtr = pPlainData;\r
3123     hcryp->pCrypOutBuffPtr = pCypherData;\r
3124     hcryp->CrypOutCount = Size;\r
3125     \r
3126     /* Change the CRYP state */\r
3127     hcryp->State = HAL_CRYP_STATE_BUSY;\r
3128     \r
3129     /* Set CRYP peripheral in TDES ECB encryption mode */\r
3130     CRYP_SetTDESECBMode(hcryp, 0);\r
3131     \r
3132     /* Enable Interrupts */\r
3133     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
3134     \r
3135     /* Enable CRYP */\r
3136     __HAL_CRYP_ENABLE();\r
3137     \r
3138     /* Return function status */\r
3139     return HAL_OK;\r
3140   }\r
3141   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
3142   {\r
3143     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
3144     /* Write the Input block in the IN FIFO */\r
3145     CRYP->DR = *(uint32_t*)(inputaddr);\r
3146     inputaddr+=4;\r
3147     CRYP->DR = *(uint32_t*)(inputaddr);\r
3148 \r
3149     hcryp->pCrypInBuffPtr += 8;\r
3150     hcryp->CrypInCount -= 8;\r
3151     if(hcryp->CrypInCount == 0)\r
3152     {\r
3153       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
3154       /* Call the Input data transfer complete callback */\r
3155       HAL_CRYP_InCpltCallback(hcryp);\r
3156     }\r
3157   }\r
3158   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
3159   {\r
3160     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
3161     /* Read the Output block from the Output FIFO */\r
3162     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
3163     outputaddr+=4;\r
3164     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
3165 \r
3166     hcryp->pCrypOutBuffPtr += 8;\r
3167     hcryp->CrypOutCount -= 8;\r
3168     if(hcryp->CrypOutCount == 0)\r
3169     {\r
3170       /* Disable IT */\r
3171       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
3172       /* Disable CRYP */\r
3173       __HAL_CRYP_DISABLE();\r
3174       /* Process Unlocked */\r
3175       __HAL_UNLOCK(hcryp);\r
3176       /* Change the CRYP state */\r
3177       hcryp->State = HAL_CRYP_STATE_READY;\r
3178       /* Call the Output data transfer complete callback */\r
3179       HAL_CRYP_OutCpltCallback(hcryp);\r
3180     }\r
3181   }\r
3182   \r
3183   /* Return function status */\r
3184   return HAL_OK;\r
3185 }\r
3186 \r
3187 /**\r
3188   * @brief  Initializes the CRYP peripheral in TDES CBC encryption mode.\r
3189   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3190   *         the configuration information for CRYP module\r
3191   * @param  pPlainData: Pointer to the plaintext buffer\r
3192   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
3193   * @param  pCypherData: Pointer to the cyphertext buffer\r
3194   * @retval HAL status\r
3195   */\r
3196 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
3197 {\r
3198   uint32_t inputaddr;\r
3199   uint32_t outputaddr;\r
3200   \r
3201   if(hcryp->State == HAL_CRYP_STATE_READY)\r
3202   {\r
3203     /* Process Locked */\r
3204     __HAL_LOCK(hcryp);\r
3205     \r
3206     hcryp->CrypInCount = Size;\r
3207     hcryp->pCrypInBuffPtr = pPlainData;\r
3208     hcryp->pCrypOutBuffPtr = pCypherData;\r
3209     hcryp->CrypOutCount = Size;\r
3210     \r
3211     /* Change the CRYP state */\r
3212     hcryp->State = HAL_CRYP_STATE_BUSY;\r
3213     \r
3214     /* Set CRYP peripheral in TDES CBC encryption mode */\r
3215     CRYP_SetTDESCBCMode(hcryp, 0);\r
3216     \r
3217     /* Enable Interrupts */\r
3218     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
3219     \r
3220     /* Enable CRYP */\r
3221     __HAL_CRYP_ENABLE();\r
3222     \r
3223     /* Return function status */\r
3224     return HAL_OK;\r
3225   }\r
3226   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
3227   {\r
3228     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
3229     /* Write the Input block in the IN FIFO */\r
3230     CRYP->DR = *(uint32_t*)(inputaddr);\r
3231     inputaddr+=4;\r
3232     CRYP->DR = *(uint32_t*)(inputaddr);\r
3233 \r
3234     hcryp->pCrypInBuffPtr += 8;\r
3235     hcryp->CrypInCount -= 8;\r
3236     if(hcryp->CrypInCount == 0)\r
3237     {\r
3238       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
3239       /* Call the Input data transfer complete callback */\r
3240       HAL_CRYP_InCpltCallback(hcryp);\r
3241     }\r
3242   }\r
3243   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
3244   {\r
3245     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
3246     /* Read the Output block from the Output FIFO */\r
3247     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
3248     outputaddr+=4;\r
3249     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
3250         \r
3251     hcryp->pCrypOutBuffPtr += 8;\r
3252     hcryp->CrypOutCount -= 8;\r
3253     if(hcryp->CrypOutCount == 0)\r
3254     {\r
3255       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
3256       /* Disable CRYP */\r
3257       __HAL_CRYP_DISABLE();\r
3258       /* Process Unlocked */\r
3259       __HAL_UNLOCK(hcryp);\r
3260       /* Change the CRYP state */\r
3261       hcryp->State = HAL_CRYP_STATE_READY;\r
3262       /* Call Input transfer complete callback */\r
3263       HAL_CRYP_OutCpltCallback(hcryp);\r
3264     }\r
3265   }\r
3266   \r
3267   /* Return function status */\r
3268   return HAL_OK;\r
3269 }\r
3270 \r
3271 /**\r
3272   * @brief  Initializes the CRYP peripheral in TDES ECB decryption mode.\r
3273   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3274   *         the configuration information for CRYP module\r
3275   * @param  pPlainData: Pointer to the plaintext buffer\r
3276   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
3277   * @param  pCypherData: Pointer to the cyphertext buffer\r
3278   * @retval HAL status\r
3279   */\r
3280 HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
3281 {\r
3282   uint32_t inputaddr;\r
3283   uint32_t outputaddr;\r
3284   \r
3285   if(hcryp->State == HAL_CRYP_STATE_READY)\r
3286   {\r
3287     /* Process Locked */\r
3288     __HAL_LOCK(hcryp);\r
3289     \r
3290     hcryp->CrypInCount = Size;\r
3291     hcryp->pCrypInBuffPtr = pCypherData;\r
3292     hcryp->pCrypOutBuffPtr = pPlainData;\r
3293     hcryp->CrypOutCount = Size;\r
3294     \r
3295     /* Change the CRYP state */\r
3296     hcryp->State = HAL_CRYP_STATE_BUSY;\r
3297     \r
3298     /* Set CRYP peripheral in TDES ECB decryption mode */\r
3299     CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);\r
3300     \r
3301     /* Enable Interrupts */\r
3302     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
3303     \r
3304     /* Enable CRYP */\r
3305     __HAL_CRYP_ENABLE();\r
3306     \r
3307     /* Return function status */\r
3308     return HAL_OK;\r
3309   }\r
3310   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
3311   {\r
3312     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
3313     /* Write the Input block in the IN FIFO */\r
3314     CRYP->DR = *(uint32_t*)(inputaddr);\r
3315     inputaddr+=4;\r
3316     CRYP->DR = *(uint32_t*)(inputaddr);\r
3317 \r
3318     hcryp->pCrypInBuffPtr += 8;\r
3319     hcryp->CrypInCount -= 8;\r
3320     if(hcryp->CrypInCount == 0)\r
3321     {\r
3322       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
3323       /* Call the Input data transfer complete callback */\r
3324       HAL_CRYP_InCpltCallback(hcryp);\r
3325     }\r
3326   }\r
3327   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
3328   {\r
3329     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
3330     /* Read the Output block from the Output FIFO */\r
3331     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
3332     outputaddr+=4;\r
3333     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
3334 \r
3335     hcryp->pCrypOutBuffPtr += 8;\r
3336     hcryp->CrypOutCount -= 8;\r
3337     if(hcryp->CrypOutCount == 0)\r
3338     {\r
3339       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
3340       /* Disable CRYP */\r
3341       __HAL_CRYP_DISABLE();\r
3342       /* Process Unlocked */\r
3343       __HAL_UNLOCK(hcryp);\r
3344       /* Change the CRYP state */\r
3345       hcryp->State = HAL_CRYP_STATE_READY;\r
3346       /* Call Input transfer complete callback */\r
3347       HAL_CRYP_OutCpltCallback(hcryp);\r
3348     }\r
3349   }\r
3350   \r
3351   /* Return function status */\r
3352   return HAL_OK;\r
3353\r
3354 \r
3355 /**\r
3356   * @brief  Initializes the CRYP peripheral in TDES CBC decryption mode.\r
3357   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3358   *         the configuration information for CRYP module\r
3359   * @param  pCypherData: Pointer to the cyphertext buffer\r
3360   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
3361   * @param  pPlainData: Pointer to the plaintext buffer\r
3362   * @retval HAL status\r
3363   */\r
3364 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
3365 {\r
3366   uint32_t inputaddr;\r
3367   uint32_t outputaddr;\r
3368   \r
3369   if(hcryp->State == HAL_CRYP_STATE_READY)\r
3370   {\r
3371     /* Process Locked */\r
3372     __HAL_LOCK(hcryp);\r
3373     \r
3374     hcryp->CrypInCount = Size;\r
3375     hcryp->pCrypInBuffPtr = pCypherData;\r
3376     hcryp->pCrypOutBuffPtr = pPlainData;\r
3377     hcryp->CrypOutCount = Size;\r
3378     \r
3379     /* Change the CRYP state */\r
3380     hcryp->State = HAL_CRYP_STATE_BUSY;\r
3381     \r
3382     /* Set CRYP peripheral in TDES CBC decryption mode */\r
3383     CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);\r
3384     \r
3385     /* Enable Interrupts */\r
3386     __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);\r
3387     \r
3388     /* Enable CRYP */\r
3389     __HAL_CRYP_ENABLE();\r
3390     \r
3391     /* Return function status */\r
3392     return HAL_OK;\r
3393   }\r
3394   else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))\r
3395   {\r
3396     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
3397     /* Write the Input block in the IN FIFO */\r
3398     CRYP->DR = *(uint32_t*)(inputaddr);\r
3399     inputaddr+=4;\r
3400     CRYP->DR = *(uint32_t*)(inputaddr);\r
3401 \r
3402     hcryp->pCrypInBuffPtr += 8;\r
3403     hcryp->CrypInCount -= 8;\r
3404     if(hcryp->CrypInCount == 0)\r
3405     {\r
3406       __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);\r
3407       /* Call the Input data transfer complete callback */\r
3408       HAL_CRYP_InCpltCallback(hcryp);\r
3409     }\r
3410   }\r
3411   else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))\r
3412   {\r
3413     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
3414     /* Read the Output block from the Output FIFO */\r
3415     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
3416     outputaddr+=4;\r
3417     *(uint32_t*)(outputaddr) = CRYP->DOUT;\r
3418 \r
3419     hcryp->pCrypOutBuffPtr += 8;\r
3420     hcryp->CrypOutCount -= 8;\r
3421     if(hcryp->CrypOutCount == 0)\r
3422     {\r
3423       __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);\r
3424       /* Disable CRYP */\r
3425       __HAL_CRYP_DISABLE();\r
3426       /* Process Unlocked */\r
3427       __HAL_UNLOCK(hcryp);\r
3428       /* Change the CRYP state */\r
3429       hcryp->State = HAL_CRYP_STATE_READY;\r
3430       /* Call Input transfer complete callback */\r
3431       HAL_CRYP_OutCpltCallback(hcryp);\r
3432     }\r
3433   }\r
3434   \r
3435   /* Return function status */\r
3436   return HAL_OK;\r
3437 }\r
3438 \r
3439 /**\r
3440   * @brief  Initializes the CRYP peripheral in TDES ECB encryption mode using DMA.\r
3441   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3442   *         the configuration information for CRYP module\r
3443   * @param  pPlainData: Pointer to the plaintext buffer\r
3444   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
3445   * @param  pCypherData: Pointer to the cyphertext buffer\r
3446   * @retval HAL status\r
3447   */\r
3448 HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
3449 {\r
3450   uint32_t inputaddr;\r
3451   uint32_t outputaddr;\r
3452   \r
3453   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
3454   {\r
3455     /* Process Locked */\r
3456     __HAL_LOCK(hcryp);\r
3457     \r
3458     inputaddr  = (uint32_t)pPlainData;\r
3459     outputaddr = (uint32_t)pCypherData;\r
3460     \r
3461     /* Change the CRYP state */\r
3462     hcryp->State = HAL_CRYP_STATE_BUSY;\r
3463     \r
3464     /* Set CRYP peripheral in TDES ECB encryption mode */\r
3465     CRYP_SetTDESECBMode(hcryp, 0);\r
3466     \r
3467     /* Set the input and output addresses and start DMA transfer */ \r
3468     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
3469     \r
3470     /* Process Unlocked */\r
3471     __HAL_UNLOCK(hcryp);\r
3472     \r
3473     /* Return function status */\r
3474     return HAL_OK;\r
3475   }\r
3476   else\r
3477   {\r
3478     return HAL_ERROR;   \r
3479   }\r
3480 }\r
3481 \r
3482 /**\r
3483   * @brief  Initializes the CRYP peripheral in TDES CBC encryption mode using DMA.\r
3484   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3485   *         the configuration information for CRYP module\r
3486   * @param  pPlainData: Pointer to the plaintext buffer\r
3487   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
3488   * @param  pCypherData: Pointer to the cyphertext buffer\r
3489   * @retval HAL status\r
3490   */\r
3491 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
3492 {\r
3493   uint32_t inputaddr;\r
3494   uint32_t outputaddr;\r
3495   \r
3496   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
3497   {\r
3498     /* Process Locked */\r
3499     __HAL_LOCK(hcryp);\r
3500     \r
3501     inputaddr  = (uint32_t)pPlainData;\r
3502     outputaddr = (uint32_t)pCypherData;\r
3503     \r
3504     /* Change the CRYP state */\r
3505     hcryp->State = HAL_CRYP_STATE_BUSY;\r
3506     \r
3507     /* Set CRYP peripheral in TDES CBC encryption mode */\r
3508     CRYP_SetTDESCBCMode(hcryp, 0);\r
3509     \r
3510     /* Set the input and output addresses and start DMA transfer */ \r
3511     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
3512     \r
3513     /* Process Unlocked */\r
3514     __HAL_UNLOCK(hcryp);\r
3515     \r
3516     /* Return function status */\r
3517     return HAL_OK;\r
3518   }\r
3519   else\r
3520   {\r
3521     return HAL_ERROR;   \r
3522   }\r
3523 }\r
3524 \r
3525 /**\r
3526   * @brief  Initializes the CRYP peripheral in TDES ECB decryption mode using DMA.\r
3527   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3528   *         the configuration information for CRYP module\r
3529   * @param  pPlainData: Pointer to the plaintext buffer\r
3530   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
3531   * @param  pCypherData: Pointer to the cyphertext buffer\r
3532   * @retval HAL status\r
3533   */\r
3534 HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
3535 {\r
3536   uint32_t inputaddr;\r
3537   uint32_t outputaddr;\r
3538   \r
3539   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
3540   {\r
3541     /* Process Locked */\r
3542     __HAL_LOCK(hcryp);\r
3543     \r
3544     inputaddr  = (uint32_t)pCypherData;\r
3545     outputaddr = (uint32_t)pPlainData;\r
3546     \r
3547     /* Change the CRYP state */\r
3548     hcryp->State = HAL_CRYP_STATE_BUSY;\r
3549     \r
3550     /* Set CRYP peripheral in TDES ECB decryption mode */\r
3551     CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);\r
3552     \r
3553     /* Set the input and output addresses and start DMA transfer */ \r
3554     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
3555     \r
3556     /* Process Unlocked */\r
3557     __HAL_UNLOCK(hcryp);\r
3558     \r
3559     /* Return function status */\r
3560     return HAL_OK;\r
3561   }\r
3562   else\r
3563   {\r
3564     return HAL_ERROR;   \r
3565   }\r
3566 }\r
3567 \r
3568 /**\r
3569   * @brief  Initializes the CRYP peripheral in TDES CBC decryption mode using DMA.\r
3570   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3571   *         the configuration information for CRYP module\r
3572   * @param  pCypherData: Pointer to the cyphertext buffer\r
3573   * @param  Size: Length of the plaintext buffer, must be a multiple of 8\r
3574   * @param  pPlainData: Pointer to the plaintext buffer\r
3575   * @retval HAL status\r
3576   */\r
3577 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
3578 {\r
3579   uint32_t inputaddr;\r
3580   uint32_t outputaddr;\r
3581   \r
3582   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
3583   {\r
3584     /* Process Locked */\r
3585     __HAL_LOCK(hcryp);\r
3586     \r
3587     inputaddr  = (uint32_t)pCypherData;\r
3588     outputaddr = (uint32_t)pPlainData;\r
3589     \r
3590     /* Change the CRYP state */\r
3591     hcryp->State = HAL_CRYP_STATE_BUSY;\r
3592     \r
3593     /* Set CRYP peripheral in TDES CBC decryption mode */\r
3594     CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);\r
3595     \r
3596     /* Set the input and output addresses and start DMA transfer */ \r
3597     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
3598     \r
3599     /* Process Unlocked */\r
3600     __HAL_UNLOCK(hcryp);\r
3601     \r
3602     /* Return function status */\r
3603     return HAL_OK;\r
3604   }\r
3605   else\r
3606   {\r
3607     return HAL_ERROR;   \r
3608   }\r
3609 }\r
3610 \r
3611 /**\r
3612   * @}\r
3613   */\r
3614 \r
3615 /** @defgroup CRYP_Exported_Functions_Group5 DMA callback functions \r
3616  *  @brief   DMA callback functions. \r
3617  *\r
3618 @verbatim   \r
3619   ==============================================================================\r
3620                       ##### DMA callback functions  #####\r
3621   ==============================================================================  \r
3622     [..]  This section provides DMA callback functions:\r
3623       (+) DMA Input data transfer complete\r
3624       (+) DMA Output data transfer complete\r
3625       (+) DMA error\r
3626 \r
3627 @endverbatim\r
3628   * @{\r
3629   */\r
3630 \r
3631 /**\r
3632   * @brief  Input FIFO transfer completed callbacks.\r
3633   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3634   *         the configuration information for CRYP module\r
3635   * @retval None\r
3636   */\r
3637 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)\r
3638 {\r
3639   /* NOTE : This function Should not be modified, when the callback is needed,\r
3640             the HAL_CRYP_InCpltCallback could be implemented in the user file\r
3641    */ \r
3642 }\r
3643 \r
3644 /**\r
3645   * @brief  Output FIFO transfer completed callbacks.\r
3646   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3647   *         the configuration information for CRYP module\r
3648   * @retval None\r
3649   */\r
3650 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)\r
3651 {\r
3652   /* NOTE : This function Should not be modified, when the callback is needed,\r
3653             the HAL_CRYP_OutCpltCallback could be implemented in the user file\r
3654    */\r
3655 }\r
3656 \r
3657 /**\r
3658   * @brief  CRYP error callbacks.\r
3659   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3660   *         the configuration information for CRYP module\r
3661   * @retval None\r
3662   */\r
3663  __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)\r
3664 {\r
3665   /* NOTE : This function Should not be modified, when the callback is needed,\r
3666             the HAL_CRYP_ErrorCallback could be implemented in the user file\r
3667    */ \r
3668 }\r
3669 \r
3670 /**\r
3671   * @}\r
3672   */\r
3673 \r
3674 /** @defgroup CRYP_Exported_Functions_Group6 CRYP IRQ handler management  \r
3675  *  @brief   CRYP IRQ handler.\r
3676  *\r
3677 @verbatim   \r
3678   ==============================================================================\r
3679                 ##### CRYP IRQ handler management #####\r
3680   ==============================================================================  \r
3681 [..]  This section provides CRYP IRQ handler function.\r
3682 \r
3683 @endverbatim\r
3684   * @{\r
3685   */\r
3686 \r
3687 /**\r
3688   * @brief  This function handles CRYP interrupt request.\r
3689   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3690   *         the configuration information for CRYP module\r
3691   * @retval None\r
3692   */\r
3693 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)\r
3694 {\r
3695   switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION)\r
3696   {\r
3697   case CRYP_CR_ALGOMODE_TDES_ECB_ENCRYPT:\r
3698     HAL_CRYP_TDESECB_Encrypt_IT(hcryp, NULL, 0, NULL);\r
3699     break;\r
3700     \r
3701   case CRYP_CR_ALGOMODE_TDES_ECB_DECRYPT:\r
3702     HAL_CRYP_TDESECB_Decrypt_IT(hcryp, NULL, 0, NULL);\r
3703     break;\r
3704     \r
3705   case CRYP_CR_ALGOMODE_TDES_CBC_ENCRYPT:\r
3706     HAL_CRYP_TDESCBC_Encrypt_IT(hcryp, NULL, 0, NULL);\r
3707     break;\r
3708     \r
3709   case CRYP_CR_ALGOMODE_TDES_CBC_DECRYPT:\r
3710     HAL_CRYP_TDESCBC_Decrypt_IT(hcryp, NULL, 0, NULL);\r
3711     break;\r
3712     \r
3713   case CRYP_CR_ALGOMODE_DES_ECB_ENCRYPT:\r
3714     HAL_CRYP_DESECB_Encrypt_IT(hcryp, NULL, 0, NULL);\r
3715     break;\r
3716     \r
3717   case CRYP_CR_ALGOMODE_DES_ECB_DECRYPT:\r
3718     HAL_CRYP_DESECB_Decrypt_IT(hcryp, NULL, 0, NULL);\r
3719     break;\r
3720     \r
3721   case CRYP_CR_ALGOMODE_DES_CBC_ENCRYPT:\r
3722     HAL_CRYP_DESCBC_Encrypt_IT(hcryp, NULL, 0, NULL);\r
3723     break;\r
3724     \r
3725   case CRYP_CR_ALGOMODE_DES_CBC_DECRYPT:\r
3726     HAL_CRYP_DESCBC_Decrypt_IT(hcryp, NULL, 0, NULL);\r
3727     break;\r
3728     \r
3729   case CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT:\r
3730     HAL_CRYP_AESECB_Encrypt_IT(hcryp, NULL, 0, NULL);\r
3731     break;\r
3732     \r
3733   case CRYP_CR_ALGOMODE_AES_ECB_DECRYPT:\r
3734     HAL_CRYP_AESECB_Decrypt_IT(hcryp, NULL, 0, NULL);\r
3735     break;\r
3736     \r
3737   case CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT:\r
3738     HAL_CRYP_AESCBC_Encrypt_IT(hcryp, NULL, 0, NULL);\r
3739     break;\r
3740     \r
3741   case CRYP_CR_ALGOMODE_AES_CBC_DECRYPT:\r
3742     HAL_CRYP_AESCBC_Decrypt_IT(hcryp, NULL, 0, NULL);\r
3743     break;\r
3744     \r
3745   case CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT:\r
3746     HAL_CRYP_AESCTR_Encrypt_IT(hcryp, NULL, 0, NULL);       \r
3747     break;\r
3748     \r
3749   case CRYP_CR_ALGOMODE_AES_CTR_DECRYPT:\r
3750     HAL_CRYP_AESCTR_Decrypt_IT(hcryp, NULL, 0, NULL);        \r
3751     break;\r
3752     \r
3753   default:\r
3754     break;\r
3755   }\r
3756 }\r
3757 \r
3758 /**\r
3759   * @}\r
3760   */\r
3761 \r
3762 /** @defgroup CRYP_Exported_Functions_Group7 Peripheral State functions \r
3763  *  @brief   Peripheral State functions. \r
3764  *\r
3765 @verbatim   \r
3766   ==============================================================================\r
3767                       ##### Peripheral State functions #####\r
3768   ==============================================================================  \r
3769     [..]\r
3770     This subsection permits to get in run-time the status of the peripheral.\r
3771 \r
3772 @endverbatim\r
3773   * @{\r
3774   */\r
3775 \r
3776 /**\r
3777   * @brief  Returns the CRYP state.\r
3778   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
3779   *         the configuration information for CRYP module\r
3780   * @retval HAL state\r
3781   */\r
3782 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)\r
3783 {\r
3784   return hcryp->State;\r
3785 }\r
3786 \r
3787 /**\r
3788   * @}\r
3789   */\r
3790 \r
3791 /**\r
3792   * @}\r
3793   */\r
3794 \r
3795 #endif /* STM32F756xx */\r
3796 \r
3797 #endif /* HAL_CRYP_MODULE_ENABLED */\r
3798 /**\r
3799   * @}\r
3800   */\r
3801 \r
3802 /**\r
3803   * @}\r
3804   */\r
3805 \r
3806 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r