You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_cryp.c 67KB

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