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