stm32f7xx_hal_cryp_ex.c 207 KB

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