stm32f4xx_hal_cryp.c 228 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_cryp.c
  4. * @author MCD Application Team
  5. * @brief CRYP HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Cryptography (CRYP) peripheral:
  8. * + Initialization, de-initialization, set config and get config functions
  9. * + DES/TDES, AES processing functions
  10. * + DMA callback functions
  11. * + CRYP IRQ handler management
  12. * + Peripheral State functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. The CRYP HAL driver can be used in CRYP or TinyAES IP as follows:
  20. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  21. (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()or __HAL_RCC_AES_CLK_ENABLE for TinyAES IP
  22. (##) In case of using interrupts (e.g. HAL_CRYP_Encrypt_IT())
  23. (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  24. (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
  25. (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
  26. (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_Encrypt_DMA())
  27. (+++) Enable the DMAx interface clock using __RCC_DMAx_CLK_ENABLE()
  28. (+++) Configure and enable two DMA streams one for managing data transfer from
  29. memory to peripheral (input stream) and another stream for managing data
  30. transfer from peripheral to memory (output stream)
  31. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  32. using __HAL_LINKDMA()
  33. (+++) Configure the priority and enable the NVIC for the transfer complete
  34. interrupt on the two DMA Streams. The output stream should have higher
  35. priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
  36. (#)Initialize the CRYP according to the specified parameters :
  37. (##) The data type: 1-bit, 8-bit, 16-bit or 32-bit.
  38. (##) The key size: 128, 192 or 256.
  39. (##) The AlgoMode DES/ TDES Algorithm ECB/CBC or AES Algorithm ECB/CBC/CTR/GCM or CCM.
  40. (##) The initialization vector (counter). It is not used in ECB mode.
  41. (##) The key buffer used for encryption/decryption.
  42. (##) The Header used only in AES GCM and CCM Algorithm for authentication.
  43. (##) The HeaderSize The size of header buffer in word.
  44. (##) The B0 block is the first authentication block used only in AES CCM mode.
  45. (#)Three processing (encryption/decryption) functions are available:
  46. (##) Polling mode: encryption and decryption APIs are blocking functions
  47. i.e. they process the data and wait till the processing is finished,
  48. e.g. HAL_CRYP_Encrypt & HAL_CRYP_Decrypt
  49. (##) Interrupt mode: encryption and decryption APIs are not blocking functions
  50. i.e. they process the data under interrupt,
  51. e.g. HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT
  52. (##) DMA mode: encryption and decryption APIs are not blocking functions
  53. i.e. the data transfer is ensured by DMA,
  54. e.g. HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA
  55. (#)When the processing function is called at first time after HAL_CRYP_Init()
  56. the CRYP peripheral is configured and processes the buffer in input.
  57. At second call, no need to Initialize the CRYP, user have to get current configuration via
  58. HAL_CRYP_GetConfig() API, then only HAL_CRYP_SetConfig() is requested to set
  59. new parametres, finally user can start encryption/decryption.
  60. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  61. (#)To process a single message with consecutive calls to HAL_CRYP_Encrypt() or HAL_CRYP_Decrypt()
  62. without having to configure again the Key or the Initialization Vector between each API call,
  63. the field KeyIVConfigSkip of the initialization structure must be set to CRYP_KEYIVCONFIG_ONCE.
  64. Same is true for consecutive calls of HAL_CRYP_Encrypt_IT(), HAL_CRYP_Decrypt_IT(), HAL_CRYP_Encrypt_DMA()
  65. or HAL_CRYP_Decrypt_DMA().
  66. [..]
  67. The cryptographic processor supports following standards:
  68. (#) The data encryption standard (DES) and Triple-DES (TDES) supported only by CRYP1 IP:
  69. (##)64-bit data block processing
  70. (##) chaining modes supported :
  71. (+++) Electronic Code Book(ECB)
  72. (+++) Cipher Block Chaining (CBC)
  73. (##) keys length supported :64-bit, 128-bit and 192-bit.
  74. (#) The advanced encryption standard (AES) supported by CRYP1 & TinyAES IP:
  75. (##)128-bit data block processing
  76. (##) chaining modes supported :
  77. (+++) Electronic Code Book(ECB)
  78. (+++) Cipher Block Chaining (CBC)
  79. (+++) Counter mode (CTR)
  80. (+++) Galois/counter mode (GCM/GMAC)
  81. (+++) Counter with Cipher Block Chaining-Message(CCM)
  82. (##) keys length Supported :
  83. (+++) for CRYP1 IP: 128-bit, 192-bit and 256-bit.
  84. (+++) for TinyAES IP: 128-bit and 256-bit
  85. [..] This section describes the AES Galois/counter mode (GCM) supported by both CRYP1 IP:
  86. (#) Algorithm supported :
  87. (##) Galois/counter mode (GCM)
  88. (##) Galois message authentication code (GMAC) :is exactly the same as
  89. GCM algorithm composed only by an header.
  90. (#) Four phases are performed in GCM :
  91. (##) Init phase: IP prepares the GCM hash subkey (H) and do the IV processing
  92. (##) Header phase: IP processes the Additional Authenticated Data (AAD), with hash
  93. computation only.
  94. (##) Payload phase: IP processes the plaintext (P) with hash computation + keystream
  95. encryption + data XORing. It works in a similar way for ciphertext (C).
  96. (##) Final phase: IP generates the authenticated tag (T) using the last block of data.
  97. (#) structure of message construction in GCM is defined as below :
  98. (##) 16 bytes Initial Counter Block (ICB)composed of IV and counter
  99. (##) The authenticated header A (also knows as Additional Authentication Data AAD)
  100. this part of the message is only authenticated, not encrypted.
  101. (##) The plaintext message P is both authenticated and encrypted as ciphertext.
  102. GCM standard specifies that ciphertext has same bit length as the plaintext.
  103. (##) The last block is composed of the length of A (on 64 bits) and the length of ciphertext
  104. (on 64 bits)
  105. [..] This section describe The AES Counter with Cipher Block Chaining-Message
  106. Authentication Code (CCM) supported by both CRYP1 IP:
  107. (#) Specific parameters for CCM :
  108. (##) B0 block : According to NIST Special Publication 800-38C,
  109. The first block B0 is formatted as follows, where l(m) is encoded in
  110. most-significant-byte first order(see below table 3)
  111. (+++) Q: a bit string representation of the octet length of P (plaintext)
  112. (+++) q The octet length of the binary representation of the octet length of the payload
  113. (+++) A nonce (N), n The octet length of the where n+q=15.
  114. (+++) Flags: most significant octet containing four flags for control information,
  115. (+++) t The octet length of the MAC.
  116. (##) B1 block (header) : associated data length(a) concatenated with Associated Data (A)
  117. the associated data length expressed in bytes (a) defined as below:
  118. (+++) If 0 < a < 216-28, then it is encoded as [a]16, i.e. two octets
  119. (+++) If 216-28 < a < 232, then it is encoded as 0xff || 0xfe || [a]32, i.e. six octets
  120. (+++) If 232 < a < 264, then it is encoded as 0xff || 0xff || [a]64, i.e. ten octets
  121. (##) CTRx block : control blocks
  122. (+++) Generation of CTR1 from first block B0 information :
  123. equal to B0 with first 5 bits zeroed and most significant bits storing octet
  124. length of P also zeroed, then incremented by one ( see below Table 4)
  125. (+++) Generation of CTR0: same as CTR1 with bit[0] set to zero.
  126. (#) Four phases are performed in CCM for CRYP1 IP:
  127. (##) Init phase: IP prepares the GCM hash subkey (H) and do the IV processing
  128. (##) Header phase: IP processes the Additional Authenticated Data (AAD), with hash
  129. computation only.
  130. (##) Payload phase: IP processes the plaintext (P) with hash computation + keystream
  131. encryption + data XORing. It works in a similar way for ciphertext (C).
  132. (##) Final phase: IP generates the authenticated tag (T) using the last block of data.
  133. *** Callback registration ***
  134. =============================================
  135. The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS when set to 1
  136. allows the user to configure dynamically the driver callbacks.
  137. Use Functions @ref HAL_CRYP_RegisterCallback() or HAL_CRYP_RegisterXXXCallback()
  138. to register an interrupt callback.
  139. Function @ref HAL_CRYP_RegisterCallback() allows to register following callbacks:
  140. (+) InCpltCallback : Input FIFO transfer completed callback.
  141. (+) OutCpltCallback : Output FIFO transfer completed callback.
  142. (+) ErrorCallback : callback for error detection.
  143. (+) MspInitCallback : CRYP MspInit.
  144. (+) MspDeInitCallback : CRYP MspDeInit.
  145. This function takes as parameters the HAL peripheral handle, the Callback ID
  146. and a pointer to the user callback function.
  147. Use function @ref HAL_CRYP_UnRegisterCallback() to reset a callback to the default
  148. weak function.
  149. @ref HAL_CRYP_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  150. and the Callback ID.
  151. This function allows to reset following callbacks:
  152. (+) InCpltCallback : Input FIFO transfer completed callback.
  153. (+) OutCpltCallback : Output FIFO transfer completed callback.
  154. (+) ErrorCallback : callback for error detection.
  155. (+) MspInitCallback : CRYP MspInit.
  156. (+) MspDeInitCallback : CRYP MspDeInit.
  157. By default, after the @ref HAL_CRYP_Init() and when the state is HAL_CRYP_STATE_RESET
  158. all callbacks are set to the corresponding weak functions :
  159. examples @ref HAL_CRYP_InCpltCallback() , @ref HAL_CRYP_OutCpltCallback().
  160. Exception done for MspInit and MspDeInit functions that are
  161. reset to the legacy weak function in the @ref HAL_CRYP_Init()/ @ref HAL_CRYP_DeInit() only when
  162. these callbacks are null (not registered beforehand).
  163. if not, MspInit or MspDeInit are not null, the @ref HAL_CRYP_Init() / @ref HAL_CRYP_DeInit()
  164. keep and use the user MspInit/MspDeInit functions (registered beforehand)
  165. Callbacks can be registered/unregistered in HAL_CRYP_STATE_READY state only.
  166. Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
  167. in HAL_CRYP_STATE_READY or HAL_CRYP_STATE_RESET state,
  168. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  169. In that case first register the MspInit/MspDeInit user callbacks
  170. using @ref HAL_CRYP_RegisterCallback() before calling @ref HAL_CRYP_DeInit()
  171. or @ref HAL_CRYP_Init() function.
  172. When The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS is set to 0 or
  173. not defined, the callback registration feature is not available and all callbacks
  174. are set to the corresponding weak functions.
  175. Table 1. Initial Counter Block (ICB)
  176. +-------------------------------------------------------+
  177. | Initialization vector (IV) | Counter |
  178. |----------------|----------------|-----------|---------|
  179. 127 95 63 31 0
  180. Bit Number Register Contents
  181. ---------- --------------- -----------
  182. 127 ...96 CRYP_IV1R[31:0] ICB[127:96]
  183. 95 ...64 CRYP_IV1L[31:0] B0[95:64]
  184. 63 ... 32 CRYP_IV0R[31:0] ICB[63:32]
  185. 31 ... 0 CRYP_IV0L[31:0] ICB[31:0], where 32-bit counter= 0x2
  186. Table 2. GCM last block definition
  187. +-------------------------------------------------------------------+
  188. | Bit[0] | Bit[32] | Bit[64] | Bit[96] |
  189. |-----------|--------------------|-----------|----------------------|
  190. | 0x0 | Header length[31:0]| 0x0 | Payload length[31:0] |
  191. |-----------|--------------------|-----------|----------------------|
  192. Table 3. B0 block
  193. Octet Number Contents
  194. ------------ ---------
  195. 0 Flags
  196. 1 ... 15-q Nonce N
  197. 16-q ... 15 Q
  198. the Flags field is formatted as follows:
  199. Bit Number Contents
  200. ---------- ----------------------
  201. 7 Reserved (always zero)
  202. 6 Adata
  203. 5 ... 3 (t-2)/2
  204. 2 ... 0 [q-1]3
  205. Table 4. CTRx block
  206. Bit Number Register Contents
  207. ---------- --------------- -----------
  208. 127 ...96 CRYP_IV1R[31:0] B0[127:96], where Q length bits are set to 0, except for
  209. bit 0 that is set to 1
  210. 95 ...64 CRYP_IV1L[31:0] B0[95:64]
  211. 63 ... 32 CRYP_IV0R[31:0] B0[63:32]
  212. 31 ... 0 CRYP_IV0L[31:0] B0[31:0], where flag bits set to 0
  213. @endverbatim
  214. ******************************************************************************
  215. * @attention
  216. *
  217. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  218. * All rights reserved.</center></h2>
  219. *
  220. * This software component is licensed by ST under BSD 3-Clause license,
  221. * the "License"; You may not use this file except in compliance with the
  222. * License. You may obtain a copy of the License at:
  223. * opensource.org/licenses/BSD-3-Clause
  224. *
  225. ******************************************************************************
  226. */
  227. /* Includes ------------------------------------------------------------------*/
  228. #include "stm32f4xx_hal.h"
  229. /** @addtogroup STM32F4xx_HAL_Driver
  230. * @{
  231. */
  232. #if defined (AES) || defined (CRYP)
  233. /** @defgroup CRYP CRYP
  234. * @brief CRYP HAL module driver.
  235. * @{
  236. */
  237. #ifdef HAL_CRYP_MODULE_ENABLED
  238. /* Private typedef -----------------------------------------------------------*/
  239. /* Private define ------------------------------------------------------------*/
  240. /** @addtogroup CRYP_Private_Defines
  241. * @{
  242. */
  243. #define CRYP_TIMEOUT_KEYPREPARATION 82U /*The latency of key preparation operation is 82 clock cycles.*/
  244. #define CRYP_TIMEOUT_GCMCCMINITPHASE 299U /* The latency of GCM/CCM init phase to prepare hash subkey is 299 clock cycles.*/
  245. #define CRYP_TIMEOUT_GCMCCMHEADERPHASE 290U /* The latency of GCM/CCM header phase is 290 clock cycles.*/
  246. #define CRYP_PHASE_READY 0x00000001U /*!< CRYP peripheral is ready for initialization. */
  247. #define CRYP_PHASE_PROCESS 0x00000002U /*!< CRYP peripheral is in processing phase */
  248. #if defined(AES)
  249. #define CRYP_OPERATINGMODE_ENCRYPT 0x00000000U /*!< Encryption mode(Mode 1) */
  250. #define CRYP_OPERATINGMODE_KEYDERIVATION AES_CR_MODE_0 /*!< Key derivation mode only used when performing ECB and CBC decryptions (Mode 2) */
  251. #define CRYP_OPERATINGMODE_DECRYPT AES_CR_MODE_1 /*!< Decryption (Mode 3) */
  252. #define CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT AES_CR_MODE /*!< Key derivation and decryption only used when performing ECB and CBC decryptions (Mode 4) */
  253. #define CRYP_PHASE_INIT 0x00000000U /*!< GCM/GMAC (or CCM) init phase */
  254. #define CRYP_PHASE_HEADER AES_CR_GCMPH_0 /*!< GCM/GMAC or CCM header phase */
  255. #define CRYP_PHASE_PAYLOAD AES_CR_GCMPH_1 /*!< GCM(/CCM) payload phase */
  256. #define CRYP_PHASE_FINAL AES_CR_GCMPH /*!< GCM/GMAC or CCM final phase */
  257. #else /* CRYP */
  258. #define CRYP_PHASE_INIT 0x00000000U /*!< GCM/GMAC (or CCM) init phase */
  259. #define CRYP_PHASE_HEADER CRYP_CR_GCM_CCMPH_0 /*!< GCM/GMAC or CCM header phase */
  260. #define CRYP_PHASE_PAYLOAD CRYP_CR_GCM_CCMPH_1 /*!< GCM(/CCM) payload phase */
  261. #define CRYP_PHASE_FINAL CRYP_CR_GCM_CCMPH /*!< GCM/GMAC or CCM final phase */
  262. #define CRYP_OPERATINGMODE_ENCRYPT 0x00000000U /*!< Encryption mode */
  263. #define CRYP_OPERATINGMODE_DECRYPT CRYP_CR_ALGODIR /*!< Decryption */
  264. #endif /* End CRYP or AES */
  265. /* CTR1 information to use in CCM algorithm */
  266. #define CRYP_CCM_CTR1_0 0x07FFFFFFU
  267. #define CRYP_CCM_CTR1_1 0xFFFFFF00U
  268. #define CRYP_CCM_CTR1_2 0x00000001U
  269. /**
  270. * @}
  271. */
  272. /* Private macro -------------------------------------------------------------*/
  273. /** @addtogroup CRYP_Private_Macros
  274. * @{
  275. */
  276. #if defined(CRYP)
  277. #define CRYP_SET_PHASE(__HANDLE__, __PHASE__) do{(__HANDLE__)->Instance->CR &= (uint32_t)(~CRYP_CR_GCM_CCMPH);\
  278. (__HANDLE__)->Instance->CR |= (uint32_t)(__PHASE__);\
  279. }while(0)
  280. #define HAL_CRYP_FIFO_FLUSH(__HANDLE__) ((__HANDLE__)->Instance->CR |= CRYP_CR_FFLUSH)
  281. #else /*AES*/
  282. #define CRYP_SET_PHASE(__HANDLE__, __PHASE__) do{(__HANDLE__)->Instance->CR &= (uint32_t)(~AES_CR_GCMPH);\
  283. (__HANDLE__)->Instance->CR |= (uint32_t)(__PHASE__);\
  284. }while(0)
  285. #endif /* End AES or CRYP*/
  286. /**
  287. * @}
  288. */
  289. /* Private struct -------------------------------------------------------------*/
  290. /* Private variables ---------------------------------------------------------*/
  291. /* Private function prototypes -----------------------------------------------*/
  292. /** @addtogroup CRYP_Private_Functions_prototypes
  293. * @{
  294. */
  295. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  296. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
  297. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
  298. static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
  299. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint32_t KeySize);
  300. static void CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
  301. #if defined (CRYP_CR_ALGOMODE_AES_GCM)|| defined (AES)
  302. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  303. static void CRYP_GCMCCM_SetPayloadPhase_IT(CRYP_HandleTypeDef *hcryp);
  304. static void CRYP_GCMCCM_SetHeaderPhase_IT(CRYP_HandleTypeDef *hcryp);
  305. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_DMA(CRYP_HandleTypeDef *hcryp);
  306. static void CRYP_Workaround(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  307. static HAL_StatusTypeDef CRYP_AESGCM_Process_DMA(CRYP_HandleTypeDef *hcryp);
  308. static HAL_StatusTypeDef CRYP_AESGCM_Process_IT(CRYP_HandleTypeDef *hcryp);
  309. static HAL_StatusTypeDef CRYP_AESGCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  310. static HAL_StatusTypeDef CRYP_AESCCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  311. static HAL_StatusTypeDef CRYP_AESCCM_Process_IT(CRYP_HandleTypeDef *hcryp);
  312. static HAL_StatusTypeDef CRYP_AESCCM_Process_DMA(CRYP_HandleTypeDef *hcryp);
  313. #endif /* AES or GCM CCM defined*/
  314. static void CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcrypt, uint32_t Timeout);
  315. static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  316. static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  317. static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp);
  318. static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp);
  319. static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp);
  320. #if defined (CRYP)
  321. static void CRYP_TDES_IT(CRYP_HandleTypeDef *hcryp);
  322. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  323. static HAL_StatusTypeDef CRYP_WaitOnIFEMFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  324. #endif /* GCM CCM defined*/
  325. static HAL_StatusTypeDef CRYP_WaitOnBUSYFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  326. static HAL_StatusTypeDef CRYP_WaitOnOFNEFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  327. static HAL_StatusTypeDef CRYP_TDES_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  328. #else /*AES*/
  329. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  330. #endif /* End CRYP or AES */
  331. /**
  332. * @}
  333. */
  334. /* Exported functions ---------------------------------------------------------*/
  335. /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
  336. * @{
  337. */
  338. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
  339. * @brief Initialization and Configuration functions.
  340. *
  341. @verbatim
  342. ========================================================================================
  343. ##### Initialization, de-initialization and Set and Get configuration functions #####
  344. ========================================================================================
  345. [..] This section provides functions allowing to:
  346. (+) Initialize the CRYP
  347. (+) DeInitialize the CRYP
  348. (+) Initialize the CRYP MSP
  349. (+) DeInitialize the CRYP MSP
  350. (+) configure CRYP (HAL_CRYP_SetConfig) with the specified parameters in the CRYP_ConfigTypeDef
  351. Parameters which are configured in This section are :
  352. (+) Key size
  353. (+) Data Type : 32,16, 8 or 1bit
  354. (+) AlgoMode :
  355. - for CRYP1 IP :
  356. ECB and CBC in DES/TDES Standard
  357. ECB,CBC,CTR,GCM/GMAC and CCM in AES Standard.
  358. - for TinyAES2 IP, only ECB,CBC,CTR,GCM/GMAC and CCM in AES Standard are supported.
  359. (+) Get CRYP configuration (HAL_CRYP_GetConfig) from the specified parameters in the CRYP_HandleTypeDef
  360. @endverbatim
  361. * @{
  362. */
  363. /**
  364. * @brief Initializes the CRYP according to the specified
  365. * parameters in the CRYP_ConfigTypeDef and creates the associated handle.
  366. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  367. * the configuration information for CRYP module
  368. * @retval HAL status
  369. */
  370. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  371. {
  372. /* Check the CRYP handle allocation */
  373. if (hcryp == NULL)
  374. {
  375. return HAL_ERROR;
  376. }
  377. /* Check parameters */
  378. assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
  379. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  380. assert_param(IS_CRYP_ALGORITHM(hcryp->Init.Algorithm));
  381. assert_param(IS_CRYP_INIT(hcryp->Init.KeyIVConfigSkip));
  382. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  383. if (hcryp->State == HAL_CRYP_STATE_RESET)
  384. {
  385. /* Allocate lock resource and initialize it */
  386. hcryp->Lock = HAL_UNLOCKED;
  387. hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak InCpltCallback */
  388. hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak OutCpltCallback */
  389. hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak ErrorCallback */
  390. if (hcryp->MspInitCallback == NULL)
  391. {
  392. hcryp->MspInitCallback = HAL_CRYP_MspInit; /* Legacy weak MspInit */
  393. }
  394. /* Init the low level hardware */
  395. hcryp->MspInitCallback(hcryp);
  396. }
  397. #else
  398. if (hcryp->State == HAL_CRYP_STATE_RESET)
  399. {
  400. /* Allocate lock resource and initialize it */
  401. hcryp->Lock = HAL_UNLOCKED;
  402. /* Init the low level hardware */
  403. HAL_CRYP_MspInit(hcryp);
  404. }
  405. #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
  406. /* Set the key size(This bit field is don’t care in the DES or TDES modes) data type and Algorithm */
  407. #if defined (CRYP)
  408. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_DATATYPE | CRYP_CR_KEYSIZE | CRYP_CR_ALGOMODE,
  409. hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  410. #else /*AES*/
  411. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE | AES_CR_KEYSIZE | AES_CR_CHMOD,
  412. hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  413. #endif /* End AES or CRYP*/
  414. /* Reset Error Code field */
  415. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  416. /* Change the CRYP state */
  417. hcryp->State = HAL_CRYP_STATE_READY;
  418. /* Set the default CRYP phase */
  419. hcryp->Phase = CRYP_PHASE_READY;
  420. /* Return function status */
  421. return HAL_OK;
  422. }
  423. /**
  424. * @brief De-Initializes the CRYP peripheral.
  425. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  426. * the configuration information for CRYP module
  427. * @retval HAL status
  428. */
  429. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  430. {
  431. /* Check the CRYP handle allocation */
  432. if (hcryp == NULL)
  433. {
  434. return HAL_ERROR;
  435. }
  436. /* Set the default CRYP phase */
  437. hcryp->Phase = CRYP_PHASE_READY;
  438. /* Reset CrypInCount and CrypOutCount */
  439. hcryp->CrypInCount = 0;
  440. hcryp->CrypOutCount = 0;
  441. hcryp->CrypHeaderCount = 0;
  442. /* Disable the CRYP peripheral clock */
  443. __HAL_CRYP_DISABLE(hcryp);
  444. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  445. if (hcryp->MspDeInitCallback == NULL)
  446. {
  447. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit; /* Legacy weak MspDeInit */
  448. }
  449. /* DeInit the low level hardware */
  450. hcryp->MspDeInitCallback(hcryp);
  451. #else
  452. /* DeInit the low level hardware: CLOCK, NVIC.*/
  453. HAL_CRYP_MspDeInit(hcryp);
  454. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  455. /* Change the CRYP state */
  456. hcryp->State = HAL_CRYP_STATE_RESET;
  457. /* Release Lock */
  458. __HAL_UNLOCK(hcryp);
  459. /* Return function status */
  460. return HAL_OK;
  461. }
  462. /**
  463. * @brief Configure the CRYP according to the specified
  464. * parameters in the CRYP_ConfigTypeDef
  465. * @param hcryp: pointer to a CRYP_HandleTypeDef structure
  466. * @param pConf: pointer to a CRYP_ConfigTypeDef structure that contains
  467. * the configuration information for CRYP module
  468. * @retval HAL status
  469. */
  470. HAL_StatusTypeDef HAL_CRYP_SetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf)
  471. {
  472. /* Check the CRYP handle allocation */
  473. if ((hcryp == NULL) || (pConf == NULL))
  474. {
  475. return HAL_ERROR;
  476. }
  477. /* Check parameters */
  478. assert_param(IS_CRYP_KEYSIZE(pConf->KeySize));
  479. assert_param(IS_CRYP_DATATYPE(pConf->DataType));
  480. assert_param(IS_CRYP_ALGORITHM(pConf->Algorithm));
  481. if (hcryp->State == HAL_CRYP_STATE_READY)
  482. {
  483. /* Change the CRYP state */
  484. hcryp->State = HAL_CRYP_STATE_BUSY;
  485. /* Process locked */
  486. __HAL_LOCK(hcryp);
  487. /* Set CRYP parameters */
  488. hcryp->Init.DataType = pConf->DataType;
  489. hcryp->Init.pKey = pConf->pKey;
  490. hcryp->Init.Algorithm = pConf->Algorithm;
  491. hcryp->Init.KeySize = pConf->KeySize;
  492. hcryp->Init.pInitVect = pConf->pInitVect;
  493. hcryp->Init.Header = pConf->Header;
  494. hcryp->Init.HeaderSize = pConf->HeaderSize;
  495. hcryp->Init.B0 = pConf->B0;
  496. hcryp->Init.DataWidthUnit = pConf->DataWidthUnit;
  497. hcryp->Init.KeyIVConfigSkip = pConf->KeyIVConfigSkip;
  498. hcryp->Init.HeaderWidthUnit = pConf->HeaderWidthUnit;
  499. /* Set the key size(This bit field is don’t care in the DES or TDES modes) data type, AlgoMode and operating mode*/
  500. #if defined (CRYP)
  501. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_DATATYPE | CRYP_CR_KEYSIZE | CRYP_CR_ALGOMODE,
  502. hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  503. #else /*AES*/
  504. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE | AES_CR_KEYSIZE | AES_CR_CHMOD,
  505. hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  506. /*clear error flags*/
  507. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR);
  508. #endif /* End AES or CRYP */
  509. /* Process Unlocked */
  510. __HAL_UNLOCK(hcryp);
  511. /* Reset Error Code field */
  512. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  513. /* Change the CRYP state */
  514. hcryp->State = HAL_CRYP_STATE_READY;
  515. /* Set the default CRYP phase */
  516. hcryp->Phase = CRYP_PHASE_READY;
  517. /* Return function status */
  518. return HAL_OK;
  519. }
  520. else
  521. {
  522. /* Process Unlocked */
  523. __HAL_UNLOCK(hcryp);
  524. /* Busy error code field */
  525. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  526. return HAL_ERROR;
  527. }
  528. }
  529. /**
  530. * @brief Get CRYP Configuration parameters in associated handle.
  531. * @param pConf: pointer to a CRYP_ConfigTypeDef structure
  532. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  533. * the configuration information for CRYP module
  534. * @retval HAL status
  535. */
  536. HAL_StatusTypeDef HAL_CRYP_GetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf)
  537. {
  538. /* Check the CRYP handle allocation */
  539. if ((hcryp == NULL) || (pConf == NULL))
  540. {
  541. return HAL_ERROR;
  542. }
  543. if (hcryp->State == HAL_CRYP_STATE_READY)
  544. {
  545. /* Change the CRYP state */
  546. hcryp->State = HAL_CRYP_STATE_BUSY;
  547. /* Process locked */
  548. __HAL_LOCK(hcryp);
  549. /* Get CRYP parameters */
  550. pConf->DataType = hcryp->Init.DataType;
  551. pConf->pKey = hcryp->Init.pKey;
  552. pConf->Algorithm = hcryp->Init.Algorithm;
  553. pConf->KeySize = hcryp->Init.KeySize ;
  554. pConf->pInitVect = hcryp->Init.pInitVect;
  555. pConf->Header = hcryp->Init.Header ;
  556. pConf->HeaderSize = hcryp->Init.HeaderSize;
  557. pConf->B0 = hcryp->Init.B0;
  558. pConf->DataWidthUnit = hcryp->Init.DataWidthUnit;
  559. pConf->KeyIVConfigSkip = hcryp->Init.KeyIVConfigSkip;
  560. pConf->HeaderWidthUnit = hcryp->Init.HeaderWidthUnit;
  561. /* Process Unlocked */
  562. __HAL_UNLOCK(hcryp);
  563. /* Change the CRYP state */
  564. hcryp->State = HAL_CRYP_STATE_READY;
  565. /* Return function status */
  566. return HAL_OK;
  567. }
  568. else
  569. {
  570. /* Process Unlocked */
  571. __HAL_UNLOCK(hcryp);
  572. /* Busy error code field */
  573. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  574. return HAL_ERROR;
  575. }
  576. }
  577. /**
  578. * @brief Initializes the CRYP MSP.
  579. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  580. * the configuration information for CRYP module
  581. * @retval None
  582. */
  583. __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
  584. {
  585. /* Prevent unused argument(s) compilation warning */
  586. UNUSED(hcryp);
  587. /* NOTE : This function should not be modified, when the callback is needed,
  588. the HAL_CRYP_MspInit can be implemented in the user file
  589. */
  590. }
  591. /**
  592. * @brief DeInitializes CRYP MSP.
  593. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  594. * the configuration information for CRYP module
  595. * @retval None
  596. */
  597. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  598. {
  599. /* Prevent unused argument(s) compilation warning */
  600. UNUSED(hcryp);
  601. /* NOTE : This function should not be modified, when the callback is needed,
  602. the HAL_CRYP_MspDeInit can be implemented in the user file
  603. */
  604. }
  605. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  606. /**
  607. * @brief Register a User CRYP Callback
  608. * To be used instead of the weak predefined callback
  609. * @param hcryp cryp handle
  610. * @param CallbackID ID of the callback to be registered
  611. * This parameter can be one of the following values:
  612. * @arg @ref HAL_CRYP_INPUT_COMPLETE_CB_ID Input FIFO transfer completed callback ID
  613. * @arg @ref HAL_CRYP_OUTPUT_COMPLETE_CB_ID Output FIFO transfer completed callback ID
  614. * @arg @ref HAL_CRYP_ERROR_CB_ID Error callback ID
  615. * @arg @ref HAL_CRYP_MSPINIT_CB_ID MspInit callback ID
  616. * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID MspDeInit callback ID
  617. * @param pCallback pointer to the Callback function
  618. * @retval status
  619. */
  620. HAL_StatusTypeDef HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID,
  621. pCRYP_CallbackTypeDef pCallback)
  622. {
  623. HAL_StatusTypeDef status = HAL_OK;
  624. if (pCallback == NULL)
  625. {
  626. /* Update the error code */
  627. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  628. return HAL_ERROR;
  629. }
  630. /* Process locked */
  631. __HAL_LOCK(hcryp);
  632. if (hcryp->State == HAL_CRYP_STATE_READY)
  633. {
  634. switch (CallbackID)
  635. {
  636. case HAL_CRYP_INPUT_COMPLETE_CB_ID :
  637. hcryp->InCpltCallback = pCallback;
  638. break;
  639. case HAL_CRYP_OUTPUT_COMPLETE_CB_ID :
  640. hcryp->OutCpltCallback = pCallback;
  641. break;
  642. case HAL_CRYP_ERROR_CB_ID :
  643. hcryp->ErrorCallback = pCallback;
  644. break;
  645. case HAL_CRYP_MSPINIT_CB_ID :
  646. hcryp->MspInitCallback = pCallback;
  647. break;
  648. case HAL_CRYP_MSPDEINIT_CB_ID :
  649. hcryp->MspDeInitCallback = pCallback;
  650. break;
  651. default :
  652. /* Update the error code */
  653. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  654. /* Return error status */
  655. status = HAL_ERROR;
  656. break;
  657. }
  658. }
  659. else if (hcryp->State == HAL_CRYP_STATE_RESET)
  660. {
  661. switch (CallbackID)
  662. {
  663. case HAL_CRYP_MSPINIT_CB_ID :
  664. hcryp->MspInitCallback = pCallback;
  665. break;
  666. case HAL_CRYP_MSPDEINIT_CB_ID :
  667. hcryp->MspDeInitCallback = pCallback;
  668. break;
  669. default :
  670. /* Update the error code */
  671. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  672. /* Return error status */
  673. status = HAL_ERROR;
  674. break;
  675. }
  676. }
  677. else
  678. {
  679. /* Update the error code */
  680. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  681. /* Return error status */
  682. status = HAL_ERROR;
  683. }
  684. /* Release Lock */
  685. __HAL_UNLOCK(hcryp);
  686. return status;
  687. }
  688. /**
  689. * @brief Unregister an CRYP Callback
  690. * CRYP callback is redirected to the weak predefined callback
  691. * @param hcryp cryp handle
  692. * @param CallbackID ID of the callback to be unregistered
  693. * This parameter can be one of the following values:
  694. * @arg @ref HAL_CRYP_INPUT_COMPLETE_CB_ID Input FIFO transfer completed callback ID
  695. * @arg @ref HAL_CRYP_OUTPUT_COMPLETE_CB_ID Output FIFO transfer completed callback ID
  696. * @arg @ref HAL_CRYP_ERROR_CB_ID Error callback ID
  697. * @arg @ref HAL_CRYP_MSPINIT_CB_ID MspInit callback ID
  698. * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID MspDeInit callback ID
  699. * @retval status
  700. */
  701. HAL_StatusTypeDef HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID)
  702. {
  703. HAL_StatusTypeDef status = HAL_OK;
  704. /* Process locked */
  705. __HAL_LOCK(hcryp);
  706. if (hcryp->State == HAL_CRYP_STATE_READY)
  707. {
  708. switch (CallbackID)
  709. {
  710. case HAL_CRYP_INPUT_COMPLETE_CB_ID :
  711. hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak InCpltCallback */
  712. break;
  713. case HAL_CRYP_OUTPUT_COMPLETE_CB_ID :
  714. hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak OutCpltCallback */
  715. break;
  716. case HAL_CRYP_ERROR_CB_ID :
  717. hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak ErrorCallback */
  718. break;
  719. case HAL_CRYP_MSPINIT_CB_ID :
  720. hcryp->MspInitCallback = HAL_CRYP_MspInit;
  721. break;
  722. case HAL_CRYP_MSPDEINIT_CB_ID :
  723. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
  724. break;
  725. default :
  726. /* Update the error code */
  727. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  728. /* Return error status */
  729. status = HAL_ERROR;
  730. break;
  731. }
  732. }
  733. else if (hcryp->State == HAL_CRYP_STATE_RESET)
  734. {
  735. switch (CallbackID)
  736. {
  737. case HAL_CRYP_MSPINIT_CB_ID :
  738. hcryp->MspInitCallback = HAL_CRYP_MspInit;
  739. break;
  740. case HAL_CRYP_MSPDEINIT_CB_ID :
  741. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
  742. break;
  743. default :
  744. /* Update the error code */
  745. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  746. /* Return error status */
  747. status = HAL_ERROR;
  748. break;
  749. }
  750. }
  751. else
  752. {
  753. /* Update the error code */
  754. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  755. /* Return error status */
  756. status = HAL_ERROR;
  757. }
  758. /* Release Lock */
  759. __HAL_UNLOCK(hcryp);
  760. return status;
  761. }
  762. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  763. /**
  764. * @}
  765. */
  766. /** @defgroup CRYP_Exported_Functions_Group2 Encrypt Decrypt functions
  767. * @brief processing functions.
  768. *
  769. @verbatim
  770. ==============================================================================
  771. ##### Encrypt Decrypt functions #####
  772. ==============================================================================
  773. [..] This section provides API allowing to Encrypt/Decrypt Data following
  774. Standard DES/TDES or AES, and Algorithm configured by the user:
  775. (+) Standard DES/TDES only supported by CRYP1 IP, below list of Algorithm supported :
  776. - Electronic Code Book(ECB)
  777. - Cipher Block Chaining (CBC)
  778. (+) Standard AES supported by CRYP1 IP & TinyAES, list of Algorithm supported:
  779. - Electronic Code Book(ECB)
  780. - Cipher Block Chaining (CBC)
  781. - Counter mode (CTR)
  782. - Cipher Block Chaining (CBC)
  783. - Counter mode (CTR)
  784. - Galois/counter mode (GCM)
  785. - Counter with Cipher Block Chaining-Message(CCM)
  786. [..] Three processing functions are available:
  787. (+) Polling mode : HAL_CRYP_Encrypt & HAL_CRYP_Decrypt
  788. (+) Interrupt mode : HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT
  789. (+) DMA mode : HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA
  790. @endverbatim
  791. * @{
  792. */
  793. /**
  794. * @brief Encryption mode.
  795. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  796. * the configuration information for CRYP module
  797. * @param Input: Pointer to the input buffer (plaintext)
  798. * @param Size: Length of the plaintext buffer in word.
  799. * @param Output: Pointer to the output buffer(ciphertext)
  800. * @param Timeout: Specify Timeout value
  801. * @retval HAL status
  802. */
  803. HAL_StatusTypeDef HAL_CRYP_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output,
  804. uint32_t Timeout)
  805. {
  806. uint32_t algo;
  807. HAL_StatusTypeDef status;
  808. if (hcryp->State == HAL_CRYP_STATE_READY)
  809. {
  810. /* Change state Busy */
  811. hcryp->State = HAL_CRYP_STATE_BUSY;
  812. /* Process locked */
  813. __HAL_LOCK(hcryp);
  814. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  815. hcryp->CrypInCount = 0U;
  816. hcryp->CrypOutCount = 0U;
  817. hcryp->pCrypInBuffPtr = Input;
  818. hcryp->pCrypOutBuffPtr = Output;
  819. /* Calculate Size parameter in Byte*/
  820. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  821. {
  822. hcryp->Size = Size * 4U;
  823. }
  824. else
  825. {
  826. hcryp->Size = Size;
  827. }
  828. #if defined (CRYP)
  829. /* Set Encryption operating mode*/
  830. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
  831. /* algo get algorithm selected */
  832. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  833. switch (algo)
  834. {
  835. case CRYP_DES_ECB:
  836. case CRYP_DES_CBC:
  837. case CRYP_TDES_ECB:
  838. case CRYP_TDES_CBC:
  839. /*Set Key */
  840. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  841. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  842. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  843. {
  844. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  845. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  846. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  847. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  848. }
  849. /*Set Initialization Vector (IV)*/
  850. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  851. {
  852. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  853. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  854. }
  855. /* Flush FIFO */
  856. HAL_CRYP_FIFO_FLUSH(hcryp);
  857. /* Set the phase */
  858. hcryp->Phase = CRYP_PHASE_PROCESS;
  859. /* Statrt DES/TDES encryption process */
  860. status = CRYP_TDES_Process(hcryp, Timeout);
  861. break;
  862. case CRYP_AES_ECB:
  863. case CRYP_AES_CBC:
  864. case CRYP_AES_CTR:
  865. /* AES encryption */
  866. status = CRYP_AES_Encrypt(hcryp, Timeout);
  867. break;
  868. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  869. case CRYP_AES_GCM:
  870. /* AES GCM encryption */
  871. status = CRYP_AESGCM_Process(hcryp, Timeout);
  872. break;
  873. case CRYP_AES_CCM:
  874. /* AES CCM encryption */
  875. status = CRYP_AESCCM_Process(hcryp, Timeout);
  876. break;
  877. #endif /* GCM CCM defined*/
  878. default:
  879. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  880. /* Change the CRYP peripheral state */
  881. hcryp->State = HAL_CRYP_STATE_READY;
  882. /* Process unlocked */
  883. __HAL_UNLOCK(hcryp);
  884. return HAL_ERROR;
  885. }
  886. #else /*AES*/
  887. /* Set the operating mode*/
  888. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  889. /* algo get algorithm selected */
  890. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  891. switch (algo)
  892. {
  893. case CRYP_AES_ECB:
  894. case CRYP_AES_CBC:
  895. case CRYP_AES_CTR:
  896. /* AES encryption */
  897. status = CRYP_AES_Encrypt(hcryp, Timeout);
  898. break;
  899. case CRYP_AES_GCM_GMAC:
  900. /* AES GCM encryption */
  901. status = CRYP_AESGCM_Process(hcryp, Timeout) ;
  902. break;
  903. case CRYP_AES_CCM:
  904. /* AES CCM encryption */
  905. status = CRYP_AESCCM_Process(hcryp, Timeout);
  906. break;
  907. default:
  908. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  909. /* Change the CRYP peripheral state */
  910. hcryp->State = HAL_CRYP_STATE_READY;
  911. /* Process unlocked */
  912. __HAL_UNLOCK(hcryp);
  913. return HAL_ERROR;
  914. }
  915. #endif /*end AES or CRYP */
  916. if (status == HAL_OK)
  917. {
  918. /* Change the CRYP peripheral state */
  919. hcryp->State = HAL_CRYP_STATE_READY;
  920. /* Process unlocked */
  921. __HAL_UNLOCK(hcryp);
  922. }
  923. }
  924. else
  925. {
  926. /* Busy error code field */
  927. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  928. return HAL_ERROR;
  929. }
  930. /* Return function status */
  931. return HAL_OK;
  932. }
  933. /**
  934. * @brief Decryption mode.
  935. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  936. * the configuration information for CRYP module
  937. * @param Input: Pointer to the input buffer (ciphertext )
  938. * @param Size: Length of the plaintext buffer in word.
  939. * @param Output: Pointer to the output buffer(plaintext)
  940. * @param Timeout: Specify Timeout value
  941. * @retval HAL status
  942. */
  943. HAL_StatusTypeDef HAL_CRYP_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output,
  944. uint32_t Timeout)
  945. {
  946. HAL_StatusTypeDef status;
  947. uint32_t algo;
  948. if (hcryp->State == HAL_CRYP_STATE_READY)
  949. {
  950. /* Change state Busy */
  951. hcryp->State = HAL_CRYP_STATE_BUSY;
  952. /* Process locked */
  953. __HAL_LOCK(hcryp);
  954. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  955. hcryp->CrypInCount = 0U;
  956. hcryp->CrypOutCount = 0U;
  957. hcryp->pCrypInBuffPtr = Input;
  958. hcryp->pCrypOutBuffPtr = Output;
  959. /* Calculate Size parameter in Byte*/
  960. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  961. {
  962. hcryp->Size = Size * 4U;
  963. }
  964. else
  965. {
  966. hcryp->Size = Size;
  967. }
  968. #if defined (CRYP)
  969. /* Set Decryption operating mode*/
  970. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT);
  971. /* algo get algorithm selected */
  972. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  973. switch (algo)
  974. {
  975. case CRYP_DES_ECB:
  976. case CRYP_DES_CBC:
  977. case CRYP_TDES_ECB:
  978. case CRYP_TDES_CBC:
  979. /*Set Key */
  980. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  981. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  982. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  983. {
  984. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  985. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  986. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  987. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  988. }
  989. /*Set Initialization Vector (IV)*/
  990. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  991. {
  992. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  993. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  994. }
  995. /* Flush FIFO */
  996. HAL_CRYP_FIFO_FLUSH(hcryp);
  997. /* Set the phase */
  998. hcryp->Phase = CRYP_PHASE_PROCESS;
  999. /* Start DES/TDES decryption process */
  1000. status = CRYP_TDES_Process(hcryp, Timeout);
  1001. break;
  1002. case CRYP_AES_ECB:
  1003. case CRYP_AES_CBC:
  1004. case CRYP_AES_CTR:
  1005. /* AES decryption */
  1006. status = CRYP_AES_Decrypt(hcryp, Timeout);
  1007. break;
  1008. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1009. case CRYP_AES_GCM:
  1010. /* AES GCM decryption */
  1011. status = CRYP_AESGCM_Process(hcryp, Timeout) ;
  1012. break;
  1013. case CRYP_AES_CCM:
  1014. /* AES CCM decryption */
  1015. status = CRYP_AESCCM_Process(hcryp, Timeout);
  1016. break;
  1017. #endif /* GCM CCM defined*/
  1018. default:
  1019. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1020. /* Change the CRYP peripheral state */
  1021. hcryp->State = HAL_CRYP_STATE_READY;
  1022. /* Process unlocked */
  1023. __HAL_UNLOCK(hcryp);
  1024. return HAL_ERROR;
  1025. }
  1026. #else /*AES*/
  1027. /* Set Decryption operating mode*/
  1028. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  1029. /* algo get algorithm selected */
  1030. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1031. switch (algo)
  1032. {
  1033. case CRYP_AES_ECB:
  1034. case CRYP_AES_CBC:
  1035. case CRYP_AES_CTR:
  1036. /* AES decryption */
  1037. status = CRYP_AES_Decrypt(hcryp, Timeout);
  1038. break;
  1039. case CRYP_AES_GCM_GMAC:
  1040. /* AES GCM decryption */
  1041. status = CRYP_AESGCM_Process(hcryp, Timeout) ;
  1042. break;
  1043. case CRYP_AES_CCM:
  1044. /* AES CCM decryption */
  1045. status = CRYP_AESCCM_Process(hcryp, Timeout);
  1046. break;
  1047. default:
  1048. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1049. /* Change the CRYP peripheral state */
  1050. hcryp->State = HAL_CRYP_STATE_READY;
  1051. /* Process unlocked */
  1052. __HAL_UNLOCK(hcryp);
  1053. return HAL_ERROR;
  1054. }
  1055. #endif /* End AES or CRYP */
  1056. if (status == HAL_OK)
  1057. {
  1058. /* Change the CRYP peripheral state */
  1059. hcryp->State = HAL_CRYP_STATE_READY;
  1060. /* Process unlocked */
  1061. __HAL_UNLOCK(hcryp);
  1062. }
  1063. }
  1064. else
  1065. {
  1066. /* Busy error code field */
  1067. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1068. return HAL_ERROR;
  1069. }
  1070. /* Return function status */
  1071. return HAL_OK;
  1072. }
  1073. /**
  1074. * @brief Encryption in interrupt mode.
  1075. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1076. * the configuration information for CRYP module
  1077. * @param Input: Pointer to the input buffer (plaintext)
  1078. * @param Size: Length of the plaintext buffer in word
  1079. * @param Output: Pointer to the output buffer(ciphertext)
  1080. * @retval HAL status
  1081. */
  1082. HAL_StatusTypeDef HAL_CRYP_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1083. {
  1084. uint32_t algo;
  1085. HAL_StatusTypeDef status = HAL_OK;
  1086. if (hcryp->State == HAL_CRYP_STATE_READY)
  1087. {
  1088. /* Change state Busy */
  1089. hcryp->State = HAL_CRYP_STATE_BUSY;
  1090. /* Process locked */
  1091. __HAL_LOCK(hcryp);
  1092. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  1093. hcryp->CrypInCount = 0U;
  1094. hcryp->CrypOutCount = 0U;
  1095. hcryp->pCrypInBuffPtr = Input;
  1096. hcryp->pCrypOutBuffPtr = Output;
  1097. /* Calculate Size parameter in Byte*/
  1098. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1099. {
  1100. hcryp->Size = Size * 4U;
  1101. }
  1102. else
  1103. {
  1104. hcryp->Size = Size;
  1105. }
  1106. #if defined (CRYP)
  1107. /* Set encryption operating mode*/
  1108. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
  1109. /* algo get algorithm selected */
  1110. algo = (hcryp->Instance->CR & CRYP_CR_ALGOMODE);
  1111. switch (algo)
  1112. {
  1113. case CRYP_DES_ECB:
  1114. case CRYP_DES_CBC:
  1115. case CRYP_TDES_ECB:
  1116. case CRYP_TDES_CBC:
  1117. /*Set Key */
  1118. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  1119. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  1120. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1121. {
  1122. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  1123. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  1124. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  1125. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  1126. }
  1127. /* Set the Initialization Vector*/
  1128. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1129. {
  1130. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  1131. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  1132. }
  1133. /* Flush FIFO */
  1134. HAL_CRYP_FIFO_FLUSH(hcryp);
  1135. /* Set the phase */
  1136. hcryp->Phase = CRYP_PHASE_PROCESS;
  1137. /* Enable interrupts */
  1138. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1139. /* Enable CRYP to start DES/TDES process*/
  1140. __HAL_CRYP_ENABLE(hcryp);
  1141. break;
  1142. case CRYP_AES_ECB:
  1143. case CRYP_AES_CBC:
  1144. case CRYP_AES_CTR:
  1145. status = CRYP_AES_Encrypt_IT(hcryp);
  1146. break;
  1147. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1148. case CRYP_AES_GCM:
  1149. status = CRYP_AESGCM_Process_IT(hcryp) ;
  1150. break;
  1151. case CRYP_AES_CCM:
  1152. status = CRYP_AESCCM_Process_IT(hcryp);
  1153. break;
  1154. #endif /* GCM CCM defined*/
  1155. default:
  1156. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1157. /* Change the CRYP peripheral state */
  1158. hcryp->State = HAL_CRYP_STATE_READY;
  1159. /* Process unlocked */
  1160. __HAL_UNLOCK(hcryp);
  1161. status = HAL_ERROR;
  1162. break;
  1163. }
  1164. #else /* AES */
  1165. /* Set encryption operating mode*/
  1166. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  1167. /* algo get algorithm selected */
  1168. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1169. switch (algo)
  1170. {
  1171. case CRYP_AES_ECB:
  1172. case CRYP_AES_CBC:
  1173. case CRYP_AES_CTR:
  1174. /* AES encryption */
  1175. status = CRYP_AES_Encrypt_IT(hcryp);
  1176. break;
  1177. case CRYP_AES_GCM_GMAC:
  1178. /* AES GCM encryption */
  1179. status = CRYP_AESGCM_Process_IT(hcryp) ;
  1180. break;
  1181. case CRYP_AES_CCM:
  1182. /* AES CCM encryption */
  1183. status = CRYP_AESCCM_Process_IT(hcryp);
  1184. break;
  1185. default:
  1186. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1187. /* Change the CRYP peripheral state */
  1188. hcryp->State = HAL_CRYP_STATE_READY;
  1189. /* Process unlocked */
  1190. __HAL_UNLOCK(hcryp);
  1191. status = HAL_ERROR;
  1192. break;
  1193. }
  1194. #endif /*end AES or CRYP*/
  1195. }
  1196. else
  1197. {
  1198. /* Busy error code field */
  1199. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1200. status = HAL_ERROR;
  1201. }
  1202. /* Return function status */
  1203. return status;
  1204. }
  1205. /**
  1206. * @brief Decryption in itnterrupt mode.
  1207. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1208. * the configuration information for CRYP module
  1209. * @param Input: Pointer to the input buffer (ciphertext )
  1210. * @param Size: Length of the plaintext buffer in word.
  1211. * @param Output: Pointer to the output buffer(plaintext)
  1212. * @retval HAL status
  1213. */
  1214. HAL_StatusTypeDef HAL_CRYP_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1215. {
  1216. uint32_t algo;
  1217. HAL_StatusTypeDef status = HAL_OK;
  1218. if (hcryp->State == HAL_CRYP_STATE_READY)
  1219. {
  1220. /* Change state Busy */
  1221. hcryp->State = HAL_CRYP_STATE_BUSY;
  1222. /* Process locked */
  1223. __HAL_LOCK(hcryp);
  1224. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  1225. hcryp->CrypInCount = 0U;
  1226. hcryp->CrypOutCount = 0U;
  1227. hcryp->pCrypInBuffPtr = Input;
  1228. hcryp->pCrypOutBuffPtr = Output;
  1229. /* Calculate Size parameter in Byte*/
  1230. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1231. {
  1232. hcryp->Size = Size * 4U;
  1233. }
  1234. else
  1235. {
  1236. hcryp->Size = Size;
  1237. }
  1238. #if defined (CRYP)
  1239. /* Set decryption operating mode*/
  1240. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT);
  1241. /* algo get algorithm selected */
  1242. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  1243. switch (algo)
  1244. {
  1245. case CRYP_DES_ECB:
  1246. case CRYP_DES_CBC:
  1247. case CRYP_TDES_ECB:
  1248. case CRYP_TDES_CBC:
  1249. /*Set Key */
  1250. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  1251. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  1252. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1253. {
  1254. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  1255. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  1256. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  1257. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  1258. }
  1259. /* Set the Initialization Vector*/
  1260. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1261. {
  1262. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  1263. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  1264. }
  1265. /* Flush FIFO */
  1266. HAL_CRYP_FIFO_FLUSH(hcryp);
  1267. /* Set the phase */
  1268. hcryp->Phase = CRYP_PHASE_PROCESS;
  1269. /* Enable interrupts */
  1270. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1271. /* Enable CRYP and start DES/TDES process*/
  1272. __HAL_CRYP_ENABLE(hcryp);
  1273. break;
  1274. case CRYP_AES_ECB:
  1275. case CRYP_AES_CBC:
  1276. case CRYP_AES_CTR:
  1277. /* AES decryption */
  1278. status = CRYP_AES_Decrypt_IT(hcryp);
  1279. break;
  1280. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1281. case CRYP_AES_GCM:
  1282. /* AES GCM decryption */
  1283. status = CRYP_AESGCM_Process_IT(hcryp) ;
  1284. break;
  1285. case CRYP_AES_CCM:
  1286. /* AES CCMdecryption */
  1287. status = CRYP_AESCCM_Process_IT(hcryp);
  1288. break;
  1289. #endif /* GCM CCM defined*/
  1290. default:
  1291. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1292. /* Change the CRYP peripheral state */
  1293. hcryp->State = HAL_CRYP_STATE_READY;
  1294. /* Process unlocked */
  1295. __HAL_UNLOCK(hcryp);
  1296. status = HAL_ERROR;
  1297. break;
  1298. }
  1299. #else /*AES*/
  1300. /* Set decryption operating mode*/
  1301. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  1302. /* algo get algorithm selected */
  1303. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1304. switch (algo)
  1305. {
  1306. case CRYP_AES_ECB:
  1307. case CRYP_AES_CBC:
  1308. case CRYP_AES_CTR:
  1309. /* AES decryption */
  1310. status = CRYP_AES_Decrypt_IT(hcryp);
  1311. break;
  1312. case CRYP_AES_GCM_GMAC:
  1313. /* AES GCM decryption */
  1314. status = CRYP_AESGCM_Process_IT(hcryp) ;
  1315. break;
  1316. case CRYP_AES_CCM:
  1317. /* AES CCM decryption */
  1318. status = CRYP_AESCCM_Process_IT(hcryp);
  1319. break;
  1320. default:
  1321. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1322. /* Change the CRYP peripheral state */
  1323. hcryp->State = HAL_CRYP_STATE_READY;
  1324. /* Process unlocked */
  1325. __HAL_UNLOCK(hcryp);
  1326. status = HAL_ERROR;
  1327. break;
  1328. }
  1329. #endif /* End AES or CRYP */
  1330. }
  1331. else
  1332. {
  1333. /* Busy error code field */
  1334. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1335. status = HAL_ERROR;
  1336. }
  1337. /* Return function status */
  1338. return status;
  1339. }
  1340. /**
  1341. * @brief Encryption in DMA mode.
  1342. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1343. * the configuration information for CRYP module
  1344. * @param Input: Pointer to the input buffer (plaintext)
  1345. * @param Size: Length of the plaintext buffer in word.
  1346. * @param Output: Pointer to the output buffer(ciphertext)
  1347. * @retval HAL status
  1348. */
  1349. HAL_StatusTypeDef HAL_CRYP_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1350. {
  1351. uint32_t algo;
  1352. HAL_StatusTypeDef status = HAL_OK;
  1353. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  1354. if (hcryp->State == HAL_CRYP_STATE_READY)
  1355. {
  1356. /* Change state Busy */
  1357. hcryp->State = HAL_CRYP_STATE_BUSY;
  1358. /* Process locked */
  1359. __HAL_LOCK(hcryp);
  1360. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  1361. hcryp->CrypInCount = 0U;
  1362. hcryp->CrypOutCount = 0U;
  1363. hcryp->pCrypInBuffPtr = Input;
  1364. hcryp->pCrypOutBuffPtr = Output;
  1365. /* Calculate Size parameter in Byte*/
  1366. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1367. {
  1368. hcryp->Size = Size * 4U;
  1369. }
  1370. else
  1371. {
  1372. hcryp->Size = Size;
  1373. }
  1374. #if defined (CRYP)
  1375. /* Set encryption operating mode*/
  1376. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
  1377. /* algo get algorithm selected */
  1378. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  1379. switch (algo)
  1380. {
  1381. case CRYP_DES_ECB:
  1382. case CRYP_DES_CBC:
  1383. case CRYP_TDES_ECB:
  1384. case CRYP_TDES_CBC:
  1385. /*Set Key */
  1386. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  1387. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  1388. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1389. {
  1390. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  1391. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  1392. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  1393. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  1394. }
  1395. /* Set the Initialization Vector*/
  1396. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1397. {
  1398. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  1399. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  1400. }
  1401. /* Flush FIFO */
  1402. HAL_CRYP_FIFO_FLUSH(hcryp);
  1403. /* Set the phase */
  1404. hcryp->Phase = CRYP_PHASE_PROCESS;
  1405. /* Start DMA process transfer for DES/TDES */
  1406. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)(hcryp->Size) / 4U),
  1407. (uint32_t)(hcryp->pCrypOutBuffPtr));
  1408. break;
  1409. case CRYP_AES_ECB:
  1410. case CRYP_AES_CBC:
  1411. case CRYP_AES_CTR:
  1412. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  1413. {
  1414. if (hcryp->KeyIVConfig == 1U)
  1415. {
  1416. /* If the Key and IV configuration has to be done only once
  1417. and if it has already been done, skip it */
  1418. DoKeyIVConfig = 0U;
  1419. }
  1420. else
  1421. {
  1422. /* If the Key and IV configuration has to be done only once
  1423. and if it has not been done already, do it and set KeyIVConfig
  1424. to keep track it won't have to be done again next time */
  1425. hcryp->KeyIVConfig = 1U;
  1426. }
  1427. }
  1428. if (DoKeyIVConfig == 1U)
  1429. {
  1430. /* Set the Key*/
  1431. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1432. /* Set the Initialization Vector*/
  1433. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1434. {
  1435. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  1436. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1U);
  1437. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2U);
  1438. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3U);
  1439. }
  1440. } /* if (DoKeyIVConfig == 1U) */
  1441. /* Set the phase */
  1442. hcryp->Phase = CRYP_PHASE_PROCESS;
  1443. /* Start DMA process transfer for AES */
  1444. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)(hcryp->Size) / 4U),
  1445. (uint32_t)(hcryp->pCrypOutBuffPtr));
  1446. break;
  1447. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1448. case CRYP_AES_GCM:
  1449. /* AES GCM encryption */
  1450. status = CRYP_AESGCM_Process_DMA(hcryp) ;
  1451. break;
  1452. case CRYP_AES_CCM:
  1453. /* AES CCM encryption */
  1454. status = CRYP_AESCCM_Process_DMA(hcryp);
  1455. break;
  1456. #endif /* GCM CCM defined*/
  1457. default:
  1458. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1459. /* Change the CRYP peripheral state */
  1460. hcryp->State = HAL_CRYP_STATE_READY;
  1461. /* Process unlocked */
  1462. __HAL_UNLOCK(hcryp);
  1463. status = HAL_ERROR;
  1464. break;
  1465. }
  1466. #else /*AES*/
  1467. /* Set encryption operating mode*/
  1468. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  1469. /* algo get algorithm selected */
  1470. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1471. switch (algo)
  1472. {
  1473. case CRYP_AES_ECB:
  1474. case CRYP_AES_CBC:
  1475. case CRYP_AES_CTR:
  1476. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  1477. {
  1478. if (hcryp->KeyIVConfig == 1U)
  1479. {
  1480. /* If the Key and IV configuration has to be done only once
  1481. and if it has already been done, skip it */
  1482. DoKeyIVConfig = 0U;
  1483. }
  1484. else
  1485. {
  1486. /* If the Key and IV configuration has to be done only once
  1487. and if it has not been done already, do it and set KeyIVConfig
  1488. to keep track it won't have to be done again next time */
  1489. hcryp->KeyIVConfig = 1U;
  1490. }
  1491. }
  1492. if (DoKeyIVConfig == 1U)
  1493. {
  1494. /* Set the Key*/
  1495. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1496. /* Set the Initialization Vector*/
  1497. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1498. {
  1499. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  1500. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  1501. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  1502. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  1503. }
  1504. } /* if (DoKeyIVConfig == 1U) */
  1505. /* Set the phase */
  1506. hcryp->Phase = CRYP_PHASE_PROCESS;
  1507. /* Start DMA process transfer for AES */
  1508. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
  1509. break;
  1510. case CRYP_AES_GCM_GMAC:
  1511. /* AES GCM encryption */
  1512. status = CRYP_AESGCM_Process_DMA(hcryp) ;
  1513. break;
  1514. case CRYP_AES_CCM:
  1515. /* AES CCM encryption */
  1516. status = CRYP_AESCCM_Process_DMA(hcryp);
  1517. break;
  1518. default:
  1519. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1520. /* Change the CRYP peripheral state */
  1521. hcryp->State = HAL_CRYP_STATE_READY;
  1522. /* Process unlocked */
  1523. __HAL_UNLOCK(hcryp);
  1524. status = HAL_ERROR;
  1525. break;
  1526. }
  1527. #endif /* End AES or CRYP */
  1528. }
  1529. else
  1530. {
  1531. /* Busy error code field */
  1532. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1533. status = HAL_ERROR;
  1534. }
  1535. /* Return function status */
  1536. return status;
  1537. }
  1538. /**
  1539. * @brief Decryption in DMA mode.
  1540. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1541. * the configuration information for CRYP module
  1542. * @param Input: Pointer to the input buffer (ciphertext )
  1543. * @param Size: Length of the plaintext buffer in word
  1544. * @param Output: Pointer to the output buffer(plaintext)
  1545. * @retval HAL status
  1546. */
  1547. HAL_StatusTypeDef HAL_CRYP_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1548. {
  1549. uint32_t algo;
  1550. HAL_StatusTypeDef status = HAL_OK;
  1551. if (hcryp->State == HAL_CRYP_STATE_READY)
  1552. {
  1553. /* Change state Busy */
  1554. hcryp->State = HAL_CRYP_STATE_BUSY;
  1555. /* Process locked */
  1556. __HAL_LOCK(hcryp);
  1557. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
  1558. hcryp->CrypInCount = 0U;
  1559. hcryp->CrypOutCount = 0U;
  1560. hcryp->pCrypInBuffPtr = Input;
  1561. hcryp->pCrypOutBuffPtr = Output;
  1562. /* Calculate Size parameter in Byte*/
  1563. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1564. {
  1565. hcryp->Size = Size * 4U;
  1566. }
  1567. else
  1568. {
  1569. hcryp->Size = Size;
  1570. }
  1571. #if defined (CRYP)
  1572. /* Set decryption operating mode*/
  1573. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT);
  1574. /* algo get algorithm selected */
  1575. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  1576. switch (algo)
  1577. {
  1578. case CRYP_DES_ECB:
  1579. case CRYP_DES_CBC:
  1580. case CRYP_TDES_ECB:
  1581. case CRYP_TDES_CBC:
  1582. /*Set Key */
  1583. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  1584. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  1585. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1586. {
  1587. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  1588. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  1589. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  1590. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  1591. }
  1592. /* Set the Initialization Vector*/
  1593. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1594. {
  1595. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  1596. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  1597. }
  1598. /* Flush FIFO */
  1599. HAL_CRYP_FIFO_FLUSH(hcryp);
  1600. /* Set the phase */
  1601. hcryp->Phase = CRYP_PHASE_PROCESS;
  1602. /* Start DMA process transfer for DES/TDES */
  1603. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)(hcryp->Size) / 4U),
  1604. (uint32_t)(hcryp->pCrypOutBuffPtr));
  1605. break;
  1606. case CRYP_AES_ECB:
  1607. case CRYP_AES_CBC:
  1608. case CRYP_AES_CTR:
  1609. /* AES decryption */
  1610. status = CRYP_AES_Decrypt_DMA(hcryp);
  1611. break;
  1612. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1613. case CRYP_AES_GCM:
  1614. /* AES GCM decryption */
  1615. status = CRYP_AESGCM_Process_DMA(hcryp) ;
  1616. break;
  1617. case CRYP_AES_CCM:
  1618. /* AES CCM decryption */
  1619. status = CRYP_AESCCM_Process_DMA(hcryp);
  1620. break;
  1621. #endif /* GCM CCM defined*/
  1622. default:
  1623. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1624. /* Change the CRYP peripheral state */
  1625. hcryp->State = HAL_CRYP_STATE_READY;
  1626. /* Process unlocked */
  1627. __HAL_UNLOCK(hcryp);
  1628. status = HAL_ERROR;
  1629. break;
  1630. }
  1631. #else /*AES*/
  1632. /* Set decryption operating mode*/
  1633. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  1634. /* algo get algorithm selected */
  1635. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1636. switch (algo)
  1637. {
  1638. case CRYP_AES_ECB:
  1639. case CRYP_AES_CBC:
  1640. case CRYP_AES_CTR:
  1641. /* AES decryption */
  1642. status = CRYP_AES_Decrypt_DMA(hcryp);
  1643. break;
  1644. case CRYP_AES_GCM_GMAC:
  1645. /* AES GCM decryption */
  1646. status = CRYP_AESGCM_Process_DMA(hcryp) ;
  1647. break;
  1648. case CRYP_AES_CCM:
  1649. /* AES CCM decryption */
  1650. status = CRYP_AESCCM_Process_DMA(hcryp);
  1651. break;
  1652. default:
  1653. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1654. /* Change the CRYP peripheral state */
  1655. hcryp->State = HAL_CRYP_STATE_READY;
  1656. /* Process unlocked */
  1657. __HAL_UNLOCK(hcryp);
  1658. status = HAL_ERROR;
  1659. break;
  1660. }
  1661. #endif /* End AES or CRYP */
  1662. }
  1663. else
  1664. {
  1665. /* Busy error code field */
  1666. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1667. status = HAL_ERROR;
  1668. }
  1669. /* Return function status */
  1670. return status;
  1671. }
  1672. /**
  1673. * @}
  1674. */
  1675. /** @defgroup CRYP_Exported_Functions_Group3 CRYP IRQ handler management
  1676. * @brief CRYP IRQ handler.
  1677. *
  1678. @verbatim
  1679. ==============================================================================
  1680. ##### CRYP IRQ handler management #####
  1681. ==============================================================================
  1682. [..] This section provides CRYP IRQ handler and callback functions.
  1683. (+) HAL_CRYP_IRQHandler CRYP interrupt request
  1684. (+) HAL_CRYP_InCpltCallback input data transfer complete callback
  1685. (+) HAL_CRYP_OutCpltCallback output data transfer complete callback
  1686. (+) HAL_CRYP_ErrorCallback CRYP error callback
  1687. (+) HAL_CRYP_GetState return the CRYP state
  1688. (+) HAL_CRYP_GetError return the CRYP error code
  1689. @endverbatim
  1690. * @{
  1691. */
  1692. /**
  1693. * @brief This function handles cryptographic interrupt request.
  1694. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1695. * the configuration information for CRYP module
  1696. * @retval None
  1697. */
  1698. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  1699. {
  1700. #if defined (CRYP)
  1701. uint32_t itstatus = hcryp->Instance->MISR;
  1702. if ((itstatus & (CRYP_IT_INI | CRYP_IT_OUTI)) != 0U)
  1703. {
  1704. if ((hcryp->Init.Algorithm == CRYP_DES_ECB) || (hcryp->Init.Algorithm == CRYP_DES_CBC)
  1705. || (hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1706. {
  1707. CRYP_TDES_IT(hcryp); /* DES or TDES*/
  1708. }
  1709. else if ((hcryp->Init.Algorithm == CRYP_AES_ECB) || (hcryp->Init.Algorithm == CRYP_AES_CBC)
  1710. || (hcryp->Init.Algorithm == CRYP_AES_CTR))
  1711. {
  1712. CRYP_AES_IT(hcryp); /*AES*/
  1713. }
  1714. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1715. else if ((hcryp->Init.Algorithm == CRYP_AES_GCM) || (hcryp->Init.Algorithm == CRYP_CR_ALGOMODE_AES_CCM))
  1716. {
  1717. /* if header phase */
  1718. if ((hcryp->Instance->CR & CRYP_PHASE_HEADER) == CRYP_PHASE_HEADER)
  1719. {
  1720. CRYP_GCMCCM_SetHeaderPhase_IT(hcryp);
  1721. }
  1722. else /* if payload phase */
  1723. {
  1724. CRYP_GCMCCM_SetPayloadPhase_IT(hcryp);
  1725. }
  1726. }
  1727. #endif /* GCM CCM defined*/
  1728. else
  1729. {
  1730. /* Nothing to do */
  1731. }
  1732. }
  1733. #else /*AES*/
  1734. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_CCF) != RESET)
  1735. {
  1736. if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_CCFIE) != RESET)
  1737. {
  1738. /* Clear computation complete flag */
  1739. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1740. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  1741. {
  1742. /* if header phase */
  1743. if ((hcryp->Instance->CR & CRYP_PHASE_HEADER) == CRYP_PHASE_HEADER)
  1744. {
  1745. CRYP_GCMCCM_SetHeaderPhase_IT(hcryp);
  1746. }
  1747. else /* if payload phase */
  1748. {
  1749. CRYP_GCMCCM_SetPayloadPhase_IT(hcryp);
  1750. }
  1751. }
  1752. else if (hcryp->Init.Algorithm == CRYP_AES_CCM)
  1753. {
  1754. /* if header phase */
  1755. if (hcryp->Init.HeaderSize >= hcryp->CrypHeaderCount)
  1756. {
  1757. CRYP_GCMCCM_SetHeaderPhase_IT(hcryp);
  1758. }
  1759. else /* if payload phase */
  1760. {
  1761. CRYP_GCMCCM_SetPayloadPhase_IT(hcryp);
  1762. }
  1763. }
  1764. else /* AES Algorithm ECB,CBC or CTR*/
  1765. {
  1766. CRYP_AES_IT(hcryp);
  1767. }
  1768. }
  1769. }
  1770. /* Check if error occurred */
  1771. if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_ERRIE) != RESET)
  1772. {
  1773. /* If write Error occurred */
  1774. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_WRERR) != RESET)
  1775. {
  1776. hcryp->ErrorCode |= HAL_CRYP_ERROR_WRITE;
  1777. }
  1778. /* If read Error occurred */
  1779. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_RDERR) != RESET)
  1780. {
  1781. hcryp->ErrorCode |= HAL_CRYP_ERROR_READ;
  1782. }
  1783. }
  1784. #endif /* End AES or CRYP */
  1785. }
  1786. /**
  1787. * @brief Return the CRYP error code.
  1788. * @param hcryp : pointer to a CRYP_HandleTypeDef structure that contains
  1789. * the configuration information for the CRYP IP
  1790. * @retval CRYP error code
  1791. */
  1792. uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
  1793. {
  1794. return hcryp->ErrorCode;
  1795. }
  1796. /**
  1797. * @brief Returns the CRYP state.
  1798. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1799. * the configuration information for CRYP module.
  1800. * @retval HAL state
  1801. */
  1802. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  1803. {
  1804. return hcryp->State;
  1805. }
  1806. /**
  1807. * @brief Input FIFO transfer completed callback.
  1808. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1809. * the configuration information for CRYP module.
  1810. * @retval None
  1811. */
  1812. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  1813. {
  1814. /* Prevent unused argument(s) compilation warning */
  1815. UNUSED(hcryp);
  1816. /* NOTE : This function should not be modified, when the callback is needed,
  1817. the HAL_CRYP_InCpltCallback can be implemented in the user file
  1818. */
  1819. }
  1820. /**
  1821. * @brief Output FIFO transfer completed callback.
  1822. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1823. * the configuration information for CRYP module.
  1824. * @retval None
  1825. */
  1826. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  1827. {
  1828. /* Prevent unused argument(s) compilation warning */
  1829. UNUSED(hcryp);
  1830. /* NOTE : This function should not be modified, when the callback is needed,
  1831. the HAL_CRYP_OutCpltCallback can be implemented in the user file
  1832. */
  1833. }
  1834. /**
  1835. * @brief CRYP error callback.
  1836. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1837. * the configuration information for CRYP module.
  1838. * @retval None
  1839. */
  1840. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  1841. {
  1842. /* Prevent unused argument(s) compilation warning */
  1843. UNUSED(hcryp);
  1844. /* NOTE : This function Should not be modified, when the callback is needed,
  1845. the HAL_CRYP_ErrorCallback could be implemented in the user file
  1846. */
  1847. }
  1848. /**
  1849. * @}
  1850. */
  1851. /* Private functions ---------------------------------------------------------*/
  1852. /** @addtogroup CRYP_Private_Functions
  1853. * @{
  1854. */
  1855. #if defined (CRYP)
  1856. /**
  1857. * @brief Encryption in ECB/CBC Algorithm with DES/TDES standard.
  1858. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1859. * the configuration information for CRYP module
  1860. * @param Timeout: specify Timeout value
  1861. * @retval HAL status
  1862. */
  1863. static HAL_StatusTypeDef CRYP_TDES_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  1864. {
  1865. uint32_t temp[2]; /* Temporary CrypOutBuff */
  1866. uint16_t incount; /* Temporary CrypInCount Value */
  1867. uint16_t outcount; /* Temporary CrypOutCount Value */
  1868. uint32_t i;
  1869. /* Enable CRYP */
  1870. __HAL_CRYP_ENABLE(hcryp);
  1871. /*Temporary CrypOutCount Value*/
  1872. outcount = hcryp->CrypOutCount;
  1873. /*Start processing*/
  1874. while ((hcryp->CrypInCount < (hcryp->Size / 4U)) && (outcount < (hcryp->Size / 4U)))
  1875. {
  1876. /* Temporary CrypInCount Value */
  1877. incount = hcryp->CrypInCount;
  1878. /* Write plain data and get cipher data */
  1879. if (((hcryp->Instance->SR & CRYP_FLAG_IFNF) != 0x0U) && (incount < (hcryp->Size / 4U)))
  1880. {
  1881. /* Write the input block in the IN FIFO */
  1882. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1883. hcryp->CrypInCount++;
  1884. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1885. hcryp->CrypInCount++;
  1886. }
  1887. /* Wait for OFNE flag to be raised */
  1888. if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  1889. {
  1890. /* Disable the CRYP peripheral clock */
  1891. __HAL_CRYP_DISABLE(hcryp);
  1892. /* Change state & errorCode*/
  1893. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1894. hcryp->State = HAL_CRYP_STATE_READY;
  1895. /* Process unlocked */
  1896. __HAL_UNLOCK(hcryp);
  1897. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  1898. /*Call registered error callback*/
  1899. hcryp->ErrorCallback(hcryp);
  1900. #else
  1901. /*Call legacy weak error callback*/
  1902. HAL_CRYP_ErrorCallback(hcryp);
  1903. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  1904. }
  1905. /*Temporary CrypOutCount Value*/
  1906. outcount = hcryp->CrypOutCount;
  1907. if (((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) && (outcount < (hcryp->Size / 4U)))
  1908. {
  1909. /* Read the output block from the Output FIFO and put them in temporary Buffer then get CrypOutBuff from temporary buffer */
  1910. for (i = 0U; i < 2U; i++)
  1911. {
  1912. temp[i] = hcryp->Instance->DOUT;
  1913. }
  1914. i = 0U;
  1915. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 2U))
  1916. {
  1917. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  1918. hcryp->CrypOutCount++;
  1919. i++;
  1920. }
  1921. }
  1922. /*Temporary CrypOutCount Value*/
  1923. outcount = hcryp->CrypOutCount;
  1924. }
  1925. /* Disable CRYP */
  1926. __HAL_CRYP_DISABLE(hcryp);
  1927. /* Change the CRYP state */
  1928. hcryp->State = HAL_CRYP_STATE_READY;
  1929. /* Return function status */
  1930. return HAL_OK;
  1931. }
  1932. /**
  1933. * @brief CRYP block input/output data handling under interruption with DES/TDES standard.
  1934. * @note The function is called under interruption only, once
  1935. * interruptions have been enabled by CRYP_Decrypt_IT() and CRYP_Encrypt_IT().
  1936. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1937. * the configuration information for CRYP module.
  1938. * @retval none
  1939. */
  1940. static void CRYP_TDES_IT(CRYP_HandleTypeDef *hcryp)
  1941. {
  1942. uint32_t temp[2]; /* Temporary CrypOutBuff */
  1943. uint32_t i;
  1944. if (hcryp->State == HAL_CRYP_STATE_BUSY)
  1945. {
  1946. if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI) != 0x0U)
  1947. {
  1948. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_FLAG_INRIS) != 0x0U)
  1949. {
  1950. /* Write input block in the IN FIFO */
  1951. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1952. hcryp->CrypInCount++;
  1953. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1954. hcryp->CrypInCount++;
  1955. if (hcryp->CrypInCount == ((uint16_t)(hcryp->Size) / 4U))
  1956. {
  1957. /* Disable interruption */
  1958. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1959. /* Call the input data transfer complete callback */
  1960. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  1961. /*Call registered Input complete callback*/
  1962. hcryp->InCpltCallback(hcryp);
  1963. #else
  1964. /*Call legacy weak Input complete callback*/
  1965. HAL_CRYP_InCpltCallback(hcryp);
  1966. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  1967. }
  1968. }
  1969. }
  1970. if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI) != 0x0U)
  1971. {
  1972. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_FLAG_OUTRIS) != 0x0U)
  1973. {
  1974. /* Read the output block from the Output FIFO and put them in temporary Buffer then get CrypOutBuff from temporary buffer */
  1975. for (i = 0U; i < 2U; i++)
  1976. {
  1977. temp[i] = hcryp->Instance->DOUT;
  1978. }
  1979. i = 0U;
  1980. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 2U))
  1981. {
  1982. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  1983. hcryp->CrypOutCount++;
  1984. i++;
  1985. }
  1986. if (hcryp->CrypOutCount == ((uint16_t)(hcryp->Size) / 4U))
  1987. {
  1988. /* Disable interruption */
  1989. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1990. /* Disable CRYP */
  1991. __HAL_CRYP_DISABLE(hcryp);
  1992. /* Process unlocked */
  1993. __HAL_UNLOCK(hcryp);
  1994. /* Change the CRYP state */
  1995. hcryp->State = HAL_CRYP_STATE_READY;
  1996. /* Call output transfer complete callback */
  1997. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  1998. /*Call registered Output complete callback*/
  1999. hcryp->OutCpltCallback(hcryp);
  2000. #else
  2001. /*Call legacy weak Output complete callback*/
  2002. HAL_CRYP_OutCpltCallback(hcryp);
  2003. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2004. }
  2005. }
  2006. }
  2007. }
  2008. else
  2009. {
  2010. /* Process unlocked */
  2011. __HAL_UNLOCK(hcryp);
  2012. /* Busy error code field */
  2013. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  2014. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2015. /*Call registered error callback*/
  2016. hcryp->ErrorCallback(hcryp);
  2017. #else
  2018. /*Call legacy weak error callback*/
  2019. HAL_CRYP_ErrorCallback(hcryp);
  2020. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2021. }
  2022. }
  2023. #endif /* CRYP */
  2024. /**
  2025. * @brief Encryption in ECB/CBC & CTR Algorithm with AES Standard
  2026. * @param hcryp: pointer to a CRYP_HandleTypeDef structure
  2027. * @param Timeout: specify Timeout value
  2028. * @retval HAL status
  2029. */
  2030. static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2031. {
  2032. uint16_t outcount; /* Temporary CrypOutCount Value */
  2033. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2034. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2035. {
  2036. if (hcryp->KeyIVConfig == 1U)
  2037. {
  2038. /* If the Key and IV configuration has to be done only once
  2039. and if it has already been done, skip it */
  2040. DoKeyIVConfig = 0U;
  2041. }
  2042. else
  2043. {
  2044. /* If the Key and IV configuration has to be done only once
  2045. and if it has not been done already, do it and set KeyIVConfig
  2046. to keep track it won't have to be done again next time */
  2047. hcryp->KeyIVConfig = 1U;
  2048. }
  2049. }
  2050. if (DoKeyIVConfig == 1U)
  2051. {
  2052. /* Set the Key*/
  2053. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2054. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  2055. {
  2056. /* Set the Initialization Vector*/
  2057. #if defined (AES)
  2058. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2059. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2060. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2061. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2062. #else /* CRYP */
  2063. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  2064. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2065. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2066. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2067. #endif /* End AES or CRYP */
  2068. }
  2069. } /* if (DoKeyIVConfig == 1U) */
  2070. /* Set the phase */
  2071. hcryp->Phase = CRYP_PHASE_PROCESS;
  2072. /* Enable CRYP */
  2073. __HAL_CRYP_ENABLE(hcryp);
  2074. /*Temporary CrypOutCount Value*/
  2075. outcount = hcryp->CrypOutCount;
  2076. while ((hcryp->CrypInCount < (hcryp->Size / 4U)) && (outcount < (hcryp->Size / 4U)))
  2077. {
  2078. /* Write plain Ddta and get cipher data */
  2079. CRYP_AES_ProcessData(hcryp, Timeout);
  2080. /*Temporary CrypOutCount Value*/
  2081. outcount = hcryp->CrypOutCount;
  2082. }
  2083. /* Disable CRYP */
  2084. __HAL_CRYP_DISABLE(hcryp);
  2085. /* Change the CRYP state */
  2086. hcryp->State = HAL_CRYP_STATE_READY;
  2087. /* Return function status */
  2088. return HAL_OK;
  2089. }
  2090. /**
  2091. * @brief Encryption in ECB/CBC & CTR mode with AES Standard using interrupt mode
  2092. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2093. * the configuration information for CRYP module
  2094. * @retval HAL status
  2095. */
  2096. static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp)
  2097. {
  2098. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2099. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2100. {
  2101. if (hcryp->KeyIVConfig == 1U)
  2102. {
  2103. /* If the Key and IV configuration has to be done only once
  2104. and if it has already been done, skip it */
  2105. DoKeyIVConfig = 0U;
  2106. }
  2107. else
  2108. {
  2109. /* If the Key and IV configuration has to be done only once
  2110. and if it has not been done already, do it and set KeyIVConfig
  2111. to keep track it won't have to be done again next time */
  2112. hcryp->KeyIVConfig = 1U;
  2113. }
  2114. }
  2115. if (DoKeyIVConfig == 1U)
  2116. {
  2117. /* Set the Key*/
  2118. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2119. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  2120. {
  2121. /* Set the Initialization Vector*/
  2122. #if defined (AES)
  2123. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2124. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2125. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2126. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2127. #else /* CRYP */
  2128. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  2129. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2130. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2131. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2132. #endif /* End AES or CRYP */
  2133. }
  2134. } /* if (DoKeyIVConfig == 1U) */
  2135. /* Set the phase */
  2136. hcryp->Phase = CRYP_PHASE_PROCESS;
  2137. if (hcryp->Size != 0U)
  2138. {
  2139. #if defined (AES)
  2140. /* Enable computation complete flag and error interrupts */
  2141. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  2142. /* Enable CRYP */
  2143. __HAL_CRYP_ENABLE(hcryp);
  2144. /* Write the input block in the IN FIFO */
  2145. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2146. hcryp->CrypInCount++;
  2147. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2148. hcryp->CrypInCount++;
  2149. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2150. hcryp->CrypInCount++;
  2151. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2152. hcryp->CrypInCount++;
  2153. #else /* CRYP */
  2154. /* Enable interrupts */
  2155. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2156. /* Enable CRYP */
  2157. __HAL_CRYP_ENABLE(hcryp);
  2158. #endif /* End AES or CRYP */
  2159. }
  2160. else
  2161. {
  2162. /* Change the CRYP state */
  2163. hcryp->State = HAL_CRYP_STATE_READY;
  2164. /* Process unlocked */
  2165. __HAL_UNLOCK(hcryp);
  2166. }
  2167. /* Return function status */
  2168. return HAL_OK;
  2169. }
  2170. /**
  2171. * @brief Decryption in ECB/CBC & CTR mode with AES Standard
  2172. * @param hcryp: pointer to a CRYP_HandleTypeDef structure
  2173. * @param Timeout: Specify Timeout value
  2174. * @retval HAL status
  2175. */
  2176. static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2177. {
  2178. uint16_t outcount; /* Temporary CrypOutCount Value */
  2179. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2180. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2181. {
  2182. if (hcryp->KeyIVConfig == 1U)
  2183. {
  2184. /* If the Key and IV configuration has to be done only once
  2185. and if it has already been done, skip it */
  2186. DoKeyIVConfig = 0U;
  2187. }
  2188. else
  2189. {
  2190. /* If the Key and IV configuration has to be done only once
  2191. and if it has not been done already, do it and set KeyIVConfig
  2192. to keep track it won't have to be done again next time */
  2193. hcryp->KeyIVConfig = 1U;
  2194. }
  2195. }
  2196. if (DoKeyIVConfig == 1U)
  2197. {
  2198. /* Key preparation for ECB/CBC */
  2199. if (hcryp->Init.Algorithm != CRYP_AES_CTR)
  2200. {
  2201. #if defined (AES)
  2202. if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 Key preparation*/
  2203. {
  2204. /* Set key preparation for decryption operating mode*/
  2205. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION);
  2206. /* Set the Key*/
  2207. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2208. /* Enable CRYP */
  2209. __HAL_CRYP_ENABLE(hcryp);
  2210. /* Wait for CCF flag to be raised */
  2211. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  2212. {
  2213. /* Disable the CRYP peripheral clock */
  2214. __HAL_CRYP_DISABLE(hcryp);
  2215. /* Change state & error code*/
  2216. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2217. hcryp->State = HAL_CRYP_STATE_READY;
  2218. /* Process unlocked */
  2219. __HAL_UNLOCK(hcryp);
  2220. return HAL_ERROR;
  2221. }
  2222. /* Clear CCF Flag */
  2223. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2224. /* Return to decryption operating mode(Mode 3)*/
  2225. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  2226. }
  2227. else /*Mode 4 : decryption & Key preparation*/
  2228. {
  2229. /* Set the Key*/
  2230. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2231. /* Set decryption & Key preparation operating mode*/
  2232. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT);
  2233. }
  2234. #else /* CRYP */
  2235. /* change ALGOMODE to key preparation for decryption*/
  2236. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY);
  2237. /* Set the Key*/
  2238. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2239. /* Enable CRYP */
  2240. __HAL_CRYP_ENABLE(hcryp);
  2241. /* Wait for BUSY flag to be raised */
  2242. if (CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK)
  2243. {
  2244. /* Disable the CRYP peripheral clock */
  2245. __HAL_CRYP_DISABLE(hcryp);
  2246. /* Change state */
  2247. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2248. hcryp->State = HAL_CRYP_STATE_READY;
  2249. /* Process unlocked */
  2250. __HAL_UNLOCK(hcryp);
  2251. return HAL_ERROR;
  2252. }
  2253. /* Turn back to ALGOMODE of the configuration */
  2254. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm);
  2255. #endif /* End AES or CRYP */
  2256. }
  2257. else /*Algorithm CTR */
  2258. {
  2259. /* Set the Key*/
  2260. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2261. }
  2262. /* Set IV */
  2263. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  2264. {
  2265. /* Set the Initialization Vector*/
  2266. #if defined (AES)
  2267. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2268. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2269. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2270. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2271. #else /* CRYP */
  2272. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  2273. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2274. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2275. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2276. #endif /* End AES or CRYP */
  2277. }
  2278. } /* if (DoKeyIVConfig == 1U) */
  2279. /* Set the phase */
  2280. hcryp->Phase = CRYP_PHASE_PROCESS;
  2281. /* Enable CRYP */
  2282. __HAL_CRYP_ENABLE(hcryp);
  2283. /*Temporary CrypOutCount Value*/
  2284. outcount = hcryp->CrypOutCount;
  2285. while ((hcryp->CrypInCount < (hcryp->Size / 4U)) && (outcount < (hcryp->Size / 4U)))
  2286. {
  2287. /* Write plain data and get cipher data */
  2288. CRYP_AES_ProcessData(hcryp, Timeout);
  2289. /*Temporary CrypOutCount Value*/
  2290. outcount = hcryp->CrypOutCount;
  2291. }
  2292. /* Disable CRYP */
  2293. __HAL_CRYP_DISABLE(hcryp);
  2294. /* Change the CRYP state */
  2295. hcryp->State = HAL_CRYP_STATE_READY;
  2296. /* Return function status */
  2297. return HAL_OK;
  2298. }
  2299. /**
  2300. * @brief Decryption in ECB/CBC & CTR mode with AES Standard using interrupt mode
  2301. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2302. * the configuration information for CRYP module
  2303. * @retval HAL status
  2304. */
  2305. static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp)
  2306. {
  2307. __IO uint32_t count = 0U;
  2308. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2309. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2310. {
  2311. if (hcryp->KeyIVConfig == 1U)
  2312. {
  2313. /* If the Key and IV configuration has to be done only once
  2314. and if it has already been done, skip it */
  2315. DoKeyIVConfig = 0U;
  2316. }
  2317. else
  2318. {
  2319. /* If the Key and IV configuration has to be done only once
  2320. and if it has not been done already, do it and set KeyIVConfig
  2321. to keep track it won't have to be done again next time */
  2322. hcryp->KeyIVConfig = 1U;
  2323. }
  2324. }
  2325. if (DoKeyIVConfig == 1U)
  2326. {
  2327. /* Key preparation for ECB/CBC */
  2328. if (hcryp->Init.Algorithm != CRYP_AES_CTR)
  2329. {
  2330. #if defined (AES)
  2331. if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 Key preparation*/
  2332. {
  2333. /* Set key preparation for decryption operating mode*/
  2334. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION);
  2335. /* Set the Key*/
  2336. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2337. /* Enable CRYP */
  2338. __HAL_CRYP_ENABLE(hcryp);
  2339. /* Wait for CCF flag to be raised */
  2340. count = CRYP_TIMEOUT_KEYPREPARATION;
  2341. do
  2342. {
  2343. count-- ;
  2344. if (count == 0U)
  2345. {
  2346. /* Disable the CRYP peripheral clock */
  2347. __HAL_CRYP_DISABLE(hcryp);
  2348. /* Change state */
  2349. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2350. hcryp->State = HAL_CRYP_STATE_READY;
  2351. /* Process unlocked */
  2352. __HAL_UNLOCK(hcryp);
  2353. return HAL_ERROR;
  2354. }
  2355. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  2356. /* Clear CCF Flag */
  2357. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2358. /* Return to decryption operating mode(Mode 3)*/
  2359. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  2360. }
  2361. else /*Mode 4 : decryption & key preparation*/
  2362. {
  2363. /* Set the Key*/
  2364. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2365. /* Set decryption & key preparation operating mode*/
  2366. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT);
  2367. }
  2368. #else /* CRYP */
  2369. /* change ALGOMODE to key preparation for decryption*/
  2370. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY);
  2371. /* Set the Key*/
  2372. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2373. /* Enable CRYP */
  2374. __HAL_CRYP_ENABLE(hcryp);
  2375. /* Wait for BUSY flag to be raised */
  2376. count = CRYP_TIMEOUT_KEYPREPARATION;
  2377. do
  2378. {
  2379. count-- ;
  2380. if (count == 0U)
  2381. {
  2382. /* Change state */
  2383. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2384. hcryp->State = HAL_CRYP_STATE_READY;
  2385. /* Process unlocked */
  2386. __HAL_UNLOCK(hcryp);
  2387. return HAL_ERROR;
  2388. }
  2389. } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
  2390. /* Turn back to ALGOMODE of the configuration */
  2391. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm);
  2392. #endif /* End AES or CRYP */
  2393. }
  2394. else /*Algorithm CTR */
  2395. {
  2396. /* Set the Key*/
  2397. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2398. }
  2399. /* Set IV */
  2400. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  2401. {
  2402. /* Set the Initialization Vector*/
  2403. #if defined (AES)
  2404. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2405. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2406. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2407. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2408. #else /* CRYP */
  2409. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  2410. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2411. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2412. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2413. #endif /* End AES or CRYP */
  2414. }
  2415. } /* if (DoKeyIVConfig == 1U) */
  2416. /* Set the phase */
  2417. hcryp->Phase = CRYP_PHASE_PROCESS;
  2418. if (hcryp->Size != 0U)
  2419. {
  2420. #if defined (AES)
  2421. /* Enable computation complete flag and error interrupts */
  2422. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  2423. /* Enable CRYP */
  2424. __HAL_CRYP_ENABLE(hcryp);
  2425. /* Write the input block in the IN FIFO */
  2426. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2427. hcryp->CrypInCount++;
  2428. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2429. hcryp->CrypInCount++;
  2430. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2431. hcryp->CrypInCount++;
  2432. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2433. hcryp->CrypInCount++;
  2434. #else /* CRYP */
  2435. /* Enable interrupts */
  2436. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2437. /* Enable CRYP */
  2438. __HAL_CRYP_ENABLE(hcryp);
  2439. #endif /* End AES or CRYP */
  2440. }
  2441. else
  2442. {
  2443. /* Process locked */
  2444. __HAL_UNLOCK(hcryp);
  2445. /* Change the CRYP state */
  2446. hcryp->State = HAL_CRYP_STATE_READY;
  2447. }
  2448. /* Return function status */
  2449. return HAL_OK;
  2450. }
  2451. /**
  2452. * @brief Decryption in ECB/CBC & CTR mode with AES Standard using DMA mode
  2453. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2454. * the configuration information for CRYP module
  2455. * @retval HAL status
  2456. */
  2457. static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp)
  2458. {
  2459. __IO uint32_t count = 0U;
  2460. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2461. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2462. {
  2463. if (hcryp->KeyIVConfig == 1U)
  2464. {
  2465. /* If the Key and IV configuration has to be done only once
  2466. and if it has already been done, skip it */
  2467. DoKeyIVConfig = 0U;
  2468. }
  2469. else
  2470. {
  2471. /* If the Key and IV configuration has to be done only once
  2472. and if it has not been done already, do it and set KeyIVConfig
  2473. to keep track it won't have to be done again next time */
  2474. hcryp->KeyIVConfig = 1U;
  2475. }
  2476. }
  2477. if (DoKeyIVConfig == 1U)
  2478. {
  2479. /* Key preparation for ECB/CBC */
  2480. if (hcryp->Init.Algorithm != CRYP_AES_CTR)
  2481. {
  2482. #if defined (AES)
  2483. if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 key preparation*/
  2484. {
  2485. /* Set key preparation for decryption operating mode*/
  2486. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION);
  2487. /* Set the Key*/
  2488. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2489. /* Enable CRYP */
  2490. __HAL_CRYP_ENABLE(hcryp);
  2491. /* Wait for CCF flag to be raised */
  2492. count = CRYP_TIMEOUT_KEYPREPARATION;
  2493. do
  2494. {
  2495. count-- ;
  2496. if (count == 0U)
  2497. {
  2498. /* Disable the CRYP peripheral clock */
  2499. __HAL_CRYP_DISABLE(hcryp);
  2500. /* Change state */
  2501. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2502. hcryp->State = HAL_CRYP_STATE_READY;
  2503. /* Process unlocked */
  2504. __HAL_UNLOCK(hcryp);
  2505. return HAL_ERROR;
  2506. }
  2507. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  2508. /* Clear CCF Flag */
  2509. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2510. /* Return to decryption operating mode(Mode 3)*/
  2511. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  2512. }
  2513. else /*Mode 4 : decryption & key preparation*/
  2514. {
  2515. /* Set the Key*/
  2516. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2517. /* Set decryption & Key preparation operating mode*/
  2518. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT);
  2519. }
  2520. #else /* CRYP */
  2521. /* change ALGOMODE to key preparation for decryption*/
  2522. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY);
  2523. /* Set the Key*/
  2524. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2525. /* Enable CRYP */
  2526. __HAL_CRYP_ENABLE(hcryp);
  2527. /* Wait for BUSY flag to be raised */
  2528. count = CRYP_TIMEOUT_KEYPREPARATION;
  2529. do
  2530. {
  2531. count-- ;
  2532. if (count == 0U)
  2533. {
  2534. /* Disable the CRYP peripheral clock */
  2535. __HAL_CRYP_DISABLE(hcryp);
  2536. /* Change state */
  2537. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2538. hcryp->State = HAL_CRYP_STATE_READY;
  2539. /* Process unlocked */
  2540. __HAL_UNLOCK(hcryp);
  2541. return HAL_ERROR;
  2542. }
  2543. } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
  2544. /* Turn back to ALGOMODE of the configuration */
  2545. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm);
  2546. #endif /* End AES or CRYP */
  2547. }
  2548. else /*Algorithm CTR */
  2549. {
  2550. /* Set the Key*/
  2551. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2552. }
  2553. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  2554. {
  2555. /* Set the Initialization Vector*/
  2556. #if defined (AES)
  2557. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2558. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2559. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2560. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2561. #else /* CRYP */
  2562. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  2563. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2564. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2565. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2566. #endif /* End AES or CRYP */
  2567. }
  2568. } /* if (DoKeyIVConfig == 1U) */
  2569. /* Set the phase */
  2570. hcryp->Phase = CRYP_PHASE_PROCESS;
  2571. if (hcryp->Size != 0U)
  2572. {
  2573. /* Set the input and output addresses and start DMA transfer */
  2574. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
  2575. }
  2576. else
  2577. {
  2578. /* Process unlocked */
  2579. __HAL_UNLOCK(hcryp);
  2580. /* Change the CRYP state */
  2581. hcryp->State = HAL_CRYP_STATE_READY;
  2582. }
  2583. /* Return function status */
  2584. return HAL_OK;
  2585. }
  2586. /**
  2587. * @brief DMA CRYP input data process complete callback.
  2588. * @param hdma: DMA handle
  2589. * @retval None
  2590. */
  2591. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
  2592. {
  2593. CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2594. /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit
  2595. in the DMACR register */
  2596. #if defined (CRYP)
  2597. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN);
  2598. #else /* AES */
  2599. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  2600. /* TinyAES2, No output on CCM AES, unlock should be done when input data process complete */
  2601. if ((hcryp->Init.Algorithm & CRYP_AES_CCM) == CRYP_AES_CCM)
  2602. {
  2603. /* Clear CCF flag */
  2604. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2605. /* Change the CRYP state to ready */
  2606. hcryp->State = HAL_CRYP_STATE_READY;
  2607. /* Process Unlocked */
  2608. __HAL_UNLOCK(hcryp);
  2609. }
  2610. #endif /* End AES or CRYP */
  2611. /* Call input data transfer complete callback */
  2612. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2613. /*Call registered Input complete callback*/
  2614. hcryp->InCpltCallback(hcryp);
  2615. #else
  2616. /*Call legacy weak Input complete callback*/
  2617. HAL_CRYP_InCpltCallback(hcryp);
  2618. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2619. }
  2620. /**
  2621. * @brief DMA CRYP output data process complete callback.
  2622. * @param hdma: DMA handle
  2623. * @retval None
  2624. */
  2625. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
  2626. {
  2627. CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2628. /* Disable the DMA transfer for output FIFO request by resetting
  2629. the DOEN bit in the DMACR register */
  2630. #if defined (CRYP)
  2631. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN);
  2632. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  2633. if ((hcryp->Init.Algorithm & CRYP_AES_GCM) != CRYP_AES_GCM)
  2634. {
  2635. /* Disable CRYP (not allowed in GCM)*/
  2636. __HAL_CRYP_DISABLE(hcryp);
  2637. }
  2638. #else /*NO GCM CCM */
  2639. /* Disable CRYP */
  2640. __HAL_CRYP_DISABLE(hcryp);
  2641. #endif /* GCM CCM defined*/
  2642. #else /* AES */
  2643. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  2644. /* Clear CCF flag */
  2645. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2646. if ((hcryp->Init.Algorithm & CRYP_AES_GCM_GMAC) != CRYP_AES_GCM_GMAC)
  2647. {
  2648. /* Disable CRYP (not allowed in GCM)*/
  2649. __HAL_CRYP_DISABLE(hcryp);
  2650. }
  2651. #endif /* End AES or CRYP */
  2652. /* Change the CRYP state to ready */
  2653. hcryp->State = HAL_CRYP_STATE_READY;
  2654. /* Process unlocked */
  2655. __HAL_UNLOCK(hcryp);
  2656. /* Call output data transfer complete callback */
  2657. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2658. /*Call registered Output complete callback*/
  2659. hcryp->OutCpltCallback(hcryp);
  2660. #else
  2661. /*Call legacy weak Output complete callback*/
  2662. HAL_CRYP_OutCpltCallback(hcryp);
  2663. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2664. }
  2665. /**
  2666. * @brief DMA CRYP communication error callback.
  2667. * @param hdma: DMA handle
  2668. * @retval None
  2669. */
  2670. static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
  2671. {
  2672. CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2673. /* Change the CRYP peripheral state */
  2674. hcryp->State = HAL_CRYP_STATE_READY;
  2675. /* DMA error code field */
  2676. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2677. #if defined (AES)
  2678. /* Clear CCF flag */
  2679. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2680. #endif /* AES */
  2681. /* Call error callback */
  2682. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2683. /*Call registered error callback*/
  2684. hcryp->ErrorCallback(hcryp);
  2685. #else
  2686. /*Call legacy weak error callback*/
  2687. HAL_CRYP_ErrorCallback(hcryp);
  2688. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2689. }
  2690. /**
  2691. * @brief Set the DMA configuration and start the DMA transfer
  2692. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2693. * the configuration information for CRYP module
  2694. * @param inputaddr: address of the input buffer
  2695. * @param Size: size of the input buffer, must be a multiple of 16.
  2696. * @param outputaddr: address of the output buffer
  2697. * @retval None
  2698. */
  2699. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  2700. {
  2701. /* Set the CRYP DMA transfer complete callback */
  2702. hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
  2703. /* Set the DMA input error callback */
  2704. hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
  2705. /* Set the CRYP DMA transfer complete callback */
  2706. hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
  2707. /* Set the DMA output error callback */
  2708. hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
  2709. #if defined (CRYP)
  2710. /* Enable CRYP */
  2711. __HAL_CRYP_ENABLE(hcryp);
  2712. /* Enable the input DMA Stream */
  2713. if (HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DIN, Size) != HAL_OK)
  2714. {
  2715. /* DMA error code field */
  2716. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2717. /* Call error callback */
  2718. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2719. /*Call registered error callback*/
  2720. hcryp->ErrorCallback(hcryp);
  2721. #else
  2722. /*Call legacy weak error callback*/
  2723. HAL_CRYP_ErrorCallback(hcryp);
  2724. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2725. }
  2726. /* Enable the output DMA Stream */
  2727. if (HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size) != HAL_OK)
  2728. {
  2729. /* DMA error code field */
  2730. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2731. /* Call error callback */
  2732. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2733. /*Call registered error callback*/
  2734. hcryp->ErrorCallback(hcryp);
  2735. #else
  2736. /*Call legacy weak error callback*/
  2737. HAL_CRYP_ErrorCallback(hcryp);
  2738. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2739. }
  2740. /* Enable In/Out DMA request */
  2741. hcryp->Instance->DMACR = CRYP_DMACR_DOEN | CRYP_DMACR_DIEN;
  2742. #else /* AES */
  2743. if (((hcryp->Init.Algorithm & CRYP_AES_GCM_GMAC) != CRYP_AES_GCM_GMAC)
  2744. && ((hcryp->Init.Algorithm & CRYP_AES_CCM) != CRYP_AES_CCM))
  2745. {
  2746. /* Enable CRYP (not allowed in GCM & CCM)*/
  2747. __HAL_CRYP_ENABLE(hcryp);
  2748. }
  2749. /* Enable the DMA input stream */
  2750. if (HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size) != HAL_OK)
  2751. {
  2752. /* DMA error code field */
  2753. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2754. /* Call error callback */
  2755. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2756. /*Call registered error callback*/
  2757. hcryp->ErrorCallback(hcryp);
  2758. #else
  2759. /*Call legacy weak error callback*/
  2760. HAL_CRYP_ErrorCallback(hcryp);
  2761. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2762. }
  2763. /* Enable the DMA output stream */
  2764. if (HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size) != HAL_OK)
  2765. {
  2766. /* DMA error code field */
  2767. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2768. /* Call error callback */
  2769. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2770. /*Call registered error callback*/
  2771. hcryp->ErrorCallback(hcryp);
  2772. #else
  2773. /*Call legacy weak error callback*/
  2774. HAL_CRYP_ErrorCallback(hcryp);
  2775. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2776. }
  2777. /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */
  2778. /* Enable In and Out DMA requests */
  2779. if ((hcryp->Init.Algorithm & CRYP_AES_CCM) == CRYP_AES_CCM)
  2780. {
  2781. /* Enable only In DMA requests for CCM*/
  2782. SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN));
  2783. }
  2784. else
  2785. {
  2786. /* Enable In and Out DMA requests */
  2787. SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN));
  2788. }
  2789. #endif /* End AES or CRYP */
  2790. }
  2791. /**
  2792. * @brief Process Data: Write Input data in polling mode and used in AES functions.
  2793. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2794. * the configuration information for CRYP module
  2795. * @param Timeout: Specify Timeout value
  2796. * @retval None
  2797. */
  2798. static void CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2799. {
  2800. uint32_t temp[4]; /* Temporary CrypOutBuff */
  2801. uint32_t i;
  2802. #if defined (CRYP)
  2803. uint16_t incount; /* Temporary CrypInCount Value */
  2804. uint16_t outcount; /* Temporary CrypOutCount Value */
  2805. #endif
  2806. #if defined (CRYP)
  2807. /*Temporary CrypOutCount Value*/
  2808. incount = hcryp->CrypInCount;
  2809. if (((hcryp->Instance->SR & CRYP_FLAG_IFNF) != 0x0U) && (incount < (hcryp->Size / 4U)))
  2810. {
  2811. /* Write the input block in the IN FIFO */
  2812. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2813. hcryp->CrypInCount++;
  2814. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2815. hcryp->CrypInCount++;
  2816. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2817. hcryp->CrypInCount++;
  2818. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2819. hcryp->CrypInCount++;
  2820. }
  2821. /* Wait for OFNE flag to be raised */
  2822. if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  2823. {
  2824. /* Disable the CRYP peripheral clock */
  2825. __HAL_CRYP_DISABLE(hcryp);
  2826. /* Change state & error code*/
  2827. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2828. hcryp->State = HAL_CRYP_STATE_READY;
  2829. /* Process unlocked */
  2830. __HAL_UNLOCK(hcryp);
  2831. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2832. /*Call registered error callback*/
  2833. hcryp->ErrorCallback(hcryp);
  2834. #else
  2835. /*Call legacy weak error callback*/
  2836. HAL_CRYP_ErrorCallback(hcryp);
  2837. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2838. }
  2839. /*Temporary CrypOutCount Value*/
  2840. outcount = hcryp->CrypOutCount;
  2841. if (((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) && (outcount < (hcryp->Size / 4U)))
  2842. {
  2843. /* Read the output block from the Output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */
  2844. for (i = 0U; i < 4U; i++)
  2845. {
  2846. temp[i] = hcryp->Instance->DOUT;
  2847. }
  2848. i = 0U;
  2849. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U))
  2850. {
  2851. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  2852. hcryp->CrypOutCount++;
  2853. i++;
  2854. }
  2855. }
  2856. #else /* AES */
  2857. /* Write the input block in the IN FIFO */
  2858. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2859. hcryp->CrypInCount++;
  2860. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2861. hcryp->CrypInCount++;
  2862. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2863. hcryp->CrypInCount++;
  2864. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2865. hcryp->CrypInCount++;
  2866. /* Wait for CCF flag to be raised */
  2867. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  2868. {
  2869. /* Disable the CRYP peripheral clock */
  2870. __HAL_CRYP_DISABLE(hcryp);
  2871. /* Change state */
  2872. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2873. hcryp->State = HAL_CRYP_STATE_READY;
  2874. /* Process unlocked */
  2875. __HAL_UNLOCK(hcryp);
  2876. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2877. /*Call registered error callback*/
  2878. hcryp->ErrorCallback(hcryp);
  2879. #else
  2880. /*Call legacy weak error callback*/
  2881. HAL_CRYP_ErrorCallback(hcryp);
  2882. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2883. }
  2884. /* Clear CCF Flag */
  2885. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2886. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer*/
  2887. for (i = 0U; i < 4U; i++)
  2888. {
  2889. temp[i] = hcryp->Instance->DOUTR;
  2890. }
  2891. i = 0U;
  2892. while ((hcryp->CrypOutCount < ((hcryp->Size + 3U) / 4U)) && (i < 4U))
  2893. {
  2894. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  2895. hcryp->CrypOutCount++;
  2896. i++;
  2897. }
  2898. #endif /* End AES or CRYP */
  2899. }
  2900. /**
  2901. * @brief Handle CRYP block input/output data handling under interruption.
  2902. * @note The function is called under interruption only, once
  2903. * interruptions have been enabled by HAL_CRYP_Encrypt_IT or HAL_CRYP_Decrypt_IT.
  2904. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2905. * the configuration information for CRYP module.
  2906. * @retval HAL status
  2907. */
  2908. static void CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
  2909. {
  2910. uint32_t temp[4]; /* Temporary CrypOutBuff */
  2911. uint32_t i;
  2912. #if defined (CRYP)
  2913. uint16_t incount; /* Temporary CrypInCount Value */
  2914. uint16_t outcount; /* Temporary CrypOutCount Value */
  2915. #endif
  2916. if (hcryp->State == HAL_CRYP_STATE_BUSY)
  2917. {
  2918. #if defined (CRYP)
  2919. /*Temporary CrypOutCount Value*/
  2920. incount = hcryp->CrypInCount;
  2921. if (((hcryp->Instance->SR & CRYP_FLAG_IFNF) != 0x0U) && (incount < (hcryp->Size / 4U)))
  2922. {
  2923. /* Write the input block in the IN FIFO */
  2924. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2925. hcryp->CrypInCount++;
  2926. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2927. hcryp->CrypInCount++;
  2928. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2929. hcryp->CrypInCount++;
  2930. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2931. hcryp->CrypInCount++;
  2932. if (hcryp->CrypInCount == ((uint16_t)(hcryp->Size) / 4U))
  2933. {
  2934. /* Disable interrupts */
  2935. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2936. /* Call the input data transfer complete callback */
  2937. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2938. /*Call registered Input complete callback*/
  2939. hcryp->InCpltCallback(hcryp);
  2940. #else
  2941. /*Call legacy weak Input complete callback*/
  2942. HAL_CRYP_InCpltCallback(hcryp);
  2943. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2944. }
  2945. }
  2946. /*Temporary CrypOutCount Value*/
  2947. outcount = hcryp->CrypOutCount;
  2948. if (((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) && (outcount < (hcryp->Size / 4U)))
  2949. {
  2950. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */
  2951. for (i = 0U; i < 4U; i++)
  2952. {
  2953. temp[i] = hcryp->Instance->DOUT;
  2954. }
  2955. i = 0U;
  2956. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U))
  2957. {
  2958. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  2959. hcryp->CrypOutCount++;
  2960. i++;
  2961. }
  2962. if (hcryp->CrypOutCount == ((uint16_t)(hcryp->Size) / 4U))
  2963. {
  2964. /* Disable interrupts */
  2965. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2966. /* Change the CRYP state */
  2967. hcryp->State = HAL_CRYP_STATE_READY;
  2968. /* Disable CRYP */
  2969. __HAL_CRYP_DISABLE(hcryp);
  2970. /* Process unlocked */
  2971. __HAL_UNLOCK(hcryp);
  2972. /* Call Output transfer complete callback */
  2973. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2974. /*Call registered Output complete callback*/
  2975. hcryp->OutCpltCallback(hcryp);
  2976. #else
  2977. /*Call legacy weak Output complete callback*/
  2978. HAL_CRYP_OutCpltCallback(hcryp);
  2979. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2980. }
  2981. }
  2982. #else /*AES*/
  2983. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer*/
  2984. for (i = 0U; i < 4U; i++)
  2985. {
  2986. temp[i] = hcryp->Instance->DOUTR;
  2987. }
  2988. i = 0U;
  2989. while ((hcryp->CrypOutCount < ((hcryp->Size + 3U) / 4U)) && (i < 4U))
  2990. {
  2991. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  2992. hcryp->CrypOutCount++;
  2993. i++;
  2994. }
  2995. if (hcryp->CrypOutCount == (hcryp->Size / 4U))
  2996. {
  2997. /* Disable Computation Complete flag and errors interrupts */
  2998. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  2999. /* Change the CRYP state */
  3000. hcryp->State = HAL_CRYP_STATE_READY;
  3001. /* Disable CRYP */
  3002. __HAL_CRYP_DISABLE(hcryp);
  3003. /* Process Unlocked */
  3004. __HAL_UNLOCK(hcryp);
  3005. /* Call Output transfer complete callback */
  3006. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  3007. /*Call registered Output complete callback*/
  3008. hcryp->OutCpltCallback(hcryp);
  3009. #else
  3010. /*Call legacy weak Output complete callback*/
  3011. HAL_CRYP_OutCpltCallback(hcryp);
  3012. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3013. }
  3014. else
  3015. {
  3016. /* Write the input block in the IN FIFO */
  3017. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3018. hcryp->CrypInCount++;
  3019. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3020. hcryp->CrypInCount++;
  3021. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3022. hcryp->CrypInCount++;
  3023. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3024. hcryp->CrypInCount++;
  3025. if (hcryp->CrypInCount == (hcryp->Size / 4U))
  3026. {
  3027. /* Call Input transfer complete callback */
  3028. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  3029. /*Call registered Input complete callback*/
  3030. hcryp->InCpltCallback(hcryp);
  3031. #else
  3032. /*Call legacy weak Input complete callback*/
  3033. HAL_CRYP_InCpltCallback(hcryp);
  3034. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3035. }
  3036. }
  3037. #endif /* End AES or CRYP */
  3038. }
  3039. else
  3040. {
  3041. /* Busy error code field */
  3042. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  3043. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  3044. /*Call registered error callback*/
  3045. hcryp->ErrorCallback(hcryp);
  3046. #else
  3047. /*Call legacy weak error callback*/
  3048. HAL_CRYP_ErrorCallback(hcryp);
  3049. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3050. }
  3051. }
  3052. /**
  3053. * @brief Writes Key in Key registers.
  3054. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3055. * the configuration information for CRYP module
  3056. * @param KeySize: Size of Key
  3057. * @retval None
  3058. */
  3059. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint32_t KeySize)
  3060. {
  3061. #if defined (CRYP)
  3062. switch (KeySize)
  3063. {
  3064. case CRYP_KEYSIZE_256B:
  3065. hcryp->Instance->K0LR = *(uint32_t *)(hcryp->Init.pKey);
  3066. hcryp->Instance->K0RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  3067. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  3068. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  3069. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  3070. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  3071. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 6);
  3072. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 7);
  3073. break;
  3074. case CRYP_KEYSIZE_192B:
  3075. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  3076. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  3077. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  3078. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  3079. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  3080. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  3081. break;
  3082. case CRYP_KEYSIZE_128B:
  3083. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey);
  3084. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  3085. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  3086. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  3087. break;
  3088. default:
  3089. break;
  3090. }
  3091. #else /*AES*/
  3092. switch (KeySize)
  3093. {
  3094. case CRYP_KEYSIZE_256B:
  3095. hcryp->Instance->KEYR7 = *(uint32_t *)(hcryp->Init.pKey);
  3096. hcryp->Instance->KEYR6 = *(uint32_t *)(hcryp->Init.pKey + 1);
  3097. hcryp->Instance->KEYR5 = *(uint32_t *)(hcryp->Init.pKey + 2);
  3098. hcryp->Instance->KEYR4 = *(uint32_t *)(hcryp->Init.pKey + 3);
  3099. hcryp->Instance->KEYR3 = *(uint32_t *)(hcryp->Init.pKey + 4);
  3100. hcryp->Instance->KEYR2 = *(uint32_t *)(hcryp->Init.pKey + 5);
  3101. hcryp->Instance->KEYR1 = *(uint32_t *)(hcryp->Init.pKey + 6);
  3102. hcryp->Instance->KEYR0 = *(uint32_t *)(hcryp->Init.pKey + 7);
  3103. break;
  3104. case CRYP_KEYSIZE_128B:
  3105. hcryp->Instance->KEYR3 = *(uint32_t *)(hcryp->Init.pKey);
  3106. hcryp->Instance->KEYR2 = *(uint32_t *)(hcryp->Init.pKey + 1);
  3107. hcryp->Instance->KEYR1 = *(uint32_t *)(hcryp->Init.pKey + 2);
  3108. hcryp->Instance->KEYR0 = *(uint32_t *)(hcryp->Init.pKey + 3);
  3109. break;
  3110. default:
  3111. break;
  3112. }
  3113. #endif /* End AES or CRYP */
  3114. }
  3115. #if defined (CRYP_CR_ALGOMODE_AES_GCM)|| defined (AES)
  3116. /**
  3117. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG
  3118. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3119. * the configuration information for CRYP module
  3120. * @param Timeout: Timeout duration
  3121. * @retval HAL status
  3122. */
  3123. static HAL_StatusTypeDef CRYP_AESGCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  3124. {
  3125. uint32_t tickstart;
  3126. uint32_t wordsize = (uint32_t)(hcryp->Size) / 4U ;
  3127. uint16_t outcount; /* Temporary CrypOutCount Value */
  3128. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3129. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3130. {
  3131. if (hcryp->KeyIVConfig == 1U)
  3132. {
  3133. /* If the Key and IV configuration has to be done only once
  3134. and if it has already been done, skip it */
  3135. DoKeyIVConfig = 0U;
  3136. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3137. }
  3138. else
  3139. {
  3140. /* If the Key and IV configuration has to be done only once
  3141. and if it has not been done already, do it and set KeyIVConfig
  3142. to keep track it won't have to be done again next time */
  3143. hcryp->KeyIVConfig = 1U;
  3144. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3145. }
  3146. }
  3147. else
  3148. {
  3149. hcryp->SizesSum = hcryp->Size;
  3150. }
  3151. if (DoKeyIVConfig == 1U)
  3152. {
  3153. /* Reset CrypHeaderCount */
  3154. hcryp->CrypHeaderCount = 0U;
  3155. /****************************** Init phase **********************************/
  3156. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3157. /* Set the key */
  3158. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3159. #if defined(CRYP)
  3160. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3161. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  3162. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3163. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3164. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3165. /* Enable the CRYP peripheral */
  3166. __HAL_CRYP_ENABLE(hcryp);
  3167. /* Get tick */
  3168. tickstart = HAL_GetTick();
  3169. /*Wait for the CRYPEN bit to be cleared*/
  3170. while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  3171. {
  3172. /* Check for the Timeout */
  3173. if (Timeout != HAL_MAX_DELAY)
  3174. {
  3175. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  3176. {
  3177. /* Disable the CRYP peripheral clock */
  3178. __HAL_CRYP_DISABLE(hcryp);
  3179. /* Change state */
  3180. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3181. hcryp->State = HAL_CRYP_STATE_READY;
  3182. /* Process unlocked */
  3183. __HAL_UNLOCK(hcryp);
  3184. return HAL_ERROR;
  3185. }
  3186. }
  3187. }
  3188. #else /* AES */
  3189. /* Workaround 1 : only AES.
  3190. Datatype configuration must be 32 bits during Init phase. Only, after Init, and before re
  3191. enabling the IP, datatype different from 32 bits can be configured.*/
  3192. /* Select DATATYPE 32 */
  3193. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, CRYP_DATATYPE_32B);
  3194. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3195. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  3196. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3197. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3198. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3199. /* Enable the CRYP peripheral */
  3200. __HAL_CRYP_ENABLE(hcryp);
  3201. /* just wait for hash computation */
  3202. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3203. {
  3204. /* Change state */
  3205. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3206. hcryp->State = HAL_CRYP_STATE_READY;
  3207. /* Process unlocked & return error */
  3208. __HAL_UNLOCK(hcryp);
  3209. return HAL_ERROR;
  3210. }
  3211. /* Clear CCF flag */
  3212. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3213. #endif /* End AES or CRYP */
  3214. /************************ Header phase *************************************/
  3215. if (CRYP_GCMCCM_SetHeaderPhase(hcryp, Timeout) != HAL_OK)
  3216. {
  3217. return HAL_ERROR;
  3218. }
  3219. /*************************Payload phase ************************************/
  3220. /* Set the phase */
  3221. hcryp->Phase = CRYP_PHASE_PROCESS;
  3222. #if defined(CRYP)
  3223. /* Disable the CRYP peripheral */
  3224. __HAL_CRYP_DISABLE(hcryp);
  3225. /* Select payload phase once the header phase is performed */
  3226. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  3227. /* Enable the CRYP peripheral */
  3228. __HAL_CRYP_ENABLE(hcryp);
  3229. #else /* AES */
  3230. /* Select payload phase once the header phase is performed */
  3231. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  3232. #endif /* End AES or CRYP */
  3233. } /* if (DoKeyIVConfig == 1U) */
  3234. if ((hcryp->Size % 16U) != 0U)
  3235. {
  3236. /* recalculate wordsize */
  3237. wordsize = ((wordsize / 4U) * 4U) ;
  3238. }
  3239. /* Get tick */
  3240. tickstart = HAL_GetTick();
  3241. /*Temporary CrypOutCount Value*/
  3242. outcount = hcryp->CrypOutCount;
  3243. /* Write input data and get output Data */
  3244. while ((hcryp->CrypInCount < wordsize) && (outcount < wordsize))
  3245. {
  3246. /* Write plain data and get cipher data */
  3247. CRYP_AES_ProcessData(hcryp, Timeout);
  3248. /*Temporary CrypOutCount Value*/
  3249. outcount = hcryp->CrypOutCount;
  3250. /* Check for the Timeout */
  3251. if (Timeout != HAL_MAX_DELAY)
  3252. {
  3253. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  3254. {
  3255. /* Disable the CRYP peripheral clock */
  3256. __HAL_CRYP_DISABLE(hcryp);
  3257. /* Change state & error code */
  3258. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3259. hcryp->State = HAL_CRYP_STATE_READY;
  3260. /* Process unlocked */
  3261. __HAL_UNLOCK(hcryp);
  3262. return HAL_ERROR;
  3263. }
  3264. }
  3265. }
  3266. if ((hcryp->Size % 16U) != 0U)
  3267. {
  3268. /* Workaround 2 : CRYP1 & AES generates correct TAG for GCM mode only when input block size is multiple of
  3269. 128 bits. If lthe size of the last block of payload is inferior to 128 bits, when GCM encryption
  3270. is selected, then the TAG message will be wrong.*/
  3271. CRYP_Workaround(hcryp, Timeout);
  3272. }
  3273. /* Return function status */
  3274. return HAL_OK;
  3275. }
  3276. /**
  3277. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG in interrupt mode
  3278. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3279. * the configuration information for CRYP module
  3280. * @retval HAL status
  3281. */
  3282. static HAL_StatusTypeDef CRYP_AESGCM_Process_IT(CRYP_HandleTypeDef *hcryp)
  3283. {
  3284. __IO uint32_t count = 0U;
  3285. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3286. #if defined(AES)
  3287. uint32_t loopcounter;
  3288. uint32_t lastwordsize;
  3289. uint32_t npblb;
  3290. #endif /* AES */
  3291. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3292. {
  3293. if (hcryp->KeyIVConfig == 1U)
  3294. {
  3295. /* If the Key and IV configuration has to be done only once
  3296. and if it has already been done, skip it */
  3297. DoKeyIVConfig = 0U;
  3298. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3299. }
  3300. else
  3301. {
  3302. /* If the Key and IV configuration has to be done only once
  3303. and if it has not been done already, do it and set KeyIVConfig
  3304. to keep track it won't have to be done again next time */
  3305. hcryp->KeyIVConfig = 1U;
  3306. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3307. }
  3308. }
  3309. else
  3310. {
  3311. hcryp->SizesSum = hcryp->Size;
  3312. }
  3313. /* Configure Key, IV and process message (header and payload) */
  3314. if (DoKeyIVConfig == 1U)
  3315. {
  3316. /* Reset CrypHeaderCount */
  3317. hcryp->CrypHeaderCount = 0U;
  3318. /******************************* Init phase *********************************/
  3319. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3320. /* Set the key */
  3321. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3322. #if defined(CRYP)
  3323. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3324. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  3325. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3326. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3327. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3328. /* Enable the CRYP peripheral */
  3329. __HAL_CRYP_ENABLE(hcryp);
  3330. /*Wait for the CRYPEN bit to be cleared*/
  3331. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  3332. do
  3333. {
  3334. count-- ;
  3335. if (count == 0U)
  3336. {
  3337. /* Disable the CRYP peripheral clock */
  3338. __HAL_CRYP_DISABLE(hcryp);
  3339. /* Change state */
  3340. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3341. hcryp->State = HAL_CRYP_STATE_READY;
  3342. /* Process unlocked */
  3343. __HAL_UNLOCK(hcryp);
  3344. return HAL_ERROR;
  3345. }
  3346. } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  3347. #else /* AES */
  3348. /* Workaround 1 : only AES
  3349. Datatype configuration must be 32 bits during INIT phase. Only, after INIT, and before re
  3350. enabling the IP, datatype different from 32 bits can be configured.*/
  3351. /* Select DATATYPE 32 */
  3352. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, CRYP_DATATYPE_32B);
  3353. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3354. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  3355. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3356. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3357. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3358. /* Enable the CRYP peripheral */
  3359. __HAL_CRYP_ENABLE(hcryp);
  3360. /* just wait for hash computation */
  3361. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  3362. do
  3363. {
  3364. count-- ;
  3365. if (count == 0U)
  3366. {
  3367. /* Disable the CRYP peripheral clock */
  3368. __HAL_CRYP_DISABLE(hcryp);
  3369. /* Change state */
  3370. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3371. hcryp->State = HAL_CRYP_STATE_READY;
  3372. /* Process unlocked */
  3373. __HAL_UNLOCK(hcryp);
  3374. return HAL_ERROR;
  3375. }
  3376. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  3377. /* Clear CCF flag */
  3378. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3379. #endif /* End AES or CRYP */
  3380. /***************************** Header phase *********************************/
  3381. #if defined(CRYP)
  3382. /* Select header phase */
  3383. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  3384. /* Enable interrupts */
  3385. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI);
  3386. /* Enable CRYP */
  3387. __HAL_CRYP_ENABLE(hcryp);
  3388. #else /* AES */
  3389. /* Workaround 1: only AES , before re-enabling the IP, datatype can be configured*/
  3390. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  3391. /* Select header phase */
  3392. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  3393. /* Enable computation complete flag and error interrupts */
  3394. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  3395. /* Enable the CRYP peripheral */
  3396. __HAL_CRYP_ENABLE(hcryp);
  3397. if (hcryp->Init.HeaderSize == 0U) /*header phase is skipped*/
  3398. {
  3399. /* Set the phase */
  3400. hcryp->Phase = CRYP_PHASE_PROCESS;
  3401. /* Select payload phase once the header phase is performed */
  3402. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_PAYLOAD);
  3403. /* Write the payload Input block in the IN FIFO */
  3404. if (hcryp->Size == 0U)
  3405. {
  3406. /* Disable interrupts */
  3407. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  3408. /* Change the CRYP state */
  3409. hcryp->State = HAL_CRYP_STATE_READY;
  3410. /* Process unlocked */
  3411. __HAL_UNLOCK(hcryp);
  3412. }
  3413. else if (hcryp->Size >= 16U)
  3414. {
  3415. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3416. hcryp->CrypInCount++;
  3417. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3418. hcryp->CrypInCount++;
  3419. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3420. hcryp->CrypInCount++;
  3421. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3422. hcryp->CrypInCount++;
  3423. if (hcryp->CrypInCount == (hcryp->Size / 4U))
  3424. {
  3425. /* Call Input transfer complete callback */
  3426. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  3427. /*Call registered Input complete callback*/
  3428. hcryp->InCpltCallback(hcryp);
  3429. #else
  3430. /*Call legacy weak Input complete callback*/
  3431. HAL_CRYP_InCpltCallback(hcryp);
  3432. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3433. }
  3434. }
  3435. else /* Size < 16Bytes : first block is the last block*/
  3436. {
  3437. /* Workaround not implemented*/
  3438. /* Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption:
  3439. Workaround is implemented in polling mode, so if last block of
  3440. payload <128bit don't use CRYP_Encrypt_IT otherwise TAG is incorrectly generated for GCM Encryption. */
  3441. /* Compute the number of padding bytes in last block of payload */
  3442. npblb = 16U - (uint32_t)(hcryp->Size);
  3443. /* Number of valid words (lastwordsize) in last block */
  3444. if ((npblb % 4U) == 0U)
  3445. {
  3446. lastwordsize = (16U - npblb) / 4U;
  3447. }
  3448. else
  3449. {
  3450. lastwordsize = ((16U - npblb) / 4U) + 1U;
  3451. }
  3452. /* last block optionally pad the data with zeros*/
  3453. for (loopcounter = 0U; loopcounter < lastwordsize ; loopcounter++)
  3454. {
  3455. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3456. hcryp->CrypInCount++;
  3457. }
  3458. while (loopcounter < 4U)
  3459. {
  3460. /* pad the data with zeros to have a complete block */
  3461. hcryp->Instance->DINR = 0x0U;
  3462. loopcounter++;
  3463. }
  3464. }
  3465. }
  3466. else if ((hcryp->Init.HeaderSize) < 4U)
  3467. {
  3468. for (loopcounter = 0U; loopcounter < hcryp->Init.HeaderSize ; loopcounter++)
  3469. {
  3470. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3471. hcryp->CrypHeaderCount++ ;
  3472. }
  3473. while (loopcounter < 4U)
  3474. {
  3475. /* pad the data with zeros to have a complete block */
  3476. hcryp->Instance->DINR = 0x0U;
  3477. loopcounter++;
  3478. }
  3479. /* Set the phase */
  3480. hcryp->Phase = CRYP_PHASE_PROCESS;
  3481. /* Select payload phase once the header phase is performed */
  3482. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  3483. /* Call Input transfer complete callback */
  3484. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  3485. /*Call registered Input complete callback*/
  3486. hcryp->InCpltCallback(hcryp);
  3487. #else
  3488. /*Call legacy weak Input complete callback*/
  3489. HAL_CRYP_InCpltCallback(hcryp);
  3490. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3491. }
  3492. else if ((hcryp->Init.HeaderSize) >= 4U)
  3493. {
  3494. /* Write the input block in the IN FIFO */
  3495. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3496. hcryp->CrypHeaderCount++;
  3497. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3498. hcryp->CrypHeaderCount++;
  3499. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3500. hcryp->CrypHeaderCount++;
  3501. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3502. hcryp->CrypHeaderCount++;
  3503. }
  3504. else
  3505. {
  3506. /* Nothing to do */
  3507. }
  3508. #endif /* End AES or CRYP */
  3509. } /* end of if (DoKeyIVConfig == 1U) */
  3510. /* Return function status */
  3511. return HAL_OK;
  3512. }
  3513. /**
  3514. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG using DMA
  3515. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3516. * the configuration information for CRYP module
  3517. * @retval HAL status
  3518. */
  3519. static HAL_StatusTypeDef CRYP_AESGCM_Process_DMA(CRYP_HandleTypeDef *hcryp)
  3520. {
  3521. __IO uint32_t count = 0U;
  3522. uint32_t wordsize;
  3523. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3524. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3525. {
  3526. if (hcryp->KeyIVConfig == 1U)
  3527. {
  3528. /* If the Key and IV configuration has to be done only once
  3529. and if it has already been done, skip it */
  3530. DoKeyIVConfig = 0U;
  3531. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3532. }
  3533. else
  3534. {
  3535. /* If the Key and IV configuration has to be done only once
  3536. and if it has not been done already, do it and set KeyIVConfig
  3537. to keep track it won't have to be done again next time */
  3538. hcryp->KeyIVConfig = 1U;
  3539. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3540. }
  3541. }
  3542. else
  3543. {
  3544. hcryp->SizesSum = hcryp->Size;
  3545. }
  3546. if (DoKeyIVConfig == 1U)
  3547. {
  3548. /* Reset CrypHeaderCount */
  3549. hcryp->CrypHeaderCount = 0U;
  3550. /*************************** Init phase ************************************/
  3551. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3552. /* Set the key */
  3553. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3554. #if defined(CRYP)
  3555. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3556. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  3557. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3558. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3559. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3560. /* Enable the CRYP peripheral */
  3561. __HAL_CRYP_ENABLE(hcryp);
  3562. /*Wait for the CRYPEN bit to be cleared*/
  3563. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  3564. do
  3565. {
  3566. count-- ;
  3567. if (count == 0U)
  3568. {
  3569. /* Disable the CRYP peripheral clock */
  3570. __HAL_CRYP_DISABLE(hcryp);
  3571. /* Change state */
  3572. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3573. hcryp->State = HAL_CRYP_STATE_READY;
  3574. /* Process unlocked */
  3575. __HAL_UNLOCK(hcryp);
  3576. return HAL_ERROR;
  3577. }
  3578. } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  3579. #else /* AES */
  3580. /*Workaround 1 : only AES
  3581. Datatype configuration must be 32 bits during Init phase. Only, after Init, and before re
  3582. enabling the IP, datatype different from 32 bits can be configured.*/
  3583. /* Select DATATYPE 32 */
  3584. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, CRYP_DATATYPE_32B);
  3585. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3586. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  3587. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3588. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3589. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3590. /* Enable the CRYP peripheral */
  3591. __HAL_CRYP_ENABLE(hcryp);
  3592. /* just wait for hash computation */
  3593. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  3594. do
  3595. {
  3596. count-- ;
  3597. if (count == 0U)
  3598. {
  3599. /* Disable the CRYP peripheral clock */
  3600. __HAL_CRYP_DISABLE(hcryp);
  3601. /* Change state */
  3602. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3603. hcryp->State = HAL_CRYP_STATE_READY;
  3604. /* Process unlocked */
  3605. __HAL_UNLOCK(hcryp);
  3606. return HAL_ERROR;
  3607. }
  3608. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  3609. /* Clear CCF flag */
  3610. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3611. #endif /* End AES or CRYP */
  3612. /************************ Header phase *************************************/
  3613. if (CRYP_GCMCCM_SetHeaderPhase_DMA(hcryp) != HAL_OK)
  3614. {
  3615. return HAL_ERROR;
  3616. }
  3617. /************************ Payload phase ************************************/
  3618. /* Set the phase */
  3619. hcryp->Phase = CRYP_PHASE_PROCESS;
  3620. #if defined(CRYP)
  3621. /* Disable the CRYP peripheral */
  3622. __HAL_CRYP_DISABLE(hcryp);
  3623. #endif /* CRYP */
  3624. /* Select payload phase once the header phase is performed */
  3625. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  3626. } /* if (DoKeyIVConfig == 1U) */
  3627. if (hcryp->Size != 0U)
  3628. {
  3629. /* CRYP1 IP V < 2.2.1 Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption:
  3630. Workaround is implemented in polling mode, so if last block of
  3631. payload <128bit don't use DMA mode otherwise TAG is incorrectly generated . */
  3632. /* Set the input and output addresses and start DMA transfer */
  3633. if ((hcryp->Size % 16U) == 0U)
  3634. {
  3635. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
  3636. }
  3637. else /*to compute last word<128bits, otherwise it will not be encrypted/decrypted */
  3638. {
  3639. wordsize = (uint32_t)(hcryp->Size) + (16U - ((uint32_t)(hcryp->Size) % 16U)) ;
  3640. /* Set the input and output addresses and start DMA transfer, pCrypOutBuffPtr size should be %4 */
  3641. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)wordsize / 4U),
  3642. (uint32_t)(hcryp->pCrypOutBuffPtr));
  3643. }
  3644. }
  3645. else
  3646. {
  3647. /* Process unLocked */
  3648. __HAL_UNLOCK(hcryp);
  3649. /* Change the CRYP state and phase */
  3650. hcryp->State = HAL_CRYP_STATE_READY;
  3651. }
  3652. /* Return function status */
  3653. return HAL_OK;
  3654. }
  3655. /**
  3656. * @brief AES CCM encryption/decryption processing in polling mode
  3657. * for TinyAES IP, no encrypt/decrypt performed, only authentication preparation.
  3658. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3659. * the configuration information for CRYP module
  3660. * @param Timeout: Timeout duration
  3661. * @retval HAL status
  3662. */
  3663. static HAL_StatusTypeDef CRYP_AESCCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  3664. {
  3665. uint32_t tickstart;
  3666. uint32_t wordsize = (uint32_t)(hcryp->Size) / 4U;
  3667. uint16_t outcount; /* Temporary CrypOutCount Value */
  3668. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3669. #if defined(AES)
  3670. uint32_t loopcounter;
  3671. uint32_t npblb;
  3672. uint32_t lastwordsize;
  3673. #endif /* AES */
  3674. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3675. {
  3676. if (hcryp->KeyIVConfig == 1U)
  3677. {
  3678. /* If the Key and IV configuration has to be done only once
  3679. and if it has already been done, skip it */
  3680. DoKeyIVConfig = 0U;
  3681. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3682. }
  3683. else
  3684. {
  3685. /* If the Key and IV configuration has to be done only once
  3686. and if it has not been done already, do it and set KeyIVConfig
  3687. to keep track it won't have to be done again next time */
  3688. hcryp->KeyIVConfig = 1U;
  3689. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3690. }
  3691. }
  3692. else
  3693. {
  3694. hcryp->SizesSum = hcryp->Size;
  3695. }
  3696. if (DoKeyIVConfig == 1U)
  3697. {
  3698. /* Reset CrypHeaderCount */
  3699. hcryp->CrypHeaderCount = 0U;
  3700. #if defined(CRYP)
  3701. /********************** Init phase ******************************************/
  3702. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3703. /* Set the key */
  3704. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3705. /* Set the initialization vector (IV) with CTR1 information */
  3706. hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0;
  3707. hcryp->Instance->IV0RR = hcryp->Init.B0[1];
  3708. hcryp->Instance->IV1LR = hcryp->Init.B0[2];
  3709. hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1) | CRYP_CCM_CTR1_2;
  3710. /* Enable the CRYP peripheral */
  3711. __HAL_CRYP_ENABLE(hcryp);
  3712. /*Write B0 packet into CRYP_DIN Register*/
  3713. if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
  3714. {
  3715. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0));
  3716. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 1));
  3717. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 2));
  3718. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 3));
  3719. }
  3720. else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
  3721. {
  3722. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0), 16);
  3723. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 1), 16);
  3724. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 2), 16);
  3725. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 3), 16);
  3726. }
  3727. else if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
  3728. {
  3729. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0));
  3730. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 1));
  3731. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 2));
  3732. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 3));
  3733. }
  3734. else
  3735. {
  3736. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0);
  3737. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 1);
  3738. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 2);
  3739. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 3);
  3740. }
  3741. /* Get tick */
  3742. tickstart = HAL_GetTick();
  3743. /*Wait for the CRYPEN bit to be cleared*/
  3744. while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  3745. {
  3746. /* Check for the Timeout */
  3747. if (Timeout != HAL_MAX_DELAY)
  3748. {
  3749. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  3750. {
  3751. /* Disable the CRYP peripheral clock */
  3752. __HAL_CRYP_DISABLE(hcryp);
  3753. /* Change state */
  3754. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3755. hcryp->State = HAL_CRYP_STATE_READY;
  3756. /* Process unlocked */
  3757. __HAL_UNLOCK(hcryp);
  3758. return HAL_ERROR;
  3759. }
  3760. }
  3761. }
  3762. #else /* AES */
  3763. /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */
  3764. /* Select header phase */
  3765. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  3766. /* configured encryption mode */
  3767. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  3768. /* Set the key */
  3769. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3770. /* Set the initialization vector with zero values*/
  3771. hcryp->Instance->IVR3 = 0U;
  3772. hcryp->Instance->IVR2 = 0U;
  3773. hcryp->Instance->IVR1 = 0U;
  3774. hcryp->Instance->IVR0 = 0U;
  3775. /* Enable the CRYP peripheral */
  3776. __HAL_CRYP_ENABLE(hcryp);
  3777. /*Write the B0 packet into CRYP_DIN*/
  3778. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0);
  3779. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 1);
  3780. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 2);
  3781. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 3);
  3782. /* wait until the end of computation */
  3783. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3784. {
  3785. /* Change state */
  3786. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3787. hcryp->State = HAL_CRYP_STATE_READY;
  3788. /* Process unlocked & return error */
  3789. __HAL_UNLOCK(hcryp);
  3790. return HAL_ERROR;
  3791. }
  3792. /* Clear CCF flag */
  3793. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3794. /* Set the phase */
  3795. hcryp->Phase = CRYP_PHASE_PROCESS;
  3796. /* From that point the whole message must be processed, first the Header then the payload.
  3797. First the Header block(B1) : associated data length expressed in bytes concatenated with Associated Data (A)*/
  3798. if (hcryp->Init.HeaderSize != 0U)
  3799. {
  3800. if ((hcryp->Init.HeaderSize % 4U) == 0U)
  3801. {
  3802. /* HeaderSize %4, no padding */
  3803. for (loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter += 4U)
  3804. {
  3805. /* Write the Input block in the Data Input register */
  3806. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3807. hcryp->CrypHeaderCount++ ;
  3808. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3809. hcryp->CrypHeaderCount++ ;
  3810. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3811. hcryp->CrypHeaderCount++ ;
  3812. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3813. hcryp->CrypHeaderCount++ ;
  3814. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3815. {
  3816. /* Disable the CRYP peripheral clock */
  3817. __HAL_CRYP_DISABLE(hcryp);
  3818. /* Change state */
  3819. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3820. hcryp->State = HAL_CRYP_STATE_READY;
  3821. /* Process unlocked */
  3822. __HAL_UNLOCK(hcryp);
  3823. return HAL_ERROR;
  3824. }
  3825. /* Clear CCF Flag */
  3826. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3827. }
  3828. }
  3829. else
  3830. {
  3831. /*Write Header block in the IN FIFO without last block */
  3832. for (loopcounter = 0U; (loopcounter < ((hcryp->Init.HeaderSize) - (hcryp->Init.HeaderSize % 4U))); loopcounter += 4U)
  3833. {
  3834. /* Write the input block in the data input register */
  3835. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3836. hcryp->CrypHeaderCount++ ;
  3837. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3838. hcryp->CrypHeaderCount++ ;
  3839. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3840. hcryp->CrypHeaderCount++ ;
  3841. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3842. hcryp->CrypHeaderCount++ ;
  3843. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3844. {
  3845. /* Disable the CRYP peripheral clock */
  3846. __HAL_CRYP_DISABLE(hcryp);
  3847. /* Change state */
  3848. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3849. hcryp->State = HAL_CRYP_STATE_READY;
  3850. /* Process unlocked */
  3851. __HAL_UNLOCK(hcryp);
  3852. return HAL_ERROR;
  3853. }
  3854. /* Clear CCF Flag */
  3855. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3856. }
  3857. /* Last block optionally pad the data with zeros*/
  3858. for (loopcounter = 0U; (loopcounter < (hcryp->Init.HeaderSize % 4U)); loopcounter++)
  3859. {
  3860. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3861. hcryp->CrypHeaderCount++ ;
  3862. }
  3863. while (loopcounter < 4U)
  3864. {
  3865. /* Pad the data with zeros to have a complete block */
  3866. hcryp->Instance->DINR = 0x0U;
  3867. loopcounter++;
  3868. }
  3869. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3870. {
  3871. /* Disable the CRYP peripheral clock */
  3872. __HAL_CRYP_DISABLE(hcryp);
  3873. /* Change state */
  3874. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3875. hcryp->State = HAL_CRYP_STATE_READY;
  3876. /* Process unlocked */
  3877. __HAL_UNLOCK(hcryp);
  3878. return HAL_ERROR;
  3879. }
  3880. /* Clear CCF flag */
  3881. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3882. }
  3883. }
  3884. } /* if (DoKeyIVConfig == 1U) */
  3885. /* Then the payload: cleartext payload (not the ciphertext payload).
  3886. Write input Data, no output Data to get */
  3887. if (hcryp->Size != 0U)
  3888. {
  3889. if ((hcryp->Size % 16U) != 0U)
  3890. {
  3891. /* recalculate wordsize */
  3892. wordsize = ((wordsize / 4U) * 4U) ;
  3893. }
  3894. /* Get tick */
  3895. tickstart = HAL_GetTick();
  3896. /*Temporary CrypOutCount Value*/
  3897. outcount = hcryp->CrypOutCount;
  3898. while ((hcryp->CrypInCount < wordsize) && (outcount < wordsize))
  3899. {
  3900. /* Write plain data and get cipher data */
  3901. CRYP_AES_ProcessData(hcryp, Timeout);
  3902. /*Temporary CrypOutCount Value*/
  3903. outcount = hcryp->CrypOutCount;
  3904. /* Check for the Timeout */
  3905. if (Timeout != HAL_MAX_DELAY)
  3906. {
  3907. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  3908. {
  3909. /* Disable the CRYP peripheral clock */
  3910. __HAL_CRYP_DISABLE(hcryp);
  3911. /* Change state */
  3912. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3913. hcryp->State = HAL_CRYP_STATE_READY;
  3914. /* Process unlocked */
  3915. __HAL_UNLOCK(hcryp);
  3916. return HAL_ERROR;
  3917. }
  3918. }
  3919. }
  3920. if ((hcryp->Size % 16U) != 0U)
  3921. {
  3922. /* Compute the number of padding bytes in last block of payload */
  3923. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  3924. /* Number of valid words (lastwordsize) in last block */
  3925. if ((npblb % 4U) == 0U)
  3926. {
  3927. lastwordsize = (16U - npblb) / 4U;
  3928. }
  3929. else
  3930. {
  3931. lastwordsize = ((16U - npblb) / 4U) + 1U;
  3932. }
  3933. /* Last block optionally pad the data with zeros*/
  3934. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter ++)
  3935. {
  3936. /* Write the last input block in the IN FIFO */
  3937. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3938. hcryp->CrypInCount++;
  3939. }
  3940. while (loopcounter < 4U)
  3941. {
  3942. /* Pad the data with zeros to have a complete block */
  3943. hcryp->Instance->DINR = 0U;
  3944. loopcounter++;
  3945. }
  3946. /* Wait for CCF flag to be raised */
  3947. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3948. {
  3949. /* Disable the CRYP peripheral clock */
  3950. __HAL_CRYP_DISABLE(hcryp);
  3951. /* Change state */
  3952. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3953. hcryp->State = HAL_CRYP_STATE_READY;
  3954. /* Process unlocked */
  3955. __HAL_UNLOCK(hcryp);
  3956. return HAL_ERROR;
  3957. }
  3958. /* Clear CCF flag */
  3959. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3960. }
  3961. }
  3962. #endif /* End AES or CRYP */
  3963. #if defined(CRYP)
  3964. /************************* Header phase *************************************/
  3965. /* Header block(B1) : associated data length expressed in bytes concatenated
  3966. with Associated Data (A)*/
  3967. if (CRYP_GCMCCM_SetHeaderPhase(hcryp, Timeout) != HAL_OK)
  3968. {
  3969. return HAL_ERROR;
  3970. }
  3971. /********************** Payload phase ***************************************/
  3972. /* Set the phase */
  3973. hcryp->Phase = CRYP_PHASE_PROCESS;
  3974. /* Disable the CRYP peripheral */
  3975. __HAL_CRYP_DISABLE(hcryp);
  3976. /* Select payload phase once the header phase is performed */
  3977. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  3978. /* Enable the CRYP peripheral */
  3979. __HAL_CRYP_ENABLE(hcryp);
  3980. } /* if (DoKeyIVConfig == 1U) */
  3981. if ((hcryp->Size % 16U) != 0U)
  3982. {
  3983. /* recalculate wordsize */
  3984. wordsize = ((wordsize / 4U) * 4U) ;
  3985. }
  3986. /* Get tick */
  3987. tickstart = HAL_GetTick();
  3988. /*Temporary CrypOutCount Value*/
  3989. outcount = hcryp->CrypOutCount;
  3990. /* Write input data and get output data */
  3991. while ((hcryp->CrypInCount < wordsize) && (outcount < wordsize))
  3992. {
  3993. /* Write plain data and get cipher data */
  3994. CRYP_AES_ProcessData(hcryp, Timeout);
  3995. /* Check for the Timeout */
  3996. if (Timeout != HAL_MAX_DELAY)
  3997. {
  3998. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  3999. {
  4000. /* Disable the CRYP peripheral clock */
  4001. __HAL_CRYP_DISABLE(hcryp);
  4002. /* Change state */
  4003. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4004. hcryp->State = HAL_CRYP_STATE_READY;
  4005. /* Process unlocked */
  4006. __HAL_UNLOCK(hcryp);
  4007. return HAL_ERROR;
  4008. }
  4009. }
  4010. }
  4011. if ((hcryp->Size % 16U) != 0U)
  4012. {
  4013. /* CRYP Workaround : CRYP1 generates correct TAG during CCM decryption only when ciphertext blocks size is multiple of
  4014. 128 bits. If lthe size of the last block of payload is inferior to 128 bits, when CCM decryption
  4015. is selected, then the TAG message will be wrong.*/
  4016. CRYP_Workaround(hcryp, Timeout);
  4017. }
  4018. #endif /* CRYP */
  4019. /* Return function status */
  4020. return HAL_OK;
  4021. }
  4022. /**
  4023. * @brief AES CCM encryption/decryption process in interrupt mode
  4024. * for TinyAES IP, no encrypt/decrypt performed, only authentication preparation.
  4025. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4026. * the configuration information for CRYP module
  4027. * @retval HAL status
  4028. */
  4029. static HAL_StatusTypeDef CRYP_AESCCM_Process_IT(CRYP_HandleTypeDef *hcryp)
  4030. {
  4031. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  4032. #if defined(CRYP)
  4033. __IO uint32_t count = 0U;
  4034. #endif /* CRYP */
  4035. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  4036. {
  4037. if (hcryp->KeyIVConfig == 1U)
  4038. {
  4039. /* If the Key and IV configuration has to be done only once
  4040. and if it has already been done, skip it */
  4041. DoKeyIVConfig = 0U;
  4042. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  4043. }
  4044. else
  4045. {
  4046. /* If the Key and IV configuration has to be done only once
  4047. and if it has not been done already, do it and set KeyIVConfig
  4048. to keep track it won't have to be done again next time */
  4049. hcryp->KeyIVConfig = 1U;
  4050. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  4051. }
  4052. }
  4053. else
  4054. {
  4055. hcryp->SizesSum = hcryp->Size;
  4056. }
  4057. /* Configure Key, IV and process message (header and payload) */
  4058. if (DoKeyIVConfig == 1U)
  4059. {
  4060. /* Reset CrypHeaderCount */
  4061. hcryp->CrypHeaderCount = 0U;
  4062. #if defined(CRYP)
  4063. /************ Init phase ************/
  4064. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  4065. /* Set the key */
  4066. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  4067. /* Set the initialization vector (IV) with CTR1 information */
  4068. hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0;
  4069. hcryp->Instance->IV0RR = hcryp->Init.B0[1];
  4070. hcryp->Instance->IV1LR = hcryp->Init.B0[2];
  4071. hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1) | CRYP_CCM_CTR1_2;
  4072. /* Enable the CRYP peripheral */
  4073. __HAL_CRYP_ENABLE(hcryp);
  4074. /*Write the B0 packet into CRYP_DIN Register*/
  4075. if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
  4076. {
  4077. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0));
  4078. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 1));
  4079. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 2));
  4080. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 3));
  4081. }
  4082. else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
  4083. {
  4084. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0), 16);
  4085. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 1), 16);
  4086. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 2), 16);
  4087. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 3), 16);
  4088. }
  4089. else if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
  4090. {
  4091. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0));
  4092. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 1));
  4093. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 2));
  4094. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 3));
  4095. }
  4096. else
  4097. {
  4098. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0);
  4099. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 1);
  4100. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 2);
  4101. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 3);
  4102. }
  4103. /*Wait for the CRYPEN bit to be cleared*/
  4104. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4105. do
  4106. {
  4107. count-- ;
  4108. if (count == 0U)
  4109. {
  4110. /* Disable the CRYP peripheral clock */
  4111. __HAL_CRYP_DISABLE(hcryp);
  4112. /* Change state */
  4113. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4114. hcryp->State = HAL_CRYP_STATE_READY;
  4115. /* Process unlocked */
  4116. __HAL_UNLOCK(hcryp);
  4117. return HAL_ERROR;
  4118. }
  4119. } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  4120. /* Select header phase */
  4121. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4122. } /* end of if (DoKeyIVConfig == 1U) */
  4123. /* Enable interrupts */
  4124. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI);
  4125. /* Enable CRYP */
  4126. __HAL_CRYP_ENABLE(hcryp);
  4127. #else /* AES */
  4128. /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */
  4129. /* Select header phase */
  4130. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4131. /* configured mode and encryption mode */
  4132. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  4133. /* Set the key */
  4134. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  4135. /* Set the initialization vector with zero values*/
  4136. hcryp->Instance->IVR3 = 0U;
  4137. hcryp->Instance->IVR2 = 0U;
  4138. hcryp->Instance->IVR1 = 0U;
  4139. hcryp->Instance->IVR0 = 0U;
  4140. /* Enable interrupts */
  4141. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  4142. /* Enable the CRYP peripheral */
  4143. __HAL_CRYP_ENABLE(hcryp);
  4144. /*Write the B0 packet into CRYP_DIN*/
  4145. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0);
  4146. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 1);
  4147. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 2);
  4148. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 3);
  4149. } /* end of if (DoKeyIVConfig == 1U) */
  4150. #endif /* End AES or CRYP */
  4151. /* Return function status */
  4152. return HAL_OK;
  4153. }
  4154. /**
  4155. * @brief AES CCM encryption/decryption process in DMA mode
  4156. * for TinyAES IP, no encrypt/decrypt performed, only authentication preparation.
  4157. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4158. * the configuration information for CRYP module
  4159. * @retval HAL status
  4160. */
  4161. static HAL_StatusTypeDef CRYP_AESCCM_Process_DMA(CRYP_HandleTypeDef *hcryp)
  4162. {
  4163. uint32_t wordsize;
  4164. __IO uint32_t count = 0U;
  4165. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  4166. #if defined(AES)
  4167. uint32_t loopcounter;
  4168. uint32_t npblb;
  4169. uint32_t lastwordsize;
  4170. #endif
  4171. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  4172. {
  4173. if (hcryp->KeyIVConfig == 1U)
  4174. {
  4175. /* If the Key and IV configuration has to be done only once
  4176. and if it has already been done, skip it */
  4177. DoKeyIVConfig = 0U;
  4178. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  4179. }
  4180. else
  4181. {
  4182. /* If the Key and IV configuration has to be done only once
  4183. and if it has not been done already, do it and set KeyIVConfig
  4184. to keep track it won't have to be done again next time */
  4185. hcryp->KeyIVConfig = 1U;
  4186. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  4187. }
  4188. }
  4189. else
  4190. {
  4191. hcryp->SizesSum = hcryp->Size;
  4192. }
  4193. if (DoKeyIVConfig == 1U)
  4194. {
  4195. /* Reset CrypHeaderCount */
  4196. hcryp->CrypHeaderCount = 0U;
  4197. #if defined(CRYP)
  4198. /************************** Init phase **************************************/
  4199. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  4200. /* Set the key */
  4201. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  4202. /* Set the initialization vector (IV) with CTR1 information */
  4203. hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0;
  4204. hcryp->Instance->IV0RR = hcryp->Init.B0[1];
  4205. hcryp->Instance->IV1LR = hcryp->Init.B0[2];
  4206. hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1) | CRYP_CCM_CTR1_2;
  4207. /* Enable the CRYP peripheral */
  4208. __HAL_CRYP_ENABLE(hcryp);
  4209. /*Write the B0 packet into CRYP_DIN Register*/
  4210. if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
  4211. {
  4212. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0));
  4213. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 1));
  4214. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 2));
  4215. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 3));
  4216. }
  4217. else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
  4218. {
  4219. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0), 16);
  4220. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 1), 16);
  4221. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 2), 16);
  4222. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 3), 16);
  4223. }
  4224. else if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
  4225. {
  4226. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0));
  4227. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 1));
  4228. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 2));
  4229. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 3));
  4230. }
  4231. else
  4232. {
  4233. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0);
  4234. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 1);
  4235. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 2);
  4236. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 3);
  4237. }
  4238. /*Wait for the CRYPEN bit to be cleared*/
  4239. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4240. do
  4241. {
  4242. count-- ;
  4243. if (count == 0U)
  4244. {
  4245. /* Disable the CRYP peripheral clock */
  4246. __HAL_CRYP_DISABLE(hcryp);
  4247. /* Change state */
  4248. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4249. hcryp->State = HAL_CRYP_STATE_READY;
  4250. /* Process unlocked */
  4251. __HAL_UNLOCK(hcryp);
  4252. return HAL_ERROR;
  4253. }
  4254. } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  4255. #else /* AES */
  4256. /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */
  4257. /* Select header phase */
  4258. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4259. /* configured encryption mode */
  4260. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  4261. /* Set the key */
  4262. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  4263. /* Set the initialization vector with zero values*/
  4264. hcryp->Instance->IVR3 = 0U;
  4265. hcryp->Instance->IVR2 = 0U;
  4266. hcryp->Instance->IVR1 = 0U;
  4267. hcryp->Instance->IVR0 = 0U;
  4268. /* Enable the CRYP peripheral */
  4269. __HAL_CRYP_ENABLE(hcryp);
  4270. /*Write the B0 packet into CRYP_DIN*/
  4271. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0);
  4272. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 1);
  4273. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 2);
  4274. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 3);
  4275. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4276. do
  4277. {
  4278. count-- ;
  4279. if (count == 0U)
  4280. {
  4281. /* Disable the CRYP peripheral clock */
  4282. __HAL_CRYP_DISABLE(hcryp);
  4283. /* Change state */
  4284. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4285. hcryp->State = HAL_CRYP_STATE_READY;
  4286. /* Process Unlocked */
  4287. __HAL_UNLOCK(hcryp);
  4288. return HAL_ERROR;
  4289. }
  4290. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  4291. /* Clear CCF flag */
  4292. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4293. /* Set the phase */
  4294. hcryp->Phase = CRYP_PHASE_PROCESS;
  4295. /* From that point the whole message must be processed, first the Header then the payload.
  4296. First the Header block(B1) : associated data length expressed in bytes concatenated with Associated Data (A)*/
  4297. if (hcryp->Init.HeaderSize != 0U)
  4298. {
  4299. if ((hcryp->Init.HeaderSize % 4U) == 0U)
  4300. {
  4301. /* HeaderSize %4, no padding */
  4302. for (loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter += 4U)
  4303. {
  4304. /* Write the Input block in the Data Input register */
  4305. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4306. hcryp->CrypHeaderCount++ ;
  4307. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4308. hcryp->CrypHeaderCount++ ;
  4309. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4310. hcryp->CrypHeaderCount++ ;
  4311. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4312. hcryp->CrypHeaderCount++ ;
  4313. /* wait until the end of computation */
  4314. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4315. do
  4316. {
  4317. count-- ;
  4318. if (count == 0U)
  4319. {
  4320. /* Disable the CRYP peripheral clock */
  4321. __HAL_CRYP_DISABLE(hcryp);
  4322. /* Change state */
  4323. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4324. hcryp->State = HAL_CRYP_STATE_READY;
  4325. /* Process Unlocked */
  4326. __HAL_UNLOCK(hcryp);
  4327. return HAL_ERROR;
  4328. }
  4329. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  4330. /* Clear CCF flag */
  4331. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4332. }
  4333. }
  4334. else
  4335. {
  4336. /*Write Header block in the IN FIFO without last block */
  4337. for (loopcounter = 0U; (loopcounter < ((hcryp->Init.HeaderSize) - (hcryp->Init.HeaderSize % 4U))); loopcounter += 4U)
  4338. {
  4339. /* Write the input block in the data input register */
  4340. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4341. hcryp->CrypHeaderCount++ ;
  4342. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4343. hcryp->CrypHeaderCount++ ;
  4344. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4345. hcryp->CrypHeaderCount++ ;
  4346. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4347. hcryp->CrypHeaderCount++ ;
  4348. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4349. do
  4350. {
  4351. count-- ;
  4352. if (count == 0U)
  4353. {
  4354. /* Disable the CRYP peripheral clock */
  4355. __HAL_CRYP_DISABLE(hcryp);
  4356. /* Change state */
  4357. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4358. hcryp->State = HAL_CRYP_STATE_READY;
  4359. /* Process Unlocked */
  4360. __HAL_UNLOCK(hcryp);
  4361. return HAL_ERROR;
  4362. }
  4363. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  4364. /* Clear CCF flag */
  4365. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4366. }
  4367. /* Last block optionally pad the data with zeros*/
  4368. for (loopcounter = 0U; (loopcounter < (hcryp->Init.HeaderSize % 4U)); loopcounter++)
  4369. {
  4370. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4371. hcryp->CrypHeaderCount++ ;
  4372. }
  4373. while (loopcounter < 4U)
  4374. {
  4375. /* Pad the data with zeros to have a complete block */
  4376. hcryp->Instance->DINR = 0x0U;
  4377. loopcounter++;
  4378. }
  4379. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4380. do
  4381. {
  4382. count-- ;
  4383. if (count == 0U)
  4384. {
  4385. /* Disable the CRYP peripheral clock */
  4386. __HAL_CRYP_DISABLE(hcryp);
  4387. /* Change state */
  4388. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4389. hcryp->State = HAL_CRYP_STATE_READY;
  4390. /* Process Unlocked */
  4391. __HAL_UNLOCK(hcryp);
  4392. return HAL_ERROR;
  4393. }
  4394. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  4395. /* Clear CCF flag */
  4396. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4397. }
  4398. }
  4399. } /* if (DoKeyIVConfig == 1U) */
  4400. /* Then the payload: cleartext payload (not the ciphertext payload).
  4401. Write input Data, no output Data to get */
  4402. if (hcryp->Size != 0U)
  4403. {
  4404. if (hcryp->Size >= 16U)
  4405. {
  4406. if ((hcryp->Size % 16U) == 0U)
  4407. {
  4408. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
  4409. }
  4410. else /*to compute last word<128bits, otherwise it will not be encrypted/decrypted */
  4411. {
  4412. wordsize = (uint32_t)(hcryp->Size) + (16U - ((uint32_t)(hcryp->Size) % 16U)) ;
  4413. /* Set the input and output addresses and start DMA transfer, pCrypOutBuffPtr size should be %4 */
  4414. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)wordsize / 4U),
  4415. (uint32_t)(hcryp->pCrypOutBuffPtr));
  4416. }
  4417. }
  4418. if ((hcryp->Size < 16U) != 0U)
  4419. {
  4420. /* Compute the number of padding bytes in last block of payload */
  4421. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  4422. /* Number of valid words (lastwordsize) in last block */
  4423. if ((npblb % 4U) == 0U)
  4424. {
  4425. lastwordsize = (16U - npblb) / 4U;
  4426. }
  4427. else
  4428. {
  4429. lastwordsize = ((16U - npblb) / 4U) + 1U;
  4430. }
  4431. /* Last block optionally pad the data with zeros*/
  4432. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter ++)
  4433. {
  4434. /* Write the last input block in the IN FIFO */
  4435. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4436. hcryp->CrypInCount++;
  4437. }
  4438. while (loopcounter < 4U)
  4439. {
  4440. /* Pad the data with zeros to have a complete block */
  4441. hcryp->Instance->DINR = 0U;
  4442. loopcounter++;
  4443. }
  4444. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4445. do
  4446. {
  4447. count-- ;
  4448. if (count == 0U)
  4449. {
  4450. /* Disable the CRYP peripheral clock */
  4451. __HAL_CRYP_DISABLE(hcryp);
  4452. /* Change state */
  4453. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4454. hcryp->State = HAL_CRYP_STATE_READY;
  4455. /* Process Unlocked */
  4456. __HAL_UNLOCK(hcryp);
  4457. return HAL_ERROR;
  4458. }
  4459. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  4460. /* Clear CCF flag */
  4461. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4462. /* Process unlocked */
  4463. __HAL_UNLOCK(hcryp);
  4464. /* Change the CRYP state and phase */
  4465. hcryp->State = HAL_CRYP_STATE_READY;
  4466. }
  4467. }
  4468. else
  4469. {
  4470. /* Process unLocked */
  4471. __HAL_UNLOCK(hcryp);
  4472. /* Change the CRYP state and phase */
  4473. hcryp->State = HAL_CRYP_STATE_READY;
  4474. }
  4475. #endif /* AES */
  4476. #if defined(CRYP)
  4477. /********************* Header phase *****************************************/
  4478. if (CRYP_GCMCCM_SetHeaderPhase_DMA(hcryp) != HAL_OK)
  4479. {
  4480. return HAL_ERROR;
  4481. }
  4482. /******************** Payload phase *****************************************/
  4483. /* Set the phase */
  4484. hcryp->Phase = CRYP_PHASE_PROCESS;
  4485. /* Disable the CRYP peripheral */
  4486. __HAL_CRYP_DISABLE(hcryp);
  4487. /* Select payload phase once the header phase is performed */
  4488. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  4489. } /* if (DoKeyIVConfig == 1U) */
  4490. if (hcryp->Size != 0U)
  4491. {
  4492. /* Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption & CCM Decryption
  4493. Workaround is implemented in polling mode, so if last block of
  4494. payload <128bit don't use HAL_CRYP_AESGCM_DMA otherwise TAG is incorrectly generated for GCM Encryption. */
  4495. /* Set the input and output addresses and start DMA transfer */
  4496. if ((hcryp->Size % 16U) == 0U)
  4497. {
  4498. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), hcryp->Size / 4U, (uint32_t)(hcryp->pCrypOutBuffPtr));
  4499. }
  4500. else
  4501. {
  4502. wordsize = (uint32_t)(hcryp->Size) + 16U - ((uint32_t)(hcryp->Size) % 16U) ;
  4503. /* Set the input and output addresses and start DMA transfer, pCrypOutBuffPtr size should be %4*/
  4504. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (uint16_t)wordsize / 4U,
  4505. (uint32_t)(hcryp->pCrypOutBuffPtr));
  4506. }
  4507. }
  4508. else /*Size = 0*/
  4509. {
  4510. /* Process unlocked */
  4511. __HAL_UNLOCK(hcryp);
  4512. /* Change the CRYP state and phase */
  4513. hcryp->State = HAL_CRYP_STATE_READY;
  4514. }
  4515. #endif /* CRYP */
  4516. /* Return function status */
  4517. return HAL_OK;
  4518. }
  4519. /**
  4520. * @brief Sets the payload phase in iterrupt mode
  4521. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4522. * the configuration information for CRYP module
  4523. * @retval state
  4524. */
  4525. static void CRYP_GCMCCM_SetPayloadPhase_IT(CRYP_HandleTypeDef *hcryp)
  4526. {
  4527. uint32_t loopcounter;
  4528. uint32_t temp[4]; /* Temporary CrypOutBuff */
  4529. uint32_t lastwordsize;
  4530. uint32_t npblb;
  4531. uint32_t i;
  4532. #if defined(AES)
  4533. uint16_t outcount; /* Temporary CrypOutCount Value */
  4534. #endif /* AES */
  4535. /***************************** Payload phase *******************************/
  4536. #if defined(CRYP)
  4537. if (hcryp->Size == 0U)
  4538. {
  4539. /* Disable interrupts */
  4540. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  4541. /* Process unlocked */
  4542. __HAL_UNLOCK(hcryp);
  4543. /* Change the CRYP state */
  4544. hcryp->State = HAL_CRYP_STATE_READY;
  4545. }
  4546. else if (((hcryp->Size / 4U) - (hcryp->CrypInCount)) >= 4U)
  4547. {
  4548. /* Write the input block in the IN FIFO */
  4549. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4550. hcryp->CrypInCount++;
  4551. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4552. hcryp->CrypInCount++;
  4553. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4554. hcryp->CrypInCount++;
  4555. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4556. hcryp->CrypInCount++;
  4557. if (((hcryp->Size / 4U) == hcryp->CrypInCount) && ((hcryp->Size % 16U) == 0U))
  4558. {
  4559. /* Disable interrupts */
  4560. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  4561. /* Call the input data transfer complete callback */
  4562. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  4563. /*Call registered Input complete callback*/
  4564. hcryp->InCpltCallback(hcryp);
  4565. #else
  4566. /*Call legacy weak Input complete callback*/
  4567. HAL_CRYP_InCpltCallback(hcryp);
  4568. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4569. }
  4570. if (hcryp->CrypOutCount < (hcryp->Size / 4U))
  4571. {
  4572. /* Read the output block from the Output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */
  4573. for (i = 0U; i < 4U; i++)
  4574. {
  4575. temp[i] = hcryp->Instance->DOUT;
  4576. }
  4577. i = 0U;
  4578. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U))
  4579. {
  4580. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  4581. hcryp->CrypOutCount++;
  4582. i++;
  4583. }
  4584. if (((hcryp->Size / 4U) == hcryp->CrypOutCount) && ((hcryp->Size % 16U) == 0U))
  4585. {
  4586. /* Disable interrupts */
  4587. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  4588. /* Change the CRYP state */
  4589. hcryp->State = HAL_CRYP_STATE_READY;
  4590. /* Disable CRYP */
  4591. __HAL_CRYP_DISABLE(hcryp);
  4592. /* Process unlocked */
  4593. __HAL_UNLOCK(hcryp);
  4594. /* Call output transfer complete callback */
  4595. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  4596. /*Call registered Output complete callback*/
  4597. hcryp->OutCpltCallback(hcryp);
  4598. #else
  4599. /*Call legacy weak Output complete callback*/
  4600. HAL_CRYP_OutCpltCallback(hcryp);
  4601. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4602. }
  4603. }
  4604. }
  4605. else if ((hcryp->Size % 16U) != 0U)
  4606. {
  4607. /* Size should be %4 in word and %16 in byte otherwise TAG will be incorrectly generated for GCM Encryption & CCM Decryption
  4608. Workaround is implemented in polling mode, so if last block of
  4609. payload <128bit don't use CRYP_AESGCM_Encrypt_IT otherwise TAG is incorrectly generated. */
  4610. /* Compute the number of padding bytes in last block of payload */
  4611. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  4612. /* Number of valid words (lastwordsize) in last block */
  4613. if ((npblb % 4U) == 0U)
  4614. {
  4615. lastwordsize = (16U - npblb) / 4U;
  4616. }
  4617. else
  4618. {
  4619. lastwordsize = ((16U - npblb) / 4U) + 1U;
  4620. }
  4621. /* Last block optionally pad the data with zeros*/
  4622. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++)
  4623. {
  4624. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4625. hcryp->CrypInCount++;
  4626. }
  4627. while (loopcounter < 4U)
  4628. {
  4629. /* Pad the data with zeros to have a complete block */
  4630. hcryp->Instance->DIN = 0x0U;
  4631. loopcounter++;
  4632. }
  4633. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  4634. if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U)
  4635. {
  4636. for (i = 0U; i < 4U; i++)
  4637. {
  4638. temp[i] = hcryp->Instance->DOUT;
  4639. }
  4640. if (((hcryp->Size) / 4U) == 0U)
  4641. {
  4642. for (i = 0U; i < ((uint32_t)(hcryp->Size) % 4U); i++)
  4643. {
  4644. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  4645. hcryp->CrypOutCount++;
  4646. }
  4647. }
  4648. i = 0x0U;
  4649. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U))
  4650. {
  4651. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  4652. hcryp->CrypOutCount++;
  4653. i++;
  4654. }
  4655. }
  4656. if (hcryp->CrypOutCount >= (hcryp->Size / 4U))
  4657. {
  4658. /* Disable interrupts */
  4659. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI | CRYP_IT_INI);
  4660. /* Change the CRYP peripheral state */
  4661. hcryp->State = HAL_CRYP_STATE_READY;
  4662. /* Process unlocked */
  4663. __HAL_UNLOCK(hcryp);
  4664. /* Call output transfer complete callback */
  4665. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  4666. /*Call registered Output complete callback*/
  4667. hcryp->OutCpltCallback(hcryp);
  4668. #else
  4669. /*Call legacy weak Output complete callback*/
  4670. HAL_CRYP_OutCpltCallback(hcryp);
  4671. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4672. }
  4673. }
  4674. else
  4675. {
  4676. /* Nothing to do */
  4677. }
  4678. #else /* AES */
  4679. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer*/
  4680. for (i = 0U; i < 4U; i++)
  4681. {
  4682. temp[i] = hcryp->Instance->DOUTR;
  4683. }
  4684. i = 0U;
  4685. while ((hcryp->CrypOutCount < ((hcryp->Size + 3U) / 4U)) && (i < 4U))
  4686. {
  4687. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  4688. hcryp->CrypOutCount++;
  4689. i++;
  4690. }
  4691. /*Temporary CrypOutCount Value*/
  4692. outcount = hcryp->CrypOutCount;
  4693. if ((hcryp->CrypOutCount >= (hcryp->Size / 4U)) && ((outcount * 4U) >= hcryp->Size))
  4694. {
  4695. /* Disable computation complete flag and errors interrupts */
  4696. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  4697. /* Change the CRYP state */
  4698. hcryp->State = HAL_CRYP_STATE_READY;
  4699. /* Process unlocked */
  4700. __HAL_UNLOCK(hcryp);
  4701. /* Call output transfer complete callback */
  4702. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  4703. /*Call registered Output complete callback*/
  4704. hcryp->OutCpltCallback(hcryp);
  4705. #else
  4706. /*Call legacy weak Output complete callback*/
  4707. HAL_CRYP_OutCpltCallback(hcryp);
  4708. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4709. }
  4710. else if (((hcryp->Size / 4U) - (hcryp->CrypInCount)) >= 4U)
  4711. {
  4712. /* Write the input block in the IN FIFO */
  4713. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4714. hcryp->CrypInCount++;
  4715. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4716. hcryp->CrypInCount++;
  4717. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4718. hcryp->CrypInCount++;
  4719. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4720. hcryp->CrypInCount++;
  4721. if ((hcryp->CrypInCount == (hcryp->Size / 4U)) && ((hcryp->Size % 16U) == 0U))
  4722. {
  4723. /* Call Input transfer complete callback */
  4724. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  4725. /*Call registered Input complete callback*/
  4726. hcryp->InCpltCallback(hcryp);
  4727. #else
  4728. /*Call legacy weak Input complete callback*/
  4729. HAL_CRYP_InCpltCallback(hcryp);
  4730. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4731. }
  4732. }
  4733. else /* Last block of payload < 128bit*/
  4734. {
  4735. /* Workaround not implemented, Size should be %4 otherwise Tag will be incorrectly
  4736. generated for GCM Encryption & CCM Decryption. Workaround is implemented in polling mode, so if last block of
  4737. payload <128bit don't use CRYP_Encrypt_IT otherwise TAG is incorrectly generated for GCM Encryption & CCM Decryption. */
  4738. /* Compute the number of padding bytes in last block of payload */
  4739. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  4740. /* Number of valid words (lastwordsize) in last block */
  4741. if ((npblb % 4U) == 0U)
  4742. {
  4743. lastwordsize = (16U - npblb) / 4U;
  4744. }
  4745. else
  4746. {
  4747. lastwordsize = ((16U - npblb) / 4U) + 1U;
  4748. }
  4749. /* Last block optionally pad the data with zeros*/
  4750. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++)
  4751. {
  4752. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4753. hcryp->CrypInCount++;
  4754. }
  4755. while (loopcounter < 4U)
  4756. {
  4757. /* pad the data with zeros to have a complete block */
  4758. hcryp->Instance->DINR = 0x0U;
  4759. loopcounter++;
  4760. }
  4761. }
  4762. #endif /* AES */
  4763. }
  4764. /**
  4765. * @brief Sets the header phase in polling mode
  4766. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4767. * the configuration information for CRYP module(Header & HeaderSize)
  4768. * @param Timeout: Timeout value
  4769. * @retval state
  4770. */
  4771. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  4772. {
  4773. uint32_t loopcounter;
  4774. uint32_t size_in_bytes;
  4775. uint32_t tmp;
  4776. uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */
  4777. 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */
  4778. 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU}; /* 8-bit data type */
  4779. /***************************** Header phase for GCM/GMAC or CCM *********************************/
  4780. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD)
  4781. {
  4782. size_in_bytes = hcryp->Init.HeaderSize * 4U;
  4783. }
  4784. else
  4785. {
  4786. size_in_bytes = hcryp->Init.HeaderSize;
  4787. }
  4788. if (size_in_bytes != 0U)
  4789. {
  4790. #if defined(CRYP)
  4791. /* Select header phase */
  4792. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4793. /* Enable the CRYP peripheral */
  4794. __HAL_CRYP_ENABLE(hcryp);
  4795. if ((size_in_bytes % 16U) == 0U)
  4796. {
  4797. /* HeaderSize %4, no padding */
  4798. for (loopcounter = 0U; (loopcounter < (size_in_bytes / 4U)); loopcounter += 4U)
  4799. {
  4800. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4801. hcryp->CrypHeaderCount++ ;
  4802. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4803. hcryp->CrypHeaderCount++ ;
  4804. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4805. hcryp->CrypHeaderCount++ ;
  4806. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4807. hcryp->CrypHeaderCount++ ;
  4808. /* Wait for IFEM to be raised */
  4809. if (CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK)
  4810. {
  4811. /* Disable the CRYP peripheral clock */
  4812. __HAL_CRYP_DISABLE(hcryp);
  4813. /* Change state */
  4814. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4815. hcryp->State = HAL_CRYP_STATE_READY;
  4816. /* Process unlocked */
  4817. __HAL_UNLOCK(hcryp);
  4818. return HAL_ERROR;
  4819. }
  4820. }
  4821. }
  4822. else
  4823. {
  4824. /*Write header block in the IN FIFO without last block */
  4825. for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 16U) * 4U)); loopcounter += 4U)
  4826. {
  4827. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4828. hcryp->CrypHeaderCount++ ;
  4829. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4830. hcryp->CrypHeaderCount++ ;
  4831. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4832. hcryp->CrypHeaderCount++ ;
  4833. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4834. hcryp->CrypHeaderCount++ ;
  4835. /* Wait for IFEM to be raised */
  4836. if (CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK)
  4837. {
  4838. /* Disable the CRYP peripheral clock */
  4839. __HAL_CRYP_DISABLE(hcryp);
  4840. /* Change state */
  4841. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4842. hcryp->State = HAL_CRYP_STATE_READY;
  4843. /* Process unlocked */
  4844. __HAL_UNLOCK(hcryp);
  4845. return HAL_ERROR;
  4846. }
  4847. }
  4848. /* Last block optionally pad the data with zeros*/
  4849. for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 4U) % 4U)); loopcounter++)
  4850. {
  4851. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4852. hcryp->CrypHeaderCount++ ;
  4853. }
  4854. /* If the header size is a multiple of words */
  4855. if ((size_in_bytes % 4U) == 0U)
  4856. {
  4857. /* Pad the data with zeros to have a complete block */
  4858. while (loopcounter < 4U)
  4859. {
  4860. hcryp->Instance->DIN = 0x0U;
  4861. loopcounter++;
  4862. }
  4863. }
  4864. else
  4865. {
  4866. /* Enter last bytes, padded with zeroes */
  4867. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4868. tmp &= mask[(hcryp->Init.DataType * 2U) + (size_in_bytes % 4U)];
  4869. hcryp->Instance->DIN = tmp;
  4870. loopcounter++;
  4871. /* Pad the data with zeros to have a complete block */
  4872. while (loopcounter < 4U)
  4873. {
  4874. hcryp->Instance->DIN = 0x0U;
  4875. loopcounter++;
  4876. }
  4877. }
  4878. /* Wait for CCF IFEM to be raised */
  4879. if (CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK)
  4880. {
  4881. /* Disable the CRYP peripheral clock */
  4882. __HAL_CRYP_DISABLE(hcryp);
  4883. /* Change state */
  4884. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4885. hcryp->State = HAL_CRYP_STATE_READY;
  4886. /* Process unlocked */
  4887. __HAL_UNLOCK(hcryp);
  4888. return HAL_ERROR;
  4889. }
  4890. }
  4891. /* Wait until the complete message has been processed */
  4892. if (CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK)
  4893. {
  4894. /* Disable the CRYP peripheral clock */
  4895. __HAL_CRYP_DISABLE(hcryp);
  4896. /* Change state */
  4897. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4898. hcryp->State = HAL_CRYP_STATE_READY;
  4899. /* Process unlocked & return error */
  4900. __HAL_UNLOCK(hcryp);
  4901. return HAL_ERROR;
  4902. }
  4903. #else /* AES */
  4904. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  4905. {
  4906. /* Workaround 1 :only AES before re-enabling the IP, datatype can be configured.*/
  4907. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  4908. /* Select header phase */
  4909. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4910. /* Enable the CRYP peripheral */
  4911. __HAL_CRYP_ENABLE(hcryp);
  4912. }
  4913. /* If size_in_bytes is a multiple of blocks (a multiple of four 32-bits words ) */
  4914. if ((size_in_bytes % 16U) == 0U)
  4915. {
  4916. /* No padding */
  4917. for (loopcounter = 0U; (loopcounter < (size_in_bytes / 4U)); loopcounter += 4U)
  4918. {
  4919. /* Write the input block in the data input register */
  4920. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4921. hcryp->CrypHeaderCount++ ;
  4922. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4923. hcryp->CrypHeaderCount++ ;
  4924. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4925. hcryp->CrypHeaderCount++ ;
  4926. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4927. hcryp->CrypHeaderCount++ ;
  4928. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  4929. {
  4930. /* Disable the CRYP peripheral clock */
  4931. __HAL_CRYP_DISABLE(hcryp);
  4932. /* Change state */
  4933. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4934. hcryp->State = HAL_CRYP_STATE_READY;
  4935. /* Process unlocked */
  4936. __HAL_UNLOCK(hcryp);
  4937. return HAL_ERROR;
  4938. }
  4939. /* Clear CCF flag */
  4940. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4941. }
  4942. }
  4943. else
  4944. {
  4945. /*Write header block in the IN FIFO without last block */
  4946. for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 16U) * 4U)); loopcounter += 4U)
  4947. {
  4948. /* Write the input block in the data input register */
  4949. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4950. hcryp->CrypHeaderCount++ ;
  4951. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4952. hcryp->CrypHeaderCount++ ;
  4953. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4954. hcryp->CrypHeaderCount++ ;
  4955. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4956. hcryp->CrypHeaderCount++ ;
  4957. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  4958. {
  4959. /* Disable the CRYP peripheral clock */
  4960. __HAL_CRYP_DISABLE(hcryp);
  4961. /* Change state */
  4962. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4963. hcryp->State = HAL_CRYP_STATE_READY;
  4964. /* Process unlocked */
  4965. __HAL_UNLOCK(hcryp);
  4966. return HAL_ERROR;
  4967. }
  4968. /* Clear CCF flag */
  4969. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4970. }
  4971. /* Write last complete words */
  4972. for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 4U) % 4U)); loopcounter++)
  4973. {
  4974. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4975. hcryp->CrypHeaderCount++ ;
  4976. }
  4977. /* If the header size is a multiple of words */
  4978. if ((size_in_bytes % 4U) == 0U)
  4979. {
  4980. /* Pad the data with zeros to have a complete block */
  4981. while (loopcounter < 4U)
  4982. {
  4983. hcryp->Instance->DINR = 0x0U;
  4984. loopcounter++;
  4985. }
  4986. }
  4987. else
  4988. {
  4989. /* Enter last bytes, padded with zeroes */
  4990. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4991. tmp &= mask[(hcryp->Init.DataType * 2U) + (size_in_bytes % 4U)];
  4992. hcryp->Instance->DINR = tmp;
  4993. loopcounter++;
  4994. /* Pad the data with zeros to have a complete block */
  4995. while (loopcounter < 4U)
  4996. {
  4997. hcryp->Instance->DINR = 0x0U;
  4998. loopcounter++;
  4999. }
  5000. }
  5001. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  5002. {
  5003. /* Disable the CRYP peripheral clock */
  5004. __HAL_CRYP_DISABLE(hcryp);
  5005. /* Change state */
  5006. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5007. hcryp->State = HAL_CRYP_STATE_READY;
  5008. /* Process unlocked */
  5009. __HAL_UNLOCK(hcryp);
  5010. return HAL_ERROR;
  5011. }
  5012. /* Clear CCF flag */
  5013. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  5014. }
  5015. #endif /* End AES or CRYP */
  5016. }
  5017. else
  5018. {
  5019. #if defined(AES)
  5020. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  5021. {
  5022. /*Workaround 1: only AES, before re-enabling the IP, datatype can be configured.*/
  5023. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  5024. /* Select header phase */
  5025. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  5026. /* Enable the CRYP peripheral */
  5027. __HAL_CRYP_ENABLE(hcryp);
  5028. }
  5029. #endif /* AES */
  5030. }
  5031. /* Return function status */
  5032. return HAL_OK;
  5033. }
  5034. /**
  5035. * @brief Sets the header phase when using DMA in process
  5036. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  5037. * the configuration information for CRYP module(Header & HeaderSize)
  5038. * @retval None
  5039. */
  5040. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_DMA(CRYP_HandleTypeDef *hcryp)
  5041. {
  5042. __IO uint32_t count = 0U;
  5043. uint32_t loopcounter;
  5044. uint32_t headersize_in_bytes;
  5045. uint32_t tmp;
  5046. uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */
  5047. 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */
  5048. 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU}; /* 8-bit data type */
  5049. /***************************** Header phase for GCM/GMAC or CCM *********************************/
  5050. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD)
  5051. {
  5052. headersize_in_bytes = hcryp->Init.HeaderSize * 4U;
  5053. }
  5054. else
  5055. {
  5056. headersize_in_bytes = hcryp->Init.HeaderSize;
  5057. }
  5058. if (headersize_in_bytes != 0U)
  5059. {
  5060. #if defined(CRYP)
  5061. /* Select header phase */
  5062. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  5063. /* Enable the CRYP peripheral */
  5064. __HAL_CRYP_ENABLE(hcryp);
  5065. if ((headersize_in_bytes % 16U) == 0U)
  5066. {
  5067. /* HeaderSize %4, no padding */
  5068. for (loopcounter = 0U; (loopcounter < (headersize_in_bytes / 4U)); loopcounter += 4U)
  5069. {
  5070. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5071. hcryp->CrypHeaderCount++ ;
  5072. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5073. hcryp->CrypHeaderCount++ ;
  5074. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5075. hcryp->CrypHeaderCount++ ;
  5076. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5077. hcryp->CrypHeaderCount++ ;
  5078. /* Wait for IFEM to be raised */
  5079. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5080. do
  5081. {
  5082. count-- ;
  5083. if (count == 0U)
  5084. {
  5085. /* Disable the CRYP peripheral clock */
  5086. __HAL_CRYP_DISABLE(hcryp);
  5087. /* Change state */
  5088. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5089. hcryp->State = HAL_CRYP_STATE_READY;
  5090. /* Process unlocked */
  5091. __HAL_UNLOCK(hcryp);
  5092. return HAL_ERROR;
  5093. }
  5094. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
  5095. }
  5096. }
  5097. else
  5098. {
  5099. /*Write header block in the IN FIFO without last block */
  5100. for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 16U) * 4U)); loopcounter += 4U)
  5101. {
  5102. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5103. hcryp->CrypHeaderCount++ ;
  5104. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5105. hcryp->CrypHeaderCount++ ;
  5106. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5107. hcryp->CrypHeaderCount++ ;
  5108. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5109. hcryp->CrypHeaderCount++ ;
  5110. /* Wait for IFEM to be raised */
  5111. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5112. do
  5113. {
  5114. count-- ;
  5115. if (count == 0U)
  5116. {
  5117. /* Disable the CRYP peripheral clock */
  5118. __HAL_CRYP_DISABLE(hcryp);
  5119. /* Change state */
  5120. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5121. hcryp->State = HAL_CRYP_STATE_READY;
  5122. /* Process unlocked */
  5123. __HAL_UNLOCK(hcryp);
  5124. return HAL_ERROR;
  5125. }
  5126. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
  5127. }
  5128. /* Last block optionally pad the data with zeros*/
  5129. for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 4U) % 4U)); loopcounter++)
  5130. {
  5131. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5132. hcryp->CrypHeaderCount++ ;
  5133. }
  5134. /* If the header size is a multiple of words */
  5135. if ((headersize_in_bytes % 4U) == 0U)
  5136. {
  5137. /* Pad the data with zeros to have a complete block */
  5138. while (loopcounter < 4U)
  5139. {
  5140. hcryp->Instance->DIN = 0x0U;
  5141. loopcounter++;
  5142. }
  5143. }
  5144. else
  5145. {
  5146. /* Enter last bytes, padded with zeroes */
  5147. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5148. tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)];
  5149. hcryp->Instance->DIN = tmp;
  5150. loopcounter++;
  5151. /* Pad the data with zeros to have a complete block */
  5152. while (loopcounter < 4U)
  5153. {
  5154. hcryp->Instance->DIN = 0x0U;
  5155. loopcounter++;
  5156. }
  5157. }
  5158. /* Wait for IFEM to be raised */
  5159. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5160. do
  5161. {
  5162. count-- ;
  5163. if (count == 0U)
  5164. {
  5165. /* Disable the CRYP peripheral clock */
  5166. __HAL_CRYP_DISABLE(hcryp);
  5167. /* Change state */
  5168. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5169. hcryp->State = HAL_CRYP_STATE_READY;
  5170. /* Process unlocked */
  5171. __HAL_UNLOCK(hcryp);
  5172. return HAL_ERROR;
  5173. }
  5174. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
  5175. }
  5176. /* Wait until the complete message has been processed */
  5177. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5178. do
  5179. {
  5180. count-- ;
  5181. if (count == 0U)
  5182. {
  5183. /* Disable the CRYP peripheral clock */
  5184. __HAL_CRYP_DISABLE(hcryp);
  5185. /* Change state */
  5186. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5187. hcryp->State = HAL_CRYP_STATE_READY;
  5188. /* Process unlocked */
  5189. __HAL_UNLOCK(hcryp);
  5190. return HAL_ERROR;
  5191. }
  5192. } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
  5193. #else /* AES */
  5194. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  5195. {
  5196. /* Workaround 1: only AES, before re-enabling the IP, datatype can be configured.*/
  5197. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  5198. /* Select header phase */
  5199. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  5200. /* Enable the CRYP peripheral */
  5201. __HAL_CRYP_ENABLE(hcryp);
  5202. }
  5203. if ((headersize_in_bytes % 16U) == 0U)
  5204. {
  5205. /* HeaderSize %4, no padding */
  5206. for (loopcounter = 0U; (loopcounter < (headersize_in_bytes / 4U)); loopcounter += 4U)
  5207. {
  5208. /* Write the input block in the data input register */
  5209. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5210. hcryp->CrypHeaderCount++ ;
  5211. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5212. hcryp->CrypHeaderCount++ ;
  5213. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5214. hcryp->CrypHeaderCount++ ;
  5215. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5216. hcryp->CrypHeaderCount++ ;
  5217. /*Wait on CCF flag*/
  5218. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5219. do
  5220. {
  5221. count-- ;
  5222. if (count == 0U)
  5223. {
  5224. /* Disable the CRYP peripheral clock */
  5225. __HAL_CRYP_DISABLE(hcryp);
  5226. /* Change state */
  5227. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5228. hcryp->State = HAL_CRYP_STATE_READY;
  5229. /* Process unlocked */
  5230. __HAL_UNLOCK(hcryp);
  5231. return HAL_ERROR;
  5232. }
  5233. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  5234. /* Clear CCF flag */
  5235. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  5236. }
  5237. }
  5238. else
  5239. {
  5240. /*Write header block in the IN FIFO without last block */
  5241. for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 16U) * 4U)); loopcounter += 4U)
  5242. {
  5243. /* Write the Input block in the Data Input register */
  5244. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5245. hcryp->CrypHeaderCount++ ;
  5246. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5247. hcryp->CrypHeaderCount++ ;
  5248. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5249. hcryp->CrypHeaderCount++ ;
  5250. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5251. hcryp->CrypHeaderCount++ ;
  5252. /*Wait on CCF flag*/
  5253. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5254. do
  5255. {
  5256. count-- ;
  5257. if (count == 0U)
  5258. {
  5259. /* Disable the CRYP peripheral clock */
  5260. __HAL_CRYP_DISABLE(hcryp);
  5261. /* Change state */
  5262. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5263. hcryp->State = HAL_CRYP_STATE_READY;
  5264. /* Process unlocked */
  5265. __HAL_UNLOCK(hcryp);
  5266. return HAL_ERROR;
  5267. }
  5268. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  5269. /* Clear CCF flag */
  5270. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  5271. }
  5272. /* Last block optionally pad the data with zeros*/
  5273. for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes /4U) % 4U)); loopcounter++)
  5274. {
  5275. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5276. hcryp->CrypHeaderCount++ ;
  5277. }
  5278. /* If the header size is a multiple of words */
  5279. if ((headersize_in_bytes % 4U) == 0U)
  5280. {
  5281. /* Pad the data with zeros to have a complete block */
  5282. while (loopcounter < 4U)
  5283. {
  5284. hcryp->Instance->DINR = 0x0U;
  5285. loopcounter++;
  5286. }
  5287. }
  5288. else
  5289. {
  5290. /* Enter last bytes, padded with zeroes */
  5291. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5292. tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)];
  5293. hcryp->Instance->DINR = tmp;
  5294. loopcounter++;
  5295. /* Pad the data with zeros to have a complete block */
  5296. while (loopcounter < 4U)
  5297. {
  5298. hcryp->Instance->DINR = 0x0U;
  5299. loopcounter++;
  5300. }
  5301. }
  5302. /*Wait on CCF flag*/
  5303. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5304. do
  5305. {
  5306. count-- ;
  5307. if (count == 0U)
  5308. {
  5309. /* Disable the CRYP peripheral clock */
  5310. __HAL_CRYP_DISABLE(hcryp);
  5311. /* Change state */
  5312. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5313. hcryp->State = HAL_CRYP_STATE_READY;
  5314. /* Process unlocked */
  5315. __HAL_UNLOCK(hcryp);
  5316. return HAL_ERROR;
  5317. }
  5318. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  5319. /* Clear CCF flag */
  5320. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  5321. }
  5322. #endif /* End AES or CRYP */
  5323. }
  5324. else
  5325. {
  5326. #if defined(AES)
  5327. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  5328. {
  5329. /*Workaround 1: only AES, before re-enabling the IP, datatype can be configured.*/
  5330. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  5331. /* Select header phase */
  5332. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  5333. /* Enable the CRYP peripheral */
  5334. __HAL_CRYP_ENABLE(hcryp);
  5335. }
  5336. #endif /* AES */
  5337. }
  5338. /* Return function status */
  5339. return HAL_OK;
  5340. }
  5341. /**
  5342. * @brief Sets the header phase in interrupt mode
  5343. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  5344. * the configuration information for CRYP module(Header & HeaderSize)
  5345. * @retval None
  5346. */
  5347. static void CRYP_GCMCCM_SetHeaderPhase_IT(CRYP_HandleTypeDef *hcryp)
  5348. {
  5349. uint32_t loopcounter;
  5350. #if defined(AES)
  5351. uint32_t lastwordsize;
  5352. uint32_t npblb;
  5353. #endif
  5354. uint32_t headersize_in_bytes;
  5355. uint32_t tmp;
  5356. uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */
  5357. 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */
  5358. 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU}; /* 8-bit data type */
  5359. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD)
  5360. {
  5361. headersize_in_bytes = hcryp->Init.HeaderSize * 4U;
  5362. }
  5363. else
  5364. {
  5365. headersize_in_bytes = hcryp->Init.HeaderSize;
  5366. }
  5367. /***************************** Header phase *********************************/
  5368. #if defined(CRYP)
  5369. if (headersize_in_bytes <= ((uint32_t)(hcryp->CrypHeaderCount) * 4U))
  5370. {
  5371. /* Disable interrupts */
  5372. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  5373. /* Disable the CRYP peripheral */
  5374. __HAL_CRYP_DISABLE(hcryp);
  5375. /* Set the phase */
  5376. hcryp->Phase = CRYP_PHASE_PROCESS;
  5377. /* Select payload phase once the header phase is performed */
  5378. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  5379. /* Enable Interrupts */
  5380. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  5381. /* Enable the CRYP peripheral */
  5382. __HAL_CRYP_ENABLE(hcryp);
  5383. }
  5384. else if (((headersize_in_bytes / 4U) - (hcryp->CrypHeaderCount)) >= 4U)
  5385. {
  5386. /* HeaderSize %4, no padding */
  5387. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5388. hcryp->CrypHeaderCount++ ;
  5389. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5390. hcryp->CrypHeaderCount++ ;
  5391. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5392. hcryp->CrypHeaderCount++ ;
  5393. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5394. hcryp->CrypHeaderCount++ ;
  5395. }
  5396. else
  5397. {
  5398. /* Last block optionally pad the data with zeros*/
  5399. for (loopcounter = 0U; loopcounter < ((headersize_in_bytes / 4U) % 4U); loopcounter++)
  5400. {
  5401. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5402. hcryp->CrypHeaderCount++ ;
  5403. }
  5404. if ((headersize_in_bytes % 4U) == 0U)
  5405. {
  5406. /* Pad the data with zeros to have a complete block */
  5407. while (loopcounter < 4U)
  5408. {
  5409. hcryp->Instance->DIN = 0x0U;
  5410. loopcounter++;
  5411. hcryp->CrypHeaderCount++;
  5412. }
  5413. }
  5414. else
  5415. {
  5416. /* Enter last bytes, padded with zeros */
  5417. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5418. tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)];
  5419. hcryp->Instance->DIN = tmp;
  5420. loopcounter++;
  5421. hcryp->CrypHeaderCount++;
  5422. /* Pad the data with zeros to have a complete block */
  5423. while (loopcounter < 4U)
  5424. {
  5425. hcryp->Instance->DIN = 0x0U;
  5426. loopcounter++;
  5427. hcryp->CrypHeaderCount++;
  5428. }
  5429. }
  5430. }
  5431. #else /* AES */
  5432. if (headersize_in_bytes <= ((uint32_t)(hcryp->CrypHeaderCount) * 4U))
  5433. {
  5434. /* Set the phase */
  5435. hcryp->Phase = CRYP_PHASE_PROCESS;
  5436. /* Payload phase not supported in CCM AES2 */
  5437. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  5438. {
  5439. /* Select payload phase once the header phase is performed */
  5440. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_PAYLOAD);
  5441. }
  5442. if (hcryp->Init.Algorithm == CRYP_AES_CCM)
  5443. {
  5444. /* Increment CrypHeaderCount to pass in CRYP_GCMCCM_SetPayloadPhase_IT */
  5445. hcryp->CrypHeaderCount++;
  5446. }
  5447. /* Write the payload Input block in the IN FIFO */
  5448. if (hcryp->Size == 0U)
  5449. {
  5450. /* Disable interrupts */
  5451. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  5452. /* Change the CRYP state */
  5453. hcryp->State = HAL_CRYP_STATE_READY;
  5454. /* Process unlocked */
  5455. __HAL_UNLOCK(hcryp);
  5456. }
  5457. else if (hcryp->Size >= 16U)
  5458. {
  5459. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5460. hcryp->CrypInCount++;
  5461. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5462. hcryp->CrypInCount++;
  5463. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5464. hcryp->CrypInCount++;
  5465. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5466. hcryp->CrypInCount++;
  5467. if ((hcryp->CrypInCount == (hcryp->Size / 4U)) && ((hcryp->Size % 16U) == 0U))
  5468. {
  5469. /* Call the input data transfer complete callback */
  5470. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  5471. /*Call registered Input complete callback*/
  5472. hcryp->InCpltCallback(hcryp);
  5473. #else
  5474. /*Call legacy weak Input complete callback*/
  5475. HAL_CRYP_InCpltCallback(hcryp);
  5476. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5477. }
  5478. }
  5479. else /* Size < 4 words : first block is the last block*/
  5480. {
  5481. /* Workaround not implemented, Size should be %4 otherwise Tag will be incorrectly
  5482. generated for GCM Encryption. Workaround is implemented in polling mode, so if last block of
  5483. payload <128bit don't use CRYP_Encrypt_IT otherwise TAG is incorrectly generated for GCM Encryption. */
  5484. /* Compute the number of padding bytes in last block of payload */
  5485. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  5486. /* Number of valid words (lastwordsize) in last block */
  5487. if ((npblb % 4U) == 0U)
  5488. {
  5489. lastwordsize = (16U - npblb) / 4U;
  5490. }
  5491. else
  5492. {
  5493. lastwordsize = ((16U - npblb) / 4U) + 1U;
  5494. }
  5495. /* Last block optionally pad the data with zeros*/
  5496. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++)
  5497. {
  5498. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5499. hcryp->CrypInCount++;
  5500. }
  5501. while (loopcounter < 4U)
  5502. {
  5503. /* Pad the data with zeros to have a complete block */
  5504. hcryp->Instance->DINR = 0x0U;
  5505. loopcounter++;
  5506. }
  5507. }
  5508. }
  5509. else if (((headersize_in_bytes / 4U) - (hcryp->CrypHeaderCount)) >= 4U)
  5510. {
  5511. /* Write the input block in the IN FIFO */
  5512. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5513. hcryp->CrypHeaderCount++;
  5514. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5515. hcryp->CrypHeaderCount++;
  5516. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5517. hcryp->CrypHeaderCount++;
  5518. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5519. hcryp->CrypHeaderCount++;
  5520. }
  5521. else /*HeaderSize < 4 or HeaderSize >4 & HeaderSize %4 != 0*/
  5522. {
  5523. /* Last block optionally pad the data with zeros*/
  5524. for (loopcounter = 0U; loopcounter < ((headersize_in_bytes / 4U) % 4U); loopcounter++)
  5525. {
  5526. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5527. hcryp->CrypHeaderCount++ ;
  5528. }
  5529. /* If the header size is a multiple of words */
  5530. if ((headersize_in_bytes % 4U) == 0U)
  5531. {
  5532. /* Pad the data with zeros to have a complete block */
  5533. while (loopcounter < 4U)
  5534. {
  5535. hcryp->Instance->DINR = 0x0U;
  5536. loopcounter++;
  5537. hcryp->CrypHeaderCount++;
  5538. }
  5539. }
  5540. else
  5541. {
  5542. /* Enter last bytes, padded with zeros */
  5543. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5544. tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)];
  5545. hcryp->Instance->DINR = tmp;
  5546. loopcounter++;
  5547. hcryp->CrypHeaderCount++;
  5548. /* Pad the data with zeros to have a complete block */
  5549. while (loopcounter < 4U)
  5550. {
  5551. hcryp->Instance->DINR = 0x0U;
  5552. loopcounter++;
  5553. hcryp->CrypHeaderCount++;
  5554. }
  5555. }
  5556. }
  5557. #endif /* End AES or CRYP */
  5558. }
  5559. /**
  5560. * @brief Workaround used for GCM/CCM mode.
  5561. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  5562. * the configuration information for CRYP module
  5563. * @param Timeout: specify Timeout value
  5564. * @retval None
  5565. */
  5566. static void CRYP_Workaround(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  5567. {
  5568. uint32_t lastwordsize;
  5569. uint32_t npblb;
  5570. #if defined(CRYP)
  5571. uint32_t iv1temp;
  5572. uint32_t temp[4] = {0};
  5573. uint32_t temp2[4] = {0};
  5574. #endif /* CRYP */
  5575. uint32_t intermediate_data[4] = {0};
  5576. uint32_t index;
  5577. /* Compute the number of padding bytes in last block of payload */
  5578. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  5579. /* Number of valid words (lastwordsize) in last block */
  5580. if ((npblb % 4U) == 0U)
  5581. {
  5582. lastwordsize = (16U - npblb) / 4U;
  5583. }
  5584. else
  5585. {
  5586. lastwordsize = ((16U - npblb) / 4U) + 1U;
  5587. }
  5588. #if defined(CRYP)
  5589. /* Workaround 2, case GCM encryption */
  5590. if (hcryp->Init.Algorithm == CRYP_AES_GCM)
  5591. {
  5592. if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_ENCRYPT)
  5593. {
  5594. /*Workaround in order to properly compute authentication tags while doing
  5595. a GCM encryption with the last block of payload size inferior to 128 bits*/
  5596. /* Disable CRYP to start the final phase */
  5597. __HAL_CRYP_DISABLE(hcryp);
  5598. /*Update CRYP_IV1R register and ALGOMODE*/
  5599. hcryp->Instance->IV1RR = ((hcryp->Instance->CSGCMCCM7R) - 1U);
  5600. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CTR);
  5601. /* Enable CRYP to start the final phase */
  5602. __HAL_CRYP_ENABLE(hcryp);
  5603. }
  5604. /* Last block optionally pad the data with zeros*/
  5605. for (index = 0; index < lastwordsize; index ++)
  5606. {
  5607. /* Write the last input block in the IN FIFO */
  5608. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5609. hcryp->CrypInCount++;
  5610. }
  5611. while (index < 4U)
  5612. {
  5613. /* Pad the data with zeros to have a complete block */
  5614. hcryp->Instance->DIN = 0U;
  5615. index++;
  5616. }
  5617. /* Wait for OFNE flag to be raised */
  5618. if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  5619. {
  5620. /* Disable the CRYP peripheral clock */
  5621. __HAL_CRYP_DISABLE(hcryp);
  5622. /* Change state */
  5623. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5624. hcryp->State = HAL_CRYP_STATE_READY;
  5625. /* Process Unlocked */
  5626. __HAL_UNLOCK(hcryp);
  5627. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  5628. /*Call registered error callback*/
  5629. hcryp->ErrorCallback(hcryp);
  5630. #else
  5631. /*Call legacy weak error callback*/
  5632. HAL_CRYP_ErrorCallback(hcryp);
  5633. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5634. }
  5635. if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U)
  5636. {
  5637. for (index = 0U; index < 4U; index++)
  5638. {
  5639. /* Read the output block from the output FIFO */
  5640. intermediate_data[index] = hcryp->Instance->DOUT;
  5641. /* Intermediate data buffer to be used in for the workaround*/
  5642. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = intermediate_data[index];
  5643. hcryp->CrypOutCount++;
  5644. }
  5645. }
  5646. if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_ENCRYPT)
  5647. {
  5648. /*workaround in order to properly compute authentication tags while doing
  5649. a GCM encryption with the last block of payload size inferior to 128 bits*/
  5650. /* Change the AES mode to GCM mode and Select Final phase */
  5651. /* configured CHMOD GCM */
  5652. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_GCM);
  5653. /* configured final phase */
  5654. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH, CRYP_PHASE_FINAL);
  5655. if ((hcryp->Instance->CR & CRYP_CR_DATATYPE) == CRYP_DATATYPE_32B)
  5656. {
  5657. if ((npblb % 4U) == 1U)
  5658. {
  5659. intermediate_data[lastwordsize - 1U] &= 0xFFFFFF00U;
  5660. }
  5661. if ((npblb % 4U) == 2U)
  5662. {
  5663. intermediate_data[lastwordsize - 1U] &= 0xFFFF0000U;
  5664. }
  5665. if ((npblb % 4U) == 3U)
  5666. {
  5667. intermediate_data[lastwordsize - 1U] &= 0xFF000000U;
  5668. }
  5669. }
  5670. else if ((hcryp->Instance->CR & CRYP_CR_DATATYPE) == CRYP_DATATYPE_8B)
  5671. {
  5672. if ((npblb % 4U) == 1U)
  5673. {
  5674. intermediate_data[lastwordsize - 1U] &= __REV(0xFFFFFF00U);
  5675. }
  5676. if ((npblb % 4U) == 2U)
  5677. {
  5678. intermediate_data[lastwordsize - 1U] &= __REV(0xFFFF0000U);
  5679. }
  5680. if ((npblb % 4U) == 3U)
  5681. {
  5682. intermediate_data[lastwordsize - 1U] &= __REV(0xFF000000U);
  5683. }
  5684. }
  5685. else if ((hcryp->Instance->CR & CRYP_CR_DATATYPE) == CRYP_DATATYPE_16B)
  5686. {
  5687. if ((npblb % 4U) == 1U)
  5688. {
  5689. intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFFFF00U), 16);
  5690. }
  5691. if ((npblb % 4U) == 2U)
  5692. {
  5693. intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFF0000U), 16);
  5694. }
  5695. if ((npblb % 4U) == 3U)
  5696. {
  5697. intermediate_data[lastwordsize - 1U] &= __ROR((0xFF000000U), 16);
  5698. }
  5699. }
  5700. else /*CRYP_DATATYPE_1B*/
  5701. {
  5702. if ((npblb % 4U) == 1U)
  5703. {
  5704. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFFFF00U);
  5705. }
  5706. if ((npblb % 4U) == 2U)
  5707. {
  5708. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFF0000U);
  5709. }
  5710. if ((npblb % 4U) == 3U)
  5711. {
  5712. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFF000000U);
  5713. }
  5714. }
  5715. for (index = 0U; index < lastwordsize; index ++)
  5716. {
  5717. /*Write the intermediate_data in the IN FIFO */
  5718. hcryp->Instance->DIN = intermediate_data[index];
  5719. }
  5720. while (index < 4U)
  5721. {
  5722. /* Pad the data with zeros to have a complete block */
  5723. hcryp->Instance->DIN = 0x0U;
  5724. index++;
  5725. }
  5726. /* Wait for OFNE flag to be raised */
  5727. if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  5728. {
  5729. /* Disable the CRYP peripheral clock */
  5730. __HAL_CRYP_DISABLE(hcryp);
  5731. /* Change state */
  5732. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5733. hcryp->State = HAL_CRYP_STATE_READY;
  5734. /* Process unlocked */
  5735. __HAL_UNLOCK(hcryp);
  5736. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  5737. /*Call registered error callback*/
  5738. hcryp->ErrorCallback(hcryp);
  5739. #else
  5740. /*Call legacy weak error callback*/
  5741. HAL_CRYP_ErrorCallback(hcryp);
  5742. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5743. }
  5744. if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U)
  5745. {
  5746. for (index = 0U; index < 4U; index++)
  5747. {
  5748. intermediate_data[index] = hcryp->Instance->DOUT;
  5749. }
  5750. }
  5751. }
  5752. } /* End of GCM encryption */
  5753. else
  5754. {
  5755. /* Workaround 2, case CCM decryption, in order to properly compute
  5756. authentication tags while doing a CCM decryption with the last block
  5757. of payload size inferior to 128 bits*/
  5758. if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_DECRYPT)
  5759. {
  5760. iv1temp = hcryp->Instance->CSGCMCCM7R;
  5761. /* Disable CRYP to start the final phase */
  5762. __HAL_CRYP_DISABLE(hcryp);
  5763. temp[0] = hcryp->Instance->CSGCMCCM0R;
  5764. temp[1] = hcryp->Instance->CSGCMCCM1R;
  5765. temp[2] = hcryp->Instance->CSGCMCCM2R;
  5766. temp[3] = hcryp->Instance->CSGCMCCM3R;
  5767. hcryp->Instance->IV1RR = iv1temp;
  5768. /* Configured CHMOD CTR */
  5769. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CTR);
  5770. /* Enable CRYP to start the final phase */
  5771. __HAL_CRYP_ENABLE(hcryp);
  5772. }
  5773. /* Last block optionally pad the data with zeros*/
  5774. for (index = 0; index < lastwordsize; index ++)
  5775. {
  5776. /* Write the last Input block in the IN FIFO */
  5777. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5778. hcryp->CrypInCount++;
  5779. }
  5780. while (index < 4U)
  5781. {
  5782. /* Pad the data with zeros to have a complete block */
  5783. hcryp->Instance->DIN = 0U;
  5784. index++;
  5785. }
  5786. /* Wait for OFNE flag to be raised */
  5787. if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  5788. {
  5789. /* Disable the CRYP peripheral clock */
  5790. __HAL_CRYP_DISABLE(hcryp);
  5791. /* Change state */
  5792. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5793. hcryp->State = HAL_CRYP_STATE_READY;
  5794. /* Process Unlocked */
  5795. __HAL_UNLOCK(hcryp);
  5796. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  5797. /*Call registered error callback*/
  5798. hcryp->ErrorCallback(hcryp);
  5799. #else
  5800. /*Call legacy weak error callback*/
  5801. HAL_CRYP_ErrorCallback(hcryp);
  5802. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5803. }
  5804. if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U)
  5805. {
  5806. for (index = 0U; index < 4U; index++)
  5807. {
  5808. /* Read the Output block from the Output FIFO */
  5809. intermediate_data[index] = hcryp->Instance->DOUT;
  5810. /*intermediate data buffer to be used in for the workaround*/
  5811. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = intermediate_data[index];
  5812. hcryp->CrypOutCount++;
  5813. }
  5814. }
  5815. if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_DECRYPT)
  5816. {
  5817. temp2[0] = hcryp->Instance->CSGCMCCM0R;
  5818. temp2[1] = hcryp->Instance->CSGCMCCM1R;
  5819. temp2[2] = hcryp->Instance->CSGCMCCM2R;
  5820. temp2[3] = hcryp->Instance->CSGCMCCM3R;
  5821. /* configured CHMOD CCM */
  5822. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CCM);
  5823. /* configured Header phase */
  5824. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH, CRYP_PHASE_HEADER);
  5825. /*set to zero the bits corresponding to the padded bits*/
  5826. for (index = lastwordsize; index < 4U; index ++)
  5827. {
  5828. intermediate_data[index] = 0U;
  5829. }
  5830. if ((npblb % 4U) == 1U)
  5831. {
  5832. intermediate_data[lastwordsize - 1U] &= 0xFFFFFF00U;
  5833. }
  5834. if ((npblb % 4U) == 2U)
  5835. {
  5836. intermediate_data[lastwordsize - 1U] &= 0xFFFF0000U;
  5837. }
  5838. if ((npblb % 4U) == 3U)
  5839. {
  5840. intermediate_data[lastwordsize - 1U] &= 0xFF000000U;
  5841. }
  5842. for (index = 0U; index < 4U ; index ++)
  5843. {
  5844. intermediate_data[index] ^= temp[index];
  5845. intermediate_data[index] ^= temp2[index];
  5846. }
  5847. for (index = 0U; index < 4U; index ++)
  5848. {
  5849. /* Write the last Input block in the IN FIFO */
  5850. hcryp->Instance->DIN = intermediate_data[index] ;
  5851. }
  5852. /* Wait for BUSY flag to be raised */
  5853. if (CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK)
  5854. {
  5855. /* Disable the CRYP peripheral clock */
  5856. __HAL_CRYP_DISABLE(hcryp);
  5857. /* Change state */
  5858. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5859. hcryp->State = HAL_CRYP_STATE_READY;
  5860. /* Process Unlocked */
  5861. __HAL_UNLOCK(hcryp);
  5862. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  5863. /*Call registered error callback*/
  5864. hcryp->ErrorCallback(hcryp);
  5865. #else
  5866. /*Call legacy weak error callback*/
  5867. HAL_CRYP_ErrorCallback(hcryp);
  5868. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5869. }
  5870. }
  5871. } /* End of CCM WKA*/
  5872. /* Process Unlocked */
  5873. __HAL_UNLOCK(hcryp);
  5874. #else /* AES */
  5875. /*Workaround 2: case GCM encryption, during payload phase and before inserting
  5876. the last block of paylaod, which size is inferior to 128 bits */
  5877. if ((hcryp->Instance->CR & AES_CR_MODE) == CRYP_OPERATINGMODE_ENCRYPT)
  5878. {
  5879. /* configured CHMOD CTR */
  5880. MODIFY_REG(hcryp->Instance->CR, AES_CR_CHMOD, CRYP_AES_CTR);
  5881. }
  5882. /* last block optionally pad the data with zeros*/
  5883. for (index = 0U; index < lastwordsize; index ++)
  5884. {
  5885. /* Write the last Input block in the IN FIFO */
  5886. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5887. hcryp->CrypInCount++;
  5888. }
  5889. while (index < 4U)
  5890. {
  5891. /* pad the data with zeros to have a complete block */
  5892. hcryp->Instance->DINR = 0U;
  5893. index++;
  5894. }
  5895. /* Wait for CCF flag to be raised */
  5896. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  5897. {
  5898. hcryp->State = HAL_CRYP_STATE_READY;
  5899. __HAL_UNLOCK(hcryp);
  5900. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  5901. /*Call registered error callback*/
  5902. hcryp->ErrorCallback(hcryp);
  5903. #else
  5904. /*Call legacy weak error callback*/
  5905. HAL_CRYP_ErrorCallback(hcryp);
  5906. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5907. }
  5908. /* Clear CCF Flag */
  5909. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  5910. for (index = 0U; index < 4U; index++)
  5911. {
  5912. /* Read the Output block from the Output FIFO */
  5913. intermediate_data[index] = hcryp->Instance->DOUTR;
  5914. /*intermediate data buffer to be used in the workaround*/
  5915. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = intermediate_data[index];
  5916. hcryp->CrypOutCount++;
  5917. }
  5918. if ((hcryp->Instance->CR & AES_CR_MODE) == CRYP_OPERATINGMODE_ENCRYPT)
  5919. {
  5920. /* configured CHMOD GCM */
  5921. MODIFY_REG(hcryp->Instance->CR, AES_CR_CHMOD, CRYP_AES_GCM_GMAC);
  5922. /* Select final phase */
  5923. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_FINAL);
  5924. if ((hcryp->Instance->CR & AES_CR_DATATYPE) == CRYP_DATATYPE_32B)
  5925. {
  5926. if ((npblb % 4U) == 1U)
  5927. {
  5928. intermediate_data[lastwordsize - 1U] &= 0xFFFFFF00U;
  5929. }
  5930. if ((npblb % 4U) == 2U)
  5931. {
  5932. intermediate_data[lastwordsize - 1U] &= 0xFFFF0000U;
  5933. }
  5934. if ((npblb % 4U) == 3U)
  5935. {
  5936. intermediate_data[lastwordsize - 1U] &= 0xFF000000U;
  5937. }
  5938. }
  5939. else if ((hcryp->Instance->CR & AES_CR_DATATYPE) == CRYP_DATATYPE_8B)
  5940. {
  5941. if ((npblb % 4U) == 1U)
  5942. {
  5943. intermediate_data[lastwordsize - 1U] &= __REV(0xFFFFFF00U);
  5944. }
  5945. if ((npblb % 4U) == 2U)
  5946. {
  5947. intermediate_data[lastwordsize - 1U] &= __REV(0xFFFF0000U);
  5948. }
  5949. if ((npblb % 4U) == 3U)
  5950. {
  5951. intermediate_data[lastwordsize - 1U] &= __REV(0xFF000000U);
  5952. }
  5953. }
  5954. else if ((hcryp->Instance->CR & AES_CR_DATATYPE) == CRYP_DATATYPE_16B)
  5955. {
  5956. if ((npblb % 4U) == 1U)
  5957. {
  5958. intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFFFF00U), 16);
  5959. }
  5960. if ((npblb % 4U) == 2U)
  5961. {
  5962. intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFF0000U), 16);
  5963. }
  5964. if ((npblb % 4U) == 3U)
  5965. {
  5966. intermediate_data[lastwordsize - 1U] &= __ROR((0xFF000000U), 16);
  5967. }
  5968. }
  5969. else /*CRYP_DATATYPE_1B*/
  5970. {
  5971. if ((npblb % 4U) == 1U)
  5972. {
  5973. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFFFF00U);
  5974. }
  5975. if ((npblb % 4U) == 2U)
  5976. {
  5977. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFF0000U);
  5978. }
  5979. if ((npblb % 4U) == 3U)
  5980. {
  5981. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFF000000U);
  5982. }
  5983. }
  5984. /*Write the intermediate_data in the IN FIFO */
  5985. for (index = 0U; index < lastwordsize; index ++)
  5986. {
  5987. hcryp->Instance->DINR = intermediate_data[index];
  5988. }
  5989. while (index < 4U)
  5990. {
  5991. /* pad the data with zeros to have a complete block */
  5992. hcryp->Instance->DINR = 0U;
  5993. index++;
  5994. }
  5995. /* Wait for CCF flag to be raised */
  5996. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  5997. {
  5998. /* Disable the CRYP peripheral clock */
  5999. __HAL_CRYP_DISABLE(hcryp);
  6000. /* Change state */
  6001. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  6002. hcryp->State = HAL_CRYP_STATE_READY;
  6003. /* Process Unlocked */
  6004. __HAL_UNLOCK(hcryp);
  6005. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  6006. /*Call registered error callback*/
  6007. hcryp->ErrorCallback(hcryp);
  6008. #else
  6009. /*Call legacy weak error callback*/
  6010. HAL_CRYP_ErrorCallback(hcryp);
  6011. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  6012. }
  6013. /* Clear CCF Flag */
  6014. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  6015. for (index = 0U; index < 4U; index++)
  6016. {
  6017. intermediate_data[index] = hcryp->Instance->DOUTR;
  6018. }
  6019. }/*End of Workaround 2*/
  6020. #endif /* End AES or CRYP */
  6021. }
  6022. #endif /* AES or GCM CCM defined*/
  6023. #if defined (CRYP)
  6024. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  6025. /**
  6026. * @brief Handle CRYP hardware block Timeout when waiting for IFEM flag to be raised.
  6027. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  6028. * the configuration information for CRYP module.
  6029. * @param Timeout: Timeout duration.
  6030. * @retval HAL status
  6031. */
  6032. static HAL_StatusTypeDef CRYP_WaitOnIFEMFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  6033. {
  6034. uint32_t tickstart;
  6035. /* Get timeout */
  6036. tickstart = HAL_GetTick();
  6037. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  6038. {
  6039. /* Check for the Timeout */
  6040. if (Timeout != HAL_MAX_DELAY)
  6041. {
  6042. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  6043. {
  6044. return HAL_ERROR;
  6045. }
  6046. }
  6047. }
  6048. return HAL_OK;
  6049. }
  6050. #endif /* GCM CCM defined*/
  6051. /**
  6052. * @brief Handle CRYP hardware block Timeout when waiting for BUSY flag to be raised.
  6053. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  6054. * the configuration information for CRYP module.
  6055. * @param Timeout: Timeout duration.
  6056. * @retval HAL status
  6057. */
  6058. static HAL_StatusTypeDef CRYP_WaitOnBUSYFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  6059. {
  6060. uint32_t tickstart;
  6061. /* Get timeout */
  6062. tickstart = HAL_GetTick();
  6063. while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  6064. {
  6065. /* Check for the Timeout */
  6066. if (Timeout != HAL_MAX_DELAY)
  6067. {
  6068. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  6069. {
  6070. return HAL_ERROR;
  6071. }
  6072. }
  6073. }
  6074. return HAL_OK;
  6075. }
  6076. /**
  6077. * @brief Handle CRYP hardware block Timeout when waiting for OFNE flag to be raised.
  6078. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  6079. * the configuration information for CRYP module.
  6080. * @param Timeout: Timeout duration.
  6081. * @retval HAL status
  6082. */
  6083. static HAL_StatusTypeDef CRYP_WaitOnOFNEFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  6084. {
  6085. uint32_t tickstart;
  6086. /* Get timeout */
  6087. tickstart = HAL_GetTick();
  6088. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  6089. {
  6090. /* Check for the Timeout */
  6091. if (Timeout != HAL_MAX_DELAY)
  6092. {
  6093. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  6094. {
  6095. return HAL_ERROR;
  6096. }
  6097. }
  6098. }
  6099. return HAL_OK;
  6100. }
  6101. #else /* AES */
  6102. /**
  6103. * @brief Handle CRYP hardware block Timeout when waiting for CCF flag to be raised.
  6104. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  6105. * the configuration information for CRYP module.
  6106. * @param Timeout: Timeout duration.
  6107. * @retval HAL status
  6108. */
  6109. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  6110. {
  6111. uint32_t tickstart;
  6112. /* Get timeout */
  6113. tickstart = HAL_GetTick();
  6114. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
  6115. {
  6116. /* Check for the Timeout */
  6117. if (Timeout != HAL_MAX_DELAY)
  6118. {
  6119. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  6120. {
  6121. return HAL_ERROR;
  6122. }
  6123. }
  6124. }
  6125. return HAL_OK;
  6126. }
  6127. #endif /* End AES or CRYP */
  6128. /**
  6129. * @}
  6130. */
  6131. /**
  6132. * @}
  6133. */
  6134. /**
  6135. * @}
  6136. */
  6137. #endif /* HAL_CRYP_MODULE_ENABLED */
  6138. /**
  6139. * @}
  6140. */
  6141. #endif /* TinyAES or CRYP*/
  6142. /**
  6143. * @}
  6144. */
  6145. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/