| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228 |
- /* BEGIN_HEADER */
- #include <stdint.h>
- #include "mbedtls/asn1.h"
- #include "mbedtls/asn1write.h"
- #include "mbedtls/oid.h"
- #include "common.h"
- /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
- * uses mbedtls_ctr_drbg internally. */
- #include "mbedtls/ctr_drbg.h"
- #include "psa/crypto.h"
- #include "psa_crypto_slot_management.h"
- /* For psa_can_do_hash() */
- #include "psa_crypto_core.h"
- #include "test/asn1_helpers.h"
- #include "test/psa_crypto_helpers.h"
- #include "test/psa_exercise_key.h"
- #if defined(PSA_CRYPTO_DRIVER_TEST)
- #include "test/drivers/test_driver.h"
- #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
- #else
- #define TEST_DRIVER_LOCATION 0x7fffff
- #endif
- #include "mbedtls/legacy_or_psa.h"
- /* If this comes up, it's a bug in the test code or in the test data. */
- #define UNUSED 0xdeadbeef
- /* Assert that an operation is (not) active.
- * This serves as a proxy for checking if the operation is aborted. */
- #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
- #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
- #if defined(PSA_WANT_ALG_JPAKE)
- int ecjpake_operation_setup(psa_pake_operation_t *operation,
- psa_pake_cipher_suite_t *cipher_suite,
- psa_pake_role_t role,
- mbedtls_svc_key_id_t key,
- size_t key_available)
- {
- PSA_ASSERT(psa_pake_abort(operation));
- PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
- PSA_ASSERT(psa_pake_set_role(operation, role));
- if (key_available) {
- PSA_ASSERT(psa_pake_set_password_key(operation, key));
- }
- return 0;
- exit:
- return 1;
- }
- #endif
- /** An invalid export length that will never be set by psa_export_key(). */
- static const size_t INVALID_EXPORT_LENGTH = ~0U;
- /** Test if a buffer contains a constant byte value.
- *
- * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
- *
- * \param buffer Pointer to the beginning of the buffer.
- * \param c Expected value of every byte.
- * \param size Size of the buffer in bytes.
- *
- * \return 1 if the buffer is all-bits-zero.
- * \return 0 if there is at least one nonzero byte.
- */
- static int mem_is_char(void *buffer, unsigned char c, size_t size)
- {
- size_t i;
- for (i = 0; i < size; i++) {
- if (((unsigned char *) buffer)[i] != c) {
- return 0;
- }
- }
- return 1;
- }
- #if defined(MBEDTLS_ASN1_WRITE_C)
- /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
- static int asn1_write_10x(unsigned char **p,
- unsigned char *start,
- size_t bits,
- unsigned char x)
- {
- int ret;
- int len = bits / 8 + 1;
- if (bits == 0) {
- return MBEDTLS_ERR_ASN1_INVALID_DATA;
- }
- if (bits <= 8 && x >= 1 << (bits - 1)) {
- return MBEDTLS_ERR_ASN1_INVALID_DATA;
- }
- if (*p < start || *p - start < (ptrdiff_t) len) {
- return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
- }
- *p -= len;
- (*p)[len-1] = x;
- if (bits % 8 == 0) {
- (*p)[1] |= 1;
- } else {
- (*p)[0] |= 1 << (bits % 8);
- }
- MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
- MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
- MBEDTLS_ASN1_INTEGER));
- return len;
- }
- static int construct_fake_rsa_key(unsigned char *buffer,
- size_t buffer_size,
- unsigned char **p,
- size_t bits,
- int keypair)
- {
- size_t half_bits = (bits + 1) / 2;
- int ret;
- int len = 0;
- /* Construct something that looks like a DER encoding of
- * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
- * RSAPrivateKey ::= SEQUENCE {
- * version Version,
- * modulus INTEGER, -- n
- * publicExponent INTEGER, -- e
- * privateExponent INTEGER, -- d
- * prime1 INTEGER, -- p
- * prime2 INTEGER, -- q
- * exponent1 INTEGER, -- d mod (p-1)
- * exponent2 INTEGER, -- d mod (q-1)
- * coefficient INTEGER, -- (inverse of q) mod p
- * otherPrimeInfos OtherPrimeInfos OPTIONAL
- * }
- * Or, for a public key, the same structure with only
- * version, modulus and publicExponent.
- */
- *p = buffer + buffer_size;
- if (keypair) {
- MBEDTLS_ASN1_CHK_ADD(len, /* pq */
- asn1_write_10x(p, buffer, half_bits, 1));
- MBEDTLS_ASN1_CHK_ADD(len, /* dq */
- asn1_write_10x(p, buffer, half_bits, 1));
- MBEDTLS_ASN1_CHK_ADD(len, /* dp */
- asn1_write_10x(p, buffer, half_bits, 1));
- MBEDTLS_ASN1_CHK_ADD(len, /* q */
- asn1_write_10x(p, buffer, half_bits, 1));
- MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
- asn1_write_10x(p, buffer, half_bits, 3));
- MBEDTLS_ASN1_CHK_ADD(len, /* d */
- asn1_write_10x(p, buffer, bits, 1));
- }
- MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
- asn1_write_10x(p, buffer, 17, 1));
- MBEDTLS_ASN1_CHK_ADD(len, /* n */
- asn1_write_10x(p, buffer, bits, 1));
- if (keypair) {
- MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
- mbedtls_asn1_write_int(p, buffer, 0));
- }
- MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
- {
- const unsigned char tag =
- MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
- MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
- }
- return len;
- }
- #endif /* MBEDTLS_ASN1_WRITE_C */
- int exercise_mac_setup(psa_key_type_t key_type,
- const unsigned char *key_bytes,
- size_t key_length,
- psa_algorithm_t alg,
- psa_mac_operation_t *operation,
- psa_status_t *status)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
- *status = psa_mac_sign_setup(operation, key, alg);
- /* Whether setup succeeded or failed, abort must succeed. */
- PSA_ASSERT(psa_mac_abort(operation));
- /* If setup failed, reproduce the failure, so that the caller can
- * test the resulting state of the operation object. */
- if (*status != PSA_SUCCESS) {
- TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
- }
- psa_destroy_key(key);
- return 1;
- exit:
- psa_destroy_key(key);
- return 0;
- }
- int exercise_cipher_setup(psa_key_type_t key_type,
- const unsigned char *key_bytes,
- size_t key_length,
- psa_algorithm_t alg,
- psa_cipher_operation_t *operation,
- psa_status_t *status)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
- *status = psa_cipher_encrypt_setup(operation, key, alg);
- /* Whether setup succeeded or failed, abort must succeed. */
- PSA_ASSERT(psa_cipher_abort(operation));
- /* If setup failed, reproduce the failure, so that the caller can
- * test the resulting state of the operation object. */
- if (*status != PSA_SUCCESS) {
- TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
- *status);
- }
- psa_destroy_key(key);
- return 1;
- exit:
- psa_destroy_key(key);
- return 0;
- }
- static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
- {
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
- uint8_t buffer[1];
- size_t length;
- int ok = 0;
- psa_set_key_id(&attributes, key_id);
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
- psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
- TEST_EQUAL(psa_get_key_attributes(key, &attributes),
- PSA_ERROR_INVALID_HANDLE);
- TEST_EQUAL(
- MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
- TEST_EQUAL(
- MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
- TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
- TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
- TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
- TEST_EQUAL(psa_get_key_type(&attributes), 0);
- TEST_EQUAL(psa_get_key_bits(&attributes), 0);
- TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
- PSA_ERROR_INVALID_HANDLE);
- TEST_EQUAL(psa_export_public_key(key,
- buffer, sizeof(buffer), &length),
- PSA_ERROR_INVALID_HANDLE);
- ok = 1;
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- return ok;
- }
- /* Assert that a key isn't reported as having a slot number. */
- #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
- #define ASSERT_NO_SLOT_NUMBER(attributes) \
- do \
- { \
- psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
- TEST_EQUAL(psa_get_key_slot_number( \
- attributes, \
- &ASSERT_NO_SLOT_NUMBER_slot_number), \
- PSA_ERROR_INVALID_ARGUMENT); \
- } \
- while (0)
- #else /* MBEDTLS_PSA_CRYPTO_SE_C */
- #define ASSERT_NO_SLOT_NUMBER(attributes) \
- ((void) 0)
- #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
- /* An overapproximation of the amount of storage needed for a key of the
- * given type and with the given content. The API doesn't make it easy
- * to find a good value for the size. The current implementation doesn't
- * care about the value anyway. */
- #define KEY_BITS_FROM_DATA(type, data) \
- (data)->len
- typedef enum {
- IMPORT_KEY = 0,
- GENERATE_KEY = 1,
- DERIVE_KEY = 2
- } generate_method;
- typedef enum {
- DO_NOT_SET_LENGTHS = 0,
- SET_LENGTHS_BEFORE_NONCE = 1,
- SET_LENGTHS_AFTER_NONCE = 2
- } set_lengths_method_t;
- typedef enum {
- USE_NULL_TAG = 0,
- USE_GIVEN_TAG = 1,
- } tag_usage_method_t;
- /*!
- * \brief Internal Function for AEAD multipart tests.
- * \param key_type_arg Type of key passed in
- * \param key_data The encryption / decryption key data
- * \param alg_arg The type of algorithm used
- * \param nonce Nonce data
- * \param additional_data Additional data
- * \param ad_part_len_arg If not -1, the length of chunks to
- * feed additional data in to be encrypted /
- * decrypted. If -1, no chunking.
- * \param input_data Data to encrypt / decrypt
- * \param data_part_len_arg If not -1, the length of chunks to feed
- * the data in to be encrypted / decrypted. If
- * -1, no chunking
- * \param set_lengths_method A member of the set_lengths_method_t enum is
- * expected here, this controls whether or not
- * to set lengths, and in what order with
- * respect to set nonce.
- * \param expected_output Expected output
- * \param is_encrypt If non-zero this is an encryption operation.
- * \param do_zero_parts If non-zero, interleave zero length chunks
- * with normal length chunks.
- * \return int Zero on failure, non-zero on success.
- */
- static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
- int alg_arg,
- data_t *nonce,
- data_t *additional_data,
- int ad_part_len_arg,
- data_t *input_data,
- int data_part_len_arg,
- set_lengths_method_t set_lengths_method,
- data_t *expected_output,
- int is_encrypt,
- int do_zero_parts)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
- unsigned char *output_data = NULL;
- unsigned char *part_data = NULL;
- unsigned char *final_data = NULL;
- size_t data_true_size = 0;
- size_t part_data_size = 0;
- size_t output_size = 0;
- size_t final_output_size = 0;
- size_t output_length = 0;
- size_t key_bits = 0;
- size_t tag_length = 0;
- size_t part_offset = 0;
- size_t part_length = 0;
- size_t output_part_length = 0;
- size_t tag_size = 0;
- size_t ad_part_len = 0;
- size_t data_part_len = 0;
- uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- int test_ok = 0;
- size_t part_count = 0;
- PSA_ASSERT(psa_crypto_init());
- if (is_encrypt) {
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- } else {
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
- }
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
- if (is_encrypt) {
- /* Tag gets written at end of buffer. */
- output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
- (input_data->len +
- tag_length));
- data_true_size = input_data->len;
- } else {
- output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
- (input_data->len -
- tag_length));
- /* Do not want to attempt to decrypt tag. */
- data_true_size = input_data->len - tag_length;
- }
- ASSERT_ALLOC(output_data, output_size);
- if (is_encrypt) {
- final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
- TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
- } else {
- final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
- TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
- }
- ASSERT_ALLOC(final_data, final_output_size);
- if (is_encrypt) {
- status = psa_aead_encrypt_setup(&operation, key, alg);
- } else {
- status = psa_aead_decrypt_setup(&operation, key, alg);
- }
- /* If the operation is not supported, just skip and not fail in case the
- * encryption involves a common limitation of cryptography hardwares and
- * an alternative implementation. */
- if (status == PSA_ERROR_NOT_SUPPORTED) {
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
- }
- PSA_ASSERT(status);
- if (set_lengths_method == DO_NOT_SET_LENGTHS) {
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- data_true_size));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- data_true_size));
- }
- if (ad_part_len_arg != -1) {
- /* Pass additional data in parts */
- ad_part_len = (size_t) ad_part_len_arg;
- for (part_offset = 0, part_count = 0;
- part_offset < additional_data->len;
- part_offset += part_length, part_count++) {
- if (do_zero_parts && (part_count & 0x01)) {
- part_length = 0;
- } else if (additional_data->len - part_offset < ad_part_len) {
- part_length = additional_data->len - part_offset;
- } else {
- part_length = ad_part_len;
- }
- PSA_ASSERT(psa_aead_update_ad(&operation,
- additional_data->x + part_offset,
- part_length));
- }
- } else {
- /* Pass additional data in one go. */
- PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len));
- }
- if (data_part_len_arg != -1) {
- /* Pass data in parts */
- data_part_len = (size_t) data_part_len_arg;
- part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
- (size_t) data_part_len);
- ASSERT_ALLOC(part_data, part_data_size);
- for (part_offset = 0, part_count = 0;
- part_offset < data_true_size;
- part_offset += part_length, part_count++) {
- if (do_zero_parts && (part_count & 0x01)) {
- part_length = 0;
- } else if ((data_true_size - part_offset) < data_part_len) {
- part_length = (data_true_size - part_offset);
- } else {
- part_length = data_part_len;
- }
- PSA_ASSERT(psa_aead_update(&operation,
- (input_data->x + part_offset),
- part_length, part_data,
- part_data_size,
- &output_part_length));
- if (output_data && output_part_length) {
- memcpy((output_data + output_length), part_data,
- output_part_length);
- }
- output_length += output_part_length;
- }
- } else {
- /* Pass all data in one go. */
- PSA_ASSERT(psa_aead_update(&operation, input_data->x,
- data_true_size, output_data,
- output_size, &output_length));
- }
- if (is_encrypt) {
- PSA_ASSERT(psa_aead_finish(&operation, final_data,
- final_output_size,
- &output_part_length,
- tag_buffer, tag_length,
- &tag_size));
- } else {
- PSA_ASSERT(psa_aead_verify(&operation, final_data,
- final_output_size,
- &output_part_length,
- (input_data->x + data_true_size),
- tag_length));
- }
- if (output_data && output_part_length) {
- memcpy((output_data + output_length), final_data,
- output_part_length);
- }
- output_length += output_part_length;
- /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
- * should be exact.*/
- if (is_encrypt) {
- TEST_EQUAL(tag_length, tag_size);
- if (output_data && tag_length) {
- memcpy((output_data + output_length), tag_buffer,
- tag_length);
- }
- output_length += tag_length;
- TEST_EQUAL(output_length,
- PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
- input_data->len));
- TEST_LE_U(output_length,
- PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
- } else {
- TEST_EQUAL(output_length,
- PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
- input_data->len));
- TEST_LE_U(output_length,
- PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
- }
- ASSERT_COMPARE(expected_output->x, expected_output->len,
- output_data, output_length);
- test_ok = 1;
- exit:
- psa_destroy_key(key);
- psa_aead_abort(&operation);
- mbedtls_free(output_data);
- mbedtls_free(part_data);
- mbedtls_free(final_data);
- PSA_DONE();
- return test_ok;
- }
- /*!
- * \brief Internal Function for MAC multipart tests.
- * \param key_type_arg Type of key passed in
- * \param key_data The encryption / decryption key data
- * \param alg_arg The type of algorithm used
- * \param input_data Data to encrypt / decrypt
- * \param data_part_len_arg If not -1, the length of chunks to feed
- * the data in to be encrypted / decrypted. If
- * -1, no chunking
- * \param expected_output Expected output
- * \param is_verify If non-zero this is a verify operation.
- * \param do_zero_parts If non-zero, interleave zero length chunks
- * with normal length chunks.
- * \return int Zero on failure, non-zero on success.
- */
- static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
- int alg_arg,
- data_t *input_data,
- int data_part_len_arg,
- data_t *expected_output,
- int is_verify,
- int do_zero_parts)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
- unsigned char mac[PSA_MAC_MAX_SIZE];
- size_t part_offset = 0;
- size_t part_length = 0;
- size_t data_part_len = 0;
- size_t mac_len = 0;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- int test_ok = 0;
- size_t part_count = 0;
- PSA_INIT();
- if (is_verify) {
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
- } else {
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
- }
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- if (is_verify) {
- status = psa_mac_verify_setup(&operation, key, alg);
- } else {
- status = psa_mac_sign_setup(&operation, key, alg);
- }
- PSA_ASSERT(status);
- if (data_part_len_arg != -1) {
- /* Pass data in parts */
- data_part_len = (size_t) data_part_len_arg;
- for (part_offset = 0, part_count = 0;
- part_offset < input_data->len;
- part_offset += part_length, part_count++) {
- if (do_zero_parts && (part_count & 0x01)) {
- part_length = 0;
- } else if ((input_data->len - part_offset) < data_part_len) {
- part_length = (input_data->len - part_offset);
- } else {
- part_length = data_part_len;
- }
- PSA_ASSERT(psa_mac_update(&operation,
- (input_data->x + part_offset),
- part_length));
- }
- } else {
- /* Pass all data in one go. */
- PSA_ASSERT(psa_mac_update(&operation, input_data->x,
- input_data->len));
- }
- if (is_verify) {
- PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
- expected_output->len));
- } else {
- PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
- PSA_MAC_MAX_SIZE, &mac_len));
- ASSERT_COMPARE(expected_output->x, expected_output->len,
- mac, mac_len);
- }
- test_ok = 1;
- exit:
- psa_destroy_key(key);
- psa_mac_abort(&operation);
- PSA_DONE();
- return test_ok;
- }
- #if defined(PSA_WANT_ALG_JPAKE)
- static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
- psa_pake_operation_t *server,
- psa_pake_operation_t *client,
- int client_input_first,
- int round, int inject_error)
- {
- unsigned char *buffer0 = NULL, *buffer1 = NULL;
- size_t buffer_length = (
- PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
- PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
- PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
- /* The output should be exactly this size according to the spec */
- const size_t expected_size_key_share =
- PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
- /* The output should be exactly this size according to the spec */
- const size_t expected_size_zk_public =
- PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
- /* The output can be smaller: the spec allows stripping leading zeroes */
- const size_t max_expected_size_zk_proof =
- PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
- size_t buffer0_off = 0;
- size_t buffer1_off = 0;
- size_t s_g1_len, s_g2_len, s_a_len;
- size_t s_g1_off, s_g2_off, s_a_off;
- size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
- size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
- size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
- size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
- size_t c_g1_len, c_g2_len, c_a_len;
- size_t c_g1_off, c_g2_off, c_a_off;
- size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
- size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
- size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
- size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
- psa_status_t expected_status = PSA_SUCCESS;
- psa_status_t status;
- ASSERT_ALLOC(buffer0, buffer_length);
- ASSERT_ALLOC(buffer1, buffer_length);
- switch (round) {
- case 1:
- /* Server first round Output */
- PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
- buffer0 + buffer0_off,
- 512 - buffer0_off, &s_g1_len));
- TEST_EQUAL(s_g1_len, expected_size_key_share);
- s_g1_off = buffer0_off;
- buffer0_off += s_g1_len;
- PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer0 + buffer0_off,
- 512 - buffer0_off, &s_x1_pk_len));
- TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
- s_x1_pk_off = buffer0_off;
- buffer0_off += s_x1_pk_len;
- PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
- buffer0 + buffer0_off,
- 512 - buffer0_off, &s_x1_pr_len));
- TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
- s_x1_pr_off = buffer0_off;
- buffer0_off += s_x1_pr_len;
- PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
- buffer0 + buffer0_off,
- 512 - buffer0_off, &s_g2_len));
- TEST_EQUAL(s_g2_len, expected_size_key_share);
- s_g2_off = buffer0_off;
- buffer0_off += s_g2_len;
- PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer0 + buffer0_off,
- 512 - buffer0_off, &s_x2_pk_len));
- TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
- s_x2_pk_off = buffer0_off;
- buffer0_off += s_x2_pk_len;
- PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
- buffer0 + buffer0_off,
- 512 - buffer0_off, &s_x2_pr_len));
- TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
- s_x2_pr_off = buffer0_off;
- buffer0_off += s_x2_pr_len;
- if (inject_error == 1) {
- buffer0[s_x1_pr_off + 8] ^= 1;
- buffer0[s_x2_pr_off + 7] ^= 1;
- expected_status = PSA_ERROR_DATA_INVALID;
- }
- /*
- * When injecting errors in inputs, the implementation is
- * free to detect it right away of with a delay.
- * This permits delaying the error until the end of the input
- * sequence, if no error appears then, this will be treated
- * as an error.
- */
- if (client_input_first == 1) {
- /* Client first round Input */
- status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
- buffer0 + s_g1_off, s_g1_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer0 + s_x1_pk_off,
- s_x1_pk_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
- buffer0 + s_x1_pr_off,
- s_x1_pr_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
- buffer0 + s_g2_off,
- s_g2_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer0 + s_x2_pk_off,
- s_x2_pk_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
- buffer0 + s_x2_pr_off,
- s_x2_pr_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- /* Error didn't trigger, make test fail */
- if (inject_error == 1) {
- TEST_ASSERT(
- !"One of the last psa_pake_input() calls should have returned the expected error.");
- }
- }
- /* Client first round Output */
- PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
- buffer1 + buffer1_off,
- 512 - buffer1_off, &c_g1_len));
- TEST_EQUAL(c_g1_len, expected_size_key_share);
- c_g1_off = buffer1_off;
- buffer1_off += c_g1_len;
- PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer1 + buffer1_off,
- 512 - buffer1_off, &c_x1_pk_len));
- TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
- c_x1_pk_off = buffer1_off;
- buffer1_off += c_x1_pk_len;
- PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
- buffer1 + buffer1_off,
- 512 - buffer1_off, &c_x1_pr_len));
- TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
- c_x1_pr_off = buffer1_off;
- buffer1_off += c_x1_pr_len;
- PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
- buffer1 + buffer1_off,
- 512 - buffer1_off, &c_g2_len));
- TEST_EQUAL(c_g2_len, expected_size_key_share);
- c_g2_off = buffer1_off;
- buffer1_off += c_g2_len;
- PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer1 + buffer1_off,
- 512 - buffer1_off, &c_x2_pk_len));
- TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
- c_x2_pk_off = buffer1_off;
- buffer1_off += c_x2_pk_len;
- PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
- buffer1 + buffer1_off,
- 512 - buffer1_off, &c_x2_pr_len));
- TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
- c_x2_pr_off = buffer1_off;
- buffer1_off += c_x2_pr_len;
- if (client_input_first == 0) {
- /* Client first round Input */
- status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
- buffer0 + s_g1_off, s_g1_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer0 + s_x1_pk_off,
- s_x1_pk_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
- buffer0 + s_x1_pr_off,
- s_x1_pr_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
- buffer0 + s_g2_off,
- s_g2_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer0 + s_x2_pk_off,
- s_x2_pk_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
- buffer0 + s_x2_pr_off,
- s_x2_pr_len);
- if (inject_error == 1 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- /* Error didn't trigger, make test fail */
- if (inject_error == 1) {
- TEST_ASSERT(
- !"One of the last psa_pake_input() calls should have returned the expected error.");
- }
- }
- if (inject_error == 2) {
- buffer1[c_x1_pr_off + 12] ^= 1;
- buffer1[c_x2_pr_off + 7] ^= 1;
- expected_status = PSA_ERROR_DATA_INVALID;
- }
- /* Server first round Input */
- status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
- buffer1 + c_g1_off, c_g1_len);
- if (inject_error == 2 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer1 + c_x1_pk_off, c_x1_pk_len);
- if (inject_error == 2 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
- buffer1 + c_x1_pr_off, c_x1_pr_len);
- if (inject_error == 2 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
- buffer1 + c_g2_off, c_g2_len);
- if (inject_error == 2 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer1 + c_x2_pk_off, c_x2_pk_len);
- if (inject_error == 2 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
- buffer1 + c_x2_pr_off, c_x2_pr_len);
- if (inject_error == 2 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- /* Error didn't trigger, make test fail */
- if (inject_error == 2) {
- TEST_ASSERT(
- !"One of the last psa_pake_input() calls should have returned the expected error.");
- }
- break;
- case 2:
- /* Server second round Output */
- buffer0_off = 0;
- PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
- buffer0 + buffer0_off,
- 512 - buffer0_off, &s_a_len));
- TEST_EQUAL(s_a_len, expected_size_key_share);
- s_a_off = buffer0_off;
- buffer0_off += s_a_len;
- PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer0 + buffer0_off,
- 512 - buffer0_off, &s_x2s_pk_len));
- TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
- s_x2s_pk_off = buffer0_off;
- buffer0_off += s_x2s_pk_len;
- PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
- buffer0 + buffer0_off,
- 512 - buffer0_off, &s_x2s_pr_len));
- TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
- s_x2s_pr_off = buffer0_off;
- buffer0_off += s_x2s_pr_len;
- if (inject_error == 3) {
- buffer0[s_x2s_pk_off + 12] += 0x33;
- expected_status = PSA_ERROR_DATA_INVALID;
- }
- if (client_input_first == 1) {
- /* Client second round Input */
- status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
- buffer0 + s_a_off, s_a_len);
- if (inject_error == 3 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer0 + s_x2s_pk_off,
- s_x2s_pk_len);
- if (inject_error == 3 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
- buffer0 + s_x2s_pr_off,
- s_x2s_pr_len);
- if (inject_error == 3 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- /* Error didn't trigger, make test fail */
- if (inject_error == 3) {
- TEST_ASSERT(
- !"One of the last psa_pake_input() calls should have returned the expected error.");
- }
- }
- /* Client second round Output */
- buffer1_off = 0;
- PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
- buffer1 + buffer1_off,
- 512 - buffer1_off, &c_a_len));
- TEST_EQUAL(c_a_len, expected_size_key_share);
- c_a_off = buffer1_off;
- buffer1_off += c_a_len;
- PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer1 + buffer1_off,
- 512 - buffer1_off, &c_x2s_pk_len));
- TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
- c_x2s_pk_off = buffer1_off;
- buffer1_off += c_x2s_pk_len;
- PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
- buffer1 + buffer1_off,
- 512 - buffer1_off, &c_x2s_pr_len));
- TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
- c_x2s_pr_off = buffer1_off;
- buffer1_off += c_x2s_pr_len;
- if (client_input_first == 0) {
- /* Client second round Input */
- status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
- buffer0 + s_a_off, s_a_len);
- if (inject_error == 3 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer0 + s_x2s_pk_off,
- s_x2s_pk_len);
- if (inject_error == 3 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
- buffer0 + s_x2s_pr_off,
- s_x2s_pr_len);
- if (inject_error == 3 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- /* Error didn't trigger, make test fail */
- if (inject_error == 3) {
- TEST_ASSERT(
- !"One of the last psa_pake_input() calls should have returned the expected error.");
- }
- }
- if (inject_error == 4) {
- buffer1[c_x2s_pk_off + 7] += 0x28;
- expected_status = PSA_ERROR_DATA_INVALID;
- }
- /* Server second round Input */
- status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
- buffer1 + c_a_off, c_a_len);
- if (inject_error == 4 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
- buffer1 + c_x2s_pk_off, c_x2s_pk_len);
- if (inject_error == 4 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
- buffer1 + c_x2s_pr_off, c_x2s_pr_len);
- if (inject_error == 4 && status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- break;
- } else {
- TEST_EQUAL(status, PSA_SUCCESS);
- }
- /* Error didn't trigger, make test fail */
- if (inject_error == 4) {
- TEST_ASSERT(
- !"One of the last psa_pake_input() calls should have returned the expected error.");
- }
- break;
- }
- exit:
- mbedtls_free(buffer0);
- mbedtls_free(buffer1);
- }
- #endif /* PSA_WANT_ALG_JPAKE */
- typedef enum {
- INJECT_ERR_NONE = 0,
- INJECT_ERR_UNINITIALIZED_ACCESS,
- INJECT_ERR_DUPLICATE_SETUP,
- INJECT_ERR_INVALID_USER,
- INJECT_ERR_INVALID_PEER,
- INJECT_ERR_SET_USER,
- INJECT_ERR_SET_PEER,
- INJECT_EMPTY_IO_BUFFER,
- INJECT_UNKNOWN_STEP,
- INJECT_INVALID_FIRST_STEP,
- INJECT_WRONG_BUFFER_SIZE,
- INJECT_VALID_OPERATION_AFTER_FAILURE,
- INJECT_ANTICIPATE_KEY_DERIVATION_1,
- INJECT_ANTICIPATE_KEY_DERIVATION_2,
- } ecjpake_injected_failure_t;
- #if defined(MBEDTLS_ECP_RESTARTABLE)
- static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
- psa_status_t expected_status,
- size_t *min_completes,
- size_t *max_completes)
- {
- /* This is slightly contrived, but we only really know that with a minimum
- value of max_ops that a successful operation should take more than one op
- to complete, and likewise that with a max_ops of
- PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
- if (max_ops == 0 || max_ops == 1) {
- if (expected_status == PSA_SUCCESS) {
- *min_completes = 2;
- } else {
- *min_completes = 1;
- }
- *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
- } else {
- *min_completes = 1;
- *max_completes = 1;
- }
- }
- #endif /* MBEDTLS_ECP_RESTARTABLE */
- /* END_HEADER */
- /* BEGIN_DEPENDENCIES
- * depends_on:MBEDTLS_PSA_CRYPTO_C
- * END_DEPENDENCIES
- */
- /* BEGIN_CASE */
- void psa_can_do_hash()
- {
- /* We can't test that this is specific to drivers until partial init has
- * been implemented, but we can at least test before/after full init. */
- TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
- PSA_INIT();
- TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void static_checks()
- {
- size_t max_truncated_mac_size =
- PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
- /* Check that the length for a truncated MAC always fits in the algorithm
- * encoding. The shifted mask is the maximum truncated value. The
- * untruncated algorithm may be one byte larger. */
- TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void import_with_policy(int type_arg,
- int usage_arg, int alg_arg,
- int expected_status_arg)
- {
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t type = type_arg;
- psa_key_usage_t usage = usage_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t expected_status = expected_status_arg;
- const uint8_t key_material[16] = { 0 };
- psa_status_t status;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_type(&attributes, type);
- psa_set_key_usage_flags(&attributes, usage);
- psa_set_key_algorithm(&attributes, alg);
- status = psa_import_key(&attributes,
- key_material, sizeof(key_material),
- &key);
- TEST_EQUAL(status, expected_status);
- if (status != PSA_SUCCESS) {
- goto exit;
- }
- PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
- TEST_EQUAL(psa_get_key_type(&got_attributes), type);
- TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
- mbedtls_test_update_key_usage_flags(usage));
- TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
- ASSERT_NO_SLOT_NUMBER(&got_attributes);
- PSA_ASSERT(psa_destroy_key(key));
- test_operations_on_invalid_key(key);
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&got_attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void import_with_data(data_t *data, int type_arg,
- int attr_bits_arg,
- int expected_status_arg)
- {
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t type = type_arg;
- size_t attr_bits = attr_bits_arg;
- psa_status_t expected_status = expected_status_arg;
- psa_status_t status;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_type(&attributes, type);
- psa_set_key_bits(&attributes, attr_bits);
- status = psa_import_key(&attributes, data->x, data->len, &key);
- TEST_EQUAL(status, expected_status);
- if (status != PSA_SUCCESS) {
- goto exit;
- }
- PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
- TEST_EQUAL(psa_get_key_type(&got_attributes), type);
- if (attr_bits != 0) {
- TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
- }
- ASSERT_NO_SLOT_NUMBER(&got_attributes);
- PSA_ASSERT(psa_destroy_key(key));
- test_operations_on_invalid_key(key);
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&got_attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- /* Construct and attempt to import a large unstructured key. */
- void import_large_key(int type_arg, int byte_size_arg,
- int expected_status_arg)
- {
- psa_key_type_t type = type_arg;
- size_t byte_size = byte_size_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t expected_status = expected_status_arg;
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_status_t status;
- uint8_t *buffer = NULL;
- size_t buffer_size = byte_size + 1;
- size_t n;
- /* Skip the test case if the target running the test cannot
- * accommodate large keys due to heap size constraints */
- ASSERT_ALLOC_WEAK(buffer, buffer_size);
- memset(buffer, 'K', byte_size);
- PSA_ASSERT(psa_crypto_init());
- /* Try importing the key */
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
- psa_set_key_type(&attributes, type);
- status = psa_import_key(&attributes, buffer, byte_size, &key);
- TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
- TEST_EQUAL(status, expected_status);
- if (status == PSA_SUCCESS) {
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- TEST_EQUAL(psa_get_key_type(&attributes), type);
- TEST_EQUAL(psa_get_key_bits(&attributes),
- PSA_BYTES_TO_BITS(byte_size));
- ASSERT_NO_SLOT_NUMBER(&attributes);
- memset(buffer, 0, byte_size + 1);
- PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
- for (n = 0; n < byte_size; n++) {
- TEST_EQUAL(buffer[n], 'K');
- }
- for (n = byte_size; n < buffer_size; n++) {
- TEST_EQUAL(buffer[n], 0);
- }
- }
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- PSA_DONE();
- mbedtls_free(buffer);
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
- /* Import an RSA key with a valid structure (but not valid numbers
- * inside, beyond having sensible size and parity). This is expected to
- * fail for large keys. */
- void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- size_t bits = bits_arg;
- psa_status_t expected_status = expected_status_arg;
- psa_status_t status;
- psa_key_type_t type =
- keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
- size_t buffer_size = /* Slight overapproximations */
- keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
- unsigned char *buffer = NULL;
- unsigned char *p;
- int ret;
- size_t length;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- ASSERT_ALLOC(buffer, buffer_size);
- TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
- bits, keypair)) >= 0);
- length = ret;
- /* Try importing the key */
- psa_set_key_type(&attributes, type);
- status = psa_import_key(&attributes, p, length, &key);
- TEST_EQUAL(status, expected_status);
- if (status == PSA_SUCCESS) {
- PSA_ASSERT(psa_destroy_key(key));
- }
- exit:
- mbedtls_free(buffer);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void import_export(data_t *data,
- int type_arg,
- int usage_arg, int alg_arg,
- int lifetime_arg,
- int expected_bits,
- int export_size_delta,
- int expected_export_status_arg,
- /*whether reexport must give the original input exactly*/
- int canonical_input)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t type = type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t expected_export_status = expected_export_status_arg;
- psa_status_t status;
- psa_key_lifetime_t lifetime = lifetime_arg;
- unsigned char *exported = NULL;
- unsigned char *reexported = NULL;
- size_t export_size;
- size_t exported_length = INVALID_EXPORT_LENGTH;
- size_t reexported_length;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
- export_size = (ptrdiff_t) data->len + export_size_delta;
- ASSERT_ALLOC(exported, export_size);
- if (!canonical_input) {
- ASSERT_ALLOC(reexported, export_size);
- }
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_lifetime(&attributes, lifetime);
- psa_set_key_usage_flags(&attributes, usage_arg);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, type);
- /* Import the key */
- PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
- /* Test the key information */
- PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
- TEST_EQUAL(psa_get_key_type(&got_attributes), type);
- TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
- ASSERT_NO_SLOT_NUMBER(&got_attributes);
- /* Export the key */
- status = psa_export_key(key, exported, export_size, &exported_length);
- TEST_EQUAL(status, expected_export_status);
- /* The exported length must be set by psa_export_key() to a value between 0
- * and export_size. On errors, the exported length must be 0. */
- TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
- TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
- TEST_LE_U(exported_length, export_size);
- TEST_ASSERT(mem_is_char(exported + exported_length, 0,
- export_size - exported_length));
- if (status != PSA_SUCCESS) {
- TEST_EQUAL(exported_length, 0);
- goto destroy;
- }
- /* Run sanity checks on the exported key. For non-canonical inputs,
- * this validates the canonical representations. For canonical inputs,
- * this doesn't directly validate the implementation, but it still helps
- * by cross-validating the test data with the sanity check code. */
- if (!psa_key_lifetime_is_external(lifetime)) {
- if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
- goto exit;
- }
- }
- if (canonical_input) {
- ASSERT_COMPARE(data->x, data->len, exported, exported_length);
- } else {
- mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
- PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
- &key2));
- PSA_ASSERT(psa_export_key(key2,
- reexported,
- export_size,
- &reexported_length));
- ASSERT_COMPARE(exported, exported_length,
- reexported, reexported_length);
- PSA_ASSERT(psa_destroy_key(key2));
- }
- TEST_LE_U(exported_length,
- PSA_EXPORT_KEY_OUTPUT_SIZE(type,
- psa_get_key_bits(&got_attributes)));
- TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
- destroy:
- /* Destroy the key */
- PSA_ASSERT(psa_destroy_key(key));
- test_operations_on_invalid_key(key);
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&got_attributes);
- psa_destroy_key(key);
- mbedtls_free(exported);
- mbedtls_free(reexported);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void import_export_public_key(data_t *data,
- int type_arg, // key pair or public key
- int alg_arg,
- int lifetime_arg,
- int export_size_delta,
- int expected_export_status_arg,
- data_t *expected_public_key)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t type = type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t expected_export_status = expected_export_status_arg;
- psa_status_t status;
- psa_key_lifetime_t lifetime = lifetime_arg;
- unsigned char *exported = NULL;
- size_t export_size = expected_public_key->len + export_size_delta;
- size_t exported_length = INVALID_EXPORT_LENGTH;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_lifetime(&attributes, lifetime);
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, type);
- /* Import the key */
- PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
- /* Export the public key */
- ASSERT_ALLOC(exported, export_size);
- status = psa_export_public_key(key,
- exported, export_size,
- &exported_length);
- TEST_EQUAL(status, expected_export_status);
- if (status == PSA_SUCCESS) {
- psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
- size_t bits;
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- bits = psa_get_key_bits(&attributes);
- TEST_LE_U(expected_public_key->len,
- PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
- TEST_LE_U(expected_public_key->len,
- PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
- TEST_LE_U(expected_public_key->len,
- PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
- ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
- exported, exported_length);
- }
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- mbedtls_free(exported);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void import_and_exercise_key(data_t *data,
- int type_arg,
- int bits_arg,
- int alg_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t type = type_arg;
- size_t bits = bits_arg;
- psa_algorithm_t alg = alg_arg;
- psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, usage);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, type);
- /* Import the key */
- PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
- /* Test the key information */
- PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
- TEST_EQUAL(psa_get_key_type(&got_attributes), type);
- TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
- /* Do something with the key according to its type and permitted usage. */
- if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
- goto exit;
- }
- PSA_ASSERT(psa_destroy_key(key));
- test_operations_on_invalid_key(key);
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&got_attributes);
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void effective_key_attributes(int type_arg, int expected_type_arg,
- int bits_arg, int expected_bits_arg,
- int usage_arg, int expected_usage_arg,
- int alg_arg, int expected_alg_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = type_arg;
- psa_key_type_t expected_key_type = expected_type_arg;
- size_t bits = bits_arg;
- size_t expected_bits = expected_bits_arg;
- psa_algorithm_t alg = alg_arg;
- psa_algorithm_t expected_alg = expected_alg_arg;
- psa_key_usage_t usage = usage_arg;
- psa_key_usage_t expected_usage = expected_usage_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, usage);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- psa_set_key_bits(&attributes, bits);
- PSA_ASSERT(psa_generate_key(&attributes, &key));
- psa_reset_key_attributes(&attributes);
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
- TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
- TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
- TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void check_key_policy(int type_arg, int bits_arg,
- int usage_arg, int alg_arg)
- {
- test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
- usage_arg,
- mbedtls_test_update_key_usage_flags(usage_arg),
- alg_arg, alg_arg);
- goto exit;
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void key_attributes_init()
- {
- /* Test each valid way of initializing the object, except for `= {0}`, as
- * Clang 5 complains when `-Wmissing-field-initializers` is used, even
- * though it's OK by the C standard. We could test for this, but we'd need
- * to suppress the Clang warning for the test. */
- psa_key_attributes_t func = psa_key_attributes_init();
- psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t zero;
- memset(&zero, 0, sizeof(zero));
- TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
- TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
- TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
- TEST_EQUAL(psa_get_key_type(&func), 0);
- TEST_EQUAL(psa_get_key_type(&init), 0);
- TEST_EQUAL(psa_get_key_type(&zero), 0);
- TEST_EQUAL(psa_get_key_bits(&func), 0);
- TEST_EQUAL(psa_get_key_bits(&init), 0);
- TEST_EQUAL(psa_get_key_bits(&zero), 0);
- TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
- TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
- TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
- TEST_EQUAL(psa_get_key_algorithm(&func), 0);
- TEST_EQUAL(psa_get_key_algorithm(&init), 0);
- TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void mac_key_policy(int policy_usage_arg,
- int policy_alg_arg,
- int key_type_arg,
- data_t *key_data,
- int exercise_alg_arg,
- int expected_status_sign_arg,
- int expected_status_verify_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t policy_alg = policy_alg_arg;
- psa_algorithm_t exercise_alg = exercise_alg_arg;
- psa_key_usage_t policy_usage = policy_usage_arg;
- psa_status_t status;
- psa_status_t expected_status_sign = expected_status_sign_arg;
- psa_status_t expected_status_verify = expected_status_verify_arg;
- unsigned char mac[PSA_MAC_MAX_SIZE];
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, policy_usage);
- psa_set_key_algorithm(&attributes, policy_alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- TEST_EQUAL(psa_get_key_usage_flags(&attributes),
- mbedtls_test_update_key_usage_flags(policy_usage));
- status = psa_mac_sign_setup(&operation, key, exercise_alg);
- TEST_EQUAL(status, expected_status_sign);
- /* Calculate the MAC, one-shot case. */
- uint8_t input[128] = { 0 };
- size_t mac_len;
- TEST_EQUAL(psa_mac_compute(key, exercise_alg,
- input, 128,
- mac, PSA_MAC_MAX_SIZE, &mac_len),
- expected_status_sign);
- /* Calculate the MAC, multi-part case. */
- PSA_ASSERT(psa_mac_abort(&operation));
- status = psa_mac_sign_setup(&operation, key, exercise_alg);
- if (status == PSA_SUCCESS) {
- status = psa_mac_update(&operation, input, 128);
- if (status == PSA_SUCCESS) {
- TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
- &mac_len),
- expected_status_sign);
- } else {
- TEST_EQUAL(status, expected_status_sign);
- }
- } else {
- TEST_EQUAL(status, expected_status_sign);
- }
- PSA_ASSERT(psa_mac_abort(&operation));
- /* Verify correct MAC, one-shot case. */
- status = psa_mac_verify(key, exercise_alg, input, 128,
- mac, mac_len);
- if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
- TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
- } else {
- TEST_EQUAL(status, expected_status_verify);
- }
- /* Verify correct MAC, multi-part case. */
- status = psa_mac_verify_setup(&operation, key, exercise_alg);
- if (status == PSA_SUCCESS) {
- status = psa_mac_update(&operation, input, 128);
- if (status == PSA_SUCCESS) {
- status = psa_mac_verify_finish(&operation, mac, mac_len);
- if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
- TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
- } else {
- TEST_EQUAL(status, expected_status_verify);
- }
- } else {
- TEST_EQUAL(status, expected_status_verify);
- }
- } else {
- TEST_EQUAL(status, expected_status_verify);
- }
- psa_mac_abort(&operation);
- memset(mac, 0, sizeof(mac));
- status = psa_mac_verify_setup(&operation, key, exercise_alg);
- TEST_EQUAL(status, expected_status_verify);
- exit:
- psa_mac_abort(&operation);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_key_policy(int policy_usage_arg,
- int policy_alg,
- int key_type,
- data_t *key_data,
- int exercise_alg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- psa_key_usage_t policy_usage = policy_usage_arg;
- size_t output_buffer_size = 0;
- size_t input_buffer_size = 0;
- size_t output_length = 0;
- uint8_t *output = NULL;
- uint8_t *input = NULL;
- psa_status_t status;
- input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
- output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
- input_buffer_size);
- ASSERT_ALLOC(input, input_buffer_size);
- ASSERT_ALLOC(output, output_buffer_size);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, policy_usage);
- psa_set_key_algorithm(&attributes, policy_alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- /* Check if no key usage flag implication is done */
- TEST_EQUAL(policy_usage,
- mbedtls_test_update_key_usage_flags(policy_usage));
- /* Encrypt check, one-shot */
- status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
- output, output_buffer_size,
- &output_length);
- if (policy_alg == exercise_alg &&
- (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
- PSA_ASSERT(status);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- /* Encrypt check, multi-part */
- status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
- if (policy_alg == exercise_alg &&
- (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
- PSA_ASSERT(status);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- psa_cipher_abort(&operation);
- /* Decrypt check, one-shot */
- status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
- input, input_buffer_size,
- &output_length);
- if (policy_alg == exercise_alg &&
- (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
- PSA_ASSERT(status);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- /* Decrypt check, multi-part */
- status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
- if (policy_alg == exercise_alg &&
- (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
- PSA_ASSERT(status);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- exit:
- psa_cipher_abort(&operation);
- mbedtls_free(input);
- mbedtls_free(output);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_key_policy(int policy_usage_arg,
- int policy_alg,
- int key_type,
- data_t *key_data,
- int nonce_length_arg,
- int tag_length_arg,
- int exercise_alg,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
- psa_key_usage_t policy_usage = policy_usage_arg;
- psa_status_t status;
- psa_status_t expected_status = expected_status_arg;
- unsigned char nonce[16] = { 0 };
- size_t nonce_length = nonce_length_arg;
- unsigned char tag[16];
- size_t tag_length = tag_length_arg;
- size_t output_length;
- TEST_LE_U(nonce_length, sizeof(nonce));
- TEST_LE_U(tag_length, sizeof(tag));
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, policy_usage);
- psa_set_key_algorithm(&attributes, policy_alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- /* Check if no key usage implication is done */
- TEST_EQUAL(policy_usage,
- mbedtls_test_update_key_usage_flags(policy_usage));
- /* Encrypt check, one-shot */
- status = psa_aead_encrypt(key, exercise_alg,
- nonce, nonce_length,
- NULL, 0,
- NULL, 0,
- tag, tag_length,
- &output_length);
- if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
- TEST_EQUAL(status, expected_status);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- /* Encrypt check, multi-part */
- status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
- if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
- TEST_EQUAL(status, expected_status);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- /* Decrypt check, one-shot */
- memset(tag, 0, sizeof(tag));
- status = psa_aead_decrypt(key, exercise_alg,
- nonce, nonce_length,
- NULL, 0,
- tag, tag_length,
- NULL, 0,
- &output_length);
- if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- } else if (expected_status == PSA_SUCCESS) {
- TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
- } else {
- TEST_EQUAL(status, expected_status);
- }
- /* Decrypt check, multi-part */
- PSA_ASSERT(psa_aead_abort(&operation));
- status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
- if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- } else {
- TEST_EQUAL(status, expected_status);
- }
- exit:
- PSA_ASSERT(psa_aead_abort(&operation));
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void asymmetric_encryption_key_policy(int policy_usage_arg,
- int policy_alg,
- int key_type,
- data_t *key_data,
- int exercise_alg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_usage_t policy_usage = policy_usage_arg;
- psa_status_t status;
- size_t key_bits;
- size_t buffer_length;
- unsigned char *buffer = NULL;
- size_t output_length;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, policy_usage);
- psa_set_key_algorithm(&attributes, policy_alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- /* Check if no key usage implication is done */
- TEST_EQUAL(policy_usage,
- mbedtls_test_update_key_usage_flags(policy_usage));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
- exercise_alg);
- ASSERT_ALLOC(buffer, buffer_length);
- status = psa_asymmetric_encrypt(key, exercise_alg,
- NULL, 0,
- NULL, 0,
- buffer, buffer_length,
- &output_length);
- if (policy_alg == exercise_alg &&
- (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
- PSA_ASSERT(status);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- if (buffer_length != 0) {
- memset(buffer, 0, buffer_length);
- }
- status = psa_asymmetric_decrypt(key, exercise_alg,
- buffer, buffer_length,
- NULL, 0,
- buffer, buffer_length,
- &output_length);
- if (policy_alg == exercise_alg &&
- (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
- TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- PSA_DONE();
- mbedtls_free(buffer);
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void asymmetric_signature_key_policy(int policy_usage_arg,
- int policy_alg,
- int key_type,
- data_t *key_data,
- int exercise_alg,
- int payload_length_arg,
- int expected_usage_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_usage_t policy_usage = policy_usage_arg;
- psa_key_usage_t expected_usage = expected_usage_arg;
- psa_status_t status;
- unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
- /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
- * compatible with the policy and `payload_length_arg` is supposed to be
- * a valid input length to sign. If `payload_length_arg <= 0`,
- * `exercise_alg` is supposed to be forbidden by the policy. */
- int compatible_alg = payload_length_arg > 0;
- size_t payload_length = compatible_alg ? payload_length_arg : 0;
- unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
- size_t signature_length;
- /* Check if all implicit usage flags are deployed
- in the expected usage flags. */
- TEST_EQUAL(expected_usage,
- mbedtls_test_update_key_usage_flags(policy_usage));
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, policy_usage);
- psa_set_key_algorithm(&attributes, policy_alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
- status = psa_sign_hash(key, exercise_alg,
- payload, payload_length,
- signature, sizeof(signature),
- &signature_length);
- if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
- PSA_ASSERT(status);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- memset(signature, 0, sizeof(signature));
- status = psa_verify_hash(key, exercise_alg,
- payload, payload_length,
- signature, sizeof(signature));
- if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
- TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
- PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
- status = psa_sign_message(key, exercise_alg,
- payload, payload_length,
- signature, sizeof(signature),
- &signature_length);
- if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
- PSA_ASSERT(status);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- memset(signature, 0, sizeof(signature));
- status = psa_verify_message(key, exercise_alg,
- payload, payload_length,
- signature, sizeof(signature));
- if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
- TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- }
- exit:
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_key_policy(int policy_usage,
- int policy_alg,
- int key_type,
- data_t *key_data,
- int exercise_alg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_status_t status;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, policy_usage);
- psa_set_key_algorithm(&attributes, policy_alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
- if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
- PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
- PSA_ASSERT(psa_key_derivation_input_bytes(
- &operation,
- PSA_KEY_DERIVATION_INPUT_SEED,
- (const uint8_t *) "", 0));
- }
- status = psa_key_derivation_input_key(&operation,
- PSA_KEY_DERIVATION_INPUT_SECRET,
- key);
- if (policy_alg == exercise_alg &&
- (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
- PSA_ASSERT(status);
- } else {
- TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
- }
- exit:
- psa_key_derivation_abort(&operation);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void agreement_key_policy(int policy_usage,
- int policy_alg,
- int key_type_arg,
- data_t *key_data,
- int exercise_alg,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_status_t status;
- psa_status_t expected_status = expected_status_arg;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, policy_usage);
- psa_set_key_algorithm(&attributes, policy_alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
- status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
- TEST_EQUAL(status, expected_status);
- exit:
- psa_key_derivation_abort(&operation);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void key_policy_alg2(int key_type_arg, data_t *key_data,
- int usage_arg, int alg_arg, int alg2_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_usage_t usage = usage_arg;
- psa_algorithm_t alg = alg_arg;
- psa_algorithm_t alg2 = alg2_arg;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, usage);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_enrollment_algorithm(&attributes, alg2);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- /* Update the usage flags to obtain implicit usage flags */
- usage = mbedtls_test_update_key_usage_flags(usage);
- PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
- TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
- TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
- TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
- if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
- goto exit;
- }
- if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
- goto exit;
- }
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&got_attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void raw_agreement_key_policy(int policy_usage,
- int policy_alg,
- int key_type_arg,
- data_t *key_data,
- int exercise_alg,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_status_t status;
- psa_status_t expected_status = expected_status_arg;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, policy_usage);
- psa_set_key_algorithm(&attributes, policy_alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
- TEST_EQUAL(status, expected_status);
- exit:
- psa_key_derivation_abort(&operation);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void copy_success(int source_usage_arg,
- int source_alg_arg, int source_alg2_arg,
- unsigned int source_lifetime_arg,
- int type_arg, data_t *material,
- int copy_attributes,
- int target_usage_arg,
- int target_alg_arg, int target_alg2_arg,
- unsigned int target_lifetime_arg,
- int expected_usage_arg,
- int expected_alg_arg, int expected_alg2_arg)
- {
- psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_usage_t expected_usage = expected_usage_arg;
- psa_algorithm_t expected_alg = expected_alg_arg;
- psa_algorithm_t expected_alg2 = expected_alg2_arg;
- psa_key_lifetime_t source_lifetime = source_lifetime_arg;
- psa_key_lifetime_t target_lifetime = target_lifetime_arg;
- mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
- mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
- uint8_t *export_buffer = NULL;
- PSA_ASSERT(psa_crypto_init());
- /* Prepare the source key. */
- psa_set_key_usage_flags(&source_attributes, source_usage_arg);
- psa_set_key_algorithm(&source_attributes, source_alg_arg);
- psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
- psa_set_key_type(&source_attributes, type_arg);
- psa_set_key_lifetime(&source_attributes, source_lifetime);
- PSA_ASSERT(psa_import_key(&source_attributes,
- material->x, material->len,
- &source_key));
- PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
- /* Prepare the target attributes. */
- if (copy_attributes) {
- target_attributes = source_attributes;
- }
- psa_set_key_lifetime(&target_attributes, target_lifetime);
- if (target_usage_arg != -1) {
- psa_set_key_usage_flags(&target_attributes, target_usage_arg);
- }
- if (target_alg_arg != -1) {
- psa_set_key_algorithm(&target_attributes, target_alg_arg);
- }
- if (target_alg2_arg != -1) {
- psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
- }
- /* Copy the key. */
- PSA_ASSERT(psa_copy_key(source_key,
- &target_attributes, &target_key));
- /* Destroy the source to ensure that this doesn't affect the target. */
- PSA_ASSERT(psa_destroy_key(source_key));
- /* Test that the target slot has the expected content and policy. */
- PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
- TEST_EQUAL(psa_get_key_type(&source_attributes),
- psa_get_key_type(&target_attributes));
- TEST_EQUAL(psa_get_key_bits(&source_attributes),
- psa_get_key_bits(&target_attributes));
- TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
- TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
- TEST_EQUAL(expected_alg2,
- psa_get_key_enrollment_algorithm(&target_attributes));
- if (expected_usage & PSA_KEY_USAGE_EXPORT) {
- size_t length;
- ASSERT_ALLOC(export_buffer, material->len);
- PSA_ASSERT(psa_export_key(target_key, export_buffer,
- material->len, &length));
- ASSERT_COMPARE(material->x, material->len,
- export_buffer, length);
- }
- if (!psa_key_lifetime_is_external(target_lifetime)) {
- if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
- goto exit;
- }
- if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
- goto exit;
- }
- }
- PSA_ASSERT(psa_destroy_key(target_key));
- exit:
- /*
- * Source and target key attributes may have been returned by
- * psa_get_key_attributes() thus reset them as required.
- */
- psa_reset_key_attributes(&source_attributes);
- psa_reset_key_attributes(&target_attributes);
- PSA_DONE();
- mbedtls_free(export_buffer);
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void copy_fail(int source_usage_arg,
- int source_alg_arg, int source_alg2_arg,
- int source_lifetime_arg,
- int type_arg, data_t *material,
- int target_type_arg, int target_bits_arg,
- int target_usage_arg,
- int target_alg_arg, int target_alg2_arg,
- int target_id_arg, int target_lifetime_arg,
- int expected_status_arg)
- {
- psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
- mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
- mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
- mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
- PSA_ASSERT(psa_crypto_init());
- /* Prepare the source key. */
- psa_set_key_usage_flags(&source_attributes, source_usage_arg);
- psa_set_key_algorithm(&source_attributes, source_alg_arg);
- psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
- psa_set_key_type(&source_attributes, type_arg);
- psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
- PSA_ASSERT(psa_import_key(&source_attributes,
- material->x, material->len,
- &source_key));
- /* Prepare the target attributes. */
- psa_set_key_id(&target_attributes, key_id);
- psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
- psa_set_key_type(&target_attributes, target_type_arg);
- psa_set_key_bits(&target_attributes, target_bits_arg);
- psa_set_key_usage_flags(&target_attributes, target_usage_arg);
- psa_set_key_algorithm(&target_attributes, target_alg_arg);
- psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
- /* Try to copy the key. */
- TEST_EQUAL(psa_copy_key(source_key,
- &target_attributes, &target_key),
- expected_status_arg);
- PSA_ASSERT(psa_destroy_key(source_key));
- exit:
- psa_reset_key_attributes(&source_attributes);
- psa_reset_key_attributes(&target_attributes);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void hash_operation_init()
- {
- const uint8_t input[1] = { 0 };
- /* Test each valid way of initializing the object, except for `= {0}`, as
- * Clang 5 complains when `-Wmissing-field-initializers` is used, even
- * though it's OK by the C standard. We could test for this, but we'd need
- * to suppress the Clang warning for the test. */
- psa_hash_operation_t func = psa_hash_operation_init();
- psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
- psa_hash_operation_t zero;
- memset(&zero, 0, sizeof(zero));
- /* A freshly-initialized hash operation should not be usable. */
- TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- /* A default hash operation should be abortable without error. */
- PSA_ASSERT(psa_hash_abort(&func));
- PSA_ASSERT(psa_hash_abort(&init));
- PSA_ASSERT(psa_hash_abort(&zero));
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void hash_setup(int alg_arg,
- int expected_status_arg)
- {
- psa_algorithm_t alg = alg_arg;
- uint8_t *output = NULL;
- size_t output_size = 0;
- size_t output_length = 0;
- psa_status_t expected_status = expected_status_arg;
- psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
- psa_status_t status;
- PSA_ASSERT(psa_crypto_init());
- /* Hash Setup, one-shot */
- output_size = PSA_HASH_LENGTH(alg);
- ASSERT_ALLOC(output, output_size);
- status = psa_hash_compute(alg, NULL, 0,
- output, output_size, &output_length);
- TEST_EQUAL(status, expected_status);
- /* Hash Setup, multi-part */
- status = psa_hash_setup(&operation, alg);
- TEST_EQUAL(status, expected_status);
- /* Whether setup succeeded or failed, abort must succeed. */
- PSA_ASSERT(psa_hash_abort(&operation));
- /* If setup failed, reproduce the failure, so as to
- * test the resulting state of the operation object. */
- if (status != PSA_SUCCESS) {
- TEST_EQUAL(psa_hash_setup(&operation, alg), status);
- }
- /* Now the operation object should be reusable. */
- #if defined(KNOWN_SUPPORTED_HASH_ALG)
- PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
- PSA_ASSERT(psa_hash_abort(&operation));
- #endif
- exit:
- mbedtls_free(output);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void hash_compute_fail(int alg_arg, data_t *input,
- int output_size_arg, int expected_status_arg)
- {
- psa_algorithm_t alg = alg_arg;
- uint8_t *output = NULL;
- size_t output_size = output_size_arg;
- size_t output_length = INVALID_EXPORT_LENGTH;
- psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
- psa_status_t expected_status = expected_status_arg;
- psa_status_t status;
- ASSERT_ALLOC(output, output_size);
- PSA_ASSERT(psa_crypto_init());
- /* Hash Compute, one-shot */
- status = psa_hash_compute(alg, input->x, input->len,
- output, output_size, &output_length);
- TEST_EQUAL(status, expected_status);
- TEST_LE_U(output_length, output_size);
- /* Hash Compute, multi-part */
- status = psa_hash_setup(&operation, alg);
- if (status == PSA_SUCCESS) {
- status = psa_hash_update(&operation, input->x, input->len);
- if (status == PSA_SUCCESS) {
- status = psa_hash_finish(&operation, output, output_size,
- &output_length);
- if (status == PSA_SUCCESS) {
- TEST_LE_U(output_length, output_size);
- } else {
- TEST_EQUAL(status, expected_status);
- }
- } else {
- TEST_EQUAL(status, expected_status);
- }
- } else {
- TEST_EQUAL(status, expected_status);
- }
- exit:
- PSA_ASSERT(psa_hash_abort(&operation));
- mbedtls_free(output);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void hash_compare_fail(int alg_arg, data_t *input,
- data_t *reference_hash,
- int expected_status_arg)
- {
- psa_algorithm_t alg = alg_arg;
- psa_status_t expected_status = expected_status_arg;
- psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
- psa_status_t status;
- PSA_ASSERT(psa_crypto_init());
- /* Hash Compare, one-shot */
- status = psa_hash_compare(alg, input->x, input->len,
- reference_hash->x, reference_hash->len);
- TEST_EQUAL(status, expected_status);
- /* Hash Compare, multi-part */
- status = psa_hash_setup(&operation, alg);
- if (status == PSA_SUCCESS) {
- status = psa_hash_update(&operation, input->x, input->len);
- if (status == PSA_SUCCESS) {
- status = psa_hash_verify(&operation, reference_hash->x,
- reference_hash->len);
- TEST_EQUAL(status, expected_status);
- } else {
- TEST_EQUAL(status, expected_status);
- }
- } else {
- TEST_EQUAL(status, expected_status);
- }
- exit:
- PSA_ASSERT(psa_hash_abort(&operation));
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void hash_compute_compare(int alg_arg, data_t *input,
- data_t *expected_output)
- {
- psa_algorithm_t alg = alg_arg;
- uint8_t output[PSA_HASH_MAX_SIZE + 1];
- size_t output_length = INVALID_EXPORT_LENGTH;
- psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
- size_t i;
- PSA_ASSERT(psa_crypto_init());
- /* Compute with tight buffer, one-shot */
- PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
- output, PSA_HASH_LENGTH(alg),
- &output_length));
- TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
- ASSERT_COMPARE(output, output_length,
- expected_output->x, expected_output->len);
- /* Compute with tight buffer, multi-part */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
- PSA_ASSERT(psa_hash_finish(&operation, output,
- PSA_HASH_LENGTH(alg),
- &output_length));
- TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
- ASSERT_COMPARE(output, output_length,
- expected_output->x, expected_output->len);
- /* Compute with larger buffer, one-shot */
- PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
- output, sizeof(output),
- &output_length));
- TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
- ASSERT_COMPARE(output, output_length,
- expected_output->x, expected_output->len);
- /* Compute with larger buffer, multi-part */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
- PSA_ASSERT(psa_hash_finish(&operation, output,
- sizeof(output), &output_length));
- TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
- ASSERT_COMPARE(output, output_length,
- expected_output->x, expected_output->len);
- /* Compare with correct hash, one-shot */
- PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
- output, output_length));
- /* Compare with correct hash, multi-part */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
- PSA_ASSERT(psa_hash_verify(&operation, output,
- output_length));
- /* Compare with trailing garbage, one-shot */
- TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
- output, output_length + 1),
- PSA_ERROR_INVALID_SIGNATURE);
- /* Compare with trailing garbage, multi-part */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
- TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
- PSA_ERROR_INVALID_SIGNATURE);
- /* Compare with truncated hash, one-shot */
- TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
- output, output_length - 1),
- PSA_ERROR_INVALID_SIGNATURE);
- /* Compare with truncated hash, multi-part */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
- TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
- PSA_ERROR_INVALID_SIGNATURE);
- /* Compare with corrupted value */
- for (i = 0; i < output_length; i++) {
- mbedtls_test_set_step(i);
- output[i] ^= 1;
- /* One-shot */
- TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
- output, output_length),
- PSA_ERROR_INVALID_SIGNATURE);
- /* Multi-Part */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
- TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
- PSA_ERROR_INVALID_SIGNATURE);
- output[i] ^= 1;
- }
- exit:
- PSA_ASSERT(psa_hash_abort(&operation));
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
- void hash_bad_order()
- {
- psa_algorithm_t alg = PSA_ALG_SHA_256;
- unsigned char input[] = "";
- /* SHA-256 hash of an empty string */
- const unsigned char valid_hash[] = {
- 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
- 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
- 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
- };
- unsigned char hash[sizeof(valid_hash)] = { 0 };
- size_t hash_len;
- psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
- PSA_ASSERT(psa_crypto_init());
- /* Call setup twice in a row. */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_hash_setup(&operation, alg),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_hash_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- /* Call update without calling setup beforehand. */
- TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_hash_abort(&operation));
- /* Check that update calls abort on error. */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- operation.id = UINT_MAX;
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_hash_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- /* Call update after finish. */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- PSA_ASSERT(psa_hash_finish(&operation,
- hash, sizeof(hash), &hash_len));
- TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_hash_abort(&operation));
- /* Call verify without calling setup beforehand. */
- TEST_EQUAL(psa_hash_verify(&operation,
- valid_hash, sizeof(valid_hash)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_hash_abort(&operation));
- /* Call verify after finish. */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- PSA_ASSERT(psa_hash_finish(&operation,
- hash, sizeof(hash), &hash_len));
- TEST_EQUAL(psa_hash_verify(&operation,
- valid_hash, sizeof(valid_hash)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_hash_abort(&operation));
- /* Call verify twice in a row. */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- ASSERT_OPERATION_IS_ACTIVE(operation);
- PSA_ASSERT(psa_hash_verify(&operation,
- valid_hash, sizeof(valid_hash)));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- TEST_EQUAL(psa_hash_verify(&operation,
- valid_hash, sizeof(valid_hash)),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_hash_abort(&operation));
- /* Call finish without calling setup beforehand. */
- TEST_EQUAL(psa_hash_finish(&operation,
- hash, sizeof(hash), &hash_len),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_hash_abort(&operation));
- /* Call finish twice in a row. */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- PSA_ASSERT(psa_hash_finish(&operation,
- hash, sizeof(hash), &hash_len));
- TEST_EQUAL(psa_hash_finish(&operation,
- hash, sizeof(hash), &hash_len),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_hash_abort(&operation));
- /* Call finish after calling verify. */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- PSA_ASSERT(psa_hash_verify(&operation,
- valid_hash, sizeof(valid_hash)));
- TEST_EQUAL(psa_hash_finish(&operation,
- hash, sizeof(hash), &hash_len),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_hash_abort(&operation));
- exit:
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
- void hash_verify_bad_args()
- {
- psa_algorithm_t alg = PSA_ALG_SHA_256;
- /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
- * appended to it */
- unsigned char hash[] = {
- 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
- 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
- 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
- };
- size_t expected_size = PSA_HASH_LENGTH(alg);
- psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
- PSA_ASSERT(psa_crypto_init());
- /* psa_hash_verify with a smaller hash than expected */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
- PSA_ERROR_INVALID_SIGNATURE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_hash_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- /* psa_hash_verify with a non-matching hash */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
- PSA_ERROR_INVALID_SIGNATURE);
- /* psa_hash_verify with a hash longer than expected */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
- PSA_ERROR_INVALID_SIGNATURE);
- exit:
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
- void hash_finish_bad_args()
- {
- psa_algorithm_t alg = PSA_ALG_SHA_256;
- unsigned char hash[PSA_HASH_MAX_SIZE];
- size_t expected_size = PSA_HASH_LENGTH(alg);
- psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
- size_t hash_len;
- PSA_ASSERT(psa_crypto_init());
- /* psa_hash_finish with a smaller hash buffer than expected */
- PSA_ASSERT(psa_hash_setup(&operation, alg));
- TEST_EQUAL(psa_hash_finish(&operation,
- hash, expected_size - 1, &hash_len),
- PSA_ERROR_BUFFER_TOO_SMALL);
- exit:
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
- void hash_clone_source_state()
- {
- psa_algorithm_t alg = PSA_ALG_SHA_256;
- unsigned char hash[PSA_HASH_MAX_SIZE];
- psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
- psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
- psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
- psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
- psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
- size_t hash_len;
- PSA_ASSERT(psa_crypto_init());
- PSA_ASSERT(psa_hash_setup(&op_source, alg));
- PSA_ASSERT(psa_hash_setup(&op_setup, alg));
- PSA_ASSERT(psa_hash_setup(&op_finished, alg));
- PSA_ASSERT(psa_hash_finish(&op_finished,
- hash, sizeof(hash), &hash_len));
- PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
- PSA_ASSERT(psa_hash_abort(&op_aborted));
- TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
- PSA_ASSERT(psa_hash_finish(&op_init,
- hash, sizeof(hash), &hash_len));
- PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
- PSA_ASSERT(psa_hash_finish(&op_finished,
- hash, sizeof(hash), &hash_len));
- PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
- PSA_ASSERT(psa_hash_finish(&op_aborted,
- hash, sizeof(hash), &hash_len));
- exit:
- psa_hash_abort(&op_source);
- psa_hash_abort(&op_init);
- psa_hash_abort(&op_setup);
- psa_hash_abort(&op_finished);
- psa_hash_abort(&op_aborted);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
- void hash_clone_target_state()
- {
- psa_algorithm_t alg = PSA_ALG_SHA_256;
- unsigned char hash[PSA_HASH_MAX_SIZE];
- psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
- psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
- psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
- psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
- psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
- size_t hash_len;
- PSA_ASSERT(psa_crypto_init());
- PSA_ASSERT(psa_hash_setup(&op_setup, alg));
- PSA_ASSERT(psa_hash_setup(&op_finished, alg));
- PSA_ASSERT(psa_hash_finish(&op_finished,
- hash, sizeof(hash), &hash_len));
- PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
- PSA_ASSERT(psa_hash_abort(&op_aborted));
- PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
- PSA_ASSERT(psa_hash_finish(&op_target,
- hash, sizeof(hash), &hash_len));
- TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
- PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
- PSA_ERROR_BAD_STATE);
- exit:
- psa_hash_abort(&op_target);
- psa_hash_abort(&op_init);
- psa_hash_abort(&op_setup);
- psa_hash_abort(&op_finished);
- psa_hash_abort(&op_aborted);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void mac_operation_init()
- {
- const uint8_t input[1] = { 0 };
- /* Test each valid way of initializing the object, except for `= {0}`, as
- * Clang 5 complains when `-Wmissing-field-initializers` is used, even
- * though it's OK by the C standard. We could test for this, but we'd need
- * to suppress the Clang warning for the test. */
- psa_mac_operation_t func = psa_mac_operation_init();
- psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
- psa_mac_operation_t zero;
- memset(&zero, 0, sizeof(zero));
- /* A freshly-initialized MAC operation should not be usable. */
- TEST_EQUAL(psa_mac_update(&func,
- input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_mac_update(&init,
- input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_mac_update(&zero,
- input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- /* A default MAC operation should be abortable without error. */
- PSA_ASSERT(psa_mac_abort(&func));
- PSA_ASSERT(psa_mac_abort(&init));
- PSA_ASSERT(psa_mac_abort(&zero));
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void mac_setup(int key_type_arg,
- data_t *key,
- int alg_arg,
- int expected_status_arg)
- {
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t expected_status = expected_status_arg;
- psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- #if defined(KNOWN_SUPPORTED_MAC_ALG)
- const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
- #endif
- PSA_ASSERT(psa_crypto_init());
- if (!exercise_mac_setup(key_type, key->x, key->len, alg,
- &operation, &status)) {
- goto exit;
- }
- TEST_EQUAL(status, expected_status);
- /* The operation object should be reusable. */
- #if defined(KNOWN_SUPPORTED_MAC_ALG)
- if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
- smoke_test_key_data,
- sizeof(smoke_test_key_data),
- KNOWN_SUPPORTED_MAC_ALG,
- &operation, &status)) {
- goto exit;
- }
- TEST_EQUAL(status, PSA_SUCCESS);
- #endif
- exit:
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
- void mac_bad_order()
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
- psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
- const uint8_t key_data[] = {
- 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
- 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
- 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
- };
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
- uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
- size_t sign_mac_length = 0;
- const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
- const uint8_t verify_mac[] = {
- 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
- 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
- 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
- };
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
- &key));
- /* Call update without calling setup beforehand. */
- TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_mac_abort(&operation));
- /* Call sign finish without calling setup beforehand. */
- TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
- &sign_mac_length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_mac_abort(&operation));
- /* Call verify finish without calling setup beforehand. */
- TEST_EQUAL(psa_mac_verify_finish(&operation,
- verify_mac, sizeof(verify_mac)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_mac_abort(&operation));
- /* Call setup twice in a row. */
- PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_mac_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- /* Call update after sign finish. */
- PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
- PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
- PSA_ASSERT(psa_mac_sign_finish(&operation,
- sign_mac, sizeof(sign_mac),
- &sign_mac_length));
- TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_mac_abort(&operation));
- /* Call update after verify finish. */
- PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
- PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
- PSA_ASSERT(psa_mac_verify_finish(&operation,
- verify_mac, sizeof(verify_mac)));
- TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_mac_abort(&operation));
- /* Call sign finish twice in a row. */
- PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
- PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
- PSA_ASSERT(psa_mac_sign_finish(&operation,
- sign_mac, sizeof(sign_mac),
- &sign_mac_length));
- TEST_EQUAL(psa_mac_sign_finish(&operation,
- sign_mac, sizeof(sign_mac),
- &sign_mac_length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_mac_abort(&operation));
- /* Call verify finish twice in a row. */
- PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
- PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
- PSA_ASSERT(psa_mac_verify_finish(&operation,
- verify_mac, sizeof(verify_mac)));
- TEST_EQUAL(psa_mac_verify_finish(&operation,
- verify_mac, sizeof(verify_mac)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_mac_abort(&operation));
- /* Setup sign but try verify. */
- PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
- PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_mac_verify_finish(&operation,
- verify_mac, sizeof(verify_mac)),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_mac_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- /* Setup verify but try sign. */
- PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
- PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_mac_sign_finish(&operation,
- sign_mac, sizeof(sign_mac),
- &sign_mac_length),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_mac_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_destroy_key(key));
- exit:
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void mac_sign_verify_multi(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *input,
- int is_verify,
- data_t *expected_mac)
- {
- size_t data_part_len = 0;
- for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
- /* Split data into length(data_part_len) parts. */
- mbedtls_test_set_step(2000 + data_part_len);
- if (mac_multipart_internal_func(key_type_arg, key_data,
- alg_arg,
- input, data_part_len,
- expected_mac,
- is_verify, 0) == 0) {
- break;
- }
- /* length(0) part, length(data_part_len) part, length(0) part... */
- mbedtls_test_set_step(3000 + data_part_len);
- if (mac_multipart_internal_func(key_type_arg, key_data,
- alg_arg,
- input, data_part_len,
- expected_mac,
- is_verify, 1) == 0) {
- break;
- }
- }
- /* Goto is required to silence warnings about unused labels, as we
- * don't actually do any test assertions in this function. */
- goto exit;
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void mac_sign(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *input,
- data_t *expected_mac)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- uint8_t *actual_mac = NULL;
- size_t mac_buffer_size =
- PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
- size_t mac_length = 0;
- const size_t output_sizes_to_test[] = {
- 0,
- 1,
- expected_mac->len - 1,
- expected_mac->len,
- expected_mac->len + 1,
- };
- TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
- /* We expect PSA_MAC_LENGTH to be exact. */
- TEST_ASSERT(expected_mac->len == mac_buffer_size);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
- const size_t output_size = output_sizes_to_test[i];
- psa_status_t expected_status =
- (output_size >= expected_mac->len ? PSA_SUCCESS :
- PSA_ERROR_BUFFER_TOO_SMALL);
- mbedtls_test_set_step(output_size);
- ASSERT_ALLOC(actual_mac, output_size);
- /* Calculate the MAC, one-shot case. */
- TEST_EQUAL(psa_mac_compute(key, alg,
- input->x, input->len,
- actual_mac, output_size, &mac_length),
- expected_status);
- if (expected_status == PSA_SUCCESS) {
- ASSERT_COMPARE(expected_mac->x, expected_mac->len,
- actual_mac, mac_length);
- }
- if (output_size > 0) {
- memset(actual_mac, 0, output_size);
- }
- /* Calculate the MAC, multi-part case. */
- PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
- PSA_ASSERT(psa_mac_update(&operation,
- input->x, input->len));
- TEST_EQUAL(psa_mac_sign_finish(&operation,
- actual_mac, output_size,
- &mac_length),
- expected_status);
- PSA_ASSERT(psa_mac_abort(&operation));
- if (expected_status == PSA_SUCCESS) {
- ASSERT_COMPARE(expected_mac->x, expected_mac->len,
- actual_mac, mac_length);
- }
- mbedtls_free(actual_mac);
- actual_mac = NULL;
- }
- exit:
- psa_mac_abort(&operation);
- psa_destroy_key(key);
- PSA_DONE();
- mbedtls_free(actual_mac);
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void mac_verify(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *input,
- data_t *expected_mac)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- uint8_t *perturbed_mac = NULL;
- TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- /* Verify correct MAC, one-shot case. */
- PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
- expected_mac->x, expected_mac->len));
- /* Verify correct MAC, multi-part case. */
- PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
- PSA_ASSERT(psa_mac_update(&operation,
- input->x, input->len));
- PSA_ASSERT(psa_mac_verify_finish(&operation,
- expected_mac->x,
- expected_mac->len));
- /* Test a MAC that's too short, one-shot case. */
- TEST_EQUAL(psa_mac_verify(key, alg,
- input->x, input->len,
- expected_mac->x,
- expected_mac->len - 1),
- PSA_ERROR_INVALID_SIGNATURE);
- /* Test a MAC that's too short, multi-part case. */
- PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
- PSA_ASSERT(psa_mac_update(&operation,
- input->x, input->len));
- TEST_EQUAL(psa_mac_verify_finish(&operation,
- expected_mac->x,
- expected_mac->len - 1),
- PSA_ERROR_INVALID_SIGNATURE);
- /* Test a MAC that's too long, one-shot case. */
- ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
- memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
- TEST_EQUAL(psa_mac_verify(key, alg,
- input->x, input->len,
- perturbed_mac, expected_mac->len + 1),
- PSA_ERROR_INVALID_SIGNATURE);
- /* Test a MAC that's too long, multi-part case. */
- PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
- PSA_ASSERT(psa_mac_update(&operation,
- input->x, input->len));
- TEST_EQUAL(psa_mac_verify_finish(&operation,
- perturbed_mac,
- expected_mac->len + 1),
- PSA_ERROR_INVALID_SIGNATURE);
- /* Test changing one byte. */
- for (size_t i = 0; i < expected_mac->len; i++) {
- mbedtls_test_set_step(i);
- perturbed_mac[i] ^= 1;
- TEST_EQUAL(psa_mac_verify(key, alg,
- input->x, input->len,
- perturbed_mac, expected_mac->len),
- PSA_ERROR_INVALID_SIGNATURE);
- PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
- PSA_ASSERT(psa_mac_update(&operation,
- input->x, input->len));
- TEST_EQUAL(psa_mac_verify_finish(&operation,
- perturbed_mac,
- expected_mac->len),
- PSA_ERROR_INVALID_SIGNATURE);
- perturbed_mac[i] ^= 1;
- }
- exit:
- psa_mac_abort(&operation);
- psa_destroy_key(key);
- PSA_DONE();
- mbedtls_free(perturbed_mac);
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_operation_init()
- {
- const uint8_t input[1] = { 0 };
- unsigned char output[1] = { 0 };
- size_t output_length;
- /* Test each valid way of initializing the object, except for `= {0}`, as
- * Clang 5 complains when `-Wmissing-field-initializers` is used, even
- * though it's OK by the C standard. We could test for this, but we'd need
- * to suppress the Clang warning for the test. */
- psa_cipher_operation_t func = psa_cipher_operation_init();
- psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
- psa_cipher_operation_t zero;
- memset(&zero, 0, sizeof(zero));
- /* A freshly-initialized cipher operation should not be usable. */
- TEST_EQUAL(psa_cipher_update(&func,
- input, sizeof(input),
- output, sizeof(output),
- &output_length),
- PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_cipher_update(&init,
- input, sizeof(input),
- output, sizeof(output),
- &output_length),
- PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_cipher_update(&zero,
- input, sizeof(input),
- output, sizeof(output),
- &output_length),
- PSA_ERROR_BAD_STATE);
- /* A default cipher operation should be abortable without error. */
- PSA_ASSERT(psa_cipher_abort(&func));
- PSA_ASSERT(psa_cipher_abort(&init));
- PSA_ASSERT(psa_cipher_abort(&zero));
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_setup(int key_type_arg,
- data_t *key,
- int alg_arg,
- int expected_status_arg)
- {
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t expected_status = expected_status_arg;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- psa_status_t status;
- #if defined(KNOWN_SUPPORTED_CIPHER_ALG)
- const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
- #endif
- PSA_ASSERT(psa_crypto_init());
- if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
- &operation, &status)) {
- goto exit;
- }
- TEST_EQUAL(status, expected_status);
- /* The operation object should be reusable. */
- #if defined(KNOWN_SUPPORTED_CIPHER_ALG)
- if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
- smoke_test_key_data,
- sizeof(smoke_test_key_data),
- KNOWN_SUPPORTED_CIPHER_ALG,
- &operation, &status)) {
- goto exit;
- }
- TEST_EQUAL(status, PSA_SUCCESS);
- #endif
- exit:
- psa_cipher_abort(&operation);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
- void cipher_bad_order()
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = PSA_KEY_TYPE_AES;
- psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
- const uint8_t key_data[] = {
- 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
- 0xaa, 0xaa, 0xaa, 0xaa
- };
- const uint8_t text[] = {
- 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
- 0xbb, 0xbb, 0xbb, 0xbb
- };
- uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
- size_t length = 0;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
- &key));
- /* Call encrypt setup twice in a row. */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_cipher_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- /* Call decrypt setup twice in a row. */
- PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_cipher_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- /* Generate an IV without calling setup beforehand. */
- TEST_EQUAL(psa_cipher_generate_iv(&operation,
- buffer, sizeof(buffer),
- &length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_cipher_abort(&operation));
- /* Generate an IV twice in a row. */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_cipher_generate_iv(&operation,
- buffer, sizeof(buffer),
- &length));
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_cipher_generate_iv(&operation,
- buffer, sizeof(buffer),
- &length),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_cipher_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- /* Generate an IV after it's already set. */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_cipher_set_iv(&operation,
- iv, sizeof(iv)));
- TEST_EQUAL(psa_cipher_generate_iv(&operation,
- buffer, sizeof(buffer),
- &length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_cipher_abort(&operation));
- /* Set an IV without calling setup beforehand. */
- TEST_EQUAL(psa_cipher_set_iv(&operation,
- iv, sizeof(iv)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_cipher_abort(&operation));
- /* Set an IV after it's already set. */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_cipher_set_iv(&operation,
- iv, sizeof(iv)));
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_cipher_set_iv(&operation,
- iv, sizeof(iv)),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_cipher_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- /* Set an IV after it's already generated. */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_cipher_generate_iv(&operation,
- buffer, sizeof(buffer),
- &length));
- TEST_EQUAL(psa_cipher_set_iv(&operation,
- iv, sizeof(iv)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_cipher_abort(&operation));
- /* Call update without calling setup beforehand. */
- TEST_EQUAL(psa_cipher_update(&operation,
- text, sizeof(text),
- buffer, sizeof(buffer),
- &length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_cipher_abort(&operation));
- /* Call update without an IV where an IV is required. */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_cipher_update(&operation,
- text, sizeof(text),
- buffer, sizeof(buffer),
- &length),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_cipher_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- /* Call update after finish. */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_cipher_set_iv(&operation,
- iv, sizeof(iv)));
- PSA_ASSERT(psa_cipher_finish(&operation,
- buffer, sizeof(buffer), &length));
- TEST_EQUAL(psa_cipher_update(&operation,
- text, sizeof(text),
- buffer, sizeof(buffer),
- &length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_cipher_abort(&operation));
- /* Call finish without calling setup beforehand. */
- TEST_EQUAL(psa_cipher_finish(&operation,
- buffer, sizeof(buffer), &length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_cipher_abort(&operation));
- /* Call finish without an IV where an IV is required. */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- /* Not calling update means we are encrypting an empty buffer, which is OK
- * for cipher modes with padding. */
- ASSERT_OPERATION_IS_ACTIVE(operation);
- TEST_EQUAL(psa_cipher_finish(&operation,
- buffer, sizeof(buffer), &length),
- PSA_ERROR_BAD_STATE);
- ASSERT_OPERATION_IS_INACTIVE(operation);
- PSA_ASSERT(psa_cipher_abort(&operation));
- ASSERT_OPERATION_IS_INACTIVE(operation);
- /* Call finish twice in a row. */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_cipher_set_iv(&operation,
- iv, sizeof(iv)));
- PSA_ASSERT(psa_cipher_finish(&operation,
- buffer, sizeof(buffer), &length));
- TEST_EQUAL(psa_cipher_finish(&operation,
- buffer, sizeof(buffer), &length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_cipher_abort(&operation));
- PSA_ASSERT(psa_destroy_key(key));
- exit:
- psa_cipher_abort(&operation);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_encrypt_fail(int alg_arg,
- int key_type_arg,
- data_t *key_data,
- data_t *input,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_status_t status;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t expected_status = expected_status_arg;
- unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
- size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
- size_t iv_length = 0;
- unsigned char *output = NULL;
- size_t output_buffer_size = 0;
- size_t output_length = 0;
- size_t function_output_length;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- if (PSA_ERROR_BAD_STATE != expected_status) {
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
- input->len);
- ASSERT_ALLOC(output, output_buffer_size);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- }
- /* Encrypt, one-shot */
- status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
- output_buffer_size, &output_length);
- TEST_EQUAL(status, expected_status);
- /* Encrypt, multi-part */
- status = psa_cipher_encrypt_setup(&operation, key, alg);
- if (status == PSA_SUCCESS) {
- if (alg != PSA_ALG_ECB_NO_PADDING) {
- PSA_ASSERT(psa_cipher_generate_iv(&operation,
- iv, iv_size,
- &iv_length));
- }
- status = psa_cipher_update(&operation, input->x, input->len,
- output, output_buffer_size,
- &function_output_length);
- if (status == PSA_SUCCESS) {
- output_length += function_output_length;
- status = psa_cipher_finish(&operation, output + output_length,
- output_buffer_size - output_length,
- &function_output_length);
- TEST_EQUAL(status, expected_status);
- } else {
- TEST_EQUAL(status, expected_status);
- }
- } else {
- TEST_EQUAL(status, expected_status);
- }
- exit:
- psa_cipher_abort(&operation);
- mbedtls_free(output);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
- data_t *input, int iv_length,
- int expected_result)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- size_t output_buffer_size = 0;
- unsigned char *output = NULL;
- output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
- ASSERT_ALLOC(output, output_buffer_size);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
- iv_length));
- exit:
- psa_cipher_abort(&operation);
- mbedtls_free(output);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
- data_t *plaintext, data_t *ciphertext)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- uint8_t iv[1] = { 0x5a };
- unsigned char *output = NULL;
- size_t output_buffer_size = 0;
- size_t output_length, length;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- /* Validate size macros */
- TEST_LE_U(ciphertext->len,
- PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
- TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
- PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
- TEST_LE_U(plaintext->len,
- PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
- TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
- PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
- /* Set up key and output buffer */
- psa_set_key_usage_flags(&attributes,
- PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
- plaintext->len);
- ASSERT_ALLOC(output, output_buffer_size);
- /* set_iv() is not allowed */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
- PSA_ERROR_BAD_STATE);
- /* generate_iv() is not allowed */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
- &length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
- &length),
- PSA_ERROR_BAD_STATE);
- /* Multipart encryption */
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- output_length = 0;
- length = ~0;
- PSA_ASSERT(psa_cipher_update(&operation,
- plaintext->x, plaintext->len,
- output, output_buffer_size,
- &length));
- TEST_LE_U(length, output_buffer_size);
- output_length += length;
- PSA_ASSERT(psa_cipher_finish(&operation,
- mbedtls_buffer_offset(output, output_length),
- output_buffer_size - output_length,
- &length));
- output_length += length;
- ASSERT_COMPARE(ciphertext->x, ciphertext->len,
- output, output_length);
- /* Multipart encryption */
- PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
- output_length = 0;
- length = ~0;
- PSA_ASSERT(psa_cipher_update(&operation,
- ciphertext->x, ciphertext->len,
- output, output_buffer_size,
- &length));
- TEST_LE_U(length, output_buffer_size);
- output_length += length;
- PSA_ASSERT(psa_cipher_finish(&operation,
- mbedtls_buffer_offset(output, output_length),
- output_buffer_size - output_length,
- &length));
- output_length += length;
- ASSERT_COMPARE(plaintext->x, plaintext->len,
- output, output_length);
- /* One-shot encryption */
- output_length = ~0;
- PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
- output, output_buffer_size,
- &output_length));
- ASSERT_COMPARE(ciphertext->x, ciphertext->len,
- output, output_length);
- /* One-shot decryption */
- output_length = ~0;
- PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
- output, output_buffer_size,
- &output_length));
- ASSERT_COMPARE(plaintext->x, plaintext->len,
- output, output_length);
- exit:
- PSA_ASSERT(psa_cipher_abort(&operation));
- mbedtls_free(output);
- psa_cipher_abort(&operation);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_algorithm_t alg = alg_arg;
- psa_key_type_t key_type = key_type_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- psa_status_t status;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- /* Usage of either of these two size macros would cause divide by zero
- * with incorrect key types previously. Input length should be irrelevant
- * here. */
- TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
- 0);
- TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- /* Should fail due to invalid alg type (to support invalid key type).
- * Encrypt or decrypt will end up in the same place. */
- status = psa_cipher_encrypt_setup(&operation, key, alg);
- TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
- exit:
- psa_cipher_abort(&operation);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_encrypt_validation(int alg_arg,
- int key_type_arg,
- data_t *key_data,
- data_t *input)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
- unsigned char *output1 = NULL;
- size_t output1_buffer_size = 0;
- size_t output1_length = 0;
- unsigned char *output2 = NULL;
- size_t output2_buffer_size = 0;
- size_t output2_length = 0;
- size_t function_output_length = 0;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
- output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
- PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
- ASSERT_ALLOC(output1, output1_buffer_size);
- ASSERT_ALLOC(output2, output2_buffer_size);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- /* The one-shot cipher encryption uses generated iv so validating
- the output is not possible. Validating with multipart encryption. */
- PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
- output1_buffer_size, &output1_length));
- TEST_LE_U(output1_length,
- PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
- TEST_LE_U(output1_length,
- PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
- PSA_ASSERT(psa_cipher_update(&operation,
- input->x, input->len,
- output2, output2_buffer_size,
- &function_output_length));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
- output2_length += function_output_length;
- PSA_ASSERT(psa_cipher_finish(&operation,
- output2 + output2_length,
- output2_buffer_size - output2_length,
- &function_output_length));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
- output2_length += function_output_length;
- PSA_ASSERT(psa_cipher_abort(&operation));
- ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
- output2, output2_length);
- exit:
- psa_cipher_abort(&operation);
- mbedtls_free(output1);
- mbedtls_free(output2);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
- data_t *key_data, data_t *iv,
- data_t *input,
- int first_part_size_arg,
- int output1_length_arg, int output2_length_arg,
- data_t *expected_output,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t status;
- psa_status_t expected_status = expected_status_arg;
- size_t first_part_size = first_part_size_arg;
- size_t output1_length = output1_length_arg;
- size_t output2_length = output2_length_arg;
- unsigned char *output = NULL;
- size_t output_buffer_size = 0;
- size_t function_output_length = 0;
- size_t total_output_length = 0;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
- if (iv->len > 0) {
- PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
- }
- output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
- PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
- ASSERT_ALLOC(output, output_buffer_size);
- TEST_LE_U(first_part_size, input->len);
- PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
- output, output_buffer_size,
- &function_output_length));
- TEST_ASSERT(function_output_length == output1_length);
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
- total_output_length += function_output_length;
- if (first_part_size < input->len) {
- PSA_ASSERT(psa_cipher_update(&operation,
- input->x + first_part_size,
- input->len - first_part_size,
- (output_buffer_size == 0 ? NULL :
- output + total_output_length),
- output_buffer_size - total_output_length,
- &function_output_length));
- TEST_ASSERT(function_output_length == output2_length);
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
- alg,
- input->len - first_part_size));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
- total_output_length += function_output_length;
- }
- status = psa_cipher_finish(&operation,
- (output_buffer_size == 0 ? NULL :
- output + total_output_length),
- output_buffer_size - total_output_length,
- &function_output_length);
- TEST_LE_U(function_output_length,
- PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
- total_output_length += function_output_length;
- TEST_EQUAL(status, expected_status);
- if (expected_status == PSA_SUCCESS) {
- PSA_ASSERT(psa_cipher_abort(&operation));
- ASSERT_COMPARE(expected_output->x, expected_output->len,
- output, total_output_length);
- }
- exit:
- psa_cipher_abort(&operation);
- mbedtls_free(output);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
- data_t *key_data, data_t *iv,
- data_t *input,
- int first_part_size_arg,
- int output1_length_arg, int output2_length_arg,
- data_t *expected_output,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t status;
- psa_status_t expected_status = expected_status_arg;
- size_t first_part_size = first_part_size_arg;
- size_t output1_length = output1_length_arg;
- size_t output2_length = output2_length_arg;
- unsigned char *output = NULL;
- size_t output_buffer_size = 0;
- size_t function_output_length = 0;
- size_t total_output_length = 0;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
- if (iv->len > 0) {
- PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
- }
- output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
- PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
- ASSERT_ALLOC(output, output_buffer_size);
- TEST_LE_U(first_part_size, input->len);
- PSA_ASSERT(psa_cipher_update(&operation,
- input->x, first_part_size,
- output, output_buffer_size,
- &function_output_length));
- TEST_ASSERT(function_output_length == output1_length);
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
- total_output_length += function_output_length;
- if (first_part_size < input->len) {
- PSA_ASSERT(psa_cipher_update(&operation,
- input->x + first_part_size,
- input->len - first_part_size,
- (output_buffer_size == 0 ? NULL :
- output + total_output_length),
- output_buffer_size - total_output_length,
- &function_output_length));
- TEST_ASSERT(function_output_length == output2_length);
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
- alg,
- input->len - first_part_size));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
- total_output_length += function_output_length;
- }
- status = psa_cipher_finish(&operation,
- (output_buffer_size == 0 ? NULL :
- output + total_output_length),
- output_buffer_size - total_output_length,
- &function_output_length);
- TEST_LE_U(function_output_length,
- PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
- total_output_length += function_output_length;
- TEST_EQUAL(status, expected_status);
- if (expected_status == PSA_SUCCESS) {
- PSA_ASSERT(psa_cipher_abort(&operation));
- ASSERT_COMPARE(expected_output->x, expected_output->len,
- output, total_output_length);
- }
- exit:
- psa_cipher_abort(&operation);
- mbedtls_free(output);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_decrypt_fail(int alg_arg,
- int key_type_arg,
- data_t *key_data,
- data_t *iv,
- data_t *input_arg,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_status_t status;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t expected_status = expected_status_arg;
- unsigned char *input = NULL;
- size_t input_buffer_size = 0;
- unsigned char *output = NULL;
- unsigned char *output_multi = NULL;
- size_t output_buffer_size = 0;
- size_t output_length = 0;
- size_t function_output_length;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- if (PSA_ERROR_BAD_STATE != expected_status) {
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- }
- /* Allocate input buffer and copy the iv and the plaintext */
- input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
- if (input_buffer_size > 0) {
- ASSERT_ALLOC(input, input_buffer_size);
- memcpy(input, iv->x, iv->len);
- memcpy(input + iv->len, input_arg->x, input_arg->len);
- }
- output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
- ASSERT_ALLOC(output, output_buffer_size);
- /* Decrypt, one-short */
- status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
- output_buffer_size, &output_length);
- TEST_EQUAL(status, expected_status);
- /* Decrypt, multi-part */
- status = psa_cipher_decrypt_setup(&operation, key, alg);
- if (status == PSA_SUCCESS) {
- output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
- input_arg->len) +
- PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
- ASSERT_ALLOC(output_multi, output_buffer_size);
- if (iv->len > 0) {
- status = psa_cipher_set_iv(&operation, iv->x, iv->len);
- if (status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_status);
- }
- }
- if (status == PSA_SUCCESS) {
- status = psa_cipher_update(&operation,
- input_arg->x, input_arg->len,
- output_multi, output_buffer_size,
- &function_output_length);
- if (status == PSA_SUCCESS) {
- output_length = function_output_length;
- status = psa_cipher_finish(&operation,
- output_multi + output_length,
- output_buffer_size - output_length,
- &function_output_length);
- TEST_EQUAL(status, expected_status);
- } else {
- TEST_EQUAL(status, expected_status);
- }
- } else {
- TEST_EQUAL(status, expected_status);
- }
- } else {
- TEST_EQUAL(status, expected_status);
- }
- exit:
- psa_cipher_abort(&operation);
- mbedtls_free(input);
- mbedtls_free(output);
- mbedtls_free(output_multi);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_decrypt(int alg_arg,
- int key_type_arg,
- data_t *key_data,
- data_t *iv,
- data_t *input_arg,
- data_t *expected_output)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- unsigned char *input = NULL;
- size_t input_buffer_size = 0;
- unsigned char *output = NULL;
- size_t output_buffer_size = 0;
- size_t output_length = 0;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- /* Allocate input buffer and copy the iv and the plaintext */
- input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
- if (input_buffer_size > 0) {
- ASSERT_ALLOC(input, input_buffer_size);
- memcpy(input, iv->x, iv->len);
- memcpy(input + iv->len, input_arg->x, input_arg->len);
- }
- output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
- ASSERT_ALLOC(output, output_buffer_size);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
- output_buffer_size, &output_length));
- TEST_LE_U(output_length,
- PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
- TEST_LE_U(output_length,
- PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
- ASSERT_COMPARE(expected_output->x, expected_output->len,
- output, output_length);
- exit:
- mbedtls_free(input);
- mbedtls_free(output);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_verify_output(int alg_arg,
- int key_type_arg,
- data_t *key_data,
- data_t *input)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- unsigned char *output1 = NULL;
- size_t output1_size = 0;
- size_t output1_length = 0;
- unsigned char *output2 = NULL;
- size_t output2_size = 0;
- size_t output2_length = 0;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
- ASSERT_ALLOC(output1, output1_size);
- PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
- output1, output1_size,
- &output1_length));
- TEST_LE_U(output1_length,
- PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
- TEST_LE_U(output1_length,
- PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
- output2_size = output1_length;
- ASSERT_ALLOC(output2, output2_size);
- PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
- output2, output2_size,
- &output2_length));
- TEST_LE_U(output2_length,
- PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
- TEST_LE_U(output2_length,
- PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
- ASSERT_COMPARE(input->x, input->len, output2, output2_length);
- exit:
- mbedtls_free(output1);
- mbedtls_free(output2);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_verify_output_multipart(int alg_arg,
- int key_type_arg,
- data_t *key_data,
- data_t *input,
- int first_part_size_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t first_part_size = first_part_size_arg;
- unsigned char iv[16] = { 0 };
- size_t iv_size = 16;
- size_t iv_length = 0;
- unsigned char *output1 = NULL;
- size_t output1_buffer_size = 0;
- size_t output1_length = 0;
- unsigned char *output2 = NULL;
- size_t output2_buffer_size = 0;
- size_t output2_length = 0;
- size_t function_output_length;
- psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
- psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
- PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
- if (alg != PSA_ALG_ECB_NO_PADDING) {
- PSA_ASSERT(psa_cipher_generate_iv(&operation1,
- iv, iv_size,
- &iv_length));
- }
- output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
- TEST_LE_U(output1_buffer_size,
- PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
- ASSERT_ALLOC(output1, output1_buffer_size);
- TEST_LE_U(first_part_size, input->len);
- PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
- output1, output1_buffer_size,
- &function_output_length));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
- output1_length += function_output_length;
- PSA_ASSERT(psa_cipher_update(&operation1,
- input->x + first_part_size,
- input->len - first_part_size,
- output1, output1_buffer_size,
- &function_output_length));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
- alg,
- input->len - first_part_size));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
- output1_length += function_output_length;
- PSA_ASSERT(psa_cipher_finish(&operation1,
- output1 + output1_length,
- output1_buffer_size - output1_length,
- &function_output_length));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
- output1_length += function_output_length;
- PSA_ASSERT(psa_cipher_abort(&operation1));
- output2_buffer_size = output1_length;
- TEST_LE_U(output2_buffer_size,
- PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
- TEST_LE_U(output2_buffer_size,
- PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
- ASSERT_ALLOC(output2, output2_buffer_size);
- if (iv_length > 0) {
- PSA_ASSERT(psa_cipher_set_iv(&operation2,
- iv, iv_length));
- }
- PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
- output2, output2_buffer_size,
- &function_output_length));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
- output2_length += function_output_length;
- PSA_ASSERT(psa_cipher_update(&operation2,
- output1 + first_part_size,
- output1_length - first_part_size,
- output2, output2_buffer_size,
- &function_output_length));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
- alg,
- output1_length - first_part_size));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
- output2_length += function_output_length;
- PSA_ASSERT(psa_cipher_finish(&operation2,
- output2 + output2_length,
- output2_buffer_size - output2_length,
- &function_output_length));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
- TEST_LE_U(function_output_length,
- PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
- output2_length += function_output_length;
- PSA_ASSERT(psa_cipher_abort(&operation2));
- ASSERT_COMPARE(input->x, input->len, output2, output2_length);
- exit:
- psa_cipher_abort(&operation1);
- psa_cipher_abort(&operation2);
- mbedtls_free(output1);
- mbedtls_free(output2);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
- int alg_arg,
- data_t *nonce,
- data_t *additional_data,
- data_t *input_data,
- int expected_result_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *output_data = NULL;
- size_t output_size = 0;
- size_t output_length = 0;
- unsigned char *output_data2 = NULL;
- size_t output_length2 = 0;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- psa_status_t expected_result = expected_result_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
- alg);
- /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
- * should be exact. */
- if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
- expected_result != PSA_ERROR_NOT_SUPPORTED) {
- TEST_EQUAL(output_size,
- PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
- TEST_LE_U(output_size,
- PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
- }
- ASSERT_ALLOC(output_data, output_size);
- status = psa_aead_encrypt(key, alg,
- nonce->x, nonce->len,
- additional_data->x,
- additional_data->len,
- input_data->x, input_data->len,
- output_data, output_size,
- &output_length);
- /* If the operation is not supported, just skip and not fail in case the
- * encryption involves a common limitation of cryptography hardwares and
- * an alternative implementation. */
- if (status == PSA_ERROR_NOT_SUPPORTED) {
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
- }
- TEST_EQUAL(status, expected_result);
- if (PSA_SUCCESS == expected_result) {
- ASSERT_ALLOC(output_data2, output_length);
- /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
- * should be exact. */
- TEST_EQUAL(input_data->len,
- PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
- TEST_LE_U(input_data->len,
- PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
- TEST_EQUAL(psa_aead_decrypt(key, alg,
- nonce->x, nonce->len,
- additional_data->x,
- additional_data->len,
- output_data, output_length,
- output_data2, output_length,
- &output_length2),
- expected_result);
- ASSERT_COMPARE(input_data->x, input_data->len,
- output_data2, output_length2);
- }
- exit:
- psa_destroy_key(key);
- mbedtls_free(output_data);
- mbedtls_free(output_data2);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_encrypt(int key_type_arg, data_t *key_data,
- int alg_arg,
- data_t *nonce,
- data_t *additional_data,
- data_t *input_data,
- data_t *expected_result)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *output_data = NULL;
- size_t output_size = 0;
- size_t output_length = 0;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
- alg);
- /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
- * should be exact. */
- TEST_EQUAL(output_size,
- PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
- TEST_LE_U(output_size,
- PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
- ASSERT_ALLOC(output_data, output_size);
- status = psa_aead_encrypt(key, alg,
- nonce->x, nonce->len,
- additional_data->x, additional_data->len,
- input_data->x, input_data->len,
- output_data, output_size,
- &output_length);
- /* If the operation is not supported, just skip and not fail in case the
- * encryption involves a common limitation of cryptography hardwares and
- * an alternative implementation. */
- if (status == PSA_ERROR_NOT_SUPPORTED) {
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
- }
- PSA_ASSERT(status);
- ASSERT_COMPARE(expected_result->x, expected_result->len,
- output_data, output_length);
- exit:
- psa_destroy_key(key);
- mbedtls_free(output_data);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_decrypt(int key_type_arg, data_t *key_data,
- int alg_arg,
- data_t *nonce,
- data_t *additional_data,
- data_t *input_data,
- data_t *expected_data,
- int expected_result_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *output_data = NULL;
- size_t output_size = 0;
- size_t output_length = 0;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t expected_result = expected_result_arg;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
- alg);
- if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
- expected_result != PSA_ERROR_NOT_SUPPORTED) {
- /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
- * should be exact. */
- TEST_EQUAL(output_size,
- PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
- TEST_LE_U(output_size,
- PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
- }
- ASSERT_ALLOC(output_data, output_size);
- status = psa_aead_decrypt(key, alg,
- nonce->x, nonce->len,
- additional_data->x,
- additional_data->len,
- input_data->x, input_data->len,
- output_data, output_size,
- &output_length);
- /* If the operation is not supported, just skip and not fail in case the
- * decryption involves a common limitation of cryptography hardwares and
- * an alternative implementation. */
- if (status == PSA_ERROR_NOT_SUPPORTED) {
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
- }
- TEST_EQUAL(status, expected_result);
- if (expected_result == PSA_SUCCESS) {
- ASSERT_COMPARE(expected_data->x, expected_data->len,
- output_data, output_length);
- }
- exit:
- psa_destroy_key(key);
- mbedtls_free(output_data);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
- int alg_arg,
- data_t *nonce,
- data_t *additional_data,
- data_t *input_data,
- int do_set_lengths,
- data_t *expected_output)
- {
- size_t ad_part_len = 0;
- size_t data_part_len = 0;
- set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
- for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
- mbedtls_test_set_step(ad_part_len);
- if (do_set_lengths) {
- if (ad_part_len & 0x01) {
- set_lengths_method = SET_LENGTHS_AFTER_NONCE;
- } else {
- set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
- }
- }
- /* Split ad into length(ad_part_len) parts. */
- if (!aead_multipart_internal_func(key_type_arg, key_data,
- alg_arg, nonce,
- additional_data,
- ad_part_len,
- input_data, -1,
- set_lengths_method,
- expected_output,
- 1, 0)) {
- break;
- }
- /* length(0) part, length(ad_part_len) part, length(0) part... */
- mbedtls_test_set_step(1000 + ad_part_len);
- if (!aead_multipart_internal_func(key_type_arg, key_data,
- alg_arg, nonce,
- additional_data,
- ad_part_len,
- input_data, -1,
- set_lengths_method,
- expected_output,
- 1, 1)) {
- break;
- }
- }
- for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
- /* Split data into length(data_part_len) parts. */
- mbedtls_test_set_step(2000 + data_part_len);
- if (do_set_lengths) {
- if (data_part_len & 0x01) {
- set_lengths_method = SET_LENGTHS_AFTER_NONCE;
- } else {
- set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
- }
- }
- if (!aead_multipart_internal_func(key_type_arg, key_data,
- alg_arg, nonce,
- additional_data, -1,
- input_data, data_part_len,
- set_lengths_method,
- expected_output,
- 1, 0)) {
- break;
- }
- /* length(0) part, length(data_part_len) part, length(0) part... */
- mbedtls_test_set_step(3000 + data_part_len);
- if (!aead_multipart_internal_func(key_type_arg, key_data,
- alg_arg, nonce,
- additional_data, -1,
- input_data, data_part_len,
- set_lengths_method,
- expected_output,
- 1, 1)) {
- break;
- }
- }
- /* Goto is required to silence warnings about unused labels, as we
- * don't actually do any test assertions in this function. */
- goto exit;
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
- int alg_arg,
- data_t *nonce,
- data_t *additional_data,
- data_t *input_data,
- int do_set_lengths,
- data_t *expected_output)
- {
- size_t ad_part_len = 0;
- size_t data_part_len = 0;
- set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
- for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
- /* Split ad into length(ad_part_len) parts. */
- mbedtls_test_set_step(ad_part_len);
- if (do_set_lengths) {
- if (ad_part_len & 0x01) {
- set_lengths_method = SET_LENGTHS_AFTER_NONCE;
- } else {
- set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
- }
- }
- if (!aead_multipart_internal_func(key_type_arg, key_data,
- alg_arg, nonce,
- additional_data,
- ad_part_len,
- input_data, -1,
- set_lengths_method,
- expected_output,
- 0, 0)) {
- break;
- }
- /* length(0) part, length(ad_part_len) part, length(0) part... */
- mbedtls_test_set_step(1000 + ad_part_len);
- if (!aead_multipart_internal_func(key_type_arg, key_data,
- alg_arg, nonce,
- additional_data,
- ad_part_len,
- input_data, -1,
- set_lengths_method,
- expected_output,
- 0, 1)) {
- break;
- }
- }
- for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
- /* Split data into length(data_part_len) parts. */
- mbedtls_test_set_step(2000 + data_part_len);
- if (do_set_lengths) {
- if (data_part_len & 0x01) {
- set_lengths_method = SET_LENGTHS_AFTER_NONCE;
- } else {
- set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
- }
- }
- if (!aead_multipart_internal_func(key_type_arg, key_data,
- alg_arg, nonce,
- additional_data, -1,
- input_data, data_part_len,
- set_lengths_method,
- expected_output,
- 0, 0)) {
- break;
- }
- /* length(0) part, length(data_part_len) part, length(0) part... */
- mbedtls_test_set_step(3000 + data_part_len);
- if (!aead_multipart_internal_func(key_type_arg, key_data,
- alg_arg, nonce,
- additional_data, -1,
- input_data, data_part_len,
- set_lengths_method,
- expected_output,
- 0, 1)) {
- break;
- }
- }
- /* Goto is required to silence warnings about unused labels, as we
- * don't actually do any test assertions in this function. */
- goto exit;
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
- int alg_arg,
- int nonce_length,
- int expected_nonce_length_arg,
- data_t *additional_data,
- data_t *input_data,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
- uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- psa_status_t expected_status = expected_status_arg;
- size_t actual_nonce_length = 0;
- size_t expected_nonce_length = expected_nonce_length_arg;
- unsigned char *output = NULL;
- unsigned char *ciphertext = NULL;
- size_t output_size = 0;
- size_t ciphertext_size = 0;
- size_t ciphertext_length = 0;
- size_t tag_length = 0;
- uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
- ASSERT_ALLOC(output, output_size);
- ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
- TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
- ASSERT_ALLOC(ciphertext, ciphertext_size);
- status = psa_aead_encrypt_setup(&operation, key, alg);
- /* If the operation is not supported, just skip and not fail in case the
- * encryption involves a common limitation of cryptography hardwares and
- * an alternative implementation. */
- if (status == PSA_ERROR_NOT_SUPPORTED) {
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
- }
- PSA_ASSERT(status);
- status = psa_aead_generate_nonce(&operation, nonce_buffer,
- nonce_length,
- &actual_nonce_length);
- TEST_EQUAL(status, expected_status);
- TEST_EQUAL(actual_nonce_length, expected_nonce_length);
- if (expected_status == PSA_SUCCESS) {
- TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
- alg));
- }
- TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
- if (expected_status == PSA_SUCCESS) {
- /* Ensure we can still complete operation. */
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len));
- PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
- output, output_size,
- &ciphertext_length));
- PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
- &ciphertext_length, tag_buffer,
- PSA_AEAD_TAG_MAX_SIZE, &tag_length));
- }
- exit:
- psa_destroy_key(key);
- mbedtls_free(output);
- mbedtls_free(ciphertext);
- psa_aead_abort(&operation);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
- int alg_arg,
- int nonce_length_arg,
- int set_lengths_method_arg,
- data_t *additional_data,
- data_t *input_data,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
- uint8_t *nonce_buffer = NULL;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- psa_status_t expected_status = expected_status_arg;
- unsigned char *output = NULL;
- unsigned char *ciphertext = NULL;
- size_t nonce_length;
- size_t output_size = 0;
- size_t ciphertext_size = 0;
- size_t ciphertext_length = 0;
- size_t tag_length = 0;
- uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
- size_t index = 0;
- set_lengths_method_t set_lengths_method = set_lengths_method_arg;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
- ASSERT_ALLOC(output, output_size);
- ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
- TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
- ASSERT_ALLOC(ciphertext, ciphertext_size);
- status = psa_aead_encrypt_setup(&operation, key, alg);
- /* If the operation is not supported, just skip and not fail in case the
- * encryption involves a common limitation of cryptography hardwares and
- * an alternative implementation. */
- if (status == PSA_ERROR_NOT_SUPPORTED) {
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
- }
- PSA_ASSERT(status);
- /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
- if (nonce_length_arg == -1) {
- /* Arbitrary size buffer, to test zero length valid buffer. */
- ASSERT_ALLOC(nonce_buffer, 4);
- nonce_length = 0;
- } else {
- /* If length is zero, then this will return NULL. */
- nonce_length = (size_t) nonce_length_arg;
- ASSERT_ALLOC(nonce_buffer, nonce_length);
- if (nonce_buffer) {
- for (index = 0; index < nonce_length - 1; ++index) {
- nonce_buffer[index] = 'a' + index;
- }
- }
- }
- if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- }
- status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
- TEST_EQUAL(status, expected_status);
- if (expected_status == PSA_SUCCESS) {
- if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- }
- if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
- expected_status = PSA_ERROR_BAD_STATE;
- }
- /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
- TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len),
- expected_status);
- TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
- output, output_size,
- &ciphertext_length),
- expected_status);
- TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
- &ciphertext_length, tag_buffer,
- PSA_AEAD_TAG_MAX_SIZE, &tag_length),
- expected_status);
- }
- exit:
- psa_destroy_key(key);
- mbedtls_free(output);
- mbedtls_free(ciphertext);
- mbedtls_free(nonce_buffer);
- psa_aead_abort(&operation);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
- int alg_arg,
- int output_size_arg,
- data_t *nonce,
- data_t *additional_data,
- data_t *input_data,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- psa_status_t expected_status = expected_status_arg;
- unsigned char *output = NULL;
- unsigned char *ciphertext = NULL;
- size_t output_size = output_size_arg;
- size_t ciphertext_size = 0;
- size_t ciphertext_length = 0;
- size_t tag_length = 0;
- uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- ASSERT_ALLOC(output, output_size);
- ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
- ASSERT_ALLOC(ciphertext, ciphertext_size);
- status = psa_aead_encrypt_setup(&operation, key, alg);
- /* If the operation is not supported, just skip and not fail in case the
- * encryption involves a common limitation of cryptography hardwares and
- * an alternative implementation. */
- if (status == PSA_ERROR_NOT_SUPPORTED) {
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
- }
- PSA_ASSERT(status);
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len));
- status = psa_aead_update(&operation, input_data->x, input_data->len,
- output, output_size, &ciphertext_length);
- TEST_EQUAL(status, expected_status);
- if (expected_status == PSA_SUCCESS) {
- /* Ensure we can still complete operation. */
- PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
- &ciphertext_length, tag_buffer,
- PSA_AEAD_TAG_MAX_SIZE, &tag_length));
- }
- exit:
- psa_destroy_key(key);
- mbedtls_free(output);
- mbedtls_free(ciphertext);
- psa_aead_abort(&operation);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
- int alg_arg,
- int finish_ciphertext_size_arg,
- int tag_size_arg,
- data_t *nonce,
- data_t *additional_data,
- data_t *input_data,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- psa_status_t expected_status = expected_status_arg;
- unsigned char *ciphertext = NULL;
- unsigned char *finish_ciphertext = NULL;
- unsigned char *tag_buffer = NULL;
- size_t ciphertext_size = 0;
- size_t ciphertext_length = 0;
- size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
- size_t tag_size = (size_t) tag_size_arg;
- size_t tag_length = 0;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
- ASSERT_ALLOC(ciphertext, ciphertext_size);
- ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
- ASSERT_ALLOC(tag_buffer, tag_size);
- status = psa_aead_encrypt_setup(&operation, key, alg);
- /* If the operation is not supported, just skip and not fail in case the
- * encryption involves a common limitation of cryptography hardwares and
- * an alternative implementation. */
- if (status == PSA_ERROR_NOT_SUPPORTED) {
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
- }
- PSA_ASSERT(status);
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len));
- PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
- ciphertext, ciphertext_size, &ciphertext_length));
- /* Ensure we can still complete operation. */
- status = psa_aead_finish(&operation, finish_ciphertext,
- finish_ciphertext_size,
- &ciphertext_length, tag_buffer,
- tag_size, &tag_length);
- TEST_EQUAL(status, expected_status);
- exit:
- psa_destroy_key(key);
- mbedtls_free(ciphertext);
- mbedtls_free(finish_ciphertext);
- mbedtls_free(tag_buffer);
- psa_aead_abort(&operation);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_multipart_verify(int key_type_arg, data_t *key_data,
- int alg_arg,
- data_t *nonce,
- data_t *additional_data,
- data_t *input_data,
- data_t *tag,
- int tag_usage_arg,
- int expected_setup_status_arg,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- psa_status_t expected_status = expected_status_arg;
- psa_status_t expected_setup_status = expected_setup_status_arg;
- unsigned char *plaintext = NULL;
- unsigned char *finish_plaintext = NULL;
- size_t plaintext_size = 0;
- size_t plaintext_length = 0;
- size_t verify_plaintext_size = 0;
- tag_usage_method_t tag_usage = tag_usage_arg;
- unsigned char *tag_buffer = NULL;
- size_t tag_size = 0;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
- input_data->len);
- ASSERT_ALLOC(plaintext, plaintext_size);
- verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
- ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
- status = psa_aead_decrypt_setup(&operation, key, alg);
- /* If the operation is not supported, just skip and not fail in case the
- * encryption involves a common limitation of cryptography hardwares and
- * an alternative implementation. */
- if (status == PSA_ERROR_NOT_SUPPORTED) {
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
- MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
- }
- TEST_EQUAL(status, expected_setup_status);
- if (status != PSA_SUCCESS) {
- goto exit;
- }
- PSA_ASSERT(status);
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- status = psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len);
- PSA_ASSERT(status);
- PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len));
- PSA_ASSERT(psa_aead_update(&operation, input_data->x,
- input_data->len,
- plaintext, plaintext_size,
- &plaintext_length));
- if (tag_usage == USE_GIVEN_TAG) {
- tag_buffer = tag->x;
- tag_size = tag->len;
- }
- status = psa_aead_verify(&operation, finish_plaintext,
- verify_plaintext_size,
- &plaintext_length,
- tag_buffer, tag_size);
- TEST_EQUAL(status, expected_status);
- exit:
- psa_destroy_key(key);
- mbedtls_free(plaintext);
- mbedtls_free(finish_plaintext);
- psa_aead_abort(&operation);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_multipart_setup(int key_type_arg, data_t *key_data,
- int alg_arg, int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_ERROR_GENERIC_ERROR;
- psa_status_t expected_status = expected_status_arg;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes,
- PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- status = psa_aead_encrypt_setup(&operation, key, alg);
- TEST_EQUAL(status, expected_status);
- psa_aead_abort(&operation);
- status = psa_aead_decrypt_setup(&operation, key, alg);
- TEST_EQUAL(status, expected_status);
- exit:
- psa_destroy_key(key);
- psa_aead_abort(&operation);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void aead_multipart_state_test(int key_type_arg, data_t *key_data,
- int alg_arg,
- data_t *nonce,
- data_t *additional_data,
- data_t *input_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
- unsigned char *output_data = NULL;
- unsigned char *final_data = NULL;
- size_t output_size = 0;
- size_t finish_output_size = 0;
- size_t output_length = 0;
- size_t key_bits = 0;
- size_t tag_length = 0;
- size_t tag_size = 0;
- size_t nonce_length = 0;
- uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
- uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
- size_t output_part_length = 0;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes,
- PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
- TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
- output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
- ASSERT_ALLOC(output_data, output_size);
- finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
- TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
- ASSERT_ALLOC(final_data, finish_output_size);
- /* Test all operations error without calling setup first. */
- TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- TEST_EQUAL(psa_aead_update(&operation, input_data->x,
- input_data->len, output_data,
- output_size, &output_length),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- TEST_EQUAL(psa_aead_finish(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer, tag_length,
- &tag_size),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- TEST_EQUAL(psa_aead_verify(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer,
- tag_length),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* Test for double setups. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* Test for not setting a nonce. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_aead_update(&operation, input_data->x,
- input_data->len, output_data,
- output_size, &output_length),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_aead_finish(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer, tag_length,
- &tag_size),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_aead_verify(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer,
- tag_length),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* Test for double setting nonce. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* Test for double generating nonce. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length));
- TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* Test for generate nonce then set and vice versa */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length));
- TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* Test for generating nonce after calling set lengths */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length));
- psa_aead_abort(&operation);
- /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- if (operation.alg == PSA_ALG_CCM) {
- TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
- input_data->len),
- PSA_ERROR_INVALID_ARGUMENT);
- TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length),
- PSA_ERROR_BAD_STATE);
- } else {
- PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
- input_data->len));
- PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length));
- }
- psa_aead_abort(&operation);
- /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
- #if SIZE_MAX > UINT32_MAX
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
- TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
- input_data->len),
- PSA_ERROR_INVALID_ARGUMENT);
- TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length),
- PSA_ERROR_BAD_STATE);
- } else {
- PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
- input_data->len));
- PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length));
- }
- psa_aead_abort(&operation);
- #endif
- /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length));
- if (operation.alg == PSA_ALG_CCM) {
- TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
- input_data->len),
- PSA_ERROR_INVALID_ARGUMENT);
- } else {
- PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
- input_data->len));
- }
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- /* Test for setting nonce after calling set lengths */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- psa_aead_abort(&operation);
- /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- if (operation.alg == PSA_ALG_CCM) {
- TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
- input_data->len),
- PSA_ERROR_INVALID_ARGUMENT);
- TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
- PSA_ERROR_BAD_STATE);
- } else {
- PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
- input_data->len));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- }
- psa_aead_abort(&operation);
- /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
- #if SIZE_MAX > UINT32_MAX
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
- TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
- input_data->len),
- PSA_ERROR_INVALID_ARGUMENT);
- TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
- PSA_ERROR_BAD_STATE);
- } else {
- PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
- input_data->len));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- }
- psa_aead_abort(&operation);
- #endif
- /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- if (operation.alg == PSA_ALG_CCM) {
- TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
- input_data->len),
- PSA_ERROR_INVALID_ARGUMENT);
- } else {
- PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
- input_data->len));
- }
- psa_aead_abort(&operation);
- /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
- #if SIZE_MAX > UINT32_MAX
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- if (operation.alg == PSA_ALG_GCM) {
- TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
- SIZE_MAX),
- PSA_ERROR_INVALID_ARGUMENT);
- TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
- PSA_ERROR_BAD_STATE);
- } else if (operation.alg != PSA_ALG_CCM) {
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- SIZE_MAX));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- }
- psa_aead_abort(&operation);
- #endif
- /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
- #if SIZE_MAX > UINT32_MAX
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- if (operation.alg == PSA_ALG_GCM) {
- TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
- SIZE_MAX),
- PSA_ERROR_INVALID_ARGUMENT);
- } else if (operation.alg != PSA_ALG_CCM) {
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- SIZE_MAX));
- }
- psa_aead_abort(&operation);
- #endif
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* Test for generating nonce in decrypt setup. */
- PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
- TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* Test for setting lengths twice. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* Test for setting lengths after setting nonce + already starting data. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- if (operation.alg == PSA_ALG_CCM) {
- TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len),
- PSA_ERROR_BAD_STATE);
- } else {
- PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len));
- TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len),
- PSA_ERROR_BAD_STATE);
- }
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- if (operation.alg == PSA_ALG_CCM) {
- TEST_EQUAL(psa_aead_update(&operation, input_data->x,
- input_data->len, output_data,
- output_size, &output_length),
- PSA_ERROR_BAD_STATE);
- } else {
- PSA_ASSERT(psa_aead_update(&operation, input_data->x,
- input_data->len, output_data,
- output_size, &output_length));
- TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len),
- PSA_ERROR_BAD_STATE);
- }
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- if (operation.alg == PSA_ALG_CCM) {
- PSA_ASSERT(psa_aead_finish(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer, tag_length,
- &tag_size));
- } else {
- PSA_ASSERT(psa_aead_finish(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer, tag_length,
- &tag_size));
- TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len),
- PSA_ERROR_BAD_STATE);
- }
- psa_aead_abort(&operation);
- /* Test for setting lengths after generating nonce + already starting data. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length));
- if (operation.alg == PSA_ALG_CCM) {
- TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len),
- PSA_ERROR_BAD_STATE);
- } else {
- PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len));
- TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len),
- PSA_ERROR_BAD_STATE);
- }
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length));
- if (operation.alg == PSA_ALG_CCM) {
- TEST_EQUAL(psa_aead_update(&operation, input_data->x,
- input_data->len, output_data,
- output_size, &output_length),
- PSA_ERROR_BAD_STATE);
- } else {
- PSA_ASSERT(psa_aead_update(&operation, input_data->x,
- input_data->len, output_data,
- output_size, &output_length));
- TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len),
- PSA_ERROR_BAD_STATE);
- }
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
- PSA_AEAD_NONCE_MAX_SIZE,
- &nonce_length));
- if (operation.alg == PSA_ALG_CCM) {
- PSA_ASSERT(psa_aead_finish(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer, tag_length,
- &tag_size));
- } else {
- PSA_ASSERT(psa_aead_finish(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer, tag_length,
- &tag_size));
- TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len),
- PSA_ERROR_BAD_STATE);
- }
- psa_aead_abort(&operation);
- /* Test for not sending any additional data or data after setting non zero
- * lengths for them. (encrypt) */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- TEST_EQUAL(psa_aead_finish(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer, tag_length,
- &tag_size),
- PSA_ERROR_INVALID_ARGUMENT);
- psa_aead_abort(&operation);
- /* Test for not sending any additional data or data after setting non-zero
- * lengths for them. (decrypt) */
- PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- TEST_EQUAL(psa_aead_verify(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer,
- tag_length),
- PSA_ERROR_INVALID_ARGUMENT);
- psa_aead_abort(&operation);
- /* Test for not sending any additional data after setting a non-zero length
- * for it. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- TEST_EQUAL(psa_aead_update(&operation, input_data->x,
- input_data->len, output_data,
- output_size, &output_length),
- PSA_ERROR_INVALID_ARGUMENT);
- psa_aead_abort(&operation);
- /* Test for not sending any data after setting a non-zero length for it.*/
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len));
- TEST_EQUAL(psa_aead_finish(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer, tag_length,
- &tag_size),
- PSA_ERROR_INVALID_ARGUMENT);
- psa_aead_abort(&operation);
- /* Test for sending too much additional data after setting lengths. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
- TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len),
- PSA_ERROR_INVALID_ARGUMENT);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len));
- TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
- 1),
- PSA_ERROR_INVALID_ARGUMENT);
- psa_aead_abort(&operation);
- /* Test for sending too much data after setting lengths. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
- TEST_EQUAL(psa_aead_update(&operation, input_data->x,
- input_data->len, output_data,
- output_size, &output_length),
- PSA_ERROR_INVALID_ARGUMENT);
- psa_aead_abort(&operation);
- /* ------------------------------------------------------- */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
- input_data->len));
- PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len));
- PSA_ASSERT(psa_aead_update(&operation, input_data->x,
- input_data->len, output_data,
- output_size, &output_length));
- TEST_EQUAL(psa_aead_update(&operation, input_data->x,
- 1, output_data,
- output_size, &output_length),
- PSA_ERROR_INVALID_ARGUMENT);
- psa_aead_abort(&operation);
- /* Test sending additional data after data. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- if (operation.alg != PSA_ALG_CCM) {
- PSA_ASSERT(psa_aead_update(&operation, input_data->x,
- input_data->len, output_data,
- output_size, &output_length));
- TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
- additional_data->len),
- PSA_ERROR_BAD_STATE);
- }
- psa_aead_abort(&operation);
- /* Test calling finish on decryption. */
- PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- TEST_EQUAL(psa_aead_finish(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer, tag_length,
- &tag_size),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- /* Test calling verify on encryption. */
- PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
- PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
- TEST_EQUAL(psa_aead_verify(&operation, final_data,
- finish_output_size,
- &output_part_length,
- tag_buffer,
- tag_length),
- PSA_ERROR_BAD_STATE);
- psa_aead_abort(&operation);
- exit:
- psa_destroy_key(key);
- psa_aead_abort(&operation);
- mbedtls_free(output_data);
- mbedtls_free(final_data);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void signature_size(int type_arg,
- int bits,
- int alg_arg,
- int expected_size_arg)
- {
- psa_key_type_t type = type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
- TEST_EQUAL(actual_size, (size_t) expected_size_arg);
- exit:
- ;
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void sign_hash_deterministic(int key_type_arg, data_t *key_data,
- int alg_arg, data_t *input_data,
- data_t *output_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *signature = NULL;
- size_t signature_size;
- size_t signature_length = 0xdeadbeef;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- /* Allocate a buffer which has the size advertised by the
- * library. */
- signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
- key_bits, alg);
- TEST_ASSERT(signature_size != 0);
- TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
- ASSERT_ALLOC(signature, signature_size);
- /* Perform the signature. */
- PSA_ASSERT(psa_sign_hash(key, alg,
- input_data->x, input_data->len,
- signature, signature_size,
- &signature_length));
- /* Verify that the signature is what is expected. */
- ASSERT_COMPARE(output_data->x, output_data->len,
- signature, signature_length);
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
- /**
- * sign_hash_interruptible() test intentions:
- *
- * Note: This test can currently only handle ECDSA.
- *
- * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
- * and private keys / keypairs only).
- *
- * 2. Test the number of calls to psa_sign_hash_complete() required are as
- * expected for different max_ops values.
- *
- * 3. Test that the number of ops done prior to start and after abort is zero
- * and that each successful stage completes some ops (this is not mandated by
- * the PSA specification, but is currently the case).
- *
- * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
- * complete() calls does not alter the number of ops returned.
- */
- void sign_hash_interruptible(int key_type_arg, data_t *key_data,
- int alg_arg, data_t *input_data,
- data_t *output_data, int max_ops_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *signature = NULL;
- size_t signature_size;
- size_t signature_length = 0xdeadbeef;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_OPERATION_INCOMPLETE;
- uint32_t num_ops = 0;
- uint32_t max_ops = max_ops_arg;
- size_t num_ops_prior = 0;
- size_t num_completes = 0;
- size_t min_completes = 0;
- size_t max_completes = 0;
- psa_sign_hash_interruptible_operation_t operation =
- psa_sign_hash_interruptible_operation_init();
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- /* Allocate a buffer which has the size advertised by the
- * library. */
- signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
- key_bits, alg);
- TEST_ASSERT(signature_size != 0);
- TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
- ASSERT_ALLOC(signature, signature_size);
- psa_interruptible_set_max_ops(max_ops);
- interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
- &min_completes, &max_completes);
- num_ops_prior = psa_sign_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops_prior == 0);
- /* Start performing the signature. */
- PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
- input_data->x, input_data->len));
- num_ops_prior = psa_sign_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops_prior == 0);
- /* Continue performing the signature until complete. */
- do {
- status = psa_sign_hash_complete(&operation, signature, signature_size,
- &signature_length);
- num_completes++;
- if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
- num_ops = psa_sign_hash_get_num_ops(&operation);
- /* We are asserting here that every complete makes progress
- * (completes some ops), which is true of the internal
- * implementation and probably any implementation, however this is
- * not mandated by the PSA specification. */
- TEST_ASSERT(num_ops > num_ops_prior);
- num_ops_prior = num_ops;
- /* Ensure calling get_num_ops() twice still returns the same
- * number of ops as previously reported. */
- num_ops = psa_sign_hash_get_num_ops(&operation);
- TEST_EQUAL(num_ops, num_ops_prior);
- }
- } while (status == PSA_OPERATION_INCOMPLETE);
- TEST_ASSERT(status == PSA_SUCCESS);
- TEST_LE_U(min_completes, num_completes);
- TEST_LE_U(num_completes, max_completes);
- /* Verify that the signature is what is expected. */
- ASSERT_COMPARE(output_data->x, output_data->len,
- signature, signature_length);
- PSA_ASSERT(psa_sign_hash_abort(&operation));
- num_ops = psa_sign_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops == 0);
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void sign_hash_fail(int key_type_arg, data_t *key_data,
- int alg_arg, data_t *input_data,
- int signature_size_arg, int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t signature_size = signature_size_arg;
- psa_status_t actual_status;
- psa_status_t expected_status = expected_status_arg;
- unsigned char *signature = NULL;
- size_t signature_length = 0xdeadbeef;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- ASSERT_ALLOC(signature, signature_size);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- actual_status = psa_sign_hash(key, alg,
- input_data->x, input_data->len,
- signature, signature_size,
- &signature_length);
- TEST_EQUAL(actual_status, expected_status);
- /* The value of *signature_length is unspecified on error, but
- * whatever it is, it should be less than signature_size, so that
- * if the caller tries to read *signature_length bytes without
- * checking the error code then they don't overflow a buffer. */
- TEST_LE_U(signature_length, signature_size);
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
- /**
- * sign_hash_fail_interruptible() test intentions:
- *
- * Note: This test can currently only handle ECDSA.
- *
- * 1. Test that various failure cases for interruptible sign hash fail with the
- * correct error codes, and at the correct point (at start or during
- * complete).
- *
- * 2. Test the number of calls to psa_sign_hash_complete() required are as
- * expected for different max_ops values.
- *
- * 3. Test that the number of ops done prior to start and after abort is zero
- * and that each successful stage completes some ops (this is not mandated by
- * the PSA specification, but is currently the case).
- *
- * 4. Check that calling complete() when start() fails and complete()
- * after completion results in a BAD_STATE error.
- *
- * 5. Check that calling start() again after start fails results in a BAD_STATE
- * error.
- */
- void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
- int alg_arg, data_t *input_data,
- int signature_size_arg,
- int expected_start_status_arg,
- int expected_complete_status_arg,
- int max_ops_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t signature_size = signature_size_arg;
- psa_status_t actual_status;
- psa_status_t expected_start_status = expected_start_status_arg;
- psa_status_t expected_complete_status = expected_complete_status_arg;
- unsigned char *signature = NULL;
- size_t signature_length = 0xdeadbeef;
- uint32_t num_ops = 0;
- uint32_t max_ops = max_ops_arg;
- size_t num_ops_prior = 0;
- size_t num_completes = 0;
- size_t min_completes = 0;
- size_t max_completes = 0;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_sign_hash_interruptible_operation_t operation =
- psa_sign_hash_interruptible_operation_init();
- ASSERT_ALLOC(signature, signature_size);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- psa_interruptible_set_max_ops(max_ops);
- interruptible_signverify_get_minmax_completes(max_ops,
- expected_complete_status,
- &min_completes,
- &max_completes);
- num_ops_prior = psa_sign_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops_prior == 0);
- /* Start performing the signature. */
- actual_status = psa_sign_hash_start(&operation, key, alg,
- input_data->x, input_data->len);
- TEST_EQUAL(actual_status, expected_start_status);
- if (expected_start_status != PSA_SUCCESS) {
- /* Emulate poor application code, and call complete anyway, even though
- * start failed. */
- actual_status = psa_sign_hash_complete(&operation, signature,
- signature_size,
- &signature_length);
- TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
- /* Test that calling start again after failure also causes BAD_STATE. */
- actual_status = psa_sign_hash_start(&operation, key, alg,
- input_data->x, input_data->len);
- TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
- }
- num_ops_prior = psa_sign_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops_prior == 0);
- /* Continue performing the signature until complete. */
- do {
- actual_status = psa_sign_hash_complete(&operation, signature,
- signature_size,
- &signature_length);
- num_completes++;
- if (actual_status == PSA_SUCCESS ||
- actual_status == PSA_OPERATION_INCOMPLETE) {
- num_ops = psa_sign_hash_get_num_ops(&operation);
- /* We are asserting here that every complete makes progress
- * (completes some ops), which is true of the internal
- * implementation and probably any implementation, however this is
- * not mandated by the PSA specification. */
- TEST_ASSERT(num_ops > num_ops_prior);
- num_ops_prior = num_ops;
- }
- } while (actual_status == PSA_OPERATION_INCOMPLETE);
- TEST_EQUAL(actual_status, expected_complete_status);
- /* Check that another complete returns BAD_STATE. */
- actual_status = psa_sign_hash_complete(&operation, signature,
- signature_size,
- &signature_length);
- TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_sign_hash_abort(&operation));
- num_ops = psa_sign_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops == 0);
- /* The value of *signature_length is unspecified on error, but
- * whatever it is, it should be less than signature_size, so that
- * if the caller tries to read *signature_length bytes without
- * checking the error code then they don't overflow a buffer. */
- TEST_LE_U(signature_length, signature_size);
- TEST_LE_U(min_completes, num_completes);
- TEST_LE_U(num_completes, max_completes);
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void sign_verify_hash(int key_type_arg, data_t *key_data,
- int alg_arg, data_t *input_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *signature = NULL;
- size_t signature_size;
- size_t signature_length = 0xdeadbeef;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- /* Allocate a buffer which has the size advertised by the
- * library. */
- signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
- key_bits, alg);
- TEST_ASSERT(signature_size != 0);
- TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
- ASSERT_ALLOC(signature, signature_size);
- /* Perform the signature. */
- PSA_ASSERT(psa_sign_hash(key, alg,
- input_data->x, input_data->len,
- signature, signature_size,
- &signature_length));
- /* Check that the signature length looks sensible. */
- TEST_LE_U(signature_length, signature_size);
- TEST_ASSERT(signature_length > 0);
- /* Use the library to verify that the signature is correct. */
- PSA_ASSERT(psa_verify_hash(key, alg,
- input_data->x, input_data->len,
- signature, signature_length));
- if (input_data->len != 0) {
- /* Flip a bit in the input and verify that the signature is now
- * detected as invalid. Flip a bit at the beginning, not at the end,
- * because ECDSA may ignore the last few bits of the input. */
- input_data->x[0] ^= 1;
- TEST_EQUAL(psa_verify_hash(key, alg,
- input_data->x, input_data->len,
- signature, signature_length),
- PSA_ERROR_INVALID_SIGNATURE);
- }
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
- /**
- * sign_verify_hash_interruptible() test intentions:
- *
- * Note: This test can currently only handle ECDSA.
- *
- * 1. Test that we can sign an input hash with the given keypair and then
- * afterwards verify that signature. This is currently the only way to test
- * non deterministic ECDSA, but this test can also handle deterministic.
- *
- * 2. Test that after corrupting the hash, the verification detects an invalid
- * signature.
- *
- * 3. Test the number of calls to psa_sign_hash_complete() required are as
- * expected for different max_ops values.
- *
- * 4. Test that the number of ops done prior to starting signing and after abort
- * is zero and that each successful signing stage completes some ops (this is
- * not mandated by the PSA specification, but is currently the case).
- */
- void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
- int alg_arg, data_t *input_data,
- int max_ops_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *signature = NULL;
- size_t signature_size;
- size_t signature_length = 0xdeadbeef;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_OPERATION_INCOMPLETE;
- uint32_t max_ops = max_ops_arg;
- uint32_t num_ops = 0;
- uint32_t num_ops_prior = 0;
- size_t num_completes = 0;
- size_t min_completes = 0;
- size_t max_completes = 0;
- psa_sign_hash_interruptible_operation_t sign_operation =
- psa_sign_hash_interruptible_operation_init();
- psa_verify_hash_interruptible_operation_t verify_operation =
- psa_verify_hash_interruptible_operation_init();
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
- PSA_KEY_USAGE_VERIFY_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- /* Allocate a buffer which has the size advertised by the
- * library. */
- signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
- key_bits, alg);
- TEST_ASSERT(signature_size != 0);
- TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
- ASSERT_ALLOC(signature, signature_size);
- psa_interruptible_set_max_ops(max_ops);
- interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
- &min_completes, &max_completes);
- num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
- TEST_ASSERT(num_ops_prior == 0);
- /* Start performing the signature. */
- PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
- input_data->x, input_data->len));
- num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
- TEST_ASSERT(num_ops_prior == 0);
- /* Continue performing the signature until complete. */
- do {
- status = psa_sign_hash_complete(&sign_operation, signature,
- signature_size,
- &signature_length);
- num_completes++;
- if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
- num_ops = psa_sign_hash_get_num_ops(&sign_operation);
- /* We are asserting here that every complete makes progress
- * (completes some ops), which is true of the internal
- * implementation and probably any implementation, however this is
- * not mandated by the PSA specification. */
- TEST_ASSERT(num_ops > num_ops_prior);
- num_ops_prior = num_ops;
- }
- } while (status == PSA_OPERATION_INCOMPLETE);
- TEST_ASSERT(status == PSA_SUCCESS);
- TEST_LE_U(min_completes, num_completes);
- TEST_LE_U(num_completes, max_completes);
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- num_ops = psa_sign_hash_get_num_ops(&sign_operation);
- TEST_ASSERT(num_ops == 0);
- /* Check that the signature length looks sensible. */
- TEST_LE_U(signature_length, signature_size);
- TEST_ASSERT(signature_length > 0);
- num_completes = 0;
- /* Start verification. */
- PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
- input_data->x, input_data->len,
- signature, signature_length));
- /* Continue performing the signature until complete. */
- do {
- status = psa_verify_hash_complete(&verify_operation);
- num_completes++;
- } while (status == PSA_OPERATION_INCOMPLETE);
- TEST_ASSERT(status == PSA_SUCCESS);
- TEST_LE_U(min_completes, num_completes);
- TEST_LE_U(num_completes, max_completes);
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- verify_operation = psa_verify_hash_interruptible_operation_init();
- if (input_data->len != 0) {
- /* Flip a bit in the input and verify that the signature is now
- * detected as invalid. Flip a bit at the beginning, not at the end,
- * because ECDSA may ignore the last few bits of the input. */
- input_data->x[0] ^= 1;
- /* Start verification. */
- PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
- input_data->x, input_data->len,
- signature, signature_length));
- /* Continue performing the signature until complete. */
- do {
- status = psa_verify_hash_complete(&verify_operation);
- } while (status == PSA_OPERATION_INCOMPLETE);
- TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
- }
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void verify_hash(int key_type_arg, data_t *key_data,
- int alg_arg, data_t *hash_data,
- data_t *signature_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_verify_hash(key, alg,
- hash_data->x, hash_data->len,
- signature_data->x, signature_data->len));
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
- /**
- * verify_hash_interruptible() test intentions:
- *
- * Note: This test can currently only handle ECDSA.
- *
- * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
- * only). Given this test only does verification it can accept public keys as
- * well as private keys / keypairs.
- *
- * 2. Test the number of calls to psa_verify_hash_complete() required are as
- * expected for different max_ops values.
- *
- * 3. Test that the number of ops done prior to start and after abort is zero
- * and that each successful stage completes some ops (this is not mandated by
- * the PSA specification, but is currently the case).
- *
- * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
- * complete() calls does not alter the number of ops returned.
- *
- * 5. Test that after corrupting the hash, the verification detects an invalid
- * signature.
- */
- void verify_hash_interruptible(int key_type_arg, data_t *key_data,
- int alg_arg, data_t *hash_data,
- data_t *signature_data, int max_ops_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t status = PSA_OPERATION_INCOMPLETE;
- uint32_t num_ops = 0;
- uint32_t max_ops = max_ops_arg;
- size_t num_ops_prior = 0;
- size_t num_completes = 0;
- size_t min_completes = 0;
- size_t max_completes = 0;
- psa_verify_hash_interruptible_operation_t operation =
- psa_verify_hash_interruptible_operation_init();
- TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- psa_interruptible_set_max_ops(max_ops);
- interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
- &min_completes, &max_completes);
- num_ops_prior = psa_verify_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops_prior == 0);
- /* Start verification. */
- PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
- hash_data->x, hash_data->len,
- signature_data->x, signature_data->len)
- );
- num_ops_prior = psa_verify_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops_prior == 0);
- /* Continue performing the signature until complete. */
- do {
- status = psa_verify_hash_complete(&operation);
- num_completes++;
- if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
- num_ops = psa_verify_hash_get_num_ops(&operation);
- /* We are asserting here that every complete makes progress
- * (completes some ops), which is true of the internal
- * implementation and probably any implementation, however this is
- * not mandated by the PSA specification. */
- TEST_ASSERT(num_ops > num_ops_prior);
- num_ops_prior = num_ops;
- /* Ensure calling get_num_ops() twice still returns the same
- * number of ops as previously reported. */
- num_ops = psa_verify_hash_get_num_ops(&operation);
- TEST_EQUAL(num_ops, num_ops_prior);
- }
- } while (status == PSA_OPERATION_INCOMPLETE);
- TEST_ASSERT(status == PSA_SUCCESS);
- TEST_LE_U(min_completes, num_completes);
- TEST_LE_U(num_completes, max_completes);
- PSA_ASSERT(psa_verify_hash_abort(&operation));
- num_ops = psa_verify_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops == 0);
- if (hash_data->len != 0) {
- /* Flip a bit in the hash and verify that the signature is now detected
- * as invalid. Flip a bit at the beginning, not at the end, because
- * ECDSA may ignore the last few bits of the input. */
- hash_data->x[0] ^= 1;
- /* Start verification. */
- PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
- hash_data->x, hash_data->len,
- signature_data->x, signature_data->len));
- /* Continue performing the signature until complete. */
- do {
- status = psa_verify_hash_complete(&operation);
- } while (status == PSA_OPERATION_INCOMPLETE);
- TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
- }
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void verify_hash_fail(int key_type_arg, data_t *key_data,
- int alg_arg, data_t *hash_data,
- data_t *signature_data,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t actual_status;
- psa_status_t expected_status = expected_status_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- actual_status = psa_verify_hash(key, alg,
- hash_data->x, hash_data->len,
- signature_data->x, signature_data->len);
- TEST_EQUAL(actual_status, expected_status);
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
- /**
- * verify_hash_fail_interruptible() test intentions:
- *
- * Note: This test can currently only handle ECDSA.
- *
- * 1. Test that various failure cases for interruptible verify hash fail with
- * the correct error codes, and at the correct point (at start or during
- * complete).
- *
- * 2. Test the number of calls to psa_verify_hash_complete() required are as
- * expected for different max_ops values.
- *
- * 3. Test that the number of ops done prior to start and after abort is zero
- * and that each successful stage completes some ops (this is not mandated by
- * the PSA specification, but is currently the case).
- *
- * 4. Check that calling complete() when start() fails and complete()
- * after completion results in a BAD_STATE error.
- *
- * 5. Check that calling start() again after start fails results in a BAD_STATE
- * error.
- */
- void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
- int alg_arg, data_t *hash_data,
- data_t *signature_data,
- int expected_start_status_arg,
- int expected_complete_status_arg,
- int max_ops_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t actual_status;
- psa_status_t expected_start_status = expected_start_status_arg;
- psa_status_t expected_complete_status = expected_complete_status_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- uint32_t num_ops = 0;
- uint32_t max_ops = max_ops_arg;
- size_t num_ops_prior = 0;
- size_t num_completes = 0;
- size_t min_completes = 0;
- size_t max_completes = 0;
- psa_verify_hash_interruptible_operation_t operation =
- psa_verify_hash_interruptible_operation_init();
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- psa_interruptible_set_max_ops(max_ops);
- interruptible_signverify_get_minmax_completes(max_ops,
- expected_complete_status,
- &min_completes,
- &max_completes);
- num_ops_prior = psa_verify_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops_prior == 0);
- /* Start verification. */
- actual_status = psa_verify_hash_start(&operation, key, alg,
- hash_data->x, hash_data->len,
- signature_data->x,
- signature_data->len);
- TEST_EQUAL(actual_status, expected_start_status);
- if (expected_start_status != PSA_SUCCESS) {
- /* Emulate poor application code, and call complete anyway, even though
- * start failed. */
- actual_status = psa_verify_hash_complete(&operation);
- TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
- /* Test that calling start again after failure also causes BAD_STATE. */
- actual_status = psa_verify_hash_start(&operation, key, alg,
- hash_data->x, hash_data->len,
- signature_data->x,
- signature_data->len);
- TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
- }
- num_ops_prior = psa_verify_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops_prior == 0);
- /* Continue performing the signature until complete. */
- do {
- actual_status = psa_verify_hash_complete(&operation);
- num_completes++;
- if (actual_status == PSA_SUCCESS ||
- actual_status == PSA_OPERATION_INCOMPLETE) {
- num_ops = psa_verify_hash_get_num_ops(&operation);
- /* We are asserting here that every complete makes progress
- * (completes some ops), which is true of the internal
- * implementation and probably any implementation, however this is
- * not mandated by the PSA specification. */
- TEST_ASSERT(num_ops > num_ops_prior);
- num_ops_prior = num_ops;
- }
- } while (actual_status == PSA_OPERATION_INCOMPLETE);
- TEST_EQUAL(actual_status, expected_complete_status);
- /* Check that another complete returns BAD_STATE. */
- actual_status = psa_verify_hash_complete(&operation);
- TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
- TEST_LE_U(min_completes, num_completes);
- TEST_LE_U(num_completes, max_completes);
- PSA_ASSERT(psa_verify_hash_abort(&operation));
- num_ops = psa_verify_hash_get_num_ops(&operation);
- TEST_ASSERT(num_ops == 0);
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
- /**
- * interruptible_signverify_hash_state_test() test intentions:
- *
- * Note: This test can currently only handle ECDSA.
- *
- * 1. Test that calling the various interruptible sign and verify hash functions
- * in incorrect orders returns BAD_STATE errors.
- */
- void interruptible_signverify_hash_state_test(int key_type_arg,
- data_t *key_data, int alg_arg, data_t *input_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *signature = NULL;
- size_t signature_size;
- size_t signature_length = 0xdeadbeef;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_sign_hash_interruptible_operation_t sign_operation =
- psa_sign_hash_interruptible_operation_init();
- psa_verify_hash_interruptible_operation_t verify_operation =
- psa_verify_hash_interruptible_operation_init();
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
- PSA_KEY_USAGE_VERIFY_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- /* Allocate a buffer which has the size advertised by the
- * library. */
- signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
- key_bits, alg);
- TEST_ASSERT(signature_size != 0);
- TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
- ASSERT_ALLOC(signature, signature_size);
- psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
- /* --- Attempt completes prior to starts --- */
- TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
- signature_size,
- &signature_length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- /* --- Aborts in all other places. --- */
- psa_sign_hash_abort(&sign_operation);
- PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
- input_data->x, input_data->len));
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- psa_interruptible_set_max_ops(1);
- PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
- input_data->x, input_data->len));
- TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
- signature_size,
- &signature_length),
- PSA_OPERATION_INCOMPLETE);
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
- PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
- input_data->x, input_data->len));
- PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
- signature_size,
- &signature_length));
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
- input_data->x, input_data->len,
- signature, signature_length));
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- psa_interruptible_set_max_ops(1);
- PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
- input_data->x, input_data->len,
- signature, signature_length));
- TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
- PSA_OPERATION_INCOMPLETE);
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
- PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
- input_data->x, input_data->len,
- signature, signature_length));
- PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- /* --- Attempt double starts. --- */
- PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
- input_data->x, input_data->len));
- TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
- input_data->x, input_data->len),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
- input_data->x, input_data->len,
- signature, signature_length));
- TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
- input_data->x, input_data->len,
- signature, signature_length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
- /**
- * interruptible_signverify_hash_edgecase_tests() test intentions:
- *
- * Note: This test can currently only handle ECDSA.
- *
- * 1. Test various edge cases in the interruptible sign and verify hash
- * interfaces.
- */
- void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
- data_t *key_data, int alg_arg, data_t *input_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *signature = NULL;
- size_t signature_size;
- size_t signature_length = 0xdeadbeef;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- uint8_t *input_buffer = NULL;
- psa_sign_hash_interruptible_operation_t sign_operation =
- psa_sign_hash_interruptible_operation_init();
- psa_verify_hash_interruptible_operation_t verify_operation =
- psa_verify_hash_interruptible_operation_init();
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
- PSA_KEY_USAGE_VERIFY_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- /* Allocate a buffer which has the size advertised by the
- * library. */
- signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
- key_bits, alg);
- TEST_ASSERT(signature_size != 0);
- TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
- ASSERT_ALLOC(signature, signature_size);
- /* --- Change function inputs mid run, to cause an error (sign only,
- * verify passes all inputs to start. --- */
- psa_interruptible_set_max_ops(1);
- PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
- input_data->x, input_data->len));
- TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
- signature_size,
- &signature_length),
- PSA_OPERATION_INCOMPLETE);
- TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
- 0,
- &signature_length),
- PSA_ERROR_BUFFER_TOO_SMALL);
- /* And test that this invalidates the operation. */
- TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
- 0,
- &signature_length),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- /* Trash the hash buffer in between start and complete, to ensure
- * no reliance on external buffers. */
- psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
- input_buffer = mbedtls_calloc(1, input_data->len);
- TEST_ASSERT(input_buffer != NULL);
- memcpy(input_buffer, input_data->x, input_data->len);
- PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
- input_buffer, input_data->len));
- memset(input_buffer, '!', input_data->len);
- mbedtls_free(input_buffer);
- input_buffer = NULL;
- PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
- signature_size,
- &signature_length));
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- input_buffer = mbedtls_calloc(1, input_data->len);
- TEST_ASSERT(input_buffer != NULL);
- memcpy(input_buffer, input_data->x, input_data->len);
- PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
- input_buffer, input_data->len,
- signature, signature_length));
- memset(input_buffer, '!', input_data->len);
- mbedtls_free(input_buffer);
- input_buffer = NULL;
- PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
- /**
- * interruptible_signverify_hash_ops_tests() test intentions:
- *
- * Note: This test can currently only handle ECDSA.
- *
- * 1. Test that setting max ops is reflected in both interruptible sign and
- * verify hash
- * 2. Test that changing the value of max_ops to unlimited during an operation
- * causes that operation to complete in the next call.
- *
- * 3. Test that calling get_num_ops() between complete calls gives the same
- * result as calling get_num_ops() once at the end of the operation.
- */
- void interruptible_signverify_hash_ops_tests(int key_type_arg,
- data_t *key_data, int alg_arg,
- data_t *input_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- size_t key_bits;
- unsigned char *signature = NULL;
- size_t signature_size;
- size_t signature_length = 0xdeadbeef;
- uint32_t num_ops = 0;
- psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
- psa_sign_hash_interruptible_operation_t sign_operation =
- psa_sign_hash_interruptible_operation_init();
- psa_verify_hash_interruptible_operation_t verify_operation =
- psa_verify_hash_interruptible_operation_init();
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
- PSA_KEY_USAGE_VERIFY_HASH);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- /* Allocate a buffer which has the size advertised by the
- * library. */
- signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
- TEST_ASSERT(signature_size != 0);
- TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
- ASSERT_ALLOC(signature, signature_size);
- /* Check that default max ops gets set if we don't set it. */
- PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
- input_data->x, input_data->len));
- TEST_EQUAL(psa_interruptible_get_max_ops(),
- PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
- input_data->x, input_data->len,
- signature, signature_size));
- TEST_EQUAL(psa_interruptible_get_max_ops(),
- PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- /* Check that max ops gets set properly. */
- psa_interruptible_set_max_ops(0xbeef);
- TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
- /* --- Ensure changing the max ops mid operation works (operation should
- * complete successfully after setting max ops to unlimited --- */
- psa_interruptible_set_max_ops(1);
- PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
- input_data->x, input_data->len));
- TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
- signature_size,
- &signature_length),
- PSA_OPERATION_INCOMPLETE);
- psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
- PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
- signature_size,
- &signature_length));
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- psa_interruptible_set_max_ops(1);
- PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
- input_data->x, input_data->len,
- signature, signature_length));
- TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
- PSA_OPERATION_INCOMPLETE);
- psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
- PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- /* --- Test that not calling get_num_ops inbetween complete calls does not
- * result in lost ops. ---*/
- psa_interruptible_set_max_ops(1);
- PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
- input_data->x, input_data->len));
- /* Continue performing the signature until complete. */
- do {
- status = psa_sign_hash_complete(&sign_operation, signature,
- signature_size,
- &signature_length);
- num_ops = psa_sign_hash_get_num_ops(&sign_operation);
- } while (status == PSA_OPERATION_INCOMPLETE);
- PSA_ASSERT(status);
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
- input_data->x, input_data->len));
- /* Continue performing the signature until complete. */
- do {
- status = psa_sign_hash_complete(&sign_operation, signature,
- signature_size,
- &signature_length);
- } while (status == PSA_OPERATION_INCOMPLETE);
- PSA_ASSERT(status);
- TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
- PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
- PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
- input_data->x, input_data->len,
- signature, signature_length));
- /* Continue performing the verification until complete. */
- do {
- status = psa_verify_hash_complete(&verify_operation);
- num_ops = psa_verify_hash_get_num_ops(&verify_operation);
- } while (status == PSA_OPERATION_INCOMPLETE);
- PSA_ASSERT(status);
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
- input_data->x, input_data->len,
- signature, signature_length));
- /* Continue performing the verification until complete. */
- do {
- status = psa_verify_hash_complete(&verify_operation);
- } while (status == PSA_OPERATION_INCOMPLETE);
- PSA_ASSERT(status);
- TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
- PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void sign_message_deterministic(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *input_data,
- data_t *output_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *signature = NULL;
- size_t signature_size;
- size_t signature_length = 0xdeadbeef;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
- TEST_ASSERT(signature_size != 0);
- TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
- ASSERT_ALLOC(signature, signature_size);
- PSA_ASSERT(psa_sign_message(key, alg,
- input_data->x, input_data->len,
- signature, signature_size,
- &signature_length));
- ASSERT_COMPARE(output_data->x, output_data->len,
- signature, signature_length);
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void sign_message_fail(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *input_data,
- int signature_size_arg,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t signature_size = signature_size_arg;
- psa_status_t actual_status;
- psa_status_t expected_status = expected_status_arg;
- unsigned char *signature = NULL;
- size_t signature_length = 0xdeadbeef;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- ASSERT_ALLOC(signature, signature_size);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- actual_status = psa_sign_message(key, alg,
- input_data->x, input_data->len,
- signature, signature_size,
- &signature_length);
- TEST_EQUAL(actual_status, expected_status);
- /* The value of *signature_length is unspecified on error, but
- * whatever it is, it should be less than signature_size, so that
- * if the caller tries to read *signature_length bytes without
- * checking the error code then they don't overflow a buffer. */
- TEST_LE_U(signature_length, signature_size);
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void sign_verify_message(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *input_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *signature = NULL;
- size_t signature_size;
- size_t signature_length = 0xdeadbeef;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
- PSA_KEY_USAGE_VERIFY_MESSAGE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
- TEST_ASSERT(signature_size != 0);
- TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
- ASSERT_ALLOC(signature, signature_size);
- PSA_ASSERT(psa_sign_message(key, alg,
- input_data->x, input_data->len,
- signature, signature_size,
- &signature_length));
- TEST_LE_U(signature_length, signature_size);
- TEST_ASSERT(signature_length > 0);
- PSA_ASSERT(psa_verify_message(key, alg,
- input_data->x, input_data->len,
- signature, signature_length));
- if (input_data->len != 0) {
- /* Flip a bit in the input and verify that the signature is now
- * detected as invalid. Flip a bit at the beginning, not at the end,
- * because ECDSA may ignore the last few bits of the input. */
- input_data->x[0] ^= 1;
- TEST_EQUAL(psa_verify_message(key, alg,
- input_data->x, input_data->len,
- signature, signature_length),
- PSA_ERROR_INVALID_SIGNATURE);
- }
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(signature);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void verify_message(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *input_data,
- data_t *signature_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_verify_message(key, alg,
- input_data->x, input_data->len,
- signature_data->x, signature_data->len));
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void verify_message_fail(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *hash_data,
- data_t *signature_data,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t actual_status;
- psa_status_t expected_status = expected_status_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- actual_status = psa_verify_message(key, alg,
- hash_data->x, hash_data->len,
- signature_data->x,
- signature_data->len);
- TEST_EQUAL(actual_status, expected_status);
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void asymmetric_encrypt(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *input_data,
- data_t *label,
- int expected_output_length_arg,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t expected_output_length = expected_output_length_arg;
- size_t key_bits;
- unsigned char *output = NULL;
- size_t output_size;
- size_t output_length = ~0;
- psa_status_t actual_status;
- psa_status_t expected_status = expected_status_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- /* Import the key */
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- /* Determine the maximum output length */
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
- TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
- ASSERT_ALLOC(output, output_size);
- /* Encrypt the input */
- actual_status = psa_asymmetric_encrypt(key, alg,
- input_data->x, input_data->len,
- label->x, label->len,
- output, output_size,
- &output_length);
- TEST_EQUAL(actual_status, expected_status);
- if (actual_status == PSA_SUCCESS) {
- TEST_EQUAL(output_length, expected_output_length);
- } else {
- TEST_LE_U(output_length, output_size);
- }
- /* If the label is empty, the test framework puts a non-null pointer
- * in label->x. Test that a null pointer works as well. */
- if (label->len == 0) {
- output_length = ~0;
- if (output_size != 0) {
- memset(output, 0, output_size);
- }
- actual_status = psa_asymmetric_encrypt(key, alg,
- input_data->x, input_data->len,
- NULL, label->len,
- output, output_size,
- &output_length);
- TEST_EQUAL(actual_status, expected_status);
- if (actual_status == PSA_SUCCESS) {
- TEST_EQUAL(output_length, expected_output_length);
- } else {
- TEST_LE_U(output_length, output_size);
- }
- }
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(output);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void asymmetric_encrypt_decrypt(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *input_data,
- data_t *label)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *output = NULL;
- size_t output_size;
- size_t output_length = ~0;
- unsigned char *output2 = NULL;
- size_t output2_size;
- size_t output2_length = ~0;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- /* Determine the maximum ciphertext length */
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
- TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
- ASSERT_ALLOC(output, output_size);
- output2_size = input_data->len;
- TEST_LE_U(output2_size,
- PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
- TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
- ASSERT_ALLOC(output2, output2_size);
- /* We test encryption by checking that encrypt-then-decrypt gives back
- * the original plaintext because of the non-optional random
- * part of encryption process which prevents using fixed vectors. */
- PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
- input_data->x, input_data->len,
- label->x, label->len,
- output, output_size,
- &output_length));
- /* We don't know what ciphertext length to expect, but check that
- * it looks sensible. */
- TEST_LE_U(output_length, output_size);
- PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
- output, output_length,
- label->x, label->len,
- output2, output2_size,
- &output2_length));
- ASSERT_COMPARE(input_data->x, input_data->len,
- output2, output2_length);
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(output);
- mbedtls_free(output2);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void asymmetric_decrypt(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *input_data,
- data_t *label,
- data_t *expected_data)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- size_t key_bits;
- unsigned char *output = NULL;
- size_t output_size = 0;
- size_t output_length = ~0;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- /* Determine the maximum ciphertext length */
- output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
- TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
- ASSERT_ALLOC(output, output_size);
- PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
- input_data->x, input_data->len,
- label->x, label->len,
- output,
- output_size,
- &output_length));
- ASSERT_COMPARE(expected_data->x, expected_data->len,
- output, output_length);
- /* If the label is empty, the test framework puts a non-null pointer
- * in label->x. Test that a null pointer works as well. */
- if (label->len == 0) {
- output_length = ~0;
- if (output_size != 0) {
- memset(output, 0, output_size);
- }
- PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
- input_data->x, input_data->len,
- NULL, label->len,
- output,
- output_size,
- &output_length));
- ASSERT_COMPARE(expected_data->x, expected_data->len,
- output, output_length);
- }
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(output);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void asymmetric_decrypt_fail(int key_type_arg,
- data_t *key_data,
- int alg_arg,
- data_t *input_data,
- data_t *label,
- int output_size_arg,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t key_type = key_type_arg;
- psa_algorithm_t alg = alg_arg;
- unsigned char *output = NULL;
- size_t output_size = output_size_arg;
- size_t output_length = ~0;
- psa_status_t actual_status;
- psa_status_t expected_status = expected_status_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- ASSERT_ALLOC(output, output_size);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- actual_status = psa_asymmetric_decrypt(key, alg,
- input_data->x, input_data->len,
- label->x, label->len,
- output, output_size,
- &output_length);
- TEST_EQUAL(actual_status, expected_status);
- TEST_LE_U(output_length, output_size);
- /* If the label is empty, the test framework puts a non-null pointer
- * in label->x. Test that a null pointer works as well. */
- if (label->len == 0) {
- output_length = ~0;
- if (output_size != 0) {
- memset(output, 0, output_size);
- }
- actual_status = psa_asymmetric_decrypt(key, alg,
- input_data->x, input_data->len,
- NULL, label->len,
- output, output_size,
- &output_length);
- TEST_EQUAL(actual_status, expected_status);
- TEST_LE_U(output_length, output_size);
- }
- exit:
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- mbedtls_free(output);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void key_derivation_init()
- {
- /* Test each valid way of initializing the object, except for `= {0}`, as
- * Clang 5 complains when `-Wmissing-field-initializers` is used, even
- * though it's OK by the C standard. We could test for this, but we'd need
- * to suppress the Clang warning for the test. */
- size_t capacity;
- psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
- psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_key_derivation_operation_t zero;
- memset(&zero, 0, sizeof(zero));
- /* A default operation should not be able to report its capacity. */
- TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
- PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
- PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
- PSA_ERROR_BAD_STATE);
- /* A default operation should be abortable without error. */
- PSA_ASSERT(psa_key_derivation_abort(&func));
- PSA_ASSERT(psa_key_derivation_abort(&init));
- PSA_ASSERT(psa_key_derivation_abort(&zero));
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_setup(int alg_arg, int expected_status_arg)
- {
- psa_algorithm_t alg = alg_arg;
- psa_status_t expected_status = expected_status_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- PSA_ASSERT(psa_crypto_init());
- TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
- expected_status);
- exit:
- psa_key_derivation_abort(&operation);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_set_capacity(int alg_arg, int capacity_arg,
- int expected_status_arg)
- {
- psa_algorithm_t alg = alg_arg;
- size_t capacity = capacity_arg;
- psa_status_t expected_status = expected_status_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- PSA_ASSERT(psa_crypto_init());
- PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
- TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
- expected_status);
- exit:
- psa_key_derivation_abort(&operation);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_input(int alg_arg,
- int step_arg1, int key_type_arg1, data_t *input1,
- int expected_status_arg1,
- int step_arg2, int key_type_arg2, data_t *input2,
- int expected_status_arg2,
- int step_arg3, int key_type_arg3, data_t *input3,
- int expected_status_arg3,
- int output_key_type_arg, int expected_output_status_arg)
- {
- psa_algorithm_t alg = alg_arg;
- psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
- psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
- psa_status_t expected_statuses[] = { expected_status_arg1,
- expected_status_arg2,
- expected_status_arg3 };
- data_t *inputs[] = { input1, input2, input3 };
- mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
- MBEDTLS_SVC_KEY_ID_INIT,
- MBEDTLS_SVC_KEY_ID_INIT };
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- size_t i;
- psa_key_type_t output_key_type = output_key_type_arg;
- mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_status_t expected_output_status = expected_output_status_arg;
- psa_status_t actual_output_status;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes, alg);
- PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
- for (i = 0; i < ARRAY_LENGTH(steps); i++) {
- mbedtls_test_set_step(i);
- if (steps[i] == 0) {
- /* Skip this step */
- } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
- psa_set_key_type(&attributes, key_types[i]);
- PSA_ASSERT(psa_import_key(&attributes,
- inputs[i]->x, inputs[i]->len,
- &keys[i]));
- if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
- steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
- // When taking a private key as secret input, use key agreement
- // to add the shared secret to the derivation
- TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
- &operation, keys[i]),
- expected_statuses[i]);
- } else {
- TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
- keys[i]),
- expected_statuses[i]);
- }
- } else {
- TEST_EQUAL(psa_key_derivation_input_bytes(
- &operation, steps[i],
- inputs[i]->x, inputs[i]->len),
- expected_statuses[i]);
- }
- }
- if (output_key_type != PSA_KEY_TYPE_NONE) {
- psa_reset_key_attributes(&attributes);
- psa_set_key_type(&attributes, output_key_type);
- psa_set_key_bits(&attributes, 8);
- actual_output_status =
- psa_key_derivation_output_key(&attributes, &operation,
- &output_key);
- } else {
- uint8_t buffer[1];
- actual_output_status =
- psa_key_derivation_output_bytes(&operation,
- buffer, sizeof(buffer));
- }
- TEST_EQUAL(actual_output_status, expected_output_status);
- exit:
- psa_key_derivation_abort(&operation);
- for (i = 0; i < ARRAY_LENGTH(keys); i++) {
- psa_destroy_key(keys[i]);
- }
- psa_destroy_key(output_key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_over_capacity(int alg_arg)
- {
- psa_algorithm_t alg = alg_arg;
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- size_t key_type = PSA_KEY_TYPE_DERIVE;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- unsigned char input1[] = "Input 1";
- size_t input1_length = sizeof(input1);
- unsigned char input2[] = "Input 2";
- size_t input2_length = sizeof(input2);
- uint8_t buffer[42];
- size_t capacity = sizeof(buffer);
- const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
- 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
- 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type);
- PSA_ASSERT(psa_import_key(&attributes,
- key_data, sizeof(key_data),
- &key));
- /* valid key derivation */
- if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
- input1, input1_length,
- input2, input2_length,
- capacity)) {
- goto exit;
- }
- /* state of operation shouldn't allow additional generation */
- TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
- PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
- TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
- PSA_ERROR_INSUFFICIENT_DATA);
- exit:
- psa_key_derivation_abort(&operation);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_actions_without_setup()
- {
- uint8_t output_buffer[16];
- size_t buffer_size = 16;
- size_t capacity = 0;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
- output_buffer, buffer_size)
- == PSA_ERROR_BAD_STATE);
- TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
- == PSA_ERROR_BAD_STATE);
- PSA_ASSERT(psa_key_derivation_abort(&operation));
- TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
- output_buffer, buffer_size)
- == PSA_ERROR_BAD_STATE);
- TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
- == PSA_ERROR_BAD_STATE);
- exit:
- psa_key_derivation_abort(&operation);
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_output(int alg_arg,
- int step1_arg, data_t *input1, int expected_status_arg1,
- int step2_arg, data_t *input2, int expected_status_arg2,
- int step3_arg, data_t *input3, int expected_status_arg3,
- int step4_arg, data_t *input4, int expected_status_arg4,
- data_t *key_agreement_peer_key,
- int requested_capacity_arg,
- data_t *expected_output1,
- data_t *expected_output2,
- int other_key_input_type,
- int key_input_type,
- int derive_type)
- {
- psa_algorithm_t alg = alg_arg;
- psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
- data_t *inputs[] = { input1, input2, input3, input4 };
- mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
- MBEDTLS_SVC_KEY_ID_INIT,
- MBEDTLS_SVC_KEY_ID_INIT,
- MBEDTLS_SVC_KEY_ID_INIT };
- psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
- expected_status_arg3, expected_status_arg4 };
- size_t requested_capacity = requested_capacity_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- uint8_t *expected_outputs[2] =
- { expected_output1->x, expected_output2->x };
- size_t output_sizes[2] =
- { expected_output1->len, expected_output2->len };
- size_t output_buffer_size = 0;
- uint8_t *output_buffer = NULL;
- size_t expected_capacity;
- size_t current_capacity;
- psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
- mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_status_t status;
- size_t i;
- for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
- if (output_sizes[i] > output_buffer_size) {
- output_buffer_size = output_sizes[i];
- }
- if (output_sizes[i] == 0) {
- expected_outputs[i] = NULL;
- }
- }
- ASSERT_ALLOC(output_buffer, output_buffer_size);
- PSA_ASSERT(psa_crypto_init());
- /* Extraction phase. */
- PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
- PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
- requested_capacity));
- for (i = 0; i < ARRAY_LENGTH(steps); i++) {
- switch (steps[i]) {
- case 0:
- break;
- case PSA_KEY_DERIVATION_INPUT_SECRET:
- switch (key_input_type) {
- case 0: // input bytes
- TEST_EQUAL(psa_key_derivation_input_bytes(
- &operation, steps[i],
- inputs[i]->x, inputs[i]->len),
- statuses[i]);
- if (statuses[i] != PSA_SUCCESS) {
- goto exit;
- }
- break;
- case 1: // input key
- psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes1, alg);
- psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
- PSA_ASSERT(psa_import_key(&attributes1,
- inputs[i]->x, inputs[i]->len,
- &keys[i]));
- if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
- PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
- TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
- PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
- }
- PSA_ASSERT(psa_key_derivation_input_key(&operation,
- steps[i],
- keys[i]));
- break;
- default:
- TEST_ASSERT(!"default case not supported");
- break;
- }
- break;
- case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
- switch (other_key_input_type) {
- case 0: // input bytes
- TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
- steps[i],
- inputs[i]->x,
- inputs[i]->len),
- statuses[i]);
- break;
- case 1: // input key, type DERIVE
- case 11: // input key, type RAW
- psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes2, alg);
- psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
- // other secret of type RAW_DATA passed with input_key
- if (other_key_input_type == 11) {
- psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
- }
- PSA_ASSERT(psa_import_key(&attributes2,
- inputs[i]->x, inputs[i]->len,
- &keys[i]));
- TEST_EQUAL(psa_key_derivation_input_key(&operation,
- steps[i],
- keys[i]),
- statuses[i]);
- break;
- case 2: // key agreement
- psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes3, alg);
- psa_set_key_type(&attributes3,
- PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
- PSA_ASSERT(psa_import_key(&attributes3,
- inputs[i]->x, inputs[i]->len,
- &keys[i]));
- TEST_EQUAL(psa_key_derivation_key_agreement(
- &operation,
- PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
- keys[i], key_agreement_peer_key->x,
- key_agreement_peer_key->len), statuses[i]);
- break;
- default:
- TEST_ASSERT(!"default case not supported");
- break;
- }
- if (statuses[i] != PSA_SUCCESS) {
- goto exit;
- }
- break;
- default:
- TEST_EQUAL(psa_key_derivation_input_bytes(
- &operation, steps[i],
- inputs[i]->x, inputs[i]->len), statuses[i]);
- if (statuses[i] != PSA_SUCCESS) {
- goto exit;
- }
- break;
- }
- }
- PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
- ¤t_capacity));
- TEST_EQUAL(current_capacity, requested_capacity);
- expected_capacity = requested_capacity;
- if (derive_type == 1) { // output key
- psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
- /* For output key derivation secret must be provided using
- input key, otherwise operation is not permitted. */
- if (key_input_type == 1) {
- expected_status = PSA_SUCCESS;
- }
- psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
- psa_set_key_algorithm(&attributes4, alg);
- psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
- psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
- TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
- &derived_key), expected_status);
- } else { // output bytes
- /* Expansion phase. */
- for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
- /* Read some bytes. */
- status = psa_key_derivation_output_bytes(&operation,
- output_buffer, output_sizes[i]);
- if (expected_capacity == 0 && output_sizes[i] == 0) {
- /* Reading 0 bytes when 0 bytes are available can go either way. */
- TEST_ASSERT(status == PSA_SUCCESS ||
- status == PSA_ERROR_INSUFFICIENT_DATA);
- continue;
- } else if (expected_capacity == 0 ||
- output_sizes[i] > expected_capacity) {
- /* Capacity exceeded. */
- TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
- expected_capacity = 0;
- continue;
- }
- /* Success. Check the read data. */
- PSA_ASSERT(status);
- if (output_sizes[i] != 0) {
- ASSERT_COMPARE(output_buffer, output_sizes[i],
- expected_outputs[i], output_sizes[i]);
- }
- /* Check the operation status. */
- expected_capacity -= output_sizes[i];
- PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
- ¤t_capacity));
- TEST_EQUAL(expected_capacity, current_capacity);
- }
- }
- PSA_ASSERT(psa_key_derivation_abort(&operation));
- exit:
- mbedtls_free(output_buffer);
- psa_key_derivation_abort(&operation);
- for (i = 0; i < ARRAY_LENGTH(keys); i++) {
- psa_destroy_key(keys[i]);
- }
- psa_destroy_key(derived_key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_full(int alg_arg,
- data_t *key_data,
- data_t *input1,
- data_t *input2,
- int requested_capacity_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_algorithm_t alg = alg_arg;
- size_t requested_capacity = requested_capacity_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- unsigned char output_buffer[16];
- size_t expected_capacity = requested_capacity;
- size_t current_capacity;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &key));
- if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
- input1->x, input1->len,
- input2->x, input2->len,
- requested_capacity)) {
- goto exit;
- }
- PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
- ¤t_capacity));
- TEST_EQUAL(current_capacity, expected_capacity);
- /* Expansion phase. */
- while (current_capacity > 0) {
- size_t read_size = sizeof(output_buffer);
- if (read_size > current_capacity) {
- read_size = current_capacity;
- }
- PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
- output_buffer,
- read_size));
- expected_capacity -= read_size;
- PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
- ¤t_capacity));
- TEST_EQUAL(current_capacity, expected_capacity);
- }
- /* Check that the operation refuses to go over capacity. */
- TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
- PSA_ERROR_INSUFFICIENT_DATA);
- PSA_ASSERT(psa_key_derivation_abort(&operation));
- exit:
- psa_key_derivation_abort(&operation);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
- void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
- int derivation_step,
- int capacity, int expected_capacity_status_arg,
- data_t *expected_output,
- int expected_output_status_arg)
- {
- psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
- uint8_t *output_buffer = NULL;
- psa_status_t status;
- psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
- psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
- psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
- ASSERT_ALLOC(output_buffer, expected_output->len);
- PSA_ASSERT(psa_crypto_init());
- PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
- TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
- expected_capacity_status);
- TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
- step, input->x, input->len),
- expected_input_status);
- if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
- goto exit;
- }
- status = psa_key_derivation_output_bytes(&operation, output_buffer,
- expected_output->len);
- TEST_EQUAL(status, expected_output_status);
- if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
- ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
- expected_output->len);
- }
- exit:
- mbedtls_free(output_buffer);
- psa_key_derivation_abort(&operation);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_key_exercise(int alg_arg,
- data_t *key_data,
- data_t *input1,
- data_t *input2,
- int derived_type_arg,
- int derived_bits_arg,
- int derived_usage_arg,
- int derived_alg_arg)
- {
- mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
- mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_algorithm_t alg = alg_arg;
- psa_key_type_t derived_type = derived_type_arg;
- size_t derived_bits = derived_bits_arg;
- psa_key_usage_t derived_usage = derived_usage_arg;
- psa_algorithm_t derived_alg = derived_alg_arg;
- size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
- PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
- &base_key));
- /* Derive a key. */
- if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
- input1->x, input1->len,
- input2->x, input2->len,
- capacity)) {
- goto exit;
- }
- psa_set_key_usage_flags(&attributes, derived_usage);
- psa_set_key_algorithm(&attributes, derived_alg);
- psa_set_key_type(&attributes, derived_type);
- psa_set_key_bits(&attributes, derived_bits);
- PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
- &derived_key));
- /* Test the key information */
- PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
- TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
- TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
- /* Exercise the derived key. */
- if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
- goto exit;
- }
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&got_attributes);
- psa_key_derivation_abort(&operation);
- psa_destroy_key(base_key);
- psa_destroy_key(derived_key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_key_export(int alg_arg,
- data_t *key_data,
- data_t *input1,
- data_t *input2,
- int bytes1_arg,
- int bytes2_arg)
- {
- mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
- mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_algorithm_t alg = alg_arg;
- size_t bytes1 = bytes1_arg;
- size_t bytes2 = bytes2_arg;
- size_t capacity = bytes1 + bytes2;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- uint8_t *output_buffer = NULL;
- uint8_t *export_buffer = NULL;
- psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
- size_t length;
- ASSERT_ALLOC(output_buffer, capacity);
- ASSERT_ALLOC(export_buffer, capacity);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&base_attributes, alg);
- psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
- PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
- &base_key));
- /* Derive some material and output it. */
- if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
- input1->x, input1->len,
- input2->x, input2->len,
- capacity)) {
- goto exit;
- }
- PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
- output_buffer,
- capacity));
- PSA_ASSERT(psa_key_derivation_abort(&operation));
- /* Derive the same output again, but this time store it in key objects. */
- if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
- input1->x, input1->len,
- input2->x, input2->len,
- capacity)) {
- goto exit;
- }
- psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
- psa_set_key_algorithm(&derived_attributes, 0);
- psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
- psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
- PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
- &derived_key));
- PSA_ASSERT(psa_export_key(derived_key,
- export_buffer, bytes1,
- &length));
- TEST_EQUAL(length, bytes1);
- PSA_ASSERT(psa_destroy_key(derived_key));
- psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
- PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
- &derived_key));
- PSA_ASSERT(psa_export_key(derived_key,
- export_buffer + bytes1, bytes2,
- &length));
- TEST_EQUAL(length, bytes2);
- /* Compare the outputs from the two runs. */
- ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
- export_buffer, capacity);
- exit:
- mbedtls_free(output_buffer);
- mbedtls_free(export_buffer);
- psa_key_derivation_abort(&operation);
- psa_destroy_key(base_key);
- psa_destroy_key(derived_key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_key_type(int alg_arg,
- data_t *key_data,
- data_t *input1,
- data_t *input2,
- int key_type_arg, int bits_arg,
- data_t *expected_export)
- {
- mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
- mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
- const psa_algorithm_t alg = alg_arg;
- const psa_key_type_t key_type = key_type_arg;
- const size_t bits = bits_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- const size_t export_buffer_size =
- PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
- uint8_t *export_buffer = NULL;
- psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
- size_t export_length;
- ASSERT_ALLOC(export_buffer, export_buffer_size);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&base_attributes, alg);
- psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
- PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
- &base_key));
- if (mbedtls_test_psa_setup_key_derivation_wrap(
- &operation, base_key, alg,
- input1->x, input1->len,
- input2->x, input2->len,
- PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
- goto exit;
- }
- psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
- psa_set_key_algorithm(&derived_attributes, 0);
- psa_set_key_type(&derived_attributes, key_type);
- psa_set_key_bits(&derived_attributes, bits);
- PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
- &derived_key));
- PSA_ASSERT(psa_export_key(derived_key,
- export_buffer, export_buffer_size,
- &export_length));
- ASSERT_COMPARE(export_buffer, export_length,
- expected_export->x, expected_export->len);
- exit:
- mbedtls_free(export_buffer);
- psa_key_derivation_abort(&operation);
- psa_destroy_key(base_key);
- psa_destroy_key(derived_key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void derive_key(int alg_arg,
- data_t *key_data, data_t *input1, data_t *input2,
- int type_arg, int bits_arg,
- int expected_status_arg,
- int is_large_output)
- {
- mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
- mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_algorithm_t alg = alg_arg;
- psa_key_type_t type = type_arg;
- size_t bits = bits_arg;
- psa_status_t expected_status = expected_status_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&base_attributes, alg);
- psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
- PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
- &base_key));
- if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
- input1->x, input1->len,
- input2->x, input2->len,
- SIZE_MAX)) {
- goto exit;
- }
- psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
- psa_set_key_algorithm(&derived_attributes, 0);
- psa_set_key_type(&derived_attributes, type);
- psa_set_key_bits(&derived_attributes, bits);
- psa_status_t status =
- psa_key_derivation_output_key(&derived_attributes,
- &operation,
- &derived_key);
- if (is_large_output > 0) {
- TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
- }
- TEST_EQUAL(status, expected_status);
- exit:
- psa_key_derivation_abort(&operation);
- psa_destroy_key(base_key);
- psa_destroy_key(derived_key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void key_agreement_setup(int alg_arg,
- int our_key_type_arg, int our_key_alg_arg,
- data_t *our_key_data, data_t *peer_key_data,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_algorithm_t alg = alg_arg;
- psa_algorithm_t our_key_alg = our_key_alg_arg;
- psa_key_type_t our_key_type = our_key_type_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_status_t expected_status = expected_status_arg;
- psa_status_t status;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes, our_key_alg);
- psa_set_key_type(&attributes, our_key_type);
- PSA_ASSERT(psa_import_key(&attributes,
- our_key_data->x, our_key_data->len,
- &our_key));
- /* The tests currently include inputs that should fail at either step.
- * Test cases that fail at the setup step should be changed to call
- * key_derivation_setup instead, and this function should be renamed
- * to key_agreement_fail. */
- status = psa_key_derivation_setup(&operation, alg);
- if (status == PSA_SUCCESS) {
- TEST_EQUAL(psa_key_derivation_key_agreement(
- &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
- our_key,
- peer_key_data->x, peer_key_data->len),
- expected_status);
- } else {
- TEST_ASSERT(status == expected_status);
- }
- exit:
- psa_key_derivation_abort(&operation);
- psa_destroy_key(our_key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void raw_key_agreement(int alg_arg,
- int our_key_type_arg, data_t *our_key_data,
- data_t *peer_key_data,
- data_t *expected_output)
- {
- mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_algorithm_t alg = alg_arg;
- psa_key_type_t our_key_type = our_key_type_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- unsigned char *output = NULL;
- size_t output_length = ~0;
- size_t key_bits;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, our_key_type);
- PSA_ASSERT(psa_import_key(&attributes,
- our_key_data->x, our_key_data->len,
- &our_key));
- PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
- key_bits = psa_get_key_bits(&attributes);
- /* Validate size macros */
- TEST_LE_U(expected_output->len,
- PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
- TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
- PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
- /* Good case with exact output size */
- ASSERT_ALLOC(output, expected_output->len);
- PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
- peer_key_data->x, peer_key_data->len,
- output, expected_output->len,
- &output_length));
- ASSERT_COMPARE(output, output_length,
- expected_output->x, expected_output->len);
- mbedtls_free(output);
- output = NULL;
- output_length = ~0;
- /* Larger buffer */
- ASSERT_ALLOC(output, expected_output->len + 1);
- PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
- peer_key_data->x, peer_key_data->len,
- output, expected_output->len + 1,
- &output_length));
- ASSERT_COMPARE(output, output_length,
- expected_output->x, expected_output->len);
- mbedtls_free(output);
- output = NULL;
- output_length = ~0;
- /* Buffer too small */
- ASSERT_ALLOC(output, expected_output->len - 1);
- TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
- peer_key_data->x, peer_key_data->len,
- output, expected_output->len - 1,
- &output_length),
- PSA_ERROR_BUFFER_TOO_SMALL);
- /* Not required by the spec, but good robustness */
- TEST_LE_U(output_length, expected_output->len - 1);
- mbedtls_free(output);
- output = NULL;
- exit:
- mbedtls_free(output);
- psa_destroy_key(our_key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void key_agreement_capacity(int alg_arg,
- int our_key_type_arg, data_t *our_key_data,
- data_t *peer_key_data,
- int expected_capacity_arg)
- {
- mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_algorithm_t alg = alg_arg;
- psa_key_type_t our_key_type = our_key_type_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- size_t actual_capacity;
- unsigned char output[16];
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, our_key_type);
- PSA_ASSERT(psa_import_key(&attributes,
- our_key_data->x, our_key_data->len,
- &our_key));
- PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
- PSA_ASSERT(psa_key_derivation_key_agreement(
- &operation,
- PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
- peer_key_data->x, peer_key_data->len));
- if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
- /* The test data is for info="" */
- PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
- PSA_KEY_DERIVATION_INPUT_INFO,
- NULL, 0));
- }
- /* Test the advertised capacity. */
- PSA_ASSERT(psa_key_derivation_get_capacity(
- &operation, &actual_capacity));
- TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
- /* Test the actual capacity by reading the output. */
- while (actual_capacity > sizeof(output)) {
- PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
- output, sizeof(output)));
- actual_capacity -= sizeof(output);
- }
- PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
- output, actual_capacity));
- TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
- PSA_ERROR_INSUFFICIENT_DATA);
- exit:
- psa_key_derivation_abort(&operation);
- psa_destroy_key(our_key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void key_agreement_output(int alg_arg,
- int our_key_type_arg, data_t *our_key_data,
- data_t *peer_key_data,
- data_t *expected_output1, data_t *expected_output2)
- {
- mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_algorithm_t alg = alg_arg;
- psa_key_type_t our_key_type = our_key_type_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- uint8_t *actual_output = NULL;
- ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
- expected_output2->len));
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, our_key_type);
- PSA_ASSERT(psa_import_key(&attributes,
- our_key_data->x, our_key_data->len,
- &our_key));
- PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
- PSA_ASSERT(psa_key_derivation_key_agreement(
- &operation,
- PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
- peer_key_data->x, peer_key_data->len));
- if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
- /* The test data is for info="" */
- PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
- PSA_KEY_DERIVATION_INPUT_INFO,
- NULL, 0));
- }
- PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
- actual_output,
- expected_output1->len));
- ASSERT_COMPARE(actual_output, expected_output1->len,
- expected_output1->x, expected_output1->len);
- if (expected_output2->len != 0) {
- PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
- actual_output,
- expected_output2->len));
- ASSERT_COMPARE(actual_output, expected_output2->len,
- expected_output2->x, expected_output2->len);
- }
- exit:
- psa_key_derivation_abort(&operation);
- psa_destroy_key(our_key);
- PSA_DONE();
- mbedtls_free(actual_output);
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void generate_random(int bytes_arg)
- {
- size_t bytes = bytes_arg;
- unsigned char *output = NULL;
- unsigned char *changed = NULL;
- size_t i;
- unsigned run;
- TEST_ASSERT(bytes_arg >= 0);
- ASSERT_ALLOC(output, bytes);
- ASSERT_ALLOC(changed, bytes);
- PSA_ASSERT(psa_crypto_init());
- /* Run several times, to ensure that every output byte will be
- * nonzero at least once with overwhelming probability
- * (2^(-8*number_of_runs)). */
- for (run = 0; run < 10; run++) {
- if (bytes != 0) {
- memset(output, 0, bytes);
- }
- PSA_ASSERT(psa_generate_random(output, bytes));
- for (i = 0; i < bytes; i++) {
- if (output[i] != 0) {
- ++changed[i];
- }
- }
- }
- /* Check that every byte was changed to nonzero at least once. This
- * validates that psa_generate_random is overwriting every byte of
- * the output buffer. */
- for (i = 0; i < bytes; i++) {
- TEST_ASSERT(changed[i] != 0);
- }
- exit:
- PSA_DONE();
- mbedtls_free(output);
- mbedtls_free(changed);
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void generate_key(int type_arg,
- int bits_arg,
- int usage_arg,
- int alg_arg,
- int expected_status_arg,
- int is_large_key)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t type = type_arg;
- psa_key_usage_t usage = usage_arg;
- size_t bits = bits_arg;
- psa_algorithm_t alg = alg_arg;
- psa_status_t expected_status = expected_status_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, usage);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, type);
- psa_set_key_bits(&attributes, bits);
- /* Generate a key */
- psa_status_t status = psa_generate_key(&attributes, &key);
- if (is_large_key > 0) {
- TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
- }
- TEST_EQUAL(status, expected_status);
- if (expected_status != PSA_SUCCESS) {
- goto exit;
- }
- /* Test the key information */
- PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
- TEST_EQUAL(psa_get_key_type(&got_attributes), type);
- TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
- /* Do something with the key according to its type and permitted usage. */
- if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
- goto exit;
- }
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&got_attributes);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* 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 */
- void generate_key_rsa(int bits_arg,
- data_t *e_arg,
- int expected_status_arg)
- {
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
- size_t bits = bits_arg;
- psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
- psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
- psa_status_t expected_status = expected_status_arg;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- uint8_t *exported = NULL;
- size_t exported_size =
- PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
- size_t exported_length = SIZE_MAX;
- uint8_t *e_read_buffer = NULL;
- int is_default_public_exponent = 0;
- size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
- size_t e_read_length = SIZE_MAX;
- if (e_arg->len == 0 ||
- (e_arg->len == 3 &&
- e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
- is_default_public_exponent = 1;
- e_read_size = 0;
- }
- ASSERT_ALLOC(e_read_buffer, e_read_size);
- ASSERT_ALLOC(exported, exported_size);
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_usage_flags(&attributes, usage);
- psa_set_key_algorithm(&attributes, alg);
- PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
- e_arg->x, e_arg->len));
- psa_set_key_bits(&attributes, bits);
- /* Generate a key */
- TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
- if (expected_status != PSA_SUCCESS) {
- goto exit;
- }
- /* Test the key information */
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- TEST_EQUAL(psa_get_key_type(&attributes), type);
- TEST_EQUAL(psa_get_key_bits(&attributes), bits);
- PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
- e_read_buffer, e_read_size,
- &e_read_length));
- if (is_default_public_exponent) {
- TEST_EQUAL(e_read_length, 0);
- } else {
- ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
- }
- /* Do something with the key according to its type and permitted usage. */
- if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
- goto exit;
- }
- /* Export the key and check the public exponent. */
- PSA_ASSERT(psa_export_public_key(key,
- exported, exported_size,
- &exported_length));
- {
- uint8_t *p = exported;
- uint8_t *end = exported + exported_length;
- size_t len;
- /* RSAPublicKey ::= SEQUENCE {
- * modulus INTEGER, -- n
- * publicExponent INTEGER } -- e
- */
- TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
- MBEDTLS_ASN1_SEQUENCE |
- MBEDTLS_ASN1_CONSTRUCTED));
- TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
- TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
- MBEDTLS_ASN1_INTEGER));
- if (len >= 1 && p[0] == 0) {
- ++p;
- --len;
- }
- if (e_arg->len == 0) {
- TEST_EQUAL(len, 3);
- TEST_EQUAL(p[0], 1);
- TEST_EQUAL(p[1], 0);
- TEST_EQUAL(p[2], 1);
- } else {
- ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
- }
- }
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes() or
- * set by psa_set_key_domain_parameters() thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- psa_destroy_key(key);
- PSA_DONE();
- mbedtls_free(e_read_buffer);
- mbedtls_free(exported);
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
- void persistent_key_load_key_from_storage(data_t *data,
- int type_arg, int bits_arg,
- int usage_flags_arg, int alg_arg,
- int generation_method)
- {
- mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_type_t type = type_arg;
- size_t bits = bits_arg;
- psa_key_usage_t usage_flags = usage_flags_arg;
- psa_algorithm_t alg = alg_arg;
- psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
- unsigned char *first_export = NULL;
- unsigned char *second_export = NULL;
- size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
- size_t first_exported_length;
- size_t second_exported_length;
- if (usage_flags & PSA_KEY_USAGE_EXPORT) {
- ASSERT_ALLOC(first_export, export_size);
- ASSERT_ALLOC(second_export, export_size);
- }
- PSA_ASSERT(psa_crypto_init());
- psa_set_key_id(&attributes, key_id);
- psa_set_key_usage_flags(&attributes, usage_flags);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, type);
- psa_set_key_bits(&attributes, bits);
- switch (generation_method) {
- case IMPORT_KEY:
- /* Import the key */
- PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
- &key));
- break;
- case GENERATE_KEY:
- /* Generate a key */
- PSA_ASSERT(psa_generate_key(&attributes, &key));
- break;
- case DERIVE_KEY:
- #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
- {
- /* Create base key */
- psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
- psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_set_key_usage_flags(&base_attributes,
- PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&base_attributes, derive_alg);
- psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
- PSA_ASSERT(psa_import_key(&base_attributes,
- data->x, data->len,
- &base_key));
- /* Derive a key. */
- PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
- PSA_ASSERT(psa_key_derivation_input_key(
- &operation,
- PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
- PSA_ASSERT(psa_key_derivation_input_bytes(
- &operation, PSA_KEY_DERIVATION_INPUT_INFO,
- NULL, 0));
- PSA_ASSERT(psa_key_derivation_output_key(&attributes,
- &operation,
- &key));
- PSA_ASSERT(psa_key_derivation_abort(&operation));
- PSA_ASSERT(psa_destroy_key(base_key));
- base_key = MBEDTLS_SVC_KEY_ID_INIT;
- }
- #else
- TEST_ASSUME(!"KDF not supported in this configuration");
- #endif
- break;
- default:
- TEST_ASSERT(!"generation_method not implemented in test");
- break;
- }
- psa_reset_key_attributes(&attributes);
- /* Export the key if permitted by the key policy. */
- if (usage_flags & PSA_KEY_USAGE_EXPORT) {
- PSA_ASSERT(psa_export_key(key,
- first_export, export_size,
- &first_exported_length));
- if (generation_method == IMPORT_KEY) {
- ASSERT_COMPARE(data->x, data->len,
- first_export, first_exported_length);
- }
- }
- /* Shutdown and restart */
- PSA_ASSERT(psa_purge_key(key));
- PSA_DONE();
- PSA_ASSERT(psa_crypto_init());
- /* Check key slot still contains key data */
- PSA_ASSERT(psa_get_key_attributes(key, &attributes));
- TEST_ASSERT(mbedtls_svc_key_id_equal(
- psa_get_key_id(&attributes), key_id));
- TEST_EQUAL(psa_get_key_lifetime(&attributes),
- PSA_KEY_LIFETIME_PERSISTENT);
- TEST_EQUAL(psa_get_key_type(&attributes), type);
- TEST_EQUAL(psa_get_key_bits(&attributes), bits);
- TEST_EQUAL(psa_get_key_usage_flags(&attributes),
- mbedtls_test_update_key_usage_flags(usage_flags));
- TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
- /* Export the key again if permitted by the key policy. */
- if (usage_flags & PSA_KEY_USAGE_EXPORT) {
- PSA_ASSERT(psa_export_key(key,
- second_export, export_size,
- &second_exported_length));
- ASSERT_COMPARE(first_export, first_exported_length,
- second_export, second_exported_length);
- }
- /* Do something with the key according to its type and permitted usage. */
- if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
- goto exit;
- }
- exit:
- /*
- * Key attributes may have been returned by psa_get_key_attributes()
- * thus reset them as required.
- */
- psa_reset_key_attributes(&attributes);
- mbedtls_free(first_export);
- mbedtls_free(second_export);
- psa_key_derivation_abort(&operation);
- psa_destroy_key(base_key);
- psa_destroy_key(key);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
- void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
- int primitive_arg, int hash_arg, int role_arg,
- int test_input, data_t *pw_data,
- int inj_err_type_arg,
- int expected_error_arg)
- {
- psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
- psa_pake_operation_t operation = psa_pake_operation_init();
- psa_algorithm_t alg = alg_arg;
- psa_pake_primitive_t primitive = primitive_arg;
- psa_key_type_t key_type_pw = key_type_pw_arg;
- psa_key_usage_t key_usage_pw = key_usage_pw_arg;
- psa_algorithm_t hash_alg = hash_arg;
- psa_pake_role_t role = role_arg;
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
- psa_status_t expected_error = expected_error_arg;
- psa_status_t status;
- unsigned char *output_buffer = NULL;
- size_t output_len = 0;
- PSA_INIT();
- size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
- PSA_PAKE_STEP_KEY_SHARE);
- ASSERT_ALLOC(output_buffer, buf_size);
- if (pw_data->len > 0) {
- psa_set_key_usage_flags(&attributes, key_usage_pw);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, key_type_pw);
- PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
- &key));
- }
- psa_pake_cs_set_algorithm(&cipher_suite, alg);
- psa_pake_cs_set_primitive(&cipher_suite, primitive);
- psa_pake_cs_set_hash(&cipher_suite, hash_alg);
- PSA_ASSERT(psa_pake_abort(&operation));
- if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
- TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
- expected_error);
- PSA_ASSERT(psa_pake_abort(&operation));
- TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
- expected_error);
- PSA_ASSERT(psa_pake_abort(&operation));
- TEST_EQUAL(psa_pake_set_password_key(&operation, key),
- expected_error);
- PSA_ASSERT(psa_pake_abort(&operation));
- TEST_EQUAL(psa_pake_set_role(&operation, role),
- expected_error);
- PSA_ASSERT(psa_pake_abort(&operation));
- TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
- NULL, 0, NULL),
- expected_error);
- PSA_ASSERT(psa_pake_abort(&operation));
- TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
- expected_error);
- PSA_ASSERT(psa_pake_abort(&operation));
- goto exit;
- }
- status = psa_pake_setup(&operation, &cipher_suite);
- if (status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_error);
- goto exit;
- }
- if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
- TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
- expected_error);
- goto exit;
- }
- status = psa_pake_set_role(&operation, role);
- if (status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_error);
- goto exit;
- }
- if (pw_data->len > 0) {
- status = psa_pake_set_password_key(&operation, key);
- if (status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_error);
- goto exit;
- }
- }
- if (inj_err_type == INJECT_ERR_INVALID_USER) {
- TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
- PSA_ERROR_INVALID_ARGUMENT);
- goto exit;
- }
- if (inj_err_type == INJECT_ERR_INVALID_PEER) {
- TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
- PSA_ERROR_INVALID_ARGUMENT);
- goto exit;
- }
- if (inj_err_type == INJECT_ERR_SET_USER) {
- const uint8_t unsupported_id[] = "abcd";
- TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
- PSA_ERROR_NOT_SUPPORTED);
- goto exit;
- }
- if (inj_err_type == INJECT_ERR_SET_PEER) {
- const uint8_t unsupported_id[] = "abcd";
- TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
- PSA_ERROR_NOT_SUPPORTED);
- goto exit;
- }
- const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
- PSA_PAKE_STEP_KEY_SHARE);
- const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
- PSA_PAKE_STEP_ZK_PUBLIC);
- const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
- PSA_PAKE_STEP_ZK_PROOF);
- if (test_input) {
- if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
- TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
- PSA_ERROR_INVALID_ARGUMENT);
- goto exit;
- }
- if (inj_err_type == INJECT_UNKNOWN_STEP) {
- TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
- output_buffer, size_zk_proof),
- PSA_ERROR_INVALID_ARGUMENT);
- goto exit;
- }
- if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
- TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
- output_buffer, size_zk_proof),
- PSA_ERROR_BAD_STATE);
- goto exit;
- }
- status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
- output_buffer, size_key_share);
- if (status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_error);
- goto exit;
- }
- if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
- TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
- output_buffer, size_zk_public + 1),
- PSA_ERROR_INVALID_ARGUMENT);
- goto exit;
- }
- if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
- // Just trigger any kind of error. We don't care about the result here
- psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
- output_buffer, size_zk_public + 1);
- TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
- output_buffer, size_zk_public),
- PSA_ERROR_BAD_STATE);
- goto exit;
- }
- } else {
- if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
- TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
- NULL, 0, NULL),
- PSA_ERROR_INVALID_ARGUMENT);
- goto exit;
- }
- if (inj_err_type == INJECT_UNKNOWN_STEP) {
- TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
- output_buffer, buf_size, &output_len),
- PSA_ERROR_INVALID_ARGUMENT);
- goto exit;
- }
- if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
- TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
- output_buffer, buf_size, &output_len),
- PSA_ERROR_BAD_STATE);
- goto exit;
- }
- status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
- output_buffer, buf_size, &output_len);
- if (status != PSA_SUCCESS) {
- TEST_EQUAL(status, expected_error);
- goto exit;
- }
- TEST_ASSERT(output_len > 0);
- if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
- TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
- output_buffer, size_zk_public - 1, &output_len),
- PSA_ERROR_BUFFER_TOO_SMALL);
- goto exit;
- }
- if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
- // Just trigger any kind of error. We don't care about the result here
- psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
- output_buffer, size_zk_public - 1, &output_len);
- TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
- output_buffer, buf_size, &output_len),
- PSA_ERROR_BAD_STATE);
- goto exit;
- }
- }
- exit:
- PSA_ASSERT(psa_destroy_key(key));
- PSA_ASSERT(psa_pake_abort(&operation));
- mbedtls_free(output_buffer);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
- void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
- int client_input_first, int inject_error,
- data_t *pw_data)
- {
- psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
- psa_pake_operation_t server = psa_pake_operation_init();
- psa_pake_operation_t client = psa_pake_operation_init();
- psa_algorithm_t alg = alg_arg;
- psa_algorithm_t hash_alg = hash_arg;
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- PSA_INIT();
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
- PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
- &key));
- psa_pake_cs_set_algorithm(&cipher_suite, alg);
- psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
- psa_pake_cs_set_hash(&cipher_suite, hash_alg);
- PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
- PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
- PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
- PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
- PSA_ASSERT(psa_pake_set_password_key(&server, key));
- PSA_ASSERT(psa_pake_set_password_key(&client, key));
- ecjpake_do_round(alg, primitive_arg, &server, &client,
- client_input_first, 1, inject_error);
- if (inject_error == 1 || inject_error == 2) {
- goto exit;
- }
- ecjpake_do_round(alg, primitive_arg, &server, &client,
- client_input_first, 2, inject_error);
- exit:
- psa_destroy_key(key);
- psa_pake_abort(&server);
- psa_pake_abort(&client);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
- void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
- int derive_alg_arg, data_t *pw_data,
- int client_input_first, int inj_err_type_arg)
- {
- psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
- psa_pake_operation_t server = psa_pake_operation_init();
- psa_pake_operation_t client = psa_pake_operation_init();
- psa_algorithm_t alg = alg_arg;
- psa_algorithm_t hash_alg = hash_arg;
- psa_algorithm_t derive_alg = derive_alg_arg;
- mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- psa_key_derivation_operation_t server_derive =
- PSA_KEY_DERIVATION_OPERATION_INIT;
- psa_key_derivation_operation_t client_derive =
- PSA_KEY_DERIVATION_OPERATION_INIT;
- ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
- PSA_INIT();
- psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
- psa_set_key_algorithm(&attributes, alg);
- psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
- PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
- &key));
- psa_pake_cs_set_algorithm(&cipher_suite, alg);
- psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
- psa_pake_cs_set_hash(&cipher_suite, hash_alg);
- /* Get shared key */
- PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
- PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
- if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
- PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
- PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
- PSA_KEY_DERIVATION_INPUT_SEED,
- (const uint8_t *) "", 0));
- PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
- PSA_KEY_DERIVATION_INPUT_SEED,
- (const uint8_t *) "", 0));
- }
- PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
- PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
- PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
- PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
- PSA_ASSERT(psa_pake_set_password_key(&server, key));
- PSA_ASSERT(psa_pake_set_password_key(&client, key));
- if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
- TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
- PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
- PSA_ERROR_BAD_STATE);
- goto exit;
- }
- /* First round */
- ecjpake_do_round(alg, primitive_arg, &server, &client,
- client_input_first, 1, 0);
- if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
- TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
- PSA_ERROR_BAD_STATE);
- TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
- PSA_ERROR_BAD_STATE);
- goto exit;
- }
- /* Second round */
- ecjpake_do_round(alg, primitive_arg, &server, &client,
- client_input_first, 2, 0);
- PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
- PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
- exit:
- psa_key_derivation_abort(&server_derive);
- psa_key_derivation_abort(&client_derive);
- psa_destroy_key(key);
- psa_pake_abort(&server);
- psa_pake_abort(&client);
- PSA_DONE();
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void ecjpake_size_macros()
- {
- const psa_algorithm_t alg = PSA_ALG_JPAKE;
- const size_t bits = 256;
- const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
- PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
- const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
- PSA_ECC_FAMILY_SECP_R1);
- // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
- /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
- TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
- PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
- TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
- PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
- /* The output for ZK_PROOF is the same bitsize as the curve */
- TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
- PSA_BITS_TO_BYTES(bits));
- /* Input sizes are the same as output sizes */
- TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
- PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
- TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
- PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
- TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
- PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
- /* These inequalities will always hold even when other PAKEs are added */
- TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
- PSA_PAKE_OUTPUT_MAX_SIZE);
- TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
- PSA_PAKE_OUTPUT_MAX_SIZE);
- TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
- PSA_PAKE_OUTPUT_MAX_SIZE);
- TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
- PSA_PAKE_INPUT_MAX_SIZE);
- TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
- PSA_PAKE_INPUT_MAX_SIZE);
- TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
- PSA_PAKE_INPUT_MAX_SIZE);
- }
- /* END_CASE */
|