stm32l1xx_hal_cryp.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_cryp.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  7. * @brief CRYP HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the Cryptography (CRYP) peripheral:
  11. * + Initialization and de-initialization functions
  12. * + Processing functions by algorithm using polling mode
  13. * + Processing functions by algorithm using interrupt mode
  14. * + Processing functions by algorithm using DMA mode
  15. * + Peripheral State functions
  16. *
  17. @verbatim
  18. ==============================================================================
  19. ##### How to use this driver #####
  20. ==============================================================================
  21. [..]
  22. The CRYP HAL driver can be used as follows:
  23. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  24. (##) Enable the CRYP interface clock using __CRYP_CLK_ENABLE()
  25. (##) In case of using interrupts (e.g. HAL_CRYP_AESECB_Encrypt_IT())
  26. (+) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  27. (+) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
  28. (+) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
  29. (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_AESECB_Encrypt_DMA())
  30. (+) Enable the DMA2 interface clock using
  31. (++) __DMA2_CLK_ENABLE()
  32. (+) Configure and enable two DMA Channels one for managing data transfer from
  33. memory to peripheral (input channel) and another channel for managing data
  34. transfer from peripheral to memory (output channel)
  35. (+) Associate the initialized DMA handle to the CRYP DMA handle
  36. using __HAL_LINKDMA()
  37. (+) Configure the priority and enable the NVIC for the transfer complete
  38. interrupt on the two DMA Streams. The output stream should have higher
  39. priority than the input stream.
  40. (++) HAL_NVIC_SetPriority()
  41. (++) HAL_NVIC_EnableIRQ()
  42. (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly:
  43. (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit
  44. (##) The encryption/decryption key.
  45. (##) The initialization vector (counter). It is not used ECB mode.
  46. (#)Three processing (encryption/decryption) functions are available:
  47. (##) Polling mode: encryption and decryption APIs are blocking functions
  48. i.e. they process the data and wait till the processing is finished
  49. e.g. HAL_CRYP_AESCBC_Encrypt()
  50. (##) Interrupt mode: encryption and decryption APIs are not blocking functions
  51. i.e. they process the data under interrupt
  52. e.g. HAL_CRYP_AESCBC_Encrypt_IT()
  53. (##) DMA mode: encryption and decryption APIs are not blocking functions
  54. i.e. the data transfer is ensured by DMA
  55. e.g. HAL_CRYP_AESCBC_Encrypt_DMA()
  56. (#)When the processing function is called for the first time after HAL_CRYP_Init()
  57. the CRYP peripheral is initialized and processes the buffer in input.
  58. At second call, the processing function performs an append of the already
  59. processed buffer.
  60. When a new data block is to be processed, call HAL_CRYP_Init() then the
  61. processing function.
  62. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  63. @endverbatim
  64. ******************************************************************************
  65. * @attention
  66. *
  67. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  68. *
  69. * Redistribution and use in source and binary forms, with or without modification,
  70. * are permitted provided that the following conditions are met:
  71. * 1. Redistributions of source code must retain the above copyright notice,
  72. * this list of conditions and the following disclaimer.
  73. * 2. Redistributions in binary form must reproduce the above copyright notice,
  74. * this list of conditions and the following disclaimer in the documentation
  75. * and/or other materials provided with the distribution.
  76. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  77. * may be used to endorse or promote products derived from this software
  78. * without specific prior written permission.
  79. *
  80. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  81. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  83. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  84. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  85. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  86. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  87. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  88. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  89. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  90. *
  91. ******************************************************************************
  92. */
  93. /* Includes ------------------------------------------------------------------*/
  94. #include "stm32l1xx_hal.h"
  95. #ifdef HAL_CRYP_MODULE_ENABLED
  96. /** @addtogroup STM32L1xx_HAL_Driver
  97. * @{
  98. */
  99. /** @defgroup CRYP CRYP
  100. * @brief CRYP HAL module driver.
  101. * @{
  102. */
  103. #if defined(STM32L162xC) || defined(STM32L162xCA) || defined(STM32L162xD) || defined(STM32L162xE)
  104. /* Private typedef -----------------------------------------------------------*/
  105. /* Private define ------------------------------------------------------------*/
  106. /** @defgroup CRYP_Private_Defines CRYP Private Defines
  107. * @{
  108. */
  109. #define CRYP_ALGO_CHAIN_MASK (AES_CR_MODE | AES_CR_CHMOD)
  110. /**
  111. * @}
  112. */
  113. /* Private macro -------------------------------------------------------------*/
  114. /* Private variables ---------------------------------------------------------*/
  115. /* Private function prototypes -----------------------------------------------*/
  116. /** @defgroup CRYP_Private_Functions CRYP Private Functions
  117. * @{
  118. */
  119. static HAL_StatusTypeDef CRYP_EncryptDecrypt_IT(CRYP_HandleTypeDef *hcryp);
  120. static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector);
  121. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key);
  122. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
  123. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
  124. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
  125. static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
  126. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  127. /**
  128. * @}
  129. */
  130. /* Private functions ---------------------------------------------------------*/
  131. /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
  132. * @{
  133. */
  134. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
  135. * @brief Initialization and Configuration functions.
  136. *
  137. @verbatim
  138. ==============================================================================
  139. ##### Initialization and de-initialization functions #####
  140. ==============================================================================
  141. [..] This section provides functions allowing to:
  142. (+) Initialize the CRYP according to the specified parameters
  143. in the CRYP_InitTypeDef and creates the associated handle
  144. (+) DeInitialize the CRYP peripheral
  145. (+) Initialize the CRYP MSP
  146. (+) DeInitialize CRYP MSP
  147. @endverbatim
  148. * @{
  149. */
  150. /**
  151. * @brief Initializes the CRYP according to the specified
  152. * parameters in the CRYP_InitTypeDef and creates the associated handle.
  153. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  154. * the configuration information for CRYP module
  155. * @retval HAL status
  156. */
  157. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  158. {
  159. /* Check the CRYP handle allocation */
  160. if(hcryp == NULL)
  161. {
  162. return HAL_ERROR;
  163. }
  164. /* Check the parameters */
  165. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  166. if(hcryp->State == HAL_CRYP_STATE_RESET)
  167. {
  168. /* Init the low level hardware */
  169. HAL_CRYP_MspInit(hcryp);
  170. }
  171. /* Check if AES already enabled */
  172. if (HAL_IS_BIT_CLR(AES->CR, AES_CR_EN))
  173. {
  174. /* Change the CRYP state */
  175. hcryp->State = HAL_CRYP_STATE_BUSY;
  176. /* Set the data type*/
  177. MODIFY_REG(AES->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  178. /* Reset CrypInCount and CrypOutCount */
  179. hcryp->CrypInCount = 0;
  180. hcryp->CrypOutCount = 0;
  181. /* Change the CRYP state */
  182. hcryp->State = HAL_CRYP_STATE_READY;
  183. /* Set the default CRYP phase */
  184. hcryp->Phase = HAL_CRYP_PHASE_READY;
  185. /* Return function status */
  186. return HAL_OK;
  187. }
  188. else
  189. {
  190. /* The Datatype selection must be changed if the AES is disabled. Writing these bits while the AES is */
  191. /* enabled is forbidden to avoid unpredictable AES behavior.*/
  192. /* Return function status */
  193. return HAL_ERROR;
  194. }
  195. }
  196. /**
  197. * @brief DeInitializes the CRYP peripheral.
  198. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  199. * the configuration information for CRYP module
  200. * @retval HAL status
  201. */
  202. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  203. {
  204. /* Check the CRYP handle allocation */
  205. if(hcryp == NULL)
  206. {
  207. return HAL_ERROR;
  208. }
  209. /* Change the CRYP state */
  210. hcryp->State = HAL_CRYP_STATE_BUSY;
  211. /* Set the default CRYP phase */
  212. hcryp->Phase = HAL_CRYP_PHASE_READY;
  213. /* Reset CrypInCount and CrypOutCount */
  214. hcryp->CrypInCount = 0;
  215. hcryp->CrypOutCount = 0;
  216. /* Disable the CRYP Peripheral Clock */
  217. __HAL_CRYP_DISABLE();
  218. /* DeInit the low level hardware: CLOCK, NVIC.*/
  219. HAL_CRYP_MspDeInit(hcryp);
  220. /* Change the CRYP state */
  221. hcryp->State = HAL_CRYP_STATE_RESET;
  222. /* Release Lock */
  223. __HAL_UNLOCK(hcryp);
  224. /* Return function status */
  225. return HAL_OK;
  226. }
  227. /**
  228. * @brief Initializes the CRYP MSP.
  229. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  230. * the configuration information for CRYP module
  231. * @retval None
  232. */
  233. __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
  234. {
  235. /* NOTE : This function should not be modified; when the callback is needed,
  236. the HAL_CRYP_MspInit can be implemented in the user file */
  237. }
  238. /**
  239. * @brief DeInitializes CRYP MSP.
  240. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  241. * the configuration information for CRYP module
  242. * @retval None
  243. */
  244. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  245. {
  246. /* NOTE : This function should not be modified; when the callback is needed,
  247. the HAL_CRYP_MspDeInit can be implemented in the user file */
  248. }
  249. /**
  250. * @}
  251. */
  252. /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
  253. * @brief processing functions.
  254. *
  255. @verbatim
  256. ==============================================================================
  257. ##### AES processing functions #####
  258. ==============================================================================
  259. [..] This section provides functions allowing to:
  260. (+) Encrypt plaintext using AES algorithm in different chaining modes
  261. (+) Decrypt cyphertext using AES algorithm in different chaining modes
  262. [..] Three processing functions are available:
  263. (+) Polling mode
  264. (+) Interrupt mode
  265. (+) DMA mode
  266. @endverbatim
  267. * @{
  268. */
  269. /**
  270. * @brief Initializes the CRYP peripheral in AES ECB encryption mode
  271. * then encrypt pPlainData. The cypher data are available in pCypherData
  272. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  273. * the configuration information for CRYP module
  274. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  275. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  276. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  277. * @param Timeout: Specify Timeout value
  278. * @retval HAL status
  279. */
  280. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  281. {
  282. /* Process Locked */
  283. __HAL_LOCK(hcryp);
  284. /* Check that data aligned on u32 and Size multiple of 16*/
  285. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  286. {
  287. /* Process Locked */
  288. __HAL_UNLOCK(hcryp);
  289. /* Return function status */
  290. return HAL_ERROR;
  291. }
  292. /* Check if HAL_CRYP_Init has been called */
  293. if(hcryp->State != HAL_CRYP_STATE_RESET)
  294. {
  295. /* Change the CRYP state */
  296. hcryp->State = HAL_CRYP_STATE_BUSY;
  297. /* Check if initialization phase has already been performed */
  298. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  299. {
  300. /* Set the key */
  301. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  302. /* Reset the CHMOD & MODE bits */
  303. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  304. /* Set the CRYP peripheral in AES ECB mode */
  305. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
  306. /* Enable CRYP */
  307. __HAL_CRYP_ENABLE();
  308. /* Set the phase */
  309. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  310. }
  311. /* Write Plain Data and Get Cypher Data */
  312. if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  313. {
  314. return HAL_TIMEOUT;
  315. }
  316. /* Change the CRYP state */
  317. hcryp->State = HAL_CRYP_STATE_READY;
  318. /* Process Unlocked */
  319. __HAL_UNLOCK(hcryp);
  320. /* Return function status */
  321. return HAL_OK;
  322. }
  323. else
  324. {
  325. /* Return function status */
  326. return HAL_ERROR;
  327. }
  328. }
  329. /**
  330. * @brief Initializes the CRYP peripheral in AES CBC encryption mode
  331. * then encrypt pPlainData. The cypher data are available in pCypherData
  332. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  333. * the configuration information for CRYP module
  334. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  335. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  336. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  337. * @param Timeout: Specify Timeout value
  338. * @retval HAL status
  339. */
  340. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  341. {
  342. /* Process Locked */
  343. __HAL_LOCK(hcryp);
  344. /* Check that data aligned on u32 */
  345. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  346. {
  347. /* Process Locked */
  348. __HAL_UNLOCK(hcryp);
  349. /* Return function status */
  350. return HAL_ERROR;
  351. }
  352. /* Check if HAL_CRYP_Init has been called */
  353. if(hcryp->State != HAL_CRYP_STATE_RESET)
  354. {
  355. /* Change the CRYP state */
  356. hcryp->State = HAL_CRYP_STATE_BUSY;
  357. /* Check if initialization phase has already been performed */
  358. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  359. {
  360. /* Set the key */
  361. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  362. /* Reset the CHMOD & MODE bits */
  363. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  364. /* Set the CRYP peripheral in AES CBC mode */
  365. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
  366. /* Set the Initialization Vector */
  367. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  368. /* Enable CRYP */
  369. __HAL_CRYP_ENABLE();
  370. /* Set the phase */
  371. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  372. }
  373. /* Write Plain Data and Get Cypher Data */
  374. if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  375. {
  376. return HAL_TIMEOUT;
  377. }
  378. /* Change the CRYP state */
  379. hcryp->State = HAL_CRYP_STATE_READY;
  380. /* Process Unlocked */
  381. __HAL_UNLOCK(hcryp);
  382. /* Return function status */
  383. return HAL_OK;
  384. }
  385. else
  386. {
  387. /* Return function status */
  388. return HAL_ERROR;
  389. }
  390. }
  391. /**
  392. * @brief Initializes the CRYP peripheral in AES CTR encryption mode
  393. * then encrypt pPlainData. The cypher data are available in pCypherData
  394. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  395. * the configuration information for CRYP module
  396. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  397. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  398. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  399. * @param Timeout: Specify Timeout value
  400. * @retval HAL status
  401. */
  402. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  403. {
  404. /* Process Locked */
  405. __HAL_LOCK(hcryp);
  406. /* Check that data aligned on u32 */
  407. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  408. {
  409. /* Process Locked */
  410. __HAL_UNLOCK(hcryp);
  411. /* Return function status */
  412. return HAL_ERROR;
  413. }
  414. /* Check if HAL_CRYP_Init has been called */
  415. if(hcryp->State != HAL_CRYP_STATE_RESET)
  416. {
  417. /* Change the CRYP state */
  418. hcryp->State = HAL_CRYP_STATE_BUSY;
  419. /* Check if initialization phase has already been performed */
  420. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  421. {
  422. /* Set the key */
  423. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  424. /* Reset the CHMOD & MODE bits */
  425. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  426. /* Set the CRYP peripheral in AES CTR mode */
  427. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
  428. /* Set the Initialization Vector */
  429. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  430. /* Enable CRYP */
  431. __HAL_CRYP_ENABLE();
  432. /* Set the phase */
  433. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  434. }
  435. /* Write Plain Data and Get Cypher Data */
  436. if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  437. {
  438. return HAL_TIMEOUT;
  439. }
  440. /* Change the CRYP state */
  441. hcryp->State = HAL_CRYP_STATE_READY;
  442. /* Process Unlocked */
  443. __HAL_UNLOCK(hcryp);
  444. /* Return function status */
  445. return HAL_OK;
  446. }
  447. else
  448. {
  449. /* Return function status */
  450. return HAL_ERROR;
  451. }
  452. }
  453. /**
  454. * @brief Initializes the CRYP peripheral in AES ECB decryption mode
  455. * then decrypted pCypherData. The cypher data are available in pPlainData
  456. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  457. * the configuration information for CRYP module
  458. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  459. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  460. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  461. * @param Timeout: Specify Timeout value
  462. * @retval HAL status
  463. */
  464. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  465. {
  466. /* Process Locked */
  467. __HAL_LOCK(hcryp);
  468. /* Check that data aligned on u32 */
  469. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  470. {
  471. /* Process Locked */
  472. __HAL_UNLOCK(hcryp);
  473. /* Return function status */
  474. return HAL_ERROR;
  475. }
  476. /* Check if HAL_CRYP_Init has been called */
  477. if(hcryp->State != HAL_CRYP_STATE_RESET)
  478. {
  479. /* Change the CRYP state */
  480. hcryp->State = HAL_CRYP_STATE_BUSY;
  481. /* Check if initialization phase has already been performed */
  482. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  483. {
  484. /* Set the key */
  485. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  486. /* Reset the CHMOD & MODE bits */
  487. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  488. /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
  489. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
  490. /* Enable CRYP */
  491. __HAL_CRYP_ENABLE();
  492. /* Set the phase */
  493. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  494. }
  495. /* Write Cypher Data and Get Plain Data */
  496. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  497. {
  498. return HAL_TIMEOUT;
  499. }
  500. /* Change the CRYP state */
  501. hcryp->State = HAL_CRYP_STATE_READY;
  502. /* Process Unlocked */
  503. __HAL_UNLOCK(hcryp);
  504. /* Return function status */
  505. return HAL_OK;
  506. }
  507. else
  508. {
  509. /* Return function status */
  510. return HAL_ERROR;
  511. }
  512. }
  513. /**
  514. * @brief Initializes the CRYP peripheral in AES ECB decryption mode
  515. * then decrypted pCypherData. The cypher data are available in pPlainData
  516. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  517. * the configuration information for CRYP module
  518. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  519. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  520. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  521. * @param Timeout: Specify Timeout value
  522. * @retval HAL status
  523. */
  524. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  525. {
  526. /* Process Locked */
  527. __HAL_LOCK(hcryp);
  528. /* Check that data aligned on u32 */
  529. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  530. {
  531. /* Process Locked */
  532. __HAL_UNLOCK(hcryp);
  533. /* Return function status */
  534. return HAL_ERROR;
  535. }
  536. /* Check if HAL_CRYP_Init has been called */
  537. if(hcryp->State != HAL_CRYP_STATE_RESET)
  538. {
  539. /* Change the CRYP state */
  540. hcryp->State = HAL_CRYP_STATE_BUSY;
  541. /* Check if initialization phase has already been performed */
  542. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  543. {
  544. /* Set the key */
  545. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  546. /* Reset the CHMOD & MODE bits */
  547. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  548. /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
  549. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
  550. /* Set the Initialization Vector */
  551. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  552. /* Enable CRYP */
  553. __HAL_CRYP_ENABLE();
  554. /* Set the phase */
  555. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  556. }
  557. /* Write Cypher Data and Get Plain Data */
  558. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  559. {
  560. return HAL_TIMEOUT;
  561. }
  562. /* Change the CRYP state */
  563. hcryp->State = HAL_CRYP_STATE_READY;
  564. /* Process Unlocked */
  565. __HAL_UNLOCK(hcryp);
  566. /* Return function status */
  567. return HAL_OK;
  568. }
  569. else
  570. {
  571. /* Return function status */
  572. return HAL_ERROR;
  573. }
  574. }
  575. /**
  576. * @brief Initializes the CRYP peripheral in AES CTR decryption mode
  577. * then decrypted pCypherData. The cypher data are available in pPlainData
  578. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  579. * the configuration information for CRYP module
  580. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  581. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  582. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  583. * @param Timeout: Specify Timeout value
  584. * @retval HAL status
  585. */
  586. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  587. {
  588. /* Process Locked */
  589. __HAL_LOCK(hcryp);
  590. /* Check that data aligned on u32 */
  591. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  592. {
  593. /* Process Locked */
  594. __HAL_UNLOCK(hcryp);
  595. /* Return function status */
  596. return HAL_ERROR;
  597. }
  598. /* Check if initialization phase has already been performed */
  599. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->Phase == HAL_CRYP_PHASE_READY))
  600. {
  601. /* Change the CRYP state */
  602. hcryp->State = HAL_CRYP_STATE_BUSY;
  603. /* Set the key */
  604. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  605. /* Reset the CHMOD & MODE bits */
  606. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  607. /* Set the CRYP peripheral in AES CTR decryption mode */
  608. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
  609. /* Set the Initialization Vector */
  610. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  611. /* Enable CRYP */
  612. __HAL_CRYP_ENABLE();
  613. /* Set the phase */
  614. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  615. }
  616. /* Write Cypher Data and Get Plain Data */
  617. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  618. {
  619. return HAL_TIMEOUT;
  620. }
  621. /* Change the CRYP state */
  622. hcryp->State = HAL_CRYP_STATE_READY;
  623. /* Process Unlocked */
  624. __HAL_UNLOCK(hcryp);
  625. /* Return function status */
  626. return HAL_OK;
  627. }
  628. /**
  629. * @brief Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt.
  630. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  631. * the configuration information for CRYP module
  632. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  633. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  634. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  635. * @retval HAL status
  636. */
  637. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  638. {
  639. uint32_t inputaddr = 0;
  640. /* Check that data aligned on u32 */
  641. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  642. {
  643. /* Process Locked */
  644. __HAL_UNLOCK(hcryp);
  645. /* Return function status */
  646. return HAL_ERROR;
  647. }
  648. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  649. {
  650. /* Process Locked */
  651. __HAL_LOCK(hcryp);
  652. /* Get the buffer addresses and sizes */
  653. hcryp->CrypInCount = Size;
  654. hcryp->pCrypInBuffPtr = pPlainData;
  655. hcryp->pCrypOutBuffPtr = pCypherData;
  656. hcryp->CrypOutCount = Size;
  657. /* Change the CRYP state */
  658. hcryp->State = HAL_CRYP_STATE_BUSY;
  659. /* Check if initialization phase has already been performed */
  660. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  661. {
  662. /* Set the key */
  663. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  664. /* Reset the CHMOD & MODE bits */
  665. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  666. /* Set the CRYP peripheral in AES ECB mode */
  667. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
  668. /* Set the phase */
  669. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  670. }
  671. /* Enable Interrupts */
  672. __HAL_CRYP_ENABLE_IT(AES_IT_CC);
  673. /* Enable CRYP */
  674. __HAL_CRYP_ENABLE();
  675. /* Get the last input data adress */
  676. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  677. /* Write the Input block in the Data Input register */
  678. AES->DINR = *(uint32_t*)(inputaddr);
  679. inputaddr+=4;
  680. AES->DINR = *(uint32_t*)(inputaddr);
  681. inputaddr+=4;
  682. AES->DINR = *(uint32_t*)(inputaddr);
  683. inputaddr+=4;
  684. AES->DINR = *(uint32_t*)(inputaddr);
  685. hcryp->pCrypInBuffPtr += 16;
  686. hcryp->CrypInCount -= 16;
  687. /* Return function status */
  688. return HAL_OK;
  689. }
  690. else
  691. {
  692. /* Return function status */
  693. return HAL_ERROR;
  694. }
  695. }
  696. /**
  697. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt.
  698. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  699. * the configuration information for CRYP module
  700. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  701. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  702. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  703. * @retval HAL status
  704. */
  705. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  706. {
  707. uint32_t inputaddr = 0;
  708. /* Check that data aligned on u32 */
  709. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  710. {
  711. /* Process Locked */
  712. __HAL_UNLOCK(hcryp);
  713. /* Return function status */
  714. return HAL_ERROR;
  715. }
  716. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  717. {
  718. /* Process Locked */
  719. __HAL_LOCK(hcryp);
  720. /* Get the buffer addresses and sizes */
  721. hcryp->CrypInCount = Size;
  722. hcryp->pCrypInBuffPtr = pPlainData;
  723. hcryp->pCrypOutBuffPtr = pCypherData;
  724. hcryp->CrypOutCount = Size;
  725. /* Change the CRYP state */
  726. hcryp->State = HAL_CRYP_STATE_BUSY;
  727. /* Check if initialization phase has already been performed */
  728. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  729. {
  730. /* Set the key */
  731. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  732. /* Reset the CHMOD & MODE bits */
  733. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  734. /* Set the CRYP peripheral in AES CBC mode */
  735. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
  736. /* Set the Initialization Vector */
  737. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  738. /* Set the phase */
  739. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  740. }
  741. /* Enable Interrupts */
  742. __HAL_CRYP_ENABLE_IT(AES_IT_CC);
  743. /* Enable CRYP */
  744. __HAL_CRYP_ENABLE();
  745. /* Get the last input data adress */
  746. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  747. /* Write the Input block in the Data Input register */
  748. AES->DINR = *(uint32_t*)(inputaddr);
  749. inputaddr+=4;
  750. AES->DINR = *(uint32_t*)(inputaddr);
  751. inputaddr+=4;
  752. AES->DINR = *(uint32_t*)(inputaddr);
  753. inputaddr+=4;
  754. AES->DINR = *(uint32_t*)(inputaddr);
  755. hcryp->pCrypInBuffPtr += 16;
  756. hcryp->CrypInCount -= 16;
  757. /* Return function status */
  758. return HAL_OK;
  759. }
  760. else
  761. {
  762. /* Return function status */
  763. return HAL_ERROR;
  764. }
  765. }
  766. /**
  767. * @brief Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt.
  768. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  769. * the configuration information for CRYP module
  770. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  771. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  772. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  773. * @retval HAL status
  774. */
  775. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  776. {
  777. uint32_t inputaddr = 0;
  778. /* Check that data aligned on u32 */
  779. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  780. {
  781. /* Process Locked */
  782. __HAL_UNLOCK(hcryp);
  783. /* Return function status */
  784. return HAL_ERROR;
  785. }
  786. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  787. {
  788. /* Process Locked */
  789. __HAL_LOCK(hcryp);
  790. /* Get the buffer addresses and sizes */
  791. hcryp->CrypInCount = Size;
  792. hcryp->pCrypInBuffPtr = pPlainData;
  793. hcryp->pCrypOutBuffPtr = pCypherData;
  794. hcryp->CrypOutCount = Size;
  795. /* Change the CRYP state */
  796. hcryp->State = HAL_CRYP_STATE_BUSY;
  797. /* Check if initialization phase has already been performed */
  798. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  799. {
  800. /* Set the key */
  801. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  802. /* Reset the CHMOD & MODE bits */
  803. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  804. /* Set the CRYP peripheral in AES CTR mode */
  805. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
  806. /* Set the Initialization Vector */
  807. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  808. /* Set the phase */
  809. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  810. }
  811. /* Enable Interrupts */
  812. __HAL_CRYP_ENABLE_IT(AES_IT_CC);
  813. /* Enable CRYP */
  814. __HAL_CRYP_ENABLE();
  815. /* Get the last input data adress */
  816. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  817. /* Write the Input block in the Data Input register */
  818. AES->DINR = *(uint32_t*)(inputaddr);
  819. inputaddr+=4;
  820. AES->DINR = *(uint32_t*)(inputaddr);
  821. inputaddr+=4;
  822. AES->DINR = *(uint32_t*)(inputaddr);
  823. inputaddr+=4;
  824. AES->DINR = *(uint32_t*)(inputaddr);
  825. hcryp->pCrypInBuffPtr += 16;
  826. hcryp->CrypInCount -= 16;
  827. /* Return function status */
  828. return HAL_OK;
  829. }
  830. else
  831. {
  832. /* Return function status */
  833. return HAL_ERROR;
  834. }
  835. }
  836. /**
  837. * @brief Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt.
  838. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  839. * the configuration information for CRYP module
  840. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  841. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  842. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  843. * @retval HAL status
  844. */
  845. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  846. {
  847. uint32_t inputaddr = 0;
  848. /* Check that data aligned on u32 */
  849. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  850. {
  851. /* Process Locked */
  852. __HAL_UNLOCK(hcryp);
  853. /* Return function status */
  854. return HAL_ERROR;
  855. }
  856. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  857. {
  858. /* Process Locked */
  859. __HAL_LOCK(hcryp);
  860. /* Get the buffer addresses and sizes */
  861. hcryp->CrypInCount = Size;
  862. hcryp->pCrypInBuffPtr = pCypherData;
  863. hcryp->pCrypOutBuffPtr = pPlainData;
  864. hcryp->CrypOutCount = Size;
  865. /* Change the CRYP state */
  866. hcryp->State = HAL_CRYP_STATE_BUSY;
  867. /* Check if initialization phase has already been performed */
  868. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  869. {
  870. /* Set the key */
  871. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  872. /* Reset the CHMOD & MODE bits */
  873. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  874. /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
  875. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
  876. /* Set the phase */
  877. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  878. }
  879. /* Enable Interrupts */
  880. __HAL_CRYP_ENABLE_IT(AES_IT_CC);
  881. /* Enable CRYP */
  882. __HAL_CRYP_ENABLE();
  883. /* Get the last input data adress */
  884. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  885. /* Write the Input block in the Data Input register */
  886. AES->DINR = *(uint32_t*)(inputaddr);
  887. inputaddr+=4;
  888. AES->DINR = *(uint32_t*)(inputaddr);
  889. inputaddr+=4;
  890. AES->DINR = *(uint32_t*)(inputaddr);
  891. inputaddr+=4;
  892. AES->DINR = *(uint32_t*)(inputaddr);
  893. hcryp->pCrypInBuffPtr += 16;
  894. hcryp->CrypInCount -= 16;
  895. /* Return function status */
  896. return HAL_OK;
  897. }
  898. else
  899. {
  900. /* Return function status */
  901. return HAL_ERROR;
  902. }
  903. }
  904. /**
  905. * @brief Initializes the CRYP peripheral in AES CBC decryption mode using IT.
  906. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  907. * the configuration information for CRYP module
  908. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  909. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  910. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  911. * @retval HAL status
  912. */
  913. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  914. {
  915. uint32_t inputaddr = 0;
  916. /* Check that data aligned on u32 */
  917. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  918. {
  919. /* Process Locked */
  920. __HAL_UNLOCK(hcryp);
  921. /* Return function status */
  922. return HAL_ERROR;
  923. }
  924. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  925. {
  926. /* Process Locked */
  927. __HAL_LOCK(hcryp);
  928. /* Get the buffer addresses and sizes */
  929. hcryp->CrypInCount = Size;
  930. hcryp->pCrypInBuffPtr = pCypherData;
  931. hcryp->pCrypOutBuffPtr = pPlainData;
  932. hcryp->CrypOutCount = Size;
  933. /* Change the CRYP state */
  934. hcryp->State = HAL_CRYP_STATE_BUSY;
  935. /* Check if initialization phase has already been performed */
  936. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  937. {
  938. /* Set the key */
  939. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  940. /* Reset the CHMOD & MODE bits */
  941. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  942. /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
  943. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
  944. /* Set the Initialization Vector */
  945. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  946. /* Set the phase */
  947. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  948. }
  949. /* Enable Interrupts */
  950. __HAL_CRYP_ENABLE_IT(AES_IT_CC);
  951. /* Enable CRYP */
  952. __HAL_CRYP_ENABLE();
  953. /* Get the last input data adress */
  954. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  955. /* Write the Input block in the Data Input register */
  956. AES->DINR = *(uint32_t*)(inputaddr);
  957. inputaddr+=4;
  958. AES->DINR = *(uint32_t*)(inputaddr);
  959. inputaddr+=4;
  960. AES->DINR = *(uint32_t*)(inputaddr);
  961. inputaddr+=4;
  962. AES->DINR = *(uint32_t*)(inputaddr);
  963. hcryp->pCrypInBuffPtr += 16;
  964. hcryp->CrypInCount -= 16;
  965. /* Return function status */
  966. return HAL_OK;
  967. }
  968. else
  969. {
  970. /* Return function status */
  971. return HAL_ERROR;
  972. }
  973. }
  974. /**
  975. * @brief Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt.
  976. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  977. * the configuration information for CRYP module
  978. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  979. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  980. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  981. * @retval HAL status
  982. */
  983. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  984. {
  985. uint32_t inputaddr = 0;
  986. /* Check that data aligned on u32 */
  987. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  988. {
  989. /* Process Locked */
  990. __HAL_UNLOCK(hcryp);
  991. /* Return function status */
  992. return HAL_ERROR;
  993. }
  994. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  995. {
  996. /* Process Locked */
  997. __HAL_LOCK(hcryp);
  998. /* Get the buffer addresses and sizes */
  999. hcryp->CrypInCount = Size;
  1000. hcryp->pCrypInBuffPtr = pCypherData;
  1001. hcryp->pCrypOutBuffPtr = pPlainData;
  1002. hcryp->CrypOutCount = Size;
  1003. /* Change the CRYP state */
  1004. hcryp->State = HAL_CRYP_STATE_BUSY;
  1005. /* Check if initialization phase has already been performed */
  1006. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1007. {
  1008. /* Set the key */
  1009. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  1010. /* Reset the CHMOD & MODE bits */
  1011. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  1012. /* Set the CRYP peripheral in AES CTR decryption mode */
  1013. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
  1014. /* Set the Initialization Vector */
  1015. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  1016. /* Set the phase */
  1017. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1018. }
  1019. /* Enable Interrupts */
  1020. __HAL_CRYP_ENABLE_IT(AES_IT_CC);
  1021. /* Enable CRYP */
  1022. __HAL_CRYP_ENABLE();
  1023. /* Get the last input data adress */
  1024. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1025. /* Write the Input block in the Data Input register */
  1026. AES->DINR = *(uint32_t*)(inputaddr);
  1027. inputaddr+=4;
  1028. AES->DINR = *(uint32_t*)(inputaddr);
  1029. inputaddr+=4;
  1030. AES->DINR = *(uint32_t*)(inputaddr);
  1031. inputaddr+=4;
  1032. AES->DINR = *(uint32_t*)(inputaddr);
  1033. hcryp->pCrypInBuffPtr += 16;
  1034. hcryp->CrypInCount -= 16;
  1035. /* Return function status */
  1036. return HAL_OK;
  1037. }
  1038. else
  1039. {
  1040. /* Return function status */
  1041. return HAL_ERROR;
  1042. }
  1043. }
  1044. /**
  1045. * @brief Initializes the CRYP peripheral in AES ECB encryption mode using DMA.
  1046. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1047. * the configuration information for CRYP module
  1048. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  1049. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1050. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  1051. * @retval HAL status
  1052. */
  1053. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1054. {
  1055. uint32_t inputaddr = 0, outputaddr = 0;
  1056. /* Check that data aligned on u32 */
  1057. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  1058. {
  1059. /* Process Locked */
  1060. __HAL_UNLOCK(hcryp);
  1061. /* Return function status */
  1062. return HAL_ERROR;
  1063. }
  1064. /* Check if HAL_CRYP_Init has been called */
  1065. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  1066. {
  1067. /* Process Locked */
  1068. __HAL_LOCK(hcryp);
  1069. inputaddr = (uint32_t)pPlainData;
  1070. outputaddr = (uint32_t)pCypherData;
  1071. /* Change the CRYP state */
  1072. hcryp->State = HAL_CRYP_STATE_BUSY;
  1073. /* Check if initialization phase has already been performed */
  1074. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1075. {
  1076. /* Set the key */
  1077. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  1078. /* Set the CRYP peripheral in AES ECB mode */
  1079. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
  1080. /* Set the phase */
  1081. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1082. }
  1083. /* Set the input and output addresses and start DMA transfer */
  1084. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1085. /* Process Unlocked */
  1086. __HAL_UNLOCK(hcryp);
  1087. /* Return function status */
  1088. return HAL_OK;
  1089. }
  1090. else
  1091. {
  1092. return HAL_ERROR;
  1093. }
  1094. }
  1095. /**
  1096. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
  1097. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1098. * the configuration information for CRYP module
  1099. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  1100. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  1101. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  1102. * @retval HAL status
  1103. */
  1104. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1105. {
  1106. uint32_t inputaddr = 0, outputaddr = 0;
  1107. /* Check that data aligned on u32 */
  1108. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  1109. {
  1110. /* Process Locked */
  1111. __HAL_UNLOCK(hcryp);
  1112. /* Return function status */
  1113. return HAL_ERROR;
  1114. }
  1115. /* Check if HAL_CRYP_Init has been called */
  1116. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  1117. {
  1118. /* Process Locked */
  1119. __HAL_LOCK(hcryp);
  1120. inputaddr = (uint32_t)pPlainData;
  1121. outputaddr = (uint32_t)pCypherData;
  1122. /* Change the CRYP state */
  1123. hcryp->State = HAL_CRYP_STATE_BUSY;
  1124. /* Check if initialization phase has already been performed */
  1125. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1126. {
  1127. /* Set the key */
  1128. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  1129. /* Set the CRYP peripheral in AES CBC mode */
  1130. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
  1131. /* Set the Initialization Vector */
  1132. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  1133. /* Set the phase */
  1134. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1135. }
  1136. /* Set the input and output addresses and start DMA transfer */
  1137. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1138. /* Process Unlocked */
  1139. __HAL_UNLOCK(hcryp);
  1140. /* Return function status */
  1141. return HAL_OK;
  1142. }
  1143. else
  1144. {
  1145. return HAL_ERROR;
  1146. }
  1147. }
  1148. /**
  1149. * @brief Initializes the CRYP peripheral in AES CTR encryption mode using DMA.
  1150. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1151. * the configuration information for CRYP module
  1152. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  1153. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  1154. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  1155. * @retval HAL status
  1156. */
  1157. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1158. {
  1159. uint32_t inputaddr = 0, outputaddr = 0;
  1160. /* Check that data aligned on u32 */
  1161. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  1162. {
  1163. /* Process Locked */
  1164. __HAL_UNLOCK(hcryp);
  1165. /* Return function status */
  1166. return HAL_ERROR;
  1167. }
  1168. /* Check if HAL_CRYP_Init has been called */
  1169. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  1170. {
  1171. /* Process Locked */
  1172. __HAL_LOCK(hcryp);
  1173. inputaddr = (uint32_t)pPlainData;
  1174. outputaddr = (uint32_t)pCypherData;
  1175. /* Change the CRYP state */
  1176. hcryp->State = HAL_CRYP_STATE_BUSY;
  1177. /* Check if initialization phase has already been performed */
  1178. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1179. {
  1180. /* Set the key */
  1181. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  1182. /* Set the CRYP peripheral in AES CTR mode */
  1183. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
  1184. /* Set the Initialization Vector */
  1185. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  1186. /* Set the phase */
  1187. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1188. }
  1189. /* Set the input and output addresses and start DMA transfer */
  1190. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1191. /* Process Unlocked */
  1192. __HAL_UNLOCK(hcryp);
  1193. /* Return function status */
  1194. return HAL_OK;
  1195. }
  1196. else
  1197. {
  1198. return HAL_ERROR;
  1199. }
  1200. }
  1201. /**
  1202. * @brief Initializes the CRYP peripheral in AES ECB decryption mode using DMA.
  1203. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1204. * the configuration information for CRYP module
  1205. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  1206. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1207. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  1208. * @retval HAL status
  1209. */
  1210. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1211. {
  1212. uint32_t inputaddr = 0, outputaddr = 0;
  1213. /* Check that data aligned on u32 */
  1214. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  1215. {
  1216. /* Process Locked */
  1217. __HAL_UNLOCK(hcryp);
  1218. /* Return function status */
  1219. return HAL_ERROR;
  1220. }
  1221. /* Check if HAL_CRYP_Init has been called */
  1222. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  1223. {
  1224. /* Process Locked */
  1225. __HAL_LOCK(hcryp);
  1226. inputaddr = (uint32_t)pCypherData;
  1227. outputaddr = (uint32_t)pPlainData;
  1228. /* Change the CRYP state */
  1229. hcryp->State = HAL_CRYP_STATE_BUSY;
  1230. /* Check if initialization phase has already been performed */
  1231. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1232. {
  1233. /* Set the key */
  1234. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  1235. /* Reset the CHMOD & MODE bits */
  1236. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  1237. /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
  1238. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
  1239. /* Set the phase */
  1240. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1241. }
  1242. /* Set the input and output addresses and start DMA transfer */
  1243. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1244. /* Process Unlocked */
  1245. __HAL_UNLOCK(hcryp);
  1246. /* Return function status */
  1247. return HAL_OK;
  1248. }
  1249. else
  1250. {
  1251. return HAL_ERROR;
  1252. }
  1253. }
  1254. /**
  1255. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
  1256. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1257. * the configuration information for CRYP module
  1258. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  1259. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1260. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  1261. * @retval HAL status
  1262. */
  1263. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1264. {
  1265. uint32_t inputaddr = 0, outputaddr = 0;
  1266. /* Check that data aligned on u32 */
  1267. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  1268. {
  1269. /* Process Locked */
  1270. __HAL_UNLOCK(hcryp);
  1271. /* Return function status */
  1272. return HAL_ERROR;
  1273. }
  1274. /* Check if HAL_CRYP_Init has been called */
  1275. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  1276. {
  1277. /* Process Locked */
  1278. __HAL_LOCK(hcryp);
  1279. inputaddr = (uint32_t)pCypherData;
  1280. outputaddr = (uint32_t)pPlainData;
  1281. /* Change the CRYP state */
  1282. hcryp->State = HAL_CRYP_STATE_BUSY;
  1283. /* Check if initialization phase has already been performed */
  1284. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1285. {
  1286. /* Set the key */
  1287. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  1288. /* Reset the CHMOD & MODE bits */
  1289. CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
  1290. /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
  1291. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
  1292. /* Set the Initialization Vector */
  1293. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  1294. /* Set the phase */
  1295. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1296. }
  1297. /* Set the input and output addresses and start DMA transfer */
  1298. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1299. /* Process Unlocked */
  1300. __HAL_UNLOCK(hcryp);
  1301. /* Return function status */
  1302. return HAL_OK;
  1303. }
  1304. else
  1305. {
  1306. return HAL_ERROR;
  1307. }
  1308. }
  1309. /**
  1310. * @brief Initializes the CRYP peripheral in AES CTR decryption mode using DMA.
  1311. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1312. * the configuration information for CRYP module
  1313. * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
  1314. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  1315. * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
  1316. * @retval HAL status
  1317. */
  1318. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1319. {
  1320. uint32_t inputaddr = 0, outputaddr = 0;
  1321. /* Check that data aligned on u32 */
  1322. if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
  1323. {
  1324. /* Process Locked */
  1325. __HAL_UNLOCK(hcryp);
  1326. /* Return function status */
  1327. return HAL_ERROR;
  1328. }
  1329. /* Check if HAL_CRYP_Init has been called */
  1330. if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
  1331. {
  1332. /* Process Locked */
  1333. __HAL_LOCK(hcryp);
  1334. inputaddr = (uint32_t)pCypherData;
  1335. outputaddr = (uint32_t)pPlainData;
  1336. /* Change the CRYP state */
  1337. hcryp->State = HAL_CRYP_STATE_BUSY;
  1338. /* Check if initialization phase has already been performed */
  1339. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1340. {
  1341. /* Set the key */
  1342. CRYP_SetKey(hcryp, hcryp->Init.pKey);
  1343. /* Set the CRYP peripheral in AES CTR mode */
  1344. __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
  1345. /* Set the Initialization Vector */
  1346. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
  1347. /* Set the phase */
  1348. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1349. }
  1350. /* Set the input and output addresses and start DMA transfer */
  1351. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1352. /* Process Unlocked */
  1353. __HAL_UNLOCK(hcryp);
  1354. /* Return function status */
  1355. return HAL_OK;
  1356. }
  1357. else
  1358. {
  1359. return HAL_ERROR;
  1360. }
  1361. }
  1362. /**
  1363. * @}
  1364. */
  1365. /** @defgroup CRYP_Exported_Functions_Group3 DMA callback functions
  1366. * @brief DMA callback functions.
  1367. *
  1368. @verbatim
  1369. ==============================================================================
  1370. ##### DMA callback functions #####
  1371. ==============================================================================
  1372. [..] This section provides DMA callback functions:
  1373. (+) DMA Input data transfer complete
  1374. (+) DMA Output data transfer complete
  1375. (+) DMA error
  1376. @endverbatim
  1377. * @{
  1378. */
  1379. /**
  1380. * @brief CRYP error callback.
  1381. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1382. * the configuration information for CRYP module
  1383. * @retval None
  1384. */
  1385. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  1386. {
  1387. /* NOTE : This function should not be modified; when the callback is needed,
  1388. the HAL_CRYP_ErrorCallback can be implemented in the user file
  1389. */
  1390. }
  1391. /**
  1392. * @brief Input transfer completed callback.
  1393. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1394. * the configuration information for CRYP module
  1395. * @retval None
  1396. */
  1397. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  1398. {
  1399. /* NOTE : This function should not be modified; when the callback is needed,
  1400. the HAL_CRYP_InCpltCallback can be implemented in the user file
  1401. */
  1402. }
  1403. /**
  1404. * @brief Output transfer completed callback.
  1405. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1406. * the configuration information for CRYP module
  1407. * @retval None
  1408. */
  1409. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  1410. {
  1411. /* NOTE : This function should not be modified; when the callback is needed,
  1412. the HAL_CRYP_OutCpltCallback can be implemented in the user file
  1413. */
  1414. }
  1415. /**
  1416. * @}
  1417. */
  1418. /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
  1419. * @brief CRYP IRQ handler.
  1420. *
  1421. @verbatim
  1422. ==============================================================================
  1423. ##### CRYP IRQ handler management #####
  1424. ==============================================================================
  1425. [..] This section provides CRYP IRQ handler function.
  1426. @endverbatim
  1427. * @{
  1428. */
  1429. /**
  1430. * @brief This function handles CRYP interrupt request.
  1431. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1432. * the configuration information for CRYP module
  1433. * @retval None
  1434. */
  1435. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  1436. {
  1437. /* Check if error occurred*/
  1438. if (__HAL_CRYP_GET_IT_SOURCE(hcryp, AES_IT_ERR) != RESET)
  1439. {
  1440. if (__HAL_CRYP_GET_FLAG(AES_FLAG_RDERR) != RESET)
  1441. {
  1442. __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_RDERR);
  1443. }
  1444. if (__HAL_CRYP_GET_FLAG(AES_FLAG_WRERR) != RESET)
  1445. {
  1446. __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_WRERR);
  1447. }
  1448. if (__HAL_CRYP_GET_FLAG(AES_FLAG_CCF) != RESET)
  1449. {
  1450. __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
  1451. }
  1452. hcryp->State= HAL_CRYP_STATE_ERROR;
  1453. /* Disable Computation Complete Interrupt */
  1454. __HAL_CRYP_DISABLE_IT(AES_IT_CC);
  1455. __HAL_CRYP_DISABLE_IT(AES_IT_ERR);
  1456. HAL_CRYP_ErrorCallback(hcryp);
  1457. /* Process Unlocked */
  1458. __HAL_UNLOCK(hcryp);
  1459. return;
  1460. }
  1461. /* Check if computation complete interrupt was enabled*/
  1462. if (__HAL_CRYP_GET_IT_SOURCE(hcryp, AES_IT_CC) != RESET)
  1463. {
  1464. /* Clear CCF Flag */
  1465. __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
  1466. CRYP_EncryptDecrypt_IT(hcryp);
  1467. }
  1468. }
  1469. /**
  1470. * @}
  1471. */
  1472. /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
  1473. * @brief Peripheral State functions.
  1474. *
  1475. @verbatim
  1476. ==============================================================================
  1477. ##### Peripheral State functions #####
  1478. ==============================================================================
  1479. [..]
  1480. This subsection permits to get in run-time the status of the peripheral.
  1481. @endverbatim
  1482. * @{
  1483. */
  1484. /**
  1485. * @brief Returns the CRYP state.
  1486. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1487. * the configuration information for CRYP module
  1488. * @retval HAL state
  1489. */
  1490. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  1491. {
  1492. return hcryp->State;
  1493. }
  1494. /**
  1495. * @}
  1496. */
  1497. /**
  1498. * @}
  1499. */
  1500. /** @addtogroup CRYP_Private_Functions
  1501. * @{
  1502. */
  1503. /**
  1504. * @brief IT function called under interruption context to continue encryption or decryption
  1505. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1506. * the configuration information for CRYP module
  1507. * @retval HAL status
  1508. */
  1509. static HAL_StatusTypeDef CRYP_EncryptDecrypt_IT(CRYP_HandleTypeDef *hcryp)
  1510. {
  1511. uint32_t inputaddr = 0, outputaddr = 0;
  1512. /* Get the last Output data adress */
  1513. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1514. /* Read the Output block from the Output Register */
  1515. *(uint32_t*)(outputaddr) = AES->DOUTR;
  1516. outputaddr+=4;
  1517. *(uint32_t*)(outputaddr) = AES->DOUTR;
  1518. outputaddr+=4;
  1519. *(uint32_t*)(outputaddr) = AES->DOUTR;
  1520. outputaddr+=4;
  1521. *(uint32_t*)(outputaddr) = AES->DOUTR;
  1522. hcryp->pCrypOutBuffPtr += 16;
  1523. hcryp->CrypOutCount -= 16;
  1524. /* Check if all input text is encrypted or decrypted */
  1525. if(hcryp->CrypOutCount == 0)
  1526. {
  1527. /* Disable Computation Complete Interrupt */
  1528. __HAL_CRYP_DISABLE_IT(AES_IT_CC);
  1529. __HAL_CRYP_DISABLE_IT(AES_IT_ERR);
  1530. /* Process Unlocked */
  1531. __HAL_UNLOCK(hcryp);
  1532. /* Change the CRYP state */
  1533. hcryp->State = HAL_CRYP_STATE_READY;
  1534. /* Call computation complete callback */
  1535. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  1536. }
  1537. else /* Process the rest of input text */
  1538. {
  1539. /* Get the last Intput data adress */
  1540. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1541. /* Write the Input block in the Data Input register */
  1542. AES->DINR = *(uint32_t*)(inputaddr);
  1543. inputaddr+=4;
  1544. AES->DINR = *(uint32_t*)(inputaddr);
  1545. inputaddr+=4;
  1546. AES->DINR = *(uint32_t*)(inputaddr);
  1547. inputaddr+=4;
  1548. AES->DINR = *(uint32_t*)(inputaddr);
  1549. hcryp->pCrypInBuffPtr += 16;
  1550. hcryp->CrypInCount -= 16;
  1551. }
  1552. return HAL_OK;
  1553. }
  1554. /**
  1555. * @brief DMA CRYP Input Data process complete callback.
  1556. * @param hdma: DMA handle
  1557. * @retval None
  1558. */
  1559. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
  1560. {
  1561. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1562. /* Disable the DMA transfer for input request */
  1563. CLEAR_BIT(AES->CR, AES_CR_DMAINEN);
  1564. /* Call input data transfer complete callback */
  1565. HAL_CRYP_InCpltCallback(hcryp);
  1566. }
  1567. /**
  1568. * @brief DMA CRYP Output Data process complete callback.
  1569. * @param hdma: DMA handle
  1570. * @retval None
  1571. */
  1572. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
  1573. {
  1574. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1575. /* Disable the DMA transfer for output request by resetting the DMAOUTEN bit
  1576. in the DMACR register */
  1577. CLEAR_BIT(AES->CR, AES_CR_DMAOUTEN);
  1578. /* Clear CCF Flag */
  1579. __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
  1580. /* Disable CRYP */
  1581. __HAL_CRYP_DISABLE();
  1582. /* Change the CRYP state to ready */
  1583. hcryp->State = HAL_CRYP_STATE_READY;
  1584. /* Call output data transfer complete callback */
  1585. HAL_CRYP_OutCpltCallback(hcryp);
  1586. }
  1587. /**
  1588. * @brief DMA CRYP communication error callback.
  1589. * @param hdma: DMA handle
  1590. * @retval None
  1591. */
  1592. static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
  1593. {
  1594. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1595. hcryp->State= HAL_CRYP_STATE_ERROR;
  1596. HAL_CRYP_ErrorCallback(hcryp);
  1597. }
  1598. /**
  1599. * @brief Writes the Key in Key registers.
  1600. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1601. * the configuration information for CRYP module
  1602. * @param Key: Pointer to Key buffer
  1603. * @note Key must be written as little endian.
  1604. * If Key pointer points at address n,
  1605. * n[15:0] contains key[96:127],
  1606. * (n+4)[15:0] contains key[64:95],
  1607. * (n+8)[15:0] contains key[32:63] and
  1608. * (n+12)[15:0] contains key[0:31]
  1609. * @retval None
  1610. */
  1611. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key)
  1612. {
  1613. uint32_t keyaddr = (uint32_t)Key;
  1614. AES->KEYR3 = __REV(*(uint32_t*)(keyaddr));
  1615. keyaddr+=4;
  1616. AES->KEYR2 = __REV(*(uint32_t*)(keyaddr));
  1617. keyaddr+=4;
  1618. AES->KEYR1 = __REV(*(uint32_t*)(keyaddr));
  1619. keyaddr+=4;
  1620. AES->KEYR0 = __REV(*(uint32_t*)(keyaddr));
  1621. }
  1622. /**
  1623. * @brief Writes the InitVector/InitCounter in IV registers.
  1624. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1625. * the configuration information for CRYP module
  1626. * @param InitVector: Pointer to InitVector/InitCounter buffer
  1627. * @note Init Vector must be written as little endian.
  1628. * If Init Vector pointer points at address n,
  1629. * n[15:0] contains Vector[96:127],
  1630. * (n+4)[15:0] contains Vector[64:95],
  1631. * (n+8)[15:0] contains Vector[32:63] and
  1632. * (n+12)[15:0] contains Vector[0:31]
  1633. * @retval None
  1634. */
  1635. static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector)
  1636. {
  1637. uint32_t ivaddr = (uint32_t)InitVector;
  1638. AES->IVR3 = __REV(*(uint32_t*)(ivaddr));
  1639. ivaddr+=4;
  1640. AES->IVR2 = __REV(*(uint32_t*)(ivaddr));
  1641. ivaddr+=4;
  1642. AES->IVR1 = __REV(*(uint32_t*)(ivaddr));
  1643. ivaddr+=4;
  1644. AES->IVR0 = __REV(*(uint32_t*)(ivaddr));
  1645. }
  1646. /**
  1647. * @brief Process Data: Writes Input data in polling mode and reads the output data
  1648. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1649. * the configuration information for CRYP module
  1650. * @param Input: Pointer to the Input buffer
  1651. * @param Ilength: Length of the Input buffer, must be a multiple of 16.
  1652. * @param Output: Pointer to the returned buffer
  1653. * @param Timeout: Specify Timeout value
  1654. * @retval None
  1655. */
  1656. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
  1657. {
  1658. uint32_t tickstart = 0;
  1659. uint32_t index = 0;
  1660. uint32_t inputaddr = (uint32_t)Input;
  1661. uint32_t outputaddr = (uint32_t)Output;
  1662. for(index=0; (index < Ilength); index += 16)
  1663. {
  1664. /* Write the Input block in the Data Input register */
  1665. AES->DINR = *(uint32_t*)(inputaddr);
  1666. inputaddr+=4;
  1667. AES->DINR = *(uint32_t*)(inputaddr);
  1668. inputaddr+=4;
  1669. AES->DINR = *(uint32_t*)(inputaddr);
  1670. inputaddr+=4;
  1671. AES->DINR = *(uint32_t*)(inputaddr);
  1672. inputaddr+=4;
  1673. /* Get timeout */
  1674. tickstart = HAL_GetTick();
  1675. while(HAL_IS_BIT_CLR(AES->SR, AES_SR_CCF))
  1676. {
  1677. /* Check for the Timeout */
  1678. if(Timeout != HAL_MAX_DELAY)
  1679. {
  1680. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1681. {
  1682. /* Change state */
  1683. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1684. /* Process Unlocked */
  1685. __HAL_UNLOCK(hcryp);
  1686. return HAL_TIMEOUT;
  1687. }
  1688. }
  1689. }
  1690. /* Clear CCF Flag */
  1691. __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
  1692. /* Read the Output block from the Data Output Register */
  1693. *(uint32_t*)(outputaddr) = AES->DOUTR;
  1694. outputaddr+=4;
  1695. *(uint32_t*)(outputaddr) = AES->DOUTR;
  1696. outputaddr+=4;
  1697. *(uint32_t*)(outputaddr) = AES->DOUTR;
  1698. outputaddr+=4;
  1699. *(uint32_t*)(outputaddr) = AES->DOUTR;
  1700. outputaddr+=4;
  1701. }
  1702. /* Return function status */
  1703. return HAL_OK;
  1704. }
  1705. /**
  1706. * @brief Set the DMA configuration and start the DMA transfer
  1707. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1708. * the configuration information for CRYP module
  1709. * @param inputaddr: address of the Input buffer
  1710. * @param Size: Size of the Input buffer, must be a multiple of 16.
  1711. * @param outputaddr: address of the Output buffer
  1712. * @retval None
  1713. */
  1714. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  1715. {
  1716. /* Set the CRYP DMA transfer complete callback */
  1717. hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
  1718. /* Set the DMA error callback */
  1719. hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
  1720. /* Set the CRYP DMA transfer complete callback */
  1721. hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
  1722. /* Set the DMA error callback */
  1723. hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
  1724. /* Enable the DMA In DMA Stream */
  1725. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&AES->DINR, Size/4);
  1726. /* Enable the DMA Out DMA Stream */
  1727. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&AES->DOUTR, outputaddr, Size/4);
  1728. /* Enable In and Out DMA requests */
  1729. SET_BIT(AES->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN));
  1730. /* Enable CRYP */
  1731. __HAL_CRYP_ENABLE();
  1732. }
  1733. /**
  1734. * @}
  1735. */
  1736. #endif /* STM32L162xC || STM32L162xCA || STM32L162xD || STM32L162xE*/
  1737. /**
  1738. * @}
  1739. */
  1740. /**
  1741. * @}
  1742. */
  1743. #endif /* HAL_CRYP_MODULE_ENABLED */
  1744. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/