stm32f4xx_hal_cryp.c 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_cryp.c
  4. * @author MCD Application Team
  5. * @brief CRYP HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Cryptography (CRYP) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + AES processing functions
  10. * + DES processing functions
  11. * + TDES processing functions
  12. * + DMA callback functions
  13. * + CRYP IRQ handler management
  14. * + Peripheral State functions
  15. *
  16. @verbatim
  17. ==============================================================================
  18. ##### How to use this driver #####
  19. ==============================================================================
  20. [..]
  21. The CRYP HAL driver can be used as follows:
  22. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  23. (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()
  24. (##) In case of using interrupts (e.g. HAL_CRYP_AESECB_Encrypt_IT())
  25. (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  26. (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
  27. (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
  28. (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_AESECB_Encrypt_DMA())
  29. (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE()
  30. (+++) Configure and enable two DMA streams one for managing data transfer from
  31. memory to peripheral (input stream) and another stream for managing data
  32. transfer from peripheral to memory (output stream)
  33. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  34. using __HAL_LINKDMA()
  35. (+++) Configure the priority and enable the NVIC for the transfer complete
  36. interrupt on the two DMA Streams. The output stream should have higher
  37. priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
  38. (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly:
  39. (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit
  40. (##) The key size: 128, 192 and 256. This parameter is relevant only for AES
  41. (##) The encryption/decryption key. It's size depends on the algorithm
  42. used for encryption/decryption
  43. (##) The initialization vector (counter). It is not used ECB mode.
  44. (#)Three processing (encryption/decryption) functions are available:
  45. (##) Polling mode: encryption and decryption APIs are blocking functions
  46. i.e. they process the data and wait till the processing is finished,
  47. e.g. HAL_CRYP_AESCBC_Encrypt()
  48. (##) Interrupt mode: encryption and decryption APIs are not blocking functions
  49. i.e. they process the data under interrupt,
  50. e.g. HAL_CRYP_AESCBC_Encrypt_IT()
  51. (##) DMA mode: encryption and decryption APIs are not blocking functions
  52. i.e. the data transfer is ensured by DMA,
  53. e.g. HAL_CRYP_AESCBC_Encrypt_DMA()
  54. (#)When the processing function is called at first time after HAL_CRYP_Init()
  55. the CRYP peripheral is initialized and processes the buffer in input.
  56. At second call, the processing function performs an append of the already
  57. processed buffer.
  58. When a new data block is to be processed, call HAL_CRYP_Init() then the
  59. processing function.
  60. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  61. @endverbatim
  62. ******************************************************************************
  63. * @attention
  64. *
  65. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  66. *
  67. * Redistribution and use in source and binary forms, with or without modification,
  68. * are permitted provided that the following conditions are met:
  69. * 1. Redistributions of source code must retain the above copyright notice,
  70. * this list of conditions and the following disclaimer.
  71. * 2. Redistributions in binary form must reproduce the above copyright notice,
  72. * this list of conditions and the following disclaimer in the documentation
  73. * and/or other materials provided with the distribution.
  74. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  75. * may be used to endorse or promote products derived from this software
  76. * without specific prior written permission.
  77. *
  78. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  79. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  80. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  81. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  82. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  83. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  84. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  85. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  86. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  87. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  88. *
  89. ******************************************************************************
  90. */
  91. /* Includes ------------------------------------------------------------------*/
  92. //#include "stm32f4xx_hal.h"
  93. #include "stm32f4xx_hal_cryp.h"
  94. /** @addtogroup STM32F4xx_HAL_Driver
  95. * @{
  96. */
  97. #define HAL_CRYP_MODULE_ENABLED
  98. #ifdef HAL_CRYP_MODULE_ENABLED
  99. #if defined(CRYP)
  100. /** @defgroup CRYP CRYP
  101. * @brief CRYP HAL module driver.
  102. * @{
  103. */
  104. /* Private typedef -----------------------------------------------------------*/
  105. /* Private define ------------------------------------------------------------*/
  106. /** @addtogroup CRYP_Private_define
  107. * @{
  108. */
  109. #define CRYP_TIMEOUT_VALUE 1U
  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+=4U;
  198. hcryp->Instance->K0RR = __REV(*(uint32_t*)(keyaddr));
  199. keyaddr+=4U;
  200. hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));
  201. keyaddr+=4U;
  202. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  203. keyaddr+=4U;
  204. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  205. keyaddr+=4U;
  206. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  207. keyaddr+=4U;
  208. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  209. keyaddr+=4U;
  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+=4U;
  215. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  216. keyaddr+=4U;
  217. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  218. keyaddr+=4U;
  219. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  220. keyaddr+=4U;
  221. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  222. keyaddr+=4U;
  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+=4U;
  228. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  229. keyaddr+=4U;
  230. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  231. keyaddr+=4U;
  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+=4U;
  254. hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr));
  255. ivaddr+=4U;
  256. hcryp->Instance->IV1LR = __REV(*(uint32_t*)(ivaddr));
  257. ivaddr+=4U;
  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+=4U;
  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+=4U;
  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 = 0U;
  288. uint32_t i = 0U;
  289. uint32_t inputaddr = (uint32_t)Input;
  290. uint32_t outputaddr = (uint32_t)Output;
  291. for(i=0U; (i < Ilength); i+=16U)
  292. {
  293. /* Write the Input block in the IN FIFO */
  294. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  295. inputaddr+=4U;
  296. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  297. inputaddr+=4U;
  298. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  299. inputaddr+=4U;
  300. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  301. inputaddr+=4U;
  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 == 0U)||((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+=4U;
  322. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  323. outputaddr+=4U;
  324. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  325. outputaddr+=4U;
  326. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  327. outputaddr+=4U;
  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 = 0U;
  345. uint32_t i = 0U;
  346. uint32_t inputaddr = (uint32_t)Input;
  347. uint32_t outputaddr = (uint32_t)Output;
  348. for(i=0U; (i < Ilength); i+=8U)
  349. {
  350. /* Write the Input block in the IN FIFO */
  351. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  352. inputaddr+=4U;
  353. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  354. inputaddr+=4U;
  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 == 0U)||((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+=4U;
  375. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  376. outputaddr+=4U;
  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/4U);
  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/4U);
  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+4U));
  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+4U));
  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 = 0U;
  558. hcryp->CrypOutCount = 0U;
  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 = 0U;
  585. hcryp->CrypOutCount = 0U;
  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 = 0U;
  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 == 0U)||((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 = 0U;
  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 == 0U)||((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+=4U;
  1009. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1010. inputaddr+=4U;
  1011. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1012. inputaddr+=4U;
  1013. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1014. hcryp->pCrypInBuffPtr += 16U;
  1015. hcryp->CrypInCount -= 16U;
  1016. if(hcryp->CrypInCount == 0U)
  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+=4U;
  1029. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1030. outputaddr+=4U;
  1031. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1032. outputaddr+=4U;
  1033. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1034. hcryp->pCrypOutBuffPtr += 16U;
  1035. hcryp->CrypOutCount -= 16U;
  1036. if(hcryp->CrypOutCount == 0U)
  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+=4U;
  1100. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1101. inputaddr+=4U;
  1102. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1103. inputaddr+=4U;
  1104. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1105. hcryp->pCrypInBuffPtr += 16U;
  1106. hcryp->CrypInCount -= 16U;
  1107. if(hcryp->CrypInCount == 0U)
  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+=4U;
  1120. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1121. outputaddr+=4U;
  1122. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1123. outputaddr+=4U;
  1124. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1125. hcryp->pCrypOutBuffPtr += 16U;
  1126. hcryp->CrypOutCount -= 16U;
  1127. if(hcryp->CrypOutCount == 0U)
  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+=4U;
  1191. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1192. inputaddr+=4U;
  1193. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1194. inputaddr+=4U;
  1195. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1196. hcryp->pCrypInBuffPtr += 16U;
  1197. hcryp->CrypInCount -= 16U;
  1198. if(hcryp->CrypInCount == 0U)
  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+=4U;
  1211. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1212. outputaddr+=4U;
  1213. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1214. outputaddr+=4U;
  1215. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1216. hcryp->pCrypOutBuffPtr += 16U;
  1217. hcryp->CrypOutCount -= 16U;
  1218. if(hcryp->CrypOutCount == 0U)
  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 = 0U;
  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+=4U;
  1301. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1302. inputaddr+=4U;
  1303. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1304. inputaddr+=4U;
  1305. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1306. hcryp->pCrypInBuffPtr += 16U;
  1307. hcryp->CrypInCount -= 16U;
  1308. if(hcryp->CrypInCount == 0U)
  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+=4U;
  1321. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1322. outputaddr+=4U;
  1323. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1324. outputaddr+=4U;
  1325. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1326. hcryp->pCrypOutBuffPtr += 16U;
  1327. hcryp->CrypOutCount -= 16U;
  1328. if(hcryp->CrypOutCount == 0U)
  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 = 0U;
  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+=4U;
  1416. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1417. inputaddr+=4U;
  1418. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1419. inputaddr+=4U;
  1420. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1421. hcryp->pCrypInBuffPtr += 16U;
  1422. hcryp->CrypInCount -= 16U;
  1423. if(hcryp->CrypInCount == 0U)
  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+=4U;
  1436. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1437. outputaddr+=4U;
  1438. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1439. outputaddr+=4U;
  1440. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1441. hcryp->pCrypOutBuffPtr += 16U;
  1442. hcryp->CrypOutCount -= 16U;
  1443. if(hcryp->CrypOutCount == 0U)
  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+=4U;
  1508. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1509. inputaddr+=4U;
  1510. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1511. inputaddr+=4U;
  1512. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1513. hcryp->pCrypInBuffPtr += 16U;
  1514. hcryp->CrypInCount -= 16U;
  1515. if(hcryp->CrypInCount == 0U)
  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+=4U;
  1528. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1529. outputaddr+=4U;
  1530. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1531. outputaddr+=4U;
  1532. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1533. hcryp->pCrypOutBuffPtr += 16U;
  1534. hcryp->CrypOutCount -= 16U;
  1535. if(hcryp->CrypOutCount == 0U)
  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 = 0U;
  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 = 0U;
  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, 0U);
  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, 0U);
  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, 0U);
  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+=4U;
  2055. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2056. hcryp->pCrypInBuffPtr += 8U;
  2057. hcryp->CrypInCount -= 8U;
  2058. if(hcryp->CrypInCount == 0U)
  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+=4U;
  2071. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2072. hcryp->pCrypOutBuffPtr += 8U;
  2073. hcryp->CrypOutCount -= 8U;
  2074. if(hcryp->CrypOutCount == 0U)
  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, 0U);
  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+=4U;
  2129. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2130. hcryp->pCrypInBuffPtr += 8U;
  2131. hcryp->CrypInCount -= 8U;
  2132. if(hcryp->CrypInCount == 0U)
  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+=4U;
  2145. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2146. hcryp->pCrypOutBuffPtr += 8U;
  2147. hcryp->CrypOutCount -= 8U;
  2148. if(hcryp->CrypOutCount == 0U)
  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+=4U;
  2203. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2204. hcryp->pCrypInBuffPtr += 8U;
  2205. hcryp->CrypInCount -= 8U;
  2206. if(hcryp->CrypInCount == 0U)
  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+=4U;
  2219. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2220. hcryp->pCrypOutBuffPtr += 8U;
  2221. hcryp->CrypOutCount -= 8U;
  2222. if(hcryp->CrypOutCount == 0U)
  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+=4U;
  2277. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2278. hcryp->pCrypInBuffPtr += 8U;
  2279. hcryp->CrypInCount -= 8U;
  2280. if(hcryp->CrypInCount == 0U)
  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+=4U;
  2293. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2294. hcryp->pCrypOutBuffPtr += 8U;
  2295. hcryp->CrypOutCount -= 8U;
  2296. if(hcryp->CrypOutCount == 0U)
  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, 0U);
  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, 0U);
  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, 0U);
  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, 0U);
  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, 0U);
  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+=4U;
  2643. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2644. hcryp->pCrypInBuffPtr += 8U;
  2645. hcryp->CrypInCount -= 8U;
  2646. if(hcryp->CrypInCount == 0U)
  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+=4U;
  2659. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2660. hcryp->pCrypOutBuffPtr += 8U;
  2661. hcryp->CrypOutCount -= 8U;
  2662. if(hcryp->CrypOutCount == 0U)
  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, 0U);
  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+=4U;
  2717. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2718. hcryp->pCrypInBuffPtr += 8U;
  2719. hcryp->CrypInCount -= 8U;
  2720. if(hcryp->CrypInCount == 0U)
  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+=4U;
  2733. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2734. hcryp->pCrypOutBuffPtr += 8U;
  2735. hcryp->CrypOutCount -= 8U;
  2736. if(hcryp->CrypOutCount == 0U)
  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+=4U;
  2790. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2791. hcryp->pCrypInBuffPtr += 8U;
  2792. hcryp->CrypInCount -= 8U;
  2793. if(hcryp->CrypInCount == 0U)
  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+=4U;
  2806. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2807. hcryp->pCrypOutBuffPtr += 8U;
  2808. hcryp->CrypOutCount -= 8U;
  2809. if(hcryp->CrypOutCount == 0U)
  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+=4U;
  2863. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2864. hcryp->pCrypInBuffPtr += 8U;
  2865. hcryp->CrypInCount -= 8U;
  2866. if(hcryp->CrypInCount == 0U)
  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+=4U;
  2879. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2880. hcryp->pCrypOutBuffPtr += 8U;
  2881. hcryp->CrypOutCount -= 8U;
  2882. if(hcryp->CrypOutCount == 0U)
  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, 0U);
  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, 0U);
  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, 0U, NULL);
  3123. break;
  3124. case CRYP_CR_ALGOMODE_TDES_ECB_DECRYPT:
  3125. HAL_CRYP_TDESECB_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3126. break;
  3127. case CRYP_CR_ALGOMODE_TDES_CBC_ENCRYPT:
  3128. HAL_CRYP_TDESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3129. break;
  3130. case CRYP_CR_ALGOMODE_TDES_CBC_DECRYPT:
  3131. HAL_CRYP_TDESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3132. break;
  3133. case CRYP_CR_ALGOMODE_DES_ECB_ENCRYPT:
  3134. HAL_CRYP_DESECB_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3135. break;
  3136. case CRYP_CR_ALGOMODE_DES_ECB_DECRYPT:
  3137. HAL_CRYP_DESECB_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3138. break;
  3139. case CRYP_CR_ALGOMODE_DES_CBC_ENCRYPT:
  3140. HAL_CRYP_DESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3141. break;
  3142. case CRYP_CR_ALGOMODE_DES_CBC_DECRYPT:
  3143. HAL_CRYP_DESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3144. break;
  3145. case CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT:
  3146. HAL_CRYP_AESECB_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3147. break;
  3148. case CRYP_CR_ALGOMODE_AES_ECB_DECRYPT:
  3149. HAL_CRYP_AESECB_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3150. break;
  3151. case CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT:
  3152. HAL_CRYP_AESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3153. break;
  3154. case CRYP_CR_ALGOMODE_AES_CBC_DECRYPT:
  3155. HAL_CRYP_AESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3156. break;
  3157. case CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT:
  3158. HAL_CRYP_AESCTR_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3159. break;
  3160. case CRYP_CR_ALGOMODE_AES_CTR_DECRYPT:
  3161. HAL_CRYP_AESCTR_Decrypt_IT(hcryp, NULL, 0U, 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 /* CRYP */
  3199. #if defined (AES)
  3200. /** @defgroup AES AES
  3201. * @brief AES HAL module driver.
  3202. * @{
  3203. */
  3204. /* Private typedef -----------------------------------------------------------*/
  3205. /* Private define ------------------------------------------------------------*/
  3206. /* Private macro -------------------------------------------------------------*/
  3207. /* Private variables ---------------------------------------------------------*/
  3208. /* Private functions --------------------------------------------------------*/
  3209. /** @defgroup CRYP_Private_Functions CRYP Private Functions
  3210. * @{
  3211. */
  3212. static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp);
  3213. static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp);
  3214. static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
  3215. /**
  3216. * @}
  3217. */
  3218. /* Exported functions ---------------------------------------------------------*/
  3219. /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
  3220. * @{
  3221. */
  3222. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions
  3223. * @brief Initialization and Configuration functions.
  3224. *
  3225. @verbatim
  3226. ==============================================================================
  3227. ##### Initialization and deinitialization functions #####
  3228. ==============================================================================
  3229. [..] This section provides functions allowing to:
  3230. (+) Initialize the CRYP according to the specified parameters
  3231. in the CRYP_InitTypeDef and creates the associated handle
  3232. (+) DeInitialize the CRYP peripheral
  3233. (+) Initialize the CRYP MSP (MCU Specific Package)
  3234. (+) De-Initialize the CRYP MSP
  3235. [..]
  3236. (@) Specific care must be taken to format the key and the Initialization Vector IV!
  3237. [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where
  3238. b127 is the MSB and b0 the LSB, the key must be stored in MCU memory
  3239. (+) as a sequence of words where the MSB word comes first (occupies the
  3240. lowest memory address)
  3241. (+) where each word is byte-swapped:
  3242. (++) address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120
  3243. (++) address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88
  3244. (++) address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56
  3245. (++) address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24
  3246. [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}.
  3247. The 4 32-bit words that make the key must be stored as follows in MCU memory:
  3248. (+) address n+0 : 0x B12 B13 B14 B15
  3249. (+) address n+4 : 0x B8 B9 B10 B11
  3250. (+) address n+8 : 0x B4 B5 B6 B7
  3251. (+) address n+C : 0x B0 B1 B2 B3
  3252. [..] which leads to the expected setting
  3253. (+) AES_KEYR3 = 0x B15 B14 B13 B12
  3254. (+) AES_KEYR2 = 0x B11 B10 B9 B8
  3255. (+) AES_KEYR1 = 0x B7 B6 B5 B4
  3256. (+) AES_KEYR0 = 0x B3 B2 B1 B0
  3257. [..] Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}.
  3258. The 8 32-bit words that make the key must be stored as follows in MCU memory:
  3259. (+) address n+00 : 0x B28 B29 B30 B31
  3260. (+) address n+04 : 0x B24 B25 B26 B27
  3261. (+) address n+08 : 0x B20 B21 B22 B23
  3262. (+) address n+0C : 0x B16 B17 B18 B19
  3263. (+) address n+10 : 0x B12 B13 B14 B15
  3264. (+) address n+14 : 0x B8 B9 B10 B11
  3265. (+) address n+18 : 0x B4 B5 B6 B7
  3266. (+) address n+1C : 0x B0 B1 B2 B3
  3267. [..] which leads to the expected setting
  3268. (+) AES_KEYR7 = 0x B31 B30 B29 B28
  3269. (+) AES_KEYR6 = 0x B27 B26 B25 B24
  3270. (+) AES_KEYR5 = 0x B23 B22 B21 B20
  3271. (+) AES_KEYR4 = 0x B19 B18 B17 B16
  3272. (+) AES_KEYR3 = 0x B15 B14 B13 B12
  3273. (+) AES_KEYR2 = 0x B11 B10 B9 B8
  3274. (+) AES_KEYR1 = 0x B7 B6 B5 B4
  3275. (+) AES_KEYR0 = 0x B3 B2 B1 B0
  3276. [..] Initialization Vector IV (4 32-bit words) format must follow the same as
  3277. that of a 128-bit long key.
  3278. [..]
  3279. @endverbatim
  3280. * @{
  3281. */
  3282. /**
  3283. * @brief Initialize the CRYP according to the specified
  3284. * parameters in the CRYP_InitTypeDef and initialize the associated handle.
  3285. * @note Specific care must be taken to format the key and the Initialization Vector IV
  3286. * stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations
  3287. * hereabove.
  3288. * @retval HAL status
  3289. */
  3290. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  3291. {
  3292. /* Check the CRYP handle allocation */
  3293. if(hcryp == NULL)
  3294. {
  3295. return HAL_ERROR;
  3296. }
  3297. /* Check the instance */
  3298. assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance));
  3299. /* Check the parameters */
  3300. assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
  3301. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  3302. assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode));
  3303. /* ChainingMode parameter is irrelevant when mode is set to Key derivation */
  3304. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  3305. {
  3306. assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode));
  3307. }
  3308. assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag));
  3309. /*========================================================*/
  3310. /* Check the proper operating/chaining modes combinations */
  3311. /*========================================================*/
  3312. /* Check the proper chaining when the operating mode is key derivation and decryption */
  3313. #if defined(AES_CR_NPBLB)
  3314. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
  3315. ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
  3316. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
  3317. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)))
  3318. #else
  3319. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
  3320. ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
  3321. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
  3322. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)))
  3323. #endif
  3324. {
  3325. return HAL_ERROR;
  3326. }
  3327. /* Check that key derivation is not set in CMAC mode or CCM mode when applicable */
  3328. #if defined(AES_CR_NPBLB)
  3329. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  3330. && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC))
  3331. #else
  3332. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  3333. && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  3334. #endif
  3335. {
  3336. return HAL_ERROR;
  3337. }
  3338. /*================*/
  3339. /* Initialization */
  3340. /*================*/
  3341. /* Initialization start */
  3342. if(hcryp->State == HAL_CRYP_STATE_RESET)
  3343. {
  3344. /* Allocate lock resource and initialize it */
  3345. hcryp->Lock = HAL_UNLOCKED;
  3346. /* Init the low level hardware */
  3347. HAL_CRYP_MspInit(hcryp);
  3348. }
  3349. /* Change the CRYP state */
  3350. hcryp->State = HAL_CRYP_STATE_BUSY;
  3351. /* Disable the Peripheral */
  3352. __HAL_CRYP_DISABLE();
  3353. /*=============================================================*/
  3354. /* AES initialization common to all operating modes */
  3355. /*=============================================================*/
  3356. /* Set the Key size selection */
  3357. MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize);
  3358. /* Set the default CRYP phase when this parameter is not used.
  3359. Phase is updated below in case of GCM/GMAC/CMAC(/CCM) setting. */
  3360. hcryp->Phase = HAL_CRYP_PHASE_NOT_USED;
  3361. /*=============================================================*/
  3362. /* Carry on the initialization based on the AES operating mode */
  3363. /*=============================================================*/
  3364. /* Key derivation */
  3365. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  3366. {
  3367. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION);
  3368. /* Configure the Key registers */
  3369. if (CRYP_SetKey(hcryp) != HAL_OK)
  3370. {
  3371. return HAL_ERROR;
  3372. }
  3373. }
  3374. else
  3375. /* Encryption / Decryption (with or without key derivation) / authentication */
  3376. {
  3377. /* Set data type, operating and chaining modes.
  3378. In case of GCM or GMAC, data type is forced to 0b00 */
  3379. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3380. {
  3381. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
  3382. }
  3383. else
  3384. {
  3385. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
  3386. }
  3387. /* Specify the encryption/decryption phase in case of Galois counter mode (GCM),
  3388. Galois message authentication code (GMAC), cipher message authentication code (CMAC)
  3389. or Counter with Cipher Mode (CCM) when applicable */
  3390. #if defined(AES_CR_NPBLB)
  3391. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3392. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC))
  3393. #else
  3394. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3395. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  3396. #endif
  3397. {
  3398. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase);
  3399. hcryp->Phase = HAL_CRYP_PHASE_START;
  3400. }
  3401. /* Configure the Key registers if no need to bypass this step */
  3402. if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE)
  3403. {
  3404. if (CRYP_SetKey(hcryp) != HAL_OK)
  3405. {
  3406. return HAL_ERROR;
  3407. }
  3408. }
  3409. /* If applicable, configure the Initialization Vector */
  3410. if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB)
  3411. {
  3412. if (CRYP_SetInitVector(hcryp) != HAL_OK)
  3413. {
  3414. return HAL_ERROR;
  3415. }
  3416. }
  3417. }
  3418. #if defined(AES_CR_NPBLB)
  3419. /* Clear NPBLB field */
  3420. CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
  3421. #endif
  3422. /* Reset CrypInCount and CrypOutCount */
  3423. hcryp->CrypInCount = 0U;
  3424. hcryp->CrypOutCount = 0U;
  3425. /* Reset ErrorCode field */
  3426. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  3427. /* Reset Mode suspension request */
  3428. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  3429. /* Change the CRYP state */
  3430. hcryp->State = HAL_CRYP_STATE_READY;
  3431. /* Enable the Peripheral */
  3432. __HAL_CRYP_ENABLE();
  3433. /* Return function status */
  3434. return HAL_OK;
  3435. }
  3436. /**
  3437. * @brief DeInitialize the CRYP peripheral.
  3438. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3439. * the configuration information for CRYP module
  3440. * @retval HAL status
  3441. */
  3442. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  3443. {
  3444. /* Check the CRYP handle allocation */
  3445. if(hcryp == NULL)
  3446. {
  3447. return HAL_ERROR;
  3448. }
  3449. /* Change the CRYP state */
  3450. hcryp->State = HAL_CRYP_STATE_BUSY;
  3451. /* Set the default CRYP phase */
  3452. hcryp->Phase = HAL_CRYP_PHASE_READY;
  3453. /* Reset CrypInCount and CrypOutCount */
  3454. hcryp->CrypInCount = 0U;
  3455. hcryp->CrypOutCount = 0U;
  3456. /* Disable the CRYP Peripheral Clock */
  3457. __HAL_CRYP_DISABLE();
  3458. /* DeInit the low level hardware: CLOCK, NVIC.*/
  3459. HAL_CRYP_MspDeInit(hcryp);
  3460. /* Change the CRYP state */
  3461. hcryp->State = HAL_CRYP_STATE_RESET;
  3462. /* Release Lock */
  3463. __HAL_UNLOCK(hcryp);
  3464. /* Return function status */
  3465. return HAL_OK;
  3466. }
  3467. /**
  3468. * @brief Initialize the CRYP MSP.
  3469. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3470. * the configuration information for CRYP module
  3471. * @retval None
  3472. */
  3473. __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
  3474. {
  3475. /* Prevent unused argument(s) compilation warning */
  3476. UNUSED(hcryp);
  3477. /* NOTE : This function should not be modified; when the callback is needed,
  3478. the HAL_CRYP_MspInit can be implemented in the user file
  3479. */
  3480. }
  3481. /**
  3482. * @brief DeInitialize CRYP MSP.
  3483. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3484. * the configuration information for CRYP module
  3485. * @retval None
  3486. */
  3487. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  3488. {
  3489. /* Prevent unused argument(s) compilation warning */
  3490. UNUSED(hcryp);
  3491. /* NOTE : This function should not be modified; when the callback is needed,
  3492. the HAL_CRYP_MspDeInit can be implemented in the user file
  3493. */
  3494. }
  3495. /**
  3496. * @}
  3497. */
  3498. /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
  3499. * @brief Processing functions.
  3500. *
  3501. @verbatim
  3502. ==============================================================================
  3503. ##### AES processing functions #####
  3504. ==============================================================================
  3505. [..] This section provides functions allowing to:
  3506. (+) Encrypt plaintext using AES algorithm in different chaining modes
  3507. (+) Decrypt cyphertext using AES algorithm in different chaining modes
  3508. [..] Three processing functions are available:
  3509. (+) Polling mode
  3510. (+) Interrupt mode
  3511. (+) DMA mode
  3512. @endverbatim
  3513. * @{
  3514. */
  3515. /**
  3516. * @brief Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData.
  3517. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3518. * the configuration information for CRYP module
  3519. * @param pPlainData Pointer to the plaintext buffer
  3520. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3521. * @param pCypherData Pointer to the cyphertext buffer
  3522. * @param Timeout Specify Timeout value
  3523. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3524. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3525. * @retval HAL status
  3526. */
  3527. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  3528. {
  3529. /* Re-initialize AES IP with proper parameters */
  3530. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3531. {
  3532. return HAL_ERROR;
  3533. }
  3534. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3535. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3536. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3537. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3538. {
  3539. return HAL_ERROR;
  3540. }
  3541. return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
  3542. }
  3543. /**
  3544. * @brief Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData.
  3545. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3546. * the configuration information for CRYP module
  3547. * @param pPlainData Pointer to the plaintext buffer
  3548. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3549. * @param pCypherData Pointer to the cyphertext buffer
  3550. * @param Timeout Specify Timeout value
  3551. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3552. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3553. * @retval HAL status
  3554. */
  3555. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  3556. {
  3557. /* Re-initialize AES IP with proper parameters */
  3558. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3559. {
  3560. return HAL_ERROR;
  3561. }
  3562. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3563. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3564. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3565. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3566. {
  3567. return HAL_ERROR;
  3568. }
  3569. return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
  3570. }
  3571. /**
  3572. * @brief Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData
  3573. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3574. * the configuration information for CRYP module
  3575. * @param pPlainData Pointer to the plaintext buffer
  3576. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3577. * @param pCypherData Pointer to the cyphertext buffer
  3578. * @param Timeout Specify Timeout value
  3579. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3580. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3581. * @retval HAL status
  3582. */
  3583. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  3584. {
  3585. /* Re-initialize AES IP with proper parameters */
  3586. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3587. {
  3588. return HAL_ERROR;
  3589. }
  3590. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3591. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  3592. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3593. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3594. {
  3595. return HAL_ERROR;
  3596. }
  3597. return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
  3598. }
  3599. /**
  3600. * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
  3601. * the decyphered data are available in pPlainData.
  3602. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3603. * the configuration information for CRYP module
  3604. * @param pCypherData Pointer to the cyphertext buffer
  3605. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3606. * @param pPlainData Pointer to the plaintext buffer
  3607. * @param Timeout Specify Timeout value
  3608. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3609. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3610. * @retval HAL status
  3611. */
  3612. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  3613. {
  3614. /* Re-initialize AES IP with proper parameters */
  3615. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3616. {
  3617. return HAL_ERROR;
  3618. }
  3619. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3620. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3621. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3622. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3623. {
  3624. return HAL_ERROR;
  3625. }
  3626. return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
  3627. }
  3628. /**
  3629. * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
  3630. * the decyphered data are available in pPlainData.
  3631. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3632. * the configuration information for CRYP module
  3633. * @param pCypherData Pointer to the cyphertext buffer
  3634. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3635. * @param pPlainData Pointer to the plaintext buffer
  3636. * @param Timeout Specify Timeout value
  3637. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3638. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3639. * @retval HAL status
  3640. */
  3641. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  3642. {
  3643. /* Re-initialize AES IP with proper parameters */
  3644. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3645. {
  3646. return HAL_ERROR;
  3647. }
  3648. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3649. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3650. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3651. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3652. {
  3653. return HAL_ERROR;
  3654. }
  3655. return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
  3656. }
  3657. /**
  3658. * @brief Decrypt pCypherData in AES CTR decryption mode,
  3659. * the decyphered data are available in pPlainData.
  3660. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3661. * the configuration information for CRYP module
  3662. * @param pCypherData Pointer to the cyphertext buffer
  3663. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3664. * @param pPlainData Pointer to the plaintext buffer
  3665. * @param Timeout Specify Timeout value
  3666. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3667. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3668. * @retval HAL status
  3669. */
  3670. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  3671. {
  3672. /* Re-initialize AES IP with proper parameters */
  3673. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3674. {
  3675. return HAL_ERROR;
  3676. }
  3677. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  3678. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  3679. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3680. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3681. {
  3682. return HAL_ERROR;
  3683. }
  3684. return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
  3685. }
  3686. /**
  3687. * @brief Encrypt pPlainData in AES ECB encryption mode using Interrupt,
  3688. * the cypher data are available in pCypherData.
  3689. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3690. * the configuration information for CRYP module
  3691. * @param pPlainData Pointer to the plaintext buffer
  3692. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3693. * @param pCypherData Pointer to the cyphertext buffer
  3694. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3695. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3696. * @retval HAL status
  3697. */
  3698. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3699. {
  3700. /* Re-initialize AES IP with proper parameters */
  3701. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3702. {
  3703. return HAL_ERROR;
  3704. }
  3705. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3706. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3707. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3708. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3709. {
  3710. return HAL_ERROR;
  3711. }
  3712. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  3713. }
  3714. /**
  3715. * @brief Encrypt pPlainData in AES CBC encryption mode using Interrupt,
  3716. * the cypher data are available in pCypherData.
  3717. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3718. * the configuration information for CRYP module
  3719. * @param pPlainData Pointer to the plaintext buffer
  3720. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3721. * @param pCypherData Pointer to the cyphertext buffer
  3722. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3723. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3724. * @retval HAL status
  3725. */
  3726. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3727. {
  3728. /* Re-initialize AES IP with proper parameters */
  3729. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3730. {
  3731. return HAL_ERROR;
  3732. }
  3733. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3734. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3735. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3736. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3737. {
  3738. return HAL_ERROR;
  3739. }
  3740. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  3741. }
  3742. /**
  3743. * @brief Encrypt pPlainData in AES CTR encryption mode using Interrupt,
  3744. * the cypher data are available in pCypherData.
  3745. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3746. * the configuration information for CRYP module
  3747. * @param pPlainData Pointer to the plaintext buffer
  3748. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3749. * @param pCypherData Pointer to the cyphertext buffer
  3750. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3751. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3752. * @retval HAL status
  3753. */
  3754. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3755. {
  3756. /* Re-initialize AES IP with proper parameters */
  3757. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3758. {
  3759. return HAL_ERROR;
  3760. }
  3761. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3762. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  3763. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3764. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3765. {
  3766. return HAL_ERROR;
  3767. }
  3768. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  3769. }
  3770. /**
  3771. * @brief Decrypt pCypherData in AES ECB decryption mode using Interrupt,
  3772. * the decyphered data are available in pPlainData.
  3773. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3774. * the configuration information for CRYP module
  3775. * @param pCypherData Pointer to the cyphertext buffer
  3776. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3777. * @param pPlainData Pointer to the plaintext buffer.
  3778. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3779. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3780. * @retval HAL status
  3781. */
  3782. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3783. {
  3784. /* Re-initialize AES IP with proper parameters */
  3785. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3786. {
  3787. return HAL_ERROR;
  3788. }
  3789. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3790. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3791. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3792. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3793. {
  3794. return HAL_ERROR;
  3795. }
  3796. return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
  3797. }
  3798. /**
  3799. * @brief Decrypt pCypherData in AES CBC decryption mode using Interrupt,
  3800. * the decyphered data are available in pPlainData.
  3801. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3802. * the configuration information for CRYP module
  3803. * @param pCypherData Pointer to the cyphertext buffer
  3804. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3805. * @param pPlainData Pointer to the plaintext buffer
  3806. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3807. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3808. * @retval HAL status
  3809. */
  3810. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3811. {
  3812. /* Re-initialize AES IP with proper parameters */
  3813. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3814. {
  3815. return HAL_ERROR;
  3816. }
  3817. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3818. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3819. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3820. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3821. {
  3822. return HAL_ERROR;
  3823. }
  3824. return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
  3825. }
  3826. /**
  3827. * @brief Decrypt pCypherData in AES CTR decryption mode using Interrupt,
  3828. * the decyphered data are available in pPlainData.
  3829. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3830. * the configuration information for CRYP module
  3831. * @param pCypherData Pointer to the cyphertext buffer
  3832. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3833. * @param pPlainData Pointer to the plaintext buffer
  3834. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3835. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3836. * @retval HAL status
  3837. */
  3838. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3839. {
  3840. /* Re-initialize AES IP with proper parameters */
  3841. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3842. {
  3843. return HAL_ERROR;
  3844. }
  3845. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  3846. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  3847. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3848. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3849. {
  3850. return HAL_ERROR;
  3851. }
  3852. return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
  3853. }
  3854. /**
  3855. * @brief Encrypt pPlainData in AES ECB encryption mode using DMA,
  3856. * the cypher data are available in pCypherData.
  3857. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3858. * the configuration information for CRYP module
  3859. * @param pPlainData Pointer to the plaintext buffer
  3860. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3861. * @param pCypherData Pointer to the cyphertext buffer
  3862. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3863. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  3864. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3865. * @retval HAL status
  3866. */
  3867. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3868. {
  3869. /* Re-initialize AES IP with proper parameters */
  3870. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3871. {
  3872. return HAL_ERROR;
  3873. }
  3874. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3875. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3876. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3877. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3878. {
  3879. return HAL_ERROR;
  3880. }
  3881. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  3882. }
  3883. /**
  3884. * @brief Encrypt pPlainData in AES CBC encryption mode using DMA,
  3885. * the cypher data are available in pCypherData.
  3886. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3887. * the configuration information for CRYP module
  3888. * @param pPlainData Pointer to the plaintext buffer
  3889. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  3890. * @param pCypherData Pointer to the cyphertext buffer
  3891. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3892. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  3893. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3894. * @retval HAL status
  3895. */
  3896. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3897. {
  3898. /* Re-initialize AES IP with proper parameters */
  3899. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3900. {
  3901. return HAL_ERROR;
  3902. }
  3903. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3904. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3905. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3906. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3907. {
  3908. return HAL_ERROR;
  3909. }
  3910. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  3911. }
  3912. /**
  3913. * @brief Encrypt pPlainData in AES CTR encryption mode using DMA,
  3914. * the cypher data are available in pCypherData.
  3915. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3916. * the configuration information for CRYP module
  3917. * @param pPlainData Pointer to the plaintext buffer
  3918. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3919. * @param pCypherData Pointer to the cyphertext buffer.
  3920. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3921. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  3922. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3923. * @retval HAL status
  3924. */
  3925. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3926. {
  3927. /* Re-initialize AES IP with proper parameters */
  3928. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3929. {
  3930. return HAL_ERROR;
  3931. }
  3932. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3933. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  3934. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3935. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3936. {
  3937. return HAL_ERROR;
  3938. }
  3939. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  3940. }
  3941. /**
  3942. * @brief Decrypt pCypherData in AES ECB decryption mode using DMA,
  3943. * the decyphered data are available in pPlainData.
  3944. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3945. * the configuration information for CRYP module
  3946. * @param pCypherData Pointer to the cyphertext buffer
  3947. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3948. * @param pPlainData Pointer to the plaintext buffer
  3949. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3950. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  3951. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3952. * @retval HAL status
  3953. */
  3954. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3955. {
  3956. /* Re-initialize AES IP with proper parameters */
  3957. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3958. {
  3959. return HAL_ERROR;
  3960. }
  3961. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3962. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3963. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3964. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3965. {
  3966. return HAL_ERROR;
  3967. }
  3968. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  3969. }
  3970. /**
  3971. * @brief Decrypt pCypherData in AES CBC decryption mode using DMA,
  3972. * the decyphered data are available in pPlainData.
  3973. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3974. * the configuration information for CRYP module
  3975. * @param pCypherData Pointer to the cyphertext buffer
  3976. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3977. * @param pPlainData Pointer to the plaintext buffer
  3978. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3979. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  3980. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3981. * @retval HAL status
  3982. */
  3983. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3984. {
  3985. /* Re-initialize AES IP with proper parameters */
  3986. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3987. {
  3988. return HAL_ERROR;
  3989. }
  3990. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3991. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3992. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3993. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3994. {
  3995. return HAL_ERROR;
  3996. }
  3997. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  3998. }
  3999. /**
  4000. * @brief Decrypt pCypherData in AES CTR decryption mode using DMA,
  4001. * the decyphered data are available in pPlainData.
  4002. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4003. * the configuration information for CRYP module
  4004. * @param pCypherData Pointer to the cyphertext buffer
  4005. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  4006. * @param pPlainData Pointer to the plaintext buffer
  4007. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  4008. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  4009. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  4010. * @retval HAL status
  4011. */
  4012. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  4013. {
  4014. /* Re-initialize AES IP with proper parameters */
  4015. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  4016. {
  4017. return HAL_ERROR;
  4018. }
  4019. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  4020. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  4021. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  4022. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  4023. {
  4024. return HAL_ERROR;
  4025. }
  4026. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  4027. }
  4028. /**
  4029. * @}
  4030. */
  4031. /** @defgroup CRYP_Exported_Functions_Group3 Callback functions
  4032. * @brief Callback functions.
  4033. *
  4034. @verbatim
  4035. ==============================================================================
  4036. ##### Callback functions #####
  4037. ==============================================================================
  4038. [..] This section provides Interruption and DMA callback functions:
  4039. (+) DMA Input data transfer complete
  4040. (+) DMA Output data transfer complete
  4041. (+) DMA or Interrupt error
  4042. @endverbatim
  4043. * @{
  4044. */
  4045. /**
  4046. * @brief CRYP error callback.
  4047. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4048. * the configuration information for CRYP module
  4049. * @retval None
  4050. */
  4051. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  4052. {
  4053. /* Prevent unused argument(s) compilation warning */
  4054. UNUSED(hcryp);
  4055. /* NOTE : This function should not be modified; when the callback is needed,
  4056. the HAL_CRYP_ErrorCallback can be implemented in the user file
  4057. */
  4058. }
  4059. /**
  4060. * @brief Input DMA transfer complete callback.
  4061. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4062. * the configuration information for CRYP module
  4063. * @retval None
  4064. */
  4065. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  4066. {
  4067. /* Prevent unused argument(s) compilation warning */
  4068. UNUSED(hcryp);
  4069. /* NOTE : This function should not be modified; when the callback is needed,
  4070. the HAL_CRYP_InCpltCallback can be implemented in the user file
  4071. */
  4072. }
  4073. /**
  4074. * @brief Output DMA transfer complete callback.
  4075. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4076. * the configuration information for CRYP module
  4077. * @retval None
  4078. */
  4079. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  4080. {
  4081. /* Prevent unused argument(s) compilation warning */
  4082. UNUSED(hcryp);
  4083. /* NOTE : This function should not be modified; when the callback is needed,
  4084. the HAL_CRYP_OutCpltCallback can be implemented in the user file
  4085. */
  4086. }
  4087. /**
  4088. * @}
  4089. */
  4090. /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
  4091. * @brief AES IRQ handler.
  4092. *
  4093. @verbatim
  4094. ==============================================================================
  4095. ##### AES IRQ handler management #####
  4096. ==============================================================================
  4097. [..] This section provides AES IRQ handler function.
  4098. @endverbatim
  4099. * @{
  4100. */
  4101. /**
  4102. * @brief Handle AES interrupt request.
  4103. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4104. * the configuration information for CRYP module
  4105. * @retval None
  4106. */
  4107. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  4108. {
  4109. /* Check if error occurred */
  4110. if (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_ERRIE) != RESET)
  4111. {
  4112. /* If Write Error occurred */
  4113. if (__HAL_CRYP_GET_FLAG(CRYP_IT_WRERR) != RESET)
  4114. {
  4115. hcryp->ErrorCode |= HAL_CRYP_WRITE_ERROR;
  4116. hcryp->State = HAL_CRYP_STATE_ERROR;
  4117. }
  4118. /* If Read Error occurred */
  4119. if (__HAL_CRYP_GET_FLAG(CRYP_IT_RDERR) != RESET)
  4120. {
  4121. hcryp->ErrorCode |= HAL_CRYP_READ_ERROR;
  4122. hcryp->State = HAL_CRYP_STATE_ERROR;
  4123. }
  4124. /* If an error has been reported */
  4125. if (hcryp->State == HAL_CRYP_STATE_ERROR)
  4126. {
  4127. /* Disable Error and Computation Complete Interrupts */
  4128. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4129. /* Clear all Interrupt flags */
  4130. __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR|CRYP_CCF_CLEAR);
  4131. /* Process Unlocked */
  4132. __HAL_UNLOCK(hcryp);
  4133. HAL_CRYP_ErrorCallback(hcryp);
  4134. return;
  4135. }
  4136. }
  4137. /* Check if computation complete interrupt is enabled
  4138. and if the computation complete flag is raised */
  4139. if((__HAL_CRYP_GET_FLAG(CRYP_IT_CCF) != RESET) && (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_CCFIE) != RESET))
  4140. {
  4141. #if defined(AES_CR_NPBLB)
  4142. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  4143. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC))
  4144. #else
  4145. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  4146. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  4147. #endif
  4148. {
  4149. /* To ensure proper suspension requests management, CCF flag
  4150. is reset in CRYP_AES_Auth_IT() according to the current
  4151. phase under handling */
  4152. CRYP_AES_Auth_IT(hcryp);
  4153. }
  4154. else
  4155. {
  4156. /* Clear Computation Complete Flag */
  4157. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4158. CRYP_AES_IT(hcryp);
  4159. }
  4160. }
  4161. }
  4162. /**
  4163. * @}
  4164. */
  4165. /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
  4166. * @brief Peripheral State functions.
  4167. *
  4168. @verbatim
  4169. ==============================================================================
  4170. ##### Peripheral State functions #####
  4171. ==============================================================================
  4172. [..]
  4173. This subsection permits to get in run-time the status of the peripheral.
  4174. @endverbatim
  4175. * @{
  4176. */
  4177. /**
  4178. * @brief Return the CRYP handle state.
  4179. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4180. * the configuration information for CRYP module
  4181. * @retval HAL state
  4182. */
  4183. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  4184. {
  4185. /* Return CRYP handle state */
  4186. return hcryp->State;
  4187. }
  4188. /**
  4189. * @brief Return the CRYP peripheral error.
  4190. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4191. * the configuration information for CRYP module
  4192. * @note The returned error is a bit-map combination of possible errors
  4193. * @retval Error bit-map
  4194. */
  4195. uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
  4196. {
  4197. return hcryp->ErrorCode;
  4198. }
  4199. /**
  4200. * @}
  4201. */
  4202. /**
  4203. * @}
  4204. */
  4205. /** @addtogroup CRYP_Private_Functions
  4206. * @{
  4207. */
  4208. /**
  4209. * @brief Write the Key in KeyRx registers.
  4210. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4211. * the configuration information for CRYP module
  4212. * @retval None
  4213. */
  4214. static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp)
  4215. {
  4216. uint32_t keyaddr = 0x0U;
  4217. if ((uint32_t)(hcryp->Init.pKey == NULL))
  4218. {
  4219. return HAL_ERROR;
  4220. }
  4221. keyaddr = (uint32_t)(hcryp->Init.pKey);
  4222. if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
  4223. {
  4224. hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
  4225. keyaddr+=4U;
  4226. hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
  4227. keyaddr+=4U;
  4228. hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
  4229. keyaddr+=4U;
  4230. hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
  4231. keyaddr+=4U;
  4232. }
  4233. hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
  4234. keyaddr+=4U;
  4235. hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
  4236. keyaddr+=4U;
  4237. hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
  4238. keyaddr+=4U;
  4239. hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
  4240. return HAL_OK;
  4241. }
  4242. /**
  4243. * @brief Write the InitVector/InitCounter in IVRx registers.
  4244. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4245. * the configuration information for CRYP module
  4246. * @retval None
  4247. */
  4248. static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp)
  4249. {
  4250. uint32_t ivaddr = 0x0U;
  4251. #if !defined(AES_CR_NPBLB)
  4252. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  4253. {
  4254. hcryp->Instance->IVR3 = 0U;
  4255. hcryp->Instance->IVR2 = 0U;
  4256. hcryp->Instance->IVR1 = 0U;
  4257. hcryp->Instance->IVR0 = 0U;
  4258. }
  4259. else
  4260. #endif
  4261. {
  4262. if (hcryp->Init.pInitVect == NULL)
  4263. {
  4264. return HAL_ERROR;
  4265. }
  4266. ivaddr = (uint32_t)(hcryp->Init.pInitVect);
  4267. hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
  4268. ivaddr+=4U;
  4269. hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
  4270. ivaddr+=4U;
  4271. hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
  4272. ivaddr+=4U;
  4273. hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
  4274. }
  4275. return HAL_OK;
  4276. }
  4277. /**
  4278. * @brief Handle CRYP block input/output data handling under interruption.
  4279. * @note The function is called under interruption only, once
  4280. * interruptions have been enabled by HAL_CRYPEx_AES_IT().
  4281. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4282. * the configuration information for CRYP module.
  4283. * @retval HAL status
  4284. */
  4285. static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
  4286. {
  4287. uint32_t inputaddr = 0U;
  4288. uint32_t outputaddr = 0U;
  4289. if(hcryp->State == HAL_CRYP_STATE_BUSY)
  4290. {
  4291. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  4292. {
  4293. /* Get the output data address */
  4294. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  4295. /* Read the last available output block from the Data Output Register */
  4296. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4297. outputaddr+=4U;
  4298. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4299. outputaddr+=4U;
  4300. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4301. outputaddr+=4U;
  4302. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4303. hcryp->pCrypOutBuffPtr += 16U;
  4304. hcryp->CrypOutCount -= 16U;
  4305. }
  4306. else
  4307. {
  4308. /* Read the derived key from the Key registers */
  4309. if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
  4310. {
  4311. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
  4312. outputaddr+=4U;
  4313. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
  4314. outputaddr+=4U;
  4315. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
  4316. outputaddr+=4U;
  4317. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
  4318. outputaddr+=4U;
  4319. }
  4320. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
  4321. outputaddr+=4U;
  4322. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
  4323. outputaddr+=4U;
  4324. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
  4325. outputaddr+=4U;
  4326. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
  4327. }
  4328. /* In case of ciphering or deciphering, check if all output text has been retrieved;
  4329. In case of key derivation, stop right there */
  4330. if ((hcryp->CrypOutCount == 0U) || (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION))
  4331. {
  4332. /* Disable Computation Complete Flag and Errors Interrupts */
  4333. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4334. /* Change the CRYP state */
  4335. hcryp->State = HAL_CRYP_STATE_READY;
  4336. /* Process Unlocked */
  4337. __HAL_UNLOCK(hcryp);
  4338. /* Call computation complete callback */
  4339. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4340. return HAL_OK;
  4341. }
  4342. /* If suspension flag has been raised, suspend processing */
  4343. else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  4344. {
  4345. /* reset ModeSuspend */
  4346. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  4347. /* Disable Computation Complete Flag and Errors Interrupts */
  4348. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4349. /* Change the CRYP state */
  4350. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  4351. /* Process Unlocked */
  4352. __HAL_UNLOCK(hcryp);
  4353. return HAL_OK;
  4354. }
  4355. else /* Process the rest of input data */
  4356. {
  4357. /* Get the Intput data address */
  4358. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  4359. /* Increment/decrement instance pointer/counter */
  4360. hcryp->pCrypInBuffPtr += 16U;
  4361. hcryp->CrypInCount -= 16U;
  4362. /* Write the next input block in the Data Input register */
  4363. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4364. inputaddr+=4U;
  4365. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4366. inputaddr+=4U;
  4367. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4368. inputaddr+=4U;
  4369. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4370. return HAL_OK;
  4371. }
  4372. }
  4373. else
  4374. {
  4375. return HAL_BUSY;
  4376. }
  4377. }
  4378. /**
  4379. * @}
  4380. */
  4381. #endif /* AES */
  4382. #endif /* HAL_CRYP_MODULE_ENABLED */
  4383. /**
  4384. * @}
  4385. */
  4386. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/