aes.c 261 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802
  1. /* aes.c
  2. *
  3. * Copyright (C) 2006-2020 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/wolfcrypt/error-crypt.h>
  26. #if !defined(NO_AES)
  27. /* Tip: Locate the software cipher modes by searching for "Software AES" */
  28. #if defined(HAVE_FIPS) && \
  29. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  30. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  31. #define FIPS_NO_WRAPPERS
  32. #ifdef USE_WINDOWS_API
  33. #pragma code_seg(".fipsA$g")
  34. #pragma const_seg(".fipsB$g")
  35. #endif
  36. #endif
  37. #include <wolfssl/wolfcrypt/aes.h>
  38. #include <wolfssl/wolfcrypt/cpuid.h>
  39. #ifdef WOLF_CRYPTO_CB
  40. #include <wolfssl/wolfcrypt/cryptocb.h>
  41. #endif
  42. /* fips wrapper calls, user can call direct */
  43. #if defined(HAVE_FIPS) && \
  44. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  45. int wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
  46. int dir)
  47. {
  48. if (aes == NULL || !( (len == 16) || (len == 24) || (len == 32)) ) {
  49. return BAD_FUNC_ARG;
  50. }
  51. return AesSetKey_fips(aes, key, len, iv, dir);
  52. }
  53. int wc_AesSetIV(Aes* aes, const byte* iv)
  54. {
  55. if (aes == NULL) {
  56. return BAD_FUNC_ARG;
  57. }
  58. return AesSetIV_fips(aes, iv);
  59. }
  60. #ifdef HAVE_AES_CBC
  61. int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  62. {
  63. if (aes == NULL || out == NULL || in == NULL) {
  64. return BAD_FUNC_ARG;
  65. }
  66. return AesCbcEncrypt_fips(aes, out, in, sz);
  67. }
  68. #ifdef HAVE_AES_DECRYPT
  69. int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  70. {
  71. if (aes == NULL || out == NULL || in == NULL
  72. || sz % AES_BLOCK_SIZE != 0) {
  73. return BAD_FUNC_ARG;
  74. }
  75. return AesCbcDecrypt_fips(aes, out, in, sz);
  76. }
  77. #endif /* HAVE_AES_DECRYPT */
  78. #endif /* HAVE_AES_CBC */
  79. /* AES-CTR */
  80. #ifdef WOLFSSL_AES_COUNTER
  81. int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  82. {
  83. if (aes == NULL || out == NULL || in == NULL) {
  84. return BAD_FUNC_ARG;
  85. }
  86. return AesCtrEncrypt(aes, out, in, sz);
  87. }
  88. #endif
  89. /* AES-DIRECT */
  90. #if defined(WOLFSSL_AES_DIRECT)
  91. void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
  92. {
  93. AesEncryptDirect(aes, out, in);
  94. }
  95. #ifdef HAVE_AES_DECRYPT
  96. void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
  97. {
  98. AesDecryptDirect(aes, out, in);
  99. }
  100. #endif /* HAVE_AES_DECRYPT */
  101. int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
  102. const byte* iv, int dir)
  103. {
  104. return AesSetKeyDirect(aes, key, len, iv, dir);
  105. }
  106. #endif /* WOLFSSL_AES_DIRECT */
  107. /* AES-GCM */
  108. #ifdef HAVE_AESGCM
  109. int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
  110. {
  111. if (aes == NULL || !( (len == 16) || (len == 24) || (len == 32)) ) {
  112. return BAD_FUNC_ARG;
  113. }
  114. return AesGcmSetKey_fips(aes, key, len);
  115. }
  116. int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
  117. const byte* iv, word32 ivSz,
  118. byte* authTag, word32 authTagSz,
  119. const byte* authIn, word32 authInSz)
  120. {
  121. if (aes == NULL || authTagSz > AES_BLOCK_SIZE
  122. || authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ ||
  123. ivSz > AES_BLOCK_SIZE) {
  124. return BAD_FUNC_ARG;
  125. }
  126. return AesGcmEncrypt_fips(aes, out, in, sz, iv, ivSz, authTag,
  127. authTagSz, authIn, authInSz);
  128. }
  129. #ifdef HAVE_AES_DECRYPT
  130. int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
  131. const byte* iv, word32 ivSz,
  132. const byte* authTag, word32 authTagSz,
  133. const byte* authIn, word32 authInSz)
  134. {
  135. if (aes == NULL || out == NULL || in == NULL || iv == NULL
  136. || authTag == NULL || authTagSz > AES_BLOCK_SIZE ||
  137. ivSz > AES_BLOCK_SIZE) {
  138. return BAD_FUNC_ARG;
  139. }
  140. return AesGcmDecrypt_fips(aes, out, in, sz, iv, ivSz, authTag,
  141. authTagSz, authIn, authInSz);
  142. }
  143. #endif /* HAVE_AES_DECRYPT */
  144. int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len)
  145. {
  146. if (gmac == NULL || key == NULL || !((len == 16) ||
  147. (len == 24) || (len == 32)) ) {
  148. return BAD_FUNC_ARG;
  149. }
  150. return GmacSetKey(gmac, key, len);
  151. }
  152. int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
  153. const byte* authIn, word32 authInSz,
  154. byte* authTag, word32 authTagSz)
  155. {
  156. if (gmac == NULL || authTagSz > AES_BLOCK_SIZE ||
  157. authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ) {
  158. return BAD_FUNC_ARG;
  159. }
  160. return GmacUpdate(gmac, iv, ivSz, authIn, authInSz,
  161. authTag, authTagSz);
  162. }
  163. #endif /* HAVE_AESGCM */
  164. /* AES-CCM */
  165. #if defined(HAVE_AESCCM) && \
  166. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  167. int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
  168. {
  169. return AesCcmSetKey(aes, key, keySz);
  170. }
  171. int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
  172. const byte* nonce, word32 nonceSz,
  173. byte* authTag, word32 authTagSz,
  174. const byte* authIn, word32 authInSz)
  175. {
  176. /* sanity check on arguments */
  177. if (aes == NULL || out == NULL || in == NULL || nonce == NULL
  178. || authTag == NULL || nonceSz < 7 || nonceSz > 13)
  179. return BAD_FUNC_ARG;
  180. AesCcmEncrypt(aes, out, in, inSz, nonce, nonceSz, authTag,
  181. authTagSz, authIn, authInSz);
  182. return 0;
  183. }
  184. #ifdef HAVE_AES_DECRYPT
  185. int wc_AesCcmDecrypt(Aes* aes, byte* out,
  186. const byte* in, word32 inSz,
  187. const byte* nonce, word32 nonceSz,
  188. const byte* authTag, word32 authTagSz,
  189. const byte* authIn, word32 authInSz)
  190. {
  191. if (aes == NULL || out == NULL || in == NULL || nonce == NULL
  192. || authTag == NULL || nonceSz < 7 || nonceSz > 13) {
  193. return BAD_FUNC_ARG;
  194. }
  195. return AesCcmDecrypt(aes, out, in, inSz, nonce, nonceSz,
  196. authTag, authTagSz, authIn, authInSz);
  197. }
  198. #endif /* HAVE_AES_DECRYPT */
  199. #endif /* HAVE_AESCCM && HAVE_FIPS_VERSION 2 */
  200. int wc_AesInit(Aes* aes, void* h, int i)
  201. {
  202. if (aes == NULL)
  203. return BAD_FUNC_ARG;
  204. (void)h;
  205. (void)i;
  206. /* FIPS doesn't support:
  207. return AesInit(aes, h, i); */
  208. return 0;
  209. }
  210. void wc_AesFree(Aes* aes)
  211. {
  212. (void)aes;
  213. /* FIPS doesn't support:
  214. AesFree(aes); */
  215. }
  216. #else /* else build without fips, or for FIPS v2 */
  217. #if defined(WOLFSSL_TI_CRYPT)
  218. #include <wolfcrypt/src/port/ti/ti-aes.c>
  219. #else
  220. #include <wolfssl/wolfcrypt/logging.h>
  221. #ifdef NO_INLINE
  222. #include <wolfssl/wolfcrypt/misc.h>
  223. #else
  224. #define WOLFSSL_MISC_INCLUDED
  225. #include <wolfcrypt/src/misc.c>
  226. #endif
  227. #if !defined(WOLFSSL_ARMASM)
  228. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  229. /* case of possibly not using hardware acceleration for AES but using key
  230. blobs */
  231. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  232. #endif
  233. #ifdef DEBUG_AESNI
  234. #include <stdio.h>
  235. #endif
  236. #ifdef _MSC_VER
  237. /* 4127 warning constant while(1) */
  238. #pragma warning(disable: 4127)
  239. #endif
  240. /* Define AES implementation includes and functions */
  241. #if defined(STM32_CRYPTO)
  242. /* STM32F2/F4/F7/L4 hardware AES support for ECB, CBC, CTR and GCM modes */
  243. #if defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  244. static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  245. {
  246. int ret = 0;
  247. #ifdef WOLFSSL_STM32_CUBEMX
  248. CRYP_HandleTypeDef hcryp;
  249. #else
  250. CRYP_InitTypeDef cryptInit;
  251. CRYP_KeyInitTypeDef keyInit;
  252. #endif
  253. #ifdef WOLFSSL_STM32_CUBEMX
  254. ret = wc_Stm32_Aes_Init(aes, &hcryp);
  255. if (ret != 0)
  256. return ret;
  257. #ifdef STM32_CRYPTO_AES_ONLY
  258. hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  259. hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  260. hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  261. #elif defined(STM32_HAL_V2)
  262. hcryp.Init.Algorithm = CRYP_AES_ECB;
  263. #endif
  264. HAL_CRYP_Init(&hcryp);
  265. #ifdef STM32_CRYPTO_AES_ONLY
  266. ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
  267. outBlock, STM32_HAL_TIMEOUT);
  268. #elif defined(STM32_HAL_V2)
  269. ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)inBlock, AES_BLOCK_SIZE,
  270. (uint32_t*)outBlock, STM32_HAL_TIMEOUT);
  271. #else
  272. ret = HAL_CRYP_AESECB_Encrypt(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
  273. outBlock, STM32_HAL_TIMEOUT);
  274. #endif
  275. if (ret != HAL_OK) {
  276. ret = WC_TIMEOUT_E;
  277. }
  278. HAL_CRYP_DeInit(&hcryp);
  279. #else /* STD_PERI_LIB */
  280. ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
  281. if (ret != 0)
  282. return ret;
  283. /* reset registers to their default values */
  284. CRYP_DeInit();
  285. /* setup key */
  286. CRYP_KeyInit(&keyInit);
  287. /* set direction and mode */
  288. cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  289. cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_ECB;
  290. CRYP_Init(&cryptInit);
  291. /* enable crypto processor */
  292. CRYP_Cmd(ENABLE);
  293. /* flush IN/OUT FIFOs */
  294. CRYP_FIFOFlush();
  295. CRYP_DataIn(*(uint32_t*)&inBlock[0]);
  296. CRYP_DataIn(*(uint32_t*)&inBlock[4]);
  297. CRYP_DataIn(*(uint32_t*)&inBlock[8]);
  298. CRYP_DataIn(*(uint32_t*)&inBlock[12]);
  299. /* wait until the complete message has been processed */
  300. while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
  301. *(uint32_t*)&outBlock[0] = CRYP_DataOut();
  302. *(uint32_t*)&outBlock[4] = CRYP_DataOut();
  303. *(uint32_t*)&outBlock[8] = CRYP_DataOut();
  304. *(uint32_t*)&outBlock[12] = CRYP_DataOut();
  305. /* disable crypto processor */
  306. CRYP_Cmd(DISABLE);
  307. #endif /* WOLFSSL_STM32_CUBEMX */
  308. return ret;
  309. }
  310. #endif /* WOLFSSL_AES_DIRECT || HAVE_AESGCM || HAVE_AESCCM */
  311. #ifdef HAVE_AES_DECRYPT
  312. #if defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESCCM)
  313. static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  314. {
  315. int ret = 0;
  316. #ifdef WOLFSSL_STM32_CUBEMX
  317. CRYP_HandleTypeDef hcryp;
  318. #else
  319. CRYP_InitTypeDef cryptInit;
  320. CRYP_KeyInitTypeDef keyInit;
  321. #endif
  322. #ifdef WOLFSSL_STM32_CUBEMX
  323. ret = wc_Stm32_Aes_Init(aes, &hcryp);
  324. if (ret != 0)
  325. return ret;
  326. #ifdef STM32_CRYPTO_AES_ONLY
  327. hcryp.Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  328. hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  329. hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  330. #elif defined(STM32_HAL_V2)
  331. hcryp.Init.Algorithm = CRYP_AES_ECB;
  332. #endif
  333. HAL_CRYP_Init(&hcryp);
  334. #ifdef STM32_CRYPTO_AES_ONLY
  335. ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
  336. outBlock, STM32_HAL_TIMEOUT);
  337. #elif defined(STM32_HAL_V2)
  338. ret = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)inBlock, AES_BLOCK_SIZE,
  339. (uint32_t*)outBlock, STM32_HAL_TIMEOUT);
  340. #else
  341. ret = HAL_CRYP_AESECB_Decrypt(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
  342. outBlock, STM32_HAL_TIMEOUT);
  343. #endif
  344. if (ret != HAL_OK) {
  345. ret = WC_TIMEOUT_E;
  346. }
  347. HAL_CRYP_DeInit(&hcryp);
  348. #else /* STD_PERI_LIB */
  349. ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
  350. if (ret != 0)
  351. return ret;
  352. /* reset registers to their default values */
  353. CRYP_DeInit();
  354. /* set direction and key */
  355. CRYP_KeyInit(&keyInit);
  356. cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  357. cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;
  358. CRYP_Init(&cryptInit);
  359. /* enable crypto processor */
  360. CRYP_Cmd(ENABLE);
  361. /* wait until decrypt key has been initialized */
  362. while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
  363. /* set direction and mode */
  364. cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  365. cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_ECB;
  366. CRYP_Init(&cryptInit);
  367. /* enable crypto processor */
  368. CRYP_Cmd(ENABLE);
  369. /* flush IN/OUT FIFOs */
  370. CRYP_FIFOFlush();
  371. CRYP_DataIn(*(uint32_t*)&inBlock[0]);
  372. CRYP_DataIn(*(uint32_t*)&inBlock[4]);
  373. CRYP_DataIn(*(uint32_t*)&inBlock[8]);
  374. CRYP_DataIn(*(uint32_t*)&inBlock[12]);
  375. /* wait until the complete message has been processed */
  376. while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
  377. *(uint32_t*)&outBlock[0] = CRYP_DataOut();
  378. *(uint32_t*)&outBlock[4] = CRYP_DataOut();
  379. *(uint32_t*)&outBlock[8] = CRYP_DataOut();
  380. *(uint32_t*)&outBlock[12] = CRYP_DataOut();
  381. /* disable crypto processor */
  382. CRYP_Cmd(DISABLE);
  383. #endif /* WOLFSSL_STM32_CUBEMX */
  384. return ret;
  385. }
  386. #endif /* WOLFSSL_AES_DIRECT || HAVE_AESCCM */
  387. #endif /* HAVE_AES_DECRYPT */
  388. #elif defined(HAVE_COLDFIRE_SEC)
  389. /* Freescale Coldfire SEC support for CBC mode.
  390. * NOTE: no support for AES-CTR/GCM/CCM/Direct */
  391. #include <wolfssl/wolfcrypt/types.h>
  392. #include "sec.h"
  393. #include "mcf5475_sec.h"
  394. #include "mcf5475_siu.h"
  395. #elif defined(FREESCALE_LTC)
  396. #include "fsl_ltc.h"
  397. #if defined(FREESCALE_LTC_AES_GCM)
  398. #undef NEED_AES_TABLES
  399. #undef GCM_TABLE
  400. #else
  401. /* if LTC doesn't have GCM, use software with LTC AES ECB mode */
  402. static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  403. {
  404. wc_AesEncryptDirect(aes, outBlock, inBlock);
  405. return 0;
  406. }
  407. static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  408. {
  409. wc_AesDecryptDirect(aes, outBlock, inBlock);
  410. return 0;
  411. }
  412. #endif
  413. #elif defined(FREESCALE_MMCAU)
  414. /* Freescale mmCAU hardware AES support for Direct, CBC, CCM, GCM modes
  415. * through the CAU/mmCAU library. Documentation located in
  416. * ColdFire/ColdFire+ CAU and Kinetis mmCAU Software Library User
  417. * Guide (See note in README). */
  418. #ifdef FREESCALE_MMCAU_CLASSIC
  419. /* MMCAU 1.4 library used with non-KSDK / classic MQX builds */
  420. #include "cau_api.h"
  421. #else
  422. #include "fsl_mmcau.h"
  423. #endif
  424. static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  425. {
  426. int ret;
  427. #ifdef FREESCALE_MMCAU_CLASSIC
  428. if ((wolfssl_word)outBlock % WOLFSSL_MMCAU_ALIGNMENT) {
  429. WOLFSSL_MSG("Bad cau_aes_encrypt alignment");
  430. return BAD_ALIGN_E;
  431. }
  432. #endif
  433. ret = wolfSSL_CryptHwMutexLock();
  434. if(ret == 0) {
  435. #ifdef FREESCALE_MMCAU_CLASSIC
  436. cau_aes_encrypt(inBlock, (byte*)aes->key, aes->rounds, outBlock);
  437. #else
  438. MMCAU_AES_EncryptEcb(inBlock, (byte*)aes->key, aes->rounds,
  439. outBlock);
  440. #endif
  441. wolfSSL_CryptHwMutexUnLock();
  442. }
  443. return ret;
  444. }
  445. #ifdef HAVE_AES_DECRYPT
  446. static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  447. {
  448. int ret;
  449. #ifdef FREESCALE_MMCAU_CLASSIC
  450. if ((wolfssl_word)outBlock % WOLFSSL_MMCAU_ALIGNMENT) {
  451. WOLFSSL_MSG("Bad cau_aes_decrypt alignment");
  452. return BAD_ALIGN_E;
  453. }
  454. #endif
  455. ret = wolfSSL_CryptHwMutexLock();
  456. if(ret == 0) {
  457. #ifdef FREESCALE_MMCAU_CLASSIC
  458. cau_aes_decrypt(inBlock, (byte*)aes->key, aes->rounds, outBlock);
  459. #else
  460. MMCAU_AES_DecryptEcb(inBlock, (byte*)aes->key, aes->rounds,
  461. outBlock);
  462. #endif
  463. wolfSSL_CryptHwMutexUnLock();
  464. }
  465. return ret;
  466. }
  467. #endif /* HAVE_AES_DECRYPT */
  468. #elif defined(WOLFSSL_PIC32MZ_CRYPT)
  469. #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
  470. #if defined(HAVE_AESGCM) || defined(WOLFSSL_AES_DIRECT)
  471. static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  472. {
  473. return wc_Pic32AesCrypt(aes->key, aes->keylen, NULL, 0,
  474. outBlock, inBlock, AES_BLOCK_SIZE,
  475. PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RECB);
  476. }
  477. #endif
  478. #if defined(HAVE_AES_DECRYPT) && defined(WOLFSSL_AES_DIRECT)
  479. static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  480. {
  481. return wc_Pic32AesCrypt(aes->key, aes->keylen, NULL, 0,
  482. outBlock, inBlock, AES_BLOCK_SIZE,
  483. PIC32_DECRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RECB);
  484. }
  485. #endif
  486. #elif defined(WOLFSSL_NRF51_AES)
  487. /* Use built-in AES hardware - AES 128 ECB Encrypt Only */
  488. #include "wolfssl/wolfcrypt/port/nrf51.h"
  489. static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  490. {
  491. return nrf51_aes_encrypt(inBlock, (byte*)aes->key, aes->rounds, outBlock);
  492. }
  493. #ifdef HAVE_AES_DECRYPT
  494. #error nRF51 AES Hardware does not support decrypt
  495. #endif /* HAVE_AES_DECRYPT */
  496. #elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
  497. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
  498. #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
  499. #if defined(HAVE_AESGCM) || defined(WOLFSSL_AES_DIRECT)
  500. static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  501. {
  502. return wc_esp32AesEncrypt(aes, inBlock, outBlock);
  503. }
  504. #endif
  505. #if defined(HAVE_AES_DECRYPT) && defined(WOLFSSL_AES_DIRECT)
  506. static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  507. {
  508. return wc_esp32AesDecrypt(aes, inBlock, outBlock);
  509. }
  510. #endif
  511. #elif defined(WOLFSSL_AESNI)
  512. #define NEED_AES_TABLES
  513. /* Each platform needs to query info type 1 from cpuid to see if aesni is
  514. * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
  515. */
  516. #ifndef AESNI_ALIGN
  517. #define AESNI_ALIGN 16
  518. #endif
  519. #ifdef _MSC_VER
  520. #define XASM_LINK(f)
  521. #elif defined(__APPLE__)
  522. #define XASM_LINK(f) asm("_" f)
  523. #else
  524. #define XASM_LINK(f) asm(f)
  525. #endif /* _MSC_VER */
  526. static int checkAESNI = 0;
  527. static int haveAESNI = 0;
  528. static word32 intel_flags = 0;
  529. static int Check_CPU_support_AES(void)
  530. {
  531. intel_flags = cpuid_get_flags();
  532. return IS_INTEL_AESNI(intel_flags) != 0;
  533. }
  534. /* tell C compiler these are asm functions in case any mix up of ABI underscore
  535. prefix between clang/gcc/llvm etc */
  536. #ifdef HAVE_AES_CBC
  537. void AES_CBC_encrypt(const unsigned char* in, unsigned char* out,
  538. unsigned char* ivec, unsigned long length,
  539. const unsigned char* KS, int nr)
  540. XASM_LINK("AES_CBC_encrypt");
  541. #ifdef HAVE_AES_DECRYPT
  542. #if defined(WOLFSSL_AESNI_BY4)
  543. void AES_CBC_decrypt_by4(const unsigned char* in, unsigned char* out,
  544. unsigned char* ivec, unsigned long length,
  545. const unsigned char* KS, int nr)
  546. XASM_LINK("AES_CBC_decrypt_by4");
  547. #elif defined(WOLFSSL_AESNI_BY6)
  548. void AES_CBC_decrypt_by6(const unsigned char* in, unsigned char* out,
  549. unsigned char* ivec, unsigned long length,
  550. const unsigned char* KS, int nr)
  551. XASM_LINK("AES_CBC_decrypt_by6");
  552. #else /* WOLFSSL_AESNI_BYx */
  553. void AES_CBC_decrypt_by8(const unsigned char* in, unsigned char* out,
  554. unsigned char* ivec, unsigned long length,
  555. const unsigned char* KS, int nr)
  556. XASM_LINK("AES_CBC_decrypt_by8");
  557. #endif /* WOLFSSL_AESNI_BYx */
  558. #endif /* HAVE_AES_DECRYPT */
  559. #endif /* HAVE_AES_CBC */
  560. void AES_ECB_encrypt(const unsigned char* in, unsigned char* out,
  561. unsigned long length, const unsigned char* KS, int nr)
  562. XASM_LINK("AES_ECB_encrypt");
  563. #ifdef HAVE_AES_DECRYPT
  564. void AES_ECB_decrypt(const unsigned char* in, unsigned char* out,
  565. unsigned long length, const unsigned char* KS, int nr)
  566. XASM_LINK("AES_ECB_decrypt");
  567. #endif
  568. void AES_128_Key_Expansion(const unsigned char* userkey,
  569. unsigned char* key_schedule)
  570. XASM_LINK("AES_128_Key_Expansion");
  571. void AES_192_Key_Expansion(const unsigned char* userkey,
  572. unsigned char* key_schedule)
  573. XASM_LINK("AES_192_Key_Expansion");
  574. void AES_256_Key_Expansion(const unsigned char* userkey,
  575. unsigned char* key_schedule)
  576. XASM_LINK("AES_256_Key_Expansion");
  577. static int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
  578. Aes* aes)
  579. {
  580. int ret;
  581. if (!userKey || !aes)
  582. return BAD_FUNC_ARG;
  583. switch (bits) {
  584. case 128:
  585. AES_128_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 10;
  586. return 0;
  587. case 192:
  588. AES_192_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 12;
  589. return 0;
  590. case 256:
  591. AES_256_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 14;
  592. return 0;
  593. default:
  594. ret = BAD_FUNC_ARG;
  595. }
  596. return ret;
  597. }
  598. #ifdef HAVE_AES_DECRYPT
  599. static int AES_set_decrypt_key(const unsigned char* userKey,
  600. const int bits, Aes* aes)
  601. {
  602. int nr;
  603. Aes temp_key;
  604. __m128i *Key_Schedule = (__m128i*)aes->key;
  605. __m128i *Temp_Key_Schedule = (__m128i*)temp_key.key;
  606. if (!userKey || !aes)
  607. return BAD_FUNC_ARG;
  608. if (AES_set_encrypt_key(userKey,bits,&temp_key) == BAD_FUNC_ARG)
  609. return BAD_FUNC_ARG;
  610. nr = temp_key.rounds;
  611. aes->rounds = nr;
  612. Key_Schedule[nr] = Temp_Key_Schedule[0];
  613. Key_Schedule[nr-1] = _mm_aesimc_si128(Temp_Key_Schedule[1]);
  614. Key_Schedule[nr-2] = _mm_aesimc_si128(Temp_Key_Schedule[2]);
  615. Key_Schedule[nr-3] = _mm_aesimc_si128(Temp_Key_Schedule[3]);
  616. Key_Schedule[nr-4] = _mm_aesimc_si128(Temp_Key_Schedule[4]);
  617. Key_Schedule[nr-5] = _mm_aesimc_si128(Temp_Key_Schedule[5]);
  618. Key_Schedule[nr-6] = _mm_aesimc_si128(Temp_Key_Schedule[6]);
  619. Key_Schedule[nr-7] = _mm_aesimc_si128(Temp_Key_Schedule[7]);
  620. Key_Schedule[nr-8] = _mm_aesimc_si128(Temp_Key_Schedule[8]);
  621. Key_Schedule[nr-9] = _mm_aesimc_si128(Temp_Key_Schedule[9]);
  622. if (nr>10) {
  623. Key_Schedule[nr-10] = _mm_aesimc_si128(Temp_Key_Schedule[10]);
  624. Key_Schedule[nr-11] = _mm_aesimc_si128(Temp_Key_Schedule[11]);
  625. }
  626. if (nr>12) {
  627. Key_Schedule[nr-12] = _mm_aesimc_si128(Temp_Key_Schedule[12]);
  628. Key_Schedule[nr-13] = _mm_aesimc_si128(Temp_Key_Schedule[13]);
  629. }
  630. Key_Schedule[0] = Temp_Key_Schedule[nr];
  631. return 0;
  632. }
  633. #endif /* HAVE_AES_DECRYPT */
  634. #elif (defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)) || \
  635. ((defined(WOLFSSL_AFALG) || defined(WOLFSSL_DEVCRYPTO_AES)) && \
  636. defined(HAVE_AESCCM))
  637. static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  638. {
  639. wc_AesEncryptDirect(aes, outBlock, inBlock);
  640. return 0;
  641. }
  642. #elif defined(WOLFSSL_AFALG)
  643. #elif defined(WOLFSSL_DEVCRYPTO_AES)
  644. #else
  645. /* using wolfCrypt software implementation */
  646. #define NEED_AES_TABLES
  647. #endif
  648. #ifdef NEED_AES_TABLES
  649. static const word32 rcon[] = {
  650. 0x01000000, 0x02000000, 0x04000000, 0x08000000,
  651. 0x10000000, 0x20000000, 0x40000000, 0x80000000,
  652. 0x1B000000, 0x36000000,
  653. /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
  654. };
  655. static const word32 Te[4][256] = {
  656. {
  657. 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
  658. 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U,
  659. 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU,
  660. 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU,
  661. 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U,
  662. 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU,
  663. 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU,
  664. 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU,
  665. 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU,
  666. 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU,
  667. 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U,
  668. 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU,
  669. 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU,
  670. 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U,
  671. 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU,
  672. 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU,
  673. 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU,
  674. 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU,
  675. 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU,
  676. 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U,
  677. 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU,
  678. 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU,
  679. 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU,
  680. 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU,
  681. 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U,
  682. 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U,
  683. 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U,
  684. 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U,
  685. 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU,
  686. 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U,
  687. 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U,
  688. 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU,
  689. 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU,
  690. 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U,
  691. 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U,
  692. 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U,
  693. 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU,
  694. 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U,
  695. 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU,
  696. 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U,
  697. 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU,
  698. 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U,
  699. 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U,
  700. 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU,
  701. 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U,
  702. 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U,
  703. 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U,
  704. 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U,
  705. 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U,
  706. 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U,
  707. 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U,
  708. 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U,
  709. 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU,
  710. 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U,
  711. 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U,
  712. 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U,
  713. 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U,
  714. 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U,
  715. 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U,
  716. 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU,
  717. 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U,
  718. 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U,
  719. 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U,
  720. 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU,
  721. },
  722. {
  723. 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU,
  724. 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U,
  725. 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU,
  726. 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U,
  727. 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU,
  728. 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U,
  729. 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU,
  730. 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U,
  731. 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U,
  732. 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU,
  733. 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U,
  734. 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U,
  735. 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U,
  736. 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU,
  737. 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U,
  738. 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U,
  739. 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU,
  740. 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U,
  741. 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U,
  742. 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U,
  743. 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU,
  744. 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU,
  745. 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U,
  746. 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU,
  747. 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU,
  748. 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U,
  749. 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU,
  750. 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U,
  751. 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU,
  752. 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U,
  753. 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U,
  754. 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U,
  755. 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU,
  756. 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U,
  757. 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU,
  758. 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U,
  759. 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU,
  760. 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U,
  761. 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U,
  762. 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU,
  763. 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU,
  764. 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU,
  765. 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U,
  766. 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U,
  767. 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU,
  768. 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U,
  769. 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU,
  770. 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U,
  771. 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU,
  772. 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U,
  773. 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU,
  774. 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU,
  775. 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U,
  776. 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU,
  777. 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U,
  778. 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU,
  779. 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U,
  780. 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U,
  781. 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U,
  782. 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU,
  783. 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU,
  784. 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U,
  785. 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU,
  786. 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U,
  787. },
  788. {
  789. 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU,
  790. 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U,
  791. 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU,
  792. 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U,
  793. 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU,
  794. 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U,
  795. 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU,
  796. 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U,
  797. 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U,
  798. 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU,
  799. 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U,
  800. 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U,
  801. 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U,
  802. 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU,
  803. 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U,
  804. 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U,
  805. 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU,
  806. 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U,
  807. 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U,
  808. 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U,
  809. 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU,
  810. 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU,
  811. 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U,
  812. 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU,
  813. 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU,
  814. 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U,
  815. 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU,
  816. 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U,
  817. 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU,
  818. 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U,
  819. 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U,
  820. 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U,
  821. 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU,
  822. 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U,
  823. 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU,
  824. 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U,
  825. 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU,
  826. 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U,
  827. 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U,
  828. 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU,
  829. 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU,
  830. 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU,
  831. 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U,
  832. 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U,
  833. 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU,
  834. 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U,
  835. 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU,
  836. 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U,
  837. 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU,
  838. 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U,
  839. 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU,
  840. 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU,
  841. 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U,
  842. 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU,
  843. 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U,
  844. 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU,
  845. 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U,
  846. 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U,
  847. 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U,
  848. 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU,
  849. 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU,
  850. 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U,
  851. 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU,
  852. 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U,
  853. },
  854. {
  855. 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U,
  856. 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U,
  857. 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U,
  858. 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU,
  859. 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU,
  860. 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU,
  861. 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U,
  862. 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU,
  863. 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU,
  864. 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U,
  865. 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U,
  866. 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU,
  867. 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU,
  868. 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU,
  869. 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU,
  870. 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU,
  871. 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U,
  872. 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU,
  873. 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU,
  874. 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U,
  875. 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U,
  876. 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U,
  877. 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U,
  878. 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U,
  879. 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU,
  880. 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U,
  881. 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU,
  882. 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU,
  883. 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U,
  884. 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U,
  885. 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U,
  886. 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU,
  887. 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U,
  888. 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU,
  889. 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU,
  890. 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U,
  891. 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U,
  892. 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU,
  893. 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U,
  894. 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU,
  895. 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U,
  896. 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U,
  897. 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U,
  898. 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U,
  899. 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU,
  900. 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U,
  901. 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU,
  902. 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U,
  903. 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU,
  904. 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U,
  905. 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU,
  906. 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU,
  907. 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU,
  908. 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU,
  909. 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U,
  910. 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U,
  911. 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U,
  912. 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U,
  913. 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U,
  914. 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U,
  915. 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU,
  916. 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U,
  917. 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU,
  918. 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU,
  919. }
  920. };
  921. #ifdef HAVE_AES_DECRYPT
  922. static const word32 Td[4][256] = {
  923. {
  924. 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U,
  925. 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U,
  926. 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U,
  927. 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU,
  928. 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U,
  929. 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U,
  930. 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU,
  931. 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U,
  932. 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU,
  933. 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U,
  934. 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U,
  935. 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U,
  936. 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U,
  937. 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU,
  938. 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U,
  939. 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU,
  940. 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U,
  941. 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU,
  942. 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U,
  943. 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U,
  944. 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U,
  945. 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU,
  946. 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U,
  947. 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU,
  948. 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U,
  949. 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU,
  950. 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U,
  951. 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU,
  952. 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU,
  953. 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U,
  954. 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU,
  955. 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U,
  956. 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU,
  957. 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U,
  958. 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U,
  959. 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U,
  960. 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU,
  961. 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U,
  962. 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U,
  963. 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU,
  964. 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U,
  965. 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U,
  966. 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U,
  967. 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U,
  968. 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U,
  969. 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU,
  970. 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U,
  971. 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U,
  972. 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U,
  973. 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U,
  974. 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U,
  975. 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU,
  976. 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU,
  977. 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU,
  978. 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU,
  979. 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U,
  980. 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U,
  981. 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU,
  982. 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU,
  983. 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U,
  984. 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU,
  985. 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U,
  986. 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U,
  987. 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U,
  988. },
  989. {
  990. 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU,
  991. 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U,
  992. 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU,
  993. 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U,
  994. 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U,
  995. 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U,
  996. 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U,
  997. 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U,
  998. 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U,
  999. 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU,
  1000. 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU,
  1001. 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU,
  1002. 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U,
  1003. 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU,
  1004. 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U,
  1005. 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U,
  1006. 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U,
  1007. 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU,
  1008. 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU,
  1009. 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U,
  1010. 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU,
  1011. 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U,
  1012. 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU,
  1013. 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU,
  1014. 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U,
  1015. 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U,
  1016. 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U,
  1017. 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU,
  1018. 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U,
  1019. 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU,
  1020. 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U,
  1021. 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U,
  1022. 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U,
  1023. 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU,
  1024. 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U,
  1025. 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U,
  1026. 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U,
  1027. 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U,
  1028. 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U,
  1029. 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U,
  1030. 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU,
  1031. 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU,
  1032. 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U,
  1033. 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU,
  1034. 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U,
  1035. 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU,
  1036. 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU,
  1037. 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U,
  1038. 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU,
  1039. 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U,
  1040. 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U,
  1041. 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U,
  1042. 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U,
  1043. 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U,
  1044. 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U,
  1045. 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U,
  1046. 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU,
  1047. 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U,
  1048. 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U,
  1049. 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU,
  1050. 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U,
  1051. 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U,
  1052. 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U,
  1053. 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U,
  1054. },
  1055. {
  1056. 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U,
  1057. 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U,
  1058. 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U,
  1059. 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U,
  1060. 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU,
  1061. 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U,
  1062. 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U,
  1063. 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U,
  1064. 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U,
  1065. 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU,
  1066. 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U,
  1067. 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U,
  1068. 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU,
  1069. 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U,
  1070. 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U,
  1071. 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U,
  1072. 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U,
  1073. 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U,
  1074. 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U,
  1075. 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU,
  1076. 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U,
  1077. 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U,
  1078. 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U,
  1079. 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U,
  1080. 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U,
  1081. 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU,
  1082. 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU,
  1083. 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U,
  1084. 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU,
  1085. 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U,
  1086. 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU,
  1087. 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU,
  1088. 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU,
  1089. 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU,
  1090. 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U,
  1091. 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U,
  1092. 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U,
  1093. 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U,
  1094. 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U,
  1095. 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U,
  1096. 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U,
  1097. 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU,
  1098. 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU,
  1099. 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U,
  1100. 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U,
  1101. 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU,
  1102. 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU,
  1103. 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U,
  1104. 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U,
  1105. 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U,
  1106. 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U,
  1107. 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U,
  1108. 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U,
  1109. 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U,
  1110. 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU,
  1111. 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U,
  1112. 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U,
  1113. 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U,
  1114. 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U,
  1115. 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U,
  1116. 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U,
  1117. 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU,
  1118. 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U,
  1119. 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U,
  1120. },
  1121. {
  1122. 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU,
  1123. 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU,
  1124. 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U,
  1125. 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U,
  1126. 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU,
  1127. 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU,
  1128. 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U,
  1129. 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU,
  1130. 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U,
  1131. 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU,
  1132. 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U,
  1133. 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U,
  1134. 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U,
  1135. 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U,
  1136. 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U,
  1137. 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU,
  1138. 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU,
  1139. 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U,
  1140. 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U,
  1141. 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU,
  1142. 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU,
  1143. 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U,
  1144. 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U,
  1145. 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U,
  1146. 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U,
  1147. 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU,
  1148. 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U,
  1149. 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U,
  1150. 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU,
  1151. 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU,
  1152. 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U,
  1153. 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U,
  1154. 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U,
  1155. 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU,
  1156. 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U,
  1157. 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U,
  1158. 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U,
  1159. 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U,
  1160. 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U,
  1161. 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U,
  1162. 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U,
  1163. 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU,
  1164. 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U,
  1165. 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U,
  1166. 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU,
  1167. 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU,
  1168. 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U,
  1169. 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU,
  1170. 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U,
  1171. 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U,
  1172. 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U,
  1173. 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U,
  1174. 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U,
  1175. 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U,
  1176. 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU,
  1177. 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU,
  1178. 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU,
  1179. 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU,
  1180. 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U,
  1181. 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U,
  1182. 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U,
  1183. 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU,
  1184. 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U,
  1185. 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U,
  1186. }
  1187. };
  1188. #if (defined(HAVE_AES_CBC) && !defined(WOLFSSL_DEVCRYPTO_CBC)) \
  1189. || defined(WOLFSSL_AES_DIRECT)
  1190. static const byte Td4[256] =
  1191. {
  1192. 0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U,
  1193. 0xbfU, 0x40U, 0xa3U, 0x9eU, 0x81U, 0xf3U, 0xd7U, 0xfbU,
  1194. 0x7cU, 0xe3U, 0x39U, 0x82U, 0x9bU, 0x2fU, 0xffU, 0x87U,
  1195. 0x34U, 0x8eU, 0x43U, 0x44U, 0xc4U, 0xdeU, 0xe9U, 0xcbU,
  1196. 0x54U, 0x7bU, 0x94U, 0x32U, 0xa6U, 0xc2U, 0x23U, 0x3dU,
  1197. 0xeeU, 0x4cU, 0x95U, 0x0bU, 0x42U, 0xfaU, 0xc3U, 0x4eU,
  1198. 0x08U, 0x2eU, 0xa1U, 0x66U, 0x28U, 0xd9U, 0x24U, 0xb2U,
  1199. 0x76U, 0x5bU, 0xa2U, 0x49U, 0x6dU, 0x8bU, 0xd1U, 0x25U,
  1200. 0x72U, 0xf8U, 0xf6U, 0x64U, 0x86U, 0x68U, 0x98U, 0x16U,
  1201. 0xd4U, 0xa4U, 0x5cU, 0xccU, 0x5dU, 0x65U, 0xb6U, 0x92U,
  1202. 0x6cU, 0x70U, 0x48U, 0x50U, 0xfdU, 0xedU, 0xb9U, 0xdaU,
  1203. 0x5eU, 0x15U, 0x46U, 0x57U, 0xa7U, 0x8dU, 0x9dU, 0x84U,
  1204. 0x90U, 0xd8U, 0xabU, 0x00U, 0x8cU, 0xbcU, 0xd3U, 0x0aU,
  1205. 0xf7U, 0xe4U, 0x58U, 0x05U, 0xb8U, 0xb3U, 0x45U, 0x06U,
  1206. 0xd0U, 0x2cU, 0x1eU, 0x8fU, 0xcaU, 0x3fU, 0x0fU, 0x02U,
  1207. 0xc1U, 0xafU, 0xbdU, 0x03U, 0x01U, 0x13U, 0x8aU, 0x6bU,
  1208. 0x3aU, 0x91U, 0x11U, 0x41U, 0x4fU, 0x67U, 0xdcU, 0xeaU,
  1209. 0x97U, 0xf2U, 0xcfU, 0xceU, 0xf0U, 0xb4U, 0xe6U, 0x73U,
  1210. 0x96U, 0xacU, 0x74U, 0x22U, 0xe7U, 0xadU, 0x35U, 0x85U,
  1211. 0xe2U, 0xf9U, 0x37U, 0xe8U, 0x1cU, 0x75U, 0xdfU, 0x6eU,
  1212. 0x47U, 0xf1U, 0x1aU, 0x71U, 0x1dU, 0x29U, 0xc5U, 0x89U,
  1213. 0x6fU, 0xb7U, 0x62U, 0x0eU, 0xaaU, 0x18U, 0xbeU, 0x1bU,
  1214. 0xfcU, 0x56U, 0x3eU, 0x4bU, 0xc6U, 0xd2U, 0x79U, 0x20U,
  1215. 0x9aU, 0xdbU, 0xc0U, 0xfeU, 0x78U, 0xcdU, 0x5aU, 0xf4U,
  1216. 0x1fU, 0xddU, 0xa8U, 0x33U, 0x88U, 0x07U, 0xc7U, 0x31U,
  1217. 0xb1U, 0x12U, 0x10U, 0x59U, 0x27U, 0x80U, 0xecU, 0x5fU,
  1218. 0x60U, 0x51U, 0x7fU, 0xa9U, 0x19U, 0xb5U, 0x4aU, 0x0dU,
  1219. 0x2dU, 0xe5U, 0x7aU, 0x9fU, 0x93U, 0xc9U, 0x9cU, 0xefU,
  1220. 0xa0U, 0xe0U, 0x3bU, 0x4dU, 0xaeU, 0x2aU, 0xf5U, 0xb0U,
  1221. 0xc8U, 0xebU, 0xbbU, 0x3cU, 0x83U, 0x53U, 0x99U, 0x61U,
  1222. 0x17U, 0x2bU, 0x04U, 0x7eU, 0xbaU, 0x77U, 0xd6U, 0x26U,
  1223. 0xe1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0cU, 0x7dU,
  1224. };
  1225. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  1226. #endif /* HAVE_AES_DECRYPT */
  1227. #define GETBYTE(x, y) (word32)((byte)((x) >> (8 * (y))))
  1228. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESGCM)
  1229. #ifndef WC_CACHE_LINE_SZ
  1230. #if defined(__x86_64__) || defined(_M_X64) || \
  1231. (defined(__ILP32__) && (__ILP32__ >= 1))
  1232. #define WC_CACHE_LINE_SZ 64
  1233. #else
  1234. /* default cache line size */
  1235. #define WC_CACHE_LINE_SZ 32
  1236. #endif
  1237. #endif
  1238. /* load 4 Te Tables into cache by cache line stride */
  1239. static WC_INLINE word32 PreFetchTe(void)
  1240. {
  1241. word32 x = 0;
  1242. int i,j;
  1243. for (i = 0; i < 4; i++) {
  1244. /* 256 elements, each one is 4 bytes */
  1245. for (j = 0; j < 256; j += WC_CACHE_LINE_SZ/4) {
  1246. x &= Te[i][j];
  1247. }
  1248. }
  1249. return x;
  1250. }
  1251. /* Software AES - ECB Encrypt */
  1252. static void wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  1253. {
  1254. word32 s0, s1, s2, s3;
  1255. word32 t0, t1, t2, t3;
  1256. word32 r = aes->rounds >> 1;
  1257. const word32* rk = aes->key;
  1258. if (r > 7 || r == 0) {
  1259. WOLFSSL_MSG("AesEncrypt encountered improper key, set it up");
  1260. return; /* stop instead of seg-faulting, set up your keys! */
  1261. }
  1262. #ifdef WOLFSSL_AESNI
  1263. if (haveAESNI && aes->use_aesni) {
  1264. #ifdef DEBUG_AESNI
  1265. printf("about to aes encrypt\n");
  1266. printf("in = %p\n", inBlock);
  1267. printf("out = %p\n", outBlock);
  1268. printf("aes->key = %p\n", aes->key);
  1269. printf("aes->rounds = %d\n", aes->rounds);
  1270. printf("sz = %d\n", AES_BLOCK_SIZE);
  1271. #endif
  1272. /* check alignment, decrypt doesn't need alignment */
  1273. if ((wolfssl_word)inBlock % AESNI_ALIGN) {
  1274. #ifndef NO_WOLFSSL_ALLOC_ALIGN
  1275. byte* tmp = (byte*)XMALLOC(AES_BLOCK_SIZE + AESNI_ALIGN, aes->heap,
  1276. DYNAMIC_TYPE_TMP_BUFFER);
  1277. byte* tmp_align;
  1278. if (tmp == NULL) return;
  1279. tmp_align = tmp + (AESNI_ALIGN - ((size_t)tmp % AESNI_ALIGN));
  1280. XMEMCPY(tmp_align, inBlock, AES_BLOCK_SIZE);
  1281. AES_ECB_encrypt(tmp_align, tmp_align, AES_BLOCK_SIZE, (byte*)aes->key,
  1282. aes->rounds);
  1283. XMEMCPY(outBlock, tmp_align, AES_BLOCK_SIZE);
  1284. XFREE(tmp, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1285. return;
  1286. #else
  1287. WOLFSSL_MSG("AES-ECB encrypt with bad alignment");
  1288. return;
  1289. #endif
  1290. }
  1291. AES_ECB_encrypt(inBlock, outBlock, AES_BLOCK_SIZE, (byte*)aes->key,
  1292. aes->rounds);
  1293. return;
  1294. }
  1295. else {
  1296. #ifdef DEBUG_AESNI
  1297. printf("Skipping AES-NI\n");
  1298. #endif
  1299. }
  1300. #endif
  1301. /*
  1302. * map byte array block to cipher state
  1303. * and add initial round key:
  1304. */
  1305. XMEMCPY(&s0, inBlock, sizeof(s0));
  1306. XMEMCPY(&s1, inBlock + sizeof(s0), sizeof(s1));
  1307. XMEMCPY(&s2, inBlock + 2 * sizeof(s0), sizeof(s2));
  1308. XMEMCPY(&s3, inBlock + 3 * sizeof(s0), sizeof(s3));
  1309. #ifdef LITTLE_ENDIAN_ORDER
  1310. s0 = ByteReverseWord32(s0);
  1311. s1 = ByteReverseWord32(s1);
  1312. s2 = ByteReverseWord32(s2);
  1313. s3 = ByteReverseWord32(s3);
  1314. #endif
  1315. s0 ^= rk[0];
  1316. s1 ^= rk[1];
  1317. s2 ^= rk[2];
  1318. s3 ^= rk[3];
  1319. s0 |= PreFetchTe();
  1320. /*
  1321. * Nr - 1 full rounds:
  1322. */
  1323. for (;;) {
  1324. t0 =
  1325. Te[0][GETBYTE(s0, 3)] ^
  1326. Te[1][GETBYTE(s1, 2)] ^
  1327. Te[2][GETBYTE(s2, 1)] ^
  1328. Te[3][GETBYTE(s3, 0)] ^
  1329. rk[4];
  1330. t1 =
  1331. Te[0][GETBYTE(s1, 3)] ^
  1332. Te[1][GETBYTE(s2, 2)] ^
  1333. Te[2][GETBYTE(s3, 1)] ^
  1334. Te[3][GETBYTE(s0, 0)] ^
  1335. rk[5];
  1336. t2 =
  1337. Te[0][GETBYTE(s2, 3)] ^
  1338. Te[1][GETBYTE(s3, 2)] ^
  1339. Te[2][GETBYTE(s0, 1)] ^
  1340. Te[3][GETBYTE(s1, 0)] ^
  1341. rk[6];
  1342. t3 =
  1343. Te[0][GETBYTE(s3, 3)] ^
  1344. Te[1][GETBYTE(s0, 2)] ^
  1345. Te[2][GETBYTE(s1, 1)] ^
  1346. Te[3][GETBYTE(s2, 0)] ^
  1347. rk[7];
  1348. rk += 8;
  1349. if (--r == 0) {
  1350. break;
  1351. }
  1352. s0 =
  1353. Te[0][GETBYTE(t0, 3)] ^
  1354. Te[1][GETBYTE(t1, 2)] ^
  1355. Te[2][GETBYTE(t2, 1)] ^
  1356. Te[3][GETBYTE(t3, 0)] ^
  1357. rk[0];
  1358. s1 =
  1359. Te[0][GETBYTE(t1, 3)] ^
  1360. Te[1][GETBYTE(t2, 2)] ^
  1361. Te[2][GETBYTE(t3, 1)] ^
  1362. Te[3][GETBYTE(t0, 0)] ^
  1363. rk[1];
  1364. s2 =
  1365. Te[0][GETBYTE(t2, 3)] ^
  1366. Te[1][GETBYTE(t3, 2)] ^
  1367. Te[2][GETBYTE(t0, 1)] ^
  1368. Te[3][GETBYTE(t1, 0)] ^
  1369. rk[2];
  1370. s3 =
  1371. Te[0][GETBYTE(t3, 3)] ^
  1372. Te[1][GETBYTE(t0, 2)] ^
  1373. Te[2][GETBYTE(t1, 1)] ^
  1374. Te[3][GETBYTE(t2, 0)] ^
  1375. rk[3];
  1376. }
  1377. /*
  1378. * apply last round and
  1379. * map cipher state to byte array block:
  1380. */
  1381. s0 =
  1382. (Te[2][GETBYTE(t0, 3)] & 0xff000000) ^
  1383. (Te[3][GETBYTE(t1, 2)] & 0x00ff0000) ^
  1384. (Te[0][GETBYTE(t2, 1)] & 0x0000ff00) ^
  1385. (Te[1][GETBYTE(t3, 0)] & 0x000000ff) ^
  1386. rk[0];
  1387. s1 =
  1388. (Te[2][GETBYTE(t1, 3)] & 0xff000000) ^
  1389. (Te[3][GETBYTE(t2, 2)] & 0x00ff0000) ^
  1390. (Te[0][GETBYTE(t3, 1)] & 0x0000ff00) ^
  1391. (Te[1][GETBYTE(t0, 0)] & 0x000000ff) ^
  1392. rk[1];
  1393. s2 =
  1394. (Te[2][GETBYTE(t2, 3)] & 0xff000000) ^
  1395. (Te[3][GETBYTE(t3, 2)] & 0x00ff0000) ^
  1396. (Te[0][GETBYTE(t0, 1)] & 0x0000ff00) ^
  1397. (Te[1][GETBYTE(t1, 0)] & 0x000000ff) ^
  1398. rk[2];
  1399. s3 =
  1400. (Te[2][GETBYTE(t3, 3)] & 0xff000000) ^
  1401. (Te[3][GETBYTE(t0, 2)] & 0x00ff0000) ^
  1402. (Te[0][GETBYTE(t1, 1)] & 0x0000ff00) ^
  1403. (Te[1][GETBYTE(t2, 0)] & 0x000000ff) ^
  1404. rk[3];
  1405. /* write out */
  1406. #ifdef LITTLE_ENDIAN_ORDER
  1407. s0 = ByteReverseWord32(s0);
  1408. s1 = ByteReverseWord32(s1);
  1409. s2 = ByteReverseWord32(s2);
  1410. s3 = ByteReverseWord32(s3);
  1411. #endif
  1412. XMEMCPY(outBlock, &s0, sizeof(s0));
  1413. XMEMCPY(outBlock + sizeof(s0), &s1, sizeof(s1));
  1414. XMEMCPY(outBlock + 2 * sizeof(s0), &s2, sizeof(s2));
  1415. XMEMCPY(outBlock + 3 * sizeof(s0), &s3, sizeof(s3));
  1416. }
  1417. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT || HAVE_AESGCM */
  1418. #if defined(HAVE_AES_DECRYPT)
  1419. #if (defined(HAVE_AES_CBC) && !defined(WOLFSSL_DEVCRYPTO_CBC)) || \
  1420. defined(WOLFSSL_AES_DIRECT)
  1421. /* load 4 Td Tables into cache by cache line stride */
  1422. static WC_INLINE word32 PreFetchTd(void)
  1423. {
  1424. word32 x = 0;
  1425. int i,j;
  1426. for (i = 0; i < 4; i++) {
  1427. /* 256 elements, each one is 4 bytes */
  1428. for (j = 0; j < 256; j += WC_CACHE_LINE_SZ/4) {
  1429. x &= Td[i][j];
  1430. }
  1431. }
  1432. return x;
  1433. }
  1434. /* load Td Table4 into cache by cache line stride */
  1435. static WC_INLINE word32 PreFetchTd4(void)
  1436. {
  1437. word32 x = 0;
  1438. int i;
  1439. for (i = 0; i < 256; i += WC_CACHE_LINE_SZ) {
  1440. x &= (word32)Td4[i];
  1441. }
  1442. return x;
  1443. }
  1444. /* Software AES - ECB Decrypt */
  1445. static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
  1446. {
  1447. word32 s0, s1, s2, s3;
  1448. word32 t0, t1, t2, t3;
  1449. word32 r = aes->rounds >> 1;
  1450. const word32* rk = aes->key;
  1451. if (r > 7 || r == 0) {
  1452. WOLFSSL_MSG("AesDecrypt encountered improper key, set it up");
  1453. return; /* stop instead of seg-faulting, set up your keys! */
  1454. }
  1455. #ifdef WOLFSSL_AESNI
  1456. if (haveAESNI && aes->use_aesni) {
  1457. #ifdef DEBUG_AESNI
  1458. printf("about to aes decrypt\n");
  1459. printf("in = %p\n", inBlock);
  1460. printf("out = %p\n", outBlock);
  1461. printf("aes->key = %p\n", aes->key);
  1462. printf("aes->rounds = %d\n", aes->rounds);
  1463. printf("sz = %d\n", AES_BLOCK_SIZE);
  1464. #endif
  1465. /* if input and output same will overwrite input iv */
  1466. if ((const byte*)aes->tmp != inBlock)
  1467. XMEMCPY(aes->tmp, inBlock, AES_BLOCK_SIZE);
  1468. AES_ECB_decrypt(inBlock, outBlock, AES_BLOCK_SIZE, (byte*)aes->key,
  1469. aes->rounds);
  1470. return;
  1471. }
  1472. else {
  1473. #ifdef DEBUG_AESNI
  1474. printf("Skipping AES-NI\n");
  1475. #endif
  1476. }
  1477. #endif /* WOLFSSL_AESNI */
  1478. /*
  1479. * map byte array block to cipher state
  1480. * and add initial round key:
  1481. */
  1482. XMEMCPY(&s0, inBlock, sizeof(s0));
  1483. XMEMCPY(&s1, inBlock + sizeof(s0), sizeof(s1));
  1484. XMEMCPY(&s2, inBlock + 2 * sizeof(s0), sizeof(s2));
  1485. XMEMCPY(&s3, inBlock + 3 * sizeof(s0), sizeof(s3));
  1486. #ifdef LITTLE_ENDIAN_ORDER
  1487. s0 = ByteReverseWord32(s0);
  1488. s1 = ByteReverseWord32(s1);
  1489. s2 = ByteReverseWord32(s2);
  1490. s3 = ByteReverseWord32(s3);
  1491. #endif
  1492. s0 ^= rk[0];
  1493. s1 ^= rk[1];
  1494. s2 ^= rk[2];
  1495. s3 ^= rk[3];
  1496. s0 |= PreFetchTd();
  1497. /*
  1498. * Nr - 1 full rounds:
  1499. */
  1500. for (;;) {
  1501. t0 =
  1502. Td[0][GETBYTE(s0, 3)] ^
  1503. Td[1][GETBYTE(s3, 2)] ^
  1504. Td[2][GETBYTE(s2, 1)] ^
  1505. Td[3][GETBYTE(s1, 0)] ^
  1506. rk[4];
  1507. t1 =
  1508. Td[0][GETBYTE(s1, 3)] ^
  1509. Td[1][GETBYTE(s0, 2)] ^
  1510. Td[2][GETBYTE(s3, 1)] ^
  1511. Td[3][GETBYTE(s2, 0)] ^
  1512. rk[5];
  1513. t2 =
  1514. Td[0][GETBYTE(s2, 3)] ^
  1515. Td[1][GETBYTE(s1, 2)] ^
  1516. Td[2][GETBYTE(s0, 1)] ^
  1517. Td[3][GETBYTE(s3, 0)] ^
  1518. rk[6];
  1519. t3 =
  1520. Td[0][GETBYTE(s3, 3)] ^
  1521. Td[1][GETBYTE(s2, 2)] ^
  1522. Td[2][GETBYTE(s1, 1)] ^
  1523. Td[3][GETBYTE(s0, 0)] ^
  1524. rk[7];
  1525. rk += 8;
  1526. if (--r == 0) {
  1527. break;
  1528. }
  1529. s0 =
  1530. Td[0][GETBYTE(t0, 3)] ^
  1531. Td[1][GETBYTE(t3, 2)] ^
  1532. Td[2][GETBYTE(t2, 1)] ^
  1533. Td[3][GETBYTE(t1, 0)] ^
  1534. rk[0];
  1535. s1 =
  1536. Td[0][GETBYTE(t1, 3)] ^
  1537. Td[1][GETBYTE(t0, 2)] ^
  1538. Td[2][GETBYTE(t3, 1)] ^
  1539. Td[3][GETBYTE(t2, 0)] ^
  1540. rk[1];
  1541. s2 =
  1542. Td[0][GETBYTE(t2, 3)] ^
  1543. Td[1][GETBYTE(t1, 2)] ^
  1544. Td[2][GETBYTE(t0, 1)] ^
  1545. Td[3][GETBYTE(t3, 0)] ^
  1546. rk[2];
  1547. s3 =
  1548. Td[0][GETBYTE(t3, 3)] ^
  1549. Td[1][GETBYTE(t2, 2)] ^
  1550. Td[2][GETBYTE(t1, 1)] ^
  1551. Td[3][GETBYTE(t0, 0)] ^
  1552. rk[3];
  1553. }
  1554. /*
  1555. * apply last round and
  1556. * map cipher state to byte array block:
  1557. */
  1558. t0 |= PreFetchTd4();
  1559. s0 =
  1560. ((word32)Td4[GETBYTE(t0, 3)] << 24) ^
  1561. ((word32)Td4[GETBYTE(t3, 2)] << 16) ^
  1562. ((word32)Td4[GETBYTE(t2, 1)] << 8) ^
  1563. ((word32)Td4[GETBYTE(t1, 0)]) ^
  1564. rk[0];
  1565. s1 =
  1566. ((word32)Td4[GETBYTE(t1, 3)] << 24) ^
  1567. ((word32)Td4[GETBYTE(t0, 2)] << 16) ^
  1568. ((word32)Td4[GETBYTE(t3, 1)] << 8) ^
  1569. ((word32)Td4[GETBYTE(t2, 0)]) ^
  1570. rk[1];
  1571. s2 =
  1572. ((word32)Td4[GETBYTE(t2, 3)] << 24) ^
  1573. ((word32)Td4[GETBYTE(t1, 2)] << 16) ^
  1574. ((word32)Td4[GETBYTE(t0, 1)] << 8) ^
  1575. ((word32)Td4[GETBYTE(t3, 0)]) ^
  1576. rk[2];
  1577. s3 =
  1578. ((word32)Td4[GETBYTE(t3, 3)] << 24) ^
  1579. ((word32)Td4[GETBYTE(t2, 2)] << 16) ^
  1580. ((word32)Td4[GETBYTE(t1, 1)] << 8) ^
  1581. ((word32)Td4[GETBYTE(t0, 0)]) ^
  1582. rk[3];
  1583. /* write out */
  1584. #ifdef LITTLE_ENDIAN_ORDER
  1585. s0 = ByteReverseWord32(s0);
  1586. s1 = ByteReverseWord32(s1);
  1587. s2 = ByteReverseWord32(s2);
  1588. s3 = ByteReverseWord32(s3);
  1589. #endif
  1590. XMEMCPY(outBlock, &s0, sizeof(s0));
  1591. XMEMCPY(outBlock + sizeof(s0), &s1, sizeof(s1));
  1592. XMEMCPY(outBlock + 2 * sizeof(s0), &s2, sizeof(s2));
  1593. XMEMCPY(outBlock + 3 * sizeof(s0), &s3, sizeof(s3));
  1594. }
  1595. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  1596. #endif /* HAVE_AES_DECRYPT */
  1597. #endif /* NEED_AES_TABLES */
  1598. /* wc_AesSetKey */
  1599. #if defined(STM32_CRYPTO)
  1600. int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
  1601. const byte* iv, int dir)
  1602. {
  1603. word32 *rk = aes->key;
  1604. (void)dir;
  1605. if (keylen != 16 &&
  1606. #ifdef WOLFSSL_AES_192
  1607. keylen != 24 &&
  1608. #endif
  1609. keylen != 32) {
  1610. return BAD_FUNC_ARG;
  1611. }
  1612. aes->keylen = keylen;
  1613. aes->rounds = keylen/4 + 6;
  1614. XMEMCPY(rk, userKey, keylen);
  1615. #if !defined(WOLFSSL_STM32_CUBEMX) || defined(STM32_HAL_V2)
  1616. ByteReverseWords(rk, rk, keylen);
  1617. #endif
  1618. #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER)
  1619. aes->left = 0;
  1620. #endif
  1621. return wc_AesSetIV(aes, iv);
  1622. }
  1623. #if defined(WOLFSSL_AES_DIRECT)
  1624. int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
  1625. const byte* iv, int dir)
  1626. {
  1627. return wc_AesSetKey(aes, userKey, keylen, iv, dir);
  1628. }
  1629. #endif
  1630. #elif defined(HAVE_COLDFIRE_SEC)
  1631. #if defined (HAVE_THREADX)
  1632. #include "memory_pools.h"
  1633. extern TX_BYTE_POOL mp_ncached; /* Non Cached memory pool */
  1634. #endif
  1635. #define AES_BUFFER_SIZE (AES_BLOCK_SIZE * 64)
  1636. static unsigned char *AESBuffIn = NULL;
  1637. static unsigned char *AESBuffOut = NULL;
  1638. static byte *secReg;
  1639. static byte *secKey;
  1640. static volatile SECdescriptorType *secDesc;
  1641. static wolfSSL_Mutex Mutex_AesSEC;
  1642. #define SEC_DESC_AES_CBC_ENCRYPT 0x60300010
  1643. #define SEC_DESC_AES_CBC_DECRYPT 0x60200010
  1644. extern volatile unsigned char __MBAR[];
  1645. int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
  1646. const byte* iv, int dir)
  1647. {
  1648. if (AESBuffIn == NULL) {
  1649. #if defined (HAVE_THREADX)
  1650. int s1, s2, s3, s4, s5;
  1651. s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc,
  1652. sizeof(SECdescriptorType), TX_NO_WAIT);
  1653. s1 = tx_byte_allocate(&mp_ncached, (void *)&AESBuffIn,
  1654. AES_BUFFER_SIZE, TX_NO_WAIT);
  1655. s2 = tx_byte_allocate(&mp_ncached, (void *)&AESBuffOut,
  1656. AES_BUFFER_SIZE, TX_NO_WAIT);
  1657. s3 = tx_byte_allocate(&mp_ncached, (void *)&secKey,
  1658. AES_BLOCK_SIZE*2, TX_NO_WAIT);
  1659. s4 = tx_byte_allocate(&mp_ncached, (void *)&secReg,
  1660. AES_BLOCK_SIZE, TX_NO_WAIT);
  1661. if (s1 || s2 || s3 || s4 || s5)
  1662. return BAD_FUNC_ARG;
  1663. #else
  1664. #warning "Allocate non-Cache buffers"
  1665. #endif
  1666. wc_InitMutex(&Mutex_AesSEC);
  1667. }
  1668. if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
  1669. return BAD_FUNC_ARG;
  1670. if (aes == NULL)
  1671. return BAD_FUNC_ARG;
  1672. aes->keylen = keylen;
  1673. aes->rounds = keylen/4 + 6;
  1674. XMEMCPY(aes->key, userKey, keylen);
  1675. if (iv)
  1676. XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
  1677. #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER)
  1678. aes->left = 0;
  1679. #endif
  1680. return 0;
  1681. }
  1682. #elif defined(FREESCALE_LTC)
  1683. int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv,
  1684. int dir)
  1685. {
  1686. if (aes == NULL || !((keylen == 16) || (keylen == 24) || (keylen == 32)))
  1687. return BAD_FUNC_ARG;
  1688. aes->rounds = keylen/4 + 6;
  1689. XMEMCPY(aes->key, userKey, keylen);
  1690. #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER)
  1691. aes->left = 0;
  1692. #endif
  1693. return wc_AesSetIV(aes, iv);
  1694. }
  1695. int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
  1696. const byte* iv, int dir)
  1697. {
  1698. return wc_AesSetKey(aes, userKey, keylen, iv, dir);
  1699. }
  1700. #elif defined(FREESCALE_MMCAU)
  1701. int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
  1702. const byte* iv, int dir)
  1703. {
  1704. int ret;
  1705. byte* rk = (byte*)aes->key;
  1706. byte* tmpKey = (byte*)userKey;
  1707. int tmpKeyDynamic = 0;
  1708. word32 alignOffset = 0;
  1709. (void)dir;
  1710. if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
  1711. return BAD_FUNC_ARG;
  1712. if (rk == NULL)
  1713. return BAD_FUNC_ARG;
  1714. #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER)
  1715. aes->left = 0;
  1716. #endif
  1717. aes->rounds = keylen/4 + 6;
  1718. #ifdef FREESCALE_MMCAU_CLASSIC
  1719. if ((wolfssl_word)userKey % WOLFSSL_MMCAU_ALIGNMENT) {
  1720. #ifndef NO_WOLFSSL_ALLOC_ALIGN
  1721. byte* tmp = (byte*)XMALLOC(keylen + WOLFSSL_MMCAU_ALIGNMENT,
  1722. aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1723. if (tmp == NULL) {
  1724. return MEMORY_E;
  1725. }
  1726. alignOffset = WOLFSSL_MMCAU_ALIGNMENT -
  1727. ((wolfssl_word)tmp % WOLFSSL_MMCAU_ALIGNMENT);
  1728. tmpKey = tmp + alignOffset;
  1729. XMEMCPY(tmpKey, userKey, keylen);
  1730. tmpKeyDynamic = 1;
  1731. #else
  1732. WOLFSSL_MSG("Bad cau_aes_set_key alignment");
  1733. return BAD_ALIGN_E;
  1734. #endif
  1735. }
  1736. #endif
  1737. ret = wolfSSL_CryptHwMutexLock();
  1738. if(ret == 0) {
  1739. #ifdef FREESCALE_MMCAU_CLASSIC
  1740. cau_aes_set_key(tmpKey, keylen*8, rk);
  1741. #else
  1742. MMCAU_AES_SetKey(tmpKey, keylen, rk);
  1743. #endif
  1744. wolfSSL_CryptHwMutexUnLock();
  1745. ret = wc_AesSetIV(aes, iv);
  1746. }
  1747. if (tmpKeyDynamic == 1) {
  1748. XFREE(tmpKey - alignOffset, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1749. }
  1750. return ret;
  1751. }
  1752. int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
  1753. const byte* iv, int dir)
  1754. {
  1755. return wc_AesSetKey(aes, userKey, keylen, iv, dir);
  1756. }
  1757. #elif defined(WOLFSSL_NRF51_AES)
  1758. int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
  1759. const byte* iv, int dir)
  1760. {
  1761. int ret;
  1762. (void)dir;
  1763. (void)iv;
  1764. if (keylen != 16)
  1765. return BAD_FUNC_ARG;
  1766. aes->keylen = keylen;
  1767. aes->rounds = keylen/4 + 6;
  1768. ret = nrf51_aes_set_key(userKey);
  1769. #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER)
  1770. aes->left = 0;
  1771. #endif
  1772. return ret;
  1773. }
  1774. int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
  1775. const byte* iv, int dir)
  1776. {
  1777. return wc_AesSetKey(aes, userKey, keylen, iv, dir);
  1778. }
  1779. #elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
  1780. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
  1781. int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
  1782. const byte* iv, int dir)
  1783. {
  1784. (void)dir;
  1785. (void)iv;
  1786. if ( aes == NULL || (keylen != 16 && keylen != 24 && keylen != 32)) {
  1787. return BAD_FUNC_ARG;
  1788. }
  1789. aes->keylen = keylen;
  1790. aes->rounds = keylen/4 + 6;
  1791. XMEMCPY(aes->key, userKey, keylen);
  1792. return wc_AesSetIV(aes, iv);
  1793. }
  1794. int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
  1795. const byte* iv, int dir)
  1796. {
  1797. return wc_AesSetKey(aes, userKey, keylen, iv, dir);
  1798. }
  1799. #elif defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES)
  1800. int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv,
  1801. int dir)
  1802. {
  1803. SaSiError_t ret = SASI_OK;
  1804. SaSiAesIv_t iv_aes;
  1805. if (aes == NULL ||
  1806. (keylen != AES_128_KEY_SIZE &&
  1807. keylen != AES_192_KEY_SIZE &&
  1808. keylen != AES_256_KEY_SIZE)) {
  1809. return BAD_FUNC_ARG;
  1810. }
  1811. #if defined(AES_MAX_KEY_SIZE)
  1812. if (keylen > (AES_MAX_KEY_SIZE/8)) {
  1813. return BAD_FUNC_ARG;
  1814. }
  1815. #endif
  1816. if (dir != AES_ENCRYPTION &&
  1817. dir != AES_DECRYPTION) {
  1818. return BAD_FUNC_ARG;
  1819. }
  1820. if (dir == AES_ENCRYPTION) {
  1821. aes->ctx.mode = SASI_AES_ENCRYPT;
  1822. SaSi_AesInit(&aes->ctx.user_ctx,
  1823. SASI_AES_ENCRYPT,
  1824. SASI_AES_MODE_CBC,
  1825. SASI_AES_PADDING_NONE);
  1826. }
  1827. else {
  1828. aes->ctx.mode = SASI_AES_DECRYPT;
  1829. SaSi_AesInit(&aes->ctx.user_ctx,
  1830. SASI_AES_DECRYPT,
  1831. SASI_AES_MODE_CBC,
  1832. SASI_AES_PADDING_NONE);
  1833. }
  1834. aes->keylen = keylen;
  1835. aes->rounds = keylen/4 + 6;
  1836. XMEMCPY(aes->key, userKey, keylen);
  1837. aes->ctx.key.pKey = (uint8_t*)aes->key;
  1838. aes->ctx.key.keySize= keylen;
  1839. ret = SaSi_AesSetKey(&aes->ctx.user_ctx,
  1840. SASI_AES_USER_KEY,
  1841. &aes->ctx.key,
  1842. sizeof(aes->ctx.key));
  1843. if (ret != SASI_OK) {
  1844. return BAD_FUNC_ARG;
  1845. }
  1846. ret = wc_AesSetIV(aes, iv);
  1847. if (iv)
  1848. XMEMCPY(iv_aes, iv, AES_BLOCK_SIZE);
  1849. else
  1850. XMEMSET(iv_aes, 0, AES_BLOCK_SIZE);
  1851. ret = SaSi_AesSetIv(&aes->ctx.user_ctx, iv_aes);
  1852. if (ret != SASI_OK) {
  1853. return ret;
  1854. }
  1855. return ret;
  1856. }
  1857. #if defined(WOLFSSL_AES_DIRECT)
  1858. int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
  1859. const byte* iv, int dir)
  1860. {
  1861. return wc_AesSetKey(aes, userKey, keylen, iv, dir);
  1862. }
  1863. #endif
  1864. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
  1865. /* implemented in wolfcrypt/src/port/caam/caam_aes.c */
  1866. #elif defined(WOLFSSL_AFALG)
  1867. /* implemented in wolfcrypt/src/port/af_alg/afalg_aes.c */
  1868. #elif defined(WOLFSSL_DEVCRYPTO_AES)
  1869. /* implemented in wolfcrypt/src/port/devcrypto/devcrypto_aes.c */
  1870. #else
  1871. /* Software AES - SetKey */
  1872. static int wc_AesSetKeyLocal(Aes* aes, const byte* userKey, word32 keylen,
  1873. const byte* iv, int dir)
  1874. {
  1875. word32 *rk = aes->key;
  1876. #ifdef NEED_AES_TABLES
  1877. word32 temp;
  1878. unsigned int i = 0;
  1879. #endif
  1880. #ifdef WOLFSSL_AESNI
  1881. aes->use_aesni = 0;
  1882. #endif /* WOLFSSL_AESNI */
  1883. #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER)
  1884. aes->left = 0;
  1885. #endif
  1886. aes->keylen = keylen;
  1887. aes->rounds = (keylen/4) + 6;
  1888. XMEMCPY(rk, userKey, keylen);
  1889. #if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  1890. (!defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
  1891. defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES))
  1892. ByteReverseWords(rk, rk, keylen);
  1893. #endif
  1894. #ifdef NEED_AES_TABLES
  1895. (void)temp;
  1896. switch (keylen) {
  1897. #if defined(AES_MAX_KEY_SIZE) && AES_MAX_KEY_SIZE >= 128 && \
  1898. defined(WOLFSSL_AES_128)
  1899. case 16:
  1900. while (1)
  1901. {
  1902. temp = rk[3];
  1903. rk[4] = rk[0] ^
  1904. (Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
  1905. (Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
  1906. (Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
  1907. (Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
  1908. rcon[i];
  1909. rk[5] = rk[1] ^ rk[4];
  1910. rk[6] = rk[2] ^ rk[5];
  1911. rk[7] = rk[3] ^ rk[6];
  1912. if (++i == 10)
  1913. break;
  1914. rk += 4;
  1915. }
  1916. break;
  1917. #endif /* 128 */
  1918. #if defined(AES_MAX_KEY_SIZE) && AES_MAX_KEY_SIZE >= 192 && \
  1919. defined(WOLFSSL_AES_192)
  1920. case 24:
  1921. /* for (;;) here triggers a bug in VC60 SP4 w/ Pro Pack */
  1922. while (1)
  1923. {
  1924. temp = rk[ 5];
  1925. rk[ 6] = rk[ 0] ^
  1926. (Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
  1927. (Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
  1928. (Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
  1929. (Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
  1930. rcon[i];
  1931. rk[ 7] = rk[ 1] ^ rk[ 6];
  1932. rk[ 8] = rk[ 2] ^ rk[ 7];
  1933. rk[ 9] = rk[ 3] ^ rk[ 8];
  1934. if (++i == 8)
  1935. break;
  1936. rk[10] = rk[ 4] ^ rk[ 9];
  1937. rk[11] = rk[ 5] ^ rk[10];
  1938. rk += 6;
  1939. }
  1940. break;
  1941. #endif /* 192 */
  1942. #if defined(AES_MAX_KEY_SIZE) && AES_MAX_KEY_SIZE >= 256 && \
  1943. defined(WOLFSSL_AES_256)
  1944. case 32:
  1945. while (1)
  1946. {
  1947. temp = rk[ 7];
  1948. rk[ 8] = rk[ 0] ^
  1949. (Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
  1950. (Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
  1951. (Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
  1952. (Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
  1953. rcon[i];
  1954. rk[ 9] = rk[ 1] ^ rk[ 8];
  1955. rk[10] = rk[ 2] ^ rk[ 9];
  1956. rk[11] = rk[ 3] ^ rk[10];
  1957. if (++i == 7)
  1958. break;
  1959. temp = rk[11];
  1960. rk[12] = rk[ 4] ^
  1961. (Te[2][GETBYTE(temp, 3)] & 0xff000000) ^
  1962. (Te[3][GETBYTE(temp, 2)] & 0x00ff0000) ^
  1963. (Te[0][GETBYTE(temp, 1)] & 0x0000ff00) ^
  1964. (Te[1][GETBYTE(temp, 0)] & 0x000000ff);
  1965. rk[13] = rk[ 5] ^ rk[12];
  1966. rk[14] = rk[ 6] ^ rk[13];
  1967. rk[15] = rk[ 7] ^ rk[14];
  1968. rk += 8;
  1969. }
  1970. break;
  1971. #endif /* 256 */
  1972. default:
  1973. return BAD_FUNC_ARG;
  1974. } /* switch */
  1975. #ifdef HAVE_AES_DECRYPT
  1976. if (dir == AES_DECRYPTION) {
  1977. unsigned int j;
  1978. rk = aes->key;
  1979. /* invert the order of the round keys: */
  1980. for (i = 0, j = 4* aes->rounds; i < j; i += 4, j -= 4) {
  1981. temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
  1982. temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
  1983. temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
  1984. temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
  1985. }
  1986. /* apply the inverse MixColumn transform to all round keys but the
  1987. first and the last: */
  1988. for (i = 1; i < aes->rounds; i++) {
  1989. rk += 4;
  1990. rk[0] =
  1991. Td[0][Te[1][GETBYTE(rk[0], 3)] & 0xff] ^
  1992. Td[1][Te[1][GETBYTE(rk[0], 2)] & 0xff] ^
  1993. Td[2][Te[1][GETBYTE(rk[0], 1)] & 0xff] ^
  1994. Td[3][Te[1][GETBYTE(rk[0], 0)] & 0xff];
  1995. rk[1] =
  1996. Td[0][Te[1][GETBYTE(rk[1], 3)] & 0xff] ^
  1997. Td[1][Te[1][GETBYTE(rk[1], 2)] & 0xff] ^
  1998. Td[2][Te[1][GETBYTE(rk[1], 1)] & 0xff] ^
  1999. Td[3][Te[1][GETBYTE(rk[1], 0)] & 0xff];
  2000. rk[2] =
  2001. Td[0][Te[1][GETBYTE(rk[2], 3)] & 0xff] ^
  2002. Td[1][Te[1][GETBYTE(rk[2], 2)] & 0xff] ^
  2003. Td[2][Te[1][GETBYTE(rk[2], 1)] & 0xff] ^
  2004. Td[3][Te[1][GETBYTE(rk[2], 0)] & 0xff];
  2005. rk[3] =
  2006. Td[0][Te[1][GETBYTE(rk[3], 3)] & 0xff] ^
  2007. Td[1][Te[1][GETBYTE(rk[3], 2)] & 0xff] ^
  2008. Td[2][Te[1][GETBYTE(rk[3], 1)] & 0xff] ^
  2009. Td[3][Te[1][GETBYTE(rk[3], 0)] & 0xff];
  2010. }
  2011. }
  2012. #else
  2013. (void)dir;
  2014. #endif /* HAVE_AES_DECRYPT */
  2015. #endif /* NEED_AES_TABLES */
  2016. return wc_AesSetIV(aes, iv);
  2017. }
  2018. int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
  2019. const byte* iv, int dir)
  2020. {
  2021. int ret;
  2022. #if defined(AES_MAX_KEY_SIZE)
  2023. const word32 max_key_len = (AES_MAX_KEY_SIZE / 8);
  2024. #endif
  2025. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  2026. byte local[32];
  2027. word32 localSz = 32;
  2028. if (keylen == (16 + WC_CAAM_BLOB_SZ) ||
  2029. keylen == (24 + WC_CAAM_BLOB_SZ) ||
  2030. keylen == (32 + WC_CAAM_BLOB_SZ)) {
  2031. if (wc_caamOpenBlob((byte*)userKey, keylen, local, &localSz) != 0) {
  2032. return BAD_FUNC_ARG;
  2033. }
  2034. /* set local values */
  2035. userKey = local;
  2036. keylen = localSz;
  2037. }
  2038. #endif
  2039. if (aes == NULL ||
  2040. !((keylen == 16) || (keylen == 24) || (keylen == 32))) {
  2041. return BAD_FUNC_ARG;
  2042. }
  2043. #if defined(AES_MAX_KEY_SIZE)
  2044. /* Check key length */
  2045. if (keylen > max_key_len) {
  2046. return BAD_FUNC_ARG;
  2047. }
  2048. #endif
  2049. aes->keylen = keylen;
  2050. aes->rounds = keylen/4 + 6;
  2051. #if defined(WOLF_CRYPTO_CB) || (defined(WOLFSSL_DEVCRYPTO) && \
  2052. (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))) || \
  2053. (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES))
  2054. #ifdef WOLF_CRYPTO_CB
  2055. if (aes->devId != INVALID_DEVID)
  2056. #endif
  2057. {
  2058. XMEMCPY(aes->devKey, userKey, keylen);
  2059. }
  2060. #endif
  2061. #ifdef WOLFSSL_AESNI
  2062. if (checkAESNI == 0) {
  2063. haveAESNI = Check_CPU_support_AES();
  2064. checkAESNI = 1;
  2065. }
  2066. if (haveAESNI) {
  2067. #if defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_CFB)
  2068. aes->left = 0;
  2069. #endif /* WOLFSSL_AES_COUNTER */
  2070. aes->use_aesni = 1;
  2071. if (iv)
  2072. XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
  2073. else
  2074. XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
  2075. if (dir == AES_ENCRYPTION)
  2076. return AES_set_encrypt_key(userKey, keylen * 8, aes);
  2077. #ifdef HAVE_AES_DECRYPT
  2078. else
  2079. return AES_set_decrypt_key(userKey, keylen * 8, aes);
  2080. #endif
  2081. }
  2082. #endif /* WOLFSSL_AESNI */
  2083. ret = wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir);
  2084. #if defined(WOLFSSL_DEVCRYPTO) && \
  2085. (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))
  2086. aes->ctx.cfd = -1;
  2087. #endif
  2088. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  2089. ForceZero(local, sizeof(local));
  2090. #endif
  2091. return ret;
  2092. }
  2093. #if defined(WOLFSSL_AES_DIRECT) || defined(WOLFSSL_AES_COUNTER)
  2094. /* AES-CTR and AES-DIRECT need to use this for key setup, no aesni yet */
  2095. int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
  2096. const byte* iv, int dir)
  2097. {
  2098. int ret;
  2099. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  2100. byte local[32];
  2101. word32 localSz = 32;
  2102. if (keylen == (16 + WC_CAAM_BLOB_SZ) ||
  2103. keylen == (24 + WC_CAAM_BLOB_SZ) ||
  2104. keylen == (32 + WC_CAAM_BLOB_SZ)) {
  2105. if (wc_caamOpenBlob((byte*)userKey, keylen, local, &localSz)
  2106. != 0) {
  2107. return BAD_FUNC_ARG;
  2108. }
  2109. /* set local values */
  2110. userKey = local;
  2111. keylen = localSz;
  2112. }
  2113. #endif
  2114. ret = wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir);
  2115. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  2116. ForceZero(local, sizeof(local));
  2117. #endif
  2118. return ret;
  2119. }
  2120. #endif /* WOLFSSL_AES_DIRECT || WOLFSSL_AES_COUNTER */
  2121. #endif /* wc_AesSetKey block */
  2122. /* wc_AesSetIV is shared between software and hardware */
  2123. int wc_AesSetIV(Aes* aes, const byte* iv)
  2124. {
  2125. if (aes == NULL)
  2126. return BAD_FUNC_ARG;
  2127. if (iv)
  2128. XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
  2129. else
  2130. XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
  2131. return 0;
  2132. }
  2133. /* AES-DIRECT */
  2134. #if defined(WOLFSSL_AES_DIRECT)
  2135. #if defined(HAVE_COLDFIRE_SEC)
  2136. #error "Coldfire SEC doesn't yet support AES direct"
  2137. #elif defined(FREESCALE_LTC)
  2138. /* Allow direct access to one block encrypt */
  2139. void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
  2140. {
  2141. byte *key;
  2142. uint32_t keySize;
  2143. key = (byte*)aes->key;
  2144. wc_AesGetKeySize(aes, &keySize);
  2145. LTC_AES_EncryptEcb(LTC_BASE, in, out, AES_BLOCK_SIZE,
  2146. key, keySize);
  2147. }
  2148. /* Allow direct access to one block decrypt */
  2149. void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
  2150. {
  2151. byte *key;
  2152. uint32_t keySize;
  2153. key = (byte*)aes->key;
  2154. wc_AesGetKeySize(aes, &keySize);
  2155. LTC_AES_DecryptEcb(LTC_BASE, in, out, AES_BLOCK_SIZE,
  2156. key, keySize, kLTC_EncryptKey);
  2157. }
  2158. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
  2159. /* implemented in wolfcrypt/src/port/caam/caam_aes.c */
  2160. #elif defined(WOLFSSL_AFALG)
  2161. /* implemented in wolfcrypt/src/port/af_alg/afalg_aes.c */
  2162. #elif defined(WOLFSSL_DEVCRYPTO_AES)
  2163. /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
  2164. #elif defined(STM32_CRYPTO)
  2165. /* Allow direct access to one block encrypt */
  2166. void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
  2167. {
  2168. if (wolfSSL_CryptHwMutexLock() == 0) {
  2169. wc_AesEncrypt(aes, in, out);
  2170. wolfSSL_CryptHwMutexUnLock();
  2171. }
  2172. }
  2173. #ifdef HAVE_AES_DECRYPT
  2174. /* Allow direct access to one block decrypt */
  2175. void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
  2176. {
  2177. if (wolfSSL_CryptHwMutexLock() == 0) {
  2178. wc_AesDecrypt(aes, in, out);
  2179. wolfSSL_CryptHwMutexUnLock();
  2180. }
  2181. }
  2182. #endif /* HAVE_AES_DECRYPT */
  2183. #else
  2184. /* Allow direct access to one block encrypt */
  2185. void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
  2186. {
  2187. wc_AesEncrypt(aes, in, out);
  2188. }
  2189. #ifdef HAVE_AES_DECRYPT
  2190. /* Allow direct access to one block decrypt */
  2191. void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
  2192. {
  2193. wc_AesDecrypt(aes, in, out);
  2194. }
  2195. #endif /* HAVE_AES_DECRYPT */
  2196. #endif /* AES direct block */
  2197. #endif /* WOLFSSL_AES_DIRECT */
  2198. /* AES-CBC */
  2199. #ifdef HAVE_AES_CBC
  2200. #if defined(STM32_CRYPTO)
  2201. #ifdef WOLFSSL_STM32_CUBEMX
  2202. int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2203. {
  2204. int ret = 0;
  2205. word32 blocks = (sz / AES_BLOCK_SIZE);
  2206. CRYP_HandleTypeDef hcryp;
  2207. ret = wc_Stm32_Aes_Init(aes, &hcryp);
  2208. if (ret != 0)
  2209. return ret;
  2210. ret = wolfSSL_CryptHwMutexLock();
  2211. if (ret != 0) {
  2212. return ret;
  2213. }
  2214. #ifdef STM32_CRYPTO_AES_ONLY
  2215. hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  2216. hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  2217. hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  2218. #elif defined(STM32_HAL_V2)
  2219. hcryp.Init.Algorithm = CRYP_AES_CBC;
  2220. ByteReverseWords(aes->reg, aes->reg, AES_BLOCK_SIZE);
  2221. #endif
  2222. hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg;
  2223. HAL_CRYP_Init(&hcryp);
  2224. while (blocks--) {
  2225. #ifdef STM32_CRYPTO_AES_ONLY
  2226. ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
  2227. out, STM32_HAL_TIMEOUT);
  2228. #elif defined(STM32_HAL_V2)
  2229. ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in, AES_BLOCK_SIZE,
  2230. (uint32_t*)out, STM32_HAL_TIMEOUT);
  2231. #else
  2232. ret = HAL_CRYP_AESCBC_Encrypt(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
  2233. out, STM32_HAL_TIMEOUT);
  2234. #endif
  2235. if (ret != HAL_OK) {
  2236. ret = WC_TIMEOUT_E;
  2237. break;
  2238. }
  2239. /* store iv for next call */
  2240. XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2241. sz -= AES_BLOCK_SIZE;
  2242. in += AES_BLOCK_SIZE;
  2243. out += AES_BLOCK_SIZE;
  2244. }
  2245. HAL_CRYP_DeInit(&hcryp);
  2246. wolfSSL_CryptHwMutexUnLock();
  2247. return ret;
  2248. }
  2249. #ifdef HAVE_AES_DECRYPT
  2250. int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2251. {
  2252. int ret = 0;
  2253. word32 blocks = (sz / AES_BLOCK_SIZE);
  2254. CRYP_HandleTypeDef hcryp;
  2255. ret = wc_Stm32_Aes_Init(aes, &hcryp);
  2256. if (ret != 0)
  2257. return ret;
  2258. ret = wolfSSL_CryptHwMutexLock();
  2259. if (ret != 0) {
  2260. return ret;
  2261. }
  2262. /* if input and output same will overwrite input iv */
  2263. XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2264. #ifdef STM32_CRYPTO_AES_ONLY
  2265. hcryp.Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  2266. hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  2267. hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  2268. #elif defined(STM32_HAL_V2)
  2269. hcryp.Init.Algorithm = CRYP_AES_CBC;
  2270. ByteReverseWords(aes->reg, aes->reg, AES_BLOCK_SIZE);
  2271. #endif
  2272. hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg;
  2273. HAL_CRYP_Init(&hcryp);
  2274. while (blocks--) {
  2275. #ifdef STM32_CRYPTO_AES_ONLY
  2276. ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
  2277. out, STM32_HAL_TIMEOUT);
  2278. #elif defined(STM32_HAL_V2)
  2279. ret = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)in, AES_BLOCK_SIZE,
  2280. (uint32_t*)out, STM32_HAL_TIMEOUT);
  2281. #else
  2282. ret = HAL_CRYP_AESCBC_Decrypt(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
  2283. out, STM32_HAL_TIMEOUT);
  2284. #endif
  2285. if (ret != HAL_OK) {
  2286. ret = WC_TIMEOUT_E;
  2287. break;
  2288. }
  2289. /* store iv for next call */
  2290. XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
  2291. in += AES_BLOCK_SIZE;
  2292. out += AES_BLOCK_SIZE;
  2293. }
  2294. HAL_CRYP_DeInit(&hcryp);
  2295. wolfSSL_CryptHwMutexUnLock();
  2296. return ret;
  2297. }
  2298. #endif /* HAVE_AES_DECRYPT */
  2299. #else /* STD_PERI_LIB */
  2300. int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2301. {
  2302. int ret;
  2303. word32 *iv;
  2304. word32 blocks = (sz / AES_BLOCK_SIZE);
  2305. CRYP_InitTypeDef cryptInit;
  2306. CRYP_KeyInitTypeDef keyInit;
  2307. CRYP_IVInitTypeDef ivInit;
  2308. ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
  2309. if (ret != 0)
  2310. return ret;
  2311. ret = wolfSSL_CryptHwMutexLock();
  2312. if (ret != 0) {
  2313. return ret;
  2314. }
  2315. /* reset registers to their default values */
  2316. CRYP_DeInit();
  2317. /* set key */
  2318. CRYP_KeyInit(&keyInit);
  2319. /* set iv */
  2320. iv = aes->reg;
  2321. CRYP_IVStructInit(&ivInit);
  2322. ByteReverseWords(iv, iv, AES_BLOCK_SIZE);
  2323. ivInit.CRYP_IV0Left = iv[0];
  2324. ivInit.CRYP_IV0Right = iv[1];
  2325. ivInit.CRYP_IV1Left = iv[2];
  2326. ivInit.CRYP_IV1Right = iv[3];
  2327. CRYP_IVInit(&ivInit);
  2328. /* set direction and mode */
  2329. cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  2330. cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;
  2331. CRYP_Init(&cryptInit);
  2332. /* enable crypto processor */
  2333. CRYP_Cmd(ENABLE);
  2334. while (blocks--) {
  2335. /* flush IN/OUT FIFOs */
  2336. CRYP_FIFOFlush();
  2337. CRYP_DataIn(*(uint32_t*)&in[0]);
  2338. CRYP_DataIn(*(uint32_t*)&in[4]);
  2339. CRYP_DataIn(*(uint32_t*)&in[8]);
  2340. CRYP_DataIn(*(uint32_t*)&in[12]);
  2341. /* wait until the complete message has been processed */
  2342. while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
  2343. *(uint32_t*)&out[0] = CRYP_DataOut();
  2344. *(uint32_t*)&out[4] = CRYP_DataOut();
  2345. *(uint32_t*)&out[8] = CRYP_DataOut();
  2346. *(uint32_t*)&out[12] = CRYP_DataOut();
  2347. /* store iv for next call */
  2348. XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2349. sz -= AES_BLOCK_SIZE;
  2350. in += AES_BLOCK_SIZE;
  2351. out += AES_BLOCK_SIZE;
  2352. }
  2353. /* disable crypto processor */
  2354. CRYP_Cmd(DISABLE);
  2355. wolfSSL_CryptHwMutexUnLock();
  2356. return ret;
  2357. }
  2358. #ifdef HAVE_AES_DECRYPT
  2359. int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2360. {
  2361. int ret;
  2362. word32 *iv;
  2363. word32 blocks = (sz / AES_BLOCK_SIZE);
  2364. CRYP_InitTypeDef cryptInit;
  2365. CRYP_KeyInitTypeDef keyInit;
  2366. CRYP_IVInitTypeDef ivInit;
  2367. ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
  2368. if (ret != 0)
  2369. return ret;
  2370. ret = wolfSSL_CryptHwMutexLock();
  2371. if (ret != 0) {
  2372. return ret;
  2373. }
  2374. /* if input and output same will overwrite input iv */
  2375. XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2376. /* reset registers to their default values */
  2377. CRYP_DeInit();
  2378. /* set direction and key */
  2379. CRYP_KeyInit(&keyInit);
  2380. cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  2381. cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;
  2382. CRYP_Init(&cryptInit);
  2383. /* enable crypto processor */
  2384. CRYP_Cmd(ENABLE);
  2385. /* wait until key has been prepared */
  2386. while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
  2387. /* set direction and mode */
  2388. cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  2389. cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;
  2390. CRYP_Init(&cryptInit);
  2391. /* set iv */
  2392. iv = aes->reg;
  2393. CRYP_IVStructInit(&ivInit);
  2394. ByteReverseWords(iv, iv, AES_BLOCK_SIZE);
  2395. ivInit.CRYP_IV0Left = iv[0];
  2396. ivInit.CRYP_IV0Right = iv[1];
  2397. ivInit.CRYP_IV1Left = iv[2];
  2398. ivInit.CRYP_IV1Right = iv[3];
  2399. CRYP_IVInit(&ivInit);
  2400. /* enable crypto processor */
  2401. CRYP_Cmd(ENABLE);
  2402. while (blocks--) {
  2403. /* flush IN/OUT FIFOs */
  2404. CRYP_FIFOFlush();
  2405. CRYP_DataIn(*(uint32_t*)&in[0]);
  2406. CRYP_DataIn(*(uint32_t*)&in[4]);
  2407. CRYP_DataIn(*(uint32_t*)&in[8]);
  2408. CRYP_DataIn(*(uint32_t*)&in[12]);
  2409. /* wait until the complete message has been processed */
  2410. while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
  2411. *(uint32_t*)&out[0] = CRYP_DataOut();
  2412. *(uint32_t*)&out[4] = CRYP_DataOut();
  2413. *(uint32_t*)&out[8] = CRYP_DataOut();
  2414. *(uint32_t*)&out[12] = CRYP_DataOut();
  2415. /* store iv for next call */
  2416. XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
  2417. in += AES_BLOCK_SIZE;
  2418. out += AES_BLOCK_SIZE;
  2419. }
  2420. /* disable crypto processor */
  2421. CRYP_Cmd(DISABLE);
  2422. wolfSSL_CryptHwMutexUnLock();
  2423. return ret;
  2424. }
  2425. #endif /* HAVE_AES_DECRYPT */
  2426. #endif /* WOLFSSL_STM32_CUBEMX */
  2427. #elif defined(HAVE_COLDFIRE_SEC)
  2428. static int wc_AesCbcCrypt(Aes* aes, byte* po, const byte* pi, word32 sz,
  2429. word32 descHeader)
  2430. {
  2431. #ifdef DEBUG_WOLFSSL
  2432. int i; int stat1, stat2; int ret;
  2433. #endif
  2434. int size;
  2435. volatile int v;
  2436. if ((pi == NULL) || (po == NULL))
  2437. return BAD_FUNC_ARG; /*wrong pointer*/
  2438. wc_LockMutex(&Mutex_AesSEC);
  2439. /* Set descriptor for SEC */
  2440. secDesc->length1 = 0x0;
  2441. secDesc->pointer1 = NULL;
  2442. secDesc->length2 = AES_BLOCK_SIZE;
  2443. secDesc->pointer2 = (byte *)secReg; /* Initial Vector */
  2444. switch(aes->rounds) {
  2445. case 10: secDesc->length3 = 16; break;
  2446. case 12: secDesc->length3 = 24; break;
  2447. case 14: secDesc->length3 = 32; break;
  2448. }
  2449. XMEMCPY(secKey, aes->key, secDesc->length3);
  2450. secDesc->pointer3 = (byte *)secKey;
  2451. secDesc->pointer4 = AESBuffIn;
  2452. secDesc->pointer5 = AESBuffOut;
  2453. secDesc->length6 = 0x0;
  2454. secDesc->pointer6 = NULL;
  2455. secDesc->length7 = 0x0;
  2456. secDesc->pointer7 = NULL;
  2457. secDesc->nextDescriptorPtr = NULL;
  2458. while (sz) {
  2459. secDesc->header = descHeader;
  2460. XMEMCPY(secReg, aes->reg, AES_BLOCK_SIZE);
  2461. if ((sz % AES_BUFFER_SIZE) == sz) {
  2462. size = sz;
  2463. sz = 0;
  2464. } else {
  2465. size = AES_BUFFER_SIZE;
  2466. sz -= AES_BUFFER_SIZE;
  2467. }
  2468. secDesc->length4 = size;
  2469. secDesc->length5 = size;
  2470. XMEMCPY(AESBuffIn, pi, size);
  2471. if(descHeader == SEC_DESC_AES_CBC_DECRYPT) {
  2472. XMEMCPY((void*)aes->tmp, (void*)&(pi[size-AES_BLOCK_SIZE]),
  2473. AES_BLOCK_SIZE);
  2474. }
  2475. /* Point SEC to the location of the descriptor */
  2476. MCF_SEC_FR0 = (uint32)secDesc;
  2477. /* Initialize SEC and wait for encryption to complete */
  2478. MCF_SEC_CCCR0 = 0x0000001a;
  2479. /* poll SISR to determine when channel is complete */
  2480. v=0;
  2481. while ((secDesc->header>> 24) != 0xff) v++;
  2482. #ifdef DEBUG_WOLFSSL
  2483. ret = MCF_SEC_SISRH;
  2484. stat1 = MCF_SEC_AESSR;
  2485. stat2 = MCF_SEC_AESISR;
  2486. if (ret & 0xe0000000) {
  2487. db_printf("Aes_Cbc(i=%d):ISRH=%08x, AESSR=%08x, "
  2488. "AESISR=%08x\n", i, ret, stat1, stat2);
  2489. }
  2490. #endif
  2491. XMEMCPY(po, AESBuffOut, size);
  2492. if (descHeader == SEC_DESC_AES_CBC_ENCRYPT) {
  2493. XMEMCPY((void*)aes->reg, (void*)&(po[size-AES_BLOCK_SIZE]),
  2494. AES_BLOCK_SIZE);
  2495. } else {
  2496. XMEMCPY((void*)aes->reg, (void*)aes->tmp, AES_BLOCK_SIZE);
  2497. }
  2498. pi += size;
  2499. po += size;
  2500. }
  2501. wc_UnLockMutex(&Mutex_AesSEC);
  2502. return 0;
  2503. }
  2504. int wc_AesCbcEncrypt(Aes* aes, byte* po, const byte* pi, word32 sz)
  2505. {
  2506. return (wc_AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_ENCRYPT));
  2507. }
  2508. #ifdef HAVE_AES_DECRYPT
  2509. int wc_AesCbcDecrypt(Aes* aes, byte* po, const byte* pi, word32 sz)
  2510. {
  2511. return (wc_AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_DECRYPT));
  2512. }
  2513. #endif /* HAVE_AES_DECRYPT */
  2514. #elif defined(FREESCALE_LTC)
  2515. int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2516. {
  2517. uint32_t keySize;
  2518. status_t status;
  2519. byte *iv, *enc_key;
  2520. word32 blocks = (sz / AES_BLOCK_SIZE);
  2521. iv = (byte*)aes->reg;
  2522. enc_key = (byte*)aes->key;
  2523. status = wc_AesGetKeySize(aes, &keySize);
  2524. if (status != 0) {
  2525. return status;
  2526. }
  2527. status = LTC_AES_EncryptCbc(LTC_BASE, in, out, blocks * AES_BLOCK_SIZE,
  2528. iv, enc_key, keySize);
  2529. /* store iv for next call */
  2530. if (status == kStatus_Success) {
  2531. XMEMCPY(iv, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2532. }
  2533. return (status == kStatus_Success) ? 0 : -1;
  2534. }
  2535. #ifdef HAVE_AES_DECRYPT
  2536. int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2537. {
  2538. uint32_t keySize;
  2539. status_t status;
  2540. byte* iv, *dec_key;
  2541. word32 blocks = (sz / AES_BLOCK_SIZE);
  2542. byte temp_block[AES_BLOCK_SIZE];
  2543. iv = (byte*)aes->reg;
  2544. dec_key = (byte*)aes->key;
  2545. status = wc_AesGetKeySize(aes, &keySize);
  2546. if (status != 0) {
  2547. return status;
  2548. }
  2549. /* get IV for next call */
  2550. XMEMCPY(temp_block, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2551. status = LTC_AES_DecryptCbc(LTC_BASE, in, out, blocks * AES_BLOCK_SIZE,
  2552. iv, dec_key, keySize, kLTC_EncryptKey);
  2553. /* store IV for next call */
  2554. if (status == kStatus_Success) {
  2555. XMEMCPY(iv, temp_block, AES_BLOCK_SIZE);
  2556. }
  2557. return (status == kStatus_Success) ? 0 : -1;
  2558. }
  2559. #endif /* HAVE_AES_DECRYPT */
  2560. #elif defined(FREESCALE_MMCAU)
  2561. int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2562. {
  2563. int i;
  2564. int offset = 0;
  2565. word32 blocks = (sz / AES_BLOCK_SIZE);
  2566. byte *iv;
  2567. byte temp_block[AES_BLOCK_SIZE];
  2568. iv = (byte*)aes->reg;
  2569. while (blocks--) {
  2570. XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
  2571. /* XOR block with IV for CBC */
  2572. for (i = 0; i < AES_BLOCK_SIZE; i++)
  2573. temp_block[i] ^= iv[i];
  2574. wc_AesEncrypt(aes, temp_block, out + offset);
  2575. offset += AES_BLOCK_SIZE;
  2576. /* store IV for next block */
  2577. XMEMCPY(iv, out + offset - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2578. }
  2579. return 0;
  2580. }
  2581. #ifdef HAVE_AES_DECRYPT
  2582. int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2583. {
  2584. int i;
  2585. int offset = 0;
  2586. word32 blocks = (sz / AES_BLOCK_SIZE);
  2587. byte* iv;
  2588. byte temp_block[AES_BLOCK_SIZE];
  2589. iv = (byte*)aes->reg;
  2590. while (blocks--) {
  2591. XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
  2592. wc_AesDecrypt(aes, in + offset, out + offset);
  2593. /* XOR block with IV for CBC */
  2594. for (i = 0; i < AES_BLOCK_SIZE; i++)
  2595. (out + offset)[i] ^= iv[i];
  2596. /* store IV for next block */
  2597. XMEMCPY(iv, temp_block, AES_BLOCK_SIZE);
  2598. offset += AES_BLOCK_SIZE;
  2599. }
  2600. return 0;
  2601. }
  2602. #endif /* HAVE_AES_DECRYPT */
  2603. #elif defined(WOLFSSL_PIC32MZ_CRYPT)
  2604. int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2605. {
  2606. int ret;
  2607. /* hardware fails on input that is not a multiple of AES block size */
  2608. if (sz % AES_BLOCK_SIZE != 0) {
  2609. return BAD_FUNC_ARG;
  2610. }
  2611. ret = wc_Pic32AesCrypt(
  2612. aes->key, aes->keylen, aes->reg, AES_BLOCK_SIZE,
  2613. out, in, sz, PIC32_ENCRYPTION,
  2614. PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCBC);
  2615. /* store iv for next call */
  2616. if (ret == 0) {
  2617. XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2618. }
  2619. return ret;
  2620. }
  2621. #ifdef HAVE_AES_DECRYPT
  2622. int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2623. {
  2624. int ret;
  2625. byte scratch[AES_BLOCK_SIZE];
  2626. /* hardware fails on input that is not a multiple of AES block size */
  2627. if (sz % AES_BLOCK_SIZE != 0) {
  2628. return BAD_FUNC_ARG;
  2629. }
  2630. XMEMCPY(scratch, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2631. ret = wc_Pic32AesCrypt(
  2632. aes->key, aes->keylen, aes->reg, AES_BLOCK_SIZE,
  2633. out, in, sz, PIC32_DECRYPTION,
  2634. PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCBC);
  2635. /* store iv for next call */
  2636. if (ret == 0) {
  2637. XMEMCPY((byte*)aes->reg, scratch, AES_BLOCK_SIZE);
  2638. }
  2639. return ret;
  2640. }
  2641. #endif /* HAVE_AES_DECRYPT */
  2642. #elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
  2643. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
  2644. int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2645. {
  2646. return wc_esp32AesCbcEncrypt(aes, out, in, sz);
  2647. }
  2648. int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2649. {
  2650. return wc_esp32AesCbcDecrypt(aes, out, in, sz);
  2651. }
  2652. #elif defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES)
  2653. int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2654. {
  2655. return SaSi_AesBlock(&aes->ctx.user_ctx, (uint8_t* )in, sz, out);
  2656. }
  2657. int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2658. {
  2659. return SaSi_AesBlock(&aes->ctx.user_ctx, (uint8_t* )in, sz, out);
  2660. }
  2661. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
  2662. /* implemented in wolfcrypt/src/port/caam/caam_aes.c */
  2663. #elif defined(WOLFSSL_AFALG)
  2664. /* implemented in wolfcrypt/src/port/af_alg/afalg_aes.c */
  2665. #elif defined(WOLFSSL_DEVCRYPTO_CBC)
  2666. /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
  2667. #else
  2668. /* Software AES - CBC Encrypt */
  2669. int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2670. {
  2671. word32 blocks = (sz / AES_BLOCK_SIZE);
  2672. if (aes == NULL || out == NULL || in == NULL) {
  2673. return BAD_FUNC_ARG;
  2674. }
  2675. #ifdef WOLF_CRYPTO_CB
  2676. if (aes->devId != INVALID_DEVID) {
  2677. int ret = wc_CryptoCb_AesCbcEncrypt(aes, out, in, sz);
  2678. if (ret != CRYPTOCB_UNAVAILABLE)
  2679. return ret;
  2680. /* fall-through when unavailable */
  2681. }
  2682. #endif
  2683. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
  2684. /* if async and byte count above threshold */
  2685. if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
  2686. sz >= WC_ASYNC_THRESH_AES_CBC) {
  2687. #if defined(HAVE_CAVIUM)
  2688. return NitroxAesCbcEncrypt(aes, out, in, sz);
  2689. #elif defined(HAVE_INTEL_QA)
  2690. return IntelQaSymAesCbcEncrypt(&aes->asyncDev, out, in, sz,
  2691. (const byte*)aes->devKey, aes->keylen,
  2692. (byte*)aes->reg, AES_BLOCK_SIZE);
  2693. #else /* WOLFSSL_ASYNC_CRYPT_TEST */
  2694. if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_CBC_ENCRYPT)) {
  2695. WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
  2696. testDev->aes.aes = aes;
  2697. testDev->aes.out = out;
  2698. testDev->aes.in = in;
  2699. testDev->aes.sz = sz;
  2700. return WC_PENDING_E;
  2701. }
  2702. #endif
  2703. }
  2704. #endif /* WOLFSSL_ASYNC_CRYPT */
  2705. #ifdef WOLFSSL_AESNI
  2706. if (haveAESNI) {
  2707. #ifdef DEBUG_AESNI
  2708. printf("about to aes cbc encrypt\n");
  2709. printf("in = %p\n", in);
  2710. printf("out = %p\n", out);
  2711. printf("aes->key = %p\n", aes->key);
  2712. printf("aes->reg = %p\n", aes->reg);
  2713. printf("aes->rounds = %d\n", aes->rounds);
  2714. printf("sz = %d\n", sz);
  2715. #endif
  2716. /* check alignment, decrypt doesn't need alignment */
  2717. if ((wolfssl_word)in % AESNI_ALIGN) {
  2718. #ifndef NO_WOLFSSL_ALLOC_ALIGN
  2719. byte* tmp = (byte*)XMALLOC(sz + AES_BLOCK_SIZE + AESNI_ALIGN,
  2720. aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
  2721. byte* tmp_align;
  2722. if (tmp == NULL) return MEMORY_E;
  2723. tmp_align = tmp + (AESNI_ALIGN - ((size_t)tmp % AESNI_ALIGN));
  2724. XMEMCPY(tmp_align, in, sz);
  2725. AES_CBC_encrypt(tmp_align, tmp_align, (byte*)aes->reg, sz,
  2726. (byte*)aes->key, aes->rounds);
  2727. /* store iv for next call */
  2728. XMEMCPY(aes->reg, tmp_align + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2729. XMEMCPY(out, tmp_align, sz);
  2730. XFREE(tmp, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
  2731. return 0;
  2732. #else
  2733. WOLFSSL_MSG("AES-CBC encrypt with bad alignment");
  2734. return BAD_ALIGN_E;
  2735. #endif
  2736. }
  2737. AES_CBC_encrypt(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
  2738. aes->rounds);
  2739. /* store iv for next call */
  2740. XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2741. return 0;
  2742. }
  2743. #endif
  2744. while (blocks--) {
  2745. xorbuf((byte*)aes->reg, in, AES_BLOCK_SIZE);
  2746. wc_AesEncrypt(aes, (byte*)aes->reg, (byte*)aes->reg);
  2747. XMEMCPY(out, aes->reg, AES_BLOCK_SIZE);
  2748. out += AES_BLOCK_SIZE;
  2749. in += AES_BLOCK_SIZE;
  2750. }
  2751. return 0;
  2752. }
  2753. #ifdef HAVE_AES_DECRYPT
  2754. /* Software AES - CBC Decrypt */
  2755. int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2756. {
  2757. word32 blocks;
  2758. if (aes == NULL || out == NULL || in == NULL
  2759. || sz % AES_BLOCK_SIZE != 0) {
  2760. return BAD_FUNC_ARG;
  2761. }
  2762. #ifdef WOLF_CRYPTO_CB
  2763. if (aes->devId != INVALID_DEVID) {
  2764. int ret = wc_CryptoCb_AesCbcDecrypt(aes, out, in, sz);
  2765. if (ret != CRYPTOCB_UNAVAILABLE)
  2766. return ret;
  2767. /* fall-through when unavailable */
  2768. }
  2769. #endif
  2770. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
  2771. /* if async and byte count above threshold */
  2772. if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
  2773. sz >= WC_ASYNC_THRESH_AES_CBC) {
  2774. #if defined(HAVE_CAVIUM)
  2775. return NitroxAesCbcDecrypt(aes, out, in, sz);
  2776. #elif defined(HAVE_INTEL_QA)
  2777. return IntelQaSymAesCbcDecrypt(&aes->asyncDev, out, in, sz,
  2778. (const byte*)aes->devKey, aes->keylen,
  2779. (byte*)aes->reg, AES_BLOCK_SIZE);
  2780. #else /* WOLFSSL_ASYNC_CRYPT_TEST */
  2781. if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_CBC_DECRYPT)) {
  2782. WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
  2783. testDev->aes.aes = aes;
  2784. testDev->aes.out = out;
  2785. testDev->aes.in = in;
  2786. testDev->aes.sz = sz;
  2787. return WC_PENDING_E;
  2788. }
  2789. #endif
  2790. }
  2791. #endif
  2792. #ifdef WOLFSSL_AESNI
  2793. if (haveAESNI) {
  2794. #ifdef DEBUG_AESNI
  2795. printf("about to aes cbc decrypt\n");
  2796. printf("in = %p\n", in);
  2797. printf("out = %p\n", out);
  2798. printf("aes->key = %p\n", aes->key);
  2799. printf("aes->reg = %p\n", aes->reg);
  2800. printf("aes->rounds = %d\n", aes->rounds);
  2801. printf("sz = %d\n", sz);
  2802. #endif
  2803. /* if input and output same will overwrite input iv */
  2804. XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  2805. #if defined(WOLFSSL_AESNI_BY4)
  2806. AES_CBC_decrypt_by4(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
  2807. aes->rounds);
  2808. #elif defined(WOLFSSL_AESNI_BY6)
  2809. AES_CBC_decrypt_by6(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
  2810. aes->rounds);
  2811. #else /* WOLFSSL_AESNI_BYx */
  2812. AES_CBC_decrypt_by8(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
  2813. aes->rounds);
  2814. #endif /* WOLFSSL_AESNI_BYx */
  2815. /* store iv for next call */
  2816. XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
  2817. return 0;
  2818. }
  2819. #endif
  2820. blocks = sz / AES_BLOCK_SIZE;
  2821. while (blocks--) {
  2822. XMEMCPY(aes->tmp, in, AES_BLOCK_SIZE);
  2823. wc_AesDecrypt(aes, (byte*)aes->tmp, out);
  2824. xorbuf(out, (byte*)aes->reg, AES_BLOCK_SIZE);
  2825. /* store iv for next call */
  2826. XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
  2827. out += AES_BLOCK_SIZE;
  2828. in += AES_BLOCK_SIZE;
  2829. }
  2830. return 0;
  2831. }
  2832. #endif
  2833. #endif /* AES-CBC block */
  2834. #endif /* HAVE_AES_CBC */
  2835. /* AES-CTR */
  2836. #if defined(WOLFSSL_AES_COUNTER)
  2837. #ifdef STM32_CRYPTO
  2838. #define NEED_AES_CTR_SOFT
  2839. #define XTRANSFORM_AESCTRBLOCK wc_AesCtrEncryptBlock
  2840. int wc_AesCtrEncryptBlock(Aes* aes, byte* out, const byte* in)
  2841. {
  2842. int ret = 0;
  2843. #ifdef WOLFSSL_STM32_CUBEMX
  2844. CRYP_HandleTypeDef hcryp;
  2845. #ifdef STM32_HAL_V2
  2846. word32 iv[AES_BLOCK_SIZE/sizeof(word32)];
  2847. #endif
  2848. #else
  2849. word32 *iv;
  2850. CRYP_InitTypeDef cryptInit;
  2851. CRYP_KeyInitTypeDef keyInit;
  2852. CRYP_IVInitTypeDef ivInit;
  2853. #endif
  2854. ret = wolfSSL_CryptHwMutexLock();
  2855. if (ret != 0) {
  2856. return ret;
  2857. }
  2858. #ifdef WOLFSSL_STM32_CUBEMX
  2859. ret = wc_Stm32_Aes_Init(aes, &hcryp);
  2860. if (ret != 0) {
  2861. wolfSSL_CryptHwMutexUnLock();
  2862. return ret;
  2863. }
  2864. #ifdef STM32_CRYPTO_AES_ONLY
  2865. hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  2866. hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  2867. hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  2868. hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg;
  2869. #elif defined(STM32_HAL_V2)
  2870. hcryp.Init.Algorithm = CRYP_AES_CTR;
  2871. ByteReverseWords(iv, aes->reg, AES_BLOCK_SIZE);
  2872. hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)iv;
  2873. #else
  2874. hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg;
  2875. #endif
  2876. HAL_CRYP_Init(&hcryp);
  2877. #ifdef STM32_CRYPTO_AES_ONLY
  2878. ret = HAL_CRYPEx_AES(&hcryp, (byte*)in, AES_BLOCK_SIZE,
  2879. out, STM32_HAL_TIMEOUT);
  2880. #elif defined(STM32_HAL_V2)
  2881. ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in, AES_BLOCK_SIZE,
  2882. (uint32_t*)out, STM32_HAL_TIMEOUT);
  2883. #else
  2884. ret = HAL_CRYP_AESCTR_Encrypt(&hcryp, (byte*)in, AES_BLOCK_SIZE,
  2885. out, STM32_HAL_TIMEOUT);
  2886. #endif
  2887. if (ret != HAL_OK) {
  2888. ret = WC_TIMEOUT_E;
  2889. }
  2890. HAL_CRYP_DeInit(&hcryp);
  2891. #else /* STD_PERI_LIB */
  2892. ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
  2893. if (ret != 0) {
  2894. wolfSSL_CryptHwMutexUnLock();
  2895. return ret;
  2896. }
  2897. /* reset registers to their default values */
  2898. CRYP_DeInit();
  2899. /* set key */
  2900. CRYP_KeyInit(&keyInit);
  2901. /* set iv */
  2902. iv = aes->reg;
  2903. CRYP_IVStructInit(&ivInit);
  2904. ivInit.CRYP_IV0Left = ByteReverseWord32(iv[0]);
  2905. ivInit.CRYP_IV0Right = ByteReverseWord32(iv[1]);
  2906. ivInit.CRYP_IV1Left = ByteReverseWord32(iv[2]);
  2907. ivInit.CRYP_IV1Right = ByteReverseWord32(iv[3]);
  2908. CRYP_IVInit(&ivInit);
  2909. /* set direction and mode */
  2910. cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  2911. cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_CTR;
  2912. CRYP_Init(&cryptInit);
  2913. /* enable crypto processor */
  2914. CRYP_Cmd(ENABLE);
  2915. /* flush IN/OUT FIFOs */
  2916. CRYP_FIFOFlush();
  2917. CRYP_DataIn(*(uint32_t*)&in[0]);
  2918. CRYP_DataIn(*(uint32_t*)&in[4]);
  2919. CRYP_DataIn(*(uint32_t*)&in[8]);
  2920. CRYP_DataIn(*(uint32_t*)&in[12]);
  2921. /* wait until the complete message has been processed */
  2922. while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
  2923. *(uint32_t*)&out[0] = CRYP_DataOut();
  2924. *(uint32_t*)&out[4] = CRYP_DataOut();
  2925. *(uint32_t*)&out[8] = CRYP_DataOut();
  2926. *(uint32_t*)&out[12] = CRYP_DataOut();
  2927. /* disable crypto processor */
  2928. CRYP_Cmd(DISABLE);
  2929. #endif /* WOLFSSL_STM32_CUBEMX */
  2930. wolfSSL_CryptHwMutexUnLock();
  2931. return ret;
  2932. }
  2933. #elif defined(WOLFSSL_PIC32MZ_CRYPT)
  2934. #define NEED_AES_CTR_SOFT
  2935. #define XTRANSFORM_AESCTRBLOCK wc_AesCtrEncryptBlock
  2936. int wc_AesCtrEncryptBlock(Aes* aes, byte* out, const byte* in)
  2937. {
  2938. word32 tmpIv[AES_BLOCK_SIZE / sizeof(word32)];
  2939. XMEMCPY(tmpIv, aes->reg, AES_BLOCK_SIZE);
  2940. return wc_Pic32AesCrypt(
  2941. aes->key, aes->keylen, tmpIv, AES_BLOCK_SIZE,
  2942. out, in, AES_BLOCK_SIZE,
  2943. PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCTR);
  2944. }
  2945. #elif defined(HAVE_COLDFIRE_SEC)
  2946. #error "Coldfire SEC doesn't currently support AES-CTR mode"
  2947. #elif defined(FREESCALE_LTC)
  2948. int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2949. {
  2950. uint32_t keySize;
  2951. byte *iv, *enc_key;
  2952. byte* tmp;
  2953. if (aes == NULL || out == NULL || in == NULL) {
  2954. return BAD_FUNC_ARG;
  2955. }
  2956. /* consume any unused bytes left in aes->tmp */
  2957. tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
  2958. while (aes->left && sz) {
  2959. *(out++) = *(in++) ^ *(tmp++);
  2960. aes->left--;
  2961. sz--;
  2962. }
  2963. if (sz) {
  2964. iv = (byte*)aes->reg;
  2965. enc_key = (byte*)aes->key;
  2966. wc_AesGetKeySize(aes, &keySize);
  2967. LTC_AES_CryptCtr(LTC_BASE, in, out, sz,
  2968. iv, enc_key, keySize, (byte*)aes->tmp,
  2969. (uint32_t*)&aes->left);
  2970. }
  2971. return 0;
  2972. }
  2973. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
  2974. /* implemented in wolfcrypt/src/port/caam/caam_aes.c */
  2975. #elif defined(WOLFSSL_AFALG)
  2976. /* implemented in wolfcrypt/src/port/af_alg/afalg_aes.c */
  2977. #elif defined(WOLFSSL_DEVCRYPTO_AES)
  2978. /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
  2979. #else
  2980. /* Use software based AES counter */
  2981. #define NEED_AES_CTR_SOFT
  2982. #endif
  2983. #ifdef NEED_AES_CTR_SOFT
  2984. /* Increment AES counter */
  2985. static WC_INLINE void IncrementAesCounter(byte* inOutCtr)
  2986. {
  2987. /* in network byte order so start at end and work back */
  2988. int i;
  2989. for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
  2990. if (++inOutCtr[i]) /* we're done unless we overflow */
  2991. return;
  2992. }
  2993. }
  2994. /* Software AES - CTR Encrypt */
  2995. int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  2996. {
  2997. byte* tmp;
  2998. byte scratch[AES_BLOCK_SIZE];
  2999. if (aes == NULL || out == NULL || in == NULL) {
  3000. return BAD_FUNC_ARG;
  3001. }
  3002. /* consume any unused bytes left in aes->tmp */
  3003. tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
  3004. while (aes->left && sz) {
  3005. *(out++) = *(in++) ^ *(tmp++);
  3006. aes->left--;
  3007. sz--;
  3008. }
  3009. /* do as many block size ops as possible */
  3010. while (sz >= AES_BLOCK_SIZE) {
  3011. #ifdef XTRANSFORM_AESCTRBLOCK
  3012. XTRANSFORM_AESCTRBLOCK(aes, out, in);
  3013. #else
  3014. wc_AesEncrypt(aes, (byte*)aes->reg, scratch);
  3015. xorbuf(scratch, in, AES_BLOCK_SIZE);
  3016. XMEMCPY(out, scratch, AES_BLOCK_SIZE);
  3017. #endif
  3018. IncrementAesCounter((byte*)aes->reg);
  3019. out += AES_BLOCK_SIZE;
  3020. in += AES_BLOCK_SIZE;
  3021. sz -= AES_BLOCK_SIZE;
  3022. aes->left = 0;
  3023. }
  3024. ForceZero(scratch, AES_BLOCK_SIZE);
  3025. /* handle non block size remaining and store unused byte count in left */
  3026. if (sz) {
  3027. wc_AesEncrypt(aes, (byte*)aes->reg, (byte*)aes->tmp);
  3028. IncrementAesCounter((byte*)aes->reg);
  3029. aes->left = AES_BLOCK_SIZE;
  3030. tmp = (byte*)aes->tmp;
  3031. while (sz--) {
  3032. *(out++) = *(in++) ^ *(tmp++);
  3033. aes->left--;
  3034. }
  3035. }
  3036. return 0;
  3037. }
  3038. #endif /* NEED_AES_CTR_SOFT */
  3039. #endif /* WOLFSSL_AES_COUNTER */
  3040. #endif /* !WOLFSSL_ARMASM */
  3041. /*
  3042. * The IV for AES GCM and CCM, stored in struct Aes's member reg, is comprised
  3043. * of two parts in order:
  3044. * 1. The fixed field which may be 0 or 4 bytes long. In TLS, this is set
  3045. * to the implicit IV.
  3046. * 2. The explicit IV is generated by wolfCrypt. It needs to be managed
  3047. * by wolfCrypt to ensure the IV is unique for each call to encrypt.
  3048. * The IV may be a 96-bit random value, or the 32-bit fixed value and a
  3049. * 64-bit set of 0 or random data. The final 32-bits of reg is used as a
  3050. * block counter during the encryption.
  3051. */
  3052. #if (defined(HAVE_AESGCM) && !defined(WC_NO_RNG)) || defined(HAVE_AESCCM)
  3053. static WC_INLINE void IncCtr(byte* ctr, word32 ctrSz)
  3054. {
  3055. int i;
  3056. for (i = ctrSz-1; i >= 0; i--) {
  3057. if (++ctr[i])
  3058. break;
  3059. }
  3060. }
  3061. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  3062. #ifdef HAVE_AESGCM
  3063. #if defined(HAVE_COLDFIRE_SEC)
  3064. #error "Coldfire SEC doesn't currently support AES-GCM mode"
  3065. #elif defined(WOLFSSL_NRF51_AES)
  3066. #error "nRF51 doesn't currently support AES-GCM mode"
  3067. #endif
  3068. #ifdef WOLFSSL_ARMASM
  3069. /* implementation is located in wolfcrypt/src/port/arm/armv8-aes.c */
  3070. #elif defined(WOLFSSL_AFALG)
  3071. /* implemented in wolfcrypt/src/port/afalg/afalg_aes.c */
  3072. #elif defined(WOLFSSL_DEVCRYPTO_AES)
  3073. /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
  3074. #else /* software + AESNI implementation */
  3075. #if !defined(FREESCALE_LTC_AES_GCM)
  3076. static WC_INLINE void IncrementGcmCounter(byte* inOutCtr)
  3077. {
  3078. int i;
  3079. /* in network byte order so start at end and work back */
  3080. for (i = AES_BLOCK_SIZE - 1; i >= AES_BLOCK_SIZE - CTR_SZ; i--) {
  3081. if (++inOutCtr[i]) /* we're done unless we overflow */
  3082. return;
  3083. }
  3084. }
  3085. #ifdef STM32_CRYPTO_AES_GCM
  3086. static WC_INLINE void DecrementGcmCounter(byte* inOutCtr)
  3087. {
  3088. int i;
  3089. /* in network byte order so start at end and work back */
  3090. for (i = AES_BLOCK_SIZE - 1; i >= AES_BLOCK_SIZE - CTR_SZ; i--) {
  3091. if (--inOutCtr[i] != 0xFF) /* we're done unless we underflow */
  3092. return;
  3093. }
  3094. }
  3095. #endif /* STM32_CRYPTO_AES_GCM */
  3096. #endif /* !FREESCALE_LTC_AES_GCM */
  3097. #if defined(GCM_SMALL) || defined(GCM_TABLE)
  3098. static WC_INLINE void FlattenSzInBits(byte* buf, word32 sz)
  3099. {
  3100. /* Multiply the sz by 8 */
  3101. word32 szHi = (sz >> (8*sizeof(sz) - 3));
  3102. sz <<= 3;
  3103. /* copy over the words of the sz into the destination buffer */
  3104. buf[0] = (szHi >> 24) & 0xff;
  3105. buf[1] = (szHi >> 16) & 0xff;
  3106. buf[2] = (szHi >> 8) & 0xff;
  3107. buf[3] = szHi & 0xff;
  3108. buf[4] = (sz >> 24) & 0xff;
  3109. buf[5] = (sz >> 16) & 0xff;
  3110. buf[6] = (sz >> 8) & 0xff;
  3111. buf[7] = sz & 0xff;
  3112. }
  3113. static WC_INLINE void RIGHTSHIFTX(byte* x)
  3114. {
  3115. int i;
  3116. int carryOut = 0;
  3117. int carryIn = 0;
  3118. int borrow = x[15] & 0x01;
  3119. for (i = 0; i < AES_BLOCK_SIZE; i++) {
  3120. carryOut = x[i] & 0x01;
  3121. x[i] = (x[i] >> 1) | (carryIn ? 0x80 : 0);
  3122. carryIn = carryOut;
  3123. }
  3124. if (borrow) x[0] ^= 0xE1;
  3125. }
  3126. #endif /* defined(GCM_SMALL) || defined(GCM_TABLE) */
  3127. #ifdef GCM_TABLE
  3128. static void GenerateM0(Aes* aes)
  3129. {
  3130. int i, j;
  3131. byte (*m)[AES_BLOCK_SIZE] = aes->M0;
  3132. XMEMCPY(m[128], aes->H, AES_BLOCK_SIZE);
  3133. for (i = 64; i > 0; i /= 2) {
  3134. XMEMCPY(m[i], m[i*2], AES_BLOCK_SIZE);
  3135. RIGHTSHIFTX(m[i]);
  3136. }
  3137. for (i = 2; i < 256; i *= 2) {
  3138. for (j = 1; j < i; j++) {
  3139. XMEMCPY(m[i+j], m[i], AES_BLOCK_SIZE);
  3140. xorbuf(m[i+j], m[j], AES_BLOCK_SIZE);
  3141. }
  3142. }
  3143. XMEMSET(m[0], 0, AES_BLOCK_SIZE);
  3144. }
  3145. #endif /* GCM_TABLE */
  3146. /* Software AES - GCM SetKey */
  3147. int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
  3148. {
  3149. int ret;
  3150. byte iv[AES_BLOCK_SIZE];
  3151. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  3152. byte local[32];
  3153. word32 localSz = 32;
  3154. if (len == (16 + WC_CAAM_BLOB_SZ) ||
  3155. len == (24 + WC_CAAM_BLOB_SZ) ||
  3156. len == (32 + WC_CAAM_BLOB_SZ)) {
  3157. if (wc_caamOpenBlob((byte*)key, len, local, &localSz) != 0) {
  3158. return BAD_FUNC_ARG;
  3159. }
  3160. /* set local values */
  3161. key = local;
  3162. len = localSz;
  3163. }
  3164. #endif
  3165. if (!((len == 16) || (len == 24) || (len == 32)))
  3166. return BAD_FUNC_ARG;
  3167. #ifdef OPENSSL_EXTRA
  3168. if (aes != NULL) {
  3169. XMEMSET(aes->aadH, 0, sizeof(aes->aadH));
  3170. aes->aadLen = 0;
  3171. }
  3172. #endif
  3173. XMEMSET(iv, 0, AES_BLOCK_SIZE);
  3174. ret = wc_AesSetKey(aes, key, len, iv, AES_ENCRYPTION);
  3175. #ifdef WOLFSSL_AESNI
  3176. /* AES-NI code generates its own H value. */
  3177. if (haveAESNI)
  3178. return ret;
  3179. #endif /* WOLFSSL_AESNI */
  3180. #if !defined(FREESCALE_LTC_AES_GCM)
  3181. if (ret == 0) {
  3182. wc_AesEncrypt(aes, iv, aes->H);
  3183. #ifdef GCM_TABLE
  3184. GenerateM0(aes);
  3185. #endif /* GCM_TABLE */
  3186. }
  3187. #endif /* FREESCALE_LTC_AES_GCM */
  3188. #if defined(WOLFSSL_XILINX_CRYPT)
  3189. wc_AesGcmSetKey_ex(aes, key, len, XSECURE_CSU_AES_KEY_SRC_KUP);
  3190. #elif defined(WOLFSSL_AFALG_XILINX_AES)
  3191. wc_AesGcmSetKey_ex(aes, key, len, 0);
  3192. #endif
  3193. #ifdef WOLF_CRYPTO_CB
  3194. if (aes->devId != INVALID_DEVID) {
  3195. XMEMCPY(aes->devKey, key, len);
  3196. }
  3197. #endif
  3198. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  3199. ForceZero(local, sizeof(local));
  3200. #endif
  3201. return ret;
  3202. }
  3203. #ifdef WOLFSSL_AESNI
  3204. #if defined(USE_INTEL_SPEEDUP)
  3205. #define HAVE_INTEL_AVX1
  3206. #define HAVE_INTEL_AVX2
  3207. #endif /* USE_INTEL_SPEEDUP */
  3208. #ifndef _MSC_VER
  3209. void AES_GCM_encrypt(const unsigned char *in, unsigned char *out,
  3210. const unsigned char* addt, const unsigned char* ivec,
  3211. unsigned char *tag, unsigned int nbytes,
  3212. unsigned int abytes, unsigned int ibytes,
  3213. unsigned int tbytes, const unsigned char* key, int nr)
  3214. XASM_LINK("AES_GCM_encrypt");
  3215. #ifdef HAVE_INTEL_AVX1
  3216. void AES_GCM_encrypt_avx1(const unsigned char *in, unsigned char *out,
  3217. const unsigned char* addt, const unsigned char* ivec,
  3218. unsigned char *tag, unsigned int nbytes,
  3219. unsigned int abytes, unsigned int ibytes,
  3220. unsigned int tbytes, const unsigned char* key,
  3221. int nr)
  3222. XASM_LINK("AES_GCM_encrypt_avx1");
  3223. #ifdef HAVE_INTEL_AVX2
  3224. void AES_GCM_encrypt_avx2(const unsigned char *in, unsigned char *out,
  3225. const unsigned char* addt, const unsigned char* ivec,
  3226. unsigned char *tag, unsigned int nbytes,
  3227. unsigned int abytes, unsigned int ibytes,
  3228. unsigned int tbytes, const unsigned char* key,
  3229. int nr)
  3230. XASM_LINK("AES_GCM_encrypt_avx2");
  3231. #endif /* HAVE_INTEL_AVX2 */
  3232. #endif /* HAVE_INTEL_AVX1 */
  3233. #ifdef HAVE_AES_DECRYPT
  3234. void AES_GCM_decrypt(const unsigned char *in, unsigned char *out,
  3235. const unsigned char* addt, const unsigned char* ivec,
  3236. const unsigned char *tag, int nbytes, int abytes,
  3237. int ibytes, int tbytes, const unsigned char* key, int nr,
  3238. int* res)
  3239. XASM_LINK("AES_GCM_decrypt");
  3240. #ifdef HAVE_INTEL_AVX1
  3241. void AES_GCM_decrypt_avx1(const unsigned char *in, unsigned char *out,
  3242. const unsigned char* addt, const unsigned char* ivec,
  3243. const unsigned char *tag, int nbytes, int abytes,
  3244. int ibytes, int tbytes, const unsigned char* key,
  3245. int nr, int* res)
  3246. XASM_LINK("AES_GCM_decrypt_avx1");
  3247. #ifdef HAVE_INTEL_AVX2
  3248. void AES_GCM_decrypt_avx2(const unsigned char *in, unsigned char *out,
  3249. const unsigned char* addt, const unsigned char* ivec,
  3250. const unsigned char *tag, int nbytes, int abytes,
  3251. int ibytes, int tbytes, const unsigned char* key,
  3252. int nr, int* res)
  3253. XASM_LINK("AES_GCM_decrypt_avx2");
  3254. #endif /* HAVE_INTEL_AVX2 */
  3255. #endif /* HAVE_INTEL_AVX1 */
  3256. #endif /* HAVE_AES_DECRYPT */
  3257. #else /* _MSC_VER */
  3258. #define S(w,z) ((char)((unsigned long long)(w) >> (8*(7-(z))) & 0xFF))
  3259. #define M128_INIT(x,y) { S((x),7), S((x),6), S((x),5), S((x),4), \
  3260. S((x),3), S((x),2), S((x),1), S((x),0), \
  3261. S((y),7), S((y),6), S((y),5), S((y),4), \
  3262. S((y),3), S((y),2), S((y),1), S((y),0) }
  3263. static const __m128i MOD2_128 =
  3264. M128_INIT(0x1, (long long int)0xc200000000000000UL);
  3265. /* See Intel® Carry-Less Multiplication Instruction
  3266. * and its Usage for Computing the GCM Mode White Paper
  3267. * by Shay Gueron, Intel Mobility Group, Israel Development Center;
  3268. * and Michael E. Kounavis, Intel Labs, Circuits and Systems Research */
  3269. /* Figure 9. AES-GCM – Encrypt With Single Block Ghash at a Time */
  3270. static const __m128i ONE = M128_INIT(0x0, 0x1);
  3271. #ifndef AES_GCM_AESNI_NO_UNROLL
  3272. static const __m128i TWO = M128_INIT(0x0, 0x2);
  3273. static const __m128i THREE = M128_INIT(0x0, 0x3);
  3274. static const __m128i FOUR = M128_INIT(0x0, 0x4);
  3275. static const __m128i FIVE = M128_INIT(0x0, 0x5);
  3276. static const __m128i SIX = M128_INIT(0x0, 0x6);
  3277. static const __m128i SEVEN = M128_INIT(0x0, 0x7);
  3278. static const __m128i EIGHT = M128_INIT(0x0, 0x8);
  3279. #endif
  3280. static const __m128i BSWAP_EPI64 =
  3281. M128_INIT(0x0001020304050607, 0x08090a0b0c0d0e0f);
  3282. static const __m128i BSWAP_MASK =
  3283. M128_INIT(0x08090a0b0c0d0e0f, 0x0001020304050607);
  3284. /* The following are for MSC based builds which do not allow
  3285. * inline assembly. Intrinsic functions are used instead. */
  3286. #define aes_gcm_calc_iv_12(KEY, ivec, nr, H, Y, T) \
  3287. do \
  3288. { \
  3289. word32 iv12[4]; \
  3290. iv12[0] = *(word32*)&ivec[0]; \
  3291. iv12[1] = *(word32*)&ivec[4]; \
  3292. iv12[2] = *(word32*)&ivec[8]; \
  3293. iv12[3] = 0x01000000; \
  3294. Y = _mm_loadu_si128((__m128i*)iv12); \
  3295. \
  3296. /* (Compute E[ZERO, KS] and E[Y0, KS] together */ \
  3297. tmp1 = _mm_load_si128(&KEY[0]); \
  3298. tmp2 = _mm_xor_si128(Y, KEY[0]); \
  3299. tmp1 = _mm_aesenc_si128(tmp1, KEY[1]); \
  3300. tmp2 = _mm_aesenc_si128(tmp2, KEY[1]); \
  3301. tmp1 = _mm_aesenc_si128(tmp1, KEY[2]); \
  3302. tmp2 = _mm_aesenc_si128(tmp2, KEY[2]); \
  3303. tmp1 = _mm_aesenc_si128(tmp1, KEY[3]); \
  3304. tmp2 = _mm_aesenc_si128(tmp2, KEY[3]); \
  3305. tmp1 = _mm_aesenc_si128(tmp1, KEY[4]); \
  3306. tmp2 = _mm_aesenc_si128(tmp2, KEY[4]); \
  3307. tmp1 = _mm_aesenc_si128(tmp1, KEY[5]); \
  3308. tmp2 = _mm_aesenc_si128(tmp2, KEY[5]); \
  3309. tmp1 = _mm_aesenc_si128(tmp1, KEY[6]); \
  3310. tmp2 = _mm_aesenc_si128(tmp2, KEY[6]); \
  3311. tmp1 = _mm_aesenc_si128(tmp1, KEY[7]); \
  3312. tmp2 = _mm_aesenc_si128(tmp2, KEY[7]); \
  3313. tmp1 = _mm_aesenc_si128(tmp1, KEY[8]); \
  3314. tmp2 = _mm_aesenc_si128(tmp2, KEY[8]); \
  3315. tmp1 = _mm_aesenc_si128(tmp1, KEY[9]); \
  3316. tmp2 = _mm_aesenc_si128(tmp2, KEY[9]); \
  3317. lastKey = KEY[10]; \
  3318. if (nr > 10) { \
  3319. tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
  3320. tmp2 = _mm_aesenc_si128(tmp2, lastKey); \
  3321. tmp1 = _mm_aesenc_si128(tmp1, KEY[11]); \
  3322. tmp2 = _mm_aesenc_si128(tmp2, KEY[11]); \
  3323. lastKey = KEY[12]; \
  3324. if (nr > 12) { \
  3325. tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
  3326. tmp2 = _mm_aesenc_si128(tmp2, lastKey); \
  3327. tmp1 = _mm_aesenc_si128(tmp1, KEY[13]); \
  3328. tmp2 = _mm_aesenc_si128(tmp2, KEY[13]); \
  3329. lastKey = KEY[14]; \
  3330. } \
  3331. } \
  3332. H = _mm_aesenclast_si128(tmp1, lastKey); \
  3333. T = _mm_aesenclast_si128(tmp2, lastKey); \
  3334. H = _mm_shuffle_epi8(H, BSWAP_MASK); \
  3335. } \
  3336. while (0)
  3337. #define aes_gcm_calc_iv(KEY, ivec, ibytes, nr, H, Y, T) \
  3338. do \
  3339. { \
  3340. if (ibytes % 16) { \
  3341. i = ibytes / 16; \
  3342. for (j=0; j < (int)(ibytes%16); j++) \
  3343. ((unsigned char*)&last_block)[j] = ivec[i*16+j]; \
  3344. } \
  3345. tmp1 = _mm_load_si128(&KEY[0]); \
  3346. tmp1 = _mm_aesenc_si128(tmp1, KEY[1]); \
  3347. tmp1 = _mm_aesenc_si128(tmp1, KEY[2]); \
  3348. tmp1 = _mm_aesenc_si128(tmp1, KEY[3]); \
  3349. tmp1 = _mm_aesenc_si128(tmp1, KEY[4]); \
  3350. tmp1 = _mm_aesenc_si128(tmp1, KEY[5]); \
  3351. tmp1 = _mm_aesenc_si128(tmp1, KEY[6]); \
  3352. tmp1 = _mm_aesenc_si128(tmp1, KEY[7]); \
  3353. tmp1 = _mm_aesenc_si128(tmp1, KEY[8]); \
  3354. tmp1 = _mm_aesenc_si128(tmp1, KEY[9]); \
  3355. lastKey = KEY[10]; \
  3356. if (nr > 10) { \
  3357. tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
  3358. tmp1 = _mm_aesenc_si128(tmp1, KEY[11]); \
  3359. lastKey = KEY[12]; \
  3360. if (nr > 12) { \
  3361. tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
  3362. tmp1 = _mm_aesenc_si128(tmp1, KEY[13]); \
  3363. lastKey = KEY[14]; \
  3364. } \
  3365. } \
  3366. H = _mm_aesenclast_si128(tmp1, lastKey); \
  3367. H = _mm_shuffle_epi8(H, BSWAP_MASK); \
  3368. Y = _mm_setzero_si128(); \
  3369. for (i=0; i < (int)(ibytes/16); i++) { \
  3370. tmp1 = _mm_loadu_si128(&((__m128i*)ivec)[i]); \
  3371. tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK); \
  3372. Y = _mm_xor_si128(Y, tmp1); \
  3373. Y = gfmul_sw(Y, H); \
  3374. } \
  3375. if (ibytes % 16) { \
  3376. tmp1 = last_block; \
  3377. tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK); \
  3378. Y = _mm_xor_si128(Y, tmp1); \
  3379. Y = gfmul_sw(Y, H); \
  3380. } \
  3381. tmp1 = _mm_insert_epi64(tmp1, ibytes*8, 0); \
  3382. tmp1 = _mm_insert_epi64(tmp1, 0, 1); \
  3383. Y = _mm_xor_si128(Y, tmp1); \
  3384. Y = gfmul_sw(Y, H); \
  3385. Y = _mm_shuffle_epi8(Y, BSWAP_MASK); /* Compute E(K, Y0) */ \
  3386. tmp1 = _mm_xor_si128(Y, KEY[0]); \
  3387. tmp1 = _mm_aesenc_si128(tmp1, KEY[1]); \
  3388. tmp1 = _mm_aesenc_si128(tmp1, KEY[2]); \
  3389. tmp1 = _mm_aesenc_si128(tmp1, KEY[3]); \
  3390. tmp1 = _mm_aesenc_si128(tmp1, KEY[4]); \
  3391. tmp1 = _mm_aesenc_si128(tmp1, KEY[5]); \
  3392. tmp1 = _mm_aesenc_si128(tmp1, KEY[6]); \
  3393. tmp1 = _mm_aesenc_si128(tmp1, KEY[7]); \
  3394. tmp1 = _mm_aesenc_si128(tmp1, KEY[8]); \
  3395. tmp1 = _mm_aesenc_si128(tmp1, KEY[9]); \
  3396. lastKey = KEY[10]; \
  3397. if (nr > 10) { \
  3398. tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
  3399. tmp1 = _mm_aesenc_si128(tmp1, KEY[11]); \
  3400. lastKey = KEY[12]; \
  3401. if (nr > 12) { \
  3402. tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
  3403. tmp1 = _mm_aesenc_si128(tmp1, KEY[13]); \
  3404. lastKey = KEY[14]; \
  3405. } \
  3406. } \
  3407. T = _mm_aesenclast_si128(tmp1, lastKey); \
  3408. } \
  3409. while (0)
  3410. #define AES_ENC_8(j) \
  3411. tmp1 = _mm_aesenc_si128(tmp1, KEY[j]); \
  3412. tmp2 = _mm_aesenc_si128(tmp2, KEY[j]); \
  3413. tmp3 = _mm_aesenc_si128(tmp3, KEY[j]); \
  3414. tmp4 = _mm_aesenc_si128(tmp4, KEY[j]); \
  3415. tmp5 = _mm_aesenc_si128(tmp5, KEY[j]); \
  3416. tmp6 = _mm_aesenc_si128(tmp6, KEY[j]); \
  3417. tmp7 = _mm_aesenc_si128(tmp7, KEY[j]); \
  3418. tmp8 = _mm_aesenc_si128(tmp8, KEY[j]);
  3419. #define AES_ENC_LAST_8() \
  3420. tmp1 =_mm_aesenclast_si128(tmp1, lastKey); \
  3421. tmp2 =_mm_aesenclast_si128(tmp2, lastKey); \
  3422. tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[i*8+0])); \
  3423. tmp2 = _mm_xor_si128(tmp2, _mm_loadu_si128(&((__m128i*)in)[i*8+1])); \
  3424. _mm_storeu_si128(&((__m128i*)out)[i*8+0], tmp1); \
  3425. _mm_storeu_si128(&((__m128i*)out)[i*8+1], tmp2); \
  3426. tmp3 =_mm_aesenclast_si128(tmp3, lastKey); \
  3427. tmp4 =_mm_aesenclast_si128(tmp4, lastKey); \
  3428. tmp3 = _mm_xor_si128(tmp3, _mm_loadu_si128(&((__m128i*)in)[i*8+2])); \
  3429. tmp4 = _mm_xor_si128(tmp4, _mm_loadu_si128(&((__m128i*)in)[i*8+3])); \
  3430. _mm_storeu_si128(&((__m128i*)out)[i*8+2], tmp3); \
  3431. _mm_storeu_si128(&((__m128i*)out)[i*8+3], tmp4); \
  3432. tmp5 =_mm_aesenclast_si128(tmp5, lastKey); \
  3433. tmp6 =_mm_aesenclast_si128(tmp6, lastKey); \
  3434. tmp5 = _mm_xor_si128(tmp5, _mm_loadu_si128(&((__m128i*)in)[i*8+4])); \
  3435. tmp6 = _mm_xor_si128(tmp6, _mm_loadu_si128(&((__m128i*)in)[i*8+5])); \
  3436. _mm_storeu_si128(&((__m128i*)out)[i*8+4], tmp5); \
  3437. _mm_storeu_si128(&((__m128i*)out)[i*8+5], tmp6); \
  3438. tmp7 =_mm_aesenclast_si128(tmp7, lastKey); \
  3439. tmp8 =_mm_aesenclast_si128(tmp8, lastKey); \
  3440. tmp7 = _mm_xor_si128(tmp7, _mm_loadu_si128(&((__m128i*)in)[i*8+6])); \
  3441. tmp8 = _mm_xor_si128(tmp8, _mm_loadu_si128(&((__m128i*)in)[i*8+7])); \
  3442. _mm_storeu_si128(&((__m128i*)out)[i*8+6], tmp7); \
  3443. _mm_storeu_si128(&((__m128i*)out)[i*8+7], tmp8);
  3444. static __m128i gfmul_sw(__m128i a, __m128i b)
  3445. {
  3446. __m128i r, t1, t2, t3, t4, t5, t6, t7;
  3447. t2 = _mm_shuffle_epi32(b, 78);
  3448. t3 = _mm_shuffle_epi32(a, 78);
  3449. t2 = _mm_xor_si128(t2, b);
  3450. t3 = _mm_xor_si128(t3, a);
  3451. t4 = _mm_clmulepi64_si128(b, a, 0x11);
  3452. t1 = _mm_clmulepi64_si128(b, a, 0x00);
  3453. t2 = _mm_clmulepi64_si128(t2, t3, 0x00);
  3454. t2 = _mm_xor_si128(t2, t1);
  3455. t2 = _mm_xor_si128(t2, t4);
  3456. t3 = _mm_slli_si128(t2, 8);
  3457. t2 = _mm_srli_si128(t2, 8);
  3458. t1 = _mm_xor_si128(t1, t3);
  3459. t4 = _mm_xor_si128(t4, t2);
  3460. t5 = _mm_srli_epi32(t1, 31);
  3461. t6 = _mm_srli_epi32(t4, 31);
  3462. t1 = _mm_slli_epi32(t1, 1);
  3463. t4 = _mm_slli_epi32(t4, 1);
  3464. t7 = _mm_srli_si128(t5, 12);
  3465. t5 = _mm_slli_si128(t5, 4);
  3466. t6 = _mm_slli_si128(t6, 4);
  3467. t4 = _mm_or_si128(t4, t7);
  3468. t1 = _mm_or_si128(t1, t5);
  3469. t4 = _mm_or_si128(t4, t6);
  3470. t5 = _mm_slli_epi32(t1, 31);
  3471. t6 = _mm_slli_epi32(t1, 30);
  3472. t7 = _mm_slli_epi32(t1, 25);
  3473. t5 = _mm_xor_si128(t5, t6);
  3474. t5 = _mm_xor_si128(t5, t7);
  3475. t6 = _mm_srli_si128(t5, 4);
  3476. t5 = _mm_slli_si128(t5, 12);
  3477. t1 = _mm_xor_si128(t1, t5);
  3478. t7 = _mm_srli_epi32(t1, 1);
  3479. t3 = _mm_srli_epi32(t1, 2);
  3480. t2 = _mm_srli_epi32(t1, 7);
  3481. t7 = _mm_xor_si128(t7, t3);
  3482. t7 = _mm_xor_si128(t7, t2);
  3483. t7 = _mm_xor_si128(t7, t6);
  3484. t7 = _mm_xor_si128(t7, t1);
  3485. r = _mm_xor_si128(t4, t7);
  3486. return r;
  3487. }
  3488. static void gfmul_only(__m128i a, __m128i b, __m128i* r0, __m128i* r1)
  3489. {
  3490. __m128i t1, t2, t3, t4;
  3491. /* 128 x 128 Carryless Multiply */
  3492. t2 = _mm_shuffle_epi32(b, 78);
  3493. t3 = _mm_shuffle_epi32(a, 78);
  3494. t2 = _mm_xor_si128(t2, b);
  3495. t3 = _mm_xor_si128(t3, a);
  3496. t4 = _mm_clmulepi64_si128(b, a, 0x11);
  3497. t1 = _mm_clmulepi64_si128(b, a, 0x00);
  3498. t2 = _mm_clmulepi64_si128(t2, t3, 0x00);
  3499. t2 = _mm_xor_si128(t2, t1);
  3500. t2 = _mm_xor_si128(t2, t4);
  3501. t3 = _mm_slli_si128(t2, 8);
  3502. t2 = _mm_srli_si128(t2, 8);
  3503. t1 = _mm_xor_si128(t1, t3);
  3504. t4 = _mm_xor_si128(t4, t2);
  3505. *r0 = _mm_xor_si128(t1, *r0);
  3506. *r1 = _mm_xor_si128(t4, *r1);
  3507. }
  3508. static __m128i gfmul_shl1(__m128i a)
  3509. {
  3510. __m128i t1 = a, t2;
  3511. t2 = _mm_srli_epi64(t1, 63);
  3512. t1 = _mm_slli_epi64(t1, 1);
  3513. t2 = _mm_slli_si128(t2, 8);
  3514. t1 = _mm_or_si128(t1, t2);
  3515. /* if (a[1] >> 63) t1 = _mm_xor_si128(t1, MOD2_128); */
  3516. a = _mm_shuffle_epi32(a, 0xff);
  3517. a = _mm_srai_epi32(a, 31);
  3518. a = _mm_and_si128(a, MOD2_128);
  3519. t1 = _mm_xor_si128(t1, a);
  3520. return t1;
  3521. }
  3522. static __m128i ghash_red(__m128i r0, __m128i r1)
  3523. {
  3524. __m128i t2, t3;
  3525. __m128i t5, t6, t7;
  3526. t5 = _mm_slli_epi32(r0, 31);
  3527. t6 = _mm_slli_epi32(r0, 30);
  3528. t7 = _mm_slli_epi32(r0, 25);
  3529. t5 = _mm_xor_si128(t5, t6);
  3530. t5 = _mm_xor_si128(t5, t7);
  3531. t6 = _mm_srli_si128(t5, 4);
  3532. t5 = _mm_slli_si128(t5, 12);
  3533. r0 = _mm_xor_si128(r0, t5);
  3534. t7 = _mm_srli_epi32(r0, 1);
  3535. t3 = _mm_srli_epi32(r0, 2);
  3536. t2 = _mm_srli_epi32(r0, 7);
  3537. t7 = _mm_xor_si128(t7, t3);
  3538. t7 = _mm_xor_si128(t7, t2);
  3539. t7 = _mm_xor_si128(t7, t6);
  3540. t7 = _mm_xor_si128(t7, r0);
  3541. return _mm_xor_si128(r1, t7);
  3542. }
  3543. static __m128i gfmul_shifted(__m128i a, __m128i b)
  3544. {
  3545. __m128i t0 = _mm_setzero_si128(), t1 = _mm_setzero_si128();
  3546. gfmul_only(a, b, &t0, &t1);
  3547. return ghash_red(t0, t1);
  3548. }
  3549. #ifndef AES_GCM_AESNI_NO_UNROLL
  3550. static __m128i gfmul8(__m128i a1, __m128i a2, __m128i a3, __m128i a4,
  3551. __m128i a5, __m128i a6, __m128i a7, __m128i a8,
  3552. __m128i b1, __m128i b2, __m128i b3, __m128i b4,
  3553. __m128i b5, __m128i b6, __m128i b7, __m128i b8)
  3554. {
  3555. __m128i t0 = _mm_setzero_si128(), t1 = _mm_setzero_si128();
  3556. gfmul_only(a1, b8, &t0, &t1);
  3557. gfmul_only(a2, b7, &t0, &t1);
  3558. gfmul_only(a3, b6, &t0, &t1);
  3559. gfmul_only(a4, b5, &t0, &t1);
  3560. gfmul_only(a5, b4, &t0, &t1);
  3561. gfmul_only(a6, b3, &t0, &t1);
  3562. gfmul_only(a7, b2, &t0, &t1);
  3563. gfmul_only(a8, b1, &t0, &t1);
  3564. return ghash_red(t0, t1);
  3565. }
  3566. #endif
  3567. static void AES_GCM_encrypt(const unsigned char *in,
  3568. unsigned char *out,
  3569. const unsigned char* addt,
  3570. const unsigned char* ivec,
  3571. unsigned char *tag, unsigned int nbytes,
  3572. unsigned int abytes, unsigned int ibytes,
  3573. unsigned int tbytes,
  3574. const unsigned char* key, int nr)
  3575. {
  3576. int i, j ,k;
  3577. __m128i ctr1;
  3578. __m128i H, Y, T;
  3579. __m128i X = _mm_setzero_si128();
  3580. __m128i *KEY = (__m128i*)key, lastKey;
  3581. __m128i last_block = _mm_setzero_si128();
  3582. __m128i tmp1, tmp2;
  3583. #ifndef AES_GCM_AESNI_NO_UNROLL
  3584. __m128i HT[8];
  3585. __m128i r0, r1;
  3586. __m128i XV;
  3587. __m128i tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
  3588. #endif
  3589. if (ibytes == GCM_NONCE_MID_SZ)
  3590. aes_gcm_calc_iv_12(KEY, ivec, nr, H, Y, T);
  3591. else
  3592. aes_gcm_calc_iv(KEY, ivec, ibytes, nr, H, Y, T);
  3593. for (i=0; i < (int)(abytes/16); i++) {
  3594. tmp1 = _mm_loadu_si128(&((__m128i*)addt)[i]);
  3595. tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
  3596. X = _mm_xor_si128(X, tmp1);
  3597. X = gfmul_sw(X, H);
  3598. }
  3599. if (abytes%16) {
  3600. last_block = _mm_setzero_si128();
  3601. for (j=0; j < (int)(abytes%16); j++)
  3602. ((unsigned char*)&last_block)[j] = addt[i*16+j];
  3603. tmp1 = last_block;
  3604. tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
  3605. X = _mm_xor_si128(X, tmp1);
  3606. X = gfmul_sw(X, H);
  3607. }
  3608. tmp1 = _mm_shuffle_epi8(Y, BSWAP_EPI64);
  3609. ctr1 = _mm_add_epi32(tmp1, ONE);
  3610. H = gfmul_shl1(H);
  3611. #ifndef AES_GCM_AESNI_NO_UNROLL
  3612. i = 0;
  3613. if (nbytes >= 16*8) {
  3614. HT[0] = H;
  3615. HT[1] = gfmul_shifted(H, H);
  3616. HT[2] = gfmul_shifted(H, HT[1]);
  3617. HT[3] = gfmul_shifted(HT[1], HT[1]);
  3618. HT[4] = gfmul_shifted(HT[1], HT[2]);
  3619. HT[5] = gfmul_shifted(HT[2], HT[2]);
  3620. HT[6] = gfmul_shifted(HT[2], HT[3]);
  3621. HT[7] = gfmul_shifted(HT[3], HT[3]);
  3622. tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
  3623. tmp2 = _mm_add_epi32(ctr1, ONE);
  3624. tmp2 = _mm_shuffle_epi8(tmp2, BSWAP_EPI64);
  3625. tmp3 = _mm_add_epi32(ctr1, TWO);
  3626. tmp3 = _mm_shuffle_epi8(tmp3, BSWAP_EPI64);
  3627. tmp4 = _mm_add_epi32(ctr1, THREE);
  3628. tmp4 = _mm_shuffle_epi8(tmp4, BSWAP_EPI64);
  3629. tmp5 = _mm_add_epi32(ctr1, FOUR);
  3630. tmp5 = _mm_shuffle_epi8(tmp5, BSWAP_EPI64);
  3631. tmp6 = _mm_add_epi32(ctr1, FIVE);
  3632. tmp6 = _mm_shuffle_epi8(tmp6, BSWAP_EPI64);
  3633. tmp7 = _mm_add_epi32(ctr1, SIX);
  3634. tmp7 = _mm_shuffle_epi8(tmp7, BSWAP_EPI64);
  3635. tmp8 = _mm_add_epi32(ctr1, SEVEN);
  3636. tmp8 = _mm_shuffle_epi8(tmp8, BSWAP_EPI64);
  3637. ctr1 = _mm_add_epi32(ctr1, EIGHT);
  3638. tmp1 =_mm_xor_si128(tmp1, KEY[0]);
  3639. tmp2 =_mm_xor_si128(tmp2, KEY[0]);
  3640. tmp3 =_mm_xor_si128(tmp3, KEY[0]);
  3641. tmp4 =_mm_xor_si128(tmp4, KEY[0]);
  3642. tmp5 =_mm_xor_si128(tmp5, KEY[0]);
  3643. tmp6 =_mm_xor_si128(tmp6, KEY[0]);
  3644. tmp7 =_mm_xor_si128(tmp7, KEY[0]);
  3645. tmp8 =_mm_xor_si128(tmp8, KEY[0]);
  3646. AES_ENC_8(1);
  3647. AES_ENC_8(2);
  3648. AES_ENC_8(3);
  3649. AES_ENC_8(4);
  3650. AES_ENC_8(5);
  3651. AES_ENC_8(6);
  3652. AES_ENC_8(7);
  3653. AES_ENC_8(8);
  3654. AES_ENC_8(9);
  3655. lastKey = KEY[10];
  3656. if (nr > 10) {
  3657. AES_ENC_8(10);
  3658. AES_ENC_8(11);
  3659. lastKey = KEY[12];
  3660. if (nr > 12) {
  3661. AES_ENC_8(12);
  3662. AES_ENC_8(13);
  3663. lastKey = KEY[14];
  3664. }
  3665. }
  3666. AES_ENC_LAST_8();
  3667. for (i=1; i < (int)(nbytes/16/8); i++) {
  3668. r0 = _mm_setzero_si128();
  3669. r1 = _mm_setzero_si128();
  3670. tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
  3671. tmp2 = _mm_add_epi32(ctr1, ONE);
  3672. tmp2 = _mm_shuffle_epi8(tmp2, BSWAP_EPI64);
  3673. tmp3 = _mm_add_epi32(ctr1, TWO);
  3674. tmp3 = _mm_shuffle_epi8(tmp3, BSWAP_EPI64);
  3675. tmp4 = _mm_add_epi32(ctr1, THREE);
  3676. tmp4 = _mm_shuffle_epi8(tmp4, BSWAP_EPI64);
  3677. tmp5 = _mm_add_epi32(ctr1, FOUR);
  3678. tmp5 = _mm_shuffle_epi8(tmp5, BSWAP_EPI64);
  3679. tmp6 = _mm_add_epi32(ctr1, FIVE);
  3680. tmp6 = _mm_shuffle_epi8(tmp6, BSWAP_EPI64);
  3681. tmp7 = _mm_add_epi32(ctr1, SIX);
  3682. tmp7 = _mm_shuffle_epi8(tmp7, BSWAP_EPI64);
  3683. tmp8 = _mm_add_epi32(ctr1, SEVEN);
  3684. tmp8 = _mm_shuffle_epi8(tmp8, BSWAP_EPI64);
  3685. ctr1 = _mm_add_epi32(ctr1, EIGHT);
  3686. tmp1 =_mm_xor_si128(tmp1, KEY[0]);
  3687. tmp2 =_mm_xor_si128(tmp2, KEY[0]);
  3688. tmp3 =_mm_xor_si128(tmp3, KEY[0]);
  3689. tmp4 =_mm_xor_si128(tmp4, KEY[0]);
  3690. tmp5 =_mm_xor_si128(tmp5, KEY[0]);
  3691. tmp6 =_mm_xor_si128(tmp6, KEY[0]);
  3692. tmp7 =_mm_xor_si128(tmp7, KEY[0]);
  3693. tmp8 =_mm_xor_si128(tmp8, KEY[0]);
  3694. /* 128 x 128 Carryless Multiply */
  3695. XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+0]);
  3696. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  3697. XV = _mm_xor_si128(XV, X);
  3698. gfmul_only(XV, HT[7], &r0, &r1);
  3699. tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
  3700. tmp2 = _mm_aesenc_si128(tmp2, KEY[1]);
  3701. tmp3 = _mm_aesenc_si128(tmp3, KEY[1]);
  3702. tmp4 = _mm_aesenc_si128(tmp4, KEY[1]);
  3703. tmp5 = _mm_aesenc_si128(tmp5, KEY[1]);
  3704. tmp6 = _mm_aesenc_si128(tmp6, KEY[1]);
  3705. tmp7 = _mm_aesenc_si128(tmp7, KEY[1]);
  3706. tmp8 = _mm_aesenc_si128(tmp8, KEY[1]);
  3707. /* 128 x 128 Carryless Multiply */
  3708. XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+1]);
  3709. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  3710. gfmul_only(XV, HT[6], &r0, &r1);
  3711. tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
  3712. tmp2 = _mm_aesenc_si128(tmp2, KEY[2]);
  3713. tmp3 = _mm_aesenc_si128(tmp3, KEY[2]);
  3714. tmp4 = _mm_aesenc_si128(tmp4, KEY[2]);
  3715. tmp5 = _mm_aesenc_si128(tmp5, KEY[2]);
  3716. tmp6 = _mm_aesenc_si128(tmp6, KEY[2]);
  3717. tmp7 = _mm_aesenc_si128(tmp7, KEY[2]);
  3718. tmp8 = _mm_aesenc_si128(tmp8, KEY[2]);
  3719. /* 128 x 128 Carryless Multiply */
  3720. XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+2]);
  3721. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  3722. gfmul_only(XV, HT[5], &r0, &r1);
  3723. tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
  3724. tmp2 = _mm_aesenc_si128(tmp2, KEY[3]);
  3725. tmp3 = _mm_aesenc_si128(tmp3, KEY[3]);
  3726. tmp4 = _mm_aesenc_si128(tmp4, KEY[3]);
  3727. tmp5 = _mm_aesenc_si128(tmp5, KEY[3]);
  3728. tmp6 = _mm_aesenc_si128(tmp6, KEY[3]);
  3729. tmp7 = _mm_aesenc_si128(tmp7, KEY[3]);
  3730. tmp8 = _mm_aesenc_si128(tmp8, KEY[3]);
  3731. /* 128 x 128 Carryless Multiply */
  3732. XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+3]);
  3733. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  3734. gfmul_only(XV, HT[4], &r0, &r1);
  3735. tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
  3736. tmp2 = _mm_aesenc_si128(tmp2, KEY[4]);
  3737. tmp3 = _mm_aesenc_si128(tmp3, KEY[4]);
  3738. tmp4 = _mm_aesenc_si128(tmp4, KEY[4]);
  3739. tmp5 = _mm_aesenc_si128(tmp5, KEY[4]);
  3740. tmp6 = _mm_aesenc_si128(tmp6, KEY[4]);
  3741. tmp7 = _mm_aesenc_si128(tmp7, KEY[4]);
  3742. tmp8 = _mm_aesenc_si128(tmp8, KEY[4]);
  3743. /* 128 x 128 Carryless Multiply */
  3744. XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+4]);
  3745. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  3746. gfmul_only(XV, HT[3], &r0, &r1);
  3747. tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
  3748. tmp2 = _mm_aesenc_si128(tmp2, KEY[5]);
  3749. tmp3 = _mm_aesenc_si128(tmp3, KEY[5]);
  3750. tmp4 = _mm_aesenc_si128(tmp4, KEY[5]);
  3751. tmp5 = _mm_aesenc_si128(tmp5, KEY[5]);
  3752. tmp6 = _mm_aesenc_si128(tmp6, KEY[5]);
  3753. tmp7 = _mm_aesenc_si128(tmp7, KEY[5]);
  3754. tmp8 = _mm_aesenc_si128(tmp8, KEY[5]);
  3755. /* 128 x 128 Carryless Multiply */
  3756. XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+5]);
  3757. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  3758. gfmul_only(XV, HT[2], &r0, &r1);
  3759. tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
  3760. tmp2 = _mm_aesenc_si128(tmp2, KEY[6]);
  3761. tmp3 = _mm_aesenc_si128(tmp3, KEY[6]);
  3762. tmp4 = _mm_aesenc_si128(tmp4, KEY[6]);
  3763. tmp5 = _mm_aesenc_si128(tmp5, KEY[6]);
  3764. tmp6 = _mm_aesenc_si128(tmp6, KEY[6]);
  3765. tmp7 = _mm_aesenc_si128(tmp7, KEY[6]);
  3766. tmp8 = _mm_aesenc_si128(tmp8, KEY[6]);
  3767. /* 128 x 128 Carryless Multiply */
  3768. XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+6]);
  3769. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  3770. gfmul_only(XV, HT[1], &r0, &r1);
  3771. tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
  3772. tmp2 = _mm_aesenc_si128(tmp2, KEY[7]);
  3773. tmp3 = _mm_aesenc_si128(tmp3, KEY[7]);
  3774. tmp4 = _mm_aesenc_si128(tmp4, KEY[7]);
  3775. tmp5 = _mm_aesenc_si128(tmp5, KEY[7]);
  3776. tmp6 = _mm_aesenc_si128(tmp6, KEY[7]);
  3777. tmp7 = _mm_aesenc_si128(tmp7, KEY[7]);
  3778. tmp8 = _mm_aesenc_si128(tmp8, KEY[7]);
  3779. /* 128 x 128 Carryless Multiply */
  3780. XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+7]);
  3781. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  3782. gfmul_only(XV, HT[0], &r0, &r1);
  3783. tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
  3784. tmp2 = _mm_aesenc_si128(tmp2, KEY[8]);
  3785. tmp3 = _mm_aesenc_si128(tmp3, KEY[8]);
  3786. tmp4 = _mm_aesenc_si128(tmp4, KEY[8]);
  3787. tmp5 = _mm_aesenc_si128(tmp5, KEY[8]);
  3788. tmp6 = _mm_aesenc_si128(tmp6, KEY[8]);
  3789. tmp7 = _mm_aesenc_si128(tmp7, KEY[8]);
  3790. tmp8 = _mm_aesenc_si128(tmp8, KEY[8]);
  3791. /* Reduction */
  3792. X = ghash_red(r0, r1);
  3793. tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
  3794. tmp2 = _mm_aesenc_si128(tmp2, KEY[9]);
  3795. tmp3 = _mm_aesenc_si128(tmp3, KEY[9]);
  3796. tmp4 = _mm_aesenc_si128(tmp4, KEY[9]);
  3797. tmp5 = _mm_aesenc_si128(tmp5, KEY[9]);
  3798. tmp6 = _mm_aesenc_si128(tmp6, KEY[9]);
  3799. tmp7 = _mm_aesenc_si128(tmp7, KEY[9]);
  3800. tmp8 = _mm_aesenc_si128(tmp8, KEY[9]);
  3801. lastKey = KEY[10];
  3802. if (nr > 10) {
  3803. tmp1 = _mm_aesenc_si128(tmp1, KEY[10]);
  3804. tmp2 = _mm_aesenc_si128(tmp2, KEY[10]);
  3805. tmp3 = _mm_aesenc_si128(tmp3, KEY[10]);
  3806. tmp4 = _mm_aesenc_si128(tmp4, KEY[10]);
  3807. tmp5 = _mm_aesenc_si128(tmp5, KEY[10]);
  3808. tmp6 = _mm_aesenc_si128(tmp6, KEY[10]);
  3809. tmp7 = _mm_aesenc_si128(tmp7, KEY[10]);
  3810. tmp8 = _mm_aesenc_si128(tmp8, KEY[10]);
  3811. tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
  3812. tmp2 = _mm_aesenc_si128(tmp2, KEY[11]);
  3813. tmp3 = _mm_aesenc_si128(tmp3, KEY[11]);
  3814. tmp4 = _mm_aesenc_si128(tmp4, KEY[11]);
  3815. tmp5 = _mm_aesenc_si128(tmp5, KEY[11]);
  3816. tmp6 = _mm_aesenc_si128(tmp6, KEY[11]);
  3817. tmp7 = _mm_aesenc_si128(tmp7, KEY[11]);
  3818. tmp8 = _mm_aesenc_si128(tmp8, KEY[11]);
  3819. lastKey = KEY[12];
  3820. if (nr > 12) {
  3821. tmp1 = _mm_aesenc_si128(tmp1, KEY[12]);
  3822. tmp2 = _mm_aesenc_si128(tmp2, KEY[12]);
  3823. tmp3 = _mm_aesenc_si128(tmp3, KEY[12]);
  3824. tmp4 = _mm_aesenc_si128(tmp4, KEY[12]);
  3825. tmp5 = _mm_aesenc_si128(tmp5, KEY[12]);
  3826. tmp6 = _mm_aesenc_si128(tmp6, KEY[12]);
  3827. tmp7 = _mm_aesenc_si128(tmp7, KEY[12]);
  3828. tmp8 = _mm_aesenc_si128(tmp8, KEY[12]);
  3829. tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
  3830. tmp2 = _mm_aesenc_si128(tmp2, KEY[13]);
  3831. tmp3 = _mm_aesenc_si128(tmp3, KEY[13]);
  3832. tmp4 = _mm_aesenc_si128(tmp4, KEY[13]);
  3833. tmp5 = _mm_aesenc_si128(tmp5, KEY[13]);
  3834. tmp6 = _mm_aesenc_si128(tmp6, KEY[13]);
  3835. tmp7 = _mm_aesenc_si128(tmp7, KEY[13]);
  3836. tmp8 = _mm_aesenc_si128(tmp8, KEY[13]);
  3837. lastKey = KEY[14];
  3838. }
  3839. }
  3840. AES_ENC_LAST_8();
  3841. }
  3842. tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
  3843. tmp2 = _mm_shuffle_epi8(tmp2, BSWAP_MASK);
  3844. tmp3 = _mm_shuffle_epi8(tmp3, BSWAP_MASK);
  3845. tmp4 = _mm_shuffle_epi8(tmp4, BSWAP_MASK);
  3846. tmp5 = _mm_shuffle_epi8(tmp5, BSWAP_MASK);
  3847. tmp6 = _mm_shuffle_epi8(tmp6, BSWAP_MASK);
  3848. tmp7 = _mm_shuffle_epi8(tmp7, BSWAP_MASK);
  3849. tmp8 = _mm_shuffle_epi8(tmp8, BSWAP_MASK);
  3850. tmp1 = _mm_xor_si128(X, tmp1);
  3851. X = gfmul8(tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8,
  3852. HT[0], HT[1], HT[2], HT[3], HT[4], HT[5], HT[6], HT[7]);
  3853. }
  3854. for (k = i*8; k < (int)(nbytes/16); k++) {
  3855. tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
  3856. ctr1 = _mm_add_epi32(ctr1, ONE);
  3857. tmp1 = _mm_xor_si128(tmp1, KEY[0]);
  3858. tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
  3859. tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
  3860. tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
  3861. tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
  3862. tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
  3863. tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
  3864. tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
  3865. tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
  3866. tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
  3867. lastKey = KEY[10];
  3868. if (nr > 10) {
  3869. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  3870. tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
  3871. lastKey = KEY[12];
  3872. if (nr > 12) {
  3873. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  3874. tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
  3875. lastKey = KEY[14];
  3876. }
  3877. }
  3878. tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
  3879. tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[k]));
  3880. _mm_storeu_si128(&((__m128i*)out)[k], tmp1);
  3881. tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
  3882. X =_mm_xor_si128(X, tmp1);
  3883. X = gfmul_shifted(X, H);
  3884. }
  3885. #else /* AES_GCM_AESNI_NO_UNROLL */
  3886. for (k = 0; k < (int)(nbytes/16) && k < 1; k++) {
  3887. tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
  3888. ctr1 = _mm_add_epi32(ctr1, ONE);
  3889. tmp1 = _mm_xor_si128(tmp1, KEY[0]);
  3890. tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
  3891. tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
  3892. tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
  3893. tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
  3894. tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
  3895. tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
  3896. tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
  3897. tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
  3898. tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
  3899. lastKey = KEY[10];
  3900. if (nr > 10) {
  3901. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  3902. tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
  3903. lastKey = KEY[12];
  3904. if (nr > 12) {
  3905. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  3906. tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
  3907. lastKey = KEY[14];
  3908. }
  3909. }
  3910. tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
  3911. tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[k]));
  3912. _mm_storeu_si128(&((__m128i*)out)[k], tmp1);
  3913. tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
  3914. X =_mm_xor_si128(X, tmp1);
  3915. }
  3916. for (; k < (int)(nbytes/16); k++) {
  3917. tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
  3918. ctr1 = _mm_add_epi32(ctr1, ONE);
  3919. tmp1 = _mm_xor_si128(tmp1, KEY[0]);
  3920. tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
  3921. tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
  3922. tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
  3923. tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
  3924. tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
  3925. tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
  3926. tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
  3927. tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
  3928. tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
  3929. X = gfmul_shifted(X, H);
  3930. lastKey = KEY[10];
  3931. if (nr > 10) {
  3932. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  3933. tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
  3934. lastKey = KEY[12];
  3935. if (nr > 12) {
  3936. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  3937. tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
  3938. lastKey = KEY[14];
  3939. }
  3940. }
  3941. tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
  3942. tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[k]));
  3943. _mm_storeu_si128(&((__m128i*)out)[k], tmp1);
  3944. tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
  3945. X =_mm_xor_si128(X, tmp1);
  3946. }
  3947. if (k > 0) {
  3948. X = gfmul_shifted(X, H);
  3949. }
  3950. #endif /* AES_GCM_AESNI_NO_UNROLL */
  3951. /* If one partial block remains */
  3952. if (nbytes % 16) {
  3953. tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
  3954. tmp1 = _mm_xor_si128(tmp1, KEY[0]);
  3955. tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
  3956. tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
  3957. tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
  3958. tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
  3959. tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
  3960. tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
  3961. tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
  3962. tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
  3963. tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
  3964. lastKey = KEY[10];
  3965. if (nr > 10) {
  3966. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  3967. tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
  3968. lastKey = KEY[12];
  3969. if (nr > 12) {
  3970. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  3971. tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
  3972. lastKey = KEY[14];
  3973. }
  3974. }
  3975. tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
  3976. last_block = tmp1;
  3977. for (j=0; j < (int)(nbytes%16); j++)
  3978. ((unsigned char*)&last_block)[j] = in[k*16+j];
  3979. tmp1 = _mm_xor_si128(tmp1, last_block);
  3980. last_block = tmp1;
  3981. for (j=0; j < (int)(nbytes%16); j++)
  3982. out[k*16+j] = ((unsigned char*)&last_block)[j];
  3983. tmp1 = last_block;
  3984. tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
  3985. X =_mm_xor_si128(X, tmp1);
  3986. X = gfmul_shifted(X, H);
  3987. }
  3988. tmp1 = _mm_insert_epi64(tmp1, nbytes*8, 0);
  3989. tmp1 = _mm_insert_epi64(tmp1, abytes*8, 1);
  3990. X = _mm_xor_si128(X, tmp1);
  3991. X = gfmul_shifted(X, H);
  3992. X = _mm_shuffle_epi8(X, BSWAP_MASK);
  3993. T = _mm_xor_si128(X, T);
  3994. /*_mm_storeu_si128((__m128i*)tag, T);*/
  3995. XMEMCPY(tag, &T, tbytes);
  3996. }
  3997. #ifdef HAVE_AES_DECRYPT
  3998. static void AES_GCM_decrypt(const unsigned char *in,
  3999. unsigned char *out,
  4000. const unsigned char* addt,
  4001. const unsigned char* ivec,
  4002. const unsigned char *tag, int nbytes, int abytes,
  4003. int ibytes, word32 tbytes, const unsigned char* key,
  4004. int nr, int* res)
  4005. {
  4006. int i, j ,k;
  4007. __m128i H, Y, T;
  4008. __m128i *KEY = (__m128i*)key, lastKey;
  4009. __m128i ctr1;
  4010. __m128i last_block = _mm_setzero_si128();
  4011. __m128i X = _mm_setzero_si128();
  4012. __m128i tmp1, tmp2, XV;
  4013. #ifndef AES_GCM_AESNI_NO_UNROLL
  4014. __m128i HT[8];
  4015. __m128i r0, r1;
  4016. __m128i tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
  4017. #endif /* AES_GCM_AESNI_NO_UNROLL */
  4018. if (ibytes == GCM_NONCE_MID_SZ)
  4019. aes_gcm_calc_iv_12(KEY, ivec, nr, H, Y, T);
  4020. else
  4021. aes_gcm_calc_iv(KEY, ivec, ibytes, nr, H, Y, T);
  4022. for (i=0; i<abytes/16; i++) {
  4023. tmp1 = _mm_loadu_si128(&((__m128i*)addt)[i]);
  4024. tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
  4025. X = _mm_xor_si128(X, tmp1);
  4026. X = gfmul_sw(X, H);
  4027. }
  4028. if (abytes%16) {
  4029. last_block = _mm_setzero_si128();
  4030. for (j=0; j<abytes%16; j++)
  4031. ((unsigned char*)&last_block)[j] = addt[i*16+j];
  4032. tmp1 = last_block;
  4033. tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
  4034. X = _mm_xor_si128(X, tmp1);
  4035. X = gfmul_sw(X, H);
  4036. }
  4037. tmp1 = _mm_shuffle_epi8(Y, BSWAP_EPI64);
  4038. ctr1 = _mm_add_epi32(tmp1, ONE);
  4039. H = gfmul_shl1(H);
  4040. i = 0;
  4041. #ifndef AES_GCM_AESNI_NO_UNROLL
  4042. if (0 < nbytes/16/8) {
  4043. HT[0] = H;
  4044. HT[1] = gfmul_shifted(H, H);
  4045. HT[2] = gfmul_shifted(H, HT[1]);
  4046. HT[3] = gfmul_shifted(HT[1], HT[1]);
  4047. HT[4] = gfmul_shifted(HT[1], HT[2]);
  4048. HT[5] = gfmul_shifted(HT[2], HT[2]);
  4049. HT[6] = gfmul_shifted(HT[2], HT[3]);
  4050. HT[7] = gfmul_shifted(HT[3], HT[3]);
  4051. for (; i < nbytes/16/8; i++) {
  4052. r0 = _mm_setzero_si128();
  4053. r1 = _mm_setzero_si128();
  4054. tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
  4055. tmp2 = _mm_add_epi32(ctr1, ONE);
  4056. tmp2 = _mm_shuffle_epi8(tmp2, BSWAP_EPI64);
  4057. tmp3 = _mm_add_epi32(ctr1, TWO);
  4058. tmp3 = _mm_shuffle_epi8(tmp3, BSWAP_EPI64);
  4059. tmp4 = _mm_add_epi32(ctr1, THREE);
  4060. tmp4 = _mm_shuffle_epi8(tmp4, BSWAP_EPI64);
  4061. tmp5 = _mm_add_epi32(ctr1, FOUR);
  4062. tmp5 = _mm_shuffle_epi8(tmp5, BSWAP_EPI64);
  4063. tmp6 = _mm_add_epi32(ctr1, FIVE);
  4064. tmp6 = _mm_shuffle_epi8(tmp6, BSWAP_EPI64);
  4065. tmp7 = _mm_add_epi32(ctr1, SIX);
  4066. tmp7 = _mm_shuffle_epi8(tmp7, BSWAP_EPI64);
  4067. tmp8 = _mm_add_epi32(ctr1, SEVEN);
  4068. tmp8 = _mm_shuffle_epi8(tmp8, BSWAP_EPI64);
  4069. ctr1 = _mm_add_epi32(ctr1, EIGHT);
  4070. tmp1 =_mm_xor_si128(tmp1, KEY[0]);
  4071. tmp2 =_mm_xor_si128(tmp2, KEY[0]);
  4072. tmp3 =_mm_xor_si128(tmp3, KEY[0]);
  4073. tmp4 =_mm_xor_si128(tmp4, KEY[0]);
  4074. tmp5 =_mm_xor_si128(tmp5, KEY[0]);
  4075. tmp6 =_mm_xor_si128(tmp6, KEY[0]);
  4076. tmp7 =_mm_xor_si128(tmp7, KEY[0]);
  4077. tmp8 =_mm_xor_si128(tmp8, KEY[0]);
  4078. /* 128 x 128 Carryless Multiply */
  4079. XV = _mm_loadu_si128(&((__m128i*)in)[i*8+0]);
  4080. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  4081. XV = _mm_xor_si128(XV, X);
  4082. gfmul_only(XV, HT[7], &r0, &r1);
  4083. tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
  4084. tmp2 = _mm_aesenc_si128(tmp2, KEY[1]);
  4085. tmp3 = _mm_aesenc_si128(tmp3, KEY[1]);
  4086. tmp4 = _mm_aesenc_si128(tmp4, KEY[1]);
  4087. tmp5 = _mm_aesenc_si128(tmp5, KEY[1]);
  4088. tmp6 = _mm_aesenc_si128(tmp6, KEY[1]);
  4089. tmp7 = _mm_aesenc_si128(tmp7, KEY[1]);
  4090. tmp8 = _mm_aesenc_si128(tmp8, KEY[1]);
  4091. /* 128 x 128 Carryless Multiply */
  4092. XV = _mm_loadu_si128(&((__m128i*)in)[i*8+1]);
  4093. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  4094. gfmul_only(XV, HT[6], &r0, &r1);
  4095. tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
  4096. tmp2 = _mm_aesenc_si128(tmp2, KEY[2]);
  4097. tmp3 = _mm_aesenc_si128(tmp3, KEY[2]);
  4098. tmp4 = _mm_aesenc_si128(tmp4, KEY[2]);
  4099. tmp5 = _mm_aesenc_si128(tmp5, KEY[2]);
  4100. tmp6 = _mm_aesenc_si128(tmp6, KEY[2]);
  4101. tmp7 = _mm_aesenc_si128(tmp7, KEY[2]);
  4102. tmp8 = _mm_aesenc_si128(tmp8, KEY[2]);
  4103. /* 128 x 128 Carryless Multiply */
  4104. XV = _mm_loadu_si128(&((__m128i*)in)[i*8+2]);
  4105. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  4106. gfmul_only(XV, HT[5], &r0, &r1);
  4107. tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
  4108. tmp2 = _mm_aesenc_si128(tmp2, KEY[3]);
  4109. tmp3 = _mm_aesenc_si128(tmp3, KEY[3]);
  4110. tmp4 = _mm_aesenc_si128(tmp4, KEY[3]);
  4111. tmp5 = _mm_aesenc_si128(tmp5, KEY[3]);
  4112. tmp6 = _mm_aesenc_si128(tmp6, KEY[3]);
  4113. tmp7 = _mm_aesenc_si128(tmp7, KEY[3]);
  4114. tmp8 = _mm_aesenc_si128(tmp8, KEY[3]);
  4115. /* 128 x 128 Carryless Multiply */
  4116. XV = _mm_loadu_si128(&((__m128i*)in)[i*8+3]);
  4117. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  4118. gfmul_only(XV, HT[4], &r0, &r1);
  4119. tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
  4120. tmp2 = _mm_aesenc_si128(tmp2, KEY[4]);
  4121. tmp3 = _mm_aesenc_si128(tmp3, KEY[4]);
  4122. tmp4 = _mm_aesenc_si128(tmp4, KEY[4]);
  4123. tmp5 = _mm_aesenc_si128(tmp5, KEY[4]);
  4124. tmp6 = _mm_aesenc_si128(tmp6, KEY[4]);
  4125. tmp7 = _mm_aesenc_si128(tmp7, KEY[4]);
  4126. tmp8 = _mm_aesenc_si128(tmp8, KEY[4]);
  4127. /* 128 x 128 Carryless Multiply */
  4128. XV = _mm_loadu_si128(&((__m128i*)in)[i*8+4]);
  4129. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  4130. gfmul_only(XV, HT[3], &r0, &r1);
  4131. tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
  4132. tmp2 = _mm_aesenc_si128(tmp2, KEY[5]);
  4133. tmp3 = _mm_aesenc_si128(tmp3, KEY[5]);
  4134. tmp4 = _mm_aesenc_si128(tmp4, KEY[5]);
  4135. tmp5 = _mm_aesenc_si128(tmp5, KEY[5]);
  4136. tmp6 = _mm_aesenc_si128(tmp6, KEY[5]);
  4137. tmp7 = _mm_aesenc_si128(tmp7, KEY[5]);
  4138. tmp8 = _mm_aesenc_si128(tmp8, KEY[5]);
  4139. /* 128 x 128 Carryless Multiply */
  4140. XV = _mm_loadu_si128(&((__m128i*)in)[i*8+5]);
  4141. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  4142. gfmul_only(XV, HT[2], &r0, &r1);
  4143. tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
  4144. tmp2 = _mm_aesenc_si128(tmp2, KEY[6]);
  4145. tmp3 = _mm_aesenc_si128(tmp3, KEY[6]);
  4146. tmp4 = _mm_aesenc_si128(tmp4, KEY[6]);
  4147. tmp5 = _mm_aesenc_si128(tmp5, KEY[6]);
  4148. tmp6 = _mm_aesenc_si128(tmp6, KEY[6]);
  4149. tmp7 = _mm_aesenc_si128(tmp7, KEY[6]);
  4150. tmp8 = _mm_aesenc_si128(tmp8, KEY[6]);
  4151. /* 128 x 128 Carryless Multiply */
  4152. XV = _mm_loadu_si128(&((__m128i*)in)[i*8+6]);
  4153. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  4154. gfmul_only(XV, HT[1], &r0, &r1);
  4155. tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
  4156. tmp2 = _mm_aesenc_si128(tmp2, KEY[7]);
  4157. tmp3 = _mm_aesenc_si128(tmp3, KEY[7]);
  4158. tmp4 = _mm_aesenc_si128(tmp4, KEY[7]);
  4159. tmp5 = _mm_aesenc_si128(tmp5, KEY[7]);
  4160. tmp6 = _mm_aesenc_si128(tmp6, KEY[7]);
  4161. tmp7 = _mm_aesenc_si128(tmp7, KEY[7]);
  4162. tmp8 = _mm_aesenc_si128(tmp8, KEY[7]);
  4163. /* 128 x 128 Carryless Multiply */
  4164. XV = _mm_loadu_si128(&((__m128i*)in)[i*8+7]);
  4165. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  4166. gfmul_only(XV, HT[0], &r0, &r1);
  4167. tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
  4168. tmp2 = _mm_aesenc_si128(tmp2, KEY[8]);
  4169. tmp3 = _mm_aesenc_si128(tmp3, KEY[8]);
  4170. tmp4 = _mm_aesenc_si128(tmp4, KEY[8]);
  4171. tmp5 = _mm_aesenc_si128(tmp5, KEY[8]);
  4172. tmp6 = _mm_aesenc_si128(tmp6, KEY[8]);
  4173. tmp7 = _mm_aesenc_si128(tmp7, KEY[8]);
  4174. tmp8 = _mm_aesenc_si128(tmp8, KEY[8]);
  4175. /* Reduction */
  4176. X = ghash_red(r0, r1);
  4177. tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
  4178. tmp2 = _mm_aesenc_si128(tmp2, KEY[9]);
  4179. tmp3 = _mm_aesenc_si128(tmp3, KEY[9]);
  4180. tmp4 = _mm_aesenc_si128(tmp4, KEY[9]);
  4181. tmp5 = _mm_aesenc_si128(tmp5, KEY[9]);
  4182. tmp6 = _mm_aesenc_si128(tmp6, KEY[9]);
  4183. tmp7 = _mm_aesenc_si128(tmp7, KEY[9]);
  4184. tmp8 = _mm_aesenc_si128(tmp8, KEY[9]);
  4185. lastKey = KEY[10];
  4186. if (nr > 10) {
  4187. tmp1 = _mm_aesenc_si128(tmp1, KEY[10]);
  4188. tmp2 = _mm_aesenc_si128(tmp2, KEY[10]);
  4189. tmp3 = _mm_aesenc_si128(tmp3, KEY[10]);
  4190. tmp4 = _mm_aesenc_si128(tmp4, KEY[10]);
  4191. tmp5 = _mm_aesenc_si128(tmp5, KEY[10]);
  4192. tmp6 = _mm_aesenc_si128(tmp6, KEY[10]);
  4193. tmp7 = _mm_aesenc_si128(tmp7, KEY[10]);
  4194. tmp8 = _mm_aesenc_si128(tmp8, KEY[10]);
  4195. tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
  4196. tmp2 = _mm_aesenc_si128(tmp2, KEY[11]);
  4197. tmp3 = _mm_aesenc_si128(tmp3, KEY[11]);
  4198. tmp4 = _mm_aesenc_si128(tmp4, KEY[11]);
  4199. tmp5 = _mm_aesenc_si128(tmp5, KEY[11]);
  4200. tmp6 = _mm_aesenc_si128(tmp6, KEY[11]);
  4201. tmp7 = _mm_aesenc_si128(tmp7, KEY[11]);
  4202. tmp8 = _mm_aesenc_si128(tmp8, KEY[11]);
  4203. lastKey = KEY[12];
  4204. if (nr > 12) {
  4205. tmp1 = _mm_aesenc_si128(tmp1, KEY[12]);
  4206. tmp2 = _mm_aesenc_si128(tmp2, KEY[12]);
  4207. tmp3 = _mm_aesenc_si128(tmp3, KEY[12]);
  4208. tmp4 = _mm_aesenc_si128(tmp4, KEY[12]);
  4209. tmp5 = _mm_aesenc_si128(tmp5, KEY[12]);
  4210. tmp6 = _mm_aesenc_si128(tmp6, KEY[12]);
  4211. tmp7 = _mm_aesenc_si128(tmp7, KEY[12]);
  4212. tmp8 = _mm_aesenc_si128(tmp8, KEY[12]);
  4213. tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
  4214. tmp2 = _mm_aesenc_si128(tmp2, KEY[13]);
  4215. tmp3 = _mm_aesenc_si128(tmp3, KEY[13]);
  4216. tmp4 = _mm_aesenc_si128(tmp4, KEY[13]);
  4217. tmp5 = _mm_aesenc_si128(tmp5, KEY[13]);
  4218. tmp6 = _mm_aesenc_si128(tmp6, KEY[13]);
  4219. tmp7 = _mm_aesenc_si128(tmp7, KEY[13]);
  4220. tmp8 = _mm_aesenc_si128(tmp8, KEY[13]);
  4221. lastKey = KEY[14];
  4222. }
  4223. }
  4224. AES_ENC_LAST_8();
  4225. }
  4226. }
  4227. #endif /* AES_GCM_AESNI_NO_UNROLL */
  4228. for (k = i*8; k < nbytes/16; k++) {
  4229. tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
  4230. ctr1 = _mm_add_epi32(ctr1, ONE);
  4231. tmp1 = _mm_xor_si128(tmp1, KEY[0]);
  4232. tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
  4233. tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
  4234. tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
  4235. tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
  4236. tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
  4237. tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
  4238. tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
  4239. tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
  4240. tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
  4241. /* 128 x 128 Carryless Multiply */
  4242. XV = _mm_loadu_si128(&((__m128i*)in)[k]);
  4243. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  4244. XV = _mm_xor_si128(XV, X);
  4245. X = gfmul_shifted(XV, H);
  4246. lastKey = KEY[10];
  4247. if (nr > 10) {
  4248. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  4249. tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
  4250. lastKey = KEY[12];
  4251. if (nr > 12) {
  4252. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  4253. tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
  4254. lastKey = KEY[14];
  4255. }
  4256. }
  4257. tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
  4258. tmp2 = _mm_loadu_si128(&((__m128i*)in)[k]);
  4259. tmp1 = _mm_xor_si128(tmp1, tmp2);
  4260. _mm_storeu_si128(&((__m128i*)out)[k], tmp1);
  4261. }
  4262. /* If one partial block remains */
  4263. if (nbytes % 16) {
  4264. tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
  4265. tmp1 = _mm_xor_si128(tmp1, KEY[0]);
  4266. tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
  4267. tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
  4268. tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
  4269. tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
  4270. tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
  4271. tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
  4272. tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
  4273. tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
  4274. tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
  4275. lastKey = KEY[10];
  4276. if (nr > 10) {
  4277. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  4278. tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
  4279. lastKey = KEY[12];
  4280. if (nr > 12) {
  4281. tmp1 = _mm_aesenc_si128(tmp1, lastKey);
  4282. tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
  4283. lastKey = KEY[14];
  4284. }
  4285. }
  4286. tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
  4287. last_block = _mm_setzero_si128();
  4288. for (j=0; j < nbytes%16; j++)
  4289. ((unsigned char*)&last_block)[j] = in[k*16+j];
  4290. XV = last_block;
  4291. tmp1 = _mm_xor_si128(tmp1, last_block);
  4292. last_block = tmp1;
  4293. for (j=0; j < nbytes%16; j++)
  4294. out[k*16+j] = ((unsigned char*)&last_block)[j];
  4295. XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
  4296. XV = _mm_xor_si128(XV, X);
  4297. X = gfmul_shifted(XV, H);
  4298. }
  4299. tmp1 = _mm_insert_epi64(tmp1, nbytes*8, 0);
  4300. tmp1 = _mm_insert_epi64(tmp1, abytes*8, 1);
  4301. /* 128 x 128 Carryless Multiply */
  4302. X = _mm_xor_si128(X, tmp1);
  4303. X = gfmul_shifted(X, H);
  4304. X = _mm_shuffle_epi8(X, BSWAP_MASK);
  4305. T = _mm_xor_si128(X, T);
  4306. /* if (0xffff !=
  4307. _mm_movemask_epi8(_mm_cmpeq_epi8(T, _mm_loadu_si128((__m128i*)tag)))) */
  4308. if (XMEMCMP(tag, &T, tbytes) != 0)
  4309. *res = 0; /* in case the authentication failed */
  4310. else
  4311. *res = 1; /* when successful returns 1 */
  4312. }
  4313. #endif /* HAVE_AES_DECRYPT */
  4314. #endif /* _MSC_VER */
  4315. #endif /* WOLFSSL_AESNI */
  4316. #if defined(GCM_SMALL)
  4317. static void GMULT(byte* X, byte* Y)
  4318. {
  4319. byte Z[AES_BLOCK_SIZE];
  4320. byte V[AES_BLOCK_SIZE];
  4321. int i, j;
  4322. XMEMSET(Z, 0, AES_BLOCK_SIZE);
  4323. XMEMCPY(V, X, AES_BLOCK_SIZE);
  4324. for (i = 0; i < AES_BLOCK_SIZE; i++)
  4325. {
  4326. byte y = Y[i];
  4327. for (j = 0; j < 8; j++)
  4328. {
  4329. if (y & 0x80) {
  4330. xorbuf(Z, V, AES_BLOCK_SIZE);
  4331. }
  4332. RIGHTSHIFTX(V);
  4333. y = y << 1;
  4334. }
  4335. }
  4336. XMEMCPY(X, Z, AES_BLOCK_SIZE);
  4337. }
  4338. void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
  4339. word32 cSz, byte* s, word32 sSz)
  4340. {
  4341. byte x[AES_BLOCK_SIZE];
  4342. byte scratch[AES_BLOCK_SIZE];
  4343. word32 blocks, partial;
  4344. byte* h = aes->H;
  4345. XMEMSET(x, 0, AES_BLOCK_SIZE);
  4346. /* Hash in A, the Additional Authentication Data */
  4347. if (aSz != 0 && a != NULL) {
  4348. blocks = aSz / AES_BLOCK_SIZE;
  4349. partial = aSz % AES_BLOCK_SIZE;
  4350. while (blocks--) {
  4351. xorbuf(x, a, AES_BLOCK_SIZE);
  4352. GMULT(x, h);
  4353. a += AES_BLOCK_SIZE;
  4354. }
  4355. if (partial != 0) {
  4356. XMEMSET(scratch, 0, AES_BLOCK_SIZE);
  4357. XMEMCPY(scratch, a, partial);
  4358. xorbuf(x, scratch, AES_BLOCK_SIZE);
  4359. GMULT(x, h);
  4360. }
  4361. }
  4362. /* Hash in C, the Ciphertext */
  4363. if (cSz != 0 && c != NULL) {
  4364. blocks = cSz / AES_BLOCK_SIZE;
  4365. partial = cSz % AES_BLOCK_SIZE;
  4366. while (blocks--) {
  4367. xorbuf(x, c, AES_BLOCK_SIZE);
  4368. GMULT(x, h);
  4369. c += AES_BLOCK_SIZE;
  4370. }
  4371. if (partial != 0) {
  4372. XMEMSET(scratch, 0, AES_BLOCK_SIZE);
  4373. XMEMCPY(scratch, c, partial);
  4374. xorbuf(x, scratch, AES_BLOCK_SIZE);
  4375. GMULT(x, h);
  4376. }
  4377. }
  4378. /* Hash in the lengths of A and C in bits */
  4379. FlattenSzInBits(&scratch[0], aSz);
  4380. FlattenSzInBits(&scratch[8], cSz);
  4381. xorbuf(x, scratch, AES_BLOCK_SIZE);
  4382. GMULT(x, h);
  4383. /* Copy the result into s. */
  4384. XMEMCPY(s, x, sSz);
  4385. }
  4386. /* end GCM_SMALL */
  4387. #elif defined(GCM_TABLE)
  4388. static const byte R[256][2] = {
  4389. {0x00, 0x00}, {0x01, 0xc2}, {0x03, 0x84}, {0x02, 0x46},
  4390. {0x07, 0x08}, {0x06, 0xca}, {0x04, 0x8c}, {0x05, 0x4e},
  4391. {0x0e, 0x10}, {0x0f, 0xd2}, {0x0d, 0x94}, {0x0c, 0x56},
  4392. {0x09, 0x18}, {0x08, 0xda}, {0x0a, 0x9c}, {0x0b, 0x5e},
  4393. {0x1c, 0x20}, {0x1d, 0xe2}, {0x1f, 0xa4}, {0x1e, 0x66},
  4394. {0x1b, 0x28}, {0x1a, 0xea}, {0x18, 0xac}, {0x19, 0x6e},
  4395. {0x12, 0x30}, {0x13, 0xf2}, {0x11, 0xb4}, {0x10, 0x76},
  4396. {0x15, 0x38}, {0x14, 0xfa}, {0x16, 0xbc}, {0x17, 0x7e},
  4397. {0x38, 0x40}, {0x39, 0x82}, {0x3b, 0xc4}, {0x3a, 0x06},
  4398. {0x3f, 0x48}, {0x3e, 0x8a}, {0x3c, 0xcc}, {0x3d, 0x0e},
  4399. {0x36, 0x50}, {0x37, 0x92}, {0x35, 0xd4}, {0x34, 0x16},
  4400. {0x31, 0x58}, {0x30, 0x9a}, {0x32, 0xdc}, {0x33, 0x1e},
  4401. {0x24, 0x60}, {0x25, 0xa2}, {0x27, 0xe4}, {0x26, 0x26},
  4402. {0x23, 0x68}, {0x22, 0xaa}, {0x20, 0xec}, {0x21, 0x2e},
  4403. {0x2a, 0x70}, {0x2b, 0xb2}, {0x29, 0xf4}, {0x28, 0x36},
  4404. {0x2d, 0x78}, {0x2c, 0xba}, {0x2e, 0xfc}, {0x2f, 0x3e},
  4405. {0x70, 0x80}, {0x71, 0x42}, {0x73, 0x04}, {0x72, 0xc6},
  4406. {0x77, 0x88}, {0x76, 0x4a}, {0x74, 0x0c}, {0x75, 0xce},
  4407. {0x7e, 0x90}, {0x7f, 0x52}, {0x7d, 0x14}, {0x7c, 0xd6},
  4408. {0x79, 0x98}, {0x78, 0x5a}, {0x7a, 0x1c}, {0x7b, 0xde},
  4409. {0x6c, 0xa0}, {0x6d, 0x62}, {0x6f, 0x24}, {0x6e, 0xe6},
  4410. {0x6b, 0xa8}, {0x6a, 0x6a}, {0x68, 0x2c}, {0x69, 0xee},
  4411. {0x62, 0xb0}, {0x63, 0x72}, {0x61, 0x34}, {0x60, 0xf6},
  4412. {0x65, 0xb8}, {0x64, 0x7a}, {0x66, 0x3c}, {0x67, 0xfe},
  4413. {0x48, 0xc0}, {0x49, 0x02}, {0x4b, 0x44}, {0x4a, 0x86},
  4414. {0x4f, 0xc8}, {0x4e, 0x0a}, {0x4c, 0x4c}, {0x4d, 0x8e},
  4415. {0x46, 0xd0}, {0x47, 0x12}, {0x45, 0x54}, {0x44, 0x96},
  4416. {0x41, 0xd8}, {0x40, 0x1a}, {0x42, 0x5c}, {0x43, 0x9e},
  4417. {0x54, 0xe0}, {0x55, 0x22}, {0x57, 0x64}, {0x56, 0xa6},
  4418. {0x53, 0xe8}, {0x52, 0x2a}, {0x50, 0x6c}, {0x51, 0xae},
  4419. {0x5a, 0xf0}, {0x5b, 0x32}, {0x59, 0x74}, {0x58, 0xb6},
  4420. {0x5d, 0xf8}, {0x5c, 0x3a}, {0x5e, 0x7c}, {0x5f, 0xbe},
  4421. {0xe1, 0x00}, {0xe0, 0xc2}, {0xe2, 0x84}, {0xe3, 0x46},
  4422. {0xe6, 0x08}, {0xe7, 0xca}, {0xe5, 0x8c}, {0xe4, 0x4e},
  4423. {0xef, 0x10}, {0xee, 0xd2}, {0xec, 0x94}, {0xed, 0x56},
  4424. {0xe8, 0x18}, {0xe9, 0xda}, {0xeb, 0x9c}, {0xea, 0x5e},
  4425. {0xfd, 0x20}, {0xfc, 0xe2}, {0xfe, 0xa4}, {0xff, 0x66},
  4426. {0xfa, 0x28}, {0xfb, 0xea}, {0xf9, 0xac}, {0xf8, 0x6e},
  4427. {0xf3, 0x30}, {0xf2, 0xf2}, {0xf0, 0xb4}, {0xf1, 0x76},
  4428. {0xf4, 0x38}, {0xf5, 0xfa}, {0xf7, 0xbc}, {0xf6, 0x7e},
  4429. {0xd9, 0x40}, {0xd8, 0x82}, {0xda, 0xc4}, {0xdb, 0x06},
  4430. {0xde, 0x48}, {0xdf, 0x8a}, {0xdd, 0xcc}, {0xdc, 0x0e},
  4431. {0xd7, 0x50}, {0xd6, 0x92}, {0xd4, 0xd4}, {0xd5, 0x16},
  4432. {0xd0, 0x58}, {0xd1, 0x9a}, {0xd3, 0xdc}, {0xd2, 0x1e},
  4433. {0xc5, 0x60}, {0xc4, 0xa2}, {0xc6, 0xe4}, {0xc7, 0x26},
  4434. {0xc2, 0x68}, {0xc3, 0xaa}, {0xc1, 0xec}, {0xc0, 0x2e},
  4435. {0xcb, 0x70}, {0xca, 0xb2}, {0xc8, 0xf4}, {0xc9, 0x36},
  4436. {0xcc, 0x78}, {0xcd, 0xba}, {0xcf, 0xfc}, {0xce, 0x3e},
  4437. {0x91, 0x80}, {0x90, 0x42}, {0x92, 0x04}, {0x93, 0xc6},
  4438. {0x96, 0x88}, {0x97, 0x4a}, {0x95, 0x0c}, {0x94, 0xce},
  4439. {0x9f, 0x90}, {0x9e, 0x52}, {0x9c, 0x14}, {0x9d, 0xd6},
  4440. {0x98, 0x98}, {0x99, 0x5a}, {0x9b, 0x1c}, {0x9a, 0xde},
  4441. {0x8d, 0xa0}, {0x8c, 0x62}, {0x8e, 0x24}, {0x8f, 0xe6},
  4442. {0x8a, 0xa8}, {0x8b, 0x6a}, {0x89, 0x2c}, {0x88, 0xee},
  4443. {0x83, 0xb0}, {0x82, 0x72}, {0x80, 0x34}, {0x81, 0xf6},
  4444. {0x84, 0xb8}, {0x85, 0x7a}, {0x87, 0x3c}, {0x86, 0xfe},
  4445. {0xa9, 0xc0}, {0xa8, 0x02}, {0xaa, 0x44}, {0xab, 0x86},
  4446. {0xae, 0xc8}, {0xaf, 0x0a}, {0xad, 0x4c}, {0xac, 0x8e},
  4447. {0xa7, 0xd0}, {0xa6, 0x12}, {0xa4, 0x54}, {0xa5, 0x96},
  4448. {0xa0, 0xd8}, {0xa1, 0x1a}, {0xa3, 0x5c}, {0xa2, 0x9e},
  4449. {0xb5, 0xe0}, {0xb4, 0x22}, {0xb6, 0x64}, {0xb7, 0xa6},
  4450. {0xb2, 0xe8}, {0xb3, 0x2a}, {0xb1, 0x6c}, {0xb0, 0xae},
  4451. {0xbb, 0xf0}, {0xba, 0x32}, {0xb8, 0x74}, {0xb9, 0xb6},
  4452. {0xbc, 0xf8}, {0xbd, 0x3a}, {0xbf, 0x7c}, {0xbe, 0xbe} };
  4453. static void GMULT(byte *x, byte m[256][AES_BLOCK_SIZE])
  4454. {
  4455. int i, j;
  4456. byte Z[AES_BLOCK_SIZE];
  4457. byte a;
  4458. XMEMSET(Z, 0, sizeof(Z));
  4459. for (i = 15; i > 0; i--) {
  4460. xorbuf(Z, m[x[i]], AES_BLOCK_SIZE);
  4461. a = Z[15];
  4462. for (j = 15; j > 0; j--) {
  4463. Z[j] = Z[j-1];
  4464. }
  4465. Z[0] = R[a][0];
  4466. Z[1] ^= R[a][1];
  4467. }
  4468. xorbuf(Z, m[x[0]], AES_BLOCK_SIZE);
  4469. XMEMCPY(x, Z, AES_BLOCK_SIZE);
  4470. }
  4471. void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
  4472. word32 cSz, byte* s, word32 sSz)
  4473. {
  4474. byte x[AES_BLOCK_SIZE];
  4475. byte scratch[AES_BLOCK_SIZE];
  4476. word32 blocks, partial;
  4477. XMEMSET(x, 0, AES_BLOCK_SIZE);
  4478. /* Hash in A, the Additional Authentication Data */
  4479. if (aSz != 0 && a != NULL) {
  4480. blocks = aSz / AES_BLOCK_SIZE;
  4481. partial = aSz % AES_BLOCK_SIZE;
  4482. while (blocks--) {
  4483. xorbuf(x, a, AES_BLOCK_SIZE);
  4484. GMULT(x, aes->M0);
  4485. a += AES_BLOCK_SIZE;
  4486. }
  4487. if (partial != 0) {
  4488. XMEMSET(scratch, 0, AES_BLOCK_SIZE);
  4489. XMEMCPY(scratch, a, partial);
  4490. xorbuf(x, scratch, AES_BLOCK_SIZE);
  4491. GMULT(x, aes->M0);
  4492. }
  4493. }
  4494. /* Hash in C, the Ciphertext */
  4495. if (cSz != 0 && c != NULL) {
  4496. blocks = cSz / AES_BLOCK_SIZE;
  4497. partial = cSz % AES_BLOCK_SIZE;
  4498. while (blocks--) {
  4499. xorbuf(x, c, AES_BLOCK_SIZE);
  4500. GMULT(x, aes->M0);
  4501. c += AES_BLOCK_SIZE;
  4502. }
  4503. if (partial != 0) {
  4504. XMEMSET(scratch, 0, AES_BLOCK_SIZE);
  4505. XMEMCPY(scratch, c, partial);
  4506. xorbuf(x, scratch, AES_BLOCK_SIZE);
  4507. GMULT(x, aes->M0);
  4508. }
  4509. }
  4510. /* Hash in the lengths of A and C in bits */
  4511. FlattenSzInBits(&scratch[0], aSz);
  4512. FlattenSzInBits(&scratch[8], cSz);
  4513. xorbuf(x, scratch, AES_BLOCK_SIZE);
  4514. GMULT(x, aes->M0);
  4515. /* Copy the result into s. */
  4516. XMEMCPY(s, x, sSz);
  4517. }
  4518. /* end GCM_TABLE */
  4519. #elif defined(WORD64_AVAILABLE) && !defined(GCM_WORD32)
  4520. #if !defined(FREESCALE_LTC_AES_GCM)
  4521. static void GMULT(word64* X, word64* Y)
  4522. {
  4523. word64 Z[2] = {0,0};
  4524. word64 V[2];
  4525. int i, j;
  4526. V[0] = X[0]; V[1] = X[1];
  4527. for (i = 0; i < 2; i++)
  4528. {
  4529. word64 y = Y[i];
  4530. for (j = 0; j < 64; j++)
  4531. {
  4532. if (y & 0x8000000000000000ULL) {
  4533. Z[0] ^= V[0];
  4534. Z[1] ^= V[1];
  4535. }
  4536. if (V[1] & 0x0000000000000001) {
  4537. V[1] >>= 1;
  4538. V[1] |= ((V[0] & 0x0000000000000001) ?
  4539. 0x8000000000000000ULL : 0);
  4540. V[0] >>= 1;
  4541. V[0] ^= 0xE100000000000000ULL;
  4542. }
  4543. else {
  4544. V[1] >>= 1;
  4545. V[1] |= ((V[0] & 0x0000000000000001) ?
  4546. 0x8000000000000000ULL : 0);
  4547. V[0] >>= 1;
  4548. }
  4549. y <<= 1;
  4550. }
  4551. }
  4552. X[0] = Z[0];
  4553. X[1] = Z[1];
  4554. }
  4555. void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
  4556. word32 cSz, byte* s, word32 sSz)
  4557. {
  4558. word64 x[2] = {0,0};
  4559. word32 blocks, partial;
  4560. word64 bigH[2];
  4561. XMEMCPY(bigH, aes->H, AES_BLOCK_SIZE);
  4562. #ifdef LITTLE_ENDIAN_ORDER
  4563. ByteReverseWords64(bigH, bigH, AES_BLOCK_SIZE);
  4564. #endif
  4565. /* Hash in A, the Additional Authentication Data */
  4566. if (aSz != 0 && a != NULL) {
  4567. word64 bigA[2];
  4568. blocks = aSz / AES_BLOCK_SIZE;
  4569. partial = aSz % AES_BLOCK_SIZE;
  4570. while (blocks--) {
  4571. XMEMCPY(bigA, a, AES_BLOCK_SIZE);
  4572. #ifdef LITTLE_ENDIAN_ORDER
  4573. ByteReverseWords64(bigA, bigA, AES_BLOCK_SIZE);
  4574. #endif
  4575. x[0] ^= bigA[0];
  4576. x[1] ^= bigA[1];
  4577. GMULT(x, bigH);
  4578. a += AES_BLOCK_SIZE;
  4579. }
  4580. if (partial != 0) {
  4581. XMEMSET(bigA, 0, AES_BLOCK_SIZE);
  4582. XMEMCPY(bigA, a, partial);
  4583. #ifdef LITTLE_ENDIAN_ORDER
  4584. ByteReverseWords64(bigA, bigA, AES_BLOCK_SIZE);
  4585. #endif
  4586. x[0] ^= bigA[0];
  4587. x[1] ^= bigA[1];
  4588. GMULT(x, bigH);
  4589. }
  4590. #ifdef OPENSSL_EXTRA
  4591. /* store AAD partial tag for next call */
  4592. aes->aadH[0] = (word32)((x[0] & 0xFFFFFFFF00000000) >> 32);
  4593. aes->aadH[1] = (word32)(x[0] & 0xFFFFFFFF);
  4594. aes->aadH[2] = (word32)((x[1] & 0xFFFFFFFF00000000) >> 32);
  4595. aes->aadH[3] = (word32)(x[1] & 0xFFFFFFFF);
  4596. #endif
  4597. }
  4598. /* Hash in C, the Ciphertext */
  4599. if (cSz != 0 && c != NULL) {
  4600. word64 bigC[2];
  4601. blocks = cSz / AES_BLOCK_SIZE;
  4602. partial = cSz % AES_BLOCK_SIZE;
  4603. #ifdef OPENSSL_EXTRA
  4604. /* Start from last AAD partial tag */
  4605. if(aes->aadLen) {
  4606. x[0] = ((word64)aes->aadH[0]) << 32 | aes->aadH[1];
  4607. x[1] = ((word64)aes->aadH[2]) << 32 | aes->aadH[3];
  4608. }
  4609. #endif
  4610. while (blocks--) {
  4611. XMEMCPY(bigC, c, AES_BLOCK_SIZE);
  4612. #ifdef LITTLE_ENDIAN_ORDER
  4613. ByteReverseWords64(bigC, bigC, AES_BLOCK_SIZE);
  4614. #endif
  4615. x[0] ^= bigC[0];
  4616. x[1] ^= bigC[1];
  4617. GMULT(x, bigH);
  4618. c += AES_BLOCK_SIZE;
  4619. }
  4620. if (partial != 0) {
  4621. XMEMSET(bigC, 0, AES_BLOCK_SIZE);
  4622. XMEMCPY(bigC, c, partial);
  4623. #ifdef LITTLE_ENDIAN_ORDER
  4624. ByteReverseWords64(bigC, bigC, AES_BLOCK_SIZE);
  4625. #endif
  4626. x[0] ^= bigC[0];
  4627. x[1] ^= bigC[1];
  4628. GMULT(x, bigH);
  4629. }
  4630. }
  4631. /* Hash in the lengths in bits of A and C */
  4632. {
  4633. word64 len[2];
  4634. len[0] = aSz; len[1] = cSz;
  4635. #ifdef OPENSSL_EXTRA
  4636. if (aes->aadLen)
  4637. len[0] = (word64)aes->aadLen;
  4638. #endif
  4639. /* Lengths are in bytes. Convert to bits. */
  4640. len[0] *= 8;
  4641. len[1] *= 8;
  4642. x[0] ^= len[0];
  4643. x[1] ^= len[1];
  4644. GMULT(x, bigH);
  4645. }
  4646. #ifdef LITTLE_ENDIAN_ORDER
  4647. ByteReverseWords64(x, x, AES_BLOCK_SIZE);
  4648. #endif
  4649. XMEMCPY(s, x, sSz);
  4650. }
  4651. #endif /* !FREESCALE_LTC_AES_GCM */
  4652. /* end defined(WORD64_AVAILABLE) && !defined(GCM_WORD32) */
  4653. #else /* GCM_WORD32 */
  4654. static void GMULT(word32* X, word32* Y)
  4655. {
  4656. word32 Z[4] = {0,0,0,0};
  4657. word32 V[4];
  4658. int i, j;
  4659. V[0] = X[0]; V[1] = X[1]; V[2] = X[2]; V[3] = X[3];
  4660. for (i = 0; i < 4; i++)
  4661. {
  4662. word32 y = Y[i];
  4663. for (j = 0; j < 32; j++)
  4664. {
  4665. if (y & 0x80000000) {
  4666. Z[0] ^= V[0];
  4667. Z[1] ^= V[1];
  4668. Z[2] ^= V[2];
  4669. Z[3] ^= V[3];
  4670. }
  4671. if (V[3] & 0x00000001) {
  4672. V[3] >>= 1;
  4673. V[3] |= ((V[2] & 0x00000001) ? 0x80000000 : 0);
  4674. V[2] >>= 1;
  4675. V[2] |= ((V[1] & 0x00000001) ? 0x80000000 : 0);
  4676. V[1] >>= 1;
  4677. V[1] |= ((V[0] & 0x00000001) ? 0x80000000 : 0);
  4678. V[0] >>= 1;
  4679. V[0] ^= 0xE1000000;
  4680. } else {
  4681. V[3] >>= 1;
  4682. V[3] |= ((V[2] & 0x00000001) ? 0x80000000 : 0);
  4683. V[2] >>= 1;
  4684. V[2] |= ((V[1] & 0x00000001) ? 0x80000000 : 0);
  4685. V[1] >>= 1;
  4686. V[1] |= ((V[0] & 0x00000001) ? 0x80000000 : 0);
  4687. V[0] >>= 1;
  4688. }
  4689. y <<= 1;
  4690. }
  4691. }
  4692. X[0] = Z[0];
  4693. X[1] = Z[1];
  4694. X[2] = Z[2];
  4695. X[3] = Z[3];
  4696. }
  4697. void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
  4698. word32 cSz, byte* s, word32 sSz)
  4699. {
  4700. word32 x[4] = {0,0,0,0};
  4701. word32 blocks, partial;
  4702. word32 bigH[4];
  4703. XMEMCPY(bigH, aes->H, AES_BLOCK_SIZE);
  4704. #ifdef LITTLE_ENDIAN_ORDER
  4705. ByteReverseWords(bigH, bigH, AES_BLOCK_SIZE);
  4706. #endif
  4707. /* Hash in A, the Additional Authentication Data */
  4708. if (aSz != 0 && a != NULL) {
  4709. word32 bigA[4];
  4710. blocks = aSz / AES_BLOCK_SIZE;
  4711. partial = aSz % AES_BLOCK_SIZE;
  4712. while (blocks--) {
  4713. XMEMCPY(bigA, a, AES_BLOCK_SIZE);
  4714. #ifdef LITTLE_ENDIAN_ORDER
  4715. ByteReverseWords(bigA, bigA, AES_BLOCK_SIZE);
  4716. #endif
  4717. x[0] ^= bigA[0];
  4718. x[1] ^= bigA[1];
  4719. x[2] ^= bigA[2];
  4720. x[3] ^= bigA[3];
  4721. GMULT(x, bigH);
  4722. a += AES_BLOCK_SIZE;
  4723. }
  4724. if (partial != 0) {
  4725. XMEMSET(bigA, 0, AES_BLOCK_SIZE);
  4726. XMEMCPY(bigA, a, partial);
  4727. #ifdef LITTLE_ENDIAN_ORDER
  4728. ByteReverseWords(bigA, bigA, AES_BLOCK_SIZE);
  4729. #endif
  4730. x[0] ^= bigA[0];
  4731. x[1] ^= bigA[1];
  4732. x[2] ^= bigA[2];
  4733. x[3] ^= bigA[3];
  4734. GMULT(x, bigH);
  4735. }
  4736. }
  4737. /* Hash in C, the Ciphertext */
  4738. if (cSz != 0 && c != NULL) {
  4739. word32 bigC[4];
  4740. blocks = cSz / AES_BLOCK_SIZE;
  4741. partial = cSz % AES_BLOCK_SIZE;
  4742. while (blocks--) {
  4743. XMEMCPY(bigC, c, AES_BLOCK_SIZE);
  4744. #ifdef LITTLE_ENDIAN_ORDER
  4745. ByteReverseWords(bigC, bigC, AES_BLOCK_SIZE);
  4746. #endif
  4747. x[0] ^= bigC[0];
  4748. x[1] ^= bigC[1];
  4749. x[2] ^= bigC[2];
  4750. x[3] ^= bigC[3];
  4751. GMULT(x, bigH);
  4752. c += AES_BLOCK_SIZE;
  4753. }
  4754. if (partial != 0) {
  4755. XMEMSET(bigC, 0, AES_BLOCK_SIZE);
  4756. XMEMCPY(bigC, c, partial);
  4757. #ifdef LITTLE_ENDIAN_ORDER
  4758. ByteReverseWords(bigC, bigC, AES_BLOCK_SIZE);
  4759. #endif
  4760. x[0] ^= bigC[0];
  4761. x[1] ^= bigC[1];
  4762. x[2] ^= bigC[2];
  4763. x[3] ^= bigC[3];
  4764. GMULT(x, bigH);
  4765. }
  4766. }
  4767. /* Hash in the lengths in bits of A and C */
  4768. {
  4769. word32 len[4];
  4770. /* Lengths are in bytes. Convert to bits. */
  4771. len[0] = (aSz >> (8*sizeof(aSz) - 3));
  4772. len[1] = aSz << 3;
  4773. len[2] = (cSz >> (8*sizeof(cSz) - 3));
  4774. len[3] = cSz << 3;
  4775. x[0] ^= len[0];
  4776. x[1] ^= len[1];
  4777. x[2] ^= len[2];
  4778. x[3] ^= len[3];
  4779. GMULT(x, bigH);
  4780. }
  4781. #ifdef LITTLE_ENDIAN_ORDER
  4782. ByteReverseWords(x, x, AES_BLOCK_SIZE);
  4783. #endif
  4784. XMEMCPY(s, x, sSz);
  4785. }
  4786. #endif /* end GCM_WORD32 */
  4787. #if !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES)
  4788. #ifdef FREESCALE_LTC_AES_GCM
  4789. int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
  4790. const byte* iv, word32 ivSz,
  4791. byte* authTag, word32 authTagSz,
  4792. const byte* authIn, word32 authInSz)
  4793. {
  4794. status_t status;
  4795. word32 keySize;
  4796. /* argument checks */
  4797. if (aes == NULL || authTagSz > AES_BLOCK_SIZE) {
  4798. return BAD_FUNC_ARG;
  4799. }
  4800. if (authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ) {
  4801. WOLFSSL_MSG("GcmEncrypt authTagSz too small error");
  4802. return BAD_FUNC_ARG;
  4803. }
  4804. status = wc_AesGetKeySize(aes, &keySize);
  4805. if (status)
  4806. return status;
  4807. status = LTC_AES_EncryptTagGcm(LTC_BASE, in, out, sz, iv, ivSz,
  4808. authIn, authInSz, (byte*)aes->key, keySize, authTag, authTagSz);
  4809. return (status == kStatus_Success) ? 0 : AES_GCM_AUTH_E;
  4810. }
  4811. #else
  4812. #ifdef STM32_CRYPTO_AES_GCM
  4813. /* this function supports inline encrypt */
  4814. static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz,
  4815. const byte* iv, word32 ivSz,
  4816. byte* authTag, word32 authTagSz,
  4817. const byte* authIn, word32 authInSz)
  4818. {
  4819. int ret;
  4820. #ifdef WOLFSSL_STM32_CUBEMX
  4821. CRYP_HandleTypeDef hcryp;
  4822. #else
  4823. word32 keyCopy[AES_256_KEY_SIZE/sizeof(word32)];
  4824. #endif
  4825. word32 keySize;
  4826. int status = HAL_OK;
  4827. word32 blocks = sz / AES_BLOCK_SIZE;
  4828. word32 partial = sz % AES_BLOCK_SIZE;
  4829. byte tag[AES_BLOCK_SIZE];
  4830. byte partialBlock[AES_BLOCK_SIZE];
  4831. byte ctr[AES_BLOCK_SIZE];
  4832. byte* authInPadded = NULL;
  4833. int authPadSz;
  4834. ret = wc_AesGetKeySize(aes, &keySize);
  4835. if (ret != 0)
  4836. return ret;
  4837. #ifdef WOLFSSL_STM32_CUBEMX
  4838. ret = wc_Stm32_Aes_Init(aes, &hcryp);
  4839. if (ret != 0)
  4840. return ret;
  4841. #endif
  4842. ret = wolfSSL_CryptHwMutexLock();
  4843. if (ret != 0) {
  4844. return ret;
  4845. }
  4846. XMEMSET(ctr, 0, AES_BLOCK_SIZE);
  4847. if (ivSz == GCM_NONCE_MID_SZ) {
  4848. XMEMCPY(ctr, iv, ivSz);
  4849. ctr[AES_BLOCK_SIZE - 1] = 1;
  4850. }
  4851. else {
  4852. GHASH(aes, NULL, 0, iv, ivSz, ctr, AES_BLOCK_SIZE);
  4853. }
  4854. /* Hardware requires counter + 1 */
  4855. IncrementGcmCounter(ctr);
  4856. if (authInSz == 0 || (authInSz % AES_BLOCK_SIZE) != 0) {
  4857. /* Need to pad the AAD to a full block with zeros. */
  4858. authPadSz = ((authInSz / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
  4859. authInPadded = (byte*)XMALLOC(authPadSz, aes->heap,
  4860. DYNAMIC_TYPE_TMP_BUFFER);
  4861. if (authInPadded == NULL) {
  4862. wolfSSL_CryptHwMutexUnLock();
  4863. return MEMORY_E;
  4864. }
  4865. XMEMSET(authInPadded, 0, authPadSz);
  4866. XMEMCPY(authInPadded, authIn, authInSz);
  4867. } else {
  4868. authPadSz = authInSz;
  4869. authInPadded = (byte*)authIn;
  4870. }
  4871. #ifdef WOLFSSL_STM32_CUBEMX
  4872. hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ctr;
  4873. hcryp.Init.Header = (STM_CRYPT_TYPE*)authInPadded;
  4874. hcryp.Init.HeaderSize = authInSz;
  4875. #ifdef STM32_CRYPTO_AES_ONLY
  4876. /* Set the CRYP parameters */
  4877. hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_GCM_GMAC;
  4878. hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  4879. hcryp.Init.GCMCMACPhase = CRYP_INIT_PHASE;
  4880. HAL_CRYP_Init(&hcryp);
  4881. /* GCM init phase */
  4882. status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, 0, NULL, STM32_HAL_TIMEOUT);
  4883. if (status == HAL_OK) {
  4884. /* GCM header phase */
  4885. hcryp.Init.GCMCMACPhase = CRYP_HEADER_PHASE;
  4886. status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, 0, NULL, STM32_HAL_TIMEOUT);
  4887. }
  4888. if (status == HAL_OK) {
  4889. /* GCM payload phase - blocks */
  4890. hcryp.Init.GCMCMACPhase = CRYP_PAYLOAD_PHASE;
  4891. if (blocks) {
  4892. status = HAL_CRYPEx_AES_Auth(&hcryp, (byte*)in,
  4893. (blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT);
  4894. }
  4895. }
  4896. if (status == HAL_OK && (partial != 0 || blocks == 0)) {
  4897. /* GCM payload phase - partial remainder */
  4898. XMEMSET(partialBlock, 0, sizeof(partialBlock));
  4899. XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
  4900. status = HAL_CRYPEx_AES_Auth(&hcryp, partialBlock, partial,
  4901. partialBlock, STM32_HAL_TIMEOUT);
  4902. XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
  4903. }
  4904. if (status == HAL_OK) {
  4905. /* GCM final phase */
  4906. hcryp.Init.GCMCMACPhase = CRYP_FINAL_PHASE;
  4907. status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, tag, STM32_HAL_TIMEOUT);
  4908. }
  4909. #elif defined(STM32_HAL_V2)
  4910. hcryp.Init.Algorithm = CRYP_AES_GCM;
  4911. ByteReverseWords((word32*)partialBlock, (word32*)ctr, AES_BLOCK_SIZE);
  4912. hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock;
  4913. HAL_CRYP_Init(&hcryp);
  4914. /* GCM payload phase - can handle partial blocks */
  4915. status = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in,
  4916. (blocks * AES_BLOCK_SIZE) + partial, (uint32_t*)out, STM32_HAL_TIMEOUT);
  4917. if (status == HAL_OK) {
  4918. /* Compute the authTag */
  4919. status = HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp, (uint32_t*)tag,
  4920. STM32_HAL_TIMEOUT);
  4921. }
  4922. #else
  4923. HAL_CRYP_Init(&hcryp);
  4924. if (blocks) {
  4925. /* GCM payload phase - blocks */
  4926. status = HAL_CRYPEx_AESGCM_Encrypt(&hcryp, (byte*)in,
  4927. (blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT);
  4928. }
  4929. if (status == HAL_OK && (partial != 0 || blocks == 0)) {
  4930. /* GCM payload phase - partial remainder */
  4931. XMEMSET(partialBlock, 0, sizeof(partialBlock));
  4932. XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
  4933. status = HAL_CRYPEx_AESGCM_Encrypt(&hcryp, partialBlock, partial,
  4934. partialBlock, STM32_HAL_TIMEOUT);
  4935. XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
  4936. }
  4937. if (status == HAL_OK) {
  4938. /* Compute the authTag */
  4939. status = HAL_CRYPEx_AESGCM_Finish(&hcryp, sz, tag, STM32_HAL_TIMEOUT);
  4940. }
  4941. #endif
  4942. if (status != HAL_OK)
  4943. ret = AES_GCM_AUTH_E;
  4944. HAL_CRYP_DeInit(&hcryp);
  4945. #else /* STD_PERI_LIB */
  4946. ByteReverseWords(keyCopy, (word32*)aes->key, keySize);
  4947. status = CRYP_AES_GCM(MODE_ENCRYPT, (uint8_t*)ctr,
  4948. (uint8_t*)keyCopy, keySize * 8,
  4949. (uint8_t*)in, sz,
  4950. (uint8_t*)authInPadded, authInSz,
  4951. (uint8_t*)out, tag);
  4952. if (status != SUCCESS)
  4953. ret = AES_GCM_AUTH_E;
  4954. #endif /* WOLFSSL_STM32_CUBEMX */
  4955. if (ret == 0) {
  4956. /* return authTag */
  4957. if (authTag) {
  4958. /* STM32 GCM won't compute Auth correctly for partial or
  4959. when IV != 12, so use software here */
  4960. if (sz == 0 || partial != 0 || ivSz != GCM_NONCE_MID_SZ) {
  4961. DecrementGcmCounter(ctr); /* hardware requires +1, so subtract it */
  4962. GHASH(aes, authIn, authInSz, out, sz, authTag, authTagSz);
  4963. wc_AesEncrypt(aes, ctr, tag);
  4964. xorbuf(authTag, tag, authTagSz);
  4965. }
  4966. else {
  4967. XMEMCPY(authTag, tag, authTagSz);
  4968. }
  4969. }
  4970. }
  4971. /* Free memory if not a multiple of AES_BLOCK_SZ */
  4972. if (authInPadded != authIn) {
  4973. XFREE(authInPadded, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
  4974. }
  4975. wolfSSL_CryptHwMutexUnLock();
  4976. return ret;
  4977. }
  4978. #endif /* STM32_CRYPTO_AES_GCM */
  4979. #ifdef WOLFSSL_AESNI
  4980. int AES_GCM_encrypt_C(Aes* aes, byte* out, const byte* in, word32 sz,
  4981. const byte* iv, word32 ivSz,
  4982. byte* authTag, word32 authTagSz,
  4983. const byte* authIn, word32 authInSz);
  4984. #else
  4985. static
  4986. #endif
  4987. int AES_GCM_encrypt_C(Aes* aes, byte* out, const byte* in, word32 sz,
  4988. const byte* iv, word32 ivSz,
  4989. byte* authTag, word32 authTagSz,
  4990. const byte* authIn, word32 authInSz)
  4991. {
  4992. int ret = 0;
  4993. word32 blocks = sz / AES_BLOCK_SIZE;
  4994. word32 partial = sz % AES_BLOCK_SIZE;
  4995. const byte* p = in;
  4996. byte* c = out;
  4997. byte counter[AES_BLOCK_SIZE];
  4998. byte initialCounter[AES_BLOCK_SIZE];
  4999. byte *ctr;
  5000. byte scratch[AES_BLOCK_SIZE];
  5001. #ifdef OPENSSL_EXTRA
  5002. word32 aadTemp;
  5003. #endif
  5004. ctr = counter;
  5005. XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
  5006. XMEMSET(scratch, 0, AES_BLOCK_SIZE);
  5007. if (ivSz == GCM_NONCE_MID_SZ) {
  5008. XMEMCPY(initialCounter, iv, ivSz);
  5009. initialCounter[AES_BLOCK_SIZE - 1] = 1;
  5010. }
  5011. else {
  5012. #ifdef OPENSSL_EXTRA
  5013. aadTemp = aes->aadLen;
  5014. aes->aadLen = 0;
  5015. #endif
  5016. GHASH(aes, NULL, 0, iv, ivSz, initialCounter, AES_BLOCK_SIZE);
  5017. #ifdef OPENSSL_EXTRA
  5018. aes->aadLen = aadTemp;
  5019. #endif
  5020. }
  5021. XMEMCPY(ctr, initialCounter, AES_BLOCK_SIZE);
  5022. #ifdef WOLFSSL_PIC32MZ_CRYPT
  5023. if (blocks) {
  5024. /* use initial IV for HW, but don't use it below */
  5025. XMEMCPY(aes->reg, ctr, AES_BLOCK_SIZE);
  5026. ret = wc_Pic32AesCrypt(
  5027. aes->key, aes->keylen, aes->reg, AES_BLOCK_SIZE,
  5028. out, in, (blocks * AES_BLOCK_SIZE),
  5029. PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_AES_GCM);
  5030. if (ret != 0)
  5031. return ret;
  5032. }
  5033. /* process remainder using partial handling */
  5034. #endif
  5035. #if defined(HAVE_AES_ECB) && !defined(WOLFSSL_PIC32MZ_CRYPT)
  5036. /* some hardware acceleration can gain performance from doing AES encryption
  5037. * of the whole buffer at once */
  5038. if (c != p) { /* can not handle inline encryption */
  5039. while (blocks--) {
  5040. IncrementGcmCounter(ctr);
  5041. XMEMCPY(c, ctr, AES_BLOCK_SIZE);
  5042. c += AES_BLOCK_SIZE;
  5043. }
  5044. /* reset number of blocks and then do encryption */
  5045. blocks = sz / AES_BLOCK_SIZE;
  5046. wc_AesEcbEncrypt(aes, out, out, AES_BLOCK_SIZE * blocks);
  5047. xorbuf(out, p, AES_BLOCK_SIZE * blocks);
  5048. p += AES_BLOCK_SIZE * blocks;
  5049. }
  5050. else
  5051. #endif /* HAVE_AES_ECB && !WOLFSSL_PIC32MZ_CRYPT */
  5052. while (blocks--) {
  5053. IncrementGcmCounter(ctr);
  5054. #if !defined(WOLFSSL_PIC32MZ_CRYPT)
  5055. wc_AesEncrypt(aes, ctr, scratch);
  5056. xorbuf(scratch, p, AES_BLOCK_SIZE);
  5057. XMEMCPY(c, scratch, AES_BLOCK_SIZE);
  5058. #endif
  5059. p += AES_BLOCK_SIZE;
  5060. c += AES_BLOCK_SIZE;
  5061. }
  5062. if (partial != 0) {
  5063. IncrementGcmCounter(ctr);
  5064. wc_AesEncrypt(aes, ctr, scratch);
  5065. xorbuf(scratch, p, partial);
  5066. XMEMCPY(c, scratch, partial);
  5067. }
  5068. if (authTag) {
  5069. GHASH(aes, authIn, authInSz, out, sz, authTag, authTagSz);
  5070. wc_AesEncrypt(aes, initialCounter, scratch);
  5071. xorbuf(authTag, scratch, authTagSz);
  5072. #ifdef OPENSSL_EXTRA
  5073. if (!in && !sz)
  5074. /* store AAD size for next call */
  5075. aes->aadLen = authInSz;
  5076. #endif
  5077. }
  5078. return ret;
  5079. }
  5080. /* Software AES - GCM Encrypt */
  5081. int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
  5082. const byte* iv, word32 ivSz,
  5083. byte* authTag, word32 authTagSz,
  5084. const byte* authIn, word32 authInSz)
  5085. {
  5086. /* argument checks */
  5087. if (aes == NULL || authTagSz > AES_BLOCK_SIZE) {
  5088. return BAD_FUNC_ARG;
  5089. }
  5090. if (authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ) {
  5091. WOLFSSL_MSG("GcmEncrypt authTagSz too small error");
  5092. return BAD_FUNC_ARG;
  5093. }
  5094. #ifdef WOLF_CRYPTO_CB
  5095. if (aes->devId != INVALID_DEVID) {
  5096. int ret = wc_CryptoCb_AesGcmEncrypt(aes, out, in, sz, iv, ivSz,
  5097. authTag, authTagSz, authIn, authInSz);
  5098. if (ret != CRYPTOCB_UNAVAILABLE)
  5099. return ret;
  5100. /* fall-through when unavailable */
  5101. }
  5102. #endif
  5103. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
  5104. /* if async and byte count above threshold */
  5105. /* only 12-byte IV is supported in HW */
  5106. if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
  5107. sz >= WC_ASYNC_THRESH_AES_GCM && ivSz == GCM_NONCE_MID_SZ) {
  5108. #if defined(HAVE_CAVIUM)
  5109. #ifdef HAVE_CAVIUM_V
  5110. if (authInSz == 20) { /* Nitrox V GCM is only working with 20 byte AAD */
  5111. return NitroxAesGcmEncrypt(aes, out, in, sz,
  5112. (const byte*)aes->devKey, aes->keylen, iv, ivSz,
  5113. authTag, authTagSz, authIn, authInSz);
  5114. }
  5115. #endif
  5116. #elif defined(HAVE_INTEL_QA)
  5117. return IntelQaSymAesGcmEncrypt(&aes->asyncDev, out, in, sz,
  5118. (const byte*)aes->devKey, aes->keylen, iv, ivSz,
  5119. authTag, authTagSz, authIn, authInSz);
  5120. #else /* WOLFSSL_ASYNC_CRYPT_TEST */
  5121. if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_GCM_ENCRYPT)) {
  5122. WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
  5123. testDev->aes.aes = aes;
  5124. testDev->aes.out = out;
  5125. testDev->aes.in = in;
  5126. testDev->aes.sz = sz;
  5127. testDev->aes.iv = iv;
  5128. testDev->aes.ivSz = ivSz;
  5129. testDev->aes.authTag = authTag;
  5130. testDev->aes.authTagSz = authTagSz;
  5131. testDev->aes.authIn = authIn;
  5132. testDev->aes.authInSz = authInSz;
  5133. return WC_PENDING_E;
  5134. }
  5135. #endif
  5136. }
  5137. #endif /* WOLFSSL_ASYNC_CRYPT */
  5138. #ifdef STM32_CRYPTO_AES_GCM
  5139. /* The STM standard peripheral library API's doesn't support partial blocks */
  5140. #ifdef STD_PERI_LIB
  5141. if (partial == 0)
  5142. #endif
  5143. {
  5144. return wc_AesGcmEncrypt_STM32(
  5145. aes, out, in, sz, iv, ivSz,
  5146. authTag, authTagSz, authIn, authInSz);
  5147. }
  5148. #endif /* STM32_CRYPTO_AES_GCM */
  5149. #ifdef WOLFSSL_AESNI
  5150. #ifdef HAVE_INTEL_AVX2
  5151. if (IS_INTEL_AVX2(intel_flags)) {
  5152. AES_GCM_encrypt_avx2(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
  5153. authTagSz, (const byte*)aes->key, aes->rounds);
  5154. return 0;
  5155. }
  5156. else
  5157. #endif
  5158. #ifdef HAVE_INTEL_AVX1
  5159. if (IS_INTEL_AVX1(intel_flags)) {
  5160. AES_GCM_encrypt_avx1(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
  5161. authTagSz, (const byte*)aes->key, aes->rounds);
  5162. return 0;
  5163. }
  5164. else
  5165. #endif
  5166. if (haveAESNI) {
  5167. AES_GCM_encrypt(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
  5168. authTagSz, (const byte*)aes->key, aes->rounds);
  5169. return 0;
  5170. }
  5171. else
  5172. #endif
  5173. {
  5174. return AES_GCM_encrypt_C(aes, out, in, sz, iv, ivSz, authTag, authTagSz,
  5175. authIn, authInSz);
  5176. }
  5177. }
  5178. #endif
  5179. /* AES GCM Decrypt */
  5180. #if defined(HAVE_AES_DECRYPT) || defined(HAVE_AESGCM_DECRYPT)
  5181. #ifdef FREESCALE_LTC_AES_GCM
  5182. int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
  5183. const byte* iv, word32 ivSz,
  5184. const byte* authTag, word32 authTagSz,
  5185. const byte* authIn, word32 authInSz)
  5186. {
  5187. int ret;
  5188. word32 keySize;
  5189. status_t status;
  5190. /* argument checks */
  5191. /* If the sz is non-zero, both in and out must be set. If sz is 0,
  5192. * in and out are don't cares, as this is is the GMAC case. */
  5193. if (aes == NULL || iv == NULL || (sz != 0 && (in == NULL || out == NULL)) ||
  5194. authTag == NULL || authTagSz > AES_BLOCK_SIZE || authTagSz == 0) {
  5195. return BAD_FUNC_ARG;
  5196. }
  5197. ret = wc_AesGetKeySize(aes, &keySize);
  5198. if (ret != 0) {
  5199. return ret;
  5200. }
  5201. status = LTC_AES_DecryptTagGcm(LTC_BASE, in, out, sz, iv, ivSz,
  5202. authIn, authInSz, (byte*)aes->key, keySize, authTag, authTagSz);
  5203. return (status == kStatus_Success) ? 0 : AES_GCM_AUTH_E;
  5204. }
  5205. #else
  5206. #ifdef STM32_CRYPTO_AES_GCM
  5207. /* this function supports inline decrypt */
  5208. static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out,
  5209. const byte* in, word32 sz,
  5210. const byte* iv, word32 ivSz,
  5211. const byte* authTag, word32 authTagSz,
  5212. const byte* authIn, word32 authInSz)
  5213. {
  5214. int ret;
  5215. #ifdef WOLFSSL_STM32_CUBEMX
  5216. CRYP_HandleTypeDef hcryp;
  5217. #else
  5218. word32 keyCopy[AES_256_KEY_SIZE/sizeof(word32)];
  5219. #endif
  5220. word32 keySize;
  5221. int status = HAL_OK;
  5222. word32 blocks = sz / AES_BLOCK_SIZE;
  5223. word32 partial = sz % AES_BLOCK_SIZE;
  5224. byte tag[AES_BLOCK_SIZE];
  5225. byte partialBlock[AES_BLOCK_SIZE];
  5226. byte ctr[AES_BLOCK_SIZE];
  5227. byte* authInPadded = NULL;
  5228. int authPadSz;
  5229. ret = wc_AesGetKeySize(aes, &keySize);
  5230. if (ret != 0)
  5231. return ret;
  5232. #ifdef WOLFSSL_STM32_CUBEMX
  5233. ret = wc_Stm32_Aes_Init(aes, &hcryp);
  5234. if (ret != 0)
  5235. return ret;
  5236. #endif
  5237. ret = wolfSSL_CryptHwMutexLock();
  5238. if (ret != 0) {
  5239. return ret;
  5240. }
  5241. XMEMSET(ctr, 0, AES_BLOCK_SIZE);
  5242. if (ivSz == GCM_NONCE_MID_SZ) {
  5243. XMEMCPY(ctr, iv, ivSz);
  5244. ctr[AES_BLOCK_SIZE - 1] = 1;
  5245. }
  5246. else {
  5247. GHASH(aes, NULL, 0, iv, ivSz, ctr, AES_BLOCK_SIZE);
  5248. }
  5249. /* Hardware requires counter + 1 */
  5250. IncrementGcmCounter(ctr);
  5251. if (authInSz == 0 || (authInSz % AES_BLOCK_SIZE) != 0) {
  5252. /* Need to pad the AAD to a full block with zeros. */
  5253. authPadSz = ((authInSz / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
  5254. authInPadded = (byte*)XMALLOC(authPadSz, aes->heap,
  5255. DYNAMIC_TYPE_TMP_BUFFER);
  5256. if (authInPadded == NULL) {
  5257. wolfSSL_CryptHwMutexUnLock();
  5258. return MEMORY_E;
  5259. }
  5260. XMEMSET(authInPadded, 0, authPadSz);
  5261. XMEMCPY(authInPadded, authIn, authInSz);
  5262. } else {
  5263. authPadSz = authInSz;
  5264. authInPadded = (byte*)authIn;
  5265. }
  5266. #ifdef WOLFSSL_STM32_CUBEMX
  5267. hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ctr;
  5268. hcryp.Init.Header = (STM_CRYPT_TYPE*)authInPadded;
  5269. hcryp.Init.HeaderSize = authInSz;
  5270. #ifdef STM32_CRYPTO_AES_ONLY
  5271. /* Set the CRYP parameters */
  5272. hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_GCM_GMAC;
  5273. hcryp.Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  5274. hcryp.Init.GCMCMACPhase = CRYP_INIT_PHASE;
  5275. HAL_CRYP_Init(&hcryp);
  5276. /* GCM init phase */
  5277. status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, 0, NULL, STM32_HAL_TIMEOUT);
  5278. if (status == HAL_OK) {
  5279. /* GCM header phase */
  5280. hcryp.Init.GCMCMACPhase = CRYP_HEADER_PHASE;
  5281. status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, 0, NULL, STM32_HAL_TIMEOUT);
  5282. }
  5283. if (status == HAL_OK) {
  5284. /* GCM payload phase - blocks */
  5285. hcryp.Init.GCMCMACPhase = CRYP_PAYLOAD_PHASE;
  5286. if (blocks) {
  5287. status = HAL_CRYPEx_AES_Auth(&hcryp, (byte*)in,
  5288. (blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT);
  5289. }
  5290. }
  5291. if (status == HAL_OK && (partial != 0 || blocks == 0)) {
  5292. /* GCM payload phase - partial remainder */
  5293. XMEMSET(partialBlock, 0, sizeof(partialBlock));
  5294. XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
  5295. status = HAL_CRYPEx_AES_Auth(&hcryp, partialBlock, partial,
  5296. partialBlock, STM32_HAL_TIMEOUT);
  5297. XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
  5298. }
  5299. if (status == HAL_OK) {
  5300. /* GCM final phase */
  5301. hcryp.Init.GCMCMACPhase = CRYP_FINAL_PHASE;
  5302. status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, tag, STM32_HAL_TIMEOUT);
  5303. }
  5304. #elif defined(STM32_HAL_V2)
  5305. hcryp.Init.Algorithm = CRYP_AES_GCM;
  5306. ByteReverseWords((word32*)partialBlock, (word32*)ctr, AES_BLOCK_SIZE);
  5307. hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock;
  5308. HAL_CRYP_Init(&hcryp);
  5309. /* GCM payload phase - can handle partial blocks */
  5310. status = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)in,
  5311. (blocks * AES_BLOCK_SIZE) + partial, (uint32_t*)out, STM32_HAL_TIMEOUT);
  5312. if (status == HAL_OK) {
  5313. /* Compute the authTag */
  5314. status = HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp, (uint32_t*)tag,
  5315. STM32_HAL_TIMEOUT);
  5316. }
  5317. #else
  5318. HAL_CRYP_Init(&hcryp);
  5319. if (blocks) {
  5320. /* GCM payload phase - blocks */
  5321. status = HAL_CRYPEx_AESGCM_Decrypt(&hcryp, (byte*)in,
  5322. (blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT);
  5323. }
  5324. if (status == HAL_OK && (partial != 0 || blocks == 0)) {
  5325. /* GCM payload phase - partial remainder */
  5326. XMEMSET(partialBlock, 0, sizeof(partialBlock));
  5327. XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
  5328. status = HAL_CRYPEx_AESGCM_Decrypt(&hcryp, partialBlock, partial,
  5329. partialBlock, STM32_HAL_TIMEOUT);
  5330. XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
  5331. }
  5332. if (status == HAL_OK) {
  5333. /* Compute the authTag */
  5334. status = HAL_CRYPEx_AESGCM_Finish(&hcryp, sz, tag, STM32_HAL_TIMEOUT);
  5335. }
  5336. #endif
  5337. if (status != HAL_OK)
  5338. ret = AES_GCM_AUTH_E;
  5339. HAL_CRYP_DeInit(&hcryp);
  5340. #else /* STD_PERI_LIB */
  5341. ByteReverseWords(keyCopy, (word32*)aes->key, aes->keylen);
  5342. /* Input size and auth size need to be the actual sizes, even though
  5343. * they are not block aligned, because this length (in bits) is used
  5344. * in the final GHASH. */
  5345. status = CRYP_AES_GCM(MODE_DECRYPT, (uint8_t*)ctr,
  5346. (uint8_t*)keyCopy, keySize * 8,
  5347. (uint8_t*)in, sz,
  5348. (uint8_t*)authInPadded, authInSz,
  5349. (uint8_t*)out, tag);
  5350. if (status != SUCCESS)
  5351. ret = AES_GCM_AUTH_E;
  5352. #endif /* WOLFSSL_STM32_CUBEMX */
  5353. /* STM32 GCM hardware only supports IV of 12 bytes, so use software for auth */
  5354. if (sz == 0 || ivSz != GCM_NONCE_MID_SZ) {
  5355. DecrementGcmCounter(ctr); /* hardware requires +1, so subtract it */
  5356. GHASH(aes, authIn, authInSz, in, sz, tag, sizeof(tag));
  5357. wc_AesEncrypt(aes, ctr, partialBlock);
  5358. xorbuf(tag, partialBlock, sizeof(tag));
  5359. }
  5360. if (ConstantCompare(authTag, tag, authTagSz) != 0) {
  5361. ret = AES_GCM_AUTH_E;
  5362. }
  5363. /* Free memory if not a multiple of AES_BLOCK_SZ */
  5364. if (authInPadded != authIn) {
  5365. XFREE(authInPadded, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
  5366. }
  5367. wolfSSL_CryptHwMutexUnLock();
  5368. return ret;
  5369. }
  5370. #endif /* STM32_CRYPTO_AES_GCM */
  5371. #ifdef WOLFSSL_AESNI
  5372. int AES_GCM_decrypt_C(Aes* aes, byte* out, const byte* in, word32 sz,
  5373. const byte* iv, word32 ivSz,
  5374. const byte* authTag, word32 authTagSz,
  5375. const byte* authIn, word32 authInSz);
  5376. #else
  5377. static
  5378. #endif
  5379. int AES_GCM_decrypt_C(Aes* aes, byte* out, const byte* in, word32 sz,
  5380. const byte* iv, word32 ivSz,
  5381. const byte* authTag, word32 authTagSz,
  5382. const byte* authIn, word32 authInSz)
  5383. {
  5384. int ret = 0;
  5385. word32 blocks = sz / AES_BLOCK_SIZE;
  5386. word32 partial = sz % AES_BLOCK_SIZE;
  5387. const byte* c = in;
  5388. byte* p = out;
  5389. byte counter[AES_BLOCK_SIZE];
  5390. byte initialCounter[AES_BLOCK_SIZE];
  5391. byte *ctr;
  5392. byte scratch[AES_BLOCK_SIZE];
  5393. byte Tprime[AES_BLOCK_SIZE];
  5394. byte EKY0[AES_BLOCK_SIZE];
  5395. #ifdef OPENSSL_EXTRA
  5396. word32 aadTemp;
  5397. #endif
  5398. ctr = counter;
  5399. XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
  5400. if (ivSz == GCM_NONCE_MID_SZ) {
  5401. XMEMCPY(initialCounter, iv, ivSz);
  5402. initialCounter[AES_BLOCK_SIZE - 1] = 1;
  5403. }
  5404. else {
  5405. #ifdef OPENSSL_EXTRA
  5406. aadTemp = aes->aadLen;
  5407. aes->aadLen = 0;
  5408. #endif
  5409. GHASH(aes, NULL, 0, iv, ivSz, initialCounter, AES_BLOCK_SIZE);
  5410. #ifdef OPENSSL_EXTRA
  5411. aes->aadLen = aadTemp;
  5412. #endif
  5413. }
  5414. XMEMCPY(ctr, initialCounter, AES_BLOCK_SIZE);
  5415. /* Calc the authTag again using the received auth data and the cipher text */
  5416. GHASH(aes, authIn, authInSz, in, sz, Tprime, sizeof(Tprime));
  5417. wc_AesEncrypt(aes, ctr, EKY0);
  5418. xorbuf(Tprime, EKY0, sizeof(Tprime));
  5419. #ifdef OPENSSL_EXTRA
  5420. if (!out) {
  5421. /* authenticated, non-confidential data */
  5422. /* store AAD size for next call */
  5423. aes->aadLen = authInSz;
  5424. }
  5425. #endif
  5426. if (ConstantCompare(authTag, Tprime, authTagSz) != 0) {
  5427. return AES_GCM_AUTH_E;
  5428. }
  5429. #if defined(WOLFSSL_PIC32MZ_CRYPT)
  5430. if (blocks) {
  5431. /* use initial IV for HW, but don't use it below */
  5432. XMEMCPY(aes->reg, ctr, AES_BLOCK_SIZE);
  5433. ret = wc_Pic32AesCrypt(
  5434. aes->key, aes->keylen, aes->reg, AES_BLOCK_SIZE,
  5435. out, in, (blocks * AES_BLOCK_SIZE),
  5436. PIC32_DECRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_AES_GCM);
  5437. if (ret != 0)
  5438. return ret;
  5439. }
  5440. /* process remainder using partial handling */
  5441. #endif
  5442. #if defined(HAVE_AES_ECB) && !defined(WOLFSSL_PIC32MZ_CRYPT)
  5443. /* some hardware acceleration can gain performance from doing AES encryption
  5444. * of the whole buffer at once */
  5445. if (c != p) { /* can not handle inline decryption */
  5446. while (blocks--) {
  5447. IncrementGcmCounter(ctr);
  5448. XMEMCPY(p, ctr, AES_BLOCK_SIZE);
  5449. p += AES_BLOCK_SIZE;
  5450. }
  5451. /* reset number of blocks and then do encryption */
  5452. blocks = sz / AES_BLOCK_SIZE;
  5453. wc_AesEcbEncrypt(aes, out, out, AES_BLOCK_SIZE * blocks);
  5454. xorbuf(out, c, AES_BLOCK_SIZE * blocks);
  5455. c += AES_BLOCK_SIZE * blocks;
  5456. }
  5457. else
  5458. #endif /* HAVE_AES_ECB && !PIC32MZ */
  5459. while (blocks--) {
  5460. IncrementGcmCounter(ctr);
  5461. #if !defined(WOLFSSL_PIC32MZ_CRYPT)
  5462. wc_AesEncrypt(aes, ctr, scratch);
  5463. xorbuf(scratch, c, AES_BLOCK_SIZE);
  5464. XMEMCPY(p, scratch, AES_BLOCK_SIZE);
  5465. #endif
  5466. p += AES_BLOCK_SIZE;
  5467. c += AES_BLOCK_SIZE;
  5468. }
  5469. if (partial != 0) {
  5470. IncrementGcmCounter(ctr);
  5471. wc_AesEncrypt(aes, ctr, scratch);
  5472. xorbuf(scratch, c, partial);
  5473. XMEMCPY(p, scratch, partial);
  5474. }
  5475. return ret;
  5476. }
  5477. /* Software AES - GCM Decrypt */
  5478. int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
  5479. const byte* iv, word32 ivSz,
  5480. const byte* authTag, word32 authTagSz,
  5481. const byte* authIn, word32 authInSz)
  5482. {
  5483. #ifdef WOLFSSL_AESNI
  5484. int res = AES_GCM_AUTH_E;
  5485. #endif
  5486. /* argument checks */
  5487. /* If the sz is non-zero, both in and out must be set. If sz is 0,
  5488. * in and out are don't cares, as this is is the GMAC case. */
  5489. if (aes == NULL || iv == NULL || (sz != 0 && (in == NULL || out == NULL)) ||
  5490. authTag == NULL || authTagSz > AES_BLOCK_SIZE || authTagSz == 0) {
  5491. return BAD_FUNC_ARG;
  5492. }
  5493. #ifdef WOLF_CRYPTO_CB
  5494. if (aes->devId != INVALID_DEVID) {
  5495. int ret = wc_CryptoCb_AesGcmDecrypt(aes, out, in, sz, iv, ivSz,
  5496. authTag, authTagSz, authIn, authInSz);
  5497. if (ret != CRYPTOCB_UNAVAILABLE)
  5498. return ret;
  5499. /* fall-through when unavailable */
  5500. }
  5501. #endif
  5502. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
  5503. /* if async and byte count above threshold */
  5504. /* only 12-byte IV is supported in HW */
  5505. if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
  5506. sz >= WC_ASYNC_THRESH_AES_GCM && ivSz == GCM_NONCE_MID_SZ) {
  5507. #if defined(HAVE_CAVIUM)
  5508. #ifdef HAVE_CAVIUM_V
  5509. if (authInSz == 20) { /* Nitrox V GCM is only working with 20 byte AAD */
  5510. return NitroxAesGcmDecrypt(aes, out, in, sz,
  5511. (const byte*)aes->devKey, aes->keylen, iv, ivSz,
  5512. authTag, authTagSz, authIn, authInSz);
  5513. }
  5514. #endif
  5515. #elif defined(HAVE_INTEL_QA)
  5516. return IntelQaSymAesGcmDecrypt(&aes->asyncDev, out, in, sz,
  5517. (const byte*)aes->devKey, aes->keylen, iv, ivSz,
  5518. authTag, authTagSz, authIn, authInSz);
  5519. #else /* WOLFSSL_ASYNC_CRYPT_TEST */
  5520. if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_GCM_DECRYPT)) {
  5521. WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
  5522. testDev->aes.aes = aes;
  5523. testDev->aes.out = out;
  5524. testDev->aes.in = in;
  5525. testDev->aes.sz = sz;
  5526. testDev->aes.iv = iv;
  5527. testDev->aes.ivSz = ivSz;
  5528. testDev->aes.authTag = (byte*)authTag;
  5529. testDev->aes.authTagSz = authTagSz;
  5530. testDev->aes.authIn = authIn;
  5531. testDev->aes.authInSz = authInSz;
  5532. return WC_PENDING_E;
  5533. }
  5534. #endif
  5535. }
  5536. #endif /* WOLFSSL_ASYNC_CRYPT */
  5537. #ifdef STM32_CRYPTO_AES_GCM
  5538. /* The STM standard peripheral library API's doesn't support partial blocks */
  5539. #ifdef STD_PERI_LIB
  5540. if (partial == 0)
  5541. #endif
  5542. {
  5543. return wc_AesGcmDecrypt_STM32(
  5544. aes, out, in, sz, iv, ivSz,
  5545. authTag, authTagSz, authIn, authInSz);
  5546. }
  5547. #endif /* STM32_CRYPTO_AES_GCM */
  5548. #ifdef WOLFSSL_AESNI
  5549. #ifdef HAVE_INTEL_AVX2
  5550. if (IS_INTEL_AVX2(intel_flags)) {
  5551. AES_GCM_decrypt_avx2(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
  5552. authTagSz, (byte*)aes->key, aes->rounds, &res);
  5553. if (res == 0)
  5554. return AES_GCM_AUTH_E;
  5555. return 0;
  5556. }
  5557. else
  5558. #endif
  5559. #ifdef HAVE_INTEL_AVX1
  5560. if (IS_INTEL_AVX1(intel_flags)) {
  5561. AES_GCM_decrypt_avx1(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
  5562. authTagSz, (byte*)aes->key, aes->rounds, &res);
  5563. if (res == 0)
  5564. return AES_GCM_AUTH_E;
  5565. return 0;
  5566. }
  5567. else
  5568. #endif
  5569. if (haveAESNI) {
  5570. AES_GCM_decrypt(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
  5571. authTagSz, (byte*)aes->key, aes->rounds, &res);
  5572. if (res == 0)
  5573. return AES_GCM_AUTH_E;
  5574. return 0;
  5575. }
  5576. else
  5577. #endif
  5578. {
  5579. return AES_GCM_decrypt_C(aes, out, in, sz, iv, ivSz, authTag, authTagSz,
  5580. authIn, authInSz);
  5581. }
  5582. }
  5583. #endif
  5584. #endif /* HAVE_AES_DECRYPT || HAVE_AESGCM_DECRYPT */
  5585. #endif /* WOLFSSL_XILINX_CRYPT */
  5586. #endif /* end of block for AESGCM implementation selection */
  5587. /* Common to all, abstract functions that build off of lower level AESGCM
  5588. * functions */
  5589. #ifndef WC_NO_RNG
  5590. int wc_AesGcmSetExtIV(Aes* aes, const byte* iv, word32 ivSz)
  5591. {
  5592. int ret = 0;
  5593. if (aes == NULL || iv == NULL ||
  5594. (ivSz != GCM_NONCE_MIN_SZ && ivSz != GCM_NONCE_MID_SZ &&
  5595. ivSz != GCM_NONCE_MAX_SZ)) {
  5596. ret = BAD_FUNC_ARG;
  5597. }
  5598. if (ret == 0) {
  5599. XMEMCPY((byte*)aes->reg, iv, ivSz);
  5600. /* If the IV is 96, allow for a 2^64 invocation counter.
  5601. * For any other size for the nonce, limit the invocation
  5602. * counter to 32-bits. (SP 800-38D 8.3) */
  5603. aes->invokeCtr[0] = 0;
  5604. aes->invokeCtr[1] = (ivSz == GCM_NONCE_MID_SZ) ? 0 : 0xFFFFFFFF;
  5605. aes->nonceSz = ivSz;
  5606. }
  5607. return ret;
  5608. }
  5609. int wc_AesGcmSetIV(Aes* aes, word32 ivSz,
  5610. const byte* ivFixed, word32 ivFixedSz,
  5611. WC_RNG* rng)
  5612. {
  5613. int ret = 0;
  5614. if (aes == NULL || rng == NULL ||
  5615. (ivSz != GCM_NONCE_MIN_SZ && ivSz != GCM_NONCE_MID_SZ &&
  5616. ivSz != GCM_NONCE_MAX_SZ) ||
  5617. (ivFixed == NULL && ivFixedSz != 0) ||
  5618. (ivFixed != NULL && ivFixedSz != AES_IV_FIXED_SZ)) {
  5619. ret = BAD_FUNC_ARG;
  5620. }
  5621. if (ret == 0) {
  5622. byte* iv = (byte*)aes->reg;
  5623. if (ivFixedSz)
  5624. XMEMCPY(iv, ivFixed, ivFixedSz);
  5625. ret = wc_RNG_GenerateBlock(rng, iv + ivFixedSz, ivSz - ivFixedSz);
  5626. }
  5627. if (ret == 0) {
  5628. /* If the IV is 96, allow for a 2^64 invocation counter.
  5629. * For any other size for the nonce, limit the invocation
  5630. * counter to 32-bits. (SP 800-38D 8.3) */
  5631. aes->invokeCtr[0] = 0;
  5632. aes->invokeCtr[1] = (ivSz == GCM_NONCE_MID_SZ) ? 0 : 0xFFFFFFFF;
  5633. aes->nonceSz = ivSz;
  5634. }
  5635. return ret;
  5636. }
  5637. int wc_AesGcmEncrypt_ex(Aes* aes, byte* out, const byte* in, word32 sz,
  5638. byte* ivOut, word32 ivOutSz,
  5639. byte* authTag, word32 authTagSz,
  5640. const byte* authIn, word32 authInSz)
  5641. {
  5642. int ret = 0;
  5643. if (aes == NULL || (sz != 0 && (in == NULL || out == NULL)) ||
  5644. ivOut == NULL || ivOutSz != aes->nonceSz ||
  5645. (authIn == NULL && authInSz != 0)) {
  5646. ret = BAD_FUNC_ARG;
  5647. }
  5648. if (ret == 0) {
  5649. aes->invokeCtr[0]++;
  5650. if (aes->invokeCtr[0] == 0) {
  5651. aes->invokeCtr[1]++;
  5652. if (aes->invokeCtr[1] == 0)
  5653. ret = AES_GCM_OVERFLOW_E;
  5654. }
  5655. }
  5656. if (ret == 0) {
  5657. XMEMCPY(ivOut, aes->reg, ivOutSz);
  5658. ret = wc_AesGcmEncrypt(aes, out, in, sz,
  5659. (byte*)aes->reg, ivOutSz,
  5660. authTag, authTagSz,
  5661. authIn, authInSz);
  5662. if (ret == 0)
  5663. IncCtr((byte*)aes->reg, ivOutSz);
  5664. }
  5665. return ret;
  5666. }
  5667. int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
  5668. const byte* authIn, word32 authInSz,
  5669. byte* authTag, word32 authTagSz, WC_RNG* rng)
  5670. {
  5671. Aes aes;
  5672. int ret;
  5673. if (key == NULL || iv == NULL || (authIn == NULL && authInSz != 0) ||
  5674. authTag == NULL || authTagSz == 0 || rng == NULL) {
  5675. return BAD_FUNC_ARG;
  5676. }
  5677. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  5678. if (ret == 0) {
  5679. ret = wc_AesGcmSetKey(&aes, key, keySz);
  5680. if (ret == 0)
  5681. ret = wc_AesGcmSetIV(&aes, ivSz, NULL, 0, rng);
  5682. if (ret == 0)
  5683. ret = wc_AesGcmEncrypt_ex(&aes, NULL, NULL, 0, iv, ivSz,
  5684. authTag, authTagSz, authIn, authInSz);
  5685. wc_AesFree(&aes);
  5686. }
  5687. ForceZero(&aes, sizeof(aes));
  5688. return ret;
  5689. }
  5690. int wc_GmacVerify(const byte* key, word32 keySz,
  5691. const byte* iv, word32 ivSz,
  5692. const byte* authIn, word32 authInSz,
  5693. const byte* authTag, word32 authTagSz)
  5694. {
  5695. int ret;
  5696. #ifndef NO_AES_DECRYPT
  5697. Aes aes;
  5698. if (key == NULL || iv == NULL || (authIn == NULL && authInSz != 0) ||
  5699. authTag == NULL || authTagSz == 0 || authTagSz > AES_BLOCK_SIZE) {
  5700. return BAD_FUNC_ARG;
  5701. }
  5702. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  5703. if (ret == 0) {
  5704. ret = wc_AesGcmSetKey(&aes, key, keySz);
  5705. if (ret == 0)
  5706. ret = wc_AesGcmDecrypt(&aes, NULL, NULL, 0, iv, ivSz,
  5707. authTag, authTagSz, authIn, authInSz);
  5708. wc_AesFree(&aes);
  5709. }
  5710. ForceZero(&aes, sizeof(aes));
  5711. #else
  5712. (void)key;
  5713. (void)keySz;
  5714. (void)iv;
  5715. (void)ivSz;
  5716. (void)authIn;
  5717. (void)authInSz;
  5718. (void)authTag;
  5719. (void)authTagSz;
  5720. ret = NOT_COMPILED_IN;
  5721. #endif
  5722. return ret;
  5723. }
  5724. #endif /* WC_NO_RNG */
  5725. WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len)
  5726. {
  5727. if (gmac == NULL || key == NULL) {
  5728. return BAD_FUNC_ARG;
  5729. }
  5730. return wc_AesGcmSetKey(&gmac->aes, key, len);
  5731. }
  5732. WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
  5733. const byte* authIn, word32 authInSz,
  5734. byte* authTag, word32 authTagSz)
  5735. {
  5736. return wc_AesGcmEncrypt(&gmac->aes, NULL, NULL, 0, iv, ivSz,
  5737. authTag, authTagSz, authIn, authInSz);
  5738. }
  5739. #endif /* HAVE_AESGCM */
  5740. #ifdef HAVE_AESCCM
  5741. int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
  5742. {
  5743. if (!((keySz == 16) || (keySz == 24) || (keySz == 32)))
  5744. return BAD_FUNC_ARG;
  5745. return wc_AesSetKey(aes, key, keySz, NULL, AES_ENCRYPTION);
  5746. }
  5747. #ifdef WOLFSSL_ARMASM
  5748. /* implementation located in wolfcrypt/src/port/arm/armv8-aes.c */
  5749. #elif defined(HAVE_COLDFIRE_SEC)
  5750. #error "Coldfire SEC doesn't currently support AES-CCM mode"
  5751. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
  5752. /* implemented in wolfcrypt/src/port/caam_aes.c */
  5753. #elif defined(FREESCALE_LTC)
  5754. /* return 0 on success */
  5755. int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
  5756. const byte* nonce, word32 nonceSz,
  5757. byte* authTag, word32 authTagSz,
  5758. const byte* authIn, word32 authInSz)
  5759. {
  5760. byte *key;
  5761. uint32_t keySize;
  5762. status_t status;
  5763. /* sanity check on arguments */
  5764. if (aes == NULL || out == NULL || in == NULL || nonce == NULL
  5765. || authTag == NULL || nonceSz < 7 || nonceSz > 13)
  5766. return BAD_FUNC_ARG;
  5767. key = (byte*)aes->key;
  5768. status = wc_AesGetKeySize(aes, &keySize);
  5769. if (status != 0) {
  5770. return status;
  5771. }
  5772. status = LTC_AES_EncryptTagCcm(LTC_BASE, in, out, inSz,
  5773. nonce, nonceSz, authIn, authInSz, key, keySize, authTag, authTagSz);
  5774. return (kStatus_Success == status) ? 0 : BAD_FUNC_ARG;
  5775. }
  5776. #ifdef HAVE_AES_DECRYPT
  5777. int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
  5778. const byte* nonce, word32 nonceSz,
  5779. const byte* authTag, word32 authTagSz,
  5780. const byte* authIn, word32 authInSz)
  5781. {
  5782. byte *key;
  5783. uint32_t keySize;
  5784. status_t status;
  5785. /* sanity check on arguments */
  5786. if (aes == NULL || out == NULL || in == NULL || nonce == NULL
  5787. || authTag == NULL || nonceSz < 7 || nonceSz > 13)
  5788. return BAD_FUNC_ARG;
  5789. key = (byte*)aes->key;
  5790. status = wc_AesGetKeySize(aes, &keySize);
  5791. if (status != 0) {
  5792. return status;
  5793. }
  5794. status = LTC_AES_DecryptTagCcm(LTC_BASE, in, out, inSz,
  5795. nonce, nonceSz, authIn, authInSz, key, keySize, authTag, authTagSz);
  5796. if (status == kStatus_Success) {
  5797. return 0;
  5798. }
  5799. else {
  5800. XMEMSET(out, 0, inSz);
  5801. return AES_CCM_AUTH_E;
  5802. }
  5803. }
  5804. #endif /* HAVE_AES_DECRYPT */
  5805. #else
  5806. /* Software CCM */
  5807. static void roll_x(Aes* aes, const byte* in, word32 inSz, byte* out)
  5808. {
  5809. /* process the bulk of the data */
  5810. while (inSz >= AES_BLOCK_SIZE) {
  5811. xorbuf(out, in, AES_BLOCK_SIZE);
  5812. in += AES_BLOCK_SIZE;
  5813. inSz -= AES_BLOCK_SIZE;
  5814. wc_AesEncrypt(aes, out, out);
  5815. }
  5816. /* process remainder of the data */
  5817. if (inSz > 0) {
  5818. xorbuf(out, in, inSz);
  5819. wc_AesEncrypt(aes, out, out);
  5820. }
  5821. }
  5822. static void roll_auth(Aes* aes, const byte* in, word32 inSz, byte* out)
  5823. {
  5824. word32 authLenSz;
  5825. word32 remainder;
  5826. /* encode the length in */
  5827. if (inSz <= 0xFEFF) {
  5828. authLenSz = 2;
  5829. out[0] ^= ((inSz & 0xFF00) >> 8);
  5830. out[1] ^= (inSz & 0x00FF);
  5831. }
  5832. else if (inSz <= 0xFFFFFFFF) {
  5833. authLenSz = 6;
  5834. out[0] ^= 0xFF; out[1] ^= 0xFE;
  5835. out[2] ^= ((inSz & 0xFF000000) >> 24);
  5836. out[3] ^= ((inSz & 0x00FF0000) >> 16);
  5837. out[4] ^= ((inSz & 0x0000FF00) >> 8);
  5838. out[5] ^= (inSz & 0x000000FF);
  5839. }
  5840. /* Note, the protocol handles auth data up to 2^64, but we are
  5841. * using 32-bit sizes right now, so the bigger data isn't handled
  5842. * else if (inSz <= 0xFFFFFFFFFFFFFFFF) {} */
  5843. else
  5844. return;
  5845. /* start fill out the rest of the first block */
  5846. remainder = AES_BLOCK_SIZE - authLenSz;
  5847. if (inSz >= remainder) {
  5848. /* plenty of bulk data to fill the remainder of this block */
  5849. xorbuf(out + authLenSz, in, remainder);
  5850. inSz -= remainder;
  5851. in += remainder;
  5852. }
  5853. else {
  5854. /* not enough bulk data, copy what is available, and pad zero */
  5855. xorbuf(out + authLenSz, in, inSz);
  5856. inSz = 0;
  5857. }
  5858. wc_AesEncrypt(aes, out, out);
  5859. if (inSz > 0)
  5860. roll_x(aes, in, inSz, out);
  5861. }
  5862. static WC_INLINE void AesCcmCtrInc(byte* B, word32 lenSz)
  5863. {
  5864. word32 i;
  5865. for (i = 0; i < lenSz; i++) {
  5866. if (++B[AES_BLOCK_SIZE - 1 - i] != 0) return;
  5867. }
  5868. }
  5869. /* Software AES - CCM Encrypt */
  5870. /* return 0 on success */
  5871. int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
  5872. const byte* nonce, word32 nonceSz,
  5873. byte* authTag, word32 authTagSz,
  5874. const byte* authIn, word32 authInSz)
  5875. {
  5876. byte A[AES_BLOCK_SIZE];
  5877. byte B[AES_BLOCK_SIZE];
  5878. byte lenSz;
  5879. word32 i;
  5880. byte mask = 0xFF;
  5881. const word32 wordSz = (word32)sizeof(word32);
  5882. /* sanity check on arguments */
  5883. if (aes == NULL || out == NULL || in == NULL || nonce == NULL
  5884. || authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
  5885. authTagSz > AES_BLOCK_SIZE)
  5886. return BAD_FUNC_ARG;
  5887. XMEMSET(A, 0, sizeof(A));
  5888. XMEMCPY(B+1, nonce, nonceSz);
  5889. lenSz = AES_BLOCK_SIZE - 1 - (byte)nonceSz;
  5890. B[0] = (authInSz > 0 ? 64 : 0)
  5891. + (8 * (((byte)authTagSz - 2) / 2))
  5892. + (lenSz - 1);
  5893. for (i = 0; i < lenSz; i++) {
  5894. if (mask && i >= wordSz)
  5895. mask = 0x00;
  5896. B[AES_BLOCK_SIZE - 1 - i] = (inSz >> ((8 * i) & mask)) & mask;
  5897. }
  5898. wc_AesEncrypt(aes, B, A);
  5899. if (authInSz > 0)
  5900. roll_auth(aes, authIn, authInSz, A);
  5901. if (inSz > 0)
  5902. roll_x(aes, in, inSz, A);
  5903. XMEMCPY(authTag, A, authTagSz);
  5904. B[0] = lenSz - 1;
  5905. for (i = 0; i < lenSz; i++)
  5906. B[AES_BLOCK_SIZE - 1 - i] = 0;
  5907. wc_AesEncrypt(aes, B, A);
  5908. xorbuf(authTag, A, authTagSz);
  5909. B[15] = 1;
  5910. while (inSz >= AES_BLOCK_SIZE) {
  5911. wc_AesEncrypt(aes, B, A);
  5912. xorbuf(A, in, AES_BLOCK_SIZE);
  5913. XMEMCPY(out, A, AES_BLOCK_SIZE);
  5914. AesCcmCtrInc(B, lenSz);
  5915. inSz -= AES_BLOCK_SIZE;
  5916. in += AES_BLOCK_SIZE;
  5917. out += AES_BLOCK_SIZE;
  5918. }
  5919. if (inSz > 0) {
  5920. wc_AesEncrypt(aes, B, A);
  5921. xorbuf(A, in, inSz);
  5922. XMEMCPY(out, A, inSz);
  5923. }
  5924. ForceZero(A, AES_BLOCK_SIZE);
  5925. ForceZero(B, AES_BLOCK_SIZE);
  5926. return 0;
  5927. }
  5928. #ifdef HAVE_AES_DECRYPT
  5929. /* Software AES - CCM Decrypt */
  5930. int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
  5931. const byte* nonce, word32 nonceSz,
  5932. const byte* authTag, word32 authTagSz,
  5933. const byte* authIn, word32 authInSz)
  5934. {
  5935. byte A[AES_BLOCK_SIZE];
  5936. byte B[AES_BLOCK_SIZE];
  5937. byte* o;
  5938. byte lenSz;
  5939. word32 i, oSz;
  5940. int result = 0;
  5941. byte mask = 0xFF;
  5942. const word32 wordSz = (word32)sizeof(word32);
  5943. /* sanity check on arguments */
  5944. if (aes == NULL || out == NULL || in == NULL || nonce == NULL
  5945. || authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
  5946. authTagSz > AES_BLOCK_SIZE)
  5947. return BAD_FUNC_ARG;
  5948. o = out;
  5949. oSz = inSz;
  5950. XMEMCPY(B+1, nonce, nonceSz);
  5951. lenSz = AES_BLOCK_SIZE - 1 - (byte)nonceSz;
  5952. B[0] = lenSz - 1;
  5953. for (i = 0; i < lenSz; i++)
  5954. B[AES_BLOCK_SIZE - 1 - i] = 0;
  5955. B[15] = 1;
  5956. while (oSz >= AES_BLOCK_SIZE) {
  5957. wc_AesEncrypt(aes, B, A);
  5958. xorbuf(A, in, AES_BLOCK_SIZE);
  5959. XMEMCPY(o, A, AES_BLOCK_SIZE);
  5960. AesCcmCtrInc(B, lenSz);
  5961. oSz -= AES_BLOCK_SIZE;
  5962. in += AES_BLOCK_SIZE;
  5963. o += AES_BLOCK_SIZE;
  5964. }
  5965. if (inSz > 0) {
  5966. wc_AesEncrypt(aes, B, A);
  5967. xorbuf(A, in, oSz);
  5968. XMEMCPY(o, A, oSz);
  5969. }
  5970. for (i = 0; i < lenSz; i++)
  5971. B[AES_BLOCK_SIZE - 1 - i] = 0;
  5972. wc_AesEncrypt(aes, B, A);
  5973. o = out;
  5974. oSz = inSz;
  5975. B[0] = (authInSz > 0 ? 64 : 0)
  5976. + (8 * (((byte)authTagSz - 2) / 2))
  5977. + (lenSz - 1);
  5978. for (i = 0; i < lenSz; i++) {
  5979. if (mask && i >= wordSz)
  5980. mask = 0x00;
  5981. B[AES_BLOCK_SIZE - 1 - i] = (inSz >> ((8 * i) & mask)) & mask;
  5982. }
  5983. wc_AesEncrypt(aes, B, A);
  5984. if (authInSz > 0)
  5985. roll_auth(aes, authIn, authInSz, A);
  5986. if (inSz > 0)
  5987. roll_x(aes, o, oSz, A);
  5988. B[0] = lenSz - 1;
  5989. for (i = 0; i < lenSz; i++)
  5990. B[AES_BLOCK_SIZE - 1 - i] = 0;
  5991. wc_AesEncrypt(aes, B, B);
  5992. xorbuf(A, B, authTagSz);
  5993. if (ConstantCompare(A, authTag, authTagSz) != 0) {
  5994. /* If the authTag check fails, don't keep the decrypted data.
  5995. * Unfortunately, you need the decrypted data to calculate the
  5996. * check value. */
  5997. XMEMSET(out, 0, inSz);
  5998. result = AES_CCM_AUTH_E;
  5999. }
  6000. ForceZero(A, AES_BLOCK_SIZE);
  6001. ForceZero(B, AES_BLOCK_SIZE);
  6002. o = NULL;
  6003. return result;
  6004. }
  6005. #endif /* HAVE_AES_DECRYPT */
  6006. #endif /* software CCM */
  6007. /* abstract functions that call lower level AESCCM functions */
  6008. #ifndef WC_NO_RNG
  6009. int wc_AesCcmSetNonce(Aes* aes, const byte* nonce, word32 nonceSz)
  6010. {
  6011. int ret = 0;
  6012. if (aes == NULL || nonce == NULL ||
  6013. nonceSz < CCM_NONCE_MIN_SZ || nonceSz > CCM_NONCE_MAX_SZ) {
  6014. ret = BAD_FUNC_ARG;
  6015. }
  6016. if (ret == 0) {
  6017. XMEMCPY(aes->reg, nonce, nonceSz);
  6018. aes->nonceSz = nonceSz;
  6019. /* Invocation counter should be 2^61 */
  6020. aes->invokeCtr[0] = 0;
  6021. aes->invokeCtr[1] = 0xE0000000;
  6022. }
  6023. return ret;
  6024. }
  6025. int wc_AesCcmEncrypt_ex(Aes* aes, byte* out, const byte* in, word32 sz,
  6026. byte* ivOut, word32 ivOutSz,
  6027. byte* authTag, word32 authTagSz,
  6028. const byte* authIn, word32 authInSz)
  6029. {
  6030. int ret = 0;
  6031. if (aes == NULL || out == NULL ||
  6032. (in == NULL && sz != 0) ||
  6033. ivOut == NULL ||
  6034. (authIn == NULL && authInSz != 0) ||
  6035. (ivOutSz != aes->nonceSz)) {
  6036. ret = BAD_FUNC_ARG;
  6037. }
  6038. if (ret == 0) {
  6039. aes->invokeCtr[0]++;
  6040. if (aes->invokeCtr[0] == 0) {
  6041. aes->invokeCtr[1]++;
  6042. if (aes->invokeCtr[1] == 0)
  6043. ret = AES_CCM_OVERFLOW_E;
  6044. }
  6045. }
  6046. if (ret == 0) {
  6047. ret = wc_AesCcmEncrypt(aes, out, in, sz,
  6048. (byte*)aes->reg, aes->nonceSz,
  6049. authTag, authTagSz,
  6050. authIn, authInSz);
  6051. if (ret == 0) {
  6052. XMEMCPY(ivOut, aes->reg, aes->nonceSz);
  6053. IncCtr((byte*)aes->reg, aes->nonceSz);
  6054. }
  6055. }
  6056. return ret;
  6057. }
  6058. #endif /* WC_NO_RNG */
  6059. #endif /* HAVE_AESCCM */
  6060. /* Initialize Aes for use with async hardware */
  6061. int wc_AesInit(Aes* aes, void* heap, int devId)
  6062. {
  6063. int ret = 0;
  6064. if (aes == NULL)
  6065. return BAD_FUNC_ARG;
  6066. aes->heap = heap;
  6067. #ifdef WOLF_CRYPTO_CB
  6068. aes->devId = devId;
  6069. aes->devCtx = NULL;
  6070. #else
  6071. (void)devId;
  6072. #endif
  6073. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
  6074. ret = wolfAsync_DevCtxInit(&aes->asyncDev, WOLFSSL_ASYNC_MARKER_AES,
  6075. aes->heap, devId);
  6076. #endif /* WOLFSSL_ASYNC_CRYPT */
  6077. #ifdef WOLFSSL_AFALG
  6078. aes->alFd = -1;
  6079. aes->rdFd = -1;
  6080. #endif
  6081. #if defined(WOLFSSL_DEVCRYPTO) && \
  6082. (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))
  6083. aes->ctx.cfd = -1;
  6084. #endif
  6085. #if defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES)
  6086. XMEMSET(&aes->ctx, 0, sizeof(aes->ctx));
  6087. #endif
  6088. #ifdef HAVE_AESGCM
  6089. #ifdef OPENSSL_EXTRA
  6090. XMEMSET(aes->aadH, 0, sizeof(aes->aadH));
  6091. aes->aadLen = 0;
  6092. #endif
  6093. #endif
  6094. return ret;
  6095. }
  6096. #ifdef HAVE_PKCS11
  6097. int wc_AesInit_Id(Aes* aes, unsigned char* id, int len, void* heap, int devId)
  6098. {
  6099. int ret = 0;
  6100. if (aes == NULL)
  6101. ret = BAD_FUNC_ARG;
  6102. if (ret == 0 && (len < 0 || len > AES_MAX_ID_LEN))
  6103. ret = BUFFER_E;
  6104. if (ret == 0)
  6105. ret = wc_AesInit(aes, heap, devId);
  6106. if (ret == 0) {
  6107. XMEMCPY(aes->id, id, len);
  6108. aes->idLen = len;
  6109. }
  6110. return ret;
  6111. }
  6112. #endif
  6113. /* Free Aes from use with async hardware */
  6114. void wc_AesFree(Aes* aes)
  6115. {
  6116. if (aes == NULL)
  6117. return;
  6118. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
  6119. wolfAsync_DevCtxFree(&aes->asyncDev, WOLFSSL_ASYNC_MARKER_AES);
  6120. #endif /* WOLFSSL_ASYNC_CRYPT */
  6121. #if defined(WOLFSSL_AFALG) || defined(WOLFSSL_AFALG_XILINX_AES)
  6122. if (aes->rdFd > 0) { /* negative is error case */
  6123. close(aes->rdFd);
  6124. }
  6125. if (aes->alFd > 0) {
  6126. close(aes->alFd);
  6127. }
  6128. #endif /* WOLFSSL_AFALG */
  6129. #if defined(WOLFSSL_DEVCRYPTO) && \
  6130. (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))
  6131. wc_DevCryptoFree(&aes->ctx);
  6132. #endif
  6133. #if defined(WOLF_CRYPTO_CB) || (defined(WOLFSSL_DEVCRYPTO) && \
  6134. (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))) || \
  6135. (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES))
  6136. ForceZero((byte*)aes->devKey, AES_MAX_KEY_SIZE/WOLFSSL_BIT_SIZE);
  6137. #endif
  6138. }
  6139. int wc_AesGetKeySize(Aes* aes, word32* keySize)
  6140. {
  6141. int ret = 0;
  6142. if (aes == NULL || keySize == NULL) {
  6143. return BAD_FUNC_ARG;
  6144. }
  6145. #if defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES)
  6146. *keySize = aes->ctx.key.keySize;
  6147. return ret;
  6148. #endif
  6149. switch (aes->rounds) {
  6150. #ifdef WOLFSSL_AES_128
  6151. case 10:
  6152. *keySize = 16;
  6153. break;
  6154. #endif
  6155. #ifdef WOLFSSL_AES_192
  6156. case 12:
  6157. *keySize = 24;
  6158. break;
  6159. #endif
  6160. #ifdef WOLFSSL_AES_256
  6161. case 14:
  6162. *keySize = 32;
  6163. break;
  6164. #endif
  6165. default:
  6166. *keySize = 0;
  6167. ret = BAD_FUNC_ARG;
  6168. }
  6169. return ret;
  6170. }
  6171. #endif /* !WOLFSSL_TI_CRYPT */
  6172. #ifdef HAVE_AES_ECB
  6173. #if defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
  6174. /* implemented in wolfcrypt/src/port/caam/caam_aes.c */
  6175. #elif defined(WOLFSSL_AFALG)
  6176. /* implemented in wolfcrypt/src/port/af_alg/afalg_aes.c */
  6177. #elif defined(WOLFSSL_DEVCRYPTO_AES)
  6178. /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
  6179. #else
  6180. /* Software AES - ECB */
  6181. int wc_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  6182. {
  6183. word32 blocks = sz / AES_BLOCK_SIZE;
  6184. if ((in == NULL) || (out == NULL) || (aes == NULL))
  6185. return BAD_FUNC_ARG;
  6186. while (blocks>0) {
  6187. wc_AesEncryptDirect(aes, out, in);
  6188. out += AES_BLOCK_SIZE;
  6189. in += AES_BLOCK_SIZE;
  6190. sz -= AES_BLOCK_SIZE;
  6191. blocks--;
  6192. }
  6193. return 0;
  6194. }
  6195. int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  6196. {
  6197. word32 blocks = sz / AES_BLOCK_SIZE;
  6198. if ((in == NULL) || (out == NULL) || (aes == NULL))
  6199. return BAD_FUNC_ARG;
  6200. while (blocks>0) {
  6201. wc_AesDecryptDirect(aes, out, in);
  6202. out += AES_BLOCK_SIZE;
  6203. in += AES_BLOCK_SIZE;
  6204. sz -= AES_BLOCK_SIZE;
  6205. blocks--;
  6206. }
  6207. return 0;
  6208. }
  6209. #endif
  6210. #endif /* HAVE_AES_ECB */
  6211. #ifdef WOLFSSL_AES_CFB
  6212. /* CFB 128
  6213. *
  6214. * aes structure holding key to use for encryption
  6215. * out buffer to hold result of encryption (must be at least as large as input
  6216. * buffer)
  6217. * in buffer to encrypt
  6218. * sz size of input buffer
  6219. *
  6220. * returns 0 on success and negative error values on failure
  6221. */
  6222. /* Software AES - CFB Encrypt */
  6223. int wc_AesCfbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  6224. {
  6225. byte* tmp = NULL;
  6226. byte* reg = NULL;
  6227. if (aes == NULL || out == NULL || in == NULL) {
  6228. return BAD_FUNC_ARG;
  6229. }
  6230. if (aes->left && sz) {
  6231. reg = (byte*)aes->reg + AES_BLOCK_SIZE - aes->left;
  6232. }
  6233. /* consume any unused bytes left in aes->tmp */
  6234. tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
  6235. while (aes->left && sz) {
  6236. *(out++) = *(reg++) = *(in++) ^ *(tmp++);
  6237. aes->left--;
  6238. sz--;
  6239. }
  6240. while (sz >= AES_BLOCK_SIZE) {
  6241. wc_AesEncryptDirect(aes, out, (byte*)aes->reg);
  6242. xorbuf(out, in, AES_BLOCK_SIZE);
  6243. XMEMCPY(aes->reg, out, AES_BLOCK_SIZE);
  6244. out += AES_BLOCK_SIZE;
  6245. in += AES_BLOCK_SIZE;
  6246. sz -= AES_BLOCK_SIZE;
  6247. aes->left = 0;
  6248. }
  6249. /* encrypt left over data */
  6250. if (sz) {
  6251. wc_AesEncryptDirect(aes, (byte*)aes->tmp, (byte*)aes->reg);
  6252. aes->left = AES_BLOCK_SIZE;
  6253. tmp = (byte*)aes->tmp;
  6254. reg = (byte*)aes->reg;
  6255. while (sz--) {
  6256. *(out++) = *(reg++) = *(in++) ^ *(tmp++);
  6257. aes->left--;
  6258. }
  6259. }
  6260. return 0;
  6261. }
  6262. #ifdef HAVE_AES_DECRYPT
  6263. /* CFB 128
  6264. *
  6265. * aes structure holding key to use for decryption
  6266. * out buffer to hold result of decryption (must be at least as large as input
  6267. * buffer)
  6268. * in buffer to decrypt
  6269. * sz size of input buffer
  6270. *
  6271. * returns 0 on success and negative error values on failure
  6272. */
  6273. /* Software AES - CFB Decrypt */
  6274. int wc_AesCfbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
  6275. {
  6276. byte* tmp;
  6277. if (aes == NULL || out == NULL || in == NULL) {
  6278. return BAD_FUNC_ARG;
  6279. }
  6280. /* check if more input needs copied over to aes->reg */
  6281. if (aes->left && sz) {
  6282. int size = min(aes->left, sz);
  6283. XMEMCPY((byte*)aes->reg + AES_BLOCK_SIZE - aes->left, in, size);
  6284. }
  6285. /* consume any unused bytes left in aes->tmp */
  6286. tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
  6287. while (aes->left && sz) {
  6288. *(out++) = *(in++) ^ *(tmp++);
  6289. aes->left--;
  6290. sz--;
  6291. }
  6292. while (sz > AES_BLOCK_SIZE) {
  6293. wc_AesEncryptDirect(aes, out, (byte*)aes->reg);
  6294. xorbuf(out, in, AES_BLOCK_SIZE);
  6295. XMEMCPY(aes->reg, in, AES_BLOCK_SIZE);
  6296. out += AES_BLOCK_SIZE;
  6297. in += AES_BLOCK_SIZE;
  6298. sz -= AES_BLOCK_SIZE;
  6299. aes->left = 0;
  6300. }
  6301. /* decrypt left over data */
  6302. if (sz) {
  6303. wc_AesEncryptDirect(aes, (byte*)aes->tmp, (byte*)aes->reg);
  6304. XMEMCPY(aes->reg, in, sz);
  6305. aes->left = AES_BLOCK_SIZE;
  6306. tmp = (byte*)aes->tmp;
  6307. while (sz--) {
  6308. *(out++) = *(in++) ^ *(tmp++);
  6309. aes->left--;
  6310. }
  6311. }
  6312. return 0;
  6313. }
  6314. #endif /* HAVE_AES_DECRYPT */
  6315. #endif /* WOLFSSL_AES_CFB */
  6316. #ifdef HAVE_AES_KEYWRAP
  6317. /* Initialize key wrap counter with value */
  6318. static WC_INLINE void InitKeyWrapCounter(byte* inOutCtr, word32 value)
  6319. {
  6320. int i;
  6321. word32 bytes;
  6322. bytes = sizeof(word32);
  6323. for (i = 0; i < (int)sizeof(word32); i++) {
  6324. inOutCtr[i+sizeof(word32)] = (value >> ((bytes - 1) * 8)) & 0xFF;
  6325. bytes--;
  6326. }
  6327. }
  6328. /* Increment key wrap counter */
  6329. static WC_INLINE void IncrementKeyWrapCounter(byte* inOutCtr)
  6330. {
  6331. int i;
  6332. /* in network byte order so start at end and work back */
  6333. for (i = KEYWRAP_BLOCK_SIZE - 1; i >= 0; i--) {
  6334. if (++inOutCtr[i]) /* we're done unless we overflow */
  6335. return;
  6336. }
  6337. }
  6338. /* Decrement key wrap counter */
  6339. static WC_INLINE void DecrementKeyWrapCounter(byte* inOutCtr)
  6340. {
  6341. int i;
  6342. for (i = KEYWRAP_BLOCK_SIZE - 1; i >= 0; i--) {
  6343. if (--inOutCtr[i] != 0xFF) /* we're done unless we underflow */
  6344. return;
  6345. }
  6346. }
  6347. /* perform AES key wrap (RFC3394), return out sz on success, negative on err */
  6348. int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
  6349. byte* out, word32 outSz, const byte* iv)
  6350. {
  6351. Aes aes;
  6352. byte* r;
  6353. word32 i;
  6354. int ret, j;
  6355. byte t[KEYWRAP_BLOCK_SIZE];
  6356. byte tmp[AES_BLOCK_SIZE];
  6357. /* n must be at least 2, output size is n + 8 bytes */
  6358. if (key == NULL || in == NULL || inSz < 2 ||
  6359. out == NULL || outSz < (inSz + KEYWRAP_BLOCK_SIZE))
  6360. return BAD_FUNC_ARG;
  6361. /* input must be multiple of 64-bits */
  6362. if (inSz % KEYWRAP_BLOCK_SIZE != 0)
  6363. return BAD_FUNC_ARG;
  6364. /* user IV is optional */
  6365. if (iv == NULL) {
  6366. XMEMSET(tmp, 0xA6, KEYWRAP_BLOCK_SIZE);
  6367. } else {
  6368. XMEMCPY(tmp, iv, KEYWRAP_BLOCK_SIZE);
  6369. }
  6370. r = out + 8;
  6371. XMEMCPY(r, in, inSz);
  6372. XMEMSET(t, 0, sizeof(t));
  6373. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  6374. if (ret != 0)
  6375. return ret;
  6376. ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_ENCRYPTION);
  6377. if (ret != 0)
  6378. return ret;
  6379. for (j = 0; j <= 5; j++) {
  6380. for (i = 1; i <= inSz / KEYWRAP_BLOCK_SIZE; i++) {
  6381. /* load R[i] */
  6382. XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
  6383. wc_AesEncryptDirect(&aes, tmp, tmp);
  6384. /* calculate new A */
  6385. IncrementKeyWrapCounter(t);
  6386. xorbuf(tmp, t, KEYWRAP_BLOCK_SIZE);
  6387. /* save R[i] */
  6388. XMEMCPY(r, tmp + KEYWRAP_BLOCK_SIZE, KEYWRAP_BLOCK_SIZE);
  6389. r += KEYWRAP_BLOCK_SIZE;
  6390. }
  6391. r = out + KEYWRAP_BLOCK_SIZE;
  6392. }
  6393. /* C[0] = A */
  6394. XMEMCPY(out, tmp, KEYWRAP_BLOCK_SIZE);
  6395. wc_AesFree(&aes);
  6396. return inSz + KEYWRAP_BLOCK_SIZE;
  6397. }
  6398. int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
  6399. byte* out, word32 outSz, const byte* iv)
  6400. {
  6401. Aes aes;
  6402. byte* r;
  6403. word32 i, n;
  6404. int ret, j;
  6405. byte t[KEYWRAP_BLOCK_SIZE];
  6406. byte tmp[AES_BLOCK_SIZE];
  6407. const byte* expIv;
  6408. const byte defaultIV[] = {
  6409. 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6
  6410. };
  6411. (void)iv;
  6412. if (key == NULL || in == NULL || inSz < 3 ||
  6413. out == NULL || outSz < (inSz - KEYWRAP_BLOCK_SIZE))
  6414. return BAD_FUNC_ARG;
  6415. /* input must be multiple of 64-bits */
  6416. if (inSz % KEYWRAP_BLOCK_SIZE != 0)
  6417. return BAD_FUNC_ARG;
  6418. /* user IV optional */
  6419. if (iv != NULL) {
  6420. expIv = iv;
  6421. } else {
  6422. expIv = defaultIV;
  6423. }
  6424. /* A = C[0], R[i] = C[i] */
  6425. XMEMCPY(tmp, in, KEYWRAP_BLOCK_SIZE);
  6426. XMEMCPY(out, in + KEYWRAP_BLOCK_SIZE, inSz - KEYWRAP_BLOCK_SIZE);
  6427. XMEMSET(t, 0, sizeof(t));
  6428. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  6429. if (ret != 0)
  6430. return ret;
  6431. ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_DECRYPTION);
  6432. if (ret != 0)
  6433. return ret;
  6434. /* initialize counter to 6n */
  6435. n = (inSz - 1) / KEYWRAP_BLOCK_SIZE;
  6436. InitKeyWrapCounter(t, 6 * n);
  6437. for (j = 5; j >= 0; j--) {
  6438. for (i = n; i >= 1; i--) {
  6439. /* calculate A */
  6440. xorbuf(tmp, t, KEYWRAP_BLOCK_SIZE);
  6441. DecrementKeyWrapCounter(t);
  6442. /* load R[i], starting at end of R */
  6443. r = out + ((i - 1) * KEYWRAP_BLOCK_SIZE);
  6444. XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
  6445. wc_AesDecryptDirect(&aes, tmp, tmp);
  6446. /* save R[i] */
  6447. XMEMCPY(r, tmp + KEYWRAP_BLOCK_SIZE, KEYWRAP_BLOCK_SIZE);
  6448. }
  6449. }
  6450. wc_AesFree(&aes);
  6451. /* verify IV */
  6452. if (XMEMCMP(tmp, expIv, KEYWRAP_BLOCK_SIZE) != 0)
  6453. return BAD_KEYWRAP_IV_E;
  6454. return inSz - KEYWRAP_BLOCK_SIZE;
  6455. }
  6456. #endif /* HAVE_AES_KEYWRAP */
  6457. #ifdef WOLFSSL_AES_XTS
  6458. /* Galios Field to use */
  6459. #define GF_XTS 0x87
  6460. /* This is to help with setting keys to correct encrypt or decrypt type.
  6461. *
  6462. * tweak AES key for tweak in XTS
  6463. * aes AES key for encrypt/decrypt process
  6464. * key buffer holding aes key | tweak key
  6465. * len length of key buffer in bytes. Should be twice that of key size. i.e.
  6466. * 32 for a 16 byte key.
  6467. * dir direction, either AES_ENCRYPTION or AES_DECRYPTION
  6468. * heap heap hint to use for memory. Can be NULL
  6469. * devId id to use with async crypto. Can be 0
  6470. *
  6471. * Note: is up to user to call wc_AesFree on tweak and aes key when done.
  6472. *
  6473. * return 0 on success
  6474. */
  6475. int wc_AesXtsSetKey(XtsAes* aes, const byte* key, word32 len, int dir,
  6476. void* heap, int devId)
  6477. {
  6478. word32 keySz;
  6479. int ret = 0;
  6480. if (aes == NULL || key == NULL) {
  6481. return BAD_FUNC_ARG;
  6482. }
  6483. if ((ret = wc_AesInit(&aes->tweak, heap, devId)) != 0) {
  6484. return ret;
  6485. }
  6486. if ((ret = wc_AesInit(&aes->aes, heap, devId)) != 0) {
  6487. return ret;
  6488. }
  6489. keySz = len/2;
  6490. if (keySz != 16 && keySz != 32) {
  6491. WOLFSSL_MSG("Unsupported key size");
  6492. return WC_KEY_SIZE_E;
  6493. }
  6494. if ((ret = wc_AesSetKey(&aes->aes, key, keySz, NULL, dir)) == 0) {
  6495. ret = wc_AesSetKey(&aes->tweak, key + keySz, keySz, NULL,
  6496. AES_ENCRYPTION);
  6497. if (ret != 0) {
  6498. wc_AesFree(&aes->aes);
  6499. }
  6500. }
  6501. return ret;
  6502. }
  6503. /* This is used to free up resources used by Aes structs
  6504. *
  6505. * aes AES keys to free
  6506. *
  6507. * return 0 on success
  6508. */
  6509. int wc_AesXtsFree(XtsAes* aes)
  6510. {
  6511. if (aes != NULL) {
  6512. wc_AesFree(&aes->aes);
  6513. wc_AesFree(&aes->tweak);
  6514. }
  6515. return 0;
  6516. }
  6517. /* Same process as wc_AesXtsEncrypt but uses a word64 type as the tweak value
  6518. * instead of a byte array. This just converts the word64 to a byte array and
  6519. * calls wc_AesXtsEncrypt.
  6520. *
  6521. * aes AES keys to use for block encrypt/decrypt
  6522. * out output buffer to hold cipher text
  6523. * in input plain text buffer to encrypt
  6524. * sz size of both out and in buffers
  6525. * sector value to use for tweak
  6526. *
  6527. * returns 0 on success
  6528. */
  6529. int wc_AesXtsEncryptSector(XtsAes* aes, byte* out, const byte* in,
  6530. word32 sz, word64 sector)
  6531. {
  6532. byte* pt;
  6533. byte i[AES_BLOCK_SIZE];
  6534. XMEMSET(i, 0, AES_BLOCK_SIZE);
  6535. #ifdef BIG_ENDIAN_ORDER
  6536. sector = ByteReverseWord64(sector);
  6537. #endif
  6538. pt = (byte*)&sector;
  6539. XMEMCPY(i, pt, sizeof(word64));
  6540. return wc_AesXtsEncrypt(aes, out, in, sz, (const byte*)i, AES_BLOCK_SIZE);
  6541. }
  6542. /* Same process as wc_AesXtsDecrypt but uses a word64 type as the tweak value
  6543. * instead of a byte array. This just converts the word64 to a byte array.
  6544. *
  6545. * aes AES keys to use for block encrypt/decrypt
  6546. * out output buffer to hold plain text
  6547. * in input cipher text buffer to encrypt
  6548. * sz size of both out and in buffers
  6549. * sector value to use for tweak
  6550. *
  6551. * returns 0 on success
  6552. */
  6553. int wc_AesXtsDecryptSector(XtsAes* aes, byte* out, const byte* in, word32 sz,
  6554. word64 sector)
  6555. {
  6556. byte* pt;
  6557. byte i[AES_BLOCK_SIZE];
  6558. XMEMSET(i, 0, AES_BLOCK_SIZE);
  6559. #ifdef BIG_ENDIAN_ORDER
  6560. sector = ByteReverseWord64(sector);
  6561. #endif
  6562. pt = (byte*)&sector;
  6563. XMEMCPY(i, pt, sizeof(word64));
  6564. return wc_AesXtsDecrypt(aes, out, in, sz, (const byte*)i, AES_BLOCK_SIZE);
  6565. }
  6566. #ifdef HAVE_AES_ECB
  6567. /* helper function for encrypting / decrypting full buffer at once */
  6568. static int _AesXtsHelper(Aes* aes, byte* out, const byte* in, word32 sz, int dir)
  6569. {
  6570. word32 outSz = sz;
  6571. word32 totalSz = (sz / AES_BLOCK_SIZE) * AES_BLOCK_SIZE; /* total bytes */
  6572. byte* pt = out;
  6573. outSz -= AES_BLOCK_SIZE;
  6574. while (outSz > 0) {
  6575. word32 j;
  6576. byte carry = 0;
  6577. /* multiply by shift left and propagate carry */
  6578. for (j = 0; j < AES_BLOCK_SIZE && outSz > 0; j++, outSz--) {
  6579. byte tmpC;
  6580. tmpC = (pt[j] >> 7) & 0x01;
  6581. pt[j+AES_BLOCK_SIZE] = ((pt[j] << 1) + carry) & 0xFF;
  6582. carry = tmpC;
  6583. }
  6584. if (carry) {
  6585. pt[AES_BLOCK_SIZE] ^= GF_XTS;
  6586. }
  6587. pt += AES_BLOCK_SIZE;
  6588. }
  6589. xorbuf(out, in, totalSz);
  6590. if (dir == AES_ENCRYPTION) {
  6591. return wc_AesEcbEncrypt(aes, out, out, totalSz);
  6592. }
  6593. else {
  6594. return wc_AesEcbDecrypt(aes, out, out, totalSz);
  6595. }
  6596. }
  6597. #endif /* HAVE_AES_ECB */
  6598. /* AES with XTS mode. (XTS) XEX encryption with Tweak and cipher text Stealing.
  6599. *
  6600. * xaes AES keys to use for block encrypt/decrypt
  6601. * out output buffer to hold cipher text
  6602. * in input plain text buffer to encrypt
  6603. * sz size of both out and in buffers
  6604. * i value to use for tweak
  6605. * iSz size of i buffer, should always be AES_BLOCK_SIZE but having this input
  6606. * adds a sanity check on how the user calls the function.
  6607. *
  6608. * returns 0 on success
  6609. */
  6610. /* Software AES - XTS Encrypt */
  6611. int wc_AesXtsEncrypt(XtsAes* xaes, byte* out, const byte* in, word32 sz,
  6612. const byte* i, word32 iSz)
  6613. {
  6614. int ret = 0;
  6615. word32 blocks = (sz / AES_BLOCK_SIZE);
  6616. Aes *aes, *tweak;
  6617. if (xaes == NULL || out == NULL || in == NULL) {
  6618. return BAD_FUNC_ARG;
  6619. }
  6620. aes = &xaes->aes;
  6621. tweak = &xaes->tweak;
  6622. if (iSz < AES_BLOCK_SIZE) {
  6623. return BAD_FUNC_ARG;
  6624. }
  6625. if (blocks > 0) {
  6626. byte tmp[AES_BLOCK_SIZE];
  6627. XMEMSET(tmp, 0, AES_BLOCK_SIZE); /* set to 0's in case of improper AES
  6628. * key setup passed to encrypt direct*/
  6629. wc_AesEncryptDirect(tweak, tmp, i);
  6630. #ifdef HAVE_AES_ECB
  6631. /* encrypt all of buffer at once when possible */
  6632. if (in != out) { /* can not handle inline */
  6633. XMEMCPY(out, tmp, AES_BLOCK_SIZE);
  6634. if ((ret = _AesXtsHelper(aes, out, in, sz, AES_ENCRYPTION)) != 0) {
  6635. return ret;
  6636. }
  6637. }
  6638. #endif
  6639. while (blocks > 0) {
  6640. word32 j;
  6641. byte carry = 0;
  6642. byte buf[AES_BLOCK_SIZE];
  6643. #ifdef HAVE_AES_ECB
  6644. if (in == out) { /* check for if inline */
  6645. #endif
  6646. XMEMCPY(buf, in, AES_BLOCK_SIZE);
  6647. xorbuf(buf, tmp, AES_BLOCK_SIZE);
  6648. wc_AesEncryptDirect(aes, out, buf);
  6649. #ifdef HAVE_AES_ECB
  6650. }
  6651. #endif
  6652. xorbuf(out, tmp, AES_BLOCK_SIZE);
  6653. /* multiply by shift left and propagate carry */
  6654. for (j = 0; j < AES_BLOCK_SIZE; j++) {
  6655. byte tmpC;
  6656. tmpC = (tmp[j] >> 7) & 0x01;
  6657. tmp[j] = ((tmp[j] << 1) + carry) & 0xFF;
  6658. carry = tmpC;
  6659. }
  6660. if (carry) {
  6661. tmp[0] ^= GF_XTS;
  6662. }
  6663. in += AES_BLOCK_SIZE;
  6664. out += AES_BLOCK_SIZE;
  6665. sz -= AES_BLOCK_SIZE;
  6666. blocks--;
  6667. }
  6668. /* stealing operation of XTS to handle left overs */
  6669. if (sz > 0) {
  6670. byte buf[AES_BLOCK_SIZE];
  6671. XMEMCPY(buf, out - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6672. if (sz >= AES_BLOCK_SIZE) { /* extra sanity check before copy */
  6673. return BUFFER_E;
  6674. }
  6675. XMEMCPY(out, buf, sz);
  6676. XMEMCPY(buf, in, sz);
  6677. xorbuf(buf, tmp, AES_BLOCK_SIZE);
  6678. wc_AesEncryptDirect(aes, out - AES_BLOCK_SIZE, buf);
  6679. xorbuf(out - AES_BLOCK_SIZE, tmp, AES_BLOCK_SIZE);
  6680. }
  6681. }
  6682. else {
  6683. WOLFSSL_MSG("Plain text input too small for encryption");
  6684. return BAD_FUNC_ARG;
  6685. }
  6686. return ret;
  6687. }
  6688. /* Same process as encryption but Aes key is AES_DECRYPTION type.
  6689. *
  6690. * xaes AES keys to use for block encrypt/decrypt
  6691. * out output buffer to hold plain text
  6692. * in input cipher text buffer to decrypt
  6693. * sz size of both out and in buffers
  6694. * i value to use for tweak
  6695. * iSz size of i buffer, should always be AES_BLOCK_SIZE but having this input
  6696. * adds a sanity check on how the user calls the function.
  6697. *
  6698. * returns 0 on success
  6699. */
  6700. /* Software AES - XTS Decrypt */
  6701. int wc_AesXtsDecrypt(XtsAes* xaes, byte* out, const byte* in, word32 sz,
  6702. const byte* i, word32 iSz)
  6703. {
  6704. int ret = 0;
  6705. word32 blocks = (sz / AES_BLOCK_SIZE);
  6706. Aes *aes, *tweak;
  6707. if (xaes == NULL || out == NULL || in == NULL) {
  6708. return BAD_FUNC_ARG;
  6709. }
  6710. aes = &xaes->aes;
  6711. tweak = &xaes->tweak;
  6712. if (iSz < AES_BLOCK_SIZE) {
  6713. return BAD_FUNC_ARG;
  6714. }
  6715. if (blocks > 0) {
  6716. word32 j;
  6717. byte carry = 0;
  6718. byte tmp[AES_BLOCK_SIZE];
  6719. byte stl = (sz % AES_BLOCK_SIZE);
  6720. XMEMSET(tmp, 0, AES_BLOCK_SIZE); /* set to 0's in case of improper AES
  6721. * key setup passed to decrypt direct*/
  6722. wc_AesEncryptDirect(tweak, tmp, i);
  6723. /* if Stealing then break out of loop one block early to handle special
  6724. * case */
  6725. if (stl > 0) {
  6726. blocks--;
  6727. }
  6728. #ifdef HAVE_AES_ECB
  6729. /* decrypt all of buffer at once when possible */
  6730. if (in != out) { /* can not handle inline */
  6731. XMEMCPY(out, tmp, AES_BLOCK_SIZE);
  6732. if ((ret = _AesXtsHelper(aes, out, in, sz, AES_DECRYPTION)) != 0) {
  6733. return ret;
  6734. }
  6735. }
  6736. #endif
  6737. while (blocks > 0) {
  6738. byte buf[AES_BLOCK_SIZE];
  6739. #ifdef HAVE_AES_ECB
  6740. if (in == out) { /* check for if inline */
  6741. #endif
  6742. XMEMCPY(buf, in, AES_BLOCK_SIZE);
  6743. xorbuf(buf, tmp, AES_BLOCK_SIZE);
  6744. wc_AesDecryptDirect(aes, out, buf);
  6745. #ifdef HAVE_AES_ECB
  6746. }
  6747. #endif
  6748. xorbuf(out, tmp, AES_BLOCK_SIZE);
  6749. /* multiply by shift left and propagate carry */
  6750. for (j = 0; j < AES_BLOCK_SIZE; j++) {
  6751. byte tmpC;
  6752. tmpC = (tmp[j] >> 7) & 0x01;
  6753. tmp[j] = ((tmp[j] << 1) + carry) & 0xFF;
  6754. carry = tmpC;
  6755. }
  6756. if (carry) {
  6757. tmp[0] ^= GF_XTS;
  6758. }
  6759. carry = 0;
  6760. in += AES_BLOCK_SIZE;
  6761. out += AES_BLOCK_SIZE;
  6762. sz -= AES_BLOCK_SIZE;
  6763. blocks--;
  6764. }
  6765. /* stealing operation of XTS to handle left overs */
  6766. if (sz > 0) {
  6767. byte buf[AES_BLOCK_SIZE];
  6768. byte tmp2[AES_BLOCK_SIZE];
  6769. /* multiply by shift left and propagate carry */
  6770. for (j = 0; j < AES_BLOCK_SIZE; j++) {
  6771. byte tmpC;
  6772. tmpC = (tmp[j] >> 7) & 0x01;
  6773. tmp2[j] = ((tmp[j] << 1) + carry) & 0xFF;
  6774. carry = tmpC;
  6775. }
  6776. if (carry) {
  6777. tmp2[0] ^= GF_XTS;
  6778. }
  6779. XMEMCPY(buf, in, AES_BLOCK_SIZE);
  6780. xorbuf(buf, tmp2, AES_BLOCK_SIZE);
  6781. wc_AesDecryptDirect(aes, out, buf);
  6782. xorbuf(out, tmp2, AES_BLOCK_SIZE);
  6783. /* tmp2 holds partial | last */
  6784. XMEMCPY(tmp2, out, AES_BLOCK_SIZE);
  6785. in += AES_BLOCK_SIZE;
  6786. out += AES_BLOCK_SIZE;
  6787. sz -= AES_BLOCK_SIZE;
  6788. /* Make buffer with end of cipher text | last */
  6789. XMEMCPY(buf, tmp2, AES_BLOCK_SIZE);
  6790. if (sz >= AES_BLOCK_SIZE) { /* extra sanity check before copy */
  6791. return BUFFER_E;
  6792. }
  6793. XMEMCPY(buf, in, sz);
  6794. XMEMCPY(out, tmp2, sz);
  6795. xorbuf(buf, tmp, AES_BLOCK_SIZE);
  6796. wc_AesDecryptDirect(aes, tmp2, buf);
  6797. xorbuf(tmp2, tmp, AES_BLOCK_SIZE);
  6798. XMEMCPY(out - AES_BLOCK_SIZE, tmp2, AES_BLOCK_SIZE);
  6799. }
  6800. }
  6801. else {
  6802. WOLFSSL_MSG("Plain text input too small for encryption");
  6803. return BAD_FUNC_ARG;
  6804. }
  6805. return ret;
  6806. }
  6807. #endif /* WOLFSSL_AES_XTS */
  6808. #endif /* HAVE_FIPS */
  6809. #endif /* !NO_AES */