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