L4 HAL Drivers

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4xx_hal_cryp.c Source File

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   * @version V1.1.0
00006   * @date    16-September-2015
00007   * @brief   CRYP HAL module driver.
00008   *          This file provides firmware functions to manage the following 
00009   *          functionalities of the Cryptography (CRYP) peripheral:
00010   *           + Initialization and de-initialization functions
00011   *           + Processing functions using polling mode
00012   *           + Processing functions using interrupt mode
00013   *           + Processing functions using DMA mode
00014   *           + Peripheral State functions
00015   *         
00016   @verbatim
00017   ==============================================================================
00018                      ##### How to use this driver #####
00019   ==============================================================================
00020     [..]
00021       The CRYP HAL driver can be used as follows:
00022     
00023       (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
00024          (++) Enable the CRYP interface clock using __HAL_RCC_AES_CLK_ENABLE()
00025          (++) In case of using interrupts (e.g. HAL_CRYP_AES_IT())
00026              (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
00027              (+++) Enable the AES IRQ handler using HAL_NVIC_EnableIRQ()
00028              (+++) In AES IRQ handler, call HAL_CRYP_IRQHandler()
00029          (++) In case of using DMA to control data transfer (e.g. HAL_CRYPEx_AES_DMA())
00030              (+++) Enable the DMA2 interface clock using 
00031                  __HAL_RCC_DMA2_CLK_ENABLE()
00032              (+++) Configure and enable two DMA channels one for managing data transfer from
00033                  memory to peripheral (input channel) and another channel for managing data
00034                  transfer from peripheral to memory (output channel)
00035              (+++) Associate the initialized DMA handle to the CRYP DMA handle
00036                  using __HAL_LINKDMA()
00037              (+++) Configure the priority and enable the NVIC for the transfer complete
00038                  interrupt on the two DMA channels. The output channel should have higher
00039                  priority than the input channel.
00040                  Resort to HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
00041                                                        
00042       (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures:
00043          (++) The data type: 1-bit, 8-bit, 16-bit and 32-bit
00044          (++) The AES operating mode (encryption, key derivation and/or decryption)
00045          (++) The AES chaining mode (ECB, CBC, CTR, GCM, GMAC, CMAC)         
00046          (++) The encryption/decryption key if so required
00047          (++) The initialization vector or nonce if applicable (not used in ECB mode).
00048     
00049       (#)Three processing (encryption/decryption) functions are available:
00050          (++) Polling mode: encryption and decryption APIs are blocking functions
00051               i.e. they process the data and wait till the processing is finished
00052          (++) Interrupt mode: encryption and decryption APIs are not blocking functions
00053               i.e. they process the data under interrupt
00054          (++) DMA mode: encryption and decryption APIs are not blocking functions
00055               i.e. the data transfer is ensured by DMA
00056          
00057        (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
00058 
00059   @endverbatim
00060   ******************************************************************************
00061   * @attention
00062   *
00063   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00064   *
00065   * Redistribution and use in source and binary forms, with or without modification,
00066   * are permitted provided that the following conditions are met:
00067   *   1. Redistributions of source code must retain the above copyright notice,
00068   *      this list of conditions and the following disclaimer.
00069   *   2. Redistributions in binary form must reproduce the above copyright notice,
00070   *      this list of conditions and the following disclaimer in the documentation
00071   *      and/or other materials provided with the distribution.
00072   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00073   *      may be used to endorse or promote products derived from this software
00074   *      without specific prior written permission.
00075   *
00076   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00077   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00078   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00079   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00080   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00081   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00082   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00083   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00084   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00085   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00086   *
00087   ******************************************************************************  
00088   */ 
00089 
00090 /* Includes ------------------------------------------------------------------*/
00091 #include "stm32l4xx_hal.h"
00092 
00093 #ifdef HAL_CRYP_MODULE_ENABLED
00094 
00095 #if defined(STM32L485xx) || defined(STM32L486xx)
00096 
00097 /** @addtogroup STM32L4xx_HAL_Driver
00098   * @{
00099   */
00100 
00101 /** @defgroup CRYP CRYP
00102   * @brief CRYP HAL module driver.
00103   * @{
00104   */
00105 
00106 
00107 
00108 /* Private typedef -----------------------------------------------------------*/
00109 /* Private define ------------------------------------------------------------*/
00110 /* Private macro -------------------------------------------------------------*/
00111 /* Private variables ---------------------------------------------------------*/
00112 /* Private functions --------------------------------------------------------*/
00113 
00114 /** @defgroup CRYP_Private_Functions CRYP Private Functions
00115   * @{
00116   */
00117 
00118 static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp);
00119 static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp);
00120 static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
00121 
00122 /**
00123   * @}
00124   */
00125 
00126 /* Exported functions ---------------------------------------------------------*/
00127 
00128 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
00129   * @{
00130   */
00131 
00132 /** @defgroup CRYP_Group1 Initialization and deinitialization functions 
00133  *  @brief    Initialization and Configuration functions. 
00134  *
00135 @verbatim    
00136   ==============================================================================
00137               ##### Initialization and deinitialization functions #####
00138   ==============================================================================
00139     [..]  This section provides functions allowing to:
00140       (+) Initialize the CRYP according to the specified parameters 
00141           in the CRYP_InitTypeDef and creates the associated handle
00142       (+) DeInitialize the CRYP peripheral
00143       (+) Initialize the CRYP MSP (MCU Specific Package)
00144       (+) DeInitialize the CRC MSP
00145  
00146 @endverbatim
00147   * @{
00148   */
00149 
00150 /**
00151   * @brief  Initialize the CRYP according to the specified
00152   *         parameters in the CRYP_InitTypeDef and initialize the associated handle.
00153   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00154   *         the configuration information for CRYP module
00155   * @retval HAL status
00156   */
00157 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
00158 {   
00159   /* Check the CRYP handle allocation */
00160   if(hcryp == NULL)
00161   {
00162     return HAL_ERROR;
00163   }
00164   
00165   /* Check the instance */
00166   assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance));
00167   
00168   /* Check the parameters */
00169   assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
00170   assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
00171   assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode));
00172   /* ChainingMode parameter is irrelevant when mode is set to Key derivation */
00173   if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
00174   {
00175     assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode));
00176   }
00177   assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag));
00178   
00179   /*========================================================*/
00180   /* Check the proper operating/chaining modes combinations */
00181   /*========================================================*/  
00182   /* Check the proper chaining when the operating mode is key derivation and decryption */
00183   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
00184          ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR)           \
00185        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)      \
00186        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)))        
00187   {
00188     return HAL_ERROR;
00189   }  
00190   /* Check that key derivation is not set in CMAC mode */  
00191   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) 
00192    && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))        
00193   {
00194     return HAL_ERROR;
00195   }
00196   
00197   
00198   /*================*/
00199   /* Initialization */
00200   /*================*/  
00201   /* Initialization start */
00202   if(hcryp->State == HAL_CRYP_STATE_RESET)
00203   {
00204     /* Allocate lock resource and initialize it */
00205     hcryp->Lock = HAL_UNLOCKED;
00206 
00207     /* Init the low level hardware */
00208     HAL_CRYP_MspInit(hcryp);
00209   }
00210   
00211   /* Change the CRYP state */
00212   hcryp->State = HAL_CRYP_STATE_BUSY;  
00213   
00214   /* Disable the Peripheral */
00215   __HAL_CRYP_DISABLE();
00216   
00217   /*=============================================================*/
00218   /* AES initialization common to all operating modes            */ 
00219   /*=============================================================*/
00220   /* Set the Key size selection */
00221   MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize);
00222   
00223   /* Set the default CRYP phase when this parameter is not used.
00224      Phase is updated below in case of GCM/GMAC/CMAC setting. */
00225   hcryp->Phase = HAL_CRYP_PHASE_NOT_USED;
00226   
00227   
00228 
00229   /*=============================================================*/
00230   /* Carry on the initialization based on the AES operating mode */ 
00231   /*=============================================================*/
00232   /* Key derivation */ 
00233   if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
00234   {
00235     MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION);
00236     
00237     /* Configure the Key registers */
00238     if (CRYP_SetKey(hcryp) != HAL_OK)
00239     {
00240       return HAL_ERROR;
00241     }
00242   }
00243   else
00244   /* Encryption / Decryption (with or without key derivation) / authentication */
00245   {    
00246     /* Set data type, operating and chaining modes.
00247        In case of GCM or GMAC, data type is forced to 0b00 */
00248     if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
00249     {
00250       MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
00251     }
00252     else
00253     {
00254       MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
00255     }
00256 
00257     
00258    /* Specify the encryption/decryption phase in case of Galois counter mode (GCM), 
00259       Galois message authentication code (GMAC) or cipher message authentication code (CMAC) */
00260    if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
00261     || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
00262     {
00263       MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase);
00264       hcryp->Phase = HAL_CRYP_PHASE_START;
00265     }
00266 
00267     
00268     /* Configure the Key registers if no need to bypass this step */
00269     if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE)
00270     {
00271       if (CRYP_SetKey(hcryp) != HAL_OK)
00272       {
00273         return HAL_ERROR;
00274       }      
00275     }
00276     
00277     /* If applicable, configure the Initialization Vector */
00278     if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB)
00279     {
00280       if (CRYP_SetInitVector(hcryp) != HAL_OK)
00281       {
00282         return HAL_ERROR;
00283       }
00284     }
00285   }
00286 
00287   /* Reset CrypInCount and CrypOutCount */
00288   hcryp->CrypInCount = 0;
00289   hcryp->CrypOutCount = 0;
00290   
00291   /* Reset ErrorCode field */
00292   hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
00293   
00294   /* Reset Mode suspension request */
00295   hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
00296   
00297   /* Change the CRYP state */
00298   hcryp->State = HAL_CRYP_STATE_READY;
00299   
00300   /* Enable the Peripheral */
00301   __HAL_CRYP_ENABLE();
00302   
00303   /* Return function status */
00304   return HAL_OK;
00305 }
00306 
00307 /**
00308   * @brief  DeInitialize the CRYP peripheral. 
00309   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00310   *         the configuration information for CRYP module
00311   * @retval HAL status
00312   */
00313 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
00314 {
00315   /* Check the CRYP handle allocation */
00316   if(hcryp == NULL)
00317   {
00318     return HAL_ERROR;
00319   }
00320   
00321   /* Change the CRYP state */
00322   hcryp->State = HAL_CRYP_STATE_BUSY;
00323   
00324   /* Set the default CRYP phase */
00325   hcryp->Phase = HAL_CRYP_PHASE_READY;
00326   
00327   /* Reset CrypInCount and CrypOutCount */
00328   hcryp->CrypInCount = 0;
00329   hcryp->CrypOutCount = 0;
00330   
00331   /* Disable the CRYP Peripheral Clock */
00332   __HAL_CRYP_DISABLE();
00333   
00334   /* DeInit the low level hardware: CLOCK, NVIC.*/
00335   HAL_CRYP_MspDeInit(hcryp);
00336   
00337   /* Change the CRYP state */
00338   hcryp->State = HAL_CRYP_STATE_RESET;
00339   
00340   /* Release Lock */
00341   __HAL_UNLOCK(hcryp);
00342   
00343   /* Return function status */
00344   return HAL_OK;
00345 }
00346 
00347 /**
00348   * @brief  Initialize the CRYP MSP.
00349   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00350   *         the configuration information for CRYP module
00351   * @retval None
00352   */
00353 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
00354 {
00355   /* NOTE : This function should not be modified; when the callback is needed,
00356             the HAL_CRYP_MspInit can be implemented in the user file
00357    */
00358 }
00359 
00360 /**
00361   * @brief  DeInitialize CRYP MSP.
00362   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00363   *         the configuration information for CRYP module
00364   * @retval None
00365   */
00366 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
00367 {
00368   /* NOTE : This function should not be modified; when the callback is needed,
00369             the HAL_CRYP_MspDeInit can be implemented in the user file
00370    */
00371 }
00372 
00373 /**
00374   * @}
00375   */
00376 
00377 /** @defgroup CRYP_Group2 AES processing functions 
00378  *  @brief   Processing functions. 
00379  *
00380 @verbatim   
00381   ==============================================================================
00382                       ##### AES processing functions #####
00383   ==============================================================================  
00384     [..]  This section provides functions allowing to:
00385       (+) Encrypt plaintext using AES algorithm in different chaining modes
00386       (+) Decrypt cyphertext using AES algorithm in different chaining modes
00387     [..]  Three processing functions are available:
00388       (+) Polling mode
00389       (+) Interrupt mode
00390       (+) DMA mode
00391 
00392 @endverbatim
00393   * @{
00394   */
00395   
00396   
00397 /**
00398   * @brief  Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData.
00399   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00400   *         the configuration information for CRYP module
00401   * @param  pPlainData: Pointer to the plaintext buffer
00402   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00403   * @param  pCypherData: Pointer to the cyphertext buffer
00404   * @param  Timeout: Specify Timeout value 
00405   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00406   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).      
00407   * @retval HAL status
00408   */
00409 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
00410 {
00411   /* Re-initialize AES IP with proper parameters */
00412   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00413   {
00414     return HAL_ERROR;
00415   }
00416   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00417   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00418   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00419   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00420   {
00421     return HAL_ERROR;
00422   }
00423 
00424   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
00425 }
00426  
00427 
00428 /**
00429   * @brief  Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData.
00430   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00431   *         the configuration information for CRYP module
00432   * @param  pPlainData: Pointer to the plaintext buffer
00433   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00434   * @param  pCypherData: Pointer to the cyphertext buffer
00435   * @param  Timeout: Specify Timeout value
00436   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00437   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).     
00438   * @retval HAL status
00439   */
00440 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
00441 { 
00442   /* Re-initialize AES IP with proper parameters */
00443   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00444   {
00445     return HAL_ERROR;
00446   }
00447   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00448   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00449   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00450   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00451   {
00452     return HAL_ERROR;
00453   }
00454   
00455   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
00456 }
00457 
00458 
00459 /**
00460   * @brief  Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData
00461   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00462   *         the configuration information for CRYP module
00463   * @param  pPlainData: Pointer to the plaintext buffer
00464   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00465   * @param  pCypherData: Pointer to the cyphertext buffer
00466   * @param  Timeout: Specify Timeout value 
00467   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00468   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).    
00469   * @retval HAL status
00470   */
00471 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
00472 {
00473   /* Re-initialize AES IP with proper parameters */
00474   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00475   {
00476     return HAL_ERROR;
00477   }
00478   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00479   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00480   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00481   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00482   {
00483     return HAL_ERROR;
00484   }
00485 
00486   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
00487 }
00488 
00489 /**
00490   * @brief  Decrypt pCypherData in AES ECB decryption mode with key derivation, 
00491   *         the decyphered data are available in pPlainData.
00492   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00493   *         the configuration information for CRYP module
00494   * @param  pCypherData: Pointer to the cyphertext buffer
00495   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00496   * @param  pPlainData: Pointer to the plaintext buffer
00497   * @param  Timeout: Specify Timeout value 
00498   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00499   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).   
00500   * @retval HAL status
00501   */
00502 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
00503 {
00504   /* Re-initialize AES IP with proper parameters */
00505   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00506   {
00507     return HAL_ERROR;
00508   }
00509   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00510   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00511   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00512   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00513   {
00514     return HAL_ERROR;
00515   }
00516 
00517   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
00518 }
00519 
00520 /**
00521   * @brief  Decrypt pCypherData in AES ECB decryption mode with key derivation, 
00522   *         the decyphered data are available in pPlainData.
00523   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00524   *         the configuration information for CRYP module
00525   * @param  pCypherData: Pointer to the cyphertext buffer
00526   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00527   * @param  pPlainData: Pointer to the plaintext buffer
00528   * @param  Timeout: Specify Timeout value 
00529   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00530   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).    
00531   * @retval HAL status
00532   */
00533 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
00534 {
00535   /* Re-initialize AES IP with proper parameters */
00536   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00537   {
00538     return HAL_ERROR;
00539   }
00540   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00541   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00542   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00543   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00544   {
00545     return HAL_ERROR;
00546   }
00547   
00548   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
00549 }
00550 
00551 /**
00552   * @brief  Decrypt pCypherData in AES CTR decryption mode, 
00553   *         the decyphered data are available in pPlainData.
00554   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00555   *         the configuration information for CRYP module
00556   * @param  pCypherData: Pointer to the cyphertext buffer
00557   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00558   * @param  pPlainData: Pointer to the plaintext buffer
00559   * @param  Timeout: Specify Timeout value
00560   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00561   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).     
00562   * @retval HAL status
00563   */
00564 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
00565 {
00566   /* Re-initialize AES IP with proper parameters */
00567   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00568   {
00569     return HAL_ERROR;
00570   }
00571   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
00572   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00573   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00574   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00575   {
00576     return HAL_ERROR;
00577   }  
00578   
00579   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
00580 }
00581 
00582 /**
00583   * @brief  Encrypt pPlainData in AES ECB encryption mode using Interrupt,
00584   *         the cypher data are available in pCypherData.
00585   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00586   *         the configuration information for CRYP module
00587   * @param  pPlainData: Pointer to the plaintext buffer
00588   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00589   * @param  pCypherData: Pointer to the cyphertext buffer
00590   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00591   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).  
00592   * @retval HAL status
00593   */
00594 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00595 {
00596   /* Re-initialize AES IP with proper parameters */
00597   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00598   {
00599     return HAL_ERROR;
00600   }
00601   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00602   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00603   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00604   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00605   {
00606     return HAL_ERROR;
00607   }  
00608   
00609   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
00610 }
00611 
00612 /**
00613   * @brief  Encrypt pPlainData in AES CBC encryption mode using Interrupt,
00614   *         the cypher data are available in pCypherData.  
00615   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00616   *         the configuration information for CRYP module
00617   * @param  pPlainData: Pointer to the plaintext buffer
00618   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00619   * @param  pCypherData: Pointer to the cyphertext buffer
00620   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00621   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).   
00622   * @retval HAL status
00623   */
00624 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00625 {
00626   /* Re-initialize AES IP with proper parameters */
00627   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00628   {
00629     return HAL_ERROR;
00630   }
00631   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00632   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00633   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00634   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00635   {
00636     return HAL_ERROR;
00637   }
00638   
00639   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
00640 }
00641   
00642 
00643 /**
00644   * @brief  Encrypt pPlainData in AES CTR encryption mode using Interrupt,
00645   *         the cypher data are available in pCypherData.  
00646   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00647   *         the configuration information for CRYP module
00648   * @param  pPlainData: Pointer to the plaintext buffer
00649   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00650   * @param  pCypherData: Pointer to the cyphertext buffer
00651   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00652   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).   
00653   * @retval HAL status
00654   */
00655 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00656 {
00657   /* Re-initialize AES IP with proper parameters */
00658   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00659   {
00660     return HAL_ERROR;
00661   }
00662   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00663   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00664   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00665   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00666   {
00667     return HAL_ERROR;
00668   }
00669   
00670   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
00671 }
00672 
00673 /**
00674   * @brief  Decrypt pCypherData in AES ECB decryption mode using Interrupt,
00675   *         the decyphered data are available in pPlainData.   
00676   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00677   *         the configuration information for CRYP module
00678   * @param  pCypherData: Pointer to the cyphertext buffer
00679   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00680   * @param  pPlainData: Pointer to the plaintext buffer.
00681   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00682   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).      
00683   * @retval HAL status
00684   */
00685 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00686 {
00687   /* Re-initialize AES IP with proper parameters */
00688   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00689   {
00690     return HAL_ERROR;
00691   }
00692   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00693   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00694   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00695   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00696   {
00697     return HAL_ERROR;
00698   }
00699   
00700   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
00701 }
00702 
00703 /**
00704   * @brief  Decrypt pCypherData in AES CBC decryption mode using Interrupt,
00705   *         the decyphered data are available in pPlainData.  
00706   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00707   *         the configuration information for CRYP module
00708   * @param  pCypherData: Pointer to the cyphertext buffer
00709   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00710   * @param  pPlainData: Pointer to the plaintext buffer
00711   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00712   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).  
00713   * @retval HAL status
00714   */
00715 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00716 {
00717   /* Re-initialize AES IP with proper parameters */
00718   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00719   {
00720     return HAL_ERROR;
00721   }
00722   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00723   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00724   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00725   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00726   {
00727     return HAL_ERROR;
00728   }
00729   
00730   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
00731 }
00732 
00733 /**
00734   * @brief  Decrypt pCypherData in AES CTR decryption mode using Interrupt,
00735   *         the decyphered data are available in pPlainData. 
00736   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00737   *         the configuration information for CRYP module
00738   * @param  pCypherData: Pointer to the cyphertext buffer
00739   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00740   * @param  pPlainData: Pointer to the plaintext buffer
00741   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00742   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).    
00743   * @retval HAL status
00744   */
00745 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00746 {
00747   /* Re-initialize AES IP with proper parameters */
00748   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00749   {
00750     return HAL_ERROR;
00751   }
00752   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
00753   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00754   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00755   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00756   {
00757     return HAL_ERROR;
00758   }  
00759   
00760   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
00761 }
00762 
00763 /**
00764   * @brief  Encrypt pPlainData in AES ECB encryption mode using DMA,
00765   *         the cypher data are available in pCypherData.   
00766   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00767   *         the configuration information for CRYP module
00768   * @param  pPlainData: Pointer to the plaintext buffer
00769   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00770   * @param  pCypherData: Pointer to the cyphertext buffer
00771   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00772   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
00773   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.    
00774   * @retval HAL status
00775   */
00776 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00777 {
00778   /* Re-initialize AES IP with proper parameters */
00779   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00780   {
00781     return HAL_ERROR;
00782   }
00783   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00784   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00785   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00786   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00787   {
00788     return HAL_ERROR;
00789   }
00790   
00791   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
00792 }
00793   
00794  
00795 
00796 /**
00797   * @brief  Encrypt pPlainData in AES CBC encryption mode using DMA,
00798   *         the cypher data are available in pCypherData.  
00799   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00800   *         the configuration information for CRYP module
00801   * @param  pPlainData: Pointer to the plaintext buffer
00802   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.
00803   * @param  pCypherData: Pointer to the cyphertext buffer
00804   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00805   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
00806   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.       
00807   * @retval HAL status
00808   */
00809 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00810 {
00811   /* Re-initialize AES IP with proper parameters */
00812   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00813   {
00814     return HAL_ERROR;
00815   }
00816   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00817   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00818   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00819   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00820   {
00821     return HAL_ERROR;
00822   }
00823   
00824   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
00825 }
00826 
00827 /**
00828   * @brief  Encrypt pPlainData in AES CTR encryption mode using DMA,
00829   *         the cypher data are available in pCypherData. 
00830   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00831   *         the configuration information for CRYP module
00832   * @param  pPlainData: Pointer to the plaintext buffer
00833   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00834   * @param  pCypherData: Pointer to the cyphertext buffer.
00835   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00836   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
00837   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.      
00838   * @retval HAL status
00839   */
00840 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00841 {
00842   /* Re-initialize AES IP with proper parameters */
00843   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00844   {
00845     return HAL_ERROR;
00846   }
00847   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00848   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00849   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00850   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00851   {
00852     return HAL_ERROR;
00853   }
00854 
00855   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
00856 }
00857 
00858 /**
00859   * @brief  Decrypt pCypherData in AES ECB decryption mode using DMA,
00860   *         the decyphered data are available in pPlainData.   
00861   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00862   *         the configuration information for CRYP module
00863   * @param  pCypherData: Pointer to the cyphertext buffer
00864   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00865   * @param  pPlainData: Pointer to the plaintext buffer
00866   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00867   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). 
00868   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.     
00869   * @retval HAL status
00870   */
00871 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00872 {
00873   /* Re-initialize AES IP with proper parameters */
00874   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00875   {
00876     return HAL_ERROR;
00877   }
00878   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00879   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00880   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00881   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00882   {
00883     return HAL_ERROR;
00884   }
00885   
00886   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
00887 }
00888 
00889 /**
00890   * @brief  Decrypt pCypherData in AES CBC decryption mode using DMA,
00891   *         the decyphered data are available in pPlainData.  
00892   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00893   *         the configuration information for CRYP module
00894   * @param  pCypherData: Pointer to the cyphertext buffer
00895   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00896   * @param  pPlainData: Pointer to the plaintext buffer
00897   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00898   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
00899   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.      
00900   * @retval HAL status
00901   */
00902 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00903 {
00904   /* Re-initialize AES IP with proper parameters */
00905   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00906   {
00907     return HAL_ERROR;
00908   }
00909   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00910   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00911   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00912   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00913   {
00914     return HAL_ERROR;
00915   }
00916   
00917   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
00918 }
00919 
00920 /**
00921   * @brief  Decrypt pCypherData in AES CTR decryption mode using DMA,
00922   *         the decyphered data are available in pPlainData. 
00923   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00924   *         the configuration information for CRYP module
00925   * @param  pCypherData: Pointer to the cyphertext buffer
00926   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00927   * @param  pPlainData: Pointer to the plaintext buffer
00928   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00929   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). 
00930   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.     
00931   * @retval HAL status
00932   */
00933 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00934 {
00935   /* Re-initialize AES IP with proper parameters */
00936   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00937   {
00938     return HAL_ERROR;
00939   }
00940   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
00941   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00942   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00943   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00944   {
00945     return HAL_ERROR;
00946   }  
00947   
00948   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
00949 }
00950 
00951 
00952 /**
00953   * @}
00954   */
00955 
00956 /** @defgroup CRYP_Group3 Callback functions 
00957  *  @brief   Callback functions. 
00958  *
00959 @verbatim   
00960   ==============================================================================
00961                       ##### Callback functions  #####
00962   ==============================================================================  
00963     [..]  This section provides Interruption and DMA callback functions:
00964       (+) DMA Input data transfer complete
00965       (+) DMA Output data transfer complete
00966       (+) DMA or Interrupt error
00967 
00968 @endverbatim
00969   * @{
00970   */
00971 
00972 /**
00973   * @brief  CRYP error callback.
00974   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00975   *         the configuration information for CRYP module
00976   * @retval None
00977   */
00978  __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
00979 {
00980   /* NOTE : This function should not be modified; when the callback is needed,
00981             the HAL_CRYP_ErrorCallback can be implemented in the user file
00982    */
00983 }
00984 
00985 /**
00986   * @brief  Input DMA transfer complete callback.
00987   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00988   *         the configuration information for CRYP module
00989   * @retval None
00990   */
00991 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
00992 {
00993   /* NOTE : This function should not be modified; when the callback is needed,
00994             the HAL_CRYP_InCpltCallback can be implemented in the user file
00995    */
00996 }
00997 
00998 /**
00999   * @brief  Output DMA transfer complete callback.
01000   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01001   *         the configuration information for CRYP module
01002   * @retval None
01003   */
01004 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
01005 {
01006   /* NOTE : This function should not be modified; when the callback is needed,
01007             the HAL_CRYP_OutCpltCallback can be implemented in the user file
01008    */
01009 }
01010 
01011 /**
01012   * @}
01013   */
01014 
01015 /** @defgroup CRYP_Group4 CRYP IRQ handler 
01016  *  @brief   AES IRQ handler.
01017  *
01018 @verbatim   
01019   ==============================================================================
01020                 ##### AES IRQ handler management #####
01021   ==============================================================================  
01022 [..]  This section provides AES IRQ handler function.
01023 
01024 @endverbatim
01025   * @{
01026   */
01027 
01028 /**
01029   * @brief  Handle AES interrupt request.
01030   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01031   *         the configuration information for CRYP module
01032   * @retval None
01033   */
01034 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
01035 {
01036   /* Check if error occurred */
01037   if (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_ERRIE) != RESET)
01038   {
01039     /* If Write Error occurred */
01040     if (__HAL_CRYP_GET_FLAG(CRYP_IT_WRERR) != RESET)
01041     {
01042       hcryp->ErrorCode |= HAL_CRYP_WRITE_ERROR;
01043       hcryp->State = HAL_CRYP_STATE_ERROR;
01044     }
01045     /* If Read Error occurred */
01046     if (__HAL_CRYP_GET_FLAG(CRYP_IT_RDERR) != RESET)
01047     {
01048       hcryp->ErrorCode |= HAL_CRYP_READ_ERROR;
01049       hcryp->State = HAL_CRYP_STATE_ERROR;
01050     }
01051     
01052     /* If an error has been reported */
01053     if (hcryp->State == HAL_CRYP_STATE_ERROR)
01054     {  
01055       /* Disable Error and Computation Complete Interrupts */
01056       __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
01057       /* Clear all Interrupt flags */
01058       __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR|CRYP_CCF_CLEAR);
01059     
01060       /* Process Unlocked */
01061       __HAL_UNLOCK(hcryp);  
01062     
01063       HAL_CRYP_ErrorCallback(hcryp);
01064   
01065       return; 
01066     }
01067   }
01068   
01069   /* Check if computation complete interrupt is enabled 
01070      and if the computation complete flag is raised */
01071   if((__HAL_CRYP_GET_FLAG(CRYP_IT_CCF) != RESET) && (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_CCFIE) != RESET))
01072   {    
01073     if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
01074      || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
01075     {
01076      /* To ensure proper suspension requests management, CCF flag 
01077         is reset in CRYP_AES_Auth_IT() according to the current 
01078         phase under handling */
01079       CRYP_AES_Auth_IT(hcryp);
01080     }
01081     else
01082     {
01083       /* Clear Computation Complete Flag */
01084       __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
01085       CRYP_AES_IT(hcryp);
01086     }
01087   }
01088 }
01089 
01090 /**
01091   * @}
01092   */
01093 
01094 /** @defgroup CRYP_Group5 Peripheral State functions 
01095  *  @brief   Peripheral State functions. 
01096  *
01097 @verbatim   
01098   ==============================================================================
01099                       ##### Peripheral State functions #####
01100   ==============================================================================  
01101     [..]
01102     This subsection permits to get in run-time the status of the peripheral.
01103 
01104 @endverbatim
01105   * @{
01106   */
01107 
01108 /**
01109   * @brief  Return the CRYP handle state.
01110   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01111   *         the configuration information for CRYP module
01112   * @retval HAL state
01113   */
01114 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
01115 {
01116   /* Return CRYP handle state */
01117   return hcryp->State;
01118 }
01119 
01120 /**
01121   * @brief  Return the CRYP peripheral error.
01122   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01123   *         the configuration information for CRYP module
01124   * @note   The returned error is a bit-map combination of possible errors          
01125   * @retval Error bit-map
01126   */
01127 uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
01128 {
01129   return hcryp->ErrorCode;
01130 }
01131 
01132 /**
01133   * @}
01134   */
01135 
01136 /**
01137   * @}
01138   */
01139 
01140 /** @addtogroup CRYP_Private_Functions
01141   * @{
01142   */
01143 
01144 
01145 /**
01146   * @brief  Write the Key in KeyRx registers. 
01147   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01148   *         the configuration information for CRYP module
01149   * @retval HAL status
01150   */
01151 static HAL_StatusTypeDef  CRYP_SetKey(CRYP_HandleTypeDef *hcryp)
01152 {  
01153   uint32_t keyaddr = 0x0;
01154   
01155   if ((uint32_t)(hcryp->Init.pKey == NULL))
01156   {
01157     return HAL_ERROR;
01158   }
01159   
01160   
01161   keyaddr = (uint32_t)(hcryp->Init.pKey);
01162   
01163   if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
01164   {
01165     hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
01166     keyaddr+=4;
01167     hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
01168     keyaddr+=4;
01169     hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
01170     keyaddr+=4;
01171     hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
01172     keyaddr+=4;      
01173   }  
01174   
01175   hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
01176   keyaddr+=4;
01177   hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
01178   keyaddr+=4;
01179   hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
01180   keyaddr+=4;
01181   hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));  
01182   
01183   return HAL_OK;
01184 }
01185 
01186 /**
01187   * @brief  Write the InitVector/InitCounter in IVRx registers. 
01188   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01189   *         the configuration information for CRYP module
01190   * @retval HAL status
01191   */
01192 static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp)
01193 {
01194   uint32_t ivaddr = 0x0;
01195  
01196   if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
01197   {
01198     hcryp->Instance->IVR3 = 0;
01199     hcryp->Instance->IVR2 = 0;
01200     hcryp->Instance->IVR1 = 0;        
01201     hcryp->Instance->IVR0 = 0;
01202   }
01203   else
01204   {
01205     if (hcryp->Init.pInitVect == NULL)
01206     {
01207       return HAL_ERROR;
01208     } 
01209   
01210     ivaddr = (uint32_t)(hcryp->Init.pInitVect);
01211   
01212     hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
01213     ivaddr+=4;
01214     hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
01215     ivaddr+=4;
01216     hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
01217     ivaddr+=4;
01218     hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
01219   }
01220   return HAL_OK;
01221 }
01222 
01223 
01224 
01225 /** 
01226   * @brief  Handle CRYP block input/output data handling under interruption.
01227   * @note   The function is called under interruption only, once
01228   *         interruptions have been enabled by HAL_CRYPEx_AES_IT().
01229   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01230   *         the configuration information for CRYP module.
01231   * @retval HAL status
01232   */
01233 static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
01234 {
01235   uint32_t inputaddr = 0;
01236   uint32_t outputaddr = 0;  
01237 
01238   if(hcryp->State == HAL_CRYP_STATE_BUSY)
01239   {
01240     if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
01241     {
01242       /* Get the output data address */
01243       outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
01244       
01245       /* Read the last available output block from the Data Output Register */
01246       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01247       outputaddr+=4;
01248       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01249       outputaddr+=4;
01250       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01251       outputaddr+=4;
01252       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01253       hcryp->pCrypOutBuffPtr += 16;
01254       hcryp->CrypOutCount -= 16;
01255     
01256     }
01257     else
01258     {
01259       /* Read the derived key from the Key registers */
01260       if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
01261       {   
01262         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
01263         outputaddr+=4;
01264         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
01265         outputaddr+=4;
01266         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
01267         outputaddr+=4;
01268         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
01269         outputaddr+=4;
01270       }
01271       
01272         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
01273         outputaddr+=4;
01274         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
01275         outputaddr+=4;
01276         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
01277         outputaddr+=4;
01278         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
01279     }
01280     
01281     /* In case of ciphering or deciphering, check if all output text has been retrieved;
01282        In case of key derivation, stop right there */
01283     if ((hcryp->CrypOutCount == 0) || (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION))
01284     {
01285       /* Disable Computation Complete Flag and Errors Interrupts */
01286       __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
01287       /* Change the CRYP state */
01288       hcryp->State = HAL_CRYP_STATE_READY;
01289       
01290      /* Process Unlocked */
01291       __HAL_UNLOCK(hcryp);
01292       
01293       /* Call computation complete callback */
01294       HAL_CRYPEx_ComputationCpltCallback(hcryp);
01295       
01296       return HAL_OK;
01297     }
01298     /* If suspension flag has been raised, suspend processing */
01299     else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
01300     {
01301       /* reset ModeSuspend */
01302       hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
01303       
01304       /* Disable Computation Complete Flag and Errors Interrupts */
01305       __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
01306       /* Change the CRYP state */
01307       hcryp->State = HAL_CRYP_STATE_SUSPENDED;
01308       
01309      /* Process Unlocked */
01310       __HAL_UNLOCK(hcryp);
01311       
01312       return HAL_OK;
01313     }
01314     else /* Process the rest of input data */
01315     {
01316       /* Get the Intput data address */
01317       inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
01318       
01319       /* Increment/decrement instance pointer/counter */
01320       hcryp->pCrypInBuffPtr += 16;
01321       hcryp->CrypInCount -= 16;
01322       
01323       /* Write the next input block in the Data Input register */
01324       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
01325       inputaddr+=4;
01326       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
01327       inputaddr+=4;
01328       hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
01329       inputaddr+=4;
01330       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
01331       
01332       return HAL_OK;      
01333     }
01334   }
01335   else
01336   {
01337     return HAL_BUSY; 
01338   }
01339 }
01340         
01341 
01342 
01343 
01344 /**
01345   * @}
01346   */
01347 
01348 
01349 
01350 /**
01351   * @}
01352   */
01353 
01354 /**
01355   * @}
01356   */
01357   
01358 #endif /* defined(STM32L485xx) || defined(STM32L486xx) */
01359 
01360 #endif /* HAL_CRYP_MODULE_ENABLED */
01361 
01362 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
01363