test_suite_psa_crypto.function 379 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228
  1. /* BEGIN_HEADER */
  2. #include <stdint.h>
  3. #include "mbedtls/asn1.h"
  4. #include "mbedtls/asn1write.h"
  5. #include "mbedtls/oid.h"
  6. #include "common.h"
  7. /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
  8. * uses mbedtls_ctr_drbg internally. */
  9. #include "mbedtls/ctr_drbg.h"
  10. #include "psa/crypto.h"
  11. #include "psa_crypto_slot_management.h"
  12. /* For psa_can_do_hash() */
  13. #include "psa_crypto_core.h"
  14. #include "test/asn1_helpers.h"
  15. #include "test/psa_crypto_helpers.h"
  16. #include "test/psa_exercise_key.h"
  17. #if defined(PSA_CRYPTO_DRIVER_TEST)
  18. #include "test/drivers/test_driver.h"
  19. #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
  20. #else
  21. #define TEST_DRIVER_LOCATION 0x7fffff
  22. #endif
  23. #include "mbedtls/legacy_or_psa.h"
  24. /* If this comes up, it's a bug in the test code or in the test data. */
  25. #define UNUSED 0xdeadbeef
  26. /* Assert that an operation is (not) active.
  27. * This serves as a proxy for checking if the operation is aborted. */
  28. #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
  29. #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
  30. #if defined(PSA_WANT_ALG_JPAKE)
  31. int ecjpake_operation_setup(psa_pake_operation_t *operation,
  32. psa_pake_cipher_suite_t *cipher_suite,
  33. psa_pake_role_t role,
  34. mbedtls_svc_key_id_t key,
  35. size_t key_available)
  36. {
  37. PSA_ASSERT(psa_pake_abort(operation));
  38. PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
  39. PSA_ASSERT(psa_pake_set_role(operation, role));
  40. if (key_available) {
  41. PSA_ASSERT(psa_pake_set_password_key(operation, key));
  42. }
  43. return 0;
  44. exit:
  45. return 1;
  46. }
  47. #endif
  48. /** An invalid export length that will never be set by psa_export_key(). */
  49. static const size_t INVALID_EXPORT_LENGTH = ~0U;
  50. /** Test if a buffer contains a constant byte value.
  51. *
  52. * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
  53. *
  54. * \param buffer Pointer to the beginning of the buffer.
  55. * \param c Expected value of every byte.
  56. * \param size Size of the buffer in bytes.
  57. *
  58. * \return 1 if the buffer is all-bits-zero.
  59. * \return 0 if there is at least one nonzero byte.
  60. */
  61. static int mem_is_char(void *buffer, unsigned char c, size_t size)
  62. {
  63. size_t i;
  64. for (i = 0; i < size; i++) {
  65. if (((unsigned char *) buffer)[i] != c) {
  66. return 0;
  67. }
  68. }
  69. return 1;
  70. }
  71. #if defined(MBEDTLS_ASN1_WRITE_C)
  72. /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
  73. static int asn1_write_10x(unsigned char **p,
  74. unsigned char *start,
  75. size_t bits,
  76. unsigned char x)
  77. {
  78. int ret;
  79. int len = bits / 8 + 1;
  80. if (bits == 0) {
  81. return MBEDTLS_ERR_ASN1_INVALID_DATA;
  82. }
  83. if (bits <= 8 && x >= 1 << (bits - 1)) {
  84. return MBEDTLS_ERR_ASN1_INVALID_DATA;
  85. }
  86. if (*p < start || *p - start < (ptrdiff_t) len) {
  87. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  88. }
  89. *p -= len;
  90. (*p)[len-1] = x;
  91. if (bits % 8 == 0) {
  92. (*p)[1] |= 1;
  93. } else {
  94. (*p)[0] |= 1 << (bits % 8);
  95. }
  96. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  97. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
  98. MBEDTLS_ASN1_INTEGER));
  99. return len;
  100. }
  101. static int construct_fake_rsa_key(unsigned char *buffer,
  102. size_t buffer_size,
  103. unsigned char **p,
  104. size_t bits,
  105. int keypair)
  106. {
  107. size_t half_bits = (bits + 1) / 2;
  108. int ret;
  109. int len = 0;
  110. /* Construct something that looks like a DER encoding of
  111. * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
  112. * RSAPrivateKey ::= SEQUENCE {
  113. * version Version,
  114. * modulus INTEGER, -- n
  115. * publicExponent INTEGER, -- e
  116. * privateExponent INTEGER, -- d
  117. * prime1 INTEGER, -- p
  118. * prime2 INTEGER, -- q
  119. * exponent1 INTEGER, -- d mod (p-1)
  120. * exponent2 INTEGER, -- d mod (q-1)
  121. * coefficient INTEGER, -- (inverse of q) mod p
  122. * otherPrimeInfos OtherPrimeInfos OPTIONAL
  123. * }
  124. * Or, for a public key, the same structure with only
  125. * version, modulus and publicExponent.
  126. */
  127. *p = buffer + buffer_size;
  128. if (keypair) {
  129. MBEDTLS_ASN1_CHK_ADD(len, /* pq */
  130. asn1_write_10x(p, buffer, half_bits, 1));
  131. MBEDTLS_ASN1_CHK_ADD(len, /* dq */
  132. asn1_write_10x(p, buffer, half_bits, 1));
  133. MBEDTLS_ASN1_CHK_ADD(len, /* dp */
  134. asn1_write_10x(p, buffer, half_bits, 1));
  135. MBEDTLS_ASN1_CHK_ADD(len, /* q */
  136. asn1_write_10x(p, buffer, half_bits, 1));
  137. MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
  138. asn1_write_10x(p, buffer, half_bits, 3));
  139. MBEDTLS_ASN1_CHK_ADD(len, /* d */
  140. asn1_write_10x(p, buffer, bits, 1));
  141. }
  142. MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
  143. asn1_write_10x(p, buffer, 17, 1));
  144. MBEDTLS_ASN1_CHK_ADD(len, /* n */
  145. asn1_write_10x(p, buffer, bits, 1));
  146. if (keypair) {
  147. MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
  148. mbedtls_asn1_write_int(p, buffer, 0));
  149. }
  150. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
  151. {
  152. const unsigned char tag =
  153. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
  154. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
  155. }
  156. return len;
  157. }
  158. #endif /* MBEDTLS_ASN1_WRITE_C */
  159. int exercise_mac_setup(psa_key_type_t key_type,
  160. const unsigned char *key_bytes,
  161. size_t key_length,
  162. psa_algorithm_t alg,
  163. psa_mac_operation_t *operation,
  164. psa_status_t *status)
  165. {
  166. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  167. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  168. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
  169. psa_set_key_algorithm(&attributes, alg);
  170. psa_set_key_type(&attributes, key_type);
  171. PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
  172. *status = psa_mac_sign_setup(operation, key, alg);
  173. /* Whether setup succeeded or failed, abort must succeed. */
  174. PSA_ASSERT(psa_mac_abort(operation));
  175. /* If setup failed, reproduce the failure, so that the caller can
  176. * test the resulting state of the operation object. */
  177. if (*status != PSA_SUCCESS) {
  178. TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
  179. }
  180. psa_destroy_key(key);
  181. return 1;
  182. exit:
  183. psa_destroy_key(key);
  184. return 0;
  185. }
  186. int exercise_cipher_setup(psa_key_type_t key_type,
  187. const unsigned char *key_bytes,
  188. size_t key_length,
  189. psa_algorithm_t alg,
  190. psa_cipher_operation_t *operation,
  191. psa_status_t *status)
  192. {
  193. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  194. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  195. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  196. psa_set_key_algorithm(&attributes, alg);
  197. psa_set_key_type(&attributes, key_type);
  198. PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
  199. *status = psa_cipher_encrypt_setup(operation, key, alg);
  200. /* Whether setup succeeded or failed, abort must succeed. */
  201. PSA_ASSERT(psa_cipher_abort(operation));
  202. /* If setup failed, reproduce the failure, so that the caller can
  203. * test the resulting state of the operation object. */
  204. if (*status != PSA_SUCCESS) {
  205. TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
  206. *status);
  207. }
  208. psa_destroy_key(key);
  209. return 1;
  210. exit:
  211. psa_destroy_key(key);
  212. return 0;
  213. }
  214. static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
  215. {
  216. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  217. mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
  218. uint8_t buffer[1];
  219. size_t length;
  220. int ok = 0;
  221. psa_set_key_id(&attributes, key_id);
  222. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  223. psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
  224. psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
  225. TEST_EQUAL(psa_get_key_attributes(key, &attributes),
  226. PSA_ERROR_INVALID_HANDLE);
  227. TEST_EQUAL(
  228. MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
  229. TEST_EQUAL(
  230. MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
  231. TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
  232. TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
  233. TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
  234. TEST_EQUAL(psa_get_key_type(&attributes), 0);
  235. TEST_EQUAL(psa_get_key_bits(&attributes), 0);
  236. TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
  237. PSA_ERROR_INVALID_HANDLE);
  238. TEST_EQUAL(psa_export_public_key(key,
  239. buffer, sizeof(buffer), &length),
  240. PSA_ERROR_INVALID_HANDLE);
  241. ok = 1;
  242. exit:
  243. /*
  244. * Key attributes may have been returned by psa_get_key_attributes()
  245. * thus reset them as required.
  246. */
  247. psa_reset_key_attributes(&attributes);
  248. return ok;
  249. }
  250. /* Assert that a key isn't reported as having a slot number. */
  251. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  252. #define ASSERT_NO_SLOT_NUMBER(attributes) \
  253. do \
  254. { \
  255. psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
  256. TEST_EQUAL(psa_get_key_slot_number( \
  257. attributes, \
  258. &ASSERT_NO_SLOT_NUMBER_slot_number), \
  259. PSA_ERROR_INVALID_ARGUMENT); \
  260. } \
  261. while (0)
  262. #else /* MBEDTLS_PSA_CRYPTO_SE_C */
  263. #define ASSERT_NO_SLOT_NUMBER(attributes) \
  264. ((void) 0)
  265. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  266. /* An overapproximation of the amount of storage needed for a key of the
  267. * given type and with the given content. The API doesn't make it easy
  268. * to find a good value for the size. The current implementation doesn't
  269. * care about the value anyway. */
  270. #define KEY_BITS_FROM_DATA(type, data) \
  271. (data)->len
  272. typedef enum {
  273. IMPORT_KEY = 0,
  274. GENERATE_KEY = 1,
  275. DERIVE_KEY = 2
  276. } generate_method;
  277. typedef enum {
  278. DO_NOT_SET_LENGTHS = 0,
  279. SET_LENGTHS_BEFORE_NONCE = 1,
  280. SET_LENGTHS_AFTER_NONCE = 2
  281. } set_lengths_method_t;
  282. typedef enum {
  283. USE_NULL_TAG = 0,
  284. USE_GIVEN_TAG = 1,
  285. } tag_usage_method_t;
  286. /*!
  287. * \brief Internal Function for AEAD multipart tests.
  288. * \param key_type_arg Type of key passed in
  289. * \param key_data The encryption / decryption key data
  290. * \param alg_arg The type of algorithm used
  291. * \param nonce Nonce data
  292. * \param additional_data Additional data
  293. * \param ad_part_len_arg If not -1, the length of chunks to
  294. * feed additional data in to be encrypted /
  295. * decrypted. If -1, no chunking.
  296. * \param input_data Data to encrypt / decrypt
  297. * \param data_part_len_arg If not -1, the length of chunks to feed
  298. * the data in to be encrypted / decrypted. If
  299. * -1, no chunking
  300. * \param set_lengths_method A member of the set_lengths_method_t enum is
  301. * expected here, this controls whether or not
  302. * to set lengths, and in what order with
  303. * respect to set nonce.
  304. * \param expected_output Expected output
  305. * \param is_encrypt If non-zero this is an encryption operation.
  306. * \param do_zero_parts If non-zero, interleave zero length chunks
  307. * with normal length chunks.
  308. * \return int Zero on failure, non-zero on success.
  309. */
  310. static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
  311. int alg_arg,
  312. data_t *nonce,
  313. data_t *additional_data,
  314. int ad_part_len_arg,
  315. data_t *input_data,
  316. int data_part_len_arg,
  317. set_lengths_method_t set_lengths_method,
  318. data_t *expected_output,
  319. int is_encrypt,
  320. int do_zero_parts)
  321. {
  322. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  323. psa_key_type_t key_type = key_type_arg;
  324. psa_algorithm_t alg = alg_arg;
  325. psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
  326. unsigned char *output_data = NULL;
  327. unsigned char *part_data = NULL;
  328. unsigned char *final_data = NULL;
  329. size_t data_true_size = 0;
  330. size_t part_data_size = 0;
  331. size_t output_size = 0;
  332. size_t final_output_size = 0;
  333. size_t output_length = 0;
  334. size_t key_bits = 0;
  335. size_t tag_length = 0;
  336. size_t part_offset = 0;
  337. size_t part_length = 0;
  338. size_t output_part_length = 0;
  339. size_t tag_size = 0;
  340. size_t ad_part_len = 0;
  341. size_t data_part_len = 0;
  342. uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
  343. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  344. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  345. int test_ok = 0;
  346. size_t part_count = 0;
  347. PSA_ASSERT(psa_crypto_init());
  348. if (is_encrypt) {
  349. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  350. } else {
  351. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
  352. }
  353. psa_set_key_algorithm(&attributes, alg);
  354. psa_set_key_type(&attributes, key_type);
  355. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  356. &key));
  357. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  358. key_bits = psa_get_key_bits(&attributes);
  359. tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
  360. if (is_encrypt) {
  361. /* Tag gets written at end of buffer. */
  362. output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
  363. (input_data->len +
  364. tag_length));
  365. data_true_size = input_data->len;
  366. } else {
  367. output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
  368. (input_data->len -
  369. tag_length));
  370. /* Do not want to attempt to decrypt tag. */
  371. data_true_size = input_data->len - tag_length;
  372. }
  373. ASSERT_ALLOC(output_data, output_size);
  374. if (is_encrypt) {
  375. final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
  376. TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
  377. } else {
  378. final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
  379. TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
  380. }
  381. ASSERT_ALLOC(final_data, final_output_size);
  382. if (is_encrypt) {
  383. status = psa_aead_encrypt_setup(&operation, key, alg);
  384. } else {
  385. status = psa_aead_decrypt_setup(&operation, key, alg);
  386. }
  387. /* If the operation is not supported, just skip and not fail in case the
  388. * encryption involves a common limitation of cryptography hardwares and
  389. * an alternative implementation. */
  390. if (status == PSA_ERROR_NOT_SUPPORTED) {
  391. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
  392. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
  393. }
  394. PSA_ASSERT(status);
  395. if (set_lengths_method == DO_NOT_SET_LENGTHS) {
  396. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  397. } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
  398. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  399. data_true_size));
  400. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  401. } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
  402. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  403. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  404. data_true_size));
  405. }
  406. if (ad_part_len_arg != -1) {
  407. /* Pass additional data in parts */
  408. ad_part_len = (size_t) ad_part_len_arg;
  409. for (part_offset = 0, part_count = 0;
  410. part_offset < additional_data->len;
  411. part_offset += part_length, part_count++) {
  412. if (do_zero_parts && (part_count & 0x01)) {
  413. part_length = 0;
  414. } else if (additional_data->len - part_offset < ad_part_len) {
  415. part_length = additional_data->len - part_offset;
  416. } else {
  417. part_length = ad_part_len;
  418. }
  419. PSA_ASSERT(psa_aead_update_ad(&operation,
  420. additional_data->x + part_offset,
  421. part_length));
  422. }
  423. } else {
  424. /* Pass additional data in one go. */
  425. PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
  426. additional_data->len));
  427. }
  428. if (data_part_len_arg != -1) {
  429. /* Pass data in parts */
  430. data_part_len = (size_t) data_part_len_arg;
  431. part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
  432. (size_t) data_part_len);
  433. ASSERT_ALLOC(part_data, part_data_size);
  434. for (part_offset = 0, part_count = 0;
  435. part_offset < data_true_size;
  436. part_offset += part_length, part_count++) {
  437. if (do_zero_parts && (part_count & 0x01)) {
  438. part_length = 0;
  439. } else if ((data_true_size - part_offset) < data_part_len) {
  440. part_length = (data_true_size - part_offset);
  441. } else {
  442. part_length = data_part_len;
  443. }
  444. PSA_ASSERT(psa_aead_update(&operation,
  445. (input_data->x + part_offset),
  446. part_length, part_data,
  447. part_data_size,
  448. &output_part_length));
  449. if (output_data && output_part_length) {
  450. memcpy((output_data + output_length), part_data,
  451. output_part_length);
  452. }
  453. output_length += output_part_length;
  454. }
  455. } else {
  456. /* Pass all data in one go. */
  457. PSA_ASSERT(psa_aead_update(&operation, input_data->x,
  458. data_true_size, output_data,
  459. output_size, &output_length));
  460. }
  461. if (is_encrypt) {
  462. PSA_ASSERT(psa_aead_finish(&operation, final_data,
  463. final_output_size,
  464. &output_part_length,
  465. tag_buffer, tag_length,
  466. &tag_size));
  467. } else {
  468. PSA_ASSERT(psa_aead_verify(&operation, final_data,
  469. final_output_size,
  470. &output_part_length,
  471. (input_data->x + data_true_size),
  472. tag_length));
  473. }
  474. if (output_data && output_part_length) {
  475. memcpy((output_data + output_length), final_data,
  476. output_part_length);
  477. }
  478. output_length += output_part_length;
  479. /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
  480. * should be exact.*/
  481. if (is_encrypt) {
  482. TEST_EQUAL(tag_length, tag_size);
  483. if (output_data && tag_length) {
  484. memcpy((output_data + output_length), tag_buffer,
  485. tag_length);
  486. }
  487. output_length += tag_length;
  488. TEST_EQUAL(output_length,
  489. PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
  490. input_data->len));
  491. TEST_LE_U(output_length,
  492. PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
  493. } else {
  494. TEST_EQUAL(output_length,
  495. PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
  496. input_data->len));
  497. TEST_LE_U(output_length,
  498. PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
  499. }
  500. ASSERT_COMPARE(expected_output->x, expected_output->len,
  501. output_data, output_length);
  502. test_ok = 1;
  503. exit:
  504. psa_destroy_key(key);
  505. psa_aead_abort(&operation);
  506. mbedtls_free(output_data);
  507. mbedtls_free(part_data);
  508. mbedtls_free(final_data);
  509. PSA_DONE();
  510. return test_ok;
  511. }
  512. /*!
  513. * \brief Internal Function for MAC multipart tests.
  514. * \param key_type_arg Type of key passed in
  515. * \param key_data The encryption / decryption key data
  516. * \param alg_arg The type of algorithm used
  517. * \param input_data Data to encrypt / decrypt
  518. * \param data_part_len_arg If not -1, the length of chunks to feed
  519. * the data in to be encrypted / decrypted. If
  520. * -1, no chunking
  521. * \param expected_output Expected output
  522. * \param is_verify If non-zero this is a verify operation.
  523. * \param do_zero_parts If non-zero, interleave zero length chunks
  524. * with normal length chunks.
  525. * \return int Zero on failure, non-zero on success.
  526. */
  527. static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
  528. int alg_arg,
  529. data_t *input_data,
  530. int data_part_len_arg,
  531. data_t *expected_output,
  532. int is_verify,
  533. int do_zero_parts)
  534. {
  535. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  536. psa_key_type_t key_type = key_type_arg;
  537. psa_algorithm_t alg = alg_arg;
  538. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  539. unsigned char mac[PSA_MAC_MAX_SIZE];
  540. size_t part_offset = 0;
  541. size_t part_length = 0;
  542. size_t data_part_len = 0;
  543. size_t mac_len = 0;
  544. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  545. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  546. int test_ok = 0;
  547. size_t part_count = 0;
  548. PSA_INIT();
  549. if (is_verify) {
  550. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
  551. } else {
  552. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
  553. }
  554. psa_set_key_algorithm(&attributes, alg);
  555. psa_set_key_type(&attributes, key_type);
  556. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  557. &key));
  558. if (is_verify) {
  559. status = psa_mac_verify_setup(&operation, key, alg);
  560. } else {
  561. status = psa_mac_sign_setup(&operation, key, alg);
  562. }
  563. PSA_ASSERT(status);
  564. if (data_part_len_arg != -1) {
  565. /* Pass data in parts */
  566. data_part_len = (size_t) data_part_len_arg;
  567. for (part_offset = 0, part_count = 0;
  568. part_offset < input_data->len;
  569. part_offset += part_length, part_count++) {
  570. if (do_zero_parts && (part_count & 0x01)) {
  571. part_length = 0;
  572. } else if ((input_data->len - part_offset) < data_part_len) {
  573. part_length = (input_data->len - part_offset);
  574. } else {
  575. part_length = data_part_len;
  576. }
  577. PSA_ASSERT(psa_mac_update(&operation,
  578. (input_data->x + part_offset),
  579. part_length));
  580. }
  581. } else {
  582. /* Pass all data in one go. */
  583. PSA_ASSERT(psa_mac_update(&operation, input_data->x,
  584. input_data->len));
  585. }
  586. if (is_verify) {
  587. PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
  588. expected_output->len));
  589. } else {
  590. PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
  591. PSA_MAC_MAX_SIZE, &mac_len));
  592. ASSERT_COMPARE(expected_output->x, expected_output->len,
  593. mac, mac_len);
  594. }
  595. test_ok = 1;
  596. exit:
  597. psa_destroy_key(key);
  598. psa_mac_abort(&operation);
  599. PSA_DONE();
  600. return test_ok;
  601. }
  602. #if defined(PSA_WANT_ALG_JPAKE)
  603. static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
  604. psa_pake_operation_t *server,
  605. psa_pake_operation_t *client,
  606. int client_input_first,
  607. int round, int inject_error)
  608. {
  609. unsigned char *buffer0 = NULL, *buffer1 = NULL;
  610. size_t buffer_length = (
  611. PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
  612. PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
  613. PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
  614. /* The output should be exactly this size according to the spec */
  615. const size_t expected_size_key_share =
  616. PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
  617. /* The output should be exactly this size according to the spec */
  618. const size_t expected_size_zk_public =
  619. PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
  620. /* The output can be smaller: the spec allows stripping leading zeroes */
  621. const size_t max_expected_size_zk_proof =
  622. PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
  623. size_t buffer0_off = 0;
  624. size_t buffer1_off = 0;
  625. size_t s_g1_len, s_g2_len, s_a_len;
  626. size_t s_g1_off, s_g2_off, s_a_off;
  627. size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
  628. size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
  629. size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
  630. size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
  631. size_t c_g1_len, c_g2_len, c_a_len;
  632. size_t c_g1_off, c_g2_off, c_a_off;
  633. size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
  634. size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
  635. size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
  636. size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
  637. psa_status_t expected_status = PSA_SUCCESS;
  638. psa_status_t status;
  639. ASSERT_ALLOC(buffer0, buffer_length);
  640. ASSERT_ALLOC(buffer1, buffer_length);
  641. switch (round) {
  642. case 1:
  643. /* Server first round Output */
  644. PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
  645. buffer0 + buffer0_off,
  646. 512 - buffer0_off, &s_g1_len));
  647. TEST_EQUAL(s_g1_len, expected_size_key_share);
  648. s_g1_off = buffer0_off;
  649. buffer0_off += s_g1_len;
  650. PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
  651. buffer0 + buffer0_off,
  652. 512 - buffer0_off, &s_x1_pk_len));
  653. TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
  654. s_x1_pk_off = buffer0_off;
  655. buffer0_off += s_x1_pk_len;
  656. PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
  657. buffer0 + buffer0_off,
  658. 512 - buffer0_off, &s_x1_pr_len));
  659. TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
  660. s_x1_pr_off = buffer0_off;
  661. buffer0_off += s_x1_pr_len;
  662. PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
  663. buffer0 + buffer0_off,
  664. 512 - buffer0_off, &s_g2_len));
  665. TEST_EQUAL(s_g2_len, expected_size_key_share);
  666. s_g2_off = buffer0_off;
  667. buffer0_off += s_g2_len;
  668. PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
  669. buffer0 + buffer0_off,
  670. 512 - buffer0_off, &s_x2_pk_len));
  671. TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
  672. s_x2_pk_off = buffer0_off;
  673. buffer0_off += s_x2_pk_len;
  674. PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
  675. buffer0 + buffer0_off,
  676. 512 - buffer0_off, &s_x2_pr_len));
  677. TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
  678. s_x2_pr_off = buffer0_off;
  679. buffer0_off += s_x2_pr_len;
  680. if (inject_error == 1) {
  681. buffer0[s_x1_pr_off + 8] ^= 1;
  682. buffer0[s_x2_pr_off + 7] ^= 1;
  683. expected_status = PSA_ERROR_DATA_INVALID;
  684. }
  685. /*
  686. * When injecting errors in inputs, the implementation is
  687. * free to detect it right away of with a delay.
  688. * This permits delaying the error until the end of the input
  689. * sequence, if no error appears then, this will be treated
  690. * as an error.
  691. */
  692. if (client_input_first == 1) {
  693. /* Client first round Input */
  694. status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
  695. buffer0 + s_g1_off, s_g1_len);
  696. if (inject_error == 1 && status != PSA_SUCCESS) {
  697. TEST_EQUAL(status, expected_status);
  698. break;
  699. } else {
  700. TEST_EQUAL(status, PSA_SUCCESS);
  701. }
  702. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
  703. buffer0 + s_x1_pk_off,
  704. s_x1_pk_len);
  705. if (inject_error == 1 && status != PSA_SUCCESS) {
  706. TEST_EQUAL(status, expected_status);
  707. break;
  708. } else {
  709. TEST_EQUAL(status, PSA_SUCCESS);
  710. }
  711. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
  712. buffer0 + s_x1_pr_off,
  713. s_x1_pr_len);
  714. if (inject_error == 1 && status != PSA_SUCCESS) {
  715. TEST_EQUAL(status, expected_status);
  716. break;
  717. } else {
  718. TEST_EQUAL(status, PSA_SUCCESS);
  719. }
  720. status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
  721. buffer0 + s_g2_off,
  722. s_g2_len);
  723. if (inject_error == 1 && status != PSA_SUCCESS) {
  724. TEST_EQUAL(status, expected_status);
  725. break;
  726. } else {
  727. TEST_EQUAL(status, PSA_SUCCESS);
  728. }
  729. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
  730. buffer0 + s_x2_pk_off,
  731. s_x2_pk_len);
  732. if (inject_error == 1 && status != PSA_SUCCESS) {
  733. TEST_EQUAL(status, expected_status);
  734. break;
  735. } else {
  736. TEST_EQUAL(status, PSA_SUCCESS);
  737. }
  738. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
  739. buffer0 + s_x2_pr_off,
  740. s_x2_pr_len);
  741. if (inject_error == 1 && status != PSA_SUCCESS) {
  742. TEST_EQUAL(status, expected_status);
  743. break;
  744. } else {
  745. TEST_EQUAL(status, PSA_SUCCESS);
  746. }
  747. /* Error didn't trigger, make test fail */
  748. if (inject_error == 1) {
  749. TEST_ASSERT(
  750. !"One of the last psa_pake_input() calls should have returned the expected error.");
  751. }
  752. }
  753. /* Client first round Output */
  754. PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
  755. buffer1 + buffer1_off,
  756. 512 - buffer1_off, &c_g1_len));
  757. TEST_EQUAL(c_g1_len, expected_size_key_share);
  758. c_g1_off = buffer1_off;
  759. buffer1_off += c_g1_len;
  760. PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
  761. buffer1 + buffer1_off,
  762. 512 - buffer1_off, &c_x1_pk_len));
  763. TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
  764. c_x1_pk_off = buffer1_off;
  765. buffer1_off += c_x1_pk_len;
  766. PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
  767. buffer1 + buffer1_off,
  768. 512 - buffer1_off, &c_x1_pr_len));
  769. TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
  770. c_x1_pr_off = buffer1_off;
  771. buffer1_off += c_x1_pr_len;
  772. PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
  773. buffer1 + buffer1_off,
  774. 512 - buffer1_off, &c_g2_len));
  775. TEST_EQUAL(c_g2_len, expected_size_key_share);
  776. c_g2_off = buffer1_off;
  777. buffer1_off += c_g2_len;
  778. PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
  779. buffer1 + buffer1_off,
  780. 512 - buffer1_off, &c_x2_pk_len));
  781. TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
  782. c_x2_pk_off = buffer1_off;
  783. buffer1_off += c_x2_pk_len;
  784. PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
  785. buffer1 + buffer1_off,
  786. 512 - buffer1_off, &c_x2_pr_len));
  787. TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
  788. c_x2_pr_off = buffer1_off;
  789. buffer1_off += c_x2_pr_len;
  790. if (client_input_first == 0) {
  791. /* Client first round Input */
  792. status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
  793. buffer0 + s_g1_off, s_g1_len);
  794. if (inject_error == 1 && status != PSA_SUCCESS) {
  795. TEST_EQUAL(status, expected_status);
  796. break;
  797. } else {
  798. TEST_EQUAL(status, PSA_SUCCESS);
  799. }
  800. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
  801. buffer0 + s_x1_pk_off,
  802. s_x1_pk_len);
  803. if (inject_error == 1 && status != PSA_SUCCESS) {
  804. TEST_EQUAL(status, expected_status);
  805. break;
  806. } else {
  807. TEST_EQUAL(status, PSA_SUCCESS);
  808. }
  809. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
  810. buffer0 + s_x1_pr_off,
  811. s_x1_pr_len);
  812. if (inject_error == 1 && status != PSA_SUCCESS) {
  813. TEST_EQUAL(status, expected_status);
  814. break;
  815. } else {
  816. TEST_EQUAL(status, PSA_SUCCESS);
  817. }
  818. status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
  819. buffer0 + s_g2_off,
  820. s_g2_len);
  821. if (inject_error == 1 && status != PSA_SUCCESS) {
  822. TEST_EQUAL(status, expected_status);
  823. break;
  824. } else {
  825. TEST_EQUAL(status, PSA_SUCCESS);
  826. }
  827. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
  828. buffer0 + s_x2_pk_off,
  829. s_x2_pk_len);
  830. if (inject_error == 1 && status != PSA_SUCCESS) {
  831. TEST_EQUAL(status, expected_status);
  832. break;
  833. } else {
  834. TEST_EQUAL(status, PSA_SUCCESS);
  835. }
  836. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
  837. buffer0 + s_x2_pr_off,
  838. s_x2_pr_len);
  839. if (inject_error == 1 && status != PSA_SUCCESS) {
  840. TEST_EQUAL(status, expected_status);
  841. break;
  842. } else {
  843. TEST_EQUAL(status, PSA_SUCCESS);
  844. }
  845. /* Error didn't trigger, make test fail */
  846. if (inject_error == 1) {
  847. TEST_ASSERT(
  848. !"One of the last psa_pake_input() calls should have returned the expected error.");
  849. }
  850. }
  851. if (inject_error == 2) {
  852. buffer1[c_x1_pr_off + 12] ^= 1;
  853. buffer1[c_x2_pr_off + 7] ^= 1;
  854. expected_status = PSA_ERROR_DATA_INVALID;
  855. }
  856. /* Server first round Input */
  857. status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
  858. buffer1 + c_g1_off, c_g1_len);
  859. if (inject_error == 2 && status != PSA_SUCCESS) {
  860. TEST_EQUAL(status, expected_status);
  861. break;
  862. } else {
  863. TEST_EQUAL(status, PSA_SUCCESS);
  864. }
  865. status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
  866. buffer1 + c_x1_pk_off, c_x1_pk_len);
  867. if (inject_error == 2 && status != PSA_SUCCESS) {
  868. TEST_EQUAL(status, expected_status);
  869. break;
  870. } else {
  871. TEST_EQUAL(status, PSA_SUCCESS);
  872. }
  873. status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
  874. buffer1 + c_x1_pr_off, c_x1_pr_len);
  875. if (inject_error == 2 && status != PSA_SUCCESS) {
  876. TEST_EQUAL(status, expected_status);
  877. break;
  878. } else {
  879. TEST_EQUAL(status, PSA_SUCCESS);
  880. }
  881. status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
  882. buffer1 + c_g2_off, c_g2_len);
  883. if (inject_error == 2 && status != PSA_SUCCESS) {
  884. TEST_EQUAL(status, expected_status);
  885. break;
  886. } else {
  887. TEST_EQUAL(status, PSA_SUCCESS);
  888. }
  889. status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
  890. buffer1 + c_x2_pk_off, c_x2_pk_len);
  891. if (inject_error == 2 && status != PSA_SUCCESS) {
  892. TEST_EQUAL(status, expected_status);
  893. break;
  894. } else {
  895. TEST_EQUAL(status, PSA_SUCCESS);
  896. }
  897. status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
  898. buffer1 + c_x2_pr_off, c_x2_pr_len);
  899. if (inject_error == 2 && status != PSA_SUCCESS) {
  900. TEST_EQUAL(status, expected_status);
  901. break;
  902. } else {
  903. TEST_EQUAL(status, PSA_SUCCESS);
  904. }
  905. /* Error didn't trigger, make test fail */
  906. if (inject_error == 2) {
  907. TEST_ASSERT(
  908. !"One of the last psa_pake_input() calls should have returned the expected error.");
  909. }
  910. break;
  911. case 2:
  912. /* Server second round Output */
  913. buffer0_off = 0;
  914. PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
  915. buffer0 + buffer0_off,
  916. 512 - buffer0_off, &s_a_len));
  917. TEST_EQUAL(s_a_len, expected_size_key_share);
  918. s_a_off = buffer0_off;
  919. buffer0_off += s_a_len;
  920. PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
  921. buffer0 + buffer0_off,
  922. 512 - buffer0_off, &s_x2s_pk_len));
  923. TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
  924. s_x2s_pk_off = buffer0_off;
  925. buffer0_off += s_x2s_pk_len;
  926. PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
  927. buffer0 + buffer0_off,
  928. 512 - buffer0_off, &s_x2s_pr_len));
  929. TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
  930. s_x2s_pr_off = buffer0_off;
  931. buffer0_off += s_x2s_pr_len;
  932. if (inject_error == 3) {
  933. buffer0[s_x2s_pk_off + 12] += 0x33;
  934. expected_status = PSA_ERROR_DATA_INVALID;
  935. }
  936. if (client_input_first == 1) {
  937. /* Client second round Input */
  938. status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
  939. buffer0 + s_a_off, s_a_len);
  940. if (inject_error == 3 && status != PSA_SUCCESS) {
  941. TEST_EQUAL(status, expected_status);
  942. break;
  943. } else {
  944. TEST_EQUAL(status, PSA_SUCCESS);
  945. }
  946. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
  947. buffer0 + s_x2s_pk_off,
  948. s_x2s_pk_len);
  949. if (inject_error == 3 && status != PSA_SUCCESS) {
  950. TEST_EQUAL(status, expected_status);
  951. break;
  952. } else {
  953. TEST_EQUAL(status, PSA_SUCCESS);
  954. }
  955. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
  956. buffer0 + s_x2s_pr_off,
  957. s_x2s_pr_len);
  958. if (inject_error == 3 && status != PSA_SUCCESS) {
  959. TEST_EQUAL(status, expected_status);
  960. break;
  961. } else {
  962. TEST_EQUAL(status, PSA_SUCCESS);
  963. }
  964. /* Error didn't trigger, make test fail */
  965. if (inject_error == 3) {
  966. TEST_ASSERT(
  967. !"One of the last psa_pake_input() calls should have returned the expected error.");
  968. }
  969. }
  970. /* Client second round Output */
  971. buffer1_off = 0;
  972. PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
  973. buffer1 + buffer1_off,
  974. 512 - buffer1_off, &c_a_len));
  975. TEST_EQUAL(c_a_len, expected_size_key_share);
  976. c_a_off = buffer1_off;
  977. buffer1_off += c_a_len;
  978. PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
  979. buffer1 + buffer1_off,
  980. 512 - buffer1_off, &c_x2s_pk_len));
  981. TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
  982. c_x2s_pk_off = buffer1_off;
  983. buffer1_off += c_x2s_pk_len;
  984. PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
  985. buffer1 + buffer1_off,
  986. 512 - buffer1_off, &c_x2s_pr_len));
  987. TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
  988. c_x2s_pr_off = buffer1_off;
  989. buffer1_off += c_x2s_pr_len;
  990. if (client_input_first == 0) {
  991. /* Client second round Input */
  992. status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
  993. buffer0 + s_a_off, s_a_len);
  994. if (inject_error == 3 && status != PSA_SUCCESS) {
  995. TEST_EQUAL(status, expected_status);
  996. break;
  997. } else {
  998. TEST_EQUAL(status, PSA_SUCCESS);
  999. }
  1000. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
  1001. buffer0 + s_x2s_pk_off,
  1002. s_x2s_pk_len);
  1003. if (inject_error == 3 && status != PSA_SUCCESS) {
  1004. TEST_EQUAL(status, expected_status);
  1005. break;
  1006. } else {
  1007. TEST_EQUAL(status, PSA_SUCCESS);
  1008. }
  1009. status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
  1010. buffer0 + s_x2s_pr_off,
  1011. s_x2s_pr_len);
  1012. if (inject_error == 3 && status != PSA_SUCCESS) {
  1013. TEST_EQUAL(status, expected_status);
  1014. break;
  1015. } else {
  1016. TEST_EQUAL(status, PSA_SUCCESS);
  1017. }
  1018. /* Error didn't trigger, make test fail */
  1019. if (inject_error == 3) {
  1020. TEST_ASSERT(
  1021. !"One of the last psa_pake_input() calls should have returned the expected error.");
  1022. }
  1023. }
  1024. if (inject_error == 4) {
  1025. buffer1[c_x2s_pk_off + 7] += 0x28;
  1026. expected_status = PSA_ERROR_DATA_INVALID;
  1027. }
  1028. /* Server second round Input */
  1029. status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
  1030. buffer1 + c_a_off, c_a_len);
  1031. if (inject_error == 4 && status != PSA_SUCCESS) {
  1032. TEST_EQUAL(status, expected_status);
  1033. break;
  1034. } else {
  1035. TEST_EQUAL(status, PSA_SUCCESS);
  1036. }
  1037. status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
  1038. buffer1 + c_x2s_pk_off, c_x2s_pk_len);
  1039. if (inject_error == 4 && status != PSA_SUCCESS) {
  1040. TEST_EQUAL(status, expected_status);
  1041. break;
  1042. } else {
  1043. TEST_EQUAL(status, PSA_SUCCESS);
  1044. }
  1045. status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
  1046. buffer1 + c_x2s_pr_off, c_x2s_pr_len);
  1047. if (inject_error == 4 && status != PSA_SUCCESS) {
  1048. TEST_EQUAL(status, expected_status);
  1049. break;
  1050. } else {
  1051. TEST_EQUAL(status, PSA_SUCCESS);
  1052. }
  1053. /* Error didn't trigger, make test fail */
  1054. if (inject_error == 4) {
  1055. TEST_ASSERT(
  1056. !"One of the last psa_pake_input() calls should have returned the expected error.");
  1057. }
  1058. break;
  1059. }
  1060. exit:
  1061. mbedtls_free(buffer0);
  1062. mbedtls_free(buffer1);
  1063. }
  1064. #endif /* PSA_WANT_ALG_JPAKE */
  1065. typedef enum {
  1066. INJECT_ERR_NONE = 0,
  1067. INJECT_ERR_UNINITIALIZED_ACCESS,
  1068. INJECT_ERR_DUPLICATE_SETUP,
  1069. INJECT_ERR_INVALID_USER,
  1070. INJECT_ERR_INVALID_PEER,
  1071. INJECT_ERR_SET_USER,
  1072. INJECT_ERR_SET_PEER,
  1073. INJECT_EMPTY_IO_BUFFER,
  1074. INJECT_UNKNOWN_STEP,
  1075. INJECT_INVALID_FIRST_STEP,
  1076. INJECT_WRONG_BUFFER_SIZE,
  1077. INJECT_VALID_OPERATION_AFTER_FAILURE,
  1078. INJECT_ANTICIPATE_KEY_DERIVATION_1,
  1079. INJECT_ANTICIPATE_KEY_DERIVATION_2,
  1080. } ecjpake_injected_failure_t;
  1081. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1082. static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
  1083. psa_status_t expected_status,
  1084. size_t *min_completes,
  1085. size_t *max_completes)
  1086. {
  1087. /* This is slightly contrived, but we only really know that with a minimum
  1088. value of max_ops that a successful operation should take more than one op
  1089. to complete, and likewise that with a max_ops of
  1090. PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
  1091. if (max_ops == 0 || max_ops == 1) {
  1092. if (expected_status == PSA_SUCCESS) {
  1093. *min_completes = 2;
  1094. } else {
  1095. *min_completes = 1;
  1096. }
  1097. *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
  1098. } else {
  1099. *min_completes = 1;
  1100. *max_completes = 1;
  1101. }
  1102. }
  1103. #endif /* MBEDTLS_ECP_RESTARTABLE */
  1104. /* END_HEADER */
  1105. /* BEGIN_DEPENDENCIES
  1106. * depends_on:MBEDTLS_PSA_CRYPTO_C
  1107. * END_DEPENDENCIES
  1108. */
  1109. /* BEGIN_CASE */
  1110. void psa_can_do_hash()
  1111. {
  1112. /* We can't test that this is specific to drivers until partial init has
  1113. * been implemented, but we can at least test before/after full init. */
  1114. TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
  1115. PSA_INIT();
  1116. TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
  1117. PSA_DONE();
  1118. }
  1119. /* END_CASE */
  1120. /* BEGIN_CASE */
  1121. void static_checks()
  1122. {
  1123. size_t max_truncated_mac_size =
  1124. PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
  1125. /* Check that the length for a truncated MAC always fits in the algorithm
  1126. * encoding. The shifted mask is the maximum truncated value. The
  1127. * untruncated algorithm may be one byte larger. */
  1128. TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
  1129. }
  1130. /* END_CASE */
  1131. /* BEGIN_CASE */
  1132. void import_with_policy(int type_arg,
  1133. int usage_arg, int alg_arg,
  1134. int expected_status_arg)
  1135. {
  1136. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1137. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  1138. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1139. psa_key_type_t type = type_arg;
  1140. psa_key_usage_t usage = usage_arg;
  1141. psa_algorithm_t alg = alg_arg;
  1142. psa_status_t expected_status = expected_status_arg;
  1143. const uint8_t key_material[16] = { 0 };
  1144. psa_status_t status;
  1145. PSA_ASSERT(psa_crypto_init());
  1146. psa_set_key_type(&attributes, type);
  1147. psa_set_key_usage_flags(&attributes, usage);
  1148. psa_set_key_algorithm(&attributes, alg);
  1149. status = psa_import_key(&attributes,
  1150. key_material, sizeof(key_material),
  1151. &key);
  1152. TEST_EQUAL(status, expected_status);
  1153. if (status != PSA_SUCCESS) {
  1154. goto exit;
  1155. }
  1156. PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
  1157. TEST_EQUAL(psa_get_key_type(&got_attributes), type);
  1158. TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
  1159. mbedtls_test_update_key_usage_flags(usage));
  1160. TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
  1161. ASSERT_NO_SLOT_NUMBER(&got_attributes);
  1162. PSA_ASSERT(psa_destroy_key(key));
  1163. test_operations_on_invalid_key(key);
  1164. exit:
  1165. /*
  1166. * Key attributes may have been returned by psa_get_key_attributes()
  1167. * thus reset them as required.
  1168. */
  1169. psa_reset_key_attributes(&got_attributes);
  1170. psa_destroy_key(key);
  1171. PSA_DONE();
  1172. }
  1173. /* END_CASE */
  1174. /* BEGIN_CASE */
  1175. void import_with_data(data_t *data, int type_arg,
  1176. int attr_bits_arg,
  1177. int expected_status_arg)
  1178. {
  1179. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1180. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  1181. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1182. psa_key_type_t type = type_arg;
  1183. size_t attr_bits = attr_bits_arg;
  1184. psa_status_t expected_status = expected_status_arg;
  1185. psa_status_t status;
  1186. PSA_ASSERT(psa_crypto_init());
  1187. psa_set_key_type(&attributes, type);
  1188. psa_set_key_bits(&attributes, attr_bits);
  1189. status = psa_import_key(&attributes, data->x, data->len, &key);
  1190. TEST_EQUAL(status, expected_status);
  1191. if (status != PSA_SUCCESS) {
  1192. goto exit;
  1193. }
  1194. PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
  1195. TEST_EQUAL(psa_get_key_type(&got_attributes), type);
  1196. if (attr_bits != 0) {
  1197. TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
  1198. }
  1199. ASSERT_NO_SLOT_NUMBER(&got_attributes);
  1200. PSA_ASSERT(psa_destroy_key(key));
  1201. test_operations_on_invalid_key(key);
  1202. exit:
  1203. /*
  1204. * Key attributes may have been returned by psa_get_key_attributes()
  1205. * thus reset them as required.
  1206. */
  1207. psa_reset_key_attributes(&got_attributes);
  1208. psa_destroy_key(key);
  1209. PSA_DONE();
  1210. }
  1211. /* END_CASE */
  1212. /* BEGIN_CASE */
  1213. /* Construct and attempt to import a large unstructured key. */
  1214. void import_large_key(int type_arg, int byte_size_arg,
  1215. int expected_status_arg)
  1216. {
  1217. psa_key_type_t type = type_arg;
  1218. size_t byte_size = byte_size_arg;
  1219. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1220. psa_status_t expected_status = expected_status_arg;
  1221. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1222. psa_status_t status;
  1223. uint8_t *buffer = NULL;
  1224. size_t buffer_size = byte_size + 1;
  1225. size_t n;
  1226. /* Skip the test case if the target running the test cannot
  1227. * accommodate large keys due to heap size constraints */
  1228. ASSERT_ALLOC_WEAK(buffer, buffer_size);
  1229. memset(buffer, 'K', byte_size);
  1230. PSA_ASSERT(psa_crypto_init());
  1231. /* Try importing the key */
  1232. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
  1233. psa_set_key_type(&attributes, type);
  1234. status = psa_import_key(&attributes, buffer, byte_size, &key);
  1235. TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
  1236. TEST_EQUAL(status, expected_status);
  1237. if (status == PSA_SUCCESS) {
  1238. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  1239. TEST_EQUAL(psa_get_key_type(&attributes), type);
  1240. TEST_EQUAL(psa_get_key_bits(&attributes),
  1241. PSA_BYTES_TO_BITS(byte_size));
  1242. ASSERT_NO_SLOT_NUMBER(&attributes);
  1243. memset(buffer, 0, byte_size + 1);
  1244. PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
  1245. for (n = 0; n < byte_size; n++) {
  1246. TEST_EQUAL(buffer[n], 'K');
  1247. }
  1248. for (n = byte_size; n < buffer_size; n++) {
  1249. TEST_EQUAL(buffer[n], 0);
  1250. }
  1251. }
  1252. exit:
  1253. /*
  1254. * Key attributes may have been returned by psa_get_key_attributes()
  1255. * thus reset them as required.
  1256. */
  1257. psa_reset_key_attributes(&attributes);
  1258. psa_destroy_key(key);
  1259. PSA_DONE();
  1260. mbedtls_free(buffer);
  1261. }
  1262. /* END_CASE */
  1263. /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
  1264. /* Import an RSA key with a valid structure (but not valid numbers
  1265. * inside, beyond having sensible size and parity). This is expected to
  1266. * fail for large keys. */
  1267. void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
  1268. {
  1269. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1270. size_t bits = bits_arg;
  1271. psa_status_t expected_status = expected_status_arg;
  1272. psa_status_t status;
  1273. psa_key_type_t type =
  1274. keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
  1275. size_t buffer_size = /* Slight overapproximations */
  1276. keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
  1277. unsigned char *buffer = NULL;
  1278. unsigned char *p;
  1279. int ret;
  1280. size_t length;
  1281. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1282. PSA_ASSERT(psa_crypto_init());
  1283. ASSERT_ALLOC(buffer, buffer_size);
  1284. TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
  1285. bits, keypair)) >= 0);
  1286. length = ret;
  1287. /* Try importing the key */
  1288. psa_set_key_type(&attributes, type);
  1289. status = psa_import_key(&attributes, p, length, &key);
  1290. TEST_EQUAL(status, expected_status);
  1291. if (status == PSA_SUCCESS) {
  1292. PSA_ASSERT(psa_destroy_key(key));
  1293. }
  1294. exit:
  1295. mbedtls_free(buffer);
  1296. PSA_DONE();
  1297. }
  1298. /* END_CASE */
  1299. /* BEGIN_CASE */
  1300. void import_export(data_t *data,
  1301. int type_arg,
  1302. int usage_arg, int alg_arg,
  1303. int lifetime_arg,
  1304. int expected_bits,
  1305. int export_size_delta,
  1306. int expected_export_status_arg,
  1307. /*whether reexport must give the original input exactly*/
  1308. int canonical_input)
  1309. {
  1310. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1311. psa_key_type_t type = type_arg;
  1312. psa_algorithm_t alg = alg_arg;
  1313. psa_status_t expected_export_status = expected_export_status_arg;
  1314. psa_status_t status;
  1315. psa_key_lifetime_t lifetime = lifetime_arg;
  1316. unsigned char *exported = NULL;
  1317. unsigned char *reexported = NULL;
  1318. size_t export_size;
  1319. size_t exported_length = INVALID_EXPORT_LENGTH;
  1320. size_t reexported_length;
  1321. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1322. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  1323. export_size = (ptrdiff_t) data->len + export_size_delta;
  1324. ASSERT_ALLOC(exported, export_size);
  1325. if (!canonical_input) {
  1326. ASSERT_ALLOC(reexported, export_size);
  1327. }
  1328. PSA_ASSERT(psa_crypto_init());
  1329. psa_set_key_lifetime(&attributes, lifetime);
  1330. psa_set_key_usage_flags(&attributes, usage_arg);
  1331. psa_set_key_algorithm(&attributes, alg);
  1332. psa_set_key_type(&attributes, type);
  1333. /* Import the key */
  1334. PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
  1335. /* Test the key information */
  1336. PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
  1337. TEST_EQUAL(psa_get_key_type(&got_attributes), type);
  1338. TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
  1339. ASSERT_NO_SLOT_NUMBER(&got_attributes);
  1340. /* Export the key */
  1341. status = psa_export_key(key, exported, export_size, &exported_length);
  1342. TEST_EQUAL(status, expected_export_status);
  1343. /* The exported length must be set by psa_export_key() to a value between 0
  1344. * and export_size. On errors, the exported length must be 0. */
  1345. TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
  1346. TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
  1347. TEST_LE_U(exported_length, export_size);
  1348. TEST_ASSERT(mem_is_char(exported + exported_length, 0,
  1349. export_size - exported_length));
  1350. if (status != PSA_SUCCESS) {
  1351. TEST_EQUAL(exported_length, 0);
  1352. goto destroy;
  1353. }
  1354. /* Run sanity checks on the exported key. For non-canonical inputs,
  1355. * this validates the canonical representations. For canonical inputs,
  1356. * this doesn't directly validate the implementation, but it still helps
  1357. * by cross-validating the test data with the sanity check code. */
  1358. if (!psa_key_lifetime_is_external(lifetime)) {
  1359. if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
  1360. goto exit;
  1361. }
  1362. }
  1363. if (canonical_input) {
  1364. ASSERT_COMPARE(data->x, data->len, exported, exported_length);
  1365. } else {
  1366. mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
  1367. PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
  1368. &key2));
  1369. PSA_ASSERT(psa_export_key(key2,
  1370. reexported,
  1371. export_size,
  1372. &reexported_length));
  1373. ASSERT_COMPARE(exported, exported_length,
  1374. reexported, reexported_length);
  1375. PSA_ASSERT(psa_destroy_key(key2));
  1376. }
  1377. TEST_LE_U(exported_length,
  1378. PSA_EXPORT_KEY_OUTPUT_SIZE(type,
  1379. psa_get_key_bits(&got_attributes)));
  1380. TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
  1381. destroy:
  1382. /* Destroy the key */
  1383. PSA_ASSERT(psa_destroy_key(key));
  1384. test_operations_on_invalid_key(key);
  1385. exit:
  1386. /*
  1387. * Key attributes may have been returned by psa_get_key_attributes()
  1388. * thus reset them as required.
  1389. */
  1390. psa_reset_key_attributes(&got_attributes);
  1391. psa_destroy_key(key);
  1392. mbedtls_free(exported);
  1393. mbedtls_free(reexported);
  1394. PSA_DONE();
  1395. }
  1396. /* END_CASE */
  1397. /* BEGIN_CASE */
  1398. void import_export_public_key(data_t *data,
  1399. int type_arg, // key pair or public key
  1400. int alg_arg,
  1401. int lifetime_arg,
  1402. int export_size_delta,
  1403. int expected_export_status_arg,
  1404. data_t *expected_public_key)
  1405. {
  1406. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1407. psa_key_type_t type = type_arg;
  1408. psa_algorithm_t alg = alg_arg;
  1409. psa_status_t expected_export_status = expected_export_status_arg;
  1410. psa_status_t status;
  1411. psa_key_lifetime_t lifetime = lifetime_arg;
  1412. unsigned char *exported = NULL;
  1413. size_t export_size = expected_public_key->len + export_size_delta;
  1414. size_t exported_length = INVALID_EXPORT_LENGTH;
  1415. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1416. PSA_ASSERT(psa_crypto_init());
  1417. psa_set_key_lifetime(&attributes, lifetime);
  1418. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
  1419. psa_set_key_algorithm(&attributes, alg);
  1420. psa_set_key_type(&attributes, type);
  1421. /* Import the key */
  1422. PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
  1423. /* Export the public key */
  1424. ASSERT_ALLOC(exported, export_size);
  1425. status = psa_export_public_key(key,
  1426. exported, export_size,
  1427. &exported_length);
  1428. TEST_EQUAL(status, expected_export_status);
  1429. if (status == PSA_SUCCESS) {
  1430. psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
  1431. size_t bits;
  1432. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  1433. bits = psa_get_key_bits(&attributes);
  1434. TEST_LE_U(expected_public_key->len,
  1435. PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
  1436. TEST_LE_U(expected_public_key->len,
  1437. PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
  1438. TEST_LE_U(expected_public_key->len,
  1439. PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
  1440. ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
  1441. exported, exported_length);
  1442. }
  1443. exit:
  1444. /*
  1445. * Key attributes may have been returned by psa_get_key_attributes()
  1446. * thus reset them as required.
  1447. */
  1448. psa_reset_key_attributes(&attributes);
  1449. mbedtls_free(exported);
  1450. psa_destroy_key(key);
  1451. PSA_DONE();
  1452. }
  1453. /* END_CASE */
  1454. /* BEGIN_CASE */
  1455. void import_and_exercise_key(data_t *data,
  1456. int type_arg,
  1457. int bits_arg,
  1458. int alg_arg)
  1459. {
  1460. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1461. psa_key_type_t type = type_arg;
  1462. size_t bits = bits_arg;
  1463. psa_algorithm_t alg = alg_arg;
  1464. psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
  1465. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1466. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  1467. PSA_ASSERT(psa_crypto_init());
  1468. psa_set_key_usage_flags(&attributes, usage);
  1469. psa_set_key_algorithm(&attributes, alg);
  1470. psa_set_key_type(&attributes, type);
  1471. /* Import the key */
  1472. PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
  1473. /* Test the key information */
  1474. PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
  1475. TEST_EQUAL(psa_get_key_type(&got_attributes), type);
  1476. TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
  1477. /* Do something with the key according to its type and permitted usage. */
  1478. if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
  1479. goto exit;
  1480. }
  1481. PSA_ASSERT(psa_destroy_key(key));
  1482. test_operations_on_invalid_key(key);
  1483. exit:
  1484. /*
  1485. * Key attributes may have been returned by psa_get_key_attributes()
  1486. * thus reset them as required.
  1487. */
  1488. psa_reset_key_attributes(&got_attributes);
  1489. psa_reset_key_attributes(&attributes);
  1490. psa_destroy_key(key);
  1491. PSA_DONE();
  1492. }
  1493. /* END_CASE */
  1494. /* BEGIN_CASE */
  1495. void effective_key_attributes(int type_arg, int expected_type_arg,
  1496. int bits_arg, int expected_bits_arg,
  1497. int usage_arg, int expected_usage_arg,
  1498. int alg_arg, int expected_alg_arg)
  1499. {
  1500. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1501. psa_key_type_t key_type = type_arg;
  1502. psa_key_type_t expected_key_type = expected_type_arg;
  1503. size_t bits = bits_arg;
  1504. size_t expected_bits = expected_bits_arg;
  1505. psa_algorithm_t alg = alg_arg;
  1506. psa_algorithm_t expected_alg = expected_alg_arg;
  1507. psa_key_usage_t usage = usage_arg;
  1508. psa_key_usage_t expected_usage = expected_usage_arg;
  1509. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1510. PSA_ASSERT(psa_crypto_init());
  1511. psa_set_key_usage_flags(&attributes, usage);
  1512. psa_set_key_algorithm(&attributes, alg);
  1513. psa_set_key_type(&attributes, key_type);
  1514. psa_set_key_bits(&attributes, bits);
  1515. PSA_ASSERT(psa_generate_key(&attributes, &key));
  1516. psa_reset_key_attributes(&attributes);
  1517. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  1518. TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
  1519. TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
  1520. TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
  1521. TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
  1522. exit:
  1523. /*
  1524. * Key attributes may have been returned by psa_get_key_attributes()
  1525. * thus reset them as required.
  1526. */
  1527. psa_reset_key_attributes(&attributes);
  1528. psa_destroy_key(key);
  1529. PSA_DONE();
  1530. }
  1531. /* END_CASE */
  1532. /* BEGIN_CASE */
  1533. void check_key_policy(int type_arg, int bits_arg,
  1534. int usage_arg, int alg_arg)
  1535. {
  1536. test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
  1537. usage_arg,
  1538. mbedtls_test_update_key_usage_flags(usage_arg),
  1539. alg_arg, alg_arg);
  1540. goto exit;
  1541. }
  1542. /* END_CASE */
  1543. /* BEGIN_CASE */
  1544. void key_attributes_init()
  1545. {
  1546. /* Test each valid way of initializing the object, except for `= {0}`, as
  1547. * Clang 5 complains when `-Wmissing-field-initializers` is used, even
  1548. * though it's OK by the C standard. We could test for this, but we'd need
  1549. * to suppress the Clang warning for the test. */
  1550. psa_key_attributes_t func = psa_key_attributes_init();
  1551. psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
  1552. psa_key_attributes_t zero;
  1553. memset(&zero, 0, sizeof(zero));
  1554. TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
  1555. TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
  1556. TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
  1557. TEST_EQUAL(psa_get_key_type(&func), 0);
  1558. TEST_EQUAL(psa_get_key_type(&init), 0);
  1559. TEST_EQUAL(psa_get_key_type(&zero), 0);
  1560. TEST_EQUAL(psa_get_key_bits(&func), 0);
  1561. TEST_EQUAL(psa_get_key_bits(&init), 0);
  1562. TEST_EQUAL(psa_get_key_bits(&zero), 0);
  1563. TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
  1564. TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
  1565. TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
  1566. TEST_EQUAL(psa_get_key_algorithm(&func), 0);
  1567. TEST_EQUAL(psa_get_key_algorithm(&init), 0);
  1568. TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
  1569. }
  1570. /* END_CASE */
  1571. /* BEGIN_CASE */
  1572. void mac_key_policy(int policy_usage_arg,
  1573. int policy_alg_arg,
  1574. int key_type_arg,
  1575. data_t *key_data,
  1576. int exercise_alg_arg,
  1577. int expected_status_sign_arg,
  1578. int expected_status_verify_arg)
  1579. {
  1580. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1581. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1582. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  1583. psa_key_type_t key_type = key_type_arg;
  1584. psa_algorithm_t policy_alg = policy_alg_arg;
  1585. psa_algorithm_t exercise_alg = exercise_alg_arg;
  1586. psa_key_usage_t policy_usage = policy_usage_arg;
  1587. psa_status_t status;
  1588. psa_status_t expected_status_sign = expected_status_sign_arg;
  1589. psa_status_t expected_status_verify = expected_status_verify_arg;
  1590. unsigned char mac[PSA_MAC_MAX_SIZE];
  1591. PSA_ASSERT(psa_crypto_init());
  1592. psa_set_key_usage_flags(&attributes, policy_usage);
  1593. psa_set_key_algorithm(&attributes, policy_alg);
  1594. psa_set_key_type(&attributes, key_type);
  1595. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  1596. &key));
  1597. TEST_EQUAL(psa_get_key_usage_flags(&attributes),
  1598. mbedtls_test_update_key_usage_flags(policy_usage));
  1599. status = psa_mac_sign_setup(&operation, key, exercise_alg);
  1600. TEST_EQUAL(status, expected_status_sign);
  1601. /* Calculate the MAC, one-shot case. */
  1602. uint8_t input[128] = { 0 };
  1603. size_t mac_len;
  1604. TEST_EQUAL(psa_mac_compute(key, exercise_alg,
  1605. input, 128,
  1606. mac, PSA_MAC_MAX_SIZE, &mac_len),
  1607. expected_status_sign);
  1608. /* Calculate the MAC, multi-part case. */
  1609. PSA_ASSERT(psa_mac_abort(&operation));
  1610. status = psa_mac_sign_setup(&operation, key, exercise_alg);
  1611. if (status == PSA_SUCCESS) {
  1612. status = psa_mac_update(&operation, input, 128);
  1613. if (status == PSA_SUCCESS) {
  1614. TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
  1615. &mac_len),
  1616. expected_status_sign);
  1617. } else {
  1618. TEST_EQUAL(status, expected_status_sign);
  1619. }
  1620. } else {
  1621. TEST_EQUAL(status, expected_status_sign);
  1622. }
  1623. PSA_ASSERT(psa_mac_abort(&operation));
  1624. /* Verify correct MAC, one-shot case. */
  1625. status = psa_mac_verify(key, exercise_alg, input, 128,
  1626. mac, mac_len);
  1627. if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
  1628. TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
  1629. } else {
  1630. TEST_EQUAL(status, expected_status_verify);
  1631. }
  1632. /* Verify correct MAC, multi-part case. */
  1633. status = psa_mac_verify_setup(&operation, key, exercise_alg);
  1634. if (status == PSA_SUCCESS) {
  1635. status = psa_mac_update(&operation, input, 128);
  1636. if (status == PSA_SUCCESS) {
  1637. status = psa_mac_verify_finish(&operation, mac, mac_len);
  1638. if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
  1639. TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
  1640. } else {
  1641. TEST_EQUAL(status, expected_status_verify);
  1642. }
  1643. } else {
  1644. TEST_EQUAL(status, expected_status_verify);
  1645. }
  1646. } else {
  1647. TEST_EQUAL(status, expected_status_verify);
  1648. }
  1649. psa_mac_abort(&operation);
  1650. memset(mac, 0, sizeof(mac));
  1651. status = psa_mac_verify_setup(&operation, key, exercise_alg);
  1652. TEST_EQUAL(status, expected_status_verify);
  1653. exit:
  1654. psa_mac_abort(&operation);
  1655. psa_destroy_key(key);
  1656. PSA_DONE();
  1657. }
  1658. /* END_CASE */
  1659. /* BEGIN_CASE */
  1660. void cipher_key_policy(int policy_usage_arg,
  1661. int policy_alg,
  1662. int key_type,
  1663. data_t *key_data,
  1664. int exercise_alg)
  1665. {
  1666. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1667. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1668. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  1669. psa_key_usage_t policy_usage = policy_usage_arg;
  1670. size_t output_buffer_size = 0;
  1671. size_t input_buffer_size = 0;
  1672. size_t output_length = 0;
  1673. uint8_t *output = NULL;
  1674. uint8_t *input = NULL;
  1675. psa_status_t status;
  1676. input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
  1677. output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
  1678. input_buffer_size);
  1679. ASSERT_ALLOC(input, input_buffer_size);
  1680. ASSERT_ALLOC(output, output_buffer_size);
  1681. PSA_ASSERT(psa_crypto_init());
  1682. psa_set_key_usage_flags(&attributes, policy_usage);
  1683. psa_set_key_algorithm(&attributes, policy_alg);
  1684. psa_set_key_type(&attributes, key_type);
  1685. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  1686. &key));
  1687. /* Check if no key usage flag implication is done */
  1688. TEST_EQUAL(policy_usage,
  1689. mbedtls_test_update_key_usage_flags(policy_usage));
  1690. /* Encrypt check, one-shot */
  1691. status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
  1692. output, output_buffer_size,
  1693. &output_length);
  1694. if (policy_alg == exercise_alg &&
  1695. (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
  1696. PSA_ASSERT(status);
  1697. } else {
  1698. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1699. }
  1700. /* Encrypt check, multi-part */
  1701. status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
  1702. if (policy_alg == exercise_alg &&
  1703. (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
  1704. PSA_ASSERT(status);
  1705. } else {
  1706. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1707. }
  1708. psa_cipher_abort(&operation);
  1709. /* Decrypt check, one-shot */
  1710. status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
  1711. input, input_buffer_size,
  1712. &output_length);
  1713. if (policy_alg == exercise_alg &&
  1714. (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
  1715. PSA_ASSERT(status);
  1716. } else {
  1717. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1718. }
  1719. /* Decrypt check, multi-part */
  1720. status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
  1721. if (policy_alg == exercise_alg &&
  1722. (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
  1723. PSA_ASSERT(status);
  1724. } else {
  1725. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1726. }
  1727. exit:
  1728. psa_cipher_abort(&operation);
  1729. mbedtls_free(input);
  1730. mbedtls_free(output);
  1731. psa_destroy_key(key);
  1732. PSA_DONE();
  1733. }
  1734. /* END_CASE */
  1735. /* BEGIN_CASE */
  1736. void aead_key_policy(int policy_usage_arg,
  1737. int policy_alg,
  1738. int key_type,
  1739. data_t *key_data,
  1740. int nonce_length_arg,
  1741. int tag_length_arg,
  1742. int exercise_alg,
  1743. int expected_status_arg)
  1744. {
  1745. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1746. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1747. psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
  1748. psa_key_usage_t policy_usage = policy_usage_arg;
  1749. psa_status_t status;
  1750. psa_status_t expected_status = expected_status_arg;
  1751. unsigned char nonce[16] = { 0 };
  1752. size_t nonce_length = nonce_length_arg;
  1753. unsigned char tag[16];
  1754. size_t tag_length = tag_length_arg;
  1755. size_t output_length;
  1756. TEST_LE_U(nonce_length, sizeof(nonce));
  1757. TEST_LE_U(tag_length, sizeof(tag));
  1758. PSA_ASSERT(psa_crypto_init());
  1759. psa_set_key_usage_flags(&attributes, policy_usage);
  1760. psa_set_key_algorithm(&attributes, policy_alg);
  1761. psa_set_key_type(&attributes, key_type);
  1762. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  1763. &key));
  1764. /* Check if no key usage implication is done */
  1765. TEST_EQUAL(policy_usage,
  1766. mbedtls_test_update_key_usage_flags(policy_usage));
  1767. /* Encrypt check, one-shot */
  1768. status = psa_aead_encrypt(key, exercise_alg,
  1769. nonce, nonce_length,
  1770. NULL, 0,
  1771. NULL, 0,
  1772. tag, tag_length,
  1773. &output_length);
  1774. if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
  1775. TEST_EQUAL(status, expected_status);
  1776. } else {
  1777. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1778. }
  1779. /* Encrypt check, multi-part */
  1780. status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
  1781. if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
  1782. TEST_EQUAL(status, expected_status);
  1783. } else {
  1784. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1785. }
  1786. /* Decrypt check, one-shot */
  1787. memset(tag, 0, sizeof(tag));
  1788. status = psa_aead_decrypt(key, exercise_alg,
  1789. nonce, nonce_length,
  1790. NULL, 0,
  1791. tag, tag_length,
  1792. NULL, 0,
  1793. &output_length);
  1794. if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
  1795. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1796. } else if (expected_status == PSA_SUCCESS) {
  1797. TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
  1798. } else {
  1799. TEST_EQUAL(status, expected_status);
  1800. }
  1801. /* Decrypt check, multi-part */
  1802. PSA_ASSERT(psa_aead_abort(&operation));
  1803. status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
  1804. if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
  1805. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1806. } else {
  1807. TEST_EQUAL(status, expected_status);
  1808. }
  1809. exit:
  1810. PSA_ASSERT(psa_aead_abort(&operation));
  1811. psa_destroy_key(key);
  1812. PSA_DONE();
  1813. }
  1814. /* END_CASE */
  1815. /* BEGIN_CASE */
  1816. void asymmetric_encryption_key_policy(int policy_usage_arg,
  1817. int policy_alg,
  1818. int key_type,
  1819. data_t *key_data,
  1820. int exercise_alg)
  1821. {
  1822. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1823. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1824. psa_key_usage_t policy_usage = policy_usage_arg;
  1825. psa_status_t status;
  1826. size_t key_bits;
  1827. size_t buffer_length;
  1828. unsigned char *buffer = NULL;
  1829. size_t output_length;
  1830. PSA_ASSERT(psa_crypto_init());
  1831. psa_set_key_usage_flags(&attributes, policy_usage);
  1832. psa_set_key_algorithm(&attributes, policy_alg);
  1833. psa_set_key_type(&attributes, key_type);
  1834. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  1835. &key));
  1836. /* Check if no key usage implication is done */
  1837. TEST_EQUAL(policy_usage,
  1838. mbedtls_test_update_key_usage_flags(policy_usage));
  1839. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  1840. key_bits = psa_get_key_bits(&attributes);
  1841. buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
  1842. exercise_alg);
  1843. ASSERT_ALLOC(buffer, buffer_length);
  1844. status = psa_asymmetric_encrypt(key, exercise_alg,
  1845. NULL, 0,
  1846. NULL, 0,
  1847. buffer, buffer_length,
  1848. &output_length);
  1849. if (policy_alg == exercise_alg &&
  1850. (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
  1851. PSA_ASSERT(status);
  1852. } else {
  1853. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1854. }
  1855. if (buffer_length != 0) {
  1856. memset(buffer, 0, buffer_length);
  1857. }
  1858. status = psa_asymmetric_decrypt(key, exercise_alg,
  1859. buffer, buffer_length,
  1860. NULL, 0,
  1861. buffer, buffer_length,
  1862. &output_length);
  1863. if (policy_alg == exercise_alg &&
  1864. (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
  1865. TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
  1866. } else {
  1867. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1868. }
  1869. exit:
  1870. /*
  1871. * Key attributes may have been returned by psa_get_key_attributes()
  1872. * thus reset them as required.
  1873. */
  1874. psa_reset_key_attributes(&attributes);
  1875. psa_destroy_key(key);
  1876. PSA_DONE();
  1877. mbedtls_free(buffer);
  1878. }
  1879. /* END_CASE */
  1880. /* BEGIN_CASE */
  1881. void asymmetric_signature_key_policy(int policy_usage_arg,
  1882. int policy_alg,
  1883. int key_type,
  1884. data_t *key_data,
  1885. int exercise_alg,
  1886. int payload_length_arg,
  1887. int expected_usage_arg)
  1888. {
  1889. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1890. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1891. psa_key_usage_t policy_usage = policy_usage_arg;
  1892. psa_key_usage_t expected_usage = expected_usage_arg;
  1893. psa_status_t status;
  1894. unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
  1895. /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
  1896. * compatible with the policy and `payload_length_arg` is supposed to be
  1897. * a valid input length to sign. If `payload_length_arg <= 0`,
  1898. * `exercise_alg` is supposed to be forbidden by the policy. */
  1899. int compatible_alg = payload_length_arg > 0;
  1900. size_t payload_length = compatible_alg ? payload_length_arg : 0;
  1901. unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
  1902. size_t signature_length;
  1903. /* Check if all implicit usage flags are deployed
  1904. in the expected usage flags. */
  1905. TEST_EQUAL(expected_usage,
  1906. mbedtls_test_update_key_usage_flags(policy_usage));
  1907. PSA_ASSERT(psa_crypto_init());
  1908. psa_set_key_usage_flags(&attributes, policy_usage);
  1909. psa_set_key_algorithm(&attributes, policy_alg);
  1910. psa_set_key_type(&attributes, key_type);
  1911. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  1912. &key));
  1913. TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
  1914. status = psa_sign_hash(key, exercise_alg,
  1915. payload, payload_length,
  1916. signature, sizeof(signature),
  1917. &signature_length);
  1918. if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
  1919. PSA_ASSERT(status);
  1920. } else {
  1921. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1922. }
  1923. memset(signature, 0, sizeof(signature));
  1924. status = psa_verify_hash(key, exercise_alg,
  1925. payload, payload_length,
  1926. signature, sizeof(signature));
  1927. if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
  1928. TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
  1929. } else {
  1930. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1931. }
  1932. if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
  1933. PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
  1934. status = psa_sign_message(key, exercise_alg,
  1935. payload, payload_length,
  1936. signature, sizeof(signature),
  1937. &signature_length);
  1938. if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
  1939. PSA_ASSERT(status);
  1940. } else {
  1941. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1942. }
  1943. memset(signature, 0, sizeof(signature));
  1944. status = psa_verify_message(key, exercise_alg,
  1945. payload, payload_length,
  1946. signature, sizeof(signature));
  1947. if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
  1948. TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
  1949. } else {
  1950. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1951. }
  1952. }
  1953. exit:
  1954. psa_destroy_key(key);
  1955. PSA_DONE();
  1956. }
  1957. /* END_CASE */
  1958. /* BEGIN_CASE */
  1959. void derive_key_policy(int policy_usage,
  1960. int policy_alg,
  1961. int key_type,
  1962. data_t *key_data,
  1963. int exercise_alg)
  1964. {
  1965. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1966. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1967. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  1968. psa_status_t status;
  1969. PSA_ASSERT(psa_crypto_init());
  1970. psa_set_key_usage_flags(&attributes, policy_usage);
  1971. psa_set_key_algorithm(&attributes, policy_alg);
  1972. psa_set_key_type(&attributes, key_type);
  1973. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  1974. &key));
  1975. PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
  1976. if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
  1977. PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
  1978. PSA_ASSERT(psa_key_derivation_input_bytes(
  1979. &operation,
  1980. PSA_KEY_DERIVATION_INPUT_SEED,
  1981. (const uint8_t *) "", 0));
  1982. }
  1983. status = psa_key_derivation_input_key(&operation,
  1984. PSA_KEY_DERIVATION_INPUT_SECRET,
  1985. key);
  1986. if (policy_alg == exercise_alg &&
  1987. (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
  1988. PSA_ASSERT(status);
  1989. } else {
  1990. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  1991. }
  1992. exit:
  1993. psa_key_derivation_abort(&operation);
  1994. psa_destroy_key(key);
  1995. PSA_DONE();
  1996. }
  1997. /* END_CASE */
  1998. /* BEGIN_CASE */
  1999. void agreement_key_policy(int policy_usage,
  2000. int policy_alg,
  2001. int key_type_arg,
  2002. data_t *key_data,
  2003. int exercise_alg,
  2004. int expected_status_arg)
  2005. {
  2006. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2007. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2008. psa_key_type_t key_type = key_type_arg;
  2009. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  2010. psa_status_t status;
  2011. psa_status_t expected_status = expected_status_arg;
  2012. PSA_ASSERT(psa_crypto_init());
  2013. psa_set_key_usage_flags(&attributes, policy_usage);
  2014. psa_set_key_algorithm(&attributes, policy_alg);
  2015. psa_set_key_type(&attributes, key_type);
  2016. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  2017. &key));
  2018. PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
  2019. status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
  2020. TEST_EQUAL(status, expected_status);
  2021. exit:
  2022. psa_key_derivation_abort(&operation);
  2023. psa_destroy_key(key);
  2024. PSA_DONE();
  2025. }
  2026. /* END_CASE */
  2027. /* BEGIN_CASE */
  2028. void key_policy_alg2(int key_type_arg, data_t *key_data,
  2029. int usage_arg, int alg_arg, int alg2_arg)
  2030. {
  2031. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2032. psa_key_type_t key_type = key_type_arg;
  2033. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2034. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  2035. psa_key_usage_t usage = usage_arg;
  2036. psa_algorithm_t alg = alg_arg;
  2037. psa_algorithm_t alg2 = alg2_arg;
  2038. PSA_ASSERT(psa_crypto_init());
  2039. psa_set_key_usage_flags(&attributes, usage);
  2040. psa_set_key_algorithm(&attributes, alg);
  2041. psa_set_key_enrollment_algorithm(&attributes, alg2);
  2042. psa_set_key_type(&attributes, key_type);
  2043. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  2044. &key));
  2045. /* Update the usage flags to obtain implicit usage flags */
  2046. usage = mbedtls_test_update_key_usage_flags(usage);
  2047. PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
  2048. TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
  2049. TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
  2050. TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
  2051. if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
  2052. goto exit;
  2053. }
  2054. if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
  2055. goto exit;
  2056. }
  2057. exit:
  2058. /*
  2059. * Key attributes may have been returned by psa_get_key_attributes()
  2060. * thus reset them as required.
  2061. */
  2062. psa_reset_key_attributes(&got_attributes);
  2063. psa_destroy_key(key);
  2064. PSA_DONE();
  2065. }
  2066. /* END_CASE */
  2067. /* BEGIN_CASE */
  2068. void raw_agreement_key_policy(int policy_usage,
  2069. int policy_alg,
  2070. int key_type_arg,
  2071. data_t *key_data,
  2072. int exercise_alg,
  2073. int expected_status_arg)
  2074. {
  2075. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2076. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2077. psa_key_type_t key_type = key_type_arg;
  2078. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  2079. psa_status_t status;
  2080. psa_status_t expected_status = expected_status_arg;
  2081. PSA_ASSERT(psa_crypto_init());
  2082. psa_set_key_usage_flags(&attributes, policy_usage);
  2083. psa_set_key_algorithm(&attributes, policy_alg);
  2084. psa_set_key_type(&attributes, key_type);
  2085. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  2086. &key));
  2087. status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
  2088. TEST_EQUAL(status, expected_status);
  2089. exit:
  2090. psa_key_derivation_abort(&operation);
  2091. psa_destroy_key(key);
  2092. PSA_DONE();
  2093. }
  2094. /* END_CASE */
  2095. /* BEGIN_CASE */
  2096. void copy_success(int source_usage_arg,
  2097. int source_alg_arg, int source_alg2_arg,
  2098. unsigned int source_lifetime_arg,
  2099. int type_arg, data_t *material,
  2100. int copy_attributes,
  2101. int target_usage_arg,
  2102. int target_alg_arg, int target_alg2_arg,
  2103. unsigned int target_lifetime_arg,
  2104. int expected_usage_arg,
  2105. int expected_alg_arg, int expected_alg2_arg)
  2106. {
  2107. psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
  2108. psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
  2109. psa_key_usage_t expected_usage = expected_usage_arg;
  2110. psa_algorithm_t expected_alg = expected_alg_arg;
  2111. psa_algorithm_t expected_alg2 = expected_alg2_arg;
  2112. psa_key_lifetime_t source_lifetime = source_lifetime_arg;
  2113. psa_key_lifetime_t target_lifetime = target_lifetime_arg;
  2114. mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
  2115. mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
  2116. uint8_t *export_buffer = NULL;
  2117. PSA_ASSERT(psa_crypto_init());
  2118. /* Prepare the source key. */
  2119. psa_set_key_usage_flags(&source_attributes, source_usage_arg);
  2120. psa_set_key_algorithm(&source_attributes, source_alg_arg);
  2121. psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
  2122. psa_set_key_type(&source_attributes, type_arg);
  2123. psa_set_key_lifetime(&source_attributes, source_lifetime);
  2124. PSA_ASSERT(psa_import_key(&source_attributes,
  2125. material->x, material->len,
  2126. &source_key));
  2127. PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
  2128. /* Prepare the target attributes. */
  2129. if (copy_attributes) {
  2130. target_attributes = source_attributes;
  2131. }
  2132. psa_set_key_lifetime(&target_attributes, target_lifetime);
  2133. if (target_usage_arg != -1) {
  2134. psa_set_key_usage_flags(&target_attributes, target_usage_arg);
  2135. }
  2136. if (target_alg_arg != -1) {
  2137. psa_set_key_algorithm(&target_attributes, target_alg_arg);
  2138. }
  2139. if (target_alg2_arg != -1) {
  2140. psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
  2141. }
  2142. /* Copy the key. */
  2143. PSA_ASSERT(psa_copy_key(source_key,
  2144. &target_attributes, &target_key));
  2145. /* Destroy the source to ensure that this doesn't affect the target. */
  2146. PSA_ASSERT(psa_destroy_key(source_key));
  2147. /* Test that the target slot has the expected content and policy. */
  2148. PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
  2149. TEST_EQUAL(psa_get_key_type(&source_attributes),
  2150. psa_get_key_type(&target_attributes));
  2151. TEST_EQUAL(psa_get_key_bits(&source_attributes),
  2152. psa_get_key_bits(&target_attributes));
  2153. TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
  2154. TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
  2155. TEST_EQUAL(expected_alg2,
  2156. psa_get_key_enrollment_algorithm(&target_attributes));
  2157. if (expected_usage & PSA_KEY_USAGE_EXPORT) {
  2158. size_t length;
  2159. ASSERT_ALLOC(export_buffer, material->len);
  2160. PSA_ASSERT(psa_export_key(target_key, export_buffer,
  2161. material->len, &length));
  2162. ASSERT_COMPARE(material->x, material->len,
  2163. export_buffer, length);
  2164. }
  2165. if (!psa_key_lifetime_is_external(target_lifetime)) {
  2166. if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
  2167. goto exit;
  2168. }
  2169. if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
  2170. goto exit;
  2171. }
  2172. }
  2173. PSA_ASSERT(psa_destroy_key(target_key));
  2174. exit:
  2175. /*
  2176. * Source and target key attributes may have been returned by
  2177. * psa_get_key_attributes() thus reset them as required.
  2178. */
  2179. psa_reset_key_attributes(&source_attributes);
  2180. psa_reset_key_attributes(&target_attributes);
  2181. PSA_DONE();
  2182. mbedtls_free(export_buffer);
  2183. }
  2184. /* END_CASE */
  2185. /* BEGIN_CASE */
  2186. void copy_fail(int source_usage_arg,
  2187. int source_alg_arg, int source_alg2_arg,
  2188. int source_lifetime_arg,
  2189. int type_arg, data_t *material,
  2190. int target_type_arg, int target_bits_arg,
  2191. int target_usage_arg,
  2192. int target_alg_arg, int target_alg2_arg,
  2193. int target_id_arg, int target_lifetime_arg,
  2194. int expected_status_arg)
  2195. {
  2196. psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
  2197. psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
  2198. mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
  2199. mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
  2200. mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
  2201. PSA_ASSERT(psa_crypto_init());
  2202. /* Prepare the source key. */
  2203. psa_set_key_usage_flags(&source_attributes, source_usage_arg);
  2204. psa_set_key_algorithm(&source_attributes, source_alg_arg);
  2205. psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
  2206. psa_set_key_type(&source_attributes, type_arg);
  2207. psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
  2208. PSA_ASSERT(psa_import_key(&source_attributes,
  2209. material->x, material->len,
  2210. &source_key));
  2211. /* Prepare the target attributes. */
  2212. psa_set_key_id(&target_attributes, key_id);
  2213. psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
  2214. psa_set_key_type(&target_attributes, target_type_arg);
  2215. psa_set_key_bits(&target_attributes, target_bits_arg);
  2216. psa_set_key_usage_flags(&target_attributes, target_usage_arg);
  2217. psa_set_key_algorithm(&target_attributes, target_alg_arg);
  2218. psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
  2219. /* Try to copy the key. */
  2220. TEST_EQUAL(psa_copy_key(source_key,
  2221. &target_attributes, &target_key),
  2222. expected_status_arg);
  2223. PSA_ASSERT(psa_destroy_key(source_key));
  2224. exit:
  2225. psa_reset_key_attributes(&source_attributes);
  2226. psa_reset_key_attributes(&target_attributes);
  2227. PSA_DONE();
  2228. }
  2229. /* END_CASE */
  2230. /* BEGIN_CASE */
  2231. void hash_operation_init()
  2232. {
  2233. const uint8_t input[1] = { 0 };
  2234. /* Test each valid way of initializing the object, except for `= {0}`, as
  2235. * Clang 5 complains when `-Wmissing-field-initializers` is used, even
  2236. * though it's OK by the C standard. We could test for this, but we'd need
  2237. * to suppress the Clang warning for the test. */
  2238. psa_hash_operation_t func = psa_hash_operation_init();
  2239. psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
  2240. psa_hash_operation_t zero;
  2241. memset(&zero, 0, sizeof(zero));
  2242. /* A freshly-initialized hash operation should not be usable. */
  2243. TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
  2244. PSA_ERROR_BAD_STATE);
  2245. TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
  2246. PSA_ERROR_BAD_STATE);
  2247. TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
  2248. PSA_ERROR_BAD_STATE);
  2249. /* A default hash operation should be abortable without error. */
  2250. PSA_ASSERT(psa_hash_abort(&func));
  2251. PSA_ASSERT(psa_hash_abort(&init));
  2252. PSA_ASSERT(psa_hash_abort(&zero));
  2253. }
  2254. /* END_CASE */
  2255. /* BEGIN_CASE */
  2256. void hash_setup(int alg_arg,
  2257. int expected_status_arg)
  2258. {
  2259. psa_algorithm_t alg = alg_arg;
  2260. uint8_t *output = NULL;
  2261. size_t output_size = 0;
  2262. size_t output_length = 0;
  2263. psa_status_t expected_status = expected_status_arg;
  2264. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  2265. psa_status_t status;
  2266. PSA_ASSERT(psa_crypto_init());
  2267. /* Hash Setup, one-shot */
  2268. output_size = PSA_HASH_LENGTH(alg);
  2269. ASSERT_ALLOC(output, output_size);
  2270. status = psa_hash_compute(alg, NULL, 0,
  2271. output, output_size, &output_length);
  2272. TEST_EQUAL(status, expected_status);
  2273. /* Hash Setup, multi-part */
  2274. status = psa_hash_setup(&operation, alg);
  2275. TEST_EQUAL(status, expected_status);
  2276. /* Whether setup succeeded or failed, abort must succeed. */
  2277. PSA_ASSERT(psa_hash_abort(&operation));
  2278. /* If setup failed, reproduce the failure, so as to
  2279. * test the resulting state of the operation object. */
  2280. if (status != PSA_SUCCESS) {
  2281. TEST_EQUAL(psa_hash_setup(&operation, alg), status);
  2282. }
  2283. /* Now the operation object should be reusable. */
  2284. #if defined(KNOWN_SUPPORTED_HASH_ALG)
  2285. PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
  2286. PSA_ASSERT(psa_hash_abort(&operation));
  2287. #endif
  2288. exit:
  2289. mbedtls_free(output);
  2290. PSA_DONE();
  2291. }
  2292. /* END_CASE */
  2293. /* BEGIN_CASE */
  2294. void hash_compute_fail(int alg_arg, data_t *input,
  2295. int output_size_arg, int expected_status_arg)
  2296. {
  2297. psa_algorithm_t alg = alg_arg;
  2298. uint8_t *output = NULL;
  2299. size_t output_size = output_size_arg;
  2300. size_t output_length = INVALID_EXPORT_LENGTH;
  2301. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  2302. psa_status_t expected_status = expected_status_arg;
  2303. psa_status_t status;
  2304. ASSERT_ALLOC(output, output_size);
  2305. PSA_ASSERT(psa_crypto_init());
  2306. /* Hash Compute, one-shot */
  2307. status = psa_hash_compute(alg, input->x, input->len,
  2308. output, output_size, &output_length);
  2309. TEST_EQUAL(status, expected_status);
  2310. TEST_LE_U(output_length, output_size);
  2311. /* Hash Compute, multi-part */
  2312. status = psa_hash_setup(&operation, alg);
  2313. if (status == PSA_SUCCESS) {
  2314. status = psa_hash_update(&operation, input->x, input->len);
  2315. if (status == PSA_SUCCESS) {
  2316. status = psa_hash_finish(&operation, output, output_size,
  2317. &output_length);
  2318. if (status == PSA_SUCCESS) {
  2319. TEST_LE_U(output_length, output_size);
  2320. } else {
  2321. TEST_EQUAL(status, expected_status);
  2322. }
  2323. } else {
  2324. TEST_EQUAL(status, expected_status);
  2325. }
  2326. } else {
  2327. TEST_EQUAL(status, expected_status);
  2328. }
  2329. exit:
  2330. PSA_ASSERT(psa_hash_abort(&operation));
  2331. mbedtls_free(output);
  2332. PSA_DONE();
  2333. }
  2334. /* END_CASE */
  2335. /* BEGIN_CASE */
  2336. void hash_compare_fail(int alg_arg, data_t *input,
  2337. data_t *reference_hash,
  2338. int expected_status_arg)
  2339. {
  2340. psa_algorithm_t alg = alg_arg;
  2341. psa_status_t expected_status = expected_status_arg;
  2342. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  2343. psa_status_t status;
  2344. PSA_ASSERT(psa_crypto_init());
  2345. /* Hash Compare, one-shot */
  2346. status = psa_hash_compare(alg, input->x, input->len,
  2347. reference_hash->x, reference_hash->len);
  2348. TEST_EQUAL(status, expected_status);
  2349. /* Hash Compare, multi-part */
  2350. status = psa_hash_setup(&operation, alg);
  2351. if (status == PSA_SUCCESS) {
  2352. status = psa_hash_update(&operation, input->x, input->len);
  2353. if (status == PSA_SUCCESS) {
  2354. status = psa_hash_verify(&operation, reference_hash->x,
  2355. reference_hash->len);
  2356. TEST_EQUAL(status, expected_status);
  2357. } else {
  2358. TEST_EQUAL(status, expected_status);
  2359. }
  2360. } else {
  2361. TEST_EQUAL(status, expected_status);
  2362. }
  2363. exit:
  2364. PSA_ASSERT(psa_hash_abort(&operation));
  2365. PSA_DONE();
  2366. }
  2367. /* END_CASE */
  2368. /* BEGIN_CASE */
  2369. void hash_compute_compare(int alg_arg, data_t *input,
  2370. data_t *expected_output)
  2371. {
  2372. psa_algorithm_t alg = alg_arg;
  2373. uint8_t output[PSA_HASH_MAX_SIZE + 1];
  2374. size_t output_length = INVALID_EXPORT_LENGTH;
  2375. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  2376. size_t i;
  2377. PSA_ASSERT(psa_crypto_init());
  2378. /* Compute with tight buffer, one-shot */
  2379. PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
  2380. output, PSA_HASH_LENGTH(alg),
  2381. &output_length));
  2382. TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
  2383. ASSERT_COMPARE(output, output_length,
  2384. expected_output->x, expected_output->len);
  2385. /* Compute with tight buffer, multi-part */
  2386. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2387. PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
  2388. PSA_ASSERT(psa_hash_finish(&operation, output,
  2389. PSA_HASH_LENGTH(alg),
  2390. &output_length));
  2391. TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
  2392. ASSERT_COMPARE(output, output_length,
  2393. expected_output->x, expected_output->len);
  2394. /* Compute with larger buffer, one-shot */
  2395. PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
  2396. output, sizeof(output),
  2397. &output_length));
  2398. TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
  2399. ASSERT_COMPARE(output, output_length,
  2400. expected_output->x, expected_output->len);
  2401. /* Compute with larger buffer, multi-part */
  2402. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2403. PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
  2404. PSA_ASSERT(psa_hash_finish(&operation, output,
  2405. sizeof(output), &output_length));
  2406. TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
  2407. ASSERT_COMPARE(output, output_length,
  2408. expected_output->x, expected_output->len);
  2409. /* Compare with correct hash, one-shot */
  2410. PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
  2411. output, output_length));
  2412. /* Compare with correct hash, multi-part */
  2413. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2414. PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
  2415. PSA_ASSERT(psa_hash_verify(&operation, output,
  2416. output_length));
  2417. /* Compare with trailing garbage, one-shot */
  2418. TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
  2419. output, output_length + 1),
  2420. PSA_ERROR_INVALID_SIGNATURE);
  2421. /* Compare with trailing garbage, multi-part */
  2422. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2423. PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
  2424. TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
  2425. PSA_ERROR_INVALID_SIGNATURE);
  2426. /* Compare with truncated hash, one-shot */
  2427. TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
  2428. output, output_length - 1),
  2429. PSA_ERROR_INVALID_SIGNATURE);
  2430. /* Compare with truncated hash, multi-part */
  2431. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2432. PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
  2433. TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
  2434. PSA_ERROR_INVALID_SIGNATURE);
  2435. /* Compare with corrupted value */
  2436. for (i = 0; i < output_length; i++) {
  2437. mbedtls_test_set_step(i);
  2438. output[i] ^= 1;
  2439. /* One-shot */
  2440. TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
  2441. output, output_length),
  2442. PSA_ERROR_INVALID_SIGNATURE);
  2443. /* Multi-Part */
  2444. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2445. PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
  2446. TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
  2447. PSA_ERROR_INVALID_SIGNATURE);
  2448. output[i] ^= 1;
  2449. }
  2450. exit:
  2451. PSA_ASSERT(psa_hash_abort(&operation));
  2452. PSA_DONE();
  2453. }
  2454. /* END_CASE */
  2455. /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
  2456. void hash_bad_order()
  2457. {
  2458. psa_algorithm_t alg = PSA_ALG_SHA_256;
  2459. unsigned char input[] = "";
  2460. /* SHA-256 hash of an empty string */
  2461. const unsigned char valid_hash[] = {
  2462. 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
  2463. 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
  2464. 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
  2465. };
  2466. unsigned char hash[sizeof(valid_hash)] = { 0 };
  2467. size_t hash_len;
  2468. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  2469. PSA_ASSERT(psa_crypto_init());
  2470. /* Call setup twice in a row. */
  2471. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2472. ASSERT_OPERATION_IS_ACTIVE(operation);
  2473. TEST_EQUAL(psa_hash_setup(&operation, alg),
  2474. PSA_ERROR_BAD_STATE);
  2475. ASSERT_OPERATION_IS_INACTIVE(operation);
  2476. PSA_ASSERT(psa_hash_abort(&operation));
  2477. ASSERT_OPERATION_IS_INACTIVE(operation);
  2478. /* Call update without calling setup beforehand. */
  2479. TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
  2480. PSA_ERROR_BAD_STATE);
  2481. PSA_ASSERT(psa_hash_abort(&operation));
  2482. /* Check that update calls abort on error. */
  2483. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2484. operation.id = UINT_MAX;
  2485. ASSERT_OPERATION_IS_ACTIVE(operation);
  2486. TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
  2487. PSA_ERROR_BAD_STATE);
  2488. ASSERT_OPERATION_IS_INACTIVE(operation);
  2489. PSA_ASSERT(psa_hash_abort(&operation));
  2490. ASSERT_OPERATION_IS_INACTIVE(operation);
  2491. /* Call update after finish. */
  2492. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2493. PSA_ASSERT(psa_hash_finish(&operation,
  2494. hash, sizeof(hash), &hash_len));
  2495. TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
  2496. PSA_ERROR_BAD_STATE);
  2497. PSA_ASSERT(psa_hash_abort(&operation));
  2498. /* Call verify without calling setup beforehand. */
  2499. TEST_EQUAL(psa_hash_verify(&operation,
  2500. valid_hash, sizeof(valid_hash)),
  2501. PSA_ERROR_BAD_STATE);
  2502. PSA_ASSERT(psa_hash_abort(&operation));
  2503. /* Call verify after finish. */
  2504. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2505. PSA_ASSERT(psa_hash_finish(&operation,
  2506. hash, sizeof(hash), &hash_len));
  2507. TEST_EQUAL(psa_hash_verify(&operation,
  2508. valid_hash, sizeof(valid_hash)),
  2509. PSA_ERROR_BAD_STATE);
  2510. PSA_ASSERT(psa_hash_abort(&operation));
  2511. /* Call verify twice in a row. */
  2512. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2513. ASSERT_OPERATION_IS_ACTIVE(operation);
  2514. PSA_ASSERT(psa_hash_verify(&operation,
  2515. valid_hash, sizeof(valid_hash)));
  2516. ASSERT_OPERATION_IS_INACTIVE(operation);
  2517. TEST_EQUAL(psa_hash_verify(&operation,
  2518. valid_hash, sizeof(valid_hash)),
  2519. PSA_ERROR_BAD_STATE);
  2520. ASSERT_OPERATION_IS_INACTIVE(operation);
  2521. PSA_ASSERT(psa_hash_abort(&operation));
  2522. /* Call finish without calling setup beforehand. */
  2523. TEST_EQUAL(psa_hash_finish(&operation,
  2524. hash, sizeof(hash), &hash_len),
  2525. PSA_ERROR_BAD_STATE);
  2526. PSA_ASSERT(psa_hash_abort(&operation));
  2527. /* Call finish twice in a row. */
  2528. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2529. PSA_ASSERT(psa_hash_finish(&operation,
  2530. hash, sizeof(hash), &hash_len));
  2531. TEST_EQUAL(psa_hash_finish(&operation,
  2532. hash, sizeof(hash), &hash_len),
  2533. PSA_ERROR_BAD_STATE);
  2534. PSA_ASSERT(psa_hash_abort(&operation));
  2535. /* Call finish after calling verify. */
  2536. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2537. PSA_ASSERT(psa_hash_verify(&operation,
  2538. valid_hash, sizeof(valid_hash)));
  2539. TEST_EQUAL(psa_hash_finish(&operation,
  2540. hash, sizeof(hash), &hash_len),
  2541. PSA_ERROR_BAD_STATE);
  2542. PSA_ASSERT(psa_hash_abort(&operation));
  2543. exit:
  2544. PSA_DONE();
  2545. }
  2546. /* END_CASE */
  2547. /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
  2548. void hash_verify_bad_args()
  2549. {
  2550. psa_algorithm_t alg = PSA_ALG_SHA_256;
  2551. /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
  2552. * appended to it */
  2553. unsigned char hash[] = {
  2554. 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
  2555. 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
  2556. 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
  2557. };
  2558. size_t expected_size = PSA_HASH_LENGTH(alg);
  2559. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  2560. PSA_ASSERT(psa_crypto_init());
  2561. /* psa_hash_verify with a smaller hash than expected */
  2562. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2563. ASSERT_OPERATION_IS_ACTIVE(operation);
  2564. TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
  2565. PSA_ERROR_INVALID_SIGNATURE);
  2566. ASSERT_OPERATION_IS_INACTIVE(operation);
  2567. PSA_ASSERT(psa_hash_abort(&operation));
  2568. ASSERT_OPERATION_IS_INACTIVE(operation);
  2569. /* psa_hash_verify with a non-matching hash */
  2570. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2571. TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
  2572. PSA_ERROR_INVALID_SIGNATURE);
  2573. /* psa_hash_verify with a hash longer than expected */
  2574. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2575. TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
  2576. PSA_ERROR_INVALID_SIGNATURE);
  2577. exit:
  2578. PSA_DONE();
  2579. }
  2580. /* END_CASE */
  2581. /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
  2582. void hash_finish_bad_args()
  2583. {
  2584. psa_algorithm_t alg = PSA_ALG_SHA_256;
  2585. unsigned char hash[PSA_HASH_MAX_SIZE];
  2586. size_t expected_size = PSA_HASH_LENGTH(alg);
  2587. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  2588. size_t hash_len;
  2589. PSA_ASSERT(psa_crypto_init());
  2590. /* psa_hash_finish with a smaller hash buffer than expected */
  2591. PSA_ASSERT(psa_hash_setup(&operation, alg));
  2592. TEST_EQUAL(psa_hash_finish(&operation,
  2593. hash, expected_size - 1, &hash_len),
  2594. PSA_ERROR_BUFFER_TOO_SMALL);
  2595. exit:
  2596. PSA_DONE();
  2597. }
  2598. /* END_CASE */
  2599. /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
  2600. void hash_clone_source_state()
  2601. {
  2602. psa_algorithm_t alg = PSA_ALG_SHA_256;
  2603. unsigned char hash[PSA_HASH_MAX_SIZE];
  2604. psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
  2605. psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
  2606. psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
  2607. psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
  2608. psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
  2609. size_t hash_len;
  2610. PSA_ASSERT(psa_crypto_init());
  2611. PSA_ASSERT(psa_hash_setup(&op_source, alg));
  2612. PSA_ASSERT(psa_hash_setup(&op_setup, alg));
  2613. PSA_ASSERT(psa_hash_setup(&op_finished, alg));
  2614. PSA_ASSERT(psa_hash_finish(&op_finished,
  2615. hash, sizeof(hash), &hash_len));
  2616. PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
  2617. PSA_ASSERT(psa_hash_abort(&op_aborted));
  2618. TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
  2619. PSA_ERROR_BAD_STATE);
  2620. PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
  2621. PSA_ASSERT(psa_hash_finish(&op_init,
  2622. hash, sizeof(hash), &hash_len));
  2623. PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
  2624. PSA_ASSERT(psa_hash_finish(&op_finished,
  2625. hash, sizeof(hash), &hash_len));
  2626. PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
  2627. PSA_ASSERT(psa_hash_finish(&op_aborted,
  2628. hash, sizeof(hash), &hash_len));
  2629. exit:
  2630. psa_hash_abort(&op_source);
  2631. psa_hash_abort(&op_init);
  2632. psa_hash_abort(&op_setup);
  2633. psa_hash_abort(&op_finished);
  2634. psa_hash_abort(&op_aborted);
  2635. PSA_DONE();
  2636. }
  2637. /* END_CASE */
  2638. /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
  2639. void hash_clone_target_state()
  2640. {
  2641. psa_algorithm_t alg = PSA_ALG_SHA_256;
  2642. unsigned char hash[PSA_HASH_MAX_SIZE];
  2643. psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
  2644. psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
  2645. psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
  2646. psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
  2647. psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
  2648. size_t hash_len;
  2649. PSA_ASSERT(psa_crypto_init());
  2650. PSA_ASSERT(psa_hash_setup(&op_setup, alg));
  2651. PSA_ASSERT(psa_hash_setup(&op_finished, alg));
  2652. PSA_ASSERT(psa_hash_finish(&op_finished,
  2653. hash, sizeof(hash), &hash_len));
  2654. PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
  2655. PSA_ASSERT(psa_hash_abort(&op_aborted));
  2656. PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
  2657. PSA_ASSERT(psa_hash_finish(&op_target,
  2658. hash, sizeof(hash), &hash_len));
  2659. TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
  2660. TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
  2661. PSA_ERROR_BAD_STATE);
  2662. TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
  2663. PSA_ERROR_BAD_STATE);
  2664. exit:
  2665. psa_hash_abort(&op_target);
  2666. psa_hash_abort(&op_init);
  2667. psa_hash_abort(&op_setup);
  2668. psa_hash_abort(&op_finished);
  2669. psa_hash_abort(&op_aborted);
  2670. PSA_DONE();
  2671. }
  2672. /* END_CASE */
  2673. /* BEGIN_CASE */
  2674. void mac_operation_init()
  2675. {
  2676. const uint8_t input[1] = { 0 };
  2677. /* Test each valid way of initializing the object, except for `= {0}`, as
  2678. * Clang 5 complains when `-Wmissing-field-initializers` is used, even
  2679. * though it's OK by the C standard. We could test for this, but we'd need
  2680. * to suppress the Clang warning for the test. */
  2681. psa_mac_operation_t func = psa_mac_operation_init();
  2682. psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
  2683. psa_mac_operation_t zero;
  2684. memset(&zero, 0, sizeof(zero));
  2685. /* A freshly-initialized MAC operation should not be usable. */
  2686. TEST_EQUAL(psa_mac_update(&func,
  2687. input, sizeof(input)),
  2688. PSA_ERROR_BAD_STATE);
  2689. TEST_EQUAL(psa_mac_update(&init,
  2690. input, sizeof(input)),
  2691. PSA_ERROR_BAD_STATE);
  2692. TEST_EQUAL(psa_mac_update(&zero,
  2693. input, sizeof(input)),
  2694. PSA_ERROR_BAD_STATE);
  2695. /* A default MAC operation should be abortable without error. */
  2696. PSA_ASSERT(psa_mac_abort(&func));
  2697. PSA_ASSERT(psa_mac_abort(&init));
  2698. PSA_ASSERT(psa_mac_abort(&zero));
  2699. }
  2700. /* END_CASE */
  2701. /* BEGIN_CASE */
  2702. void mac_setup(int key_type_arg,
  2703. data_t *key,
  2704. int alg_arg,
  2705. int expected_status_arg)
  2706. {
  2707. psa_key_type_t key_type = key_type_arg;
  2708. psa_algorithm_t alg = alg_arg;
  2709. psa_status_t expected_status = expected_status_arg;
  2710. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  2711. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  2712. #if defined(KNOWN_SUPPORTED_MAC_ALG)
  2713. const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
  2714. #endif
  2715. PSA_ASSERT(psa_crypto_init());
  2716. if (!exercise_mac_setup(key_type, key->x, key->len, alg,
  2717. &operation, &status)) {
  2718. goto exit;
  2719. }
  2720. TEST_EQUAL(status, expected_status);
  2721. /* The operation object should be reusable. */
  2722. #if defined(KNOWN_SUPPORTED_MAC_ALG)
  2723. if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
  2724. smoke_test_key_data,
  2725. sizeof(smoke_test_key_data),
  2726. KNOWN_SUPPORTED_MAC_ALG,
  2727. &operation, &status)) {
  2728. goto exit;
  2729. }
  2730. TEST_EQUAL(status, PSA_SUCCESS);
  2731. #endif
  2732. exit:
  2733. PSA_DONE();
  2734. }
  2735. /* END_CASE */
  2736. /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
  2737. void mac_bad_order()
  2738. {
  2739. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2740. psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
  2741. psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
  2742. const uint8_t key_data[] = {
  2743. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  2744. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  2745. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
  2746. };
  2747. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2748. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  2749. uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
  2750. size_t sign_mac_length = 0;
  2751. const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
  2752. const uint8_t verify_mac[] = {
  2753. 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
  2754. 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
  2755. 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
  2756. };
  2757. PSA_ASSERT(psa_crypto_init());
  2758. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
  2759. psa_set_key_algorithm(&attributes, alg);
  2760. psa_set_key_type(&attributes, key_type);
  2761. PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
  2762. &key));
  2763. /* Call update without calling setup beforehand. */
  2764. TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
  2765. PSA_ERROR_BAD_STATE);
  2766. PSA_ASSERT(psa_mac_abort(&operation));
  2767. /* Call sign finish without calling setup beforehand. */
  2768. TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
  2769. &sign_mac_length),
  2770. PSA_ERROR_BAD_STATE);
  2771. PSA_ASSERT(psa_mac_abort(&operation));
  2772. /* Call verify finish without calling setup beforehand. */
  2773. TEST_EQUAL(psa_mac_verify_finish(&operation,
  2774. verify_mac, sizeof(verify_mac)),
  2775. PSA_ERROR_BAD_STATE);
  2776. PSA_ASSERT(psa_mac_abort(&operation));
  2777. /* Call setup twice in a row. */
  2778. PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
  2779. ASSERT_OPERATION_IS_ACTIVE(operation);
  2780. TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
  2781. PSA_ERROR_BAD_STATE);
  2782. ASSERT_OPERATION_IS_INACTIVE(operation);
  2783. PSA_ASSERT(psa_mac_abort(&operation));
  2784. ASSERT_OPERATION_IS_INACTIVE(operation);
  2785. /* Call update after sign finish. */
  2786. PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
  2787. PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
  2788. PSA_ASSERT(psa_mac_sign_finish(&operation,
  2789. sign_mac, sizeof(sign_mac),
  2790. &sign_mac_length));
  2791. TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
  2792. PSA_ERROR_BAD_STATE);
  2793. PSA_ASSERT(psa_mac_abort(&operation));
  2794. /* Call update after verify finish. */
  2795. PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
  2796. PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
  2797. PSA_ASSERT(psa_mac_verify_finish(&operation,
  2798. verify_mac, sizeof(verify_mac)));
  2799. TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
  2800. PSA_ERROR_BAD_STATE);
  2801. PSA_ASSERT(psa_mac_abort(&operation));
  2802. /* Call sign finish twice in a row. */
  2803. PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
  2804. PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
  2805. PSA_ASSERT(psa_mac_sign_finish(&operation,
  2806. sign_mac, sizeof(sign_mac),
  2807. &sign_mac_length));
  2808. TEST_EQUAL(psa_mac_sign_finish(&operation,
  2809. sign_mac, sizeof(sign_mac),
  2810. &sign_mac_length),
  2811. PSA_ERROR_BAD_STATE);
  2812. PSA_ASSERT(psa_mac_abort(&operation));
  2813. /* Call verify finish twice in a row. */
  2814. PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
  2815. PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
  2816. PSA_ASSERT(psa_mac_verify_finish(&operation,
  2817. verify_mac, sizeof(verify_mac)));
  2818. TEST_EQUAL(psa_mac_verify_finish(&operation,
  2819. verify_mac, sizeof(verify_mac)),
  2820. PSA_ERROR_BAD_STATE);
  2821. PSA_ASSERT(psa_mac_abort(&operation));
  2822. /* Setup sign but try verify. */
  2823. PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
  2824. PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
  2825. ASSERT_OPERATION_IS_ACTIVE(operation);
  2826. TEST_EQUAL(psa_mac_verify_finish(&operation,
  2827. verify_mac, sizeof(verify_mac)),
  2828. PSA_ERROR_BAD_STATE);
  2829. ASSERT_OPERATION_IS_INACTIVE(operation);
  2830. PSA_ASSERT(psa_mac_abort(&operation));
  2831. ASSERT_OPERATION_IS_INACTIVE(operation);
  2832. /* Setup verify but try sign. */
  2833. PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
  2834. PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
  2835. ASSERT_OPERATION_IS_ACTIVE(operation);
  2836. TEST_EQUAL(psa_mac_sign_finish(&operation,
  2837. sign_mac, sizeof(sign_mac),
  2838. &sign_mac_length),
  2839. PSA_ERROR_BAD_STATE);
  2840. ASSERT_OPERATION_IS_INACTIVE(operation);
  2841. PSA_ASSERT(psa_mac_abort(&operation));
  2842. ASSERT_OPERATION_IS_INACTIVE(operation);
  2843. PSA_ASSERT(psa_destroy_key(key));
  2844. exit:
  2845. PSA_DONE();
  2846. }
  2847. /* END_CASE */
  2848. /* BEGIN_CASE */
  2849. void mac_sign_verify_multi(int key_type_arg,
  2850. data_t *key_data,
  2851. int alg_arg,
  2852. data_t *input,
  2853. int is_verify,
  2854. data_t *expected_mac)
  2855. {
  2856. size_t data_part_len = 0;
  2857. for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
  2858. /* Split data into length(data_part_len) parts. */
  2859. mbedtls_test_set_step(2000 + data_part_len);
  2860. if (mac_multipart_internal_func(key_type_arg, key_data,
  2861. alg_arg,
  2862. input, data_part_len,
  2863. expected_mac,
  2864. is_verify, 0) == 0) {
  2865. break;
  2866. }
  2867. /* length(0) part, length(data_part_len) part, length(0) part... */
  2868. mbedtls_test_set_step(3000 + data_part_len);
  2869. if (mac_multipart_internal_func(key_type_arg, key_data,
  2870. alg_arg,
  2871. input, data_part_len,
  2872. expected_mac,
  2873. is_verify, 1) == 0) {
  2874. break;
  2875. }
  2876. }
  2877. /* Goto is required to silence warnings about unused labels, as we
  2878. * don't actually do any test assertions in this function. */
  2879. goto exit;
  2880. }
  2881. /* END_CASE */
  2882. /* BEGIN_CASE */
  2883. void mac_sign(int key_type_arg,
  2884. data_t *key_data,
  2885. int alg_arg,
  2886. data_t *input,
  2887. data_t *expected_mac)
  2888. {
  2889. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2890. psa_key_type_t key_type = key_type_arg;
  2891. psa_algorithm_t alg = alg_arg;
  2892. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  2893. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2894. uint8_t *actual_mac = NULL;
  2895. size_t mac_buffer_size =
  2896. PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
  2897. size_t mac_length = 0;
  2898. const size_t output_sizes_to_test[] = {
  2899. 0,
  2900. 1,
  2901. expected_mac->len - 1,
  2902. expected_mac->len,
  2903. expected_mac->len + 1,
  2904. };
  2905. TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
  2906. /* We expect PSA_MAC_LENGTH to be exact. */
  2907. TEST_ASSERT(expected_mac->len == mac_buffer_size);
  2908. PSA_ASSERT(psa_crypto_init());
  2909. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
  2910. psa_set_key_algorithm(&attributes, alg);
  2911. psa_set_key_type(&attributes, key_type);
  2912. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  2913. &key));
  2914. for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
  2915. const size_t output_size = output_sizes_to_test[i];
  2916. psa_status_t expected_status =
  2917. (output_size >= expected_mac->len ? PSA_SUCCESS :
  2918. PSA_ERROR_BUFFER_TOO_SMALL);
  2919. mbedtls_test_set_step(output_size);
  2920. ASSERT_ALLOC(actual_mac, output_size);
  2921. /* Calculate the MAC, one-shot case. */
  2922. TEST_EQUAL(psa_mac_compute(key, alg,
  2923. input->x, input->len,
  2924. actual_mac, output_size, &mac_length),
  2925. expected_status);
  2926. if (expected_status == PSA_SUCCESS) {
  2927. ASSERT_COMPARE(expected_mac->x, expected_mac->len,
  2928. actual_mac, mac_length);
  2929. }
  2930. if (output_size > 0) {
  2931. memset(actual_mac, 0, output_size);
  2932. }
  2933. /* Calculate the MAC, multi-part case. */
  2934. PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
  2935. PSA_ASSERT(psa_mac_update(&operation,
  2936. input->x, input->len));
  2937. TEST_EQUAL(psa_mac_sign_finish(&operation,
  2938. actual_mac, output_size,
  2939. &mac_length),
  2940. expected_status);
  2941. PSA_ASSERT(psa_mac_abort(&operation));
  2942. if (expected_status == PSA_SUCCESS) {
  2943. ASSERT_COMPARE(expected_mac->x, expected_mac->len,
  2944. actual_mac, mac_length);
  2945. }
  2946. mbedtls_free(actual_mac);
  2947. actual_mac = NULL;
  2948. }
  2949. exit:
  2950. psa_mac_abort(&operation);
  2951. psa_destroy_key(key);
  2952. PSA_DONE();
  2953. mbedtls_free(actual_mac);
  2954. }
  2955. /* END_CASE */
  2956. /* BEGIN_CASE */
  2957. void mac_verify(int key_type_arg,
  2958. data_t *key_data,
  2959. int alg_arg,
  2960. data_t *input,
  2961. data_t *expected_mac)
  2962. {
  2963. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2964. psa_key_type_t key_type = key_type_arg;
  2965. psa_algorithm_t alg = alg_arg;
  2966. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  2967. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2968. uint8_t *perturbed_mac = NULL;
  2969. TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
  2970. PSA_ASSERT(psa_crypto_init());
  2971. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
  2972. psa_set_key_algorithm(&attributes, alg);
  2973. psa_set_key_type(&attributes, key_type);
  2974. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  2975. &key));
  2976. /* Verify correct MAC, one-shot case. */
  2977. PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
  2978. expected_mac->x, expected_mac->len));
  2979. /* Verify correct MAC, multi-part case. */
  2980. PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
  2981. PSA_ASSERT(psa_mac_update(&operation,
  2982. input->x, input->len));
  2983. PSA_ASSERT(psa_mac_verify_finish(&operation,
  2984. expected_mac->x,
  2985. expected_mac->len));
  2986. /* Test a MAC that's too short, one-shot case. */
  2987. TEST_EQUAL(psa_mac_verify(key, alg,
  2988. input->x, input->len,
  2989. expected_mac->x,
  2990. expected_mac->len - 1),
  2991. PSA_ERROR_INVALID_SIGNATURE);
  2992. /* Test a MAC that's too short, multi-part case. */
  2993. PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
  2994. PSA_ASSERT(psa_mac_update(&operation,
  2995. input->x, input->len));
  2996. TEST_EQUAL(psa_mac_verify_finish(&operation,
  2997. expected_mac->x,
  2998. expected_mac->len - 1),
  2999. PSA_ERROR_INVALID_SIGNATURE);
  3000. /* Test a MAC that's too long, one-shot case. */
  3001. ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
  3002. memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
  3003. TEST_EQUAL(psa_mac_verify(key, alg,
  3004. input->x, input->len,
  3005. perturbed_mac, expected_mac->len + 1),
  3006. PSA_ERROR_INVALID_SIGNATURE);
  3007. /* Test a MAC that's too long, multi-part case. */
  3008. PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
  3009. PSA_ASSERT(psa_mac_update(&operation,
  3010. input->x, input->len));
  3011. TEST_EQUAL(psa_mac_verify_finish(&operation,
  3012. perturbed_mac,
  3013. expected_mac->len + 1),
  3014. PSA_ERROR_INVALID_SIGNATURE);
  3015. /* Test changing one byte. */
  3016. for (size_t i = 0; i < expected_mac->len; i++) {
  3017. mbedtls_test_set_step(i);
  3018. perturbed_mac[i] ^= 1;
  3019. TEST_EQUAL(psa_mac_verify(key, alg,
  3020. input->x, input->len,
  3021. perturbed_mac, expected_mac->len),
  3022. PSA_ERROR_INVALID_SIGNATURE);
  3023. PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
  3024. PSA_ASSERT(psa_mac_update(&operation,
  3025. input->x, input->len));
  3026. TEST_EQUAL(psa_mac_verify_finish(&operation,
  3027. perturbed_mac,
  3028. expected_mac->len),
  3029. PSA_ERROR_INVALID_SIGNATURE);
  3030. perturbed_mac[i] ^= 1;
  3031. }
  3032. exit:
  3033. psa_mac_abort(&operation);
  3034. psa_destroy_key(key);
  3035. PSA_DONE();
  3036. mbedtls_free(perturbed_mac);
  3037. }
  3038. /* END_CASE */
  3039. /* BEGIN_CASE */
  3040. void cipher_operation_init()
  3041. {
  3042. const uint8_t input[1] = { 0 };
  3043. unsigned char output[1] = { 0 };
  3044. size_t output_length;
  3045. /* Test each valid way of initializing the object, except for `= {0}`, as
  3046. * Clang 5 complains when `-Wmissing-field-initializers` is used, even
  3047. * though it's OK by the C standard. We could test for this, but we'd need
  3048. * to suppress the Clang warning for the test. */
  3049. psa_cipher_operation_t func = psa_cipher_operation_init();
  3050. psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
  3051. psa_cipher_operation_t zero;
  3052. memset(&zero, 0, sizeof(zero));
  3053. /* A freshly-initialized cipher operation should not be usable. */
  3054. TEST_EQUAL(psa_cipher_update(&func,
  3055. input, sizeof(input),
  3056. output, sizeof(output),
  3057. &output_length),
  3058. PSA_ERROR_BAD_STATE);
  3059. TEST_EQUAL(psa_cipher_update(&init,
  3060. input, sizeof(input),
  3061. output, sizeof(output),
  3062. &output_length),
  3063. PSA_ERROR_BAD_STATE);
  3064. TEST_EQUAL(psa_cipher_update(&zero,
  3065. input, sizeof(input),
  3066. output, sizeof(output),
  3067. &output_length),
  3068. PSA_ERROR_BAD_STATE);
  3069. /* A default cipher operation should be abortable without error. */
  3070. PSA_ASSERT(psa_cipher_abort(&func));
  3071. PSA_ASSERT(psa_cipher_abort(&init));
  3072. PSA_ASSERT(psa_cipher_abort(&zero));
  3073. }
  3074. /* END_CASE */
  3075. /* BEGIN_CASE */
  3076. void cipher_setup(int key_type_arg,
  3077. data_t *key,
  3078. int alg_arg,
  3079. int expected_status_arg)
  3080. {
  3081. psa_key_type_t key_type = key_type_arg;
  3082. psa_algorithm_t alg = alg_arg;
  3083. psa_status_t expected_status = expected_status_arg;
  3084. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  3085. psa_status_t status;
  3086. #if defined(KNOWN_SUPPORTED_CIPHER_ALG)
  3087. const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
  3088. #endif
  3089. PSA_ASSERT(psa_crypto_init());
  3090. if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
  3091. &operation, &status)) {
  3092. goto exit;
  3093. }
  3094. TEST_EQUAL(status, expected_status);
  3095. /* The operation object should be reusable. */
  3096. #if defined(KNOWN_SUPPORTED_CIPHER_ALG)
  3097. if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
  3098. smoke_test_key_data,
  3099. sizeof(smoke_test_key_data),
  3100. KNOWN_SUPPORTED_CIPHER_ALG,
  3101. &operation, &status)) {
  3102. goto exit;
  3103. }
  3104. TEST_EQUAL(status, PSA_SUCCESS);
  3105. #endif
  3106. exit:
  3107. psa_cipher_abort(&operation);
  3108. PSA_DONE();
  3109. }
  3110. /* END_CASE */
  3111. /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
  3112. void cipher_bad_order()
  3113. {
  3114. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3115. psa_key_type_t key_type = PSA_KEY_TYPE_AES;
  3116. psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
  3117. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3118. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  3119. unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
  3120. const uint8_t key_data[] = {
  3121. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  3122. 0xaa, 0xaa, 0xaa, 0xaa
  3123. };
  3124. const uint8_t text[] = {
  3125. 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
  3126. 0xbb, 0xbb, 0xbb, 0xbb
  3127. };
  3128. uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
  3129. size_t length = 0;
  3130. PSA_ASSERT(psa_crypto_init());
  3131. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
  3132. psa_set_key_algorithm(&attributes, alg);
  3133. psa_set_key_type(&attributes, key_type);
  3134. PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
  3135. &key));
  3136. /* Call encrypt setup twice in a row. */
  3137. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3138. ASSERT_OPERATION_IS_ACTIVE(operation);
  3139. TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
  3140. PSA_ERROR_BAD_STATE);
  3141. ASSERT_OPERATION_IS_INACTIVE(operation);
  3142. PSA_ASSERT(psa_cipher_abort(&operation));
  3143. ASSERT_OPERATION_IS_INACTIVE(operation);
  3144. /* Call decrypt setup twice in a row. */
  3145. PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
  3146. ASSERT_OPERATION_IS_ACTIVE(operation);
  3147. TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
  3148. PSA_ERROR_BAD_STATE);
  3149. ASSERT_OPERATION_IS_INACTIVE(operation);
  3150. PSA_ASSERT(psa_cipher_abort(&operation));
  3151. ASSERT_OPERATION_IS_INACTIVE(operation);
  3152. /* Generate an IV without calling setup beforehand. */
  3153. TEST_EQUAL(psa_cipher_generate_iv(&operation,
  3154. buffer, sizeof(buffer),
  3155. &length),
  3156. PSA_ERROR_BAD_STATE);
  3157. PSA_ASSERT(psa_cipher_abort(&operation));
  3158. /* Generate an IV twice in a row. */
  3159. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3160. PSA_ASSERT(psa_cipher_generate_iv(&operation,
  3161. buffer, sizeof(buffer),
  3162. &length));
  3163. ASSERT_OPERATION_IS_ACTIVE(operation);
  3164. TEST_EQUAL(psa_cipher_generate_iv(&operation,
  3165. buffer, sizeof(buffer),
  3166. &length),
  3167. PSA_ERROR_BAD_STATE);
  3168. ASSERT_OPERATION_IS_INACTIVE(operation);
  3169. PSA_ASSERT(psa_cipher_abort(&operation));
  3170. ASSERT_OPERATION_IS_INACTIVE(operation);
  3171. /* Generate an IV after it's already set. */
  3172. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3173. PSA_ASSERT(psa_cipher_set_iv(&operation,
  3174. iv, sizeof(iv)));
  3175. TEST_EQUAL(psa_cipher_generate_iv(&operation,
  3176. buffer, sizeof(buffer),
  3177. &length),
  3178. PSA_ERROR_BAD_STATE);
  3179. PSA_ASSERT(psa_cipher_abort(&operation));
  3180. /* Set an IV without calling setup beforehand. */
  3181. TEST_EQUAL(psa_cipher_set_iv(&operation,
  3182. iv, sizeof(iv)),
  3183. PSA_ERROR_BAD_STATE);
  3184. PSA_ASSERT(psa_cipher_abort(&operation));
  3185. /* Set an IV after it's already set. */
  3186. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3187. PSA_ASSERT(psa_cipher_set_iv(&operation,
  3188. iv, sizeof(iv)));
  3189. ASSERT_OPERATION_IS_ACTIVE(operation);
  3190. TEST_EQUAL(psa_cipher_set_iv(&operation,
  3191. iv, sizeof(iv)),
  3192. PSA_ERROR_BAD_STATE);
  3193. ASSERT_OPERATION_IS_INACTIVE(operation);
  3194. PSA_ASSERT(psa_cipher_abort(&operation));
  3195. ASSERT_OPERATION_IS_INACTIVE(operation);
  3196. /* Set an IV after it's already generated. */
  3197. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3198. PSA_ASSERT(psa_cipher_generate_iv(&operation,
  3199. buffer, sizeof(buffer),
  3200. &length));
  3201. TEST_EQUAL(psa_cipher_set_iv(&operation,
  3202. iv, sizeof(iv)),
  3203. PSA_ERROR_BAD_STATE);
  3204. PSA_ASSERT(psa_cipher_abort(&operation));
  3205. /* Call update without calling setup beforehand. */
  3206. TEST_EQUAL(psa_cipher_update(&operation,
  3207. text, sizeof(text),
  3208. buffer, sizeof(buffer),
  3209. &length),
  3210. PSA_ERROR_BAD_STATE);
  3211. PSA_ASSERT(psa_cipher_abort(&operation));
  3212. /* Call update without an IV where an IV is required. */
  3213. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3214. ASSERT_OPERATION_IS_ACTIVE(operation);
  3215. TEST_EQUAL(psa_cipher_update(&operation,
  3216. text, sizeof(text),
  3217. buffer, sizeof(buffer),
  3218. &length),
  3219. PSA_ERROR_BAD_STATE);
  3220. ASSERT_OPERATION_IS_INACTIVE(operation);
  3221. PSA_ASSERT(psa_cipher_abort(&operation));
  3222. ASSERT_OPERATION_IS_INACTIVE(operation);
  3223. /* Call update after finish. */
  3224. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3225. PSA_ASSERT(psa_cipher_set_iv(&operation,
  3226. iv, sizeof(iv)));
  3227. PSA_ASSERT(psa_cipher_finish(&operation,
  3228. buffer, sizeof(buffer), &length));
  3229. TEST_EQUAL(psa_cipher_update(&operation,
  3230. text, sizeof(text),
  3231. buffer, sizeof(buffer),
  3232. &length),
  3233. PSA_ERROR_BAD_STATE);
  3234. PSA_ASSERT(psa_cipher_abort(&operation));
  3235. /* Call finish without calling setup beforehand. */
  3236. TEST_EQUAL(psa_cipher_finish(&operation,
  3237. buffer, sizeof(buffer), &length),
  3238. PSA_ERROR_BAD_STATE);
  3239. PSA_ASSERT(psa_cipher_abort(&operation));
  3240. /* Call finish without an IV where an IV is required. */
  3241. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3242. /* Not calling update means we are encrypting an empty buffer, which is OK
  3243. * for cipher modes with padding. */
  3244. ASSERT_OPERATION_IS_ACTIVE(operation);
  3245. TEST_EQUAL(psa_cipher_finish(&operation,
  3246. buffer, sizeof(buffer), &length),
  3247. PSA_ERROR_BAD_STATE);
  3248. ASSERT_OPERATION_IS_INACTIVE(operation);
  3249. PSA_ASSERT(psa_cipher_abort(&operation));
  3250. ASSERT_OPERATION_IS_INACTIVE(operation);
  3251. /* Call finish twice in a row. */
  3252. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3253. PSA_ASSERT(psa_cipher_set_iv(&operation,
  3254. iv, sizeof(iv)));
  3255. PSA_ASSERT(psa_cipher_finish(&operation,
  3256. buffer, sizeof(buffer), &length));
  3257. TEST_EQUAL(psa_cipher_finish(&operation,
  3258. buffer, sizeof(buffer), &length),
  3259. PSA_ERROR_BAD_STATE);
  3260. PSA_ASSERT(psa_cipher_abort(&operation));
  3261. PSA_ASSERT(psa_destroy_key(key));
  3262. exit:
  3263. psa_cipher_abort(&operation);
  3264. PSA_DONE();
  3265. }
  3266. /* END_CASE */
  3267. /* BEGIN_CASE */
  3268. void cipher_encrypt_fail(int alg_arg,
  3269. int key_type_arg,
  3270. data_t *key_data,
  3271. data_t *input,
  3272. int expected_status_arg)
  3273. {
  3274. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3275. psa_status_t status;
  3276. psa_key_type_t key_type = key_type_arg;
  3277. psa_algorithm_t alg = alg_arg;
  3278. psa_status_t expected_status = expected_status_arg;
  3279. unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
  3280. size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
  3281. size_t iv_length = 0;
  3282. unsigned char *output = NULL;
  3283. size_t output_buffer_size = 0;
  3284. size_t output_length = 0;
  3285. size_t function_output_length;
  3286. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  3287. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3288. if (PSA_ERROR_BAD_STATE != expected_status) {
  3289. PSA_ASSERT(psa_crypto_init());
  3290. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  3291. psa_set_key_algorithm(&attributes, alg);
  3292. psa_set_key_type(&attributes, key_type);
  3293. output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
  3294. input->len);
  3295. ASSERT_ALLOC(output, output_buffer_size);
  3296. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  3297. &key));
  3298. }
  3299. /* Encrypt, one-shot */
  3300. status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
  3301. output_buffer_size, &output_length);
  3302. TEST_EQUAL(status, expected_status);
  3303. /* Encrypt, multi-part */
  3304. status = psa_cipher_encrypt_setup(&operation, key, alg);
  3305. if (status == PSA_SUCCESS) {
  3306. if (alg != PSA_ALG_ECB_NO_PADDING) {
  3307. PSA_ASSERT(psa_cipher_generate_iv(&operation,
  3308. iv, iv_size,
  3309. &iv_length));
  3310. }
  3311. status = psa_cipher_update(&operation, input->x, input->len,
  3312. output, output_buffer_size,
  3313. &function_output_length);
  3314. if (status == PSA_SUCCESS) {
  3315. output_length += function_output_length;
  3316. status = psa_cipher_finish(&operation, output + output_length,
  3317. output_buffer_size - output_length,
  3318. &function_output_length);
  3319. TEST_EQUAL(status, expected_status);
  3320. } else {
  3321. TEST_EQUAL(status, expected_status);
  3322. }
  3323. } else {
  3324. TEST_EQUAL(status, expected_status);
  3325. }
  3326. exit:
  3327. psa_cipher_abort(&operation);
  3328. mbedtls_free(output);
  3329. psa_destroy_key(key);
  3330. PSA_DONE();
  3331. }
  3332. /* END_CASE */
  3333. /* BEGIN_CASE */
  3334. void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
  3335. data_t *input, int iv_length,
  3336. int expected_result)
  3337. {
  3338. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3339. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  3340. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3341. size_t output_buffer_size = 0;
  3342. unsigned char *output = NULL;
  3343. output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
  3344. ASSERT_ALLOC(output, output_buffer_size);
  3345. PSA_ASSERT(psa_crypto_init());
  3346. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  3347. psa_set_key_algorithm(&attributes, alg);
  3348. psa_set_key_type(&attributes, key_type);
  3349. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  3350. &key));
  3351. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3352. TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
  3353. iv_length));
  3354. exit:
  3355. psa_cipher_abort(&operation);
  3356. mbedtls_free(output);
  3357. psa_destroy_key(key);
  3358. PSA_DONE();
  3359. }
  3360. /* END_CASE */
  3361. /* BEGIN_CASE */
  3362. void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
  3363. data_t *plaintext, data_t *ciphertext)
  3364. {
  3365. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3366. psa_key_type_t key_type = key_type_arg;
  3367. psa_algorithm_t alg = alg_arg;
  3368. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  3369. uint8_t iv[1] = { 0x5a };
  3370. unsigned char *output = NULL;
  3371. size_t output_buffer_size = 0;
  3372. size_t output_length, length;
  3373. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3374. PSA_ASSERT(psa_crypto_init());
  3375. /* Validate size macros */
  3376. TEST_LE_U(ciphertext->len,
  3377. PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
  3378. TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
  3379. PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
  3380. TEST_LE_U(plaintext->len,
  3381. PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
  3382. TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
  3383. PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
  3384. /* Set up key and output buffer */
  3385. psa_set_key_usage_flags(&attributes,
  3386. PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
  3387. psa_set_key_algorithm(&attributes, alg);
  3388. psa_set_key_type(&attributes, key_type);
  3389. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  3390. &key));
  3391. output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
  3392. plaintext->len);
  3393. ASSERT_ALLOC(output, output_buffer_size);
  3394. /* set_iv() is not allowed */
  3395. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3396. TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
  3397. PSA_ERROR_BAD_STATE);
  3398. PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
  3399. TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
  3400. PSA_ERROR_BAD_STATE);
  3401. /* generate_iv() is not allowed */
  3402. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3403. TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
  3404. &length),
  3405. PSA_ERROR_BAD_STATE);
  3406. PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
  3407. TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
  3408. &length),
  3409. PSA_ERROR_BAD_STATE);
  3410. /* Multipart encryption */
  3411. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3412. output_length = 0;
  3413. length = ~0;
  3414. PSA_ASSERT(psa_cipher_update(&operation,
  3415. plaintext->x, plaintext->len,
  3416. output, output_buffer_size,
  3417. &length));
  3418. TEST_LE_U(length, output_buffer_size);
  3419. output_length += length;
  3420. PSA_ASSERT(psa_cipher_finish(&operation,
  3421. mbedtls_buffer_offset(output, output_length),
  3422. output_buffer_size - output_length,
  3423. &length));
  3424. output_length += length;
  3425. ASSERT_COMPARE(ciphertext->x, ciphertext->len,
  3426. output, output_length);
  3427. /* Multipart encryption */
  3428. PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
  3429. output_length = 0;
  3430. length = ~0;
  3431. PSA_ASSERT(psa_cipher_update(&operation,
  3432. ciphertext->x, ciphertext->len,
  3433. output, output_buffer_size,
  3434. &length));
  3435. TEST_LE_U(length, output_buffer_size);
  3436. output_length += length;
  3437. PSA_ASSERT(psa_cipher_finish(&operation,
  3438. mbedtls_buffer_offset(output, output_length),
  3439. output_buffer_size - output_length,
  3440. &length));
  3441. output_length += length;
  3442. ASSERT_COMPARE(plaintext->x, plaintext->len,
  3443. output, output_length);
  3444. /* One-shot encryption */
  3445. output_length = ~0;
  3446. PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
  3447. output, output_buffer_size,
  3448. &output_length));
  3449. ASSERT_COMPARE(ciphertext->x, ciphertext->len,
  3450. output, output_length);
  3451. /* One-shot decryption */
  3452. output_length = ~0;
  3453. PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
  3454. output, output_buffer_size,
  3455. &output_length));
  3456. ASSERT_COMPARE(plaintext->x, plaintext->len,
  3457. output, output_length);
  3458. exit:
  3459. PSA_ASSERT(psa_cipher_abort(&operation));
  3460. mbedtls_free(output);
  3461. psa_cipher_abort(&operation);
  3462. psa_destroy_key(key);
  3463. PSA_DONE();
  3464. }
  3465. /* END_CASE */
  3466. /* BEGIN_CASE */
  3467. void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
  3468. {
  3469. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3470. psa_algorithm_t alg = alg_arg;
  3471. psa_key_type_t key_type = key_type_arg;
  3472. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3473. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  3474. psa_status_t status;
  3475. PSA_ASSERT(psa_crypto_init());
  3476. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  3477. psa_set_key_algorithm(&attributes, alg);
  3478. psa_set_key_type(&attributes, key_type);
  3479. /* Usage of either of these two size macros would cause divide by zero
  3480. * with incorrect key types previously. Input length should be irrelevant
  3481. * here. */
  3482. TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
  3483. 0);
  3484. TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
  3485. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  3486. &key));
  3487. /* Should fail due to invalid alg type (to support invalid key type).
  3488. * Encrypt or decrypt will end up in the same place. */
  3489. status = psa_cipher_encrypt_setup(&operation, key, alg);
  3490. TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
  3491. exit:
  3492. psa_cipher_abort(&operation);
  3493. psa_destroy_key(key);
  3494. PSA_DONE();
  3495. }
  3496. /* END_CASE */
  3497. /* BEGIN_CASE */
  3498. void cipher_encrypt_validation(int alg_arg,
  3499. int key_type_arg,
  3500. data_t *key_data,
  3501. data_t *input)
  3502. {
  3503. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3504. psa_key_type_t key_type = key_type_arg;
  3505. psa_algorithm_t alg = alg_arg;
  3506. size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
  3507. unsigned char *output1 = NULL;
  3508. size_t output1_buffer_size = 0;
  3509. size_t output1_length = 0;
  3510. unsigned char *output2 = NULL;
  3511. size_t output2_buffer_size = 0;
  3512. size_t output2_length = 0;
  3513. size_t function_output_length = 0;
  3514. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  3515. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3516. PSA_ASSERT(psa_crypto_init());
  3517. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  3518. psa_set_key_algorithm(&attributes, alg);
  3519. psa_set_key_type(&attributes, key_type);
  3520. output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
  3521. output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
  3522. PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
  3523. ASSERT_ALLOC(output1, output1_buffer_size);
  3524. ASSERT_ALLOC(output2, output2_buffer_size);
  3525. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  3526. &key));
  3527. /* The one-shot cipher encryption uses generated iv so validating
  3528. the output is not possible. Validating with multipart encryption. */
  3529. PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
  3530. output1_buffer_size, &output1_length));
  3531. TEST_LE_U(output1_length,
  3532. PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
  3533. TEST_LE_U(output1_length,
  3534. PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
  3535. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3536. PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
  3537. PSA_ASSERT(psa_cipher_update(&operation,
  3538. input->x, input->len,
  3539. output2, output2_buffer_size,
  3540. &function_output_length));
  3541. TEST_LE_U(function_output_length,
  3542. PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
  3543. TEST_LE_U(function_output_length,
  3544. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
  3545. output2_length += function_output_length;
  3546. PSA_ASSERT(psa_cipher_finish(&operation,
  3547. output2 + output2_length,
  3548. output2_buffer_size - output2_length,
  3549. &function_output_length));
  3550. TEST_LE_U(function_output_length,
  3551. PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
  3552. TEST_LE_U(function_output_length,
  3553. PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
  3554. output2_length += function_output_length;
  3555. PSA_ASSERT(psa_cipher_abort(&operation));
  3556. ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
  3557. output2, output2_length);
  3558. exit:
  3559. psa_cipher_abort(&operation);
  3560. mbedtls_free(output1);
  3561. mbedtls_free(output2);
  3562. psa_destroy_key(key);
  3563. PSA_DONE();
  3564. }
  3565. /* END_CASE */
  3566. /* BEGIN_CASE */
  3567. void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
  3568. data_t *key_data, data_t *iv,
  3569. data_t *input,
  3570. int first_part_size_arg,
  3571. int output1_length_arg, int output2_length_arg,
  3572. data_t *expected_output,
  3573. int expected_status_arg)
  3574. {
  3575. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3576. psa_key_type_t key_type = key_type_arg;
  3577. psa_algorithm_t alg = alg_arg;
  3578. psa_status_t status;
  3579. psa_status_t expected_status = expected_status_arg;
  3580. size_t first_part_size = first_part_size_arg;
  3581. size_t output1_length = output1_length_arg;
  3582. size_t output2_length = output2_length_arg;
  3583. unsigned char *output = NULL;
  3584. size_t output_buffer_size = 0;
  3585. size_t function_output_length = 0;
  3586. size_t total_output_length = 0;
  3587. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  3588. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3589. PSA_ASSERT(psa_crypto_init());
  3590. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  3591. psa_set_key_algorithm(&attributes, alg);
  3592. psa_set_key_type(&attributes, key_type);
  3593. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  3594. &key));
  3595. PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
  3596. if (iv->len > 0) {
  3597. PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
  3598. }
  3599. output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
  3600. PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
  3601. ASSERT_ALLOC(output, output_buffer_size);
  3602. TEST_LE_U(first_part_size, input->len);
  3603. PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
  3604. output, output_buffer_size,
  3605. &function_output_length));
  3606. TEST_ASSERT(function_output_length == output1_length);
  3607. TEST_LE_U(function_output_length,
  3608. PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
  3609. TEST_LE_U(function_output_length,
  3610. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
  3611. total_output_length += function_output_length;
  3612. if (first_part_size < input->len) {
  3613. PSA_ASSERT(psa_cipher_update(&operation,
  3614. input->x + first_part_size,
  3615. input->len - first_part_size,
  3616. (output_buffer_size == 0 ? NULL :
  3617. output + total_output_length),
  3618. output_buffer_size - total_output_length,
  3619. &function_output_length));
  3620. TEST_ASSERT(function_output_length == output2_length);
  3621. TEST_LE_U(function_output_length,
  3622. PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
  3623. alg,
  3624. input->len - first_part_size));
  3625. TEST_LE_U(function_output_length,
  3626. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
  3627. total_output_length += function_output_length;
  3628. }
  3629. status = psa_cipher_finish(&operation,
  3630. (output_buffer_size == 0 ? NULL :
  3631. output + total_output_length),
  3632. output_buffer_size - total_output_length,
  3633. &function_output_length);
  3634. TEST_LE_U(function_output_length,
  3635. PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
  3636. TEST_LE_U(function_output_length,
  3637. PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
  3638. total_output_length += function_output_length;
  3639. TEST_EQUAL(status, expected_status);
  3640. if (expected_status == PSA_SUCCESS) {
  3641. PSA_ASSERT(psa_cipher_abort(&operation));
  3642. ASSERT_COMPARE(expected_output->x, expected_output->len,
  3643. output, total_output_length);
  3644. }
  3645. exit:
  3646. psa_cipher_abort(&operation);
  3647. mbedtls_free(output);
  3648. psa_destroy_key(key);
  3649. PSA_DONE();
  3650. }
  3651. /* END_CASE */
  3652. /* BEGIN_CASE */
  3653. void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
  3654. data_t *key_data, data_t *iv,
  3655. data_t *input,
  3656. int first_part_size_arg,
  3657. int output1_length_arg, int output2_length_arg,
  3658. data_t *expected_output,
  3659. int expected_status_arg)
  3660. {
  3661. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3662. psa_key_type_t key_type = key_type_arg;
  3663. psa_algorithm_t alg = alg_arg;
  3664. psa_status_t status;
  3665. psa_status_t expected_status = expected_status_arg;
  3666. size_t first_part_size = first_part_size_arg;
  3667. size_t output1_length = output1_length_arg;
  3668. size_t output2_length = output2_length_arg;
  3669. unsigned char *output = NULL;
  3670. size_t output_buffer_size = 0;
  3671. size_t function_output_length = 0;
  3672. size_t total_output_length = 0;
  3673. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  3674. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3675. PSA_ASSERT(psa_crypto_init());
  3676. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
  3677. psa_set_key_algorithm(&attributes, alg);
  3678. psa_set_key_type(&attributes, key_type);
  3679. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  3680. &key));
  3681. PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
  3682. if (iv->len > 0) {
  3683. PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
  3684. }
  3685. output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
  3686. PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
  3687. ASSERT_ALLOC(output, output_buffer_size);
  3688. TEST_LE_U(first_part_size, input->len);
  3689. PSA_ASSERT(psa_cipher_update(&operation,
  3690. input->x, first_part_size,
  3691. output, output_buffer_size,
  3692. &function_output_length));
  3693. TEST_ASSERT(function_output_length == output1_length);
  3694. TEST_LE_U(function_output_length,
  3695. PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
  3696. TEST_LE_U(function_output_length,
  3697. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
  3698. total_output_length += function_output_length;
  3699. if (first_part_size < input->len) {
  3700. PSA_ASSERT(psa_cipher_update(&operation,
  3701. input->x + first_part_size,
  3702. input->len - first_part_size,
  3703. (output_buffer_size == 0 ? NULL :
  3704. output + total_output_length),
  3705. output_buffer_size - total_output_length,
  3706. &function_output_length));
  3707. TEST_ASSERT(function_output_length == output2_length);
  3708. TEST_LE_U(function_output_length,
  3709. PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
  3710. alg,
  3711. input->len - first_part_size));
  3712. TEST_LE_U(function_output_length,
  3713. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
  3714. total_output_length += function_output_length;
  3715. }
  3716. status = psa_cipher_finish(&operation,
  3717. (output_buffer_size == 0 ? NULL :
  3718. output + total_output_length),
  3719. output_buffer_size - total_output_length,
  3720. &function_output_length);
  3721. TEST_LE_U(function_output_length,
  3722. PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
  3723. TEST_LE_U(function_output_length,
  3724. PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
  3725. total_output_length += function_output_length;
  3726. TEST_EQUAL(status, expected_status);
  3727. if (expected_status == PSA_SUCCESS) {
  3728. PSA_ASSERT(psa_cipher_abort(&operation));
  3729. ASSERT_COMPARE(expected_output->x, expected_output->len,
  3730. output, total_output_length);
  3731. }
  3732. exit:
  3733. psa_cipher_abort(&operation);
  3734. mbedtls_free(output);
  3735. psa_destroy_key(key);
  3736. PSA_DONE();
  3737. }
  3738. /* END_CASE */
  3739. /* BEGIN_CASE */
  3740. void cipher_decrypt_fail(int alg_arg,
  3741. int key_type_arg,
  3742. data_t *key_data,
  3743. data_t *iv,
  3744. data_t *input_arg,
  3745. int expected_status_arg)
  3746. {
  3747. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3748. psa_status_t status;
  3749. psa_key_type_t key_type = key_type_arg;
  3750. psa_algorithm_t alg = alg_arg;
  3751. psa_status_t expected_status = expected_status_arg;
  3752. unsigned char *input = NULL;
  3753. size_t input_buffer_size = 0;
  3754. unsigned char *output = NULL;
  3755. unsigned char *output_multi = NULL;
  3756. size_t output_buffer_size = 0;
  3757. size_t output_length = 0;
  3758. size_t function_output_length;
  3759. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  3760. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3761. if (PSA_ERROR_BAD_STATE != expected_status) {
  3762. PSA_ASSERT(psa_crypto_init());
  3763. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
  3764. psa_set_key_algorithm(&attributes, alg);
  3765. psa_set_key_type(&attributes, key_type);
  3766. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  3767. &key));
  3768. }
  3769. /* Allocate input buffer and copy the iv and the plaintext */
  3770. input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
  3771. if (input_buffer_size > 0) {
  3772. ASSERT_ALLOC(input, input_buffer_size);
  3773. memcpy(input, iv->x, iv->len);
  3774. memcpy(input + iv->len, input_arg->x, input_arg->len);
  3775. }
  3776. output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
  3777. ASSERT_ALLOC(output, output_buffer_size);
  3778. /* Decrypt, one-short */
  3779. status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
  3780. output_buffer_size, &output_length);
  3781. TEST_EQUAL(status, expected_status);
  3782. /* Decrypt, multi-part */
  3783. status = psa_cipher_decrypt_setup(&operation, key, alg);
  3784. if (status == PSA_SUCCESS) {
  3785. output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
  3786. input_arg->len) +
  3787. PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
  3788. ASSERT_ALLOC(output_multi, output_buffer_size);
  3789. if (iv->len > 0) {
  3790. status = psa_cipher_set_iv(&operation, iv->x, iv->len);
  3791. if (status != PSA_SUCCESS) {
  3792. TEST_EQUAL(status, expected_status);
  3793. }
  3794. }
  3795. if (status == PSA_SUCCESS) {
  3796. status = psa_cipher_update(&operation,
  3797. input_arg->x, input_arg->len,
  3798. output_multi, output_buffer_size,
  3799. &function_output_length);
  3800. if (status == PSA_SUCCESS) {
  3801. output_length = function_output_length;
  3802. status = psa_cipher_finish(&operation,
  3803. output_multi + output_length,
  3804. output_buffer_size - output_length,
  3805. &function_output_length);
  3806. TEST_EQUAL(status, expected_status);
  3807. } else {
  3808. TEST_EQUAL(status, expected_status);
  3809. }
  3810. } else {
  3811. TEST_EQUAL(status, expected_status);
  3812. }
  3813. } else {
  3814. TEST_EQUAL(status, expected_status);
  3815. }
  3816. exit:
  3817. psa_cipher_abort(&operation);
  3818. mbedtls_free(input);
  3819. mbedtls_free(output);
  3820. mbedtls_free(output_multi);
  3821. psa_destroy_key(key);
  3822. PSA_DONE();
  3823. }
  3824. /* END_CASE */
  3825. /* BEGIN_CASE */
  3826. void cipher_decrypt(int alg_arg,
  3827. int key_type_arg,
  3828. data_t *key_data,
  3829. data_t *iv,
  3830. data_t *input_arg,
  3831. data_t *expected_output)
  3832. {
  3833. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3834. psa_key_type_t key_type = key_type_arg;
  3835. psa_algorithm_t alg = alg_arg;
  3836. unsigned char *input = NULL;
  3837. size_t input_buffer_size = 0;
  3838. unsigned char *output = NULL;
  3839. size_t output_buffer_size = 0;
  3840. size_t output_length = 0;
  3841. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3842. PSA_ASSERT(psa_crypto_init());
  3843. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
  3844. psa_set_key_algorithm(&attributes, alg);
  3845. psa_set_key_type(&attributes, key_type);
  3846. /* Allocate input buffer and copy the iv and the plaintext */
  3847. input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
  3848. if (input_buffer_size > 0) {
  3849. ASSERT_ALLOC(input, input_buffer_size);
  3850. memcpy(input, iv->x, iv->len);
  3851. memcpy(input + iv->len, input_arg->x, input_arg->len);
  3852. }
  3853. output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
  3854. ASSERT_ALLOC(output, output_buffer_size);
  3855. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  3856. &key));
  3857. PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
  3858. output_buffer_size, &output_length));
  3859. TEST_LE_U(output_length,
  3860. PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
  3861. TEST_LE_U(output_length,
  3862. PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
  3863. ASSERT_COMPARE(expected_output->x, expected_output->len,
  3864. output, output_length);
  3865. exit:
  3866. mbedtls_free(input);
  3867. mbedtls_free(output);
  3868. psa_destroy_key(key);
  3869. PSA_DONE();
  3870. }
  3871. /* END_CASE */
  3872. /* BEGIN_CASE */
  3873. void cipher_verify_output(int alg_arg,
  3874. int key_type_arg,
  3875. data_t *key_data,
  3876. data_t *input)
  3877. {
  3878. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3879. psa_key_type_t key_type = key_type_arg;
  3880. psa_algorithm_t alg = alg_arg;
  3881. unsigned char *output1 = NULL;
  3882. size_t output1_size = 0;
  3883. size_t output1_length = 0;
  3884. unsigned char *output2 = NULL;
  3885. size_t output2_size = 0;
  3886. size_t output2_length = 0;
  3887. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3888. PSA_ASSERT(psa_crypto_init());
  3889. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
  3890. psa_set_key_algorithm(&attributes, alg);
  3891. psa_set_key_type(&attributes, key_type);
  3892. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  3893. &key));
  3894. output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
  3895. ASSERT_ALLOC(output1, output1_size);
  3896. PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
  3897. output1, output1_size,
  3898. &output1_length));
  3899. TEST_LE_U(output1_length,
  3900. PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
  3901. TEST_LE_U(output1_length,
  3902. PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
  3903. output2_size = output1_length;
  3904. ASSERT_ALLOC(output2, output2_size);
  3905. PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
  3906. output2, output2_size,
  3907. &output2_length));
  3908. TEST_LE_U(output2_length,
  3909. PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
  3910. TEST_LE_U(output2_length,
  3911. PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
  3912. ASSERT_COMPARE(input->x, input->len, output2, output2_length);
  3913. exit:
  3914. mbedtls_free(output1);
  3915. mbedtls_free(output2);
  3916. psa_destroy_key(key);
  3917. PSA_DONE();
  3918. }
  3919. /* END_CASE */
  3920. /* BEGIN_CASE */
  3921. void cipher_verify_output_multipart(int alg_arg,
  3922. int key_type_arg,
  3923. data_t *key_data,
  3924. data_t *input,
  3925. int first_part_size_arg)
  3926. {
  3927. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3928. psa_key_type_t key_type = key_type_arg;
  3929. psa_algorithm_t alg = alg_arg;
  3930. size_t first_part_size = first_part_size_arg;
  3931. unsigned char iv[16] = { 0 };
  3932. size_t iv_size = 16;
  3933. size_t iv_length = 0;
  3934. unsigned char *output1 = NULL;
  3935. size_t output1_buffer_size = 0;
  3936. size_t output1_length = 0;
  3937. unsigned char *output2 = NULL;
  3938. size_t output2_buffer_size = 0;
  3939. size_t output2_length = 0;
  3940. size_t function_output_length;
  3941. psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
  3942. psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
  3943. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3944. PSA_ASSERT(psa_crypto_init());
  3945. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
  3946. psa_set_key_algorithm(&attributes, alg);
  3947. psa_set_key_type(&attributes, key_type);
  3948. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  3949. &key));
  3950. PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
  3951. PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
  3952. if (alg != PSA_ALG_ECB_NO_PADDING) {
  3953. PSA_ASSERT(psa_cipher_generate_iv(&operation1,
  3954. iv, iv_size,
  3955. &iv_length));
  3956. }
  3957. output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
  3958. TEST_LE_U(output1_buffer_size,
  3959. PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
  3960. ASSERT_ALLOC(output1, output1_buffer_size);
  3961. TEST_LE_U(first_part_size, input->len);
  3962. PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
  3963. output1, output1_buffer_size,
  3964. &function_output_length));
  3965. TEST_LE_U(function_output_length,
  3966. PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
  3967. TEST_LE_U(function_output_length,
  3968. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
  3969. output1_length += function_output_length;
  3970. PSA_ASSERT(psa_cipher_update(&operation1,
  3971. input->x + first_part_size,
  3972. input->len - first_part_size,
  3973. output1, output1_buffer_size,
  3974. &function_output_length));
  3975. TEST_LE_U(function_output_length,
  3976. PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
  3977. alg,
  3978. input->len - first_part_size));
  3979. TEST_LE_U(function_output_length,
  3980. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
  3981. output1_length += function_output_length;
  3982. PSA_ASSERT(psa_cipher_finish(&operation1,
  3983. output1 + output1_length,
  3984. output1_buffer_size - output1_length,
  3985. &function_output_length));
  3986. TEST_LE_U(function_output_length,
  3987. PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
  3988. TEST_LE_U(function_output_length,
  3989. PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
  3990. output1_length += function_output_length;
  3991. PSA_ASSERT(psa_cipher_abort(&operation1));
  3992. output2_buffer_size = output1_length;
  3993. TEST_LE_U(output2_buffer_size,
  3994. PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
  3995. TEST_LE_U(output2_buffer_size,
  3996. PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
  3997. ASSERT_ALLOC(output2, output2_buffer_size);
  3998. if (iv_length > 0) {
  3999. PSA_ASSERT(psa_cipher_set_iv(&operation2,
  4000. iv, iv_length));
  4001. }
  4002. PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
  4003. output2, output2_buffer_size,
  4004. &function_output_length));
  4005. TEST_LE_U(function_output_length,
  4006. PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
  4007. TEST_LE_U(function_output_length,
  4008. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
  4009. output2_length += function_output_length;
  4010. PSA_ASSERT(psa_cipher_update(&operation2,
  4011. output1 + first_part_size,
  4012. output1_length - first_part_size,
  4013. output2, output2_buffer_size,
  4014. &function_output_length));
  4015. TEST_LE_U(function_output_length,
  4016. PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
  4017. alg,
  4018. output1_length - first_part_size));
  4019. TEST_LE_U(function_output_length,
  4020. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
  4021. output2_length += function_output_length;
  4022. PSA_ASSERT(psa_cipher_finish(&operation2,
  4023. output2 + output2_length,
  4024. output2_buffer_size - output2_length,
  4025. &function_output_length));
  4026. TEST_LE_U(function_output_length,
  4027. PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
  4028. TEST_LE_U(function_output_length,
  4029. PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
  4030. output2_length += function_output_length;
  4031. PSA_ASSERT(psa_cipher_abort(&operation2));
  4032. ASSERT_COMPARE(input->x, input->len, output2, output2_length);
  4033. exit:
  4034. psa_cipher_abort(&operation1);
  4035. psa_cipher_abort(&operation2);
  4036. mbedtls_free(output1);
  4037. mbedtls_free(output2);
  4038. psa_destroy_key(key);
  4039. PSA_DONE();
  4040. }
  4041. /* END_CASE */
  4042. /* BEGIN_CASE */
  4043. void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
  4044. int alg_arg,
  4045. data_t *nonce,
  4046. data_t *additional_data,
  4047. data_t *input_data,
  4048. int expected_result_arg)
  4049. {
  4050. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4051. psa_key_type_t key_type = key_type_arg;
  4052. psa_algorithm_t alg = alg_arg;
  4053. size_t key_bits;
  4054. unsigned char *output_data = NULL;
  4055. size_t output_size = 0;
  4056. size_t output_length = 0;
  4057. unsigned char *output_data2 = NULL;
  4058. size_t output_length2 = 0;
  4059. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  4060. psa_status_t expected_result = expected_result_arg;
  4061. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4062. PSA_ASSERT(psa_crypto_init());
  4063. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
  4064. psa_set_key_algorithm(&attributes, alg);
  4065. psa_set_key_type(&attributes, key_type);
  4066. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  4067. &key));
  4068. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  4069. key_bits = psa_get_key_bits(&attributes);
  4070. output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
  4071. alg);
  4072. /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
  4073. * should be exact. */
  4074. if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
  4075. expected_result != PSA_ERROR_NOT_SUPPORTED) {
  4076. TEST_EQUAL(output_size,
  4077. PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
  4078. TEST_LE_U(output_size,
  4079. PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
  4080. }
  4081. ASSERT_ALLOC(output_data, output_size);
  4082. status = psa_aead_encrypt(key, alg,
  4083. nonce->x, nonce->len,
  4084. additional_data->x,
  4085. additional_data->len,
  4086. input_data->x, input_data->len,
  4087. output_data, output_size,
  4088. &output_length);
  4089. /* If the operation is not supported, just skip and not fail in case the
  4090. * encryption involves a common limitation of cryptography hardwares and
  4091. * an alternative implementation. */
  4092. if (status == PSA_ERROR_NOT_SUPPORTED) {
  4093. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
  4094. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
  4095. }
  4096. TEST_EQUAL(status, expected_result);
  4097. if (PSA_SUCCESS == expected_result) {
  4098. ASSERT_ALLOC(output_data2, output_length);
  4099. /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
  4100. * should be exact. */
  4101. TEST_EQUAL(input_data->len,
  4102. PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
  4103. TEST_LE_U(input_data->len,
  4104. PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
  4105. TEST_EQUAL(psa_aead_decrypt(key, alg,
  4106. nonce->x, nonce->len,
  4107. additional_data->x,
  4108. additional_data->len,
  4109. output_data, output_length,
  4110. output_data2, output_length,
  4111. &output_length2),
  4112. expected_result);
  4113. ASSERT_COMPARE(input_data->x, input_data->len,
  4114. output_data2, output_length2);
  4115. }
  4116. exit:
  4117. psa_destroy_key(key);
  4118. mbedtls_free(output_data);
  4119. mbedtls_free(output_data2);
  4120. PSA_DONE();
  4121. }
  4122. /* END_CASE */
  4123. /* BEGIN_CASE */
  4124. void aead_encrypt(int key_type_arg, data_t *key_data,
  4125. int alg_arg,
  4126. data_t *nonce,
  4127. data_t *additional_data,
  4128. data_t *input_data,
  4129. data_t *expected_result)
  4130. {
  4131. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4132. psa_key_type_t key_type = key_type_arg;
  4133. psa_algorithm_t alg = alg_arg;
  4134. size_t key_bits;
  4135. unsigned char *output_data = NULL;
  4136. size_t output_size = 0;
  4137. size_t output_length = 0;
  4138. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4139. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  4140. PSA_ASSERT(psa_crypto_init());
  4141. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  4142. psa_set_key_algorithm(&attributes, alg);
  4143. psa_set_key_type(&attributes, key_type);
  4144. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  4145. &key));
  4146. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  4147. key_bits = psa_get_key_bits(&attributes);
  4148. output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
  4149. alg);
  4150. /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
  4151. * should be exact. */
  4152. TEST_EQUAL(output_size,
  4153. PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
  4154. TEST_LE_U(output_size,
  4155. PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
  4156. ASSERT_ALLOC(output_data, output_size);
  4157. status = psa_aead_encrypt(key, alg,
  4158. nonce->x, nonce->len,
  4159. additional_data->x, additional_data->len,
  4160. input_data->x, input_data->len,
  4161. output_data, output_size,
  4162. &output_length);
  4163. /* If the operation is not supported, just skip and not fail in case the
  4164. * encryption involves a common limitation of cryptography hardwares and
  4165. * an alternative implementation. */
  4166. if (status == PSA_ERROR_NOT_SUPPORTED) {
  4167. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
  4168. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
  4169. }
  4170. PSA_ASSERT(status);
  4171. ASSERT_COMPARE(expected_result->x, expected_result->len,
  4172. output_data, output_length);
  4173. exit:
  4174. psa_destroy_key(key);
  4175. mbedtls_free(output_data);
  4176. PSA_DONE();
  4177. }
  4178. /* END_CASE */
  4179. /* BEGIN_CASE */
  4180. void aead_decrypt(int key_type_arg, data_t *key_data,
  4181. int alg_arg,
  4182. data_t *nonce,
  4183. data_t *additional_data,
  4184. data_t *input_data,
  4185. data_t *expected_data,
  4186. int expected_result_arg)
  4187. {
  4188. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4189. psa_key_type_t key_type = key_type_arg;
  4190. psa_algorithm_t alg = alg_arg;
  4191. size_t key_bits;
  4192. unsigned char *output_data = NULL;
  4193. size_t output_size = 0;
  4194. size_t output_length = 0;
  4195. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4196. psa_status_t expected_result = expected_result_arg;
  4197. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  4198. PSA_ASSERT(psa_crypto_init());
  4199. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
  4200. psa_set_key_algorithm(&attributes, alg);
  4201. psa_set_key_type(&attributes, key_type);
  4202. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  4203. &key));
  4204. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  4205. key_bits = psa_get_key_bits(&attributes);
  4206. output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
  4207. alg);
  4208. if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
  4209. expected_result != PSA_ERROR_NOT_SUPPORTED) {
  4210. /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
  4211. * should be exact. */
  4212. TEST_EQUAL(output_size,
  4213. PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
  4214. TEST_LE_U(output_size,
  4215. PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
  4216. }
  4217. ASSERT_ALLOC(output_data, output_size);
  4218. status = psa_aead_decrypt(key, alg,
  4219. nonce->x, nonce->len,
  4220. additional_data->x,
  4221. additional_data->len,
  4222. input_data->x, input_data->len,
  4223. output_data, output_size,
  4224. &output_length);
  4225. /* If the operation is not supported, just skip and not fail in case the
  4226. * decryption involves a common limitation of cryptography hardwares and
  4227. * an alternative implementation. */
  4228. if (status == PSA_ERROR_NOT_SUPPORTED) {
  4229. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
  4230. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
  4231. }
  4232. TEST_EQUAL(status, expected_result);
  4233. if (expected_result == PSA_SUCCESS) {
  4234. ASSERT_COMPARE(expected_data->x, expected_data->len,
  4235. output_data, output_length);
  4236. }
  4237. exit:
  4238. psa_destroy_key(key);
  4239. mbedtls_free(output_data);
  4240. PSA_DONE();
  4241. }
  4242. /* END_CASE */
  4243. /* BEGIN_CASE */
  4244. void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
  4245. int alg_arg,
  4246. data_t *nonce,
  4247. data_t *additional_data,
  4248. data_t *input_data,
  4249. int do_set_lengths,
  4250. data_t *expected_output)
  4251. {
  4252. size_t ad_part_len = 0;
  4253. size_t data_part_len = 0;
  4254. set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
  4255. for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
  4256. mbedtls_test_set_step(ad_part_len);
  4257. if (do_set_lengths) {
  4258. if (ad_part_len & 0x01) {
  4259. set_lengths_method = SET_LENGTHS_AFTER_NONCE;
  4260. } else {
  4261. set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
  4262. }
  4263. }
  4264. /* Split ad into length(ad_part_len) parts. */
  4265. if (!aead_multipart_internal_func(key_type_arg, key_data,
  4266. alg_arg, nonce,
  4267. additional_data,
  4268. ad_part_len,
  4269. input_data, -1,
  4270. set_lengths_method,
  4271. expected_output,
  4272. 1, 0)) {
  4273. break;
  4274. }
  4275. /* length(0) part, length(ad_part_len) part, length(0) part... */
  4276. mbedtls_test_set_step(1000 + ad_part_len);
  4277. if (!aead_multipart_internal_func(key_type_arg, key_data,
  4278. alg_arg, nonce,
  4279. additional_data,
  4280. ad_part_len,
  4281. input_data, -1,
  4282. set_lengths_method,
  4283. expected_output,
  4284. 1, 1)) {
  4285. break;
  4286. }
  4287. }
  4288. for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
  4289. /* Split data into length(data_part_len) parts. */
  4290. mbedtls_test_set_step(2000 + data_part_len);
  4291. if (do_set_lengths) {
  4292. if (data_part_len & 0x01) {
  4293. set_lengths_method = SET_LENGTHS_AFTER_NONCE;
  4294. } else {
  4295. set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
  4296. }
  4297. }
  4298. if (!aead_multipart_internal_func(key_type_arg, key_data,
  4299. alg_arg, nonce,
  4300. additional_data, -1,
  4301. input_data, data_part_len,
  4302. set_lengths_method,
  4303. expected_output,
  4304. 1, 0)) {
  4305. break;
  4306. }
  4307. /* length(0) part, length(data_part_len) part, length(0) part... */
  4308. mbedtls_test_set_step(3000 + data_part_len);
  4309. if (!aead_multipart_internal_func(key_type_arg, key_data,
  4310. alg_arg, nonce,
  4311. additional_data, -1,
  4312. input_data, data_part_len,
  4313. set_lengths_method,
  4314. expected_output,
  4315. 1, 1)) {
  4316. break;
  4317. }
  4318. }
  4319. /* Goto is required to silence warnings about unused labels, as we
  4320. * don't actually do any test assertions in this function. */
  4321. goto exit;
  4322. }
  4323. /* END_CASE */
  4324. /* BEGIN_CASE */
  4325. void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
  4326. int alg_arg,
  4327. data_t *nonce,
  4328. data_t *additional_data,
  4329. data_t *input_data,
  4330. int do_set_lengths,
  4331. data_t *expected_output)
  4332. {
  4333. size_t ad_part_len = 0;
  4334. size_t data_part_len = 0;
  4335. set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
  4336. for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
  4337. /* Split ad into length(ad_part_len) parts. */
  4338. mbedtls_test_set_step(ad_part_len);
  4339. if (do_set_lengths) {
  4340. if (ad_part_len & 0x01) {
  4341. set_lengths_method = SET_LENGTHS_AFTER_NONCE;
  4342. } else {
  4343. set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
  4344. }
  4345. }
  4346. if (!aead_multipart_internal_func(key_type_arg, key_data,
  4347. alg_arg, nonce,
  4348. additional_data,
  4349. ad_part_len,
  4350. input_data, -1,
  4351. set_lengths_method,
  4352. expected_output,
  4353. 0, 0)) {
  4354. break;
  4355. }
  4356. /* length(0) part, length(ad_part_len) part, length(0) part... */
  4357. mbedtls_test_set_step(1000 + ad_part_len);
  4358. if (!aead_multipart_internal_func(key_type_arg, key_data,
  4359. alg_arg, nonce,
  4360. additional_data,
  4361. ad_part_len,
  4362. input_data, -1,
  4363. set_lengths_method,
  4364. expected_output,
  4365. 0, 1)) {
  4366. break;
  4367. }
  4368. }
  4369. for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
  4370. /* Split data into length(data_part_len) parts. */
  4371. mbedtls_test_set_step(2000 + data_part_len);
  4372. if (do_set_lengths) {
  4373. if (data_part_len & 0x01) {
  4374. set_lengths_method = SET_LENGTHS_AFTER_NONCE;
  4375. } else {
  4376. set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
  4377. }
  4378. }
  4379. if (!aead_multipart_internal_func(key_type_arg, key_data,
  4380. alg_arg, nonce,
  4381. additional_data, -1,
  4382. input_data, data_part_len,
  4383. set_lengths_method,
  4384. expected_output,
  4385. 0, 0)) {
  4386. break;
  4387. }
  4388. /* length(0) part, length(data_part_len) part, length(0) part... */
  4389. mbedtls_test_set_step(3000 + data_part_len);
  4390. if (!aead_multipart_internal_func(key_type_arg, key_data,
  4391. alg_arg, nonce,
  4392. additional_data, -1,
  4393. input_data, data_part_len,
  4394. set_lengths_method,
  4395. expected_output,
  4396. 0, 1)) {
  4397. break;
  4398. }
  4399. }
  4400. /* Goto is required to silence warnings about unused labels, as we
  4401. * don't actually do any test assertions in this function. */
  4402. goto exit;
  4403. }
  4404. /* END_CASE */
  4405. /* BEGIN_CASE */
  4406. void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
  4407. int alg_arg,
  4408. int nonce_length,
  4409. int expected_nonce_length_arg,
  4410. data_t *additional_data,
  4411. data_t *input_data,
  4412. int expected_status_arg)
  4413. {
  4414. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4415. psa_key_type_t key_type = key_type_arg;
  4416. psa_algorithm_t alg = alg_arg;
  4417. psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
  4418. uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
  4419. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4420. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  4421. psa_status_t expected_status = expected_status_arg;
  4422. size_t actual_nonce_length = 0;
  4423. size_t expected_nonce_length = expected_nonce_length_arg;
  4424. unsigned char *output = NULL;
  4425. unsigned char *ciphertext = NULL;
  4426. size_t output_size = 0;
  4427. size_t ciphertext_size = 0;
  4428. size_t ciphertext_length = 0;
  4429. size_t tag_length = 0;
  4430. uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
  4431. PSA_ASSERT(psa_crypto_init());
  4432. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  4433. psa_set_key_algorithm(&attributes, alg);
  4434. psa_set_key_type(&attributes, key_type);
  4435. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  4436. &key));
  4437. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  4438. output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
  4439. ASSERT_ALLOC(output, output_size);
  4440. ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
  4441. TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
  4442. ASSERT_ALLOC(ciphertext, ciphertext_size);
  4443. status = psa_aead_encrypt_setup(&operation, key, alg);
  4444. /* If the operation is not supported, just skip and not fail in case the
  4445. * encryption involves a common limitation of cryptography hardwares and
  4446. * an alternative implementation. */
  4447. if (status == PSA_ERROR_NOT_SUPPORTED) {
  4448. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
  4449. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
  4450. }
  4451. PSA_ASSERT(status);
  4452. status = psa_aead_generate_nonce(&operation, nonce_buffer,
  4453. nonce_length,
  4454. &actual_nonce_length);
  4455. TEST_EQUAL(status, expected_status);
  4456. TEST_EQUAL(actual_nonce_length, expected_nonce_length);
  4457. if (expected_status == PSA_SUCCESS) {
  4458. TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
  4459. alg));
  4460. }
  4461. TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
  4462. if (expected_status == PSA_SUCCESS) {
  4463. /* Ensure we can still complete operation. */
  4464. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  4465. input_data->len));
  4466. PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
  4467. additional_data->len));
  4468. PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
  4469. output, output_size,
  4470. &ciphertext_length));
  4471. PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
  4472. &ciphertext_length, tag_buffer,
  4473. PSA_AEAD_TAG_MAX_SIZE, &tag_length));
  4474. }
  4475. exit:
  4476. psa_destroy_key(key);
  4477. mbedtls_free(output);
  4478. mbedtls_free(ciphertext);
  4479. psa_aead_abort(&operation);
  4480. PSA_DONE();
  4481. }
  4482. /* END_CASE */
  4483. /* BEGIN_CASE */
  4484. void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
  4485. int alg_arg,
  4486. int nonce_length_arg,
  4487. int set_lengths_method_arg,
  4488. data_t *additional_data,
  4489. data_t *input_data,
  4490. int expected_status_arg)
  4491. {
  4492. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4493. psa_key_type_t key_type = key_type_arg;
  4494. psa_algorithm_t alg = alg_arg;
  4495. psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
  4496. uint8_t *nonce_buffer = NULL;
  4497. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4498. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  4499. psa_status_t expected_status = expected_status_arg;
  4500. unsigned char *output = NULL;
  4501. unsigned char *ciphertext = NULL;
  4502. size_t nonce_length;
  4503. size_t output_size = 0;
  4504. size_t ciphertext_size = 0;
  4505. size_t ciphertext_length = 0;
  4506. size_t tag_length = 0;
  4507. uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
  4508. size_t index = 0;
  4509. set_lengths_method_t set_lengths_method = set_lengths_method_arg;
  4510. PSA_ASSERT(psa_crypto_init());
  4511. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  4512. psa_set_key_algorithm(&attributes, alg);
  4513. psa_set_key_type(&attributes, key_type);
  4514. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  4515. &key));
  4516. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  4517. output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
  4518. ASSERT_ALLOC(output, output_size);
  4519. ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
  4520. TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
  4521. ASSERT_ALLOC(ciphertext, ciphertext_size);
  4522. status = psa_aead_encrypt_setup(&operation, key, alg);
  4523. /* If the operation is not supported, just skip and not fail in case the
  4524. * encryption involves a common limitation of cryptography hardwares and
  4525. * an alternative implementation. */
  4526. if (status == PSA_ERROR_NOT_SUPPORTED) {
  4527. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
  4528. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
  4529. }
  4530. PSA_ASSERT(status);
  4531. /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
  4532. if (nonce_length_arg == -1) {
  4533. /* Arbitrary size buffer, to test zero length valid buffer. */
  4534. ASSERT_ALLOC(nonce_buffer, 4);
  4535. nonce_length = 0;
  4536. } else {
  4537. /* If length is zero, then this will return NULL. */
  4538. nonce_length = (size_t) nonce_length_arg;
  4539. ASSERT_ALLOC(nonce_buffer, nonce_length);
  4540. if (nonce_buffer) {
  4541. for (index = 0; index < nonce_length - 1; ++index) {
  4542. nonce_buffer[index] = 'a' + index;
  4543. }
  4544. }
  4545. }
  4546. if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
  4547. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  4548. input_data->len));
  4549. }
  4550. status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
  4551. TEST_EQUAL(status, expected_status);
  4552. if (expected_status == PSA_SUCCESS) {
  4553. if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
  4554. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  4555. input_data->len));
  4556. }
  4557. if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
  4558. expected_status = PSA_ERROR_BAD_STATE;
  4559. }
  4560. /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
  4561. TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
  4562. additional_data->len),
  4563. expected_status);
  4564. TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
  4565. output, output_size,
  4566. &ciphertext_length),
  4567. expected_status);
  4568. TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
  4569. &ciphertext_length, tag_buffer,
  4570. PSA_AEAD_TAG_MAX_SIZE, &tag_length),
  4571. expected_status);
  4572. }
  4573. exit:
  4574. psa_destroy_key(key);
  4575. mbedtls_free(output);
  4576. mbedtls_free(ciphertext);
  4577. mbedtls_free(nonce_buffer);
  4578. psa_aead_abort(&operation);
  4579. PSA_DONE();
  4580. }
  4581. /* END_CASE */
  4582. /* BEGIN_CASE */
  4583. void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
  4584. int alg_arg,
  4585. int output_size_arg,
  4586. data_t *nonce,
  4587. data_t *additional_data,
  4588. data_t *input_data,
  4589. int expected_status_arg)
  4590. {
  4591. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4592. psa_key_type_t key_type = key_type_arg;
  4593. psa_algorithm_t alg = alg_arg;
  4594. psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
  4595. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4596. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  4597. psa_status_t expected_status = expected_status_arg;
  4598. unsigned char *output = NULL;
  4599. unsigned char *ciphertext = NULL;
  4600. size_t output_size = output_size_arg;
  4601. size_t ciphertext_size = 0;
  4602. size_t ciphertext_length = 0;
  4603. size_t tag_length = 0;
  4604. uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
  4605. PSA_ASSERT(psa_crypto_init());
  4606. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  4607. psa_set_key_algorithm(&attributes, alg);
  4608. psa_set_key_type(&attributes, key_type);
  4609. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  4610. &key));
  4611. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  4612. ASSERT_ALLOC(output, output_size);
  4613. ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
  4614. ASSERT_ALLOC(ciphertext, ciphertext_size);
  4615. status = psa_aead_encrypt_setup(&operation, key, alg);
  4616. /* If the operation is not supported, just skip and not fail in case the
  4617. * encryption involves a common limitation of cryptography hardwares and
  4618. * an alternative implementation. */
  4619. if (status == PSA_ERROR_NOT_SUPPORTED) {
  4620. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
  4621. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
  4622. }
  4623. PSA_ASSERT(status);
  4624. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  4625. input_data->len));
  4626. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  4627. PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
  4628. additional_data->len));
  4629. status = psa_aead_update(&operation, input_data->x, input_data->len,
  4630. output, output_size, &ciphertext_length);
  4631. TEST_EQUAL(status, expected_status);
  4632. if (expected_status == PSA_SUCCESS) {
  4633. /* Ensure we can still complete operation. */
  4634. PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
  4635. &ciphertext_length, tag_buffer,
  4636. PSA_AEAD_TAG_MAX_SIZE, &tag_length));
  4637. }
  4638. exit:
  4639. psa_destroy_key(key);
  4640. mbedtls_free(output);
  4641. mbedtls_free(ciphertext);
  4642. psa_aead_abort(&operation);
  4643. PSA_DONE();
  4644. }
  4645. /* END_CASE */
  4646. /* BEGIN_CASE */
  4647. void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
  4648. int alg_arg,
  4649. int finish_ciphertext_size_arg,
  4650. int tag_size_arg,
  4651. data_t *nonce,
  4652. data_t *additional_data,
  4653. data_t *input_data,
  4654. int expected_status_arg)
  4655. {
  4656. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4657. psa_key_type_t key_type = key_type_arg;
  4658. psa_algorithm_t alg = alg_arg;
  4659. psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
  4660. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4661. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  4662. psa_status_t expected_status = expected_status_arg;
  4663. unsigned char *ciphertext = NULL;
  4664. unsigned char *finish_ciphertext = NULL;
  4665. unsigned char *tag_buffer = NULL;
  4666. size_t ciphertext_size = 0;
  4667. size_t ciphertext_length = 0;
  4668. size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
  4669. size_t tag_size = (size_t) tag_size_arg;
  4670. size_t tag_length = 0;
  4671. PSA_ASSERT(psa_crypto_init());
  4672. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  4673. psa_set_key_algorithm(&attributes, alg);
  4674. psa_set_key_type(&attributes, key_type);
  4675. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  4676. &key));
  4677. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  4678. ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
  4679. ASSERT_ALLOC(ciphertext, ciphertext_size);
  4680. ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
  4681. ASSERT_ALLOC(tag_buffer, tag_size);
  4682. status = psa_aead_encrypt_setup(&operation, key, alg);
  4683. /* If the operation is not supported, just skip and not fail in case the
  4684. * encryption involves a common limitation of cryptography hardwares and
  4685. * an alternative implementation. */
  4686. if (status == PSA_ERROR_NOT_SUPPORTED) {
  4687. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
  4688. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
  4689. }
  4690. PSA_ASSERT(status);
  4691. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  4692. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  4693. input_data->len));
  4694. PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
  4695. additional_data->len));
  4696. PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
  4697. ciphertext, ciphertext_size, &ciphertext_length));
  4698. /* Ensure we can still complete operation. */
  4699. status = psa_aead_finish(&operation, finish_ciphertext,
  4700. finish_ciphertext_size,
  4701. &ciphertext_length, tag_buffer,
  4702. tag_size, &tag_length);
  4703. TEST_EQUAL(status, expected_status);
  4704. exit:
  4705. psa_destroy_key(key);
  4706. mbedtls_free(ciphertext);
  4707. mbedtls_free(finish_ciphertext);
  4708. mbedtls_free(tag_buffer);
  4709. psa_aead_abort(&operation);
  4710. PSA_DONE();
  4711. }
  4712. /* END_CASE */
  4713. /* BEGIN_CASE */
  4714. void aead_multipart_verify(int key_type_arg, data_t *key_data,
  4715. int alg_arg,
  4716. data_t *nonce,
  4717. data_t *additional_data,
  4718. data_t *input_data,
  4719. data_t *tag,
  4720. int tag_usage_arg,
  4721. int expected_setup_status_arg,
  4722. int expected_status_arg)
  4723. {
  4724. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4725. psa_key_type_t key_type = key_type_arg;
  4726. psa_algorithm_t alg = alg_arg;
  4727. psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
  4728. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4729. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  4730. psa_status_t expected_status = expected_status_arg;
  4731. psa_status_t expected_setup_status = expected_setup_status_arg;
  4732. unsigned char *plaintext = NULL;
  4733. unsigned char *finish_plaintext = NULL;
  4734. size_t plaintext_size = 0;
  4735. size_t plaintext_length = 0;
  4736. size_t verify_plaintext_size = 0;
  4737. tag_usage_method_t tag_usage = tag_usage_arg;
  4738. unsigned char *tag_buffer = NULL;
  4739. size_t tag_size = 0;
  4740. PSA_ASSERT(psa_crypto_init());
  4741. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
  4742. psa_set_key_algorithm(&attributes, alg);
  4743. psa_set_key_type(&attributes, key_type);
  4744. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  4745. &key));
  4746. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  4747. plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
  4748. input_data->len);
  4749. ASSERT_ALLOC(plaintext, plaintext_size);
  4750. verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
  4751. ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
  4752. status = psa_aead_decrypt_setup(&operation, key, alg);
  4753. /* If the operation is not supported, just skip and not fail in case the
  4754. * encryption involves a common limitation of cryptography hardwares and
  4755. * an alternative implementation. */
  4756. if (status == PSA_ERROR_NOT_SUPPORTED) {
  4757. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
  4758. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
  4759. }
  4760. TEST_EQUAL(status, expected_setup_status);
  4761. if (status != PSA_SUCCESS) {
  4762. goto exit;
  4763. }
  4764. PSA_ASSERT(status);
  4765. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  4766. status = psa_aead_set_lengths(&operation, additional_data->len,
  4767. input_data->len);
  4768. PSA_ASSERT(status);
  4769. PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
  4770. additional_data->len));
  4771. PSA_ASSERT(psa_aead_update(&operation, input_data->x,
  4772. input_data->len,
  4773. plaintext, plaintext_size,
  4774. &plaintext_length));
  4775. if (tag_usage == USE_GIVEN_TAG) {
  4776. tag_buffer = tag->x;
  4777. tag_size = tag->len;
  4778. }
  4779. status = psa_aead_verify(&operation, finish_plaintext,
  4780. verify_plaintext_size,
  4781. &plaintext_length,
  4782. tag_buffer, tag_size);
  4783. TEST_EQUAL(status, expected_status);
  4784. exit:
  4785. psa_destroy_key(key);
  4786. mbedtls_free(plaintext);
  4787. mbedtls_free(finish_plaintext);
  4788. psa_aead_abort(&operation);
  4789. PSA_DONE();
  4790. }
  4791. /* END_CASE */
  4792. /* BEGIN_CASE */
  4793. void aead_multipart_setup(int key_type_arg, data_t *key_data,
  4794. int alg_arg, int expected_status_arg)
  4795. {
  4796. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4797. psa_key_type_t key_type = key_type_arg;
  4798. psa_algorithm_t alg = alg_arg;
  4799. psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
  4800. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4801. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  4802. psa_status_t expected_status = expected_status_arg;
  4803. PSA_ASSERT(psa_crypto_init());
  4804. psa_set_key_usage_flags(&attributes,
  4805. PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
  4806. psa_set_key_algorithm(&attributes, alg);
  4807. psa_set_key_type(&attributes, key_type);
  4808. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  4809. &key));
  4810. status = psa_aead_encrypt_setup(&operation, key, alg);
  4811. TEST_EQUAL(status, expected_status);
  4812. psa_aead_abort(&operation);
  4813. status = psa_aead_decrypt_setup(&operation, key, alg);
  4814. TEST_EQUAL(status, expected_status);
  4815. exit:
  4816. psa_destroy_key(key);
  4817. psa_aead_abort(&operation);
  4818. PSA_DONE();
  4819. }
  4820. /* END_CASE */
  4821. /* BEGIN_CASE */
  4822. void aead_multipart_state_test(int key_type_arg, data_t *key_data,
  4823. int alg_arg,
  4824. data_t *nonce,
  4825. data_t *additional_data,
  4826. data_t *input_data)
  4827. {
  4828. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4829. psa_key_type_t key_type = key_type_arg;
  4830. psa_algorithm_t alg = alg_arg;
  4831. psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
  4832. unsigned char *output_data = NULL;
  4833. unsigned char *final_data = NULL;
  4834. size_t output_size = 0;
  4835. size_t finish_output_size = 0;
  4836. size_t output_length = 0;
  4837. size_t key_bits = 0;
  4838. size_t tag_length = 0;
  4839. size_t tag_size = 0;
  4840. size_t nonce_length = 0;
  4841. uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
  4842. uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
  4843. size_t output_part_length = 0;
  4844. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4845. PSA_ASSERT(psa_crypto_init());
  4846. psa_set_key_usage_flags(&attributes,
  4847. PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
  4848. psa_set_key_algorithm(&attributes, alg);
  4849. psa_set_key_type(&attributes, key_type);
  4850. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  4851. &key));
  4852. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  4853. key_bits = psa_get_key_bits(&attributes);
  4854. tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
  4855. TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
  4856. output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
  4857. ASSERT_ALLOC(output_data, output_size);
  4858. finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
  4859. TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
  4860. ASSERT_ALLOC(final_data, finish_output_size);
  4861. /* Test all operations error without calling setup first. */
  4862. TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
  4863. PSA_ERROR_BAD_STATE);
  4864. psa_aead_abort(&operation);
  4865. TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
  4866. PSA_AEAD_NONCE_MAX_SIZE,
  4867. &nonce_length),
  4868. PSA_ERROR_BAD_STATE);
  4869. psa_aead_abort(&operation);
  4870. /* ------------------------------------------------------- */
  4871. TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
  4872. input_data->len),
  4873. PSA_ERROR_BAD_STATE);
  4874. psa_aead_abort(&operation);
  4875. /* ------------------------------------------------------- */
  4876. TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
  4877. additional_data->len),
  4878. PSA_ERROR_BAD_STATE);
  4879. psa_aead_abort(&operation);
  4880. /* ------------------------------------------------------- */
  4881. TEST_EQUAL(psa_aead_update(&operation, input_data->x,
  4882. input_data->len, output_data,
  4883. output_size, &output_length),
  4884. PSA_ERROR_BAD_STATE);
  4885. psa_aead_abort(&operation);
  4886. /* ------------------------------------------------------- */
  4887. TEST_EQUAL(psa_aead_finish(&operation, final_data,
  4888. finish_output_size,
  4889. &output_part_length,
  4890. tag_buffer, tag_length,
  4891. &tag_size),
  4892. PSA_ERROR_BAD_STATE);
  4893. psa_aead_abort(&operation);
  4894. /* ------------------------------------------------------- */
  4895. TEST_EQUAL(psa_aead_verify(&operation, final_data,
  4896. finish_output_size,
  4897. &output_part_length,
  4898. tag_buffer,
  4899. tag_length),
  4900. PSA_ERROR_BAD_STATE);
  4901. psa_aead_abort(&operation);
  4902. /* Test for double setups. */
  4903. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  4904. TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
  4905. PSA_ERROR_BAD_STATE);
  4906. psa_aead_abort(&operation);
  4907. /* ------------------------------------------------------- */
  4908. PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
  4909. TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
  4910. PSA_ERROR_BAD_STATE);
  4911. psa_aead_abort(&operation);
  4912. /* ------------------------------------------------------- */
  4913. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  4914. TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
  4915. PSA_ERROR_BAD_STATE);
  4916. psa_aead_abort(&operation);
  4917. /* ------------------------------------------------------- */
  4918. PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
  4919. TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
  4920. PSA_ERROR_BAD_STATE);
  4921. psa_aead_abort(&operation);
  4922. /* Test for not setting a nonce. */
  4923. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  4924. TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
  4925. additional_data->len),
  4926. PSA_ERROR_BAD_STATE);
  4927. psa_aead_abort(&operation);
  4928. /* ------------------------------------------------------- */
  4929. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  4930. TEST_EQUAL(psa_aead_update(&operation, input_data->x,
  4931. input_data->len, output_data,
  4932. output_size, &output_length),
  4933. PSA_ERROR_BAD_STATE);
  4934. psa_aead_abort(&operation);
  4935. /* ------------------------------------------------------- */
  4936. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  4937. TEST_EQUAL(psa_aead_finish(&operation, final_data,
  4938. finish_output_size,
  4939. &output_part_length,
  4940. tag_buffer, tag_length,
  4941. &tag_size),
  4942. PSA_ERROR_BAD_STATE);
  4943. psa_aead_abort(&operation);
  4944. /* ------------------------------------------------------- */
  4945. PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
  4946. TEST_EQUAL(psa_aead_verify(&operation, final_data,
  4947. finish_output_size,
  4948. &output_part_length,
  4949. tag_buffer,
  4950. tag_length),
  4951. PSA_ERROR_BAD_STATE);
  4952. psa_aead_abort(&operation);
  4953. /* Test for double setting nonce. */
  4954. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  4955. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  4956. TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
  4957. PSA_ERROR_BAD_STATE);
  4958. psa_aead_abort(&operation);
  4959. /* Test for double generating nonce. */
  4960. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  4961. PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
  4962. PSA_AEAD_NONCE_MAX_SIZE,
  4963. &nonce_length));
  4964. TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
  4965. PSA_AEAD_NONCE_MAX_SIZE,
  4966. &nonce_length),
  4967. PSA_ERROR_BAD_STATE);
  4968. psa_aead_abort(&operation);
  4969. /* Test for generate nonce then set and vice versa */
  4970. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  4971. PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
  4972. PSA_AEAD_NONCE_MAX_SIZE,
  4973. &nonce_length));
  4974. TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
  4975. PSA_ERROR_BAD_STATE);
  4976. psa_aead_abort(&operation);
  4977. /* Test for generating nonce after calling set lengths */
  4978. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  4979. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  4980. input_data->len));
  4981. PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
  4982. PSA_AEAD_NONCE_MAX_SIZE,
  4983. &nonce_length));
  4984. psa_aead_abort(&operation);
  4985. /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
  4986. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  4987. if (operation.alg == PSA_ALG_CCM) {
  4988. TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
  4989. input_data->len),
  4990. PSA_ERROR_INVALID_ARGUMENT);
  4991. TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
  4992. PSA_AEAD_NONCE_MAX_SIZE,
  4993. &nonce_length),
  4994. PSA_ERROR_BAD_STATE);
  4995. } else {
  4996. PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
  4997. input_data->len));
  4998. PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
  4999. PSA_AEAD_NONCE_MAX_SIZE,
  5000. &nonce_length));
  5001. }
  5002. psa_aead_abort(&operation);
  5003. /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
  5004. #if SIZE_MAX > UINT32_MAX
  5005. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5006. if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
  5007. TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
  5008. input_data->len),
  5009. PSA_ERROR_INVALID_ARGUMENT);
  5010. TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
  5011. PSA_AEAD_NONCE_MAX_SIZE,
  5012. &nonce_length),
  5013. PSA_ERROR_BAD_STATE);
  5014. } else {
  5015. PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
  5016. input_data->len));
  5017. PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
  5018. PSA_AEAD_NONCE_MAX_SIZE,
  5019. &nonce_length));
  5020. }
  5021. psa_aead_abort(&operation);
  5022. #endif
  5023. /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
  5024. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5025. PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
  5026. PSA_AEAD_NONCE_MAX_SIZE,
  5027. &nonce_length));
  5028. if (operation.alg == PSA_ALG_CCM) {
  5029. TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
  5030. input_data->len),
  5031. PSA_ERROR_INVALID_ARGUMENT);
  5032. } else {
  5033. PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
  5034. input_data->len));
  5035. }
  5036. psa_aead_abort(&operation);
  5037. /* ------------------------------------------------------- */
  5038. /* Test for setting nonce after calling set lengths */
  5039. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5040. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  5041. input_data->len));
  5042. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5043. psa_aead_abort(&operation);
  5044. /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
  5045. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5046. if (operation.alg == PSA_ALG_CCM) {
  5047. TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
  5048. input_data->len),
  5049. PSA_ERROR_INVALID_ARGUMENT);
  5050. TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
  5051. PSA_ERROR_BAD_STATE);
  5052. } else {
  5053. PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
  5054. input_data->len));
  5055. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5056. }
  5057. psa_aead_abort(&operation);
  5058. /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
  5059. #if SIZE_MAX > UINT32_MAX
  5060. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5061. if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
  5062. TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
  5063. input_data->len),
  5064. PSA_ERROR_INVALID_ARGUMENT);
  5065. TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
  5066. PSA_ERROR_BAD_STATE);
  5067. } else {
  5068. PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
  5069. input_data->len));
  5070. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5071. }
  5072. psa_aead_abort(&operation);
  5073. #endif
  5074. /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
  5075. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5076. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5077. if (operation.alg == PSA_ALG_CCM) {
  5078. TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
  5079. input_data->len),
  5080. PSA_ERROR_INVALID_ARGUMENT);
  5081. } else {
  5082. PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
  5083. input_data->len));
  5084. }
  5085. psa_aead_abort(&operation);
  5086. /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
  5087. #if SIZE_MAX > UINT32_MAX
  5088. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5089. if (operation.alg == PSA_ALG_GCM) {
  5090. TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
  5091. SIZE_MAX),
  5092. PSA_ERROR_INVALID_ARGUMENT);
  5093. TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
  5094. PSA_ERROR_BAD_STATE);
  5095. } else if (operation.alg != PSA_ALG_CCM) {
  5096. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  5097. SIZE_MAX));
  5098. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5099. }
  5100. psa_aead_abort(&operation);
  5101. #endif
  5102. /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
  5103. #if SIZE_MAX > UINT32_MAX
  5104. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5105. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5106. if (operation.alg == PSA_ALG_GCM) {
  5107. TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
  5108. SIZE_MAX),
  5109. PSA_ERROR_INVALID_ARGUMENT);
  5110. } else if (operation.alg != PSA_ALG_CCM) {
  5111. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  5112. SIZE_MAX));
  5113. }
  5114. psa_aead_abort(&operation);
  5115. #endif
  5116. /* ------------------------------------------------------- */
  5117. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5118. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5119. TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
  5120. PSA_AEAD_NONCE_MAX_SIZE,
  5121. &nonce_length),
  5122. PSA_ERROR_BAD_STATE);
  5123. psa_aead_abort(&operation);
  5124. /* Test for generating nonce in decrypt setup. */
  5125. PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
  5126. TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
  5127. PSA_AEAD_NONCE_MAX_SIZE,
  5128. &nonce_length),
  5129. PSA_ERROR_BAD_STATE);
  5130. psa_aead_abort(&operation);
  5131. /* Test for setting lengths twice. */
  5132. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5133. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5134. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  5135. input_data->len));
  5136. TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
  5137. input_data->len),
  5138. PSA_ERROR_BAD_STATE);
  5139. psa_aead_abort(&operation);
  5140. /* Test for setting lengths after setting nonce + already starting data. */
  5141. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5142. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5143. if (operation.alg == PSA_ALG_CCM) {
  5144. TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
  5145. additional_data->len),
  5146. PSA_ERROR_BAD_STATE);
  5147. } else {
  5148. PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
  5149. additional_data->len));
  5150. TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
  5151. input_data->len),
  5152. PSA_ERROR_BAD_STATE);
  5153. }
  5154. psa_aead_abort(&operation);
  5155. /* ------------------------------------------------------- */
  5156. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5157. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5158. if (operation.alg == PSA_ALG_CCM) {
  5159. TEST_EQUAL(psa_aead_update(&operation, input_data->x,
  5160. input_data->len, output_data,
  5161. output_size, &output_length),
  5162. PSA_ERROR_BAD_STATE);
  5163. } else {
  5164. PSA_ASSERT(psa_aead_update(&operation, input_data->x,
  5165. input_data->len, output_data,
  5166. output_size, &output_length));
  5167. TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
  5168. input_data->len),
  5169. PSA_ERROR_BAD_STATE);
  5170. }
  5171. psa_aead_abort(&operation);
  5172. /* ------------------------------------------------------- */
  5173. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5174. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5175. if (operation.alg == PSA_ALG_CCM) {
  5176. PSA_ASSERT(psa_aead_finish(&operation, final_data,
  5177. finish_output_size,
  5178. &output_part_length,
  5179. tag_buffer, tag_length,
  5180. &tag_size));
  5181. } else {
  5182. PSA_ASSERT(psa_aead_finish(&operation, final_data,
  5183. finish_output_size,
  5184. &output_part_length,
  5185. tag_buffer, tag_length,
  5186. &tag_size));
  5187. TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
  5188. input_data->len),
  5189. PSA_ERROR_BAD_STATE);
  5190. }
  5191. psa_aead_abort(&operation);
  5192. /* Test for setting lengths after generating nonce + already starting data. */
  5193. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5194. PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
  5195. PSA_AEAD_NONCE_MAX_SIZE,
  5196. &nonce_length));
  5197. if (operation.alg == PSA_ALG_CCM) {
  5198. TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
  5199. additional_data->len),
  5200. PSA_ERROR_BAD_STATE);
  5201. } else {
  5202. PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
  5203. additional_data->len));
  5204. TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
  5205. input_data->len),
  5206. PSA_ERROR_BAD_STATE);
  5207. }
  5208. psa_aead_abort(&operation);
  5209. /* ------------------------------------------------------- */
  5210. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5211. PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
  5212. PSA_AEAD_NONCE_MAX_SIZE,
  5213. &nonce_length));
  5214. if (operation.alg == PSA_ALG_CCM) {
  5215. TEST_EQUAL(psa_aead_update(&operation, input_data->x,
  5216. input_data->len, output_data,
  5217. output_size, &output_length),
  5218. PSA_ERROR_BAD_STATE);
  5219. } else {
  5220. PSA_ASSERT(psa_aead_update(&operation, input_data->x,
  5221. input_data->len, output_data,
  5222. output_size, &output_length));
  5223. TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
  5224. input_data->len),
  5225. PSA_ERROR_BAD_STATE);
  5226. }
  5227. psa_aead_abort(&operation);
  5228. /* ------------------------------------------------------- */
  5229. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5230. PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
  5231. PSA_AEAD_NONCE_MAX_SIZE,
  5232. &nonce_length));
  5233. if (operation.alg == PSA_ALG_CCM) {
  5234. PSA_ASSERT(psa_aead_finish(&operation, final_data,
  5235. finish_output_size,
  5236. &output_part_length,
  5237. tag_buffer, tag_length,
  5238. &tag_size));
  5239. } else {
  5240. PSA_ASSERT(psa_aead_finish(&operation, final_data,
  5241. finish_output_size,
  5242. &output_part_length,
  5243. tag_buffer, tag_length,
  5244. &tag_size));
  5245. TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
  5246. input_data->len),
  5247. PSA_ERROR_BAD_STATE);
  5248. }
  5249. psa_aead_abort(&operation);
  5250. /* Test for not sending any additional data or data after setting non zero
  5251. * lengths for them. (encrypt) */
  5252. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5253. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5254. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  5255. input_data->len));
  5256. TEST_EQUAL(psa_aead_finish(&operation, final_data,
  5257. finish_output_size,
  5258. &output_part_length,
  5259. tag_buffer, tag_length,
  5260. &tag_size),
  5261. PSA_ERROR_INVALID_ARGUMENT);
  5262. psa_aead_abort(&operation);
  5263. /* Test for not sending any additional data or data after setting non-zero
  5264. * lengths for them. (decrypt) */
  5265. PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
  5266. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5267. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  5268. input_data->len));
  5269. TEST_EQUAL(psa_aead_verify(&operation, final_data,
  5270. finish_output_size,
  5271. &output_part_length,
  5272. tag_buffer,
  5273. tag_length),
  5274. PSA_ERROR_INVALID_ARGUMENT);
  5275. psa_aead_abort(&operation);
  5276. /* Test for not sending any additional data after setting a non-zero length
  5277. * for it. */
  5278. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5279. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5280. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  5281. input_data->len));
  5282. TEST_EQUAL(psa_aead_update(&operation, input_data->x,
  5283. input_data->len, output_data,
  5284. output_size, &output_length),
  5285. PSA_ERROR_INVALID_ARGUMENT);
  5286. psa_aead_abort(&operation);
  5287. /* Test for not sending any data after setting a non-zero length for it.*/
  5288. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5289. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5290. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  5291. input_data->len));
  5292. PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
  5293. additional_data->len));
  5294. TEST_EQUAL(psa_aead_finish(&operation, final_data,
  5295. finish_output_size,
  5296. &output_part_length,
  5297. tag_buffer, tag_length,
  5298. &tag_size),
  5299. PSA_ERROR_INVALID_ARGUMENT);
  5300. psa_aead_abort(&operation);
  5301. /* Test for sending too much additional data after setting lengths. */
  5302. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5303. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5304. PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
  5305. TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
  5306. additional_data->len),
  5307. PSA_ERROR_INVALID_ARGUMENT);
  5308. psa_aead_abort(&operation);
  5309. /* ------------------------------------------------------- */
  5310. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5311. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5312. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  5313. input_data->len));
  5314. PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
  5315. additional_data->len));
  5316. TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
  5317. 1),
  5318. PSA_ERROR_INVALID_ARGUMENT);
  5319. psa_aead_abort(&operation);
  5320. /* Test for sending too much data after setting lengths. */
  5321. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5322. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5323. PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
  5324. TEST_EQUAL(psa_aead_update(&operation, input_data->x,
  5325. input_data->len, output_data,
  5326. output_size, &output_length),
  5327. PSA_ERROR_INVALID_ARGUMENT);
  5328. psa_aead_abort(&operation);
  5329. /* ------------------------------------------------------- */
  5330. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5331. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5332. PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
  5333. input_data->len));
  5334. PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
  5335. additional_data->len));
  5336. PSA_ASSERT(psa_aead_update(&operation, input_data->x,
  5337. input_data->len, output_data,
  5338. output_size, &output_length));
  5339. TEST_EQUAL(psa_aead_update(&operation, input_data->x,
  5340. 1, output_data,
  5341. output_size, &output_length),
  5342. PSA_ERROR_INVALID_ARGUMENT);
  5343. psa_aead_abort(&operation);
  5344. /* Test sending additional data after data. */
  5345. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5346. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5347. if (operation.alg != PSA_ALG_CCM) {
  5348. PSA_ASSERT(psa_aead_update(&operation, input_data->x,
  5349. input_data->len, output_data,
  5350. output_size, &output_length));
  5351. TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
  5352. additional_data->len),
  5353. PSA_ERROR_BAD_STATE);
  5354. }
  5355. psa_aead_abort(&operation);
  5356. /* Test calling finish on decryption. */
  5357. PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
  5358. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5359. TEST_EQUAL(psa_aead_finish(&operation, final_data,
  5360. finish_output_size,
  5361. &output_part_length,
  5362. tag_buffer, tag_length,
  5363. &tag_size),
  5364. PSA_ERROR_BAD_STATE);
  5365. psa_aead_abort(&operation);
  5366. /* Test calling verify on encryption. */
  5367. PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
  5368. PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
  5369. TEST_EQUAL(psa_aead_verify(&operation, final_data,
  5370. finish_output_size,
  5371. &output_part_length,
  5372. tag_buffer,
  5373. tag_length),
  5374. PSA_ERROR_BAD_STATE);
  5375. psa_aead_abort(&operation);
  5376. exit:
  5377. psa_destroy_key(key);
  5378. psa_aead_abort(&operation);
  5379. mbedtls_free(output_data);
  5380. mbedtls_free(final_data);
  5381. PSA_DONE();
  5382. }
  5383. /* END_CASE */
  5384. /* BEGIN_CASE */
  5385. void signature_size(int type_arg,
  5386. int bits,
  5387. int alg_arg,
  5388. int expected_size_arg)
  5389. {
  5390. psa_key_type_t type = type_arg;
  5391. psa_algorithm_t alg = alg_arg;
  5392. size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
  5393. TEST_EQUAL(actual_size, (size_t) expected_size_arg);
  5394. exit:
  5395. ;
  5396. }
  5397. /* END_CASE */
  5398. /* BEGIN_CASE */
  5399. void sign_hash_deterministic(int key_type_arg, data_t *key_data,
  5400. int alg_arg, data_t *input_data,
  5401. data_t *output_data)
  5402. {
  5403. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  5404. psa_key_type_t key_type = key_type_arg;
  5405. psa_algorithm_t alg = alg_arg;
  5406. size_t key_bits;
  5407. unsigned char *signature = NULL;
  5408. size_t signature_size;
  5409. size_t signature_length = 0xdeadbeef;
  5410. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  5411. PSA_ASSERT(psa_crypto_init());
  5412. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
  5413. psa_set_key_algorithm(&attributes, alg);
  5414. psa_set_key_type(&attributes, key_type);
  5415. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  5416. &key));
  5417. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  5418. key_bits = psa_get_key_bits(&attributes);
  5419. /* Allocate a buffer which has the size advertised by the
  5420. * library. */
  5421. signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
  5422. key_bits, alg);
  5423. TEST_ASSERT(signature_size != 0);
  5424. TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
  5425. ASSERT_ALLOC(signature, signature_size);
  5426. /* Perform the signature. */
  5427. PSA_ASSERT(psa_sign_hash(key, alg,
  5428. input_data->x, input_data->len,
  5429. signature, signature_size,
  5430. &signature_length));
  5431. /* Verify that the signature is what is expected. */
  5432. ASSERT_COMPARE(output_data->x, output_data->len,
  5433. signature, signature_length);
  5434. exit:
  5435. /*
  5436. * Key attributes may have been returned by psa_get_key_attributes()
  5437. * thus reset them as required.
  5438. */
  5439. psa_reset_key_attributes(&attributes);
  5440. psa_destroy_key(key);
  5441. mbedtls_free(signature);
  5442. PSA_DONE();
  5443. }
  5444. /* END_CASE */
  5445. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
  5446. /**
  5447. * sign_hash_interruptible() test intentions:
  5448. *
  5449. * Note: This test can currently only handle ECDSA.
  5450. *
  5451. * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
  5452. * and private keys / keypairs only).
  5453. *
  5454. * 2. Test the number of calls to psa_sign_hash_complete() required are as
  5455. * expected for different max_ops values.
  5456. *
  5457. * 3. Test that the number of ops done prior to start and after abort is zero
  5458. * and that each successful stage completes some ops (this is not mandated by
  5459. * the PSA specification, but is currently the case).
  5460. *
  5461. * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
  5462. * complete() calls does not alter the number of ops returned.
  5463. */
  5464. void sign_hash_interruptible(int key_type_arg, data_t *key_data,
  5465. int alg_arg, data_t *input_data,
  5466. data_t *output_data, int max_ops_arg)
  5467. {
  5468. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  5469. psa_key_type_t key_type = key_type_arg;
  5470. psa_algorithm_t alg = alg_arg;
  5471. size_t key_bits;
  5472. unsigned char *signature = NULL;
  5473. size_t signature_size;
  5474. size_t signature_length = 0xdeadbeef;
  5475. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  5476. psa_status_t status = PSA_OPERATION_INCOMPLETE;
  5477. uint32_t num_ops = 0;
  5478. uint32_t max_ops = max_ops_arg;
  5479. size_t num_ops_prior = 0;
  5480. size_t num_completes = 0;
  5481. size_t min_completes = 0;
  5482. size_t max_completes = 0;
  5483. psa_sign_hash_interruptible_operation_t operation =
  5484. psa_sign_hash_interruptible_operation_init();
  5485. PSA_ASSERT(psa_crypto_init());
  5486. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
  5487. psa_set_key_algorithm(&attributes, alg);
  5488. psa_set_key_type(&attributes, key_type);
  5489. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  5490. &key));
  5491. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  5492. key_bits = psa_get_key_bits(&attributes);
  5493. /* Allocate a buffer which has the size advertised by the
  5494. * library. */
  5495. signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
  5496. key_bits, alg);
  5497. TEST_ASSERT(signature_size != 0);
  5498. TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
  5499. ASSERT_ALLOC(signature, signature_size);
  5500. psa_interruptible_set_max_ops(max_ops);
  5501. interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
  5502. &min_completes, &max_completes);
  5503. num_ops_prior = psa_sign_hash_get_num_ops(&operation);
  5504. TEST_ASSERT(num_ops_prior == 0);
  5505. /* Start performing the signature. */
  5506. PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
  5507. input_data->x, input_data->len));
  5508. num_ops_prior = psa_sign_hash_get_num_ops(&operation);
  5509. TEST_ASSERT(num_ops_prior == 0);
  5510. /* Continue performing the signature until complete. */
  5511. do {
  5512. status = psa_sign_hash_complete(&operation, signature, signature_size,
  5513. &signature_length);
  5514. num_completes++;
  5515. if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
  5516. num_ops = psa_sign_hash_get_num_ops(&operation);
  5517. /* We are asserting here that every complete makes progress
  5518. * (completes some ops), which is true of the internal
  5519. * implementation and probably any implementation, however this is
  5520. * not mandated by the PSA specification. */
  5521. TEST_ASSERT(num_ops > num_ops_prior);
  5522. num_ops_prior = num_ops;
  5523. /* Ensure calling get_num_ops() twice still returns the same
  5524. * number of ops as previously reported. */
  5525. num_ops = psa_sign_hash_get_num_ops(&operation);
  5526. TEST_EQUAL(num_ops, num_ops_prior);
  5527. }
  5528. } while (status == PSA_OPERATION_INCOMPLETE);
  5529. TEST_ASSERT(status == PSA_SUCCESS);
  5530. TEST_LE_U(min_completes, num_completes);
  5531. TEST_LE_U(num_completes, max_completes);
  5532. /* Verify that the signature is what is expected. */
  5533. ASSERT_COMPARE(output_data->x, output_data->len,
  5534. signature, signature_length);
  5535. PSA_ASSERT(psa_sign_hash_abort(&operation));
  5536. num_ops = psa_sign_hash_get_num_ops(&operation);
  5537. TEST_ASSERT(num_ops == 0);
  5538. exit:
  5539. /*
  5540. * Key attributes may have been returned by psa_get_key_attributes()
  5541. * thus reset them as required.
  5542. */
  5543. psa_reset_key_attributes(&attributes);
  5544. psa_destroy_key(key);
  5545. mbedtls_free(signature);
  5546. PSA_DONE();
  5547. }
  5548. /* END_CASE */
  5549. /* BEGIN_CASE */
  5550. void sign_hash_fail(int key_type_arg, data_t *key_data,
  5551. int alg_arg, data_t *input_data,
  5552. int signature_size_arg, int expected_status_arg)
  5553. {
  5554. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  5555. psa_key_type_t key_type = key_type_arg;
  5556. psa_algorithm_t alg = alg_arg;
  5557. size_t signature_size = signature_size_arg;
  5558. psa_status_t actual_status;
  5559. psa_status_t expected_status = expected_status_arg;
  5560. unsigned char *signature = NULL;
  5561. size_t signature_length = 0xdeadbeef;
  5562. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  5563. ASSERT_ALLOC(signature, signature_size);
  5564. PSA_ASSERT(psa_crypto_init());
  5565. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
  5566. psa_set_key_algorithm(&attributes, alg);
  5567. psa_set_key_type(&attributes, key_type);
  5568. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  5569. &key));
  5570. actual_status = psa_sign_hash(key, alg,
  5571. input_data->x, input_data->len,
  5572. signature, signature_size,
  5573. &signature_length);
  5574. TEST_EQUAL(actual_status, expected_status);
  5575. /* The value of *signature_length is unspecified on error, but
  5576. * whatever it is, it should be less than signature_size, so that
  5577. * if the caller tries to read *signature_length bytes without
  5578. * checking the error code then they don't overflow a buffer. */
  5579. TEST_LE_U(signature_length, signature_size);
  5580. exit:
  5581. psa_reset_key_attributes(&attributes);
  5582. psa_destroy_key(key);
  5583. mbedtls_free(signature);
  5584. PSA_DONE();
  5585. }
  5586. /* END_CASE */
  5587. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
  5588. /**
  5589. * sign_hash_fail_interruptible() test intentions:
  5590. *
  5591. * Note: This test can currently only handle ECDSA.
  5592. *
  5593. * 1. Test that various failure cases for interruptible sign hash fail with the
  5594. * correct error codes, and at the correct point (at start or during
  5595. * complete).
  5596. *
  5597. * 2. Test the number of calls to psa_sign_hash_complete() required are as
  5598. * expected for different max_ops values.
  5599. *
  5600. * 3. Test that the number of ops done prior to start and after abort is zero
  5601. * and that each successful stage completes some ops (this is not mandated by
  5602. * the PSA specification, but is currently the case).
  5603. *
  5604. * 4. Check that calling complete() when start() fails and complete()
  5605. * after completion results in a BAD_STATE error.
  5606. *
  5607. * 5. Check that calling start() again after start fails results in a BAD_STATE
  5608. * error.
  5609. */
  5610. void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
  5611. int alg_arg, data_t *input_data,
  5612. int signature_size_arg,
  5613. int expected_start_status_arg,
  5614. int expected_complete_status_arg,
  5615. int max_ops_arg)
  5616. {
  5617. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  5618. psa_key_type_t key_type = key_type_arg;
  5619. psa_algorithm_t alg = alg_arg;
  5620. size_t signature_size = signature_size_arg;
  5621. psa_status_t actual_status;
  5622. psa_status_t expected_start_status = expected_start_status_arg;
  5623. psa_status_t expected_complete_status = expected_complete_status_arg;
  5624. unsigned char *signature = NULL;
  5625. size_t signature_length = 0xdeadbeef;
  5626. uint32_t num_ops = 0;
  5627. uint32_t max_ops = max_ops_arg;
  5628. size_t num_ops_prior = 0;
  5629. size_t num_completes = 0;
  5630. size_t min_completes = 0;
  5631. size_t max_completes = 0;
  5632. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  5633. psa_sign_hash_interruptible_operation_t operation =
  5634. psa_sign_hash_interruptible_operation_init();
  5635. ASSERT_ALLOC(signature, signature_size);
  5636. PSA_ASSERT(psa_crypto_init());
  5637. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
  5638. psa_set_key_algorithm(&attributes, alg);
  5639. psa_set_key_type(&attributes, key_type);
  5640. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  5641. &key));
  5642. psa_interruptible_set_max_ops(max_ops);
  5643. interruptible_signverify_get_minmax_completes(max_ops,
  5644. expected_complete_status,
  5645. &min_completes,
  5646. &max_completes);
  5647. num_ops_prior = psa_sign_hash_get_num_ops(&operation);
  5648. TEST_ASSERT(num_ops_prior == 0);
  5649. /* Start performing the signature. */
  5650. actual_status = psa_sign_hash_start(&operation, key, alg,
  5651. input_data->x, input_data->len);
  5652. TEST_EQUAL(actual_status, expected_start_status);
  5653. if (expected_start_status != PSA_SUCCESS) {
  5654. /* Emulate poor application code, and call complete anyway, even though
  5655. * start failed. */
  5656. actual_status = psa_sign_hash_complete(&operation, signature,
  5657. signature_size,
  5658. &signature_length);
  5659. TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
  5660. /* Test that calling start again after failure also causes BAD_STATE. */
  5661. actual_status = psa_sign_hash_start(&operation, key, alg,
  5662. input_data->x, input_data->len);
  5663. TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
  5664. }
  5665. num_ops_prior = psa_sign_hash_get_num_ops(&operation);
  5666. TEST_ASSERT(num_ops_prior == 0);
  5667. /* Continue performing the signature until complete. */
  5668. do {
  5669. actual_status = psa_sign_hash_complete(&operation, signature,
  5670. signature_size,
  5671. &signature_length);
  5672. num_completes++;
  5673. if (actual_status == PSA_SUCCESS ||
  5674. actual_status == PSA_OPERATION_INCOMPLETE) {
  5675. num_ops = psa_sign_hash_get_num_ops(&operation);
  5676. /* We are asserting here that every complete makes progress
  5677. * (completes some ops), which is true of the internal
  5678. * implementation and probably any implementation, however this is
  5679. * not mandated by the PSA specification. */
  5680. TEST_ASSERT(num_ops > num_ops_prior);
  5681. num_ops_prior = num_ops;
  5682. }
  5683. } while (actual_status == PSA_OPERATION_INCOMPLETE);
  5684. TEST_EQUAL(actual_status, expected_complete_status);
  5685. /* Check that another complete returns BAD_STATE. */
  5686. actual_status = psa_sign_hash_complete(&operation, signature,
  5687. signature_size,
  5688. &signature_length);
  5689. TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
  5690. PSA_ASSERT(psa_sign_hash_abort(&operation));
  5691. num_ops = psa_sign_hash_get_num_ops(&operation);
  5692. TEST_ASSERT(num_ops == 0);
  5693. /* The value of *signature_length is unspecified on error, but
  5694. * whatever it is, it should be less than signature_size, so that
  5695. * if the caller tries to read *signature_length bytes without
  5696. * checking the error code then they don't overflow a buffer. */
  5697. TEST_LE_U(signature_length, signature_size);
  5698. TEST_LE_U(min_completes, num_completes);
  5699. TEST_LE_U(num_completes, max_completes);
  5700. exit:
  5701. psa_reset_key_attributes(&attributes);
  5702. psa_destroy_key(key);
  5703. mbedtls_free(signature);
  5704. PSA_DONE();
  5705. }
  5706. /* END_CASE */
  5707. /* BEGIN_CASE */
  5708. void sign_verify_hash(int key_type_arg, data_t *key_data,
  5709. int alg_arg, data_t *input_data)
  5710. {
  5711. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  5712. psa_key_type_t key_type = key_type_arg;
  5713. psa_algorithm_t alg = alg_arg;
  5714. size_t key_bits;
  5715. unsigned char *signature = NULL;
  5716. size_t signature_size;
  5717. size_t signature_length = 0xdeadbeef;
  5718. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  5719. PSA_ASSERT(psa_crypto_init());
  5720. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
  5721. psa_set_key_algorithm(&attributes, alg);
  5722. psa_set_key_type(&attributes, key_type);
  5723. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  5724. &key));
  5725. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  5726. key_bits = psa_get_key_bits(&attributes);
  5727. /* Allocate a buffer which has the size advertised by the
  5728. * library. */
  5729. signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
  5730. key_bits, alg);
  5731. TEST_ASSERT(signature_size != 0);
  5732. TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
  5733. ASSERT_ALLOC(signature, signature_size);
  5734. /* Perform the signature. */
  5735. PSA_ASSERT(psa_sign_hash(key, alg,
  5736. input_data->x, input_data->len,
  5737. signature, signature_size,
  5738. &signature_length));
  5739. /* Check that the signature length looks sensible. */
  5740. TEST_LE_U(signature_length, signature_size);
  5741. TEST_ASSERT(signature_length > 0);
  5742. /* Use the library to verify that the signature is correct. */
  5743. PSA_ASSERT(psa_verify_hash(key, alg,
  5744. input_data->x, input_data->len,
  5745. signature, signature_length));
  5746. if (input_data->len != 0) {
  5747. /* Flip a bit in the input and verify that the signature is now
  5748. * detected as invalid. Flip a bit at the beginning, not at the end,
  5749. * because ECDSA may ignore the last few bits of the input. */
  5750. input_data->x[0] ^= 1;
  5751. TEST_EQUAL(psa_verify_hash(key, alg,
  5752. input_data->x, input_data->len,
  5753. signature, signature_length),
  5754. PSA_ERROR_INVALID_SIGNATURE);
  5755. }
  5756. exit:
  5757. /*
  5758. * Key attributes may have been returned by psa_get_key_attributes()
  5759. * thus reset them as required.
  5760. */
  5761. psa_reset_key_attributes(&attributes);
  5762. psa_destroy_key(key);
  5763. mbedtls_free(signature);
  5764. PSA_DONE();
  5765. }
  5766. /* END_CASE */
  5767. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
  5768. /**
  5769. * sign_verify_hash_interruptible() test intentions:
  5770. *
  5771. * Note: This test can currently only handle ECDSA.
  5772. *
  5773. * 1. Test that we can sign an input hash with the given keypair and then
  5774. * afterwards verify that signature. This is currently the only way to test
  5775. * non deterministic ECDSA, but this test can also handle deterministic.
  5776. *
  5777. * 2. Test that after corrupting the hash, the verification detects an invalid
  5778. * signature.
  5779. *
  5780. * 3. Test the number of calls to psa_sign_hash_complete() required are as
  5781. * expected for different max_ops values.
  5782. *
  5783. * 4. Test that the number of ops done prior to starting signing and after abort
  5784. * is zero and that each successful signing stage completes some ops (this is
  5785. * not mandated by the PSA specification, but is currently the case).
  5786. */
  5787. void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
  5788. int alg_arg, data_t *input_data,
  5789. int max_ops_arg)
  5790. {
  5791. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  5792. psa_key_type_t key_type = key_type_arg;
  5793. psa_algorithm_t alg = alg_arg;
  5794. size_t key_bits;
  5795. unsigned char *signature = NULL;
  5796. size_t signature_size;
  5797. size_t signature_length = 0xdeadbeef;
  5798. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  5799. psa_status_t status = PSA_OPERATION_INCOMPLETE;
  5800. uint32_t max_ops = max_ops_arg;
  5801. uint32_t num_ops = 0;
  5802. uint32_t num_ops_prior = 0;
  5803. size_t num_completes = 0;
  5804. size_t min_completes = 0;
  5805. size_t max_completes = 0;
  5806. psa_sign_hash_interruptible_operation_t sign_operation =
  5807. psa_sign_hash_interruptible_operation_init();
  5808. psa_verify_hash_interruptible_operation_t verify_operation =
  5809. psa_verify_hash_interruptible_operation_init();
  5810. PSA_ASSERT(psa_crypto_init());
  5811. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
  5812. PSA_KEY_USAGE_VERIFY_HASH);
  5813. psa_set_key_algorithm(&attributes, alg);
  5814. psa_set_key_type(&attributes, key_type);
  5815. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  5816. &key));
  5817. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  5818. key_bits = psa_get_key_bits(&attributes);
  5819. /* Allocate a buffer which has the size advertised by the
  5820. * library. */
  5821. signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
  5822. key_bits, alg);
  5823. TEST_ASSERT(signature_size != 0);
  5824. TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
  5825. ASSERT_ALLOC(signature, signature_size);
  5826. psa_interruptible_set_max_ops(max_ops);
  5827. interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
  5828. &min_completes, &max_completes);
  5829. num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
  5830. TEST_ASSERT(num_ops_prior == 0);
  5831. /* Start performing the signature. */
  5832. PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
  5833. input_data->x, input_data->len));
  5834. num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
  5835. TEST_ASSERT(num_ops_prior == 0);
  5836. /* Continue performing the signature until complete. */
  5837. do {
  5838. status = psa_sign_hash_complete(&sign_operation, signature,
  5839. signature_size,
  5840. &signature_length);
  5841. num_completes++;
  5842. if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
  5843. num_ops = psa_sign_hash_get_num_ops(&sign_operation);
  5844. /* We are asserting here that every complete makes progress
  5845. * (completes some ops), which is true of the internal
  5846. * implementation and probably any implementation, however this is
  5847. * not mandated by the PSA specification. */
  5848. TEST_ASSERT(num_ops > num_ops_prior);
  5849. num_ops_prior = num_ops;
  5850. }
  5851. } while (status == PSA_OPERATION_INCOMPLETE);
  5852. TEST_ASSERT(status == PSA_SUCCESS);
  5853. TEST_LE_U(min_completes, num_completes);
  5854. TEST_LE_U(num_completes, max_completes);
  5855. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  5856. num_ops = psa_sign_hash_get_num_ops(&sign_operation);
  5857. TEST_ASSERT(num_ops == 0);
  5858. /* Check that the signature length looks sensible. */
  5859. TEST_LE_U(signature_length, signature_size);
  5860. TEST_ASSERT(signature_length > 0);
  5861. num_completes = 0;
  5862. /* Start verification. */
  5863. PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
  5864. input_data->x, input_data->len,
  5865. signature, signature_length));
  5866. /* Continue performing the signature until complete. */
  5867. do {
  5868. status = psa_verify_hash_complete(&verify_operation);
  5869. num_completes++;
  5870. } while (status == PSA_OPERATION_INCOMPLETE);
  5871. TEST_ASSERT(status == PSA_SUCCESS);
  5872. TEST_LE_U(min_completes, num_completes);
  5873. TEST_LE_U(num_completes, max_completes);
  5874. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  5875. verify_operation = psa_verify_hash_interruptible_operation_init();
  5876. if (input_data->len != 0) {
  5877. /* Flip a bit in the input and verify that the signature is now
  5878. * detected as invalid. Flip a bit at the beginning, not at the end,
  5879. * because ECDSA may ignore the last few bits of the input. */
  5880. input_data->x[0] ^= 1;
  5881. /* Start verification. */
  5882. PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
  5883. input_data->x, input_data->len,
  5884. signature, signature_length));
  5885. /* Continue performing the signature until complete. */
  5886. do {
  5887. status = psa_verify_hash_complete(&verify_operation);
  5888. } while (status == PSA_OPERATION_INCOMPLETE);
  5889. TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
  5890. }
  5891. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  5892. exit:
  5893. /*
  5894. * Key attributes may have been returned by psa_get_key_attributes()
  5895. * thus reset them as required.
  5896. */
  5897. psa_reset_key_attributes(&attributes);
  5898. psa_destroy_key(key);
  5899. mbedtls_free(signature);
  5900. PSA_DONE();
  5901. }
  5902. /* END_CASE */
  5903. /* BEGIN_CASE */
  5904. void verify_hash(int key_type_arg, data_t *key_data,
  5905. int alg_arg, data_t *hash_data,
  5906. data_t *signature_data)
  5907. {
  5908. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  5909. psa_key_type_t key_type = key_type_arg;
  5910. psa_algorithm_t alg = alg_arg;
  5911. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  5912. TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
  5913. PSA_ASSERT(psa_crypto_init());
  5914. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
  5915. psa_set_key_algorithm(&attributes, alg);
  5916. psa_set_key_type(&attributes, key_type);
  5917. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  5918. &key));
  5919. PSA_ASSERT(psa_verify_hash(key, alg,
  5920. hash_data->x, hash_data->len,
  5921. signature_data->x, signature_data->len));
  5922. exit:
  5923. psa_reset_key_attributes(&attributes);
  5924. psa_destroy_key(key);
  5925. PSA_DONE();
  5926. }
  5927. /* END_CASE */
  5928. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
  5929. /**
  5930. * verify_hash_interruptible() test intentions:
  5931. *
  5932. * Note: This test can currently only handle ECDSA.
  5933. *
  5934. * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
  5935. * only). Given this test only does verification it can accept public keys as
  5936. * well as private keys / keypairs.
  5937. *
  5938. * 2. Test the number of calls to psa_verify_hash_complete() required are as
  5939. * expected for different max_ops values.
  5940. *
  5941. * 3. Test that the number of ops done prior to start and after abort is zero
  5942. * and that each successful stage completes some ops (this is not mandated by
  5943. * the PSA specification, but is currently the case).
  5944. *
  5945. * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
  5946. * complete() calls does not alter the number of ops returned.
  5947. *
  5948. * 5. Test that after corrupting the hash, the verification detects an invalid
  5949. * signature.
  5950. */
  5951. void verify_hash_interruptible(int key_type_arg, data_t *key_data,
  5952. int alg_arg, data_t *hash_data,
  5953. data_t *signature_data, int max_ops_arg)
  5954. {
  5955. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  5956. psa_key_type_t key_type = key_type_arg;
  5957. psa_algorithm_t alg = alg_arg;
  5958. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  5959. psa_status_t status = PSA_OPERATION_INCOMPLETE;
  5960. uint32_t num_ops = 0;
  5961. uint32_t max_ops = max_ops_arg;
  5962. size_t num_ops_prior = 0;
  5963. size_t num_completes = 0;
  5964. size_t min_completes = 0;
  5965. size_t max_completes = 0;
  5966. psa_verify_hash_interruptible_operation_t operation =
  5967. psa_verify_hash_interruptible_operation_init();
  5968. TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
  5969. PSA_ASSERT(psa_crypto_init());
  5970. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
  5971. psa_set_key_algorithm(&attributes, alg);
  5972. psa_set_key_type(&attributes, key_type);
  5973. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  5974. &key));
  5975. psa_interruptible_set_max_ops(max_ops);
  5976. interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
  5977. &min_completes, &max_completes);
  5978. num_ops_prior = psa_verify_hash_get_num_ops(&operation);
  5979. TEST_ASSERT(num_ops_prior == 0);
  5980. /* Start verification. */
  5981. PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
  5982. hash_data->x, hash_data->len,
  5983. signature_data->x, signature_data->len)
  5984. );
  5985. num_ops_prior = psa_verify_hash_get_num_ops(&operation);
  5986. TEST_ASSERT(num_ops_prior == 0);
  5987. /* Continue performing the signature until complete. */
  5988. do {
  5989. status = psa_verify_hash_complete(&operation);
  5990. num_completes++;
  5991. if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
  5992. num_ops = psa_verify_hash_get_num_ops(&operation);
  5993. /* We are asserting here that every complete makes progress
  5994. * (completes some ops), which is true of the internal
  5995. * implementation and probably any implementation, however this is
  5996. * not mandated by the PSA specification. */
  5997. TEST_ASSERT(num_ops > num_ops_prior);
  5998. num_ops_prior = num_ops;
  5999. /* Ensure calling get_num_ops() twice still returns the same
  6000. * number of ops as previously reported. */
  6001. num_ops = psa_verify_hash_get_num_ops(&operation);
  6002. TEST_EQUAL(num_ops, num_ops_prior);
  6003. }
  6004. } while (status == PSA_OPERATION_INCOMPLETE);
  6005. TEST_ASSERT(status == PSA_SUCCESS);
  6006. TEST_LE_U(min_completes, num_completes);
  6007. TEST_LE_U(num_completes, max_completes);
  6008. PSA_ASSERT(psa_verify_hash_abort(&operation));
  6009. num_ops = psa_verify_hash_get_num_ops(&operation);
  6010. TEST_ASSERT(num_ops == 0);
  6011. if (hash_data->len != 0) {
  6012. /* Flip a bit in the hash and verify that the signature is now detected
  6013. * as invalid. Flip a bit at the beginning, not at the end, because
  6014. * ECDSA may ignore the last few bits of the input. */
  6015. hash_data->x[0] ^= 1;
  6016. /* Start verification. */
  6017. PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
  6018. hash_data->x, hash_data->len,
  6019. signature_data->x, signature_data->len));
  6020. /* Continue performing the signature until complete. */
  6021. do {
  6022. status = psa_verify_hash_complete(&operation);
  6023. } while (status == PSA_OPERATION_INCOMPLETE);
  6024. TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
  6025. }
  6026. exit:
  6027. psa_reset_key_attributes(&attributes);
  6028. psa_destroy_key(key);
  6029. PSA_DONE();
  6030. }
  6031. /* END_CASE */
  6032. /* BEGIN_CASE */
  6033. void verify_hash_fail(int key_type_arg, data_t *key_data,
  6034. int alg_arg, data_t *hash_data,
  6035. data_t *signature_data,
  6036. int expected_status_arg)
  6037. {
  6038. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6039. psa_key_type_t key_type = key_type_arg;
  6040. psa_algorithm_t alg = alg_arg;
  6041. psa_status_t actual_status;
  6042. psa_status_t expected_status = expected_status_arg;
  6043. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6044. PSA_ASSERT(psa_crypto_init());
  6045. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
  6046. psa_set_key_algorithm(&attributes, alg);
  6047. psa_set_key_type(&attributes, key_type);
  6048. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6049. &key));
  6050. actual_status = psa_verify_hash(key, alg,
  6051. hash_data->x, hash_data->len,
  6052. signature_data->x, signature_data->len);
  6053. TEST_EQUAL(actual_status, expected_status);
  6054. exit:
  6055. psa_reset_key_attributes(&attributes);
  6056. psa_destroy_key(key);
  6057. PSA_DONE();
  6058. }
  6059. /* END_CASE */
  6060. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
  6061. /**
  6062. * verify_hash_fail_interruptible() test intentions:
  6063. *
  6064. * Note: This test can currently only handle ECDSA.
  6065. *
  6066. * 1. Test that various failure cases for interruptible verify hash fail with
  6067. * the correct error codes, and at the correct point (at start or during
  6068. * complete).
  6069. *
  6070. * 2. Test the number of calls to psa_verify_hash_complete() required are as
  6071. * expected for different max_ops values.
  6072. *
  6073. * 3. Test that the number of ops done prior to start and after abort is zero
  6074. * and that each successful stage completes some ops (this is not mandated by
  6075. * the PSA specification, but is currently the case).
  6076. *
  6077. * 4. Check that calling complete() when start() fails and complete()
  6078. * after completion results in a BAD_STATE error.
  6079. *
  6080. * 5. Check that calling start() again after start fails results in a BAD_STATE
  6081. * error.
  6082. */
  6083. void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
  6084. int alg_arg, data_t *hash_data,
  6085. data_t *signature_data,
  6086. int expected_start_status_arg,
  6087. int expected_complete_status_arg,
  6088. int max_ops_arg)
  6089. {
  6090. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6091. psa_key_type_t key_type = key_type_arg;
  6092. psa_algorithm_t alg = alg_arg;
  6093. psa_status_t actual_status;
  6094. psa_status_t expected_start_status = expected_start_status_arg;
  6095. psa_status_t expected_complete_status = expected_complete_status_arg;
  6096. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6097. uint32_t num_ops = 0;
  6098. uint32_t max_ops = max_ops_arg;
  6099. size_t num_ops_prior = 0;
  6100. size_t num_completes = 0;
  6101. size_t min_completes = 0;
  6102. size_t max_completes = 0;
  6103. psa_verify_hash_interruptible_operation_t operation =
  6104. psa_verify_hash_interruptible_operation_init();
  6105. PSA_ASSERT(psa_crypto_init());
  6106. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
  6107. psa_set_key_algorithm(&attributes, alg);
  6108. psa_set_key_type(&attributes, key_type);
  6109. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6110. &key));
  6111. psa_interruptible_set_max_ops(max_ops);
  6112. interruptible_signverify_get_minmax_completes(max_ops,
  6113. expected_complete_status,
  6114. &min_completes,
  6115. &max_completes);
  6116. num_ops_prior = psa_verify_hash_get_num_ops(&operation);
  6117. TEST_ASSERT(num_ops_prior == 0);
  6118. /* Start verification. */
  6119. actual_status = psa_verify_hash_start(&operation, key, alg,
  6120. hash_data->x, hash_data->len,
  6121. signature_data->x,
  6122. signature_data->len);
  6123. TEST_EQUAL(actual_status, expected_start_status);
  6124. if (expected_start_status != PSA_SUCCESS) {
  6125. /* Emulate poor application code, and call complete anyway, even though
  6126. * start failed. */
  6127. actual_status = psa_verify_hash_complete(&operation);
  6128. TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
  6129. /* Test that calling start again after failure also causes BAD_STATE. */
  6130. actual_status = psa_verify_hash_start(&operation, key, alg,
  6131. hash_data->x, hash_data->len,
  6132. signature_data->x,
  6133. signature_data->len);
  6134. TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
  6135. }
  6136. num_ops_prior = psa_verify_hash_get_num_ops(&operation);
  6137. TEST_ASSERT(num_ops_prior == 0);
  6138. /* Continue performing the signature until complete. */
  6139. do {
  6140. actual_status = psa_verify_hash_complete(&operation);
  6141. num_completes++;
  6142. if (actual_status == PSA_SUCCESS ||
  6143. actual_status == PSA_OPERATION_INCOMPLETE) {
  6144. num_ops = psa_verify_hash_get_num_ops(&operation);
  6145. /* We are asserting here that every complete makes progress
  6146. * (completes some ops), which is true of the internal
  6147. * implementation and probably any implementation, however this is
  6148. * not mandated by the PSA specification. */
  6149. TEST_ASSERT(num_ops > num_ops_prior);
  6150. num_ops_prior = num_ops;
  6151. }
  6152. } while (actual_status == PSA_OPERATION_INCOMPLETE);
  6153. TEST_EQUAL(actual_status, expected_complete_status);
  6154. /* Check that another complete returns BAD_STATE. */
  6155. actual_status = psa_verify_hash_complete(&operation);
  6156. TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
  6157. TEST_LE_U(min_completes, num_completes);
  6158. TEST_LE_U(num_completes, max_completes);
  6159. PSA_ASSERT(psa_verify_hash_abort(&operation));
  6160. num_ops = psa_verify_hash_get_num_ops(&operation);
  6161. TEST_ASSERT(num_ops == 0);
  6162. exit:
  6163. psa_reset_key_attributes(&attributes);
  6164. psa_destroy_key(key);
  6165. PSA_DONE();
  6166. }
  6167. /* END_CASE */
  6168. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
  6169. /**
  6170. * interruptible_signverify_hash_state_test() test intentions:
  6171. *
  6172. * Note: This test can currently only handle ECDSA.
  6173. *
  6174. * 1. Test that calling the various interruptible sign and verify hash functions
  6175. * in incorrect orders returns BAD_STATE errors.
  6176. */
  6177. void interruptible_signverify_hash_state_test(int key_type_arg,
  6178. data_t *key_data, int alg_arg, data_t *input_data)
  6179. {
  6180. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6181. psa_key_type_t key_type = key_type_arg;
  6182. psa_algorithm_t alg = alg_arg;
  6183. size_t key_bits;
  6184. unsigned char *signature = NULL;
  6185. size_t signature_size;
  6186. size_t signature_length = 0xdeadbeef;
  6187. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6188. psa_sign_hash_interruptible_operation_t sign_operation =
  6189. psa_sign_hash_interruptible_operation_init();
  6190. psa_verify_hash_interruptible_operation_t verify_operation =
  6191. psa_verify_hash_interruptible_operation_init();
  6192. PSA_ASSERT(psa_crypto_init());
  6193. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
  6194. PSA_KEY_USAGE_VERIFY_HASH);
  6195. psa_set_key_algorithm(&attributes, alg);
  6196. psa_set_key_type(&attributes, key_type);
  6197. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6198. &key));
  6199. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  6200. key_bits = psa_get_key_bits(&attributes);
  6201. /* Allocate a buffer which has the size advertised by the
  6202. * library. */
  6203. signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
  6204. key_bits, alg);
  6205. TEST_ASSERT(signature_size != 0);
  6206. TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
  6207. ASSERT_ALLOC(signature, signature_size);
  6208. psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
  6209. /* --- Attempt completes prior to starts --- */
  6210. TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
  6211. signature_size,
  6212. &signature_length),
  6213. PSA_ERROR_BAD_STATE);
  6214. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  6215. TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
  6216. PSA_ERROR_BAD_STATE);
  6217. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  6218. /* --- Aborts in all other places. --- */
  6219. psa_sign_hash_abort(&sign_operation);
  6220. PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
  6221. input_data->x, input_data->len));
  6222. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  6223. psa_interruptible_set_max_ops(1);
  6224. PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
  6225. input_data->x, input_data->len));
  6226. TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
  6227. signature_size,
  6228. &signature_length),
  6229. PSA_OPERATION_INCOMPLETE);
  6230. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  6231. psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
  6232. PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
  6233. input_data->x, input_data->len));
  6234. PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
  6235. signature_size,
  6236. &signature_length));
  6237. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  6238. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  6239. PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
  6240. input_data->x, input_data->len,
  6241. signature, signature_length));
  6242. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  6243. psa_interruptible_set_max_ops(1);
  6244. PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
  6245. input_data->x, input_data->len,
  6246. signature, signature_length));
  6247. TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
  6248. PSA_OPERATION_INCOMPLETE);
  6249. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  6250. psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
  6251. PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
  6252. input_data->x, input_data->len,
  6253. signature, signature_length));
  6254. PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
  6255. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  6256. /* --- Attempt double starts. --- */
  6257. PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
  6258. input_data->x, input_data->len));
  6259. TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
  6260. input_data->x, input_data->len),
  6261. PSA_ERROR_BAD_STATE);
  6262. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  6263. PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
  6264. input_data->x, input_data->len,
  6265. signature, signature_length));
  6266. TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
  6267. input_data->x, input_data->len,
  6268. signature, signature_length),
  6269. PSA_ERROR_BAD_STATE);
  6270. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  6271. exit:
  6272. /*
  6273. * Key attributes may have been returned by psa_get_key_attributes()
  6274. * thus reset them as required.
  6275. */
  6276. psa_reset_key_attributes(&attributes);
  6277. psa_destroy_key(key);
  6278. mbedtls_free(signature);
  6279. PSA_DONE();
  6280. }
  6281. /* END_CASE */
  6282. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
  6283. /**
  6284. * interruptible_signverify_hash_edgecase_tests() test intentions:
  6285. *
  6286. * Note: This test can currently only handle ECDSA.
  6287. *
  6288. * 1. Test various edge cases in the interruptible sign and verify hash
  6289. * interfaces.
  6290. */
  6291. void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
  6292. data_t *key_data, int alg_arg, data_t *input_data)
  6293. {
  6294. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6295. psa_key_type_t key_type = key_type_arg;
  6296. psa_algorithm_t alg = alg_arg;
  6297. size_t key_bits;
  6298. unsigned char *signature = NULL;
  6299. size_t signature_size;
  6300. size_t signature_length = 0xdeadbeef;
  6301. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6302. uint8_t *input_buffer = NULL;
  6303. psa_sign_hash_interruptible_operation_t sign_operation =
  6304. psa_sign_hash_interruptible_operation_init();
  6305. psa_verify_hash_interruptible_operation_t verify_operation =
  6306. psa_verify_hash_interruptible_operation_init();
  6307. PSA_ASSERT(psa_crypto_init());
  6308. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
  6309. PSA_KEY_USAGE_VERIFY_HASH);
  6310. psa_set_key_algorithm(&attributes, alg);
  6311. psa_set_key_type(&attributes, key_type);
  6312. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6313. &key));
  6314. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  6315. key_bits = psa_get_key_bits(&attributes);
  6316. /* Allocate a buffer which has the size advertised by the
  6317. * library. */
  6318. signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
  6319. key_bits, alg);
  6320. TEST_ASSERT(signature_size != 0);
  6321. TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
  6322. ASSERT_ALLOC(signature, signature_size);
  6323. /* --- Change function inputs mid run, to cause an error (sign only,
  6324. * verify passes all inputs to start. --- */
  6325. psa_interruptible_set_max_ops(1);
  6326. PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
  6327. input_data->x, input_data->len));
  6328. TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
  6329. signature_size,
  6330. &signature_length),
  6331. PSA_OPERATION_INCOMPLETE);
  6332. TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
  6333. 0,
  6334. &signature_length),
  6335. PSA_ERROR_BUFFER_TOO_SMALL);
  6336. /* And test that this invalidates the operation. */
  6337. TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
  6338. 0,
  6339. &signature_length),
  6340. PSA_ERROR_BAD_STATE);
  6341. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  6342. /* Trash the hash buffer in between start and complete, to ensure
  6343. * no reliance on external buffers. */
  6344. psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
  6345. input_buffer = mbedtls_calloc(1, input_data->len);
  6346. TEST_ASSERT(input_buffer != NULL);
  6347. memcpy(input_buffer, input_data->x, input_data->len);
  6348. PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
  6349. input_buffer, input_data->len));
  6350. memset(input_buffer, '!', input_data->len);
  6351. mbedtls_free(input_buffer);
  6352. input_buffer = NULL;
  6353. PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
  6354. signature_size,
  6355. &signature_length));
  6356. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  6357. input_buffer = mbedtls_calloc(1, input_data->len);
  6358. TEST_ASSERT(input_buffer != NULL);
  6359. memcpy(input_buffer, input_data->x, input_data->len);
  6360. PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
  6361. input_buffer, input_data->len,
  6362. signature, signature_length));
  6363. memset(input_buffer, '!', input_data->len);
  6364. mbedtls_free(input_buffer);
  6365. input_buffer = NULL;
  6366. PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
  6367. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  6368. exit:
  6369. /*
  6370. * Key attributes may have been returned by psa_get_key_attributes()
  6371. * thus reset them as required.
  6372. */
  6373. psa_reset_key_attributes(&attributes);
  6374. psa_destroy_key(key);
  6375. mbedtls_free(signature);
  6376. PSA_DONE();
  6377. }
  6378. /* END_CASE */
  6379. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
  6380. /**
  6381. * interruptible_signverify_hash_ops_tests() test intentions:
  6382. *
  6383. * Note: This test can currently only handle ECDSA.
  6384. *
  6385. * 1. Test that setting max ops is reflected in both interruptible sign and
  6386. * verify hash
  6387. * 2. Test that changing the value of max_ops to unlimited during an operation
  6388. * causes that operation to complete in the next call.
  6389. *
  6390. * 3. Test that calling get_num_ops() between complete calls gives the same
  6391. * result as calling get_num_ops() once at the end of the operation.
  6392. */
  6393. void interruptible_signverify_hash_ops_tests(int key_type_arg,
  6394. data_t *key_data, int alg_arg,
  6395. data_t *input_data)
  6396. {
  6397. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6398. psa_key_type_t key_type = key_type_arg;
  6399. psa_algorithm_t alg = alg_arg;
  6400. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6401. size_t key_bits;
  6402. unsigned char *signature = NULL;
  6403. size_t signature_size;
  6404. size_t signature_length = 0xdeadbeef;
  6405. uint32_t num_ops = 0;
  6406. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  6407. psa_sign_hash_interruptible_operation_t sign_operation =
  6408. psa_sign_hash_interruptible_operation_init();
  6409. psa_verify_hash_interruptible_operation_t verify_operation =
  6410. psa_verify_hash_interruptible_operation_init();
  6411. PSA_ASSERT(psa_crypto_init());
  6412. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
  6413. PSA_KEY_USAGE_VERIFY_HASH);
  6414. psa_set_key_algorithm(&attributes, alg);
  6415. psa_set_key_type(&attributes, key_type);
  6416. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
  6417. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  6418. key_bits = psa_get_key_bits(&attributes);
  6419. /* Allocate a buffer which has the size advertised by the
  6420. * library. */
  6421. signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
  6422. TEST_ASSERT(signature_size != 0);
  6423. TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
  6424. ASSERT_ALLOC(signature, signature_size);
  6425. /* Check that default max ops gets set if we don't set it. */
  6426. PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
  6427. input_data->x, input_data->len));
  6428. TEST_EQUAL(psa_interruptible_get_max_ops(),
  6429. PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
  6430. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  6431. PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
  6432. input_data->x, input_data->len,
  6433. signature, signature_size));
  6434. TEST_EQUAL(psa_interruptible_get_max_ops(),
  6435. PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
  6436. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  6437. /* Check that max ops gets set properly. */
  6438. psa_interruptible_set_max_ops(0xbeef);
  6439. TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
  6440. /* --- Ensure changing the max ops mid operation works (operation should
  6441. * complete successfully after setting max ops to unlimited --- */
  6442. psa_interruptible_set_max_ops(1);
  6443. PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
  6444. input_data->x, input_data->len));
  6445. TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
  6446. signature_size,
  6447. &signature_length),
  6448. PSA_OPERATION_INCOMPLETE);
  6449. psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
  6450. PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
  6451. signature_size,
  6452. &signature_length));
  6453. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  6454. psa_interruptible_set_max_ops(1);
  6455. PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
  6456. input_data->x, input_data->len,
  6457. signature, signature_length));
  6458. TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
  6459. PSA_OPERATION_INCOMPLETE);
  6460. psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
  6461. PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
  6462. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  6463. /* --- Test that not calling get_num_ops inbetween complete calls does not
  6464. * result in lost ops. ---*/
  6465. psa_interruptible_set_max_ops(1);
  6466. PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
  6467. input_data->x, input_data->len));
  6468. /* Continue performing the signature until complete. */
  6469. do {
  6470. status = psa_sign_hash_complete(&sign_operation, signature,
  6471. signature_size,
  6472. &signature_length);
  6473. num_ops = psa_sign_hash_get_num_ops(&sign_operation);
  6474. } while (status == PSA_OPERATION_INCOMPLETE);
  6475. PSA_ASSERT(status);
  6476. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  6477. PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
  6478. input_data->x, input_data->len));
  6479. /* Continue performing the signature until complete. */
  6480. do {
  6481. status = psa_sign_hash_complete(&sign_operation, signature,
  6482. signature_size,
  6483. &signature_length);
  6484. } while (status == PSA_OPERATION_INCOMPLETE);
  6485. PSA_ASSERT(status);
  6486. TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
  6487. PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
  6488. PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
  6489. input_data->x, input_data->len,
  6490. signature, signature_length));
  6491. /* Continue performing the verification until complete. */
  6492. do {
  6493. status = psa_verify_hash_complete(&verify_operation);
  6494. num_ops = psa_verify_hash_get_num_ops(&verify_operation);
  6495. } while (status == PSA_OPERATION_INCOMPLETE);
  6496. PSA_ASSERT(status);
  6497. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  6498. PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
  6499. input_data->x, input_data->len,
  6500. signature, signature_length));
  6501. /* Continue performing the verification until complete. */
  6502. do {
  6503. status = psa_verify_hash_complete(&verify_operation);
  6504. } while (status == PSA_OPERATION_INCOMPLETE);
  6505. PSA_ASSERT(status);
  6506. TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
  6507. PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
  6508. exit:
  6509. /*
  6510. * Key attributes may have been returned by psa_get_key_attributes()
  6511. * thus reset them as required.
  6512. */
  6513. psa_reset_key_attributes(&attributes);
  6514. psa_destroy_key(key);
  6515. mbedtls_free(signature);
  6516. PSA_DONE();
  6517. }
  6518. /* END_CASE */
  6519. /* BEGIN_CASE */
  6520. void sign_message_deterministic(int key_type_arg,
  6521. data_t *key_data,
  6522. int alg_arg,
  6523. data_t *input_data,
  6524. data_t *output_data)
  6525. {
  6526. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6527. psa_key_type_t key_type = key_type_arg;
  6528. psa_algorithm_t alg = alg_arg;
  6529. size_t key_bits;
  6530. unsigned char *signature = NULL;
  6531. size_t signature_size;
  6532. size_t signature_length = 0xdeadbeef;
  6533. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6534. PSA_ASSERT(psa_crypto_init());
  6535. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
  6536. psa_set_key_algorithm(&attributes, alg);
  6537. psa_set_key_type(&attributes, key_type);
  6538. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6539. &key));
  6540. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  6541. key_bits = psa_get_key_bits(&attributes);
  6542. signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
  6543. TEST_ASSERT(signature_size != 0);
  6544. TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
  6545. ASSERT_ALLOC(signature, signature_size);
  6546. PSA_ASSERT(psa_sign_message(key, alg,
  6547. input_data->x, input_data->len,
  6548. signature, signature_size,
  6549. &signature_length));
  6550. ASSERT_COMPARE(output_data->x, output_data->len,
  6551. signature, signature_length);
  6552. exit:
  6553. psa_reset_key_attributes(&attributes);
  6554. psa_destroy_key(key);
  6555. mbedtls_free(signature);
  6556. PSA_DONE();
  6557. }
  6558. /* END_CASE */
  6559. /* BEGIN_CASE */
  6560. void sign_message_fail(int key_type_arg,
  6561. data_t *key_data,
  6562. int alg_arg,
  6563. data_t *input_data,
  6564. int signature_size_arg,
  6565. int expected_status_arg)
  6566. {
  6567. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6568. psa_key_type_t key_type = key_type_arg;
  6569. psa_algorithm_t alg = alg_arg;
  6570. size_t signature_size = signature_size_arg;
  6571. psa_status_t actual_status;
  6572. psa_status_t expected_status = expected_status_arg;
  6573. unsigned char *signature = NULL;
  6574. size_t signature_length = 0xdeadbeef;
  6575. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6576. ASSERT_ALLOC(signature, signature_size);
  6577. PSA_ASSERT(psa_crypto_init());
  6578. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
  6579. psa_set_key_algorithm(&attributes, alg);
  6580. psa_set_key_type(&attributes, key_type);
  6581. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6582. &key));
  6583. actual_status = psa_sign_message(key, alg,
  6584. input_data->x, input_data->len,
  6585. signature, signature_size,
  6586. &signature_length);
  6587. TEST_EQUAL(actual_status, expected_status);
  6588. /* The value of *signature_length is unspecified on error, but
  6589. * whatever it is, it should be less than signature_size, so that
  6590. * if the caller tries to read *signature_length bytes without
  6591. * checking the error code then they don't overflow a buffer. */
  6592. TEST_LE_U(signature_length, signature_size);
  6593. exit:
  6594. psa_reset_key_attributes(&attributes);
  6595. psa_destroy_key(key);
  6596. mbedtls_free(signature);
  6597. PSA_DONE();
  6598. }
  6599. /* END_CASE */
  6600. /* BEGIN_CASE */
  6601. void sign_verify_message(int key_type_arg,
  6602. data_t *key_data,
  6603. int alg_arg,
  6604. data_t *input_data)
  6605. {
  6606. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6607. psa_key_type_t key_type = key_type_arg;
  6608. psa_algorithm_t alg = alg_arg;
  6609. size_t key_bits;
  6610. unsigned char *signature = NULL;
  6611. size_t signature_size;
  6612. size_t signature_length = 0xdeadbeef;
  6613. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6614. PSA_ASSERT(psa_crypto_init());
  6615. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
  6616. PSA_KEY_USAGE_VERIFY_MESSAGE);
  6617. psa_set_key_algorithm(&attributes, alg);
  6618. psa_set_key_type(&attributes, key_type);
  6619. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6620. &key));
  6621. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  6622. key_bits = psa_get_key_bits(&attributes);
  6623. signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
  6624. TEST_ASSERT(signature_size != 0);
  6625. TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
  6626. ASSERT_ALLOC(signature, signature_size);
  6627. PSA_ASSERT(psa_sign_message(key, alg,
  6628. input_data->x, input_data->len,
  6629. signature, signature_size,
  6630. &signature_length));
  6631. TEST_LE_U(signature_length, signature_size);
  6632. TEST_ASSERT(signature_length > 0);
  6633. PSA_ASSERT(psa_verify_message(key, alg,
  6634. input_data->x, input_data->len,
  6635. signature, signature_length));
  6636. if (input_data->len != 0) {
  6637. /* Flip a bit in the input and verify that the signature is now
  6638. * detected as invalid. Flip a bit at the beginning, not at the end,
  6639. * because ECDSA may ignore the last few bits of the input. */
  6640. input_data->x[0] ^= 1;
  6641. TEST_EQUAL(psa_verify_message(key, alg,
  6642. input_data->x, input_data->len,
  6643. signature, signature_length),
  6644. PSA_ERROR_INVALID_SIGNATURE);
  6645. }
  6646. exit:
  6647. psa_reset_key_attributes(&attributes);
  6648. psa_destroy_key(key);
  6649. mbedtls_free(signature);
  6650. PSA_DONE();
  6651. }
  6652. /* END_CASE */
  6653. /* BEGIN_CASE */
  6654. void verify_message(int key_type_arg,
  6655. data_t *key_data,
  6656. int alg_arg,
  6657. data_t *input_data,
  6658. data_t *signature_data)
  6659. {
  6660. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6661. psa_key_type_t key_type = key_type_arg;
  6662. psa_algorithm_t alg = alg_arg;
  6663. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6664. TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
  6665. PSA_ASSERT(psa_crypto_init());
  6666. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
  6667. psa_set_key_algorithm(&attributes, alg);
  6668. psa_set_key_type(&attributes, key_type);
  6669. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6670. &key));
  6671. PSA_ASSERT(psa_verify_message(key, alg,
  6672. input_data->x, input_data->len,
  6673. signature_data->x, signature_data->len));
  6674. exit:
  6675. psa_reset_key_attributes(&attributes);
  6676. psa_destroy_key(key);
  6677. PSA_DONE();
  6678. }
  6679. /* END_CASE */
  6680. /* BEGIN_CASE */
  6681. void verify_message_fail(int key_type_arg,
  6682. data_t *key_data,
  6683. int alg_arg,
  6684. data_t *hash_data,
  6685. data_t *signature_data,
  6686. int expected_status_arg)
  6687. {
  6688. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6689. psa_key_type_t key_type = key_type_arg;
  6690. psa_algorithm_t alg = alg_arg;
  6691. psa_status_t actual_status;
  6692. psa_status_t expected_status = expected_status_arg;
  6693. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6694. PSA_ASSERT(psa_crypto_init());
  6695. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
  6696. psa_set_key_algorithm(&attributes, alg);
  6697. psa_set_key_type(&attributes, key_type);
  6698. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6699. &key));
  6700. actual_status = psa_verify_message(key, alg,
  6701. hash_data->x, hash_data->len,
  6702. signature_data->x,
  6703. signature_data->len);
  6704. TEST_EQUAL(actual_status, expected_status);
  6705. exit:
  6706. psa_reset_key_attributes(&attributes);
  6707. psa_destroy_key(key);
  6708. PSA_DONE();
  6709. }
  6710. /* END_CASE */
  6711. /* BEGIN_CASE */
  6712. void asymmetric_encrypt(int key_type_arg,
  6713. data_t *key_data,
  6714. int alg_arg,
  6715. data_t *input_data,
  6716. data_t *label,
  6717. int expected_output_length_arg,
  6718. int expected_status_arg)
  6719. {
  6720. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6721. psa_key_type_t key_type = key_type_arg;
  6722. psa_algorithm_t alg = alg_arg;
  6723. size_t expected_output_length = expected_output_length_arg;
  6724. size_t key_bits;
  6725. unsigned char *output = NULL;
  6726. size_t output_size;
  6727. size_t output_length = ~0;
  6728. psa_status_t actual_status;
  6729. psa_status_t expected_status = expected_status_arg;
  6730. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6731. PSA_ASSERT(psa_crypto_init());
  6732. /* Import the key */
  6733. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
  6734. psa_set_key_algorithm(&attributes, alg);
  6735. psa_set_key_type(&attributes, key_type);
  6736. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6737. &key));
  6738. /* Determine the maximum output length */
  6739. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  6740. key_bits = psa_get_key_bits(&attributes);
  6741. output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
  6742. TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
  6743. ASSERT_ALLOC(output, output_size);
  6744. /* Encrypt the input */
  6745. actual_status = psa_asymmetric_encrypt(key, alg,
  6746. input_data->x, input_data->len,
  6747. label->x, label->len,
  6748. output, output_size,
  6749. &output_length);
  6750. TEST_EQUAL(actual_status, expected_status);
  6751. if (actual_status == PSA_SUCCESS) {
  6752. TEST_EQUAL(output_length, expected_output_length);
  6753. } else {
  6754. TEST_LE_U(output_length, output_size);
  6755. }
  6756. /* If the label is empty, the test framework puts a non-null pointer
  6757. * in label->x. Test that a null pointer works as well. */
  6758. if (label->len == 0) {
  6759. output_length = ~0;
  6760. if (output_size != 0) {
  6761. memset(output, 0, output_size);
  6762. }
  6763. actual_status = psa_asymmetric_encrypt(key, alg,
  6764. input_data->x, input_data->len,
  6765. NULL, label->len,
  6766. output, output_size,
  6767. &output_length);
  6768. TEST_EQUAL(actual_status, expected_status);
  6769. if (actual_status == PSA_SUCCESS) {
  6770. TEST_EQUAL(output_length, expected_output_length);
  6771. } else {
  6772. TEST_LE_U(output_length, output_size);
  6773. }
  6774. }
  6775. exit:
  6776. /*
  6777. * Key attributes may have been returned by psa_get_key_attributes()
  6778. * thus reset them as required.
  6779. */
  6780. psa_reset_key_attributes(&attributes);
  6781. psa_destroy_key(key);
  6782. mbedtls_free(output);
  6783. PSA_DONE();
  6784. }
  6785. /* END_CASE */
  6786. /* BEGIN_CASE */
  6787. void asymmetric_encrypt_decrypt(int key_type_arg,
  6788. data_t *key_data,
  6789. int alg_arg,
  6790. data_t *input_data,
  6791. data_t *label)
  6792. {
  6793. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6794. psa_key_type_t key_type = key_type_arg;
  6795. psa_algorithm_t alg = alg_arg;
  6796. size_t key_bits;
  6797. unsigned char *output = NULL;
  6798. size_t output_size;
  6799. size_t output_length = ~0;
  6800. unsigned char *output2 = NULL;
  6801. size_t output2_size;
  6802. size_t output2_length = ~0;
  6803. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6804. PSA_ASSERT(psa_crypto_init());
  6805. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
  6806. psa_set_key_algorithm(&attributes, alg);
  6807. psa_set_key_type(&attributes, key_type);
  6808. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6809. &key));
  6810. /* Determine the maximum ciphertext length */
  6811. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  6812. key_bits = psa_get_key_bits(&attributes);
  6813. output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
  6814. TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
  6815. ASSERT_ALLOC(output, output_size);
  6816. output2_size = input_data->len;
  6817. TEST_LE_U(output2_size,
  6818. PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
  6819. TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
  6820. ASSERT_ALLOC(output2, output2_size);
  6821. /* We test encryption by checking that encrypt-then-decrypt gives back
  6822. * the original plaintext because of the non-optional random
  6823. * part of encryption process which prevents using fixed vectors. */
  6824. PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
  6825. input_data->x, input_data->len,
  6826. label->x, label->len,
  6827. output, output_size,
  6828. &output_length));
  6829. /* We don't know what ciphertext length to expect, but check that
  6830. * it looks sensible. */
  6831. TEST_LE_U(output_length, output_size);
  6832. PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
  6833. output, output_length,
  6834. label->x, label->len,
  6835. output2, output2_size,
  6836. &output2_length));
  6837. ASSERT_COMPARE(input_data->x, input_data->len,
  6838. output2, output2_length);
  6839. exit:
  6840. /*
  6841. * Key attributes may have been returned by psa_get_key_attributes()
  6842. * thus reset them as required.
  6843. */
  6844. psa_reset_key_attributes(&attributes);
  6845. psa_destroy_key(key);
  6846. mbedtls_free(output);
  6847. mbedtls_free(output2);
  6848. PSA_DONE();
  6849. }
  6850. /* END_CASE */
  6851. /* BEGIN_CASE */
  6852. void asymmetric_decrypt(int key_type_arg,
  6853. data_t *key_data,
  6854. int alg_arg,
  6855. data_t *input_data,
  6856. data_t *label,
  6857. data_t *expected_data)
  6858. {
  6859. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6860. psa_key_type_t key_type = key_type_arg;
  6861. psa_algorithm_t alg = alg_arg;
  6862. size_t key_bits;
  6863. unsigned char *output = NULL;
  6864. size_t output_size = 0;
  6865. size_t output_length = ~0;
  6866. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6867. PSA_ASSERT(psa_crypto_init());
  6868. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
  6869. psa_set_key_algorithm(&attributes, alg);
  6870. psa_set_key_type(&attributes, key_type);
  6871. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6872. &key));
  6873. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  6874. key_bits = psa_get_key_bits(&attributes);
  6875. /* Determine the maximum ciphertext length */
  6876. output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
  6877. TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
  6878. ASSERT_ALLOC(output, output_size);
  6879. PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
  6880. input_data->x, input_data->len,
  6881. label->x, label->len,
  6882. output,
  6883. output_size,
  6884. &output_length));
  6885. ASSERT_COMPARE(expected_data->x, expected_data->len,
  6886. output, output_length);
  6887. /* If the label is empty, the test framework puts a non-null pointer
  6888. * in label->x. Test that a null pointer works as well. */
  6889. if (label->len == 0) {
  6890. output_length = ~0;
  6891. if (output_size != 0) {
  6892. memset(output, 0, output_size);
  6893. }
  6894. PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
  6895. input_data->x, input_data->len,
  6896. NULL, label->len,
  6897. output,
  6898. output_size,
  6899. &output_length));
  6900. ASSERT_COMPARE(expected_data->x, expected_data->len,
  6901. output, output_length);
  6902. }
  6903. exit:
  6904. psa_reset_key_attributes(&attributes);
  6905. psa_destroy_key(key);
  6906. mbedtls_free(output);
  6907. PSA_DONE();
  6908. }
  6909. /* END_CASE */
  6910. /* BEGIN_CASE */
  6911. void asymmetric_decrypt_fail(int key_type_arg,
  6912. data_t *key_data,
  6913. int alg_arg,
  6914. data_t *input_data,
  6915. data_t *label,
  6916. int output_size_arg,
  6917. int expected_status_arg)
  6918. {
  6919. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  6920. psa_key_type_t key_type = key_type_arg;
  6921. psa_algorithm_t alg = alg_arg;
  6922. unsigned char *output = NULL;
  6923. size_t output_size = output_size_arg;
  6924. size_t output_length = ~0;
  6925. psa_status_t actual_status;
  6926. psa_status_t expected_status = expected_status_arg;
  6927. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  6928. ASSERT_ALLOC(output, output_size);
  6929. PSA_ASSERT(psa_crypto_init());
  6930. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
  6931. psa_set_key_algorithm(&attributes, alg);
  6932. psa_set_key_type(&attributes, key_type);
  6933. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  6934. &key));
  6935. actual_status = psa_asymmetric_decrypt(key, alg,
  6936. input_data->x, input_data->len,
  6937. label->x, label->len,
  6938. output, output_size,
  6939. &output_length);
  6940. TEST_EQUAL(actual_status, expected_status);
  6941. TEST_LE_U(output_length, output_size);
  6942. /* If the label is empty, the test framework puts a non-null pointer
  6943. * in label->x. Test that a null pointer works as well. */
  6944. if (label->len == 0) {
  6945. output_length = ~0;
  6946. if (output_size != 0) {
  6947. memset(output, 0, output_size);
  6948. }
  6949. actual_status = psa_asymmetric_decrypt(key, alg,
  6950. input_data->x, input_data->len,
  6951. NULL, label->len,
  6952. output, output_size,
  6953. &output_length);
  6954. TEST_EQUAL(actual_status, expected_status);
  6955. TEST_LE_U(output_length, output_size);
  6956. }
  6957. exit:
  6958. psa_reset_key_attributes(&attributes);
  6959. psa_destroy_key(key);
  6960. mbedtls_free(output);
  6961. PSA_DONE();
  6962. }
  6963. /* END_CASE */
  6964. /* BEGIN_CASE */
  6965. void key_derivation_init()
  6966. {
  6967. /* Test each valid way of initializing the object, except for `= {0}`, as
  6968. * Clang 5 complains when `-Wmissing-field-initializers` is used, even
  6969. * though it's OK by the C standard. We could test for this, but we'd need
  6970. * to suppress the Clang warning for the test. */
  6971. size_t capacity;
  6972. psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
  6973. psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
  6974. psa_key_derivation_operation_t zero;
  6975. memset(&zero, 0, sizeof(zero));
  6976. /* A default operation should not be able to report its capacity. */
  6977. TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
  6978. PSA_ERROR_BAD_STATE);
  6979. TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
  6980. PSA_ERROR_BAD_STATE);
  6981. TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
  6982. PSA_ERROR_BAD_STATE);
  6983. /* A default operation should be abortable without error. */
  6984. PSA_ASSERT(psa_key_derivation_abort(&func));
  6985. PSA_ASSERT(psa_key_derivation_abort(&init));
  6986. PSA_ASSERT(psa_key_derivation_abort(&zero));
  6987. }
  6988. /* END_CASE */
  6989. /* BEGIN_CASE */
  6990. void derive_setup(int alg_arg, int expected_status_arg)
  6991. {
  6992. psa_algorithm_t alg = alg_arg;
  6993. psa_status_t expected_status = expected_status_arg;
  6994. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  6995. PSA_ASSERT(psa_crypto_init());
  6996. TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
  6997. expected_status);
  6998. exit:
  6999. psa_key_derivation_abort(&operation);
  7000. PSA_DONE();
  7001. }
  7002. /* END_CASE */
  7003. /* BEGIN_CASE */
  7004. void derive_set_capacity(int alg_arg, int capacity_arg,
  7005. int expected_status_arg)
  7006. {
  7007. psa_algorithm_t alg = alg_arg;
  7008. size_t capacity = capacity_arg;
  7009. psa_status_t expected_status = expected_status_arg;
  7010. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7011. PSA_ASSERT(psa_crypto_init());
  7012. PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
  7013. TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
  7014. expected_status);
  7015. exit:
  7016. psa_key_derivation_abort(&operation);
  7017. PSA_DONE();
  7018. }
  7019. /* END_CASE */
  7020. /* BEGIN_CASE */
  7021. void derive_input(int alg_arg,
  7022. int step_arg1, int key_type_arg1, data_t *input1,
  7023. int expected_status_arg1,
  7024. int step_arg2, int key_type_arg2, data_t *input2,
  7025. int expected_status_arg2,
  7026. int step_arg3, int key_type_arg3, data_t *input3,
  7027. int expected_status_arg3,
  7028. int output_key_type_arg, int expected_output_status_arg)
  7029. {
  7030. psa_algorithm_t alg = alg_arg;
  7031. psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
  7032. psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
  7033. psa_status_t expected_statuses[] = { expected_status_arg1,
  7034. expected_status_arg2,
  7035. expected_status_arg3 };
  7036. data_t *inputs[] = { input1, input2, input3 };
  7037. mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
  7038. MBEDTLS_SVC_KEY_ID_INIT,
  7039. MBEDTLS_SVC_KEY_ID_INIT };
  7040. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7041. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  7042. size_t i;
  7043. psa_key_type_t output_key_type = output_key_type_arg;
  7044. mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
  7045. psa_status_t expected_output_status = expected_output_status_arg;
  7046. psa_status_t actual_output_status;
  7047. PSA_ASSERT(psa_crypto_init());
  7048. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
  7049. psa_set_key_algorithm(&attributes, alg);
  7050. PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
  7051. for (i = 0; i < ARRAY_LENGTH(steps); i++) {
  7052. mbedtls_test_set_step(i);
  7053. if (steps[i] == 0) {
  7054. /* Skip this step */
  7055. } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
  7056. psa_set_key_type(&attributes, key_types[i]);
  7057. PSA_ASSERT(psa_import_key(&attributes,
  7058. inputs[i]->x, inputs[i]->len,
  7059. &keys[i]));
  7060. if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
  7061. steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
  7062. // When taking a private key as secret input, use key agreement
  7063. // to add the shared secret to the derivation
  7064. TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
  7065. &operation, keys[i]),
  7066. expected_statuses[i]);
  7067. } else {
  7068. TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
  7069. keys[i]),
  7070. expected_statuses[i]);
  7071. }
  7072. } else {
  7073. TEST_EQUAL(psa_key_derivation_input_bytes(
  7074. &operation, steps[i],
  7075. inputs[i]->x, inputs[i]->len),
  7076. expected_statuses[i]);
  7077. }
  7078. }
  7079. if (output_key_type != PSA_KEY_TYPE_NONE) {
  7080. psa_reset_key_attributes(&attributes);
  7081. psa_set_key_type(&attributes, output_key_type);
  7082. psa_set_key_bits(&attributes, 8);
  7083. actual_output_status =
  7084. psa_key_derivation_output_key(&attributes, &operation,
  7085. &output_key);
  7086. } else {
  7087. uint8_t buffer[1];
  7088. actual_output_status =
  7089. psa_key_derivation_output_bytes(&operation,
  7090. buffer, sizeof(buffer));
  7091. }
  7092. TEST_EQUAL(actual_output_status, expected_output_status);
  7093. exit:
  7094. psa_key_derivation_abort(&operation);
  7095. for (i = 0; i < ARRAY_LENGTH(keys); i++) {
  7096. psa_destroy_key(keys[i]);
  7097. }
  7098. psa_destroy_key(output_key);
  7099. PSA_DONE();
  7100. }
  7101. /* END_CASE */
  7102. /* BEGIN_CASE */
  7103. void derive_over_capacity(int alg_arg)
  7104. {
  7105. psa_algorithm_t alg = alg_arg;
  7106. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  7107. size_t key_type = PSA_KEY_TYPE_DERIVE;
  7108. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7109. unsigned char input1[] = "Input 1";
  7110. size_t input1_length = sizeof(input1);
  7111. unsigned char input2[] = "Input 2";
  7112. size_t input2_length = sizeof(input2);
  7113. uint8_t buffer[42];
  7114. size_t capacity = sizeof(buffer);
  7115. const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7116. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7117. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7118. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  7119. PSA_ASSERT(psa_crypto_init());
  7120. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
  7121. psa_set_key_algorithm(&attributes, alg);
  7122. psa_set_key_type(&attributes, key_type);
  7123. PSA_ASSERT(psa_import_key(&attributes,
  7124. key_data, sizeof(key_data),
  7125. &key));
  7126. /* valid key derivation */
  7127. if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
  7128. input1, input1_length,
  7129. input2, input2_length,
  7130. capacity)) {
  7131. goto exit;
  7132. }
  7133. /* state of operation shouldn't allow additional generation */
  7134. TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
  7135. PSA_ERROR_BAD_STATE);
  7136. PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
  7137. TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
  7138. PSA_ERROR_INSUFFICIENT_DATA);
  7139. exit:
  7140. psa_key_derivation_abort(&operation);
  7141. psa_destroy_key(key);
  7142. PSA_DONE();
  7143. }
  7144. /* END_CASE */
  7145. /* BEGIN_CASE */
  7146. void derive_actions_without_setup()
  7147. {
  7148. uint8_t output_buffer[16];
  7149. size_t buffer_size = 16;
  7150. size_t capacity = 0;
  7151. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7152. TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
  7153. output_buffer, buffer_size)
  7154. == PSA_ERROR_BAD_STATE);
  7155. TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
  7156. == PSA_ERROR_BAD_STATE);
  7157. PSA_ASSERT(psa_key_derivation_abort(&operation));
  7158. TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
  7159. output_buffer, buffer_size)
  7160. == PSA_ERROR_BAD_STATE);
  7161. TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
  7162. == PSA_ERROR_BAD_STATE);
  7163. exit:
  7164. psa_key_derivation_abort(&operation);
  7165. }
  7166. /* END_CASE */
  7167. /* BEGIN_CASE */
  7168. void derive_output(int alg_arg,
  7169. int step1_arg, data_t *input1, int expected_status_arg1,
  7170. int step2_arg, data_t *input2, int expected_status_arg2,
  7171. int step3_arg, data_t *input3, int expected_status_arg3,
  7172. int step4_arg, data_t *input4, int expected_status_arg4,
  7173. data_t *key_agreement_peer_key,
  7174. int requested_capacity_arg,
  7175. data_t *expected_output1,
  7176. data_t *expected_output2,
  7177. int other_key_input_type,
  7178. int key_input_type,
  7179. int derive_type)
  7180. {
  7181. psa_algorithm_t alg = alg_arg;
  7182. psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
  7183. data_t *inputs[] = { input1, input2, input3, input4 };
  7184. mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
  7185. MBEDTLS_SVC_KEY_ID_INIT,
  7186. MBEDTLS_SVC_KEY_ID_INIT,
  7187. MBEDTLS_SVC_KEY_ID_INIT };
  7188. psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
  7189. expected_status_arg3, expected_status_arg4 };
  7190. size_t requested_capacity = requested_capacity_arg;
  7191. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7192. uint8_t *expected_outputs[2] =
  7193. { expected_output1->x, expected_output2->x };
  7194. size_t output_sizes[2] =
  7195. { expected_output1->len, expected_output2->len };
  7196. size_t output_buffer_size = 0;
  7197. uint8_t *output_buffer = NULL;
  7198. size_t expected_capacity;
  7199. size_t current_capacity;
  7200. psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
  7201. psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
  7202. psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
  7203. psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
  7204. mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
  7205. psa_status_t status;
  7206. size_t i;
  7207. for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
  7208. if (output_sizes[i] > output_buffer_size) {
  7209. output_buffer_size = output_sizes[i];
  7210. }
  7211. if (output_sizes[i] == 0) {
  7212. expected_outputs[i] = NULL;
  7213. }
  7214. }
  7215. ASSERT_ALLOC(output_buffer, output_buffer_size);
  7216. PSA_ASSERT(psa_crypto_init());
  7217. /* Extraction phase. */
  7218. PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
  7219. PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
  7220. requested_capacity));
  7221. for (i = 0; i < ARRAY_LENGTH(steps); i++) {
  7222. switch (steps[i]) {
  7223. case 0:
  7224. break;
  7225. case PSA_KEY_DERIVATION_INPUT_SECRET:
  7226. switch (key_input_type) {
  7227. case 0: // input bytes
  7228. TEST_EQUAL(psa_key_derivation_input_bytes(
  7229. &operation, steps[i],
  7230. inputs[i]->x, inputs[i]->len),
  7231. statuses[i]);
  7232. if (statuses[i] != PSA_SUCCESS) {
  7233. goto exit;
  7234. }
  7235. break;
  7236. case 1: // input key
  7237. psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
  7238. psa_set_key_algorithm(&attributes1, alg);
  7239. psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
  7240. PSA_ASSERT(psa_import_key(&attributes1,
  7241. inputs[i]->x, inputs[i]->len,
  7242. &keys[i]));
  7243. if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
  7244. PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
  7245. TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
  7246. PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
  7247. }
  7248. PSA_ASSERT(psa_key_derivation_input_key(&operation,
  7249. steps[i],
  7250. keys[i]));
  7251. break;
  7252. default:
  7253. TEST_ASSERT(!"default case not supported");
  7254. break;
  7255. }
  7256. break;
  7257. case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
  7258. switch (other_key_input_type) {
  7259. case 0: // input bytes
  7260. TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
  7261. steps[i],
  7262. inputs[i]->x,
  7263. inputs[i]->len),
  7264. statuses[i]);
  7265. break;
  7266. case 1: // input key, type DERIVE
  7267. case 11: // input key, type RAW
  7268. psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
  7269. psa_set_key_algorithm(&attributes2, alg);
  7270. psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
  7271. // other secret of type RAW_DATA passed with input_key
  7272. if (other_key_input_type == 11) {
  7273. psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
  7274. }
  7275. PSA_ASSERT(psa_import_key(&attributes2,
  7276. inputs[i]->x, inputs[i]->len,
  7277. &keys[i]));
  7278. TEST_EQUAL(psa_key_derivation_input_key(&operation,
  7279. steps[i],
  7280. keys[i]),
  7281. statuses[i]);
  7282. break;
  7283. case 2: // key agreement
  7284. psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
  7285. psa_set_key_algorithm(&attributes3, alg);
  7286. psa_set_key_type(&attributes3,
  7287. PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
  7288. PSA_ASSERT(psa_import_key(&attributes3,
  7289. inputs[i]->x, inputs[i]->len,
  7290. &keys[i]));
  7291. TEST_EQUAL(psa_key_derivation_key_agreement(
  7292. &operation,
  7293. PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
  7294. keys[i], key_agreement_peer_key->x,
  7295. key_agreement_peer_key->len), statuses[i]);
  7296. break;
  7297. default:
  7298. TEST_ASSERT(!"default case not supported");
  7299. break;
  7300. }
  7301. if (statuses[i] != PSA_SUCCESS) {
  7302. goto exit;
  7303. }
  7304. break;
  7305. default:
  7306. TEST_EQUAL(psa_key_derivation_input_bytes(
  7307. &operation, steps[i],
  7308. inputs[i]->x, inputs[i]->len), statuses[i]);
  7309. if (statuses[i] != PSA_SUCCESS) {
  7310. goto exit;
  7311. }
  7312. break;
  7313. }
  7314. }
  7315. PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
  7316. &current_capacity));
  7317. TEST_EQUAL(current_capacity, requested_capacity);
  7318. expected_capacity = requested_capacity;
  7319. if (derive_type == 1) { // output key
  7320. psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
  7321. /* For output key derivation secret must be provided using
  7322. input key, otherwise operation is not permitted. */
  7323. if (key_input_type == 1) {
  7324. expected_status = PSA_SUCCESS;
  7325. }
  7326. psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
  7327. psa_set_key_algorithm(&attributes4, alg);
  7328. psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
  7329. psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
  7330. TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
  7331. &derived_key), expected_status);
  7332. } else { // output bytes
  7333. /* Expansion phase. */
  7334. for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
  7335. /* Read some bytes. */
  7336. status = psa_key_derivation_output_bytes(&operation,
  7337. output_buffer, output_sizes[i]);
  7338. if (expected_capacity == 0 && output_sizes[i] == 0) {
  7339. /* Reading 0 bytes when 0 bytes are available can go either way. */
  7340. TEST_ASSERT(status == PSA_SUCCESS ||
  7341. status == PSA_ERROR_INSUFFICIENT_DATA);
  7342. continue;
  7343. } else if (expected_capacity == 0 ||
  7344. output_sizes[i] > expected_capacity) {
  7345. /* Capacity exceeded. */
  7346. TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
  7347. expected_capacity = 0;
  7348. continue;
  7349. }
  7350. /* Success. Check the read data. */
  7351. PSA_ASSERT(status);
  7352. if (output_sizes[i] != 0) {
  7353. ASSERT_COMPARE(output_buffer, output_sizes[i],
  7354. expected_outputs[i], output_sizes[i]);
  7355. }
  7356. /* Check the operation status. */
  7357. expected_capacity -= output_sizes[i];
  7358. PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
  7359. &current_capacity));
  7360. TEST_EQUAL(expected_capacity, current_capacity);
  7361. }
  7362. }
  7363. PSA_ASSERT(psa_key_derivation_abort(&operation));
  7364. exit:
  7365. mbedtls_free(output_buffer);
  7366. psa_key_derivation_abort(&operation);
  7367. for (i = 0; i < ARRAY_LENGTH(keys); i++) {
  7368. psa_destroy_key(keys[i]);
  7369. }
  7370. psa_destroy_key(derived_key);
  7371. PSA_DONE();
  7372. }
  7373. /* END_CASE */
  7374. /* BEGIN_CASE */
  7375. void derive_full(int alg_arg,
  7376. data_t *key_data,
  7377. data_t *input1,
  7378. data_t *input2,
  7379. int requested_capacity_arg)
  7380. {
  7381. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  7382. psa_algorithm_t alg = alg_arg;
  7383. size_t requested_capacity = requested_capacity_arg;
  7384. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7385. unsigned char output_buffer[16];
  7386. size_t expected_capacity = requested_capacity;
  7387. size_t current_capacity;
  7388. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  7389. PSA_ASSERT(psa_crypto_init());
  7390. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
  7391. psa_set_key_algorithm(&attributes, alg);
  7392. psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
  7393. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  7394. &key));
  7395. if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
  7396. input1->x, input1->len,
  7397. input2->x, input2->len,
  7398. requested_capacity)) {
  7399. goto exit;
  7400. }
  7401. PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
  7402. &current_capacity));
  7403. TEST_EQUAL(current_capacity, expected_capacity);
  7404. /* Expansion phase. */
  7405. while (current_capacity > 0) {
  7406. size_t read_size = sizeof(output_buffer);
  7407. if (read_size > current_capacity) {
  7408. read_size = current_capacity;
  7409. }
  7410. PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
  7411. output_buffer,
  7412. read_size));
  7413. expected_capacity -= read_size;
  7414. PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
  7415. &current_capacity));
  7416. TEST_EQUAL(current_capacity, expected_capacity);
  7417. }
  7418. /* Check that the operation refuses to go over capacity. */
  7419. TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
  7420. PSA_ERROR_INSUFFICIENT_DATA);
  7421. PSA_ASSERT(psa_key_derivation_abort(&operation));
  7422. exit:
  7423. psa_key_derivation_abort(&operation);
  7424. psa_destroy_key(key);
  7425. PSA_DONE();
  7426. }
  7427. /* END_CASE */
  7428. /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
  7429. void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
  7430. int derivation_step,
  7431. int capacity, int expected_capacity_status_arg,
  7432. data_t *expected_output,
  7433. int expected_output_status_arg)
  7434. {
  7435. psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
  7436. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7437. psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
  7438. uint8_t *output_buffer = NULL;
  7439. psa_status_t status;
  7440. psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
  7441. psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
  7442. psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
  7443. ASSERT_ALLOC(output_buffer, expected_output->len);
  7444. PSA_ASSERT(psa_crypto_init());
  7445. PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
  7446. TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
  7447. expected_capacity_status);
  7448. TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
  7449. step, input->x, input->len),
  7450. expected_input_status);
  7451. if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
  7452. goto exit;
  7453. }
  7454. status = psa_key_derivation_output_bytes(&operation, output_buffer,
  7455. expected_output->len);
  7456. TEST_EQUAL(status, expected_output_status);
  7457. if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
  7458. ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
  7459. expected_output->len);
  7460. }
  7461. exit:
  7462. mbedtls_free(output_buffer);
  7463. psa_key_derivation_abort(&operation);
  7464. PSA_DONE();
  7465. }
  7466. /* END_CASE */
  7467. /* BEGIN_CASE */
  7468. void derive_key_exercise(int alg_arg,
  7469. data_t *key_data,
  7470. data_t *input1,
  7471. data_t *input2,
  7472. int derived_type_arg,
  7473. int derived_bits_arg,
  7474. int derived_usage_arg,
  7475. int derived_alg_arg)
  7476. {
  7477. mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
  7478. mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
  7479. psa_algorithm_t alg = alg_arg;
  7480. psa_key_type_t derived_type = derived_type_arg;
  7481. size_t derived_bits = derived_bits_arg;
  7482. psa_key_usage_t derived_usage = derived_usage_arg;
  7483. psa_algorithm_t derived_alg = derived_alg_arg;
  7484. size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
  7485. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7486. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  7487. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  7488. PSA_ASSERT(psa_crypto_init());
  7489. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
  7490. psa_set_key_algorithm(&attributes, alg);
  7491. psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
  7492. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  7493. &base_key));
  7494. /* Derive a key. */
  7495. if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
  7496. input1->x, input1->len,
  7497. input2->x, input2->len,
  7498. capacity)) {
  7499. goto exit;
  7500. }
  7501. psa_set_key_usage_flags(&attributes, derived_usage);
  7502. psa_set_key_algorithm(&attributes, derived_alg);
  7503. psa_set_key_type(&attributes, derived_type);
  7504. psa_set_key_bits(&attributes, derived_bits);
  7505. PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
  7506. &derived_key));
  7507. /* Test the key information */
  7508. PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
  7509. TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
  7510. TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
  7511. /* Exercise the derived key. */
  7512. if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
  7513. goto exit;
  7514. }
  7515. exit:
  7516. /*
  7517. * Key attributes may have been returned by psa_get_key_attributes()
  7518. * thus reset them as required.
  7519. */
  7520. psa_reset_key_attributes(&got_attributes);
  7521. psa_key_derivation_abort(&operation);
  7522. psa_destroy_key(base_key);
  7523. psa_destroy_key(derived_key);
  7524. PSA_DONE();
  7525. }
  7526. /* END_CASE */
  7527. /* BEGIN_CASE */
  7528. void derive_key_export(int alg_arg,
  7529. data_t *key_data,
  7530. data_t *input1,
  7531. data_t *input2,
  7532. int bytes1_arg,
  7533. int bytes2_arg)
  7534. {
  7535. mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
  7536. mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
  7537. psa_algorithm_t alg = alg_arg;
  7538. size_t bytes1 = bytes1_arg;
  7539. size_t bytes2 = bytes2_arg;
  7540. size_t capacity = bytes1 + bytes2;
  7541. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7542. uint8_t *output_buffer = NULL;
  7543. uint8_t *export_buffer = NULL;
  7544. psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
  7545. psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
  7546. size_t length;
  7547. ASSERT_ALLOC(output_buffer, capacity);
  7548. ASSERT_ALLOC(export_buffer, capacity);
  7549. PSA_ASSERT(psa_crypto_init());
  7550. psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
  7551. psa_set_key_algorithm(&base_attributes, alg);
  7552. psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
  7553. PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
  7554. &base_key));
  7555. /* Derive some material and output it. */
  7556. if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
  7557. input1->x, input1->len,
  7558. input2->x, input2->len,
  7559. capacity)) {
  7560. goto exit;
  7561. }
  7562. PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
  7563. output_buffer,
  7564. capacity));
  7565. PSA_ASSERT(psa_key_derivation_abort(&operation));
  7566. /* Derive the same output again, but this time store it in key objects. */
  7567. if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
  7568. input1->x, input1->len,
  7569. input2->x, input2->len,
  7570. capacity)) {
  7571. goto exit;
  7572. }
  7573. psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
  7574. psa_set_key_algorithm(&derived_attributes, 0);
  7575. psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
  7576. psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
  7577. PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
  7578. &derived_key));
  7579. PSA_ASSERT(psa_export_key(derived_key,
  7580. export_buffer, bytes1,
  7581. &length));
  7582. TEST_EQUAL(length, bytes1);
  7583. PSA_ASSERT(psa_destroy_key(derived_key));
  7584. psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
  7585. PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
  7586. &derived_key));
  7587. PSA_ASSERT(psa_export_key(derived_key,
  7588. export_buffer + bytes1, bytes2,
  7589. &length));
  7590. TEST_EQUAL(length, bytes2);
  7591. /* Compare the outputs from the two runs. */
  7592. ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
  7593. export_buffer, capacity);
  7594. exit:
  7595. mbedtls_free(output_buffer);
  7596. mbedtls_free(export_buffer);
  7597. psa_key_derivation_abort(&operation);
  7598. psa_destroy_key(base_key);
  7599. psa_destroy_key(derived_key);
  7600. PSA_DONE();
  7601. }
  7602. /* END_CASE */
  7603. /* BEGIN_CASE */
  7604. void derive_key_type(int alg_arg,
  7605. data_t *key_data,
  7606. data_t *input1,
  7607. data_t *input2,
  7608. int key_type_arg, int bits_arg,
  7609. data_t *expected_export)
  7610. {
  7611. mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
  7612. mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
  7613. const psa_algorithm_t alg = alg_arg;
  7614. const psa_key_type_t key_type = key_type_arg;
  7615. const size_t bits = bits_arg;
  7616. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7617. const size_t export_buffer_size =
  7618. PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
  7619. uint8_t *export_buffer = NULL;
  7620. psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
  7621. psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
  7622. size_t export_length;
  7623. ASSERT_ALLOC(export_buffer, export_buffer_size);
  7624. PSA_ASSERT(psa_crypto_init());
  7625. psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
  7626. psa_set_key_algorithm(&base_attributes, alg);
  7627. psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
  7628. PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
  7629. &base_key));
  7630. if (mbedtls_test_psa_setup_key_derivation_wrap(
  7631. &operation, base_key, alg,
  7632. input1->x, input1->len,
  7633. input2->x, input2->len,
  7634. PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
  7635. goto exit;
  7636. }
  7637. psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
  7638. psa_set_key_algorithm(&derived_attributes, 0);
  7639. psa_set_key_type(&derived_attributes, key_type);
  7640. psa_set_key_bits(&derived_attributes, bits);
  7641. PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
  7642. &derived_key));
  7643. PSA_ASSERT(psa_export_key(derived_key,
  7644. export_buffer, export_buffer_size,
  7645. &export_length));
  7646. ASSERT_COMPARE(export_buffer, export_length,
  7647. expected_export->x, expected_export->len);
  7648. exit:
  7649. mbedtls_free(export_buffer);
  7650. psa_key_derivation_abort(&operation);
  7651. psa_destroy_key(base_key);
  7652. psa_destroy_key(derived_key);
  7653. PSA_DONE();
  7654. }
  7655. /* END_CASE */
  7656. /* BEGIN_CASE */
  7657. void derive_key(int alg_arg,
  7658. data_t *key_data, data_t *input1, data_t *input2,
  7659. int type_arg, int bits_arg,
  7660. int expected_status_arg,
  7661. int is_large_output)
  7662. {
  7663. mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
  7664. mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
  7665. psa_algorithm_t alg = alg_arg;
  7666. psa_key_type_t type = type_arg;
  7667. size_t bits = bits_arg;
  7668. psa_status_t expected_status = expected_status_arg;
  7669. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7670. psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
  7671. psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
  7672. PSA_ASSERT(psa_crypto_init());
  7673. psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
  7674. psa_set_key_algorithm(&base_attributes, alg);
  7675. psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
  7676. PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
  7677. &base_key));
  7678. if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
  7679. input1->x, input1->len,
  7680. input2->x, input2->len,
  7681. SIZE_MAX)) {
  7682. goto exit;
  7683. }
  7684. psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
  7685. psa_set_key_algorithm(&derived_attributes, 0);
  7686. psa_set_key_type(&derived_attributes, type);
  7687. psa_set_key_bits(&derived_attributes, bits);
  7688. psa_status_t status =
  7689. psa_key_derivation_output_key(&derived_attributes,
  7690. &operation,
  7691. &derived_key);
  7692. if (is_large_output > 0) {
  7693. TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
  7694. }
  7695. TEST_EQUAL(status, expected_status);
  7696. exit:
  7697. psa_key_derivation_abort(&operation);
  7698. psa_destroy_key(base_key);
  7699. psa_destroy_key(derived_key);
  7700. PSA_DONE();
  7701. }
  7702. /* END_CASE */
  7703. /* BEGIN_CASE */
  7704. void key_agreement_setup(int alg_arg,
  7705. int our_key_type_arg, int our_key_alg_arg,
  7706. data_t *our_key_data, data_t *peer_key_data,
  7707. int expected_status_arg)
  7708. {
  7709. mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
  7710. psa_algorithm_t alg = alg_arg;
  7711. psa_algorithm_t our_key_alg = our_key_alg_arg;
  7712. psa_key_type_t our_key_type = our_key_type_arg;
  7713. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7714. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  7715. psa_status_t expected_status = expected_status_arg;
  7716. psa_status_t status;
  7717. PSA_ASSERT(psa_crypto_init());
  7718. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
  7719. psa_set_key_algorithm(&attributes, our_key_alg);
  7720. psa_set_key_type(&attributes, our_key_type);
  7721. PSA_ASSERT(psa_import_key(&attributes,
  7722. our_key_data->x, our_key_data->len,
  7723. &our_key));
  7724. /* The tests currently include inputs that should fail at either step.
  7725. * Test cases that fail at the setup step should be changed to call
  7726. * key_derivation_setup instead, and this function should be renamed
  7727. * to key_agreement_fail. */
  7728. status = psa_key_derivation_setup(&operation, alg);
  7729. if (status == PSA_SUCCESS) {
  7730. TEST_EQUAL(psa_key_derivation_key_agreement(
  7731. &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
  7732. our_key,
  7733. peer_key_data->x, peer_key_data->len),
  7734. expected_status);
  7735. } else {
  7736. TEST_ASSERT(status == expected_status);
  7737. }
  7738. exit:
  7739. psa_key_derivation_abort(&operation);
  7740. psa_destroy_key(our_key);
  7741. PSA_DONE();
  7742. }
  7743. /* END_CASE */
  7744. /* BEGIN_CASE */
  7745. void raw_key_agreement(int alg_arg,
  7746. int our_key_type_arg, data_t *our_key_data,
  7747. data_t *peer_key_data,
  7748. data_t *expected_output)
  7749. {
  7750. mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
  7751. psa_algorithm_t alg = alg_arg;
  7752. psa_key_type_t our_key_type = our_key_type_arg;
  7753. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  7754. unsigned char *output = NULL;
  7755. size_t output_length = ~0;
  7756. size_t key_bits;
  7757. PSA_ASSERT(psa_crypto_init());
  7758. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
  7759. psa_set_key_algorithm(&attributes, alg);
  7760. psa_set_key_type(&attributes, our_key_type);
  7761. PSA_ASSERT(psa_import_key(&attributes,
  7762. our_key_data->x, our_key_data->len,
  7763. &our_key));
  7764. PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
  7765. key_bits = psa_get_key_bits(&attributes);
  7766. /* Validate size macros */
  7767. TEST_LE_U(expected_output->len,
  7768. PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
  7769. TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
  7770. PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
  7771. /* Good case with exact output size */
  7772. ASSERT_ALLOC(output, expected_output->len);
  7773. PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
  7774. peer_key_data->x, peer_key_data->len,
  7775. output, expected_output->len,
  7776. &output_length));
  7777. ASSERT_COMPARE(output, output_length,
  7778. expected_output->x, expected_output->len);
  7779. mbedtls_free(output);
  7780. output = NULL;
  7781. output_length = ~0;
  7782. /* Larger buffer */
  7783. ASSERT_ALLOC(output, expected_output->len + 1);
  7784. PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
  7785. peer_key_data->x, peer_key_data->len,
  7786. output, expected_output->len + 1,
  7787. &output_length));
  7788. ASSERT_COMPARE(output, output_length,
  7789. expected_output->x, expected_output->len);
  7790. mbedtls_free(output);
  7791. output = NULL;
  7792. output_length = ~0;
  7793. /* Buffer too small */
  7794. ASSERT_ALLOC(output, expected_output->len - 1);
  7795. TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
  7796. peer_key_data->x, peer_key_data->len,
  7797. output, expected_output->len - 1,
  7798. &output_length),
  7799. PSA_ERROR_BUFFER_TOO_SMALL);
  7800. /* Not required by the spec, but good robustness */
  7801. TEST_LE_U(output_length, expected_output->len - 1);
  7802. mbedtls_free(output);
  7803. output = NULL;
  7804. exit:
  7805. mbedtls_free(output);
  7806. psa_destroy_key(our_key);
  7807. PSA_DONE();
  7808. }
  7809. /* END_CASE */
  7810. /* BEGIN_CASE */
  7811. void key_agreement_capacity(int alg_arg,
  7812. int our_key_type_arg, data_t *our_key_data,
  7813. data_t *peer_key_data,
  7814. int expected_capacity_arg)
  7815. {
  7816. mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
  7817. psa_algorithm_t alg = alg_arg;
  7818. psa_key_type_t our_key_type = our_key_type_arg;
  7819. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7820. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  7821. size_t actual_capacity;
  7822. unsigned char output[16];
  7823. PSA_ASSERT(psa_crypto_init());
  7824. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
  7825. psa_set_key_algorithm(&attributes, alg);
  7826. psa_set_key_type(&attributes, our_key_type);
  7827. PSA_ASSERT(psa_import_key(&attributes,
  7828. our_key_data->x, our_key_data->len,
  7829. &our_key));
  7830. PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
  7831. PSA_ASSERT(psa_key_derivation_key_agreement(
  7832. &operation,
  7833. PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
  7834. peer_key_data->x, peer_key_data->len));
  7835. if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
  7836. /* The test data is for info="" */
  7837. PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
  7838. PSA_KEY_DERIVATION_INPUT_INFO,
  7839. NULL, 0));
  7840. }
  7841. /* Test the advertised capacity. */
  7842. PSA_ASSERT(psa_key_derivation_get_capacity(
  7843. &operation, &actual_capacity));
  7844. TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
  7845. /* Test the actual capacity by reading the output. */
  7846. while (actual_capacity > sizeof(output)) {
  7847. PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
  7848. output, sizeof(output)));
  7849. actual_capacity -= sizeof(output);
  7850. }
  7851. PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
  7852. output, actual_capacity));
  7853. TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
  7854. PSA_ERROR_INSUFFICIENT_DATA);
  7855. exit:
  7856. psa_key_derivation_abort(&operation);
  7857. psa_destroy_key(our_key);
  7858. PSA_DONE();
  7859. }
  7860. /* END_CASE */
  7861. /* BEGIN_CASE */
  7862. void key_agreement_output(int alg_arg,
  7863. int our_key_type_arg, data_t *our_key_data,
  7864. data_t *peer_key_data,
  7865. data_t *expected_output1, data_t *expected_output2)
  7866. {
  7867. mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
  7868. psa_algorithm_t alg = alg_arg;
  7869. psa_key_type_t our_key_type = our_key_type_arg;
  7870. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  7871. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  7872. uint8_t *actual_output = NULL;
  7873. ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
  7874. expected_output2->len));
  7875. PSA_ASSERT(psa_crypto_init());
  7876. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
  7877. psa_set_key_algorithm(&attributes, alg);
  7878. psa_set_key_type(&attributes, our_key_type);
  7879. PSA_ASSERT(psa_import_key(&attributes,
  7880. our_key_data->x, our_key_data->len,
  7881. &our_key));
  7882. PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
  7883. PSA_ASSERT(psa_key_derivation_key_agreement(
  7884. &operation,
  7885. PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
  7886. peer_key_data->x, peer_key_data->len));
  7887. if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
  7888. /* The test data is for info="" */
  7889. PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
  7890. PSA_KEY_DERIVATION_INPUT_INFO,
  7891. NULL, 0));
  7892. }
  7893. PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
  7894. actual_output,
  7895. expected_output1->len));
  7896. ASSERT_COMPARE(actual_output, expected_output1->len,
  7897. expected_output1->x, expected_output1->len);
  7898. if (expected_output2->len != 0) {
  7899. PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
  7900. actual_output,
  7901. expected_output2->len));
  7902. ASSERT_COMPARE(actual_output, expected_output2->len,
  7903. expected_output2->x, expected_output2->len);
  7904. }
  7905. exit:
  7906. psa_key_derivation_abort(&operation);
  7907. psa_destroy_key(our_key);
  7908. PSA_DONE();
  7909. mbedtls_free(actual_output);
  7910. }
  7911. /* END_CASE */
  7912. /* BEGIN_CASE */
  7913. void generate_random(int bytes_arg)
  7914. {
  7915. size_t bytes = bytes_arg;
  7916. unsigned char *output = NULL;
  7917. unsigned char *changed = NULL;
  7918. size_t i;
  7919. unsigned run;
  7920. TEST_ASSERT(bytes_arg >= 0);
  7921. ASSERT_ALLOC(output, bytes);
  7922. ASSERT_ALLOC(changed, bytes);
  7923. PSA_ASSERT(psa_crypto_init());
  7924. /* Run several times, to ensure that every output byte will be
  7925. * nonzero at least once with overwhelming probability
  7926. * (2^(-8*number_of_runs)). */
  7927. for (run = 0; run < 10; run++) {
  7928. if (bytes != 0) {
  7929. memset(output, 0, bytes);
  7930. }
  7931. PSA_ASSERT(psa_generate_random(output, bytes));
  7932. for (i = 0; i < bytes; i++) {
  7933. if (output[i] != 0) {
  7934. ++changed[i];
  7935. }
  7936. }
  7937. }
  7938. /* Check that every byte was changed to nonzero at least once. This
  7939. * validates that psa_generate_random is overwriting every byte of
  7940. * the output buffer. */
  7941. for (i = 0; i < bytes; i++) {
  7942. TEST_ASSERT(changed[i] != 0);
  7943. }
  7944. exit:
  7945. PSA_DONE();
  7946. mbedtls_free(output);
  7947. mbedtls_free(changed);
  7948. }
  7949. /* END_CASE */
  7950. /* BEGIN_CASE */
  7951. void generate_key(int type_arg,
  7952. int bits_arg,
  7953. int usage_arg,
  7954. int alg_arg,
  7955. int expected_status_arg,
  7956. int is_large_key)
  7957. {
  7958. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  7959. psa_key_type_t type = type_arg;
  7960. psa_key_usage_t usage = usage_arg;
  7961. size_t bits = bits_arg;
  7962. psa_algorithm_t alg = alg_arg;
  7963. psa_status_t expected_status = expected_status_arg;
  7964. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  7965. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  7966. PSA_ASSERT(psa_crypto_init());
  7967. psa_set_key_usage_flags(&attributes, usage);
  7968. psa_set_key_algorithm(&attributes, alg);
  7969. psa_set_key_type(&attributes, type);
  7970. psa_set_key_bits(&attributes, bits);
  7971. /* Generate a key */
  7972. psa_status_t status = psa_generate_key(&attributes, &key);
  7973. if (is_large_key > 0) {
  7974. TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
  7975. }
  7976. TEST_EQUAL(status, expected_status);
  7977. if (expected_status != PSA_SUCCESS) {
  7978. goto exit;
  7979. }
  7980. /* Test the key information */
  7981. PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
  7982. TEST_EQUAL(psa_get_key_type(&got_attributes), type);
  7983. TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
  7984. /* Do something with the key according to its type and permitted usage. */
  7985. if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
  7986. goto exit;
  7987. }
  7988. exit:
  7989. /*
  7990. * Key attributes may have been returned by psa_get_key_attributes()
  7991. * thus reset them as required.
  7992. */
  7993. psa_reset_key_attributes(&got_attributes);
  7994. psa_destroy_key(key);
  7995. PSA_DONE();
  7996. }
  7997. /* END_CASE */
  7998. /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
  7999. void generate_key_rsa(int bits_arg,
  8000. data_t *e_arg,
  8001. int expected_status_arg)
  8002. {
  8003. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  8004. psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
  8005. size_t bits = bits_arg;
  8006. psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
  8007. psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
  8008. psa_status_t expected_status = expected_status_arg;
  8009. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  8010. uint8_t *exported = NULL;
  8011. size_t exported_size =
  8012. PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
  8013. size_t exported_length = SIZE_MAX;
  8014. uint8_t *e_read_buffer = NULL;
  8015. int is_default_public_exponent = 0;
  8016. size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
  8017. size_t e_read_length = SIZE_MAX;
  8018. if (e_arg->len == 0 ||
  8019. (e_arg->len == 3 &&
  8020. e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
  8021. is_default_public_exponent = 1;
  8022. e_read_size = 0;
  8023. }
  8024. ASSERT_ALLOC(e_read_buffer, e_read_size);
  8025. ASSERT_ALLOC(exported, exported_size);
  8026. PSA_ASSERT(psa_crypto_init());
  8027. psa_set_key_usage_flags(&attributes, usage);
  8028. psa_set_key_algorithm(&attributes, alg);
  8029. PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
  8030. e_arg->x, e_arg->len));
  8031. psa_set_key_bits(&attributes, bits);
  8032. /* Generate a key */
  8033. TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
  8034. if (expected_status != PSA_SUCCESS) {
  8035. goto exit;
  8036. }
  8037. /* Test the key information */
  8038. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  8039. TEST_EQUAL(psa_get_key_type(&attributes), type);
  8040. TEST_EQUAL(psa_get_key_bits(&attributes), bits);
  8041. PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
  8042. e_read_buffer, e_read_size,
  8043. &e_read_length));
  8044. if (is_default_public_exponent) {
  8045. TEST_EQUAL(e_read_length, 0);
  8046. } else {
  8047. ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
  8048. }
  8049. /* Do something with the key according to its type and permitted usage. */
  8050. if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
  8051. goto exit;
  8052. }
  8053. /* Export the key and check the public exponent. */
  8054. PSA_ASSERT(psa_export_public_key(key,
  8055. exported, exported_size,
  8056. &exported_length));
  8057. {
  8058. uint8_t *p = exported;
  8059. uint8_t *end = exported + exported_length;
  8060. size_t len;
  8061. /* RSAPublicKey ::= SEQUENCE {
  8062. * modulus INTEGER, -- n
  8063. * publicExponent INTEGER } -- e
  8064. */
  8065. TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
  8066. MBEDTLS_ASN1_SEQUENCE |
  8067. MBEDTLS_ASN1_CONSTRUCTED));
  8068. TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
  8069. TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
  8070. MBEDTLS_ASN1_INTEGER));
  8071. if (len >= 1 && p[0] == 0) {
  8072. ++p;
  8073. --len;
  8074. }
  8075. if (e_arg->len == 0) {
  8076. TEST_EQUAL(len, 3);
  8077. TEST_EQUAL(p[0], 1);
  8078. TEST_EQUAL(p[1], 0);
  8079. TEST_EQUAL(p[2], 1);
  8080. } else {
  8081. ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
  8082. }
  8083. }
  8084. exit:
  8085. /*
  8086. * Key attributes may have been returned by psa_get_key_attributes() or
  8087. * set by psa_set_key_domain_parameters() thus reset them as required.
  8088. */
  8089. psa_reset_key_attributes(&attributes);
  8090. psa_destroy_key(key);
  8091. PSA_DONE();
  8092. mbedtls_free(e_read_buffer);
  8093. mbedtls_free(exported);
  8094. }
  8095. /* END_CASE */
  8096. /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
  8097. void persistent_key_load_key_from_storage(data_t *data,
  8098. int type_arg, int bits_arg,
  8099. int usage_flags_arg, int alg_arg,
  8100. int generation_method)
  8101. {
  8102. mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
  8103. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  8104. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  8105. mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
  8106. psa_key_type_t type = type_arg;
  8107. size_t bits = bits_arg;
  8108. psa_key_usage_t usage_flags = usage_flags_arg;
  8109. psa_algorithm_t alg = alg_arg;
  8110. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  8111. unsigned char *first_export = NULL;
  8112. unsigned char *second_export = NULL;
  8113. size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
  8114. size_t first_exported_length;
  8115. size_t second_exported_length;
  8116. if (usage_flags & PSA_KEY_USAGE_EXPORT) {
  8117. ASSERT_ALLOC(first_export, export_size);
  8118. ASSERT_ALLOC(second_export, export_size);
  8119. }
  8120. PSA_ASSERT(psa_crypto_init());
  8121. psa_set_key_id(&attributes, key_id);
  8122. psa_set_key_usage_flags(&attributes, usage_flags);
  8123. psa_set_key_algorithm(&attributes, alg);
  8124. psa_set_key_type(&attributes, type);
  8125. psa_set_key_bits(&attributes, bits);
  8126. switch (generation_method) {
  8127. case IMPORT_KEY:
  8128. /* Import the key */
  8129. PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
  8130. &key));
  8131. break;
  8132. case GENERATE_KEY:
  8133. /* Generate a key */
  8134. PSA_ASSERT(psa_generate_key(&attributes, &key));
  8135. break;
  8136. case DERIVE_KEY:
  8137. #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
  8138. {
  8139. /* Create base key */
  8140. psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
  8141. psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
  8142. psa_set_key_usage_flags(&base_attributes,
  8143. PSA_KEY_USAGE_DERIVE);
  8144. psa_set_key_algorithm(&base_attributes, derive_alg);
  8145. psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
  8146. PSA_ASSERT(psa_import_key(&base_attributes,
  8147. data->x, data->len,
  8148. &base_key));
  8149. /* Derive a key. */
  8150. PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
  8151. PSA_ASSERT(psa_key_derivation_input_key(
  8152. &operation,
  8153. PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
  8154. PSA_ASSERT(psa_key_derivation_input_bytes(
  8155. &operation, PSA_KEY_DERIVATION_INPUT_INFO,
  8156. NULL, 0));
  8157. PSA_ASSERT(psa_key_derivation_output_key(&attributes,
  8158. &operation,
  8159. &key));
  8160. PSA_ASSERT(psa_key_derivation_abort(&operation));
  8161. PSA_ASSERT(psa_destroy_key(base_key));
  8162. base_key = MBEDTLS_SVC_KEY_ID_INIT;
  8163. }
  8164. #else
  8165. TEST_ASSUME(!"KDF not supported in this configuration");
  8166. #endif
  8167. break;
  8168. default:
  8169. TEST_ASSERT(!"generation_method not implemented in test");
  8170. break;
  8171. }
  8172. psa_reset_key_attributes(&attributes);
  8173. /* Export the key if permitted by the key policy. */
  8174. if (usage_flags & PSA_KEY_USAGE_EXPORT) {
  8175. PSA_ASSERT(psa_export_key(key,
  8176. first_export, export_size,
  8177. &first_exported_length));
  8178. if (generation_method == IMPORT_KEY) {
  8179. ASSERT_COMPARE(data->x, data->len,
  8180. first_export, first_exported_length);
  8181. }
  8182. }
  8183. /* Shutdown and restart */
  8184. PSA_ASSERT(psa_purge_key(key));
  8185. PSA_DONE();
  8186. PSA_ASSERT(psa_crypto_init());
  8187. /* Check key slot still contains key data */
  8188. PSA_ASSERT(psa_get_key_attributes(key, &attributes));
  8189. TEST_ASSERT(mbedtls_svc_key_id_equal(
  8190. psa_get_key_id(&attributes), key_id));
  8191. TEST_EQUAL(psa_get_key_lifetime(&attributes),
  8192. PSA_KEY_LIFETIME_PERSISTENT);
  8193. TEST_EQUAL(psa_get_key_type(&attributes), type);
  8194. TEST_EQUAL(psa_get_key_bits(&attributes), bits);
  8195. TEST_EQUAL(psa_get_key_usage_flags(&attributes),
  8196. mbedtls_test_update_key_usage_flags(usage_flags));
  8197. TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
  8198. /* Export the key again if permitted by the key policy. */
  8199. if (usage_flags & PSA_KEY_USAGE_EXPORT) {
  8200. PSA_ASSERT(psa_export_key(key,
  8201. second_export, export_size,
  8202. &second_exported_length));
  8203. ASSERT_COMPARE(first_export, first_exported_length,
  8204. second_export, second_exported_length);
  8205. }
  8206. /* Do something with the key according to its type and permitted usage. */
  8207. if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
  8208. goto exit;
  8209. }
  8210. exit:
  8211. /*
  8212. * Key attributes may have been returned by psa_get_key_attributes()
  8213. * thus reset them as required.
  8214. */
  8215. psa_reset_key_attributes(&attributes);
  8216. mbedtls_free(first_export);
  8217. mbedtls_free(second_export);
  8218. psa_key_derivation_abort(&operation);
  8219. psa_destroy_key(base_key);
  8220. psa_destroy_key(key);
  8221. PSA_DONE();
  8222. }
  8223. /* END_CASE */
  8224. /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
  8225. void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
  8226. int primitive_arg, int hash_arg, int role_arg,
  8227. int test_input, data_t *pw_data,
  8228. int inj_err_type_arg,
  8229. int expected_error_arg)
  8230. {
  8231. psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
  8232. psa_pake_operation_t operation = psa_pake_operation_init();
  8233. psa_algorithm_t alg = alg_arg;
  8234. psa_pake_primitive_t primitive = primitive_arg;
  8235. psa_key_type_t key_type_pw = key_type_pw_arg;
  8236. psa_key_usage_t key_usage_pw = key_usage_pw_arg;
  8237. psa_algorithm_t hash_alg = hash_arg;
  8238. psa_pake_role_t role = role_arg;
  8239. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  8240. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  8241. ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
  8242. psa_status_t expected_error = expected_error_arg;
  8243. psa_status_t status;
  8244. unsigned char *output_buffer = NULL;
  8245. size_t output_len = 0;
  8246. PSA_INIT();
  8247. size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
  8248. PSA_PAKE_STEP_KEY_SHARE);
  8249. ASSERT_ALLOC(output_buffer, buf_size);
  8250. if (pw_data->len > 0) {
  8251. psa_set_key_usage_flags(&attributes, key_usage_pw);
  8252. psa_set_key_algorithm(&attributes, alg);
  8253. psa_set_key_type(&attributes, key_type_pw);
  8254. PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
  8255. &key));
  8256. }
  8257. psa_pake_cs_set_algorithm(&cipher_suite, alg);
  8258. psa_pake_cs_set_primitive(&cipher_suite, primitive);
  8259. psa_pake_cs_set_hash(&cipher_suite, hash_alg);
  8260. PSA_ASSERT(psa_pake_abort(&operation));
  8261. if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
  8262. TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
  8263. expected_error);
  8264. PSA_ASSERT(psa_pake_abort(&operation));
  8265. TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
  8266. expected_error);
  8267. PSA_ASSERT(psa_pake_abort(&operation));
  8268. TEST_EQUAL(psa_pake_set_password_key(&operation, key),
  8269. expected_error);
  8270. PSA_ASSERT(psa_pake_abort(&operation));
  8271. TEST_EQUAL(psa_pake_set_role(&operation, role),
  8272. expected_error);
  8273. PSA_ASSERT(psa_pake_abort(&operation));
  8274. TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
  8275. NULL, 0, NULL),
  8276. expected_error);
  8277. PSA_ASSERT(psa_pake_abort(&operation));
  8278. TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
  8279. expected_error);
  8280. PSA_ASSERT(psa_pake_abort(&operation));
  8281. goto exit;
  8282. }
  8283. status = psa_pake_setup(&operation, &cipher_suite);
  8284. if (status != PSA_SUCCESS) {
  8285. TEST_EQUAL(status, expected_error);
  8286. goto exit;
  8287. }
  8288. if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
  8289. TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
  8290. expected_error);
  8291. goto exit;
  8292. }
  8293. status = psa_pake_set_role(&operation, role);
  8294. if (status != PSA_SUCCESS) {
  8295. TEST_EQUAL(status, expected_error);
  8296. goto exit;
  8297. }
  8298. if (pw_data->len > 0) {
  8299. status = psa_pake_set_password_key(&operation, key);
  8300. if (status != PSA_SUCCESS) {
  8301. TEST_EQUAL(status, expected_error);
  8302. goto exit;
  8303. }
  8304. }
  8305. if (inj_err_type == INJECT_ERR_INVALID_USER) {
  8306. TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
  8307. PSA_ERROR_INVALID_ARGUMENT);
  8308. goto exit;
  8309. }
  8310. if (inj_err_type == INJECT_ERR_INVALID_PEER) {
  8311. TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
  8312. PSA_ERROR_INVALID_ARGUMENT);
  8313. goto exit;
  8314. }
  8315. if (inj_err_type == INJECT_ERR_SET_USER) {
  8316. const uint8_t unsupported_id[] = "abcd";
  8317. TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
  8318. PSA_ERROR_NOT_SUPPORTED);
  8319. goto exit;
  8320. }
  8321. if (inj_err_type == INJECT_ERR_SET_PEER) {
  8322. const uint8_t unsupported_id[] = "abcd";
  8323. TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
  8324. PSA_ERROR_NOT_SUPPORTED);
  8325. goto exit;
  8326. }
  8327. const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
  8328. PSA_PAKE_STEP_KEY_SHARE);
  8329. const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
  8330. PSA_PAKE_STEP_ZK_PUBLIC);
  8331. const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
  8332. PSA_PAKE_STEP_ZK_PROOF);
  8333. if (test_input) {
  8334. if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
  8335. TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
  8336. PSA_ERROR_INVALID_ARGUMENT);
  8337. goto exit;
  8338. }
  8339. if (inj_err_type == INJECT_UNKNOWN_STEP) {
  8340. TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
  8341. output_buffer, size_zk_proof),
  8342. PSA_ERROR_INVALID_ARGUMENT);
  8343. goto exit;
  8344. }
  8345. if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
  8346. TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
  8347. output_buffer, size_zk_proof),
  8348. PSA_ERROR_BAD_STATE);
  8349. goto exit;
  8350. }
  8351. status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
  8352. output_buffer, size_key_share);
  8353. if (status != PSA_SUCCESS) {
  8354. TEST_EQUAL(status, expected_error);
  8355. goto exit;
  8356. }
  8357. if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
  8358. TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
  8359. output_buffer, size_zk_public + 1),
  8360. PSA_ERROR_INVALID_ARGUMENT);
  8361. goto exit;
  8362. }
  8363. if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
  8364. // Just trigger any kind of error. We don't care about the result here
  8365. psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
  8366. output_buffer, size_zk_public + 1);
  8367. TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
  8368. output_buffer, size_zk_public),
  8369. PSA_ERROR_BAD_STATE);
  8370. goto exit;
  8371. }
  8372. } else {
  8373. if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
  8374. TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
  8375. NULL, 0, NULL),
  8376. PSA_ERROR_INVALID_ARGUMENT);
  8377. goto exit;
  8378. }
  8379. if (inj_err_type == INJECT_UNKNOWN_STEP) {
  8380. TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
  8381. output_buffer, buf_size, &output_len),
  8382. PSA_ERROR_INVALID_ARGUMENT);
  8383. goto exit;
  8384. }
  8385. if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
  8386. TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
  8387. output_buffer, buf_size, &output_len),
  8388. PSA_ERROR_BAD_STATE);
  8389. goto exit;
  8390. }
  8391. status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
  8392. output_buffer, buf_size, &output_len);
  8393. if (status != PSA_SUCCESS) {
  8394. TEST_EQUAL(status, expected_error);
  8395. goto exit;
  8396. }
  8397. TEST_ASSERT(output_len > 0);
  8398. if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
  8399. TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
  8400. output_buffer, size_zk_public - 1, &output_len),
  8401. PSA_ERROR_BUFFER_TOO_SMALL);
  8402. goto exit;
  8403. }
  8404. if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
  8405. // Just trigger any kind of error. We don't care about the result here
  8406. psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
  8407. output_buffer, size_zk_public - 1, &output_len);
  8408. TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
  8409. output_buffer, buf_size, &output_len),
  8410. PSA_ERROR_BAD_STATE);
  8411. goto exit;
  8412. }
  8413. }
  8414. exit:
  8415. PSA_ASSERT(psa_destroy_key(key));
  8416. PSA_ASSERT(psa_pake_abort(&operation));
  8417. mbedtls_free(output_buffer);
  8418. PSA_DONE();
  8419. }
  8420. /* END_CASE */
  8421. /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
  8422. void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
  8423. int client_input_first, int inject_error,
  8424. data_t *pw_data)
  8425. {
  8426. psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
  8427. psa_pake_operation_t server = psa_pake_operation_init();
  8428. psa_pake_operation_t client = psa_pake_operation_init();
  8429. psa_algorithm_t alg = alg_arg;
  8430. psa_algorithm_t hash_alg = hash_arg;
  8431. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  8432. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  8433. PSA_INIT();
  8434. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
  8435. psa_set_key_algorithm(&attributes, alg);
  8436. psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
  8437. PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
  8438. &key));
  8439. psa_pake_cs_set_algorithm(&cipher_suite, alg);
  8440. psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
  8441. psa_pake_cs_set_hash(&cipher_suite, hash_alg);
  8442. PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
  8443. PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
  8444. PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
  8445. PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
  8446. PSA_ASSERT(psa_pake_set_password_key(&server, key));
  8447. PSA_ASSERT(psa_pake_set_password_key(&client, key));
  8448. ecjpake_do_round(alg, primitive_arg, &server, &client,
  8449. client_input_first, 1, inject_error);
  8450. if (inject_error == 1 || inject_error == 2) {
  8451. goto exit;
  8452. }
  8453. ecjpake_do_round(alg, primitive_arg, &server, &client,
  8454. client_input_first, 2, inject_error);
  8455. exit:
  8456. psa_destroy_key(key);
  8457. psa_pake_abort(&server);
  8458. psa_pake_abort(&client);
  8459. PSA_DONE();
  8460. }
  8461. /* END_CASE */
  8462. /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
  8463. void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
  8464. int derive_alg_arg, data_t *pw_data,
  8465. int client_input_first, int inj_err_type_arg)
  8466. {
  8467. psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
  8468. psa_pake_operation_t server = psa_pake_operation_init();
  8469. psa_pake_operation_t client = psa_pake_operation_init();
  8470. psa_algorithm_t alg = alg_arg;
  8471. psa_algorithm_t hash_alg = hash_arg;
  8472. psa_algorithm_t derive_alg = derive_alg_arg;
  8473. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  8474. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  8475. psa_key_derivation_operation_t server_derive =
  8476. PSA_KEY_DERIVATION_OPERATION_INIT;
  8477. psa_key_derivation_operation_t client_derive =
  8478. PSA_KEY_DERIVATION_OPERATION_INIT;
  8479. ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
  8480. PSA_INIT();
  8481. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
  8482. psa_set_key_algorithm(&attributes, alg);
  8483. psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
  8484. PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
  8485. &key));
  8486. psa_pake_cs_set_algorithm(&cipher_suite, alg);
  8487. psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
  8488. psa_pake_cs_set_hash(&cipher_suite, hash_alg);
  8489. /* Get shared key */
  8490. PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
  8491. PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
  8492. if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
  8493. PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
  8494. PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
  8495. PSA_KEY_DERIVATION_INPUT_SEED,
  8496. (const uint8_t *) "", 0));
  8497. PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
  8498. PSA_KEY_DERIVATION_INPUT_SEED,
  8499. (const uint8_t *) "", 0));
  8500. }
  8501. PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
  8502. PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
  8503. PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
  8504. PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
  8505. PSA_ASSERT(psa_pake_set_password_key(&server, key));
  8506. PSA_ASSERT(psa_pake_set_password_key(&client, key));
  8507. if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
  8508. TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
  8509. PSA_ERROR_BAD_STATE);
  8510. TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
  8511. PSA_ERROR_BAD_STATE);
  8512. goto exit;
  8513. }
  8514. /* First round */
  8515. ecjpake_do_round(alg, primitive_arg, &server, &client,
  8516. client_input_first, 1, 0);
  8517. if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
  8518. TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
  8519. PSA_ERROR_BAD_STATE);
  8520. TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
  8521. PSA_ERROR_BAD_STATE);
  8522. goto exit;
  8523. }
  8524. /* Second round */
  8525. ecjpake_do_round(alg, primitive_arg, &server, &client,
  8526. client_input_first, 2, 0);
  8527. PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
  8528. PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
  8529. exit:
  8530. psa_key_derivation_abort(&server_derive);
  8531. psa_key_derivation_abort(&client_derive);
  8532. psa_destroy_key(key);
  8533. psa_pake_abort(&server);
  8534. psa_pake_abort(&client);
  8535. PSA_DONE();
  8536. }
  8537. /* END_CASE */
  8538. /* BEGIN_CASE */
  8539. void ecjpake_size_macros()
  8540. {
  8541. const psa_algorithm_t alg = PSA_ALG_JPAKE;
  8542. const size_t bits = 256;
  8543. const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
  8544. PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
  8545. const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
  8546. PSA_ECC_FAMILY_SECP_R1);
  8547. // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
  8548. /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
  8549. TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
  8550. PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
  8551. TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
  8552. PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
  8553. /* The output for ZK_PROOF is the same bitsize as the curve */
  8554. TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
  8555. PSA_BITS_TO_BYTES(bits));
  8556. /* Input sizes are the same as output sizes */
  8557. TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
  8558. PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
  8559. TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
  8560. PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
  8561. TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
  8562. PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
  8563. /* These inequalities will always hold even when other PAKEs are added */
  8564. TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
  8565. PSA_PAKE_OUTPUT_MAX_SIZE);
  8566. TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
  8567. PSA_PAKE_OUTPUT_MAX_SIZE);
  8568. TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
  8569. PSA_PAKE_OUTPUT_MAX_SIZE);
  8570. TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
  8571. PSA_PAKE_INPUT_MAX_SIZE);
  8572. TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
  8573. PSA_PAKE_INPUT_MAX_SIZE);
  8574. TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
  8575. PSA_PAKE_INPUT_MAX_SIZE);
  8576. }
  8577. /* END_CASE */