stm32l1xx_hal_cryp.c 68 KB

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