stm32f4xx_hal_cryp.c 207 KB

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