TUKS MCU Introductory course / TUKS-COURSE-2-LED
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.5.1
00006   * @date    31-May-2016
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) 2016 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 (STM32L442xx) || defined (STM32L443xx) || 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_Exported_Functions_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       (+) De-Initialize the CRYP MSP
00145       
00146     [..]
00147     (@) Specific care must be taken to format the key and the Initialization Vector IV!
00148     
00149    [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where 
00150         b127 is the MSB and b0 the LSB, the key must be stored in MCU memory 
00151         (+) as a sequence of words where the MSB word comes first (occupies the
00152           lowest memory address) 
00153         (+) where each word is byte-swapped:
00154          (++)   address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120
00155          (++)   address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88
00156          (++)   address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56
00157          (++)   address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24                 
00158     [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}.
00159         The 4 32-bit words that make the key must be stored as follows in MCU memory:  
00160          (+)    address n+0 : 0x B12 B13 B14 B15
00161          (+)    address n+4 : 0x B8 B9 B10 B11
00162          (+)    address n+8 : 0x B4 B5 B6 B7
00163          (+)    address n+C : 0x B0 B1 B2 B3  
00164     [..]  which leads to the expected setting  
00165       (+)       AES_KEYR3 = 0x B15 B14 B13 B12   
00166       (+)       AES_KEYR2 = 0x B11 B10 B9 B8      
00167       (+)       AES_KEYR1 = 0x B7 B6 B5 B4        
00168       (+)       AES_KEYR0 = 0x B3 B2 B1 B0      
00169    
00170    [..]  Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}. 
00171          The 8 32-bit words that make the key must be stored as follows in MCU memory:
00172          (+)    address n+00 : 0x B28 B29 B30 B31
00173          (+)    address n+04 : 0x B24 B25 B26 B27
00174          (+)    address n+08 : 0x B20 B21 B22 B23
00175          (+)    address n+0C : 0x B16 B17 B18 B19            
00176          (+)    address n+10 : 0x B12 B13 B14 B15
00177          (+)    address n+14 : 0x B8 B9 B10 B11
00178          (+)    address n+18 : 0x B4 B5 B6 B7
00179          (+)    address n+1C : 0x B0 B1 B2 B3 
00180     [..]  which leads to the expected setting 
00181       (+)       AES_KEYR7 = 0x B31 B30 B29 B28   
00182       (+)       AES_KEYR6 = 0x B27 B26 B25 B24      
00183       (+)       AES_KEYR5 = 0x B23 B22 B21 B20        
00184       (+)       AES_KEYR4 = 0x B19 B18 B17 B16       
00185       (+)       AES_KEYR3 = 0x B15 B14 B13 B12   
00186       (+)       AES_KEYR2 = 0x B11 B10 B9 B8      
00187       (+)       AES_KEYR1 = 0x B7 B6 B5 B4        
00188       (+)       AES_KEYR0 = 0x B3 B2 B1 B0           
00189    
00190    [..] Initialization Vector IV (4 32-bit words) format must follow the same as 
00191         that of a 128-bit long key.   
00192   
00193   [..]                 
00194  
00195 @endverbatim
00196   * @{
00197   */
00198 
00199   /**
00200   * @brief  Initialize the CRYP according to the specified
00201   *         parameters in the CRYP_InitTypeDef and initialize the associated handle.                   
00202   * @note Specific care must be taken to format the key and the Initialization Vector IV 
00203   *       stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations 
00204   *       hereabove.              
00205   * @retval HAL status
00206   */
00207 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
00208 {   
00209   /* Check the CRYP handle allocation */
00210   if(hcryp == NULL)
00211   {
00212     return HAL_ERROR;
00213   }
00214   
00215   /* Check the instance */
00216   assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance));
00217   
00218   /* Check the parameters */
00219   assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
00220   assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
00221   assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode));
00222   /* ChainingMode parameter is irrelevant when mode is set to Key derivation */
00223   if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
00224   {
00225     assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode));
00226   }
00227   assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag));
00228   
00229   /*========================================================*/
00230   /* Check the proper operating/chaining modes combinations */
00231   /*========================================================*/  
00232   /* Check the proper chaining when the operating mode is key derivation and decryption */
00233   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
00234          ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR)           \
00235        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)      \
00236        || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)))        
00237   {
00238     return HAL_ERROR;
00239   }  
00240   /* Check that key derivation is not set in CMAC mode */  
00241   if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) 
00242    && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))        
00243   {
00244     return HAL_ERROR;
00245   }
00246   
00247   
00248   /*================*/
00249   /* Initialization */
00250   /*================*/  
00251   /* Initialization start */
00252   if(hcryp->State == HAL_CRYP_STATE_RESET)
00253   {
00254     /* Allocate lock resource and initialize it */
00255     hcryp->Lock = HAL_UNLOCKED;
00256 
00257     /* Init the low level hardware */
00258     HAL_CRYP_MspInit(hcryp);
00259   }
00260   
00261   /* Change the CRYP state */
00262   hcryp->State = HAL_CRYP_STATE_BUSY;  
00263   
00264   /* Disable the Peripheral */
00265   __HAL_CRYP_DISABLE();
00266   
00267   /*=============================================================*/
00268   /* AES initialization common to all operating modes            */ 
00269   /*=============================================================*/
00270   /* Set the Key size selection */
00271   MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize);
00272   
00273   /* Set the default CRYP phase when this parameter is not used.
00274      Phase is updated below in case of GCM/GMAC/CMAC setting. */
00275   hcryp->Phase = HAL_CRYP_PHASE_NOT_USED;
00276   
00277   
00278 
00279   /*=============================================================*/
00280   /* Carry on the initialization based on the AES operating mode */ 
00281   /*=============================================================*/
00282   /* Key derivation */ 
00283   if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
00284   {
00285     MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION);
00286     
00287     /* Configure the Key registers */
00288     if (CRYP_SetKey(hcryp) != HAL_OK)
00289     {
00290       return HAL_ERROR;
00291     }
00292   }
00293   else
00294   /* Encryption / Decryption (with or without key derivation) / authentication */
00295   {    
00296     /* Set data type, operating and chaining modes.
00297        In case of GCM or GMAC, data type is forced to 0b00 */
00298     if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
00299     {
00300       MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
00301     }
00302     else
00303     {
00304       MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
00305     }
00306 
00307     
00308    /* Specify the encryption/decryption phase in case of Galois counter mode (GCM), 
00309       Galois message authentication code (GMAC) or cipher message authentication code (CMAC) */
00310    if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
00311     || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
00312     {
00313       MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase);
00314       hcryp->Phase = HAL_CRYP_PHASE_START;
00315     }
00316 
00317     
00318     /* Configure the Key registers if no need to bypass this step */
00319     if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE)
00320     {
00321       if (CRYP_SetKey(hcryp) != HAL_OK)
00322       {
00323         return HAL_ERROR;
00324       }      
00325     }
00326     
00327     /* If applicable, configure the Initialization Vector */
00328     if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB)
00329     {
00330       if (CRYP_SetInitVector(hcryp) != HAL_OK)
00331       {
00332         return HAL_ERROR;
00333       }
00334     }
00335   }
00336 
00337   /* Reset CrypInCount and CrypOutCount */
00338   hcryp->CrypInCount = 0;
00339   hcryp->CrypOutCount = 0;
00340   
00341   /* Reset ErrorCode field */
00342   hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
00343   
00344   /* Reset Mode suspension request */
00345   hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
00346   
00347   /* Change the CRYP state */
00348   hcryp->State = HAL_CRYP_STATE_READY;
00349   
00350   /* Enable the Peripheral */
00351   __HAL_CRYP_ENABLE();
00352   
00353   /* Return function status */
00354   return HAL_OK;
00355 }
00356 
00357 /**
00358   * @brief  DeInitialize the CRYP peripheral. 
00359   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00360   *         the configuration information for CRYP module
00361   * @retval HAL status
00362   */
00363 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
00364 {
00365   /* Check the CRYP handle allocation */
00366   if(hcryp == NULL)
00367   {
00368     return HAL_ERROR;
00369   }
00370   
00371   /* Change the CRYP state */
00372   hcryp->State = HAL_CRYP_STATE_BUSY;
00373   
00374   /* Set the default CRYP phase */
00375   hcryp->Phase = HAL_CRYP_PHASE_READY;
00376   
00377   /* Reset CrypInCount and CrypOutCount */
00378   hcryp->CrypInCount = 0;
00379   hcryp->CrypOutCount = 0;
00380   
00381   /* Disable the CRYP Peripheral Clock */
00382   __HAL_CRYP_DISABLE();
00383   
00384   /* DeInit the low level hardware: CLOCK, NVIC.*/
00385   HAL_CRYP_MspDeInit(hcryp);
00386   
00387   /* Change the CRYP state */
00388   hcryp->State = HAL_CRYP_STATE_RESET;
00389   
00390   /* Release Lock */
00391   __HAL_UNLOCK(hcryp);
00392   
00393   /* Return function status */
00394   return HAL_OK;
00395 }
00396 
00397 /**
00398   * @brief  Initialize the CRYP MSP.
00399   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00400   *         the configuration information for CRYP module
00401   * @retval None
00402   */
00403 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
00404 {
00405   /* Prevent unused argument(s) compilation warning */
00406   UNUSED(hcryp);
00407 
00408   /* NOTE : This function should not be modified; when the callback is needed,
00409             the HAL_CRYP_MspInit can be implemented in the user file
00410    */
00411 }
00412 
00413 /**
00414   * @brief  DeInitialize CRYP MSP.
00415   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00416   *         the configuration information for CRYP module
00417   * @retval None
00418   */
00419 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
00420 {
00421   /* Prevent unused argument(s) compilation warning */
00422   UNUSED(hcryp);
00423 
00424   /* NOTE : This function should not be modified; when the callback is needed,
00425             the HAL_CRYP_MspDeInit can be implemented in the user file
00426    */
00427 }
00428 
00429 /**
00430   * @}
00431   */
00432 
00433 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions 
00434  *  @brief   Processing functions. 
00435  *
00436 @verbatim   
00437   ==============================================================================
00438                       ##### AES processing functions #####
00439   ==============================================================================  
00440     [..]  This section provides functions allowing to:
00441       (+) Encrypt plaintext using AES algorithm in different chaining modes
00442       (+) Decrypt cyphertext using AES algorithm in different chaining modes
00443     [..]  Three processing functions are available:
00444       (+) Polling mode
00445       (+) Interrupt mode
00446       (+) DMA mode
00447 
00448 @endverbatim
00449   * @{
00450   */
00451   
00452   
00453 /**
00454   * @brief  Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData.
00455   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00456   *         the configuration information for CRYP module
00457   * @param  pPlainData: Pointer to the plaintext buffer
00458   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00459   * @param  pCypherData: Pointer to the cyphertext buffer
00460   * @param  Timeout: Specify Timeout value 
00461   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00462   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).      
00463   * @retval HAL status
00464   */
00465 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
00466 {
00467   /* Re-initialize AES IP with proper parameters */
00468   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00469   {
00470     return HAL_ERROR;
00471   }
00472   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00473   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00474   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00475   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00476   {
00477     return HAL_ERROR;
00478   }
00479 
00480   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
00481 }
00482  
00483 
00484 /**
00485   * @brief  Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData.
00486   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00487   *         the configuration information for CRYP module
00488   * @param  pPlainData: Pointer to the plaintext buffer
00489   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00490   * @param  pCypherData: Pointer to the cyphertext buffer
00491   * @param  Timeout: Specify Timeout value
00492   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00493   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).     
00494   * @retval HAL status
00495   */
00496 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
00497 { 
00498   /* Re-initialize AES IP with proper parameters */
00499   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00500   {
00501     return HAL_ERROR;
00502   }
00503   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00504   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00505   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00506   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00507   {
00508     return HAL_ERROR;
00509   }
00510   
00511   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
00512 }
00513 
00514 
00515 /**
00516   * @brief  Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData
00517   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00518   *         the configuration information for CRYP module
00519   * @param  pPlainData: Pointer to the plaintext buffer
00520   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00521   * @param  pCypherData: Pointer to the cyphertext buffer
00522   * @param  Timeout: Specify Timeout value 
00523   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00524   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).    
00525   * @retval HAL status
00526   */
00527 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
00528 {
00529   /* Re-initialize AES IP with proper parameters */
00530   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00531   {
00532     return HAL_ERROR;
00533   }
00534   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00535   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00536   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00537   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00538   {
00539     return HAL_ERROR;
00540   }
00541 
00542   return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
00543 }
00544 
00545 /**
00546   * @brief  Decrypt pCypherData in AES ECB decryption mode with key derivation, 
00547   *         the decyphered data are available in pPlainData.
00548   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00549   *         the configuration information for CRYP module
00550   * @param  pCypherData: Pointer to the cyphertext buffer
00551   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00552   * @param  pPlainData: Pointer to the plaintext buffer
00553   * @param  Timeout: Specify Timeout value 
00554   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00555   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).   
00556   * @retval HAL status
00557   */
00558 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
00559 {
00560   /* Re-initialize AES IP with proper parameters */
00561   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00562   {
00563     return HAL_ERROR;
00564   }
00565   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00566   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00567   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00568   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00569   {
00570     return HAL_ERROR;
00571   }
00572 
00573   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
00574 }
00575 
00576 /**
00577   * @brief  Decrypt pCypherData in AES ECB decryption mode with key derivation, 
00578   *         the decyphered data are available in pPlainData.
00579   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00580   *         the configuration information for CRYP module
00581   * @param  pCypherData: Pointer to the cyphertext buffer
00582   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00583   * @param  pPlainData: Pointer to the plaintext buffer
00584   * @param  Timeout: Specify Timeout value 
00585   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00586   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).    
00587   * @retval HAL status
00588   */
00589 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
00590 {
00591   /* Re-initialize AES IP with proper parameters */
00592   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00593   {
00594     return HAL_ERROR;
00595   }
00596   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00597   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00598   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00599   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00600   {
00601     return HAL_ERROR;
00602   }
00603   
00604   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
00605 }
00606 
00607 /**
00608   * @brief  Decrypt pCypherData in AES CTR decryption mode, 
00609   *         the decyphered data are available in pPlainData.
00610   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00611   *         the configuration information for CRYP module
00612   * @param  pCypherData: Pointer to the cyphertext buffer
00613   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00614   * @param  pPlainData: Pointer to the plaintext buffer
00615   * @param  Timeout: Specify Timeout value
00616   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00617   *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).     
00618   * @retval HAL status
00619   */
00620 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
00621 {
00622   /* Re-initialize AES IP with proper parameters */
00623   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00624   {
00625     return HAL_ERROR;
00626   }
00627   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
00628   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00629   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00630   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00631   {
00632     return HAL_ERROR;
00633   }  
00634   
00635   return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
00636 }
00637 
00638 /**
00639   * @brief  Encrypt pPlainData in AES ECB encryption mode using Interrupt,
00640   *         the cypher data are available in pCypherData.
00641   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00642   *         the configuration information for CRYP module
00643   * @param  pPlainData: Pointer to the plaintext buffer
00644   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00645   * @param  pCypherData: Pointer to the cyphertext buffer
00646   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00647   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).  
00648   * @retval HAL status
00649   */
00650 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00651 {
00652   /* Re-initialize AES IP with proper parameters */
00653   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00654   {
00655     return HAL_ERROR;
00656   }
00657   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00658   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00659   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00660   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00661   {
00662     return HAL_ERROR;
00663   }  
00664   
00665   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
00666 }
00667 
00668 /**
00669   * @brief  Encrypt pPlainData in AES CBC encryption mode using Interrupt,
00670   *         the cypher data are available in pCypherData.  
00671   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00672   *         the configuration information for CRYP module
00673   * @param  pPlainData: Pointer to the plaintext buffer
00674   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00675   * @param  pCypherData: Pointer to the cyphertext buffer
00676   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00677   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).   
00678   * @retval HAL status
00679   */
00680 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00681 {
00682   /* Re-initialize AES IP with proper parameters */
00683   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00684   {
00685     return HAL_ERROR;
00686   }
00687   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00688   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00689   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00690   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00691   {
00692     return HAL_ERROR;
00693   }
00694   
00695   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
00696 }
00697   
00698 
00699 /**
00700   * @brief  Encrypt pPlainData in AES CTR encryption mode using Interrupt,
00701   *         the cypher data are available in pCypherData.  
00702   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00703   *         the configuration information for CRYP module
00704   * @param  pPlainData: Pointer to the plaintext buffer
00705   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00706   * @param  pCypherData: Pointer to the cyphertext buffer
00707   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00708   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).   
00709   * @retval HAL status
00710   */
00711 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00712 {
00713   /* Re-initialize AES IP with proper parameters */
00714   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00715   {
00716     return HAL_ERROR;
00717   }
00718   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00719   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00720   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00721   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00722   {
00723     return HAL_ERROR;
00724   }
00725   
00726   return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
00727 }
00728 
00729 /**
00730   * @brief  Decrypt pCypherData in AES ECB decryption mode using Interrupt,
00731   *         the decyphered data are available in pPlainData.   
00732   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00733   *         the configuration information for CRYP module
00734   * @param  pCypherData: Pointer to the cyphertext buffer
00735   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00736   * @param  pPlainData: Pointer to the plaintext buffer.
00737   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00738   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).      
00739   * @retval HAL status
00740   */
00741 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00742 {
00743   /* Re-initialize AES IP with proper parameters */
00744   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00745   {
00746     return HAL_ERROR;
00747   }
00748   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00749   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00750   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00751   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00752   {
00753     return HAL_ERROR;
00754   }
00755   
00756   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
00757 }
00758 
00759 /**
00760   * @brief  Decrypt pCypherData in AES CBC decryption mode using Interrupt,
00761   *         the decyphered data are available in pPlainData.  
00762   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00763   *         the configuration information for CRYP module
00764   * @param  pCypherData: Pointer to the cyphertext buffer
00765   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00766   * @param  pPlainData: Pointer to the plaintext buffer
00767   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00768   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).  
00769   * @retval HAL status
00770   */
00771 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00772 {
00773   /* Re-initialize AES IP with proper parameters */
00774   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00775   {
00776     return HAL_ERROR;
00777   }
00778   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00779   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00780   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00781   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00782   {
00783     return HAL_ERROR;
00784   }
00785   
00786   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
00787 }
00788 
00789 /**
00790   * @brief  Decrypt pCypherData in AES CTR decryption mode using Interrupt,
00791   *         the decyphered data are available in pPlainData. 
00792   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00793   *         the configuration information for CRYP module
00794   * @param  pCypherData: Pointer to the cyphertext buffer
00795   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00796   * @param  pPlainData: Pointer to the plaintext buffer
00797   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00798   *         resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).    
00799   * @retval HAL status
00800   */
00801 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00802 {
00803   /* Re-initialize AES IP with proper parameters */
00804   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00805   {
00806     return HAL_ERROR;
00807   }
00808   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
00809   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00810   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00811   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00812   {
00813     return HAL_ERROR;
00814   }  
00815   
00816   return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
00817 }
00818 
00819 /**
00820   * @brief  Encrypt pPlainData in AES ECB encryption mode using DMA,
00821   *         the cypher data are available in pCypherData.   
00822   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00823   *         the configuration information for CRYP module
00824   * @param  pPlainData: Pointer to the plaintext buffer
00825   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00826   * @param  pCypherData: Pointer to the cyphertext buffer
00827   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00828   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
00829   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.    
00830   * @retval HAL status
00831   */
00832 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00833 {
00834   /* Re-initialize AES IP with proper parameters */
00835   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00836   {
00837     return HAL_ERROR;
00838   }
00839   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00840   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00841   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00842   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00843   {
00844     return HAL_ERROR;
00845   }
00846   
00847   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
00848 }
00849   
00850  
00851 
00852 /**
00853   * @brief  Encrypt pPlainData in AES CBC encryption mode using DMA,
00854   *         the cypher data are available in pCypherData.  
00855   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00856   *         the configuration information for CRYP module
00857   * @param  pPlainData: Pointer to the plaintext buffer
00858   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.
00859   * @param  pCypherData: Pointer to the cyphertext buffer
00860   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00861   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
00862   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.       
00863   * @retval HAL status
00864   */
00865 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00866 {
00867   /* Re-initialize AES IP with proper parameters */
00868   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00869   {
00870     return HAL_ERROR;
00871   }
00872   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00873   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00874   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00875   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00876   {
00877     return HAL_ERROR;
00878   }
00879   
00880   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
00881 }
00882 
00883 /**
00884   * @brief  Encrypt pPlainData in AES CTR encryption mode using DMA,
00885   *         the cypher data are available in pCypherData. 
00886   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00887   *         the configuration information for CRYP module
00888   * @param  pPlainData: Pointer to the plaintext buffer
00889   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00890   * @param  pCypherData: Pointer to the cyphertext buffer.
00891   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00892   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
00893   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.      
00894   * @retval HAL status
00895   */
00896 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
00897 {
00898   /* Re-initialize AES IP with proper parameters */
00899   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00900   {
00901     return HAL_ERROR;
00902   }
00903   hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
00904   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00905   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00906   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00907   {
00908     return HAL_ERROR;
00909   }
00910 
00911   return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
00912 }
00913 
00914 /**
00915   * @brief  Decrypt pCypherData in AES ECB decryption mode using DMA,
00916   *         the decyphered data are available in pPlainData.   
00917   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00918   *         the configuration information for CRYP module
00919   * @param  pCypherData: Pointer to the cyphertext buffer
00920   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00921   * @param  pPlainData: Pointer to the plaintext buffer
00922   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00923   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). 
00924   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.     
00925   * @retval HAL status
00926   */
00927 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00928 {
00929   /* Re-initialize AES IP with proper parameters */
00930   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00931   {
00932     return HAL_ERROR;
00933   }
00934   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00935   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
00936   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00937   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00938   {
00939     return HAL_ERROR;
00940   }
00941   
00942   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
00943 }
00944 
00945 /**
00946   * @brief  Decrypt pCypherData in AES CBC decryption mode using DMA,
00947   *         the decyphered data are available in pPlainData.  
00948   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00949   *         the configuration information for CRYP module
00950   * @param  pCypherData: Pointer to the cyphertext buffer
00951   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00952   * @param  pPlainData: Pointer to the plaintext buffer
00953   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00954   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
00955   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.      
00956   * @retval HAL status
00957   */
00958 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00959 {
00960   /* Re-initialize AES IP with proper parameters */
00961   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00962   {
00963     return HAL_ERROR;
00964   }
00965   hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
00966   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
00967   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00968   if (HAL_CRYP_Init(hcryp) != HAL_OK)
00969   {
00970     return HAL_ERROR;
00971   }
00972   
00973   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
00974 }
00975 
00976 /**
00977   * @brief  Decrypt pCypherData in AES CTR decryption mode using DMA,
00978   *         the decyphered data are available in pPlainData. 
00979   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00980   *         the configuration information for CRYP module
00981   * @param  pCypherData: Pointer to the cyphertext buffer
00982   * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
00983   * @param  pPlainData: Pointer to the plaintext buffer
00984   * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
00985   *         resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). 
00986   * @note   pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.     
00987   * @retval HAL status
00988   */
00989 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
00990 {
00991   /* Re-initialize AES IP with proper parameters */
00992   if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
00993   {
00994     return HAL_ERROR;
00995   }
00996   hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
00997   hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
00998   hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
00999   if (HAL_CRYP_Init(hcryp) != HAL_OK)
01000   {
01001     return HAL_ERROR;
01002   }  
01003   
01004   return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
01005 }
01006 
01007 
01008 /**
01009   * @}
01010   */
01011 
01012 /** @defgroup CRYP_Exported_Functions_Group3 Callback functions 
01013  *  @brief   Callback functions. 
01014  *
01015 @verbatim   
01016   ==============================================================================
01017                       ##### Callback functions  #####
01018   ==============================================================================  
01019     [..]  This section provides Interruption and DMA callback functions:
01020       (+) DMA Input data transfer complete
01021       (+) DMA Output data transfer complete
01022       (+) DMA or Interrupt error
01023 
01024 @endverbatim
01025   * @{
01026   */
01027 
01028 /**
01029   * @brief  CRYP error callback.
01030   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01031   *         the configuration information for CRYP module
01032   * @retval None
01033   */
01034 __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
01035 {
01036   /* Prevent unused argument(s) compilation warning */
01037   UNUSED(hcryp);
01038 
01039   /* NOTE : This function should not be modified; when the callback is needed,
01040             the HAL_CRYP_ErrorCallback can be implemented in the user file
01041    */
01042 }
01043 
01044 /**
01045   * @brief  Input DMA transfer complete callback.
01046   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01047   *         the configuration information for CRYP module
01048   * @retval None
01049   */
01050 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
01051 {
01052   /* Prevent unused argument(s) compilation warning */
01053   UNUSED(hcryp);
01054 
01055   /* NOTE : This function should not be modified; when the callback is needed,
01056             the HAL_CRYP_InCpltCallback can be implemented in the user file
01057    */
01058 }
01059 
01060 /**
01061   * @brief  Output DMA transfer complete callback.
01062   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01063   *         the configuration information for CRYP module
01064   * @retval None
01065   */
01066 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
01067 {
01068   /* Prevent unused argument(s) compilation warning */
01069   UNUSED(hcryp);
01070 
01071   /* NOTE : This function should not be modified; when the callback is needed,
01072             the HAL_CRYP_OutCpltCallback can be implemented in the user file
01073    */
01074 }
01075 
01076 /**
01077   * @}
01078   */
01079 
01080 /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler 
01081  *  @brief   AES IRQ handler.
01082  *
01083 @verbatim   
01084   ==============================================================================
01085                 ##### AES IRQ handler management #####
01086   ==============================================================================  
01087 [..]  This section provides AES IRQ handler function.
01088 
01089 @endverbatim
01090   * @{
01091   */
01092 
01093 /**
01094   * @brief  Handle AES interrupt request.
01095   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01096   *         the configuration information for CRYP module
01097   * @retval None
01098   */
01099 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
01100 {
01101   /* Check if error occurred */
01102   if (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_ERRIE) != RESET)
01103   {
01104     /* If Write Error occurred */
01105     if (__HAL_CRYP_GET_FLAG(CRYP_IT_WRERR) != RESET)
01106     {
01107       hcryp->ErrorCode |= HAL_CRYP_WRITE_ERROR;
01108       hcryp->State = HAL_CRYP_STATE_ERROR;
01109     }
01110     /* If Read Error occurred */
01111     if (__HAL_CRYP_GET_FLAG(CRYP_IT_RDERR) != RESET)
01112     {
01113       hcryp->ErrorCode |= HAL_CRYP_READ_ERROR;
01114       hcryp->State = HAL_CRYP_STATE_ERROR;
01115     }
01116     
01117     /* If an error has been reported */
01118     if (hcryp->State == HAL_CRYP_STATE_ERROR)
01119     {  
01120       /* Disable Error and Computation Complete Interrupts */
01121       __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
01122       /* Clear all Interrupt flags */
01123       __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR|CRYP_CCF_CLEAR);
01124     
01125       /* Process Unlocked */
01126       __HAL_UNLOCK(hcryp);  
01127     
01128       HAL_CRYP_ErrorCallback(hcryp);
01129   
01130       return; 
01131     }
01132   }
01133   
01134   /* Check if computation complete interrupt is enabled 
01135      and if the computation complete flag is raised */
01136   if((__HAL_CRYP_GET_FLAG(CRYP_IT_CCF) != RESET) && (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_CCFIE) != RESET))
01137   {    
01138     if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
01139      || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
01140     {
01141      /* To ensure proper suspension requests management, CCF flag 
01142         is reset in CRYP_AES_Auth_IT() according to the current 
01143         phase under handling */
01144       CRYP_AES_Auth_IT(hcryp);
01145     }
01146     else
01147     {
01148       /* Clear Computation Complete Flag */
01149       __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
01150       CRYP_AES_IT(hcryp);
01151     }
01152   }
01153 }
01154 
01155 /**
01156   * @}
01157   */
01158 
01159 /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions 
01160  *  @brief   Peripheral State functions. 
01161  *
01162 @verbatim   
01163   ==============================================================================
01164                       ##### Peripheral State functions #####
01165   ==============================================================================  
01166     [..]
01167     This subsection permits to get in run-time the status of the peripheral.
01168 
01169 @endverbatim
01170   * @{
01171   */
01172 
01173 /**
01174   * @brief  Return the CRYP handle state.
01175   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01176   *         the configuration information for CRYP module
01177   * @retval HAL state
01178   */
01179 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
01180 {
01181   /* Return CRYP handle state */
01182   return hcryp->State;
01183 }
01184 
01185 /**
01186   * @brief  Return the CRYP peripheral error.
01187   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01188   *         the configuration information for CRYP module
01189   * @note   The returned error is a bit-map combination of possible errors          
01190   * @retval Error bit-map
01191   */
01192 uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
01193 {
01194   return hcryp->ErrorCode;
01195 }
01196 
01197 /**
01198   * @}
01199   */
01200 
01201 /**
01202   * @}
01203   */
01204 
01205 /** @addtogroup CRYP_Private_Functions
01206   * @{
01207   */
01208 
01209 
01210 /**
01211   * @brief  Write the Key in KeyRx registers. 
01212   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01213   *         the configuration information for CRYP module
01214   * @retval None
01215   */
01216 static HAL_StatusTypeDef  CRYP_SetKey(CRYP_HandleTypeDef *hcryp)
01217 {  
01218   uint32_t keyaddr = 0x0;
01219   
01220   if ((uint32_t)(hcryp->Init.pKey == NULL))
01221   {
01222     return HAL_ERROR;
01223   }
01224   
01225   
01226   keyaddr = (uint32_t)(hcryp->Init.pKey);
01227   
01228   if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
01229   {
01230     hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
01231     keyaddr+=4;
01232     hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
01233     keyaddr+=4;
01234     hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
01235     keyaddr+=4;
01236     hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
01237     keyaddr+=4;      
01238   }  
01239   
01240   hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
01241   keyaddr+=4;
01242   hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
01243   keyaddr+=4;
01244   hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
01245   keyaddr+=4;
01246   hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));  
01247   
01248   return HAL_OK;
01249 }
01250 
01251 /**
01252   * @brief  Write the InitVector/InitCounter in IVRx registers. 
01253   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01254   *         the configuration information for CRYP module
01255   * @retval None
01256   */
01257 static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp)
01258 {
01259   uint32_t ivaddr = 0x0;
01260  
01261   if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
01262   {
01263     hcryp->Instance->IVR3 = 0;
01264     hcryp->Instance->IVR2 = 0;
01265     hcryp->Instance->IVR1 = 0;        
01266     hcryp->Instance->IVR0 = 0;
01267   }
01268   else
01269   {
01270     if (hcryp->Init.pInitVect == NULL)
01271     {
01272       return HAL_ERROR;
01273     } 
01274   
01275     ivaddr = (uint32_t)(hcryp->Init.pInitVect);
01276   
01277     hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
01278     ivaddr+=4;
01279     hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
01280     ivaddr+=4;
01281     hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
01282     ivaddr+=4;
01283     hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
01284   }
01285   return HAL_OK;
01286 }
01287 
01288 
01289 
01290 /** 
01291   * @brief  Handle CRYP block input/output data handling under interruption.
01292   * @note   The function is called under interruption only, once
01293   *         interruptions have been enabled by HAL_CRYPEx_AES_IT().
01294   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
01295   *         the configuration information for CRYP module.
01296   * @retval HAL status
01297   */
01298 static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
01299 {
01300   uint32_t inputaddr = 0;
01301   uint32_t outputaddr = 0;  
01302 
01303   if(hcryp->State == HAL_CRYP_STATE_BUSY)
01304   {
01305     if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
01306     {
01307       /* Get the output data address */
01308       outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
01309       
01310       /* Read the last available output block from the Data Output Register */
01311       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01312       outputaddr+=4;
01313       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01314       outputaddr+=4;
01315       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01316       outputaddr+=4;
01317       *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
01318       hcryp->pCrypOutBuffPtr += 16;
01319       hcryp->CrypOutCount -= 16;
01320     
01321     }
01322     else
01323     {
01324       /* Read the derived key from the Key registers */
01325       if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
01326       {   
01327         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
01328         outputaddr+=4;
01329         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
01330         outputaddr+=4;
01331         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
01332         outputaddr+=4;
01333         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
01334         outputaddr+=4;
01335       }
01336       
01337         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
01338         outputaddr+=4;
01339         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
01340         outputaddr+=4;
01341         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
01342         outputaddr+=4;
01343         *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
01344     }
01345     
01346     /* In case of ciphering or deciphering, check if all output text has been retrieved;
01347        In case of key derivation, stop right there */
01348     if ((hcryp->CrypOutCount == 0) || (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION))
01349     {
01350       /* Disable Computation Complete Flag and Errors Interrupts */
01351       __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
01352       /* Change the CRYP state */
01353       hcryp->State = HAL_CRYP_STATE_READY;
01354       
01355      /* Process Unlocked */
01356       __HAL_UNLOCK(hcryp);
01357       
01358       /* Call computation complete callback */
01359       HAL_CRYPEx_ComputationCpltCallback(hcryp);
01360       
01361       return HAL_OK;
01362     }
01363     /* If suspension flag has been raised, suspend processing */
01364     else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
01365     {
01366       /* reset ModeSuspend */
01367       hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
01368       
01369       /* Disable Computation Complete Flag and Errors Interrupts */
01370       __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
01371       /* Change the CRYP state */
01372       hcryp->State = HAL_CRYP_STATE_SUSPENDED;
01373       
01374      /* Process Unlocked */
01375       __HAL_UNLOCK(hcryp);
01376       
01377       return HAL_OK;
01378     }
01379     else /* Process the rest of input data */
01380     {
01381       /* Get the Intput data address */
01382       inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
01383       
01384       /* Increment/decrement instance pointer/counter */
01385       hcryp->pCrypInBuffPtr += 16;
01386       hcryp->CrypInCount -= 16;
01387       
01388       /* Write the next input block in the Data Input register */
01389       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
01390       inputaddr+=4;
01391       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
01392       inputaddr+=4;
01393       hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
01394       inputaddr+=4;
01395       hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
01396       
01397       return HAL_OK;      
01398     }
01399   }
01400   else
01401   {
01402     return HAL_BUSY; 
01403   }
01404 }
01405         
01406 
01407 
01408 
01409 /**
01410   * @}
01411   */
01412 
01413 
01414 
01415 /**
01416   * @}
01417   */
01418 
01419 /**
01420   * @}
01421   */
01422   
01423 #endif /* defined (STM32L442xx) || defined (STM32L443xx) || defined(STM32L485xx) || defined(STM32L486xx) */  
01424 
01425 #endif /* HAL_CRYP_MODULE_ENABLED */
01426 
01427 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/