]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_cryp_ex.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_hal_cryp_ex.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_cryp_ex.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    24-March-2015\r
7   * @brief   Extended CRYP HAL module driver\r
8   *          This file provides firmware functions to manage the following \r
9   *          functionalities of CRYP extension peripheral:\r
10   *           + Extended AES processing functions     \r
11   *  \r
12   @verbatim\r
13   ==============================================================================\r
14                      ##### How to use this driver #####\r
15   ==============================================================================\r
16     [..]\r
17     The CRYP Extension HAL driver can be used as follows:\r
18     (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():\r
19         (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()\r
20         (##) In case of using interrupts (e.g. HAL_CRYPEx_AESGCM_Encrypt_IT())\r
21             (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()\r
22             (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()\r
23             (+) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()\r
24         (##) In case of using DMA to control data transfer (e.g. HAL_AES_ECB_Encrypt_DMA())\r
25             (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE()\r
26             (+++) Configure and enable two DMA streams one for managing data transfer from\r
27                 memory to peripheral (input stream) and another stream for managing data\r
28                 transfer from peripheral to memory (output stream)\r
29             (+++) Associate the initialized DMA handle to the CRYP DMA handle\r
30                 using  __HAL_LINKDMA()\r
31             (+++) Configure the priority and enable the NVIC for the transfer complete\r
32                 interrupt on the two DMA Streams. The output stream should have higher\r
33                 priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()\r
34     (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly:\r
35         (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit\r
36         (##) The key size: 128, 192 and 256. This parameter is relevant only for AES\r
37         (##) The encryption/decryption key. Its size depends on the algorithm\r
38                 used for encryption/decryption\r
39         (##) The initialization vector (counter). It is not used ECB mode.\r
40     (#)Three processing (encryption/decryption) functions are available:\r
41         (##) Polling mode: encryption and decryption APIs are blocking functions\r
42              i.e. they process the data and wait till the processing is finished\r
43              e.g. HAL_CRYPEx_AESGCM_Encrypt()\r
44         (##) Interrupt mode: encryption and decryption APIs are not blocking functions\r
45                 i.e. they process the data under interrupt\r
46                 e.g. HAL_CRYPEx_AESGCM_Encrypt_IT()\r
47         (##) DMA mode: encryption and decryption APIs are not blocking functions\r
48                 i.e. the data transfer is ensured by DMA\r
49                 e.g. HAL_CRYPEx_AESGCM_Encrypt_DMA()\r
50     (#)When the processing function is called at first time after HAL_CRYP_Init()\r
51        the CRYP peripheral is initialized and processes the buffer in input.\r
52        At second call, the processing function performs an append of the already\r
53        processed buffer.\r
54        When a new data block is to be processed, call HAL_CRYP_Init() then the\r
55        processing function.\r
56     (#)In AES-GCM and AES-CCM modes are an authenticated encryption algorithms\r
57        which provide authentication messages.\r
58        HAL_AES_GCM_Finish() and HAL_AES_CCM_Finish() are used to provide those\r
59        authentication messages.\r
60        Call those functions after the processing ones (polling, interrupt or DMA).\r
61        e.g. in AES-CCM mode call HAL_CRYPEx_AESCCM_Encrypt() to encrypt the plain data\r
62             then call HAL_CRYPEx_AESCCM_Finish() to get the authentication message\r
63     @note: For CCM Encrypt/Decrypt API's, only DataType = 8-bit is supported by this version.\r
64     @note: The HAL_CRYPEx_AESGCM_xxxx() implementation is limited to 32bits inputs data length \r
65            (Plain/Cyphertext, Header) compared with GCM standards specifications (800-38D).\r
66     (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.\r
67 \r
68   @endverbatim\r
69   ******************************************************************************\r
70   * @attention\r
71   *\r
72   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
73   *\r
74   * Redistribution and use in source and binary forms, with or without modification,\r
75   * are permitted provided that the following conditions are met:\r
76   *   1. Redistributions of source code must retain the above copyright notice,\r
77   *      this list of conditions and the following disclaimer.\r
78   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
79   *      this list of conditions and the following disclaimer in the documentation\r
80   *      and/or other materials provided with the distribution.\r
81   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
82   *      may be used to endorse or promote products derived from this software\r
83   *      without specific prior written permission.\r
84   *\r
85   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
86   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
87   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
88   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
89   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
90   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
91   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
92   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
93   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
94   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
95   *\r
96   ******************************************************************************\r
97   */ \r
98 \r
99 /* Includes ------------------------------------------------------------------*/\r
100 #include "stm32f7xx_hal.h"\r
101 \r
102 /** @addtogroup STM32F7xx_HAL_Driver\r
103   * @{\r
104   */\r
105 \r
106 /** @defgroup CRYPEx CRYPEx\r
107   * @brief CRYP Extension HAL module driver.\r
108   * @{\r
109   */\r
110 \r
111 #ifdef HAL_CRYP_MODULE_ENABLED\r
112 \r
113 #if defined(STM32F756xx)\r
114 \r
115 /* Private typedef -----------------------------------------------------------*/\r
116 /* Private define ------------------------------------------------------------*/\r
117 /** @addtogroup CRYPEx_Private_define\r
118   * @{\r
119   */\r
120 #define CRYPEx_TIMEOUT_VALUE  1\r
121 /**\r
122   * @}\r
123   */ \r
124   \r
125 /* Private macro -------------------------------------------------------------*/\r
126 /* Private variables ---------------------------------------------------------*/\r
127 /* Private function prototypes -----------------------------------------------*/\r
128 /** @defgroup CRYPEx_Private_Functions_prototypes  CRYP Private Functions Prototypes\r
129   * @{\r
130   */\r
131 static void CRYPEx_GCMCCM_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector);\r
132 static void CRYPEx_GCMCCM_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize);\r
133 static HAL_StatusTypeDef CRYPEx_GCMCCM_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t *Input, uint16_t Ilength, uint8_t *Output, uint32_t Timeout);\r
134 static HAL_StatusTypeDef CRYPEx_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint32_t Timeout);\r
135 static void CRYPEx_GCMCCM_DMAInCplt(DMA_HandleTypeDef *hdma);\r
136 static void CRYPEx_GCMCCM_DMAOutCplt(DMA_HandleTypeDef *hdma);\r
137 static void CRYPEx_GCMCCM_DMAError(DMA_HandleTypeDef *hdma);\r
138 static void CRYPEx_GCMCCM_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);\r
139 /**\r
140   * @}\r
141   */ \r
142 \r
143 /* Private functions ---------------------------------------------------------*/\r
144 /** @addtogroup CRYPEx_Private_Functions\r
145   * @{\r
146   */\r
147 \r
148 /**\r
149   * @brief  DMA CRYP Input Data process complete callback. \r
150   * @param  hdma: DMA handle\r
151   * @retval None\r
152   */\r
153 static void CRYPEx_GCMCCM_DMAInCplt(DMA_HandleTypeDef *hdma)  \r
154 {\r
155   CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
156   \r
157   /* Disable the DMA transfer for input Fifo request by resetting the DIEN bit\r
158      in the DMACR register */\r
159   hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN);\r
160   \r
161   /* Call input data transfer complete callback */\r
162   HAL_CRYP_InCpltCallback(hcryp);\r
163 }\r
164 \r
165 /**\r
166   * @brief  DMA CRYP Output Data process complete callback.\r
167   * @param  hdma: DMA handle\r
168   * @retval None\r
169   */\r
170 static void CRYPEx_GCMCCM_DMAOutCplt(DMA_HandleTypeDef *hdma)\r
171 {\r
172   CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
173   \r
174   /* Disable the DMA transfer for output Fifo request by resetting the DOEN bit\r
175      in the DMACR register */\r
176   hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN);\r
177   \r
178   /* Enable the CRYP peripheral */\r
179   __HAL_CRYP_DISABLE(hcryp);\r
180   \r
181   /* Change the CRYP peripheral state */\r
182   hcryp->State = HAL_CRYP_STATE_READY;\r
183   \r
184   /* Call output data transfer complete callback */\r
185   HAL_CRYP_OutCpltCallback(hcryp);\r
186 }\r
187 \r
188 /**\r
189   * @brief  DMA CRYP communication error callback. \r
190   * @param  hdma: DMA handle\r
191   * @retval None\r
192   */\r
193 static void CRYPEx_GCMCCM_DMAError(DMA_HandleTypeDef *hdma)\r
194 {\r
195   CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
196   hcryp->State= HAL_CRYP_STATE_READY;\r
197   HAL_CRYP_ErrorCallback(hcryp);\r
198 }\r
199 \r
200 /**\r
201   * @brief  Writes the Key in Key registers. \r
202   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
203   *         the configuration information for CRYP module\r
204   * @param  Key: Pointer to Key buffer\r
205   * @param  KeySize: Size of Key\r
206   * @retval None\r
207   */\r
208 static void CRYPEx_GCMCCM_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize)\r
209 {\r
210   uint32_t keyaddr = (uint32_t)Key;\r
211   \r
212   switch(KeySize)\r
213   {\r
214   case CRYP_KEYSIZE_256B:\r
215     /* Key Initialisation */\r
216     hcryp->Instance->K0LR = __REV(*(uint32_t*)(keyaddr));\r
217     keyaddr+=4;\r
218     hcryp->Instance->K0RR = __REV(*(uint32_t*)(keyaddr));\r
219     keyaddr+=4;\r
220     hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));\r
221     keyaddr+=4;\r
222     hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));\r
223     keyaddr+=4;\r
224     hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));\r
225     keyaddr+=4;\r
226     hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));\r
227     keyaddr+=4;\r
228     hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));\r
229     keyaddr+=4;\r
230     hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));\r
231     break;\r
232   case CRYP_KEYSIZE_192B:\r
233     hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));\r
234     keyaddr+=4;\r
235     hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));\r
236     keyaddr+=4;\r
237     hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));\r
238     keyaddr+=4;\r
239     hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));\r
240     keyaddr+=4;\r
241     hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));\r
242     keyaddr+=4;\r
243     hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));\r
244     break;\r
245   case CRYP_KEYSIZE_128B:       \r
246     hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));\r
247     keyaddr+=4;\r
248     hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));\r
249     keyaddr+=4;\r
250     hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));\r
251     keyaddr+=4;\r
252     hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));\r
253     break;\r
254   default:\r
255     break;\r
256   }\r
257 }\r
258 \r
259 /**\r
260   * @brief  Writes the InitVector/InitCounter in IV registers.\r
261   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
262   *         the configuration information for CRYP module\r
263   * @param  InitVector: Pointer to InitVector/InitCounter buffer\r
264   * @retval None\r
265   */\r
266 static void CRYPEx_GCMCCM_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector)\r
267 {\r
268   uint32_t ivaddr = (uint32_t)InitVector;\r
269   \r
270   hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr));\r
271   ivaddr+=4;\r
272   hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr));\r
273   ivaddr+=4;\r
274   hcryp->Instance->IV1LR = __REV(*(uint32_t*)(ivaddr));\r
275   ivaddr+=4;\r
276   hcryp->Instance->IV1RR = __REV(*(uint32_t*)(ivaddr));\r
277 }\r
278 \r
279 /**\r
280   * @brief  Process Data: Writes Input data in polling mode and read the Output data.\r
281   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
282   *         the configuration information for CRYP module\r
283   * @param  Input: Pointer to the Input buffer.\r
284   * @param  Ilength: Length of the Input buffer, must be a multiple of 16\r
285   * @param  Output: Pointer to the returned buffer\r
286   * @param  Timeout: Timeout value \r
287   * @retval None\r
288   */\r
289 static HAL_StatusTypeDef CRYPEx_GCMCCM_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t *Input, uint16_t Ilength, uint8_t *Output, uint32_t Timeout)\r
290 {\r
291   uint32_t tickstart = 0;   \r
292   uint32_t i = 0;\r
293   uint32_t inputaddr  = (uint32_t)Input;\r
294   uint32_t outputaddr = (uint32_t)Output;\r
295   \r
296   for(i=0; (i < Ilength); i+=16)\r
297   {\r
298     /* Write the Input block in the IN FIFO */\r
299     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
300     inputaddr+=4;\r
301     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
302     inputaddr+=4;\r
303     hcryp->Instance->DR  = *(uint32_t*)(inputaddr);\r
304     inputaddr+=4;\r
305     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
306     inputaddr+=4;\r
307     \r
308     /* Get tick */\r
309     tickstart = HAL_GetTick();\r
310  \r
311     while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))\r
312     {\r
313       /* Check for the Timeout */\r
314       if(Timeout != HAL_MAX_DELAY)\r
315       {\r
316         if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
317         {\r
318           /* Change state */\r
319           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
320           \r
321           /* Process Unlocked */\r
322           __HAL_UNLOCK(hcryp);\r
323           \r
324           return HAL_TIMEOUT;\r
325         }\r
326       }\r
327     }\r
328     /* Read the Output block from the OUT FIFO */\r
329     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
330     outputaddr+=4;\r
331     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
332     outputaddr+=4;\r
333     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
334     outputaddr+=4;\r
335     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
336     outputaddr+=4;\r
337   }\r
338   /* Return function status */\r
339   return HAL_OK;\r
340 }\r
341 \r
342 /**\r
343   * @brief  Sets the header phase\r
344   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
345   *         the configuration information for CRYP module\r
346   * @param  Input: Pointer to the Input buffer.\r
347   * @param  Ilength: Length of the Input buffer, must be a multiple of 16\r
348   * @param  Timeout: Timeout value   \r
349   * @retval None\r
350   */\r
351 static HAL_StatusTypeDef CRYPEx_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint32_t Timeout)\r
352 {\r
353   uint32_t tickstart = 0;   \r
354   uint32_t loopcounter = 0;\r
355   uint32_t headeraddr = (uint32_t)Input;\r
356   \r
357   /***************************** Header phase *********************************/\r
358   if(hcryp->Init.HeaderSize != 0)\r
359   {\r
360     /* Select header phase */\r
361     __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);\r
362     /* Enable the CRYP peripheral */\r
363     __HAL_CRYP_ENABLE(hcryp);\r
364     \r
365     for(loopcounter = 0; (loopcounter < hcryp->Init.HeaderSize); loopcounter+=16)\r
366     {\r
367       /* Get tick */\r
368       tickstart = HAL_GetTick();\r
369       \r
370       while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))\r
371       {\r
372         /* Check for the Timeout */\r
373         if(Timeout != HAL_MAX_DELAY)\r
374         {\r
375           if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
376           {\r
377             /* Change state */\r
378             hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
379             \r
380             /* Process Unlocked */\r
381             __HAL_UNLOCK(hcryp);\r
382             \r
383             return HAL_TIMEOUT;\r
384           }\r
385         }\r
386       }\r
387       /* Write the Input block in the IN FIFO */\r
388       hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
389       headeraddr+=4;\r
390       hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
391       headeraddr+=4;\r
392       hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
393       headeraddr+=4;\r
394       hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
395       headeraddr+=4;\r
396     }\r
397     \r
398     /* Wait until the complete message has been processed */\r
399 \r
400     /* Get tick */\r
401     tickstart = HAL_GetTick();\r
402 \r
403     while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)\r
404     {\r
405       /* Check for the Timeout */\r
406       if(Timeout != HAL_MAX_DELAY)\r
407       {\r
408         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
409         {\r
410           /* Change state */\r
411           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
412           \r
413           /* Process Unlocked */\r
414           __HAL_UNLOCK(hcryp);\r
415           \r
416           return HAL_TIMEOUT;\r
417         }\r
418       }\r
419     }\r
420   }\r
421   /* Return function status */\r
422   return HAL_OK;\r
423 }\r
424 \r
425 /**\r
426   * @brief  Sets the DMA configuration and start the DMA transfer.\r
427   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
428   *         the configuration information for CRYP module\r
429   * @param  inputaddr: Address of the Input buffer\r
430   * @param  Size: Size of the Input buffer, must be a multiple of 16\r
431   * @param  outputaddr: Address of the Output buffer\r
432   * @retval None\r
433   */\r
434 static void CRYPEx_GCMCCM_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)\r
435 {\r
436   /* Set the CRYP DMA transfer complete callback */\r
437   hcryp->hdmain->XferCpltCallback = CRYPEx_GCMCCM_DMAInCplt;\r
438   /* Set the DMA error callback */\r
439   hcryp->hdmain->XferErrorCallback = CRYPEx_GCMCCM_DMAError;\r
440   \r
441   /* Set the CRYP DMA transfer complete callback */\r
442   hcryp->hdmaout->XferCpltCallback = CRYPEx_GCMCCM_DMAOutCplt;\r
443   /* Set the DMA error callback */\r
444   hcryp->hdmaout->XferErrorCallback = CRYPEx_GCMCCM_DMAError;\r
445   \r
446   /* Enable the CRYP peripheral */\r
447   __HAL_CRYP_ENABLE(hcryp);\r
448   \r
449   /* Enable the DMA In DMA Stream */\r
450   HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DR, Size/4);\r
451   \r
452   /* Enable In DMA request */\r
453   hcryp->Instance->DMACR = CRYP_DMACR_DIEN;\r
454   \r
455   /* Enable the DMA Out DMA Stream */\r
456   HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size/4);\r
457   \r
458   /* Enable Out DMA request */\r
459   hcryp->Instance->DMACR |= CRYP_DMACR_DOEN;\r
460 }\r
461 \r
462 /**\r
463   * @}\r
464   */\r
465 \r
466 /* Exported functions---------------------------------------------------------*/\r
467 /** @addtogroup CRYPEx_Exported_Functions\r
468   * @{\r
469   */\r
470 \r
471 /** @defgroup CRYPEx_Exported_Functions_Group1 Extended AES processing functions \r
472  *  @brief   Extended processing functions. \r
473  *\r
474 @verbatim   \r
475   ==============================================================================\r
476               ##### Extended AES processing functions #####\r
477   ==============================================================================  \r
478     [..]  This section provides functions allowing to:\r
479       (+) Encrypt plaintext using AES-128/192/256 using GCM and CCM chaining modes\r
480       (+) Decrypt cyphertext using AES-128/192/256 using GCM and CCM chaining modes\r
481       (+) Finish the processing. This function is available only for GCM and CCM\r
482     [..]  Three processing methods are available:\r
483       (+) Polling mode\r
484       (+) Interrupt mode\r
485       (+) DMA mode\r
486 \r
487 @endverbatim\r
488   * @{\r
489   */\r
490 \r
491 \r
492 /**\r
493   * @brief  Initializes the CRYP peripheral in AES CCM encryption mode then \r
494   *         encrypt pPlainData. The cypher data are available in pCypherData.\r
495   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
496   *         the configuration information for CRYP module\r
497   * @param  pPlainData: Pointer to the plaintext buffer\r
498   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
499   * @param  pCypherData: Pointer to the cyphertext buffer\r
500   * @param  Timeout: Timeout duration\r
501   * @retval HAL status\r
502   */\r
503 HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)\r
504 {\r
505   uint32_t tickstart = 0;\r
506   uint32_t headersize = hcryp->Init.HeaderSize;\r
507   uint32_t headeraddr = (uint32_t)hcryp->Init.Header;\r
508   uint32_t loopcounter = 0;\r
509   uint32_t bufferidx = 0;\r
510   uint8_t blockb0[16] = {0};/* Block B0 */\r
511   uint8_t ctr[16] = {0}; /* Counter */\r
512   uint32_t b0addr = (uint32_t)blockb0;\r
513   \r
514   /* Process Locked */\r
515   __HAL_LOCK(hcryp);\r
516   \r
517   /* Change the CRYP peripheral state */\r
518   hcryp->State = HAL_CRYP_STATE_BUSY;\r
519   \r
520   /* Check if initialization phase has already been performed */\r
521   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
522   {\r
523     /************************ Formatting the header block *********************/\r
524     if(headersize != 0)\r
525     {\r
526       /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */\r
527       if(headersize < 65280)\r
528       {\r
529         hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);\r
530         hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);\r
531         headersize += 2;\r
532       }\r
533       else\r
534       {\r
535         /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */\r
536         hcryp->Init.pScratch[bufferidx++] = 0xFF;\r
537         hcryp->Init.pScratch[bufferidx++] = 0xFE;\r
538         hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000;\r
539         hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000;\r
540         hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00;\r
541         hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ff;\r
542         headersize += 6;\r
543       }\r
544       /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */\r
545       for(loopcounter = 0; loopcounter < headersize; loopcounter++)\r
546       {\r
547         hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];\r
548       }\r
549       /* Check if the header size is modulo 16 */\r
550       if ((headersize % 16) != 0)\r
551       {\r
552         /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */\r
553         for(loopcounter = headersize; loopcounter <= ((headersize/16) + 1) * 16; loopcounter++)\r
554         {\r
555           hcryp->Init.pScratch[loopcounter] = 0;\r
556         }\r
557         /* Set the header size to modulo 16 */\r
558         headersize = ((headersize/16) + 1) * 16;\r
559       }\r
560       /* Set the pointer headeraddr to hcryp->Init.pScratch */\r
561       headeraddr = (uint32_t)hcryp->Init.pScratch;\r
562     }\r
563     /*********************** Formatting the block B0 **************************/\r
564     if(headersize != 0)\r
565     {\r
566       blockb0[0] = 0x40;\r
567     }\r
568     /* Flags byte */\r
569     /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07) */\r
570     blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07 ) << 3);\r
571     blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);\r
572  \r
573     for (loopcounter = 0; loopcounter < hcryp->Init.IVSize; loopcounter++)\r
574     {\r
575       blockb0[loopcounter+1] = hcryp->Init.pInitVect[loopcounter];\r
576     }\r
577     for ( ; loopcounter < 13; loopcounter++)\r
578     {\r
579       blockb0[loopcounter+1] = 0;\r
580     }\r
581     \r
582     blockb0[14] = (Size >> 8);\r
583     blockb0[15] = (Size & 0xFF);\r
584     \r
585     /************************* Formatting the initial counter *****************/\r
586     /* Byte 0:\r
587        Bits 7 and 6 are reserved and shall be set to 0\r
588        Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter blocks\r
589        are distinct from B0\r
590        Bits 0, 1, and 2 contain the same encoding of q as in B0\r
591     */\r
592     ctr[0] = blockb0[0] & 0x07;\r
593     /* byte 1 to NonceSize is the IV (Nonce) */\r
594     for(loopcounter = 1; loopcounter < hcryp->Init.IVSize + 1; loopcounter++)\r
595     {\r
596       ctr[loopcounter] = blockb0[loopcounter];\r
597     }\r
598     /* Set the LSB to 1 */\r
599     ctr[15] |= 0x01;\r
600     \r
601     /* Set the key */\r
602     CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
603     \r
604     /* Set the CRYP peripheral in AES CCM mode */\r
605     __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT);\r
606     \r
607     /* Set the Initialization Vector */\r
608     CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);\r
609     \r
610     /* Select init phase */\r
611     __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);\r
612     \r
613     b0addr = (uint32_t)blockb0;\r
614     /* Write the blockb0 block in the IN FIFO */\r
615     hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
616     b0addr+=4;\r
617     hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
618     b0addr+=4;\r
619     hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
620     b0addr+=4;\r
621     hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
622     \r
623     /* Enable the CRYP peripheral */\r
624     __HAL_CRYP_ENABLE(hcryp);\r
625     \r
626     /* Get tick */\r
627     tickstart = HAL_GetTick();\r
628 \r
629     while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
630     {\r
631       /* Check for the Timeout */\r
632       if(Timeout != HAL_MAX_DELAY)\r
633       {\r
634         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
635         {\r
636           /* Change state */\r
637           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
638           \r
639           /* Process Unlocked */\r
640           __HAL_UNLOCK(hcryp);\r
641         \r
642           return HAL_TIMEOUT;\r
643         }\r
644       }\r
645     }\r
646     /***************************** Header phase *******************************/\r
647     if(headersize != 0)\r
648     {\r
649       /* Select header phase */\r
650       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);\r
651       \r
652       /* Enable the CRYP peripheral */\r
653       __HAL_CRYP_ENABLE(hcryp);\r
654       \r
655       for(loopcounter = 0; (loopcounter < headersize); loopcounter+=16)\r
656       {\r
657         /* Get tick */\r
658         tickstart = HAL_GetTick();\r
659 \r
660         while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))\r
661         {\r
662           {\r
663             /* Check for the Timeout */\r
664             if(Timeout != HAL_MAX_DELAY)\r
665             {\r
666               if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
667               {\r
668                 /* Change state */\r
669                 hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
670                 \r
671                 /* Process Unlocked */\r
672                 __HAL_UNLOCK(hcryp);\r
673                 \r
674                 return HAL_TIMEOUT;\r
675               }\r
676             }\r
677           }\r
678         }\r
679         /* Write the header block in the IN FIFO */\r
680         hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
681         headeraddr+=4;\r
682         hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
683         headeraddr+=4;\r
684         hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
685         headeraddr+=4;\r
686         hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
687         headeraddr+=4;\r
688       }\r
689       \r
690       /* Get tick */\r
691       tickstart = HAL_GetTick();\r
692 \r
693       while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)\r
694       {\r
695         /* Check for the Timeout */\r
696         if(Timeout != HAL_MAX_DELAY)\r
697         {\r
698           if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
699           {\r
700             /* Change state */\r
701             hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
702             \r
703             /* Process Unlocked */\r
704             __HAL_UNLOCK(hcryp);\r
705             \r
706             return HAL_TIMEOUT;\r
707           }\r
708         }\r
709       }\r
710     }\r
711     /* Save formatted counter into the scratch buffer pScratch */\r
712     for(loopcounter = 0; (loopcounter < 16); loopcounter++)\r
713     {\r
714       hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];\r
715     }\r
716     /* Reset bit 0 */\r
717     hcryp->Init.pScratch[15] &= 0xfe;\r
718     \r
719     /* Select payload phase once the header phase is performed */\r
720     __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
721     \r
722     /* Flush FIFO */\r
723     __HAL_CRYP_FIFO_FLUSH(hcryp);\r
724     \r
725     /* Enable the CRYP peripheral */\r
726     __HAL_CRYP_ENABLE(hcryp);\r
727     \r
728     /* Set the phase */\r
729     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
730   }\r
731   \r
732   /* Write Plain Data and Get Cypher Data */\r
733   if(CRYPEx_GCMCCM_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK)\r
734   {\r
735     return HAL_TIMEOUT;\r
736   }\r
737   \r
738   /* Change the CRYP peripheral state */\r
739   hcryp->State = HAL_CRYP_STATE_READY;\r
740   \r
741   /* Process Unlocked */\r
742   __HAL_UNLOCK(hcryp);\r
743   \r
744   /* Return function status */\r
745   return HAL_OK;\r
746 }\r
747 \r
748 /**\r
749   * @brief  Initializes the CRYP peripheral in AES GCM encryption mode then \r
750   *         encrypt pPlainData. The cypher data are available in pCypherData.\r
751   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
752   *         the configuration information for CRYP module\r
753   * @param  pPlainData: Pointer to the plaintext buffer\r
754   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
755   * @param  pCypherData: Pointer to the cyphertext buffer\r
756   * @param  Timeout: Timeout duration\r
757   * @retval HAL status\r
758   */\r
759 HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)\r
760 {\r
761   uint32_t tickstart = 0;\r
762   \r
763   /* Process Locked */\r
764   __HAL_LOCK(hcryp);\r
765   \r
766   /* Change the CRYP peripheral state */\r
767   hcryp->State = HAL_CRYP_STATE_BUSY;\r
768   \r
769   /* Check if initialization phase has already been performed */\r
770   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
771   {\r
772     /* Set the key */\r
773     CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
774     \r
775     /* Set the CRYP peripheral in AES GCM mode */\r
776     __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT);\r
777     \r
778     /* Set the Initialization Vector */\r
779     CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);\r
780     \r
781     /* Flush FIFO */\r
782     __HAL_CRYP_FIFO_FLUSH(hcryp);\r
783     \r
784     /* Enable the CRYP peripheral */\r
785     __HAL_CRYP_ENABLE(hcryp);\r
786     \r
787     /* Get tick */\r
788     tickstart = HAL_GetTick();\r
789 \r
790     while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
791     {\r
792       /* Check for the Timeout */\r
793       if(Timeout != HAL_MAX_DELAY)\r
794       {\r
795         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
796         {\r
797           /* Change state */\r
798           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
799           \r
800           /* Process Unlocked */\r
801           __HAL_UNLOCK(hcryp);\r
802           \r
803           return HAL_TIMEOUT;\r
804         }\r
805       }\r
806     }\r
807     \r
808     /* Set the header phase */\r
809     if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, Timeout) != HAL_OK)\r
810     {\r
811       return HAL_TIMEOUT;\r
812     }\r
813     \r
814     /* Disable the CRYP peripheral */\r
815     __HAL_CRYP_DISABLE(hcryp);\r
816     \r
817     /* Select payload phase once the header phase is performed */\r
818     __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
819     \r
820     /* Flush FIFO */\r
821     __HAL_CRYP_FIFO_FLUSH(hcryp);\r
822     \r
823     /* Enable the CRYP peripheral */\r
824     __HAL_CRYP_ENABLE(hcryp);\r
825     \r
826     /* Set the phase */\r
827     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
828   }\r
829   \r
830   /* Write Plain Data and Get Cypher Data */\r
831   if(CRYPEx_GCMCCM_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)\r
832   {\r
833     return HAL_TIMEOUT;\r
834   }\r
835   \r
836   /* Change the CRYP peripheral state */\r
837   hcryp->State = HAL_CRYP_STATE_READY;\r
838   \r
839   /* Process Unlocked */\r
840   __HAL_UNLOCK(hcryp);\r
841   \r
842   /* Return function status */\r
843   return HAL_OK;\r
844 }\r
845 \r
846 /**\r
847   * @brief  Initializes the CRYP peripheral in AES GCM decryption mode then\r
848   *         decrypted pCypherData. The cypher data are available in pPlainData.\r
849   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
850   *         the configuration information for CRYP module\r
851   * @param  pCypherData: Pointer to the cyphertext buffer\r
852   * @param  Size: Length of the cyphertext buffer, must be a multiple of 16\r
853   * @param  pPlainData: Pointer to the plaintext buffer \r
854   * @param  Timeout: Timeout duration\r
855   * @retval HAL status\r
856   */\r
857 HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)\r
858 {\r
859   uint32_t tickstart = 0;   \r
860   \r
861   /* Process Locked */\r
862   __HAL_LOCK(hcryp);\r
863   \r
864   /* Change the CRYP peripheral state */\r
865   hcryp->State = HAL_CRYP_STATE_BUSY;\r
866   \r
867   /* Check if initialization phase has already been performed */\r
868   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
869   {\r
870     /* Set the key */\r
871     CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
872     \r
873     /* Set the CRYP peripheral in AES GCM decryption mode */\r
874     __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT);\r
875     \r
876     /* Set the Initialization Vector */\r
877     CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);\r
878     \r
879     /* Flush FIFO */\r
880     __HAL_CRYP_FIFO_FLUSH(hcryp);\r
881     \r
882     /* Enable the CRYP peripheral */\r
883     __HAL_CRYP_ENABLE(hcryp);\r
884     \r
885     /* Get tick */\r
886     tickstart = HAL_GetTick();\r
887 \r
888     while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
889     {\r
890       /* Check for the Timeout */\r
891       if(Timeout != HAL_MAX_DELAY)\r
892       {\r
893         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
894         {\r
895           /* Change state */\r
896           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
897           \r
898           /* Process Unlocked */\r
899           __HAL_UNLOCK(hcryp);\r
900           \r
901           return HAL_TIMEOUT;\r
902         }\r
903       }\r
904     }\r
905     \r
906     /* Set the header phase */\r
907     if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, Timeout) != HAL_OK)\r
908     {\r
909       return HAL_TIMEOUT;\r
910     }\r
911     /* Disable the CRYP peripheral */\r
912     __HAL_CRYP_DISABLE(hcryp);\r
913     \r
914     /* Select payload phase once the header phase is performed */\r
915     __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
916     \r
917     /* Enable the CRYP peripheral */\r
918     __HAL_CRYP_ENABLE(hcryp);\r
919     \r
920     /* Set the phase */\r
921     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
922   }\r
923   \r
924   /* Write Plain Data and Get Cypher Data */\r
925   if(CRYPEx_GCMCCM_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)\r
926   {\r
927     return HAL_TIMEOUT;\r
928   }\r
929   \r
930   /* Change the CRYP peripheral state */\r
931   hcryp->State = HAL_CRYP_STATE_READY;\r
932   \r
933   /* Process Unlocked */\r
934   __HAL_UNLOCK(hcryp);\r
935   \r
936   /* Return function status */\r
937   return HAL_OK;\r
938 }\r
939 \r
940 /**\r
941   * @brief  Computes the authentication TAG.\r
942   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
943   *         the configuration information for CRYP module\r
944   * @param  Size: Total length of the plain/cyphertext buffer\r
945   * @param  AuthTag: Pointer to the authentication buffer\r
946   * @param  Timeout: Timeout duration\r
947   * @retval HAL status\r
948   */\r
949 HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Finish(CRYP_HandleTypeDef *hcryp, uint32_t Size, uint8_t *AuthTag, uint32_t Timeout)\r
950 {\r
951   uint32_t tickstart = 0;   \r
952   uint64_t headerlength = hcryp->Init.HeaderSize * 8; /* Header length in bits */\r
953   uint64_t inputlength = Size * 8; /* input length in bits */\r
954   uint32_t tagaddr = (uint32_t)AuthTag;\r
955   \r
956   /* Process Locked */\r
957   __HAL_LOCK(hcryp);\r
958   \r
959   /* Change the CRYP peripheral state */\r
960   hcryp->State = HAL_CRYP_STATE_BUSY;\r
961   \r
962   /* Check if initialization phase has already been performed */\r
963   if(hcryp->Phase == HAL_CRYP_PHASE_PROCESS)\r
964   {\r
965     /* Change the CRYP phase */\r
966     hcryp->Phase = HAL_CRYP_PHASE_FINAL;\r
967     \r
968     /* Disable CRYP to start the final phase */\r
969     __HAL_CRYP_DISABLE(hcryp);\r
970     \r
971     /* Select final phase */\r
972     __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_FINAL);\r
973     \r
974     /* Enable the CRYP peripheral */\r
975     __HAL_CRYP_ENABLE(hcryp);\r
976     \r
977     /* Write the number of bits in header (64 bits) followed by the number of bits\r
978        in the payload */\r
979     if(hcryp->Init.DataType == CRYP_DATATYPE_1B)\r
980     {\r
981       hcryp->Instance->DR = __RBIT(headerlength >> 32);\r
982       hcryp->Instance->DR = __RBIT(headerlength);\r
983       hcryp->Instance->DR = __RBIT(inputlength >> 32);\r
984       hcryp->Instance->DR = __RBIT(inputlength);\r
985     }\r
986     else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)\r
987     {\r
988       hcryp->Instance->DR = __REV(headerlength >> 32);\r
989       hcryp->Instance->DR = __REV(headerlength);\r
990       hcryp->Instance->DR = __REV(inputlength >> 32);\r
991       hcryp->Instance->DR = __REV(inputlength);\r
992     }\r
993     else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)\r
994     {\r
995       hcryp->Instance->DR = __ROR((uint32_t)(headerlength >> 32), 16);\r
996       hcryp->Instance->DR = __ROR((uint32_t)headerlength, 16);\r
997       hcryp->Instance->DR = __ROR((uint32_t)(inputlength >> 32), 16);\r
998       hcryp->Instance->DR = __ROR((uint32_t)inputlength, 16);\r
999     }\r
1000     else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)\r
1001     {\r
1002       hcryp->Instance->DR = (uint32_t)(headerlength >> 32);\r
1003       hcryp->Instance->DR = (uint32_t)(headerlength);\r
1004       hcryp->Instance->DR = (uint32_t)(inputlength >> 32);\r
1005       hcryp->Instance->DR = (uint32_t)(inputlength);\r
1006     }\r
1007     /* Get tick */\r
1008     tickstart = HAL_GetTick();\r
1009 \r
1010     while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))\r
1011     {\r
1012       /* Check for the Timeout */\r
1013       if(Timeout != HAL_MAX_DELAY)\r
1014       {\r
1015         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
1016         {\r
1017           /* Change state */\r
1018           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1019           \r
1020           /* Process Unlocked */\r
1021           __HAL_UNLOCK(hcryp);\r
1022         \r
1023           return HAL_TIMEOUT;\r
1024         }\r
1025       }\r
1026     }\r
1027     \r
1028     /* Read the Auth TAG in the IN FIFO */\r
1029     *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;\r
1030     tagaddr+=4;\r
1031     *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;\r
1032     tagaddr+=4;\r
1033     *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;\r
1034     tagaddr+=4;\r
1035     *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;\r
1036   }\r
1037   \r
1038   /* Change the CRYP peripheral state */\r
1039   hcryp->State = HAL_CRYP_STATE_READY;\r
1040   \r
1041   /* Process Unlocked */\r
1042   __HAL_UNLOCK(hcryp);\r
1043   \r
1044   /* Return function status */\r
1045   return HAL_OK;\r
1046 }\r
1047 \r
1048 /**\r
1049   * @brief  Computes the authentication TAG for AES CCM mode.\r
1050   * @note   This API is called after HAL_AES_CCM_Encrypt()/HAL_AES_CCM_Decrypt()   \r
1051   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1052   *         the configuration information for CRYP module\r
1053   * @param  AuthTag: Pointer to the authentication buffer\r
1054   * @param  Timeout: Timeout duration\r
1055   * @retval HAL status\r
1056   */\r
1057 HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Finish(CRYP_HandleTypeDef *hcryp, uint8_t *AuthTag, uint32_t Timeout)\r
1058 {\r
1059   uint32_t tickstart = 0;   \r
1060   uint32_t tagaddr = (uint32_t)AuthTag;\r
1061   uint32_t ctraddr = (uint32_t)hcryp->Init.pScratch;\r
1062   uint32_t temptag[4] = {0}; /* Temporary TAG (MAC) */\r
1063   uint32_t loopcounter;\r
1064   \r
1065   /* Process Locked */\r
1066   __HAL_LOCK(hcryp);\r
1067   \r
1068   /* Change the CRYP peripheral state */\r
1069   hcryp->State = HAL_CRYP_STATE_BUSY;\r
1070   \r
1071   /* Check if initialization phase has already been performed */\r
1072   if(hcryp->Phase == HAL_CRYP_PHASE_PROCESS)\r
1073   {\r
1074     /* Change the CRYP phase */\r
1075     hcryp->Phase = HAL_CRYP_PHASE_FINAL;\r
1076     \r
1077     /* Disable CRYP to start the final phase */\r
1078     __HAL_CRYP_DISABLE(hcryp);\r
1079     \r
1080     /* Select final phase */\r
1081     __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_FINAL);\r
1082     \r
1083     /* Enable the CRYP peripheral */\r
1084     __HAL_CRYP_ENABLE(hcryp);\r
1085     \r
1086     /* Write the counter block in the IN FIFO */\r
1087     hcryp->Instance->DR = *(uint32_t*)ctraddr;\r
1088     ctraddr+=4;\r
1089     hcryp->Instance->DR = *(uint32_t*)ctraddr;\r
1090     ctraddr+=4;\r
1091     hcryp->Instance->DR = *(uint32_t*)ctraddr;\r
1092     ctraddr+=4;\r
1093     hcryp->Instance->DR = *(uint32_t*)ctraddr;\r
1094     \r
1095     /* Get tick */\r
1096     tickstart = HAL_GetTick();\r
1097 \r
1098     while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))\r
1099     {\r
1100       /* Check for the Timeout */\r
1101       if(Timeout != HAL_MAX_DELAY)\r
1102       {\r
1103         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
1104         {\r
1105           /* Change state */\r
1106           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1107           \r
1108           /* Process Unlocked */\r
1109           __HAL_UNLOCK(hcryp);\r
1110           \r
1111           return HAL_TIMEOUT;\r
1112         }\r
1113       }\r
1114     }\r
1115     \r
1116     /* Read the Auth TAG in the IN FIFO */\r
1117     temptag[0] = hcryp->Instance->DOUT;\r
1118     temptag[1] = hcryp->Instance->DOUT;\r
1119     temptag[2] = hcryp->Instance->DOUT;\r
1120     temptag[3] = hcryp->Instance->DOUT;\r
1121   }\r
1122   \r
1123   /* Copy temporary authentication TAG in user TAG buffer */\r
1124   for(loopcounter = 0; loopcounter < hcryp->Init.TagSize ; loopcounter++)\r
1125   {\r
1126     /* Set the authentication TAG buffer */\r
1127     *((uint8_t*)tagaddr+loopcounter) = *((uint8_t*)temptag+loopcounter);\r
1128   }\r
1129   \r
1130   /* Change the CRYP peripheral state */\r
1131   hcryp->State = HAL_CRYP_STATE_READY;\r
1132   \r
1133   /* Process Unlocked */\r
1134   __HAL_UNLOCK(hcryp);\r
1135   \r
1136   /* Return function status */\r
1137   return HAL_OK;\r
1138 }\r
1139 \r
1140 /**\r
1141   * @brief  Initializes the CRYP peripheral in AES CCM decryption mode then\r
1142   *         decrypted pCypherData. The cypher data are available in pPlainData.\r
1143   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1144   *         the configuration information for CRYP module\r
1145   * @param  pPlainData: Pointer to the plaintext buffer\r
1146   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
1147   * @param  pCypherData: Pointer to the cyphertext buffer\r
1148   * @param  Timeout: Timeout duration\r
1149   * @retval HAL status\r
1150   */\r
1151 HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)\r
1152 {\r
1153   uint32_t tickstart = 0;   \r
1154   uint32_t headersize = hcryp->Init.HeaderSize;\r
1155   uint32_t headeraddr = (uint32_t)hcryp->Init.Header;\r
1156   uint32_t loopcounter = 0;\r
1157   uint32_t bufferidx = 0;\r
1158   uint8_t blockb0[16] = {0};/* Block B0 */\r
1159   uint8_t ctr[16] = {0}; /* Counter */\r
1160   uint32_t b0addr = (uint32_t)blockb0;\r
1161   \r
1162   /* Process Locked */\r
1163   __HAL_LOCK(hcryp);\r
1164   \r
1165   /* Change the CRYP peripheral state */\r
1166   hcryp->State = HAL_CRYP_STATE_BUSY;\r
1167   \r
1168   /* Check if initialization phase has already been performed */\r
1169   if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1170   {\r
1171     /************************ Formatting the header block *********************/\r
1172     if(headersize != 0)\r
1173     {\r
1174       /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */\r
1175       if(headersize < 65280)\r
1176       {\r
1177         hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);\r
1178         hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);\r
1179         headersize += 2;\r
1180       }\r
1181       else\r
1182       {\r
1183         /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */\r
1184         hcryp->Init.pScratch[bufferidx++] = 0xFF;\r
1185         hcryp->Init.pScratch[bufferidx++] = 0xFE;\r
1186         hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000;\r
1187         hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000;\r
1188         hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00;\r
1189         hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ff;\r
1190         headersize += 6;\r
1191       }\r
1192       /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */\r
1193       for(loopcounter = 0; loopcounter < headersize; loopcounter++)\r
1194       {\r
1195         hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];\r
1196       }\r
1197       /* Check if the header size is modulo 16 */\r
1198       if ((headersize % 16) != 0)\r
1199       {\r
1200         /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */\r
1201         for(loopcounter = headersize; loopcounter <= ((headersize/16) + 1) * 16; loopcounter++)\r
1202         {\r
1203           hcryp->Init.pScratch[loopcounter] = 0;\r
1204         }\r
1205         /* Set the header size to modulo 16 */\r
1206         headersize = ((headersize/16) + 1) * 16;\r
1207       }\r
1208       /* Set the pointer headeraddr to hcryp->Init.pScratch */\r
1209       headeraddr = (uint32_t)hcryp->Init.pScratch;\r
1210     }\r
1211     /*********************** Formatting the block B0 **************************/\r
1212     if(headersize != 0)\r
1213     {\r
1214       blockb0[0] = 0x40;\r
1215     }\r
1216     /* Flags byte */\r
1217     /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07) */\r
1218     blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07 ) << 3);\r
1219     blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);\r
1220     \r
1221     for (loopcounter = 0; loopcounter < hcryp->Init.IVSize; loopcounter++)\r
1222     {\r
1223       blockb0[loopcounter+1] = hcryp->Init.pInitVect[loopcounter];\r
1224     }\r
1225     for ( ; loopcounter < 13; loopcounter++)\r
1226     {\r
1227       blockb0[loopcounter+1] = 0;\r
1228     }\r
1229     \r
1230     blockb0[14] = (Size >> 8);\r
1231     blockb0[15] = (Size & 0xFF);\r
1232     \r
1233     /************************* Formatting the initial counter *****************/\r
1234     /* Byte 0:\r
1235        Bits 7 and 6 are reserved and shall be set to 0\r
1236        Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter \r
1237        blocks are distinct from B0\r
1238        Bits 0, 1, and 2 contain the same encoding of q as in B0\r
1239     */\r
1240     ctr[0] = blockb0[0] & 0x07;\r
1241     /* byte 1 to NonceSize is the IV (Nonce) */\r
1242     for(loopcounter = 1; loopcounter < hcryp->Init.IVSize + 1; loopcounter++)\r
1243     {\r
1244       ctr[loopcounter] = blockb0[loopcounter];\r
1245     }\r
1246     /* Set the LSB to 1 */\r
1247     ctr[15] |= 0x01;\r
1248     \r
1249     /* Set the key */\r
1250     CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1251     \r
1252     /* Set the CRYP peripheral in AES CCM mode */\r
1253     __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT);\r
1254     \r
1255     /* Set the Initialization Vector */\r
1256     CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);\r
1257     \r
1258     /* Select init phase */\r
1259     __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);\r
1260     \r
1261     b0addr = (uint32_t)blockb0;\r
1262     /* Write the blockb0 block in the IN FIFO */\r
1263     hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
1264     b0addr+=4;\r
1265     hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
1266     b0addr+=4;\r
1267     hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
1268     b0addr+=4;\r
1269     hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
1270     \r
1271     /* Enable the CRYP peripheral */\r
1272     __HAL_CRYP_ENABLE(hcryp);\r
1273     \r
1274     /* Get tick */\r
1275     tickstart = HAL_GetTick();\r
1276  \r
1277     while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
1278     {\r
1279       /* Check for the Timeout */\r
1280       if(Timeout != HAL_MAX_DELAY)\r
1281       {\r
1282         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
1283         {\r
1284           /* Change state */\r
1285           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1286           \r
1287           /* Process Unlocked */\r
1288           __HAL_UNLOCK(hcryp);\r
1289         \r
1290           return HAL_TIMEOUT;\r
1291         }\r
1292       }\r
1293     }\r
1294     /***************************** Header phase *******************************/\r
1295     if(headersize != 0)\r
1296     {\r
1297       /* Select header phase */\r
1298       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);\r
1299       \r
1300       /* Enable Crypto processor */\r
1301       __HAL_CRYP_ENABLE(hcryp);\r
1302       \r
1303       for(loopcounter = 0; (loopcounter < headersize); loopcounter+=16)\r
1304       {\r
1305         /* Get tick */\r
1306         tickstart = HAL_GetTick();\r
1307 \r
1308         while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))\r
1309         {\r
1310           /* Check for the Timeout */\r
1311           if(Timeout != HAL_MAX_DELAY)\r
1312           {\r
1313             if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
1314             {\r
1315               /* Change state */\r
1316               hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1317               \r
1318               /* Process Unlocked */\r
1319               __HAL_UNLOCK(hcryp);\r
1320               \r
1321               return HAL_TIMEOUT;\r
1322             }\r
1323           }\r
1324         }\r
1325         /* Write the header block in the IN FIFO */\r
1326         hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
1327         headeraddr+=4;\r
1328         hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
1329         headeraddr+=4;\r
1330         hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
1331         headeraddr+=4;\r
1332         hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
1333         headeraddr+=4;\r
1334       }\r
1335       \r
1336       /* Get tick */\r
1337       tickstart = HAL_GetTick();\r
1338 \r
1339       while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)\r
1340       {\r
1341       /* Check for the Timeout */\r
1342         if(Timeout != HAL_MAX_DELAY)\r
1343         {\r
1344           if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
1345           {\r
1346             /* Change state */\r
1347             hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1348             \r
1349             /* Process Unlocked */\r
1350             __HAL_UNLOCK(hcryp);\r
1351             \r
1352             return HAL_TIMEOUT;\r
1353           }\r
1354         }\r
1355       }\r
1356     }\r
1357     /* Save formatted counter into the scratch buffer pScratch */\r
1358     for(loopcounter = 0; (loopcounter < 16); loopcounter++)\r
1359     {\r
1360       hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];\r
1361     }\r
1362     /* Reset bit 0 */\r
1363     hcryp->Init.pScratch[15] &= 0xfe;\r
1364     /* Select payload phase once the header phase is performed */\r
1365     __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
1366     \r
1367     /* Flush FIFO */\r
1368     __HAL_CRYP_FIFO_FLUSH(hcryp);\r
1369     \r
1370     /* Enable the CRYP peripheral */\r
1371     __HAL_CRYP_ENABLE(hcryp);\r
1372     \r
1373     /* Set the phase */\r
1374     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1375   }\r
1376   \r
1377   /* Write Plain Data and Get Cypher Data */\r
1378   if(CRYPEx_GCMCCM_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)\r
1379   {\r
1380     return HAL_TIMEOUT;\r
1381   }\r
1382   \r
1383   /* Change the CRYP peripheral state */\r
1384   hcryp->State = HAL_CRYP_STATE_READY;\r
1385   \r
1386   /* Process Unlocked */\r
1387   __HAL_UNLOCK(hcryp);\r
1388   \r
1389   /* Return function status */\r
1390   return HAL_OK;\r
1391 }\r
1392 \r
1393 /**\r
1394   * @brief  Initializes the CRYP peripheral in AES GCM encryption mode using IT.\r
1395   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1396   *         the configuration information for CRYP module\r
1397   * @param  pPlainData: Pointer to the plaintext buffer\r
1398   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
1399   * @param  pCypherData: Pointer to the cyphertext buffer\r
1400   * @retval HAL status\r
1401   */\r
1402 HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
1403 {\r
1404   uint32_t tickstart = 0;   \r
1405   uint32_t inputaddr;\r
1406   uint32_t outputaddr;\r
1407   \r
1408   if(hcryp->State == HAL_CRYP_STATE_READY)\r
1409   {\r
1410     /* Process Locked */\r
1411     __HAL_LOCK(hcryp);\r
1412     \r
1413     /* Get the buffer addresses and sizes */    \r
1414     hcryp->CrypInCount = Size;\r
1415     hcryp->pCrypInBuffPtr = pPlainData;\r
1416     hcryp->pCrypOutBuffPtr = pCypherData;\r
1417     hcryp->CrypOutCount = Size;\r
1418     \r
1419     /* Change the CRYP peripheral state */\r
1420     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1421     \r
1422     /* Check if initialization phase has already been performed */\r
1423     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1424     {\r
1425       /* Set the key */\r
1426       CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1427       \r
1428       /* Set the CRYP peripheral in AES GCM mode */\r
1429       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT);\r
1430       \r
1431       /* Set the Initialization Vector */\r
1432       CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);\r
1433       \r
1434       /* Flush FIFO */\r
1435       __HAL_CRYP_FIFO_FLUSH(hcryp);\r
1436       \r
1437       /* Enable CRYP to start the init phase */\r
1438       __HAL_CRYP_ENABLE(hcryp);\r
1439       \r
1440      /* Get tick */\r
1441      tickstart = HAL_GetTick();\r
1442 \r
1443       while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
1444       {\r
1445         /* Check for the Timeout */\r
1446         \r
1447         if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
1448         {\r
1449           /* Change state */\r
1450           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1451           \r
1452           /* Process Unlocked */\r
1453           __HAL_UNLOCK(hcryp);\r
1454           \r
1455           return HAL_TIMEOUT;\r
1456           \r
1457         }\r
1458       }\r
1459       \r
1460       /* Set the header phase */\r
1461       if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1) != HAL_OK)\r
1462       {\r
1463         return HAL_TIMEOUT;\r
1464       }\r
1465       /* Disable the CRYP peripheral */\r
1466       __HAL_CRYP_DISABLE(hcryp);\r
1467       \r
1468       /* Select payload phase once the header phase is performed */\r
1469       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
1470       \r
1471       /* Flush FIFO */\r
1472       __HAL_CRYP_FIFO_FLUSH(hcryp);\r
1473       \r
1474       /* Set the phase */\r
1475       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1476     }\r
1477     \r
1478     if(Size != 0)\r
1479     {\r
1480       /* Enable Interrupts */\r
1481       __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);\r
1482       /* Enable the CRYP peripheral */\r
1483       __HAL_CRYP_ENABLE(hcryp);\r
1484     }\r
1485     else\r
1486     {\r
1487       /* Process Locked */\r
1488       __HAL_UNLOCK(hcryp);\r
1489       /* Change the CRYP state and phase */\r
1490       hcryp->State = HAL_CRYP_STATE_READY;\r
1491     }\r
1492     /* Return function status */\r
1493     return HAL_OK;\r
1494   }\r
1495   else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))\r
1496   {\r
1497     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
1498     /* Write the Input block in the IN FIFO */\r
1499     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
1500     inputaddr+=4;\r
1501     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
1502     inputaddr+=4;\r
1503     hcryp->Instance->DR  = *(uint32_t*)(inputaddr);\r
1504     inputaddr+=4;\r
1505     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
1506     hcryp->pCrypInBuffPtr += 16;\r
1507     hcryp->CrypInCount -= 16;\r
1508     if(hcryp->CrypInCount == 0)\r
1509     {\r
1510       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);\r
1511       /* Call the Input data transfer complete callback */\r
1512       HAL_CRYP_InCpltCallback(hcryp);\r
1513     }\r
1514   }\r
1515   else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))\r
1516   {\r
1517     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
1518     /* Read the Output block from the Output FIFO */\r
1519     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1520     outputaddr+=4;\r
1521     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1522     outputaddr+=4;\r
1523     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1524     outputaddr+=4;\r
1525     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1526     hcryp->pCrypOutBuffPtr += 16;\r
1527     hcryp->CrypOutCount -= 16;\r
1528     if(hcryp->CrypOutCount == 0)\r
1529     {\r
1530       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);\r
1531       /* Process Unlocked */\r
1532       __HAL_UNLOCK(hcryp);\r
1533       /* Change the CRYP peripheral state */\r
1534       hcryp->State = HAL_CRYP_STATE_READY;\r
1535       /* Call Input transfer complete callback */\r
1536       HAL_CRYP_OutCpltCallback(hcryp);\r
1537     }\r
1538   }\r
1539   \r
1540   /* Return function status */\r
1541   return HAL_OK;\r
1542 }\r
1543 \r
1544 /**\r
1545   * @brief  Initializes the CRYP peripheral in AES CCM encryption mode using interrupt.\r
1546   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1547   *         the configuration information for CRYP module\r
1548   * @param  pPlainData: Pointer to the plaintext buffer\r
1549   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
1550   * @param  pCypherData: Pointer to the cyphertext buffer\r
1551   * @retval HAL status\r
1552   */\r
1553 HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
1554 {\r
1555   uint32_t tickstart = 0;   \r
1556   uint32_t inputaddr;\r
1557   uint32_t outputaddr;\r
1558   \r
1559   uint32_t headersize = hcryp->Init.HeaderSize;\r
1560   uint32_t headeraddr = (uint32_t)hcryp->Init.Header;\r
1561   uint32_t loopcounter = 0;\r
1562   uint32_t bufferidx = 0;\r
1563   uint8_t blockb0[16] = {0};/* Block B0 */\r
1564   uint8_t ctr[16] = {0}; /* Counter */\r
1565   uint32_t b0addr = (uint32_t)blockb0;\r
1566   \r
1567   if(hcryp->State == HAL_CRYP_STATE_READY)\r
1568   {\r
1569     /* Process Locked */\r
1570     __HAL_LOCK(hcryp);\r
1571     \r
1572     hcryp->CrypInCount = Size;\r
1573     hcryp->pCrypInBuffPtr = pPlainData;\r
1574     hcryp->pCrypOutBuffPtr = pCypherData;\r
1575     hcryp->CrypOutCount = Size;\r
1576     \r
1577     /* Change the CRYP peripheral state */\r
1578     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1579     \r
1580     /* Check if initialization phase has already been performed */\r
1581     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1582     {    \r
1583       /************************ Formatting the header block *******************/\r
1584       if(headersize != 0)\r
1585       {\r
1586         /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */\r
1587         if(headersize < 65280)\r
1588         {\r
1589           hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);\r
1590           hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);\r
1591           headersize += 2;\r
1592         }\r
1593         else\r
1594         {\r
1595           /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */\r
1596           hcryp->Init.pScratch[bufferidx++] = 0xFF;\r
1597           hcryp->Init.pScratch[bufferidx++] = 0xFE;\r
1598           hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000;\r
1599           hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000;\r
1600           hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00;\r
1601           hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ff;\r
1602           headersize += 6;\r
1603         }\r
1604         /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */\r
1605         for(loopcounter = 0; loopcounter < headersize; loopcounter++)\r
1606         {\r
1607           hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];\r
1608         }\r
1609         /* Check if the header size is modulo 16 */\r
1610         if ((headersize % 16) != 0)\r
1611         {\r
1612           /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */\r
1613           for(loopcounter = headersize; loopcounter <= ((headersize/16) + 1) * 16; loopcounter++)\r
1614           {\r
1615             hcryp->Init.pScratch[loopcounter] = 0;\r
1616           }\r
1617           /* Set the header size to modulo 16 */\r
1618           headersize = ((headersize/16) + 1) * 16;\r
1619         }\r
1620         /* Set the pointer headeraddr to hcryp->Init.pScratch */\r
1621         headeraddr = (uint32_t)hcryp->Init.pScratch;\r
1622       }\r
1623       /*********************** Formatting the block B0 ************************/\r
1624       if(headersize != 0)\r
1625       {\r
1626         blockb0[0] = 0x40;\r
1627       }\r
1628       /* Flags byte */\r
1629       /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07) */\r
1630       blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07 ) << 3);\r
1631       blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);\r
1632       \r
1633       for (loopcounter = 0; loopcounter < hcryp->Init.IVSize; loopcounter++)\r
1634       {\r
1635         blockb0[loopcounter+1] = hcryp->Init.pInitVect[loopcounter];\r
1636       }\r
1637       for ( ; loopcounter < 13; loopcounter++)\r
1638       {\r
1639         blockb0[loopcounter+1] = 0;\r
1640       }\r
1641       \r
1642       blockb0[14] = (Size >> 8);\r
1643       blockb0[15] = (Size & 0xFF);\r
1644       \r
1645       /************************* Formatting the initial counter ***************/\r
1646       /* Byte 0:\r
1647          Bits 7 and 6 are reserved and shall be set to 0\r
1648          Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter \r
1649          blocks are distinct from B0\r
1650          Bits 0, 1, and 2 contain the same encoding of q as in B0\r
1651       */\r
1652       ctr[0] = blockb0[0] & 0x07;\r
1653       /* byte 1 to NonceSize is the IV (Nonce) */\r
1654       for(loopcounter = 1; loopcounter < hcryp->Init.IVSize + 1; loopcounter++)\r
1655       {\r
1656         ctr[loopcounter] = blockb0[loopcounter];\r
1657       }\r
1658       /* Set the LSB to 1 */\r
1659       ctr[15] |= 0x01;\r
1660       \r
1661       /* Set the key */\r
1662       CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1663       \r
1664       /* Set the CRYP peripheral in AES CCM mode */\r
1665       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT);\r
1666       \r
1667       /* Set the Initialization Vector */\r
1668       CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);\r
1669       \r
1670       /* Select init phase */\r
1671       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);\r
1672       \r
1673       b0addr = (uint32_t)blockb0;\r
1674       /* Write the blockb0 block in the IN FIFO */\r
1675       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
1676       b0addr+=4;\r
1677       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
1678       b0addr+=4;\r
1679       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
1680       b0addr+=4;\r
1681       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
1682       \r
1683       /* Enable the CRYP peripheral */\r
1684       __HAL_CRYP_ENABLE(hcryp);\r
1685       \r
1686      /* Get tick */\r
1687      tickstart = HAL_GetTick();\r
1688 \r
1689       while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
1690       {\r
1691         /* Check for the Timeout */\r
1692         if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
1693         {\r
1694           /* Change state */\r
1695           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1696           \r
1697           /* Process Unlocked */\r
1698           __HAL_UNLOCK(hcryp);\r
1699           \r
1700           return HAL_TIMEOUT;\r
1701         }\r
1702       }\r
1703       /***************************** Header phase *****************************/\r
1704       if(headersize != 0)\r
1705       {\r
1706         /* Select header phase */\r
1707         __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);\r
1708         \r
1709         /* Enable Crypto processor */\r
1710         __HAL_CRYP_ENABLE(hcryp);\r
1711         \r
1712         for(loopcounter = 0; (loopcounter < headersize); loopcounter+=16)\r
1713         {\r
1714          /* Get tick */\r
1715          tickstart = HAL_GetTick();\r
1716 \r
1717           while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))\r
1718           {\r
1719             /* Check for the Timeout */\r
1720             if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
1721             {\r
1722               /* Change state */\r
1723               hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1724               \r
1725               /* Process Unlocked */\r
1726               __HAL_UNLOCK(hcryp);\r
1727               \r
1728               return HAL_TIMEOUT;\r
1729             }\r
1730           }\r
1731           /* Write the header block in the IN FIFO */\r
1732           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
1733           headeraddr+=4;\r
1734           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
1735           headeraddr+=4;\r
1736           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
1737           headeraddr+=4;\r
1738           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
1739           headeraddr+=4;\r
1740         }\r
1741 \r
1742         /* Get tick */\r
1743         tickstart = HAL_GetTick();\r
1744 \r
1745         while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)\r
1746         {\r
1747           /* Check for the Timeout */\r
1748           if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
1749           {\r
1750             /* Change state */\r
1751             hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1752             \r
1753             /* Process Unlocked */\r
1754             __HAL_UNLOCK(hcryp);\r
1755             \r
1756             return HAL_TIMEOUT;\r
1757           }\r
1758         }\r
1759       }\r
1760       /* Save formatted counter into the scratch buffer pScratch */\r
1761       for(loopcounter = 0; (loopcounter < 16); loopcounter++)\r
1762       {\r
1763         hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];\r
1764       }\r
1765       /* Reset bit 0 */\r
1766       hcryp->Init.pScratch[15] &= 0xfe;\r
1767       \r
1768       /* Select payload phase once the header phase is performed */\r
1769       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
1770       \r
1771       /* Flush FIFO */\r
1772       __HAL_CRYP_FIFO_FLUSH(hcryp);\r
1773       \r
1774       /* Set the phase */\r
1775       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1776     }\r
1777     \r
1778     if(Size != 0)\r
1779     {\r
1780       /* Enable Interrupts */\r
1781       __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);\r
1782       /* Enable the CRYP peripheral */\r
1783       __HAL_CRYP_ENABLE(hcryp);\r
1784     }\r
1785     else\r
1786     {\r
1787       /* Change the CRYP state and phase */\r
1788       hcryp->State = HAL_CRYP_STATE_READY;\r
1789     }\r
1790     \r
1791     /* Return function status */\r
1792     return HAL_OK;\r
1793   }\r
1794   else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))\r
1795   {\r
1796     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
1797     /* Write the Input block in the IN FIFO */\r
1798     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
1799     inputaddr+=4;\r
1800     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
1801     inputaddr+=4;\r
1802     hcryp->Instance->DR  = *(uint32_t*)(inputaddr);\r
1803     inputaddr+=4;\r
1804     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
1805     hcryp->pCrypInBuffPtr += 16;\r
1806     hcryp->CrypInCount -= 16;\r
1807     if(hcryp->CrypInCount == 0)\r
1808     {\r
1809       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);\r
1810       /* Call Input transfer complete callback */\r
1811       HAL_CRYP_InCpltCallback(hcryp);\r
1812     }\r
1813   }\r
1814   else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))\r
1815   {\r
1816     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
1817     /* Read the Output block from the Output FIFO */\r
1818     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1819     outputaddr+=4;\r
1820     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1821     outputaddr+=4;\r
1822     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1823     outputaddr+=4;\r
1824     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1825     hcryp->pCrypOutBuffPtr += 16;\r
1826     hcryp->CrypOutCount -= 16;\r
1827     if(hcryp->CrypOutCount == 0)\r
1828     {\r
1829       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);\r
1830       /* Process Unlocked */\r
1831       __HAL_UNLOCK(hcryp);\r
1832       /* Change the CRYP peripheral state */\r
1833       hcryp->State = HAL_CRYP_STATE_READY;\r
1834       /* Call Input transfer complete callback */\r
1835       HAL_CRYP_OutCpltCallback(hcryp);\r
1836     }\r
1837   }\r
1838   \r
1839   /* Return function status */\r
1840   return HAL_OK;\r
1841 }\r
1842 \r
1843 /**\r
1844   * @brief  Initializes the CRYP peripheral in AES GCM decryption mode using IT.\r
1845   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1846   *         the configuration information for CRYP module\r
1847   * @param  pCypherData: Pointer to the cyphertext buffer\r
1848   * @param  Size: Length of the cyphertext buffer, must be a multiple of 16\r
1849   * @param  pPlainData: Pointer to the plaintext buffer\r
1850   * @retval HAL status\r
1851   */\r
1852 HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
1853 {\r
1854   uint32_t tickstart = 0;   \r
1855   uint32_t inputaddr;\r
1856   uint32_t outputaddr;\r
1857   \r
1858   if(hcryp->State == HAL_CRYP_STATE_READY)\r
1859   {\r
1860     /* Process Locked */\r
1861     __HAL_LOCK(hcryp);\r
1862     \r
1863     /* Get the buffer addresses and sizes */    \r
1864     hcryp->CrypInCount = Size;\r
1865     hcryp->pCrypInBuffPtr = pCypherData;\r
1866     hcryp->pCrypOutBuffPtr = pPlainData;\r
1867     hcryp->CrypOutCount = Size;\r
1868     \r
1869     /* Change the CRYP peripheral state */\r
1870     hcryp->State = HAL_CRYP_STATE_BUSY;\r
1871     \r
1872     /* Check if initialization phase has already been performed */\r
1873     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
1874     {\r
1875       /* Set the key */\r
1876       CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
1877       \r
1878       /* Set the CRYP peripheral in AES GCM decryption mode */\r
1879       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT);\r
1880       \r
1881       /* Set the Initialization Vector */\r
1882       CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);\r
1883       \r
1884       /* Flush FIFO */\r
1885       __HAL_CRYP_FIFO_FLUSH(hcryp);\r
1886       \r
1887       /* Enable CRYP to start the init phase */\r
1888       __HAL_CRYP_ENABLE(hcryp);\r
1889 \r
1890         /* Get tick */\r
1891         tickstart = HAL_GetTick();\r
1892 \r
1893       while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
1894       {\r
1895         /* Check for the Timeout */\r
1896         if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
1897         {\r
1898           /* Change state */\r
1899           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
1900           \r
1901           /* Process Unlocked */\r
1902           __HAL_UNLOCK(hcryp);\r
1903           \r
1904           return HAL_TIMEOUT;\r
1905         }\r
1906       }\r
1907       \r
1908       /* Set the header phase */\r
1909       if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1) != HAL_OK)\r
1910       {\r
1911         return HAL_TIMEOUT;\r
1912       }\r
1913       /* Disable the CRYP peripheral */\r
1914       __HAL_CRYP_DISABLE(hcryp);\r
1915       \r
1916       /* Select payload phase once the header phase is performed */\r
1917       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
1918       \r
1919       /* Set the phase */\r
1920       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
1921     }\r
1922     \r
1923     if(Size != 0)\r
1924     {\r
1925       /* Enable Interrupts */\r
1926       __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);\r
1927       /* Enable the CRYP peripheral */\r
1928       __HAL_CRYP_ENABLE(hcryp);\r
1929     }\r
1930     else\r
1931     {\r
1932       /* Process Locked */\r
1933       __HAL_UNLOCK(hcryp);\r
1934       /* Change the CRYP state and phase */\r
1935       hcryp->State = HAL_CRYP_STATE_READY;\r
1936     }\r
1937     \r
1938     /* Return function status */\r
1939     return HAL_OK;\r
1940   }\r
1941   else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))\r
1942   {\r
1943     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
1944     /* Write the Input block in the IN FIFO */\r
1945     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
1946     inputaddr+=4;\r
1947     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
1948     inputaddr+=4;\r
1949     hcryp->Instance->DR  = *(uint32_t*)(inputaddr);\r
1950     inputaddr+=4;\r
1951     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
1952     hcryp->pCrypInBuffPtr += 16;\r
1953     hcryp->CrypInCount -= 16;\r
1954     if(hcryp->CrypInCount == 0)\r
1955     {\r
1956       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);\r
1957       /* Call the Input data transfer complete callback */\r
1958       HAL_CRYP_InCpltCallback(hcryp);\r
1959     }\r
1960   }\r
1961   else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))\r
1962   {\r
1963     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
1964     /* Read the Output block from the Output FIFO */\r
1965     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1966     outputaddr+=4;\r
1967     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1968     outputaddr+=4;\r
1969     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1970     outputaddr+=4;\r
1971     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
1972     hcryp->pCrypOutBuffPtr += 16;\r
1973     hcryp->CrypOutCount -= 16;\r
1974     if(hcryp->CrypOutCount == 0)\r
1975     {\r
1976       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);\r
1977       /* Process Unlocked */\r
1978       __HAL_UNLOCK(hcryp);\r
1979       /* Change the CRYP peripheral state */\r
1980       hcryp->State = HAL_CRYP_STATE_READY;\r
1981       /* Call Input transfer complete callback */\r
1982       HAL_CRYP_OutCpltCallback(hcryp);\r
1983     }\r
1984   }\r
1985   \r
1986   /* Return function status */\r
1987   return HAL_OK;\r
1988 }\r
1989 \r
1990 /**\r
1991   * @brief  Initializes the CRYP peripheral in AES CCM decryption mode using interrupt\r
1992   *         then decrypted pCypherData. The cypher data are available in pPlainData.\r
1993   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
1994   *         the configuration information for CRYP module\r
1995   * @param  pCypherData: Pointer to the cyphertext buffer \r
1996   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
1997   * @param  pPlainData: Pointer to the plaintext buffer  \r
1998   * @retval HAL status\r
1999   */\r
2000 HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
2001 {\r
2002   uint32_t inputaddr;\r
2003   uint32_t outputaddr;\r
2004   uint32_t tickstart = 0;\r
2005   uint32_t headersize = hcryp->Init.HeaderSize;\r
2006   uint32_t headeraddr = (uint32_t)hcryp->Init.Header;\r
2007   uint32_t loopcounter = 0;\r
2008   uint32_t bufferidx = 0;\r
2009   uint8_t blockb0[16] = {0};/* Block B0 */\r
2010   uint8_t ctr[16] = {0}; /* Counter */\r
2011   uint32_t b0addr = (uint32_t)blockb0;\r
2012   \r
2013   if(hcryp->State == HAL_CRYP_STATE_READY)\r
2014   {\r
2015     /* Process Locked */\r
2016     __HAL_LOCK(hcryp);\r
2017     \r
2018     hcryp->CrypInCount = Size;\r
2019     hcryp->pCrypInBuffPtr = pCypherData;\r
2020     hcryp->pCrypOutBuffPtr = pPlainData;\r
2021     hcryp->CrypOutCount = Size;\r
2022     \r
2023     /* Change the CRYP peripheral state */\r
2024     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2025     \r
2026     /* Check if initialization phase has already been performed */\r
2027     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
2028     {\r
2029       /************************ Formatting the header block *******************/\r
2030       if(headersize != 0)\r
2031       {\r
2032         /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */\r
2033         if(headersize < 65280)\r
2034         {\r
2035           hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);\r
2036           hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);\r
2037           headersize += 2;\r
2038         }\r
2039         else\r
2040         {\r
2041           /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */\r
2042           hcryp->Init.pScratch[bufferidx++] = 0xFF;\r
2043           hcryp->Init.pScratch[bufferidx++] = 0xFE;\r
2044           hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000;\r
2045           hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000;\r
2046           hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00;\r
2047           hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ff;\r
2048           headersize += 6;\r
2049         }\r
2050         /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */\r
2051         for(loopcounter = 0; loopcounter < headersize; loopcounter++)\r
2052         {\r
2053           hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];\r
2054         }\r
2055         /* Check if the header size is modulo 16 */\r
2056         if ((headersize % 16) != 0)\r
2057         {\r
2058           /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */\r
2059           for(loopcounter = headersize; loopcounter <= ((headersize/16) + 1) * 16; loopcounter++)\r
2060           {\r
2061             hcryp->Init.pScratch[loopcounter] = 0;\r
2062           }\r
2063           /* Set the header size to modulo 16 */\r
2064           headersize = ((headersize/16) + 1) * 16;\r
2065         }\r
2066         /* Set the pointer headeraddr to hcryp->Init.pScratch */\r
2067         headeraddr = (uint32_t)hcryp->Init.pScratch;\r
2068       }\r
2069       /*********************** Formatting the block B0 ************************/\r
2070       if(headersize != 0)\r
2071       {\r
2072         blockb0[0] = 0x40;\r
2073       }\r
2074       /* Flags byte */\r
2075       /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07) */\r
2076       blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07 ) << 3);\r
2077       blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);\r
2078       \r
2079       for (loopcounter = 0; loopcounter < hcryp->Init.IVSize; loopcounter++)\r
2080       {\r
2081         blockb0[loopcounter+1] = hcryp->Init.pInitVect[loopcounter];\r
2082       }\r
2083       for ( ; loopcounter < 13; loopcounter++)\r
2084       {\r
2085         blockb0[loopcounter+1] = 0;\r
2086       }\r
2087       \r
2088       blockb0[14] = (Size >> 8);\r
2089       blockb0[15] = (Size & 0xFF);\r
2090       \r
2091       /************************* Formatting the initial counter ***************/\r
2092       /* Byte 0:\r
2093          Bits 7 and 6 are reserved and shall be set to 0\r
2094          Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter \r
2095          blocks are distinct from B0\r
2096          Bits 0, 1, and 2 contain the same encoding of q as in B0\r
2097       */\r
2098       ctr[0] = blockb0[0] & 0x07;\r
2099       /* byte 1 to NonceSize is the IV (Nonce) */\r
2100       for(loopcounter = 1; loopcounter < hcryp->Init.IVSize + 1; loopcounter++)\r
2101       {\r
2102         ctr[loopcounter] = blockb0[loopcounter];\r
2103       }\r
2104       /* Set the LSB to 1 */\r
2105       ctr[15] |= 0x01;\r
2106       \r
2107       /* Set the key */\r
2108       CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
2109       \r
2110       /* Set the CRYP peripheral in AES CCM mode */\r
2111       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT);\r
2112       \r
2113       /* Set the Initialization Vector */\r
2114       CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);\r
2115       \r
2116       /* Select init phase */\r
2117       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);\r
2118       \r
2119       b0addr = (uint32_t)blockb0;\r
2120       /* Write the blockb0 block in the IN FIFO */\r
2121       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2122       b0addr+=4;\r
2123       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2124       b0addr+=4;\r
2125       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2126       b0addr+=4;\r
2127       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2128       \r
2129       /* Enable the CRYP peripheral */\r
2130       __HAL_CRYP_ENABLE(hcryp);\r
2131 \r
2132       /* Get tick */\r
2133       tickstart = HAL_GetTick();\r
2134 \r
2135       while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
2136       {\r
2137         /* Check for the Timeout */\r
2138         if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
2139         {\r
2140           /* Change state */\r
2141           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2142           \r
2143           /* Process Unlocked */\r
2144           __HAL_UNLOCK(hcryp);\r
2145           \r
2146           return HAL_TIMEOUT;\r
2147         }\r
2148       }\r
2149       /***************************** Header phase *****************************/\r
2150       if(headersize != 0)\r
2151       {\r
2152         /* Select header phase */\r
2153         __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);\r
2154         \r
2155         /* Enable Crypto processor */\r
2156         __HAL_CRYP_ENABLE(hcryp);\r
2157         \r
2158         for(loopcounter = 0; (loopcounter < headersize); loopcounter+=16)\r
2159         {\r
2160          /* Get tick */\r
2161          tickstart = HAL_GetTick();\r
2162 \r
2163           while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))\r
2164           {\r
2165             /* Check for the Timeout */\r
2166             if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
2167             {\r
2168               /* Change state */\r
2169               hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2170               \r
2171               /* Process Unlocked */\r
2172               __HAL_UNLOCK(hcryp);\r
2173               \r
2174               return HAL_TIMEOUT;\r
2175             }\r
2176           }\r
2177           /* Write the header block in the IN FIFO */\r
2178           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2179           headeraddr+=4;\r
2180           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2181           headeraddr+=4;\r
2182           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2183           headeraddr+=4;\r
2184           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2185           headeraddr+=4;\r
2186         }\r
2187 \r
2188         /* Get tick */\r
2189         tickstart = HAL_GetTick();\r
2190 \r
2191         while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)\r
2192         {\r
2193           /* Check for the Timeout */\r
2194           if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
2195           {\r
2196             /* Change state */\r
2197             hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2198             \r
2199             /* Process Unlocked */\r
2200             __HAL_UNLOCK(hcryp);\r
2201             \r
2202             return HAL_TIMEOUT;\r
2203           }\r
2204         }\r
2205       }\r
2206       /* Save formatted counter into the scratch buffer pScratch */\r
2207       for(loopcounter = 0; (loopcounter < 16); loopcounter++)\r
2208       {\r
2209         hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];\r
2210       }\r
2211       /* Reset bit 0 */\r
2212       hcryp->Init.pScratch[15] &= 0xfe;\r
2213       /* Select payload phase once the header phase is performed */\r
2214       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
2215       \r
2216       /* Flush FIFO */\r
2217       __HAL_CRYP_FIFO_FLUSH(hcryp);\r
2218       \r
2219       /* Set the phase */\r
2220       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
2221     }\r
2222     \r
2223     /* Enable Interrupts */\r
2224     __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);\r
2225     \r
2226     /* Enable the CRYP peripheral */\r
2227     __HAL_CRYP_ENABLE(hcryp);\r
2228     \r
2229     /* Return function status */\r
2230     return HAL_OK;\r
2231   }\r
2232   else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))\r
2233   {\r
2234     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;\r
2235     /* Write the Input block in the IN FIFO */\r
2236     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
2237     inputaddr+=4;\r
2238     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
2239     inputaddr+=4;\r
2240     hcryp->Instance->DR  = *(uint32_t*)(inputaddr);\r
2241     inputaddr+=4;\r
2242     hcryp->Instance->DR = *(uint32_t*)(inputaddr);\r
2243     hcryp->pCrypInBuffPtr += 16;\r
2244     hcryp->CrypInCount -= 16;\r
2245     if(hcryp->CrypInCount == 0)\r
2246     {\r
2247       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);\r
2248       /* Call the Input data transfer complete callback */\r
2249       HAL_CRYP_InCpltCallback(hcryp);\r
2250     }\r
2251   }\r
2252   else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))\r
2253   {\r
2254     outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;\r
2255     /* Read the Output block from the Output FIFO */\r
2256     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
2257     outputaddr+=4;\r
2258     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
2259     outputaddr+=4;\r
2260     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
2261     outputaddr+=4;\r
2262     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;\r
2263     hcryp->pCrypOutBuffPtr += 16;\r
2264     hcryp->CrypOutCount -= 16;\r
2265     if(hcryp->CrypOutCount == 0)\r
2266     {\r
2267       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);\r
2268       /* Process Unlocked */\r
2269       __HAL_UNLOCK(hcryp);\r
2270       /* Change the CRYP peripheral state */\r
2271       hcryp->State = HAL_CRYP_STATE_READY;\r
2272       /* Call Input transfer complete callback */\r
2273       HAL_CRYP_OutCpltCallback(hcryp);\r
2274     }\r
2275   }\r
2276   \r
2277   /* Return function status */\r
2278   return HAL_OK;\r
2279 }\r
2280 \r
2281 /**\r
2282   * @brief  Initializes the CRYP peripheral in AES GCM encryption mode using DMA.\r
2283   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2284   *         the configuration information for CRYP module\r
2285   * @param  pPlainData: Pointer to the plaintext buffer\r
2286   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
2287   * @param  pCypherData: Pointer to the cyphertext buffer\r
2288   * @retval HAL status\r
2289   */\r
2290 HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
2291 {\r
2292   uint32_t tickstart = 0;\r
2293   uint32_t inputaddr;\r
2294   uint32_t outputaddr;\r
2295   \r
2296   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
2297   {\r
2298     /* Process Locked */\r
2299     __HAL_LOCK(hcryp);\r
2300     \r
2301     inputaddr  = (uint32_t)pPlainData;\r
2302     outputaddr = (uint32_t)pCypherData;\r
2303     \r
2304     /* Change the CRYP peripheral state */\r
2305     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2306     \r
2307     /* Check if initialization phase has already been performed */\r
2308     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
2309     {\r
2310       /* Set the key */\r
2311       CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
2312       \r
2313       /* Set the CRYP peripheral in AES GCM mode */\r
2314       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT);\r
2315       \r
2316       /* Set the Initialization Vector */\r
2317       CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);\r
2318       \r
2319       /* Flush FIFO */\r
2320       __HAL_CRYP_FIFO_FLUSH(hcryp);\r
2321       \r
2322       /* Enable CRYP to start the init phase */\r
2323       __HAL_CRYP_ENABLE(hcryp);\r
2324       \r
2325       /* Get tick */\r
2326       tickstart = HAL_GetTick();\r
2327 \r
2328       while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
2329       {\r
2330         /* Check for the Timeout */\r
2331         if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
2332         {\r
2333           /* Change state */\r
2334           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2335           \r
2336           /* Process Unlocked */\r
2337           __HAL_UNLOCK(hcryp);\r
2338           \r
2339           return HAL_TIMEOUT;\r
2340         }\r
2341       }\r
2342       /* Flush FIFO */\r
2343       __HAL_CRYP_FIFO_FLUSH(hcryp);\r
2344       \r
2345       /* Set the header phase */\r
2346       if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1) != HAL_OK)\r
2347       {\r
2348         return HAL_TIMEOUT;\r
2349       }\r
2350       /* Disable the CRYP peripheral */\r
2351       __HAL_CRYP_DISABLE(hcryp);\r
2352       \r
2353       /* Select payload phase once the header phase is performed */\r
2354       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
2355       \r
2356       /* Flush FIFO */\r
2357       __HAL_CRYP_FIFO_FLUSH(hcryp);\r
2358       \r
2359       /* Set the phase */\r
2360       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
2361     }\r
2362     \r
2363     /* Set the input and output addresses and start DMA transfer */ \r
2364     CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
2365     \r
2366     /* Unlock process */\r
2367     __HAL_UNLOCK(hcryp);\r
2368     \r
2369     /* Return function status */\r
2370     return HAL_OK;\r
2371   }\r
2372   else\r
2373   {\r
2374     return HAL_ERROR;   \r
2375   }\r
2376 }\r
2377 \r
2378 /**\r
2379   * @brief  Initializes the CRYP peripheral in AES CCM encryption mode using interrupt.\r
2380   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2381   *         the configuration information for CRYP module\r
2382   * @param  pPlainData: Pointer to the plaintext buffer\r
2383   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
2384   * @param  pCypherData: Pointer to the cyphertext buffer\r
2385   * @retval HAL status\r
2386   */\r
2387 HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)\r
2388 {\r
2389   uint32_t tickstart = 0;   \r
2390   uint32_t inputaddr;\r
2391   uint32_t outputaddr;\r
2392   uint32_t headersize;\r
2393   uint32_t headeraddr;\r
2394   uint32_t loopcounter = 0;\r
2395   uint32_t bufferidx = 0;\r
2396   uint8_t blockb0[16] = {0};/* Block B0 */\r
2397   uint8_t ctr[16] = {0}; /* Counter */\r
2398   uint32_t b0addr = (uint32_t)blockb0;\r
2399   \r
2400   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
2401   {\r
2402     /* Process Locked */\r
2403     __HAL_LOCK(hcryp);\r
2404     \r
2405     inputaddr  = (uint32_t)pPlainData;\r
2406     outputaddr = (uint32_t)pCypherData;\r
2407     \r
2408     headersize = hcryp->Init.HeaderSize;\r
2409     headeraddr = (uint32_t)hcryp->Init.Header;\r
2410     \r
2411     hcryp->CrypInCount = Size;\r
2412     hcryp->pCrypInBuffPtr = pPlainData;\r
2413     hcryp->pCrypOutBuffPtr = pCypherData;\r
2414     hcryp->CrypOutCount = Size;\r
2415     \r
2416     /* Change the CRYP peripheral state */\r
2417     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2418     \r
2419     /* Check if initialization phase has already been performed */\r
2420     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
2421     {\r
2422       /************************ Formatting the header block *******************/\r
2423       if(headersize != 0)\r
2424       {\r
2425         /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */\r
2426         if(headersize < 65280)\r
2427         {\r
2428           hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);\r
2429           hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);\r
2430           headersize += 2;\r
2431         }\r
2432         else\r
2433         {\r
2434           /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */\r
2435           hcryp->Init.pScratch[bufferidx++] = 0xFF;\r
2436           hcryp->Init.pScratch[bufferidx++] = 0xFE;\r
2437           hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000;\r
2438           hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000;\r
2439           hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00;\r
2440           hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ff;\r
2441           headersize += 6;\r
2442         }\r
2443         /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */\r
2444         for(loopcounter = 0; loopcounter < headersize; loopcounter++)\r
2445         {\r
2446           hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];\r
2447         }\r
2448         /* Check if the header size is modulo 16 */\r
2449         if ((headersize % 16) != 0)\r
2450         {\r
2451           /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */\r
2452           for(loopcounter = headersize; loopcounter <= ((headersize/16) + 1) * 16; loopcounter++)\r
2453           {\r
2454             hcryp->Init.pScratch[loopcounter] = 0;\r
2455           }\r
2456           /* Set the header size to modulo 16 */\r
2457           headersize = ((headersize/16) + 1) * 16;\r
2458         }\r
2459         /* Set the pointer headeraddr to hcryp->Init.pScratch */\r
2460         headeraddr = (uint32_t)hcryp->Init.pScratch;\r
2461       }\r
2462       /*********************** Formatting the block B0 ************************/\r
2463       if(headersize != 0)\r
2464       {\r
2465         blockb0[0] = 0x40;\r
2466       }\r
2467       /* Flags byte */\r
2468       /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07) */\r
2469       blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07 ) << 3);\r
2470       blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);\r
2471       \r
2472       for (loopcounter = 0; loopcounter < hcryp->Init.IVSize; loopcounter++)\r
2473       {\r
2474         blockb0[loopcounter+1] = hcryp->Init.pInitVect[loopcounter];\r
2475       }\r
2476       for ( ; loopcounter < 13; loopcounter++)\r
2477       {\r
2478         blockb0[loopcounter+1] = 0;\r
2479       }\r
2480       \r
2481       blockb0[14] = (Size >> 8);\r
2482       blockb0[15] = (Size & 0xFF);\r
2483       \r
2484       /************************* Formatting the initial counter ***************/\r
2485       /* Byte 0:\r
2486          Bits 7 and 6 are reserved and shall be set to 0\r
2487          Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter \r
2488          blocks are distinct from B0\r
2489          Bits 0, 1, and 2 contain the same encoding of q as in B0\r
2490       */\r
2491       ctr[0] = blockb0[0] & 0x07;\r
2492       /* byte 1 to NonceSize is the IV (Nonce) */\r
2493       for(loopcounter = 1; loopcounter < hcryp->Init.IVSize + 1; loopcounter++)\r
2494       {\r
2495         ctr[loopcounter] = blockb0[loopcounter];\r
2496       }\r
2497       /* Set the LSB to 1 */\r
2498       ctr[15] |= 0x01;\r
2499       \r
2500       /* Set the key */\r
2501       CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
2502       \r
2503       /* Set the CRYP peripheral in AES CCM mode */\r
2504       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT);\r
2505       \r
2506       /* Set the Initialization Vector */\r
2507       CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);\r
2508       \r
2509       /* Select init phase */\r
2510       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);\r
2511       \r
2512       b0addr = (uint32_t)blockb0;\r
2513       /* Write the blockb0 block in the IN FIFO */\r
2514       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2515       b0addr+=4;\r
2516       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2517       b0addr+=4;\r
2518       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2519       b0addr+=4;\r
2520       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2521       \r
2522       /* Enable the CRYP peripheral */\r
2523       __HAL_CRYP_ENABLE(hcryp);\r
2524       \r
2525       /* Get tick */\r
2526       tickstart = HAL_GetTick();\r
2527  \r
2528       while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
2529       {\r
2530         /* Check for the Timeout */\r
2531         if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
2532         {\r
2533           /* Change state */\r
2534           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2535           \r
2536           /* Process Unlocked */\r
2537           __HAL_UNLOCK(hcryp);\r
2538           \r
2539           return HAL_TIMEOUT;\r
2540         }\r
2541       }\r
2542       /***************************** Header phase *****************************/\r
2543       if(headersize != 0)\r
2544       {\r
2545         /* Select header phase */\r
2546         __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);\r
2547         \r
2548         /* Enable Crypto processor */\r
2549         __HAL_CRYP_ENABLE(hcryp);\r
2550         \r
2551         for(loopcounter = 0; (loopcounter < headersize); loopcounter+=16)\r
2552         {\r
2553          /* Get tick */\r
2554          tickstart = HAL_GetTick();\r
2555 \r
2556           while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))\r
2557           {\r
2558             /* Check for the Timeout */\r
2559             if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
2560             {\r
2561               /* Change state */\r
2562               hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2563               \r
2564               /* Process Unlocked */\r
2565               __HAL_UNLOCK(hcryp);\r
2566               \r
2567               return HAL_TIMEOUT;\r
2568             }\r
2569           }\r
2570           /* Write the header block in the IN FIFO */\r
2571           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2572           headeraddr+=4;\r
2573           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2574           headeraddr+=4;\r
2575           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2576           headeraddr+=4;\r
2577           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2578           headeraddr+=4;\r
2579         }\r
2580         \r
2581         /* Get tick */\r
2582         tickstart = HAL_GetTick();\r
2583 \r
2584         while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)\r
2585         {\r
2586           /* Check for the Timeout */\r
2587           if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
2588           {\r
2589             /* Change state */\r
2590             hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2591             \r
2592             /* Process Unlocked */\r
2593             __HAL_UNLOCK(hcryp);\r
2594             \r
2595             return HAL_TIMEOUT;\r
2596           }\r
2597         }\r
2598       }\r
2599       /* Save formatted counter into the scratch buffer pScratch */\r
2600       for(loopcounter = 0; (loopcounter < 16); loopcounter++)\r
2601       {\r
2602         hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];\r
2603       }\r
2604       /* Reset bit 0 */\r
2605       hcryp->Init.pScratch[15] &= 0xfe;\r
2606       \r
2607       /* Select payload phase once the header phase is performed */\r
2608       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
2609       \r
2610       /* Flush FIFO */\r
2611       __HAL_CRYP_FIFO_FLUSH(hcryp);\r
2612       \r
2613       /* Set the phase */\r
2614       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
2615     }\r
2616     \r
2617     /* Set the input and output addresses and start DMA transfer */ \r
2618     CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
2619     \r
2620     /* Unlock process */\r
2621     __HAL_UNLOCK(hcryp);\r
2622     \r
2623     /* Return function status */\r
2624     return HAL_OK;\r
2625   }\r
2626   else\r
2627   {\r
2628     return HAL_ERROR;   \r
2629   }\r
2630 }\r
2631 \r
2632 /**\r
2633   * @brief  Initializes the CRYP peripheral in AES GCM decryption mode using DMA.\r
2634   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2635   *         the configuration information for CRYP module\r
2636   * @param  pCypherData: Pointer to the cyphertext buffer.\r
2637   * @param  Size: Length of the cyphertext buffer, must be a multiple of 16\r
2638   * @param  pPlainData: Pointer to the plaintext buffer\r
2639   * @retval HAL status\r
2640   */\r
2641 HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
2642 {\r
2643   uint32_t tickstart = 0;   \r
2644   uint32_t inputaddr;\r
2645   uint32_t outputaddr;\r
2646   \r
2647   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
2648   {\r
2649     /* Process Locked */\r
2650     __HAL_LOCK(hcryp);\r
2651     \r
2652     inputaddr  = (uint32_t)pCypherData;\r
2653     outputaddr = (uint32_t)pPlainData;\r
2654     \r
2655     /* Change the CRYP peripheral state */\r
2656     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2657     \r
2658     /* Check if initialization phase has already been performed */\r
2659     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
2660     {\r
2661       /* Set the key */\r
2662       CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
2663       \r
2664       /* Set the CRYP peripheral in AES GCM decryption mode */\r
2665       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT);\r
2666       \r
2667       /* Set the Initialization Vector */\r
2668       CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);\r
2669       \r
2670       /* Enable CRYP to start the init phase */\r
2671       __HAL_CRYP_ENABLE(hcryp);\r
2672       \r
2673       /* Get tick */\r
2674       tickstart = HAL_GetTick();\r
2675 \r
2676       while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
2677       {\r
2678         /* Check for the Timeout */\r
2679         if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
2680         {\r
2681           /* Change state */\r
2682           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2683           \r
2684           /* Process Unlocked */\r
2685           __HAL_UNLOCK(hcryp);\r
2686           \r
2687           return HAL_TIMEOUT;\r
2688         }\r
2689       }\r
2690       \r
2691       /* Set the header phase */\r
2692       if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1) != HAL_OK)\r
2693       {\r
2694         return HAL_TIMEOUT;\r
2695       }\r
2696       /* Disable the CRYP peripheral */\r
2697       __HAL_CRYP_DISABLE(hcryp);\r
2698       \r
2699       /* Select payload phase once the header phase is performed */\r
2700       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
2701       \r
2702       /* Set the phase */\r
2703       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
2704     }\r
2705     \r
2706     /* Set the input and output addresses and start DMA transfer */ \r
2707     CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
2708     \r
2709     /* Unlock process */\r
2710     __HAL_UNLOCK(hcryp);\r
2711     \r
2712     /* Return function status */\r
2713     return HAL_OK;\r
2714   }\r
2715   else\r
2716   {\r
2717     return HAL_ERROR;   \r
2718   }\r
2719 }\r
2720 \r
2721 /**\r
2722   * @brief  Initializes the CRYP peripheral in AES CCM decryption mode using DMA\r
2723   *         then decrypted pCypherData. The cypher data are available in pPlainData.\r
2724   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains\r
2725   *         the configuration information for CRYP module\r
2726   * @param  pCypherData: Pointer to the cyphertext buffer  \r
2727   * @param  Size: Length of the plaintext buffer, must be a multiple of 16\r
2728   * @param  pPlainData: Pointer to the plaintext buffer  \r
2729   * @retval HAL status\r
2730   */\r
2731 HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)\r
2732 {\r
2733   uint32_t tickstart = 0;   \r
2734   uint32_t inputaddr;\r
2735   uint32_t outputaddr;\r
2736   uint32_t headersize;\r
2737   uint32_t headeraddr;\r
2738   uint32_t loopcounter = 0;\r
2739   uint32_t bufferidx = 0;\r
2740   uint8_t blockb0[16] = {0};/* Block B0 */\r
2741   uint8_t ctr[16] = {0}; /* Counter */\r
2742   uint32_t b0addr = (uint32_t)blockb0;\r
2743   \r
2744   if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))\r
2745   {\r
2746     /* Process Locked */\r
2747     __HAL_LOCK(hcryp);\r
2748     \r
2749     inputaddr  = (uint32_t)pCypherData;\r
2750     outputaddr = (uint32_t)pPlainData;\r
2751     \r
2752     headersize = hcryp->Init.HeaderSize;\r
2753     headeraddr = (uint32_t)hcryp->Init.Header;\r
2754     \r
2755     hcryp->CrypInCount = Size;\r
2756     hcryp->pCrypInBuffPtr = pCypherData;\r
2757     hcryp->pCrypOutBuffPtr = pPlainData;\r
2758     hcryp->CrypOutCount = Size;\r
2759     \r
2760     /* Change the CRYP peripheral state */\r
2761     hcryp->State = HAL_CRYP_STATE_BUSY;\r
2762     \r
2763     /* Check if initialization phase has already been performed */\r
2764     if(hcryp->Phase == HAL_CRYP_PHASE_READY)\r
2765     {\r
2766       /************************ Formatting the header block *******************/\r
2767       if(headersize != 0)\r
2768       {\r
2769         /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */\r
2770         if(headersize < 65280)\r
2771         {\r
2772           hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);\r
2773           hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);\r
2774           headersize += 2;\r
2775         }\r
2776         else\r
2777         {\r
2778           /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */\r
2779           hcryp->Init.pScratch[bufferidx++] = 0xFF;\r
2780           hcryp->Init.pScratch[bufferidx++] = 0xFE;\r
2781           hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000;\r
2782           hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000;\r
2783           hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00;\r
2784           hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ff;\r
2785           headersize += 6;\r
2786         }\r
2787         /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */\r
2788         for(loopcounter = 0; loopcounter < headersize; loopcounter++)\r
2789         {\r
2790           hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];\r
2791         }\r
2792         /* Check if the header size is modulo 16 */\r
2793         if ((headersize % 16) != 0)\r
2794         {\r
2795           /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */\r
2796           for(loopcounter = headersize; loopcounter <= ((headersize/16) + 1) * 16; loopcounter++)\r
2797           {\r
2798             hcryp->Init.pScratch[loopcounter] = 0;\r
2799           }\r
2800           /* Set the header size to modulo 16 */\r
2801           headersize = ((headersize/16) + 1) * 16;\r
2802         }\r
2803         /* Set the pointer headeraddr to hcryp->Init.pScratch */\r
2804         headeraddr = (uint32_t)hcryp->Init.pScratch;\r
2805       }\r
2806       /*********************** Formatting the block B0 ************************/\r
2807       if(headersize != 0)\r
2808       {\r
2809         blockb0[0] = 0x40;\r
2810       }\r
2811       /* Flags byte */\r
2812       /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07) */\r
2813       blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07 ) << 3);\r
2814       blockb0[0] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);\r
2815       \r
2816       for (loopcounter = 0; loopcounter < hcryp->Init.IVSize; loopcounter++)\r
2817       {\r
2818         blockb0[loopcounter+1] = hcryp->Init.pInitVect[loopcounter];\r
2819       }\r
2820       for ( ; loopcounter < 13; loopcounter++)\r
2821       {\r
2822         blockb0[loopcounter+1] = 0;\r
2823       }\r
2824       \r
2825       blockb0[14] = (Size >> 8);\r
2826       blockb0[15] = (Size & 0xFF);\r
2827       \r
2828       /************************* Formatting the initial counter ***************/\r
2829       /* Byte 0:\r
2830          Bits 7 and 6 are reserved and shall be set to 0\r
2831          Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter \r
2832          blocks are distinct from B0\r
2833          Bits 0, 1, and 2 contain the same encoding of q as in B0\r
2834       */\r
2835       ctr[0] = blockb0[0] & 0x07;\r
2836       /* byte 1 to NonceSize is the IV (Nonce) */\r
2837       for(loopcounter = 1; loopcounter < hcryp->Init.IVSize + 1; loopcounter++)\r
2838       {\r
2839         ctr[loopcounter] = blockb0[loopcounter];\r
2840       }\r
2841       /* Set the LSB to 1 */\r
2842       ctr[15] |= 0x01;\r
2843       \r
2844       /* Set the key */\r
2845       CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);\r
2846       \r
2847       /* Set the CRYP peripheral in AES CCM mode */\r
2848       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT);\r
2849       \r
2850       /* Set the Initialization Vector */\r
2851       CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);\r
2852       \r
2853       /* Select init phase */\r
2854       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);\r
2855       \r
2856       b0addr = (uint32_t)blockb0;\r
2857       /* Write the blockb0 block in the IN FIFO */\r
2858       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2859       b0addr+=4;\r
2860       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2861       b0addr+=4;\r
2862       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2863       b0addr+=4;\r
2864       hcryp->Instance->DR = *(uint32_t*)(b0addr);\r
2865       \r
2866       /* Enable the CRYP peripheral */\r
2867       __HAL_CRYP_ENABLE(hcryp);\r
2868       \r
2869       /* Get tick */\r
2870       tickstart = HAL_GetTick();\r
2871  \r
2872       while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)\r
2873       {\r
2874         /* Check for the Timeout */\r
2875         \r
2876         if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
2877         {\r
2878           /* Change state */\r
2879           hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2880           \r
2881           /* Process Unlocked */\r
2882           __HAL_UNLOCK(hcryp);\r
2883           \r
2884           return HAL_TIMEOUT;\r
2885           \r
2886         }\r
2887       }\r
2888       /***************************** Header phase *****************************/\r
2889       if(headersize != 0)\r
2890       {\r
2891         /* Select header phase */\r
2892         __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);\r
2893         \r
2894         /* Enable Crypto processor */\r
2895         __HAL_CRYP_ENABLE(hcryp);\r
2896         \r
2897         for(loopcounter = 0; (loopcounter < headersize); loopcounter+=16)\r
2898         {\r
2899          /* Get tick */\r
2900          tickstart = HAL_GetTick();\r
2901  \r
2902           while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))\r
2903           {\r
2904             /* Check for the Timeout */\r
2905             if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
2906             {\r
2907               /* Change state */\r
2908               hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2909               \r
2910               /* Process Unlocked */\r
2911               __HAL_UNLOCK(hcryp);\r
2912               \r
2913               return HAL_TIMEOUT;\r
2914             }\r
2915           }\r
2916           /* Write the header block in the IN FIFO */\r
2917           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2918           headeraddr+=4;\r
2919           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2920           headeraddr+=4;\r
2921           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2922           headeraddr+=4;\r
2923           hcryp->Instance->DR = *(uint32_t*)(headeraddr);\r
2924           headeraddr+=4;\r
2925         }\r
2926         \r
2927         /* Get tick */\r
2928         tickstart = HAL_GetTick();\r
2929 \r
2930         while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)\r
2931         {\r
2932           /* Check for the Timeout */\r
2933           if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)\r
2934           {\r
2935             /* Change state */\r
2936             hcryp->State = HAL_CRYP_STATE_TIMEOUT;\r
2937             \r
2938             /* Process Unlocked */\r
2939             __HAL_UNLOCK(hcryp);\r
2940             \r
2941             return HAL_TIMEOUT;\r
2942           }\r
2943         }\r
2944       }\r
2945       /* Save formatted counter into the scratch buffer pScratch */\r
2946       for(loopcounter = 0; (loopcounter < 16); loopcounter++)\r
2947       {\r
2948         hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];\r
2949       }\r
2950       /* Reset bit 0 */\r
2951       hcryp->Init.pScratch[15] &= 0xfe;\r
2952       /* Select payload phase once the header phase is performed */\r
2953       __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);\r
2954       \r
2955       /* Flush FIFO */\r
2956       __HAL_CRYP_FIFO_FLUSH(hcryp);\r
2957       \r
2958       /* Set the phase */\r
2959       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;\r
2960     }\r
2961     /* Set the input and output addresses and start DMA transfer */ \r
2962     CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);\r
2963     \r
2964     /* Unlock process */\r
2965     __HAL_UNLOCK(hcryp);\r
2966     \r
2967     /* Return function status */\r
2968     return HAL_OK;\r
2969   }\r
2970   else\r
2971   {\r
2972     return HAL_ERROR;   \r
2973   }\r
2974 }\r
2975 \r
2976 /**\r
2977   * @}\r
2978   */\r
2979   \r
2980 /** @defgroup CRYPEx_Exported_Functions_Group2 CRYPEx IRQ handler management  \r
2981  *  @brief   CRYPEx IRQ handler.\r
2982  *\r
2983 @verbatim   \r
2984   ==============================================================================\r
2985                 ##### CRYPEx IRQ handler management #####\r
2986   ==============================================================================  \r
2987 [..]  This section provides CRYPEx IRQ handler function.\r
2988 \r
2989 @endverbatim\r
2990   * @{\r
2991   */\r
2992 \r
2993 /**\r
2994   * @brief  This function handles CRYPEx interrupt request.\r
2995   * @param  hcryp: pointer to a CRYPEx_HandleTypeDef structure that contains\r
2996   *         the configuration information for CRYP module\r
2997   * @retval None\r
2998   */\r
2999 void HAL_CRYPEx_GCMCCM_IRQHandler(CRYP_HandleTypeDef *hcryp)\r
3000 {\r
3001   switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION)\r
3002   {    \r
3003   case CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT:\r
3004     HAL_CRYPEx_AESGCM_Encrypt_IT(hcryp, NULL, 0, NULL);\r
3005     break;\r
3006     \r
3007   case CRYP_CR_ALGOMODE_AES_GCM_DECRYPT:\r
3008     HAL_CRYPEx_AESGCM_Decrypt_IT(hcryp, NULL, 0, NULL);\r
3009     break;\r
3010     \r
3011   case CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT:\r
3012     HAL_CRYPEx_AESCCM_Encrypt_IT(hcryp, NULL, 0, NULL);\r
3013     break;\r
3014     \r
3015   case CRYP_CR_ALGOMODE_AES_CCM_DECRYPT:\r
3016     HAL_CRYPEx_AESCCM_Decrypt_IT(hcryp, NULL, 0, NULL);\r
3017     break;\r
3018     \r
3019   default:\r
3020     break;\r
3021   }\r
3022 }\r
3023 \r
3024 /**\r
3025   * @}\r
3026   */\r
3027 \r
3028 /**\r
3029   * @}\r
3030   */\r
3031 #endif /* STM32F756xx */\r
3032 \r
3033 #endif /* HAL_CRYP_MODULE_ENABLED */\r
3034 \r
3035 /**\r
3036   * @}\r
3037   */\r
3038 \r
3039 /**\r
3040   * @}\r
3041   */\r
3042 \r
3043 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r