stm32f7xx_hal_cryp.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_cryp.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  7. * @brief CRYP HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Cryptography (CRYP) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + AES processing functions
  12. * + DES processing functions
  13. * + TDES processing functions
  14. * + DMA callback functions
  15. * + CRYP IRQ handler management
  16. * + Peripheral State functions
  17. *
  18. @verbatim
  19. ==============================================================================
  20. ##### How to use this driver #####
  21. ==============================================================================
  22. [..]
  23. The CRYP HAL driver can be used as follows:
  24. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  25. (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()
  26. (##) In case of using interrupts (e.g. HAL_CRYP_AESECB_Encrypt_IT())
  27. (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  28. (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
  29. (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
  30. (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_AESECB_Encrypt_DMA())
  31. (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE()
  32. (+++) Configure and enable two DMA streams one for managing data transfer from
  33. memory to peripheral (input stream) and another stream for managing data
  34. transfer from peripheral to memory (output stream)
  35. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  36. using __HAL_LINKDMA()
  37. (+++) Configure the priority and enable the NVIC for the transfer complete
  38. interrupt on the two DMA Streams. The output stream should have higher
  39. priority than the input stream HAL_NVIC_SetPriority() and 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 key size: 128, 192 and 256. This parameter is relevant only for AES
  43. (##) The encryption/decryption key. It's size depends on the algorithm
  44. used for encryption/decryption
  45. (##) The initialization vector (counter). It is not used ECB mode.
  46. (#)Three processing (encryption/decryption) functions are available:
  47. (##) Polling mode: encryption and decryption APIs are blocking functions
  48. i.e. they process the data and wait till the processing is finished,
  49. e.g. HAL_CRYP_AESCBC_Encrypt()
  50. (##) Interrupt mode: encryption and decryption APIs are not blocking functions
  51. i.e. they process the data under interrupt,
  52. e.g. HAL_CRYP_AESCBC_Encrypt_IT()
  53. (##) DMA mode: encryption and decryption APIs are not blocking functions
  54. i.e. the data transfer is ensured by DMA,
  55. e.g. HAL_CRYP_AESCBC_Encrypt_DMA()
  56. (#)When the processing function is called at first time after HAL_CRYP_Init()
  57. the CRYP peripheral is initialized and processes the buffer in input.
  58. At second call, the processing function performs an append of the already
  59. processed buffer.
  60. When a new data block is to be processed, call HAL_CRYP_Init() then the
  61. processing function.
  62. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  63. @endverbatim
  64. ******************************************************************************
  65. * @attention
  66. *
  67. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  68. *
  69. * Redistribution and use in source and binary forms, with or without modification,
  70. * are permitted provided that the following conditions are met:
  71. * 1. Redistributions of source code must retain the above copyright notice,
  72. * this list of conditions and the following disclaimer.
  73. * 2. Redistributions in binary form must reproduce the above copyright notice,
  74. * this list of conditions and the following disclaimer in the documentation
  75. * and/or other materials provided with the distribution.
  76. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  77. * may be used to endorse or promote products derived from this software
  78. * without specific prior written permission.
  79. *
  80. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  81. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  83. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  84. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  85. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  86. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  87. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  88. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  89. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  90. *
  91. ******************************************************************************
  92. */
  93. /* Includes ------------------------------------------------------------------*/
  94. #include "stm32f7xx_hal.h"
  95. /** @addtogroup STM32F7xx_HAL_Driver
  96. * @{
  97. */
  98. #if defined(STM32F756xx)
  99. /** @defgroup CRYP CRYP
  100. * @brief CRYP HAL module driver.
  101. * @{
  102. */
  103. #ifdef HAL_CRYP_MODULE_ENABLED
  104. /* Private typedef -----------------------------------------------------------*/
  105. /* Private define ------------------------------------------------------------*/
  106. /** @addtogroup CRYP_Private_define
  107. * @{
  108. */
  109. #define CRYP_TIMEOUT_VALUE 1
  110. /**
  111. * @}
  112. */
  113. /* Private macro -------------------------------------------------------------*/
  114. /* Private variables ---------------------------------------------------------*/
  115. /* Private function prototypes -----------------------------------------------*/
  116. /** @addtogroup CRYP_Private_Functions_prototypes
  117. * @{
  118. */
  119. static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize);
  120. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize);
  121. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
  122. static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
  123. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
  124. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
  125. static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
  126. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  127. static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
  128. static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
  129. static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
  130. static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
  131. /**
  132. * @}
  133. */
  134. /* Private functions ---------------------------------------------------------*/
  135. /** @addtogroup CRYP_Private_Functions
  136. * @{
  137. */
  138. /**
  139. * @brief DMA CRYP Input Data process complete callback.
  140. * @param hdma: DMA handle
  141. * @retval None
  142. */
  143. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
  144. {
  145. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  146. /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit
  147. in the DMACR register */
  148. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN);
  149. /* Call input data transfer complete callback */
  150. HAL_CRYP_InCpltCallback(hcryp);
  151. }
  152. /**
  153. * @brief DMA CRYP Output Data process complete callback.
  154. * @param hdma: DMA handle
  155. * @retval None
  156. */
  157. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
  158. {
  159. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  160. /* Disable the DMA transfer for output FIFO request by resetting the DOEN bit
  161. in the DMACR register */
  162. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN);
  163. /* Disable CRYP */
  164. __HAL_CRYP_DISABLE(hcryp);
  165. /* Change the CRYP state to ready */
  166. hcryp->State = HAL_CRYP_STATE_READY;
  167. /* Call output data transfer complete callback */
  168. HAL_CRYP_OutCpltCallback(hcryp);
  169. }
  170. /**
  171. * @brief DMA CRYP communication error callback.
  172. * @param hdma: DMA handle
  173. * @retval None
  174. */
  175. static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
  176. {
  177. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  178. hcryp->State= HAL_CRYP_STATE_READY;
  179. HAL_CRYP_ErrorCallback(hcryp);
  180. }
  181. /**
  182. * @brief Writes the Key in Key registers.
  183. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  184. * the configuration information for CRYP module
  185. * @param Key: Pointer to Key buffer
  186. * @param KeySize: Size of Key
  187. * @retval None
  188. */
  189. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize)
  190. {
  191. uint32_t keyaddr = (uint32_t)Key;
  192. switch(KeySize)
  193. {
  194. case CRYP_KEYSIZE_256B:
  195. /* Key Initialisation */
  196. hcryp->Instance->K0LR = __REV(*(uint32_t*)(keyaddr));
  197. keyaddr+=4;
  198. hcryp->Instance->K0RR = __REV(*(uint32_t*)(keyaddr));
  199. keyaddr+=4;
  200. hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));
  201. keyaddr+=4;
  202. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  203. keyaddr+=4;
  204. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  205. keyaddr+=4;
  206. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  207. keyaddr+=4;
  208. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  209. keyaddr+=4;
  210. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  211. break;
  212. case CRYP_KEYSIZE_192B:
  213. hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));
  214. keyaddr+=4;
  215. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  216. keyaddr+=4;
  217. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  218. keyaddr+=4;
  219. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  220. keyaddr+=4;
  221. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  222. keyaddr+=4;
  223. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  224. break;
  225. case CRYP_KEYSIZE_128B:
  226. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  227. keyaddr+=4;
  228. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  229. keyaddr+=4;
  230. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  231. keyaddr+=4;
  232. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. /**
  239. * @brief Writes the InitVector/InitCounter in IV registers.
  240. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  241. * the configuration information for CRYP module
  242. * @param InitVector: Pointer to InitVector/InitCounter buffer
  243. * @param IVSize: Size of the InitVector/InitCounter
  244. * @retval None
  245. */
  246. static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize)
  247. {
  248. uint32_t ivaddr = (uint32_t)InitVector;
  249. switch(IVSize)
  250. {
  251. case CRYP_KEYSIZE_128B:
  252. hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr));
  253. ivaddr+=4;
  254. hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr));
  255. ivaddr+=4;
  256. hcryp->Instance->IV1LR = __REV(*(uint32_t*)(ivaddr));
  257. ivaddr+=4;
  258. hcryp->Instance->IV1RR = __REV(*(uint32_t*)(ivaddr));
  259. break;
  260. /* Whatever key size 192 or 256, Init vector is written in IV0LR and IV0RR */
  261. case CRYP_KEYSIZE_192B:
  262. hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr));
  263. ivaddr+=4;
  264. hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr));
  265. break;
  266. case CRYP_KEYSIZE_256B:
  267. hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr));
  268. ivaddr+=4;
  269. hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr));
  270. break;
  271. default:
  272. break;
  273. }
  274. }
  275. /**
  276. * @brief Process Data: Writes Input data in polling mode and read the output data
  277. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  278. * the configuration information for CRYP module
  279. * @param Input: Pointer to the Input buffer
  280. * @param Ilength: Length of the Input buffer, must be a multiple of 16.
  281. * @param Output: Pointer to the returned buffer
  282. * @param Timeout: Timeout value
  283. * @retval None
  284. */
  285. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
  286. {
  287. uint32_t tickstart = 0;
  288. uint32_t i = 0;
  289. uint32_t inputaddr = (uint32_t)Input;
  290. uint32_t outputaddr = (uint32_t)Output;
  291. for(i=0; (i < Ilength); i+=16)
  292. {
  293. /* Write the Input block in the IN FIFO */
  294. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  295. inputaddr+=4;
  296. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  297. inputaddr+=4;
  298. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  299. inputaddr+=4;
  300. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  301. inputaddr+=4;
  302. /* Get tick */
  303. tickstart = HAL_GetTick();
  304. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  305. {
  306. /* Check for the Timeout */
  307. if(Timeout != HAL_MAX_DELAY)
  308. {
  309. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  310. {
  311. /* Change state */
  312. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  313. /* Process Unlocked */
  314. __HAL_UNLOCK(hcryp);
  315. return HAL_TIMEOUT;
  316. }
  317. }
  318. }
  319. /* Read the Output block from the Output FIFO */
  320. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  321. outputaddr+=4;
  322. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  323. outputaddr+=4;
  324. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  325. outputaddr+=4;
  326. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  327. outputaddr+=4;
  328. }
  329. /* Return function status */
  330. return HAL_OK;
  331. }
  332. /**
  333. * @brief Process Data: Write Input data in polling mode.
  334. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  335. * the configuration information for CRYP module
  336. * @param Input: Pointer to the Input buffer
  337. * @param Ilength: Length of the Input buffer, must be a multiple of 8
  338. * @param Output: Pointer to the returned buffer
  339. * @param Timeout: Specify Timeout value
  340. * @retval None
  341. */
  342. static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
  343. {
  344. uint32_t tickstart = 0;
  345. uint32_t i = 0;
  346. uint32_t inputaddr = (uint32_t)Input;
  347. uint32_t outputaddr = (uint32_t)Output;
  348. for(i=0; (i < Ilength); i+=8)
  349. {
  350. /* Write the Input block in the IN FIFO */
  351. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  352. inputaddr+=4;
  353. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  354. inputaddr+=4;
  355. /* Get tick */
  356. tickstart = HAL_GetTick();
  357. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  358. {
  359. /* Check for the Timeout */
  360. if(Timeout != HAL_MAX_DELAY)
  361. {
  362. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  363. {
  364. /* Change state */
  365. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  366. /* Process Unlocked */
  367. __HAL_UNLOCK(hcryp);
  368. return HAL_TIMEOUT;
  369. }
  370. }
  371. }
  372. /* Read the Output block from the Output FIFO */
  373. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  374. outputaddr+=4;
  375. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  376. outputaddr+=4;
  377. }
  378. /* Return function status */
  379. return HAL_OK;
  380. }
  381. /**
  382. * @brief Set the DMA configuration and start the DMA transfer
  383. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  384. * the configuration information for CRYP module
  385. * @param inputaddr: address of the Input buffer
  386. * @param Size: Size of the Input buffer, must be a multiple of 16.
  387. * @param outputaddr: address of the Output buffer
  388. * @retval None
  389. */
  390. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  391. {
  392. /* Set the CRYP DMA transfer complete callback */
  393. hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
  394. /* Set the DMA error callback */
  395. hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
  396. /* Set the CRYP DMA transfer complete callback */
  397. hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
  398. /* Set the DMA error callback */
  399. hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
  400. /* Enable CRYP */
  401. __HAL_CRYP_ENABLE(hcryp);
  402. /* Enable the DMA In DMA Stream */
  403. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DR, Size/4);
  404. /* Enable In DMA request */
  405. hcryp->Instance->DMACR = (CRYP_DMACR_DIEN);
  406. /* Enable the DMA Out DMA Stream */
  407. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size/4);
  408. /* Enable Out DMA request */
  409. hcryp->Instance->DMACR |= CRYP_DMACR_DOEN;
  410. }
  411. /**
  412. * @brief Sets the CRYP peripheral in DES ECB mode.
  413. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  414. * the configuration information for CRYP module
  415. * @param Direction: Encryption or decryption
  416. * @retval None
  417. */
  418. static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
  419. {
  420. /* Check if initialization phase has already been performed */
  421. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  422. {
  423. /* Set the CRYP peripheral in AES ECB mode */
  424. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_DES_ECB | Direction);
  425. /* Set the key */
  426. hcryp->Instance->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey));
  427. hcryp->Instance->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4));
  428. /* Flush FIFO */
  429. __HAL_CRYP_FIFO_FLUSH(hcryp);
  430. /* Set the phase */
  431. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  432. }
  433. }
  434. /**
  435. * @brief Sets the CRYP peripheral in DES CBC mode.
  436. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  437. * the configuration information for CRYP module
  438. * @param Direction: Encryption or decryption
  439. * @retval None
  440. */
  441. static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
  442. {
  443. /* Check if initialization phase has already been performed */
  444. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  445. {
  446. /* Set the CRYP peripheral in AES ECB mode */
  447. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_DES_CBC | Direction);
  448. /* Set the key */
  449. hcryp->Instance->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey));
  450. hcryp->Instance->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4));
  451. /* Set the Initialization Vector */
  452. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B);
  453. /* Flush FIFO */
  454. __HAL_CRYP_FIFO_FLUSH(hcryp);
  455. /* Set the phase */
  456. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  457. }
  458. }
  459. /**
  460. * @brief Sets the CRYP peripheral in TDES ECB mode.
  461. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  462. * the configuration information for CRYP module
  463. * @param Direction: Encryption or decryption
  464. * @retval None
  465. */
  466. static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
  467. {
  468. /* Check if initialization phase has already been performed */
  469. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  470. {
  471. /* Set the CRYP peripheral in AES ECB mode */
  472. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_TDES_ECB | Direction);
  473. /* Set the key */
  474. CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B);
  475. /* Flush FIFO */
  476. __HAL_CRYP_FIFO_FLUSH(hcryp);
  477. /* Set the phase */
  478. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  479. }
  480. }
  481. /**
  482. * @brief Sets the CRYP peripheral in TDES CBC mode
  483. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  484. * the configuration information for CRYP module
  485. * @param Direction: Encryption or decryption
  486. * @retval None
  487. */
  488. static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
  489. {
  490. /* Check if initialization phase has already been performed */
  491. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  492. {
  493. /* Set the CRYP peripheral in AES CBC mode */
  494. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_TDES_CBC | Direction);
  495. /* Set the key */
  496. CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B);
  497. /* Set the Initialization Vector */
  498. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B);
  499. /* Flush FIFO */
  500. __HAL_CRYP_FIFO_FLUSH(hcryp);
  501. /* Set the phase */
  502. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  503. }
  504. }
  505. /**
  506. * @}
  507. */
  508. /* Exported functions --------------------------------------------------------*/
  509. /** @addtogroup CRYP_Exported_Functions
  510. * @{
  511. */
  512. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
  513. * @brief Initialization and Configuration functions.
  514. *
  515. @verbatim
  516. ==============================================================================
  517. ##### Initialization and de-initialization functions #####
  518. ==============================================================================
  519. [..] This section provides functions allowing to:
  520. (+) Initialize the CRYP according to the specified parameters
  521. in the CRYP_InitTypeDef and creates the associated handle
  522. (+) DeInitialize the CRYP peripheral
  523. (+) Initialize the CRYP MSP
  524. (+) DeInitialize CRYP MSP
  525. @endverbatim
  526. * @{
  527. */
  528. /**
  529. * @brief Initializes the CRYP according to the specified
  530. * parameters in the CRYP_InitTypeDef and creates the associated handle.
  531. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  532. * the configuration information for CRYP module
  533. * @retval HAL status
  534. */
  535. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  536. {
  537. /* Check the CRYP handle allocation */
  538. if(hcryp == NULL)
  539. {
  540. return HAL_ERROR;
  541. }
  542. /* Check the parameters */
  543. assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
  544. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  545. if(hcryp->State == HAL_CRYP_STATE_RESET)
  546. {
  547. /* Allocate lock resource and initialize it */
  548. hcryp->Lock = HAL_UNLOCKED;
  549. /* Init the low level hardware */
  550. HAL_CRYP_MspInit(hcryp);
  551. }
  552. /* Change the CRYP state */
  553. hcryp->State = HAL_CRYP_STATE_BUSY;
  554. /* Set the key size and data type*/
  555. CRYP->CR = (uint32_t) (hcryp->Init.KeySize | hcryp->Init.DataType);
  556. /* Reset CrypInCount and CrypOutCount */
  557. hcryp->CrypInCount = 0;
  558. hcryp->CrypOutCount = 0;
  559. /* Change the CRYP state */
  560. hcryp->State = HAL_CRYP_STATE_READY;
  561. /* Set the default CRYP phase */
  562. hcryp->Phase = HAL_CRYP_PHASE_READY;
  563. /* Return function status */
  564. return HAL_OK;
  565. }
  566. /**
  567. * @brief DeInitializes the CRYP peripheral.
  568. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  569. * the configuration information for CRYP module
  570. * @retval HAL status
  571. */
  572. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  573. {
  574. /* Check the CRYP handle allocation */
  575. if(hcryp == NULL)
  576. {
  577. return HAL_ERROR;
  578. }
  579. /* Change the CRYP state */
  580. hcryp->State = HAL_CRYP_STATE_BUSY;
  581. /* Set the default CRYP phase */
  582. hcryp->Phase = HAL_CRYP_PHASE_READY;
  583. /* Reset CrypInCount and CrypOutCount */
  584. hcryp->CrypInCount = 0;
  585. hcryp->CrypOutCount = 0;
  586. /* Disable the CRYP Peripheral Clock */
  587. __HAL_CRYP_DISABLE(hcryp);
  588. /* DeInit the low level hardware: CLOCK, NVIC.*/
  589. HAL_CRYP_MspDeInit(hcryp);
  590. /* Change the CRYP state */
  591. hcryp->State = HAL_CRYP_STATE_RESET;
  592. /* Release Lock */
  593. __HAL_UNLOCK(hcryp);
  594. /* Return function status */
  595. return HAL_OK;
  596. }
  597. /**
  598. * @brief Initializes the CRYP MSP.
  599. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  600. * the configuration information for CRYP module
  601. * @retval None
  602. */
  603. __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
  604. {
  605. /* NOTE : This function Should not be modified, when the callback is needed,
  606. the HAL_CRYP_MspInit could be implemented in the user file
  607. */
  608. }
  609. /**
  610. * @brief DeInitializes CRYP MSP.
  611. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  612. * the configuration information for CRYP module
  613. * @retval None
  614. */
  615. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  616. {
  617. /* NOTE : This function Should not be modified, when the callback is needed,
  618. the HAL_CRYP_MspDeInit could be implemented in the user file
  619. */
  620. }
  621. /**
  622. * @}
  623. */
  624. /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
  625. * @brief processing functions.
  626. *
  627. @verbatim
  628. ==============================================================================
  629. ##### AES processing functions #####
  630. ==============================================================================
  631. [..] This section provides functions allowing to:
  632. (+) Encrypt plaintext using AES-128/192/256 using chaining modes
  633. (+) Decrypt cyphertext using AES-128/192/256 using chaining modes
  634. [..] Three processing functions are available:
  635. (+) Polling mode
  636. (+) Interrupt mode
  637. (+) DMA mode
  638. @endverbatim
  639. * @{
  640. */
  641. /**
  642. * @brief Initializes the CRYP peripheral in AES ECB encryption mode
  643. * then encrypt pPlainData. The cypher data are available in pCypherData
  644. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  645. * the configuration information for CRYP module
  646. * @param pPlainData: Pointer to the plaintext buffer
  647. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  648. * @param pCypherData: Pointer to the cyphertext buffer
  649. * @param Timeout: Specify Timeout value
  650. * @retval HAL status
  651. */
  652. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  653. {
  654. /* Process Locked */
  655. __HAL_LOCK(hcryp);
  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, hcryp->Init.KeySize);
  663. /* Set the CRYP peripheral in AES ECB mode */
  664. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB);
  665. /* Flush FIFO */
  666. __HAL_CRYP_FIFO_FLUSH(hcryp);
  667. /* Enable CRYP */
  668. __HAL_CRYP_ENABLE(hcryp);
  669. /* Set the phase */
  670. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  671. }
  672. /* Write Plain Data and Get Cypher Data */
  673. if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  674. {
  675. return HAL_TIMEOUT;
  676. }
  677. /* Change the CRYP state */
  678. hcryp->State = HAL_CRYP_STATE_READY;
  679. /* Process Unlocked */
  680. __HAL_UNLOCK(hcryp);
  681. /* Return function status */
  682. return HAL_OK;
  683. }
  684. /**
  685. * @brief Initializes the CRYP peripheral in AES CBC encryption mode
  686. * then encrypt pPlainData. The cypher data are available in pCypherData
  687. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  688. * the configuration information for CRYP module
  689. * @param pPlainData: Pointer to the plaintext buffer
  690. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  691. * @param pCypherData: Pointer to the cyphertext buffer
  692. * @param Timeout: Specify Timeout value
  693. * @retval HAL status
  694. */
  695. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  696. {
  697. /* Process Locked */
  698. __HAL_LOCK(hcryp);
  699. /* Change the CRYP state */
  700. hcryp->State = HAL_CRYP_STATE_BUSY;
  701. /* Check if initialization phase has already been performed */
  702. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  703. {
  704. /* Set the key */
  705. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  706. /* Set the CRYP peripheral in AES ECB mode */
  707. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC);
  708. /* Set the Initialization Vector */
  709. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  710. /* Flush FIFO */
  711. __HAL_CRYP_FIFO_FLUSH(hcryp);
  712. /* Enable CRYP */
  713. __HAL_CRYP_ENABLE(hcryp);
  714. /* Set the phase */
  715. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  716. }
  717. /* Write Plain Data and Get Cypher Data */
  718. if(CRYP_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  719. {
  720. return HAL_TIMEOUT;
  721. }
  722. /* Change the CRYP state */
  723. hcryp->State = HAL_CRYP_STATE_READY;
  724. /* Process Unlocked */
  725. __HAL_UNLOCK(hcryp);
  726. /* Return function status */
  727. return HAL_OK;
  728. }
  729. /**
  730. * @brief Initializes the CRYP peripheral in AES CTR encryption mode
  731. * then encrypt pPlainData. The cypher data are available in pCypherData
  732. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  733. * the configuration information for CRYP module
  734. * @param pPlainData: Pointer to the plaintext buffer
  735. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  736. * @param pCypherData: Pointer to the cyphertext buffer
  737. * @param Timeout: Specify Timeout value
  738. * @retval HAL status
  739. */
  740. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  741. {
  742. /* Process Locked */
  743. __HAL_LOCK(hcryp);
  744. /* Change the CRYP state */
  745. hcryp->State = HAL_CRYP_STATE_BUSY;
  746. /* Check if initialization phase has already been performed */
  747. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  748. {
  749. /* Set the key */
  750. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  751. /* Set the CRYP peripheral in AES ECB mode */
  752. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR);
  753. /* Set the Initialization Vector */
  754. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  755. /* Flush FIFO */
  756. __HAL_CRYP_FIFO_FLUSH(hcryp);
  757. /* Enable CRYP */
  758. __HAL_CRYP_ENABLE(hcryp);
  759. /* Set the phase */
  760. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  761. }
  762. /* Write Plain Data and Get Cypher Data */
  763. if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  764. {
  765. return HAL_TIMEOUT;
  766. }
  767. /* Change the CRYP state */
  768. hcryp->State = HAL_CRYP_STATE_READY;
  769. /* Process Unlocked */
  770. __HAL_UNLOCK(hcryp);
  771. /* Return function status */
  772. return HAL_OK;
  773. }
  774. /**
  775. * @brief Initializes the CRYP peripheral in AES ECB decryption mode
  776. * then decrypted pCypherData. The cypher data are available in pPlainData
  777. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  778. * the configuration information for CRYP module
  779. * @param pCypherData: Pointer to the cyphertext buffer
  780. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  781. * @param pPlainData: Pointer to the plaintext buffer
  782. * @param Timeout: Specify Timeout value
  783. * @retval HAL status
  784. */
  785. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  786. {
  787. uint32_t tickstart = 0;
  788. /* Process Locked */
  789. __HAL_LOCK(hcryp);
  790. /* Change the CRYP state */
  791. hcryp->State = HAL_CRYP_STATE_BUSY;
  792. /* Check if initialization phase has already been performed */
  793. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  794. {
  795. /* Set the key */
  796. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  797. /* Set the CRYP peripheral in AES Key mode */
  798. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  799. /* Enable CRYP */
  800. __HAL_CRYP_ENABLE(hcryp);
  801. /* Get tick */
  802. tickstart = HAL_GetTick();
  803. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  804. {
  805. /* Check for the Timeout */
  806. if(Timeout != HAL_MAX_DELAY)
  807. {
  808. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  809. {
  810. /* Change state */
  811. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  812. /* Process Unlocked */
  813. __HAL_UNLOCK(hcryp);
  814. return HAL_TIMEOUT;
  815. }
  816. }
  817. }
  818. /* Disable CRYP */
  819. __HAL_CRYP_DISABLE(hcryp);
  820. /* Reset the ALGOMODE bits*/
  821. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  822. /* Set the CRYP peripheral in AES ECB decryption mode */
  823. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
  824. /* Flush FIFO */
  825. __HAL_CRYP_FIFO_FLUSH(hcryp);
  826. /* Enable CRYP */
  827. __HAL_CRYP_ENABLE(hcryp);
  828. /* Set the phase */
  829. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  830. }
  831. /* Write Plain Data and Get Cypher Data */
  832. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  833. {
  834. return HAL_TIMEOUT;
  835. }
  836. /* Change the CRYP state */
  837. hcryp->State = HAL_CRYP_STATE_READY;
  838. /* Process Unlocked */
  839. __HAL_UNLOCK(hcryp);
  840. /* Return function status */
  841. return HAL_OK;
  842. }
  843. /**
  844. * @brief Initializes the CRYP peripheral in AES ECB decryption mode
  845. * then decrypted pCypherData. The cypher data are available in pPlainData
  846. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  847. * the configuration information for CRYP module
  848. * @param pCypherData: Pointer to the cyphertext buffer
  849. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  850. * @param pPlainData: Pointer to the plaintext buffer
  851. * @param Timeout: Specify Timeout value
  852. * @retval HAL status
  853. */
  854. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  855. {
  856. uint32_t tickstart = 0;
  857. /* Process Locked */
  858. __HAL_LOCK(hcryp);
  859. /* Change the CRYP state */
  860. hcryp->State = HAL_CRYP_STATE_BUSY;
  861. /* Check if initialization phase has already been performed */
  862. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  863. {
  864. /* Set the key */
  865. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  866. /* Set the CRYP peripheral in AES Key mode */
  867. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  868. /* Enable CRYP */
  869. __HAL_CRYP_ENABLE(hcryp);
  870. /* Get tick */
  871. tickstart = HAL_GetTick();
  872. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  873. {
  874. /* Check for the Timeout */
  875. if(Timeout != HAL_MAX_DELAY)
  876. {
  877. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  878. {
  879. /* Change state */
  880. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  881. /* Process Unlocked */
  882. __HAL_UNLOCK(hcryp);
  883. return HAL_TIMEOUT;
  884. }
  885. }
  886. }
  887. /* Reset the ALGOMODE bits*/
  888. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  889. /* Set the CRYP peripheral in AES CBC decryption mode */
  890. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
  891. /* Set the Initialization Vector */
  892. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  893. /* Flush FIFO */
  894. __HAL_CRYP_FIFO_FLUSH(hcryp);
  895. /* Enable CRYP */
  896. __HAL_CRYP_ENABLE(hcryp);
  897. /* Set the phase */
  898. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  899. }
  900. /* Write Plain Data and Get Cypher Data */
  901. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  902. {
  903. return HAL_TIMEOUT;
  904. }
  905. /* Change the CRYP state */
  906. hcryp->State = HAL_CRYP_STATE_READY;
  907. /* Process Unlocked */
  908. __HAL_UNLOCK(hcryp);
  909. /* Return function status */
  910. return HAL_OK;
  911. }
  912. /**
  913. * @brief Initializes the CRYP peripheral in AES CTR decryption mode
  914. * then decrypted pCypherData. The cypher data are available in pPlainData
  915. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  916. * the configuration information for CRYP module
  917. * @param pCypherData: Pointer to the cyphertext buffer
  918. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  919. * @param pPlainData: Pointer to the plaintext buffer
  920. * @param Timeout: Specify Timeout value
  921. * @retval HAL status
  922. */
  923. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  924. {
  925. /* Process Locked */
  926. __HAL_LOCK(hcryp);
  927. /* Check if initialization phase has already been performed */
  928. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  929. {
  930. /* Change the CRYP state */
  931. hcryp->State = HAL_CRYP_STATE_BUSY;
  932. /* Set the key */
  933. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  934. /* Set the CRYP peripheral in AES CTR mode */
  935. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
  936. /* Set the Initialization Vector */
  937. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  938. /* Flush FIFO */
  939. __HAL_CRYP_FIFO_FLUSH(hcryp);
  940. /* Enable CRYP */
  941. __HAL_CRYP_ENABLE(hcryp);
  942. /* Set the phase */
  943. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  944. }
  945. /* Write Plain Data and Get Cypher Data */
  946. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  947. {
  948. return HAL_TIMEOUT;
  949. }
  950. /* Change the CRYP state */
  951. hcryp->State = HAL_CRYP_STATE_READY;
  952. /* Process Unlocked */
  953. __HAL_UNLOCK(hcryp);
  954. /* Return function status */
  955. return HAL_OK;
  956. }
  957. /**
  958. * @brief Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt.
  959. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  960. * the configuration information for CRYP module
  961. * @param pPlainData: Pointer to the plaintext buffer
  962. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  963. * @param pCypherData: Pointer to the cyphertext buffer
  964. * @retval HAL status
  965. */
  966. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  967. {
  968. uint32_t inputaddr;
  969. uint32_t outputaddr;
  970. if(hcryp->State == HAL_CRYP_STATE_READY)
  971. {
  972. /* Process Locked */
  973. __HAL_LOCK(hcryp);
  974. hcryp->CrypInCount = Size;
  975. hcryp->pCrypInBuffPtr = pPlainData;
  976. hcryp->pCrypOutBuffPtr = pCypherData;
  977. hcryp->CrypOutCount = Size;
  978. /* Change the CRYP state */
  979. hcryp->State = HAL_CRYP_STATE_BUSY;
  980. /* Check if initialization phase has already been performed */
  981. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  982. {
  983. /* Set the key */
  984. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  985. /* Set the CRYP peripheral in AES ECB mode */
  986. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB);
  987. /* Flush FIFO */
  988. __HAL_CRYP_FIFO_FLUSH(hcryp);
  989. /* Set the phase */
  990. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  991. }
  992. /* Enable Interrupts */
  993. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  994. /* Enable CRYP */
  995. __HAL_CRYP_ENABLE(hcryp);
  996. /* Return function status */
  997. return HAL_OK;
  998. }
  999. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1000. {
  1001. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1002. /* Write the Input block in the IN FIFO */
  1003. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1004. inputaddr+=4;
  1005. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1006. inputaddr+=4;
  1007. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1008. inputaddr+=4;
  1009. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1010. hcryp->pCrypInBuffPtr += 16;
  1011. hcryp->CrypInCount -= 16;
  1012. if(hcryp->CrypInCount == 0)
  1013. {
  1014. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1015. /* Call the Input data transfer complete callback */
  1016. HAL_CRYP_InCpltCallback(hcryp);
  1017. }
  1018. }
  1019. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1020. {
  1021. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1022. /* Read the Output block from the Output FIFO */
  1023. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1024. outputaddr+=4;
  1025. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1026. outputaddr+=4;
  1027. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1028. outputaddr+=4;
  1029. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1030. hcryp->pCrypOutBuffPtr += 16;
  1031. hcryp->CrypOutCount -= 16;
  1032. if(hcryp->CrypOutCount == 0)
  1033. {
  1034. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1035. /* Process Locked */
  1036. __HAL_UNLOCK(hcryp);
  1037. /* Change the CRYP state */
  1038. hcryp->State = HAL_CRYP_STATE_READY;
  1039. /* Call Input transfer complete callback */
  1040. HAL_CRYP_OutCpltCallback(hcryp);
  1041. }
  1042. }
  1043. /* Return function status */
  1044. return HAL_OK;
  1045. }
  1046. /**
  1047. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt.
  1048. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1049. * the configuration information for CRYP module
  1050. * @param pPlainData: Pointer to the plaintext buffer
  1051. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1052. * @param pCypherData: Pointer to the cyphertext buffer
  1053. * @retval HAL status
  1054. */
  1055. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1056. {
  1057. uint32_t inputaddr;
  1058. uint32_t outputaddr;
  1059. if(hcryp->State == HAL_CRYP_STATE_READY)
  1060. {
  1061. /* Process Locked */
  1062. __HAL_LOCK(hcryp);
  1063. hcryp->CrypInCount = Size;
  1064. hcryp->pCrypInBuffPtr = pPlainData;
  1065. hcryp->pCrypOutBuffPtr = pCypherData;
  1066. hcryp->CrypOutCount = Size;
  1067. /* Change the CRYP state */
  1068. hcryp->State = HAL_CRYP_STATE_BUSY;
  1069. /* Check if initialization phase has already been performed */
  1070. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1071. {
  1072. /* Set the key */
  1073. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1074. /* Set the CRYP peripheral in AES CBC mode */
  1075. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC);
  1076. /* Set the Initialization Vector */
  1077. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1078. /* Flush FIFO */
  1079. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1080. /* Set the phase */
  1081. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1082. }
  1083. /* Enable Interrupts */
  1084. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1085. /* Enable CRYP */
  1086. __HAL_CRYP_ENABLE(hcryp);
  1087. /* Return function status */
  1088. return HAL_OK;
  1089. }
  1090. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1091. {
  1092. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1093. /* Write the Input block in the IN FIFO */
  1094. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1095. inputaddr+=4;
  1096. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1097. inputaddr+=4;
  1098. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1099. inputaddr+=4;
  1100. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1101. hcryp->pCrypInBuffPtr += 16;
  1102. hcryp->CrypInCount -= 16;
  1103. if(hcryp->CrypInCount == 0)
  1104. {
  1105. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1106. /* Call the Input data transfer complete callback */
  1107. HAL_CRYP_InCpltCallback(hcryp);
  1108. }
  1109. }
  1110. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1111. {
  1112. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1113. /* Read the Output block from the Output FIFO */
  1114. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1115. outputaddr+=4;
  1116. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1117. outputaddr+=4;
  1118. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1119. outputaddr+=4;
  1120. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1121. hcryp->pCrypOutBuffPtr += 16;
  1122. hcryp->CrypOutCount -= 16;
  1123. if(hcryp->CrypOutCount == 0)
  1124. {
  1125. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1126. /* Process Locked */
  1127. __HAL_UNLOCK(hcryp);
  1128. /* Change the CRYP state */
  1129. hcryp->State = HAL_CRYP_STATE_READY;
  1130. /* Call Input transfer complete callback */
  1131. HAL_CRYP_OutCpltCallback(hcryp);
  1132. }
  1133. }
  1134. /* Return function status */
  1135. return HAL_OK;
  1136. }
  1137. /**
  1138. * @brief Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt.
  1139. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1140. * the configuration information for CRYP module
  1141. * @param pPlainData: Pointer to the plaintext buffer
  1142. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1143. * @param pCypherData: Pointer to the cyphertext buffer
  1144. * @retval HAL status
  1145. */
  1146. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1147. {
  1148. uint32_t inputaddr;
  1149. uint32_t outputaddr;
  1150. if(hcryp->State == HAL_CRYP_STATE_READY)
  1151. {
  1152. /* Process Locked */
  1153. __HAL_LOCK(hcryp);
  1154. hcryp->CrypInCount = Size;
  1155. hcryp->pCrypInBuffPtr = pPlainData;
  1156. hcryp->pCrypOutBuffPtr = pCypherData;
  1157. hcryp->CrypOutCount = Size;
  1158. /* Change the CRYP state */
  1159. hcryp->State = HAL_CRYP_STATE_BUSY;
  1160. /* Check if initialization phase has already been performed */
  1161. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1162. {
  1163. /* Set the key */
  1164. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1165. /* Set the CRYP peripheral in AES CTR mode */
  1166. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR);
  1167. /* Set the Initialization Vector */
  1168. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1169. /* Flush FIFO */
  1170. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1171. /* Set the phase */
  1172. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1173. }
  1174. /* Enable Interrupts */
  1175. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1176. /* Enable CRYP */
  1177. __HAL_CRYP_ENABLE(hcryp);
  1178. /* Return function status */
  1179. return HAL_OK;
  1180. }
  1181. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1182. {
  1183. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1184. /* Write the Input block in the IN FIFO */
  1185. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1186. inputaddr+=4;
  1187. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1188. inputaddr+=4;
  1189. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1190. inputaddr+=4;
  1191. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1192. hcryp->pCrypInBuffPtr += 16;
  1193. hcryp->CrypInCount -= 16;
  1194. if(hcryp->CrypInCount == 0)
  1195. {
  1196. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1197. /* Call the Input data transfer complete callback */
  1198. HAL_CRYP_InCpltCallback(hcryp);
  1199. }
  1200. }
  1201. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1202. {
  1203. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1204. /* Read the Output block from the Output FIFO */
  1205. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1206. outputaddr+=4;
  1207. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1208. outputaddr+=4;
  1209. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1210. outputaddr+=4;
  1211. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1212. hcryp->pCrypOutBuffPtr += 16;
  1213. hcryp->CrypOutCount -= 16;
  1214. if(hcryp->CrypOutCount == 0)
  1215. {
  1216. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1217. /* Process Unlocked */
  1218. __HAL_UNLOCK(hcryp);
  1219. /* Change the CRYP state */
  1220. hcryp->State = HAL_CRYP_STATE_READY;
  1221. /* Call Input transfer complete callback */
  1222. HAL_CRYP_OutCpltCallback(hcryp);
  1223. }
  1224. }
  1225. /* Return function status */
  1226. return HAL_OK;
  1227. }
  1228. /**
  1229. * @brief Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt.
  1230. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1231. * the configuration information for CRYP module
  1232. * @param pCypherData: Pointer to the cyphertext buffer
  1233. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  1234. * @param pPlainData: Pointer to the plaintext buffer
  1235. * @retval HAL status
  1236. */
  1237. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1238. {
  1239. uint32_t tickstart = 0;
  1240. uint32_t inputaddr;
  1241. uint32_t outputaddr;
  1242. if(hcryp->State == HAL_CRYP_STATE_READY)
  1243. {
  1244. /* Process Locked */
  1245. __HAL_LOCK(hcryp);
  1246. hcryp->CrypInCount = Size;
  1247. hcryp->pCrypInBuffPtr = pCypherData;
  1248. hcryp->pCrypOutBuffPtr = pPlainData;
  1249. hcryp->CrypOutCount = Size;
  1250. /* Change the CRYP state */
  1251. hcryp->State = HAL_CRYP_STATE_BUSY;
  1252. /* Check if initialization phase has already been performed */
  1253. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1254. {
  1255. /* Set the key */
  1256. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1257. /* Set the CRYP peripheral in AES Key mode */
  1258. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1259. /* Enable CRYP */
  1260. __HAL_CRYP_ENABLE(hcryp);
  1261. /* Get tick */
  1262. tickstart = HAL_GetTick();
  1263. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1264. {
  1265. /* Check for the Timeout */
  1266. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1267. {
  1268. /* Change state */
  1269. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1270. /* Process Unlocked */
  1271. __HAL_UNLOCK(hcryp);
  1272. return HAL_TIMEOUT;
  1273. }
  1274. }
  1275. /* Reset the ALGOMODE bits*/
  1276. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1277. /* Set the CRYP peripheral in AES ECB decryption mode */
  1278. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
  1279. /* Flush FIFO */
  1280. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1281. /* Set the phase */
  1282. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1283. }
  1284. /* Enable Interrupts */
  1285. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1286. /* Enable CRYP */
  1287. __HAL_CRYP_ENABLE(hcryp);
  1288. /* Return function status */
  1289. return HAL_OK;
  1290. }
  1291. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1292. {
  1293. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1294. /* Write the Input block in the IN FIFO */
  1295. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1296. inputaddr+=4;
  1297. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1298. inputaddr+=4;
  1299. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1300. inputaddr+=4;
  1301. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1302. hcryp->pCrypInBuffPtr += 16;
  1303. hcryp->CrypInCount -= 16;
  1304. if(hcryp->CrypInCount == 0)
  1305. {
  1306. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1307. /* Call the Input data transfer complete callback */
  1308. HAL_CRYP_InCpltCallback(hcryp);
  1309. }
  1310. }
  1311. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1312. {
  1313. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1314. /* Read the Output block from the Output FIFO */
  1315. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1316. outputaddr+=4;
  1317. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1318. outputaddr+=4;
  1319. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1320. outputaddr+=4;
  1321. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1322. hcryp->pCrypOutBuffPtr += 16;
  1323. hcryp->CrypOutCount -= 16;
  1324. if(hcryp->CrypOutCount == 0)
  1325. {
  1326. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1327. /* Process Unlocked */
  1328. __HAL_UNLOCK(hcryp);
  1329. /* Change the CRYP state */
  1330. hcryp->State = HAL_CRYP_STATE_READY;
  1331. /* Call Input transfer complete callback */
  1332. HAL_CRYP_OutCpltCallback(hcryp);
  1333. }
  1334. }
  1335. /* Return function status */
  1336. return HAL_OK;
  1337. }
  1338. /**
  1339. * @brief Initializes the CRYP peripheral in AES CBC decryption mode using IT.
  1340. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1341. * the configuration information for CRYP module
  1342. * @param pCypherData: Pointer to the cyphertext buffer
  1343. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  1344. * @param pPlainData: Pointer to the plaintext buffer
  1345. * @retval HAL status
  1346. */
  1347. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1348. {
  1349. uint32_t tickstart = 0;
  1350. uint32_t inputaddr;
  1351. uint32_t outputaddr;
  1352. if(hcryp->State == HAL_CRYP_STATE_READY)
  1353. {
  1354. /* Process Locked */
  1355. __HAL_LOCK(hcryp);
  1356. /* Get the buffer addresses and sizes */
  1357. hcryp->CrypInCount = Size;
  1358. hcryp->pCrypInBuffPtr = pCypherData;
  1359. hcryp->pCrypOutBuffPtr = pPlainData;
  1360. hcryp->CrypOutCount = Size;
  1361. /* Change the CRYP state */
  1362. hcryp->State = HAL_CRYP_STATE_BUSY;
  1363. /* Check if initialization phase has already been performed */
  1364. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1365. {
  1366. /* Set the key */
  1367. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1368. /* Set the CRYP peripheral in AES Key mode */
  1369. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1370. /* Enable CRYP */
  1371. __HAL_CRYP_ENABLE(hcryp);
  1372. /* Get tick */
  1373. tickstart = HAL_GetTick();
  1374. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1375. {
  1376. /* Check for the Timeout */
  1377. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1378. {
  1379. /* Change state */
  1380. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1381. /* Process Unlocked */
  1382. __HAL_UNLOCK(hcryp);
  1383. return HAL_TIMEOUT;
  1384. }
  1385. }
  1386. /* Reset the ALGOMODE bits*/
  1387. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1388. /* Set the CRYP peripheral in AES CBC decryption mode */
  1389. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
  1390. /* Set the Initialization Vector */
  1391. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1392. /* Flush FIFO */
  1393. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1394. /* Enable CRYP */
  1395. __HAL_CRYP_ENABLE(hcryp);
  1396. /* Set the phase */
  1397. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1398. }
  1399. /* Enable Interrupts */
  1400. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1401. /* Enable CRYP */
  1402. __HAL_CRYP_ENABLE(hcryp);
  1403. /* Return function status */
  1404. return HAL_OK;
  1405. }
  1406. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1407. {
  1408. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1409. /* Write the Input block in the IN FIFO */
  1410. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1411. inputaddr+=4;
  1412. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1413. inputaddr+=4;
  1414. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1415. inputaddr+=4;
  1416. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1417. hcryp->pCrypInBuffPtr += 16;
  1418. hcryp->CrypInCount -= 16;
  1419. if(hcryp->CrypInCount == 0)
  1420. {
  1421. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1422. /* Call the Input data transfer complete callback */
  1423. HAL_CRYP_InCpltCallback(hcryp);
  1424. }
  1425. }
  1426. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1427. {
  1428. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1429. /* Read the Output block from the Output FIFO */
  1430. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1431. outputaddr+=4;
  1432. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1433. outputaddr+=4;
  1434. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1435. outputaddr+=4;
  1436. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1437. hcryp->pCrypOutBuffPtr += 16;
  1438. hcryp->CrypOutCount -= 16;
  1439. if(hcryp->CrypOutCount == 0)
  1440. {
  1441. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1442. /* Process Unlocked */
  1443. __HAL_UNLOCK(hcryp);
  1444. /* Change the CRYP state */
  1445. hcryp->State = HAL_CRYP_STATE_READY;
  1446. /* Call Input transfer complete callback */
  1447. HAL_CRYP_OutCpltCallback(hcryp);
  1448. }
  1449. }
  1450. /* Return function status */
  1451. return HAL_OK;
  1452. }
  1453. /**
  1454. * @brief Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt.
  1455. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1456. * the configuration information for CRYP module
  1457. * @param pCypherData: Pointer to the cyphertext buffer
  1458. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  1459. * @param pPlainData: Pointer to the plaintext buffer
  1460. * @retval HAL status
  1461. */
  1462. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1463. {
  1464. uint32_t inputaddr;
  1465. uint32_t outputaddr;
  1466. if(hcryp->State == HAL_CRYP_STATE_READY)
  1467. {
  1468. /* Process Locked */
  1469. __HAL_LOCK(hcryp);
  1470. /* Get the buffer addresses and sizes */
  1471. hcryp->CrypInCount = Size;
  1472. hcryp->pCrypInBuffPtr = pCypherData;
  1473. hcryp->pCrypOutBuffPtr = pPlainData;
  1474. hcryp->CrypOutCount = Size;
  1475. /* Change the CRYP state */
  1476. hcryp->State = HAL_CRYP_STATE_BUSY;
  1477. /* Check if initialization phase has already been performed */
  1478. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1479. {
  1480. /* Set the key */
  1481. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1482. /* Set the CRYP peripheral in AES CTR mode */
  1483. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
  1484. /* Set the Initialization Vector */
  1485. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1486. /* Flush FIFO */
  1487. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1488. /* Set the phase */
  1489. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1490. }
  1491. /* Enable Interrupts */
  1492. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1493. /* Enable CRYP */
  1494. __HAL_CRYP_ENABLE(hcryp);
  1495. /* Return function status */
  1496. return HAL_OK;
  1497. }
  1498. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1499. {
  1500. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1501. /* Write the Input block in the IN FIFO */
  1502. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1503. inputaddr+=4;
  1504. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1505. inputaddr+=4;
  1506. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1507. inputaddr+=4;
  1508. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1509. hcryp->pCrypInBuffPtr += 16;
  1510. hcryp->CrypInCount -= 16;
  1511. if(hcryp->CrypInCount == 0)
  1512. {
  1513. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1514. /* Call the Input data transfer complete callback */
  1515. HAL_CRYP_InCpltCallback(hcryp);
  1516. }
  1517. }
  1518. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1519. {
  1520. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1521. /* Read the Output block from the Output FIFO */
  1522. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1523. outputaddr+=4;
  1524. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1525. outputaddr+=4;
  1526. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1527. outputaddr+=4;
  1528. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1529. hcryp->pCrypOutBuffPtr += 16;
  1530. hcryp->CrypOutCount -= 16;
  1531. if(hcryp->CrypOutCount == 0)
  1532. {
  1533. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1534. /* Process Unlocked */
  1535. __HAL_UNLOCK(hcryp);
  1536. /* Change the CRYP state */
  1537. hcryp->State = HAL_CRYP_STATE_READY;
  1538. /* Call Input transfer complete callback */
  1539. HAL_CRYP_OutCpltCallback(hcryp);
  1540. }
  1541. }
  1542. /* Return function status */
  1543. return HAL_OK;
  1544. }
  1545. /**
  1546. * @brief Initializes the CRYP peripheral in AES ECB encryption mode using DMA.
  1547. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1548. * the configuration information for CRYP module
  1549. * @param pPlainData: Pointer to the plaintext buffer
  1550. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1551. * @param pCypherData: Pointer to the cyphertext buffer
  1552. * @retval HAL status
  1553. */
  1554. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1555. {
  1556. uint32_t inputaddr;
  1557. uint32_t outputaddr;
  1558. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1559. {
  1560. /* Process Locked */
  1561. __HAL_LOCK(hcryp);
  1562. inputaddr = (uint32_t)pPlainData;
  1563. outputaddr = (uint32_t)pCypherData;
  1564. /* Change the CRYP state */
  1565. hcryp->State = HAL_CRYP_STATE_BUSY;
  1566. /* Check if initialization phase has already been performed */
  1567. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1568. {
  1569. /* Set the key */
  1570. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1571. /* Set the CRYP peripheral in AES ECB mode */
  1572. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB);
  1573. /* Flush FIFO */
  1574. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1575. /* Set the phase */
  1576. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1577. }
  1578. /* Set the input and output addresses and start DMA transfer */
  1579. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1580. /* Process Unlocked */
  1581. __HAL_UNLOCK(hcryp);
  1582. /* Return function status */
  1583. return HAL_OK;
  1584. }
  1585. else
  1586. {
  1587. return HAL_ERROR;
  1588. }
  1589. }
  1590. /**
  1591. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
  1592. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1593. * the configuration information for CRYP module
  1594. * @param pPlainData: Pointer to the plaintext buffer
  1595. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  1596. * @param pCypherData: Pointer to the cyphertext buffer
  1597. * @retval HAL status
  1598. */
  1599. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1600. {
  1601. uint32_t inputaddr;
  1602. uint32_t outputaddr;
  1603. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1604. {
  1605. /* Process Locked */
  1606. __HAL_LOCK(hcryp);
  1607. inputaddr = (uint32_t)pPlainData;
  1608. outputaddr = (uint32_t)pCypherData;
  1609. /* Change the CRYP state */
  1610. hcryp->State = HAL_CRYP_STATE_BUSY;
  1611. /* Check if initialization phase has already been performed */
  1612. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1613. {
  1614. /* Set the key */
  1615. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1616. /* Set the CRYP peripheral in AES ECB mode */
  1617. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC);
  1618. /* Set the Initialization Vector */
  1619. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1620. /* Flush FIFO */
  1621. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1622. /* Set the phase */
  1623. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1624. }
  1625. /* Set the input and output addresses and start DMA transfer */
  1626. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1627. /* Process Unlocked */
  1628. __HAL_UNLOCK(hcryp);
  1629. /* Return function status */
  1630. return HAL_OK;
  1631. }
  1632. else
  1633. {
  1634. return HAL_ERROR;
  1635. }
  1636. }
  1637. /**
  1638. * @brief Initializes the CRYP peripheral in AES CTR encryption mode using DMA.
  1639. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1640. * the configuration information for CRYP module
  1641. * @param pPlainData: Pointer to the plaintext buffer
  1642. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  1643. * @param pCypherData: Pointer to the cyphertext buffer
  1644. * @retval HAL status
  1645. */
  1646. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1647. {
  1648. uint32_t inputaddr;
  1649. uint32_t outputaddr;
  1650. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1651. {
  1652. /* Process Locked */
  1653. __HAL_LOCK(hcryp);
  1654. inputaddr = (uint32_t)pPlainData;
  1655. outputaddr = (uint32_t)pCypherData;
  1656. /* Change the CRYP state */
  1657. hcryp->State = HAL_CRYP_STATE_BUSY;
  1658. /* Check if initialization phase has already been performed */
  1659. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1660. {
  1661. /* Set the key */
  1662. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1663. /* Set the CRYP peripheral in AES ECB mode */
  1664. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR);
  1665. /* Set the Initialization Vector */
  1666. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1667. /* Flush FIFO */
  1668. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1669. /* Set the phase */
  1670. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1671. }
  1672. /* Set the input and output addresses and start DMA transfer */
  1673. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1674. /* Process Unlocked */
  1675. __HAL_UNLOCK(hcryp);
  1676. /* Return function status */
  1677. return HAL_OK;
  1678. }
  1679. else
  1680. {
  1681. return HAL_ERROR;
  1682. }
  1683. }
  1684. /**
  1685. * @brief Initializes the CRYP peripheral in AES ECB decryption mode using DMA.
  1686. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1687. * the configuration information for CRYP module
  1688. * @param pCypherData: Pointer to the cyphertext buffer
  1689. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1690. * @param pPlainData: Pointer to the plaintext buffer
  1691. * @retval HAL status
  1692. */
  1693. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1694. {
  1695. uint32_t tickstart = 0;
  1696. uint32_t inputaddr;
  1697. uint32_t outputaddr;
  1698. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1699. {
  1700. /* Process Locked */
  1701. __HAL_LOCK(hcryp);
  1702. inputaddr = (uint32_t)pCypherData;
  1703. outputaddr = (uint32_t)pPlainData;
  1704. /* Change the CRYP state */
  1705. hcryp->State = HAL_CRYP_STATE_BUSY;
  1706. /* Check if initialization phase has already been performed */
  1707. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1708. {
  1709. /* Set the key */
  1710. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1711. /* Set the CRYP peripheral in AES Key mode */
  1712. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1713. /* Enable CRYP */
  1714. __HAL_CRYP_ENABLE(hcryp);
  1715. /* Get tick */
  1716. tickstart = HAL_GetTick();
  1717. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1718. {
  1719. /* Check for the Timeout */
  1720. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1721. {
  1722. /* Change state */
  1723. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1724. /* Process Unlocked */
  1725. __HAL_UNLOCK(hcryp);
  1726. return HAL_TIMEOUT;
  1727. }
  1728. }
  1729. /* Reset the ALGOMODE bits*/
  1730. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1731. /* Set the CRYP peripheral in AES ECB decryption mode */
  1732. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
  1733. /* Flush FIFO */
  1734. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1735. /* Set the phase */
  1736. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1737. }
  1738. /* Set the input and output addresses and start DMA transfer */
  1739. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1740. /* Process Unlocked */
  1741. __HAL_UNLOCK(hcryp);
  1742. /* Return function status */
  1743. return HAL_OK;
  1744. }
  1745. else
  1746. {
  1747. return HAL_ERROR;
  1748. }
  1749. }
  1750. /**
  1751. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
  1752. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1753. * the configuration information for CRYP module
  1754. * @param pCypherData: Pointer to the cyphertext buffer
  1755. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1756. * @param pPlainData: Pointer to the plaintext buffer
  1757. * @retval HAL status
  1758. */
  1759. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1760. {
  1761. uint32_t tickstart = 0;
  1762. uint32_t inputaddr;
  1763. uint32_t outputaddr;
  1764. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1765. {
  1766. /* Process Locked */
  1767. __HAL_LOCK(hcryp);
  1768. inputaddr = (uint32_t)pCypherData;
  1769. outputaddr = (uint32_t)pPlainData;
  1770. /* Change the CRYP state */
  1771. hcryp->State = HAL_CRYP_STATE_BUSY;
  1772. /* Check if initialization phase has already been performed */
  1773. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1774. {
  1775. /* Set the key */
  1776. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1777. /* Set the CRYP peripheral in AES Key mode */
  1778. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1779. /* Enable CRYP */
  1780. __HAL_CRYP_ENABLE(hcryp);
  1781. /* Get tick */
  1782. tickstart = HAL_GetTick();
  1783. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1784. {
  1785. /* Check for the Timeout */
  1786. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1787. {
  1788. /* Change state */
  1789. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1790. /* Process Unlocked */
  1791. __HAL_UNLOCK(hcryp);
  1792. return HAL_TIMEOUT;
  1793. }
  1794. }
  1795. /* Reset the ALGOMODE bits*/
  1796. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1797. /* Set the CRYP peripheral in AES CBC decryption mode */
  1798. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
  1799. /* Set the Initialization Vector */
  1800. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1801. /* Flush FIFO */
  1802. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1803. /* Set the phase */
  1804. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1805. }
  1806. /* Set the input and output addresses and start DMA transfer */
  1807. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1808. /* Process Unlocked */
  1809. __HAL_UNLOCK(hcryp);
  1810. /* Return function status */
  1811. return HAL_OK;
  1812. }
  1813. else
  1814. {
  1815. return HAL_ERROR;
  1816. }
  1817. }
  1818. /**
  1819. * @brief Initializes the CRYP peripheral in AES CTR decryption mode using DMA.
  1820. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1821. * the configuration information for CRYP module
  1822. * @param pCypherData: Pointer to the cyphertext buffer
  1823. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  1824. * @param pPlainData: Pointer to the plaintext buffer
  1825. * @retval HAL status
  1826. */
  1827. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1828. {
  1829. uint32_t inputaddr;
  1830. uint32_t outputaddr;
  1831. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1832. {
  1833. /* Process Locked */
  1834. __HAL_LOCK(hcryp);
  1835. inputaddr = (uint32_t)pCypherData;
  1836. outputaddr = (uint32_t)pPlainData;
  1837. /* Change the CRYP state */
  1838. hcryp->State = HAL_CRYP_STATE_BUSY;
  1839. /* Check if initialization phase has already been performed */
  1840. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1841. {
  1842. /* Set the key */
  1843. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1844. /* Set the CRYP peripheral in AES CTR mode */
  1845. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
  1846. /* Set the Initialization Vector */
  1847. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1848. /* Flush FIFO */
  1849. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1850. /* Set the phase */
  1851. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1852. }
  1853. /* Set the input and output addresses and start DMA transfer */
  1854. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1855. /* Process Unlocked */
  1856. __HAL_UNLOCK(hcryp);
  1857. /* Return function status */
  1858. return HAL_OK;
  1859. }
  1860. else
  1861. {
  1862. return HAL_ERROR;
  1863. }
  1864. }
  1865. /**
  1866. * @}
  1867. */
  1868. /** @defgroup CRYP_Exported_Functions_Group3 DES processing functions
  1869. * @brief processing functions.
  1870. *
  1871. @verbatim
  1872. ==============================================================================
  1873. ##### DES processing functions #####
  1874. ==============================================================================
  1875. [..] This section provides functions allowing to:
  1876. (+) Encrypt plaintext using DES using ECB or CBC chaining modes
  1877. (+) Decrypt cyphertext using ECB or CBC chaining modes
  1878. [..] Three processing functions are available:
  1879. (+) Polling mode
  1880. (+) Interrupt mode
  1881. (+) DMA mode
  1882. @endverbatim
  1883. * @{
  1884. */
  1885. /**
  1886. * @brief Initializes the CRYP peripheral in DES ECB encryption mode.
  1887. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1888. * the configuration information for CRYP module
  1889. * @param pPlainData: Pointer to the plaintext buffer
  1890. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  1891. * @param pCypherData: Pointer to the cyphertext buffer
  1892. * @param Timeout: Specify Timeout value
  1893. * @retval HAL status
  1894. */
  1895. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  1896. {
  1897. /* Process Locked */
  1898. __HAL_LOCK(hcryp);
  1899. /* Change the CRYP state */
  1900. hcryp->State = HAL_CRYP_STATE_BUSY;
  1901. /* Set CRYP peripheral in DES ECB encryption mode */
  1902. CRYP_SetDESECBMode(hcryp, 0);
  1903. /* Enable CRYP */
  1904. __HAL_CRYP_ENABLE(hcryp);
  1905. /* Write Plain Data and Get Cypher Data */
  1906. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  1907. {
  1908. return HAL_TIMEOUT;
  1909. }
  1910. /* Change the CRYP state */
  1911. hcryp->State = HAL_CRYP_STATE_READY;
  1912. /* Process Unlocked */
  1913. __HAL_UNLOCK(hcryp);
  1914. /* Return function status */
  1915. return HAL_OK;
  1916. }
  1917. /**
  1918. * @brief Initializes the CRYP peripheral in DES ECB decryption mode.
  1919. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1920. * the configuration information for CRYP module
  1921. * @param pPlainData: Pointer to the plaintext buffer
  1922. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  1923. * @param pCypherData: Pointer to the cyphertext buffer
  1924. * @param Timeout: Specify Timeout value
  1925. * @retval HAL status
  1926. */
  1927. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  1928. {
  1929. /* Process Locked */
  1930. __HAL_LOCK(hcryp);
  1931. /* Change the CRYP state */
  1932. hcryp->State = HAL_CRYP_STATE_BUSY;
  1933. /* Set CRYP peripheral in DES ECB decryption mode */
  1934. CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
  1935. /* Enable CRYP */
  1936. __HAL_CRYP_ENABLE(hcryp);
  1937. /* Write Plain Data and Get Cypher Data */
  1938. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  1939. {
  1940. return HAL_TIMEOUT;
  1941. }
  1942. /* Change the CRYP state */
  1943. hcryp->State = HAL_CRYP_STATE_READY;
  1944. /* Process Unlocked */
  1945. __HAL_UNLOCK(hcryp);
  1946. /* Return function status */
  1947. return HAL_OK;
  1948. }
  1949. /**
  1950. * @brief Initializes the CRYP peripheral in DES CBC encryption mode.
  1951. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1952. * the configuration information for CRYP module
  1953. * @param pPlainData: Pointer to the plaintext buffer
  1954. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  1955. * @param pCypherData: Pointer to the cyphertext buffer
  1956. * @param Timeout: Specify Timeout value
  1957. * @retval HAL status
  1958. */
  1959. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  1960. {
  1961. /* Process Locked */
  1962. __HAL_LOCK(hcryp);
  1963. /* Change the CRYP state */
  1964. hcryp->State = HAL_CRYP_STATE_BUSY;
  1965. /* Set CRYP peripheral in DES CBC encryption mode */
  1966. CRYP_SetDESCBCMode(hcryp, 0);
  1967. /* Enable CRYP */
  1968. __HAL_CRYP_ENABLE(hcryp);
  1969. /* Write Plain Data and Get Cypher Data */
  1970. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  1971. {
  1972. return HAL_TIMEOUT;
  1973. }
  1974. /* Change the CRYP state */
  1975. hcryp->State = HAL_CRYP_STATE_READY;
  1976. /* Process Unlocked */
  1977. __HAL_UNLOCK(hcryp);
  1978. /* Return function status */
  1979. return HAL_OK;
  1980. }
  1981. /**
  1982. * @brief Initializes the CRYP peripheral in DES ECB decryption mode.
  1983. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1984. * the configuration information for CRYP module
  1985. * @param pPlainData: Pointer to the plaintext buffer
  1986. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  1987. * @param pCypherData: Pointer to the cyphertext buffer
  1988. * @param Timeout: Specify Timeout value
  1989. * @retval HAL status
  1990. */
  1991. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  1992. {
  1993. /* Process Locked */
  1994. __HAL_LOCK(hcryp);
  1995. /* Change the CRYP state */
  1996. hcryp->State = HAL_CRYP_STATE_BUSY;
  1997. /* Set CRYP peripheral in DES CBC decryption mode */
  1998. CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  1999. /* Enable CRYP */
  2000. __HAL_CRYP_ENABLE(hcryp);
  2001. /* Write Plain Data and Get Cypher Data */
  2002. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  2003. {
  2004. return HAL_TIMEOUT;
  2005. }
  2006. /* Change the CRYP state */
  2007. hcryp->State = HAL_CRYP_STATE_READY;
  2008. /* Process Unlocked */
  2009. __HAL_UNLOCK(hcryp);
  2010. /* Return function status */
  2011. return HAL_OK;
  2012. }
  2013. /**
  2014. * @brief Initializes the CRYP peripheral in DES ECB encryption mode using IT.
  2015. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2016. * the configuration information for CRYP module
  2017. * @param pPlainData: Pointer to the plaintext buffer
  2018. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2019. * @param pCypherData: Pointer to the cyphertext buffer
  2020. * @retval HAL status
  2021. */
  2022. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2023. {
  2024. uint32_t inputaddr;
  2025. uint32_t outputaddr;
  2026. if(hcryp->State == HAL_CRYP_STATE_READY)
  2027. {
  2028. /* Process Locked */
  2029. __HAL_LOCK(hcryp);
  2030. hcryp->CrypInCount = Size;
  2031. hcryp->pCrypInBuffPtr = pPlainData;
  2032. hcryp->pCrypOutBuffPtr = pCypherData;
  2033. hcryp->CrypOutCount = Size;
  2034. /* Change the CRYP state */
  2035. hcryp->State = HAL_CRYP_STATE_BUSY;
  2036. /* Set CRYP peripheral in DES ECB encryption mode */
  2037. CRYP_SetDESECBMode(hcryp, 0);
  2038. /* Enable Interrupts */
  2039. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2040. /* Enable CRYP */
  2041. __HAL_CRYP_ENABLE(hcryp);
  2042. /* Return function status */
  2043. return HAL_OK;
  2044. }
  2045. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2046. {
  2047. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2048. /* Write the Input block in the IN FIFO */
  2049. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2050. inputaddr+=4;
  2051. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2052. hcryp->pCrypInBuffPtr += 8;
  2053. hcryp->CrypInCount -= 8;
  2054. if(hcryp->CrypInCount == 0)
  2055. {
  2056. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2057. /* Call the Input data transfer complete callback */
  2058. HAL_CRYP_InCpltCallback(hcryp);
  2059. }
  2060. }
  2061. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2062. {
  2063. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2064. /* Read the Output block from the Output FIFO */
  2065. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2066. outputaddr+=4;
  2067. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2068. hcryp->pCrypOutBuffPtr += 8;
  2069. hcryp->CrypOutCount -= 8;
  2070. if(hcryp->CrypOutCount == 0)
  2071. {
  2072. /* Disable IT */
  2073. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2074. /* Disable CRYP */
  2075. __HAL_CRYP_DISABLE(hcryp);
  2076. /* Process Unlocked */
  2077. __HAL_UNLOCK(hcryp);
  2078. /* Change the CRYP state */
  2079. hcryp->State = HAL_CRYP_STATE_READY;
  2080. /* Call Input transfer complete callback */
  2081. HAL_CRYP_OutCpltCallback(hcryp);
  2082. }
  2083. }
  2084. /* Return function status */
  2085. return HAL_OK;
  2086. }
  2087. /**
  2088. * @brief Initializes the CRYP peripheral in DES CBC encryption mode using interrupt.
  2089. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2090. * the configuration information for CRYP module
  2091. * @param pPlainData: Pointer to the plaintext buffer
  2092. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2093. * @param pCypherData: Pointer to the cyphertext buffer
  2094. * @retval HAL status
  2095. */
  2096. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2097. {
  2098. uint32_t inputaddr;
  2099. uint32_t outputaddr;
  2100. if(hcryp->State == HAL_CRYP_STATE_READY)
  2101. {
  2102. /* Process Locked */
  2103. __HAL_LOCK(hcryp);
  2104. hcryp->CrypInCount = Size;
  2105. hcryp->pCrypInBuffPtr = pPlainData;
  2106. hcryp->pCrypOutBuffPtr = pCypherData;
  2107. hcryp->CrypOutCount = Size;
  2108. /* Change the CRYP state */
  2109. hcryp->State = HAL_CRYP_STATE_BUSY;
  2110. /* Set CRYP peripheral in DES CBC encryption mode */
  2111. CRYP_SetDESCBCMode(hcryp, 0);
  2112. /* Enable Interrupts */
  2113. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2114. /* Enable CRYP */
  2115. __HAL_CRYP_ENABLE(hcryp);
  2116. /* Return function status */
  2117. return HAL_OK;
  2118. }
  2119. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2120. {
  2121. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2122. /* Write the Input block in the IN FIFO */
  2123. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2124. inputaddr+=4;
  2125. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2126. hcryp->pCrypInBuffPtr += 8;
  2127. hcryp->CrypInCount -= 8;
  2128. if(hcryp->CrypInCount == 0)
  2129. {
  2130. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2131. /* Call the Input data transfer complete callback */
  2132. HAL_CRYP_InCpltCallback(hcryp);
  2133. }
  2134. }
  2135. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2136. {
  2137. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2138. /* Read the Output block from the Output FIFO */
  2139. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2140. outputaddr+=4;
  2141. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2142. hcryp->pCrypOutBuffPtr += 8;
  2143. hcryp->CrypOutCount -= 8;
  2144. if(hcryp->CrypOutCount == 0)
  2145. {
  2146. /* Disable IT */
  2147. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2148. /* Disable CRYP */
  2149. __HAL_CRYP_DISABLE(hcryp);
  2150. /* Process Unlocked */
  2151. __HAL_UNLOCK(hcryp);
  2152. /* Change the CRYP state */
  2153. hcryp->State = HAL_CRYP_STATE_READY;
  2154. /* Call Input transfer complete callback */
  2155. HAL_CRYP_OutCpltCallback(hcryp);
  2156. }
  2157. }
  2158. /* Return function status */
  2159. return HAL_OK;
  2160. }
  2161. /**
  2162. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using IT.
  2163. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2164. * the configuration information for CRYP module
  2165. * @param pPlainData: Pointer to the plaintext buffer
  2166. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2167. * @param pCypherData: Pointer to the cyphertext buffer
  2168. * @retval HAL status
  2169. */
  2170. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2171. {
  2172. uint32_t inputaddr;
  2173. uint32_t outputaddr;
  2174. if(hcryp->State == HAL_CRYP_STATE_READY)
  2175. {
  2176. /* Process Locked */
  2177. __HAL_LOCK(hcryp);
  2178. hcryp->CrypInCount = Size;
  2179. hcryp->pCrypInBuffPtr = pCypherData;
  2180. hcryp->pCrypOutBuffPtr = pPlainData;
  2181. hcryp->CrypOutCount = Size;
  2182. /* Change the CRYP state */
  2183. hcryp->State = HAL_CRYP_STATE_BUSY;
  2184. /* Set CRYP peripheral in DES ECB decryption mode */
  2185. CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2186. /* Enable Interrupts */
  2187. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2188. /* Enable CRYP */
  2189. __HAL_CRYP_ENABLE(hcryp);
  2190. /* Return function status */
  2191. return HAL_OK;
  2192. }
  2193. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2194. {
  2195. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2196. /* Write the Input block in the IN FIFO */
  2197. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2198. inputaddr+=4;
  2199. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2200. hcryp->pCrypInBuffPtr += 8;
  2201. hcryp->CrypInCount -= 8;
  2202. if(hcryp->CrypInCount == 0)
  2203. {
  2204. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2205. /* Call the Input data transfer complete callback */
  2206. HAL_CRYP_InCpltCallback(hcryp);
  2207. }
  2208. }
  2209. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2210. {
  2211. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2212. /* Read the Output block from the Output FIFO */
  2213. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2214. outputaddr+=4;
  2215. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2216. hcryp->pCrypOutBuffPtr += 8;
  2217. hcryp->CrypOutCount -= 8;
  2218. if(hcryp->CrypOutCount == 0)
  2219. {
  2220. /* Disable IT */
  2221. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2222. /* Disable CRYP */
  2223. __HAL_CRYP_DISABLE(hcryp);
  2224. /* Process Unlocked */
  2225. __HAL_UNLOCK(hcryp);
  2226. /* Change the CRYP state */
  2227. hcryp->State = HAL_CRYP_STATE_READY;
  2228. /* Call Input transfer complete callback */
  2229. HAL_CRYP_OutCpltCallback(hcryp);
  2230. }
  2231. }
  2232. /* Return function status */
  2233. return HAL_OK;
  2234. }
  2235. /**
  2236. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using interrupt.
  2237. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2238. * the configuration information for CRYP module
  2239. * @param pPlainData: Pointer to the plaintext buffer
  2240. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2241. * @param pCypherData: Pointer to the cyphertext buffer
  2242. * @retval HAL status
  2243. */
  2244. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2245. {
  2246. uint32_t inputaddr;
  2247. uint32_t outputaddr;
  2248. if(hcryp->State == HAL_CRYP_STATE_READY)
  2249. {
  2250. /* Process Locked */
  2251. __HAL_LOCK(hcryp);
  2252. hcryp->CrypInCount = Size;
  2253. hcryp->pCrypInBuffPtr = pCypherData;
  2254. hcryp->pCrypOutBuffPtr = pPlainData;
  2255. hcryp->CrypOutCount = Size;
  2256. /* Change the CRYP state */
  2257. hcryp->State = HAL_CRYP_STATE_BUSY;
  2258. /* Set CRYP peripheral in DES CBC decryption mode */
  2259. CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2260. /* Enable Interrupts */
  2261. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2262. /* Enable CRYP */
  2263. __HAL_CRYP_ENABLE(hcryp);
  2264. /* Return function status */
  2265. return HAL_OK;
  2266. }
  2267. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2268. {
  2269. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2270. /* Write the Input block in the IN FIFO */
  2271. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2272. inputaddr+=4;
  2273. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2274. hcryp->pCrypInBuffPtr += 8;
  2275. hcryp->CrypInCount -= 8;
  2276. if(hcryp->CrypInCount == 0)
  2277. {
  2278. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2279. /* Call the Input data transfer complete callback */
  2280. HAL_CRYP_InCpltCallback(hcryp);
  2281. }
  2282. }
  2283. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2284. {
  2285. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2286. /* Read the Output block from the Output FIFO */
  2287. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2288. outputaddr+=4;
  2289. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2290. hcryp->pCrypOutBuffPtr += 8;
  2291. hcryp->CrypOutCount -= 8;
  2292. if(hcryp->CrypOutCount == 0)
  2293. {
  2294. /* Disable IT */
  2295. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2296. /* Disable CRYP */
  2297. __HAL_CRYP_DISABLE(hcryp);
  2298. /* Process Unlocked */
  2299. __HAL_UNLOCK(hcryp);
  2300. /* Change the CRYP state */
  2301. hcryp->State = HAL_CRYP_STATE_READY;
  2302. /* Call Input transfer complete callback */
  2303. HAL_CRYP_OutCpltCallback(hcryp);
  2304. }
  2305. }
  2306. /* Return function status */
  2307. return HAL_OK;
  2308. }
  2309. /**
  2310. * @brief Initializes the CRYP peripheral in DES ECB encryption mode using DMA.
  2311. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2312. * the configuration information for CRYP module
  2313. * @param pPlainData: Pointer to the plaintext buffer
  2314. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2315. * @param pCypherData: Pointer to the cyphertext buffer
  2316. * @retval HAL status
  2317. */
  2318. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2319. {
  2320. uint32_t inputaddr;
  2321. uint32_t outputaddr;
  2322. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2323. {
  2324. /* Process Locked */
  2325. __HAL_LOCK(hcryp);
  2326. inputaddr = (uint32_t)pPlainData;
  2327. outputaddr = (uint32_t)pCypherData;
  2328. /* Change the CRYP state */
  2329. hcryp->State = HAL_CRYP_STATE_BUSY;
  2330. /* Set CRYP peripheral in DES ECB encryption mode */
  2331. CRYP_SetDESECBMode(hcryp, 0);
  2332. /* Set the input and output addresses and start DMA transfer */
  2333. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2334. /* Process Unlocked */
  2335. __HAL_UNLOCK(hcryp);
  2336. /* Return function status */
  2337. return HAL_OK;
  2338. }
  2339. else
  2340. {
  2341. return HAL_ERROR;
  2342. }
  2343. }
  2344. /**
  2345. * @brief Initializes the CRYP peripheral in DES CBC encryption mode using DMA.
  2346. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2347. * the configuration information for CRYP module
  2348. * @param pPlainData: Pointer to the plaintext buffer
  2349. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2350. * @param pCypherData: Pointer to the cyphertext buffer
  2351. * @retval HAL status
  2352. */
  2353. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2354. {
  2355. uint32_t inputaddr;
  2356. uint32_t outputaddr;
  2357. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2358. {
  2359. /* Process Locked */
  2360. __HAL_LOCK(hcryp);
  2361. inputaddr = (uint32_t)pPlainData;
  2362. outputaddr = (uint32_t)pCypherData;
  2363. /* Change the CRYP state */
  2364. hcryp->State = HAL_CRYP_STATE_BUSY;
  2365. /* Set CRYP peripheral in DES CBC encryption mode */
  2366. CRYP_SetDESCBCMode(hcryp, 0);
  2367. /* Set the input and output addresses and start DMA transfer */
  2368. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2369. /* Process Unlocked */
  2370. __HAL_UNLOCK(hcryp);
  2371. /* Return function status */
  2372. return HAL_OK;
  2373. }
  2374. else
  2375. {
  2376. return HAL_ERROR;
  2377. }
  2378. }
  2379. /**
  2380. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA.
  2381. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2382. * the configuration information for CRYP module
  2383. * @param pPlainData: Pointer to the plaintext buffer
  2384. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2385. * @param pCypherData: Pointer to the cyphertext buffer
  2386. * @retval HAL status
  2387. */
  2388. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2389. {
  2390. uint32_t inputaddr;
  2391. uint32_t outputaddr;
  2392. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2393. {
  2394. /* Process Locked */
  2395. __HAL_LOCK(hcryp);
  2396. inputaddr = (uint32_t)pCypherData;
  2397. outputaddr = (uint32_t)pPlainData;
  2398. /* Change the CRYP state */
  2399. hcryp->State = HAL_CRYP_STATE_BUSY;
  2400. /* Set CRYP peripheral in DES ECB decryption mode */
  2401. CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2402. /* Set the input and output addresses and start DMA transfer */
  2403. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2404. /* Process Unlocked */
  2405. __HAL_UNLOCK(hcryp);
  2406. /* Return function status */
  2407. return HAL_OK;
  2408. }
  2409. else
  2410. {
  2411. return HAL_ERROR;
  2412. }
  2413. }
  2414. /**
  2415. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA.
  2416. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2417. * the configuration information for CRYP module
  2418. * @param pPlainData: Pointer to the plaintext buffer
  2419. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2420. * @param pCypherData: Pointer to the cyphertext buffer
  2421. * @retval HAL status
  2422. */
  2423. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2424. {
  2425. uint32_t inputaddr;
  2426. uint32_t outputaddr;
  2427. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2428. {
  2429. /* Process Locked */
  2430. __HAL_LOCK(hcryp);
  2431. inputaddr = (uint32_t)pCypherData;
  2432. outputaddr = (uint32_t)pPlainData;
  2433. /* Change the CRYP state */
  2434. hcryp->State = HAL_CRYP_STATE_BUSY;
  2435. /* Set CRYP peripheral in DES CBC decryption mode */
  2436. CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2437. /* Set the input and output addresses and start DMA transfer */
  2438. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2439. /* Process Unlocked */
  2440. __HAL_UNLOCK(hcryp);
  2441. /* Return function status */
  2442. return HAL_OK;
  2443. }
  2444. else
  2445. {
  2446. return HAL_ERROR;
  2447. }
  2448. }
  2449. /**
  2450. * @}
  2451. */
  2452. /** @defgroup CRYP_Exported_Functions_Group4 TDES processing functions
  2453. * @brief processing functions.
  2454. *
  2455. @verbatim
  2456. ==============================================================================
  2457. ##### TDES processing functions #####
  2458. ==============================================================================
  2459. [..] This section provides functions allowing to:
  2460. (+) Encrypt plaintext using TDES based on ECB or CBC chaining modes
  2461. (+) Decrypt cyphertext using TDES based on ECB or CBC chaining modes
  2462. [..] Three processing functions are available:
  2463. (+) Polling mode
  2464. (+) Interrupt mode
  2465. (+) DMA mode
  2466. @endverbatim
  2467. * @{
  2468. */
  2469. /**
  2470. * @brief Initializes the CRYP peripheral in TDES ECB encryption mode
  2471. * then encrypt pPlainData. The cypher data are available in pCypherData
  2472. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2473. * the configuration information for CRYP module
  2474. * @param pPlainData: Pointer to the plaintext buffer
  2475. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2476. * @param pCypherData: Pointer to the cyphertext buffer
  2477. * @param Timeout: Specify Timeout value
  2478. * @retval HAL status
  2479. */
  2480. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  2481. {
  2482. /* Process Locked */
  2483. __HAL_LOCK(hcryp);
  2484. /* Change the CRYP state */
  2485. hcryp->State = HAL_CRYP_STATE_BUSY;
  2486. /* Set CRYP peripheral in TDES ECB encryption mode */
  2487. CRYP_SetTDESECBMode(hcryp, 0);
  2488. /* Enable CRYP */
  2489. __HAL_CRYP_ENABLE(hcryp);
  2490. /* Write Plain Data and Get Cypher Data */
  2491. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  2492. {
  2493. return HAL_TIMEOUT;
  2494. }
  2495. /* Change the CRYP state */
  2496. hcryp->State = HAL_CRYP_STATE_READY;
  2497. /* Process Unlocked */
  2498. __HAL_UNLOCK(hcryp);
  2499. /* Return function status */
  2500. return HAL_OK;
  2501. }
  2502. /**
  2503. * @brief Initializes the CRYP peripheral in TDES ECB decryption mode
  2504. * then decrypted pCypherData. The cypher data are available in pPlainData
  2505. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2506. * the configuration information for CRYP module
  2507. * @param pPlainData: Pointer to the plaintext buffer
  2508. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2509. * @param pCypherData: Pointer to the cyphertext buffer
  2510. * @param Timeout: Specify Timeout value
  2511. * @retval HAL status
  2512. */
  2513. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  2514. {
  2515. /* Process Locked */
  2516. __HAL_LOCK(hcryp);
  2517. /* Change the CRYP state */
  2518. hcryp->State = HAL_CRYP_STATE_BUSY;
  2519. /* Set CRYP peripheral in TDES ECB decryption mode */
  2520. CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2521. /* Enable CRYP */
  2522. __HAL_CRYP_ENABLE(hcryp);
  2523. /* Write Cypher Data and Get Plain Data */
  2524. if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  2525. {
  2526. return HAL_TIMEOUT;
  2527. }
  2528. /* Change the CRYP state */
  2529. hcryp->State = HAL_CRYP_STATE_READY;
  2530. /* Process Unlocked */
  2531. __HAL_UNLOCK(hcryp);
  2532. /* Return function status */
  2533. return HAL_OK;
  2534. }
  2535. /**
  2536. * @brief Initializes the CRYP peripheral in TDES CBC encryption mode
  2537. * then encrypt pPlainData. The cypher data are available in pCypherData
  2538. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2539. * the configuration information for CRYP module
  2540. * @param pPlainData: Pointer to the plaintext buffer
  2541. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2542. * @param pCypherData: Pointer to the cyphertext buffer
  2543. * @param Timeout: Specify Timeout value
  2544. * @retval HAL status
  2545. */
  2546. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  2547. {
  2548. /* Process Locked */
  2549. __HAL_LOCK(hcryp);
  2550. /* Change the CRYP state */
  2551. hcryp->State = HAL_CRYP_STATE_BUSY;
  2552. /* Set CRYP peripheral in TDES CBC encryption mode */
  2553. CRYP_SetTDESCBCMode(hcryp, 0);
  2554. /* Enable CRYP */
  2555. __HAL_CRYP_ENABLE(hcryp);
  2556. /* Write Plain Data and Get Cypher Data */
  2557. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  2558. {
  2559. return HAL_TIMEOUT;
  2560. }
  2561. /* Change the CRYP state */
  2562. hcryp->State = HAL_CRYP_STATE_READY;
  2563. /* Process Unlocked */
  2564. __HAL_UNLOCK(hcryp);
  2565. /* Return function status */
  2566. return HAL_OK;
  2567. }
  2568. /**
  2569. * @brief Initializes the CRYP peripheral in TDES CBC decryption mode
  2570. * then decrypted pCypherData. The cypher data are available in pPlainData
  2571. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2572. * the configuration information for CRYP module
  2573. * @param pCypherData: Pointer to the cyphertext buffer
  2574. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2575. * @param pPlainData: Pointer to the plaintext buffer
  2576. * @param Timeout: Specify Timeout value
  2577. * @retval HAL status
  2578. */
  2579. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  2580. {
  2581. /* Process Locked */
  2582. __HAL_LOCK(hcryp);
  2583. /* Change the CRYP state */
  2584. hcryp->State = HAL_CRYP_STATE_BUSY;
  2585. /* Set CRYP peripheral in TDES CBC decryption mode */
  2586. CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2587. /* Enable CRYP */
  2588. __HAL_CRYP_ENABLE(hcryp);
  2589. /* Write Cypher Data and Get Plain Data */
  2590. if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  2591. {
  2592. return HAL_TIMEOUT;
  2593. }
  2594. /* Change the CRYP state */
  2595. hcryp->State = HAL_CRYP_STATE_READY;
  2596. /* Process Unlocked */
  2597. __HAL_UNLOCK(hcryp);
  2598. /* Return function status */
  2599. return HAL_OK;
  2600. }
  2601. /**
  2602. * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using interrupt.
  2603. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2604. * the configuration information for CRYP module
  2605. * @param pPlainData: Pointer to the plaintext buffer
  2606. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2607. * @param pCypherData: Pointer to the cyphertext buffer
  2608. * @retval HAL status
  2609. */
  2610. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2611. {
  2612. uint32_t inputaddr;
  2613. uint32_t outputaddr;
  2614. if(hcryp->State == HAL_CRYP_STATE_READY)
  2615. {
  2616. /* Process Locked */
  2617. __HAL_LOCK(hcryp);
  2618. hcryp->CrypInCount = Size;
  2619. hcryp->pCrypInBuffPtr = pPlainData;
  2620. hcryp->pCrypOutBuffPtr = pCypherData;
  2621. hcryp->CrypOutCount = Size;
  2622. /* Change the CRYP state */
  2623. hcryp->State = HAL_CRYP_STATE_BUSY;
  2624. /* Set CRYP peripheral in TDES ECB encryption mode */
  2625. CRYP_SetTDESECBMode(hcryp, 0);
  2626. /* Enable Interrupts */
  2627. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2628. /* Enable CRYP */
  2629. __HAL_CRYP_ENABLE(hcryp);
  2630. /* Return function status */
  2631. return HAL_OK;
  2632. }
  2633. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2634. {
  2635. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2636. /* Write the Input block in the IN FIFO */
  2637. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2638. inputaddr+=4;
  2639. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2640. hcryp->pCrypInBuffPtr += 8;
  2641. hcryp->CrypInCount -= 8;
  2642. if(hcryp->CrypInCount == 0)
  2643. {
  2644. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2645. /* Call the Input data transfer complete callback */
  2646. HAL_CRYP_InCpltCallback(hcryp);
  2647. }
  2648. }
  2649. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2650. {
  2651. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2652. /* Read the Output block from the Output FIFO */
  2653. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2654. outputaddr+=4;
  2655. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2656. hcryp->pCrypOutBuffPtr += 8;
  2657. hcryp->CrypOutCount -= 8;
  2658. if(hcryp->CrypOutCount == 0)
  2659. {
  2660. /* Disable IT */
  2661. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2662. /* Disable CRYP */
  2663. __HAL_CRYP_DISABLE(hcryp);
  2664. /* Process Unlocked */
  2665. __HAL_UNLOCK(hcryp);
  2666. /* Change the CRYP state */
  2667. hcryp->State = HAL_CRYP_STATE_READY;
  2668. /* Call the Output data transfer complete callback */
  2669. HAL_CRYP_OutCpltCallback(hcryp);
  2670. }
  2671. }
  2672. /* Return function status */
  2673. return HAL_OK;
  2674. }
  2675. /**
  2676. * @brief Initializes the CRYP peripheral in TDES CBC encryption mode.
  2677. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2678. * the configuration information for CRYP module
  2679. * @param pPlainData: Pointer to the plaintext buffer
  2680. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2681. * @param pCypherData: Pointer to the cyphertext buffer
  2682. * @retval HAL status
  2683. */
  2684. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2685. {
  2686. uint32_t inputaddr;
  2687. uint32_t outputaddr;
  2688. if(hcryp->State == HAL_CRYP_STATE_READY)
  2689. {
  2690. /* Process Locked */
  2691. __HAL_LOCK(hcryp);
  2692. hcryp->CrypInCount = Size;
  2693. hcryp->pCrypInBuffPtr = pPlainData;
  2694. hcryp->pCrypOutBuffPtr = pCypherData;
  2695. hcryp->CrypOutCount = Size;
  2696. /* Change the CRYP state */
  2697. hcryp->State = HAL_CRYP_STATE_BUSY;
  2698. /* Set CRYP peripheral in TDES CBC encryption mode */
  2699. CRYP_SetTDESCBCMode(hcryp, 0);
  2700. /* Enable Interrupts */
  2701. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2702. /* Enable CRYP */
  2703. __HAL_CRYP_ENABLE(hcryp);
  2704. /* Return function status */
  2705. return HAL_OK;
  2706. }
  2707. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2708. {
  2709. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2710. /* Write the Input block in the IN FIFO */
  2711. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2712. inputaddr+=4;
  2713. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2714. hcryp->pCrypInBuffPtr += 8;
  2715. hcryp->CrypInCount -= 8;
  2716. if(hcryp->CrypInCount == 0)
  2717. {
  2718. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2719. /* Call the Input data transfer complete callback */
  2720. HAL_CRYP_InCpltCallback(hcryp);
  2721. }
  2722. }
  2723. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2724. {
  2725. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2726. /* Read the Output block from the Output FIFO */
  2727. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2728. outputaddr+=4;
  2729. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2730. hcryp->pCrypOutBuffPtr += 8;
  2731. hcryp->CrypOutCount -= 8;
  2732. if(hcryp->CrypOutCount == 0)
  2733. {
  2734. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2735. /* Disable CRYP */
  2736. __HAL_CRYP_DISABLE(hcryp);
  2737. /* Process Unlocked */
  2738. __HAL_UNLOCK(hcryp);
  2739. /* Change the CRYP state */
  2740. hcryp->State = HAL_CRYP_STATE_READY;
  2741. /* Call Input transfer complete callback */
  2742. HAL_CRYP_OutCpltCallback(hcryp);
  2743. }
  2744. }
  2745. /* Return function status */
  2746. return HAL_OK;
  2747. }
  2748. /**
  2749. * @brief Initializes the CRYP peripheral in TDES ECB decryption mode.
  2750. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2751. * the configuration information for CRYP module
  2752. * @param pPlainData: Pointer to the plaintext buffer
  2753. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2754. * @param pCypherData: Pointer to the cyphertext buffer
  2755. * @retval HAL status
  2756. */
  2757. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2758. {
  2759. uint32_t inputaddr;
  2760. uint32_t outputaddr;
  2761. if(hcryp->State == HAL_CRYP_STATE_READY)
  2762. {
  2763. /* Process Locked */
  2764. __HAL_LOCK(hcryp);
  2765. hcryp->CrypInCount = Size;
  2766. hcryp->pCrypInBuffPtr = pCypherData;
  2767. hcryp->pCrypOutBuffPtr = pPlainData;
  2768. hcryp->CrypOutCount = Size;
  2769. /* Change the CRYP state */
  2770. hcryp->State = HAL_CRYP_STATE_BUSY;
  2771. /* Set CRYP peripheral in TDES ECB decryption mode */
  2772. CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2773. /* Enable Interrupts */
  2774. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2775. /* Enable CRYP */
  2776. __HAL_CRYP_ENABLE(hcryp);
  2777. /* Return function status */
  2778. return HAL_OK;
  2779. }
  2780. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2781. {
  2782. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2783. /* Write the Input block in the IN FIFO */
  2784. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2785. inputaddr+=4;
  2786. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2787. hcryp->pCrypInBuffPtr += 8;
  2788. hcryp->CrypInCount -= 8;
  2789. if(hcryp->CrypInCount == 0)
  2790. {
  2791. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2792. /* Call the Input data transfer complete callback */
  2793. HAL_CRYP_InCpltCallback(hcryp);
  2794. }
  2795. }
  2796. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2797. {
  2798. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2799. /* Read the Output block from the Output FIFO */
  2800. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2801. outputaddr+=4;
  2802. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2803. hcryp->pCrypOutBuffPtr += 8;
  2804. hcryp->CrypOutCount -= 8;
  2805. if(hcryp->CrypOutCount == 0)
  2806. {
  2807. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2808. /* Disable CRYP */
  2809. __HAL_CRYP_DISABLE(hcryp);
  2810. /* Process Unlocked */
  2811. __HAL_UNLOCK(hcryp);
  2812. /* Change the CRYP state */
  2813. hcryp->State = HAL_CRYP_STATE_READY;
  2814. /* Call Input transfer complete callback */
  2815. HAL_CRYP_OutCpltCallback(hcryp);
  2816. }
  2817. }
  2818. /* Return function status */
  2819. return HAL_OK;
  2820. }
  2821. /**
  2822. * @brief Initializes the CRYP peripheral in TDES CBC decryption mode.
  2823. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2824. * the configuration information for CRYP module
  2825. * @param pCypherData: Pointer to the cyphertext buffer
  2826. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2827. * @param pPlainData: Pointer to the plaintext buffer
  2828. * @retval HAL status
  2829. */
  2830. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2831. {
  2832. uint32_t inputaddr;
  2833. uint32_t outputaddr;
  2834. if(hcryp->State == HAL_CRYP_STATE_READY)
  2835. {
  2836. /* Process Locked */
  2837. __HAL_LOCK(hcryp);
  2838. hcryp->CrypInCount = Size;
  2839. hcryp->pCrypInBuffPtr = pCypherData;
  2840. hcryp->pCrypOutBuffPtr = pPlainData;
  2841. hcryp->CrypOutCount = Size;
  2842. /* Change the CRYP state */
  2843. hcryp->State = HAL_CRYP_STATE_BUSY;
  2844. /* Set CRYP peripheral in TDES CBC decryption mode */
  2845. CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2846. /* Enable Interrupts */
  2847. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2848. /* Enable CRYP */
  2849. __HAL_CRYP_ENABLE(hcryp);
  2850. /* Return function status */
  2851. return HAL_OK;
  2852. }
  2853. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2854. {
  2855. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2856. /* Write the Input block in the IN FIFO */
  2857. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2858. inputaddr+=4;
  2859. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2860. hcryp->pCrypInBuffPtr += 8;
  2861. hcryp->CrypInCount -= 8;
  2862. if(hcryp->CrypInCount == 0)
  2863. {
  2864. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2865. /* Call the Input data transfer complete callback */
  2866. HAL_CRYP_InCpltCallback(hcryp);
  2867. }
  2868. }
  2869. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2870. {
  2871. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2872. /* Read the Output block from the Output FIFO */
  2873. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2874. outputaddr+=4;
  2875. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2876. hcryp->pCrypOutBuffPtr += 8;
  2877. hcryp->CrypOutCount -= 8;
  2878. if(hcryp->CrypOutCount == 0)
  2879. {
  2880. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2881. /* Disable CRYP */
  2882. __HAL_CRYP_DISABLE(hcryp);
  2883. /* Process Unlocked */
  2884. __HAL_UNLOCK(hcryp);
  2885. /* Change the CRYP state */
  2886. hcryp->State = HAL_CRYP_STATE_READY;
  2887. /* Call Input transfer complete callback */
  2888. HAL_CRYP_OutCpltCallback(hcryp);
  2889. }
  2890. }
  2891. /* Return function status */
  2892. return HAL_OK;
  2893. }
  2894. /**
  2895. * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using DMA.
  2896. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2897. * the configuration information for CRYP module
  2898. * @param pPlainData: Pointer to the plaintext buffer
  2899. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2900. * @param pCypherData: Pointer to the cyphertext buffer
  2901. * @retval HAL status
  2902. */
  2903. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2904. {
  2905. uint32_t inputaddr;
  2906. uint32_t outputaddr;
  2907. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2908. {
  2909. /* Process Locked */
  2910. __HAL_LOCK(hcryp);
  2911. inputaddr = (uint32_t)pPlainData;
  2912. outputaddr = (uint32_t)pCypherData;
  2913. /* Change the CRYP state */
  2914. hcryp->State = HAL_CRYP_STATE_BUSY;
  2915. /* Set CRYP peripheral in TDES ECB encryption mode */
  2916. CRYP_SetTDESECBMode(hcryp, 0);
  2917. /* Set the input and output addresses and start DMA transfer */
  2918. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2919. /* Process Unlocked */
  2920. __HAL_UNLOCK(hcryp);
  2921. /* Return function status */
  2922. return HAL_OK;
  2923. }
  2924. else
  2925. {
  2926. return HAL_ERROR;
  2927. }
  2928. }
  2929. /**
  2930. * @brief Initializes the CRYP peripheral in TDES CBC encryption mode using DMA.
  2931. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2932. * the configuration information for CRYP module
  2933. * @param pPlainData: Pointer to the plaintext buffer
  2934. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2935. * @param pCypherData: Pointer to the cyphertext buffer
  2936. * @retval HAL status
  2937. */
  2938. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2939. {
  2940. uint32_t inputaddr;
  2941. uint32_t outputaddr;
  2942. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2943. {
  2944. /* Process Locked */
  2945. __HAL_LOCK(hcryp);
  2946. inputaddr = (uint32_t)pPlainData;
  2947. outputaddr = (uint32_t)pCypherData;
  2948. /* Change the CRYP state */
  2949. hcryp->State = HAL_CRYP_STATE_BUSY;
  2950. /* Set CRYP peripheral in TDES CBC encryption mode */
  2951. CRYP_SetTDESCBCMode(hcryp, 0);
  2952. /* Set the input and output addresses and start DMA transfer */
  2953. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2954. /* Process Unlocked */
  2955. __HAL_UNLOCK(hcryp);
  2956. /* Return function status */
  2957. return HAL_OK;
  2958. }
  2959. else
  2960. {
  2961. return HAL_ERROR;
  2962. }
  2963. }
  2964. /**
  2965. * @brief Initializes the CRYP peripheral in TDES ECB decryption mode using DMA.
  2966. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2967. * the configuration information for CRYP module
  2968. * @param pPlainData: Pointer to the plaintext buffer
  2969. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2970. * @param pCypherData: Pointer to the cyphertext buffer
  2971. * @retval HAL status
  2972. */
  2973. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2974. {
  2975. uint32_t inputaddr;
  2976. uint32_t outputaddr;
  2977. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2978. {
  2979. /* Process Locked */
  2980. __HAL_LOCK(hcryp);
  2981. inputaddr = (uint32_t)pCypherData;
  2982. outputaddr = (uint32_t)pPlainData;
  2983. /* Change the CRYP state */
  2984. hcryp->State = HAL_CRYP_STATE_BUSY;
  2985. /* Set CRYP peripheral in TDES ECB decryption mode */
  2986. CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2987. /* Set the input and output addresses and start DMA transfer */
  2988. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2989. /* Process Unlocked */
  2990. __HAL_UNLOCK(hcryp);
  2991. /* Return function status */
  2992. return HAL_OK;
  2993. }
  2994. else
  2995. {
  2996. return HAL_ERROR;
  2997. }
  2998. }
  2999. /**
  3000. * @brief Initializes the CRYP peripheral in TDES CBC decryption mode using DMA.
  3001. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3002. * the configuration information for CRYP module
  3003. * @param pCypherData: Pointer to the cyphertext buffer
  3004. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  3005. * @param pPlainData: Pointer to the plaintext buffer
  3006. * @retval HAL status
  3007. */
  3008. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3009. {
  3010. uint32_t inputaddr;
  3011. uint32_t outputaddr;
  3012. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  3013. {
  3014. /* Process Locked */
  3015. __HAL_LOCK(hcryp);
  3016. inputaddr = (uint32_t)pCypherData;
  3017. outputaddr = (uint32_t)pPlainData;
  3018. /* Change the CRYP state */
  3019. hcryp->State = HAL_CRYP_STATE_BUSY;
  3020. /* Set CRYP peripheral in TDES CBC decryption mode */
  3021. CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  3022. /* Set the input and output addresses and start DMA transfer */
  3023. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  3024. /* Process Unlocked */
  3025. __HAL_UNLOCK(hcryp);
  3026. /* Return function status */
  3027. return HAL_OK;
  3028. }
  3029. else
  3030. {
  3031. return HAL_ERROR;
  3032. }
  3033. }
  3034. /**
  3035. * @}
  3036. */
  3037. /** @defgroup CRYP_Exported_Functions_Group5 DMA callback functions
  3038. * @brief DMA callback functions.
  3039. *
  3040. @verbatim
  3041. ==============================================================================
  3042. ##### DMA callback functions #####
  3043. ==============================================================================
  3044. [..] This section provides DMA callback functions:
  3045. (+) DMA Input data transfer complete
  3046. (+) DMA Output data transfer complete
  3047. (+) DMA error
  3048. @endverbatim
  3049. * @{
  3050. */
  3051. /**
  3052. * @brief Input FIFO transfer completed callbacks.
  3053. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3054. * the configuration information for CRYP module
  3055. * @retval None
  3056. */
  3057. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  3058. {
  3059. /* NOTE : This function Should not be modified, when the callback is needed,
  3060. the HAL_CRYP_InCpltCallback could be implemented in the user file
  3061. */
  3062. }
  3063. /**
  3064. * @brief Output FIFO transfer completed callbacks.
  3065. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3066. * the configuration information for CRYP module
  3067. * @retval None
  3068. */
  3069. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  3070. {
  3071. /* NOTE : This function Should not be modified, when the callback is needed,
  3072. the HAL_CRYP_OutCpltCallback could be implemented in the user file
  3073. */
  3074. }
  3075. /**
  3076. * @brief CRYP error callbacks.
  3077. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3078. * the configuration information for CRYP module
  3079. * @retval None
  3080. */
  3081. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  3082. {
  3083. /* NOTE : This function Should not be modified, when the callback is needed,
  3084. the HAL_CRYP_ErrorCallback could be implemented in the user file
  3085. */
  3086. }
  3087. /**
  3088. * @}
  3089. */
  3090. /** @defgroup CRYP_Exported_Functions_Group6 CRYP IRQ handler management
  3091. * @brief CRYP IRQ handler.
  3092. *
  3093. @verbatim
  3094. ==============================================================================
  3095. ##### CRYP IRQ handler management #####
  3096. ==============================================================================
  3097. [..] This section provides CRYP IRQ handler function.
  3098. @endverbatim
  3099. * @{
  3100. */
  3101. /**
  3102. * @brief This function handles CRYP interrupt request.
  3103. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3104. * the configuration information for CRYP module
  3105. * @retval None
  3106. */
  3107. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  3108. {
  3109. switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION)
  3110. {
  3111. case CRYP_CR_ALGOMODE_TDES_ECB_ENCRYPT:
  3112. HAL_CRYP_TDESECB_Encrypt_IT(hcryp, NULL, 0, NULL);
  3113. break;
  3114. case CRYP_CR_ALGOMODE_TDES_ECB_DECRYPT:
  3115. HAL_CRYP_TDESECB_Decrypt_IT(hcryp, NULL, 0, NULL);
  3116. break;
  3117. case CRYP_CR_ALGOMODE_TDES_CBC_ENCRYPT:
  3118. HAL_CRYP_TDESCBC_Encrypt_IT(hcryp, NULL, 0, NULL);
  3119. break;
  3120. case CRYP_CR_ALGOMODE_TDES_CBC_DECRYPT:
  3121. HAL_CRYP_TDESCBC_Decrypt_IT(hcryp, NULL, 0, NULL);
  3122. break;
  3123. case CRYP_CR_ALGOMODE_DES_ECB_ENCRYPT:
  3124. HAL_CRYP_DESECB_Encrypt_IT(hcryp, NULL, 0, NULL);
  3125. break;
  3126. case CRYP_CR_ALGOMODE_DES_ECB_DECRYPT:
  3127. HAL_CRYP_DESECB_Decrypt_IT(hcryp, NULL, 0, NULL);
  3128. break;
  3129. case CRYP_CR_ALGOMODE_DES_CBC_ENCRYPT:
  3130. HAL_CRYP_DESCBC_Encrypt_IT(hcryp, NULL, 0, NULL);
  3131. break;
  3132. case CRYP_CR_ALGOMODE_DES_CBC_DECRYPT:
  3133. HAL_CRYP_DESCBC_Decrypt_IT(hcryp, NULL, 0, NULL);
  3134. break;
  3135. case CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT:
  3136. HAL_CRYP_AESECB_Encrypt_IT(hcryp, NULL, 0, NULL);
  3137. break;
  3138. case CRYP_CR_ALGOMODE_AES_ECB_DECRYPT:
  3139. HAL_CRYP_AESECB_Decrypt_IT(hcryp, NULL, 0, NULL);
  3140. break;
  3141. case CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT:
  3142. HAL_CRYP_AESCBC_Encrypt_IT(hcryp, NULL, 0, NULL);
  3143. break;
  3144. case CRYP_CR_ALGOMODE_AES_CBC_DECRYPT:
  3145. HAL_CRYP_AESCBC_Decrypt_IT(hcryp, NULL, 0, NULL);
  3146. break;
  3147. case CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT:
  3148. HAL_CRYP_AESCTR_Encrypt_IT(hcryp, NULL, 0, NULL);
  3149. break;
  3150. case CRYP_CR_ALGOMODE_AES_CTR_DECRYPT:
  3151. HAL_CRYP_AESCTR_Decrypt_IT(hcryp, NULL, 0, NULL);
  3152. break;
  3153. default:
  3154. break;
  3155. }
  3156. }
  3157. /**
  3158. * @}
  3159. */
  3160. /** @defgroup CRYP_Exported_Functions_Group7 Peripheral State functions
  3161. * @brief Peripheral State functions.
  3162. *
  3163. @verbatim
  3164. ==============================================================================
  3165. ##### Peripheral State functions #####
  3166. ==============================================================================
  3167. [..]
  3168. This subsection permits to get in run-time the status of the peripheral.
  3169. @endverbatim
  3170. * @{
  3171. */
  3172. /**
  3173. * @brief Returns the CRYP state.
  3174. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3175. * the configuration information for CRYP module
  3176. * @retval HAL state
  3177. */
  3178. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  3179. {
  3180. return hcryp->State;
  3181. }
  3182. /**
  3183. * @}
  3184. */
  3185. /**
  3186. * @}
  3187. */
  3188. #endif /* HAL_CRYP_MODULE_ENABLED */
  3189. /**
  3190. * @}
  3191. */
  3192. #endif /* STM32F756xx */
  3193. /**
  3194. * @}
  3195. */
  3196. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/