stm32f4xx_hal_cryp_ex.c 214 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_cryp_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended CRYP HAL module driver
  6. * This file provides firmware functions to manage the following
  7. * functionalities of CRYP extension peripheral:
  8. * + Extended AES processing functions
  9. *
  10. @verbatim
  11. ==============================================================================
  12. ##### How to use this driver #####
  13. ==============================================================================
  14. [..]
  15. The CRYP Extension HAL driver can be used as follows:
  16. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  17. (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()
  18. (##) In case of using interrupts (e.g. HAL_CRYPEx_AESGCM_Encrypt_IT())
  19. (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  20. (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
  21. (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
  22. (##) In case of using DMA to control data transfer (e.g. HAL_AES_ECB_Encrypt_DMA())
  23. (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE()
  24. (+++) Configure and enable two DMA streams one for managing data transfer from
  25. memory to peripheral (input stream) and another stream for managing data
  26. transfer from peripheral to memory (output stream)
  27. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  28. using __HAL_LINKDMA()
  29. (+++) Configure the priority and enable the NVIC for the transfer complete
  30. interrupt on the two DMA Streams. The output stream should have higher
  31. priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
  32. (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly:
  33. (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit
  34. (##) The key size: 128, 192 and 256. This parameter is relevant only for AES
  35. (##) The encryption/decryption key. Its size depends on the algorithm
  36. used for encryption/decryption
  37. (##) The initialization vector (counter). It is not used ECB mode.
  38. (#)Three processing (encryption/decryption) functions are available:
  39. (##) Polling mode: encryption and decryption APIs are blocking functions
  40. i.e. they process the data and wait till the processing is finished
  41. e.g. HAL_CRYPEx_AESGCM_Encrypt()
  42. (##) Interrupt mode: encryption and decryption APIs are not blocking functions
  43. i.e. they process the data under interrupt
  44. e.g. HAL_CRYPEx_AESGCM_Encrypt_IT()
  45. (##) DMA mode: encryption and decryption APIs are not blocking functions
  46. i.e. the data transfer is ensured by DMA
  47. e.g. HAL_CRYPEx_AESGCM_Encrypt_DMA()
  48. (#)When the processing function is called at first time after HAL_CRYP_Init()
  49. the CRYP peripheral is initialized and processes the buffer in input.
  50. At second call, the processing function performs an append of the already
  51. processed buffer.
  52. When a new data block is to be processed, call HAL_CRYP_Init() then the
  53. processing function.
  54. (#)In AES-GCM and AES-CCM modes are an authenticated encryption algorithms
  55. which provide authentication messages.
  56. HAL_AES_GCM_Finish() and HAL_AES_CCM_Finish() are used to provide those
  57. authentication messages.
  58. Call those functions after the processing ones (polling, interrupt or DMA).
  59. e.g. in AES-CCM mode call HAL_CRYPEx_AESCCM_Encrypt() to encrypt the plain data
  60. then call HAL_CRYPEx_AESCCM_Finish() to get the authentication message
  61. -@- For CCM Encrypt/Decrypt API's, only DataType = 8-bit is supported by this version.
  62. -@- The HAL_CRYPEx_AESGCM_xxxx() implementation is limited to 32bits inputs data length
  63. (Plain/Cyphertext, Header) compared with GCM standards specifications (800-38D).
  64. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  65. @endverbatim
  66. ******************************************************************************
  67. * @attention
  68. *
  69. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  70. *
  71. * Redistribution and use in source and binary forms, with or without modification,
  72. * are permitted provided that the following conditions are met:
  73. * 1. Redistributions of source code must retain the above copyright notice,
  74. * this list of conditions and the following disclaimer.
  75. * 2. Redistributions in binary form must reproduce the above copyright notice,
  76. * this list of conditions and the following disclaimer in the documentation
  77. * and/or other materials provided with the distribution.
  78. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  79. * may be used to endorse or promote products derived from this software
  80. * without specific prior written permission.
  81. *
  82. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  83. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  84. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  85. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  86. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  87. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  88. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  89. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  90. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  91. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  92. *
  93. ******************************************************************************
  94. */
  95. /* Includes ------------------------------------------------------------------*/
  96. #include "stm32f4xx_hal.h"
  97. /** @addtogroup STM32F4xx_HAL_Driver
  98. * @{
  99. */
  100. /** @defgroup CRYPEx CRYPEx
  101. * @brief CRYP Extension HAL module driver.
  102. * @{
  103. */
  104. #ifdef HAL_CRYP_MODULE_ENABLED
  105. #if defined(CRYP)
  106. /* Private typedef -----------------------------------------------------------*/
  107. /* Private define ------------------------------------------------------------*/
  108. /** @addtogroup CRYPEx_Private_define
  109. * @{
  110. */
  111. #define CRYPEx_TIMEOUT_VALUE 1U
  112. /**
  113. * @}
  114. */
  115. /* Private macro -------------------------------------------------------------*/
  116. /* Private variables ---------------------------------------------------------*/
  117. /* Private function prototypes -----------------------------------------------*/
  118. /** @defgroup CRYPEx_Private_Functions_prototypes CRYP Private Functions Prototypes
  119. * @{
  120. */
  121. static void CRYPEx_GCMCCM_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector);
  122. static void CRYPEx_GCMCCM_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize);
  123. static HAL_StatusTypeDef CRYPEx_GCMCCM_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t *Input, uint16_t Ilength, uint8_t *Output, uint32_t Timeout);
  124. static HAL_StatusTypeDef CRYPEx_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint32_t Timeout);
  125. static void CRYPEx_GCMCCM_DMAInCplt(DMA_HandleTypeDef *hdma);
  126. static void CRYPEx_GCMCCM_DMAOutCplt(DMA_HandleTypeDef *hdma);
  127. static void CRYPEx_GCMCCM_DMAError(DMA_HandleTypeDef *hdma);
  128. static void CRYPEx_GCMCCM_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  129. /**
  130. * @}
  131. */
  132. /* Private functions ---------------------------------------------------------*/
  133. /** @addtogroup CRYPEx_Private_Functions
  134. * @{
  135. */
  136. /**
  137. * @brief DMA CRYP Input Data process complete callback.
  138. * @param hdma DMA handle
  139. * @retval None
  140. */
  141. static void CRYPEx_GCMCCM_DMAInCplt(DMA_HandleTypeDef *hdma)
  142. {
  143. CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  144. /* Disable the DMA transfer for input Fifo request by resetting the DIEN bit
  145. in the DMACR register */
  146. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN);
  147. /* Call input data transfer complete callback */
  148. HAL_CRYP_InCpltCallback(hcryp);
  149. }
  150. /**
  151. * @brief DMA CRYP Output Data process complete callback.
  152. * @param hdma DMA handle
  153. * @retval None
  154. */
  155. static void CRYPEx_GCMCCM_DMAOutCplt(DMA_HandleTypeDef *hdma)
  156. {
  157. CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  158. /* Disable the DMA transfer for output Fifo request by resetting the DOEN bit
  159. in the DMACR register */
  160. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN);
  161. /* Enable the CRYP peripheral */
  162. __HAL_CRYP_DISABLE(hcryp);
  163. /* Change the CRYP peripheral state */
  164. hcryp->State = HAL_CRYP_STATE_READY;
  165. /* Call output data transfer complete callback */
  166. HAL_CRYP_OutCpltCallback(hcryp);
  167. }
  168. /**
  169. * @brief DMA CRYP communication error callback.
  170. * @param hdma DMA handle
  171. * @retval None
  172. */
  173. static void CRYPEx_GCMCCM_DMAError(DMA_HandleTypeDef *hdma)
  174. {
  175. CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  176. hcryp->State= HAL_CRYP_STATE_READY;
  177. HAL_CRYP_ErrorCallback(hcryp);
  178. }
  179. /**
  180. * @brief Writes the Key in Key registers.
  181. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  182. * the configuration information for CRYP module
  183. * @param Key Pointer to Key buffer
  184. * @param KeySize Size of Key
  185. * @retval None
  186. */
  187. static void CRYPEx_GCMCCM_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize)
  188. {
  189. uint32_t keyaddr = (uint32_t)Key;
  190. switch(KeySize)
  191. {
  192. case CRYP_KEYSIZE_256B:
  193. /* Key Initialisation */
  194. hcryp->Instance->K0LR = __REV(*(uint32_t*)(keyaddr));
  195. keyaddr+=4U;
  196. hcryp->Instance->K0RR = __REV(*(uint32_t*)(keyaddr));
  197. keyaddr+=4U;
  198. hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));
  199. keyaddr+=4U;
  200. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  201. keyaddr+=4U;
  202. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  203. keyaddr+=4U;
  204. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  205. keyaddr+=4U;
  206. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  207. keyaddr+=4U;
  208. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  209. break;
  210. case CRYP_KEYSIZE_192B:
  211. hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));
  212. keyaddr+=4U;
  213. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  214. keyaddr+=4U;
  215. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  216. keyaddr+=4U;
  217. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  218. keyaddr+=4U;
  219. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  220. keyaddr+=4U;
  221. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  222. break;
  223. case CRYP_KEYSIZE_128B:
  224. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  225. keyaddr+=4U;
  226. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  227. keyaddr+=4U;
  228. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  229. keyaddr+=4U;
  230. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. /**
  237. * @brief Writes the InitVector/InitCounter in IV registers.
  238. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  239. * the configuration information for CRYP module
  240. * @param InitVector Pointer to InitVector/InitCounter buffer
  241. * @retval None
  242. */
  243. static void CRYPEx_GCMCCM_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector)
  244. {
  245. uint32_t ivaddr = (uint32_t)InitVector;
  246. hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr));
  247. ivaddr+=4U;
  248. hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr));
  249. ivaddr+=4U;
  250. hcryp->Instance->IV1LR = __REV(*(uint32_t*)(ivaddr));
  251. ivaddr+=4U;
  252. hcryp->Instance->IV1RR = __REV(*(uint32_t*)(ivaddr));
  253. }
  254. /**
  255. * @brief Process Data: Writes Input data in polling mode and read the Output data.
  256. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  257. * the configuration information for CRYP module
  258. * @param Input Pointer to the Input buffer.
  259. * @param Ilength Length of the Input buffer, must be a multiple of 16
  260. * @param Output Pointer to the returned buffer
  261. * @param Timeout Timeout value
  262. * @retval None
  263. */
  264. static HAL_StatusTypeDef CRYPEx_GCMCCM_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t *Input, uint16_t Ilength, uint8_t *Output, uint32_t Timeout)
  265. {
  266. uint32_t tickstart = 0U;
  267. uint32_t i = 0U;
  268. uint32_t inputaddr = (uint32_t)Input;
  269. uint32_t outputaddr = (uint32_t)Output;
  270. for(i=0U; (i < Ilength); i+=16U)
  271. {
  272. /* Write the Input block in the IN FIFO */
  273. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  274. inputaddr+=4U;
  275. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  276. inputaddr+=4U;
  277. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  278. inputaddr+=4U;
  279. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  280. inputaddr+=4U;
  281. /* Get tick */
  282. tickstart = HAL_GetTick();
  283. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  284. {
  285. /* Check for the Timeout */
  286. if(Timeout != HAL_MAX_DELAY)
  287. {
  288. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  289. {
  290. /* Change state */
  291. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  292. /* Process Unlocked */
  293. __HAL_UNLOCK(hcryp);
  294. return HAL_TIMEOUT;
  295. }
  296. }
  297. }
  298. /* Read the Output block from the OUT FIFO */
  299. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  300. outputaddr+=4U;
  301. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  302. outputaddr+=4U;
  303. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  304. outputaddr+=4U;
  305. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  306. outputaddr+=4U;
  307. }
  308. /* Return function status */
  309. return HAL_OK;
  310. }
  311. /**
  312. * @brief Sets the header phase
  313. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  314. * the configuration information for CRYP module
  315. * @param Input Pointer to the Input buffer.
  316. * @param Ilength Length of the Input buffer, must be a multiple of 16
  317. * @param Timeout Timeout value
  318. * @retval None
  319. */
  320. static HAL_StatusTypeDef CRYPEx_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint32_t Timeout)
  321. {
  322. uint32_t tickstart = 0U;
  323. uint32_t loopcounter = 0U;
  324. uint32_t headeraddr = (uint32_t)Input;
  325. /* Prevent unused argument(s) compilation warning */
  326. UNUSED(Ilength);
  327. /***************************** Header phase *********************************/
  328. if(hcryp->Init.HeaderSize != 0U)
  329. {
  330. /* Select header phase */
  331. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  332. /* Enable the CRYP peripheral */
  333. __HAL_CRYP_ENABLE(hcryp);
  334. for(loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter+=16U)
  335. {
  336. /* Get tick */
  337. tickstart = HAL_GetTick();
  338. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  339. {
  340. /* Check for the Timeout */
  341. if(Timeout != HAL_MAX_DELAY)
  342. {
  343. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  344. {
  345. /* Change state */
  346. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  347. /* Process Unlocked */
  348. __HAL_UNLOCK(hcryp);
  349. return HAL_TIMEOUT;
  350. }
  351. }
  352. }
  353. /* Write the Input block in the IN FIFO */
  354. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  355. headeraddr+=4U;
  356. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  357. headeraddr+=4U;
  358. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  359. headeraddr+=4U;
  360. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  361. headeraddr+=4U;
  362. }
  363. /* Wait until the complete message has been processed */
  364. /* Get tick */
  365. tickstart = HAL_GetTick();
  366. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  367. {
  368. /* Check for the Timeout */
  369. if(Timeout != HAL_MAX_DELAY)
  370. {
  371. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  372. {
  373. /* Change state */
  374. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  375. /* Process Unlocked */
  376. __HAL_UNLOCK(hcryp);
  377. return HAL_TIMEOUT;
  378. }
  379. }
  380. }
  381. }
  382. /* Return function status */
  383. return HAL_OK;
  384. }
  385. /**
  386. * @brief Sets the DMA configuration and start the DMA transfer.
  387. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  388. * the configuration information for CRYP module
  389. * @param inputaddr Address of the Input buffer
  390. * @param Size Size of the Input buffer, must be a multiple of 16
  391. * @param outputaddr Address of the Output buffer
  392. * @retval None
  393. */
  394. static void CRYPEx_GCMCCM_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  395. {
  396. /* Set the CRYP DMA transfer complete callback */
  397. hcryp->hdmain->XferCpltCallback = CRYPEx_GCMCCM_DMAInCplt;
  398. /* Set the DMA error callback */
  399. hcryp->hdmain->XferErrorCallback = CRYPEx_GCMCCM_DMAError;
  400. /* Set the CRYP DMA transfer complete callback */
  401. hcryp->hdmaout->XferCpltCallback = CRYPEx_GCMCCM_DMAOutCplt;
  402. /* Set the DMA error callback */
  403. hcryp->hdmaout->XferErrorCallback = CRYPEx_GCMCCM_DMAError;
  404. /* Enable the CRYP peripheral */
  405. __HAL_CRYP_ENABLE(hcryp);
  406. /* Enable the DMA In DMA Stream */
  407. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DR, Size/4U);
  408. /* Enable In DMA request */
  409. hcryp->Instance->DMACR = CRYP_DMACR_DIEN;
  410. /* Enable the DMA Out DMA Stream */
  411. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size/4U);
  412. /* Enable Out DMA request */
  413. hcryp->Instance->DMACR |= CRYP_DMACR_DOEN;
  414. }
  415. /**
  416. * @}
  417. */
  418. /* Exported functions---------------------------------------------------------*/
  419. /** @addtogroup CRYPEx_Exported_Functions
  420. * @{
  421. */
  422. /** @defgroup CRYPEx_Exported_Functions_Group1 Extended AES processing functions
  423. * @brief Extended processing functions.
  424. *
  425. @verbatim
  426. ==============================================================================
  427. ##### Extended AES processing functions #####
  428. ==============================================================================
  429. [..] This section provides functions allowing to:
  430. (+) Encrypt plaintext using AES-128/192/256 using GCM and CCM chaining modes
  431. (+) Decrypt cyphertext using AES-128/192/256 using GCM and CCM chaining modes
  432. (+) Finish the processing. This function is available only for GCM and CCM
  433. [..] Three processing methods are available:
  434. (+) Polling mode
  435. (+) Interrupt mode
  436. (+) DMA mode
  437. @endverbatim
  438. * @{
  439. */
  440. /**
  441. * @brief Initializes the CRYP peripheral in AES CCM encryption mode then
  442. * encrypt pPlainData. The cypher data are available in pCypherData.
  443. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  444. * the configuration information for CRYP module
  445. * @param pPlainData Pointer to the plaintext buffer
  446. * @param Size Length of the plaintext buffer, must be a multiple of 16
  447. * @param pCypherData Pointer to the cyphertext buffer
  448. * @param Timeout Timeout duration
  449. * @retval HAL status
  450. */
  451. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  452. {
  453. uint32_t tickstart = 0U;
  454. uint32_t headersize = hcryp->Init.HeaderSize;
  455. uint32_t headeraddr = (uint32_t)hcryp->Init.Header;
  456. uint32_t loopcounter = 0U;
  457. uint32_t bufferidx = 0U;
  458. uint8_t blockb0[16U] = {0};/* Block B0 */
  459. uint8_t ctr[16U] = {0}; /* Counter */
  460. uint32_t b0addr = (uint32_t)blockb0;
  461. /* Process Locked */
  462. __HAL_LOCK(hcryp);
  463. /* Change the CRYP peripheral state */
  464. hcryp->State = HAL_CRYP_STATE_BUSY;
  465. /* Check if initialization phase has already been performed */
  466. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  467. {
  468. /************************ Formatting the header block *********************/
  469. if(headersize != 0U)
  470. {
  471. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  472. if(headersize < 65280U)
  473. {
  474. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  475. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  476. headersize += 2U;
  477. }
  478. else
  479. {
  480. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  481. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  482. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  483. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  484. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  485. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  486. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  487. headersize += 6U;
  488. }
  489. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  490. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  491. {
  492. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  493. }
  494. /* Check if the header size is modulo 16 */
  495. if ((headersize % 16U) != 0U)
  496. {
  497. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  498. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  499. {
  500. hcryp->Init.pScratch[loopcounter] = 0U;
  501. }
  502. /* Set the header size to modulo 16 */
  503. headersize = ((headersize/16U) + 1U) * 16U;
  504. }
  505. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  506. headeraddr = (uint32_t)hcryp->Init.pScratch;
  507. }
  508. /*********************** Formatting the block B0 **************************/
  509. if(headersize != 0U)
  510. {
  511. blockb0[0U] = 0x40U;
  512. }
  513. /* Flags byte */
  514. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  515. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U);
  516. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);
  517. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  518. {
  519. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  520. }
  521. for ( ; loopcounter < 13U; loopcounter++)
  522. {
  523. blockb0[loopcounter+1U] = 0U;
  524. }
  525. blockb0[14U] = (Size >> 8U);
  526. blockb0[15U] = (Size & 0xFFU);
  527. /************************* Formatting the initial counter *****************/
  528. /* Byte 0:
  529. Bits 7 and 6 are reserved and shall be set to 0
  530. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter blocks
  531. are distinct from B0
  532. Bits 0, 1, and 2 contain the same encoding of q as in B0
  533. */
  534. ctr[0U] = blockb0[0U] & 0x07U;
  535. /* byte 1 to NonceSize is the IV (Nonce) */
  536. for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  537. {
  538. ctr[loopcounter] = blockb0[loopcounter];
  539. }
  540. /* Set the LSB to 1 */
  541. ctr[15U] |= 0x01U;
  542. /* Set the key */
  543. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  544. /* Set the CRYP peripheral in AES CCM mode */
  545. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT);
  546. /* Set the Initialization Vector */
  547. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  548. /* Select init phase */
  549. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  550. b0addr = (uint32_t)blockb0;
  551. /* Write the blockb0 block in the IN FIFO */
  552. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  553. b0addr+=4U;
  554. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  555. b0addr+=4U;
  556. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  557. b0addr+=4U;
  558. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  559. /* Enable the CRYP peripheral */
  560. __HAL_CRYP_ENABLE(hcryp);
  561. /* Get tick */
  562. tickstart = HAL_GetTick();
  563. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  564. {
  565. /* Check for the Timeout */
  566. if(Timeout != HAL_MAX_DELAY)
  567. {
  568. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  569. {
  570. /* Change state */
  571. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  572. /* Process Unlocked */
  573. __HAL_UNLOCK(hcryp);
  574. return HAL_TIMEOUT;
  575. }
  576. }
  577. }
  578. /***************************** Header phase *******************************/
  579. if(headersize != 0U)
  580. {
  581. /* Select header phase */
  582. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  583. /* Enable the CRYP peripheral */
  584. __HAL_CRYP_ENABLE(hcryp);
  585. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  586. {
  587. /* Get tick */
  588. tickstart = HAL_GetTick();
  589. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  590. {
  591. {
  592. /* Check for the Timeout */
  593. if(Timeout != HAL_MAX_DELAY)
  594. {
  595. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  596. {
  597. /* Change state */
  598. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  599. /* Process Unlocked */
  600. __HAL_UNLOCK(hcryp);
  601. return HAL_TIMEOUT;
  602. }
  603. }
  604. }
  605. }
  606. /* Write the header block in the IN FIFO */
  607. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  608. headeraddr+=4U;
  609. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  610. headeraddr+=4U;
  611. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  612. headeraddr+=4U;
  613. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  614. headeraddr+=4U;
  615. }
  616. /* Get tick */
  617. tickstart = HAL_GetTick();
  618. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  619. {
  620. /* Check for the Timeout */
  621. if(Timeout != HAL_MAX_DELAY)
  622. {
  623. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  624. {
  625. /* Change state */
  626. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  627. /* Process Unlocked */
  628. __HAL_UNLOCK(hcryp);
  629. return HAL_TIMEOUT;
  630. }
  631. }
  632. }
  633. }
  634. /* Save formatted counter into the scratch buffer pScratch */
  635. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  636. {
  637. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  638. }
  639. /* Reset bit 0 */
  640. hcryp->Init.pScratch[15U] &= 0xFEU;
  641. /* Select payload phase once the header phase is performed */
  642. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  643. /* Flush FIFO */
  644. __HAL_CRYP_FIFO_FLUSH(hcryp);
  645. /* Enable the CRYP peripheral */
  646. __HAL_CRYP_ENABLE(hcryp);
  647. /* Set the phase */
  648. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  649. }
  650. /* Write Plain Data and Get Cypher Data */
  651. if(CRYPEx_GCMCCM_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  652. {
  653. return HAL_TIMEOUT;
  654. }
  655. /* Change the CRYP peripheral state */
  656. hcryp->State = HAL_CRYP_STATE_READY;
  657. /* Process Unlocked */
  658. __HAL_UNLOCK(hcryp);
  659. /* Return function status */
  660. return HAL_OK;
  661. }
  662. /**
  663. * @brief Initializes the CRYP peripheral in AES GCM encryption mode then
  664. * encrypt pPlainData. The cypher data are available in pCypherData.
  665. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  666. * the configuration information for CRYP module
  667. * @param pPlainData Pointer to the plaintext buffer
  668. * @param Size Length of the plaintext buffer, must be a multiple of 16
  669. * @param pCypherData Pointer to the cyphertext buffer
  670. * @param Timeout Timeout duration
  671. * @retval HAL status
  672. */
  673. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  674. {
  675. uint32_t tickstart = 0U;
  676. /* Process Locked */
  677. __HAL_LOCK(hcryp);
  678. /* Change the CRYP peripheral state */
  679. hcryp->State = HAL_CRYP_STATE_BUSY;
  680. /* Check if initialization phase has already been performed */
  681. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  682. {
  683. /* Set the key */
  684. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  685. /* Set the CRYP peripheral in AES GCM mode */
  686. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT);
  687. /* Set the Initialization Vector */
  688. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  689. /* Flush FIFO */
  690. __HAL_CRYP_FIFO_FLUSH(hcryp);
  691. /* Enable the CRYP peripheral */
  692. __HAL_CRYP_ENABLE(hcryp);
  693. /* Get tick */
  694. tickstart = HAL_GetTick();
  695. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  696. {
  697. /* Check for the Timeout */
  698. if(Timeout != HAL_MAX_DELAY)
  699. {
  700. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  701. {
  702. /* Change state */
  703. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  704. /* Process Unlocked */
  705. __HAL_UNLOCK(hcryp);
  706. return HAL_TIMEOUT;
  707. }
  708. }
  709. }
  710. /* Set the header phase */
  711. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, Timeout) != HAL_OK)
  712. {
  713. return HAL_TIMEOUT;
  714. }
  715. /* Disable the CRYP peripheral */
  716. __HAL_CRYP_DISABLE(hcryp);
  717. /* Select payload phase once the header phase is performed */
  718. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  719. /* Flush FIFO */
  720. __HAL_CRYP_FIFO_FLUSH(hcryp);
  721. /* Enable the CRYP peripheral */
  722. __HAL_CRYP_ENABLE(hcryp);
  723. /* Set the phase */
  724. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  725. }
  726. /* Write Plain Data and Get Cypher Data */
  727. if(CRYPEx_GCMCCM_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  728. {
  729. return HAL_TIMEOUT;
  730. }
  731. /* Change the CRYP peripheral state */
  732. hcryp->State = HAL_CRYP_STATE_READY;
  733. /* Process Unlocked */
  734. __HAL_UNLOCK(hcryp);
  735. /* Return function status */
  736. return HAL_OK;
  737. }
  738. /**
  739. * @brief Initializes the CRYP peripheral in AES GCM decryption mode then
  740. * decrypted pCypherData. The cypher data are available in pPlainData.
  741. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  742. * the configuration information for CRYP module
  743. * @param pCypherData Pointer to the cyphertext buffer
  744. * @param Size Length of the cyphertext buffer, must be a multiple of 16
  745. * @param pPlainData Pointer to the plaintext buffer
  746. * @param Timeout Timeout duration
  747. * @retval HAL status
  748. */
  749. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  750. {
  751. uint32_t tickstart = 0U;
  752. /* Process Locked */
  753. __HAL_LOCK(hcryp);
  754. /* Change the CRYP peripheral state */
  755. hcryp->State = HAL_CRYP_STATE_BUSY;
  756. /* Check if initialization phase has already been performed */
  757. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  758. {
  759. /* Set the key */
  760. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  761. /* Set the CRYP peripheral in AES GCM decryption mode */
  762. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT);
  763. /* Set the Initialization Vector */
  764. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  765. /* Flush FIFO */
  766. __HAL_CRYP_FIFO_FLUSH(hcryp);
  767. /* Enable the CRYP peripheral */
  768. __HAL_CRYP_ENABLE(hcryp);
  769. /* Get tick */
  770. tickstart = HAL_GetTick();
  771. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  772. {
  773. /* Check for the Timeout */
  774. if(Timeout != HAL_MAX_DELAY)
  775. {
  776. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  777. {
  778. /* Change state */
  779. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  780. /* Process Unlocked */
  781. __HAL_UNLOCK(hcryp);
  782. return HAL_TIMEOUT;
  783. }
  784. }
  785. }
  786. /* Set the header phase */
  787. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, Timeout) != HAL_OK)
  788. {
  789. return HAL_TIMEOUT;
  790. }
  791. /* Disable the CRYP peripheral */
  792. __HAL_CRYP_DISABLE(hcryp);
  793. /* Select payload phase once the header phase is performed */
  794. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  795. /* Enable the CRYP peripheral */
  796. __HAL_CRYP_ENABLE(hcryp);
  797. /* Set the phase */
  798. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  799. }
  800. /* Write Plain Data and Get Cypher Data */
  801. if(CRYPEx_GCMCCM_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  802. {
  803. return HAL_TIMEOUT;
  804. }
  805. /* Change the CRYP peripheral state */
  806. hcryp->State = HAL_CRYP_STATE_READY;
  807. /* Process Unlocked */
  808. __HAL_UNLOCK(hcryp);
  809. /* Return function status */
  810. return HAL_OK;
  811. }
  812. /**
  813. * @brief Computes the authentication TAG.
  814. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  815. * the configuration information for CRYP module
  816. * @param Size Total length of the plain/cyphertext buffer
  817. * @param AuthTag Pointer to the authentication buffer
  818. * @param Timeout Timeout duration
  819. * @retval HAL status
  820. */
  821. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Finish(CRYP_HandleTypeDef *hcryp, uint32_t Size, uint8_t *AuthTag, uint32_t Timeout)
  822. {
  823. uint32_t tickstart = 0U;
  824. uint64_t headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
  825. uint64_t inputlength = Size * 8U; /* input length in bits */
  826. uint32_t tagaddr = (uint32_t)AuthTag;
  827. /* Process Locked */
  828. __HAL_LOCK(hcryp);
  829. /* Change the CRYP peripheral state */
  830. hcryp->State = HAL_CRYP_STATE_BUSY;
  831. /* Check if initialization phase has already been performed */
  832. if(hcryp->Phase == HAL_CRYP_PHASE_PROCESS)
  833. {
  834. /* Change the CRYP phase */
  835. hcryp->Phase = HAL_CRYP_PHASE_FINAL;
  836. /* Disable CRYP to start the final phase */
  837. __HAL_CRYP_DISABLE(hcryp);
  838. /* Select final phase */
  839. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_FINAL);
  840. /* Enable the CRYP peripheral */
  841. __HAL_CRYP_ENABLE(hcryp);
  842. /* Write the number of bits in header (64 bits) followed by the number of bits
  843. in the payload */
  844. if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  845. {
  846. hcryp->Instance->DR = __RBIT(headerlength >> 32U);
  847. hcryp->Instance->DR = __RBIT(headerlength);
  848. hcryp->Instance->DR = __RBIT(inputlength >> 32U);
  849. hcryp->Instance->DR = __RBIT(inputlength);
  850. }
  851. else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  852. {
  853. hcryp->Instance->DR = __REV(headerlength >> 32U);
  854. hcryp->Instance->DR = __REV(headerlength);
  855. hcryp->Instance->DR = __REV(inputlength >> 32U);
  856. hcryp->Instance->DR = __REV(inputlength);
  857. }
  858. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  859. {
  860. hcryp->Instance->DR = __ROR((uint32_t)(headerlength >> 32U), 16U);
  861. hcryp->Instance->DR = __ROR((uint32_t)headerlength, 16U);
  862. hcryp->Instance->DR = __ROR((uint32_t)(inputlength >> 32U), 16U);
  863. hcryp->Instance->DR = __ROR((uint32_t)inputlength, 16U);
  864. }
  865. else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
  866. {
  867. hcryp->Instance->DR = (uint32_t)(headerlength >> 32U);
  868. hcryp->Instance->DR = (uint32_t)(headerlength);
  869. hcryp->Instance->DR = (uint32_t)(inputlength >> 32U);
  870. hcryp->Instance->DR = (uint32_t)(inputlength);
  871. }
  872. /* Get tick */
  873. tickstart = HAL_GetTick();
  874. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  875. {
  876. /* Check for the Timeout */
  877. if(Timeout != HAL_MAX_DELAY)
  878. {
  879. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  880. {
  881. /* Change state */
  882. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  883. /* Process Unlocked */
  884. __HAL_UNLOCK(hcryp);
  885. return HAL_TIMEOUT;
  886. }
  887. }
  888. }
  889. /* Read the Auth TAG in the IN FIFO */
  890. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;
  891. tagaddr+=4U;
  892. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;
  893. tagaddr+=4U;
  894. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;
  895. tagaddr+=4U;
  896. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;
  897. }
  898. /* Change the CRYP peripheral state */
  899. hcryp->State = HAL_CRYP_STATE_READY;
  900. /* Process Unlocked */
  901. __HAL_UNLOCK(hcryp);
  902. /* Return function status */
  903. return HAL_OK;
  904. }
  905. /**
  906. * @brief Computes the authentication TAG for AES CCM mode.
  907. * @note This API is called after HAL_AES_CCM_Encrypt()/HAL_AES_CCM_Decrypt()
  908. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  909. * the configuration information for CRYP module
  910. * @param AuthTag Pointer to the authentication buffer
  911. * @param Timeout Timeout duration
  912. * @retval HAL status
  913. */
  914. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Finish(CRYP_HandleTypeDef *hcryp, uint8_t *AuthTag, uint32_t Timeout)
  915. {
  916. uint32_t tickstart = 0U;
  917. uint32_t tagaddr = (uint32_t)AuthTag;
  918. uint32_t ctraddr = (uint32_t)hcryp->Init.pScratch;
  919. uint32_t temptag[4U] = {0U}; /* Temporary TAG (MAC) */
  920. uint32_t loopcounter;
  921. /* Process Locked */
  922. __HAL_LOCK(hcryp);
  923. /* Change the CRYP peripheral state */
  924. hcryp->State = HAL_CRYP_STATE_BUSY;
  925. /* Check if initialization phase has already been performed */
  926. if(hcryp->Phase == HAL_CRYP_PHASE_PROCESS)
  927. {
  928. /* Change the CRYP phase */
  929. hcryp->Phase = HAL_CRYP_PHASE_FINAL;
  930. /* Disable CRYP to start the final phase */
  931. __HAL_CRYP_DISABLE(hcryp);
  932. /* Select final phase */
  933. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_FINAL);
  934. /* Enable the CRYP peripheral */
  935. __HAL_CRYP_ENABLE(hcryp);
  936. /* Write the counter block in the IN FIFO */
  937. hcryp->Instance->DR = *(uint32_t*)ctraddr;
  938. ctraddr+=4U;
  939. hcryp->Instance->DR = *(uint32_t*)ctraddr;
  940. ctraddr+=4U;
  941. hcryp->Instance->DR = *(uint32_t*)ctraddr;
  942. ctraddr+=4U;
  943. hcryp->Instance->DR = *(uint32_t*)ctraddr;
  944. /* Get tick */
  945. tickstart = HAL_GetTick();
  946. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  947. {
  948. /* Check for the Timeout */
  949. if(Timeout != HAL_MAX_DELAY)
  950. {
  951. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  952. {
  953. /* Change state */
  954. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  955. /* Process Unlocked */
  956. __HAL_UNLOCK(hcryp);
  957. return HAL_TIMEOUT;
  958. }
  959. }
  960. }
  961. /* Read the Auth TAG in the IN FIFO */
  962. temptag[0U] = hcryp->Instance->DOUT;
  963. temptag[1U] = hcryp->Instance->DOUT;
  964. temptag[2U] = hcryp->Instance->DOUT;
  965. temptag[3U] = hcryp->Instance->DOUT;
  966. }
  967. /* Copy temporary authentication TAG in user TAG buffer */
  968. for(loopcounter = 0U; loopcounter < hcryp->Init.TagSize ; loopcounter++)
  969. {
  970. /* Set the authentication TAG buffer */
  971. *((uint8_t*)tagaddr+loopcounter) = *((uint8_t*)temptag+loopcounter);
  972. }
  973. /* Change the CRYP peripheral state */
  974. hcryp->State = HAL_CRYP_STATE_READY;
  975. /* Process Unlocked */
  976. __HAL_UNLOCK(hcryp);
  977. /* Return function status */
  978. return HAL_OK;
  979. }
  980. /**
  981. * @brief Initializes the CRYP peripheral in AES CCM decryption mode then
  982. * decrypted pCypherData. The cypher data are available in pPlainData.
  983. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  984. * the configuration information for CRYP module
  985. * @param pPlainData Pointer to the plaintext buffer
  986. * @param Size Length of the plaintext buffer, must be a multiple of 16
  987. * @param pCypherData Pointer to the cyphertext buffer
  988. * @param Timeout Timeout duration
  989. * @retval HAL status
  990. */
  991. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  992. {
  993. uint32_t tickstart = 0U;
  994. uint32_t headersize = hcryp->Init.HeaderSize;
  995. uint32_t headeraddr = (uint32_t)hcryp->Init.Header;
  996. uint32_t loopcounter = 0U;
  997. uint32_t bufferidx = 0U;
  998. uint8_t blockb0[16U] = {0};/* Block B0 */
  999. uint8_t ctr[16U] = {0}; /* Counter */
  1000. uint32_t b0addr = (uint32_t)blockb0;
  1001. /* Process Locked */
  1002. __HAL_LOCK(hcryp);
  1003. /* Change the CRYP peripheral state */
  1004. hcryp->State = HAL_CRYP_STATE_BUSY;
  1005. /* Check if initialization phase has already been performed */
  1006. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1007. {
  1008. /************************ Formatting the header block *********************/
  1009. if(headersize != 0U)
  1010. {
  1011. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  1012. if(headersize < 65280U)
  1013. {
  1014. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  1015. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  1016. headersize += 2U;
  1017. }
  1018. else
  1019. {
  1020. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  1021. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  1022. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  1023. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  1024. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  1025. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  1026. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  1027. headersize += 6U;
  1028. }
  1029. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  1030. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  1031. {
  1032. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  1033. }
  1034. /* Check if the header size is modulo 16 */
  1035. if ((headersize % 16U) != 0U)
  1036. {
  1037. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  1038. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  1039. {
  1040. hcryp->Init.pScratch[loopcounter] = 0U;
  1041. }
  1042. /* Set the header size to modulo 16 */
  1043. headersize = ((headersize/16U) + 1U) * 16U;
  1044. }
  1045. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  1046. headeraddr = (uint32_t)hcryp->Init.pScratch;
  1047. }
  1048. /*********************** Formatting the block B0 **************************/
  1049. if(headersize != 0U)
  1050. {
  1051. blockb0[0U] = 0x40U;
  1052. }
  1053. /* Flags byte */
  1054. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  1055. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2U))) >> 1U) & (uint8_t)0x07U) << 3U);
  1056. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15U) - hcryp->Init.IVSize) - (uint8_t)1U) & (uint8_t)0x07U);
  1057. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  1058. {
  1059. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  1060. }
  1061. for ( ; loopcounter < 13U; loopcounter++)
  1062. {
  1063. blockb0[loopcounter+1U] = 0U;
  1064. }
  1065. blockb0[14U] = (Size >> 8U);
  1066. blockb0[15U] = (Size & 0xFFU);
  1067. /************************* Formatting the initial counter *****************/
  1068. /* Byte 0:
  1069. Bits 7 and 6 are reserved and shall be set to 0
  1070. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter
  1071. blocks are distinct from B0
  1072. Bits 0, 1, and 2 contain the same encoding of q as in B0
  1073. */
  1074. ctr[0U] = blockb0[0U] & 0x07U;
  1075. /* byte 1 to NonceSize is the IV (Nonce) */
  1076. for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  1077. {
  1078. ctr[loopcounter] = blockb0[loopcounter];
  1079. }
  1080. /* Set the LSB to 1 */
  1081. ctr[15U] |= 0x01U;
  1082. /* Set the key */
  1083. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1084. /* Set the CRYP peripheral in AES CCM mode */
  1085. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT);
  1086. /* Set the Initialization Vector */
  1087. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  1088. /* Select init phase */
  1089. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  1090. b0addr = (uint32_t)blockb0;
  1091. /* Write the blockb0 block in the IN FIFO */
  1092. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1093. b0addr+=4U;
  1094. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1095. b0addr+=4U;
  1096. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1097. b0addr+=4U;
  1098. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1099. /* Enable the CRYP peripheral */
  1100. __HAL_CRYP_ENABLE(hcryp);
  1101. /* Get tick */
  1102. tickstart = HAL_GetTick();
  1103. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  1104. {
  1105. /* Check for the Timeout */
  1106. if(Timeout != HAL_MAX_DELAY)
  1107. {
  1108. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1109. {
  1110. /* Change state */
  1111. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1112. /* Process Unlocked */
  1113. __HAL_UNLOCK(hcryp);
  1114. return HAL_TIMEOUT;
  1115. }
  1116. }
  1117. }
  1118. /***************************** Header phase *******************************/
  1119. if(headersize != 0U)
  1120. {
  1121. /* Select header phase */
  1122. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  1123. /* Enable Crypto processor */
  1124. __HAL_CRYP_ENABLE(hcryp);
  1125. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  1126. {
  1127. /* Get tick */
  1128. tickstart = HAL_GetTick();
  1129. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  1130. {
  1131. /* Check for the Timeout */
  1132. if(Timeout != HAL_MAX_DELAY)
  1133. {
  1134. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1135. {
  1136. /* Change state */
  1137. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1138. /* Process Unlocked */
  1139. __HAL_UNLOCK(hcryp);
  1140. return HAL_TIMEOUT;
  1141. }
  1142. }
  1143. }
  1144. /* Write the header block in the IN FIFO */
  1145. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1146. headeraddr+=4U;
  1147. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1148. headeraddr+=4U;
  1149. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1150. headeraddr+=4U;
  1151. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1152. headeraddr+=4U;
  1153. }
  1154. /* Get tick */
  1155. tickstart = HAL_GetTick();
  1156. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  1157. {
  1158. /* Check for the Timeout */
  1159. if(Timeout != HAL_MAX_DELAY)
  1160. {
  1161. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1162. {
  1163. /* Change state */
  1164. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1165. /* Process Unlocked */
  1166. __HAL_UNLOCK(hcryp);
  1167. return HAL_TIMEOUT;
  1168. }
  1169. }
  1170. }
  1171. }
  1172. /* Save formatted counter into the scratch buffer pScratch */
  1173. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  1174. {
  1175. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  1176. }
  1177. /* Reset bit 0 */
  1178. hcryp->Init.pScratch[15U] &= 0xFEU;
  1179. /* Select payload phase once the header phase is performed */
  1180. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  1181. /* Flush FIFO */
  1182. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1183. /* Enable the CRYP peripheral */
  1184. __HAL_CRYP_ENABLE(hcryp);
  1185. /* Set the phase */
  1186. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1187. }
  1188. /* Write Plain Data and Get Cypher Data */
  1189. if(CRYPEx_GCMCCM_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  1190. {
  1191. return HAL_TIMEOUT;
  1192. }
  1193. /* Change the CRYP peripheral state */
  1194. hcryp->State = HAL_CRYP_STATE_READY;
  1195. /* Process Unlocked */
  1196. __HAL_UNLOCK(hcryp);
  1197. /* Return function status */
  1198. return HAL_OK;
  1199. }
  1200. /**
  1201. * @brief Initializes the CRYP peripheral in AES GCM encryption mode using IT.
  1202. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1203. * the configuration information for CRYP module
  1204. * @param pPlainData Pointer to the plaintext buffer
  1205. * @param Size Length of the plaintext buffer, must be a multiple of 16
  1206. * @param pCypherData Pointer to the cyphertext buffer
  1207. * @retval HAL status
  1208. */
  1209. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1210. {
  1211. uint32_t tickstart = 0U;
  1212. uint32_t inputaddr;
  1213. uint32_t outputaddr;
  1214. if(hcryp->State == HAL_CRYP_STATE_READY)
  1215. {
  1216. /* Process Locked */
  1217. __HAL_LOCK(hcryp);
  1218. /* Get the buffer addresses and sizes */
  1219. hcryp->CrypInCount = Size;
  1220. hcryp->pCrypInBuffPtr = pPlainData;
  1221. hcryp->pCrypOutBuffPtr = pCypherData;
  1222. hcryp->CrypOutCount = Size;
  1223. /* Change the CRYP peripheral state */
  1224. hcryp->State = HAL_CRYP_STATE_BUSY;
  1225. /* Check if initialization phase has already been performed */
  1226. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1227. {
  1228. /* Set the key */
  1229. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1230. /* Set the CRYP peripheral in AES GCM mode */
  1231. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT);
  1232. /* Set the Initialization Vector */
  1233. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  1234. /* Flush FIFO */
  1235. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1236. /* Enable CRYP to start the init phase */
  1237. __HAL_CRYP_ENABLE(hcryp);
  1238. /* Get tick */
  1239. tickstart = HAL_GetTick();
  1240. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  1241. {
  1242. /* Check for the Timeout */
  1243. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1244. {
  1245. /* Change state */
  1246. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1247. /* Process Unlocked */
  1248. __HAL_UNLOCK(hcryp);
  1249. return HAL_TIMEOUT;
  1250. }
  1251. }
  1252. /* Set the header phase */
  1253. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK)
  1254. {
  1255. return HAL_TIMEOUT;
  1256. }
  1257. /* Disable the CRYP peripheral */
  1258. __HAL_CRYP_DISABLE(hcryp);
  1259. /* Select payload phase once the header phase is performed */
  1260. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  1261. /* Flush FIFO */
  1262. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1263. /* Set the phase */
  1264. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1265. }
  1266. if(Size != 0U)
  1267. {
  1268. /* Enable Interrupts */
  1269. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1270. /* Enable the CRYP peripheral */
  1271. __HAL_CRYP_ENABLE(hcryp);
  1272. }
  1273. else
  1274. {
  1275. /* Process Locked */
  1276. __HAL_UNLOCK(hcryp);
  1277. /* Change the CRYP state and phase */
  1278. hcryp->State = HAL_CRYP_STATE_READY;
  1279. }
  1280. /* Return function status */
  1281. return HAL_OK;
  1282. }
  1283. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1284. {
  1285. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1286. /* Write the Input block in the IN FIFO */
  1287. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1288. inputaddr+=4U;
  1289. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1290. inputaddr+=4U;
  1291. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1292. inputaddr+=4U;
  1293. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1294. hcryp->pCrypInBuffPtr += 16U;
  1295. hcryp->CrypInCount -= 16U;
  1296. if(hcryp->CrypInCount == 0U)
  1297. {
  1298. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1299. /* Call the Input data transfer complete callback */
  1300. HAL_CRYP_InCpltCallback(hcryp);
  1301. }
  1302. }
  1303. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1304. {
  1305. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1306. /* Read the Output block from the Output FIFO */
  1307. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1308. outputaddr+=4U;
  1309. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1310. outputaddr+=4U;
  1311. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1312. outputaddr+=4U;
  1313. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1314. hcryp->pCrypOutBuffPtr += 16U;
  1315. hcryp->CrypOutCount -= 16U;
  1316. if(hcryp->CrypOutCount == 0U)
  1317. {
  1318. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1319. /* Process Unlocked */
  1320. __HAL_UNLOCK(hcryp);
  1321. /* Change the CRYP peripheral state */
  1322. hcryp->State = HAL_CRYP_STATE_READY;
  1323. /* Call Input transfer complete callback */
  1324. HAL_CRYP_OutCpltCallback(hcryp);
  1325. }
  1326. }
  1327. /* Return function status */
  1328. return HAL_OK;
  1329. }
  1330. /**
  1331. * @brief Initializes the CRYP peripheral in AES CCM encryption mode using interrupt.
  1332. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1333. * the configuration information for CRYP module
  1334. * @param pPlainData Pointer to the plaintext buffer
  1335. * @param Size Length of the plaintext buffer, must be a multiple of 16
  1336. * @param pCypherData Pointer to the cyphertext buffer
  1337. * @retval HAL status
  1338. */
  1339. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1340. {
  1341. uint32_t tickstart = 0U;
  1342. uint32_t inputaddr;
  1343. uint32_t outputaddr;
  1344. uint32_t headersize = hcryp->Init.HeaderSize;
  1345. uint32_t headeraddr = (uint32_t)hcryp->Init.Header;
  1346. uint32_t loopcounter = 0U;
  1347. uint32_t bufferidx = 0U;
  1348. uint8_t blockb0[16U] = {0};/* Block B0 */
  1349. uint8_t ctr[16U] = {0}; /* Counter */
  1350. uint32_t b0addr = (uint32_t)blockb0;
  1351. if(hcryp->State == HAL_CRYP_STATE_READY)
  1352. {
  1353. /* Process Locked */
  1354. __HAL_LOCK(hcryp);
  1355. hcryp->CrypInCount = Size;
  1356. hcryp->pCrypInBuffPtr = pPlainData;
  1357. hcryp->pCrypOutBuffPtr = pCypherData;
  1358. hcryp->CrypOutCount = Size;
  1359. /* Change the CRYP peripheral state */
  1360. hcryp->State = HAL_CRYP_STATE_BUSY;
  1361. /* Check if initialization phase has already been performed */
  1362. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1363. {
  1364. /************************ Formatting the header block *******************/
  1365. if(headersize != 0U)
  1366. {
  1367. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  1368. if(headersize < 65280U)
  1369. {
  1370. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  1371. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  1372. headersize += 2U;
  1373. }
  1374. else
  1375. {
  1376. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  1377. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  1378. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  1379. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  1380. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  1381. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  1382. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  1383. headersize += 6U;
  1384. }
  1385. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  1386. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  1387. {
  1388. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  1389. }
  1390. /* Check if the header size is modulo 16 */
  1391. if ((headersize % 16U) != 0U)
  1392. {
  1393. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  1394. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  1395. {
  1396. hcryp->Init.pScratch[loopcounter] = 0U;
  1397. }
  1398. /* Set the header size to modulo 16 */
  1399. headersize = ((headersize/16U) + 1U) * 16U;
  1400. }
  1401. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  1402. headeraddr = (uint32_t)hcryp->Init.pScratch;
  1403. }
  1404. /*********************** Formatting the block B0 ************************/
  1405. if(headersize != 0U)
  1406. {
  1407. blockb0[0U] = 0x40U;
  1408. }
  1409. /* Flags byte */
  1410. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  1411. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U);
  1412. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);
  1413. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  1414. {
  1415. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  1416. }
  1417. for ( ; loopcounter < 13U; loopcounter++)
  1418. {
  1419. blockb0[loopcounter+1U] = 0U;
  1420. }
  1421. blockb0[14U] = (Size >> 8U);
  1422. blockb0[15U] = (Size & 0xFFU);
  1423. /************************* Formatting the initial counter ***************/
  1424. /* Byte 0:
  1425. Bits 7 and 6 are reserved and shall be set to 0
  1426. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter
  1427. blocks are distinct from B0
  1428. Bits 0, 1, and 2 contain the same encoding of q as in B0
  1429. */
  1430. ctr[0U] = blockb0[0U] & 0x07U;
  1431. /* byte 1 to NonceSize is the IV (Nonce) */
  1432. for(loopcounter = 1; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  1433. {
  1434. ctr[loopcounter] = blockb0[loopcounter];
  1435. }
  1436. /* Set the LSB to 1 */
  1437. ctr[15U] |= 0x01U;
  1438. /* Set the key */
  1439. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1440. /* Set the CRYP peripheral in AES CCM mode */
  1441. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT);
  1442. /* Set the Initialization Vector */
  1443. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  1444. /* Select init phase */
  1445. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  1446. b0addr = (uint32_t)blockb0;
  1447. /* Write the blockb0 block in the IN FIFO */
  1448. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1449. b0addr+=4U;
  1450. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1451. b0addr+=4U;
  1452. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1453. b0addr+=4U;
  1454. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1455. /* Enable the CRYP peripheral */
  1456. __HAL_CRYP_ENABLE(hcryp);
  1457. /* Get tick */
  1458. tickstart = HAL_GetTick();
  1459. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  1460. {
  1461. /* Check for the Timeout */
  1462. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1463. {
  1464. /* Change state */
  1465. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1466. /* Process Unlocked */
  1467. __HAL_UNLOCK(hcryp);
  1468. return HAL_TIMEOUT;
  1469. }
  1470. }
  1471. /***************************** Header phase *****************************/
  1472. if(headersize != 0U)
  1473. {
  1474. /* Select header phase */
  1475. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  1476. /* Enable Crypto processor */
  1477. __HAL_CRYP_ENABLE(hcryp);
  1478. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  1479. {
  1480. /* Get tick */
  1481. tickstart = HAL_GetTick();
  1482. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  1483. {
  1484. /* Check for the Timeout */
  1485. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1486. {
  1487. /* Change state */
  1488. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1489. /* Process Unlocked */
  1490. __HAL_UNLOCK(hcryp);
  1491. return HAL_TIMEOUT;
  1492. }
  1493. }
  1494. /* Write the header block in the IN FIFO */
  1495. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1496. headeraddr+=4U;
  1497. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1498. headeraddr+=4U;
  1499. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1500. headeraddr+=4U;
  1501. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1502. headeraddr+=4U;
  1503. }
  1504. /* Get tick */
  1505. tickstart = HAL_GetTick();
  1506. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  1507. {
  1508. /* Check for the Timeout */
  1509. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1510. {
  1511. /* Change state */
  1512. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1513. /* Process Unlocked */
  1514. __HAL_UNLOCK(hcryp);
  1515. return HAL_TIMEOUT;
  1516. }
  1517. }
  1518. }
  1519. /* Save formatted counter into the scratch buffer pScratch */
  1520. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  1521. {
  1522. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  1523. }
  1524. /* Reset bit 0 */
  1525. hcryp->Init.pScratch[15U] &= 0xFEU;
  1526. /* Select payload phase once the header phase is performed */
  1527. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  1528. /* Flush FIFO */
  1529. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1530. /* Set the phase */
  1531. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1532. }
  1533. if(Size != 0U)
  1534. {
  1535. /* Enable Interrupts */
  1536. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1537. /* Enable the CRYP peripheral */
  1538. __HAL_CRYP_ENABLE(hcryp);
  1539. }
  1540. else
  1541. {
  1542. /* Change the CRYP state and phase */
  1543. hcryp->State = HAL_CRYP_STATE_READY;
  1544. }
  1545. /* Return function status */
  1546. return HAL_OK;
  1547. }
  1548. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1549. {
  1550. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1551. /* Write the Input block in the IN FIFO */
  1552. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1553. inputaddr+=4U;
  1554. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1555. inputaddr+=4U;
  1556. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1557. inputaddr+=4U;
  1558. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1559. hcryp->pCrypInBuffPtr += 16U;
  1560. hcryp->CrypInCount -= 16U;
  1561. if(hcryp->CrypInCount == 0U)
  1562. {
  1563. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1564. /* Call Input transfer complete callback */
  1565. HAL_CRYP_InCpltCallback(hcryp);
  1566. }
  1567. }
  1568. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1569. {
  1570. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1571. /* Read the Output block from the Output FIFO */
  1572. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1573. outputaddr+=4U;
  1574. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1575. outputaddr+=4U;
  1576. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1577. outputaddr+=4U;
  1578. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1579. hcryp->pCrypOutBuffPtr += 16U;
  1580. hcryp->CrypOutCount -= 16U;
  1581. if(hcryp->CrypOutCount == 0U)
  1582. {
  1583. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1584. /* Process Unlocked */
  1585. __HAL_UNLOCK(hcryp);
  1586. /* Change the CRYP peripheral state */
  1587. hcryp->State = HAL_CRYP_STATE_READY;
  1588. /* Call Input transfer complete callback */
  1589. HAL_CRYP_OutCpltCallback(hcryp);
  1590. }
  1591. }
  1592. /* Return function status */
  1593. return HAL_OK;
  1594. }
  1595. /**
  1596. * @brief Initializes the CRYP peripheral in AES GCM decryption mode using IT.
  1597. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1598. * the configuration information for CRYP module
  1599. * @param pCypherData Pointer to the cyphertext buffer
  1600. * @param Size Length of the cyphertext buffer, must be a multiple of 16
  1601. * @param pPlainData Pointer to the plaintext buffer
  1602. * @retval HAL status
  1603. */
  1604. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1605. {
  1606. uint32_t tickstart = 0U;
  1607. uint32_t inputaddr;
  1608. uint32_t outputaddr;
  1609. if(hcryp->State == HAL_CRYP_STATE_READY)
  1610. {
  1611. /* Process Locked */
  1612. __HAL_LOCK(hcryp);
  1613. /* Get the buffer addresses and sizes */
  1614. hcryp->CrypInCount = Size;
  1615. hcryp->pCrypInBuffPtr = pCypherData;
  1616. hcryp->pCrypOutBuffPtr = pPlainData;
  1617. hcryp->CrypOutCount = Size;
  1618. /* Change the CRYP peripheral state */
  1619. hcryp->State = HAL_CRYP_STATE_BUSY;
  1620. /* Check if initialization phase has already been performed */
  1621. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1622. {
  1623. /* Set the key */
  1624. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1625. /* Set the CRYP peripheral in AES GCM decryption mode */
  1626. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT);
  1627. /* Set the Initialization Vector */
  1628. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  1629. /* Flush FIFO */
  1630. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1631. /* Enable CRYP to start the init phase */
  1632. __HAL_CRYP_ENABLE(hcryp);
  1633. /* Get tick */
  1634. tickstart = HAL_GetTick();
  1635. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  1636. {
  1637. /* Check for the Timeout */
  1638. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1639. {
  1640. /* Change state */
  1641. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1642. /* Process Unlocked */
  1643. __HAL_UNLOCK(hcryp);
  1644. return HAL_TIMEOUT;
  1645. }
  1646. }
  1647. /* Set the header phase */
  1648. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK)
  1649. {
  1650. return HAL_TIMEOUT;
  1651. }
  1652. /* Disable the CRYP peripheral */
  1653. __HAL_CRYP_DISABLE(hcryp);
  1654. /* Select payload phase once the header phase is performed */
  1655. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  1656. /* Set the phase */
  1657. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1658. }
  1659. if(Size != 0U)
  1660. {
  1661. /* Enable Interrupts */
  1662. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1663. /* Enable the CRYP peripheral */
  1664. __HAL_CRYP_ENABLE(hcryp);
  1665. }
  1666. else
  1667. {
  1668. /* Process Locked */
  1669. __HAL_UNLOCK(hcryp);
  1670. /* Change the CRYP state and phase */
  1671. hcryp->State = HAL_CRYP_STATE_READY;
  1672. }
  1673. /* Return function status */
  1674. return HAL_OK;
  1675. }
  1676. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1677. {
  1678. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1679. /* Write the Input block in the IN FIFO */
  1680. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1681. inputaddr+=4U;
  1682. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1683. inputaddr+=4U;
  1684. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1685. inputaddr+=4U;
  1686. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1687. hcryp->pCrypInBuffPtr += 16U;
  1688. hcryp->CrypInCount -= 16U;
  1689. if(hcryp->CrypInCount == 0U)
  1690. {
  1691. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1692. /* Call the Input data transfer complete callback */
  1693. HAL_CRYP_InCpltCallback(hcryp);
  1694. }
  1695. }
  1696. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1697. {
  1698. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1699. /* Read the Output block from the Output FIFO */
  1700. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1701. outputaddr+=4U;
  1702. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1703. outputaddr+=4U;
  1704. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1705. outputaddr+=4U;
  1706. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1707. hcryp->pCrypOutBuffPtr += 16U;
  1708. hcryp->CrypOutCount -= 16U;
  1709. if(hcryp->CrypOutCount == 0U)
  1710. {
  1711. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1712. /* Process Unlocked */
  1713. __HAL_UNLOCK(hcryp);
  1714. /* Change the CRYP peripheral state */
  1715. hcryp->State = HAL_CRYP_STATE_READY;
  1716. /* Call Input transfer complete callback */
  1717. HAL_CRYP_OutCpltCallback(hcryp);
  1718. }
  1719. }
  1720. /* Return function status */
  1721. return HAL_OK;
  1722. }
  1723. /**
  1724. * @brief Initializes the CRYP peripheral in AES CCM decryption mode using interrupt
  1725. * then decrypted pCypherData. The cypher data are available in pPlainData.
  1726. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1727. * the configuration information for CRYP module
  1728. * @param pCypherData Pointer to the cyphertext buffer
  1729. * @param Size Length of the plaintext buffer, must be a multiple of 16
  1730. * @param pPlainData Pointer to the plaintext buffer
  1731. * @retval HAL status
  1732. */
  1733. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1734. {
  1735. uint32_t inputaddr;
  1736. uint32_t outputaddr;
  1737. uint32_t tickstart = 0U;
  1738. uint32_t headersize = hcryp->Init.HeaderSize;
  1739. uint32_t headeraddr = (uint32_t)hcryp->Init.Header;
  1740. uint32_t loopcounter = 0U;
  1741. uint32_t bufferidx = 0U;
  1742. uint8_t blockb0[16U] = {0};/* Block B0 */
  1743. uint8_t ctr[16U] = {0}; /* Counter */
  1744. uint32_t b0addr = (uint32_t)blockb0;
  1745. if(hcryp->State == HAL_CRYP_STATE_READY)
  1746. {
  1747. /* Process Locked */
  1748. __HAL_LOCK(hcryp);
  1749. hcryp->CrypInCount = Size;
  1750. hcryp->pCrypInBuffPtr = pCypherData;
  1751. hcryp->pCrypOutBuffPtr = pPlainData;
  1752. hcryp->CrypOutCount = Size;
  1753. /* Change the CRYP peripheral state */
  1754. hcryp->State = HAL_CRYP_STATE_BUSY;
  1755. /* Check if initialization phase has already been performed */
  1756. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1757. {
  1758. /************************ Formatting the header block *******************/
  1759. if(headersize != 0U)
  1760. {
  1761. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  1762. if(headersize < 65280U)
  1763. {
  1764. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  1765. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  1766. headersize += 2U;
  1767. }
  1768. else
  1769. {
  1770. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  1771. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  1772. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  1773. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  1774. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  1775. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  1776. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  1777. headersize += 6U;
  1778. }
  1779. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  1780. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  1781. {
  1782. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  1783. }
  1784. /* Check if the header size is modulo 16 */
  1785. if ((headersize % 16U) != 0U)
  1786. {
  1787. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  1788. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  1789. {
  1790. hcryp->Init.pScratch[loopcounter] = 0U;
  1791. }
  1792. /* Set the header size to modulo 16 */
  1793. headersize = ((headersize/16U) + 1U) * 16U;
  1794. }
  1795. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  1796. headeraddr = (uint32_t)hcryp->Init.pScratch;
  1797. }
  1798. /*********************** Formatting the block B0 ************************/
  1799. if(headersize != 0U)
  1800. {
  1801. blockb0[0U] = 0x40U;
  1802. }
  1803. /* Flags byte */
  1804. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  1805. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U);
  1806. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);
  1807. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  1808. {
  1809. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  1810. }
  1811. for ( ; loopcounter < 13U; loopcounter++)
  1812. {
  1813. blockb0[loopcounter+1U] = 0U;
  1814. }
  1815. blockb0[14U] = (Size >> 8U);
  1816. blockb0[15U] = (Size & 0xFFU);
  1817. /************************* Formatting the initial counter ***************/
  1818. /* Byte 0:
  1819. Bits 7 and 6 are reserved and shall be set to 0
  1820. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter
  1821. blocks are distinct from B0
  1822. Bits 0, 1, and 2 contain the same encoding of q as in B0
  1823. */
  1824. ctr[0U] = blockb0[0U] & 0x07U;
  1825. /* byte 1 to NonceSize is the IV (Nonce) */
  1826. for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  1827. {
  1828. ctr[loopcounter] = blockb0[loopcounter];
  1829. }
  1830. /* Set the LSB to 1 */
  1831. ctr[15U] |= 0x01U;
  1832. /* Set the key */
  1833. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1834. /* Set the CRYP peripheral in AES CCM mode */
  1835. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT);
  1836. /* Set the Initialization Vector */
  1837. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  1838. /* Select init phase */
  1839. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  1840. b0addr = (uint32_t)blockb0;
  1841. /* Write the blockb0 block in the IN FIFO */
  1842. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1843. b0addr+=4U;
  1844. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1845. b0addr+=4U;
  1846. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1847. b0addr+=4U;
  1848. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1849. /* Enable the CRYP peripheral */
  1850. __HAL_CRYP_ENABLE(hcryp);
  1851. /* Get tick */
  1852. tickstart = HAL_GetTick();
  1853. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  1854. {
  1855. /* Check for the Timeout */
  1856. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1857. {
  1858. /* Change state */
  1859. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1860. /* Process Unlocked */
  1861. __HAL_UNLOCK(hcryp);
  1862. return HAL_TIMEOUT;
  1863. }
  1864. }
  1865. /***************************** Header phase *****************************/
  1866. if(headersize != 0U)
  1867. {
  1868. /* Select header phase */
  1869. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  1870. /* Enable Crypto processor */
  1871. __HAL_CRYP_ENABLE(hcryp);
  1872. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  1873. {
  1874. /* Get tick */
  1875. tickstart = HAL_GetTick();
  1876. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  1877. {
  1878. /* Check for the Timeout */
  1879. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1880. {
  1881. /* Change state */
  1882. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1883. /* Process Unlocked */
  1884. __HAL_UNLOCK(hcryp);
  1885. return HAL_TIMEOUT;
  1886. }
  1887. }
  1888. /* Write the header block in the IN FIFO */
  1889. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1890. headeraddr+=4U;
  1891. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1892. headeraddr+=4U;
  1893. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1894. headeraddr+=4U;
  1895. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1896. headeraddr+=4U;
  1897. }
  1898. /* Get tick */
  1899. tickstart = HAL_GetTick();
  1900. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  1901. {
  1902. /* Check for the Timeout */
  1903. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1904. {
  1905. /* Change state */
  1906. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1907. /* Process Unlocked */
  1908. __HAL_UNLOCK(hcryp);
  1909. return HAL_TIMEOUT;
  1910. }
  1911. }
  1912. }
  1913. /* Save formatted counter into the scratch buffer pScratch */
  1914. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  1915. {
  1916. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  1917. }
  1918. /* Reset bit 0 */
  1919. hcryp->Init.pScratch[15U] &= 0xFEU;
  1920. /* Select payload phase once the header phase is performed */
  1921. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  1922. /* Flush FIFO */
  1923. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1924. /* Set the phase */
  1925. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1926. }
  1927. /* Enable Interrupts */
  1928. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1929. /* Enable the CRYP peripheral */
  1930. __HAL_CRYP_ENABLE(hcryp);
  1931. /* Return function status */
  1932. return HAL_OK;
  1933. }
  1934. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1935. {
  1936. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1937. /* Write the Input block in the IN FIFO */
  1938. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1939. inputaddr+=4U;
  1940. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1941. inputaddr+=4U;
  1942. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1943. inputaddr+=4U;
  1944. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1945. hcryp->pCrypInBuffPtr += 16U;
  1946. hcryp->CrypInCount -= 16U;
  1947. if(hcryp->CrypInCount == 0U)
  1948. {
  1949. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1950. /* Call the Input data transfer complete callback */
  1951. HAL_CRYP_InCpltCallback(hcryp);
  1952. }
  1953. }
  1954. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1955. {
  1956. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1957. /* Read the Output block from the Output FIFO */
  1958. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1959. outputaddr+=4U;
  1960. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1961. outputaddr+=4U;
  1962. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1963. outputaddr+=4U;
  1964. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1965. hcryp->pCrypOutBuffPtr += 16U;
  1966. hcryp->CrypOutCount -= 16U;
  1967. if(hcryp->CrypOutCount == 0U)
  1968. {
  1969. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1970. /* Process Unlocked */
  1971. __HAL_UNLOCK(hcryp);
  1972. /* Change the CRYP peripheral state */
  1973. hcryp->State = HAL_CRYP_STATE_READY;
  1974. /* Call Input transfer complete callback */
  1975. HAL_CRYP_OutCpltCallback(hcryp);
  1976. }
  1977. }
  1978. /* Return function status */
  1979. return HAL_OK;
  1980. }
  1981. /**
  1982. * @brief Initializes the CRYP peripheral in AES GCM encryption mode using DMA.
  1983. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1984. * the configuration information for CRYP module
  1985. * @param pPlainData Pointer to the plaintext buffer
  1986. * @param Size Length of the plaintext buffer, must be a multiple of 16
  1987. * @param pCypherData Pointer to the cyphertext buffer
  1988. * @retval HAL status
  1989. */
  1990. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1991. {
  1992. uint32_t tickstart = 0U;
  1993. uint32_t inputaddr;
  1994. uint32_t outputaddr;
  1995. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1996. {
  1997. /* Process Locked */
  1998. __HAL_LOCK(hcryp);
  1999. inputaddr = (uint32_t)pPlainData;
  2000. outputaddr = (uint32_t)pCypherData;
  2001. /* Change the CRYP peripheral state */
  2002. hcryp->State = HAL_CRYP_STATE_BUSY;
  2003. /* Check if initialization phase has already been performed */
  2004. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  2005. {
  2006. /* Set the key */
  2007. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2008. /* Set the CRYP peripheral in AES GCM mode */
  2009. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT);
  2010. /* Set the Initialization Vector */
  2011. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  2012. /* Flush FIFO */
  2013. __HAL_CRYP_FIFO_FLUSH(hcryp);
  2014. /* Enable CRYP to start the init phase */
  2015. __HAL_CRYP_ENABLE(hcryp);
  2016. /* Get tick */
  2017. tickstart = HAL_GetTick();
  2018. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  2019. {
  2020. /* Check for the Timeout */
  2021. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2022. {
  2023. /* Change state */
  2024. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2025. /* Process Unlocked */
  2026. __HAL_UNLOCK(hcryp);
  2027. return HAL_TIMEOUT;
  2028. }
  2029. }
  2030. /* Flush FIFO */
  2031. __HAL_CRYP_FIFO_FLUSH(hcryp);
  2032. /* Set the header phase */
  2033. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK)
  2034. {
  2035. return HAL_TIMEOUT;
  2036. }
  2037. /* Disable the CRYP peripheral */
  2038. __HAL_CRYP_DISABLE(hcryp);
  2039. /* Select payload phase once the header phase is performed */
  2040. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2041. /* Flush FIFO */
  2042. __HAL_CRYP_FIFO_FLUSH(hcryp);
  2043. /* Set the phase */
  2044. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  2045. }
  2046. /* Set the input and output addresses and start DMA transfer */
  2047. CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2048. /* Unlock process */
  2049. __HAL_UNLOCK(hcryp);
  2050. /* Return function status */
  2051. return HAL_OK;
  2052. }
  2053. else
  2054. {
  2055. return HAL_ERROR;
  2056. }
  2057. }
  2058. /**
  2059. * @brief Initializes the CRYP peripheral in AES CCM encryption mode using interrupt.
  2060. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2061. * the configuration information for CRYP module
  2062. * @param pPlainData Pointer to the plaintext buffer
  2063. * @param Size Length of the plaintext buffer, must be a multiple of 16
  2064. * @param pCypherData Pointer to the cyphertext buffer
  2065. * @retval HAL status
  2066. */
  2067. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2068. {
  2069. uint32_t tickstart = 0U;
  2070. uint32_t inputaddr;
  2071. uint32_t outputaddr;
  2072. uint32_t headersize;
  2073. uint32_t headeraddr;
  2074. uint32_t loopcounter = 0U;
  2075. uint32_t bufferidx = 0U;
  2076. uint8_t blockb0[16U] = {0};/* Block B0 */
  2077. uint8_t ctr[16U] = {0}; /* Counter */
  2078. uint32_t b0addr = (uint32_t)blockb0;
  2079. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2080. {
  2081. /* Process Locked */
  2082. __HAL_LOCK(hcryp);
  2083. inputaddr = (uint32_t)pPlainData;
  2084. outputaddr = (uint32_t)pCypherData;
  2085. headersize = hcryp->Init.HeaderSize;
  2086. headeraddr = (uint32_t)hcryp->Init.Header;
  2087. hcryp->CrypInCount = Size;
  2088. hcryp->pCrypInBuffPtr = pPlainData;
  2089. hcryp->pCrypOutBuffPtr = pCypherData;
  2090. hcryp->CrypOutCount = Size;
  2091. /* Change the CRYP peripheral state */
  2092. hcryp->State = HAL_CRYP_STATE_BUSY;
  2093. /* Check if initialization phase has already been performed */
  2094. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  2095. {
  2096. /************************ Formatting the header block *******************/
  2097. if(headersize != 0U)
  2098. {
  2099. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  2100. if(headersize < 65280U)
  2101. {
  2102. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  2103. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  2104. headersize += 2U;
  2105. }
  2106. else
  2107. {
  2108. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  2109. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  2110. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  2111. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  2112. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  2113. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  2114. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  2115. headersize += 6U;
  2116. }
  2117. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  2118. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  2119. {
  2120. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  2121. }
  2122. /* Check if the header size is modulo 16 */
  2123. if ((headersize % 16U) != 0U)
  2124. {
  2125. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  2126. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  2127. {
  2128. hcryp->Init.pScratch[loopcounter] = 0U;
  2129. }
  2130. /* Set the header size to modulo 16 */
  2131. headersize = ((headersize/16U) + 1U) * 16U;
  2132. }
  2133. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  2134. headeraddr = (uint32_t)hcryp->Init.pScratch;
  2135. }
  2136. /*********************** Formatting the block B0 ************************/
  2137. if(headersize != 0U)
  2138. {
  2139. blockb0[0U] = 0x40U;
  2140. }
  2141. /* Flags byte */
  2142. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  2143. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07) << 3);
  2144. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);
  2145. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  2146. {
  2147. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  2148. }
  2149. for ( ; loopcounter < 13U; loopcounter++)
  2150. {
  2151. blockb0[loopcounter+1U] = 0U;
  2152. }
  2153. blockb0[14U] = (Size >> 8U);
  2154. blockb0[15U] = (Size & 0xFFU);
  2155. /************************* Formatting the initial counter ***************/
  2156. /* Byte 0:
  2157. Bits 7 and 6 are reserved and shall be set to 0
  2158. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter
  2159. blocks are distinct from B0
  2160. Bits 0, 1, and 2 contain the same encoding of q as in B0
  2161. */
  2162. ctr[0U] = blockb0[0U] & 0x07U;
  2163. /* byte 1 to NonceSize is the IV (Nonce) */
  2164. for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  2165. {
  2166. ctr[loopcounter] = blockb0[loopcounter];
  2167. }
  2168. /* Set the LSB to 1 */
  2169. ctr[15U] |= 0x01U;
  2170. /* Set the key */
  2171. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2172. /* Set the CRYP peripheral in AES CCM mode */
  2173. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT);
  2174. /* Set the Initialization Vector */
  2175. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  2176. /* Select init phase */
  2177. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  2178. b0addr = (uint32_t)blockb0;
  2179. /* Write the blockb0 block in the IN FIFO */
  2180. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2181. b0addr+=4U;
  2182. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2183. b0addr+=4U;
  2184. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2185. b0addr+=4U;
  2186. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2187. /* Enable the CRYP peripheral */
  2188. __HAL_CRYP_ENABLE(hcryp);
  2189. /* Get tick */
  2190. tickstart = HAL_GetTick();
  2191. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  2192. {
  2193. /* Check for the Timeout */
  2194. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2195. {
  2196. /* Change state */
  2197. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2198. /* Process Unlocked */
  2199. __HAL_UNLOCK(hcryp);
  2200. return HAL_TIMEOUT;
  2201. }
  2202. }
  2203. /***************************** Header phase *****************************/
  2204. if(headersize != 0U)
  2205. {
  2206. /* Select header phase */
  2207. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  2208. /* Enable Crypto processor */
  2209. __HAL_CRYP_ENABLE(hcryp);
  2210. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  2211. {
  2212. /* Get tick */
  2213. tickstart = HAL_GetTick();
  2214. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  2215. {
  2216. /* Check for the Timeout */
  2217. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2218. {
  2219. /* Change state */
  2220. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2221. /* Process Unlocked */
  2222. __HAL_UNLOCK(hcryp);
  2223. return HAL_TIMEOUT;
  2224. }
  2225. }
  2226. /* Write the header block in the IN FIFO */
  2227. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2228. headeraddr+=4U;
  2229. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2230. headeraddr+=4U;
  2231. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2232. headeraddr+=4U;
  2233. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2234. headeraddr+=4U;
  2235. }
  2236. /* Get tick */
  2237. tickstart = HAL_GetTick();
  2238. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  2239. {
  2240. /* Check for the Timeout */
  2241. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2242. {
  2243. /* Change state */
  2244. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2245. /* Process Unlocked */
  2246. __HAL_UNLOCK(hcryp);
  2247. return HAL_TIMEOUT;
  2248. }
  2249. }
  2250. }
  2251. /* Save formatted counter into the scratch buffer pScratch */
  2252. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  2253. {
  2254. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  2255. }
  2256. /* Reset bit 0 */
  2257. hcryp->Init.pScratch[15U] &= 0xFEU;
  2258. /* Select payload phase once the header phase is performed */
  2259. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2260. /* Flush FIFO */
  2261. __HAL_CRYP_FIFO_FLUSH(hcryp);
  2262. /* Set the phase */
  2263. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  2264. }
  2265. /* Set the input and output addresses and start DMA transfer */
  2266. CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2267. /* Unlock process */
  2268. __HAL_UNLOCK(hcryp);
  2269. /* Return function status */
  2270. return HAL_OK;
  2271. }
  2272. else
  2273. {
  2274. return HAL_ERROR;
  2275. }
  2276. }
  2277. /**
  2278. * @brief Initializes the CRYP peripheral in AES GCM decryption mode using DMA.
  2279. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2280. * the configuration information for CRYP module
  2281. * @param pCypherData Pointer to the cyphertext buffer.
  2282. * @param Size Length of the cyphertext buffer, must be a multiple of 16
  2283. * @param pPlainData Pointer to the plaintext buffer
  2284. * @retval HAL status
  2285. */
  2286. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2287. {
  2288. uint32_t tickstart = 0U;
  2289. uint32_t inputaddr;
  2290. uint32_t outputaddr;
  2291. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2292. {
  2293. /* Process Locked */
  2294. __HAL_LOCK(hcryp);
  2295. inputaddr = (uint32_t)pCypherData;
  2296. outputaddr = (uint32_t)pPlainData;
  2297. /* Change the CRYP peripheral state */
  2298. hcryp->State = HAL_CRYP_STATE_BUSY;
  2299. /* Check if initialization phase has already been performed */
  2300. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  2301. {
  2302. /* Set the key */
  2303. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2304. /* Set the CRYP peripheral in AES GCM decryption mode */
  2305. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT);
  2306. /* Set the Initialization Vector */
  2307. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  2308. /* Enable CRYP to start the init phase */
  2309. __HAL_CRYP_ENABLE(hcryp);
  2310. /* Get tick */
  2311. tickstart = HAL_GetTick();
  2312. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  2313. {
  2314. /* Check for the Timeout */
  2315. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2316. {
  2317. /* Change state */
  2318. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2319. /* Process Unlocked */
  2320. __HAL_UNLOCK(hcryp);
  2321. return HAL_TIMEOUT;
  2322. }
  2323. }
  2324. /* Set the header phase */
  2325. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK)
  2326. {
  2327. return HAL_TIMEOUT;
  2328. }
  2329. /* Disable the CRYP peripheral */
  2330. __HAL_CRYP_DISABLE(hcryp);
  2331. /* Select payload phase once the header phase is performed */
  2332. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2333. /* Set the phase */
  2334. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  2335. }
  2336. /* Set the input and output addresses and start DMA transfer */
  2337. CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2338. /* Unlock process */
  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 AES CCM decryption mode using DMA
  2350. * then decrypted pCypherData. The cypher data are available in pPlainData.
  2351. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2352. * the configuration information for CRYP module
  2353. * @param pCypherData Pointer to the cyphertext buffer
  2354. * @param Size Length of the plaintext buffer, must be a multiple of 16
  2355. * @param pPlainData Pointer to the plaintext buffer
  2356. * @retval HAL status
  2357. */
  2358. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2359. {
  2360. uint32_t tickstart = 0U;
  2361. uint32_t inputaddr;
  2362. uint32_t outputaddr;
  2363. uint32_t headersize;
  2364. uint32_t headeraddr;
  2365. uint32_t loopcounter = 0U;
  2366. uint32_t bufferidx = 0U;
  2367. uint8_t blockb0[16U] = {0};/* Block B0 */
  2368. uint8_t ctr[16U] = {0}; /* Counter */
  2369. uint32_t b0addr = (uint32_t)blockb0;
  2370. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2371. {
  2372. /* Process Locked */
  2373. __HAL_LOCK(hcryp);
  2374. inputaddr = (uint32_t)pCypherData;
  2375. outputaddr = (uint32_t)pPlainData;
  2376. headersize = hcryp->Init.HeaderSize;
  2377. headeraddr = (uint32_t)hcryp->Init.Header;
  2378. hcryp->CrypInCount = Size;
  2379. hcryp->pCrypInBuffPtr = pCypherData;
  2380. hcryp->pCrypOutBuffPtr = pPlainData;
  2381. hcryp->CrypOutCount = Size;
  2382. /* Change the CRYP peripheral state */
  2383. hcryp->State = HAL_CRYP_STATE_BUSY;
  2384. /* Check if initialization phase has already been performed */
  2385. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  2386. {
  2387. /************************ Formatting the header block *******************/
  2388. if(headersize != 0U)
  2389. {
  2390. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  2391. if(headersize < 65280U)
  2392. {
  2393. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  2394. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  2395. headersize += 2U;
  2396. }
  2397. else
  2398. {
  2399. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  2400. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  2401. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  2402. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  2403. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  2404. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  2405. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  2406. headersize += 6U;
  2407. }
  2408. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  2409. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  2410. {
  2411. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  2412. }
  2413. /* Check if the header size is modulo 16 */
  2414. if ((headersize % 16U) != 0U)
  2415. {
  2416. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  2417. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  2418. {
  2419. hcryp->Init.pScratch[loopcounter] = 0U;
  2420. }
  2421. /* Set the header size to modulo 16 */
  2422. headersize = ((headersize/16U) + 1U) * 16U;
  2423. }
  2424. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  2425. headeraddr = (uint32_t)hcryp->Init.pScratch;
  2426. }
  2427. /*********************** Formatting the block B0 ************************/
  2428. if(headersize != 0U)
  2429. {
  2430. blockb0[0U] = 0x40U;
  2431. }
  2432. /* Flags byte */
  2433. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  2434. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07) << 3);
  2435. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);
  2436. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  2437. {
  2438. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  2439. }
  2440. for ( ; loopcounter < 13U; loopcounter++)
  2441. {
  2442. blockb0[loopcounter+1U] = 0U;
  2443. }
  2444. blockb0[14U] = (Size >> 8U);
  2445. blockb0[15U] = (Size & 0xFFU);
  2446. /************************* Formatting the initial counter ***************/
  2447. /* Byte 0:
  2448. Bits 7 and 6 are reserved and shall be set to 0
  2449. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter
  2450. blocks are distinct from B0
  2451. Bits 0, 1, and 2 contain the same encoding of q as in B0
  2452. */
  2453. ctr[0U] = blockb0[0U] & 0x07U;
  2454. /* byte 1 to NonceSize is the IV (Nonce) */
  2455. for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  2456. {
  2457. ctr[loopcounter] = blockb0[loopcounter];
  2458. }
  2459. /* Set the LSB to 1 */
  2460. ctr[15U] |= 0x01U;
  2461. /* Set the key */
  2462. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2463. /* Set the CRYP peripheral in AES CCM mode */
  2464. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT);
  2465. /* Set the Initialization Vector */
  2466. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  2467. /* Select init phase */
  2468. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  2469. b0addr = (uint32_t)blockb0;
  2470. /* Write the blockb0 block in the IN FIFO */
  2471. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2472. b0addr+=4U;
  2473. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2474. b0addr+=4U;
  2475. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2476. b0addr+=4U;
  2477. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2478. /* Enable the CRYP peripheral */
  2479. __HAL_CRYP_ENABLE(hcryp);
  2480. /* Get tick */
  2481. tickstart = HAL_GetTick();
  2482. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  2483. {
  2484. /* Check for the Timeout */
  2485. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2486. {
  2487. /* Change state */
  2488. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2489. /* Process Unlocked */
  2490. __HAL_UNLOCK(hcryp);
  2491. return HAL_TIMEOUT;
  2492. }
  2493. }
  2494. /***************************** Header phase *****************************/
  2495. if(headersize != 0U)
  2496. {
  2497. /* Select header phase */
  2498. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  2499. /* Enable Crypto processor */
  2500. __HAL_CRYP_ENABLE(hcryp);
  2501. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  2502. {
  2503. /* Get tick */
  2504. tickstart = HAL_GetTick();
  2505. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  2506. {
  2507. /* Check for the Timeout */
  2508. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2509. {
  2510. /* Change state */
  2511. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2512. /* Process Unlocked */
  2513. __HAL_UNLOCK(hcryp);
  2514. return HAL_TIMEOUT;
  2515. }
  2516. }
  2517. /* Write the header block in the IN FIFO */
  2518. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2519. headeraddr+=4U;
  2520. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2521. headeraddr+=4U;
  2522. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2523. headeraddr+=4U;
  2524. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2525. headeraddr+=4U;
  2526. }
  2527. /* Get tick */
  2528. tickstart = HAL_GetTick();
  2529. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  2530. {
  2531. /* Check for the Timeout */
  2532. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2533. {
  2534. /* Change state */
  2535. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2536. /* Process Unlocked */
  2537. __HAL_UNLOCK(hcryp);
  2538. return HAL_TIMEOUT;
  2539. }
  2540. }
  2541. }
  2542. /* Save formatted counter into the scratch buffer pScratch */
  2543. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  2544. {
  2545. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  2546. }
  2547. /* Reset bit 0 */
  2548. hcryp->Init.pScratch[15U] &= 0xFEU;
  2549. /* Select payload phase once the header phase is performed */
  2550. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2551. /* Flush FIFO */
  2552. __HAL_CRYP_FIFO_FLUSH(hcryp);
  2553. /* Set the phase */
  2554. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  2555. }
  2556. /* Set the input and output addresses and start DMA transfer */
  2557. CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2558. /* Unlock process */
  2559. __HAL_UNLOCK(hcryp);
  2560. /* Return function status */
  2561. return HAL_OK;
  2562. }
  2563. else
  2564. {
  2565. return HAL_ERROR;
  2566. }
  2567. }
  2568. /**
  2569. * @}
  2570. */
  2571. /** @defgroup CRYPEx_Exported_Functions_Group2 CRYPEx IRQ handler management
  2572. * @brief CRYPEx IRQ handler.
  2573. *
  2574. @verbatim
  2575. ==============================================================================
  2576. ##### CRYPEx IRQ handler management #####
  2577. ==============================================================================
  2578. [..] This section provides CRYPEx IRQ handler function.
  2579. @endverbatim
  2580. * @{
  2581. */
  2582. /**
  2583. * @brief This function handles CRYPEx interrupt request.
  2584. * @param hcryp pointer to a CRYPEx_HandleTypeDef structure that contains
  2585. * the configuration information for CRYP module
  2586. * @retval None
  2587. */
  2588. void HAL_CRYPEx_GCMCCM_IRQHandler(CRYP_HandleTypeDef *hcryp)
  2589. {
  2590. switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION)
  2591. {
  2592. case CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT:
  2593. HAL_CRYPEx_AESGCM_Encrypt_IT(hcryp, NULL, 0U, NULL);
  2594. break;
  2595. case CRYP_CR_ALGOMODE_AES_GCM_DECRYPT:
  2596. HAL_CRYPEx_AESGCM_Decrypt_IT(hcryp, NULL, 0U, NULL);
  2597. break;
  2598. case CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT:
  2599. HAL_CRYPEx_AESCCM_Encrypt_IT(hcryp, NULL, 0U, NULL);
  2600. break;
  2601. case CRYP_CR_ALGOMODE_AES_CCM_DECRYPT:
  2602. HAL_CRYPEx_AESCCM_Decrypt_IT(hcryp, NULL, 0U, NULL);
  2603. break;
  2604. default:
  2605. break;
  2606. }
  2607. }
  2608. /**
  2609. * @}
  2610. */
  2611. /**
  2612. * @}
  2613. */
  2614. #endif /* CRYP */
  2615. #if defined (AES)
  2616. /** @defgroup CRYPEx_Private_Constants CRYPEx Private Constants
  2617. * @{
  2618. */
  2619. #define CRYP_CCF_TIMEOUTVALUE 22000U /*!< CCF flag raising time-out value */
  2620. #define CRYP_BUSY_TIMEOUTVALUE 22000U /*!< BUSY flag reset time-out value */
  2621. #define CRYP_POLLING_OFF 0x0U /*!< No polling when padding */
  2622. #define CRYP_POLLING_ON 0x1U /*!< Polling when padding */
  2623. /**
  2624. * @}
  2625. */
  2626. /* Private macro -------------------------------------------------------------*/
  2627. /* Private variables ---------------------------------------------------------*/
  2628. /* Private function prototypes -----------------------------------------------*/
  2629. /** @defgroup CRYPEx_Private_Functions CRYPEx Private Functions
  2630. * @{
  2631. */
  2632. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
  2633. static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout);
  2634. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  2635. static void CRYP_GCMCMAC_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  2636. static void CRYP_GCMCMAC_DMAInCplt(DMA_HandleTypeDef *hdma);
  2637. static void CRYP_GCMCMAC_DMAError(DMA_HandleTypeDef *hdma);
  2638. static void CRYP_GCMCMAC_DMAOutCplt(DMA_HandleTypeDef *hdma);
  2639. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  2640. static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  2641. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
  2642. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
  2643. static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
  2644. static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling);
  2645. /**
  2646. * @}
  2647. */
  2648. /* Exported functions ---------------------------------------------------------*/
  2649. /** @defgroup CRYPEx_Exported_Functions CRYPEx Exported Functions
  2650. * @{
  2651. */
  2652. /** @defgroup CRYPEx_Exported_Functions_Group1 Extended callback function
  2653. * @brief Extended callback functions.
  2654. *
  2655. @verbatim
  2656. ===============================================================================
  2657. ##### Extended callback functions #####
  2658. ===============================================================================
  2659. [..] This section provides callback function:
  2660. (+) Computation completed.
  2661. @endverbatim
  2662. * @{
  2663. */
  2664. /**
  2665. * @brief Computation completed callbacks.
  2666. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2667. * the configuration information for CRYP module
  2668. * @retval None
  2669. */
  2670. __weak void HAL_CRYPEx_ComputationCpltCallback(CRYP_HandleTypeDef *hcryp)
  2671. {
  2672. /* Prevent unused argument(s) compilation warning */
  2673. UNUSED(hcryp);
  2674. /* NOTE : This function should not be modified; when the callback is needed,
  2675. the HAL_CRYPEx_ComputationCpltCallback can be implemented in the user file
  2676. */
  2677. }
  2678. /**
  2679. * @}
  2680. */
  2681. /** @defgroup CRYPEx_Exported_Functions_Group2 AES extended processing functions
  2682. * @brief Extended processing functions.
  2683. *
  2684. @verbatim
  2685. ==============================================================================
  2686. ##### AES extended processing functions #####
  2687. ==============================================================================
  2688. [..] This section provides functions allowing to:
  2689. (+) Encrypt plaintext or decrypt cipher text using AES algorithm in different chaining modes.
  2690. Functions are generic (handles ECB, CBC and CTR and all modes) and are only differentiated
  2691. based on the processing type. Three processing types are available:
  2692. (++) Polling mode
  2693. (++) Interrupt mode
  2694. (++) DMA mode
  2695. (+) Generate and authentication tag in addition to encrypt/decrypt a plain/cipher text using AES
  2696. algorithm in different chaining modes.
  2697. Functions are generic (handles GCM, GMAC, CMAC and CCM when applicable) and process only one phase
  2698. so that steps can be skipped if so required. Functions are only differentiated based on the processing type.
  2699. Three processing types are available:
  2700. (++) Polling mode
  2701. (++) Interrupt mode
  2702. (++) DMA mode
  2703. @endverbatim
  2704. * @{
  2705. */
  2706. /**
  2707. * @brief Carry out in polling mode the ciphering or deciphering operation according to
  2708. * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and
  2709. * chaining modes ECB, CBC and CTR are managed by this function in polling mode.
  2710. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2711. * the configuration information for CRYP module
  2712. * @param pInputData Pointer to the plain text in case of encryption or cipher text in case of decryption
  2713. * or key derivation+decryption.
  2714. * Parameter is meaningless in case of key derivation.
  2715. * @param Size Length of the input data buffer in bytes, must be a multiple of 16.
  2716. * Parameter is meaningless in case of key derivation.
  2717. * @param pOutputData Pointer to the cipher text in case of encryption or plain text in case of
  2718. * decryption/key derivation+decryption, or pointer to the derivative keys in
  2719. * case of key derivation only.
  2720. * @param Timeout Specify Timeout value
  2721. * @retval HAL status
  2722. */
  2723. HAL_StatusTypeDef HAL_CRYPEx_AES(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData, uint32_t Timeout)
  2724. {
  2725. if (hcryp->State == HAL_CRYP_STATE_READY)
  2726. {
  2727. /* Check parameters setting */
  2728. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  2729. {
  2730. if (pOutputData == NULL)
  2731. {
  2732. return HAL_ERROR;
  2733. }
  2734. }
  2735. else
  2736. {
  2737. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  2738. {
  2739. return HAL_ERROR;
  2740. }
  2741. }
  2742. /* Process Locked */
  2743. __HAL_LOCK(hcryp);
  2744. /* Change the CRYP state */
  2745. hcryp->State = HAL_CRYP_STATE_BUSY;
  2746. /* Call CRYP_ReadKey() API if the operating mode is set to
  2747. key derivation, CRYP_ProcessData() otherwise */
  2748. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  2749. {
  2750. if(CRYP_ReadKey(hcryp, pOutputData, Timeout) != HAL_OK)
  2751. {
  2752. return HAL_TIMEOUT;
  2753. }
  2754. }
  2755. else
  2756. {
  2757. if(CRYP_ProcessData(hcryp, pInputData, Size, pOutputData, Timeout) != HAL_OK)
  2758. {
  2759. return HAL_TIMEOUT;
  2760. }
  2761. }
  2762. /* If the state has not been set to SUSPENDED, set it to
  2763. READY, otherwise keep it as it is */
  2764. if (hcryp->State != HAL_CRYP_STATE_SUSPENDED)
  2765. {
  2766. hcryp->State = HAL_CRYP_STATE_READY;
  2767. }
  2768. /* Process Unlocked */
  2769. __HAL_UNLOCK(hcryp);
  2770. return HAL_OK;
  2771. }
  2772. else
  2773. {
  2774. return HAL_BUSY;
  2775. }
  2776. }
  2777. /**
  2778. * @brief Carry out in interrupt mode the ciphering or deciphering operation according to
  2779. * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and
  2780. * chaining modes ECB, CBC and CTR are managed by this function in interrupt mode.
  2781. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2782. * the configuration information for CRYP module
  2783. * @param pInputData Pointer to the plain text in case of encryption or cipher text in case of decryption
  2784. * or key derivation+decryption.
  2785. * Parameter is meaningless in case of key derivation.
  2786. * @param Size Length of the input data buffer in bytes, must be a multiple of 16.
  2787. * Parameter is meaningless in case of key derivation.
  2788. * @param pOutputData Pointer to the cipher text in case of encryption or plain text in case of
  2789. * decryption/key derivation+decryption, or pointer to the derivative keys in
  2790. * case of key derivation only.
  2791. * @retval HAL status
  2792. */
  2793. HAL_StatusTypeDef HAL_CRYPEx_AES_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData)
  2794. {
  2795. uint32_t inputaddr = 0U;
  2796. if(hcryp->State == HAL_CRYP_STATE_READY)
  2797. {
  2798. /* Check parameters setting */
  2799. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  2800. {
  2801. if (pOutputData == NULL)
  2802. {
  2803. return HAL_ERROR;
  2804. }
  2805. }
  2806. else
  2807. {
  2808. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  2809. {
  2810. return HAL_ERROR;
  2811. }
  2812. }
  2813. /* Process Locked */
  2814. __HAL_LOCK(hcryp);
  2815. /* If operating mode is not limited to key derivation only,
  2816. get the buffers addresses and sizes */
  2817. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  2818. {
  2819. hcryp->CrypInCount = Size;
  2820. hcryp->pCrypInBuffPtr = pInputData;
  2821. hcryp->pCrypOutBuffPtr = pOutputData;
  2822. hcryp->CrypOutCount = Size;
  2823. }
  2824. /* Change the CRYP state */
  2825. hcryp->State = HAL_CRYP_STATE_BUSY;
  2826. /* Process Unlocked */
  2827. __HAL_UNLOCK(hcryp);
  2828. /* Enable Computation Complete Flag and Error Interrupts */
  2829. __HAL_CRYP_ENABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  2830. /* If operating mode is key derivation only, the input data have
  2831. already been entered during the initialization process. For
  2832. the other operating modes, they are fed to the CRYP hardware
  2833. block at this point. */
  2834. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  2835. {
  2836. /* Initiate the processing under interrupt in entering
  2837. the first input data */
  2838. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2839. /* Increment/decrement instance pointer/counter */
  2840. hcryp->pCrypInBuffPtr += 16U;
  2841. hcryp->CrypInCount -= 16U;
  2842. /* Write the first input block in the Data Input register */
  2843. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2844. inputaddr+=4U;
  2845. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2846. inputaddr+=4U;
  2847. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2848. inputaddr+=4U;
  2849. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2850. }
  2851. /* Return function status */
  2852. return HAL_OK;
  2853. }
  2854. else
  2855. {
  2856. return HAL_BUSY;
  2857. }
  2858. }
  2859. /**
  2860. * @brief Carry out in DMA mode the ciphering or deciphering operation according to
  2861. * hcryp->Init structure fields.
  2862. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2863. * the configuration information for CRYP module
  2864. * @param pInputData Pointer to the plain text in case of encryption or cipher text in case of decryption
  2865. * or key derivation+decryption.
  2866. * @param Size Length of the input data buffer in bytes, must be a multiple of 16.
  2867. * @param pOutputData Pointer to the cipher text in case of encryption or plain text in case of
  2868. * decryption/key derivation+decryption.
  2869. * @note Chaining modes ECB, CBC and CTR are managed by this function in DMA mode.
  2870. * @note Supported operating modes are encryption, decryption and key derivation with decryption.
  2871. * @note No DMA channel is provided for key derivation only and therefore, access to AES_KEYRx
  2872. * registers must be done by software.
  2873. * @note This API is not applicable to key derivation only; for such a mode, access to AES_KEYRx
  2874. * registers must be done by software thru HAL_CRYPEx_AES() or HAL_CRYPEx_AES_IT() APIs.
  2875. * @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  2876. * @retval HAL status
  2877. */
  2878. HAL_StatusTypeDef HAL_CRYPEx_AES_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData)
  2879. {
  2880. uint32_t inputaddr = 0U;
  2881. uint32_t outputaddr = 0U;
  2882. if (hcryp->State == HAL_CRYP_STATE_READY)
  2883. {
  2884. /* Check parameters setting */
  2885. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  2886. {
  2887. /* no DMA channel is provided for key derivation operating mode,
  2888. access to AES_KEYRx registers must be done by software */
  2889. return HAL_ERROR;
  2890. }
  2891. else
  2892. {
  2893. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  2894. {
  2895. return HAL_ERROR;
  2896. }
  2897. }
  2898. /* Process Locked */
  2899. __HAL_LOCK(hcryp);
  2900. inputaddr = (uint32_t)pInputData;
  2901. outputaddr = (uint32_t)pOutputData;
  2902. /* Change the CRYP state */
  2903. hcryp->State = HAL_CRYP_STATE_BUSY;
  2904. /* Set the input and output addresses and start DMA transfer */
  2905. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2906. /* Process Unlocked */
  2907. __HAL_UNLOCK(hcryp);
  2908. /* Return function status */
  2909. return HAL_OK;
  2910. }
  2911. else
  2912. {
  2913. return HAL_BUSY;
  2914. }
  2915. }
  2916. /**
  2917. * @brief Carry out in polling mode the authentication tag generation as well as the ciphering or deciphering
  2918. * operation according to hcryp->Init structure fields.
  2919. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2920. * the configuration information for CRYP module
  2921. * @param pInputData
  2922. * - pointer to payload data in GCM payload phase,
  2923. * - pointer to B0 block in CMAC header phase,
  2924. * - pointer to C block in CMAC final phase.
  2925. * - Parameter is meaningless in case of GCM/GMAC init, header and final phases.
  2926. * @param Size
  2927. * - length of the input payload data buffer in bytes,
  2928. * - length of B0 block (in bytes) in CMAC header phase,
  2929. * - length of C block (in bytes) in CMAC final phase.
  2930. * - Parameter is meaningless in case of GCM/GMAC init and header phases.
  2931. * @param pOutputData
  2932. * - pointer to plain or cipher text in GCM payload phase,
  2933. * - pointer to authentication tag in GCM/GMAC and CMAC final phases.
  2934. * - Parameter is meaningless in case of GCM/GMAC init and header phases
  2935. * and in case of CMAC header phase.
  2936. * @param Timeout Specify Timeout value
  2937. * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC, CMAC and CCM when the latter is applicable.
  2938. * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
  2939. * can be skipped by the user if so required.
  2940. * @retval HAL status
  2941. */
  2942. HAL_StatusTypeDef HAL_CRYPEx_AES_Auth(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData, uint32_t Timeout)
  2943. {
  2944. uint32_t index = 0U;
  2945. uint32_t inputaddr = 0U;
  2946. uint32_t outputaddr = 0U;
  2947. uint32_t tagaddr = 0U;
  2948. uint64_t headerlength = 0U;
  2949. uint64_t inputlength = 0U;
  2950. uint64_t payloadlength = 0U;
  2951. uint32_t difflength = 0U;
  2952. uint32_t addhoc_process = 0U;
  2953. if (hcryp->State == HAL_CRYP_STATE_READY)
  2954. {
  2955. /* input/output parameters check */
  2956. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  2957. {
  2958. if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U))
  2959. {
  2960. return HAL_ERROR;
  2961. }
  2962. #if defined(AES_CR_NPBLB)
  2963. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  2964. #else
  2965. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  2966. #endif
  2967. {
  2968. /* In case of CMAC (or CCM) header phase resumption, we can have pInputData = NULL and Size = 0 */
  2969. if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U)))
  2970. {
  2971. return HAL_ERROR;
  2972. }
  2973. }
  2974. }
  2975. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  2976. {
  2977. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  2978. {
  2979. return HAL_ERROR;
  2980. }
  2981. }
  2982. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  2983. {
  2984. if (pOutputData == NULL)
  2985. {
  2986. return HAL_ERROR;
  2987. }
  2988. #if defined(AES_CR_NPBLB)
  2989. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL))
  2990. #else
  2991. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
  2992. #endif
  2993. {
  2994. return HAL_ERROR;
  2995. }
  2996. }
  2997. /* Process Locked */
  2998. __HAL_LOCK(hcryp);
  2999. /* Change the CRYP state */
  3000. hcryp->State = HAL_CRYP_STATE_BUSY;
  3001. /*==============================================*/
  3002. /* GCM/GMAC (or CCM when applicable) init phase */
  3003. /*==============================================*/
  3004. /* In case of init phase, the input data (Key and Initialization Vector) have
  3005. already been entered during the initialization process. Therefore, the
  3006. API just waits for the CCF flag to be set. */
  3007. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  3008. {
  3009. /* just wait for hash computation */
  3010. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3011. {
  3012. hcryp->State = HAL_CRYP_STATE_READY;
  3013. __HAL_UNLOCK(hcryp);
  3014. return HAL_TIMEOUT;
  3015. }
  3016. /* Clear CCF Flag */
  3017. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3018. /* Mark that the initialization phase is over */
  3019. hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
  3020. }
  3021. /*=====================================*/
  3022. /* GCM/GMAC or (CCM/)CMAC header phase */
  3023. /*=====================================*/
  3024. else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  3025. {
  3026. /* Set header phase; for GCM or GMAC, set data-byte at this point */
  3027. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3028. {
  3029. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
  3030. }
  3031. else
  3032. {
  3033. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
  3034. }
  3035. /* Enable the Peripheral */
  3036. __HAL_CRYP_ENABLE();
  3037. #if !defined(AES_CR_NPBLB)
  3038. /* in case of CMAC, enter B0 block in header phase, before the header itself. */
  3039. /* If Size = 0 (possible case of resumption after CMAC header phase suspension),
  3040. skip these steps and go directly to header buffer feeding to the HW */
  3041. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (Size != 0U))
  3042. {
  3043. inputaddr = (uint32_t)pInputData;
  3044. for(index=0U; (index < Size); index += 16U)
  3045. {
  3046. /* Write the Input block in the Data Input register */
  3047. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3048. inputaddr+=4U;
  3049. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3050. inputaddr+=4U;
  3051. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3052. inputaddr+=4U;
  3053. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3054. inputaddr+=4U;
  3055. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3056. {
  3057. hcryp->State = HAL_CRYP_STATE_READY;
  3058. __HAL_UNLOCK(hcryp);
  3059. return HAL_TIMEOUT;
  3060. }
  3061. /* Clear CCF Flag */
  3062. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3063. /* If the suspension flag has been raised and if the processing is not about
  3064. to end, suspend processing */
  3065. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < Size))
  3066. {
  3067. /* reset SuspendRequest */
  3068. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  3069. /* Change the CRYP state */
  3070. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  3071. /* Mark that the header phase is over */
  3072. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  3073. /* Save current reading and writing locations of Input and Output buffers */
  3074. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3075. /* Save the total number of bytes (B blocks + header) that remain to be
  3076. processed at this point */
  3077. hcryp->CrypInCount = hcryp->Init.HeaderSize + Size - (index+16U);
  3078. /* Process Unlocked */
  3079. __HAL_UNLOCK(hcryp);
  3080. return HAL_OK;
  3081. }
  3082. } /* for(index=0; (index < Size); index += 16) */
  3083. }
  3084. #endif /* !defined(AES_CR_NPBLB) */
  3085. /* Enter header */
  3086. inputaddr = (uint32_t)hcryp->Init.Header;
  3087. /* Local variable headerlength is a number of bytes multiple of 128 bits,
  3088. remaining header data (if any) are handled after this loop */
  3089. headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ;
  3090. if ((hcryp->Init.HeaderSize % 16U) != 0U)
  3091. {
  3092. difflength = (uint32_t) (hcryp->Init.HeaderSize - headerlength);
  3093. }
  3094. for(index=0U; index < headerlength; index += 16U)
  3095. {
  3096. /* Write the Input block in the Data Input register */
  3097. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3098. inputaddr+=4U;
  3099. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3100. inputaddr+=4U;
  3101. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3102. inputaddr+=4U;
  3103. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3104. inputaddr+=4U;
  3105. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3106. {
  3107. hcryp->State = HAL_CRYP_STATE_READY;
  3108. __HAL_UNLOCK(hcryp);
  3109. return HAL_TIMEOUT;
  3110. }
  3111. /* Clear CCF Flag */
  3112. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3113. /* If the suspension flag has been raised and if the processing is not about
  3114. to end, suspend processing */
  3115. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < headerlength))
  3116. {
  3117. /* reset SuspendRequest */
  3118. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  3119. /* Change the CRYP state */
  3120. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  3121. /* Mark that the header phase is over */
  3122. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  3123. /* Save current reading and writing locations of Input and Output buffers */
  3124. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3125. /* Save the total number of bytes that remain to be processed at this point */
  3126. hcryp->CrypInCount = hcryp->Init.HeaderSize - (index+16U);
  3127. /* Process Unlocked */
  3128. __HAL_UNLOCK(hcryp);
  3129. return HAL_OK;
  3130. }
  3131. }
  3132. /* Case header length is not a multiple of 16 bytes */
  3133. if (difflength != 0U)
  3134. {
  3135. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3136. CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
  3137. }
  3138. /* Mark that the header phase is over */
  3139. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  3140. }
  3141. /*============================================*/
  3142. /* GCM (or CCM when applicable) payload phase */
  3143. /*============================================*/
  3144. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  3145. {
  3146. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
  3147. /* if the header phase has been bypassed, AES must be enabled again */
  3148. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  3149. {
  3150. __HAL_CRYP_ENABLE();
  3151. }
  3152. inputaddr = (uint32_t)pInputData;
  3153. outputaddr = (uint32_t)pOutputData;
  3154. /* Enter payload */
  3155. /* Specific handling to manage payload last block size less than 128 bits */
  3156. if ((Size % 16U) != 0U)
  3157. {
  3158. payloadlength = (Size/16U) * 16U;
  3159. difflength = (uint32_t) (Size - payloadlength);
  3160. addhoc_process = 1U;
  3161. }
  3162. else
  3163. {
  3164. payloadlength = Size;
  3165. addhoc_process = 0U;
  3166. }
  3167. /* Feed payload */
  3168. for(index=0U; index < payloadlength; index += 16U)
  3169. {
  3170. /* Write the Input block in the Data Input register */
  3171. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3172. inputaddr+=4U;
  3173. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3174. inputaddr+=4U;
  3175. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3176. inputaddr+=4U;
  3177. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3178. inputaddr+=4U;
  3179. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3180. {
  3181. hcryp->State = HAL_CRYP_STATE_READY;
  3182. __HAL_UNLOCK(hcryp);
  3183. return HAL_TIMEOUT;
  3184. }
  3185. /* Clear CCF Flag */
  3186. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3187. /* Retrieve output data: read the output block
  3188. from the Data Output Register */
  3189. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  3190. outputaddr+=4U;
  3191. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  3192. outputaddr+=4U;
  3193. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  3194. outputaddr+=4U;
  3195. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  3196. outputaddr+=4U;
  3197. /* If the suspension flag has been raised and if the processing is not about
  3198. to end, suspend processing */
  3199. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < payloadlength))
  3200. {
  3201. /* no flag waiting under IRQ handling */
  3202. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  3203. {
  3204. /* Ensure that Busy flag is reset */
  3205. if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK)
  3206. {
  3207. hcryp->State = HAL_CRYP_STATE_READY;
  3208. __HAL_UNLOCK(hcryp);
  3209. return HAL_TIMEOUT;
  3210. }
  3211. }
  3212. /* reset SuspendRequest */
  3213. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  3214. /* Change the CRYP state */
  3215. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  3216. /* Mark that the header phase is over */
  3217. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  3218. /* Save current reading and writing locations of Input and Output buffers */
  3219. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  3220. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3221. /* Save the number of bytes that remain to be processed at this point */
  3222. hcryp->CrypInCount = Size - (index+16U);
  3223. /* Process Unlocked */
  3224. __HAL_UNLOCK(hcryp);
  3225. return HAL_OK;
  3226. }
  3227. }
  3228. /* Additional processing to manage GCM(/CCM) encryption and decryption cases when
  3229. payload last block size less than 128 bits */
  3230. if (addhoc_process == 1U)
  3231. {
  3232. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3233. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  3234. CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
  3235. } /* (addhoc_process == 1) */
  3236. /* Mark that the payload phase is over */
  3237. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  3238. }
  3239. /*====================================*/
  3240. /* GCM/GMAC or (CCM/)CMAC final phase */
  3241. /*====================================*/
  3242. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  3243. {
  3244. tagaddr = (uint32_t)pOutputData;
  3245. #if defined(AES_CR_NPBLB)
  3246. /* By default, clear NPBLB field */
  3247. CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
  3248. #endif
  3249. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
  3250. /* if the header and payload phases have been bypassed, AES must be enabled again */
  3251. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  3252. {
  3253. __HAL_CRYP_ENABLE();
  3254. }
  3255. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3256. {
  3257. headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
  3258. inputlength = Size * 8U; /* input length in bits */
  3259. if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  3260. {
  3261. hcryp->Instance->DINR = __RBIT((headerlength)>>32U);
  3262. hcryp->Instance->DINR = __RBIT(headerlength);
  3263. hcryp->Instance->DINR = __RBIT((inputlength)>>32U);
  3264. hcryp->Instance->DINR = __RBIT(inputlength);
  3265. }
  3266. else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  3267. {
  3268. hcryp->Instance->DINR = __REV((headerlength)>>32U);
  3269. hcryp->Instance->DINR = __REV(headerlength);
  3270. hcryp->Instance->DINR = __REV((inputlength)>>32U);
  3271. hcryp->Instance->DINR = __REV(inputlength);
  3272. }
  3273. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  3274. {
  3275. hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U);
  3276. hcryp->Instance->DINR = __ROR(headerlength, 16U);
  3277. hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U);
  3278. hcryp->Instance->DINR = __ROR(inputlength, 16U);
  3279. }
  3280. else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
  3281. {
  3282. hcryp->Instance->DINR = (uint32_t)(headerlength>>32U);
  3283. hcryp->Instance->DINR = (uint32_t)(headerlength);
  3284. hcryp->Instance->DINR = (uint32_t)(inputlength>>32U);
  3285. hcryp->Instance->DINR = (uint32_t)(inputlength);
  3286. }
  3287. }
  3288. #if !defined(AES_CR_NPBLB)
  3289. else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3290. {
  3291. inputaddr = (uint32_t)pInputData;
  3292. /* Enter the last block made of a 128-bit value formatted
  3293. from the original B0 packet. */
  3294. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3295. inputaddr+=4U;
  3296. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3297. inputaddr+=4U;
  3298. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3299. inputaddr+=4U;
  3300. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3301. }
  3302. #endif
  3303. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3304. {
  3305. hcryp->State = HAL_CRYP_STATE_READY;
  3306. __HAL_UNLOCK(hcryp);
  3307. return HAL_TIMEOUT;
  3308. }
  3309. /* Read the Auth TAG in the Data Out register */
  3310. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  3311. tagaddr+=4U;
  3312. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  3313. tagaddr+=4U;
  3314. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  3315. tagaddr+=4U;
  3316. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  3317. /* Clear CCF Flag */
  3318. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3319. /* Mark that the final phase is over */
  3320. hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
  3321. /* Disable the Peripheral */
  3322. __HAL_CRYP_DISABLE();
  3323. }
  3324. /*=================================================*/
  3325. /* case incorrect hcryp->Init.GCMCMACPhase setting */
  3326. /*=================================================*/
  3327. else
  3328. {
  3329. hcryp->State = HAL_CRYP_STATE_ERROR;
  3330. __HAL_UNLOCK(hcryp);
  3331. return HAL_ERROR;
  3332. }
  3333. /* Change the CRYP state */
  3334. hcryp->State = HAL_CRYP_STATE_READY;
  3335. /* Process Unlocked */
  3336. __HAL_UNLOCK(hcryp);
  3337. return HAL_OK;
  3338. }
  3339. else
  3340. {
  3341. return HAL_BUSY;
  3342. }
  3343. }
  3344. /**
  3345. * @brief Carry out in interrupt mode the authentication tag generation as well as the ciphering or deciphering
  3346. * operation according to hcryp->Init structure fields.
  3347. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3348. * the configuration information for CRYP module
  3349. * @param pInputData
  3350. * - pointer to payload data in GCM payload phase,
  3351. * - pointer to B0 block in CMAC header phase,
  3352. * - pointer to C block in CMAC final phase.
  3353. * Parameter is meaningless in case of GCM/GMAC init, header and final phases.
  3354. * @param Size
  3355. * - length of the input payload data buffer in bytes,
  3356. * - length of B0 block (in bytes) in CMAC header phase,
  3357. * - length of C block (in bytes) in CMAC final phase.
  3358. * - Parameter is meaningless in case of GCM/GMAC init and header phases.
  3359. * @param pOutputData
  3360. * - pointer to plain or cipher text in GCM payload phase,
  3361. * - pointer to authentication tag in GCM/GMAC and CMAC final phases.
  3362. * - Parameter is meaningless in case of GCM/GMAC init and header phases
  3363. * and in case of CMAC header phase.
  3364. * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC.
  3365. * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
  3366. * can be skipped by the user if so required.
  3367. * @retval HAL status
  3368. */
  3369. HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData)
  3370. {
  3371. uint32_t inputaddr = 0U;
  3372. uint64_t headerlength = 0U;
  3373. uint64_t inputlength = 0U;
  3374. uint32_t index = 0U;
  3375. uint32_t addhoc_process = 0U;
  3376. uint32_t difflength = 0U;
  3377. uint32_t difflengthmod4 = 0U;
  3378. uint32_t mask[3U] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU};
  3379. if (hcryp->State == HAL_CRYP_STATE_READY)
  3380. {
  3381. /* input/output parameters check */
  3382. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  3383. {
  3384. if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U))
  3385. {
  3386. return HAL_ERROR;
  3387. }
  3388. #if defined(AES_CR_NPBLB)
  3389. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  3390. #else
  3391. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3392. #endif
  3393. {
  3394. /* In case of CMAC header phase resumption, we can have pInputData = NULL and Size = 0 */
  3395. if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U)))
  3396. {
  3397. return HAL_ERROR;
  3398. }
  3399. }
  3400. }
  3401. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  3402. {
  3403. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  3404. {
  3405. return HAL_ERROR;
  3406. }
  3407. }
  3408. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  3409. {
  3410. if (pOutputData == NULL)
  3411. {
  3412. return HAL_ERROR;
  3413. }
  3414. #if defined(AES_CR_NPBLB)
  3415. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL))
  3416. #else
  3417. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
  3418. #endif
  3419. {
  3420. return HAL_ERROR;
  3421. }
  3422. }
  3423. /* Process Locked */
  3424. __HAL_LOCK(hcryp);
  3425. /* Change the CRYP state */
  3426. hcryp->State = HAL_CRYP_STATE_BUSY;
  3427. /* Process Unlocked */
  3428. __HAL_UNLOCK(hcryp);
  3429. /* Enable Computation Complete Flag and Error Interrupts */
  3430. __HAL_CRYP_ENABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  3431. /*==============================================*/
  3432. /* GCM/GMAC (or CCM when applicable) init phase */
  3433. /*==============================================*/
  3434. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  3435. {
  3436. /* In case of init phase, the input data (Key and Initialization Vector) have
  3437. already been entered during the initialization process. Therefore, the
  3438. software just waits for the CCF interrupt to be raised and which will
  3439. be handled by CRYP_AES_Auth_IT() API. */
  3440. }
  3441. /*=====================================*/
  3442. /* GCM/GMAC or (CCM/)CMAC header phase */
  3443. /*=====================================*/
  3444. else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  3445. {
  3446. #if defined(AES_CR_NPBLB)
  3447. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  3448. #else
  3449. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3450. #endif
  3451. {
  3452. /* In case of CMAC, B blocks are first entered, before the header.
  3453. Therefore, B blocks and the header are entered back-to-back
  3454. as if it was only one single block.
  3455. However, in case of resumption after suspension, if all the
  3456. B blocks have been entered (in that case, Size = 0), only the
  3457. remainder of the non-processed header bytes are entered. */
  3458. if (Size != 0U)
  3459. {
  3460. hcryp->CrypInCount = Size + hcryp->Init.HeaderSize;
  3461. hcryp->pCrypInBuffPtr = pInputData;
  3462. }
  3463. else
  3464. {
  3465. hcryp->CrypInCount = hcryp->Init.HeaderSize;
  3466. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  3467. }
  3468. }
  3469. else
  3470. {
  3471. /* Get the header addresses and sizes */
  3472. hcryp->CrypInCount = hcryp->Init.HeaderSize;
  3473. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  3474. }
  3475. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  3476. /* Set header phase; for GCM or GMAC, set data-byte at this point */
  3477. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3478. {
  3479. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
  3480. }
  3481. else
  3482. {
  3483. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
  3484. }
  3485. /* Enable the Peripheral */
  3486. __HAL_CRYP_ENABLE();
  3487. /* Increment/decrement instance pointer/counter */
  3488. if (hcryp->CrypInCount == 0U)
  3489. {
  3490. /* Case of no header */
  3491. hcryp->State = HAL_CRYP_STATE_READY;
  3492. return HAL_OK;
  3493. }
  3494. else if (hcryp->CrypInCount < 16U)
  3495. {
  3496. hcryp->CrypInCount = 0U;
  3497. addhoc_process = 1U;
  3498. difflength = (uint32_t) (hcryp->Init.HeaderSize);
  3499. difflengthmod4 = difflength%4U;
  3500. }
  3501. else
  3502. {
  3503. hcryp->pCrypInBuffPtr += 16U;
  3504. hcryp->CrypInCount -= 16U;
  3505. }
  3506. #if defined(AES_CR_NPBLB)
  3507. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  3508. #else
  3509. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3510. #endif
  3511. {
  3512. if (hcryp->CrypInCount == hcryp->Init.HeaderSize)
  3513. {
  3514. /* All B blocks will have been entered after the next
  3515. four DINR writing, so point at header buffer for
  3516. the next iteration */
  3517. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  3518. }
  3519. }
  3520. /* Enter header first block to initiate the process
  3521. in the Data Input register */
  3522. if (addhoc_process == 0U)
  3523. {
  3524. /* Header has size equal or larger than 128 bits */
  3525. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3526. inputaddr+=4U;
  3527. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3528. inputaddr+=4U;
  3529. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3530. inputaddr+=4U;
  3531. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3532. }
  3533. else
  3534. {
  3535. /* Header has size less than 128 bits */
  3536. /* Enter complete words when possible */
  3537. for(index=0U; index < (difflength/4U); index ++)
  3538. {
  3539. /* Write the Input block in the Data Input register */
  3540. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3541. inputaddr+=4U;
  3542. }
  3543. /* Enter incomplete word padded with zeroes if applicable
  3544. (case of header length not a multiple of 32-bits) */
  3545. if (difflengthmod4 != 0U)
  3546. {
  3547. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]);
  3548. }
  3549. /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
  3550. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  3551. {
  3552. hcryp->Instance->DINR = 0U;
  3553. }
  3554. }
  3555. }
  3556. /*============================================*/
  3557. /* GCM (or CCM when applicable) payload phase */
  3558. /*============================================*/
  3559. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  3560. {
  3561. /* Get the buffer addresses and sizes */
  3562. hcryp->CrypInCount = Size;
  3563. hcryp->pCrypInBuffPtr = pInputData;
  3564. hcryp->pCrypOutBuffPtr = pOutputData;
  3565. hcryp->CrypOutCount = Size;
  3566. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  3567. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCM_PAYLOAD_PHASE);
  3568. /* if the header phase has been bypassed, AES must be enabled again */
  3569. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  3570. {
  3571. __HAL_CRYP_ENABLE();
  3572. }
  3573. /* Specific handling to manage payload size less than 128 bits */
  3574. if (Size < 16U)
  3575. {
  3576. #if defined(AES_CR_NPBLB)
  3577. /* In case of GCM encryption or CCM decryption, specify the number of padding
  3578. bytes in last block of payload */
  3579. if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE)
  3580. {
  3581. if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC)
  3582. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT))
  3583. || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC)
  3584. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT)))
  3585. {
  3586. /* Set NPBLB field in writing the number of padding bytes
  3587. for the last block of payload */
  3588. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength);
  3589. }
  3590. }
  3591. #else
  3592. /* Software workaround applied to GCM encryption only */
  3593. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  3594. {
  3595. /* Change the mode configured in CHMOD bits of CR register to select CTR mode */
  3596. __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR);
  3597. }
  3598. #endif
  3599. /* Set hcryp->CrypInCount to 0 (no more data to enter) */
  3600. hcryp->CrypInCount = 0U;
  3601. /* Insert the last block (which size is inferior to 128 bits) padded with zeroes,
  3602. to have a complete block of 128 bits */
  3603. difflength = (uint32_t) (Size);
  3604. difflengthmod4 = difflength%4U;
  3605. /* Insert the last block (which size is inferior to 128 bits) padded with zeroes
  3606. to have a complete block of 128 bits */
  3607. for(index=0U; index < (difflength/4U); index ++)
  3608. {
  3609. /* Write the Input block in the Data Input register */
  3610. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3611. inputaddr+=4U;
  3612. }
  3613. /* If required, manage input data size not multiple of 32 bits */
  3614. if (difflengthmod4 != 0U)
  3615. {
  3616. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]);
  3617. }
  3618. /* Wrap-up in padding with zero-words if applicable */
  3619. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  3620. {
  3621. hcryp->Instance->DINR = 0U;
  3622. }
  3623. }
  3624. else
  3625. {
  3626. /* Increment/decrement instance pointer/counter */
  3627. hcryp->pCrypInBuffPtr += 16U;
  3628. hcryp->CrypInCount -= 16U;
  3629. /* Enter payload first block to initiate the process
  3630. in the Data Input register */
  3631. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3632. inputaddr+=4U;
  3633. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3634. inputaddr+=4U;
  3635. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3636. inputaddr+=4U;
  3637. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3638. }
  3639. }
  3640. /*====================================*/
  3641. /* GCM/GMAC or (CCM/)CMAC final phase */
  3642. /*====================================*/
  3643. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  3644. {
  3645. hcryp->pCrypOutBuffPtr = pOutputData;
  3646. #if defined(AES_CR_NPBLB)
  3647. /* By default, clear NPBLB field */
  3648. CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
  3649. #endif
  3650. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
  3651. /* if the header and payload phases have been bypassed, AES must be enabled again */
  3652. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  3653. {
  3654. __HAL_CRYP_ENABLE();
  3655. }
  3656. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3657. {
  3658. headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
  3659. inputlength = Size * 8U; /* Input length in bits */
  3660. /* Write the number of bits in the header on 64 bits followed by the number
  3661. of bits in the payload on 64 bits as well */
  3662. if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  3663. {
  3664. hcryp->Instance->DINR = __RBIT((headerlength)>>32U);
  3665. hcryp->Instance->DINR = __RBIT(headerlength);
  3666. hcryp->Instance->DINR = __RBIT((inputlength)>>32U);
  3667. hcryp->Instance->DINR = __RBIT(inputlength);
  3668. }
  3669. else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  3670. {
  3671. hcryp->Instance->DINR = __REV((headerlength)>>32U);
  3672. hcryp->Instance->DINR = __REV(headerlength);
  3673. hcryp->Instance->DINR = __REV((inputlength)>>32U);
  3674. hcryp->Instance->DINR = __REV(inputlength);
  3675. }
  3676. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  3677. {
  3678. hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U);
  3679. hcryp->Instance->DINR = __ROR(headerlength, 16U);
  3680. hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U);
  3681. hcryp->Instance->DINR = __ROR(inputlength, 16U);
  3682. }
  3683. else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
  3684. {
  3685. hcryp->Instance->DINR = (uint32_t)(headerlength>>32U);
  3686. hcryp->Instance->DINR = (uint32_t)(headerlength);
  3687. hcryp->Instance->DINR = (uint32_t)(inputlength>>32U);
  3688. hcryp->Instance->DINR = (uint32_t)(inputlength);
  3689. }
  3690. }
  3691. #if !defined(AES_CR_NPBLB)
  3692. else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3693. {
  3694. inputaddr = (uint32_t)pInputData;
  3695. /* Enter the last block made of a 128-bit value formatted
  3696. from the original B0 packet. */
  3697. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3698. inputaddr+=4U;
  3699. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3700. inputaddr+=4U;
  3701. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3702. inputaddr+=4U;
  3703. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3704. inputaddr+=4U;
  3705. }
  3706. #endif
  3707. }
  3708. /*=================================================*/
  3709. /* case incorrect hcryp->Init.GCMCMACPhase setting */
  3710. /*=================================================*/
  3711. else
  3712. {
  3713. hcryp->State = HAL_CRYP_STATE_ERROR;
  3714. return HAL_ERROR;
  3715. }
  3716. return HAL_OK;
  3717. }
  3718. else
  3719. {
  3720. return HAL_BUSY;
  3721. }
  3722. }
  3723. /**
  3724. * @brief Carry out in DMA mode the authentication tag generation as well as the ciphering or deciphering
  3725. * operation according to hcryp->Init structure fields.
  3726. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3727. * the configuration information for CRYP module
  3728. * @param pInputData
  3729. * - pointer to payload data in GCM payload phase,
  3730. * - pointer to B0 block in CMAC header phase,
  3731. * - pointer to C block in CMAC final phase.
  3732. * - Parameter is meaningless in case of GCM/GMAC init, header and final phases.
  3733. * @param Size
  3734. * - length of the input payload data buffer in bytes,
  3735. * - length of B block (in bytes) in CMAC header phase,
  3736. * - length of C block (in bytes) in CMAC final phase.
  3737. * - Parameter is meaningless in case of GCM/GMAC init and header phases.
  3738. * @param pOutputData
  3739. * - pointer to plain or cipher text in GCM payload phase,
  3740. * - pointer to authentication tag in GCM/GMAC and CMAC final phases.
  3741. * - Parameter is meaningless in case of GCM/GMAC init and header phases
  3742. * and in case of CMAC header phase.
  3743. * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC.
  3744. * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
  3745. * can be skipped by the user if so required.
  3746. * @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3747. * @retval HAL status
  3748. */
  3749. HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData)
  3750. {
  3751. uint32_t inputaddr = 0U;
  3752. uint32_t outputaddr = 0U;
  3753. uint32_t tagaddr = 0U;
  3754. uint64_t headerlength = 0U;
  3755. uint64_t inputlength = 0U;
  3756. uint64_t payloadlength = 0U;
  3757. if (hcryp->State == HAL_CRYP_STATE_READY)
  3758. {
  3759. /* input/output parameters check */
  3760. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  3761. {
  3762. if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U))
  3763. {
  3764. return HAL_ERROR;
  3765. }
  3766. #if defined(AES_CR_NPBLB)
  3767. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  3768. #else
  3769. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3770. #endif
  3771. {
  3772. if ((pInputData == NULL) || (Size == 0U))
  3773. {
  3774. return HAL_ERROR;
  3775. }
  3776. }
  3777. }
  3778. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  3779. {
  3780. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  3781. {
  3782. return HAL_ERROR;
  3783. }
  3784. }
  3785. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  3786. {
  3787. if (pOutputData == NULL)
  3788. {
  3789. return HAL_ERROR;
  3790. }
  3791. #if defined(AES_CR_NPBLB)
  3792. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL))
  3793. #else
  3794. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
  3795. #endif
  3796. {
  3797. return HAL_ERROR;
  3798. }
  3799. }
  3800. /* Process Locked */
  3801. __HAL_LOCK(hcryp);
  3802. /* Change the CRYP state */
  3803. hcryp->State = HAL_CRYP_STATE_BUSY;
  3804. /*==============================================*/
  3805. /* GCM/GMAC (or CCM when applicable) init phase */
  3806. /*==============================================*/
  3807. /* In case of init phase, the input data (Key and Initialization Vector) have
  3808. already been entered during the initialization process. No DMA transfer is
  3809. required at that point therefore, the software just waits for the CCF flag
  3810. to be raised. */
  3811. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  3812. {
  3813. /* just wait for hash computation */
  3814. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  3815. {
  3816. hcryp->State = HAL_CRYP_STATE_READY;
  3817. __HAL_UNLOCK(hcryp);
  3818. return HAL_TIMEOUT;
  3819. }
  3820. /* Clear CCF Flag */
  3821. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3822. /* Mark that the initialization phase is over */
  3823. hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
  3824. hcryp->State = HAL_CRYP_STATE_READY;
  3825. }
  3826. /*===============================*/
  3827. /* GCM/GMAC or CMAC header phase */
  3828. /*===============================*/
  3829. else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE)
  3830. {
  3831. /* Set header phase; for GCM or GMAC, set data-byte at this point */
  3832. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3833. {
  3834. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_GCMCMAC_HEADER_PHASE|hcryp->Init.DataType);
  3835. }
  3836. else
  3837. {
  3838. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_HEADER_PHASE);
  3839. }
  3840. #if !defined(AES_CR_NPBLB)
  3841. /* enter first B0 block in polling mode (no DMA transfer for B0) */
  3842. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3843. {
  3844. /* Enable the CRYP peripheral */
  3845. __HAL_CRYP_ENABLE();
  3846. inputaddr = (uint32_t)pInputData;
  3847. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3848. inputaddr+=4U;
  3849. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3850. inputaddr+=4U;
  3851. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3852. inputaddr+=4U;
  3853. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3854. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  3855. {
  3856. hcryp->State = HAL_CRYP_STATE_READY;
  3857. __HAL_UNLOCK(hcryp);
  3858. return HAL_TIMEOUT;
  3859. }
  3860. /* Clear CCF Flag */
  3861. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3862. }
  3863. #endif
  3864. /* No header case */
  3865. if (hcryp->Init.Header == NULL)
  3866. {
  3867. hcryp->State = HAL_CRYP_STATE_READY;
  3868. /* Mark that the header phase is over */
  3869. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  3870. /* Process Unlocked */
  3871. __HAL_UNLOCK(hcryp);
  3872. return HAL_OK;
  3873. }
  3874. inputaddr = (uint32_t)hcryp->Init.Header;
  3875. if ((hcryp->Init.HeaderSize % 16U) != 0U)
  3876. {
  3877. if (hcryp->Init.HeaderSize < 16U)
  3878. {
  3879. CRYP_Padding(hcryp, (uint32_t) (hcryp->Init.HeaderSize), CRYP_POLLING_OFF);
  3880. hcryp->State = HAL_CRYP_STATE_READY;
  3881. /* Mark that the header phase is over */
  3882. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  3883. /* CCF flag indicating header phase AES processing completion
  3884. will be checked at the start of the next phase:
  3885. - payload phase (GCM / CCM when applicable)
  3886. - final phase (GMAC or CMAC). */
  3887. }
  3888. else
  3889. {
  3890. /* Local variable headerlength is a number of bytes multiple of 128 bits,
  3891. remaining header data (if any) are handled after this loop */
  3892. headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ;
  3893. /* Store the ending transfer point */
  3894. hcryp->pCrypInBuffPtr = hcryp->Init.Header + headerlength;
  3895. hcryp->CrypInCount = (uint32_t)(hcryp->Init.HeaderSize - headerlength); /* remainder */
  3896. /* Set the input and output addresses and start DMA transfer */
  3897. /* (incomplete DMA transfer, will be wrapped up after completion of
  3898. the first one (initiated here) with data padding */
  3899. CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, headerlength, 0U);
  3900. }
  3901. }
  3902. else
  3903. {
  3904. hcryp->CrypInCount = 0U;
  3905. /* Set the input address and start DMA transfer */
  3906. CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, hcryp->Init.HeaderSize, 0U);
  3907. }
  3908. }
  3909. /*============================================*/
  3910. /* GCM (or CCM when applicable) payload phase */
  3911. /*============================================*/
  3912. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  3913. {
  3914. /* Coming from header phase, wait for CCF flag to be raised
  3915. if header present and fed to the IP in the previous phase */
  3916. if (hcryp->Init.Header != NULL)
  3917. {
  3918. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  3919. {
  3920. hcryp->State = HAL_CRYP_STATE_READY;
  3921. __HAL_UNLOCK(hcryp);
  3922. return HAL_TIMEOUT;
  3923. }
  3924. }
  3925. else
  3926. {
  3927. /* Enable the Peripheral since wasn't in header phase (no header case) */
  3928. __HAL_CRYP_ENABLE();
  3929. }
  3930. /* Clear CCF Flag */
  3931. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3932. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
  3933. /* Specific handling to manage payload size less than 128 bits */
  3934. if ((Size % 16U) != 0U)
  3935. {
  3936. inputaddr = (uint32_t)pInputData;
  3937. outputaddr = (uint32_t)pOutputData;
  3938. if (Size < 16U)
  3939. {
  3940. /* Block is now entered in polling mode, no actual gain in resorting to DMA */
  3941. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3942. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  3943. CRYP_Padding(hcryp, (uint32_t)Size, CRYP_POLLING_ON);
  3944. /* Change the CRYP state to ready */
  3945. hcryp->State = HAL_CRYP_STATE_READY;
  3946. /* Mark that the payload phase is over */
  3947. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  3948. /* Call output data transfer complete callback */
  3949. HAL_CRYP_OutCpltCallback(hcryp);
  3950. }
  3951. else
  3952. {
  3953. payloadlength = (Size/16U) * 16U;
  3954. /* Store the ending transfer points */
  3955. hcryp->pCrypInBuffPtr = pInputData + payloadlength;
  3956. hcryp->pCrypOutBuffPtr = pOutputData + payloadlength;
  3957. hcryp->CrypInCount = (uint32_t)(Size - payloadlength); /* remainder */
  3958. /* Set the input and output addresses and start DMA transfer */
  3959. /* (incomplete DMA transfer, will be wrapped up with data padding
  3960. after completion of the one initiated here) */
  3961. CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, payloadlength, outputaddr);
  3962. }
  3963. }
  3964. else
  3965. {
  3966. hcryp->CrypInCount = 0U;
  3967. inputaddr = (uint32_t)pInputData;
  3968. outputaddr = (uint32_t)pOutputData;
  3969. /* Set the input and output addresses and start DMA transfer */
  3970. CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  3971. }
  3972. }
  3973. /*====================================*/
  3974. /* GCM/GMAC or (CCM/)CMAC final phase */
  3975. /*====================================*/
  3976. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  3977. {
  3978. /* If coming from header phase (GMAC or CMAC case),
  3979. wait for CCF flag to be raised */
  3980. if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_HEADER_PHASE)
  3981. {
  3982. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  3983. {
  3984. hcryp->State = HAL_CRYP_STATE_READY;
  3985. __HAL_UNLOCK(hcryp);
  3986. return HAL_TIMEOUT;
  3987. }
  3988. /* Clear CCF Flag */
  3989. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3990. }
  3991. tagaddr = (uint32_t)pOutputData;
  3992. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
  3993. /* if the header and payload phases have been bypassed, AES must be enabled again */
  3994. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  3995. {
  3996. __HAL_CRYP_ENABLE();
  3997. }
  3998. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3999. {
  4000. headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
  4001. inputlength = Size * 8U; /* input length in bits */
  4002. /* Write the number of bits in the header on 64 bits followed by the number
  4003. of bits in the payload on 64 bits as well */
  4004. if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  4005. {
  4006. hcryp->Instance->DINR = __RBIT((headerlength)>>32U);
  4007. hcryp->Instance->DINR = __RBIT(headerlength);
  4008. hcryp->Instance->DINR = __RBIT((inputlength)>>32U);
  4009. hcryp->Instance->DINR = __RBIT(inputlength);
  4010. }
  4011. else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  4012. {
  4013. hcryp->Instance->DINR = __REV((headerlength)>>32U);
  4014. hcryp->Instance->DINR = __REV(headerlength);
  4015. hcryp->Instance->DINR = __REV((inputlength)>>32U);
  4016. hcryp->Instance->DINR = __REV(inputlength);
  4017. }
  4018. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  4019. {
  4020. hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U);
  4021. hcryp->Instance->DINR = __ROR(headerlength, 16U);
  4022. hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U);
  4023. hcryp->Instance->DINR = __ROR(inputlength, 16U);
  4024. }
  4025. else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
  4026. {
  4027. hcryp->Instance->DINR = (uint32_t)(headerlength>>32U);
  4028. hcryp->Instance->DINR = (uint32_t)(headerlength);
  4029. hcryp->Instance->DINR = (uint32_t)(inputlength>>32U);
  4030. hcryp->Instance->DINR = (uint32_t)(inputlength);
  4031. }
  4032. }
  4033. #if !defined(AES_CR_NPBLB)
  4034. else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  4035. {
  4036. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4037. inputaddr = (uint32_t)pInputData;
  4038. /* Enter the last block made of a 128-bit value formatted
  4039. from the original B0 packet. */
  4040. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4041. inputaddr+=4U;
  4042. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4043. inputaddr+=4U;
  4044. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4045. inputaddr+=4U;
  4046. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4047. inputaddr+=4U;
  4048. }
  4049. #endif
  4050. /* No DMA transfer is required at that point therefore, the software
  4051. just waits for the CCF flag to be raised. */
  4052. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  4053. {
  4054. hcryp->State = HAL_CRYP_STATE_READY;
  4055. __HAL_UNLOCK(hcryp);
  4056. return HAL_TIMEOUT;
  4057. }
  4058. /* Clear CCF Flag */
  4059. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4060. /* Read the Auth TAG in the IN FIFO */
  4061. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  4062. tagaddr+=4U;
  4063. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  4064. tagaddr+=4U;
  4065. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  4066. tagaddr+=4U;
  4067. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  4068. /* Mark that the final phase is over */
  4069. hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
  4070. hcryp->State = HAL_CRYP_STATE_READY;
  4071. /* Disable the Peripheral */
  4072. __HAL_CRYP_DISABLE();
  4073. }
  4074. /*=================================================*/
  4075. /* case incorrect hcryp->Init.GCMCMACPhase setting */
  4076. /*=================================================*/
  4077. else
  4078. {
  4079. hcryp->State = HAL_CRYP_STATE_ERROR;
  4080. __HAL_UNLOCK(hcryp);
  4081. return HAL_ERROR;
  4082. }
  4083. /* Process Unlocked */
  4084. __HAL_UNLOCK(hcryp);
  4085. return HAL_OK;
  4086. }
  4087. else
  4088. {
  4089. return HAL_BUSY;
  4090. }
  4091. }
  4092. /**
  4093. * @}
  4094. */
  4095. /** @defgroup CRYPEx_Exported_Functions_Group3 AES suspension/resumption functions
  4096. * @brief Extended processing functions.
  4097. *
  4098. @verbatim
  4099. ==============================================================================
  4100. ##### AES extended suspension and resumption functions #####
  4101. ==============================================================================
  4102. [..] This section provides functions allowing to:
  4103. (+) save in memory the Initialization Vector, the Key registers, the Control register or
  4104. the Suspend registers when a process is suspended by a higher priority message
  4105. (+) write back in CRYP hardware block the saved values listed above when the suspended
  4106. lower priority message processing is resumed.
  4107. @endverbatim
  4108. * @{
  4109. */
  4110. /**
  4111. * @brief In case of message processing suspension, read the Initialization Vector.
  4112. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4113. * the configuration information for CRYP module.
  4114. * @param Output Pointer to the buffer containing the saved Initialization Vector.
  4115. * @note This value has to be stored for reuse by writing the AES_IVRx registers
  4116. * as soon as the interrupted processing has to be resumed.
  4117. * Applicable to all chaining modes.
  4118. * @note AES must be disabled when reading or resetting the IV values.
  4119. * @retval None
  4120. */
  4121. void HAL_CRYPEx_Read_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
  4122. {
  4123. uint32_t outputaddr = (uint32_t)Output;
  4124. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR3);
  4125. outputaddr+=4U;
  4126. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR2);
  4127. outputaddr+=4U;
  4128. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR1);
  4129. outputaddr+=4U;
  4130. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR0);
  4131. }
  4132. /**
  4133. * @brief In case of message processing resumption, rewrite the Initialization
  4134. * Vector in the AES_IVRx registers.
  4135. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4136. * the configuration information for CRYP module.
  4137. * @param Input Pointer to the buffer containing the saved Initialization Vector to
  4138. * write back in the CRYP hardware block.
  4139. * @note Applicable to all chaining modes.
  4140. * @note AES must be disabled when reading or resetting the IV values.
  4141. * @retval None
  4142. */
  4143. void HAL_CRYPEx_Write_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
  4144. {
  4145. uint32_t ivaddr = (uint32_t)Input;
  4146. hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
  4147. ivaddr+=4U;
  4148. hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
  4149. ivaddr+=4U;
  4150. hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
  4151. ivaddr+=4U;
  4152. hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
  4153. }
  4154. /**
  4155. * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Suspend Registers.
  4156. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4157. * the configuration information for CRYP module.
  4158. * @param Output Pointer to the buffer containing the saved Suspend Registers.
  4159. * @note These values have to be stored for reuse by writing back the AES_SUSPxR registers
  4160. * as soon as the interrupted processing has to be resumed.
  4161. * @retval None
  4162. */
  4163. void HAL_CRYPEx_Read_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
  4164. {
  4165. uint32_t outputaddr = (uint32_t)Output;
  4166. /* In case of GCM payload phase encryption, check that suspension can be carried out */
  4167. if (READ_BIT(hcryp->Instance->CR, (AES_CR_GCMPH|AES_CR_MODE)) == (CRYP_GCM_PAYLOAD_PHASE|CRYP_ALGOMODE_ENCRYPT))
  4168. {
  4169. /* Ensure that Busy flag is reset */
  4170. if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK)
  4171. {
  4172. hcryp->ErrorCode |= HAL_CRYP_BUSY_ERROR;
  4173. hcryp->State = HAL_CRYP_STATE_ERROR;
  4174. /* Process Unlocked */
  4175. __HAL_UNLOCK(hcryp);
  4176. HAL_CRYP_ErrorCallback(hcryp);
  4177. return ;
  4178. }
  4179. }
  4180. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP7R);
  4181. outputaddr+=4U;
  4182. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP6R);
  4183. outputaddr+=4U;
  4184. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP5R);
  4185. outputaddr+=4U;
  4186. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP4R);
  4187. outputaddr+=4U;
  4188. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP3R);
  4189. outputaddr+=4U;
  4190. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP2R);
  4191. outputaddr+=4U;
  4192. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP1R);
  4193. outputaddr+=4U;
  4194. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP0R);
  4195. }
  4196. /**
  4197. * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Suspend
  4198. * Registers in the AES_SUSPxR registers.
  4199. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4200. * the configuration information for CRYP module.
  4201. * @param Input Pointer to the buffer containing the saved suspend registers to
  4202. * write back in the CRYP hardware block.
  4203. * @retval None
  4204. */
  4205. void HAL_CRYPEx_Write_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
  4206. {
  4207. uint32_t ivaddr = (uint32_t)Input;
  4208. hcryp->Instance->SUSP7R = __REV(*(uint32_t*)(ivaddr));
  4209. ivaddr+=4U;
  4210. hcryp->Instance->SUSP6R = __REV(*(uint32_t*)(ivaddr));
  4211. ivaddr+=4U;
  4212. hcryp->Instance->SUSP5R = __REV(*(uint32_t*)(ivaddr));
  4213. ivaddr+=4U;
  4214. hcryp->Instance->SUSP4R = __REV(*(uint32_t*)(ivaddr));
  4215. ivaddr+=4U;
  4216. hcryp->Instance->SUSP3R = __REV(*(uint32_t*)(ivaddr));
  4217. ivaddr+=4U;
  4218. hcryp->Instance->SUSP2R = __REV(*(uint32_t*)(ivaddr));
  4219. ivaddr+=4U;
  4220. hcryp->Instance->SUSP1R = __REV(*(uint32_t*)(ivaddr));
  4221. ivaddr+=4U;
  4222. hcryp->Instance->SUSP0R = __REV(*(uint32_t*)(ivaddr));
  4223. }
  4224. /**
  4225. * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Key Registers.
  4226. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4227. * the configuration information for CRYP module.
  4228. * @param Output Pointer to the buffer containing the saved Key Registers.
  4229. * @param KeySize Indicates the key size (128 or 256 bits).
  4230. * @note These values have to be stored for reuse by writing back the AES_KEYRx registers
  4231. * as soon as the interrupted processing has to be resumed.
  4232. * @retval None
  4233. */
  4234. void HAL_CRYPEx_Read_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t KeySize)
  4235. {
  4236. uint32_t keyaddr = (uint32_t)Output;
  4237. if (KeySize == CRYP_KEYSIZE_256B)
  4238. {
  4239. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR7);
  4240. keyaddr+=4U;
  4241. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR6);
  4242. keyaddr+=4U;
  4243. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR5);
  4244. keyaddr+=4U;
  4245. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR4);
  4246. keyaddr+=4U;
  4247. }
  4248. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR3);
  4249. keyaddr+=4U;
  4250. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR2);
  4251. keyaddr+=4U;
  4252. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR1);
  4253. keyaddr+=4U;
  4254. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR0);
  4255. }
  4256. /**
  4257. * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Key
  4258. * Registers in the AES_KEYRx registers.
  4259. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4260. * the configuration information for CRYP module.
  4261. * @param Input Pointer to the buffer containing the saved key registers to
  4262. * write back in the CRYP hardware block.
  4263. * @param KeySize Indicates the key size (128 or 256 bits)
  4264. * @retval None
  4265. */
  4266. void HAL_CRYPEx_Write_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint32_t KeySize)
  4267. {
  4268. uint32_t keyaddr = (uint32_t)Input;
  4269. if (KeySize == CRYP_KEYSIZE_256B)
  4270. {
  4271. hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
  4272. keyaddr+=4U;
  4273. hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
  4274. keyaddr+=4U;
  4275. hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
  4276. keyaddr+=4U;
  4277. hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
  4278. keyaddr+=4U;
  4279. }
  4280. hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
  4281. keyaddr+=4U;
  4282. hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
  4283. keyaddr+=4U;
  4284. hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
  4285. keyaddr+=4U;
  4286. hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
  4287. }
  4288. /**
  4289. * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Control Register.
  4290. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4291. * the configuration information for CRYP module.
  4292. * @param Output Pointer to the buffer containing the saved Control Register.
  4293. * @note This values has to be stored for reuse by writing back the AES_CR register
  4294. * as soon as the interrupted processing has to be resumed.
  4295. * @retval None
  4296. */
  4297. void HAL_CRYPEx_Read_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
  4298. {
  4299. *(uint32_t*)(Output) = hcryp->Instance->CR;
  4300. }
  4301. /**
  4302. * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Control
  4303. * Registers in the AES_CR register.
  4304. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4305. * the configuration information for CRYP module.
  4306. * @param Input Pointer to the buffer containing the saved Control Register to
  4307. * write back in the CRYP hardware block.
  4308. * @retval None
  4309. */
  4310. void HAL_CRYPEx_Write_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
  4311. {
  4312. hcryp->Instance->CR = *(uint32_t*)(Input);
  4313. /* At the same time, set handle state back to READY to be able to resume the AES calculations
  4314. without the processing APIs returning HAL_BUSY when called. */
  4315. hcryp->State = HAL_CRYP_STATE_READY;
  4316. }
  4317. /**
  4318. * @brief Request CRYP processing suspension when in polling or interruption mode.
  4319. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4320. * the configuration information for CRYP module.
  4321. * @note Set the handle field SuspendRequest to the appropriate value so that
  4322. * the on-going CRYP processing is suspended as soon as the required
  4323. * conditions are met.
  4324. * @note It is advised not to suspend the CRYP processing when the DMA controller
  4325. * is managing the data transfer
  4326. * @retval None
  4327. */
  4328. void HAL_CRYPEx_ProcessSuspend(CRYP_HandleTypeDef *hcryp)
  4329. {
  4330. /* Set Handle Suspend Request field */
  4331. hcryp->SuspendRequest = HAL_CRYP_SUSPEND;
  4332. }
  4333. /**
  4334. * @}
  4335. */
  4336. /**
  4337. * @}
  4338. */
  4339. /** @addtogroup CRYPEx_Private_Functions
  4340. * @{
  4341. */
  4342. /**
  4343. * @brief DMA CRYP Input Data process complete callback
  4344. * for GCM, GMAC or CMAC chainging modes.
  4345. * @note Specific setting of hcryp fields are required only
  4346. * in the case of header phase where no output data DMA
  4347. * transfer is on-going (only input data transfer is enabled
  4348. * in such a case).
  4349. * @param hdma DMA handle.
  4350. * @retval None
  4351. */
  4352. static void CRYP_GCMCMAC_DMAInCplt(DMA_HandleTypeDef *hdma)
  4353. {
  4354. uint32_t difflength = 0U;
  4355. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  4356. /* Disable the DMA transfer for input request */
  4357. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  4358. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  4359. {
  4360. if (hcryp->CrypInCount != 0U)
  4361. {
  4362. /* Last block is now entered in polling mode, no actual gain in resorting to DMA */
  4363. difflength = hcryp->CrypInCount;
  4364. hcryp->CrypInCount = 0U;
  4365. CRYP_Padding(hcryp, difflength, CRYP_POLLING_OFF);
  4366. }
  4367. hcryp->State = HAL_CRYP_STATE_READY;
  4368. /* Mark that the header phase is over */
  4369. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  4370. }
  4371. /* CCF flag indicating header phase AES processing completion
  4372. will be checked at the start of the next phase:
  4373. - payload phase (GCM or CCM when applicable)
  4374. - final phase (GMAC or CMAC).
  4375. This allows to avoid the Wait on Flag within the IRQ handling. */
  4376. /* Call input data transfer complete callback */
  4377. HAL_CRYP_InCpltCallback(hcryp);
  4378. }
  4379. /**
  4380. * @brief DMA CRYP Output Data process complete callback
  4381. * for GCM, GMAC or CMAC chainging modes.
  4382. * @note This callback is called only in the payload phase.
  4383. * @param hdma DMA handle.
  4384. * @retval None
  4385. */
  4386. static void CRYP_GCMCMAC_DMAOutCplt(DMA_HandleTypeDef *hdma)
  4387. {
  4388. uint32_t difflength = 0U;
  4389. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  4390. /* Disable the DMA transfer for output request */
  4391. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  4392. /* Clear CCF Flag */
  4393. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4394. /* Initiate additional transfer to wrap-up data feeding to the IP */
  4395. if (hcryp->CrypInCount != 0U)
  4396. {
  4397. /* Last block is now entered in polling mode, no actual gain in resorting to DMA */
  4398. difflength = hcryp->CrypInCount;
  4399. hcryp->CrypInCount = 0U;
  4400. CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
  4401. }
  4402. /* Change the CRYP state to ready */
  4403. hcryp->State = HAL_CRYP_STATE_READY;
  4404. /* Mark that the payload phase is over */
  4405. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  4406. /* Call output data transfer complete callback */
  4407. HAL_CRYP_OutCpltCallback(hcryp);
  4408. }
  4409. /**
  4410. * @brief DMA CRYP communication error callback
  4411. * for GCM, GMAC or CMAC chainging modes.
  4412. * @param hdma DMA handle
  4413. * @retval None
  4414. */
  4415. static void CRYP_GCMCMAC_DMAError(DMA_HandleTypeDef *hdma)
  4416. {
  4417. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  4418. hcryp->State= HAL_CRYP_STATE_ERROR;
  4419. hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR;
  4420. HAL_CRYP_ErrorCallback(hcryp);
  4421. /* Clear Error Flag */
  4422. __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR);
  4423. }
  4424. /**
  4425. * @brief Handle CRYP block input/output data handling under interruption
  4426. * for GCM, GMAC or CMAC chaining modes.
  4427. * @note The function is called under interruption only, once
  4428. * interruptions have been enabled by HAL_CRYPEx_AES_Auth_IT().
  4429. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4430. * the configuration information for CRYP module
  4431. * @retval HAL status
  4432. */
  4433. HAL_StatusTypeDef CRYP_AES_Auth_IT(CRYP_HandleTypeDef *hcryp)
  4434. {
  4435. uint32_t inputaddr = 0x0U;
  4436. uint32_t outputaddr = 0x0U;
  4437. uint32_t index = 0x0U;
  4438. uint32_t addhoc_process = 0U;
  4439. uint32_t difflength = 0U;
  4440. uint32_t difflengthmod4 = 0U;
  4441. uint32_t mask[3] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU};
  4442. uint32_t intermediate_data[4U] = {0U};
  4443. if(hcryp->State == HAL_CRYP_STATE_BUSY)
  4444. {
  4445. /*===========================*/
  4446. /* GCM/GMAC(/CCM) init phase */
  4447. /*===========================*/
  4448. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  4449. {
  4450. /* Clear Computation Complete Flag */
  4451. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4452. /* Disable Computation Complete Flag and Errors Interrupts */
  4453. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4454. /* Change the CRYP state */
  4455. hcryp->State = HAL_CRYP_STATE_READY;
  4456. /* Mark that the initialization phase is over */
  4457. hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
  4458. /* Process Unlocked */
  4459. __HAL_UNLOCK(hcryp);
  4460. /* Call computation complete callback */
  4461. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4462. return HAL_OK;
  4463. }
  4464. /*=====================================*/
  4465. /* GCM/GMAC or (CCM/)CMAC header phase */
  4466. /*=====================================*/
  4467. else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  4468. {
  4469. /* Check if all input header data have been entered */
  4470. if (hcryp->CrypInCount == 0U)
  4471. {
  4472. /* Clear Computation Complete Flag */
  4473. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4474. /* Disable Computation Complete Flag and Errors Interrupts */
  4475. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4476. /* Change the CRYP state */
  4477. hcryp->State = HAL_CRYP_STATE_READY;
  4478. /* Mark that the header phase is over */
  4479. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  4480. /* Process Unlocked */
  4481. __HAL_UNLOCK(hcryp);
  4482. /* Call computation complete callback */
  4483. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4484. return HAL_OK;
  4485. }
  4486. /* If suspension flag has been raised, suspend processing */
  4487. else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  4488. {
  4489. /* Clear CCF Flag */
  4490. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4491. /* reset SuspendRequest */
  4492. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  4493. /* Disable Computation Complete Flag and Errors Interrupts */
  4494. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4495. /* Change the CRYP state */
  4496. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  4497. /* Mark that the header phase is over */
  4498. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  4499. /* Process Unlocked */
  4500. __HAL_UNLOCK(hcryp);
  4501. return HAL_OK;
  4502. }
  4503. else /* Carry on feeding input data to the CRYP hardware block */
  4504. {
  4505. /* Clear Computation Complete Flag */
  4506. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4507. /* Get the last Input data address */
  4508. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  4509. /* Increment/decrement instance pointer/counter */
  4510. if (hcryp->CrypInCount < 16U)
  4511. {
  4512. difflength = hcryp->CrypInCount;
  4513. hcryp->CrypInCount = 0U;
  4514. addhoc_process = 1U;
  4515. difflengthmod4 = difflength%4U;
  4516. }
  4517. else
  4518. {
  4519. hcryp->pCrypInBuffPtr += 16U;
  4520. hcryp->CrypInCount -= 16U;
  4521. }
  4522. #if defined(AES_CR_NPBLB)
  4523. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  4524. #else
  4525. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  4526. #endif
  4527. {
  4528. if (hcryp->CrypInCount == hcryp->Init.HeaderSize)
  4529. {
  4530. /* All B blocks will have been entered after the next
  4531. four DINR writing, so point at header buffer for
  4532. the next iteration */
  4533. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  4534. }
  4535. }
  4536. /* Write the Input block in the Data Input register */
  4537. if (addhoc_process == 0U)
  4538. {
  4539. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4540. inputaddr+=4U;
  4541. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4542. inputaddr+=4U;
  4543. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4544. inputaddr+=4U;
  4545. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4546. }
  4547. else
  4548. {
  4549. /* Header remainder has size less than 128 bits */
  4550. /* Enter complete words when possible */
  4551. for(index=0U; index < (difflength/4U); index ++)
  4552. {
  4553. /* Write the Input block in the Data Input register */
  4554. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4555. inputaddr+=4U;
  4556. }
  4557. /* Enter incomplete word padded with zeroes if applicable
  4558. (case of header length not a multiple of 32-bits) */
  4559. if (difflengthmod4 != 0U)
  4560. {
  4561. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1]);
  4562. }
  4563. /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
  4564. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  4565. {
  4566. hcryp->Instance->DINR = 0U;
  4567. }
  4568. }
  4569. return HAL_OK;
  4570. }
  4571. }
  4572. /*=======================*/
  4573. /* GCM/CCM payload phase */
  4574. /*=======================*/
  4575. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  4576. {
  4577. /* Get the last output data address */
  4578. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  4579. /* Specific handling to manage payload size less than 128 bits
  4580. when GCM (or CCM when applicable) encryption or decryption is selected.
  4581. Check here if the last block output data are read */
  4582. #if defined(AES_CR_NPBLB)
  4583. if ((hcryp->CrypOutCount < 16U) && \
  4584. (hcryp->CrypOutCount > 0U))
  4585. #else
  4586. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) && \
  4587. (hcryp->CrypOutCount < 16U) && \
  4588. (hcryp->CrypOutCount > 0U))
  4589. #endif
  4590. {
  4591. addhoc_process = 1U;
  4592. difflength = hcryp->CrypOutCount;
  4593. difflengthmod4 = difflength%4U;
  4594. hcryp->CrypOutCount = 0U; /* mark that no more output data will be needed */
  4595. /* Retrieve intermediate data */
  4596. for(index=0U; index < 4U; index ++)
  4597. {
  4598. intermediate_data[index] = hcryp->Instance->DOUTR;
  4599. }
  4600. /* Retrieve last words of cyphered data */
  4601. /* First, retrieve complete output words */
  4602. for(index=0U; index < (difflength/4U); index ++)
  4603. {
  4604. *(uint32_t*)(outputaddr) = intermediate_data[index];
  4605. outputaddr+=4U;
  4606. }
  4607. /* Next, retrieve partial output word if applicable;
  4608. at the same time, start masking intermediate data
  4609. with a mask of zeros of same size than the padding
  4610. applied to the last block of payload */
  4611. if (difflengthmod4 != 0U)
  4612. {
  4613. intermediate_data[difflength/4U] &= mask[difflengthmod4-1U];
  4614. *(uint32_t*)(outputaddr) = intermediate_data[difflength/4U];
  4615. }
  4616. #if !defined(AES_CR_NPBLB)
  4617. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  4618. {
  4619. /* Change again CHMOD configuration to GCM mode */
  4620. __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_GCM_GMAC);
  4621. /* Select FINAL phase */
  4622. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_FINAL_PHASE);
  4623. /* Before inserting the intermediate data, carry on masking operation
  4624. with a mask of zeros of same size than the padding applied to the last block of payload */
  4625. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  4626. {
  4627. intermediate_data[(difflength+3U)/4U+index] = 0U;
  4628. }
  4629. /* Insert intermediate data to trigger an additional DOUTR reading round */
  4630. /* Clear Computation Complete Flag before entering new block */
  4631. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4632. for(index=0U; index < 4U; index ++)
  4633. {
  4634. hcryp->Instance->DINR = intermediate_data[index];
  4635. }
  4636. }
  4637. else
  4638. #endif
  4639. {
  4640. /* Payload phase is now over */
  4641. /* Clear Computation Complete Flag */
  4642. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4643. /* Disable Computation Complete Flag and Errors Interrupts */
  4644. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4645. /* Change the CRYP state */
  4646. hcryp->State = HAL_CRYP_STATE_READY;
  4647. /* Mark that the payload phase is over */
  4648. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  4649. /* Process Unlocked */
  4650. __HAL_UNLOCK(hcryp);
  4651. /* Call computation complete callback */
  4652. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4653. }
  4654. return HAL_OK;
  4655. }
  4656. else
  4657. {
  4658. if (hcryp->CrypOutCount != 0U)
  4659. {
  4660. /* Usual case (different than GCM/CCM last block < 128 bits ciphering) */
  4661. /* Retrieve the last block available from the CRYP hardware block:
  4662. read the output block from the Data Output Register */
  4663. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4664. outputaddr+=4U;
  4665. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4666. outputaddr+=4U;
  4667. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4668. outputaddr+=4U;
  4669. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4670. /* Increment/decrement instance pointer/counter */
  4671. hcryp->pCrypOutBuffPtr += 16U;
  4672. hcryp->CrypOutCount -= 16U;
  4673. }
  4674. #if !defined(AES_CR_NPBLB)
  4675. else
  4676. {
  4677. /* Software work-around: additional DOUTR reading round to discard the data */
  4678. for(index=0U; index < 4U; index ++)
  4679. {
  4680. intermediate_data[index] = hcryp->Instance->DOUTR;
  4681. }
  4682. }
  4683. #endif
  4684. }
  4685. /* Check if all output text has been retrieved */
  4686. if (hcryp->CrypOutCount == 0U)
  4687. {
  4688. #if !defined(AES_CR_NPBLB)
  4689. /* Make sure that software-work around is not running before disabling
  4690. the interruptions (indeed, if software work-around is running, the
  4691. interruptions must not be disabled to allow the additional DOUTR
  4692. reading round */
  4693. if (addhoc_process == 0U)
  4694. #endif
  4695. {
  4696. /* Clear Computation Complete Flag */
  4697. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4698. /* Disable Computation Complete Flag and Errors Interrupts */
  4699. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4700. /* Change the CRYP state */
  4701. hcryp->State = HAL_CRYP_STATE_READY;
  4702. /* Mark that the payload phase is over */
  4703. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  4704. /* Process Unlocked */
  4705. __HAL_UNLOCK(hcryp);
  4706. /* Call computation complete callback */
  4707. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4708. }
  4709. return HAL_OK;
  4710. }
  4711. /* If suspension flag has been raised, suspend processing */
  4712. else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  4713. {
  4714. /* Clear CCF Flag */
  4715. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4716. /* reset SuspendRequest */
  4717. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  4718. /* Disable Computation Complete Flag and Errors Interrupts */
  4719. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4720. /* Change the CRYP state */
  4721. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  4722. /* Mark that the header phase is over */
  4723. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  4724. /* Process Unlocked */
  4725. __HAL_UNLOCK(hcryp);
  4726. return HAL_OK;
  4727. }
  4728. else /* Output data are still expected, carry on feeding the CRYP
  4729. hardware block with input data */
  4730. {
  4731. /* Clear Computation Complete Flag */
  4732. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4733. /* Get the last Input data address */
  4734. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  4735. /* Usual input data feeding case */
  4736. if (hcryp->CrypInCount < 16U)
  4737. {
  4738. difflength = (uint32_t) (hcryp->CrypInCount);
  4739. difflengthmod4 = difflength%4U;
  4740. hcryp->CrypInCount = 0U;
  4741. #if defined(AES_CR_NPBLB)
  4742. /* In case of GCM encryption or CCM decryption, specify the number of padding
  4743. bytes in last block of payload */
  4744. if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC)
  4745. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT))
  4746. || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC)
  4747. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT)))
  4748. {
  4749. /* Set NPBLB field in writing the number of padding bytes
  4750. for the last block of payload */
  4751. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength);
  4752. }
  4753. #else
  4754. /* Software workaround applied to GCM encryption only */
  4755. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  4756. {
  4757. /* Change the mode configured in CHMOD bits of CR register to select CTR mode */
  4758. __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR);
  4759. }
  4760. #endif
  4761. /* Insert the last block (which size is inferior to 128 bits) padded with zeroes
  4762. to have a complete block of 128 bits */
  4763. for(index=0U; index < (difflength/4U); index ++)
  4764. {
  4765. /* Write the Input block in the Data Input register */
  4766. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4767. inputaddr+=4U;
  4768. }
  4769. /* If required, manage input data size not multiple of 32 bits */
  4770. if (difflengthmod4 != 0U)
  4771. {
  4772. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]);
  4773. }
  4774. /* Wrap-up in padding with zero-words if applicable */
  4775. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  4776. {
  4777. hcryp->Instance->DINR = 0U;
  4778. }
  4779. }
  4780. else
  4781. {
  4782. hcryp->pCrypInBuffPtr += 16U;
  4783. hcryp->CrypInCount -= 16U;
  4784. /* Write the Input block in the Data Input register */
  4785. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4786. inputaddr+=4U;
  4787. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4788. inputaddr+=4U;
  4789. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4790. inputaddr+=4U;
  4791. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4792. }
  4793. return HAL_OK;
  4794. }
  4795. }
  4796. /*====================================*/
  4797. /* GCM/GMAC or (CCM/)CMAC final phase */
  4798. /*====================================*/
  4799. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  4800. {
  4801. /* Clear Computation Complete Flag */
  4802. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4803. /* Get the last output data address */
  4804. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  4805. /* Retrieve the last expected data from the CRYP hardware block:
  4806. read the output block from the Data Output Register */
  4807. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4808. outputaddr+=4U;
  4809. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4810. outputaddr+=4U;
  4811. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4812. outputaddr+=4U;
  4813. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4814. /* Disable Computation Complete Flag and Errors Interrupts */
  4815. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4816. /* Change the CRYP state */
  4817. hcryp->State = HAL_CRYP_STATE_READY;
  4818. /* Mark that the header phase is over */
  4819. hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
  4820. /* Disable the Peripheral */
  4821. __HAL_CRYP_DISABLE();
  4822. /* Process Unlocked */
  4823. __HAL_UNLOCK(hcryp);
  4824. /* Call computation complete callback */
  4825. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4826. return HAL_OK;
  4827. }
  4828. else
  4829. {
  4830. /* Clear Computation Complete Flag */
  4831. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4832. hcryp->State = HAL_CRYP_STATE_ERROR;
  4833. __HAL_UNLOCK(hcryp);
  4834. return HAL_ERROR;
  4835. }
  4836. }
  4837. else
  4838. {
  4839. return HAL_BUSY;
  4840. }
  4841. }
  4842. /**
  4843. * @brief Set the DMA configuration and start the DMA transfer
  4844. * for GCM, GMAC or CMAC chainging modes.
  4845. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4846. * the configuration information for CRYP module.
  4847. * @param inputaddr Address of the Input buffer.
  4848. * @param Size Size of the Input buffer un bytes, must be a multiple of 16.
  4849. * @param outputaddr Address of the Output buffer, null pointer when no output DMA stream
  4850. * has to be configured.
  4851. * @retval None
  4852. */
  4853. static void CRYP_GCMCMAC_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  4854. {
  4855. /* Set the input CRYP DMA transfer complete callback */
  4856. hcryp->hdmain->XferCpltCallback = CRYP_GCMCMAC_DMAInCplt;
  4857. /* Set the DMA error callback */
  4858. hcryp->hdmain->XferErrorCallback = CRYP_GCMCMAC_DMAError;
  4859. if (outputaddr != 0U)
  4860. {
  4861. /* Set the output CRYP DMA transfer complete callback */
  4862. hcryp->hdmaout->XferCpltCallback = CRYP_GCMCMAC_DMAOutCplt;
  4863. /* Set the DMA error callback */
  4864. hcryp->hdmaout->XferErrorCallback = CRYP_GCMCMAC_DMAError;
  4865. }
  4866. /* Enable the CRYP peripheral */
  4867. __HAL_CRYP_ENABLE();
  4868. /* Enable the DMA input stream */
  4869. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4U);
  4870. /* Enable the DMA input request */
  4871. SET_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  4872. if (outputaddr != 0U)
  4873. {
  4874. /* Enable the DMA output stream */
  4875. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4U);
  4876. /* Enable the DMA output request */
  4877. SET_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  4878. }
  4879. }
  4880. /**
  4881. * @brief Write/read input/output data in polling mode.
  4882. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4883. * the configuration information for CRYP module.
  4884. * @param Input Pointer to the Input buffer.
  4885. * @param Ilength Length of the Input buffer in bytes, must be a multiple of 16.
  4886. * @param Output Pointer to the returned buffer.
  4887. * @param Timeout Specify Timeout value.
  4888. * @retval HAL status
  4889. */
  4890. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
  4891. {
  4892. uint32_t index = 0U;
  4893. uint32_t inputaddr = (uint32_t)Input;
  4894. uint32_t outputaddr = (uint32_t)Output;
  4895. for(index=0U; (index < Ilength); index += 16U)
  4896. {
  4897. /* Write the Input block in the Data Input register */
  4898. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4899. inputaddr+=4U;
  4900. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4901. inputaddr+=4U;
  4902. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4903. inputaddr+=4U;
  4904. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4905. inputaddr+=4U;
  4906. /* Wait for CCF flag to be raised */
  4907. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  4908. {
  4909. hcryp->State = HAL_CRYP_STATE_READY;
  4910. __HAL_UNLOCK(hcryp);
  4911. return HAL_TIMEOUT;
  4912. }
  4913. /* Clear CCF Flag */
  4914. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4915. /* Read the Output block from the Data Output Register */
  4916. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4917. outputaddr+=4U;
  4918. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4919. outputaddr+=4U;
  4920. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4921. outputaddr+=4U;
  4922. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4923. outputaddr+=4U;
  4924. /* If the suspension flag has been raised and if the processing is not about
  4925. to end, suspend processing */
  4926. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < Ilength))
  4927. {
  4928. /* Reset SuspendRequest */
  4929. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  4930. /* Save current reading and writing locations of Input and Output buffers */
  4931. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  4932. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  4933. /* Save the number of bytes that remain to be processed at this point */
  4934. hcryp->CrypInCount = Ilength - (index+16U);
  4935. /* Change the CRYP state */
  4936. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  4937. return HAL_OK;
  4938. }
  4939. }
  4940. /* Return function status */
  4941. return HAL_OK;
  4942. }
  4943. /**
  4944. * @brief Read derivative key in polling mode when CRYP hardware block is set
  4945. * in key derivation operating mode (mode 2).
  4946. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4947. * the configuration information for CRYP module.
  4948. * @param Output Pointer to the returned buffer.
  4949. * @param Timeout Specify Timeout value.
  4950. * @retval HAL status
  4951. */
  4952. static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout)
  4953. {
  4954. uint32_t outputaddr = (uint32_t)Output;
  4955. /* Wait for CCF flag to be raised */
  4956. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  4957. {
  4958. hcryp->State = HAL_CRYP_STATE_READY;
  4959. __HAL_UNLOCK(hcryp);
  4960. return HAL_TIMEOUT;
  4961. }
  4962. /* Clear CCF Flag */
  4963. __HAL_CRYP_CLEAR_FLAG( CRYP_CCF_CLEAR);
  4964. /* Read the derivative key from the AES_KEYRx registers */
  4965. if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
  4966. {
  4967. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
  4968. outputaddr+=4U;
  4969. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
  4970. outputaddr+=4U;
  4971. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
  4972. outputaddr+=4U;
  4973. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
  4974. outputaddr+=4U;
  4975. }
  4976. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
  4977. outputaddr+=4U;
  4978. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
  4979. outputaddr+=4U;
  4980. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
  4981. outputaddr+=4U;
  4982. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
  4983. /* Return function status */
  4984. return HAL_OK;
  4985. }
  4986. /**
  4987. * @brief Set the DMA configuration and start the DMA transfer.
  4988. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4989. * the configuration information for CRYP module.
  4990. * @param inputaddr Address of the Input buffer.
  4991. * @param Size Size of the Input buffer in bytes, must be a multiple of 16.
  4992. * @param outputaddr Address of the Output buffer.
  4993. * @retval None
  4994. */
  4995. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  4996. {
  4997. /* Set the CRYP DMA transfer complete callback */
  4998. hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
  4999. /* Set the DMA error callback */
  5000. hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
  5001. /* Set the CRYP DMA transfer complete callback */
  5002. hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
  5003. /* Set the DMA error callback */
  5004. hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
  5005. /* Enable the DMA input stream */
  5006. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4U);
  5007. /* Enable the DMA output stream */
  5008. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4U);
  5009. /* Enable In and Out DMA requests */
  5010. SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN));
  5011. /* Enable the CRYP peripheral */
  5012. __HAL_CRYP_ENABLE();
  5013. }
  5014. /**
  5015. * @brief Handle CRYP hardware block Timeout when waiting for CCF flag to be raised.
  5016. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  5017. * the configuration information for CRYP module.
  5018. * @param Timeout Timeout duration.
  5019. * @retval HAL status
  5020. */
  5021. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  5022. {
  5023. uint32_t tickstart = 0U;
  5024. /* Get timeout */
  5025. tickstart = HAL_GetTick();
  5026. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
  5027. {
  5028. /* Check for the Timeout */
  5029. if(Timeout != HAL_MAX_DELAY)
  5030. {
  5031. if((HAL_GetTick() - tickstart ) > Timeout)
  5032. {
  5033. return HAL_TIMEOUT;
  5034. }
  5035. }
  5036. }
  5037. return HAL_OK;
  5038. }
  5039. /**
  5040. * @brief Wait for Busy Flag to be reset during a GCM payload encryption process suspension.
  5041. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  5042. * the configuration information for CRYP module.
  5043. * @param Timeout Timeout duration.
  5044. * @retval HAL status
  5045. */
  5046. static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  5047. {
  5048. uint32_t tickstart = 0U;
  5049. /* Get timeout */
  5050. tickstart = HAL_GetTick();
  5051. while(HAL_IS_BIT_SET(hcryp->Instance->SR, AES_SR_BUSY))
  5052. {
  5053. /* Check for the Timeout */
  5054. if(Timeout != HAL_MAX_DELAY)
  5055. {
  5056. if((HAL_GetTick() - tickstart ) > Timeout)
  5057. {
  5058. return HAL_TIMEOUT;
  5059. }
  5060. }
  5061. }
  5062. return HAL_OK;
  5063. }
  5064. /**
  5065. * @brief DMA CRYP Input Data process complete callback.
  5066. * @param hdma DMA handle.
  5067. * @retval None
  5068. */
  5069. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
  5070. {
  5071. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  5072. /* Disable the DMA transfer for input request */
  5073. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  5074. /* Call input data transfer complete callback */
  5075. HAL_CRYP_InCpltCallback(hcryp);
  5076. }
  5077. /**
  5078. * @brief DMA CRYP Output Data process complete callback.
  5079. * @param hdma DMA handle.
  5080. * @retval None
  5081. */
  5082. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
  5083. {
  5084. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  5085. /* Disable the DMA transfer for output request */
  5086. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  5087. /* Clear CCF Flag */
  5088. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  5089. /* Disable CRYP */
  5090. __HAL_CRYP_DISABLE();
  5091. /* Change the CRYP state to ready */
  5092. hcryp->State = HAL_CRYP_STATE_READY;
  5093. /* Call output data transfer complete callback */
  5094. HAL_CRYP_OutCpltCallback(hcryp);
  5095. }
  5096. /**
  5097. * @brief DMA CRYP communication error callback.
  5098. * @param hdma DMA handle.
  5099. * @retval None
  5100. */
  5101. static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
  5102. {
  5103. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  5104. hcryp->State= HAL_CRYP_STATE_ERROR;
  5105. hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR;
  5106. HAL_CRYP_ErrorCallback(hcryp);
  5107. /* Clear Error Flag */
  5108. __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR);
  5109. }
  5110. /**
  5111. * @brief Last header or payload block padding when size is not a multiple of 128 bits.
  5112. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  5113. * the configuration information for CRYP module.
  5114. * @param difflength size remainder after having fed all complete 128-bit blocks.
  5115. * @param polling specifies whether or not polling on CCF must be done after having
  5116. * entered a complete block.
  5117. * @retval None
  5118. */
  5119. static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling)
  5120. {
  5121. uint32_t index = 0U;
  5122. uint32_t difflengthmod4 = difflength%4U;
  5123. uint32_t inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  5124. uint32_t outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  5125. uint32_t mask[3U] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU};
  5126. uint32_t intermediate_data[4U] = {0U};
  5127. #if defined(AES_CR_NPBLB)
  5128. /* In case of GCM encryption or CCM decryption, specify the number of padding
  5129. bytes in last block of payload */
  5130. if (READ_BIT(hcryp->Instance->CR,AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE)
  5131. {
  5132. if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC)
  5133. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT))
  5134. || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC)
  5135. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT)))
  5136. {
  5137. /* Set NPBLB field in writing the number of padding bytes
  5138. for the last block of payload */
  5139. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength);
  5140. }
  5141. }
  5142. #else
  5143. /* Software workaround applied to GCM encryption only */
  5144. if ((hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) &&
  5145. (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT))
  5146. {
  5147. /* Change the mode configured in CHMOD bits of CR register to select CTR mode */
  5148. __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR);
  5149. }
  5150. #endif
  5151. /* Wrap-up entering header or payload data */
  5152. /* Enter complete words when possible */
  5153. for(index=0U; index < (difflength/4U); index ++)
  5154. {
  5155. /* Write the Input block in the Data Input register */
  5156. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  5157. inputaddr+=4U;
  5158. }
  5159. /* Enter incomplete word padded with zeroes if applicable
  5160. (case of header length not a multiple of 32-bits) */
  5161. if (difflengthmod4 != 0U)
  5162. {
  5163. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1]);
  5164. }
  5165. /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
  5166. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  5167. {
  5168. hcryp->Instance->DINR = 0U;
  5169. }
  5170. if (polling == CRYP_POLLING_ON)
  5171. {
  5172. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  5173. {
  5174. hcryp->State = HAL_CRYP_STATE_READY;
  5175. __HAL_UNLOCK(hcryp);
  5176. HAL_CRYP_ErrorCallback(hcryp);
  5177. }
  5178. /* Clear CCF Flag */
  5179. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  5180. }
  5181. /* if payload */
  5182. if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE)
  5183. {
  5184. /* Retrieve intermediate data */
  5185. for(index=0U; index < 4U; index ++)
  5186. {
  5187. intermediate_data[index] = hcryp->Instance->DOUTR;
  5188. }
  5189. /* Retrieve last words of cyphered data */
  5190. /* First, retrieve complete output words */
  5191. for(index=0U; index < (difflength/4U); index ++)
  5192. {
  5193. *(uint32_t*)(outputaddr) = intermediate_data[index];
  5194. outputaddr+=4U;
  5195. }
  5196. /* Next, retrieve partial output word if applicable;
  5197. at the same time, start masking intermediate data
  5198. with a mask of zeros of same size than the padding
  5199. applied to the last block of payload */
  5200. if (difflengthmod4 != 0U)
  5201. {
  5202. intermediate_data[difflength/4U] &= mask[difflengthmod4-1U];
  5203. *(uint32_t*)(outputaddr) = intermediate_data[difflength/4U];
  5204. }
  5205. #if !defined(AES_CR_NPBLB)
  5206. /* Software workaround applied to GCM encryption only,
  5207. applicable for AES IP v2 version (where NPBLB is not defined) */
  5208. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  5209. {
  5210. /* Change again CHMOD configuration to GCM mode */
  5211. __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_GCM_GMAC);
  5212. /* Select FINAL phase */
  5213. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_FINAL_PHASE);
  5214. /* Before inserting the intermediate data, carry on masking operation
  5215. with a mask of zeros of same size than the padding applied to the last block of payload */
  5216. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  5217. {
  5218. intermediate_data[(difflength+3U)/4U+index] = 0U;
  5219. }
  5220. /* Insert intermediate data */
  5221. for(index=0U; index < 4U; index ++)
  5222. {
  5223. hcryp->Instance->DINR = intermediate_data[index];
  5224. }
  5225. /* Wait for completion, and read data on DOUT. This data is to discard. */
  5226. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  5227. {
  5228. hcryp->State = HAL_CRYP_STATE_READY;
  5229. __HAL_UNLOCK(hcryp);
  5230. HAL_CRYP_ErrorCallback(hcryp);
  5231. }
  5232. /* Read data to discard */
  5233. /* Clear CCF Flag */
  5234. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  5235. for(index=0U; index < 4U; index ++)
  5236. {
  5237. intermediate_data[index] = hcryp->Instance->DOUTR;
  5238. }
  5239. } /* if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) */
  5240. #endif /* !defined(AES_CR_NPBLB) */
  5241. } /* if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) */
  5242. }
  5243. /**
  5244. * @}
  5245. */
  5246. #endif /* AES */
  5247. #endif /* HAL_CRYP_MODULE_ENABLED */
  5248. /**
  5249. * @}
  5250. */
  5251. /**
  5252. * @}
  5253. */
  5254. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/