STM32F479xx HAL User Manual
stm32f4xx_hal_cryp_ex.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_hal_cryp_ex.c
00004   * @author  MCD Application Team
00005   * @brief   Extended CRYP HAL module driver
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of CRYP extension peripheral:
00008   *           + Extended AES processing functions
00009   *
00010   @verbatim
00011   ==============================================================================
00012                      ##### How to use this driver #####
00013   ==============================================================================
00014     [..]
00015     The CRYP extension HAL driver can be used as follows:
00016     (#)After AES-GCM or AES-CCM  Encryption/Decryption user can start following API
00017        to get the  authentication messages :
00018       (##) HAL_CRYPEx_AESGCM_GenerateAuthTAG
00019       (##) HAL_CRYPEx_AESCCM_GenerateAuthTAG
00020 
00021   @endverbatim
00022   ******************************************************************************
00023   * @attention
00024   *
00025   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00026   * All rights reserved.</center></h2>
00027   *
00028   * This software component is licensed by ST under BSD 3-Clause license,
00029   * the "License"; You may not use this file except in compliance with the
00030   * License. You may obtain a copy of the License at:
00031   *                        opensource.org/licenses/BSD-3-Clause
00032   *
00033   ******************************************************************************
00034   */
00035 
00036 /* Includes ------------------------------------------------------------------*/
00037 #include "stm32f4xx_hal.h"
00038 
00039 /** @addtogroup STM32F4xx_HAL_Driver
00040   * @{
00041   */
00042 #if defined (AES)  || defined (CRYP)
00043 #if defined (CRYP_CR_ALGOMODE_AES_GCM)|| defined (AES)
00044 /** @defgroup CRYPEx CRYPEx
00045   * @brief CRYP Extension HAL module driver.
00046   * @{
00047   */
00048 
00049 
00050 #ifdef HAL_CRYP_MODULE_ENABLED
00051 
00052 /* Private typedef -----------------------------------------------------------*/
00053 /* Private define ------------------------------------------------------------*/
00054 /** @addtogroup CRYPEx_Private_Defines
00055   * @{
00056   */
00057 #if defined(AES)
00058 #define CRYP_PHASE_INIT                              0x00000000U             /*!< GCM/GMAC (or CCM) init phase */
00059 #define CRYP_PHASE_HEADER                            AES_CR_GCMPH_0          /*!< GCM/GMAC or CCM header phase */
00060 #define CRYP_PHASE_PAYLOAD                           AES_CR_GCMPH_1          /*!< GCM(/CCM) payload phase   */
00061 #define CRYP_PHASE_FINAL                             AES_CR_GCMPH            /*!< GCM/GMAC or CCM  final phase  */
00062 
00063 #define CRYP_OPERATINGMODE_ENCRYPT                   0x00000000U             /*!< Encryption mode   */
00064 #define CRYP_OPERATINGMODE_KEYDERIVATION             AES_CR_MODE_0           /*!< Key derivation mode  only used when performing ECB and CBC decryptions  */
00065 #define CRYP_OPERATINGMODE_DECRYPT                   AES_CR_MODE_1           /*!< Decryption       */
00066 #define CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT     AES_CR_MODE             /*!< Key derivation and decryption only used when performing ECB and CBC decryptions  */
00067 
00068 #else /* CRYP */
00069 
00070 #define CRYP_PHASE_INIT                 0x00000000U
00071 #define CRYP_PHASE_HEADER               CRYP_CR_GCM_CCMPH_0
00072 #define CRYP_PHASE_PAYLOAD              CRYP_CR_GCM_CCMPH_1
00073 #define CRYP_PHASE_FINAL                CRYP_CR_GCM_CCMPH
00074 
00075 #define CRYP_OPERATINGMODE_ENCRYPT      0x00000000U
00076 #define CRYP_OPERATINGMODE_DECRYPT      CRYP_CR_ALGODIR
00077 #endif /* End AES or CRYP */
00078 
00079 #define  CRYPEx_PHASE_PROCESS       0x02U     /*!< CRYP peripheral is in processing phase */
00080 #define  CRYPEx_PHASE_FINAL         0x03U     /*!< CRYP peripheral is in final phase this is relevant only with CCM and GCM modes */
00081 
00082 /*  CTR0 information to use in CCM algorithm */
00083 #define CRYP_CCM_CTR0_0            0x07FFFFFFU
00084 #define CRYP_CCM_CTR0_3            0xFFFFFF00U
00085 
00086 
00087 /**
00088   * @}
00089   */
00090 
00091 /* Private macro -------------------------------------------------------------*/
00092 /* Private variables ---------------------------------------------------------*/
00093 /* Private function prototypes -----------------------------------------------*/
00094 
00095 
00096 
00097 /* Exported functions---------------------------------------------------------*/
00098 /** @addtogroup CRYPEx_Exported_Functions
00099   * @{
00100   */
00101 
00102 /** @defgroup CRYPEx_Exported_Functions_Group1 Extended AES processing functions
00103   *  @brief   Extended processing functions.
00104   *
00105 @verbatim
00106   ==============================================================================
00107               ##### Extended AES processing functions #####
00108   ==============================================================================
00109     [..]  This section provides functions allowing to generate the authentication
00110           TAG in Polling mode
00111       (#)HAL_CRYPEx_AESGCM_GenerateAuthTAG
00112       (#)HAL_CRYPEx_AESCCM_GenerateAuthTAG
00113          they should be used after Encrypt/Decrypt operation.
00114 
00115 @endverbatim
00116   * @{
00117   */
00118 
00119 
00120 /**
00121   * @brief  generate the GCM authentication TAG.
00122   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00123   *         the configuration information for CRYP module
00124   * @param  AuthTag: Pointer to the authentication buffer
00125   * @param  Timeout: Timeout duration
00126   * @retval HAL status
00127   */
00128 HAL_StatusTypeDef HAL_CRYPEx_AESGCM_GenerateAuthTAG(CRYP_HandleTypeDef *hcryp, uint32_t *AuthTag, uint32_t Timeout)
00129 {
00130   uint32_t tickstart;
00131   /* Assume first Init.HeaderSize is in words */
00132   uint64_t headerlength = (uint64_t)(hcryp->Init.HeaderSize) * 32U; /* Header length in bits */
00133   uint64_t inputlength = (uint64_t)hcryp->SizesSum * 8U; /* Input length in bits */
00134   uint32_t tagaddr = (uint32_t)AuthTag;
00135 
00136   /* Correct headerlength if Init.HeaderSize is actually in bytes */
00137   if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_BYTE)
00138   {
00139     headerlength /= 4U;
00140   }
00141 
00142   if (hcryp->State == HAL_CRYP_STATE_READY)
00143   {
00144     /* Process locked */
00145     __HAL_LOCK(hcryp);
00146 
00147     /* Change the CRYP peripheral state */
00148     hcryp->State = HAL_CRYP_STATE_BUSY;
00149 
00150     /* Check if initialization phase has already been performed */
00151     if (hcryp->Phase == CRYPEx_PHASE_PROCESS)
00152     {
00153       /* Change the CRYP phase */
00154       hcryp->Phase = CRYPEx_PHASE_FINAL;
00155     }
00156     else /* Initialization phase has not been performed*/
00157     {
00158       /* Disable the Peripheral */
00159       __HAL_CRYP_DISABLE(hcryp);
00160 
00161       /* Sequence error code field */
00162       hcryp->ErrorCode |= HAL_CRYP_ERROR_AUTH_TAG_SEQUENCE;
00163 
00164       /* Change the CRYP peripheral state */
00165       hcryp->State = HAL_CRYP_STATE_READY;
00166 
00167       /* Process unlocked */
00168       __HAL_UNLOCK(hcryp);
00169       return HAL_ERROR;
00170     }
00171 
00172 #if defined(CRYP)
00173 
00174     /* Disable CRYP to start the final phase */
00175     __HAL_CRYP_DISABLE(hcryp);
00176 
00177     /* Select final phase */
00178     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH, CRYP_PHASE_FINAL);
00179 
00180     /*ALGODIR bit must be set to ‘0’.*/
00181     hcryp->Instance->CR &=  ~CRYP_CR_ALGODIR;
00182 
00183     /* Enable the CRYP peripheral */
00184     __HAL_CRYP_ENABLE(hcryp);
00185 
00186     /* Write the number of bits in header (64 bits) followed by the number of bits
00187     in the payload */
00188     if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
00189     {
00190       hcryp->Instance->DIN = 0U;
00191       hcryp->Instance->DIN = __RBIT((uint32_t)(headerlength));
00192       hcryp->Instance->DIN = 0U;
00193       hcryp->Instance->DIN = __RBIT((uint32_t)(inputlength));
00194     }
00195     else if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
00196     {
00197       hcryp->Instance->DIN = 0U;
00198       hcryp->Instance->DIN = __REV((uint32_t)(headerlength));
00199       hcryp->Instance->DIN = 0U;
00200       hcryp->Instance->DIN = __REV((uint32_t)(inputlength));
00201     }
00202     else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
00203     {
00204       hcryp->Instance->DIN = 0U;
00205       hcryp->Instance->DIN = __ROR((uint32_t)headerlength, 16U);
00206       hcryp->Instance->DIN = 0U;
00207       hcryp->Instance->DIN = __ROR((uint32_t)inputlength, 16U);
00208     }
00209     else if (hcryp->Init.DataType == CRYP_DATATYPE_32B)
00210     {
00211       hcryp->Instance->DIN = 0U;
00212       hcryp->Instance->DIN = (uint32_t)(headerlength);
00213       hcryp->Instance->DIN = 0U;
00214       hcryp->Instance->DIN = (uint32_t)(inputlength);
00215     }
00216     else
00217     {
00218       /* Nothing to do */
00219     }
00220 
00221     /* Wait for OFNE flag to be raised */
00222     tickstart = HAL_GetTick();
00223     while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
00224     {
00225       /* Check for the Timeout */
00226       if (Timeout != HAL_MAX_DELAY)
00227       {
00228         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
00229         {
00230           /* Disable the CRYP Peripheral Clock */
00231           __HAL_CRYP_DISABLE(hcryp);
00232 
00233           /* Change state */
00234           hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
00235           hcryp->State = HAL_CRYP_STATE_READY;
00236 
00237           /* Process unlocked */
00238           __HAL_UNLOCK(hcryp);
00239           return HAL_ERROR;
00240         }
00241       }
00242     }
00243 
00244     /* Read the authentication TAG in the output FIFO */
00245     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
00246     tagaddr += 4U;
00247     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
00248     tagaddr += 4U;
00249     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
00250     tagaddr += 4U;
00251     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
00252 
00253 #else /* AES*/
00254 
00255     /* Select final phase */
00256     MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_FINAL);
00257 
00258     /* Write the number of bits in header (64 bits) followed by the number of bits
00259     in the payload */
00260     if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
00261     {
00262       hcryp->Instance->DINR = 0U;
00263       hcryp->Instance->DINR = __RBIT((uint32_t)(headerlength));
00264       hcryp->Instance->DINR = 0U;
00265       hcryp->Instance->DINR = __RBIT((uint32_t)(inputlength));
00266     }
00267     else if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
00268     {
00269       hcryp->Instance->DINR = 0U;
00270       hcryp->Instance->DINR = __REV((uint32_t)(headerlength));
00271       hcryp->Instance->DINR = 0U;
00272       hcryp->Instance->DINR = __REV((uint32_t)(inputlength));
00273     }
00274     else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
00275     {
00276       hcryp->Instance->DINR = 0U;
00277       hcryp->Instance->DINR = __ROR((uint32_t)headerlength, 16U);
00278       hcryp->Instance->DINR = 0U;
00279       hcryp->Instance->DINR = __ROR((uint32_t)inputlength, 16U);
00280     }
00281     else if (hcryp->Init.DataType == CRYP_DATATYPE_32B)
00282     {
00283       hcryp->Instance->DINR = 0U;
00284       hcryp->Instance->DINR = (uint32_t)(headerlength);
00285       hcryp->Instance->DINR = 0U;
00286       hcryp->Instance->DINR = (uint32_t)(inputlength);
00287     }
00288     else
00289     {
00290       /* Nothing to do */
00291     }
00292     /* Wait for CCF flag to be raised */
00293     tickstart = HAL_GetTick();
00294     while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
00295     {
00296       /* Check for the Timeout */
00297       if (Timeout != HAL_MAX_DELAY)
00298       {
00299         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
00300         {
00301           /* Disable the CRYP peripheral clock */
00302           __HAL_CRYP_DISABLE(hcryp);
00303 
00304           /* Change state */
00305           hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
00306           hcryp->State = HAL_CRYP_STATE_READY;
00307 
00308           /* Process unlocked */
00309           __HAL_UNLOCK(hcryp);
00310           return HAL_ERROR;
00311         }
00312       }
00313     }
00314 
00315     /* Read the authentication TAG in the output FIFO */
00316     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
00317     tagaddr += 4U;
00318     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
00319     tagaddr += 4U;
00320     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
00321     tagaddr += 4U;
00322     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
00323 
00324     /* Clear CCF flag */
00325     __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
00326 
00327 #endif /* End AES or CRYP */
00328 
00329     /* Disable the peripheral */
00330     __HAL_CRYP_DISABLE(hcryp);
00331 
00332     /* Change the CRYP peripheral state */
00333     hcryp->State = HAL_CRYP_STATE_READY;
00334 
00335     /* Process unlocked */
00336     __HAL_UNLOCK(hcryp);
00337   }
00338   else
00339   {
00340     /* Busy error code field */
00341     hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
00342     return HAL_ERROR;
00343   }
00344   /* Return function status */
00345   return HAL_OK;
00346 }
00347 
00348 /**
00349   * @brief  AES CCM Authentication TAG generation.
00350   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
00351   *         the configuration information for CRYP module
00352   * @param  AuthTag: Pointer to the authentication buffer
00353   * @param  Timeout: Timeout duration
00354   * @retval HAL status
00355   */
00356 HAL_StatusTypeDef HAL_CRYPEx_AESCCM_GenerateAuthTAG(CRYP_HandleTypeDef *hcryp, uint32_t *AuthTag, uint32_t Timeout)
00357 {
00358   uint32_t tagaddr = (uint32_t)AuthTag;
00359   uint32_t ctr0 [4] = {0};
00360   uint32_t ctr0addr = (uint32_t)ctr0;
00361   uint32_t tickstart;
00362 
00363   if (hcryp->State == HAL_CRYP_STATE_READY)
00364   {
00365     /* Process locked */
00366     __HAL_LOCK(hcryp);
00367 
00368     /* Change the CRYP peripheral state */
00369     hcryp->State = HAL_CRYP_STATE_BUSY;
00370 
00371     /* Check if initialization phase has already been performed */
00372     if (hcryp->Phase == CRYPEx_PHASE_PROCESS)
00373     {
00374       /* Change the CRYP phase */
00375       hcryp->Phase = CRYPEx_PHASE_FINAL;
00376     }
00377     else /* Initialization phase has not been performed*/
00378     {
00379       /* Disable the peripheral */
00380       __HAL_CRYP_DISABLE(hcryp);
00381 
00382       /* Sequence error code field */
00383       hcryp->ErrorCode |= HAL_CRYP_ERROR_AUTH_TAG_SEQUENCE;
00384 
00385       /* Change the CRYP peripheral state */
00386       hcryp->State = HAL_CRYP_STATE_READY;
00387 
00388       /* Process unlocked */
00389       __HAL_UNLOCK(hcryp);
00390       return HAL_ERROR;
00391     }
00392 
00393 #if defined(CRYP)
00394 
00395     /* Disable CRYP to start the final phase */
00396     __HAL_CRYP_DISABLE(hcryp);
00397 
00398     /* Select final phase & ALGODIR bit must be set to ‘0’. */
00399     MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH | CRYP_CR_ALGODIR, CRYP_PHASE_FINAL | CRYP_OPERATINGMODE_ENCRYPT);
00400 
00401     /* Enable the CRYP peripheral */
00402     __HAL_CRYP_ENABLE(hcryp);
00403 
00404     /* Write the counter block in the IN FIFO, CTR0 information from B0
00405     data has to be swapped according to the DATATYPE*/
00406     ctr0[0] = (hcryp->Init.B0[0]) & CRYP_CCM_CTR0_0;
00407     ctr0[1] = hcryp->Init.B0[1];
00408     ctr0[2] = hcryp->Init.B0[2];
00409     ctr0[3] = hcryp->Init.B0[3] &  CRYP_CCM_CTR0_3;
00410 
00411     if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
00412     {
00413       hcryp->Instance->DIN = __REV(*(uint32_t *)(ctr0addr));
00414       ctr0addr += 4U;
00415       hcryp->Instance->DIN = __REV(*(uint32_t *)(ctr0addr));
00416       ctr0addr += 4U;
00417       hcryp->Instance->DIN = __REV(*(uint32_t *)(ctr0addr));
00418       ctr0addr += 4U;
00419       hcryp->Instance->DIN = __REV(*(uint32_t *)(ctr0addr));
00420     }
00421     else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
00422     {
00423       hcryp->Instance->DIN = __ROR(*(uint32_t *)(ctr0addr), 16U);
00424       ctr0addr += 4U;
00425       hcryp->Instance->DIN = __ROR(*(uint32_t *)(ctr0addr), 16U);
00426       ctr0addr += 4U;
00427       hcryp->Instance->DIN = __ROR(*(uint32_t *)(ctr0addr), 16U);
00428       ctr0addr += 4U;
00429       hcryp->Instance->DIN = __ROR(*(uint32_t *)(ctr0addr), 16U);
00430     }
00431     else if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
00432     {
00433       hcryp->Instance->DIN = __RBIT(*(uint32_t *)(ctr0addr));
00434       ctr0addr += 4U;
00435       hcryp->Instance->DIN = __RBIT(*(uint32_t *)(ctr0addr));
00436       ctr0addr += 4U;
00437       hcryp->Instance->DIN = __RBIT(*(uint32_t *)(ctr0addr));
00438       ctr0addr += 4U;
00439       hcryp->Instance->DIN = __RBIT(*(uint32_t *)(ctr0addr));
00440     }
00441     else
00442     {
00443       hcryp->Instance->DIN = *(uint32_t *)(ctr0addr);
00444       ctr0addr += 4U;
00445       hcryp->Instance->DIN = *(uint32_t *)(ctr0addr);
00446       ctr0addr += 4U;
00447       hcryp->Instance->DIN = *(uint32_t *)(ctr0addr);
00448       ctr0addr += 4U;
00449       hcryp->Instance->DIN = *(uint32_t *)(ctr0addr);
00450     }
00451     /* Wait for OFNE flag to be raised */
00452     tickstart = HAL_GetTick();
00453     while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
00454     {
00455       /* Check for the Timeout */
00456       if (Timeout != HAL_MAX_DELAY)
00457       {
00458         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
00459         {
00460           /* Disable the CRYP peripheral Clock */
00461           __HAL_CRYP_DISABLE(hcryp);
00462 
00463           /* Change state */
00464           hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
00465           hcryp->State = HAL_CRYP_STATE_READY;
00466 
00467           /* Process unlocked */
00468           __HAL_UNLOCK(hcryp);
00469           return HAL_ERROR;
00470         }
00471       }
00472     }
00473 
00474     /* Read the Auth TAG in the IN FIFO */
00475     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
00476     tagaddr += 4U;
00477     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
00478     tagaddr += 4U;
00479     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
00480     tagaddr += 4U;
00481     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
00482 
00483 #else /* AES */
00484 
00485     /* Select final phase */
00486     MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_FINAL);
00487 
00488     /* Write the counter block in the IN FIFO, CTR0 information from B0
00489     data has to be swapped according to the DATATYPE*/
00490     if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
00491     {
00492       ctr0[0] = (__REV(hcryp->Init.B0[0]) & CRYP_CCM_CTR0_0);
00493       ctr0[1] = __REV(hcryp->Init.B0[1]);
00494       ctr0[2] = __REV(hcryp->Init.B0[2]);
00495       ctr0[3] = (__REV(hcryp->Init.B0[3])& CRYP_CCM_CTR0_3);
00496 
00497       hcryp->Instance->DINR = __REV(*(uint32_t *)(ctr0addr));
00498       ctr0addr += 4U;
00499       hcryp->Instance->DINR = __REV(*(uint32_t *)(ctr0addr));
00500       ctr0addr += 4U;
00501       hcryp->Instance->DINR = __REV(*(uint32_t *)(ctr0addr));
00502       ctr0addr += 4U;
00503       hcryp->Instance->DINR = __REV(*(uint32_t *)(ctr0addr));
00504     }
00505     else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
00506     {
00507       ctr0[0] = (__ROR((hcryp->Init.B0[0]), 16U)& CRYP_CCM_CTR0_0);
00508       ctr0[1] =   __ROR((hcryp->Init.B0[1]), 16U);
00509       ctr0[2] =   __ROR((hcryp->Init.B0[2]), 16U);
00510       ctr0[3] = (__ROR((hcryp->Init.B0[3]), 16U)& CRYP_CCM_CTR0_3);
00511 
00512       hcryp->Instance->DINR = __ROR(*(uint32_t *)(ctr0addr), 16U);
00513       ctr0addr += 4U;
00514       hcryp->Instance->DINR = __ROR(*(uint32_t *)(ctr0addr), 16U);
00515       ctr0addr += 4U;
00516       hcryp->Instance->DINR = __ROR(*(uint32_t *)(ctr0addr), 16U);
00517       ctr0addr += 4U;
00518       hcryp->Instance->DINR = __ROR(*(uint32_t *)(ctr0addr), 16U);
00519     }
00520     else if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
00521     {
00522       ctr0[0] = (__RBIT(hcryp->Init.B0[0])& CRYP_CCM_CTR0_0);
00523       ctr0[1] = __RBIT(hcryp->Init.B0[1]);
00524       ctr0[2] = __RBIT(hcryp->Init.B0[2]);
00525       ctr0[3] = (__RBIT(hcryp->Init.B0[3])& CRYP_CCM_CTR0_3);
00526 
00527       hcryp->Instance->DINR = __RBIT(*(uint32_t *)(ctr0addr));
00528       ctr0addr += 4U;
00529       hcryp->Instance->DINR = __RBIT(*(uint32_t *)(ctr0addr));
00530       ctr0addr += 4U;
00531       hcryp->Instance->DINR = __RBIT(*(uint32_t *)(ctr0addr));
00532       ctr0addr += 4U;
00533       hcryp->Instance->DINR = __RBIT(*(uint32_t *)(ctr0addr));
00534     }
00535     else
00536     {
00537       ctr0[0] = (hcryp->Init.B0[0]) & CRYP_CCM_CTR0_0;
00538       ctr0[1] = hcryp->Init.B0[1];
00539       ctr0[2] = hcryp->Init.B0[2];
00540       ctr0[3] = hcryp->Init.B0[3] &  CRYP_CCM_CTR0_3;
00541 
00542       hcryp->Instance->DINR = *(uint32_t *)(ctr0addr);
00543       ctr0addr += 4U;
00544       hcryp->Instance->DINR = *(uint32_t *)(ctr0addr);
00545       ctr0addr += 4U;
00546       hcryp->Instance->DINR = *(uint32_t *)(ctr0addr);
00547       ctr0addr += 4U;
00548       hcryp->Instance->DINR = *(uint32_t *)(ctr0addr);
00549     }
00550 
00551     /* Wait for CCF flag to be raised */
00552     tickstart = HAL_GetTick();
00553     while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
00554     {
00555       /* Check for the Timeout */
00556       if (Timeout != HAL_MAX_DELAY)
00557       {
00558         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
00559         {
00560           /* Disable the CRYP peripheral Clock */
00561           __HAL_CRYP_DISABLE(hcryp);
00562 
00563           /* Change state */
00564           hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
00565           hcryp->State = HAL_CRYP_STATE_READY;
00566 
00567           /* Process unlocked */
00568           __HAL_UNLOCK(hcryp);
00569           return HAL_ERROR;
00570         }
00571       }
00572     }
00573 
00574     /* Read the authentication TAG in the output FIFO */
00575     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
00576     tagaddr += 4U;
00577     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
00578     tagaddr += 4U;
00579     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
00580     tagaddr += 4U;
00581     *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
00582 
00583     /* Clear CCF Flag */
00584     __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
00585 
00586 #endif /* End of AES || CRYP */
00587 
00588     /* Change the CRYP peripheral state */
00589     hcryp->State = HAL_CRYP_STATE_READY;
00590 
00591     /* Process unlocked */
00592     __HAL_UNLOCK(hcryp);
00593 
00594     /* Disable CRYP  */
00595     __HAL_CRYP_DISABLE(hcryp);
00596   }
00597   else
00598   {
00599     /* Busy error code field */
00600     hcryp->ErrorCode = HAL_CRYP_ERROR_BUSY;
00601     return HAL_ERROR;
00602   }
00603   /* Return function status */
00604   return HAL_OK;
00605 }
00606 
00607 /**
00608   * @}
00609   */
00610 
00611 #if defined (AES)
00612 /** @defgroup CRYPEx_Exported_Functions_Group2 Key Derivation functions
00613   *  @brief   AutoKeyDerivation functions
00614   *
00615 @verbatim
00616   ==============================================================================
00617               ##### Key Derivation functions #####
00618   ==============================================================================
00619     [..]  This section provides functions allowing to Enable or Disable the
00620           the AutoKeyDerivation parameter in CRYP_HandleTypeDef structure
00621           These function are allowed only in TinyAES IP.
00622 
00623 @endverbatim
00624   * @{
00625   */
00626 
00627 /**
00628   * @brief  AES enable key derivation functions
00629   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure.
00630   * @retval None
00631   */
00632 void  HAL_CRYPEx_EnableAutoKeyDerivation(CRYP_HandleTypeDef *hcryp)
00633 {
00634   if (hcryp->State == HAL_CRYP_STATE_READY)
00635   {
00636     hcryp->AutoKeyDerivation = ENABLE;
00637   }
00638   else
00639   {
00640     /* Busy error code field */
00641     hcryp->ErrorCode = HAL_CRYP_ERROR_BUSY;
00642   }
00643 }
00644 /**
00645   * @brief  AES disable key derivation functions
00646   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure.
00647   * @retval None
00648   */
00649 void  HAL_CRYPEx_DisableAutoKeyDerivation(CRYP_HandleTypeDef *hcryp)
00650 {
00651   if (hcryp->State == HAL_CRYP_STATE_READY)
00652   {
00653     hcryp->AutoKeyDerivation = DISABLE;
00654   }
00655   else
00656   {
00657     /* Busy error code field */
00658     hcryp->ErrorCode = HAL_CRYP_ERROR_BUSY;
00659   }
00660 }
00661 
00662 /**
00663   * @}
00664   */
00665 #endif /* AES or GCM CCM defined*/
00666 #endif /* AES */
00667 #endif /* HAL_CRYP_MODULE_ENABLED */
00668 
00669 /**
00670   * @}
00671   */
00672 #endif /* TinyAES or CRYP*/
00673 /**
00674   * @}
00675   */
00676 
00677 /**
00678   * @}
00679   */
00680 
00681 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/