1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_hal_cryp.c
4   * @author  MCD Application Team
5   * @brief   CRYP HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the Cryptography (CRYP) peripheral:
8   *           + Initialization and de-initialization functions
9   *           + Processing functions using polling mode
10   *           + Processing functions using interrupt mode
11   *           + Processing functions using DMA mode
12   *           + Peripheral State functions
13   *
14   ******************************************************************************
15   * @attention
16   *
17   * Copyright (c) 2017 STMicroelectronics.
18   * All rights reserved.
19   *
20   * This software is licensed under terms that can be found in the LICENSE file in
21   * the root directory of this software component.
22   * If no LICENSE file comes with this software, it is provided AS-IS.
23   ******************************************************************************
24   @verbatim
25   ==============================================================================
26                      ##### How to use this driver #####
27   ==============================================================================
28     [..]
29       The CRYP HAL driver can be used as follows:
30 
31       (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
32          (++) Enable the CRYP interface clock using __HAL_RCC_AES_CLK_ENABLE()
33          (++) In case of using interrupts (e.g. HAL_CRYP_AES_IT())
34              (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
35              (+++) Enable the AES IRQ handler using HAL_NVIC_EnableIRQ()
36              (+++) In AES IRQ handler, call HAL_CRYP_IRQHandler()
37          (++) In case of using DMA to control data transfer (e.g. HAL_CRYPEx_AES_DMA())
38              (+++) Enable the DMA2 interface clock using
39                  __HAL_RCC_DMA2_CLK_ENABLE()
40              (+++) Configure and enable two DMA channels one for managing data transfer from
41                  memory to peripheral (input channel) and another channel for managing data
42                  transfer from peripheral to memory (output channel)
43              (+++) Associate the initialized DMA handle to the CRYP DMA handle
44                  using __HAL_LINKDMA()
45              (+++) Configure the priority and enable the NVIC for the transfer complete
46                  interrupt on the two DMA channels. The output channel should have higher
47                  priority than the input channel.
48                  Resort to HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
49 
50       (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures:
51          (++) The data type: 1-bit, 8-bit, 16-bit and 32-bit
52          (++) The AES operating mode (encryption, key derivation and/or decryption)
53          (++) The AES chaining mode (ECB, CBC, CTR, GCM, GMAC, CMAC when applicable, CCM when applicable)
54          (++) The encryption/decryption key if so required
55          (++) The initialization vector or nonce if applicable (not used in ECB mode).
56 
57       (#)Three processing (encryption/decryption) functions are available:
58          (++) Polling mode: encryption and decryption APIs are blocking functions
59               i.e. they process the data and wait till the processing is finished
60          (++) Interrupt mode: encryption and decryption APIs are not blocking functions
61               i.e. they process the data under interrupt
62          (++) DMA mode: encryption and decryption APIs are not blocking functions
63               i.e. the data transfer is ensured by DMA
64 
65        (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
66 
67      *** Callback registration ***
68      ===================================
69      [..]
70       (#) The compilation define  USE_HAL_CRYP_REGISTER_CALLBACKS when set to 1
71           allows the user to configure dynamically the driver callbacks.
72           Use function @ref HAL_CRYP_RegisterCallback() to register a user callback.
73 
74       (#) Function @ref HAL_CRYP_RegisterCallback() allows to register following callbacks:
75             (+) InCpltCallback : callback for input DMA transfer completion.
76             (+) OutCpltCallback : callback for output DMA transfer completion.
77             (+) CompCpltCallback : callback for computation completion.
78             (+) ErrorCallback : callback for error.
79             (+) MspInitCallback    : CRYP MspInit.
80             (+) MspDeInitCallback  : CRYP MspDeInit.
81           This function takes as parameters the HAL peripheral handle, the Callback ID
82           and a pointer to the user callback function.
83 
84       (#) Use function @ref HAL_CRYP_UnRegisterCallback() to reset a callback to the default
85           weak (surcharged) function.
86           @ref HAL_CRYP_UnRegisterCallback() takes as parameters the HAL peripheral handle,
87           and the Callback ID.
88           This function allows to reset following callbacks:
89             (+) InCpltCallback : callback for input DMA transfer completion.
90             (+) OutCpltCallback : callback for output DMA transfer completion.
91             (+) CompCpltCallback : callback for computation completion.
92             (+) ErrorCallback : callback for error.
93             (+) MspInitCallback    : CRYP MspInit.
94             (+) MspDeInitCallback  : CRYP MspDeInit.
95 
96       (#) By default, after the @ref HAL_CRYP_Init and if the state is HAL_CRYP_STATE_RESET
97           all callbacks are reset to the corresponding legacy weak (surcharged) functions:
98           examples @ref HAL_CRYP_InCpltCallback(), @ref HAL_CRYP_ErrorCallback()
99           Exception done for MspInit and MspDeInit callbacks that are respectively
100           reset to the legacy weak (surcharged) functions in the @ref HAL_CRYP_Init
101           and @ref HAL_CRYP_DeInit only when these callbacks are null (not registered beforehand)
102           If not, MspInit or MspDeInit are not null, the @ref HAL_CRYP_Init and @ref HAL_CRYP_DeInit
103           keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
104 
105           Callbacks can be registered/unregistered in READY state only.
106           Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
107           in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
108           during the Init/DeInit.
109           In that case first register the MspInit/MspDeInit user callbacks
110           using @ref HAL_CRYP_RegisterCallback before calling @ref HAL_CRYP_DeInit
111           or @ref HAL_�CRYP_Init function.
112 
113           When The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS is set to 0 or
114           not defined, the callback registering feature is not available
115           and weak (surcharged) callbacks are used.
116 
117 
118   @endverbatim
119   ******************************************************************************
120   */
121 
122 /* Includes ------------------------------------------------------------------*/
123 #include "stm32l4xx_hal.h"
124 
125 #ifdef HAL_CRYP_MODULE_ENABLED
126 
127 #if defined(AES)
128 
129 /** @addtogroup STM32L4xx_HAL_Driver
130   * @{
131   */
132 
133 /** @defgroup CRYP CRYP
134   * @brief CRYP HAL module driver.
135   * @{
136   */
137 
138 
139 
140 /* Private typedef -----------------------------------------------------------*/
141 /* Private define ------------------------------------------------------------*/
142 /* Private macro -------------------------------------------------------------*/
143 /* Private variables ---------------------------------------------------------*/
144 /* Private functions --------------------------------------------------------*/
145 
146 /** @defgroup CRYP_Private_Functions CRYP Private Functions
147   * @{
148   */
149 
150 static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp);
151 static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp);
152 static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
153 
154 /**
155   * @}
156   */
157 
158 /* Exported functions ---------------------------------------------------------*/
159 
160 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
161   * @{
162   */
163 
164 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions
165  *  @brief    Initialization and Configuration functions.
166  *
167 @verbatim
168   ==============================================================================
169               ##### Initialization and deinitialization functions #####
170   ==============================================================================
171     [..]  This section provides functions allowing to:
172       (+) Initialize the CRYP according to the specified parameters
173           in the CRYP_InitTypeDef and creates the associated handle
174       (+) DeInitialize the CRYP peripheral
175       (+) Initialize the CRYP MSP (MCU Specific Package)
176       (+) De-Initialize the CRYP MSP
177 
178     [..]
179     (@) Specific care must be taken to format the key and the Initialization Vector IV!
180 
181    [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where
182         b127 is the MSB and b0 the LSB, the key must be stored in MCU memory
183         (+) as a sequence of words where the MSB word comes first (occupies the
184           lowest memory address)
185         (+) where each word is byte-swapped:
186          (++)   address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120
187          (++)   address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88
188          (++)   address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56
189          (++)   address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24
190     [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}.
191         The 4 32-bit words that make the key must be stored as follows in MCU memory:
192          (+)    address n+0 : 0x B12 B13 B14 B15
193          (+)    address n+4 : 0x B8 B9 B10 B11
194          (+)    address n+8 : 0x B4 B5 B6 B7
195          (+)    address n+C : 0x B0 B1 B2 B3
196     [..]  which leads to the expected setting
197       (+)       AES_KEYR3 = 0x B15 B14 B13 B12
198       (+)       AES_KEYR2 = 0x B11 B10 B9 B8
199       (+)       AES_KEYR1 = 0x B7 B6 B5 B4
200       (+)       AES_KEYR0 = 0x B3 B2 B1 B0
201 
202    [..]  Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}.
203          The 8 32-bit words that make the key must be stored as follows in MCU memory:
204          (+)    address n+00 : 0x B28 B29 B30 B31
205          (+)    address n+04 : 0x B24 B25 B26 B27
206          (+)    address n+08 : 0x B20 B21 B22 B23
207          (+)    address n+0C : 0x B16 B17 B18 B19
208          (+)    address n+10 : 0x B12 B13 B14 B15
209          (+)    address n+14 : 0x B8 B9 B10 B11
210          (+)    address n+18 : 0x B4 B5 B6 B7
211          (+)    address n+1C : 0x B0 B1 B2 B3
212     [..]  which leads to the expected setting
213       (+)       AES_KEYR7 = 0x B31 B30 B29 B28
214       (+)       AES_KEYR6 = 0x B27 B26 B25 B24
215       (+)       AES_KEYR5 = 0x B23 B22 B21 B20
216       (+)       AES_KEYR4 = 0x B19 B18 B17 B16
217       (+)       AES_KEYR3 = 0x B15 B14 B13 B12
218       (+)       AES_KEYR2 = 0x B11 B10 B9 B8
219       (+)       AES_KEYR1 = 0x B7 B6 B5 B4
220       (+)       AES_KEYR0 = 0x B3 B2 B1 B0
221 
222    [..] Initialization Vector IV (4 32-bit words) format must follow the same as
223         that of a 128-bit long key.
224 
225   [..]
226 
227 @endverbatim
228   * @{
229   */
230 
231 /**
232   * @brief  Initialize the CRYP according to the specified
233   *         parameters in the CRYP_InitTypeDef and initialize the associated handle.
234   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
235   *         the configuration information for CRYP module
236   * @note Specific care must be taken to format the key and the Initialization Vector IV
237   *       stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations
238   *       hereabove.
239   * @retval HAL status
240   */
HAL_CRYP_Init(CRYP_HandleTypeDef * hcryp)241 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
242 {
243   /* Check the CRYP handle allocation */
244   if(hcryp == NULL)
245   {
246     return HAL_ERROR;
247   }
248 
249   /* Check the instance */
250   assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance));
251 
252   /* Check the parameters */
253   assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
254   assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
255   assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode));
256   /* ChainingMode parameter is irrelevant when mode is set to Key derivation */
257   if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
258   {
259     assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode));
260   }
261   assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag));
262 
263   /*========================================================*/
264   /* Check the proper operating/chaining modes combinations */
265   /*========================================================*/
266   /* Check the proper chaining when the operating mode is key derivation and decryption */
267 #if defined(AES_CR_NPBLB)
268   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
269          ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR)           \
270        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)      \
271        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)))
272 #else
273   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
274          ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR)           \
275        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)      \
276        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)))
277 #endif
278   {
279     return HAL_ERROR;
280   }
281   /* Check that key derivation is not set in CMAC mode or CCM mode when applicable */
282 #if defined(AES_CR_NPBLB)
283   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
284    && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
285 #else
286   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
287    && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
288 #endif
289   {
290     return HAL_ERROR;
291   }
292 
293 
294   /*================*/
295   /* Initialization */
296   /*================*/
297   /* Initialization start */
298 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
299   if (hcryp->State == HAL_CRYP_STATE_RESET)
300   {
301     /* Allocate lock resource and initialize it */
302     hcryp->Lock = HAL_UNLOCKED;
303 
304     /* Reset Callback pointers in HAL_CRYP_STATE_RESET only */
305     hcryp->InCpltCallback   =  HAL_CRYP_InCpltCallback;            /* Legacy weak (surcharged) input DMA transfer completion callback */
306     hcryp->OutCpltCallback   =  HAL_CRYP_OutCpltCallback;           /* Legacy weak (surcharged) output DMA transfer completion callback */
307     hcryp->CompCpltCallback =  HAL_CRYPEx_ComputationCpltCallback; /* Legacy weak (surcharged) computation completion callback */
308     hcryp->ErrorCallback    =  HAL_CRYP_ErrorCallback;             /* Legacy weak (surcharged) error callback */
309     if(hcryp->MspInitCallback == NULL)
310     {
311       hcryp->MspInitCallback = HAL_CRYP_MspInit;
312     }
313 
314     /* Init the low level hardware */
315     hcryp->MspInitCallback(hcryp);
316   }
317 #else
318   if(hcryp->State == HAL_CRYP_STATE_RESET)
319   {
320     /* Allocate lock resource and initialize it */
321     hcryp->Lock = HAL_UNLOCKED;
322 
323     /* Init the low level hardware */
324     HAL_CRYP_MspInit(hcryp);
325   }
326 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
327 
328   /* Change the CRYP state */
329   hcryp->State = HAL_CRYP_STATE_BUSY;
330 
331   /* Disable the Peripheral */
332   __HAL_CRYP_DISABLE(hcryp);
333 
334   /*=============================================================*/
335   /* AES initialization common to all operating modes            */
336   /*=============================================================*/
337   /* Set the Key size selection */
338   MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize);
339 
340   /* Set the default CRYP phase when this parameter is not used.
341      Phase is updated below in case of GCM/GMAC(/CMAC)(/CCM) setting. */
342   hcryp->Phase = HAL_CRYP_PHASE_NOT_USED;
343 
344 
345 
346   /*=============================================================*/
347   /* Carry on the initialization based on the AES operating mode */
348   /*=============================================================*/
349   /* Key derivation */
350   if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
351   {
352     MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION);
353 
354     /* Configure the Key registers */
355     if (CRYP_SetKey(hcryp) != HAL_OK)
356     {
357       return HAL_ERROR;
358     }
359   }
360   else
361   /* Encryption / Decryption (with or without key derivation) / authentication */
362   {
363 #if !defined(AES_CR_NPBLB)
364     /* Set data type, operating and chaining modes.
365        In case of GCM or GMAC, data type is forced to 0b00 */
366     if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
367     {
368       MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
369     }
370     else
371 #endif
372     {
373       MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
374     }
375 
376 
377    /* Specify the encryption/decryption phase in case of Galois counter mode (GCM),
378       Galois message authentication code (GMAC), cipher message authentication code (CMAC) when applicable
379       or Counter with Cipher Mode (CCM) when applicable */
380 #if defined(AES_CR_NPBLB)
381    if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
382     || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
383 #else
384    if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
385     || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
386 #endif
387     {
388       MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase);
389       hcryp->Phase = HAL_CRYP_PHASE_START;
390     }
391 
392 
393     /* Configure the Key registers if no need to bypass this step */
394     if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE)
395     {
396       if (CRYP_SetKey(hcryp) != HAL_OK)
397       {
398         return HAL_ERROR;
399       }
400     }
401 
402     /* If applicable, configure the Initialization Vector */
403     if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB)
404     {
405       if (CRYP_SetInitVector(hcryp) != HAL_OK)
406       {
407         return HAL_ERROR;
408       }
409     }
410   }
411 
412 #if defined(AES_CR_NPBLB)
413   /* Clear NPBLB field */
414   CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
415 #endif
416 
417   /* Reset CrypInCount and CrypOutCount */
418   hcryp->CrypInCount = 0;
419   hcryp->CrypOutCount = 0;
420 
421   /* Reset ErrorCode field */
422   hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
423 
424   /* Reset Mode suspension request */
425   hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
426 
427   /* Change the CRYP state */
428   hcryp->State = HAL_CRYP_STATE_READY;
429 
430   /* Enable the Peripheral */
431   __HAL_CRYP_ENABLE(hcryp);
432 
433   /* Return function status */
434   return HAL_OK;
435 }
436 
437 /**
438   * @brief  DeInitialize the CRYP peripheral.
439   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
440   *         the configuration information for CRYP module
441   * @retval HAL status
442   */
HAL_CRYP_DeInit(CRYP_HandleTypeDef * hcryp)443 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
444 {
445   /* Check the CRYP handle allocation */
446   if(hcryp == NULL)
447   {
448     return HAL_ERROR;
449   }
450 
451   /* Change the CRYP state */
452   hcryp->State = HAL_CRYP_STATE_BUSY;
453 
454   /* Set the default CRYP phase */
455   hcryp->Phase = HAL_CRYP_PHASE_READY;
456 
457   /* Reset CrypInCount and CrypOutCount */
458   hcryp->CrypInCount = 0;
459   hcryp->CrypOutCount = 0;
460 
461   /* Disable the CRYP Peripheral Clock */
462   __HAL_CRYP_DISABLE(hcryp);
463 
464 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
465     if(hcryp->MspDeInitCallback == NULL)
466     {
467       hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
468     }
469 
470     /* DeInit the low level hardware */
471     hcryp->MspDeInitCallback(hcryp);
472 #else
473   /* DeInit the low level hardware: CLOCK, NVIC.*/
474   HAL_CRYP_MspDeInit(hcryp);
475 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
476 
477   /* Change the CRYP state */
478   hcryp->State = HAL_CRYP_STATE_RESET;
479 
480   /* Release Lock */
481   __HAL_UNLOCK(hcryp);
482 
483   /* Return function status */
484   return HAL_OK;
485 }
486 
487 /**
488   * @brief  Initialize the CRYP MSP.
489   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
490   *         the configuration information for CRYP module
491   * @retval None
492   */
HAL_CRYP_MspInit(CRYP_HandleTypeDef * hcryp)493 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
494 {
495   /* Prevent unused argument(s) compilation warning */
496   UNUSED(hcryp);
497 
498   /* NOTE : This function should not be modified; when the callback is needed,
499             the HAL_CRYP_MspInit can be implemented in the user file
500    */
501 }
502 
503 /**
504   * @brief  DeInitialize CRYP MSP.
505   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
506   *         the configuration information for CRYP module
507   * @retval None
508   */
HAL_CRYP_MspDeInit(CRYP_HandleTypeDef * hcryp)509 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
510 {
511   /* Prevent unused argument(s) compilation warning */
512   UNUSED(hcryp);
513 
514   /* NOTE : This function should not be modified; when the callback is needed,
515             the HAL_CRYP_MspDeInit can be implemented in the user file
516    */
517 }
518 
519 /**
520   * @}
521   */
522 
523 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
524  *  @brief   Processing functions.
525  *
526 @verbatim
527   ==============================================================================
528                       ##### AES processing functions #####
529   ==============================================================================
530     [..]  This section provides functions allowing to:
531       (+) Encrypt plaintext using AES algorithm in different chaining modes
532       (+) Decrypt cyphertext using AES algorithm in different chaining modes
533     [..]  Three processing functions are available:
534       (+) Polling mode
535       (+) Interrupt mode
536       (+) DMA mode
537 
538 @endverbatim
539   * @{
540   */
541 
542 
543 /**
544   * @brief  Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData.
545   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
546   *         the configuration information for CRYP module
547   * @param  pPlainData Pointer to the plaintext buffer
548   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
549   * @param  pCypherData Pointer to the cyphertext buffer
550   * @param  Timeout Specify Timeout value
551   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
552   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
553   * @retval HAL status
554   */
HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData,uint32_t Timeout)555 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
556 {
557   /* Re-initialize AES IP with proper parameters */
558   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
559   {
560     return HAL_ERROR;
561   }
562   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
563   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
564   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
565   if (HAL_CRYP_Init(hcryp) != HAL_OK)
566   {
567     return HAL_ERROR;
568   }
569 
570   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
571 }
572 
573 
574 /**
575   * @brief  Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData.
576   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
577   *         the configuration information for CRYP module
578   * @param  pPlainData Pointer to the plaintext buffer
579   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
580   * @param  pCypherData Pointer to the cyphertext buffer
581   * @param  Timeout Specify Timeout value
582   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
583   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
584   * @retval HAL status
585   */
HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData,uint32_t Timeout)586 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
587 {
588   /* Re-initialize AES IP with proper parameters */
589   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
590   {
591     return HAL_ERROR;
592   }
593   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
594   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
595   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
596   if (HAL_CRYP_Init(hcryp) != HAL_OK)
597   {
598     return HAL_ERROR;
599   }
600 
601   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
602 }
603 
604 
605 /**
606   * @brief  Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData
607   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
608   *         the configuration information for CRYP module
609   * @param  pPlainData Pointer to the plaintext buffer
610   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
611   * @param  pCypherData Pointer to the cyphertext buffer
612   * @param  Timeout Specify Timeout value
613   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
614   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
615   * @retval HAL status
616   */
HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData,uint32_t Timeout)617 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
618 {
619   /* Re-initialize AES IP with proper parameters */
620   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
621   {
622     return HAL_ERROR;
623   }
624   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
625   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
626   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
627   if (HAL_CRYP_Init(hcryp) != HAL_OK)
628   {
629     return HAL_ERROR;
630   }
631 
632   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
633 }
634 
635 /**
636   * @brief  Decrypt pCypherData in AES ECB decryption mode with key derivation,
637   *         the decyphered data are available in pPlainData.
638   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
639   *         the configuration information for CRYP module
640   * @param  pCypherData Pointer to the cyphertext buffer
641   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
642   * @param  pPlainData Pointer to the plaintext buffer
643   * @param  Timeout Specify Timeout value
644   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
645   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
646   * @retval HAL status
647   */
HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData,uint32_t Timeout)648 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
649 {
650   /* Re-initialize AES IP with proper parameters */
651   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
652   {
653     return HAL_ERROR;
654   }
655   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
656   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
657   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
658   if (HAL_CRYP_Init(hcryp) != HAL_OK)
659   {
660     return HAL_ERROR;
661   }
662 
663   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
664 }
665 
666 /**
667   * @brief  Decrypt pCypherData in AES ECB decryption mode with key derivation,
668   *         the decyphered data are available in pPlainData.
669   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
670   *         the configuration information for CRYP module
671   * @param  pCypherData Pointer to the cyphertext buffer
672   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
673   * @param  pPlainData Pointer to the plaintext buffer
674   * @param  Timeout Specify Timeout value
675   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
676   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
677   * @retval HAL status
678   */
HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData,uint32_t Timeout)679 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
680 {
681   /* Re-initialize AES IP with proper parameters */
682   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
683   {
684     return HAL_ERROR;
685   }
686   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
687   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
688   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
689   if (HAL_CRYP_Init(hcryp) != HAL_OK)
690   {
691     return HAL_ERROR;
692   }
693 
694   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
695 }
696 
697 /**
698   * @brief  Decrypt pCypherData in AES CTR decryption mode,
699   *         the decyphered data are available in pPlainData.
700   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
701   *         the configuration information for CRYP module
702   * @param  pCypherData Pointer to the cyphertext buffer
703   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
704   * @param  pPlainData Pointer to the plaintext buffer
705   * @param  Timeout Specify Timeout value
706   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
707   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
708   * @retval HAL status
709   */
HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData,uint32_t Timeout)710 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
711 {
712   /* Re-initialize AES IP with proper parameters */
713   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
714   {
715     return HAL_ERROR;
716   }
717   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
718   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
719   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
720   if (HAL_CRYP_Init(hcryp) != HAL_OK)
721   {
722     return HAL_ERROR;
723   }
724 
725   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
726 }
727 
728 /**
729   * @brief  Encrypt pPlainData in AES ECB encryption mode using Interrupt,
730   *         the cypher data are available in pCypherData.
731   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
732   *         the configuration information for CRYP module
733   * @param  pPlainData Pointer to the plaintext buffer
734   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
735   * @param  pCypherData Pointer to the cyphertext buffer
736   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
737   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
738   * @retval HAL status
739   */
HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)740 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
741 {
742   /* Re-initialize AES IP with proper parameters */
743   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
744   {
745     return HAL_ERROR;
746   }
747   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
748   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
749   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
750   if (HAL_CRYP_Init(hcryp) != HAL_OK)
751   {
752     return HAL_ERROR;
753   }
754 
755   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
756 }
757 
758 /**
759   * @brief  Encrypt pPlainData in AES CBC encryption mode using Interrupt,
760   *         the cypher data are available in pCypherData.
761   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
762   *         the configuration information for CRYP module
763   * @param  pPlainData Pointer to the plaintext buffer
764   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
765   * @param  pCypherData Pointer to the cyphertext buffer
766   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
767   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
768   * @retval HAL status
769   */
HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)770 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
771 {
772   /* Re-initialize AES IP with proper parameters */
773   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
774   {
775     return HAL_ERROR;
776   }
777   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
778   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
779   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
780   if (HAL_CRYP_Init(hcryp) != HAL_OK)
781   {
782     return HAL_ERROR;
783   }
784 
785   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
786 }
787 
788 
789 /**
790   * @brief  Encrypt pPlainData in AES CTR encryption mode using Interrupt,
791   *         the cypher data are available in pCypherData.
792   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
793   *         the configuration information for CRYP module
794   * @param  pPlainData Pointer to the plaintext buffer
795   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
796   * @param  pCypherData Pointer to the cyphertext buffer
797   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
798   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
799   * @retval HAL status
800   */
HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)801 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
802 {
803   /* Re-initialize AES IP with proper parameters */
804   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
805   {
806     return HAL_ERROR;
807   }
808   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
809   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
810   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
811   if (HAL_CRYP_Init(hcryp) != HAL_OK)
812   {
813     return HAL_ERROR;
814   }
815 
816   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
817 }
818 
819 /**
820   * @brief  Decrypt pCypherData in AES ECB decryption mode using Interrupt,
821   *         the decyphered data are available in pPlainData.
822   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
823   *         the configuration information for CRYP module
824   * @param  pCypherData Pointer to the cyphertext buffer
825   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
826   * @param  pPlainData Pointer to the plaintext buffer.
827   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
828   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
829   * @retval HAL status
830   */
HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)831 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
832 {
833   /* Re-initialize AES IP with proper parameters */
834   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
835   {
836     return HAL_ERROR;
837   }
838   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
839   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
840   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
841   if (HAL_CRYP_Init(hcryp) != HAL_OK)
842   {
843     return HAL_ERROR;
844   }
845 
846   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
847 }
848 
849 /**
850   * @brief  Decrypt pCypherData in AES CBC decryption mode using Interrupt,
851   *         the decyphered data are available in pPlainData.
852   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
853   *         the configuration information for CRYP module
854   * @param  pCypherData Pointer to the cyphertext buffer
855   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
856   * @param  pPlainData Pointer to the plaintext buffer
857   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
858   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
859   * @retval HAL status
860   */
HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)861 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
862 {
863   /* Re-initialize AES IP with proper parameters */
864   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
865   {
866     return HAL_ERROR;
867   }
868   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
869   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
870   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
871   if (HAL_CRYP_Init(hcryp) != HAL_OK)
872   {
873     return HAL_ERROR;
874   }
875 
876   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
877 }
878 
879 /**
880   * @brief  Decrypt pCypherData in AES CTR decryption mode using Interrupt,
881   *         the decyphered data are available in pPlainData.
882   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
883   *         the configuration information for CRYP module
884   * @param  pCypherData Pointer to the cyphertext buffer
885   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
886   * @param  pPlainData Pointer to the plaintext buffer
887   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
888   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
889   * @retval HAL status
890   */
HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)891 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
892 {
893   /* Re-initialize AES IP with proper parameters */
894   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
895   {
896     return HAL_ERROR;
897   }
898   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
899   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
900   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
901   if (HAL_CRYP_Init(hcryp) != HAL_OK)
902   {
903     return HAL_ERROR;
904   }
905 
906   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
907 }
908 
909 /**
910   * @brief  Encrypt pPlainData in AES ECB encryption mode using DMA,
911   *         the cypher data are available in pCypherData.
912   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
913   *         the configuration information for CRYP module
914   * @param  pPlainData Pointer to the plaintext buffer
915   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
916   * @param  pCypherData Pointer to the cyphertext buffer
917   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
918   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
919   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
920   * @retval HAL status
921   */
HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)922 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
923 {
924   /* Re-initialize AES IP with proper parameters */
925   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
926   {
927     return HAL_ERROR;
928   }
929   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
930   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
931   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
932   if (HAL_CRYP_Init(hcryp) != HAL_OK)
933   {
934     return HAL_ERROR;
935   }
936 
937   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
938 }
939 
940 
941 
942 /**
943   * @brief  Encrypt pPlainData in AES CBC encryption mode using DMA,
944   *         the cypher data are available in pCypherData.
945   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
946   *         the configuration information for CRYP module
947   * @param  pPlainData Pointer to the plaintext buffer
948   * @param  Size Length of the plaintext buffer, must be a multiple of 16.
949   * @param  pCypherData Pointer to the cyphertext buffer
950   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
951   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
952   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
953   * @retval HAL status
954   */
HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)955 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
956 {
957   /* Re-initialize AES IP with proper parameters */
958   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
959   {
960     return HAL_ERROR;
961   }
962   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
963   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
964   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
965   if (HAL_CRYP_Init(hcryp) != HAL_OK)
966   {
967     return HAL_ERROR;
968   }
969 
970   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
971 }
972 
973 /**
974   * @brief  Encrypt pPlainData in AES CTR encryption mode using DMA,
975   *         the cypher data are available in pCypherData.
976   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
977   *         the configuration information for CRYP module
978   * @param  pPlainData Pointer to the plaintext buffer
979   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
980   * @param  pCypherData Pointer to the cyphertext buffer.
981   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
982   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
983   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
984   * @retval HAL status
985   */
HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)986 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
987 {
988   /* Re-initialize AES IP with proper parameters */
989   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
990   {
991     return HAL_ERROR;
992   }
993   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
994   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
995   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
996   if (HAL_CRYP_Init(hcryp) != HAL_OK)
997   {
998     return HAL_ERROR;
999   }
1000 
1001   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
1002 }
1003 
1004 /**
1005   * @brief  Decrypt pCypherData in AES ECB decryption mode using DMA,
1006   *         the decyphered data are available in pPlainData.
1007   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1008   *         the configuration information for CRYP module
1009   * @param  pCypherData Pointer to the cyphertext buffer
1010   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
1011   * @param  pPlainData Pointer to the plaintext buffer
1012   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
1013   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
1014   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
1015   * @retval HAL status
1016   */
HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)1017 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1018 {
1019   /* Re-initialize AES IP with proper parameters */
1020   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
1021   {
1022     return HAL_ERROR;
1023   }
1024   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
1025   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
1026   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
1027   if (HAL_CRYP_Init(hcryp) != HAL_OK)
1028   {
1029     return HAL_ERROR;
1030   }
1031 
1032   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
1033 }
1034 
1035 /**
1036   * @brief  Decrypt pCypherData in AES CBC decryption mode using DMA,
1037   *         the decyphered data are available in pPlainData.
1038   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1039   *         the configuration information for CRYP module
1040   * @param  pCypherData Pointer to the cyphertext buffer
1041   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
1042   * @param  pPlainData Pointer to the plaintext buffer
1043   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
1044   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
1045   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
1046   * @retval HAL status
1047   */
HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)1048 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1049 {
1050   /* Re-initialize AES IP with proper parameters */
1051   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
1052   {
1053     return HAL_ERROR;
1054   }
1055   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
1056   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
1057   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
1058   if (HAL_CRYP_Init(hcryp) != HAL_OK)
1059   {
1060     return HAL_ERROR;
1061   }
1062 
1063   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
1064 }
1065 
1066 /**
1067   * @brief  Decrypt pCypherData in AES CTR decryption mode using DMA,
1068   *         the decyphered data are available in pPlainData.
1069   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1070   *         the configuration information for CRYP module
1071   * @param  pCypherData Pointer to the cyphertext buffer
1072   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
1073   * @param  pPlainData Pointer to the plaintext buffer
1074   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
1075   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
1076   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
1077   * @retval HAL status
1078   */
HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)1079 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1080 {
1081   /* Re-initialize AES IP with proper parameters */
1082   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
1083   {
1084     return HAL_ERROR;
1085   }
1086   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
1087   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
1088   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
1089   if (HAL_CRYP_Init(hcryp) != HAL_OK)
1090   {
1091     return HAL_ERROR;
1092   }
1093 
1094   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
1095 }
1096 
1097 
1098 /**
1099   * @}
1100   */
1101 
1102 /** @defgroup CRYP_Exported_Functions_Group3 Callback functions
1103  *  @brief   Callback functions.
1104  *
1105 @verbatim
1106   ==============================================================================
1107                       ##### Callback functions  #####
1108   ==============================================================================
1109     [..]  This section provides Interruption and DMA callback functions:
1110       (+) DMA Input data transfer complete
1111       (+) DMA Output data transfer complete
1112       (+) DMA or Interrupt error
1113 
1114 @endverbatim
1115   * @{
1116   */
1117 
1118 /**
1119   * @brief  CRYP error callback.
1120   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1121   *         the configuration information for CRYP module
1122   * @retval None
1123   */
HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef * hcryp)1124 __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
1125 {
1126   /* Prevent unused argument(s) compilation warning */
1127   UNUSED(hcryp);
1128 
1129   /* NOTE : This function should not be modified; when the callback is needed,
1130             the HAL_CRYP_ErrorCallback can be implemented in the user file
1131    */
1132 }
1133 
1134 /**
1135   * @brief  Input DMA transfer complete callback.
1136   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1137   *         the configuration information for CRYP module
1138   * @retval None
1139   */
HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef * hcryp)1140 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
1141 {
1142   /* Prevent unused argument(s) compilation warning */
1143   UNUSED(hcryp);
1144 
1145   /* NOTE : This function should not be modified; when the callback is needed,
1146             the HAL_CRYP_InCpltCallback can be implemented in the user file
1147    */
1148 }
1149 
1150 /**
1151   * @brief  Output DMA transfer complete callback.
1152   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1153   *         the configuration information for CRYP module
1154   * @retval None
1155   */
HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef * hcryp)1156 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
1157 {
1158   /* Prevent unused argument(s) compilation warning */
1159   UNUSED(hcryp);
1160 
1161   /* NOTE : This function should not be modified; when the callback is needed,
1162             the HAL_CRYP_OutCpltCallback can be implemented in the user file
1163    */
1164 }
1165 
1166 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1167 /**
1168   * @brief  Register a User CRYP Callback
1169   *         To be used instead of the weak (surcharged) predefined callback
1170   * @param hcryp CRYP handle
1171   * @param CallbackID ID of the callback to be registered
1172   *        This parameter can be one of the following values:
1173   *          @arg @ref HAL_CRYP_INPUTCPLT_CB_ID CRYP input DMA transfer completion Callback ID
1174   *          @arg @ref HAL_CRYP_OUTPUTCPLT_CB_ID CRYP output DMA transfer completion Callback ID
1175   *          @arg @ref HAL_CRYP_COMPCPLT_CB_ID CRYP computation completion Callback ID
1176   *          @arg @ref HAL_CRYP_ERROR_CB_ID CRYP error callback ID
1177   *          @arg @ref HAL_CRYP_MSPINIT_CB_ID CRYP MspDeInit callback ID
1178   *          @arg @ref HAL_CRYP_MSPDEINIT_CB_ID CRYP MspDeInit callback ID
1179   * @param pCallback pointer to the Callback function
1180   * @retval status
1181   */
HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef * hcryp,HAL_CRYP_CallbackIDTypeDef CallbackID,pCRYP_CallbackTypeDef pCallback)1182 HAL_StatusTypeDef HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID, pCRYP_CallbackTypeDef pCallback)
1183 {
1184   HAL_StatusTypeDef status = HAL_OK;
1185 
1186   if(pCallback == NULL)
1187   {
1188     /* Update the error code */
1189     hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1190     return HAL_ERROR;
1191   }
1192   /* Process locked */
1193   __HAL_LOCK(hcryp);
1194 
1195   if(HAL_CRYP_STATE_READY == hcryp->State)
1196   {
1197     switch (CallbackID)
1198     {
1199     case HAL_CRYP_INPUTCPLT_CB_ID :
1200       hcryp->InCpltCallback = pCallback;
1201       break;
1202 
1203     case HAL_CRYP_OUTPUTCPLT_CB_ID :
1204       hcryp->OutCpltCallback = pCallback;
1205       break;
1206 
1207     case HAL_CRYP_COMPCPLT_CB_ID :
1208       hcryp->CompCpltCallback = pCallback;
1209       break;
1210 
1211     case HAL_CRYP_ERROR_CB_ID :
1212       hcryp->ErrorCallback = pCallback;
1213       break;
1214 
1215     case HAL_CRYP_MSPINIT_CB_ID :
1216       hcryp->MspInitCallback = pCallback;
1217       break;
1218 
1219     case HAL_CRYP_MSPDEINIT_CB_ID :
1220       hcryp->MspDeInitCallback = pCallback;
1221       break;
1222 
1223     default :
1224      /* Update the error code */
1225      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1226      /* update return status */
1227       status =  HAL_ERROR;
1228       break;
1229     }
1230   }
1231   else if(HAL_CRYP_STATE_RESET == hcryp->State)
1232   {
1233     switch (CallbackID)
1234     {
1235     case HAL_CRYP_MSPINIT_CB_ID :
1236       hcryp->MspInitCallback = pCallback;
1237       break;
1238 
1239     case HAL_CRYP_MSPDEINIT_CB_ID :
1240       hcryp->MspDeInitCallback = pCallback;
1241       break;
1242 
1243     default :
1244      /* Update the error code */
1245      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1246      /* update return status */
1247       status =  HAL_ERROR;
1248       break;
1249     }
1250   }
1251   else
1252   {
1253     /* Update the error code */
1254      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1255      /* update return status */
1256       status =  HAL_ERROR;
1257   }
1258 
1259   /* Release Lock */
1260   __HAL_UNLOCK(hcryp);
1261   return status;
1262 }
1263 
1264 /**
1265   * @brief  Unregister a CRYP Callback
1266   *         CRYP Callback is redirected to the weak (surcharged) predefined callback
1267   * @param hcryp CRYP handle
1268   * @param CallbackID ID of the callback to be unregistered
1269   *        This parameter can be one of the following values:
1270   *          @arg @ref HAL_CRYP_INPUTCPLT_CB_ID CRYP input DMA transfer completion Callback ID
1271   *          @arg @ref HAL_CRYP_OUTPUTCPLT_CB_ID CRYP output DMA transfer completion Callback ID
1272   *          @arg @ref HAL_CRYP_COMPCPLT_CB_ID CRYP computation completion Callback ID
1273   *          @arg @ref HAL_CRYP_ERROR_CB_ID CRYP error callback ID
1274   *          @arg @ref HAL_CRYP_MSPINIT_CB_ID CRYP MspDeInit callback ID
1275   *          @arg @ref HAL_CRYP_MSPDEINIT_CB_ID CRYP MspDeInit callback ID
1276   * @retval status
1277   */
HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef * hcryp,HAL_CRYP_CallbackIDTypeDef CallbackID)1278 HAL_StatusTypeDef HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID)
1279 {
1280 HAL_StatusTypeDef status = HAL_OK;
1281 
1282   /* Process locked */
1283   __HAL_LOCK(hcryp);
1284 
1285   if(HAL_CRYP_STATE_READY == hcryp->State)
1286   {
1287     switch (CallbackID)
1288     {
1289     case HAL_CRYP_INPUTCPLT_CB_ID :
1290       hcryp->InCpltCallback = HAL_CRYP_InCpltCallback;              /* Legacy weak (surcharged) input DMA transfer completion callback */
1291       break;
1292 
1293     case HAL_CRYP_OUTPUTCPLT_CB_ID :
1294       hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback;            /* Legacy weak (surcharged) output DMA transfer completion callback */
1295       break;
1296 
1297     case HAL_CRYP_COMPCPLT_CB_ID :
1298       hcryp->CompCpltCallback = HAL_CRYPEx_ComputationCpltCallback; /* Legacy weak (surcharged) computation completion callback */
1299       break;
1300 
1301     case HAL_CRYP_ERROR_CB_ID :
1302       hcryp->ErrorCallback = HAL_CRYP_ErrorCallback;                /* Legacy weak (surcharged) error callback */
1303       break;
1304 
1305     case HAL_CRYP_MSPINIT_CB_ID :
1306       hcryp->MspInitCallback = HAL_CRYP_MspInit;                    /* Legacy weak (surcharged) Msp DeInit */
1307       break;
1308 
1309     case HAL_CRYP_MSPDEINIT_CB_ID :
1310       hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;                /* Legacy weak (surcharged) Msp DeInit */
1311       break;
1312 
1313     default :
1314      /* Update the error code */
1315      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1316      /* update return status */
1317       status =  HAL_ERROR;
1318       break;
1319     }
1320   }
1321   else if(HAL_CRYP_STATE_RESET == hcryp->State)
1322   {
1323     switch (CallbackID)
1324     {
1325     case HAL_CRYP_MSPINIT_CB_ID :
1326       hcryp->MspInitCallback = HAL_CRYP_MspInit;           /* Legacy weak (surcharged) Msp Init */
1327       break;
1328 
1329     case HAL_CRYP_MSPDEINIT_CB_ID :
1330       hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;       /* Legacy weak (surcharged) Msp DeInit */
1331       break;
1332 
1333     default :
1334      /* Update the error code */
1335      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1336      /* update return status */
1337       status =  HAL_ERROR;
1338       break;
1339     }
1340   }
1341   else
1342   {
1343      /* Update the error code */
1344      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1345      /* update return status */
1346       status =  HAL_ERROR;
1347   }
1348 
1349   /* Release Lock */
1350   __HAL_UNLOCK(hcryp);
1351   return status;
1352 }
1353 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1354 
1355 /**
1356   * @}
1357   */
1358 
1359 /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
1360  *  @brief   AES IRQ handler.
1361  *
1362 @verbatim
1363   ==============================================================================
1364                 ##### AES IRQ handler management #####
1365   ==============================================================================
1366 [..]  This section provides AES IRQ handler function.
1367 
1368 @endverbatim
1369   * @{
1370   */
1371 
1372 /**
1373   * @brief  Handle AES interrupt request.
1374   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1375   *         the configuration information for CRYP module
1376   * @retval None
1377   */
HAL_CRYP_IRQHandler(CRYP_HandleTypeDef * hcryp)1378 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
1379 {
1380   /* Check if error occurred */
1381   if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_ERRIE) != RESET)
1382   {
1383     /* If Write Error occurred */
1384     if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_WRERR) != RESET)
1385     {
1386       hcryp->ErrorCode |= HAL_CRYP_WRITE_ERROR;
1387       hcryp->State = HAL_CRYP_STATE_ERROR;
1388     }
1389     /* If Read Error occurred */
1390     if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_RDERR) != RESET)
1391     {
1392       hcryp->ErrorCode |= HAL_CRYP_READ_ERROR;
1393       hcryp->State = HAL_CRYP_STATE_ERROR;
1394     }
1395 
1396     /* If an error has been reported */
1397     if (hcryp->State == HAL_CRYP_STATE_ERROR)
1398     {
1399       /* Disable Error and Computation Complete Interrupts */
1400       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
1401       /* Clear all Interrupt flags */
1402       __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR|CRYP_CCF_CLEAR);
1403 
1404       /* Process Unlocked */
1405       __HAL_UNLOCK(hcryp);
1406 
1407 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1408       hcryp->ErrorCallback(hcryp);
1409 #else
1410       HAL_CRYP_ErrorCallback(hcryp);
1411 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1412 
1413       return;
1414     }
1415 
1416   }
1417 
1418   /* Check if computation complete interrupt is enabled
1419      and if the computation complete flag is raised */
1420   if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_CCF) != RESET)
1421   {
1422     if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_CCFIE) != RESET)
1423     {
1424 #if defined(AES_CR_NPBLB)
1425       if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
1426        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
1427 #else
1428       if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
1429        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
1430 #endif
1431       {
1432        /* To ensure proper suspension requests management, CCF flag
1433           is reset in CRYP_AES_Auth_IT() according to the current
1434           phase under handling */
1435         if (CRYP_AES_Auth_IT(hcryp) !=  HAL_OK)
1436         {
1437 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1438           hcryp->ErrorCallback(hcryp);
1439 #else
1440           HAL_CRYP_ErrorCallback(hcryp);
1441 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1442         }
1443       }
1444       else
1445       {
1446         /* Clear Computation Complete Flag */
1447         __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
1448         if (CRYP_AES_IT(hcryp) !=  HAL_OK)
1449         {
1450 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1451           hcryp->ErrorCallback(hcryp);
1452 #else
1453           HAL_CRYP_ErrorCallback(hcryp);
1454 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1455         }
1456       }
1457     }
1458   }
1459 }
1460 
1461 /**
1462   * @}
1463   */
1464 
1465 /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
1466  *  @brief   Peripheral State functions.
1467  *
1468 @verbatim
1469   ==============================================================================
1470                       ##### Peripheral State functions #####
1471   ==============================================================================
1472     [..]
1473     This subsection permits to get in run-time the status of the peripheral.
1474 
1475 @endverbatim
1476   * @{
1477   */
1478 
1479 /**
1480   * @brief  Return the CRYP handle state.
1481   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1482   *         the configuration information for CRYP module
1483   * @retval HAL state
1484   */
HAL_CRYP_GetState(CRYP_HandleTypeDef * hcryp)1485 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
1486 {
1487   /* Return CRYP handle state */
1488   return hcryp->State;
1489 }
1490 
1491 /**
1492   * @brief  Return the CRYP peripheral error.
1493   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1494   *         the configuration information for CRYP module
1495   * @note   The returned error is a bit-map combination of possible errors
1496   * @retval Error bit-map
1497   */
HAL_CRYP_GetError(CRYP_HandleTypeDef * hcryp)1498 uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
1499 {
1500   return hcryp->ErrorCode;
1501 }
1502 
1503 /**
1504   * @}
1505   */
1506 
1507 /**
1508   * @}
1509   */
1510 
1511 /** @addtogroup CRYP_Private_Functions
1512   * @{
1513   */
1514 
1515 
1516 /**
1517   * @brief  Write the Key in KeyRx registers.
1518   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1519   *         the configuration information for CRYP module
1520   * @retval None
1521   */
CRYP_SetKey(CRYP_HandleTypeDef * hcryp)1522 static HAL_StatusTypeDef  CRYP_SetKey(CRYP_HandleTypeDef *hcryp)
1523 {
1524   uint32_t keyaddr;
1525 
1526   if (hcryp->Init.pKey == NULL)
1527   {
1528     return HAL_ERROR;
1529   }
1530 
1531 
1532   keyaddr = (uint32_t)(hcryp->Init.pKey);
1533 
1534   if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
1535   {
1536     hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
1537     keyaddr+=4U;
1538     hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
1539     keyaddr+=4U;
1540     hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
1541     keyaddr+=4U;
1542     hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
1543     keyaddr+=4U;
1544   }
1545 
1546   hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
1547   keyaddr+=4U;
1548   hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
1549   keyaddr+=4U;
1550   hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
1551   keyaddr+=4U;
1552   hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
1553 
1554   return HAL_OK;
1555 }
1556 
1557 /**
1558   * @brief  Write the InitVector/InitCounter in IVRx registers.
1559   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1560   *         the configuration information for CRYP module
1561   * @retval None
1562   */
CRYP_SetInitVector(CRYP_HandleTypeDef * hcryp)1563 static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp)
1564 {
1565   uint32_t ivaddr;
1566 
1567 #if !defined(AES_CR_NPBLB)
1568   if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
1569   {
1570     hcryp->Instance->IVR3 = 0;
1571     hcryp->Instance->IVR2 = 0;
1572     hcryp->Instance->IVR1 = 0;
1573     hcryp->Instance->IVR0 = 0;
1574   }
1575   else
1576 #endif
1577   {
1578     if (hcryp->Init.pInitVect == NULL)
1579     {
1580       return HAL_ERROR;
1581     }
1582 
1583     ivaddr = (uint32_t)(hcryp->Init.pInitVect);
1584 
1585     hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
1586     ivaddr+=4U;
1587     hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
1588     ivaddr+=4U;
1589     hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
1590     ivaddr+=4U;
1591     hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
1592   }
1593   return HAL_OK;
1594 }
1595 
1596 
1597 
1598 /**
1599   * @brief  Handle CRYP block input/output data handling under interruption.
1600   * @note   The function is called under interruption only, once
1601   *         interruptions have been enabled by HAL_CRYPEx_AES_IT().
1602   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
1603   *         the configuration information for CRYP module.
1604   * @retval HAL status
1605   */
CRYP_AES_IT(CRYP_HandleTypeDef * hcryp)1606 static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
1607 {
1608   uint32_t inputaddr;
1609   uint32_t outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
1610 
1611   if(hcryp->State == HAL_CRYP_STATE_BUSY)
1612   {
1613     if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
1614     {
1615       /* Read the last available output block from the Data Output Register */
1616       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1617       outputaddr+=4U;
1618       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1619       outputaddr+=4U;
1620       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1621       outputaddr+=4U;
1622       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1623       hcryp->pCrypOutBuffPtr += 16;
1624       hcryp->CrypOutCount -= 16U;
1625 
1626     }
1627     else
1628     {
1629       /* Read the derived key from the Key registers */
1630       if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
1631       {
1632         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
1633         outputaddr+=4U;
1634         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
1635         outputaddr+=4U;
1636         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
1637         outputaddr+=4U;
1638         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
1639         outputaddr+=4U;
1640       }
1641 
1642         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
1643         outputaddr+=4U;
1644         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
1645         outputaddr+=4U;
1646         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
1647         outputaddr+=4U;
1648         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
1649     }
1650 
1651     /* In case of ciphering or deciphering, check if all output text has been retrieved;
1652        In case of key derivation, stop right there */
1653     if ((hcryp->CrypOutCount == 0U) || (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION))
1654     {
1655       /* Disable Computation Complete Flag and Errors Interrupts */
1656       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
1657       /* Change the CRYP state */
1658       hcryp->State = HAL_CRYP_STATE_READY;
1659 
1660      /* Process Unlocked */
1661       __HAL_UNLOCK(hcryp);
1662 
1663       /* Call computation complete callback */
1664 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1665       hcryp->CompCpltCallback(hcryp);
1666 #else
1667       HAL_CRYPEx_ComputationCpltCallback(hcryp);
1668 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1669 
1670       return HAL_OK;
1671     }
1672     /* If suspension flag has been raised, suspend processing */
1673     else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
1674     {
1675       /* reset ModeSuspend */
1676       hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
1677 
1678       /* Disable Computation Complete Flag and Errors Interrupts */
1679       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
1680       /* Change the CRYP state */
1681       hcryp->State = HAL_CRYP_STATE_SUSPENDED;
1682 
1683      /* Process Unlocked */
1684       __HAL_UNLOCK(hcryp);
1685 
1686       return HAL_OK;
1687     }
1688     else /* Process the rest of input data */
1689     {
1690       /* Get the Intput data address */
1691       inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1692 
1693       /* Increment/decrement instance pointer/counter */
1694       hcryp->pCrypInBuffPtr += 16;
1695       hcryp->CrypInCount -= 16U;
1696 
1697       /* Write the next input block in the Data Input register */
1698       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1699       inputaddr+=4U;
1700       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1701       inputaddr+=4U;
1702       hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
1703       inputaddr+=4U;
1704       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1705 
1706       return HAL_OK;
1707     }
1708   }
1709   else
1710   {
1711     return HAL_BUSY;
1712   }
1713 }
1714 
1715 
1716 
1717 
1718 /**
1719   * @}
1720   */
1721 
1722 
1723 
1724 /**
1725   * @}
1726   */
1727 
1728 /**
1729   * @}
1730   */
1731 
1732 #endif /* AES */
1733 
1734 #endif /* HAL_CRYP_MODULE_ENABLED */
1735