stm32f4xx_hal_cryp.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_cryp.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-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 "stm32f4xx_hal.h"
  95. /** @addtogroup STM32F4xx_HAL_Driver
  96. * @{
  97. */
  98. /** @defgroup CRYP CRYP
  99. * @brief CRYP HAL module driver.
  100. * @{
  101. */
  102. #ifdef HAL_CRYP_MODULE_ENABLED
  103. #if defined(STM32F415xx) || defined(STM32F417xx) || defined(STM32F437xx) || defined(STM32F439xx) || defined(STM32F479xx)
  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. /* Prevent unused argument(s) compilation warning */
  606. UNUSED(hcryp);
  607. /* NOTE : This function Should not be modified, when the callback is needed,
  608. the HAL_CRYP_MspInit could be implemented in the user file
  609. */
  610. }
  611. /**
  612. * @brief DeInitializes CRYP MSP.
  613. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  614. * the configuration information for CRYP module
  615. * @retval None
  616. */
  617. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  618. {
  619. /* Prevent unused argument(s) compilation warning */
  620. UNUSED(hcryp);
  621. /* NOTE : This function Should not be modified, when the callback is needed,
  622. the HAL_CRYP_MspDeInit could be implemented in the user file
  623. */
  624. }
  625. /**
  626. * @}
  627. */
  628. /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
  629. * @brief processing functions.
  630. *
  631. @verbatim
  632. ==============================================================================
  633. ##### AES processing functions #####
  634. ==============================================================================
  635. [..] This section provides functions allowing to:
  636. (+) Encrypt plaintext using AES-128/192/256 using chaining modes
  637. (+) Decrypt cyphertext using AES-128/192/256 using chaining modes
  638. [..] Three processing functions are available:
  639. (+) Polling mode
  640. (+) Interrupt mode
  641. (+) DMA mode
  642. @endverbatim
  643. * @{
  644. */
  645. /**
  646. * @brief Initializes the CRYP peripheral in AES ECB encryption mode
  647. * then encrypt pPlainData. The cypher data are available in pCypherData
  648. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  649. * the configuration information for CRYP module
  650. * @param pPlainData: Pointer to the plaintext buffer
  651. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  652. * @param pCypherData: Pointer to the cyphertext buffer
  653. * @param Timeout: Specify Timeout value
  654. * @retval HAL status
  655. */
  656. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  657. {
  658. /* Process Locked */
  659. __HAL_LOCK(hcryp);
  660. /* Change the CRYP state */
  661. hcryp->State = HAL_CRYP_STATE_BUSY;
  662. /* Check if initialization phase has already been performed */
  663. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  664. {
  665. /* Set the key */
  666. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  667. /* Set the CRYP peripheral in AES ECB mode */
  668. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB);
  669. /* Flush FIFO */
  670. __HAL_CRYP_FIFO_FLUSH(hcryp);
  671. /* Enable CRYP */
  672. __HAL_CRYP_ENABLE(hcryp);
  673. /* Set the phase */
  674. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  675. }
  676. /* Write Plain Data and Get Cypher Data */
  677. if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  678. {
  679. return HAL_TIMEOUT;
  680. }
  681. /* Change the CRYP state */
  682. hcryp->State = HAL_CRYP_STATE_READY;
  683. /* Process Unlocked */
  684. __HAL_UNLOCK(hcryp);
  685. /* Return function status */
  686. return HAL_OK;
  687. }
  688. /**
  689. * @brief Initializes the CRYP peripheral in AES CBC encryption mode
  690. * then encrypt pPlainData. The cypher data are available in pCypherData
  691. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  692. * the configuration information for CRYP module
  693. * @param pPlainData: Pointer to the plaintext buffer
  694. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  695. * @param pCypherData: Pointer to the cyphertext buffer
  696. * @param Timeout: Specify Timeout value
  697. * @retval HAL status
  698. */
  699. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  700. {
  701. /* Process Locked */
  702. __HAL_LOCK(hcryp);
  703. /* Change the CRYP state */
  704. hcryp->State = HAL_CRYP_STATE_BUSY;
  705. /* Check if initialization phase has already been performed */
  706. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  707. {
  708. /* Set the key */
  709. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  710. /* Set the CRYP peripheral in AES ECB mode */
  711. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC);
  712. /* Set the Initialization Vector */
  713. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  714. /* Flush FIFO */
  715. __HAL_CRYP_FIFO_FLUSH(hcryp);
  716. /* Enable CRYP */
  717. __HAL_CRYP_ENABLE(hcryp);
  718. /* Set the phase */
  719. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  720. }
  721. /* Write Plain Data and Get Cypher Data */
  722. if(CRYP_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  723. {
  724. return HAL_TIMEOUT;
  725. }
  726. /* Change the CRYP state */
  727. hcryp->State = HAL_CRYP_STATE_READY;
  728. /* Process Unlocked */
  729. __HAL_UNLOCK(hcryp);
  730. /* Return function status */
  731. return HAL_OK;
  732. }
  733. /**
  734. * @brief Initializes the CRYP peripheral in AES CTR encryption mode
  735. * then encrypt pPlainData. The cypher data are available in pCypherData
  736. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  737. * the configuration information for CRYP module
  738. * @param pPlainData: Pointer to the plaintext buffer
  739. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  740. * @param pCypherData: Pointer to the cyphertext buffer
  741. * @param Timeout: Specify Timeout value
  742. * @retval HAL status
  743. */
  744. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  745. {
  746. /* Process Locked */
  747. __HAL_LOCK(hcryp);
  748. /* Change the CRYP state */
  749. hcryp->State = HAL_CRYP_STATE_BUSY;
  750. /* Check if initialization phase has already been performed */
  751. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  752. {
  753. /* Set the key */
  754. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  755. /* Set the CRYP peripheral in AES ECB mode */
  756. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR);
  757. /* Set the Initialization Vector */
  758. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  759. /* Flush FIFO */
  760. __HAL_CRYP_FIFO_FLUSH(hcryp);
  761. /* Enable CRYP */
  762. __HAL_CRYP_ENABLE(hcryp);
  763. /* Set the phase */
  764. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  765. }
  766. /* Write Plain Data and Get Cypher Data */
  767. if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  768. {
  769. return HAL_TIMEOUT;
  770. }
  771. /* Change the CRYP state */
  772. hcryp->State = HAL_CRYP_STATE_READY;
  773. /* Process Unlocked */
  774. __HAL_UNLOCK(hcryp);
  775. /* Return function status */
  776. return HAL_OK;
  777. }
  778. /**
  779. * @brief Initializes the CRYP peripheral in AES ECB decryption mode
  780. * then decrypted pCypherData. The cypher data are available in pPlainData
  781. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  782. * the configuration information for CRYP module
  783. * @param pCypherData: Pointer to the cyphertext buffer
  784. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  785. * @param pPlainData: Pointer to the plaintext buffer
  786. * @param Timeout: Specify Timeout value
  787. * @retval HAL status
  788. */
  789. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  790. {
  791. uint32_t tickstart = 0;
  792. /* Process Locked */
  793. __HAL_LOCK(hcryp);
  794. /* Change the CRYP state */
  795. hcryp->State = HAL_CRYP_STATE_BUSY;
  796. /* Check if initialization phase has already been performed */
  797. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  798. {
  799. /* Set the key */
  800. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  801. /* Set the CRYP peripheral in AES Key mode */
  802. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  803. /* Enable CRYP */
  804. __HAL_CRYP_ENABLE(hcryp);
  805. /* Get tick */
  806. tickstart = HAL_GetTick();
  807. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  808. {
  809. /* Check for the Timeout */
  810. if(Timeout != HAL_MAX_DELAY)
  811. {
  812. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  813. {
  814. /* Change state */
  815. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  816. /* Process Unlocked */
  817. __HAL_UNLOCK(hcryp);
  818. return HAL_TIMEOUT;
  819. }
  820. }
  821. }
  822. /* Disable CRYP */
  823. __HAL_CRYP_DISABLE(hcryp);
  824. /* Reset the ALGOMODE bits*/
  825. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  826. /* Set the CRYP peripheral in AES ECB decryption mode */
  827. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
  828. /* Flush FIFO */
  829. __HAL_CRYP_FIFO_FLUSH(hcryp);
  830. /* Enable CRYP */
  831. __HAL_CRYP_ENABLE(hcryp);
  832. /* Set the phase */
  833. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  834. }
  835. /* Write Plain Data and Get Cypher Data */
  836. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  837. {
  838. return HAL_TIMEOUT;
  839. }
  840. /* Change the CRYP state */
  841. hcryp->State = HAL_CRYP_STATE_READY;
  842. /* Process Unlocked */
  843. __HAL_UNLOCK(hcryp);
  844. /* Return function status */
  845. return HAL_OK;
  846. }
  847. /**
  848. * @brief Initializes the CRYP peripheral in AES ECB decryption mode
  849. * then decrypted pCypherData. The cypher data are available in pPlainData
  850. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  851. * the configuration information for CRYP module
  852. * @param pCypherData: Pointer to the cyphertext buffer
  853. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  854. * @param pPlainData: Pointer to the plaintext buffer
  855. * @param Timeout: Specify Timeout value
  856. * @retval HAL status
  857. */
  858. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  859. {
  860. uint32_t tickstart = 0;
  861. /* Process Locked */
  862. __HAL_LOCK(hcryp);
  863. /* Change the CRYP state */
  864. hcryp->State = HAL_CRYP_STATE_BUSY;
  865. /* Check if initialization phase has already been performed */
  866. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  867. {
  868. /* Set the key */
  869. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  870. /* Set the CRYP peripheral in AES Key mode */
  871. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  872. /* Enable CRYP */
  873. __HAL_CRYP_ENABLE(hcryp);
  874. /* Get tick */
  875. tickstart = HAL_GetTick();
  876. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  877. {
  878. /* Check for the Timeout */
  879. if(Timeout != HAL_MAX_DELAY)
  880. {
  881. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  882. {
  883. /* Change state */
  884. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  885. /* Process Unlocked */
  886. __HAL_UNLOCK(hcryp);
  887. return HAL_TIMEOUT;
  888. }
  889. }
  890. }
  891. /* Reset the ALGOMODE bits*/
  892. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  893. /* Set the CRYP peripheral in AES CBC decryption mode */
  894. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
  895. /* Set the Initialization Vector */
  896. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  897. /* Flush FIFO */
  898. __HAL_CRYP_FIFO_FLUSH(hcryp);
  899. /* Enable CRYP */
  900. __HAL_CRYP_ENABLE(hcryp);
  901. /* Set the phase */
  902. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  903. }
  904. /* Write Plain Data and Get Cypher Data */
  905. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  906. {
  907. return HAL_TIMEOUT;
  908. }
  909. /* Change the CRYP state */
  910. hcryp->State = HAL_CRYP_STATE_READY;
  911. /* Process Unlocked */
  912. __HAL_UNLOCK(hcryp);
  913. /* Return function status */
  914. return HAL_OK;
  915. }
  916. /**
  917. * @brief Initializes the CRYP peripheral in AES CTR decryption mode
  918. * then decrypted pCypherData. The cypher data are available in pPlainData
  919. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  920. * the configuration information for CRYP module
  921. * @param pCypherData: Pointer to the cyphertext buffer
  922. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  923. * @param pPlainData: Pointer to the plaintext buffer
  924. * @param Timeout: Specify Timeout value
  925. * @retval HAL status
  926. */
  927. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  928. {
  929. /* Process Locked */
  930. __HAL_LOCK(hcryp);
  931. /* Check if initialization phase has already been performed */
  932. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  933. {
  934. /* Change the CRYP state */
  935. hcryp->State = HAL_CRYP_STATE_BUSY;
  936. /* Set the key */
  937. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  938. /* Set the CRYP peripheral in AES CTR mode */
  939. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
  940. /* Set the Initialization Vector */
  941. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  942. /* Flush FIFO */
  943. __HAL_CRYP_FIFO_FLUSH(hcryp);
  944. /* Enable CRYP */
  945. __HAL_CRYP_ENABLE(hcryp);
  946. /* Set the phase */
  947. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  948. }
  949. /* Write Plain Data and Get Cypher Data */
  950. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  951. {
  952. return HAL_TIMEOUT;
  953. }
  954. /* Change the CRYP state */
  955. hcryp->State = HAL_CRYP_STATE_READY;
  956. /* Process Unlocked */
  957. __HAL_UNLOCK(hcryp);
  958. /* Return function status */
  959. return HAL_OK;
  960. }
  961. /**
  962. * @brief Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt.
  963. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  964. * the configuration information for CRYP module
  965. * @param pPlainData: Pointer to the plaintext buffer
  966. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  967. * @param pCypherData: Pointer to the cyphertext buffer
  968. * @retval HAL status
  969. */
  970. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  971. {
  972. uint32_t inputaddr;
  973. uint32_t outputaddr;
  974. if(hcryp->State == HAL_CRYP_STATE_READY)
  975. {
  976. /* Process Locked */
  977. __HAL_LOCK(hcryp);
  978. hcryp->CrypInCount = Size;
  979. hcryp->pCrypInBuffPtr = pPlainData;
  980. hcryp->pCrypOutBuffPtr = pCypherData;
  981. hcryp->CrypOutCount = Size;
  982. /* Change the CRYP state */
  983. hcryp->State = HAL_CRYP_STATE_BUSY;
  984. /* Check if initialization phase has already been performed */
  985. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  986. {
  987. /* Set the key */
  988. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  989. /* Set the CRYP peripheral in AES ECB mode */
  990. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB);
  991. /* Flush FIFO */
  992. __HAL_CRYP_FIFO_FLUSH(hcryp);
  993. /* Set the phase */
  994. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  995. }
  996. /* Enable Interrupts */
  997. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  998. /* Enable CRYP */
  999. __HAL_CRYP_ENABLE(hcryp);
  1000. /* Return function status */
  1001. return HAL_OK;
  1002. }
  1003. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1004. {
  1005. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1006. /* Write the Input block in the IN FIFO */
  1007. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1008. inputaddr+=4;
  1009. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1010. inputaddr+=4;
  1011. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1012. inputaddr+=4;
  1013. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1014. hcryp->pCrypInBuffPtr += 16;
  1015. hcryp->CrypInCount -= 16;
  1016. if(hcryp->CrypInCount == 0)
  1017. {
  1018. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1019. /* Call the Input data transfer complete callback */
  1020. HAL_CRYP_InCpltCallback(hcryp);
  1021. }
  1022. }
  1023. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1024. {
  1025. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1026. /* Read the Output block from the Output FIFO */
  1027. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1028. outputaddr+=4;
  1029. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1030. outputaddr+=4;
  1031. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1032. outputaddr+=4;
  1033. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1034. hcryp->pCrypOutBuffPtr += 16;
  1035. hcryp->CrypOutCount -= 16;
  1036. if(hcryp->CrypOutCount == 0)
  1037. {
  1038. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1039. /* Process Locked */
  1040. __HAL_UNLOCK(hcryp);
  1041. /* Change the CRYP state */
  1042. hcryp->State = HAL_CRYP_STATE_READY;
  1043. /* Call Input transfer complete callback */
  1044. HAL_CRYP_OutCpltCallback(hcryp);
  1045. }
  1046. }
  1047. /* Return function status */
  1048. return HAL_OK;
  1049. }
  1050. /**
  1051. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt.
  1052. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1053. * the configuration information for CRYP module
  1054. * @param pPlainData: Pointer to the plaintext buffer
  1055. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1056. * @param pCypherData: Pointer to the cyphertext buffer
  1057. * @retval HAL status
  1058. */
  1059. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1060. {
  1061. uint32_t inputaddr;
  1062. uint32_t outputaddr;
  1063. if(hcryp->State == HAL_CRYP_STATE_READY)
  1064. {
  1065. /* Process Locked */
  1066. __HAL_LOCK(hcryp);
  1067. hcryp->CrypInCount = Size;
  1068. hcryp->pCrypInBuffPtr = pPlainData;
  1069. hcryp->pCrypOutBuffPtr = pCypherData;
  1070. hcryp->CrypOutCount = Size;
  1071. /* Change the CRYP state */
  1072. hcryp->State = HAL_CRYP_STATE_BUSY;
  1073. /* Check if initialization phase has already been performed */
  1074. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1075. {
  1076. /* Set the key */
  1077. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1078. /* Set the CRYP peripheral in AES CBC mode */
  1079. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC);
  1080. /* Set the Initialization Vector */
  1081. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1082. /* Flush FIFO */
  1083. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1084. /* Set the phase */
  1085. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1086. }
  1087. /* Enable Interrupts */
  1088. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1089. /* Enable CRYP */
  1090. __HAL_CRYP_ENABLE(hcryp);
  1091. /* Return function status */
  1092. return HAL_OK;
  1093. }
  1094. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1095. {
  1096. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1097. /* Write the Input block in the IN FIFO */
  1098. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1099. inputaddr+=4;
  1100. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1101. inputaddr+=4;
  1102. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1103. inputaddr+=4;
  1104. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1105. hcryp->pCrypInBuffPtr += 16;
  1106. hcryp->CrypInCount -= 16;
  1107. if(hcryp->CrypInCount == 0)
  1108. {
  1109. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1110. /* Call the Input data transfer complete callback */
  1111. HAL_CRYP_InCpltCallback(hcryp);
  1112. }
  1113. }
  1114. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1115. {
  1116. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1117. /* Read the Output block from the Output FIFO */
  1118. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1119. outputaddr+=4;
  1120. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1121. outputaddr+=4;
  1122. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1123. outputaddr+=4;
  1124. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1125. hcryp->pCrypOutBuffPtr += 16;
  1126. hcryp->CrypOutCount -= 16;
  1127. if(hcryp->CrypOutCount == 0)
  1128. {
  1129. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1130. /* Process Locked */
  1131. __HAL_UNLOCK(hcryp);
  1132. /* Change the CRYP state */
  1133. hcryp->State = HAL_CRYP_STATE_READY;
  1134. /* Call Input transfer complete callback */
  1135. HAL_CRYP_OutCpltCallback(hcryp);
  1136. }
  1137. }
  1138. /* Return function status */
  1139. return HAL_OK;
  1140. }
  1141. /**
  1142. * @brief Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt.
  1143. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1144. * the configuration information for CRYP module
  1145. * @param pPlainData: Pointer to the plaintext buffer
  1146. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1147. * @param pCypherData: Pointer to the cyphertext buffer
  1148. * @retval HAL status
  1149. */
  1150. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1151. {
  1152. uint32_t inputaddr;
  1153. uint32_t outputaddr;
  1154. if(hcryp->State == HAL_CRYP_STATE_READY)
  1155. {
  1156. /* Process Locked */
  1157. __HAL_LOCK(hcryp);
  1158. hcryp->CrypInCount = Size;
  1159. hcryp->pCrypInBuffPtr = pPlainData;
  1160. hcryp->pCrypOutBuffPtr = pCypherData;
  1161. hcryp->CrypOutCount = Size;
  1162. /* Change the CRYP state */
  1163. hcryp->State = HAL_CRYP_STATE_BUSY;
  1164. /* Check if initialization phase has already been performed */
  1165. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1166. {
  1167. /* Set the key */
  1168. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1169. /* Set the CRYP peripheral in AES CTR mode */
  1170. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR);
  1171. /* Set the Initialization Vector */
  1172. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1173. /* Flush FIFO */
  1174. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1175. /* Set the phase */
  1176. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1177. }
  1178. /* Enable Interrupts */
  1179. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1180. /* Enable CRYP */
  1181. __HAL_CRYP_ENABLE(hcryp);
  1182. /* Return function status */
  1183. return HAL_OK;
  1184. }
  1185. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1186. {
  1187. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1188. /* Write the Input block in the IN FIFO */
  1189. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1190. inputaddr+=4;
  1191. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1192. inputaddr+=4;
  1193. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1194. inputaddr+=4;
  1195. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1196. hcryp->pCrypInBuffPtr += 16;
  1197. hcryp->CrypInCount -= 16;
  1198. if(hcryp->CrypInCount == 0)
  1199. {
  1200. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1201. /* Call the Input data transfer complete callback */
  1202. HAL_CRYP_InCpltCallback(hcryp);
  1203. }
  1204. }
  1205. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1206. {
  1207. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1208. /* Read the Output block from the Output FIFO */
  1209. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1210. outputaddr+=4;
  1211. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1212. outputaddr+=4;
  1213. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1214. outputaddr+=4;
  1215. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1216. hcryp->pCrypOutBuffPtr += 16;
  1217. hcryp->CrypOutCount -= 16;
  1218. if(hcryp->CrypOutCount == 0)
  1219. {
  1220. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1221. /* Process Unlocked */
  1222. __HAL_UNLOCK(hcryp);
  1223. /* Change the CRYP state */
  1224. hcryp->State = HAL_CRYP_STATE_READY;
  1225. /* Call Input transfer complete callback */
  1226. HAL_CRYP_OutCpltCallback(hcryp);
  1227. }
  1228. }
  1229. /* Return function status */
  1230. return HAL_OK;
  1231. }
  1232. /**
  1233. * @brief Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt.
  1234. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1235. * the configuration information for CRYP module
  1236. * @param pCypherData: Pointer to the cyphertext buffer
  1237. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  1238. * @param pPlainData: Pointer to the plaintext buffer
  1239. * @retval HAL status
  1240. */
  1241. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1242. {
  1243. uint32_t tickstart = 0;
  1244. uint32_t inputaddr;
  1245. uint32_t outputaddr;
  1246. if(hcryp->State == HAL_CRYP_STATE_READY)
  1247. {
  1248. /* Process Locked */
  1249. __HAL_LOCK(hcryp);
  1250. hcryp->CrypInCount = Size;
  1251. hcryp->pCrypInBuffPtr = pCypherData;
  1252. hcryp->pCrypOutBuffPtr = pPlainData;
  1253. hcryp->CrypOutCount = Size;
  1254. /* Change the CRYP state */
  1255. hcryp->State = HAL_CRYP_STATE_BUSY;
  1256. /* Check if initialization phase has already been performed */
  1257. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1258. {
  1259. /* Set the key */
  1260. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1261. /* Set the CRYP peripheral in AES Key mode */
  1262. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1263. /* Enable CRYP */
  1264. __HAL_CRYP_ENABLE(hcryp);
  1265. /* Get tick */
  1266. tickstart = HAL_GetTick();
  1267. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1268. {
  1269. /* Check for the Timeout */
  1270. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1271. {
  1272. /* Change state */
  1273. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1274. /* Process Unlocked */
  1275. __HAL_UNLOCK(hcryp);
  1276. return HAL_TIMEOUT;
  1277. }
  1278. }
  1279. /* Reset the ALGOMODE bits*/
  1280. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1281. /* Set the CRYP peripheral in AES ECB decryption mode */
  1282. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
  1283. /* Flush FIFO */
  1284. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1285. /* Set the phase */
  1286. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1287. }
  1288. /* Enable Interrupts */
  1289. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1290. /* Enable CRYP */
  1291. __HAL_CRYP_ENABLE(hcryp);
  1292. /* Return function status */
  1293. return HAL_OK;
  1294. }
  1295. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1296. {
  1297. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1298. /* Write the Input block in the IN FIFO */
  1299. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1300. inputaddr+=4;
  1301. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1302. inputaddr+=4;
  1303. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1304. inputaddr+=4;
  1305. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1306. hcryp->pCrypInBuffPtr += 16;
  1307. hcryp->CrypInCount -= 16;
  1308. if(hcryp->CrypInCount == 0)
  1309. {
  1310. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1311. /* Call the Input data transfer complete callback */
  1312. HAL_CRYP_InCpltCallback(hcryp);
  1313. }
  1314. }
  1315. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1316. {
  1317. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1318. /* Read the Output block from the Output FIFO */
  1319. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1320. outputaddr+=4;
  1321. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1322. outputaddr+=4;
  1323. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1324. outputaddr+=4;
  1325. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1326. hcryp->pCrypOutBuffPtr += 16;
  1327. hcryp->CrypOutCount -= 16;
  1328. if(hcryp->CrypOutCount == 0)
  1329. {
  1330. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1331. /* Process Unlocked */
  1332. __HAL_UNLOCK(hcryp);
  1333. /* Change the CRYP state */
  1334. hcryp->State = HAL_CRYP_STATE_READY;
  1335. /* Call Input transfer complete callback */
  1336. HAL_CRYP_OutCpltCallback(hcryp);
  1337. }
  1338. }
  1339. /* Return function status */
  1340. return HAL_OK;
  1341. }
  1342. /**
  1343. * @brief Initializes the CRYP peripheral in AES CBC decryption mode using IT.
  1344. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1345. * the configuration information for CRYP module
  1346. * @param pCypherData: Pointer to the cyphertext buffer
  1347. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  1348. * @param pPlainData: Pointer to the plaintext buffer
  1349. * @retval HAL status
  1350. */
  1351. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1352. {
  1353. uint32_t tickstart = 0;
  1354. uint32_t inputaddr;
  1355. uint32_t outputaddr;
  1356. if(hcryp->State == HAL_CRYP_STATE_READY)
  1357. {
  1358. /* Process Locked */
  1359. __HAL_LOCK(hcryp);
  1360. /* Get the buffer addresses and sizes */
  1361. hcryp->CrypInCount = Size;
  1362. hcryp->pCrypInBuffPtr = pCypherData;
  1363. hcryp->pCrypOutBuffPtr = pPlainData;
  1364. hcryp->CrypOutCount = Size;
  1365. /* Change the CRYP state */
  1366. hcryp->State = HAL_CRYP_STATE_BUSY;
  1367. /* Check if initialization phase has already been performed */
  1368. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1369. {
  1370. /* Set the key */
  1371. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1372. /* Set the CRYP peripheral in AES Key mode */
  1373. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1374. /* Enable CRYP */
  1375. __HAL_CRYP_ENABLE(hcryp);
  1376. /* Get tick */
  1377. tickstart = HAL_GetTick();
  1378. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1379. {
  1380. /* Check for the Timeout */
  1381. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1382. {
  1383. /* Change state */
  1384. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1385. /* Process Unlocked */
  1386. __HAL_UNLOCK(hcryp);
  1387. return HAL_TIMEOUT;
  1388. }
  1389. }
  1390. /* Reset the ALGOMODE bits*/
  1391. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1392. /* Set the CRYP peripheral in AES CBC decryption mode */
  1393. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
  1394. /* Set the Initialization Vector */
  1395. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1396. /* Flush FIFO */
  1397. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1398. /* Enable CRYP */
  1399. __HAL_CRYP_ENABLE(hcryp);
  1400. /* Set the phase */
  1401. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1402. }
  1403. /* Enable Interrupts */
  1404. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1405. /* Enable CRYP */
  1406. __HAL_CRYP_ENABLE(hcryp);
  1407. /* Return function status */
  1408. return HAL_OK;
  1409. }
  1410. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1411. {
  1412. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1413. /* Write the Input block in the IN FIFO */
  1414. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1415. inputaddr+=4;
  1416. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1417. inputaddr+=4;
  1418. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1419. inputaddr+=4;
  1420. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1421. hcryp->pCrypInBuffPtr += 16;
  1422. hcryp->CrypInCount -= 16;
  1423. if(hcryp->CrypInCount == 0)
  1424. {
  1425. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1426. /* Call the Input data transfer complete callback */
  1427. HAL_CRYP_InCpltCallback(hcryp);
  1428. }
  1429. }
  1430. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1431. {
  1432. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1433. /* Read the Output block from the Output FIFO */
  1434. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1435. outputaddr+=4;
  1436. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1437. outputaddr+=4;
  1438. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1439. outputaddr+=4;
  1440. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1441. hcryp->pCrypOutBuffPtr += 16;
  1442. hcryp->CrypOutCount -= 16;
  1443. if(hcryp->CrypOutCount == 0)
  1444. {
  1445. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1446. /* Process Unlocked */
  1447. __HAL_UNLOCK(hcryp);
  1448. /* Change the CRYP state */
  1449. hcryp->State = HAL_CRYP_STATE_READY;
  1450. /* Call Input transfer complete callback */
  1451. HAL_CRYP_OutCpltCallback(hcryp);
  1452. }
  1453. }
  1454. /* Return function status */
  1455. return HAL_OK;
  1456. }
  1457. /**
  1458. * @brief Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt.
  1459. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1460. * the configuration information for CRYP module
  1461. * @param pCypherData: Pointer to the cyphertext buffer
  1462. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  1463. * @param pPlainData: Pointer to the plaintext buffer
  1464. * @retval HAL status
  1465. */
  1466. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1467. {
  1468. uint32_t inputaddr;
  1469. uint32_t outputaddr;
  1470. if(hcryp->State == HAL_CRYP_STATE_READY)
  1471. {
  1472. /* Process Locked */
  1473. __HAL_LOCK(hcryp);
  1474. /* Get the buffer addresses and sizes */
  1475. hcryp->CrypInCount = Size;
  1476. hcryp->pCrypInBuffPtr = pCypherData;
  1477. hcryp->pCrypOutBuffPtr = pPlainData;
  1478. hcryp->CrypOutCount = Size;
  1479. /* Change the CRYP state */
  1480. hcryp->State = HAL_CRYP_STATE_BUSY;
  1481. /* Check if initialization phase has already been performed */
  1482. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1483. {
  1484. /* Set the key */
  1485. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1486. /* Set the CRYP peripheral in AES CTR mode */
  1487. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
  1488. /* Set the Initialization Vector */
  1489. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1490. /* Flush FIFO */
  1491. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1492. /* Set the phase */
  1493. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1494. }
  1495. /* Enable Interrupts */
  1496. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1497. /* Enable CRYP */
  1498. __HAL_CRYP_ENABLE(hcryp);
  1499. /* Return function status */
  1500. return HAL_OK;
  1501. }
  1502. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1503. {
  1504. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1505. /* Write the Input block in the IN FIFO */
  1506. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1507. inputaddr+=4;
  1508. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1509. inputaddr+=4;
  1510. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1511. inputaddr+=4;
  1512. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1513. hcryp->pCrypInBuffPtr += 16;
  1514. hcryp->CrypInCount -= 16;
  1515. if(hcryp->CrypInCount == 0)
  1516. {
  1517. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1518. /* Call the Input data transfer complete callback */
  1519. HAL_CRYP_InCpltCallback(hcryp);
  1520. }
  1521. }
  1522. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1523. {
  1524. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1525. /* Read the Output block from the Output FIFO */
  1526. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1527. outputaddr+=4;
  1528. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1529. outputaddr+=4;
  1530. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1531. outputaddr+=4;
  1532. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1533. hcryp->pCrypOutBuffPtr += 16;
  1534. hcryp->CrypOutCount -= 16;
  1535. if(hcryp->CrypOutCount == 0)
  1536. {
  1537. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1538. /* Process Unlocked */
  1539. __HAL_UNLOCK(hcryp);
  1540. /* Change the CRYP state */
  1541. hcryp->State = HAL_CRYP_STATE_READY;
  1542. /* Call Input transfer complete callback */
  1543. HAL_CRYP_OutCpltCallback(hcryp);
  1544. }
  1545. }
  1546. /* Return function status */
  1547. return HAL_OK;
  1548. }
  1549. /**
  1550. * @brief Initializes the CRYP peripheral in AES ECB encryption mode using DMA.
  1551. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1552. * the configuration information for CRYP module
  1553. * @param pPlainData: Pointer to the plaintext buffer
  1554. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1555. * @param pCypherData: Pointer to the cyphertext buffer
  1556. * @retval HAL status
  1557. */
  1558. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1559. {
  1560. uint32_t inputaddr;
  1561. uint32_t outputaddr;
  1562. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1563. {
  1564. /* Process Locked */
  1565. __HAL_LOCK(hcryp);
  1566. inputaddr = (uint32_t)pPlainData;
  1567. outputaddr = (uint32_t)pCypherData;
  1568. /* Change the CRYP state */
  1569. hcryp->State = HAL_CRYP_STATE_BUSY;
  1570. /* Check if initialization phase has already been performed */
  1571. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1572. {
  1573. /* Set the key */
  1574. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1575. /* Set the CRYP peripheral in AES ECB mode */
  1576. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB);
  1577. /* Flush FIFO */
  1578. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1579. /* Set the phase */
  1580. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1581. }
  1582. /* Set the input and output addresses and start DMA transfer */
  1583. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1584. /* Process Unlocked */
  1585. __HAL_UNLOCK(hcryp);
  1586. /* Return function status */
  1587. return HAL_OK;
  1588. }
  1589. else
  1590. {
  1591. return HAL_ERROR;
  1592. }
  1593. }
  1594. /**
  1595. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
  1596. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1597. * the configuration information for CRYP module
  1598. * @param pPlainData: Pointer to the plaintext buffer
  1599. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  1600. * @param pCypherData: Pointer to the cyphertext buffer
  1601. * @retval HAL status
  1602. */
  1603. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1604. {
  1605. uint32_t inputaddr;
  1606. uint32_t outputaddr;
  1607. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1608. {
  1609. /* Process Locked */
  1610. __HAL_LOCK(hcryp);
  1611. inputaddr = (uint32_t)pPlainData;
  1612. outputaddr = (uint32_t)pCypherData;
  1613. /* Change the CRYP state */
  1614. hcryp->State = HAL_CRYP_STATE_BUSY;
  1615. /* Check if initialization phase has already been performed */
  1616. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1617. {
  1618. /* Set the key */
  1619. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1620. /* Set the CRYP peripheral in AES ECB mode */
  1621. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC);
  1622. /* Set the Initialization Vector */
  1623. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1624. /* Flush FIFO */
  1625. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1626. /* Set the phase */
  1627. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1628. }
  1629. /* Set the input and output addresses and start DMA transfer */
  1630. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1631. /* Process Unlocked */
  1632. __HAL_UNLOCK(hcryp);
  1633. /* Return function status */
  1634. return HAL_OK;
  1635. }
  1636. else
  1637. {
  1638. return HAL_ERROR;
  1639. }
  1640. }
  1641. /**
  1642. * @brief Initializes the CRYP peripheral in AES CTR encryption mode using DMA.
  1643. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1644. * the configuration information for CRYP module
  1645. * @param pPlainData: Pointer to the plaintext buffer
  1646. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  1647. * @param pCypherData: Pointer to the cyphertext buffer
  1648. * @retval HAL status
  1649. */
  1650. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1651. {
  1652. uint32_t inputaddr;
  1653. uint32_t outputaddr;
  1654. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1655. {
  1656. /* Process Locked */
  1657. __HAL_LOCK(hcryp);
  1658. inputaddr = (uint32_t)pPlainData;
  1659. outputaddr = (uint32_t)pCypherData;
  1660. /* Change the CRYP state */
  1661. hcryp->State = HAL_CRYP_STATE_BUSY;
  1662. /* Check if initialization phase has already been performed */
  1663. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1664. {
  1665. /* Set the key */
  1666. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1667. /* Set the CRYP peripheral in AES ECB mode */
  1668. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR);
  1669. /* Set the Initialization Vector */
  1670. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1671. /* Flush FIFO */
  1672. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1673. /* Set the phase */
  1674. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1675. }
  1676. /* Set the input and output addresses and start DMA transfer */
  1677. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1678. /* Process Unlocked */
  1679. __HAL_UNLOCK(hcryp);
  1680. /* Return function status */
  1681. return HAL_OK;
  1682. }
  1683. else
  1684. {
  1685. return HAL_ERROR;
  1686. }
  1687. }
  1688. /**
  1689. * @brief Initializes the CRYP peripheral in AES ECB decryption mode using DMA.
  1690. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1691. * the configuration information for CRYP module
  1692. * @param pCypherData: Pointer to the cyphertext buffer
  1693. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1694. * @param pPlainData: Pointer to the plaintext buffer
  1695. * @retval HAL status
  1696. */
  1697. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1698. {
  1699. uint32_t tickstart = 0;
  1700. uint32_t inputaddr;
  1701. uint32_t outputaddr;
  1702. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1703. {
  1704. /* Process Locked */
  1705. __HAL_LOCK(hcryp);
  1706. inputaddr = (uint32_t)pCypherData;
  1707. outputaddr = (uint32_t)pPlainData;
  1708. /* Change the CRYP state */
  1709. hcryp->State = HAL_CRYP_STATE_BUSY;
  1710. /* Check if initialization phase has already been performed */
  1711. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1712. {
  1713. /* Set the key */
  1714. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1715. /* Set the CRYP peripheral in AES Key mode */
  1716. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1717. /* Enable CRYP */
  1718. __HAL_CRYP_ENABLE(hcryp);
  1719. /* Get tick */
  1720. tickstart = HAL_GetTick();
  1721. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1722. {
  1723. /* Check for the Timeout */
  1724. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1725. {
  1726. /* Change state */
  1727. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1728. /* Process Unlocked */
  1729. __HAL_UNLOCK(hcryp);
  1730. return HAL_TIMEOUT;
  1731. }
  1732. }
  1733. /* Reset the ALGOMODE bits*/
  1734. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1735. /* Set the CRYP peripheral in AES ECB decryption mode */
  1736. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
  1737. /* Flush FIFO */
  1738. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1739. /* Set the phase */
  1740. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1741. }
  1742. /* Set the input and output addresses and start DMA transfer */
  1743. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1744. /* Process Unlocked */
  1745. __HAL_UNLOCK(hcryp);
  1746. /* Return function status */
  1747. return HAL_OK;
  1748. }
  1749. else
  1750. {
  1751. return HAL_ERROR;
  1752. }
  1753. }
  1754. /**
  1755. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
  1756. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1757. * the configuration information for CRYP module
  1758. * @param pCypherData: Pointer to the cyphertext buffer
  1759. * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
  1760. * @param pPlainData: Pointer to the plaintext buffer
  1761. * @retval HAL status
  1762. */
  1763. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1764. {
  1765. uint32_t tickstart = 0;
  1766. uint32_t inputaddr;
  1767. uint32_t outputaddr;
  1768. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1769. {
  1770. /* Process Locked */
  1771. __HAL_LOCK(hcryp);
  1772. inputaddr = (uint32_t)pCypherData;
  1773. outputaddr = (uint32_t)pPlainData;
  1774. /* Change the CRYP state */
  1775. hcryp->State = HAL_CRYP_STATE_BUSY;
  1776. /* Check if initialization phase has already been performed */
  1777. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1778. {
  1779. /* Set the key */
  1780. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1781. /* Set the CRYP peripheral in AES Key mode */
  1782. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1783. /* Enable CRYP */
  1784. __HAL_CRYP_ENABLE(hcryp);
  1785. /* Get tick */
  1786. tickstart = HAL_GetTick();
  1787. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1788. {
  1789. /* Check for the Timeout */
  1790. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1791. {
  1792. /* Change state */
  1793. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1794. /* Process Unlocked */
  1795. __HAL_UNLOCK(hcryp);
  1796. return HAL_TIMEOUT;
  1797. }
  1798. }
  1799. /* Reset the ALGOMODE bits*/
  1800. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1801. /* Set the CRYP peripheral in AES CBC decryption mode */
  1802. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
  1803. /* Set the Initialization Vector */
  1804. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1805. /* Flush FIFO */
  1806. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1807. /* Set the phase */
  1808. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1809. }
  1810. /* Set the input and output addresses and start DMA transfer */
  1811. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1812. /* Process Unlocked */
  1813. __HAL_UNLOCK(hcryp);
  1814. /* Return function status */
  1815. return HAL_OK;
  1816. }
  1817. else
  1818. {
  1819. return HAL_ERROR;
  1820. }
  1821. }
  1822. /**
  1823. * @brief Initializes the CRYP peripheral in AES CTR decryption mode using DMA.
  1824. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1825. * the configuration information for CRYP module
  1826. * @param pCypherData: Pointer to the cyphertext buffer
  1827. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  1828. * @param pPlainData: Pointer to the plaintext buffer
  1829. * @retval HAL status
  1830. */
  1831. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1832. {
  1833. uint32_t inputaddr;
  1834. uint32_t outputaddr;
  1835. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1836. {
  1837. /* Process Locked */
  1838. __HAL_LOCK(hcryp);
  1839. inputaddr = (uint32_t)pCypherData;
  1840. outputaddr = (uint32_t)pPlainData;
  1841. /* Change the CRYP state */
  1842. hcryp->State = HAL_CRYP_STATE_BUSY;
  1843. /* Check if initialization phase has already been performed */
  1844. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1845. {
  1846. /* Set the key */
  1847. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1848. /* Set the CRYP peripheral in AES CTR mode */
  1849. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
  1850. /* Set the Initialization Vector */
  1851. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1852. /* Flush FIFO */
  1853. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1854. /* Set the phase */
  1855. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1856. }
  1857. /* Set the input and output addresses and start DMA transfer */
  1858. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1859. /* Process Unlocked */
  1860. __HAL_UNLOCK(hcryp);
  1861. /* Return function status */
  1862. return HAL_OK;
  1863. }
  1864. else
  1865. {
  1866. return HAL_ERROR;
  1867. }
  1868. }
  1869. /**
  1870. * @}
  1871. */
  1872. /** @defgroup CRYP_Exported_Functions_Group3 DES processing functions
  1873. * @brief processing functions.
  1874. *
  1875. @verbatim
  1876. ==============================================================================
  1877. ##### DES processing functions #####
  1878. ==============================================================================
  1879. [..] This section provides functions allowing to:
  1880. (+) Encrypt plaintext using DES using ECB or CBC chaining modes
  1881. (+) Decrypt cyphertext using ECB or CBC chaining modes
  1882. [..] Three processing functions are available:
  1883. (+) Polling mode
  1884. (+) Interrupt mode
  1885. (+) DMA mode
  1886. @endverbatim
  1887. * @{
  1888. */
  1889. /**
  1890. * @brief Initializes the CRYP peripheral in DES ECB encryption mode.
  1891. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1892. * the configuration information for CRYP module
  1893. * @param pPlainData: Pointer to the plaintext buffer
  1894. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  1895. * @param pCypherData: Pointer to the cyphertext buffer
  1896. * @param Timeout: Specify Timeout value
  1897. * @retval HAL status
  1898. */
  1899. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  1900. {
  1901. /* Process Locked */
  1902. __HAL_LOCK(hcryp);
  1903. /* Change the CRYP state */
  1904. hcryp->State = HAL_CRYP_STATE_BUSY;
  1905. /* Set CRYP peripheral in DES ECB encryption mode */
  1906. CRYP_SetDESECBMode(hcryp, 0);
  1907. /* Enable CRYP */
  1908. __HAL_CRYP_ENABLE(hcryp);
  1909. /* Write Plain Data and Get Cypher Data */
  1910. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  1911. {
  1912. return HAL_TIMEOUT;
  1913. }
  1914. /* Change the CRYP state */
  1915. hcryp->State = HAL_CRYP_STATE_READY;
  1916. /* Process Unlocked */
  1917. __HAL_UNLOCK(hcryp);
  1918. /* Return function status */
  1919. return HAL_OK;
  1920. }
  1921. /**
  1922. * @brief Initializes the CRYP peripheral in DES ECB decryption mode.
  1923. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1924. * the configuration information for CRYP module
  1925. * @param pCypherData: Pointer to the cyphertext buffer
  1926. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  1927. * @param pPlainData: Pointer to the plaintext buffer
  1928. * @param Timeout: Specify Timeout value
  1929. * @retval HAL status
  1930. */
  1931. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  1932. {
  1933. /* Process Locked */
  1934. __HAL_LOCK(hcryp);
  1935. /* Change the CRYP state */
  1936. hcryp->State = HAL_CRYP_STATE_BUSY;
  1937. /* Set CRYP peripheral in DES ECB decryption mode */
  1938. CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
  1939. /* Enable CRYP */
  1940. __HAL_CRYP_ENABLE(hcryp);
  1941. /* Write Plain Data and Get Cypher Data */
  1942. if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  1943. {
  1944. return HAL_TIMEOUT;
  1945. }
  1946. /* Change the CRYP state */
  1947. hcryp->State = HAL_CRYP_STATE_READY;
  1948. /* Process Unlocked */
  1949. __HAL_UNLOCK(hcryp);
  1950. /* Return function status */
  1951. return HAL_OK;
  1952. }
  1953. /**
  1954. * @brief Initializes the CRYP peripheral in DES CBC encryption mode.
  1955. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1956. * the configuration information for CRYP module
  1957. * @param pPlainData: Pointer to the plaintext buffer
  1958. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  1959. * @param pCypherData: Pointer to the cyphertext buffer
  1960. * @param Timeout: Specify Timeout value
  1961. * @retval HAL status
  1962. */
  1963. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  1964. {
  1965. /* Process Locked */
  1966. __HAL_LOCK(hcryp);
  1967. /* Change the CRYP state */
  1968. hcryp->State = HAL_CRYP_STATE_BUSY;
  1969. /* Set CRYP peripheral in DES CBC encryption mode */
  1970. CRYP_SetDESCBCMode(hcryp, 0);
  1971. /* Enable CRYP */
  1972. __HAL_CRYP_ENABLE(hcryp);
  1973. /* Write Plain Data and Get Cypher Data */
  1974. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  1975. {
  1976. return HAL_TIMEOUT;
  1977. }
  1978. /* Change the CRYP state */
  1979. hcryp->State = HAL_CRYP_STATE_READY;
  1980. /* Process Unlocked */
  1981. __HAL_UNLOCK(hcryp);
  1982. /* Return function status */
  1983. return HAL_OK;
  1984. }
  1985. /**
  1986. * @brief Initializes the CRYP peripheral in DES ECB decryption mode.
  1987. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1988. * the configuration information for CRYP module
  1989. * @param pCypherData: Pointer to the cyphertext buffer
  1990. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  1991. * @param pPlainData: Pointer to the plaintext buffer
  1992. * @param Timeout: Specify Timeout value
  1993. * @retval HAL status
  1994. */
  1995. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  1996. {
  1997. /* Process Locked */
  1998. __HAL_LOCK(hcryp);
  1999. /* Change the CRYP state */
  2000. hcryp->State = HAL_CRYP_STATE_BUSY;
  2001. /* Set CRYP peripheral in DES CBC decryption mode */
  2002. CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2003. /* Enable CRYP */
  2004. __HAL_CRYP_ENABLE(hcryp);
  2005. /* Write Plain Data and Get Cypher Data */
  2006. if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  2007. {
  2008. return HAL_TIMEOUT;
  2009. }
  2010. /* Change the CRYP state */
  2011. hcryp->State = HAL_CRYP_STATE_READY;
  2012. /* Process Unlocked */
  2013. __HAL_UNLOCK(hcryp);
  2014. /* Return function status */
  2015. return HAL_OK;
  2016. }
  2017. /**
  2018. * @brief Initializes the CRYP peripheral in DES ECB encryption mode using IT.
  2019. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2020. * the configuration information for CRYP module
  2021. * @param pPlainData: Pointer to the plaintext buffer
  2022. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2023. * @param pCypherData: Pointer to the cyphertext buffer
  2024. * @retval HAL status
  2025. */
  2026. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2027. {
  2028. uint32_t inputaddr;
  2029. uint32_t outputaddr;
  2030. if(hcryp->State == HAL_CRYP_STATE_READY)
  2031. {
  2032. /* Process Locked */
  2033. __HAL_LOCK(hcryp);
  2034. hcryp->CrypInCount = Size;
  2035. hcryp->pCrypInBuffPtr = pPlainData;
  2036. hcryp->pCrypOutBuffPtr = pCypherData;
  2037. hcryp->CrypOutCount = Size;
  2038. /* Change the CRYP state */
  2039. hcryp->State = HAL_CRYP_STATE_BUSY;
  2040. /* Set CRYP peripheral in DES ECB encryption mode */
  2041. CRYP_SetDESECBMode(hcryp, 0);
  2042. /* Enable Interrupts */
  2043. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2044. /* Enable CRYP */
  2045. __HAL_CRYP_ENABLE(hcryp);
  2046. /* Return function status */
  2047. return HAL_OK;
  2048. }
  2049. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2050. {
  2051. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2052. /* Write the Input block in the IN FIFO */
  2053. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2054. inputaddr+=4;
  2055. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2056. hcryp->pCrypInBuffPtr += 8;
  2057. hcryp->CrypInCount -= 8;
  2058. if(hcryp->CrypInCount == 0)
  2059. {
  2060. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2061. /* Call the Input data transfer complete callback */
  2062. HAL_CRYP_InCpltCallback(hcryp);
  2063. }
  2064. }
  2065. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2066. {
  2067. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2068. /* Read the Output block from the Output FIFO */
  2069. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2070. outputaddr+=4;
  2071. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2072. hcryp->pCrypOutBuffPtr += 8;
  2073. hcryp->CrypOutCount -= 8;
  2074. if(hcryp->CrypOutCount == 0)
  2075. {
  2076. /* Disable IT */
  2077. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2078. /* Disable CRYP */
  2079. __HAL_CRYP_DISABLE(hcryp);
  2080. /* Process Unlocked */
  2081. __HAL_UNLOCK(hcryp);
  2082. /* Change the CRYP state */
  2083. hcryp->State = HAL_CRYP_STATE_READY;
  2084. /* Call Input transfer complete callback */
  2085. HAL_CRYP_OutCpltCallback(hcryp);
  2086. }
  2087. }
  2088. /* Return function status */
  2089. return HAL_OK;
  2090. }
  2091. /**
  2092. * @brief Initializes the CRYP peripheral in DES CBC encryption mode using interrupt.
  2093. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2094. * the configuration information for CRYP module
  2095. * @param pPlainData: Pointer to the plaintext buffer
  2096. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2097. * @param pCypherData: Pointer to the cyphertext buffer
  2098. * @retval HAL status
  2099. */
  2100. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2101. {
  2102. uint32_t inputaddr;
  2103. uint32_t outputaddr;
  2104. if(hcryp->State == HAL_CRYP_STATE_READY)
  2105. {
  2106. /* Process Locked */
  2107. __HAL_LOCK(hcryp);
  2108. hcryp->CrypInCount = Size;
  2109. hcryp->pCrypInBuffPtr = pPlainData;
  2110. hcryp->pCrypOutBuffPtr = pCypherData;
  2111. hcryp->CrypOutCount = Size;
  2112. /* Change the CRYP state */
  2113. hcryp->State = HAL_CRYP_STATE_BUSY;
  2114. /* Set CRYP peripheral in DES CBC encryption mode */
  2115. CRYP_SetDESCBCMode(hcryp, 0);
  2116. /* Enable Interrupts */
  2117. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2118. /* Enable CRYP */
  2119. __HAL_CRYP_ENABLE(hcryp);
  2120. /* Return function status */
  2121. return HAL_OK;
  2122. }
  2123. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2124. {
  2125. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2126. /* Write the Input block in the IN FIFO */
  2127. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2128. inputaddr+=4;
  2129. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2130. hcryp->pCrypInBuffPtr += 8;
  2131. hcryp->CrypInCount -= 8;
  2132. if(hcryp->CrypInCount == 0)
  2133. {
  2134. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2135. /* Call the Input data transfer complete callback */
  2136. HAL_CRYP_InCpltCallback(hcryp);
  2137. }
  2138. }
  2139. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2140. {
  2141. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2142. /* Read the Output block from the Output FIFO */
  2143. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2144. outputaddr+=4;
  2145. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2146. hcryp->pCrypOutBuffPtr += 8;
  2147. hcryp->CrypOutCount -= 8;
  2148. if(hcryp->CrypOutCount == 0)
  2149. {
  2150. /* Disable IT */
  2151. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2152. /* Disable CRYP */
  2153. __HAL_CRYP_DISABLE(hcryp);
  2154. /* Process Unlocked */
  2155. __HAL_UNLOCK(hcryp);
  2156. /* Change the CRYP state */
  2157. hcryp->State = HAL_CRYP_STATE_READY;
  2158. /* Call Input transfer complete callback */
  2159. HAL_CRYP_OutCpltCallback(hcryp);
  2160. }
  2161. }
  2162. /* Return function status */
  2163. return HAL_OK;
  2164. }
  2165. /**
  2166. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using IT.
  2167. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2168. * the configuration information for CRYP module
  2169. * @param pPlainData: Pointer to the plaintext buffer
  2170. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2171. * @param pCypherData: Pointer to the cyphertext buffer
  2172. * @retval HAL status
  2173. */
  2174. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2175. {
  2176. uint32_t inputaddr;
  2177. uint32_t outputaddr;
  2178. if(hcryp->State == HAL_CRYP_STATE_READY)
  2179. {
  2180. /* Process Locked */
  2181. __HAL_LOCK(hcryp);
  2182. hcryp->CrypInCount = Size;
  2183. hcryp->pCrypInBuffPtr = pCypherData;
  2184. hcryp->pCrypOutBuffPtr = pPlainData;
  2185. hcryp->CrypOutCount = Size;
  2186. /* Change the CRYP state */
  2187. hcryp->State = HAL_CRYP_STATE_BUSY;
  2188. /* Set CRYP peripheral in DES ECB decryption mode */
  2189. CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2190. /* Enable Interrupts */
  2191. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2192. /* Enable CRYP */
  2193. __HAL_CRYP_ENABLE(hcryp);
  2194. /* Return function status */
  2195. return HAL_OK;
  2196. }
  2197. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2198. {
  2199. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2200. /* Write the Input block in the IN FIFO */
  2201. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2202. inputaddr+=4;
  2203. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2204. hcryp->pCrypInBuffPtr += 8;
  2205. hcryp->CrypInCount -= 8;
  2206. if(hcryp->CrypInCount == 0)
  2207. {
  2208. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2209. /* Call the Input data transfer complete callback */
  2210. HAL_CRYP_InCpltCallback(hcryp);
  2211. }
  2212. }
  2213. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2214. {
  2215. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2216. /* Read the Output block from the Output FIFO */
  2217. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2218. outputaddr+=4;
  2219. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2220. hcryp->pCrypOutBuffPtr += 8;
  2221. hcryp->CrypOutCount -= 8;
  2222. if(hcryp->CrypOutCount == 0)
  2223. {
  2224. /* Disable IT */
  2225. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2226. /* Disable CRYP */
  2227. __HAL_CRYP_DISABLE(hcryp);
  2228. /* Process Unlocked */
  2229. __HAL_UNLOCK(hcryp);
  2230. /* Change the CRYP state */
  2231. hcryp->State = HAL_CRYP_STATE_READY;
  2232. /* Call Input transfer complete callback */
  2233. HAL_CRYP_OutCpltCallback(hcryp);
  2234. }
  2235. }
  2236. /* Return function status */
  2237. return HAL_OK;
  2238. }
  2239. /**
  2240. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using interrupt.
  2241. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2242. * the configuration information for CRYP module
  2243. * @param pPlainData: Pointer to the plaintext buffer
  2244. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2245. * @param pCypherData: Pointer to the cyphertext buffer
  2246. * @retval HAL status
  2247. */
  2248. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2249. {
  2250. uint32_t inputaddr;
  2251. uint32_t outputaddr;
  2252. if(hcryp->State == HAL_CRYP_STATE_READY)
  2253. {
  2254. /* Process Locked */
  2255. __HAL_LOCK(hcryp);
  2256. hcryp->CrypInCount = Size;
  2257. hcryp->pCrypInBuffPtr = pCypherData;
  2258. hcryp->pCrypOutBuffPtr = pPlainData;
  2259. hcryp->CrypOutCount = Size;
  2260. /* Change the CRYP state */
  2261. hcryp->State = HAL_CRYP_STATE_BUSY;
  2262. /* Set CRYP peripheral in DES CBC decryption mode */
  2263. CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2264. /* Enable Interrupts */
  2265. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2266. /* Enable CRYP */
  2267. __HAL_CRYP_ENABLE(hcryp);
  2268. /* Return function status */
  2269. return HAL_OK;
  2270. }
  2271. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2272. {
  2273. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2274. /* Write the Input block in the IN FIFO */
  2275. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2276. inputaddr+=4;
  2277. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2278. hcryp->pCrypInBuffPtr += 8;
  2279. hcryp->CrypInCount -= 8;
  2280. if(hcryp->CrypInCount == 0)
  2281. {
  2282. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2283. /* Call the Input data transfer complete callback */
  2284. HAL_CRYP_InCpltCallback(hcryp);
  2285. }
  2286. }
  2287. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2288. {
  2289. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2290. /* Read the Output block from the Output FIFO */
  2291. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2292. outputaddr+=4;
  2293. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2294. hcryp->pCrypOutBuffPtr += 8;
  2295. hcryp->CrypOutCount -= 8;
  2296. if(hcryp->CrypOutCount == 0)
  2297. {
  2298. /* Disable IT */
  2299. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2300. /* Disable CRYP */
  2301. __HAL_CRYP_DISABLE(hcryp);
  2302. /* Process Unlocked */
  2303. __HAL_UNLOCK(hcryp);
  2304. /* Change the CRYP state */
  2305. hcryp->State = HAL_CRYP_STATE_READY;
  2306. /* Call Input transfer complete callback */
  2307. HAL_CRYP_OutCpltCallback(hcryp);
  2308. }
  2309. }
  2310. /* Return function status */
  2311. return HAL_OK;
  2312. }
  2313. /**
  2314. * @brief Initializes the CRYP peripheral in DES ECB encryption mode using DMA.
  2315. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2316. * the configuration information for CRYP module
  2317. * @param pPlainData: Pointer to the plaintext buffer
  2318. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2319. * @param pCypherData: Pointer to the cyphertext buffer
  2320. * @retval HAL status
  2321. */
  2322. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2323. {
  2324. uint32_t inputaddr;
  2325. uint32_t outputaddr;
  2326. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2327. {
  2328. /* Process Locked */
  2329. __HAL_LOCK(hcryp);
  2330. inputaddr = (uint32_t)pPlainData;
  2331. outputaddr = (uint32_t)pCypherData;
  2332. /* Change the CRYP state */
  2333. hcryp->State = HAL_CRYP_STATE_BUSY;
  2334. /* Set CRYP peripheral in DES ECB encryption mode */
  2335. CRYP_SetDESECBMode(hcryp, 0);
  2336. /* Set the input and output addresses and start DMA transfer */
  2337. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2338. /* Process Unlocked */
  2339. __HAL_UNLOCK(hcryp);
  2340. /* Return function status */
  2341. return HAL_OK;
  2342. }
  2343. else
  2344. {
  2345. return HAL_ERROR;
  2346. }
  2347. }
  2348. /**
  2349. * @brief Initializes the CRYP peripheral in DES CBC encryption mode using DMA.
  2350. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2351. * the configuration information for CRYP module
  2352. * @param pPlainData: Pointer to the plaintext buffer
  2353. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2354. * @param pCypherData: Pointer to the cyphertext buffer
  2355. * @retval HAL status
  2356. */
  2357. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2358. {
  2359. uint32_t inputaddr;
  2360. uint32_t outputaddr;
  2361. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2362. {
  2363. /* Process Locked */
  2364. __HAL_LOCK(hcryp);
  2365. inputaddr = (uint32_t)pPlainData;
  2366. outputaddr = (uint32_t)pCypherData;
  2367. /* Change the CRYP state */
  2368. hcryp->State = HAL_CRYP_STATE_BUSY;
  2369. /* Set CRYP peripheral in DES CBC encryption mode */
  2370. CRYP_SetDESCBCMode(hcryp, 0);
  2371. /* Set the input and output addresses and start DMA transfer */
  2372. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2373. /* Process Unlocked */
  2374. __HAL_UNLOCK(hcryp);
  2375. /* Return function status */
  2376. return HAL_OK;
  2377. }
  2378. else
  2379. {
  2380. return HAL_ERROR;
  2381. }
  2382. }
  2383. /**
  2384. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA.
  2385. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2386. * the configuration information for CRYP module
  2387. * @param pPlainData: Pointer to the plaintext buffer
  2388. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2389. * @param pCypherData: Pointer to the cyphertext buffer
  2390. * @retval HAL status
  2391. */
  2392. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2393. {
  2394. uint32_t inputaddr;
  2395. uint32_t outputaddr;
  2396. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2397. {
  2398. /* Process Locked */
  2399. __HAL_LOCK(hcryp);
  2400. inputaddr = (uint32_t)pCypherData;
  2401. outputaddr = (uint32_t)pPlainData;
  2402. /* Change the CRYP state */
  2403. hcryp->State = HAL_CRYP_STATE_BUSY;
  2404. /* Set CRYP peripheral in DES ECB decryption mode */
  2405. CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2406. /* Set the input and output addresses and start DMA transfer */
  2407. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2408. /* Process Unlocked */
  2409. __HAL_UNLOCK(hcryp);
  2410. /* Return function status */
  2411. return HAL_OK;
  2412. }
  2413. else
  2414. {
  2415. return HAL_ERROR;
  2416. }
  2417. }
  2418. /**
  2419. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA.
  2420. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2421. * the configuration information for CRYP module
  2422. * @param pPlainData: Pointer to the plaintext buffer
  2423. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2424. * @param pCypherData: Pointer to the cyphertext buffer
  2425. * @retval HAL status
  2426. */
  2427. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2428. {
  2429. uint32_t inputaddr;
  2430. uint32_t outputaddr;
  2431. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2432. {
  2433. /* Process Locked */
  2434. __HAL_LOCK(hcryp);
  2435. inputaddr = (uint32_t)pCypherData;
  2436. outputaddr = (uint32_t)pPlainData;
  2437. /* Change the CRYP state */
  2438. hcryp->State = HAL_CRYP_STATE_BUSY;
  2439. /* Set CRYP peripheral in DES CBC decryption mode */
  2440. CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2441. /* Set the input and output addresses and start DMA transfer */
  2442. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2443. /* Process Unlocked */
  2444. __HAL_UNLOCK(hcryp);
  2445. /* Return function status */
  2446. return HAL_OK;
  2447. }
  2448. else
  2449. {
  2450. return HAL_ERROR;
  2451. }
  2452. }
  2453. /**
  2454. * @}
  2455. */
  2456. /** @defgroup CRYP_Exported_Functions_Group4 TDES processing functions
  2457. * @brief processing functions.
  2458. *
  2459. @verbatim
  2460. ==============================================================================
  2461. ##### TDES processing functions #####
  2462. ==============================================================================
  2463. [..] This section provides functions allowing to:
  2464. (+) Encrypt plaintext using TDES based on ECB or CBC chaining modes
  2465. (+) Decrypt cyphertext using TDES based on ECB or CBC chaining modes
  2466. [..] Three processing functions are available:
  2467. (+) Polling mode
  2468. (+) Interrupt mode
  2469. (+) DMA mode
  2470. @endverbatim
  2471. * @{
  2472. */
  2473. /**
  2474. * @brief Initializes the CRYP peripheral in TDES ECB encryption mode
  2475. * then encrypt pPlainData. The cypher data are available in pCypherData
  2476. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2477. * the configuration information for CRYP module
  2478. * @param pPlainData: Pointer to the plaintext buffer
  2479. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2480. * @param pCypherData: Pointer to the cyphertext buffer
  2481. * @param Timeout: Specify Timeout value
  2482. * @retval HAL status
  2483. */
  2484. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  2485. {
  2486. /* Process Locked */
  2487. __HAL_LOCK(hcryp);
  2488. /* Change the CRYP state */
  2489. hcryp->State = HAL_CRYP_STATE_BUSY;
  2490. /* Set CRYP peripheral in TDES ECB encryption mode */
  2491. CRYP_SetTDESECBMode(hcryp, 0);
  2492. /* Enable CRYP */
  2493. __HAL_CRYP_ENABLE(hcryp);
  2494. /* Write Plain Data and Get Cypher Data */
  2495. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  2496. {
  2497. return HAL_TIMEOUT;
  2498. }
  2499. /* Change the CRYP state */
  2500. hcryp->State = HAL_CRYP_STATE_READY;
  2501. /* Process Unlocked */
  2502. __HAL_UNLOCK(hcryp);
  2503. /* Return function status */
  2504. return HAL_OK;
  2505. }
  2506. /**
  2507. * @brief Initializes the CRYP peripheral in TDES ECB decryption mode
  2508. * then decrypted pCypherData. The cypher data are available in pPlainData
  2509. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2510. * the configuration information for CRYP module
  2511. * @param pPlainData: Pointer to the plaintext buffer
  2512. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2513. * @param pCypherData: Pointer to the cyphertext buffer
  2514. * @param Timeout: Specify Timeout value
  2515. * @retval HAL status
  2516. */
  2517. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  2518. {
  2519. /* Process Locked */
  2520. __HAL_LOCK(hcryp);
  2521. /* Change the CRYP state */
  2522. hcryp->State = HAL_CRYP_STATE_BUSY;
  2523. /* Set CRYP peripheral in TDES ECB decryption mode */
  2524. CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2525. /* Enable CRYP */
  2526. __HAL_CRYP_ENABLE(hcryp);
  2527. /* Write Cypher Data and Get Plain Data */
  2528. if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  2529. {
  2530. return HAL_TIMEOUT;
  2531. }
  2532. /* Change the CRYP state */
  2533. hcryp->State = HAL_CRYP_STATE_READY;
  2534. /* Process Unlocked */
  2535. __HAL_UNLOCK(hcryp);
  2536. /* Return function status */
  2537. return HAL_OK;
  2538. }
  2539. /**
  2540. * @brief Initializes the CRYP peripheral in TDES CBC encryption mode
  2541. * then encrypt pPlainData. The cypher data are available in pCypherData
  2542. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2543. * the configuration information for CRYP module
  2544. * @param pPlainData: Pointer to the plaintext buffer
  2545. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2546. * @param pCypherData: Pointer to the cyphertext buffer
  2547. * @param Timeout: Specify Timeout value
  2548. * @retval HAL status
  2549. */
  2550. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  2551. {
  2552. /* Process Locked */
  2553. __HAL_LOCK(hcryp);
  2554. /* Change the CRYP state */
  2555. hcryp->State = HAL_CRYP_STATE_BUSY;
  2556. /* Set CRYP peripheral in TDES CBC encryption mode */
  2557. CRYP_SetTDESCBCMode(hcryp, 0);
  2558. /* Enable CRYP */
  2559. __HAL_CRYP_ENABLE(hcryp);
  2560. /* Write Plain Data and Get Cypher Data */
  2561. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  2562. {
  2563. return HAL_TIMEOUT;
  2564. }
  2565. /* Change the CRYP state */
  2566. hcryp->State = HAL_CRYP_STATE_READY;
  2567. /* Process Unlocked */
  2568. __HAL_UNLOCK(hcryp);
  2569. /* Return function status */
  2570. return HAL_OK;
  2571. }
  2572. /**
  2573. * @brief Initializes the CRYP peripheral in TDES CBC decryption mode
  2574. * then decrypted pCypherData. The cypher data are available in pPlainData
  2575. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2576. * the configuration information for CRYP module
  2577. * @param pCypherData: Pointer to the cyphertext buffer
  2578. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2579. * @param pPlainData: Pointer to the plaintext buffer
  2580. * @param Timeout: Specify Timeout value
  2581. * @retval HAL status
  2582. */
  2583. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  2584. {
  2585. /* Process Locked */
  2586. __HAL_LOCK(hcryp);
  2587. /* Change the CRYP state */
  2588. hcryp->State = HAL_CRYP_STATE_BUSY;
  2589. /* Set CRYP peripheral in TDES CBC decryption mode */
  2590. CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2591. /* Enable CRYP */
  2592. __HAL_CRYP_ENABLE(hcryp);
  2593. /* Write Cypher Data and Get Plain Data */
  2594. if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  2595. {
  2596. return HAL_TIMEOUT;
  2597. }
  2598. /* Change the CRYP state */
  2599. hcryp->State = HAL_CRYP_STATE_READY;
  2600. /* Process Unlocked */
  2601. __HAL_UNLOCK(hcryp);
  2602. /* Return function status */
  2603. return HAL_OK;
  2604. }
  2605. /**
  2606. * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using interrupt.
  2607. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2608. * the configuration information for CRYP module
  2609. * @param pPlainData: Pointer to the plaintext buffer
  2610. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2611. * @param pCypherData: Pointer to the cyphertext buffer
  2612. * @retval HAL status
  2613. */
  2614. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2615. {
  2616. uint32_t inputaddr;
  2617. uint32_t outputaddr;
  2618. if(hcryp->State == HAL_CRYP_STATE_READY)
  2619. {
  2620. /* Process Locked */
  2621. __HAL_LOCK(hcryp);
  2622. hcryp->CrypInCount = Size;
  2623. hcryp->pCrypInBuffPtr = pPlainData;
  2624. hcryp->pCrypOutBuffPtr = pCypherData;
  2625. hcryp->CrypOutCount = Size;
  2626. /* Change the CRYP state */
  2627. hcryp->State = HAL_CRYP_STATE_BUSY;
  2628. /* Set CRYP peripheral in TDES ECB encryption mode */
  2629. CRYP_SetTDESECBMode(hcryp, 0);
  2630. /* Enable Interrupts */
  2631. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2632. /* Enable CRYP */
  2633. __HAL_CRYP_ENABLE(hcryp);
  2634. /* Return function status */
  2635. return HAL_OK;
  2636. }
  2637. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2638. {
  2639. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2640. /* Write the Input block in the IN FIFO */
  2641. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2642. inputaddr+=4;
  2643. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2644. hcryp->pCrypInBuffPtr += 8;
  2645. hcryp->CrypInCount -= 8;
  2646. if(hcryp->CrypInCount == 0)
  2647. {
  2648. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2649. /* Call the Input data transfer complete callback */
  2650. HAL_CRYP_InCpltCallback(hcryp);
  2651. }
  2652. }
  2653. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2654. {
  2655. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2656. /* Read the Output block from the Output FIFO */
  2657. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2658. outputaddr+=4;
  2659. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2660. hcryp->pCrypOutBuffPtr += 8;
  2661. hcryp->CrypOutCount -= 8;
  2662. if(hcryp->CrypOutCount == 0)
  2663. {
  2664. /* Disable IT */
  2665. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2666. /* Disable CRYP */
  2667. __HAL_CRYP_DISABLE(hcryp);
  2668. /* Process Unlocked */
  2669. __HAL_UNLOCK(hcryp);
  2670. /* Change the CRYP state */
  2671. hcryp->State = HAL_CRYP_STATE_READY;
  2672. /* Call the Output data transfer complete callback */
  2673. HAL_CRYP_OutCpltCallback(hcryp);
  2674. }
  2675. }
  2676. /* Return function status */
  2677. return HAL_OK;
  2678. }
  2679. /**
  2680. * @brief Initializes the CRYP peripheral in TDES CBC encryption mode.
  2681. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2682. * the configuration information for CRYP module
  2683. * @param pPlainData: Pointer to the plaintext buffer
  2684. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2685. * @param pCypherData: Pointer to the cyphertext buffer
  2686. * @retval HAL status
  2687. */
  2688. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2689. {
  2690. uint32_t inputaddr;
  2691. uint32_t outputaddr;
  2692. if(hcryp->State == HAL_CRYP_STATE_READY)
  2693. {
  2694. /* Process Locked */
  2695. __HAL_LOCK(hcryp);
  2696. hcryp->CrypInCount = Size;
  2697. hcryp->pCrypInBuffPtr = pPlainData;
  2698. hcryp->pCrypOutBuffPtr = pCypherData;
  2699. hcryp->CrypOutCount = Size;
  2700. /* Change the CRYP state */
  2701. hcryp->State = HAL_CRYP_STATE_BUSY;
  2702. /* Set CRYP peripheral in TDES CBC encryption mode */
  2703. CRYP_SetTDESCBCMode(hcryp, 0);
  2704. /* Enable Interrupts */
  2705. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2706. /* Enable CRYP */
  2707. __HAL_CRYP_ENABLE(hcryp);
  2708. /* Return function status */
  2709. return HAL_OK;
  2710. }
  2711. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2712. {
  2713. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2714. /* Write the Input block in the IN FIFO */
  2715. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2716. inputaddr+=4;
  2717. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2718. hcryp->pCrypInBuffPtr += 8;
  2719. hcryp->CrypInCount -= 8;
  2720. if(hcryp->CrypInCount == 0)
  2721. {
  2722. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2723. /* Call the Input data transfer complete callback */
  2724. HAL_CRYP_InCpltCallback(hcryp);
  2725. }
  2726. }
  2727. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2728. {
  2729. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2730. /* Read the Output block from the Output FIFO */
  2731. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2732. outputaddr+=4;
  2733. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2734. hcryp->pCrypOutBuffPtr += 8;
  2735. hcryp->CrypOutCount -= 8;
  2736. if(hcryp->CrypOutCount == 0)
  2737. {
  2738. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2739. /* Disable CRYP */
  2740. __HAL_CRYP_DISABLE(hcryp);
  2741. /* Process Unlocked */
  2742. __HAL_UNLOCK(hcryp);
  2743. /* Change the CRYP state */
  2744. hcryp->State = HAL_CRYP_STATE_READY;
  2745. /* Call Input transfer complete callback */
  2746. HAL_CRYP_OutCpltCallback(hcryp);
  2747. }
  2748. }
  2749. /* Return function status */
  2750. return HAL_OK;
  2751. }
  2752. /**
  2753. * @brief Initializes the CRYP peripheral in TDES ECB decryption mode.
  2754. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2755. * the configuration information for CRYP module
  2756. * @param pPlainData: Pointer to the plaintext buffer
  2757. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2758. * @param pCypherData: Pointer to the cyphertext buffer
  2759. * @retval HAL status
  2760. */
  2761. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2762. {
  2763. uint32_t inputaddr;
  2764. uint32_t outputaddr;
  2765. if(hcryp->State == HAL_CRYP_STATE_READY)
  2766. {
  2767. /* Process Locked */
  2768. __HAL_LOCK(hcryp);
  2769. hcryp->CrypInCount = Size;
  2770. hcryp->pCrypInBuffPtr = pCypherData;
  2771. hcryp->pCrypOutBuffPtr = pPlainData;
  2772. hcryp->CrypOutCount = Size;
  2773. /* Change the CRYP state */
  2774. hcryp->State = HAL_CRYP_STATE_BUSY;
  2775. /* Set CRYP peripheral in TDES ECB decryption mode */
  2776. CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2777. /* Enable Interrupts */
  2778. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2779. /* Enable CRYP */
  2780. __HAL_CRYP_ENABLE(hcryp);
  2781. /* Return function status */
  2782. return HAL_OK;
  2783. }
  2784. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2785. {
  2786. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2787. /* Write the Input block in the IN FIFO */
  2788. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2789. inputaddr+=4;
  2790. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2791. hcryp->pCrypInBuffPtr += 8;
  2792. hcryp->CrypInCount -= 8;
  2793. if(hcryp->CrypInCount == 0)
  2794. {
  2795. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2796. /* Call the Input data transfer complete callback */
  2797. HAL_CRYP_InCpltCallback(hcryp);
  2798. }
  2799. }
  2800. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2801. {
  2802. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2803. /* Read the Output block from the Output FIFO */
  2804. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2805. outputaddr+=4;
  2806. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2807. hcryp->pCrypOutBuffPtr += 8;
  2808. hcryp->CrypOutCount -= 8;
  2809. if(hcryp->CrypOutCount == 0)
  2810. {
  2811. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2812. /* Disable CRYP */
  2813. __HAL_CRYP_DISABLE(hcryp);
  2814. /* Process Unlocked */
  2815. __HAL_UNLOCK(hcryp);
  2816. /* Change the CRYP state */
  2817. hcryp->State = HAL_CRYP_STATE_READY;
  2818. /* Call Input transfer complete callback */
  2819. HAL_CRYP_OutCpltCallback(hcryp);
  2820. }
  2821. }
  2822. /* Return function status */
  2823. return HAL_OK;
  2824. }
  2825. /**
  2826. * @brief Initializes the CRYP peripheral in TDES CBC decryption mode.
  2827. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2828. * the configuration information for CRYP module
  2829. * @param pCypherData: Pointer to the cyphertext buffer
  2830. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2831. * @param pPlainData: Pointer to the plaintext buffer
  2832. * @retval HAL status
  2833. */
  2834. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2835. {
  2836. uint32_t inputaddr;
  2837. uint32_t outputaddr;
  2838. if(hcryp->State == HAL_CRYP_STATE_READY)
  2839. {
  2840. /* Process Locked */
  2841. __HAL_LOCK(hcryp);
  2842. hcryp->CrypInCount = Size;
  2843. hcryp->pCrypInBuffPtr = pCypherData;
  2844. hcryp->pCrypOutBuffPtr = pPlainData;
  2845. hcryp->CrypOutCount = Size;
  2846. /* Change the CRYP state */
  2847. hcryp->State = HAL_CRYP_STATE_BUSY;
  2848. /* Set CRYP peripheral in TDES CBC decryption mode */
  2849. CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2850. /* Enable Interrupts */
  2851. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2852. /* Enable CRYP */
  2853. __HAL_CRYP_ENABLE(hcryp);
  2854. /* Return function status */
  2855. return HAL_OK;
  2856. }
  2857. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2858. {
  2859. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2860. /* Write the Input block in the IN FIFO */
  2861. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2862. inputaddr+=4;
  2863. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2864. hcryp->pCrypInBuffPtr += 8;
  2865. hcryp->CrypInCount -= 8;
  2866. if(hcryp->CrypInCount == 0)
  2867. {
  2868. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2869. /* Call the Input data transfer complete callback */
  2870. HAL_CRYP_InCpltCallback(hcryp);
  2871. }
  2872. }
  2873. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2874. {
  2875. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2876. /* Read the Output block from the Output FIFO */
  2877. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2878. outputaddr+=4;
  2879. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2880. hcryp->pCrypOutBuffPtr += 8;
  2881. hcryp->CrypOutCount -= 8;
  2882. if(hcryp->CrypOutCount == 0)
  2883. {
  2884. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2885. /* Disable CRYP */
  2886. __HAL_CRYP_DISABLE(hcryp);
  2887. /* Process Unlocked */
  2888. __HAL_UNLOCK(hcryp);
  2889. /* Change the CRYP state */
  2890. hcryp->State = HAL_CRYP_STATE_READY;
  2891. /* Call Input transfer complete callback */
  2892. HAL_CRYP_OutCpltCallback(hcryp);
  2893. }
  2894. }
  2895. /* Return function status */
  2896. return HAL_OK;
  2897. }
  2898. /**
  2899. * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using DMA.
  2900. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2901. * the configuration information for CRYP module
  2902. * @param pPlainData: Pointer to the plaintext buffer
  2903. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2904. * @param pCypherData: Pointer to the cyphertext buffer
  2905. * @retval HAL status
  2906. */
  2907. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2908. {
  2909. uint32_t inputaddr;
  2910. uint32_t outputaddr;
  2911. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2912. {
  2913. /* Process Locked */
  2914. __HAL_LOCK(hcryp);
  2915. inputaddr = (uint32_t)pPlainData;
  2916. outputaddr = (uint32_t)pCypherData;
  2917. /* Change the CRYP state */
  2918. hcryp->State = HAL_CRYP_STATE_BUSY;
  2919. /* Set CRYP peripheral in TDES ECB encryption mode */
  2920. CRYP_SetTDESECBMode(hcryp, 0);
  2921. /* Set the input and output addresses and start DMA transfer */
  2922. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2923. /* Process Unlocked */
  2924. __HAL_UNLOCK(hcryp);
  2925. /* Return function status */
  2926. return HAL_OK;
  2927. }
  2928. else
  2929. {
  2930. return HAL_ERROR;
  2931. }
  2932. }
  2933. /**
  2934. * @brief Initializes the CRYP peripheral in TDES CBC encryption mode using DMA.
  2935. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2936. * the configuration information for CRYP module
  2937. * @param pPlainData: Pointer to the plaintext buffer
  2938. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2939. * @param pCypherData: Pointer to the cyphertext buffer
  2940. * @retval HAL status
  2941. */
  2942. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2943. {
  2944. uint32_t inputaddr;
  2945. uint32_t outputaddr;
  2946. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2947. {
  2948. /* Process Locked */
  2949. __HAL_LOCK(hcryp);
  2950. inputaddr = (uint32_t)pPlainData;
  2951. outputaddr = (uint32_t)pCypherData;
  2952. /* Change the CRYP state */
  2953. hcryp->State = HAL_CRYP_STATE_BUSY;
  2954. /* Set CRYP peripheral in TDES CBC encryption mode */
  2955. CRYP_SetTDESCBCMode(hcryp, 0);
  2956. /* Set the input and output addresses and start DMA transfer */
  2957. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2958. /* Process Unlocked */
  2959. __HAL_UNLOCK(hcryp);
  2960. /* Return function status */
  2961. return HAL_OK;
  2962. }
  2963. else
  2964. {
  2965. return HAL_ERROR;
  2966. }
  2967. }
  2968. /**
  2969. * @brief Initializes the CRYP peripheral in TDES ECB decryption mode using DMA.
  2970. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2971. * the configuration information for CRYP module
  2972. * @param pPlainData: Pointer to the plaintext buffer
  2973. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  2974. * @param pCypherData: Pointer to the cyphertext buffer
  2975. * @retval HAL status
  2976. */
  2977. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2978. {
  2979. uint32_t inputaddr;
  2980. uint32_t outputaddr;
  2981. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2982. {
  2983. /* Process Locked */
  2984. __HAL_LOCK(hcryp);
  2985. inputaddr = (uint32_t)pCypherData;
  2986. outputaddr = (uint32_t)pPlainData;
  2987. /* Change the CRYP state */
  2988. hcryp->State = HAL_CRYP_STATE_BUSY;
  2989. /* Set CRYP peripheral in TDES ECB decryption mode */
  2990. CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2991. /* Set the input and output addresses and start DMA transfer */
  2992. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2993. /* Process Unlocked */
  2994. __HAL_UNLOCK(hcryp);
  2995. /* Return function status */
  2996. return HAL_OK;
  2997. }
  2998. else
  2999. {
  3000. return HAL_ERROR;
  3001. }
  3002. }
  3003. /**
  3004. * @brief Initializes the CRYP peripheral in TDES CBC decryption mode using DMA.
  3005. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3006. * the configuration information for CRYP module
  3007. * @param pCypherData: Pointer to the cyphertext buffer
  3008. * @param Size: Length of the plaintext buffer, must be a multiple of 8
  3009. * @param pPlainData: Pointer to the plaintext buffer
  3010. * @retval HAL status
  3011. */
  3012. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3013. {
  3014. uint32_t inputaddr;
  3015. uint32_t outputaddr;
  3016. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  3017. {
  3018. /* Process Locked */
  3019. __HAL_LOCK(hcryp);
  3020. inputaddr = (uint32_t)pCypherData;
  3021. outputaddr = (uint32_t)pPlainData;
  3022. /* Change the CRYP state */
  3023. hcryp->State = HAL_CRYP_STATE_BUSY;
  3024. /* Set CRYP peripheral in TDES CBC decryption mode */
  3025. CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  3026. /* Set the input and output addresses and start DMA transfer */
  3027. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  3028. /* Process Unlocked */
  3029. __HAL_UNLOCK(hcryp);
  3030. /* Return function status */
  3031. return HAL_OK;
  3032. }
  3033. else
  3034. {
  3035. return HAL_ERROR;
  3036. }
  3037. }
  3038. /**
  3039. * @}
  3040. */
  3041. /** @defgroup CRYP_Exported_Functions_Group5 DMA callback functions
  3042. * @brief DMA callback functions.
  3043. *
  3044. @verbatim
  3045. ==============================================================================
  3046. ##### DMA callback functions #####
  3047. ==============================================================================
  3048. [..] This section provides DMA callback functions:
  3049. (+) DMA Input data transfer complete
  3050. (+) DMA Output data transfer complete
  3051. (+) DMA error
  3052. @endverbatim
  3053. * @{
  3054. */
  3055. /**
  3056. * @brief Input FIFO transfer completed callbacks.
  3057. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3058. * the configuration information for CRYP module
  3059. * @retval None
  3060. */
  3061. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  3062. {
  3063. /* Prevent unused argument(s) compilation warning */
  3064. UNUSED(hcryp);
  3065. /* NOTE : This function Should not be modified, when the callback is needed,
  3066. the HAL_CRYP_InCpltCallback could be implemented in the user file
  3067. */
  3068. }
  3069. /**
  3070. * @brief Output FIFO transfer completed callbacks.
  3071. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3072. * the configuration information for CRYP module
  3073. * @retval None
  3074. */
  3075. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  3076. {
  3077. /* Prevent unused argument(s) compilation warning */
  3078. UNUSED(hcryp);
  3079. /* NOTE : This function Should not be modified, when the callback is needed,
  3080. the HAL_CRYP_OutCpltCallback could be implemented in the user file
  3081. */
  3082. }
  3083. /**
  3084. * @brief CRYP error callbacks.
  3085. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3086. * the configuration information for CRYP module
  3087. * @retval None
  3088. */
  3089. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  3090. {
  3091. /* Prevent unused argument(s) compilation warning */
  3092. UNUSED(hcryp);
  3093. /* NOTE : This function Should not be modified, when the callback is needed,
  3094. the HAL_CRYP_ErrorCallback could be implemented in the user file
  3095. */
  3096. }
  3097. /**
  3098. * @}
  3099. */
  3100. /** @defgroup CRYP_Exported_Functions_Group6 CRYP IRQ handler management
  3101. * @brief CRYP IRQ handler.
  3102. *
  3103. @verbatim
  3104. ==============================================================================
  3105. ##### CRYP IRQ handler management #####
  3106. ==============================================================================
  3107. [..] This section provides CRYP IRQ handler function.
  3108. @endverbatim
  3109. * @{
  3110. */
  3111. /**
  3112. * @brief This function handles CRYP interrupt request.
  3113. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3114. * the configuration information for CRYP module
  3115. * @retval None
  3116. */
  3117. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  3118. {
  3119. switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION)
  3120. {
  3121. case CRYP_CR_ALGOMODE_TDES_ECB_ENCRYPT:
  3122. HAL_CRYP_TDESECB_Encrypt_IT(hcryp, NULL, 0, NULL);
  3123. break;
  3124. case CRYP_CR_ALGOMODE_TDES_ECB_DECRYPT:
  3125. HAL_CRYP_TDESECB_Decrypt_IT(hcryp, NULL, 0, NULL);
  3126. break;
  3127. case CRYP_CR_ALGOMODE_TDES_CBC_ENCRYPT:
  3128. HAL_CRYP_TDESCBC_Encrypt_IT(hcryp, NULL, 0, NULL);
  3129. break;
  3130. case CRYP_CR_ALGOMODE_TDES_CBC_DECRYPT:
  3131. HAL_CRYP_TDESCBC_Decrypt_IT(hcryp, NULL, 0, NULL);
  3132. break;
  3133. case CRYP_CR_ALGOMODE_DES_ECB_ENCRYPT:
  3134. HAL_CRYP_DESECB_Encrypt_IT(hcryp, NULL, 0, NULL);
  3135. break;
  3136. case CRYP_CR_ALGOMODE_DES_ECB_DECRYPT:
  3137. HAL_CRYP_DESECB_Decrypt_IT(hcryp, NULL, 0, NULL);
  3138. break;
  3139. case CRYP_CR_ALGOMODE_DES_CBC_ENCRYPT:
  3140. HAL_CRYP_DESCBC_Encrypt_IT(hcryp, NULL, 0, NULL);
  3141. break;
  3142. case CRYP_CR_ALGOMODE_DES_CBC_DECRYPT:
  3143. HAL_CRYP_DESCBC_Decrypt_IT(hcryp, NULL, 0, NULL);
  3144. break;
  3145. case CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT:
  3146. HAL_CRYP_AESECB_Encrypt_IT(hcryp, NULL, 0, NULL);
  3147. break;
  3148. case CRYP_CR_ALGOMODE_AES_ECB_DECRYPT:
  3149. HAL_CRYP_AESECB_Decrypt_IT(hcryp, NULL, 0, NULL);
  3150. break;
  3151. case CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT:
  3152. HAL_CRYP_AESCBC_Encrypt_IT(hcryp, NULL, 0, NULL);
  3153. break;
  3154. case CRYP_CR_ALGOMODE_AES_CBC_DECRYPT:
  3155. HAL_CRYP_AESCBC_Decrypt_IT(hcryp, NULL, 0, NULL);
  3156. break;
  3157. case CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT:
  3158. HAL_CRYP_AESCTR_Encrypt_IT(hcryp, NULL, 0, NULL);
  3159. break;
  3160. case CRYP_CR_ALGOMODE_AES_CTR_DECRYPT:
  3161. HAL_CRYP_AESCTR_Decrypt_IT(hcryp, NULL, 0, NULL);
  3162. break;
  3163. default:
  3164. break;
  3165. }
  3166. }
  3167. /**
  3168. * @}
  3169. */
  3170. /** @defgroup CRYP_Exported_Functions_Group7 Peripheral State functions
  3171. * @brief Peripheral State functions.
  3172. *
  3173. @verbatim
  3174. ==============================================================================
  3175. ##### Peripheral State functions #####
  3176. ==============================================================================
  3177. [..]
  3178. This subsection permits to get in run-time the status of the peripheral.
  3179. @endverbatim
  3180. * @{
  3181. */
  3182. /**
  3183. * @brief Returns the CRYP state.
  3184. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3185. * the configuration information for CRYP module
  3186. * @retval HAL state
  3187. */
  3188. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  3189. {
  3190. return hcryp->State;
  3191. }
  3192. /**
  3193. * @}
  3194. */
  3195. /**
  3196. * @}
  3197. */
  3198. #endif /* STM32F415xx || STM32F417xx || STM32F437xx || STM32F439xx || STM32F479xx */
  3199. #endif /* HAL_CRYP_MODULE_ENABLED */
  3200. /**
  3201. * @}
  3202. */
  3203. /**
  3204. * @}
  3205. */
  3206. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/