12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111 |
- /**
- ******************************************************************************
- * @file stm32l1xx_hal_cryp.c
- * @author MCD Application Team
- * @version V1.0.0
- * @date 5-September-2014
- * @brief CRYP HAL module driver.
- *
- * This file provides firmware functions to manage the following
- * functionalities of the Cryptography (CRYP) peripheral:
- * + Initialization and de-initialization functions
- * + Processing functions by algorithm using polling mode
- * + Processing functions by algorithm using interrupt mode
- * + Processing functions by algorithm using DMA mode
- * + Peripheral State functions
- *
- @verbatim
- ==============================================================================
- ##### How to use this driver #####
- ==============================================================================
- [..]
- The CRYP HAL driver can be used as follows:
- (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
- (##) Enable the CRYP interface clock using __CRYP_CLK_ENABLE()
- (##) In case of using interrupts (e.g. HAL_CRYP_AESECB_Encrypt_IT())
- (+) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
- (+) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
- (+) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
- (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_AESECB_Encrypt_DMA())
- (+) Enable the DMA2 interface clock using
- (++) __DMA2_CLK_ENABLE()
- (+) Configure and enable two DMA Channels one for managing data transfer from
- memory to peripheral (input channel) and another channel for managing data
- transfer from peripheral to memory (output channel)
- (+) Associate the initialized DMA handle to the CRYP DMA handle
- using __HAL_LINKDMA()
- (+) Configure the priority and enable the NVIC for the transfer complete
- interrupt on the two DMA Streams. The output stream should have higher
- priority than the input stream.
- (++) HAL_NVIC_SetPriority()
- (++) HAL_NVIC_EnableIRQ()
-
- (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly:
- (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit
- (##) The encryption/decryption key.
- (##) The initialization vector (counter). It is not used ECB mode.
-
- (#)Three processing (encryption/decryption) functions are available:
- (##) Polling mode: encryption and decryption APIs are blocking functions
- i.e. they process the data and wait till the processing is finished
- e.g. HAL_CRYP_AESCBC_Encrypt()
- (##) Interrupt mode: encryption and decryption APIs are not blocking functions
- i.e. they process the data under interrupt
- e.g. HAL_CRYP_AESCBC_Encrypt_IT()
- (##) DMA mode: encryption and decryption APIs are not blocking functions
- i.e. the data transfer is ensured by DMA
- e.g. HAL_CRYP_AESCBC_Encrypt_DMA()
-
- (#)When the processing function is called for the first time after HAL_CRYP_Init()
- the CRYP peripheral is initialized and processes the buffer in input.
- At second call, the processing function performs an append of the already
- processed buffer.
- When a new data block is to be processed, call HAL_CRYP_Init() then the
- processing function.
-
- (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
- @endverbatim
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32l1xx_hal.h"
- #ifdef HAL_CRYP_MODULE_ENABLED
- /** @addtogroup STM32L1xx_HAL_Driver
- * @{
- */
- /** @defgroup CRYP CRYP
- * @brief CRYP HAL module driver.
- * @{
- */
- #if defined(STM32L162xC) || defined(STM32L162xCA) || defined(STM32L162xD) || defined(STM32L162xE)
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /** @defgroup CRYP_Private_Defines CRYP Private Defines
- * @{
- */
- #define CRYP_ALGO_CHAIN_MASK (AES_CR_MODE | AES_CR_CHMOD)
- /**
- * @}
- */
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /** @defgroup CRYP_Private_Functions CRYP Private Functions
- * @{
- */
- static HAL_StatusTypeDef CRYP_EncryptDecrypt_IT(CRYP_HandleTypeDef *hcryp);
- static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector);
- static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key);
- static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
- static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
- static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
- static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
- static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
- /**
- * @}
- */
- /* Private functions ---------------------------------------------------------*/
- /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
- * @{
- */
- /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
- * @brief Initialization and Configuration functions.
- *
- @verbatim
- ==============================================================================
- ##### Initialization and de-initialization functions #####
- ==============================================================================
- [..] This section provides functions allowing to:
- (+) Initialize the CRYP according to the specified parameters
- in the CRYP_InitTypeDef and creates the associated handle
- (+) DeInitialize the CRYP peripheral
- (+) Initialize the CRYP MSP
- (+) DeInitialize CRYP MSP
-
- @endverbatim
- * @{
- */
- /**
- * @brief Initializes the CRYP according to the specified
- * parameters in the CRYP_InitTypeDef and creates the associated handle.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
- {
- /* Check the CRYP handle allocation */
- if(hcryp == NULL)
- {
- return HAL_ERROR;
- }
-
- /* Check the parameters */
- assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
-
- if(hcryp->State == HAL_CRYP_STATE_RESET)
- {
- /* Init the low level hardware */
- HAL_CRYP_MspInit(hcryp);
- }
-
- /* Check if AES already enabled */
- if (HAL_IS_BIT_CLR(AES->CR, AES_CR_EN))
- {
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
- /* Set the data type*/
- MODIFY_REG(AES->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
-
- /* Reset CrypInCount and CrypOutCount */
- hcryp->CrypInCount = 0;
- hcryp->CrypOutCount = 0;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_READY;
-
- /* Set the default CRYP phase */
- hcryp->Phase = HAL_CRYP_PHASE_READY;
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* The Datatype selection must be changed if the AES is disabled. Writing these bits while the AES is */
- /* enabled is forbidden to avoid unpredictable AES behavior.*/
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief DeInitializes the CRYP peripheral.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
- {
- /* Check the CRYP handle allocation */
- if(hcryp == NULL)
- {
- return HAL_ERROR;
- }
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Set the default CRYP phase */
- hcryp->Phase = HAL_CRYP_PHASE_READY;
-
- /* Reset CrypInCount and CrypOutCount */
- hcryp->CrypInCount = 0;
- hcryp->CrypOutCount = 0;
-
- /* Disable the CRYP Peripheral Clock */
- __HAL_CRYP_DISABLE();
-
- /* DeInit the low level hardware: CLOCK, NVIC.*/
- HAL_CRYP_MspDeInit(hcryp);
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_RESET;
-
- /* Release Lock */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Initializes the CRYP MSP.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @retval None
- */
- __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
- {
- /* NOTE : This function should not be modified; when the callback is needed,
- the HAL_CRYP_MspInit can be implemented in the user file */
- }
- /**
- * @brief DeInitializes CRYP MSP.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @retval None
- */
- __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
- {
- /* NOTE : This function should not be modified; when the callback is needed,
- the HAL_CRYP_MspDeInit can be implemented in the user file */
- }
- /**
- * @}
- */
- /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
- * @brief processing functions.
- *
- @verbatim
- ==============================================================================
- ##### AES processing functions #####
- ==============================================================================
- [..] This section provides functions allowing to:
- (+) Encrypt plaintext using AES algorithm in different chaining modes
- (+) Decrypt cyphertext using AES algorithm in different chaining modes
- [..] Three processing functions are available:
- (+) Polling mode
- (+) Interrupt mode
- (+) DMA mode
- @endverbatim
- * @{
- */
- /**
- * @brief Initializes the CRYP peripheral in AES ECB encryption mode
- * then encrypt pPlainData. The cypher data are available in pCypherData
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16.
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Timeout: Specify Timeout value
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
- /* Check that data aligned on u32 and Size multiple of 16*/
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if HAL_CRYP_Init has been called */
- if(hcryp->State != HAL_CRYP_STATE_RESET)
- {
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES ECB mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Write Plain Data and Get Cypher Data */
- if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
- {
- return HAL_TIMEOUT;
- }
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES CBC encryption mode
- * then encrypt pPlainData. The cypher data are available in pCypherData
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16.
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Timeout: Specify Timeout value
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if HAL_CRYP_Init has been called */
- if(hcryp->State != HAL_CRYP_STATE_RESET)
- {
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES CBC mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Write Plain Data and Get Cypher Data */
- if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
- {
- return HAL_TIMEOUT;
- }
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES CTR encryption mode
- * then encrypt pPlainData. The cypher data are available in pCypherData
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16.
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Timeout: Specify Timeout value
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if HAL_CRYP_Init has been called */
- if(hcryp->State != HAL_CRYP_STATE_RESET)
- {
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES CTR mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Write Plain Data and Get Cypher Data */
- if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
- {
- return HAL_TIMEOUT;
- }
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES ECB decryption mode
- * then decrypted pCypherData. The cypher data are available in pPlainData
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16.
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Timeout: Specify Timeout value
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if HAL_CRYP_Init has been called */
- if(hcryp->State != HAL_CRYP_STATE_RESET)
- {
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Write Cypher Data and Get Plain Data */
- if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
- {
- return HAL_TIMEOUT;
- }
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES ECB decryption mode
- * then decrypted pCypherData. The cypher data are available in pPlainData
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16.
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Timeout: Specify Timeout value
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if HAL_CRYP_Init has been called */
- if(hcryp->State != HAL_CRYP_STATE_RESET)
- {
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Write Cypher Data and Get Plain Data */
- if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
- {
- return HAL_TIMEOUT;
- }
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES CTR decryption mode
- * then decrypted pCypherData. The cypher data are available in pPlainData
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16.
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Timeout: Specify Timeout value
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if initialization phase has already been performed */
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->Phase == HAL_CRYP_PHASE_READY))
- {
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES CTR decryption mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Write Cypher Data and Get Plain Data */
- if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
- {
- return HAL_TIMEOUT;
- }
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
- {
- uint32_t inputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- /* Get the buffer addresses and sizes */
- hcryp->CrypInCount = Size;
- hcryp->pCrypInBuffPtr = pPlainData;
- hcryp->pCrypOutBuffPtr = pCypherData;
- hcryp->CrypOutCount = Size;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES ECB mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Enable Interrupts */
- __HAL_CRYP_ENABLE_IT(AES_IT_CC);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Get the last input data adress */
- inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
-
- /* Write the Input block in the Data Input register */
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- hcryp->pCrypInBuffPtr += 16;
- hcryp->CrypInCount -= 16;
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
- {
- uint32_t inputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- /* Get the buffer addresses and sizes */
- hcryp->CrypInCount = Size;
- hcryp->pCrypInBuffPtr = pPlainData;
- hcryp->pCrypOutBuffPtr = pCypherData;
- hcryp->CrypOutCount = Size;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES CBC mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Enable Interrupts */
- __HAL_CRYP_ENABLE_IT(AES_IT_CC);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Get the last input data adress */
- inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
-
- /* Write the Input block in the Data Input register */
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- hcryp->pCrypInBuffPtr += 16;
- hcryp->CrypInCount -= 16;
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
- {
- uint32_t inputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- /* Get the buffer addresses and sizes */
- hcryp->CrypInCount = Size;
- hcryp->pCrypInBuffPtr = pPlainData;
- hcryp->pCrypOutBuffPtr = pCypherData;
- hcryp->CrypOutCount = Size;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES CTR mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Enable Interrupts */
- __HAL_CRYP_ENABLE_IT(AES_IT_CC);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Get the last input data adress */
- inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
-
- /* Write the Input block in the Data Input register */
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- hcryp->pCrypInBuffPtr += 16;
- hcryp->CrypInCount -= 16;
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16.
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
- {
- uint32_t inputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- /* Get the buffer addresses and sizes */
- hcryp->CrypInCount = Size;
- hcryp->pCrypInBuffPtr = pCypherData;
- hcryp->pCrypOutBuffPtr = pPlainData;
- hcryp->CrypOutCount = Size;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Enable Interrupts */
- __HAL_CRYP_ENABLE_IT(AES_IT_CC);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Get the last input data adress */
- inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
-
- /* Write the Input block in the Data Input register */
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- hcryp->pCrypInBuffPtr += 16;
- hcryp->CrypInCount -= 16;
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES CBC decryption mode using IT.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
- {
- uint32_t inputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- /* Get the buffer addresses and sizes */
- hcryp->CrypInCount = Size;
- hcryp->pCrypInBuffPtr = pCypherData;
- hcryp->pCrypOutBuffPtr = pPlainData;
- hcryp->CrypOutCount = Size;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Enable Interrupts */
- __HAL_CRYP_ENABLE_IT(AES_IT_CC);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Get the last input data adress */
- inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
-
- /* Write the Input block in the Data Input register */
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- hcryp->pCrypInBuffPtr += 16;
- hcryp->CrypInCount -= 16;
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
- {
- uint32_t inputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- /* Get the buffer addresses and sizes */
- hcryp->CrypInCount = Size;
- hcryp->pCrypInBuffPtr = pCypherData;
- hcryp->pCrypOutBuffPtr = pPlainData;
- hcryp->CrypOutCount = Size;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES CTR decryption mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Enable Interrupts */
- __HAL_CRYP_ENABLE_IT(AES_IT_CC);
-
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
-
- /* Get the last input data adress */
- inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
-
- /* Write the Input block in the Data Input register */
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- hcryp->pCrypInBuffPtr += 16;
- hcryp->CrypInCount -= 16;
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES ECB encryption mode using DMA.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
- {
- uint32_t inputaddr = 0, outputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if HAL_CRYP_Init has been called */
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- inputaddr = (uint32_t)pPlainData;
- outputaddr = (uint32_t)pCypherData;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Set the CRYP peripheral in AES ECB mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
- /* Set the input and output addresses and start DMA transfer */
- CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16.
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
- {
- uint32_t inputaddr = 0, outputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if HAL_CRYP_Init has been called */
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- inputaddr = (uint32_t)pPlainData;
- outputaddr = (uint32_t)pCypherData;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Set the CRYP peripheral in AES CBC mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
- /* Set the input and output addresses and start DMA transfer */
- CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES CTR encryption mode using DMA.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16.
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
- {
- uint32_t inputaddr = 0, outputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if HAL_CRYP_Init has been called */
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- inputaddr = (uint32_t)pPlainData;
- outputaddr = (uint32_t)pCypherData;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Set the CRYP peripheral in AES CTR mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Set the input and output addresses and start DMA transfer */
- CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES ECB decryption mode using DMA.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
- {
- uint32_t inputaddr = 0, outputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if HAL_CRYP_Init has been called */
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- inputaddr = (uint32_t)pCypherData;
- outputaddr = (uint32_t)pPlainData;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Set the input and output addresses and start DMA transfer */
- CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
- {
- uint32_t inputaddr = 0, outputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if HAL_CRYP_Init has been called */
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- inputaddr = (uint32_t)pCypherData;
- outputaddr = (uint32_t)pPlainData;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Reset the CHMOD & MODE bits */
- CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
-
- /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Set the input and output addresses and start DMA transfer */
- CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- return HAL_ERROR;
- }
- }
- /**
- * @brief Initializes the CRYP peripheral in AES CTR decryption mode using DMA.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
- * @param Size: Length of the plaintext buffer, must be a multiple of 16
- * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
- {
- uint32_t inputaddr = 0, outputaddr = 0;
-
- /* Check that data aligned on u32 */
- if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
- {
- /* Process Locked */
- __HAL_UNLOCK(hcryp);
- /* Return function status */
- return HAL_ERROR;
- }
-
- /* Check if HAL_CRYP_Init has been called */
- if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
- {
- /* Process Locked */
- __HAL_LOCK(hcryp);
-
- inputaddr = (uint32_t)pCypherData;
- outputaddr = (uint32_t)pPlainData;
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_BUSY;
-
- /* Check if initialization phase has already been performed */
- if(hcryp->Phase == HAL_CRYP_PHASE_READY)
- {
- /* Set the key */
- CRYP_SetKey(hcryp, hcryp->Init.pKey);
-
- /* Set the CRYP peripheral in AES CTR mode */
- __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
-
- /* Set the Initialization Vector */
- CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
-
- /* Set the phase */
- hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
- }
-
- /* Set the input and output addresses and start DMA transfer */
- CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- return HAL_ERROR;
- }
- }
- /**
- * @}
- */
- /** @defgroup CRYP_Exported_Functions_Group3 DMA callback functions
- * @brief DMA callback functions.
- *
- @verbatim
- ==============================================================================
- ##### DMA callback functions #####
- ==============================================================================
- [..] This section provides DMA callback functions:
- (+) DMA Input data transfer complete
- (+) DMA Output data transfer complete
- (+) DMA error
- @endverbatim
- * @{
- */
- /**
- * @brief CRYP error callback.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @retval None
- */
- __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
- {
- /* NOTE : This function should not be modified; when the callback is needed,
- the HAL_CRYP_ErrorCallback can be implemented in the user file
- */
- }
- /**
- * @brief Input transfer completed callback.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @retval None
- */
- __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
- {
- /* NOTE : This function should not be modified; when the callback is needed,
- the HAL_CRYP_InCpltCallback can be implemented in the user file
- */
- }
- /**
- * @brief Output transfer completed callback.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @retval None
- */
- __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
- {
- /* NOTE : This function should not be modified; when the callback is needed,
- the HAL_CRYP_OutCpltCallback can be implemented in the user file
- */
- }
- /**
- * @}
- */
- /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
- * @brief CRYP IRQ handler.
- *
- @verbatim
- ==============================================================================
- ##### CRYP IRQ handler management #####
- ==============================================================================
- [..] This section provides CRYP IRQ handler function.
- @endverbatim
- * @{
- */
- /**
- * @brief This function handles CRYP interrupt request.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @retval None
- */
- void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
- {
- /* Check if error occurred*/
- if (__HAL_CRYP_GET_IT_SOURCE(hcryp, AES_IT_ERR) != RESET)
- {
- if (__HAL_CRYP_GET_FLAG(AES_FLAG_RDERR) != RESET)
- {
- __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_RDERR);
- }
-
- if (__HAL_CRYP_GET_FLAG(AES_FLAG_WRERR) != RESET)
- {
- __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_WRERR);
- }
-
- if (__HAL_CRYP_GET_FLAG(AES_FLAG_CCF) != RESET)
- {
- __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
- }
-
- hcryp->State= HAL_CRYP_STATE_ERROR;
- /* Disable Computation Complete Interrupt */
- __HAL_CRYP_DISABLE_IT(AES_IT_CC);
- __HAL_CRYP_DISABLE_IT(AES_IT_ERR);
-
- HAL_CRYP_ErrorCallback(hcryp);
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- return;
- }
-
- /* Check if computation complete interrupt was enabled*/
- if (__HAL_CRYP_GET_IT_SOURCE(hcryp, AES_IT_CC) != RESET)
- {
- /* Clear CCF Flag */
- __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
-
- CRYP_EncryptDecrypt_IT(hcryp);
- }
- }
- /**
- * @}
- */
- /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
- * @brief Peripheral State functions.
- *
- @verbatim
- ==============================================================================
- ##### Peripheral State functions #####
- ==============================================================================
- [..]
- This subsection permits to get in run-time the status of the peripheral.
- @endverbatim
- * @{
- */
- /**
- * @brief Returns the CRYP state.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @retval HAL state
- */
- HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
- {
- return hcryp->State;
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /** @addtogroup CRYP_Private_Functions
- * @{
- */
- /**
- * @brief IT function called under interruption context to continue encryption or decryption
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @retval HAL status
- */
- static HAL_StatusTypeDef CRYP_EncryptDecrypt_IT(CRYP_HandleTypeDef *hcryp)
- {
- uint32_t inputaddr = 0, outputaddr = 0;
- /* Get the last Output data adress */
- outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
-
- /* Read the Output block from the Output Register */
- *(uint32_t*)(outputaddr) = AES->DOUTR;
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = AES->DOUTR;
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = AES->DOUTR;
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = AES->DOUTR;
-
- hcryp->pCrypOutBuffPtr += 16;
- hcryp->CrypOutCount -= 16;
-
- /* Check if all input text is encrypted or decrypted */
- if(hcryp->CrypOutCount == 0)
- {
- /* Disable Computation Complete Interrupt */
- __HAL_CRYP_DISABLE_IT(AES_IT_CC);
- __HAL_CRYP_DISABLE_IT(AES_IT_ERR);
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- /* Change the CRYP state */
- hcryp->State = HAL_CRYP_STATE_READY;
-
- /* Call computation complete callback */
- HAL_CRYPEx_ComputationCpltCallback(hcryp);
- }
- else /* Process the rest of input text */
- {
- /* Get the last Intput data adress */
- inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
-
- /* Write the Input block in the Data Input register */
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- hcryp->pCrypInBuffPtr += 16;
- hcryp->CrypInCount -= 16;
- }
- return HAL_OK;
- }
- /**
- * @brief DMA CRYP Input Data process complete callback.
- * @param hdma: DMA handle
- * @retval None
- */
- static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
- {
- CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
-
- /* Disable the DMA transfer for input request */
- CLEAR_BIT(AES->CR, AES_CR_DMAINEN);
-
- /* Call input data transfer complete callback */
- HAL_CRYP_InCpltCallback(hcryp);
- }
- /**
- * @brief DMA CRYP Output Data process complete callback.
- * @param hdma: DMA handle
- * @retval None
- */
- static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
- {
- CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
-
- /* Disable the DMA transfer for output request by resetting the DMAOUTEN bit
- in the DMACR register */
- CLEAR_BIT(AES->CR, AES_CR_DMAOUTEN);
- /* Clear CCF Flag */
- __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
- /* Disable CRYP */
- __HAL_CRYP_DISABLE();
-
- /* Change the CRYP state to ready */
- hcryp->State = HAL_CRYP_STATE_READY;
-
- /* Call output data transfer complete callback */
- HAL_CRYP_OutCpltCallback(hcryp);
- }
- /**
- * @brief DMA CRYP communication error callback.
- * @param hdma: DMA handle
- * @retval None
- */
- static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
- {
- CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
- hcryp->State= HAL_CRYP_STATE_ERROR;
- HAL_CRYP_ErrorCallback(hcryp);
- }
- /**
- * @brief Writes the Key in Key registers.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param Key: Pointer to Key buffer
- * @note Key must be written as little endian.
- * If Key pointer points at address n,
- * n[15:0] contains key[96:127],
- * (n+4)[15:0] contains key[64:95],
- * (n+8)[15:0] contains key[32:63] and
- * (n+12)[15:0] contains key[0:31]
- * @retval None
- */
- static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key)
- {
- uint32_t keyaddr = (uint32_t)Key;
-
- AES->KEYR3 = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES->KEYR2 = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES->KEYR1 = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES->KEYR0 = __REV(*(uint32_t*)(keyaddr));
- }
- /**
- * @brief Writes the InitVector/InitCounter in IV registers.
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param InitVector: Pointer to InitVector/InitCounter buffer
- * @note Init Vector must be written as little endian.
- * If Init Vector pointer points at address n,
- * n[15:0] contains Vector[96:127],
- * (n+4)[15:0] contains Vector[64:95],
- * (n+8)[15:0] contains Vector[32:63] and
- * (n+12)[15:0] contains Vector[0:31]
- * @retval None
- */
- static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector)
- {
- uint32_t ivaddr = (uint32_t)InitVector;
-
- AES->IVR3 = __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES->IVR2 = __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES->IVR1 = __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES->IVR0 = __REV(*(uint32_t*)(ivaddr));
- }
- /**
- * @brief Process Data: Writes Input data in polling mode and reads the output data
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param Input: Pointer to the Input buffer
- * @param Ilength: Length of the Input buffer, must be a multiple of 16.
- * @param Output: Pointer to the returned buffer
- * @param Timeout: Specify Timeout value
- * @retval None
- */
- static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
- {
- uint32_t tickstart = 0;
-
- uint32_t index = 0;
- uint32_t inputaddr = (uint32_t)Input;
- uint32_t outputaddr = (uint32_t)Output;
-
- for(index=0; (index < Ilength); index += 16)
- {
- /* Write the Input block in the Data Input register */
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
- AES->DINR = *(uint32_t*)(inputaddr);
- inputaddr+=4;
-
- /* Get timeout */
- tickstart = HAL_GetTick();
-
- while(HAL_IS_BIT_CLR(AES->SR, AES_SR_CCF))
- {
- /* Check for the Timeout */
- if(Timeout != HAL_MAX_DELAY)
- {
- if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
- {
- /* Change state */
- hcryp->State = HAL_CRYP_STATE_TIMEOUT;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hcryp);
-
- return HAL_TIMEOUT;
- }
- }
- }
- /* Clear CCF Flag */
- __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
-
- /* Read the Output block from the Data Output Register */
- *(uint32_t*)(outputaddr) = AES->DOUTR;
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = AES->DOUTR;
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = AES->DOUTR;
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = AES->DOUTR;
- outputaddr+=4;
- }
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Set the DMA configuration and start the DMA transfer
- * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
- * the configuration information for CRYP module
- * @param inputaddr: address of the Input buffer
- * @param Size: Size of the Input buffer, must be a multiple of 16.
- * @param outputaddr: address of the Output buffer
- * @retval None
- */
- static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
- {
- /* Set the CRYP DMA transfer complete callback */
- hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
- /* Set the DMA error callback */
- hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
-
- /* Set the CRYP DMA transfer complete callback */
- hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
- /* Set the DMA error callback */
- hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
- /* Enable the DMA In DMA Stream */
- HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&AES->DINR, Size/4);
- /* Enable the DMA Out DMA Stream */
- HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&AES->DOUTR, outputaddr, Size/4);
- /* Enable In and Out DMA requests */
- SET_BIT(AES->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN));
- /* Enable CRYP */
- __HAL_CRYP_ENABLE();
- }
- /**
- * @}
- */
- #endif /* STM32L162xC || STM32L162xCA || STM32L162xD || STM32L162xE*/
- /**
- * @}
- */
- /**
- * @}
- */
- #endif /* HAL_CRYP_MODULE_ENABLED */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|