STM32L443xx HAL User Manual
stm32l4xx_hal_cryp.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_cryp.c
00004   * @author  MCD Application Team
00005   * @brief   CRYP HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Cryptography (CRYP) peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + Processing functions using polling mode
00010   *           + Processing functions using interrupt mode
00011   *           + Processing functions using DMA mode
00012   *           + Peripheral State functions
00013   *
00014   ******************************************************************************
00015   * @attention
00016   *
00017   * Copyright (c) 2017 STMicroelectronics.
00018   * All rights reserved.
00019   *
00020   * This software is licensed under terms that can be found in the LICENSE file in
00021   * the root directory of this software component.
00022   * If no LICENSE file comes with this software, it is provided AS-IS.
00023   ******************************************************************************
00024   @verbatim
00025   ==============================================================================
00026                      ##### How to use this driver #####
00027   ==============================================================================
00028     [..]
00029       The CRYP HAL driver can be used as follows:
00030 
00031       (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
00032          (++) Enable the CRYP interface clock using __HAL_RCC_AES_CLK_ENABLE()
00033          (++) In case of using interrupts (e.g. HAL_CRYP_AES_IT())
00034              (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
00035              (+++) Enable the AES IRQ handler using HAL_NVIC_EnableIRQ()
00036              (+++) In AES IRQ handler, call HAL_CRYP_IRQHandler()
00037          (++) In case of using DMA to control data transfer (e.g. HAL_CRYPEx_AES_DMA())
00038              (+++) Enable the DMA2 interface clock using
00039                  __HAL_RCC_DMA2_CLK_ENABLE()
00040              (+++) Configure and enable two DMA channels one for managing data transfer from
00041                  memory to peripheral (input channel) and another channel for managing data
00042                  transfer from peripheral to memory (output channel)
00043              (+++) Associate the initialized DMA handle to the CRYP DMA handle
00044                  using __HAL_LINKDMA()
00045              (+++) Configure the priority and enable the NVIC for the transfer complete
00046                  interrupt on the two DMA channels. The output channel should have higher
00047                  priority than the input channel.
00048                  Resort to HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
00049 
00050       (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures:
00051          (++) The data type: 1-bit, 8-bit, 16-bit and 32-bit
00052          (++) The AES operating mode (encryption, key derivation and/or decryption)
00053          (++) The AES chaining mode (ECB, CBC, CTR, GCM, GMAC, CMAC when applicable, CCM when applicable)
00054          (++) The encryption/decryption key if so required
00055          (++) The initialization vector or nonce if applicable (not used in ECB mode).
00056 
00057       (#)Three processing (encryption/decryption) functions are available:
00058          (++) Polling mode: encryption and decryption APIs are blocking functions
00059               i.e. they process the data and wait till the processing is finished
00060          (++) Interrupt mode: encryption and decryption APIs are not blocking functions
00061               i.e. they process the data under interrupt
00062          (++) DMA mode: encryption and decryption APIs are not blocking functions
00063               i.e. the data transfer is ensured by DMA
00064 
00065        (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
00066 
00067      *** Callback registration ***
00068      ===================================
00069      [..]
00070       (#) The compilation define  USE_HAL_CRYP_REGISTER_CALLBACKS when set to 1
00071           allows the user to configure dynamically the driver callbacks.
00072           Use function @ref HAL_CRYP_RegisterCallback() to register a user callback.
00073 
00074       (#) Function @ref HAL_CRYP_RegisterCallback() allows to register following callbacks:
00075             (+) InCpltCallback : callback for input DMA transfer completion.
00076             (+) OutCpltCallback : callback for output DMA transfer completion.
00077             (+) CompCpltCallback : callback for computation completion.
00078             (+) ErrorCallback : callback for error.
00079             (+) MspInitCallback    : CRYP MspInit.
00080             (+) MspDeInitCallback  : CRYP MspDeInit.
00081           This function takes as parameters the HAL peripheral handle, the Callback ID
00082           and a pointer to the user callback function.
00083 
00084       (#) Use function @ref HAL_CRYP_UnRegisterCallback() to reset a callback to the default
00085           weak (surcharged) function.
00086           @ref HAL_CRYP_UnRegisterCallback() takes as parameters the HAL peripheral handle,
00087           and the Callback ID.
00088           This function allows to reset following callbacks:
00089             (+) InCpltCallback : callback for input DMA transfer completion.
00090             (+) OutCpltCallback : callback for output DMA transfer completion.
00091             (+) CompCpltCallback : callback for computation completion.
00092             (+) ErrorCallback : callback for error.
00093             (+) MspInitCallback    : CRYP MspInit.
00094             (+) MspDeInitCallback  : CRYP MspDeInit.
00095 
00096       (#) By default, after the @ref HAL_CRYP_Init and if the state is HAL_CRYP_STATE_RESET
00097           all callbacks are reset to the corresponding legacy weak (surcharged) functions:
00098           examples @ref HAL_CRYP_InCpltCallback(), @ref HAL_CRYP_ErrorCallback()
00099           Exception done for MspInit and MspDeInit callbacks that are respectively
00100           reset to the legacy weak (surcharged) functions in the @ref HAL_CRYP_Init
00101           and @ref HAL_CRYP_DeInit only when these callbacks are null (not registered beforehand)
00102           If not, MspInit or MspDeInit are not null, the @ref HAL_CRYP_Init and @ref HAL_CRYP_DeInit
00103           keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
00104 
00105           Callbacks can be registered/unregistered in READY state only.
00106           Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
00107           in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
00108           during the Init/DeInit.
00109           In that case first register the MspInit/MspDeInit user callbacks
00110           using @ref HAL_CRYP_RegisterCallback before calling @ref HAL_CRYP_DeInit
00111           or @ref HAL_¨CRYP_Init function.
00112 
00113           When The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS is set to 0 or
00114           not defined, the callback registering feature is not available
00115           and weak (surcharged) callbacks are used.
00116 
00117 
00118   @endverbatim
00119   ******************************************************************************
00120   */
00121 
00122 /* Includes ------------------------------------------------------------------*/
00123 #include "stm32l4xx_hal.h"
00124 
00125 #ifdef HAL_CRYP_MODULE_ENABLED
00126 
00127 #if defined(AES)
00128 
00129 /** @addtogroup STM32L4xx_HAL_Driver
00130   * @{
00131   */
00132 
00133 /** @defgroup CRYP CRYP
00134   * @brief CRYP HAL module driver.
00135   * @{
00136   */
00137 
00138 
00139 
00140 /* Private typedef -----------------------------------------------------------*/
00141 /* Private define ------------------------------------------------------------*/
00142 /* Private macro -------------------------------------------------------------*/
00143 /* Private variables ---------------------------------------------------------*/
00144 /* Private functions --------------------------------------------------------*/
00145 
00146 /** @defgroup CRYP_Private_Functions CRYP Private Functions
00147   * @{
00148   */
00149 
00150 static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp);
00151 static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp);
00152 static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
00153 
00154 /**
00155   * @}
00156   */
00157 
00158 /* Exported functions ---------------------------------------------------------*/
00159 
00160 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
00161   * @{
00162   */
00163 
00164 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions
00165  *  @brief    Initialization and Configuration functions.
00166  *
00167 @verbatim
00168   ==============================================================================
00169               ##### Initialization and deinitialization functions #####
00170   ==============================================================================
00171     [..]  This section provides functions allowing to:
00172       (+) Initialize the CRYP according to the specified parameters
00173           in the CRYP_InitTypeDef and creates the associated handle
00174       (+) DeInitialize the CRYP peripheral
00175       (+) Initialize the CRYP MSP (MCU Specific Package)
00176       (+) De-Initialize the CRYP MSP
00177 
00178     [..]
00179     (@) Specific care must be taken to format the key and the Initialization Vector IV!
00180 
00181    [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where
00182         b127 is the MSB and b0 the LSB, the key must be stored in MCU memory
00183         (+) as a sequence of words where the MSB word comes first (occupies the
00184           lowest memory address)
00185         (+) where each word is byte-swapped:
00186          (++)   address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120
00187          (++)   address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88
00188          (++)   address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56
00189          (++)   address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24
00190     [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}.
00191         The 4 32-bit words that make the key must be stored as follows in MCU memory:
00192          (+)    address n+0 : 0x B12 B13 B14 B15
00193          (+)    address n+4 : 0x B8 B9 B10 B11
00194          (+)    address n+8 : 0x B4 B5 B6 B7
00195          (+)    address n+C : 0x B0 B1 B2 B3
00196     [..]  which leads to the expected setting
00197       (+)       AES_KEYR3 = 0x B15 B14 B13 B12
00198       (+)       AES_KEYR2 = 0x B11 B10 B9 B8
00199       (+)       AES_KEYR1 = 0x B7 B6 B5 B4
00200       (+)       AES_KEYR0 = 0x B3 B2 B1 B0
00201 
00202    [..]  Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}.
00203          The 8 32-bit words that make the key must be stored as follows in MCU memory:
00204          (+)    address n+00 : 0x B28 B29 B30 B31
00205          (+)    address n+04 : 0x B24 B25 B26 B27
00206          (+)    address n+08 : 0x B20 B21 B22 B23
00207          (+)    address n+0C : 0x B16 B17 B18 B19
00208          (+)    address n+10 : 0x B12 B13 B14 B15
00209          (+)    address n+14 : 0x B8 B9 B10 B11
00210          (+)    address n+18 : 0x B4 B5 B6 B7
00211          (+)    address n+1C : 0x B0 B1 B2 B3
00212     [..]  which leads to the expected setting
00213       (+)       AES_KEYR7 = 0x B31 B30 B29 B28
00214       (+)       AES_KEYR6 = 0x B27 B26 B25 B24
00215       (+)       AES_KEYR5 = 0x B23 B22 B21 B20
00216       (+)       AES_KEYR4 = 0x B19 B18 B17 B16
00217       (+)       AES_KEYR3 = 0x B15 B14 B13 B12
00218       (+)       AES_KEYR2 = 0x B11 B10 B9 B8
00219       (+)       AES_KEYR1 = 0x B7 B6 B5 B4
00220       (+)       AES_KEYR0 = 0x B3 B2 B1 B0
00221 
00222    [..] Initialization Vector IV (4 32-bit words) format must follow the same as
00223         that of a 128-bit long key.
00224 
00225   [..]
00226 
00227 @endverbatim
00228   * @{
00229   */
00230 
00231 /**
00232   * @brief  Initialize the CRYP according to the specified
00233   *         parameters in the CRYP_InitTypeDef and initialize the associated handle.
00234   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00235   *         the configuration information for CRYP module  
00236   * @note Specific care must be taken to format the key and the Initialization Vector IV
00237   *       stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations
00238   *       hereabove.
00239   * @retval HAL status
00240   */
00241 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
00242 {
00243   /* Check the CRYP handle allocation */
00244   if(hcryp == NULL)
00245   {
00246     return HAL_ERROR;
00247   }
00248 
00249   /* Check the instance */
00250   assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance));
00251 
00252   /* Check the parameters */
00253   assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
00254   assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
00255   assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode));
00256   /* ChainingMode parameter is irrelevant when mode is set to Key derivation */
00257   if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
00258   {
00259     assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode));
00260   }
00261   assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag));
00262 
00263   /*========================================================*/
00264   /* Check the proper operating/chaining modes combinations */
00265   /*========================================================*/
00266   /* Check the proper chaining when the operating mode is key derivation and decryption */
00267 #if defined(AES_CR_NPBLB)
00268   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
00269          ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR)           \
00270        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)      \
00271        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)))
00272 #else
00273   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
00274          ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR)           \
00275        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)      \
00276        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)))
00277 #endif
00278   {
00279     return HAL_ERROR;
00280   }
00281   /* Check that key derivation is not set in CMAC mode or CCM mode when applicable */
00282 #if defined(AES_CR_NPBLB)
00283   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
00284    && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
00285 #else
00286   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
00287    && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
00288 #endif
00289   {
00290     return HAL_ERROR;
00291   }
00292 
00293 
00294   /*================*/
00295   /* Initialization */
00296   /*================*/
00297   /* Initialization start */
00298 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
00299   if (hcryp->State == HAL_CRYP_STATE_RESET)
00300   {
00301     /* Allocate lock resource and initialize it */
00302     hcryp->Lock = HAL_UNLOCKED;
00303 
00304     /* Reset Callback pointers in HAL_CRYP_STATE_RESET only */
00305     hcryp->InCpltCallback   =  HAL_CRYP_InCpltCallback;            /* Legacy weak (surcharged) input DMA transfer completion callback */
00306     hcryp->OutCpltCallback   =  HAL_CRYP_OutCpltCallback;           /* Legacy weak (surcharged) output DMA transfer completion callback */
00307     hcryp->CompCpltCallback =  HAL_CRYPEx_ComputationCpltCallback; /* Legacy weak (surcharged) computation completion callback */
00308     hcryp->ErrorCallback    =  HAL_CRYP_ErrorCallback;             /* Legacy weak (surcharged) error callback */
00309     if(hcryp->MspInitCallback == NULL)
00310     {
00311       hcryp->MspInitCallback = HAL_CRYP_MspInit;
00312     }
00313 
00314     /* Init the low level hardware */
00315     hcryp->MspInitCallback(hcryp);
00316   }
00317 #else
00318   if(hcryp->State == HAL_CRYP_STATE_RESET)
00319   {
00320     /* Allocate lock resource and initialize it */
00321     hcryp->Lock = HAL_UNLOCKED;
00322 
00323     /* Init the low level hardware */
00324     HAL_CRYP_MspInit(hcryp);
00325   }
00326 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
00327 
00328   /* Change the CRYP state */
00329   hcryp->State = HAL_CRYP_STATE_BUSY;
00330 
00331   /* Disable the Peripheral */
00332   __HAL_CRYP_DISABLE(hcryp);
00333 
00334   /*=============================================================*/
00335   /* AES initialization common to all operating modes            */
00336   /*=============================================================*/
00337   /* Set the Key size selection */
00338   MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize);
00339 
00340   /* Set the default CRYP phase when this parameter is not used.
00341      Phase is updated below in case of GCM/GMAC(/CMAC)(/CCM) setting. */
00342   hcryp->Phase = HAL_CRYP_PHASE_NOT_USED;
00343 
00344 
00345 
00346   /*=============================================================*/
00347   /* Carry on the initialization based on the AES operating mode */
00348   /*=============================================================*/
00349   /* Key derivation */
00350   if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
00351   {
00352     MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION);
00353 
00354     /* Configure the Key registers */
00355     if (CRYP_SetKey(hcryp) != HAL_OK)
00356     {
00357       return HAL_ERROR;
00358     }
00359   }
00360   else
00361   /* Encryption / Decryption (with or without key derivation) / authentication */
00362   {
00363 #if !defined(AES_CR_NPBLB)
00364     /* Set data type, operating and chaining modes.
00365        In case of GCM or GMAC, data type is forced to 0b00 */
00366     if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
00367     {
00368       MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
00369     }
00370     else
00371 #endif
00372     {
00373       MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
00374     }
00375 
00376 
00377    /* Specify the encryption/decryption phase in case of Galois counter mode (GCM),
00378       Galois message authentication code (GMAC), cipher message authentication code (CMAC) when applicable
00379       or Counter with Cipher Mode (CCM) when applicable */
00380 #if defined(AES_CR_NPBLB)
00381    if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
00382     || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
00383 #else
00384    if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
00385     || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
00386 #endif
00387     {
00388       MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase);
00389       hcryp->Phase = HAL_CRYP_PHASE_START;
00390     }
00391 
00392 
00393     /* Configure the Key registers if no need to bypass this step */
00394     if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE)
00395     {
00396       if (CRYP_SetKey(hcryp) != HAL_OK)
00397       {
00398         return HAL_ERROR;
00399       }
00400     }
00401 
00402     /* If applicable, configure the Initialization Vector */
00403     if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB)
00404     {
00405       if (CRYP_SetInitVector(hcryp) != HAL_OK)
00406       {
00407         return HAL_ERROR;
00408       }
00409     }
00410   }
00411 
00412 #if defined(AES_CR_NPBLB)
00413   /* Clear NPBLB field */
00414   CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
00415 #endif
00416 
00417   /* Reset CrypInCount and CrypOutCount */
00418   hcryp->CrypInCount = 0;
00419   hcryp->CrypOutCount = 0;
00420 
00421   /* Reset ErrorCode field */
00422   hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
00423 
00424   /* Reset Mode suspension request */
00425   hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
00426 
00427   /* Change the CRYP state */
00428   hcryp->State = HAL_CRYP_STATE_READY;
00429 
00430   /* Enable the Peripheral */
00431   __HAL_CRYP_ENABLE(hcryp);
00432 
00433   /* Return function status */
00434   return HAL_OK;
00435 }
00436 
00437 /**
00438   * @brief  DeInitialize the CRYP peripheral.
00439   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00440   *         the configuration information for CRYP module
00441   * @retval HAL status
00442   */
00443 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
00444 {
00445   /* Check the CRYP handle allocation */
00446   if(hcryp == NULL)
00447   {
00448     return HAL_ERROR;
00449   }
00450 
00451   /* Change the CRYP state */
00452   hcryp->State = HAL_CRYP_STATE_BUSY;
00453 
00454   /* Set the default CRYP phase */
00455   hcryp->Phase = HAL_CRYP_PHASE_READY;
00456 
00457   /* Reset CrypInCount and CrypOutCount */
00458   hcryp->CrypInCount = 0;
00459   hcryp->CrypOutCount = 0;
00460 
00461   /* Disable the CRYP Peripheral Clock */
00462   __HAL_CRYP_DISABLE(hcryp);
00463 
00464 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
00465     if(hcryp->MspDeInitCallback == NULL)
00466     {
00467       hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
00468     }
00469 
00470     /* DeInit the low level hardware */
00471     hcryp->MspDeInitCallback(hcryp);
00472 #else
00473   /* DeInit the low level hardware: CLOCK, NVIC.*/
00474   HAL_CRYP_MspDeInit(hcryp);
00475 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
00476 
00477   /* Change the CRYP state */
00478   hcryp->State = HAL_CRYP_STATE_RESET;
00479 
00480   /* Release Lock */
00481   __HAL_UNLOCK(hcryp);
00482 
00483   /* Return function status */
00484   return HAL_OK;
00485 }
00486 
00487 /**
00488   * @brief  Initialize the CRYP MSP.
00489   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00490   *         the configuration information for CRYP module
00491   * @retval None
00492   */
00493 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
00494 {
00495   /* Prevent unused argument(s) compilation warning */
00496   UNUSED(hcryp);
00497 
00498   /* NOTE : This function should not be modified; when the callback is needed,
00499             the HAL_CRYP_MspInit can be implemented in the user file
00500    */
00501 }
00502 
00503 /**
00504   * @brief  DeInitialize CRYP MSP.
00505   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00506   *         the configuration information for CRYP module
00507   * @retval None
00508   */
00509 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
00510 {
00511   /* Prevent unused argument(s) compilation warning */
00512   UNUSED(hcryp);
00513 
00514   /* NOTE : This function should not be modified; when the callback is needed,
00515             the HAL_CRYP_MspDeInit can be implemented in the user file
00516    */
00517 }
00518 
00519 /**
00520   * @}
00521   */
00522 
00523 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
00524  *  @brief   Processing functions.
00525  *
00526 @verbatim
00527   ==============================================================================
00528                       ##### AES processing functions #####
00529   ==============================================================================
00530     [..]  This section provides functions allowing to:
00531       (+) Encrypt plaintext using AES algorithm in different chaining modes
00532       (+) Decrypt cyphertext using AES algorithm in different chaining modes
00533     [..]  Three processing functions are available:
00534       (+) Polling mode
00535       (+) Interrupt mode
00536       (+) DMA mode
00537 
00538 @endverbatim
00539   * @{
00540   */
00541 
00542 
00543 /**
00544   * @brief  Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData.
00545   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00546   *         the configuration information for CRYP module
00547   * @param  pPlainData Pointer to the plaintext buffer
00548   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00549   * @param  pCypherData Pointer to the cyphertext buffer
00550   * @param  Timeout Specify Timeout value
00551   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00552   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
00553   * @retval HAL status
00554   */
00555 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
00556 {
00557   /* Re-initialize AES IP with proper parameters */
00558   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00559   {
00560     return HAL_ERROR;
00561   }
00562   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00563   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00564   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00565   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00566   {
00567     return HAL_ERROR;
00568   }
00569 
00570   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
00571 }
00572 
00573 
00574 /**
00575   * @brief  Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData.
00576   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00577   *         the configuration information for CRYP module
00578   * @param  pPlainData Pointer to the plaintext buffer
00579   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00580   * @param  pCypherData Pointer to the cyphertext buffer
00581   * @param  Timeout Specify Timeout value
00582   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00583   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
00584   * @retval HAL status
00585   */
00586 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
00587 {
00588   /* Re-initialize AES IP with proper parameters */
00589   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00590   {
00591     return HAL_ERROR;
00592   }
00593   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00594   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00595   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00596   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00597   {
00598     return HAL_ERROR;
00599   }
00600 
00601   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
00602 }
00603 
00604 
00605 /**
00606   * @brief  Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData
00607   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00608   *         the configuration information for CRYP module
00609   * @param  pPlainData Pointer to the plaintext buffer
00610   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00611   * @param  pCypherData Pointer to the cyphertext buffer
00612   * @param  Timeout Specify Timeout value
00613   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00614   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
00615   * @retval HAL status
00616   */
00617 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
00618 {
00619   /* Re-initialize AES IP with proper parameters */
00620   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00621   {
00622     return HAL_ERROR;
00623   }
00624   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00625   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00626   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00627   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00628   {
00629     return HAL_ERROR;
00630   }
00631 
00632   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
00633 }
00634 
00635 /**
00636   * @brief  Decrypt pCypherData in AES ECB decryption mode with key derivation,
00637   *         the decyphered data are available in pPlainData.
00638   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00639   *         the configuration information for CRYP module
00640   * @param  pCypherData Pointer to the cyphertext buffer
00641   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00642   * @param  pPlainData Pointer to the plaintext buffer
00643   * @param  Timeout Specify Timeout value
00644   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00645   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
00646   * @retval HAL status
00647   */
00648 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
00649 {
00650   /* Re-initialize AES IP with proper parameters */
00651   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00652   {
00653     return HAL_ERROR;
00654   }
00655   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00656   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00657   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00658   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00659   {
00660     return HAL_ERROR;
00661   }
00662 
00663   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
00664 }
00665 
00666 /**
00667   * @brief  Decrypt pCypherData in AES ECB decryption mode with key derivation,
00668   *         the decyphered data are available in pPlainData.
00669   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00670   *         the configuration information for CRYP module
00671   * @param  pCypherData Pointer to the cyphertext buffer
00672   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00673   * @param  pPlainData Pointer to the plaintext buffer
00674   * @param  Timeout Specify Timeout value
00675   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00676   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
00677   * @retval HAL status
00678   */
00679 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
00680 {
00681   /* Re-initialize AES IP with proper parameters */
00682   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00683   {
00684     return HAL_ERROR;
00685   }
00686   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00687   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00688   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00689   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00690   {
00691     return HAL_ERROR;
00692   }
00693 
00694   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
00695 }
00696 
00697 /**
00698   * @brief  Decrypt pCypherData in AES CTR decryption mode,
00699   *         the decyphered data are available in pPlainData.
00700   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00701   *         the configuration information for CRYP module
00702   * @param  pCypherData Pointer to the cyphertext buffer
00703   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00704   * @param  pPlainData Pointer to the plaintext buffer
00705   * @param  Timeout Specify Timeout value
00706   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00707   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
00708   * @retval HAL status
00709   */
00710 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
00711 {
00712   /* Re-initialize AES IP with proper parameters */
00713   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00714   {
00715     return HAL_ERROR;
00716   }
00717   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
00718   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00719   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00720   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00721   {
00722     return HAL_ERROR;
00723   }
00724 
00725   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
00726 }
00727 
00728 /**
00729   * @brief  Encrypt pPlainData in AES ECB encryption mode using Interrupt,
00730   *         the cypher data are available in pCypherData.
00731   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00732   *         the configuration information for CRYP module
00733   * @param  pPlainData Pointer to the plaintext buffer
00734   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00735   * @param  pCypherData Pointer to the cyphertext buffer
00736   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00737   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
00738   * @retval HAL status
00739   */
00740 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00741 {
00742   /* Re-initialize AES IP with proper parameters */
00743   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00744   {
00745     return HAL_ERROR;
00746   }
00747   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00748   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00749   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00750   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00751   {
00752     return HAL_ERROR;
00753   }
00754 
00755   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
00756 }
00757 
00758 /**
00759   * @brief  Encrypt pPlainData in AES CBC encryption mode using Interrupt,
00760   *         the cypher data are available in pCypherData.
00761   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00762   *         the configuration information for CRYP module
00763   * @param  pPlainData Pointer to the plaintext buffer
00764   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00765   * @param  pCypherData Pointer to the cyphertext buffer
00766   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00767   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
00768   * @retval HAL status
00769   */
00770 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00771 {
00772   /* Re-initialize AES IP with proper parameters */
00773   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00774   {
00775     return HAL_ERROR;
00776   }
00777   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00778   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00779   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00780   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00781   {
00782     return HAL_ERROR;
00783   }
00784 
00785   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
00786 }
00787 
00788 
00789 /**
00790   * @brief  Encrypt pPlainData in AES CTR encryption mode using Interrupt,
00791   *         the cypher data are available in pCypherData.
00792   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00793   *         the configuration information for CRYP module
00794   * @param  pPlainData Pointer to the plaintext buffer
00795   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00796   * @param  pCypherData Pointer to the cyphertext buffer
00797   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00798   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
00799   * @retval HAL status
00800   */
00801 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00802 {
00803   /* Re-initialize AES IP with proper parameters */
00804   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00805   {
00806     return HAL_ERROR;
00807   }
00808   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00809   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00810   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00811   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00812   {
00813     return HAL_ERROR;
00814   }
00815 
00816   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
00817 }
00818 
00819 /**
00820   * @brief  Decrypt pCypherData in AES ECB decryption mode using Interrupt,
00821   *         the decyphered data are available in pPlainData.
00822   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00823   *         the configuration information for CRYP module
00824   * @param  pCypherData Pointer to the cyphertext buffer
00825   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00826   * @param  pPlainData Pointer to the plaintext buffer.
00827   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00828   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
00829   * @retval HAL status
00830   */
00831 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00832 {
00833   /* Re-initialize AES IP with proper parameters */
00834   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00835   {
00836     return HAL_ERROR;
00837   }
00838   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00839   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00840   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00841   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00842   {
00843     return HAL_ERROR;
00844   }
00845 
00846   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
00847 }
00848 
00849 /**
00850   * @brief  Decrypt pCypherData in AES CBC decryption mode using Interrupt,
00851   *         the decyphered data are available in pPlainData.
00852   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00853   *         the configuration information for CRYP module
00854   * @param  pCypherData Pointer to the cyphertext buffer
00855   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00856   * @param  pPlainData Pointer to the plaintext buffer
00857   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00858   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
00859   * @retval HAL status
00860   */
00861 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00862 {
00863   /* Re-initialize AES IP with proper parameters */
00864   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00865   {
00866     return HAL_ERROR;
00867   }
00868   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00869   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00870   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00871   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00872   {
00873     return HAL_ERROR;
00874   }
00875 
00876   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
00877 }
00878 
00879 /**
00880   * @brief  Decrypt pCypherData in AES CTR decryption mode using Interrupt,
00881   *         the decyphered data are available in pPlainData.
00882   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00883   *         the configuration information for CRYP module
00884   * @param  pCypherData Pointer to the cyphertext buffer
00885   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00886   * @param  pPlainData Pointer to the plaintext buffer
00887   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00888   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
00889   * @retval HAL status
00890   */
00891 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00892 {
00893   /* Re-initialize AES IP with proper parameters */
00894   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00895   {
00896     return HAL_ERROR;
00897   }
00898   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
00899   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00900   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00901   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00902   {
00903     return HAL_ERROR;
00904   }
00905 
00906   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
00907 }
00908 
00909 /**
00910   * @brief  Encrypt pPlainData in AES ECB encryption mode using DMA,
00911   *         the cypher data are available in pCypherData.
00912   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00913   *         the configuration information for CRYP module
00914   * @param  pPlainData Pointer to the plaintext buffer
00915   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00916   * @param  pCypherData Pointer to the cyphertext buffer
00917   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00918   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
00919   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
00920   * @retval HAL status
00921   */
00922 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00923 {
00924   /* Re-initialize AES IP with proper parameters */
00925   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00926   {
00927     return HAL_ERROR;
00928   }
00929   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00930   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00931   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00932   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00933   {
00934     return HAL_ERROR;
00935   }
00936 
00937   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
00938 }
00939 
00940 
00941 
00942 /**
00943   * @brief  Encrypt pPlainData in AES CBC encryption mode using DMA,
00944   *         the cypher data are available in pCypherData.
00945   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00946   *         the configuration information for CRYP module
00947   * @param  pPlainData Pointer to the plaintext buffer
00948   * @param  Size Length of the plaintext buffer, must be a multiple of 16.
00949   * @param  pCypherData Pointer to the cyphertext buffer
00950   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00951   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
00952   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
00953   * @retval HAL status
00954   */
00955 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00956 {
00957   /* Re-initialize AES IP with proper parameters */
00958   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00959   {
00960     return HAL_ERROR;
00961   }
00962   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00963   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00964   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00965   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00966   {
00967     return HAL_ERROR;
00968   }
00969 
00970   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
00971 }
00972 
00973 /**
00974   * @brief  Encrypt pPlainData in AES CTR encryption mode using DMA,
00975   *         the cypher data are available in pCypherData.
00976   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
00977   *         the configuration information for CRYP module
00978   * @param  pPlainData Pointer to the plaintext buffer
00979   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
00980   * @param  pCypherData Pointer to the cyphertext buffer.
00981   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00982   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
00983   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
00984   * @retval HAL status
00985   */
00986 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00987 {
00988   /* Re-initialize AES IP with proper parameters */
00989   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00990   {
00991     return HAL_ERROR;
00992   }
00993   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00994   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00995   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00996   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00997   {
00998     return HAL_ERROR;
00999   }
01000 
01001   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
01002 }
01003 
01004 /**
01005   * @brief  Decrypt pCypherData in AES ECB decryption mode using DMA,
01006   *         the decyphered data are available in pPlainData.
01007   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01008   *         the configuration information for CRYP module
01009   * @param  pCypherData Pointer to the cyphertext buffer
01010   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
01011   * @param  pPlainData Pointer to the plaintext buffer
01012   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
01013   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
01014   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
01015   * @retval HAL status
01016   */
01017 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
01018 {
01019   /* Re-initialize AES IP with proper parameters */
01020   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
01021   {
01022     return HAL_ERROR;
01023   }
01024   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
01025   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
01026   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
01027   if (HAL_CRYP_Init(hcryp) != HAL_OK)
01028   {
01029     return HAL_ERROR;
01030   }
01031 
01032   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
01033 }
01034 
01035 /**
01036   * @brief  Decrypt pCypherData in AES CBC decryption mode using DMA,
01037   *         the decyphered data are available in pPlainData.
01038   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01039   *         the configuration information for CRYP module
01040   * @param  pCypherData Pointer to the cyphertext buffer
01041   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
01042   * @param  pPlainData Pointer to the plaintext buffer
01043   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
01044   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
01045   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
01046   * @retval HAL status
01047   */
01048 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
01049 {
01050   /* Re-initialize AES IP with proper parameters */
01051   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
01052   {
01053     return HAL_ERROR;
01054   }
01055   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
01056   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
01057   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
01058   if (HAL_CRYP_Init(hcryp) != HAL_OK)
01059   {
01060     return HAL_ERROR;
01061   }
01062 
01063   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
01064 }
01065 
01066 /**
01067   * @brief  Decrypt pCypherData in AES CTR decryption mode using DMA,
01068   *         the decyphered data are available in pPlainData.
01069   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01070   *         the configuration information for CRYP module
01071   * @param  pCypherData Pointer to the cyphertext buffer
01072   * @param  Size Length of the plaintext buffer in bytes, must be a multiple of 16.
01073   * @param  pPlainData Pointer to the plaintext buffer
01074   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
01075   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
01076   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
01077   * @retval HAL status
01078   */
01079 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
01080 {
01081   /* Re-initialize AES IP with proper parameters */
01082   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
01083   {
01084     return HAL_ERROR;
01085   }
01086   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
01087   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
01088   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
01089   if (HAL_CRYP_Init(hcryp) != HAL_OK)
01090   {
01091     return HAL_ERROR;
01092   }
01093 
01094   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
01095 }
01096 
01097 
01098 /**
01099   * @}
01100   */
01101 
01102 /** @defgroup CRYP_Exported_Functions_Group3 Callback functions
01103  *  @brief   Callback functions.
01104  *
01105 @verbatim
01106   ==============================================================================
01107                       ##### Callback functions  #####
01108   ==============================================================================
01109     [..]  This section provides Interruption and DMA callback functions:
01110       (+) DMA Input data transfer complete
01111       (+) DMA Output data transfer complete
01112       (+) DMA or Interrupt error
01113 
01114 @endverbatim
01115   * @{
01116   */
01117 
01118 /**
01119   * @brief  CRYP error callback.
01120   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01121   *         the configuration information for CRYP module
01122   * @retval None
01123   */
01124 __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
01125 {
01126   /* Prevent unused argument(s) compilation warning */
01127   UNUSED(hcryp);
01128 
01129   /* NOTE : This function should not be modified; when the callback is needed,
01130             the HAL_CRYP_ErrorCallback can be implemented in the user file
01131    */
01132 }
01133 
01134 /**
01135   * @brief  Input DMA transfer complete callback.
01136   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01137   *         the configuration information for CRYP module
01138   * @retval None
01139   */
01140 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
01141 {
01142   /* Prevent unused argument(s) compilation warning */
01143   UNUSED(hcryp);
01144 
01145   /* NOTE : This function should not be modified; when the callback is needed,
01146             the HAL_CRYP_InCpltCallback can be implemented in the user file
01147    */
01148 }
01149 
01150 /**
01151   * @brief  Output DMA transfer complete callback.
01152   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01153   *         the configuration information for CRYP module
01154   * @retval None
01155   */
01156 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
01157 {
01158   /* Prevent unused argument(s) compilation warning */
01159   UNUSED(hcryp);
01160 
01161   /* NOTE : This function should not be modified; when the callback is needed,
01162             the HAL_CRYP_OutCpltCallback can be implemented in the user file
01163    */
01164 }
01165 
01166 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
01167 /**
01168   * @brief  Register a User CRYP Callback
01169   *         To be used instead of the weak (surcharged) predefined callback
01170   * @param hcryp CRYP handle
01171   * @param CallbackID ID of the callback to be registered
01172   *        This parameter can be one of the following values:
01173   *          @arg @ref HAL_CRYP_INPUTCPLT_CB_ID CRYP input DMA transfer completion Callback ID
01174   *          @arg @ref HAL_CRYP_OUTPUTCPLT_CB_ID CRYP output DMA transfer completion Callback ID
01175   *          @arg @ref HAL_CRYP_COMPCPLT_CB_ID CRYP computation completion Callback ID
01176   *          @arg @ref HAL_CRYP_ERROR_CB_ID CRYP error callback ID
01177   *          @arg @ref HAL_CRYP_MSPINIT_CB_ID CRYP MspDeInit callback ID
01178   *          @arg @ref HAL_CRYP_MSPDEINIT_CB_ID CRYP MspDeInit callback ID
01179   * @param pCallback pointer to the Callback function
01180   * @retval status
01181   */
01182 HAL_StatusTypeDef HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID, pCRYP_CallbackTypeDef pCallback)
01183 {
01184   HAL_StatusTypeDef status = HAL_OK;
01185 
01186   if(pCallback == NULL)
01187   {
01188     /* Update the error code */
01189     hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
01190     return HAL_ERROR;
01191   }
01192   /* Process locked */
01193   __HAL_LOCK(hcryp);
01194 
01195   if(HAL_CRYP_STATE_READY == hcryp->State)
01196   {
01197     switch (CallbackID)
01198     {
01199     case HAL_CRYP_INPUTCPLT_CB_ID :
01200       hcryp->InCpltCallback = pCallback;
01201       break;
01202 
01203     case HAL_CRYP_OUTPUTCPLT_CB_ID :
01204       hcryp->OutCpltCallback = pCallback;
01205       break;
01206 
01207     case HAL_CRYP_COMPCPLT_CB_ID :
01208       hcryp->CompCpltCallback = pCallback;
01209       break;
01210 
01211     case HAL_CRYP_ERROR_CB_ID :
01212       hcryp->ErrorCallback = pCallback;
01213       break;
01214 
01215     case HAL_CRYP_MSPINIT_CB_ID :
01216       hcryp->MspInitCallback = pCallback;
01217       break;
01218 
01219     case HAL_CRYP_MSPDEINIT_CB_ID :
01220       hcryp->MspDeInitCallback = pCallback;
01221       break;
01222 
01223     default :
01224      /* Update the error code */
01225      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
01226      /* update return status */
01227       status =  HAL_ERROR;
01228       break;
01229     }
01230   }
01231   else if(HAL_CRYP_STATE_RESET == hcryp->State)
01232   {
01233     switch (CallbackID)
01234     {
01235     case HAL_CRYP_MSPINIT_CB_ID :
01236       hcryp->MspInitCallback = pCallback;
01237       break;
01238 
01239     case HAL_CRYP_MSPDEINIT_CB_ID :
01240       hcryp->MspDeInitCallback = pCallback;
01241       break;
01242 
01243     default :
01244      /* Update the error code */
01245      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
01246      /* update return status */
01247       status =  HAL_ERROR;
01248       break;
01249     }
01250   }
01251   else
01252   {
01253     /* Update the error code */
01254      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
01255      /* update return status */
01256       status =  HAL_ERROR;
01257   }
01258 
01259   /* Release Lock */
01260   __HAL_UNLOCK(hcryp);
01261   return status;
01262 }
01263 
01264 /**
01265   * @brief  Unregister a CRYP Callback
01266   *         CRYP Callback is redirected to the weak (surcharged) predefined callback
01267   * @param hcryp CRYP handle
01268   * @param CallbackID ID of the callback to be unregistered
01269   *        This parameter can be one of the following values:
01270   *          @arg @ref HAL_CRYP_INPUTCPLT_CB_ID CRYP input DMA transfer completion Callback ID
01271   *          @arg @ref HAL_CRYP_OUTPUTCPLT_CB_ID CRYP output DMA transfer completion Callback ID
01272   *          @arg @ref HAL_CRYP_COMPCPLT_CB_ID CRYP computation completion Callback ID
01273   *          @arg @ref HAL_CRYP_ERROR_CB_ID CRYP error callback ID
01274   *          @arg @ref HAL_CRYP_MSPINIT_CB_ID CRYP MspDeInit callback ID
01275   *          @arg @ref HAL_CRYP_MSPDEINIT_CB_ID CRYP MspDeInit callback ID
01276   * @retval status
01277   */
01278 HAL_StatusTypeDef HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID)
01279 {
01280 HAL_StatusTypeDef status = HAL_OK;
01281 
01282   /* Process locked */
01283   __HAL_LOCK(hcryp);
01284 
01285   if(HAL_CRYP_STATE_READY == hcryp->State)
01286   {
01287     switch (CallbackID)
01288     {
01289     case HAL_CRYP_INPUTCPLT_CB_ID :
01290       hcryp->InCpltCallback = HAL_CRYP_InCpltCallback;              /* Legacy weak (surcharged) input DMA transfer completion callback */
01291       break;
01292 
01293     case HAL_CRYP_OUTPUTCPLT_CB_ID :
01294       hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback;            /* Legacy weak (surcharged) output DMA transfer completion callback */
01295       break;
01296 
01297     case HAL_CRYP_COMPCPLT_CB_ID :
01298       hcryp->CompCpltCallback = HAL_CRYPEx_ComputationCpltCallback; /* Legacy weak (surcharged) computation completion callback */
01299       break;
01300 
01301     case HAL_CRYP_ERROR_CB_ID :
01302       hcryp->ErrorCallback = HAL_CRYP_ErrorCallback;                /* Legacy weak (surcharged) error callback */
01303       break;
01304 
01305     case HAL_CRYP_MSPINIT_CB_ID :
01306       hcryp->MspInitCallback = HAL_CRYP_MspInit;                    /* Legacy weak (surcharged) Msp DeInit */
01307       break;
01308 
01309     case HAL_CRYP_MSPDEINIT_CB_ID :
01310       hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;                /* Legacy weak (surcharged) Msp DeInit */
01311       break;
01312 
01313     default :
01314      /* Update the error code */
01315      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
01316      /* update return status */
01317       status =  HAL_ERROR;
01318       break;
01319     }
01320   }
01321   else if(HAL_CRYP_STATE_RESET == hcryp->State)
01322   {
01323     switch (CallbackID)
01324     {
01325     case HAL_CRYP_MSPINIT_CB_ID :
01326       hcryp->MspInitCallback = HAL_CRYP_MspInit;           /* Legacy weak (surcharged) Msp Init */
01327       break;
01328 
01329     case HAL_CRYP_MSPDEINIT_CB_ID :
01330       hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;       /* Legacy weak (surcharged) Msp DeInit */
01331       break;
01332 
01333     default :
01334      /* Update the error code */
01335      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
01336      /* update return status */
01337       status =  HAL_ERROR;
01338       break;
01339     }
01340   }
01341   else
01342   {
01343      /* Update the error code */
01344      hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
01345      /* update return status */
01346       status =  HAL_ERROR;
01347   }
01348 
01349   /* Release Lock */
01350   __HAL_UNLOCK(hcryp);
01351   return status;
01352 }
01353 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
01354 
01355 /**
01356   * @}
01357   */
01358 
01359 /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
01360  *  @brief   AES IRQ handler.
01361  *
01362 @verbatim
01363   ==============================================================================
01364                 ##### AES IRQ handler management #####
01365   ==============================================================================
01366 [..]  This section provides AES IRQ handler function.
01367 
01368 @endverbatim
01369   * @{
01370   */
01371 
01372 /**
01373   * @brief  Handle AES interrupt request.
01374   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01375   *         the configuration information for CRYP module
01376   * @retval None
01377   */
01378 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
01379 {
01380   /* Check if error occurred */
01381   if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_ERRIE) != RESET)
01382   {
01383     /* If Write Error occurred */
01384     if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_WRERR) != RESET)
01385     {
01386       hcryp->ErrorCode |= HAL_CRYP_WRITE_ERROR;
01387       hcryp->State = HAL_CRYP_STATE_ERROR;
01388     }
01389     /* If Read Error occurred */
01390     if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_RDERR) != RESET)
01391     {
01392       hcryp->ErrorCode |= HAL_CRYP_READ_ERROR;
01393       hcryp->State = HAL_CRYP_STATE_ERROR;
01394     }
01395 
01396     /* If an error has been reported */
01397     if (hcryp->State == HAL_CRYP_STATE_ERROR)
01398     {
01399       /* Disable Error and Computation Complete Interrupts */
01400       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
01401       /* Clear all Interrupt flags */
01402       __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR|CRYP_CCF_CLEAR);
01403 
01404       /* Process Unlocked */
01405       __HAL_UNLOCK(hcryp);
01406 
01407 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
01408       hcryp->ErrorCallback(hcryp);
01409 #else
01410       HAL_CRYP_ErrorCallback(hcryp);
01411 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
01412 
01413       return;
01414     }
01415 
01416   }
01417 
01418   /* Check if computation complete interrupt is enabled
01419      and if the computation complete flag is raised */
01420   if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_CCF) != RESET)
01421   {
01422     if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_CCFIE) != RESET)
01423     {
01424 #if defined(AES_CR_NPBLB)
01425       if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
01426        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
01427 #else
01428       if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
01429        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
01430 #endif
01431       {
01432        /* To ensure proper suspension requests management, CCF flag
01433           is reset in CRYP_AES_Auth_IT() according to the current
01434           phase under handling */
01435         if (CRYP_AES_Auth_IT(hcryp) !=  HAL_OK)
01436         {
01437 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
01438           hcryp->ErrorCallback(hcryp);
01439 #else
01440           HAL_CRYP_ErrorCallback(hcryp);
01441 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
01442         }
01443       }
01444       else
01445       {
01446         /* Clear Computation Complete Flag */
01447         __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
01448         if (CRYP_AES_IT(hcryp) !=  HAL_OK)
01449         {
01450 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
01451           hcryp->ErrorCallback(hcryp);
01452 #else
01453           HAL_CRYP_ErrorCallback(hcryp);
01454 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
01455         }
01456       }
01457     }
01458   }
01459 }
01460 
01461 /**
01462   * @}
01463   */
01464 
01465 /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
01466  *  @brief   Peripheral State functions.
01467  *
01468 @verbatim
01469   ==============================================================================
01470                       ##### Peripheral State functions #####
01471   ==============================================================================
01472     [..]
01473     This subsection permits to get in run-time the status of the peripheral.
01474 
01475 @endverbatim
01476   * @{
01477   */
01478 
01479 /**
01480   * @brief  Return the CRYP handle state.
01481   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01482   *         the configuration information for CRYP module
01483   * @retval HAL state
01484   */
01485 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
01486 {
01487   /* Return CRYP handle state */
01488   return hcryp->State;
01489 }
01490 
01491 /**
01492   * @brief  Return the CRYP peripheral error.
01493   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01494   *         the configuration information for CRYP module
01495   * @note   The returned error is a bit-map combination of possible errors
01496   * @retval Error bit-map
01497   */
01498 uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
01499 {
01500   return hcryp->ErrorCode;
01501 }
01502 
01503 /**
01504   * @}
01505   */
01506 
01507 /**
01508   * @}
01509   */
01510 
01511 /** @addtogroup CRYP_Private_Functions
01512   * @{
01513   */
01514 
01515 
01516 /**
01517   * @brief  Write the Key in KeyRx registers.
01518   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01519   *         the configuration information for CRYP module
01520   * @retval None
01521   */
01522 static HAL_StatusTypeDef  CRYP_SetKey(CRYP_HandleTypeDef *hcryp)
01523 {
01524   uint32_t keyaddr;
01525 
01526   if (hcryp->Init.pKey == NULL)
01527   {
01528     return HAL_ERROR;
01529   }
01530 
01531 
01532   keyaddr = (uint32_t)(hcryp->Init.pKey);
01533 
01534   if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
01535   {
01536     hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
01537     keyaddr+=4U;
01538     hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
01539     keyaddr+=4U;
01540     hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
01541     keyaddr+=4U;
01542     hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
01543     keyaddr+=4U;
01544   }
01545 
01546   hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
01547   keyaddr+=4U;
01548   hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
01549   keyaddr+=4U;
01550   hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
01551   keyaddr+=4U;
01552   hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
01553 
01554   return HAL_OK;
01555 }
01556 
01557 /**
01558   * @brief  Write the InitVector/InitCounter in IVRx registers.
01559   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01560   *         the configuration information for CRYP module
01561   * @retval None
01562   */
01563 static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp)
01564 {
01565   uint32_t ivaddr;
01566 
01567 #if !defined(AES_CR_NPBLB)
01568   if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
01569   {
01570     hcryp->Instance->IVR3 = 0;
01571     hcryp->Instance->IVR2 = 0;
01572     hcryp->Instance->IVR1 = 0;
01573     hcryp->Instance->IVR0 = 0;
01574   }
01575   else
01576 #endif
01577   {
01578     if (hcryp->Init.pInitVect == NULL)
01579     {
01580       return HAL_ERROR;
01581     }
01582 
01583     ivaddr = (uint32_t)(hcryp->Init.pInitVect);
01584 
01585     hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
01586     ivaddr+=4U;
01587     hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
01588     ivaddr+=4U;
01589     hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
01590     ivaddr+=4U;
01591     hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
01592   }
01593   return HAL_OK;
01594 }
01595 
01596 
01597 
01598 /**
01599   * @brief  Handle CRYP block input/output data handling under interruption.
01600   * @note   The function is called under interruption only, once
01601   *         interruptions have been enabled by HAL_CRYPEx_AES_IT().
01602   * @param  hcryp pointer to a CRYP_HandleTypeDef structure that contains
01603   *         the configuration information for CRYP module.
01604   * @retval HAL status
01605   */
01606 static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
01607 {
01608   uint32_t inputaddr;
01609   uint32_t outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
01610 
01611   if(hcryp->State == HAL_CRYP_STATE_BUSY)
01612   {
01613     if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
01614     {
01615       /* Read the last available output block from the Data Output Register */
01616       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01617       outputaddr+=4U;
01618       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01619       outputaddr+=4U;
01620       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01621       outputaddr+=4U;
01622       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01623       hcryp->pCrypOutBuffPtr += 16;
01624       hcryp->CrypOutCount -= 16U;
01625 
01626     }
01627     else
01628     {
01629       /* Read the derived key from the Key registers */
01630       if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
01631       {
01632         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
01633         outputaddr+=4U;
01634         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
01635         outputaddr+=4U;
01636         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
01637         outputaddr+=4U;
01638         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
01639         outputaddr+=4U;
01640       }
01641 
01642         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
01643         outputaddr+=4U;
01644         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
01645         outputaddr+=4U;
01646         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
01647         outputaddr+=4U;
01648         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
01649     }
01650 
01651     /* In case of ciphering or deciphering, check if all output text has been retrieved;
01652        In case of key derivation, stop right there */
01653     if ((hcryp->CrypOutCount == 0U) || (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION))
01654     {
01655       /* Disable Computation Complete Flag and Errors Interrupts */
01656       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
01657       /* Change the CRYP state */
01658       hcryp->State = HAL_CRYP_STATE_READY;
01659 
01660      /* Process Unlocked */
01661       __HAL_UNLOCK(hcryp);
01662 
01663       /* Call computation complete callback */
01664 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
01665       hcryp->CompCpltCallback(hcryp);
01666 #else
01667       HAL_CRYPEx_ComputationCpltCallback(hcryp);
01668 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
01669 
01670       return HAL_OK;
01671     }
01672     /* If suspension flag has been raised, suspend processing */
01673     else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
01674     {
01675       /* reset ModeSuspend */
01676       hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
01677 
01678       /* Disable Computation Complete Flag and Errors Interrupts */
01679       __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
01680       /* Change the CRYP state */
01681       hcryp->State = HAL_CRYP_STATE_SUSPENDED;
01682 
01683      /* Process Unlocked */
01684       __HAL_UNLOCK(hcryp);
01685 
01686       return HAL_OK;
01687     }
01688     else /* Process the rest of input data */
01689     {
01690       /* Get the Intput data address */
01691       inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
01692 
01693       /* Increment/decrement instance pointer/counter */
01694       hcryp->pCrypInBuffPtr += 16;
01695       hcryp->CrypInCount -= 16U;
01696 
01697       /* Write the next input block in the Data Input register */
01698       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
01699       inputaddr+=4U;
01700       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
01701       inputaddr+=4U;
01702       hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
01703       inputaddr+=4U;
01704       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
01705 
01706       return HAL_OK;
01707     }
01708   }
01709   else
01710   {
01711     return HAL_BUSY;
01712   }
01713 }
01714 
01715 
01716 
01717 
01718 /**
01719   * @}
01720   */
01721 
01722 
01723 
01724 /**
01725   * @}
01726   */
01727 
01728 /**
01729   * @}
01730   */
01731 
01732 #endif /* AES */
01733 
01734 #endif /* HAL_CRYP_MODULE_ENABLED */