12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537 |
- /* BEGIN_HEADER */
- #include <stdint.h>
- #include "mbedtls/asn1.h"
- #include "mbedtls/asn1write.h"
- #include "mbedtls/oid.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"
- #include "test/asn1_helpers.h"
- #include "test/psa_crypto_helpers.h"
- #include "test/psa_exercise_key.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 )
- /** 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 );
- }
- /* 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 );
- }
- 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;
- /* END_HEADER */
- /* BEGIN_DEPENDENCIES
- * depends_on:MBEDTLS_PSA_CRYPTO_C
- * END_DEPENDENCIES
- */
- /* 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_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
- #if defined(MBEDTLS_TEST_DEPRECATED)
- /* Check deprecated constants. */
- TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
- TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
- TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
- TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
- TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
- TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
- TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
- TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
- TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
- TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
- TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
- TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
- TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
- TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
- TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
- TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
- TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
- TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
- TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
- TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
- TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
- #endif
- }
- /* 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 */
- 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
- * accomodate 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 */
- 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 expected_bits,
- int export_size_delta,
- int expected_export_status_arg,
- 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;
- 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_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_ASSERT( 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( ! 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_ASSERT( exported_length <=
- PSA_EXPORT_KEY_OUTPUT_SIZE( type,
- psa_get_key_bits( &got_attributes ) ) );
- TEST_ASSERT( 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 );
- mbedtls_free( exported );
- mbedtls_free( reexported );
- PSA_DONE( );
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void import_export_public_key( data_t *data,
- int type_arg,
- int alg_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;
- 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_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_ASSERT( expected_public_key->len <=
- PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
- TEST_ASSERT( expected_public_key->len <=
- PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
- TEST_ASSERT( 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 supress 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 );
- /* 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 );
- 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;
- 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 ) );
- /* Check if no key usage flag implication is done */
- TEST_EQUAL( policy_usage,
- mbedtls_test_update_key_usage_flags( policy_usage ) );
- 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 );
- 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 );
- 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_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_ASSERT( nonce_length <= sizeof( nonce ) );
- TEST_ASSERT( 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 ) );
- 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 );
- 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 );
- exit:
- 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,
- int type_arg, data_t *material,
- int copy_attributes,
- int target_usage_arg,
- int target_alg_arg, int target_alg2_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;
- 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_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;
- /* Set volatile lifetime to reset the key identifier to 0. */
- psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
- }
- 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( ! 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 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_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 supress 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;
- 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( ) );
- 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:
- 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_status_t expected_status = expected_status_arg;
- psa_status_t status;
- ASSERT_ALLOC( output, output_size );
- PSA_ASSERT( psa_crypto_init( ) );
- status = psa_hash_compute( alg, input->x, input->len,
- output, output_size, &output_length );
- TEST_EQUAL( status, expected_status );
- TEST_ASSERT( output_length <= output_size );
- exit:
- 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_status_t status;
- PSA_ASSERT( psa_crypto_init( ) );
- status = psa_hash_compare( alg, input->x, input->len,
- reference_hash->x, reference_hash->len );
- TEST_EQUAL( status, expected_status );
- exit:
- 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;
- size_t i;
- PSA_ASSERT( psa_crypto_init( ) );
- /* Compute with tight buffer */
- 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 larger buffer */
- 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 );
- /* Compare with correct hash */
- PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
- output, output_length ) );
- /* Compare with trailing garbage */
- TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
- output, output_length + 1 ),
- PSA_ERROR_INVALID_SIGNATURE );
- /* Compare with truncated hash */
- TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
- 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;
- TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
- output, output_length ),
- PSA_ERROR_INVALID_SIGNATURE );
- output[i] ^= 1;
- }
- exit:
- 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 supress 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( 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_ASSERT( 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_ASSERT( 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 supress 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 *output = NULL;
- size_t output_buffer_size = 0;
- size_t output_length = 0;
- 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 ) );
- }
- status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
- output_buffer_size, &output_length );
- TEST_EQUAL( status, expected_status );
- exit:
- mbedtls_free( output );
- psa_destroy_key( key );
- PSA_DONE( );
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_encrypt_alg_without_iv( int alg_arg,
- int key_type_arg,
- data_t *key_data,
- data_t *input,
- 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;
- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
- uint8_t iv[1] = { 0x5a };
- size_t iv_length;
- 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_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 ) );
- 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_encrypt_setup( &operation, key, alg ) );
- TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
- &iv_length ),
- PSA_ERROR_BAD_STATE );
- PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
- output_buffer_size, &output_length ) );
- TEST_ASSERT( output_length <=
- PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
- TEST_ASSERT( output_length <=
- PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
- ASSERT_COMPARE( expected_output->x, expected_output->len,
- output, output_length );
- exit:
- mbedtls_free( output );
- 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_ASSERT( output1_length <=
- PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
- TEST_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
- TEST_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
- TEST_ASSERT( 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_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
- TEST_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
- alg,
- input->len - first_part_size ) );
- TEST_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
- TEST_ASSERT( 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_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
- TEST_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
- alg,
- input->len - first_part_size ) );
- TEST_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
- TEST_ASSERT( 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;
- size_t output_buffer_size = 0;
- size_t output_length = 0;
- 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 );
- status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
- output_buffer_size, &output_length );
- TEST_EQUAL( status, expected_status );
- exit:
- mbedtls_free( input );
- mbedtls_free( output );
- 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_ASSERT( output_length <=
- PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
- TEST_ASSERT( 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_ASSERT( output1_length <=
- PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
- TEST_ASSERT( 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_ASSERT( output2_length <=
- PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
- TEST_ASSERT( 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_ASSERT( output1_buffer_size <=
- PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
- ASSERT_ALLOC( output1, output1_buffer_size );
- TEST_ASSERT( first_part_size <= input->len );
- PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
- output1, output1_buffer_size,
- &function_output_length ) );
- TEST_ASSERT( function_output_length <=
- PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
- TEST_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
- alg,
- input->len - first_part_size ) );
- TEST_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
- TEST_ASSERT( 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_ASSERT( output2_buffer_size <=
- PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
- TEST_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
- TEST_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
- alg,
- output1_length - first_part_size ) );
- TEST_ASSERT( 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_ASSERT( function_output_length <=
- PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
- TEST_ASSERT( 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_ASSERT( 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_ASSERT( 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_ASSERT( 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_ASSERT( 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 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 );
- #if defined(MBEDTLS_TEST_DEPRECATED)
- TEST_EQUAL( actual_size,
- PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
- #endif /* MBEDTLS_TEST_DEPRECATED */
- 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 advertized by the
- * library. */
- signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
- key_bits, alg );
- TEST_ASSERT( signature_size != 0 );
- TEST_ASSERT( 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 );
- #if defined(MBEDTLS_TEST_DEPRECATED)
- memset( signature, 0, signature_size );
- signature_length = INVALID_EXPORT_LENGTH;
- PSA_ASSERT( psa_asymmetric_sign( key, alg,
- input_data->x, input_data->len,
- signature, signature_size,
- &signature_length ) );
- ASSERT_COMPARE( output_data->x, output_data->len,
- signature, signature_length );
- #endif /* MBEDTLS_TEST_DEPRECATED */
- 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_ASSERT( signature_length <= signature_size );
- #if defined(MBEDTLS_TEST_DEPRECATED)
- signature_length = INVALID_EXPORT_LENGTH;
- TEST_EQUAL( psa_asymmetric_sign( key, alg,
- input_data->x, input_data->len,
- signature, signature_size,
- &signature_length ),
- expected_status );
- TEST_ASSERT( signature_length <= signature_size );
- #endif /* MBEDTLS_TEST_DEPRECATED */
- 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 advertized by the
- * library. */
- signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
- key_bits, alg );
- TEST_ASSERT( signature_size != 0 );
- TEST_ASSERT( 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_ASSERT( 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 */
- 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_ASSERT( 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 ) );
- #if defined(MBEDTLS_TEST_DEPRECATED)
- PSA_ASSERT( psa_asymmetric_verify( key, alg,
- hash_data->x, hash_data->len,
- signature_data->x,
- signature_data->len ) );
- #endif /* MBEDTLS_TEST_DEPRECATED */
- 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 );
- #if defined(MBEDTLS_TEST_DEPRECATED)
- TEST_EQUAL( psa_asymmetric_verify( key, alg,
- hash_data->x, hash_data->len,
- signature_data->x, signature_data->len ),
- expected_status );
- #endif /* MBEDTLS_TEST_DEPRECATED */
- exit:
- psa_reset_key_attributes( &attributes );
- psa_destroy_key( key );
- 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_ASSERT( 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_ASSERT( 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_ASSERT( 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_ASSERT( 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_ASSERT( 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_ASSERT( 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 );
- TEST_EQUAL( output_length, expected_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 );
- 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 );
- TEST_EQUAL( output_length, expected_output_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 );
- 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_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
- ASSERT_ALLOC( output, output_size );
- output2_size = input_data->len;
- TEST_ASSERT( output2_size <=
- PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
- TEST_ASSERT( 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_ASSERT( 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_ASSERT( 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_ASSERT( 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_ASSERT( 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 supress 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 step2_arg, data_t *input2,
- int step3_arg, data_t *input3,
- int requested_capacity_arg,
- data_t *expected_output1,
- data_t *expected_output2 )
- {
- psa_algorithm_t alg = alg_arg;
- psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
- 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 };
- 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 attributes = PSA_KEY_ATTRIBUTES_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( ) );
- 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 );
- /* 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:
- PSA_ASSERT( psa_import_key( &attributes,
- 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], &attributes ) );
- TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
- PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
- }
- PSA_ASSERT( psa_key_derivation_input_key(
- &operation, steps[i], keys[i] ) );
- break;
- default:
- PSA_ASSERT( psa_key_derivation_input_bytes(
- &operation, steps[i],
- inputs[i]->x, inputs[i]->len ) );
- break;
- }
- }
- PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
- ¤t_capacity ) );
- TEST_EQUAL( current_capacity, requested_capacity );
- expected_capacity = requested_capacity;
- /* 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_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 */
- 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( 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;
- ASSERT_ALLOC( output, expected_output->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_get_key_attributes( our_key, &attributes ) );
- key_bits = psa_get_key_bits( &attributes );
- 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 );
- TEST_ASSERT( output_length <=
- PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
- TEST_ASSERT( output_length <=
- PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
- 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 advertized 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 */
|