test_suite_psa_crypto.function 209 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537
  1. /* BEGIN_HEADER */
  2. #include <stdint.h>
  3. #include "mbedtls/asn1.h"
  4. #include "mbedtls/asn1write.h"
  5. #include "mbedtls/oid.h"
  6. /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
  7. * uses mbedtls_ctr_drbg internally. */
  8. #include "mbedtls/ctr_drbg.h"
  9. #include "psa/crypto.h"
  10. #include "psa_crypto_slot_management.h"
  11. #include "test/asn1_helpers.h"
  12. #include "test/psa_crypto_helpers.h"
  13. #include "test/psa_exercise_key.h"
  14. /* If this comes up, it's a bug in the test code or in the test data. */
  15. #define UNUSED 0xdeadbeef
  16. /* Assert that an operation is (not) active.
  17. * This serves as a proxy for checking if the operation is aborted. */
  18. #define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
  19. #define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
  20. /** An invalid export length that will never be set by psa_export_key(). */
  21. static const size_t INVALID_EXPORT_LENGTH = ~0U;
  22. /** Test if a buffer contains a constant byte value.
  23. *
  24. * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
  25. *
  26. * \param buffer Pointer to the beginning of the buffer.
  27. * \param c Expected value of every byte.
  28. * \param size Size of the buffer in bytes.
  29. *
  30. * \return 1 if the buffer is all-bits-zero.
  31. * \return 0 if there is at least one nonzero byte.
  32. */
  33. static int mem_is_char( void *buffer, unsigned char c, size_t size )
  34. {
  35. size_t i;
  36. for( i = 0; i < size; i++ )
  37. {
  38. if( ( (unsigned char *) buffer )[i] != c )
  39. return( 0 );
  40. }
  41. return( 1 );
  42. }
  43. /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
  44. static int asn1_write_10x( unsigned char **p,
  45. unsigned char *start,
  46. size_t bits,
  47. unsigned char x )
  48. {
  49. int ret;
  50. int len = bits / 8 + 1;
  51. if( bits == 0 )
  52. return( MBEDTLS_ERR_ASN1_INVALID_DATA );
  53. if( bits <= 8 && x >= 1 << ( bits - 1 ) )
  54. return( MBEDTLS_ERR_ASN1_INVALID_DATA );
  55. if( *p < start || *p - start < (ptrdiff_t) len )
  56. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  57. *p -= len;
  58. ( *p )[len-1] = x;
  59. if( bits % 8 == 0 )
  60. ( *p )[1] |= 1;
  61. else
  62. ( *p )[0] |= 1 << ( bits % 8 );
  63. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  64. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
  65. MBEDTLS_ASN1_INTEGER ) );
  66. return( len );
  67. }
  68. static int construct_fake_rsa_key( unsigned char *buffer,
  69. size_t buffer_size,
  70. unsigned char **p,
  71. size_t bits,
  72. int keypair )
  73. {
  74. size_t half_bits = ( bits + 1 ) / 2;
  75. int ret;
  76. int len = 0;
  77. /* Construct something that looks like a DER encoding of
  78. * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
  79. * RSAPrivateKey ::= SEQUENCE {
  80. * version Version,
  81. * modulus INTEGER, -- n
  82. * publicExponent INTEGER, -- e
  83. * privateExponent INTEGER, -- d
  84. * prime1 INTEGER, -- p
  85. * prime2 INTEGER, -- q
  86. * exponent1 INTEGER, -- d mod (p-1)
  87. * exponent2 INTEGER, -- d mod (q-1)
  88. * coefficient INTEGER, -- (inverse of q) mod p
  89. * otherPrimeInfos OtherPrimeInfos OPTIONAL
  90. * }
  91. * Or, for a public key, the same structure with only
  92. * version, modulus and publicExponent.
  93. */
  94. *p = buffer + buffer_size;
  95. if( keypair )
  96. {
  97. MBEDTLS_ASN1_CHK_ADD( len, /* pq */
  98. asn1_write_10x( p, buffer, half_bits, 1 ) );
  99. MBEDTLS_ASN1_CHK_ADD( len, /* dq */
  100. asn1_write_10x( p, buffer, half_bits, 1 ) );
  101. MBEDTLS_ASN1_CHK_ADD( len, /* dp */
  102. asn1_write_10x( p, buffer, half_bits, 1 ) );
  103. MBEDTLS_ASN1_CHK_ADD( len, /* q */
  104. asn1_write_10x( p, buffer, half_bits, 1 ) );
  105. MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
  106. asn1_write_10x( p, buffer, half_bits, 3 ) );
  107. MBEDTLS_ASN1_CHK_ADD( len, /* d */
  108. asn1_write_10x( p, buffer, bits, 1 ) );
  109. }
  110. MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
  111. asn1_write_10x( p, buffer, 17, 1 ) );
  112. MBEDTLS_ASN1_CHK_ADD( len, /* n */
  113. asn1_write_10x( p, buffer, bits, 1 ) );
  114. if( keypair )
  115. MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
  116. mbedtls_asn1_write_int( p, buffer, 0 ) );
  117. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
  118. {
  119. const unsigned char tag =
  120. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
  121. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
  122. }
  123. return( len );
  124. }
  125. int exercise_mac_setup( psa_key_type_t key_type,
  126. const unsigned char *key_bytes,
  127. size_t key_length,
  128. psa_algorithm_t alg,
  129. psa_mac_operation_t *operation,
  130. psa_status_t *status )
  131. {
  132. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  133. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  134. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
  135. psa_set_key_algorithm( &attributes, alg );
  136. psa_set_key_type( &attributes, key_type );
  137. PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
  138. *status = psa_mac_sign_setup( operation, key, alg );
  139. /* Whether setup succeeded or failed, abort must succeed. */
  140. PSA_ASSERT( psa_mac_abort( operation ) );
  141. /* If setup failed, reproduce the failure, so that the caller can
  142. * test the resulting state of the operation object. */
  143. if( *status != PSA_SUCCESS )
  144. {
  145. TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
  146. }
  147. psa_destroy_key( key );
  148. return( 1 );
  149. exit:
  150. psa_destroy_key( key );
  151. return( 0 );
  152. }
  153. int exercise_cipher_setup( psa_key_type_t key_type,
  154. const unsigned char *key_bytes,
  155. size_t key_length,
  156. psa_algorithm_t alg,
  157. psa_cipher_operation_t *operation,
  158. psa_status_t *status )
  159. {
  160. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  161. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  162. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
  163. psa_set_key_algorithm( &attributes, alg );
  164. psa_set_key_type( &attributes, key_type );
  165. PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
  166. *status = psa_cipher_encrypt_setup( operation, key, alg );
  167. /* Whether setup succeeded or failed, abort must succeed. */
  168. PSA_ASSERT( psa_cipher_abort( operation ) );
  169. /* If setup failed, reproduce the failure, so that the caller can
  170. * test the resulting state of the operation object. */
  171. if( *status != PSA_SUCCESS )
  172. {
  173. TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
  174. *status );
  175. }
  176. psa_destroy_key( key );
  177. return( 1 );
  178. exit:
  179. psa_destroy_key( key );
  180. return( 0 );
  181. }
  182. static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
  183. {
  184. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  185. mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
  186. uint8_t buffer[1];
  187. size_t length;
  188. int ok = 0;
  189. psa_set_key_id( &attributes, key_id );
  190. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
  191. psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
  192. psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
  193. TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
  194. PSA_ERROR_INVALID_HANDLE );
  195. TEST_EQUAL(
  196. MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
  197. TEST_EQUAL(
  198. MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
  199. TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
  200. TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
  201. TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
  202. TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
  203. TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
  204. TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
  205. PSA_ERROR_INVALID_HANDLE );
  206. TEST_EQUAL( psa_export_public_key( key,
  207. buffer, sizeof( buffer ), &length ),
  208. PSA_ERROR_INVALID_HANDLE );
  209. ok = 1;
  210. exit:
  211. /*
  212. * Key attributes may have been returned by psa_get_key_attributes()
  213. * thus reset them as required.
  214. */
  215. psa_reset_key_attributes( &attributes );
  216. return( ok );
  217. }
  218. /* Assert that a key isn't reported as having a slot number. */
  219. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  220. #define ASSERT_NO_SLOT_NUMBER( attributes ) \
  221. do \
  222. { \
  223. psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
  224. TEST_EQUAL( psa_get_key_slot_number( \
  225. attributes, \
  226. &ASSERT_NO_SLOT_NUMBER_slot_number ), \
  227. PSA_ERROR_INVALID_ARGUMENT ); \
  228. } \
  229. while( 0 )
  230. #else /* MBEDTLS_PSA_CRYPTO_SE_C */
  231. #define ASSERT_NO_SLOT_NUMBER( attributes ) \
  232. ( (void) 0 )
  233. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  234. /* An overapproximation of the amount of storage needed for a key of the
  235. * given type and with the given content. The API doesn't make it easy
  236. * to find a good value for the size. The current implementation doesn't
  237. * care about the value anyway. */
  238. #define KEY_BITS_FROM_DATA( type, data ) \
  239. ( data )->len
  240. typedef enum {
  241. IMPORT_KEY = 0,
  242. GENERATE_KEY = 1,
  243. DERIVE_KEY = 2
  244. } generate_method;
  245. /* END_HEADER */
  246. /* BEGIN_DEPENDENCIES
  247. * depends_on:MBEDTLS_PSA_CRYPTO_C
  248. * END_DEPENDENCIES
  249. */
  250. /* BEGIN_CASE */
  251. void static_checks( )
  252. {
  253. size_t max_truncated_mac_size =
  254. PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
  255. /* Check that the length for a truncated MAC always fits in the algorithm
  256. * encoding. The shifted mask is the maximum truncated value. The
  257. * untruncated algorithm may be one byte larger. */
  258. TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
  259. #if defined(MBEDTLS_TEST_DEPRECATED)
  260. /* Check deprecated constants. */
  261. TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
  262. TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
  263. TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
  264. TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
  265. TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
  266. TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
  267. TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
  268. TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
  269. TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
  270. TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
  271. TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
  272. TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
  273. TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
  274. TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
  275. TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
  276. TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
  277. TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
  278. TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
  279. TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
  280. TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
  281. TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
  282. TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
  283. TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
  284. TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
  285. TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
  286. TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
  287. TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
  288. TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
  289. TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
  290. TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
  291. TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
  292. TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
  293. TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
  294. TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
  295. TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
  296. TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
  297. TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
  298. TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
  299. TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
  300. TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
  301. TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
  302. TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
  303. TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
  304. TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
  305. TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
  306. TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
  307. TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
  308. TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
  309. TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
  310. TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
  311. TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
  312. TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
  313. TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
  314. #endif
  315. }
  316. /* END_CASE */
  317. /* BEGIN_CASE */
  318. void import_with_policy( int type_arg,
  319. int usage_arg, int alg_arg,
  320. int expected_status_arg )
  321. {
  322. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  323. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  324. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  325. psa_key_type_t type = type_arg;
  326. psa_key_usage_t usage = usage_arg;
  327. psa_algorithm_t alg = alg_arg;
  328. psa_status_t expected_status = expected_status_arg;
  329. const uint8_t key_material[16] = {0};
  330. psa_status_t status;
  331. PSA_ASSERT( psa_crypto_init( ) );
  332. psa_set_key_type( &attributes, type );
  333. psa_set_key_usage_flags( &attributes, usage );
  334. psa_set_key_algorithm( &attributes, alg );
  335. status = psa_import_key( &attributes,
  336. key_material, sizeof( key_material ),
  337. &key );
  338. TEST_EQUAL( status, expected_status );
  339. if( status != PSA_SUCCESS )
  340. goto exit;
  341. PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
  342. TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
  343. TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
  344. mbedtls_test_update_key_usage_flags( usage ) );
  345. TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
  346. ASSERT_NO_SLOT_NUMBER( &got_attributes );
  347. PSA_ASSERT( psa_destroy_key( key ) );
  348. test_operations_on_invalid_key( key );
  349. exit:
  350. /*
  351. * Key attributes may have been returned by psa_get_key_attributes()
  352. * thus reset them as required.
  353. */
  354. psa_reset_key_attributes( &got_attributes );
  355. psa_destroy_key( key );
  356. PSA_DONE( );
  357. }
  358. /* END_CASE */
  359. /* BEGIN_CASE */
  360. void import_with_data( data_t *data, int type_arg,
  361. int attr_bits_arg,
  362. int expected_status_arg )
  363. {
  364. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  365. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  366. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  367. psa_key_type_t type = type_arg;
  368. size_t attr_bits = attr_bits_arg;
  369. psa_status_t expected_status = expected_status_arg;
  370. psa_status_t status;
  371. PSA_ASSERT( psa_crypto_init( ) );
  372. psa_set_key_type( &attributes, type );
  373. psa_set_key_bits( &attributes, attr_bits );
  374. status = psa_import_key( &attributes, data->x, data->len, &key );
  375. TEST_EQUAL( status, expected_status );
  376. if( status != PSA_SUCCESS )
  377. goto exit;
  378. PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
  379. TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
  380. if( attr_bits != 0 )
  381. TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
  382. ASSERT_NO_SLOT_NUMBER( &got_attributes );
  383. PSA_ASSERT( psa_destroy_key( key ) );
  384. test_operations_on_invalid_key( key );
  385. exit:
  386. /*
  387. * Key attributes may have been returned by psa_get_key_attributes()
  388. * thus reset them as required.
  389. */
  390. psa_reset_key_attributes( &got_attributes );
  391. psa_destroy_key( key );
  392. PSA_DONE( );
  393. }
  394. /* END_CASE */
  395. /* BEGIN_CASE */
  396. void import_large_key( int type_arg, int byte_size_arg,
  397. int expected_status_arg )
  398. {
  399. psa_key_type_t type = type_arg;
  400. size_t byte_size = byte_size_arg;
  401. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  402. psa_status_t expected_status = expected_status_arg;
  403. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  404. psa_status_t status;
  405. uint8_t *buffer = NULL;
  406. size_t buffer_size = byte_size + 1;
  407. size_t n;
  408. /* Skip the test case if the target running the test cannot
  409. * accomodate large keys due to heap size constraints */
  410. ASSERT_ALLOC_WEAK( buffer, buffer_size );
  411. memset( buffer, 'K', byte_size );
  412. PSA_ASSERT( psa_crypto_init( ) );
  413. /* Try importing the key */
  414. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
  415. psa_set_key_type( &attributes, type );
  416. status = psa_import_key( &attributes, buffer, byte_size, &key );
  417. TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
  418. TEST_EQUAL( status, expected_status );
  419. if( status == PSA_SUCCESS )
  420. {
  421. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  422. TEST_EQUAL( psa_get_key_type( &attributes ), type );
  423. TEST_EQUAL( psa_get_key_bits( &attributes ),
  424. PSA_BYTES_TO_BITS( byte_size ) );
  425. ASSERT_NO_SLOT_NUMBER( &attributes );
  426. memset( buffer, 0, byte_size + 1 );
  427. PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
  428. for( n = 0; n < byte_size; n++ )
  429. TEST_EQUAL( buffer[n], 'K' );
  430. for( n = byte_size; n < buffer_size; n++ )
  431. TEST_EQUAL( buffer[n], 0 );
  432. }
  433. exit:
  434. /*
  435. * Key attributes may have been returned by psa_get_key_attributes()
  436. * thus reset them as required.
  437. */
  438. psa_reset_key_attributes( &attributes );
  439. psa_destroy_key( key );
  440. PSA_DONE( );
  441. mbedtls_free( buffer );
  442. }
  443. /* END_CASE */
  444. /* BEGIN_CASE */
  445. void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
  446. {
  447. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  448. size_t bits = bits_arg;
  449. psa_status_t expected_status = expected_status_arg;
  450. psa_status_t status;
  451. psa_key_type_t type =
  452. keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
  453. size_t buffer_size = /* Slight overapproximations */
  454. keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
  455. unsigned char *buffer = NULL;
  456. unsigned char *p;
  457. int ret;
  458. size_t length;
  459. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  460. PSA_ASSERT( psa_crypto_init( ) );
  461. ASSERT_ALLOC( buffer, buffer_size );
  462. TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
  463. bits, keypair ) ) >= 0 );
  464. length = ret;
  465. /* Try importing the key */
  466. psa_set_key_type( &attributes, type );
  467. status = psa_import_key( &attributes, p, length, &key );
  468. TEST_EQUAL( status, expected_status );
  469. if( status == PSA_SUCCESS )
  470. PSA_ASSERT( psa_destroy_key( key ) );
  471. exit:
  472. mbedtls_free( buffer );
  473. PSA_DONE( );
  474. }
  475. /* END_CASE */
  476. /* BEGIN_CASE */
  477. void import_export( data_t *data,
  478. int type_arg,
  479. int usage_arg, int alg_arg,
  480. int expected_bits,
  481. int export_size_delta,
  482. int expected_export_status_arg,
  483. int canonical_input )
  484. {
  485. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  486. psa_key_type_t type = type_arg;
  487. psa_algorithm_t alg = alg_arg;
  488. psa_status_t expected_export_status = expected_export_status_arg;
  489. psa_status_t status;
  490. unsigned char *exported = NULL;
  491. unsigned char *reexported = NULL;
  492. size_t export_size;
  493. size_t exported_length = INVALID_EXPORT_LENGTH;
  494. size_t reexported_length;
  495. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  496. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  497. export_size = (ptrdiff_t) data->len + export_size_delta;
  498. ASSERT_ALLOC( exported, export_size );
  499. if( ! canonical_input )
  500. ASSERT_ALLOC( reexported, export_size );
  501. PSA_ASSERT( psa_crypto_init( ) );
  502. psa_set_key_usage_flags( &attributes, usage_arg );
  503. psa_set_key_algorithm( &attributes, alg );
  504. psa_set_key_type( &attributes, type );
  505. /* Import the key */
  506. PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
  507. /* Test the key information */
  508. PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
  509. TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
  510. TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
  511. ASSERT_NO_SLOT_NUMBER( &got_attributes );
  512. /* Export the key */
  513. status = psa_export_key( key, exported, export_size, &exported_length );
  514. TEST_EQUAL( status, expected_export_status );
  515. /* The exported length must be set by psa_export_key() to a value between 0
  516. * and export_size. On errors, the exported length must be 0. */
  517. TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
  518. TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
  519. TEST_ASSERT( exported_length <= export_size );
  520. TEST_ASSERT( mem_is_char( exported + exported_length, 0,
  521. export_size - exported_length ) );
  522. if( status != PSA_SUCCESS )
  523. {
  524. TEST_EQUAL( exported_length, 0 );
  525. goto destroy;
  526. }
  527. /* Run sanity checks on the exported key. For non-canonical inputs,
  528. * this validates the canonical representations. For canonical inputs,
  529. * this doesn't directly validate the implementation, but it still helps
  530. * by cross-validating the test data with the sanity check code. */
  531. if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
  532. goto exit;
  533. if( canonical_input )
  534. ASSERT_COMPARE( data->x, data->len, exported, exported_length );
  535. else
  536. {
  537. mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
  538. PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
  539. &key2 ) );
  540. PSA_ASSERT( psa_export_key( key2,
  541. reexported,
  542. export_size,
  543. &reexported_length ) );
  544. ASSERT_COMPARE( exported, exported_length,
  545. reexported, reexported_length );
  546. PSA_ASSERT( psa_destroy_key( key2 ) );
  547. }
  548. TEST_ASSERT( exported_length <=
  549. PSA_EXPORT_KEY_OUTPUT_SIZE( type,
  550. psa_get_key_bits( &got_attributes ) ) );
  551. TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
  552. destroy:
  553. /* Destroy the key */
  554. PSA_ASSERT( psa_destroy_key( key ) );
  555. test_operations_on_invalid_key( key );
  556. exit:
  557. /*
  558. * Key attributes may have been returned by psa_get_key_attributes()
  559. * thus reset them as required.
  560. */
  561. psa_reset_key_attributes( &got_attributes );
  562. mbedtls_free( exported );
  563. mbedtls_free( reexported );
  564. PSA_DONE( );
  565. }
  566. /* END_CASE */
  567. /* BEGIN_CASE */
  568. void import_export_public_key( data_t *data,
  569. int type_arg,
  570. int alg_arg,
  571. int export_size_delta,
  572. int expected_export_status_arg,
  573. data_t *expected_public_key )
  574. {
  575. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  576. psa_key_type_t type = type_arg;
  577. psa_algorithm_t alg = alg_arg;
  578. psa_status_t expected_export_status = expected_export_status_arg;
  579. psa_status_t status;
  580. unsigned char *exported = NULL;
  581. size_t export_size = expected_public_key->len + export_size_delta;
  582. size_t exported_length = INVALID_EXPORT_LENGTH;
  583. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  584. PSA_ASSERT( psa_crypto_init( ) );
  585. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
  586. psa_set_key_algorithm( &attributes, alg );
  587. psa_set_key_type( &attributes, type );
  588. /* Import the key */
  589. PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
  590. /* Export the public key */
  591. ASSERT_ALLOC( exported, export_size );
  592. status = psa_export_public_key( key,
  593. exported, export_size,
  594. &exported_length );
  595. TEST_EQUAL( status, expected_export_status );
  596. if( status == PSA_SUCCESS )
  597. {
  598. psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
  599. size_t bits;
  600. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  601. bits = psa_get_key_bits( &attributes );
  602. TEST_ASSERT( expected_public_key->len <=
  603. PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
  604. TEST_ASSERT( expected_public_key->len <=
  605. PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
  606. TEST_ASSERT( expected_public_key->len <=
  607. PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
  608. ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
  609. exported, exported_length );
  610. }
  611. exit:
  612. /*
  613. * Key attributes may have been returned by psa_get_key_attributes()
  614. * thus reset them as required.
  615. */
  616. psa_reset_key_attributes( &attributes );
  617. mbedtls_free( exported );
  618. psa_destroy_key( key );
  619. PSA_DONE( );
  620. }
  621. /* END_CASE */
  622. /* BEGIN_CASE */
  623. void import_and_exercise_key( data_t *data,
  624. int type_arg,
  625. int bits_arg,
  626. int alg_arg )
  627. {
  628. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  629. psa_key_type_t type = type_arg;
  630. size_t bits = bits_arg;
  631. psa_algorithm_t alg = alg_arg;
  632. psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
  633. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  634. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  635. PSA_ASSERT( psa_crypto_init( ) );
  636. psa_set_key_usage_flags( &attributes, usage );
  637. psa_set_key_algorithm( &attributes, alg );
  638. psa_set_key_type( &attributes, type );
  639. /* Import the key */
  640. PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
  641. /* Test the key information */
  642. PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
  643. TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
  644. TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
  645. /* Do something with the key according to its type and permitted usage. */
  646. if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
  647. goto exit;
  648. PSA_ASSERT( psa_destroy_key( key ) );
  649. test_operations_on_invalid_key( key );
  650. exit:
  651. /*
  652. * Key attributes may have been returned by psa_get_key_attributes()
  653. * thus reset them as required.
  654. */
  655. psa_reset_key_attributes( &got_attributes );
  656. psa_reset_key_attributes( &attributes );
  657. psa_destroy_key( key );
  658. PSA_DONE( );
  659. }
  660. /* END_CASE */
  661. /* BEGIN_CASE */
  662. void effective_key_attributes( int type_arg, int expected_type_arg,
  663. int bits_arg, int expected_bits_arg,
  664. int usage_arg, int expected_usage_arg,
  665. int alg_arg, int expected_alg_arg )
  666. {
  667. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  668. psa_key_type_t key_type = type_arg;
  669. psa_key_type_t expected_key_type = expected_type_arg;
  670. size_t bits = bits_arg;
  671. size_t expected_bits = expected_bits_arg;
  672. psa_algorithm_t alg = alg_arg;
  673. psa_algorithm_t expected_alg = expected_alg_arg;
  674. psa_key_usage_t usage = usage_arg;
  675. psa_key_usage_t expected_usage = expected_usage_arg;
  676. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  677. PSA_ASSERT( psa_crypto_init( ) );
  678. psa_set_key_usage_flags( &attributes, usage );
  679. psa_set_key_algorithm( &attributes, alg );
  680. psa_set_key_type( &attributes, key_type );
  681. psa_set_key_bits( &attributes, bits );
  682. PSA_ASSERT( psa_generate_key( &attributes, &key ) );
  683. psa_reset_key_attributes( &attributes );
  684. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  685. TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
  686. TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
  687. TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
  688. TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
  689. exit:
  690. /*
  691. * Key attributes may have been returned by psa_get_key_attributes()
  692. * thus reset them as required.
  693. */
  694. psa_reset_key_attributes( &attributes );
  695. psa_destroy_key( key );
  696. PSA_DONE( );
  697. }
  698. /* END_CASE */
  699. /* BEGIN_CASE */
  700. void check_key_policy( int type_arg, int bits_arg,
  701. int usage_arg, int alg_arg )
  702. {
  703. test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
  704. usage_arg,
  705. mbedtls_test_update_key_usage_flags( usage_arg ),
  706. alg_arg, alg_arg );
  707. goto exit;
  708. }
  709. /* END_CASE */
  710. /* BEGIN_CASE */
  711. void key_attributes_init( )
  712. {
  713. /* Test each valid way of initializing the object, except for `= {0}`, as
  714. * Clang 5 complains when `-Wmissing-field-initializers` is used, even
  715. * though it's OK by the C standard. We could test for this, but we'd need
  716. * to supress the Clang warning for the test. */
  717. psa_key_attributes_t func = psa_key_attributes_init( );
  718. psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
  719. psa_key_attributes_t zero;
  720. memset( &zero, 0, sizeof( zero ) );
  721. TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
  722. TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
  723. TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
  724. TEST_EQUAL( psa_get_key_type( &func ), 0 );
  725. TEST_EQUAL( psa_get_key_type( &init ), 0 );
  726. TEST_EQUAL( psa_get_key_type( &zero ), 0 );
  727. TEST_EQUAL( psa_get_key_bits( &func ), 0 );
  728. TEST_EQUAL( psa_get_key_bits( &init ), 0 );
  729. TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
  730. TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
  731. TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
  732. TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
  733. TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
  734. TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
  735. TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
  736. }
  737. /* END_CASE */
  738. /* BEGIN_CASE */
  739. void mac_key_policy( int policy_usage_arg,
  740. int policy_alg_arg,
  741. int key_type_arg,
  742. data_t *key_data,
  743. int exercise_alg_arg,
  744. int expected_status_sign_arg,
  745. int expected_status_verify_arg )
  746. {
  747. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  748. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  749. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  750. psa_key_type_t key_type = key_type_arg;
  751. psa_algorithm_t policy_alg = policy_alg_arg;
  752. psa_algorithm_t exercise_alg = exercise_alg_arg;
  753. psa_key_usage_t policy_usage = policy_usage_arg;
  754. psa_status_t status;
  755. psa_status_t expected_status_sign = expected_status_sign_arg;
  756. psa_status_t expected_status_verify = expected_status_verify_arg;
  757. unsigned char mac[PSA_MAC_MAX_SIZE];
  758. PSA_ASSERT( psa_crypto_init( ) );
  759. psa_set_key_usage_flags( &attributes, policy_usage );
  760. psa_set_key_algorithm( &attributes, policy_alg );
  761. psa_set_key_type( &attributes, key_type );
  762. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  763. &key ) );
  764. TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
  765. mbedtls_test_update_key_usage_flags( policy_usage ) );
  766. status = psa_mac_sign_setup( &operation, key, exercise_alg );
  767. TEST_EQUAL( status, expected_status_sign );
  768. /* Calculate the MAC, one-shot case. */
  769. uint8_t input[128] = {0};
  770. size_t mac_len;
  771. TEST_EQUAL( psa_mac_compute( key, exercise_alg,
  772. input, 128,
  773. mac, PSA_MAC_MAX_SIZE, &mac_len ),
  774. expected_status_sign );
  775. /* Verify correct MAC, one-shot case. */
  776. status = psa_mac_verify( key, exercise_alg, input, 128,
  777. mac, mac_len );
  778. if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
  779. TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
  780. else
  781. TEST_EQUAL( status, expected_status_verify );
  782. psa_mac_abort( &operation );
  783. memset( mac, 0, sizeof( mac ) );
  784. status = psa_mac_verify_setup( &operation, key, exercise_alg );
  785. TEST_EQUAL( status, expected_status_verify );
  786. exit:
  787. psa_mac_abort( &operation );
  788. psa_destroy_key( key );
  789. PSA_DONE( );
  790. }
  791. /* END_CASE */
  792. /* BEGIN_CASE */
  793. void cipher_key_policy( int policy_usage_arg,
  794. int policy_alg,
  795. int key_type,
  796. data_t *key_data,
  797. int exercise_alg )
  798. {
  799. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  800. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  801. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  802. psa_key_usage_t policy_usage = policy_usage_arg;
  803. psa_status_t status;
  804. PSA_ASSERT( psa_crypto_init( ) );
  805. psa_set_key_usage_flags( &attributes, policy_usage );
  806. psa_set_key_algorithm( &attributes, policy_alg );
  807. psa_set_key_type( &attributes, key_type );
  808. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  809. &key ) );
  810. /* Check if no key usage flag implication is done */
  811. TEST_EQUAL( policy_usage,
  812. mbedtls_test_update_key_usage_flags( policy_usage ) );
  813. status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
  814. if( policy_alg == exercise_alg &&
  815. ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
  816. PSA_ASSERT( status );
  817. else
  818. TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
  819. psa_cipher_abort( &operation );
  820. status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
  821. if( policy_alg == exercise_alg &&
  822. ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
  823. PSA_ASSERT( status );
  824. else
  825. TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
  826. exit:
  827. psa_cipher_abort( &operation );
  828. psa_destroy_key( key );
  829. PSA_DONE( );
  830. }
  831. /* END_CASE */
  832. /* BEGIN_CASE */
  833. void aead_key_policy( int policy_usage_arg,
  834. int policy_alg,
  835. int key_type,
  836. data_t *key_data,
  837. int nonce_length_arg,
  838. int tag_length_arg,
  839. int exercise_alg,
  840. int expected_status_arg )
  841. {
  842. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  843. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  844. psa_key_usage_t policy_usage = policy_usage_arg;
  845. psa_status_t status;
  846. psa_status_t expected_status = expected_status_arg;
  847. unsigned char nonce[16] = {0};
  848. size_t nonce_length = nonce_length_arg;
  849. unsigned char tag[16];
  850. size_t tag_length = tag_length_arg;
  851. size_t output_length;
  852. TEST_ASSERT( nonce_length <= sizeof( nonce ) );
  853. TEST_ASSERT( tag_length <= sizeof( tag ) );
  854. PSA_ASSERT( psa_crypto_init( ) );
  855. psa_set_key_usage_flags( &attributes, policy_usage );
  856. psa_set_key_algorithm( &attributes, policy_alg );
  857. psa_set_key_type( &attributes, key_type );
  858. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  859. &key ) );
  860. /* Check if no key usage implication is done */
  861. TEST_EQUAL( policy_usage,
  862. mbedtls_test_update_key_usage_flags( policy_usage ) );
  863. status = psa_aead_encrypt( key, exercise_alg,
  864. nonce, nonce_length,
  865. NULL, 0,
  866. NULL, 0,
  867. tag, tag_length,
  868. &output_length );
  869. if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
  870. TEST_EQUAL( status, expected_status );
  871. else
  872. TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
  873. memset( tag, 0, sizeof( tag ) );
  874. status = psa_aead_decrypt( key, exercise_alg,
  875. nonce, nonce_length,
  876. NULL, 0,
  877. tag, tag_length,
  878. NULL, 0,
  879. &output_length );
  880. if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
  881. TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
  882. else if( expected_status == PSA_SUCCESS )
  883. TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
  884. else
  885. TEST_EQUAL( status, expected_status );
  886. exit:
  887. psa_destroy_key( key );
  888. PSA_DONE( );
  889. }
  890. /* END_CASE */
  891. /* BEGIN_CASE */
  892. void asymmetric_encryption_key_policy( int policy_usage_arg,
  893. int policy_alg,
  894. int key_type,
  895. data_t *key_data,
  896. int exercise_alg )
  897. {
  898. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  899. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  900. psa_key_usage_t policy_usage = policy_usage_arg;
  901. psa_status_t status;
  902. size_t key_bits;
  903. size_t buffer_length;
  904. unsigned char *buffer = NULL;
  905. size_t output_length;
  906. PSA_ASSERT( psa_crypto_init( ) );
  907. psa_set_key_usage_flags( &attributes, policy_usage );
  908. psa_set_key_algorithm( &attributes, policy_alg );
  909. psa_set_key_type( &attributes, key_type );
  910. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  911. &key ) );
  912. /* Check if no key usage implication is done */
  913. TEST_EQUAL( policy_usage,
  914. mbedtls_test_update_key_usage_flags( policy_usage ) );
  915. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  916. key_bits = psa_get_key_bits( &attributes );
  917. buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
  918. exercise_alg );
  919. ASSERT_ALLOC( buffer, buffer_length );
  920. status = psa_asymmetric_encrypt( key, exercise_alg,
  921. NULL, 0,
  922. NULL, 0,
  923. buffer, buffer_length,
  924. &output_length );
  925. if( policy_alg == exercise_alg &&
  926. ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
  927. PSA_ASSERT( status );
  928. else
  929. TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
  930. if( buffer_length != 0 )
  931. memset( buffer, 0, buffer_length );
  932. status = psa_asymmetric_decrypt( key, exercise_alg,
  933. buffer, buffer_length,
  934. NULL, 0,
  935. buffer, buffer_length,
  936. &output_length );
  937. if( policy_alg == exercise_alg &&
  938. ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
  939. TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
  940. else
  941. TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
  942. exit:
  943. /*
  944. * Key attributes may have been returned by psa_get_key_attributes()
  945. * thus reset them as required.
  946. */
  947. psa_reset_key_attributes( &attributes );
  948. psa_destroy_key( key );
  949. PSA_DONE( );
  950. mbedtls_free( buffer );
  951. }
  952. /* END_CASE */
  953. /* BEGIN_CASE */
  954. void asymmetric_signature_key_policy( int policy_usage_arg,
  955. int policy_alg,
  956. int key_type,
  957. data_t *key_data,
  958. int exercise_alg,
  959. int payload_length_arg,
  960. int expected_usage_arg )
  961. {
  962. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  963. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  964. psa_key_usage_t policy_usage = policy_usage_arg;
  965. psa_key_usage_t expected_usage = expected_usage_arg;
  966. psa_status_t status;
  967. unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
  968. /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
  969. * compatible with the policy and `payload_length_arg` is supposed to be
  970. * a valid input length to sign. If `payload_length_arg <= 0`,
  971. * `exercise_alg` is supposed to be forbidden by the policy. */
  972. int compatible_alg = payload_length_arg > 0;
  973. size_t payload_length = compatible_alg ? payload_length_arg : 0;
  974. unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
  975. size_t signature_length;
  976. /* Check if all implicit usage flags are deployed
  977. in the expected usage flags. */
  978. TEST_EQUAL( expected_usage,
  979. mbedtls_test_update_key_usage_flags( policy_usage ) );
  980. PSA_ASSERT( psa_crypto_init( ) );
  981. psa_set_key_usage_flags( &attributes, policy_usage );
  982. psa_set_key_algorithm( &attributes, policy_alg );
  983. psa_set_key_type( &attributes, key_type );
  984. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  985. &key ) );
  986. TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
  987. status = psa_sign_hash( key, exercise_alg,
  988. payload, payload_length,
  989. signature, sizeof( signature ),
  990. &signature_length );
  991. if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
  992. PSA_ASSERT( status );
  993. else
  994. TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
  995. memset( signature, 0, sizeof( signature ) );
  996. status = psa_verify_hash( key, exercise_alg,
  997. payload, payload_length,
  998. signature, sizeof( signature ) );
  999. if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
  1000. TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
  1001. else
  1002. TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
  1003. if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
  1004. PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
  1005. {
  1006. status = psa_sign_message( key, exercise_alg,
  1007. payload, payload_length,
  1008. signature, sizeof( signature ),
  1009. &signature_length );
  1010. if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
  1011. PSA_ASSERT( status );
  1012. else
  1013. TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
  1014. memset( signature, 0, sizeof( signature ) );
  1015. status = psa_verify_message( key, exercise_alg,
  1016. payload, payload_length,
  1017. signature, sizeof( signature ) );
  1018. if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
  1019. TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
  1020. else
  1021. TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
  1022. }
  1023. exit:
  1024. psa_destroy_key( key );
  1025. PSA_DONE( );
  1026. }
  1027. /* END_CASE */
  1028. /* BEGIN_CASE */
  1029. void derive_key_policy( int policy_usage,
  1030. int policy_alg,
  1031. int key_type,
  1032. data_t *key_data,
  1033. int exercise_alg )
  1034. {
  1035. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1036. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1037. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  1038. psa_status_t status;
  1039. PSA_ASSERT( psa_crypto_init( ) );
  1040. psa_set_key_usage_flags( &attributes, policy_usage );
  1041. psa_set_key_algorithm( &attributes, policy_alg );
  1042. psa_set_key_type( &attributes, key_type );
  1043. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  1044. &key ) );
  1045. PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
  1046. if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
  1047. PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
  1048. {
  1049. PSA_ASSERT( psa_key_derivation_input_bytes(
  1050. &operation,
  1051. PSA_KEY_DERIVATION_INPUT_SEED,
  1052. (const uint8_t*) "", 0) );
  1053. }
  1054. status = psa_key_derivation_input_key( &operation,
  1055. PSA_KEY_DERIVATION_INPUT_SECRET,
  1056. key );
  1057. if( policy_alg == exercise_alg &&
  1058. ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
  1059. PSA_ASSERT( status );
  1060. else
  1061. TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
  1062. exit:
  1063. psa_key_derivation_abort( &operation );
  1064. psa_destroy_key( key );
  1065. PSA_DONE( );
  1066. }
  1067. /* END_CASE */
  1068. /* BEGIN_CASE */
  1069. void agreement_key_policy( int policy_usage,
  1070. int policy_alg,
  1071. int key_type_arg,
  1072. data_t *key_data,
  1073. int exercise_alg,
  1074. int expected_status_arg )
  1075. {
  1076. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1077. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1078. psa_key_type_t key_type = key_type_arg;
  1079. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  1080. psa_status_t status;
  1081. psa_status_t expected_status = expected_status_arg;
  1082. PSA_ASSERT( psa_crypto_init( ) );
  1083. psa_set_key_usage_flags( &attributes, policy_usage );
  1084. psa_set_key_algorithm( &attributes, policy_alg );
  1085. psa_set_key_type( &attributes, key_type );
  1086. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  1087. &key ) );
  1088. PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
  1089. status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
  1090. TEST_EQUAL( status, expected_status );
  1091. exit:
  1092. psa_key_derivation_abort( &operation );
  1093. psa_destroy_key( key );
  1094. PSA_DONE( );
  1095. }
  1096. /* END_CASE */
  1097. /* BEGIN_CASE */
  1098. void key_policy_alg2( int key_type_arg, data_t *key_data,
  1099. int usage_arg, int alg_arg, int alg2_arg )
  1100. {
  1101. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1102. psa_key_type_t key_type = key_type_arg;
  1103. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1104. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  1105. psa_key_usage_t usage = usage_arg;
  1106. psa_algorithm_t alg = alg_arg;
  1107. psa_algorithm_t alg2 = alg2_arg;
  1108. PSA_ASSERT( psa_crypto_init( ) );
  1109. psa_set_key_usage_flags( &attributes, usage );
  1110. psa_set_key_algorithm( &attributes, alg );
  1111. psa_set_key_enrollment_algorithm( &attributes, alg2 );
  1112. psa_set_key_type( &attributes, key_type );
  1113. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  1114. &key ) );
  1115. /* Update the usage flags to obtain implicit usage flags */
  1116. usage = mbedtls_test_update_key_usage_flags( usage );
  1117. PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
  1118. TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
  1119. TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
  1120. TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
  1121. if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
  1122. goto exit;
  1123. if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
  1124. goto exit;
  1125. exit:
  1126. /*
  1127. * Key attributes may have been returned by psa_get_key_attributes()
  1128. * thus reset them as required.
  1129. */
  1130. psa_reset_key_attributes( &got_attributes );
  1131. psa_destroy_key( key );
  1132. PSA_DONE( );
  1133. }
  1134. /* END_CASE */
  1135. /* BEGIN_CASE */
  1136. void raw_agreement_key_policy( int policy_usage,
  1137. int policy_alg,
  1138. int key_type_arg,
  1139. data_t *key_data,
  1140. int exercise_alg,
  1141. int expected_status_arg )
  1142. {
  1143. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1144. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1145. psa_key_type_t key_type = key_type_arg;
  1146. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  1147. psa_status_t status;
  1148. psa_status_t expected_status = expected_status_arg;
  1149. PSA_ASSERT( psa_crypto_init( ) );
  1150. psa_set_key_usage_flags( &attributes, policy_usage );
  1151. psa_set_key_algorithm( &attributes, policy_alg );
  1152. psa_set_key_type( &attributes, key_type );
  1153. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  1154. &key ) );
  1155. status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
  1156. TEST_EQUAL( status, expected_status );
  1157. exit:
  1158. psa_key_derivation_abort( &operation );
  1159. psa_destroy_key( key );
  1160. PSA_DONE( );
  1161. }
  1162. /* END_CASE */
  1163. /* BEGIN_CASE */
  1164. void copy_success( int source_usage_arg,
  1165. int source_alg_arg, int source_alg2_arg,
  1166. int type_arg, data_t *material,
  1167. int copy_attributes,
  1168. int target_usage_arg,
  1169. int target_alg_arg, int target_alg2_arg,
  1170. int expected_usage_arg,
  1171. int expected_alg_arg, int expected_alg2_arg )
  1172. {
  1173. psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
  1174. psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
  1175. psa_key_usage_t expected_usage = expected_usage_arg;
  1176. psa_algorithm_t expected_alg = expected_alg_arg;
  1177. psa_algorithm_t expected_alg2 = expected_alg2_arg;
  1178. mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
  1179. mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
  1180. uint8_t *export_buffer = NULL;
  1181. PSA_ASSERT( psa_crypto_init( ) );
  1182. /* Prepare the source key. */
  1183. psa_set_key_usage_flags( &source_attributes, source_usage_arg );
  1184. psa_set_key_algorithm( &source_attributes, source_alg_arg );
  1185. psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
  1186. psa_set_key_type( &source_attributes, type_arg );
  1187. PSA_ASSERT( psa_import_key( &source_attributes,
  1188. material->x, material->len,
  1189. &source_key ) );
  1190. PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
  1191. /* Prepare the target attributes. */
  1192. if( copy_attributes )
  1193. {
  1194. target_attributes = source_attributes;
  1195. /* Set volatile lifetime to reset the key identifier to 0. */
  1196. psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
  1197. }
  1198. if( target_usage_arg != -1 )
  1199. psa_set_key_usage_flags( &target_attributes, target_usage_arg );
  1200. if( target_alg_arg != -1 )
  1201. psa_set_key_algorithm( &target_attributes, target_alg_arg );
  1202. if( target_alg2_arg != -1 )
  1203. psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
  1204. /* Copy the key. */
  1205. PSA_ASSERT( psa_copy_key( source_key,
  1206. &target_attributes, &target_key ) );
  1207. /* Destroy the source to ensure that this doesn't affect the target. */
  1208. PSA_ASSERT( psa_destroy_key( source_key ) );
  1209. /* Test that the target slot has the expected content and policy. */
  1210. PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
  1211. TEST_EQUAL( psa_get_key_type( &source_attributes ),
  1212. psa_get_key_type( &target_attributes ) );
  1213. TEST_EQUAL( psa_get_key_bits( &source_attributes ),
  1214. psa_get_key_bits( &target_attributes ) );
  1215. TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
  1216. TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
  1217. TEST_EQUAL( expected_alg2,
  1218. psa_get_key_enrollment_algorithm( &target_attributes ) );
  1219. if( expected_usage & PSA_KEY_USAGE_EXPORT )
  1220. {
  1221. size_t length;
  1222. ASSERT_ALLOC( export_buffer, material->len );
  1223. PSA_ASSERT( psa_export_key( target_key, export_buffer,
  1224. material->len, &length ) );
  1225. ASSERT_COMPARE( material->x, material->len,
  1226. export_buffer, length );
  1227. }
  1228. if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
  1229. goto exit;
  1230. if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
  1231. goto exit;
  1232. PSA_ASSERT( psa_destroy_key( target_key ) );
  1233. exit:
  1234. /*
  1235. * Source and target key attributes may have been returned by
  1236. * psa_get_key_attributes() thus reset them as required.
  1237. */
  1238. psa_reset_key_attributes( &source_attributes );
  1239. psa_reset_key_attributes( &target_attributes );
  1240. PSA_DONE( );
  1241. mbedtls_free( export_buffer );
  1242. }
  1243. /* END_CASE */
  1244. /* BEGIN_CASE */
  1245. void copy_fail( int source_usage_arg,
  1246. int source_alg_arg, int source_alg2_arg,
  1247. int type_arg, data_t *material,
  1248. int target_type_arg, int target_bits_arg,
  1249. int target_usage_arg,
  1250. int target_alg_arg, int target_alg2_arg,
  1251. int target_id_arg, int target_lifetime_arg,
  1252. int expected_status_arg )
  1253. {
  1254. psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
  1255. psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
  1256. mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
  1257. mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
  1258. mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
  1259. PSA_ASSERT( psa_crypto_init( ) );
  1260. /* Prepare the source key. */
  1261. psa_set_key_usage_flags( &source_attributes, source_usage_arg );
  1262. psa_set_key_algorithm( &source_attributes, source_alg_arg );
  1263. psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
  1264. psa_set_key_type( &source_attributes, type_arg );
  1265. PSA_ASSERT( psa_import_key( &source_attributes,
  1266. material->x, material->len,
  1267. &source_key ) );
  1268. /* Prepare the target attributes. */
  1269. psa_set_key_id( &target_attributes, key_id );
  1270. psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
  1271. psa_set_key_type( &target_attributes, target_type_arg );
  1272. psa_set_key_bits( &target_attributes, target_bits_arg );
  1273. psa_set_key_usage_flags( &target_attributes, target_usage_arg );
  1274. psa_set_key_algorithm( &target_attributes, target_alg_arg );
  1275. psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
  1276. /* Try to copy the key. */
  1277. TEST_EQUAL( psa_copy_key( source_key,
  1278. &target_attributes, &target_key ),
  1279. expected_status_arg );
  1280. PSA_ASSERT( psa_destroy_key( source_key ) );
  1281. exit:
  1282. psa_reset_key_attributes( &source_attributes );
  1283. psa_reset_key_attributes( &target_attributes );
  1284. PSA_DONE( );
  1285. }
  1286. /* END_CASE */
  1287. /* BEGIN_CASE */
  1288. void hash_operation_init( )
  1289. {
  1290. const uint8_t input[1] = { 0 };
  1291. /* Test each valid way of initializing the object, except for `= {0}`, as
  1292. * Clang 5 complains when `-Wmissing-field-initializers` is used, even
  1293. * though it's OK by the C standard. We could test for this, but we'd need
  1294. * to supress the Clang warning for the test. */
  1295. psa_hash_operation_t func = psa_hash_operation_init( );
  1296. psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
  1297. psa_hash_operation_t zero;
  1298. memset( &zero, 0, sizeof( zero ) );
  1299. /* A freshly-initialized hash operation should not be usable. */
  1300. TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
  1301. PSA_ERROR_BAD_STATE );
  1302. TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
  1303. PSA_ERROR_BAD_STATE );
  1304. TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
  1305. PSA_ERROR_BAD_STATE );
  1306. /* A default hash operation should be abortable without error. */
  1307. PSA_ASSERT( psa_hash_abort( &func ) );
  1308. PSA_ASSERT( psa_hash_abort( &init ) );
  1309. PSA_ASSERT( psa_hash_abort( &zero ) );
  1310. }
  1311. /* END_CASE */
  1312. /* BEGIN_CASE */
  1313. void hash_setup( int alg_arg,
  1314. int expected_status_arg )
  1315. {
  1316. psa_algorithm_t alg = alg_arg;
  1317. psa_status_t expected_status = expected_status_arg;
  1318. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  1319. psa_status_t status;
  1320. PSA_ASSERT( psa_crypto_init( ) );
  1321. status = psa_hash_setup( &operation, alg );
  1322. TEST_EQUAL( status, expected_status );
  1323. /* Whether setup succeeded or failed, abort must succeed. */
  1324. PSA_ASSERT( psa_hash_abort( &operation ) );
  1325. /* If setup failed, reproduce the failure, so as to
  1326. * test the resulting state of the operation object. */
  1327. if( status != PSA_SUCCESS )
  1328. TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
  1329. /* Now the operation object should be reusable. */
  1330. #if defined(KNOWN_SUPPORTED_HASH_ALG)
  1331. PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
  1332. PSA_ASSERT( psa_hash_abort( &operation ) );
  1333. #endif
  1334. exit:
  1335. PSA_DONE( );
  1336. }
  1337. /* END_CASE */
  1338. /* BEGIN_CASE */
  1339. void hash_compute_fail( int alg_arg, data_t *input,
  1340. int output_size_arg, int expected_status_arg )
  1341. {
  1342. psa_algorithm_t alg = alg_arg;
  1343. uint8_t *output = NULL;
  1344. size_t output_size = output_size_arg;
  1345. size_t output_length = INVALID_EXPORT_LENGTH;
  1346. psa_status_t expected_status = expected_status_arg;
  1347. psa_status_t status;
  1348. ASSERT_ALLOC( output, output_size );
  1349. PSA_ASSERT( psa_crypto_init( ) );
  1350. status = psa_hash_compute( alg, input->x, input->len,
  1351. output, output_size, &output_length );
  1352. TEST_EQUAL( status, expected_status );
  1353. TEST_ASSERT( output_length <= output_size );
  1354. exit:
  1355. mbedtls_free( output );
  1356. PSA_DONE( );
  1357. }
  1358. /* END_CASE */
  1359. /* BEGIN_CASE */
  1360. void hash_compare_fail( int alg_arg, data_t *input,
  1361. data_t *reference_hash,
  1362. int expected_status_arg )
  1363. {
  1364. psa_algorithm_t alg = alg_arg;
  1365. psa_status_t expected_status = expected_status_arg;
  1366. psa_status_t status;
  1367. PSA_ASSERT( psa_crypto_init( ) );
  1368. status = psa_hash_compare( alg, input->x, input->len,
  1369. reference_hash->x, reference_hash->len );
  1370. TEST_EQUAL( status, expected_status );
  1371. exit:
  1372. PSA_DONE( );
  1373. }
  1374. /* END_CASE */
  1375. /* BEGIN_CASE */
  1376. void hash_compute_compare( int alg_arg, data_t *input,
  1377. data_t *expected_output )
  1378. {
  1379. psa_algorithm_t alg = alg_arg;
  1380. uint8_t output[PSA_HASH_MAX_SIZE + 1];
  1381. size_t output_length = INVALID_EXPORT_LENGTH;
  1382. size_t i;
  1383. PSA_ASSERT( psa_crypto_init( ) );
  1384. /* Compute with tight buffer */
  1385. PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
  1386. output, PSA_HASH_LENGTH( alg ),
  1387. &output_length ) );
  1388. TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
  1389. ASSERT_COMPARE( output, output_length,
  1390. expected_output->x, expected_output->len );
  1391. /* Compute with larger buffer */
  1392. PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
  1393. output, sizeof( output ),
  1394. &output_length ) );
  1395. TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
  1396. ASSERT_COMPARE( output, output_length,
  1397. expected_output->x, expected_output->len );
  1398. /* Compare with correct hash */
  1399. PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
  1400. output, output_length ) );
  1401. /* Compare with trailing garbage */
  1402. TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
  1403. output, output_length + 1 ),
  1404. PSA_ERROR_INVALID_SIGNATURE );
  1405. /* Compare with truncated hash */
  1406. TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
  1407. output, output_length - 1 ),
  1408. PSA_ERROR_INVALID_SIGNATURE );
  1409. /* Compare with corrupted value */
  1410. for( i = 0; i < output_length; i++ )
  1411. {
  1412. mbedtls_test_set_step( i );
  1413. output[i] ^= 1;
  1414. TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
  1415. output, output_length ),
  1416. PSA_ERROR_INVALID_SIGNATURE );
  1417. output[i] ^= 1;
  1418. }
  1419. exit:
  1420. PSA_DONE( );
  1421. }
  1422. /* END_CASE */
  1423. /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
  1424. void hash_bad_order( )
  1425. {
  1426. psa_algorithm_t alg = PSA_ALG_SHA_256;
  1427. unsigned char input[] = "";
  1428. /* SHA-256 hash of an empty string */
  1429. const unsigned char valid_hash[] = {
  1430. 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
  1431. 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
  1432. 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
  1433. unsigned char hash[sizeof(valid_hash)] = { 0 };
  1434. size_t hash_len;
  1435. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  1436. PSA_ASSERT( psa_crypto_init( ) );
  1437. /* Call setup twice in a row. */
  1438. PSA_ASSERT( psa_hash_setup( &operation, alg ) );
  1439. ASSERT_OPERATION_IS_ACTIVE( operation );
  1440. TEST_EQUAL( psa_hash_setup( &operation, alg ),
  1441. PSA_ERROR_BAD_STATE );
  1442. ASSERT_OPERATION_IS_INACTIVE( operation );
  1443. PSA_ASSERT( psa_hash_abort( &operation ) );
  1444. ASSERT_OPERATION_IS_INACTIVE( operation );
  1445. /* Call update without calling setup beforehand. */
  1446. TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
  1447. PSA_ERROR_BAD_STATE );
  1448. PSA_ASSERT( psa_hash_abort( &operation ) );
  1449. /* Check that update calls abort on error. */
  1450. PSA_ASSERT( psa_hash_setup( &operation, alg ) );
  1451. operation.id = UINT_MAX;
  1452. ASSERT_OPERATION_IS_ACTIVE( operation );
  1453. TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
  1454. PSA_ERROR_BAD_STATE );
  1455. ASSERT_OPERATION_IS_INACTIVE( operation );
  1456. PSA_ASSERT( psa_hash_abort( &operation ) );
  1457. ASSERT_OPERATION_IS_INACTIVE( operation );
  1458. /* Call update after finish. */
  1459. PSA_ASSERT( psa_hash_setup( &operation, alg ) );
  1460. PSA_ASSERT( psa_hash_finish( &operation,
  1461. hash, sizeof( hash ), &hash_len ) );
  1462. TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
  1463. PSA_ERROR_BAD_STATE );
  1464. PSA_ASSERT( psa_hash_abort( &operation ) );
  1465. /* Call verify without calling setup beforehand. */
  1466. TEST_EQUAL( psa_hash_verify( &operation,
  1467. valid_hash, sizeof( valid_hash ) ),
  1468. PSA_ERROR_BAD_STATE );
  1469. PSA_ASSERT( psa_hash_abort( &operation ) );
  1470. /* Call verify after finish. */
  1471. PSA_ASSERT( psa_hash_setup( &operation, alg ) );
  1472. PSA_ASSERT( psa_hash_finish( &operation,
  1473. hash, sizeof( hash ), &hash_len ) );
  1474. TEST_EQUAL( psa_hash_verify( &operation,
  1475. valid_hash, sizeof( valid_hash ) ),
  1476. PSA_ERROR_BAD_STATE );
  1477. PSA_ASSERT( psa_hash_abort( &operation ) );
  1478. /* Call verify twice in a row. */
  1479. PSA_ASSERT( psa_hash_setup( &operation, alg ) );
  1480. ASSERT_OPERATION_IS_ACTIVE( operation );
  1481. PSA_ASSERT( psa_hash_verify( &operation,
  1482. valid_hash, sizeof( valid_hash ) ) );
  1483. ASSERT_OPERATION_IS_INACTIVE( operation );
  1484. TEST_EQUAL( psa_hash_verify( &operation,
  1485. valid_hash, sizeof( valid_hash ) ),
  1486. PSA_ERROR_BAD_STATE );
  1487. ASSERT_OPERATION_IS_INACTIVE( operation );
  1488. PSA_ASSERT( psa_hash_abort( &operation ) );
  1489. /* Call finish without calling setup beforehand. */
  1490. TEST_EQUAL( psa_hash_finish( &operation,
  1491. hash, sizeof( hash ), &hash_len ),
  1492. PSA_ERROR_BAD_STATE );
  1493. PSA_ASSERT( psa_hash_abort( &operation ) );
  1494. /* Call finish twice in a row. */
  1495. PSA_ASSERT( psa_hash_setup( &operation, alg ) );
  1496. PSA_ASSERT( psa_hash_finish( &operation,
  1497. hash, sizeof( hash ), &hash_len ) );
  1498. TEST_EQUAL( psa_hash_finish( &operation,
  1499. hash, sizeof( hash ), &hash_len ),
  1500. PSA_ERROR_BAD_STATE );
  1501. PSA_ASSERT( psa_hash_abort( &operation ) );
  1502. /* Call finish after calling verify. */
  1503. PSA_ASSERT( psa_hash_setup( &operation, alg ) );
  1504. PSA_ASSERT( psa_hash_verify( &operation,
  1505. valid_hash, sizeof( valid_hash ) ) );
  1506. TEST_EQUAL( psa_hash_finish( &operation,
  1507. hash, sizeof( hash ), &hash_len ),
  1508. PSA_ERROR_BAD_STATE );
  1509. PSA_ASSERT( psa_hash_abort( &operation ) );
  1510. exit:
  1511. PSA_DONE( );
  1512. }
  1513. /* END_CASE */
  1514. /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
  1515. void hash_verify_bad_args( )
  1516. {
  1517. psa_algorithm_t alg = PSA_ALG_SHA_256;
  1518. /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
  1519. * appended to it */
  1520. unsigned char hash[] = {
  1521. 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
  1522. 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
  1523. 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
  1524. size_t expected_size = PSA_HASH_LENGTH( alg );
  1525. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  1526. PSA_ASSERT( psa_crypto_init( ) );
  1527. /* psa_hash_verify with a smaller hash than expected */
  1528. PSA_ASSERT( psa_hash_setup( &operation, alg ) );
  1529. ASSERT_OPERATION_IS_ACTIVE( operation );
  1530. TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
  1531. PSA_ERROR_INVALID_SIGNATURE );
  1532. ASSERT_OPERATION_IS_INACTIVE( operation );
  1533. PSA_ASSERT( psa_hash_abort( &operation ) );
  1534. ASSERT_OPERATION_IS_INACTIVE( operation );
  1535. /* psa_hash_verify with a non-matching hash */
  1536. PSA_ASSERT( psa_hash_setup( &operation, alg ) );
  1537. TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
  1538. PSA_ERROR_INVALID_SIGNATURE );
  1539. /* psa_hash_verify with a hash longer than expected */
  1540. PSA_ASSERT( psa_hash_setup( &operation, alg ) );
  1541. TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
  1542. PSA_ERROR_INVALID_SIGNATURE );
  1543. exit:
  1544. PSA_DONE( );
  1545. }
  1546. /* END_CASE */
  1547. /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
  1548. void hash_finish_bad_args( )
  1549. {
  1550. psa_algorithm_t alg = PSA_ALG_SHA_256;
  1551. unsigned char hash[PSA_HASH_MAX_SIZE];
  1552. size_t expected_size = PSA_HASH_LENGTH( alg );
  1553. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  1554. size_t hash_len;
  1555. PSA_ASSERT( psa_crypto_init( ) );
  1556. /* psa_hash_finish with a smaller hash buffer than expected */
  1557. PSA_ASSERT( psa_hash_setup( &operation, alg ) );
  1558. TEST_EQUAL( psa_hash_finish( &operation,
  1559. hash, expected_size - 1, &hash_len ),
  1560. PSA_ERROR_BUFFER_TOO_SMALL );
  1561. exit:
  1562. PSA_DONE( );
  1563. }
  1564. /* END_CASE */
  1565. /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
  1566. void hash_clone_source_state( )
  1567. {
  1568. psa_algorithm_t alg = PSA_ALG_SHA_256;
  1569. unsigned char hash[PSA_HASH_MAX_SIZE];
  1570. psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
  1571. psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
  1572. psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
  1573. psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
  1574. psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
  1575. size_t hash_len;
  1576. PSA_ASSERT( psa_crypto_init( ) );
  1577. PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
  1578. PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
  1579. PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
  1580. PSA_ASSERT( psa_hash_finish( &op_finished,
  1581. hash, sizeof( hash ), &hash_len ) );
  1582. PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
  1583. PSA_ASSERT( psa_hash_abort( &op_aborted ) );
  1584. TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
  1585. PSA_ERROR_BAD_STATE );
  1586. PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
  1587. PSA_ASSERT( psa_hash_finish( &op_init,
  1588. hash, sizeof( hash ), &hash_len ) );
  1589. PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
  1590. PSA_ASSERT( psa_hash_finish( &op_finished,
  1591. hash, sizeof( hash ), &hash_len ) );
  1592. PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
  1593. PSA_ASSERT( psa_hash_finish( &op_aborted,
  1594. hash, sizeof( hash ), &hash_len ) );
  1595. exit:
  1596. psa_hash_abort( &op_source );
  1597. psa_hash_abort( &op_init );
  1598. psa_hash_abort( &op_setup );
  1599. psa_hash_abort( &op_finished );
  1600. psa_hash_abort( &op_aborted );
  1601. PSA_DONE( );
  1602. }
  1603. /* END_CASE */
  1604. /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
  1605. void hash_clone_target_state( )
  1606. {
  1607. psa_algorithm_t alg = PSA_ALG_SHA_256;
  1608. unsigned char hash[PSA_HASH_MAX_SIZE];
  1609. psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
  1610. psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
  1611. psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
  1612. psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
  1613. psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
  1614. size_t hash_len;
  1615. PSA_ASSERT( psa_crypto_init( ) );
  1616. PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
  1617. PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
  1618. PSA_ASSERT( psa_hash_finish( &op_finished,
  1619. hash, sizeof( hash ), &hash_len ) );
  1620. PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
  1621. PSA_ASSERT( psa_hash_abort( &op_aborted ) );
  1622. PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
  1623. PSA_ASSERT( psa_hash_finish( &op_target,
  1624. hash, sizeof( hash ), &hash_len ) );
  1625. TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
  1626. TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
  1627. PSA_ERROR_BAD_STATE );
  1628. TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
  1629. PSA_ERROR_BAD_STATE );
  1630. exit:
  1631. psa_hash_abort( &op_target );
  1632. psa_hash_abort( &op_init );
  1633. psa_hash_abort( &op_setup );
  1634. psa_hash_abort( &op_finished );
  1635. psa_hash_abort( &op_aborted );
  1636. PSA_DONE( );
  1637. }
  1638. /* END_CASE */
  1639. /* BEGIN_CASE */
  1640. void mac_operation_init( )
  1641. {
  1642. const uint8_t input[1] = { 0 };
  1643. /* Test each valid way of initializing the object, except for `= {0}`, as
  1644. * Clang 5 complains when `-Wmissing-field-initializers` is used, even
  1645. * though it's OK by the C standard. We could test for this, but we'd need
  1646. * to supress the Clang warning for the test. */
  1647. psa_mac_operation_t func = psa_mac_operation_init( );
  1648. psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
  1649. psa_mac_operation_t zero;
  1650. memset( &zero, 0, sizeof( zero ) );
  1651. /* A freshly-initialized MAC operation should not be usable. */
  1652. TEST_EQUAL( psa_mac_update( &func,
  1653. input, sizeof( input ) ),
  1654. PSA_ERROR_BAD_STATE );
  1655. TEST_EQUAL( psa_mac_update( &init,
  1656. input, sizeof( input ) ),
  1657. PSA_ERROR_BAD_STATE );
  1658. TEST_EQUAL( psa_mac_update( &zero,
  1659. input, sizeof( input ) ),
  1660. PSA_ERROR_BAD_STATE );
  1661. /* A default MAC operation should be abortable without error. */
  1662. PSA_ASSERT( psa_mac_abort( &func ) );
  1663. PSA_ASSERT( psa_mac_abort( &init ) );
  1664. PSA_ASSERT( psa_mac_abort( &zero ) );
  1665. }
  1666. /* END_CASE */
  1667. /* BEGIN_CASE */
  1668. void mac_setup( int key_type_arg,
  1669. data_t *key,
  1670. int alg_arg,
  1671. int expected_status_arg )
  1672. {
  1673. psa_key_type_t key_type = key_type_arg;
  1674. psa_algorithm_t alg = alg_arg;
  1675. psa_status_t expected_status = expected_status_arg;
  1676. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  1677. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  1678. #if defined(KNOWN_SUPPORTED_MAC_ALG)
  1679. const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
  1680. #endif
  1681. PSA_ASSERT( psa_crypto_init( ) );
  1682. if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
  1683. &operation, &status ) )
  1684. goto exit;
  1685. TEST_EQUAL( status, expected_status );
  1686. /* The operation object should be reusable. */
  1687. #if defined(KNOWN_SUPPORTED_MAC_ALG)
  1688. if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
  1689. smoke_test_key_data,
  1690. sizeof( smoke_test_key_data ),
  1691. KNOWN_SUPPORTED_MAC_ALG,
  1692. &operation, &status ) )
  1693. goto exit;
  1694. TEST_EQUAL( status, PSA_SUCCESS );
  1695. #endif
  1696. exit:
  1697. PSA_DONE( );
  1698. }
  1699. /* END_CASE */
  1700. /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
  1701. void mac_bad_order( )
  1702. {
  1703. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1704. psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
  1705. psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
  1706. const uint8_t key_data[] = {
  1707. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  1708. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  1709. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
  1710. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1711. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  1712. uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
  1713. size_t sign_mac_length = 0;
  1714. const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
  1715. const uint8_t verify_mac[] = {
  1716. 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
  1717. 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
  1718. 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
  1719. PSA_ASSERT( psa_crypto_init( ) );
  1720. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
  1721. psa_set_key_algorithm( &attributes, alg );
  1722. psa_set_key_type( &attributes, key_type );
  1723. PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
  1724. &key ) );
  1725. /* Call update without calling setup beforehand. */
  1726. TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
  1727. PSA_ERROR_BAD_STATE );
  1728. PSA_ASSERT( psa_mac_abort( &operation ) );
  1729. /* Call sign finish without calling setup beforehand. */
  1730. TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
  1731. &sign_mac_length),
  1732. PSA_ERROR_BAD_STATE );
  1733. PSA_ASSERT( psa_mac_abort( &operation ) );
  1734. /* Call verify finish without calling setup beforehand. */
  1735. TEST_EQUAL( psa_mac_verify_finish( &operation,
  1736. verify_mac, sizeof( verify_mac ) ),
  1737. PSA_ERROR_BAD_STATE );
  1738. PSA_ASSERT( psa_mac_abort( &operation ) );
  1739. /* Call setup twice in a row. */
  1740. PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
  1741. ASSERT_OPERATION_IS_ACTIVE( operation );
  1742. TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
  1743. PSA_ERROR_BAD_STATE );
  1744. ASSERT_OPERATION_IS_INACTIVE( operation );
  1745. PSA_ASSERT( psa_mac_abort( &operation ) );
  1746. ASSERT_OPERATION_IS_INACTIVE( operation );
  1747. /* Call update after sign finish. */
  1748. PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
  1749. PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
  1750. PSA_ASSERT( psa_mac_sign_finish( &operation,
  1751. sign_mac, sizeof( sign_mac ),
  1752. &sign_mac_length ) );
  1753. TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
  1754. PSA_ERROR_BAD_STATE );
  1755. PSA_ASSERT( psa_mac_abort( &operation ) );
  1756. /* Call update after verify finish. */
  1757. PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
  1758. PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
  1759. PSA_ASSERT( psa_mac_verify_finish( &operation,
  1760. verify_mac, sizeof( verify_mac ) ) );
  1761. TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
  1762. PSA_ERROR_BAD_STATE );
  1763. PSA_ASSERT( psa_mac_abort( &operation ) );
  1764. /* Call sign finish twice in a row. */
  1765. PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
  1766. PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
  1767. PSA_ASSERT( psa_mac_sign_finish( &operation,
  1768. sign_mac, sizeof( sign_mac ),
  1769. &sign_mac_length ) );
  1770. TEST_EQUAL( psa_mac_sign_finish( &operation,
  1771. sign_mac, sizeof( sign_mac ),
  1772. &sign_mac_length ),
  1773. PSA_ERROR_BAD_STATE );
  1774. PSA_ASSERT( psa_mac_abort( &operation ) );
  1775. /* Call verify finish twice in a row. */
  1776. PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
  1777. PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
  1778. PSA_ASSERT( psa_mac_verify_finish( &operation,
  1779. verify_mac, sizeof( verify_mac ) ) );
  1780. TEST_EQUAL( psa_mac_verify_finish( &operation,
  1781. verify_mac, sizeof( verify_mac ) ),
  1782. PSA_ERROR_BAD_STATE );
  1783. PSA_ASSERT( psa_mac_abort( &operation ) );
  1784. /* Setup sign but try verify. */
  1785. PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
  1786. PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
  1787. ASSERT_OPERATION_IS_ACTIVE( operation );
  1788. TEST_EQUAL( psa_mac_verify_finish( &operation,
  1789. verify_mac, sizeof( verify_mac ) ),
  1790. PSA_ERROR_BAD_STATE );
  1791. ASSERT_OPERATION_IS_INACTIVE( operation );
  1792. PSA_ASSERT( psa_mac_abort( &operation ) );
  1793. ASSERT_OPERATION_IS_INACTIVE( operation );
  1794. /* Setup verify but try sign. */
  1795. PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
  1796. PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
  1797. ASSERT_OPERATION_IS_ACTIVE( operation );
  1798. TEST_EQUAL( psa_mac_sign_finish( &operation,
  1799. sign_mac, sizeof( sign_mac ),
  1800. &sign_mac_length ),
  1801. PSA_ERROR_BAD_STATE );
  1802. ASSERT_OPERATION_IS_INACTIVE( operation );
  1803. PSA_ASSERT( psa_mac_abort( &operation ) );
  1804. ASSERT_OPERATION_IS_INACTIVE( operation );
  1805. PSA_ASSERT( psa_destroy_key( key ) );
  1806. exit:
  1807. PSA_DONE( );
  1808. }
  1809. /* END_CASE */
  1810. /* BEGIN_CASE */
  1811. void mac_sign( int key_type_arg,
  1812. data_t *key_data,
  1813. int alg_arg,
  1814. data_t *input,
  1815. data_t *expected_mac )
  1816. {
  1817. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1818. psa_key_type_t key_type = key_type_arg;
  1819. psa_algorithm_t alg = alg_arg;
  1820. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  1821. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1822. uint8_t *actual_mac = NULL;
  1823. size_t mac_buffer_size =
  1824. PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
  1825. size_t mac_length = 0;
  1826. const size_t output_sizes_to_test[] = {
  1827. 0,
  1828. 1,
  1829. expected_mac->len - 1,
  1830. expected_mac->len,
  1831. expected_mac->len + 1,
  1832. };
  1833. TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
  1834. /* We expect PSA_MAC_LENGTH to be exact. */
  1835. TEST_ASSERT( expected_mac->len == mac_buffer_size );
  1836. PSA_ASSERT( psa_crypto_init( ) );
  1837. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
  1838. psa_set_key_algorithm( &attributes, alg );
  1839. psa_set_key_type( &attributes, key_type );
  1840. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  1841. &key ) );
  1842. for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
  1843. {
  1844. const size_t output_size = output_sizes_to_test[i];
  1845. psa_status_t expected_status =
  1846. ( output_size >= expected_mac->len ? PSA_SUCCESS :
  1847. PSA_ERROR_BUFFER_TOO_SMALL );
  1848. mbedtls_test_set_step( output_size );
  1849. ASSERT_ALLOC( actual_mac, output_size );
  1850. /* Calculate the MAC, one-shot case. */
  1851. TEST_EQUAL( psa_mac_compute( key, alg,
  1852. input->x, input->len,
  1853. actual_mac, output_size, &mac_length ),
  1854. expected_status );
  1855. if( expected_status == PSA_SUCCESS )
  1856. {
  1857. ASSERT_COMPARE( expected_mac->x, expected_mac->len,
  1858. actual_mac, mac_length );
  1859. }
  1860. if( output_size > 0 )
  1861. memset( actual_mac, 0, output_size );
  1862. /* Calculate the MAC, multi-part case. */
  1863. PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
  1864. PSA_ASSERT( psa_mac_update( &operation,
  1865. input->x, input->len ) );
  1866. TEST_EQUAL( psa_mac_sign_finish( &operation,
  1867. actual_mac, output_size,
  1868. &mac_length ),
  1869. expected_status );
  1870. PSA_ASSERT( psa_mac_abort( &operation ) );
  1871. if( expected_status == PSA_SUCCESS )
  1872. {
  1873. ASSERT_COMPARE( expected_mac->x, expected_mac->len,
  1874. actual_mac, mac_length );
  1875. }
  1876. mbedtls_free( actual_mac );
  1877. actual_mac = NULL;
  1878. }
  1879. exit:
  1880. psa_mac_abort( &operation );
  1881. psa_destroy_key( key );
  1882. PSA_DONE( );
  1883. mbedtls_free( actual_mac );
  1884. }
  1885. /* END_CASE */
  1886. /* BEGIN_CASE */
  1887. void mac_verify( int key_type_arg,
  1888. data_t *key_data,
  1889. int alg_arg,
  1890. data_t *input,
  1891. data_t *expected_mac )
  1892. {
  1893. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1894. psa_key_type_t key_type = key_type_arg;
  1895. psa_algorithm_t alg = alg_arg;
  1896. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  1897. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1898. uint8_t *perturbed_mac = NULL;
  1899. TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
  1900. PSA_ASSERT( psa_crypto_init( ) );
  1901. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
  1902. psa_set_key_algorithm( &attributes, alg );
  1903. psa_set_key_type( &attributes, key_type );
  1904. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  1905. &key ) );
  1906. /* Verify correct MAC, one-shot case. */
  1907. PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
  1908. expected_mac->x, expected_mac->len ) );
  1909. /* Verify correct MAC, multi-part case. */
  1910. PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
  1911. PSA_ASSERT( psa_mac_update( &operation,
  1912. input->x, input->len ) );
  1913. PSA_ASSERT( psa_mac_verify_finish( &operation,
  1914. expected_mac->x,
  1915. expected_mac->len ) );
  1916. /* Test a MAC that's too short, one-shot case. */
  1917. TEST_EQUAL( psa_mac_verify( key, alg,
  1918. input->x, input->len,
  1919. expected_mac->x,
  1920. expected_mac->len - 1 ),
  1921. PSA_ERROR_INVALID_SIGNATURE );
  1922. /* Test a MAC that's too short, multi-part case. */
  1923. PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
  1924. PSA_ASSERT( psa_mac_update( &operation,
  1925. input->x, input->len ) );
  1926. TEST_EQUAL( psa_mac_verify_finish( &operation,
  1927. expected_mac->x,
  1928. expected_mac->len - 1 ),
  1929. PSA_ERROR_INVALID_SIGNATURE );
  1930. /* Test a MAC that's too long, one-shot case. */
  1931. ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
  1932. memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
  1933. TEST_EQUAL( psa_mac_verify( key, alg,
  1934. input->x, input->len,
  1935. perturbed_mac, expected_mac->len + 1 ),
  1936. PSA_ERROR_INVALID_SIGNATURE );
  1937. /* Test a MAC that's too long, multi-part case. */
  1938. PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
  1939. PSA_ASSERT( psa_mac_update( &operation,
  1940. input->x, input->len ) );
  1941. TEST_EQUAL( psa_mac_verify_finish( &operation,
  1942. perturbed_mac,
  1943. expected_mac->len + 1 ),
  1944. PSA_ERROR_INVALID_SIGNATURE );
  1945. /* Test changing one byte. */
  1946. for( size_t i = 0; i < expected_mac->len; i++ )
  1947. {
  1948. mbedtls_test_set_step( i );
  1949. perturbed_mac[i] ^= 1;
  1950. TEST_EQUAL( psa_mac_verify( key, alg,
  1951. input->x, input->len,
  1952. perturbed_mac, expected_mac->len ),
  1953. PSA_ERROR_INVALID_SIGNATURE );
  1954. PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
  1955. PSA_ASSERT( psa_mac_update( &operation,
  1956. input->x, input->len ) );
  1957. TEST_EQUAL( psa_mac_verify_finish( &operation,
  1958. perturbed_mac,
  1959. expected_mac->len ),
  1960. PSA_ERROR_INVALID_SIGNATURE );
  1961. perturbed_mac[i] ^= 1;
  1962. }
  1963. exit:
  1964. psa_mac_abort( &operation );
  1965. psa_destroy_key( key );
  1966. PSA_DONE( );
  1967. mbedtls_free( perturbed_mac );
  1968. }
  1969. /* END_CASE */
  1970. /* BEGIN_CASE */
  1971. void cipher_operation_init( )
  1972. {
  1973. const uint8_t input[1] = { 0 };
  1974. unsigned char output[1] = { 0 };
  1975. size_t output_length;
  1976. /* Test each valid way of initializing the object, except for `= {0}`, as
  1977. * Clang 5 complains when `-Wmissing-field-initializers` is used, even
  1978. * though it's OK by the C standard. We could test for this, but we'd need
  1979. * to supress the Clang warning for the test. */
  1980. psa_cipher_operation_t func = psa_cipher_operation_init( );
  1981. psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
  1982. psa_cipher_operation_t zero;
  1983. memset( &zero, 0, sizeof( zero ) );
  1984. /* A freshly-initialized cipher operation should not be usable. */
  1985. TEST_EQUAL( psa_cipher_update( &func,
  1986. input, sizeof( input ),
  1987. output, sizeof( output ),
  1988. &output_length ),
  1989. PSA_ERROR_BAD_STATE );
  1990. TEST_EQUAL( psa_cipher_update( &init,
  1991. input, sizeof( input ),
  1992. output, sizeof( output ),
  1993. &output_length ),
  1994. PSA_ERROR_BAD_STATE );
  1995. TEST_EQUAL( psa_cipher_update( &zero,
  1996. input, sizeof( input ),
  1997. output, sizeof( output ),
  1998. &output_length ),
  1999. PSA_ERROR_BAD_STATE );
  2000. /* A default cipher operation should be abortable without error. */
  2001. PSA_ASSERT( psa_cipher_abort( &func ) );
  2002. PSA_ASSERT( psa_cipher_abort( &init ) );
  2003. PSA_ASSERT( psa_cipher_abort( &zero ) );
  2004. }
  2005. /* END_CASE */
  2006. /* BEGIN_CASE */
  2007. void cipher_setup( int key_type_arg,
  2008. data_t *key,
  2009. int alg_arg,
  2010. int expected_status_arg )
  2011. {
  2012. psa_key_type_t key_type = key_type_arg;
  2013. psa_algorithm_t alg = alg_arg;
  2014. psa_status_t expected_status = expected_status_arg;
  2015. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  2016. psa_status_t status;
  2017. #if defined(KNOWN_SUPPORTED_CIPHER_ALG)
  2018. const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
  2019. #endif
  2020. PSA_ASSERT( psa_crypto_init( ) );
  2021. if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
  2022. &operation, &status ) )
  2023. goto exit;
  2024. TEST_EQUAL( status, expected_status );
  2025. /* The operation object should be reusable. */
  2026. #if defined(KNOWN_SUPPORTED_CIPHER_ALG)
  2027. if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
  2028. smoke_test_key_data,
  2029. sizeof( smoke_test_key_data ),
  2030. KNOWN_SUPPORTED_CIPHER_ALG,
  2031. &operation, &status ) )
  2032. goto exit;
  2033. TEST_EQUAL( status, PSA_SUCCESS );
  2034. #endif
  2035. exit:
  2036. psa_cipher_abort( &operation );
  2037. PSA_DONE( );
  2038. }
  2039. /* END_CASE */
  2040. /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
  2041. void cipher_bad_order( )
  2042. {
  2043. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2044. psa_key_type_t key_type = PSA_KEY_TYPE_AES;
  2045. psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
  2046. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2047. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  2048. unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
  2049. const uint8_t key_data[] = {
  2050. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  2051. 0xaa, 0xaa, 0xaa, 0xaa };
  2052. const uint8_t text[] = {
  2053. 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
  2054. 0xbb, 0xbb, 0xbb, 0xbb };
  2055. uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
  2056. size_t length = 0;
  2057. PSA_ASSERT( psa_crypto_init( ) );
  2058. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
  2059. psa_set_key_algorithm( &attributes, alg );
  2060. psa_set_key_type( &attributes, key_type );
  2061. PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
  2062. &key ) );
  2063. /* Call encrypt setup twice in a row. */
  2064. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2065. ASSERT_OPERATION_IS_ACTIVE( operation );
  2066. TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
  2067. PSA_ERROR_BAD_STATE );
  2068. ASSERT_OPERATION_IS_INACTIVE( operation );
  2069. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2070. ASSERT_OPERATION_IS_INACTIVE( operation );
  2071. /* Call decrypt setup twice in a row. */
  2072. PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
  2073. ASSERT_OPERATION_IS_ACTIVE( operation );
  2074. TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
  2075. PSA_ERROR_BAD_STATE );
  2076. ASSERT_OPERATION_IS_INACTIVE( operation );
  2077. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2078. ASSERT_OPERATION_IS_INACTIVE( operation );
  2079. /* Generate an IV without calling setup beforehand. */
  2080. TEST_EQUAL( psa_cipher_generate_iv( &operation,
  2081. buffer, sizeof( buffer ),
  2082. &length ),
  2083. PSA_ERROR_BAD_STATE );
  2084. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2085. /* Generate an IV twice in a row. */
  2086. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2087. PSA_ASSERT( psa_cipher_generate_iv( &operation,
  2088. buffer, sizeof( buffer ),
  2089. &length ) );
  2090. ASSERT_OPERATION_IS_ACTIVE( operation );
  2091. TEST_EQUAL( psa_cipher_generate_iv( &operation,
  2092. buffer, sizeof( buffer ),
  2093. &length ),
  2094. PSA_ERROR_BAD_STATE );
  2095. ASSERT_OPERATION_IS_INACTIVE( operation );
  2096. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2097. ASSERT_OPERATION_IS_INACTIVE( operation );
  2098. /* Generate an IV after it's already set. */
  2099. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2100. PSA_ASSERT( psa_cipher_set_iv( &operation,
  2101. iv, sizeof( iv ) ) );
  2102. TEST_EQUAL( psa_cipher_generate_iv( &operation,
  2103. buffer, sizeof( buffer ),
  2104. &length ),
  2105. PSA_ERROR_BAD_STATE );
  2106. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2107. /* Set an IV without calling setup beforehand. */
  2108. TEST_EQUAL( psa_cipher_set_iv( &operation,
  2109. iv, sizeof( iv ) ),
  2110. PSA_ERROR_BAD_STATE );
  2111. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2112. /* Set an IV after it's already set. */
  2113. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2114. PSA_ASSERT( psa_cipher_set_iv( &operation,
  2115. iv, sizeof( iv ) ) );
  2116. ASSERT_OPERATION_IS_ACTIVE( operation );
  2117. TEST_EQUAL( psa_cipher_set_iv( &operation,
  2118. iv, sizeof( iv ) ),
  2119. PSA_ERROR_BAD_STATE );
  2120. ASSERT_OPERATION_IS_INACTIVE( operation );
  2121. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2122. ASSERT_OPERATION_IS_INACTIVE( operation );
  2123. /* Set an IV after it's already generated. */
  2124. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2125. PSA_ASSERT( psa_cipher_generate_iv( &operation,
  2126. buffer, sizeof( buffer ),
  2127. &length ) );
  2128. TEST_EQUAL( psa_cipher_set_iv( &operation,
  2129. iv, sizeof( iv ) ),
  2130. PSA_ERROR_BAD_STATE );
  2131. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2132. /* Call update without calling setup beforehand. */
  2133. TEST_EQUAL( psa_cipher_update( &operation,
  2134. text, sizeof( text ),
  2135. buffer, sizeof( buffer ),
  2136. &length ),
  2137. PSA_ERROR_BAD_STATE );
  2138. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2139. /* Call update without an IV where an IV is required. */
  2140. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2141. ASSERT_OPERATION_IS_ACTIVE( operation );
  2142. TEST_EQUAL( psa_cipher_update( &operation,
  2143. text, sizeof( text ),
  2144. buffer, sizeof( buffer ),
  2145. &length ),
  2146. PSA_ERROR_BAD_STATE );
  2147. ASSERT_OPERATION_IS_INACTIVE( operation );
  2148. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2149. ASSERT_OPERATION_IS_INACTIVE( operation );
  2150. /* Call update after finish. */
  2151. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2152. PSA_ASSERT( psa_cipher_set_iv( &operation,
  2153. iv, sizeof( iv ) ) );
  2154. PSA_ASSERT( psa_cipher_finish( &operation,
  2155. buffer, sizeof( buffer ), &length ) );
  2156. TEST_EQUAL( psa_cipher_update( &operation,
  2157. text, sizeof( text ),
  2158. buffer, sizeof( buffer ),
  2159. &length ),
  2160. PSA_ERROR_BAD_STATE );
  2161. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2162. /* Call finish without calling setup beforehand. */
  2163. TEST_EQUAL( psa_cipher_finish( &operation,
  2164. buffer, sizeof( buffer ), &length ),
  2165. PSA_ERROR_BAD_STATE );
  2166. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2167. /* Call finish without an IV where an IV is required. */
  2168. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2169. /* Not calling update means we are encrypting an empty buffer, which is OK
  2170. * for cipher modes with padding. */
  2171. ASSERT_OPERATION_IS_ACTIVE( operation );
  2172. TEST_EQUAL( psa_cipher_finish( &operation,
  2173. buffer, sizeof( buffer ), &length ),
  2174. PSA_ERROR_BAD_STATE );
  2175. ASSERT_OPERATION_IS_INACTIVE( operation );
  2176. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2177. ASSERT_OPERATION_IS_INACTIVE( operation );
  2178. /* Call finish twice in a row. */
  2179. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2180. PSA_ASSERT( psa_cipher_set_iv( &operation,
  2181. iv, sizeof( iv ) ) );
  2182. PSA_ASSERT( psa_cipher_finish( &operation,
  2183. buffer, sizeof( buffer ), &length ) );
  2184. TEST_EQUAL( psa_cipher_finish( &operation,
  2185. buffer, sizeof( buffer ), &length ),
  2186. PSA_ERROR_BAD_STATE );
  2187. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2188. PSA_ASSERT( psa_destroy_key( key ) );
  2189. exit:
  2190. psa_cipher_abort( &operation );
  2191. PSA_DONE( );
  2192. }
  2193. /* END_CASE */
  2194. /* BEGIN_CASE */
  2195. void cipher_encrypt_fail( int alg_arg,
  2196. int key_type_arg,
  2197. data_t *key_data,
  2198. data_t *input,
  2199. int expected_status_arg )
  2200. {
  2201. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2202. psa_status_t status;
  2203. psa_key_type_t key_type = key_type_arg;
  2204. psa_algorithm_t alg = alg_arg;
  2205. psa_status_t expected_status = expected_status_arg;
  2206. unsigned char *output = NULL;
  2207. size_t output_buffer_size = 0;
  2208. size_t output_length = 0;
  2209. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2210. if ( PSA_ERROR_BAD_STATE != expected_status )
  2211. {
  2212. PSA_ASSERT( psa_crypto_init( ) );
  2213. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
  2214. psa_set_key_algorithm( &attributes, alg );
  2215. psa_set_key_type( &attributes, key_type );
  2216. output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
  2217. input->len );
  2218. ASSERT_ALLOC( output, output_buffer_size );
  2219. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2220. &key ) );
  2221. }
  2222. status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
  2223. output_buffer_size, &output_length );
  2224. TEST_EQUAL( status, expected_status );
  2225. exit:
  2226. mbedtls_free( output );
  2227. psa_destroy_key( key );
  2228. PSA_DONE( );
  2229. }
  2230. /* END_CASE */
  2231. /* BEGIN_CASE */
  2232. void cipher_encrypt_alg_without_iv( int alg_arg,
  2233. int key_type_arg,
  2234. data_t *key_data,
  2235. data_t *input,
  2236. data_t *expected_output )
  2237. {
  2238. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2239. psa_key_type_t key_type = key_type_arg;
  2240. psa_algorithm_t alg = alg_arg;
  2241. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  2242. uint8_t iv[1] = { 0x5a };
  2243. size_t iv_length;
  2244. unsigned char *output = NULL;
  2245. size_t output_buffer_size = 0;
  2246. size_t output_length = 0;
  2247. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2248. PSA_ASSERT( psa_crypto_init( ) );
  2249. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
  2250. psa_set_key_algorithm( &attributes, alg );
  2251. psa_set_key_type( &attributes, key_type );
  2252. output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
  2253. ASSERT_ALLOC( output, output_buffer_size );
  2254. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2255. &key ) );
  2256. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2257. TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
  2258. PSA_ERROR_BAD_STATE );
  2259. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2260. TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
  2261. &iv_length ),
  2262. PSA_ERROR_BAD_STATE );
  2263. PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
  2264. output_buffer_size, &output_length ) );
  2265. TEST_ASSERT( output_length <=
  2266. PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
  2267. TEST_ASSERT( output_length <=
  2268. PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
  2269. ASSERT_COMPARE( expected_output->x, expected_output->len,
  2270. output, output_length );
  2271. exit:
  2272. mbedtls_free( output );
  2273. psa_destroy_key( key );
  2274. PSA_DONE( );
  2275. }
  2276. /* END_CASE */
  2277. /* BEGIN_CASE */
  2278. void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
  2279. {
  2280. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2281. psa_algorithm_t alg = alg_arg;
  2282. psa_key_type_t key_type = key_type_arg;
  2283. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2284. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  2285. psa_status_t status;
  2286. PSA_ASSERT( psa_crypto_init( ) );
  2287. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
  2288. psa_set_key_algorithm( &attributes, alg );
  2289. psa_set_key_type( &attributes, key_type );
  2290. /* Usage of either of these two size macros would cause divide by zero
  2291. * with incorrect key types previously. Input length should be irrelevant
  2292. * here. */
  2293. TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
  2294. 0 );
  2295. TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
  2296. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2297. &key ) );
  2298. /* Should fail due to invalid alg type (to support invalid key type).
  2299. * Encrypt or decrypt will end up in the same place. */
  2300. status = psa_cipher_encrypt_setup( &operation, key, alg );
  2301. TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
  2302. exit:
  2303. psa_cipher_abort( &operation );
  2304. psa_destroy_key( key );
  2305. PSA_DONE( );
  2306. }
  2307. /* END_CASE */
  2308. /* BEGIN_CASE */
  2309. void cipher_encrypt_validation( int alg_arg,
  2310. int key_type_arg,
  2311. data_t *key_data,
  2312. data_t *input )
  2313. {
  2314. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2315. psa_key_type_t key_type = key_type_arg;
  2316. psa_algorithm_t alg = alg_arg;
  2317. size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
  2318. unsigned char *output1 = NULL;
  2319. size_t output1_buffer_size = 0;
  2320. size_t output1_length = 0;
  2321. unsigned char *output2 = NULL;
  2322. size_t output2_buffer_size = 0;
  2323. size_t output2_length = 0;
  2324. size_t function_output_length = 0;
  2325. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  2326. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2327. PSA_ASSERT( psa_crypto_init( ) );
  2328. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
  2329. psa_set_key_algorithm( &attributes, alg );
  2330. psa_set_key_type( &attributes, key_type );
  2331. output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
  2332. output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
  2333. PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
  2334. ASSERT_ALLOC( output1, output1_buffer_size );
  2335. ASSERT_ALLOC( output2, output2_buffer_size );
  2336. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2337. &key ) );
  2338. /* The one-shot cipher encryption uses generated iv so validating
  2339. the output is not possible. Validating with multipart encryption. */
  2340. PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
  2341. output1_buffer_size, &output1_length ) );
  2342. TEST_ASSERT( output1_length <=
  2343. PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
  2344. TEST_ASSERT( output1_length <=
  2345. PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
  2346. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2347. PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
  2348. PSA_ASSERT( psa_cipher_update( &operation,
  2349. input->x, input->len,
  2350. output2, output2_buffer_size,
  2351. &function_output_length ) );
  2352. TEST_ASSERT( function_output_length <=
  2353. PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
  2354. TEST_ASSERT( function_output_length <=
  2355. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
  2356. output2_length += function_output_length;
  2357. PSA_ASSERT( psa_cipher_finish( &operation,
  2358. output2 + output2_length,
  2359. output2_buffer_size - output2_length,
  2360. &function_output_length ) );
  2361. TEST_ASSERT( function_output_length <=
  2362. PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
  2363. TEST_ASSERT( function_output_length <=
  2364. PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
  2365. output2_length += function_output_length;
  2366. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2367. ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
  2368. output2, output2_length );
  2369. exit:
  2370. psa_cipher_abort( &operation );
  2371. mbedtls_free( output1 );
  2372. mbedtls_free( output2 );
  2373. psa_destroy_key( key );
  2374. PSA_DONE( );
  2375. }
  2376. /* END_CASE */
  2377. /* BEGIN_CASE */
  2378. void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
  2379. data_t *key_data, data_t *iv,
  2380. data_t *input,
  2381. int first_part_size_arg,
  2382. int output1_length_arg, int output2_length_arg,
  2383. data_t *expected_output,
  2384. int expected_status_arg )
  2385. {
  2386. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2387. psa_key_type_t key_type = key_type_arg;
  2388. psa_algorithm_t alg = alg_arg;
  2389. psa_status_t status;
  2390. psa_status_t expected_status = expected_status_arg;
  2391. size_t first_part_size = first_part_size_arg;
  2392. size_t output1_length = output1_length_arg;
  2393. size_t output2_length = output2_length_arg;
  2394. unsigned char *output = NULL;
  2395. size_t output_buffer_size = 0;
  2396. size_t function_output_length = 0;
  2397. size_t total_output_length = 0;
  2398. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  2399. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2400. PSA_ASSERT( psa_crypto_init( ) );
  2401. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
  2402. psa_set_key_algorithm( &attributes, alg );
  2403. psa_set_key_type( &attributes, key_type );
  2404. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2405. &key ) );
  2406. PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
  2407. if( iv->len > 0 )
  2408. {
  2409. PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
  2410. }
  2411. output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
  2412. PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
  2413. ASSERT_ALLOC( output, output_buffer_size );
  2414. TEST_ASSERT( first_part_size <= input->len );
  2415. PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
  2416. output, output_buffer_size,
  2417. &function_output_length ) );
  2418. TEST_ASSERT( function_output_length == output1_length );
  2419. TEST_ASSERT( function_output_length <=
  2420. PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
  2421. TEST_ASSERT( function_output_length <=
  2422. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
  2423. total_output_length += function_output_length;
  2424. if( first_part_size < input->len )
  2425. {
  2426. PSA_ASSERT( psa_cipher_update( &operation,
  2427. input->x + first_part_size,
  2428. input->len - first_part_size,
  2429. ( output_buffer_size == 0 ? NULL :
  2430. output + total_output_length ),
  2431. output_buffer_size - total_output_length,
  2432. &function_output_length ) );
  2433. TEST_ASSERT( function_output_length == output2_length );
  2434. TEST_ASSERT( function_output_length <=
  2435. PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
  2436. alg,
  2437. input->len - first_part_size ) );
  2438. TEST_ASSERT( function_output_length <=
  2439. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
  2440. total_output_length += function_output_length;
  2441. }
  2442. status = psa_cipher_finish( &operation,
  2443. ( output_buffer_size == 0 ? NULL :
  2444. output + total_output_length ),
  2445. output_buffer_size - total_output_length,
  2446. &function_output_length );
  2447. TEST_ASSERT( function_output_length <=
  2448. PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
  2449. TEST_ASSERT( function_output_length <=
  2450. PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
  2451. total_output_length += function_output_length;
  2452. TEST_EQUAL( status, expected_status );
  2453. if( expected_status == PSA_SUCCESS )
  2454. {
  2455. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2456. ASSERT_COMPARE( expected_output->x, expected_output->len,
  2457. output, total_output_length );
  2458. }
  2459. exit:
  2460. psa_cipher_abort( &operation );
  2461. mbedtls_free( output );
  2462. psa_destroy_key( key );
  2463. PSA_DONE( );
  2464. }
  2465. /* END_CASE */
  2466. /* BEGIN_CASE */
  2467. void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
  2468. data_t *key_data, data_t *iv,
  2469. data_t *input,
  2470. int first_part_size_arg,
  2471. int output1_length_arg, int output2_length_arg,
  2472. data_t *expected_output,
  2473. int expected_status_arg )
  2474. {
  2475. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2476. psa_key_type_t key_type = key_type_arg;
  2477. psa_algorithm_t alg = alg_arg;
  2478. psa_status_t status;
  2479. psa_status_t expected_status = expected_status_arg;
  2480. size_t first_part_size = first_part_size_arg;
  2481. size_t output1_length = output1_length_arg;
  2482. size_t output2_length = output2_length_arg;
  2483. unsigned char *output = NULL;
  2484. size_t output_buffer_size = 0;
  2485. size_t function_output_length = 0;
  2486. size_t total_output_length = 0;
  2487. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  2488. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2489. PSA_ASSERT( psa_crypto_init( ) );
  2490. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
  2491. psa_set_key_algorithm( &attributes, alg );
  2492. psa_set_key_type( &attributes, key_type );
  2493. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2494. &key ) );
  2495. PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
  2496. if( iv->len > 0 )
  2497. {
  2498. PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
  2499. }
  2500. output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
  2501. PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
  2502. ASSERT_ALLOC( output, output_buffer_size );
  2503. TEST_ASSERT( first_part_size <= input->len );
  2504. PSA_ASSERT( psa_cipher_update( &operation,
  2505. input->x, first_part_size,
  2506. output, output_buffer_size,
  2507. &function_output_length ) );
  2508. TEST_ASSERT( function_output_length == output1_length );
  2509. TEST_ASSERT( function_output_length <=
  2510. PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
  2511. TEST_ASSERT( function_output_length <=
  2512. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
  2513. total_output_length += function_output_length;
  2514. if( first_part_size < input->len )
  2515. {
  2516. PSA_ASSERT( psa_cipher_update( &operation,
  2517. input->x + first_part_size,
  2518. input->len - first_part_size,
  2519. ( output_buffer_size == 0 ? NULL :
  2520. output + total_output_length ),
  2521. output_buffer_size - total_output_length,
  2522. &function_output_length ) );
  2523. TEST_ASSERT( function_output_length == output2_length );
  2524. TEST_ASSERT( function_output_length <=
  2525. PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
  2526. alg,
  2527. input->len - first_part_size ) );
  2528. TEST_ASSERT( function_output_length <=
  2529. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
  2530. total_output_length += function_output_length;
  2531. }
  2532. status = psa_cipher_finish( &operation,
  2533. ( output_buffer_size == 0 ? NULL :
  2534. output + total_output_length ),
  2535. output_buffer_size - total_output_length,
  2536. &function_output_length );
  2537. TEST_ASSERT( function_output_length <=
  2538. PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
  2539. TEST_ASSERT( function_output_length <=
  2540. PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
  2541. total_output_length += function_output_length;
  2542. TEST_EQUAL( status, expected_status );
  2543. if( expected_status == PSA_SUCCESS )
  2544. {
  2545. PSA_ASSERT( psa_cipher_abort( &operation ) );
  2546. ASSERT_COMPARE( expected_output->x, expected_output->len,
  2547. output, total_output_length );
  2548. }
  2549. exit:
  2550. psa_cipher_abort( &operation );
  2551. mbedtls_free( output );
  2552. psa_destroy_key( key );
  2553. PSA_DONE( );
  2554. }
  2555. /* END_CASE */
  2556. /* BEGIN_CASE */
  2557. void cipher_decrypt_fail( int alg_arg,
  2558. int key_type_arg,
  2559. data_t *key_data,
  2560. data_t *iv,
  2561. data_t *input_arg,
  2562. int expected_status_arg )
  2563. {
  2564. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2565. psa_status_t status;
  2566. psa_key_type_t key_type = key_type_arg;
  2567. psa_algorithm_t alg = alg_arg;
  2568. psa_status_t expected_status = expected_status_arg;
  2569. unsigned char *input = NULL;
  2570. size_t input_buffer_size = 0;
  2571. unsigned char *output = NULL;
  2572. size_t output_buffer_size = 0;
  2573. size_t output_length = 0;
  2574. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2575. if ( PSA_ERROR_BAD_STATE != expected_status )
  2576. {
  2577. PSA_ASSERT( psa_crypto_init( ) );
  2578. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
  2579. psa_set_key_algorithm( &attributes, alg );
  2580. psa_set_key_type( &attributes, key_type );
  2581. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2582. &key ) );
  2583. }
  2584. /* Allocate input buffer and copy the iv and the plaintext */
  2585. input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
  2586. if ( input_buffer_size > 0 )
  2587. {
  2588. ASSERT_ALLOC( input, input_buffer_size );
  2589. memcpy( input, iv->x, iv->len );
  2590. memcpy( input + iv->len, input_arg->x, input_arg->len );
  2591. }
  2592. output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
  2593. ASSERT_ALLOC( output, output_buffer_size );
  2594. status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
  2595. output_buffer_size, &output_length );
  2596. TEST_EQUAL( status, expected_status );
  2597. exit:
  2598. mbedtls_free( input );
  2599. mbedtls_free( output );
  2600. psa_destroy_key( key );
  2601. PSA_DONE( );
  2602. }
  2603. /* END_CASE */
  2604. /* BEGIN_CASE */
  2605. void cipher_decrypt( int alg_arg,
  2606. int key_type_arg,
  2607. data_t *key_data,
  2608. data_t *iv,
  2609. data_t *input_arg,
  2610. data_t *expected_output )
  2611. {
  2612. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2613. psa_key_type_t key_type = key_type_arg;
  2614. psa_algorithm_t alg = alg_arg;
  2615. unsigned char *input = NULL;
  2616. size_t input_buffer_size = 0;
  2617. unsigned char *output = NULL;
  2618. size_t output_buffer_size = 0;
  2619. size_t output_length = 0;
  2620. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2621. PSA_ASSERT( psa_crypto_init( ) );
  2622. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
  2623. psa_set_key_algorithm( &attributes, alg );
  2624. psa_set_key_type( &attributes, key_type );
  2625. /* Allocate input buffer and copy the iv and the plaintext */
  2626. input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
  2627. if ( input_buffer_size > 0 )
  2628. {
  2629. ASSERT_ALLOC( input, input_buffer_size );
  2630. memcpy( input, iv->x, iv->len );
  2631. memcpy( input + iv->len, input_arg->x, input_arg->len );
  2632. }
  2633. output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
  2634. ASSERT_ALLOC( output, output_buffer_size );
  2635. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2636. &key ) );
  2637. PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
  2638. output_buffer_size, &output_length ) );
  2639. TEST_ASSERT( output_length <=
  2640. PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
  2641. TEST_ASSERT( output_length <=
  2642. PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
  2643. ASSERT_COMPARE( expected_output->x, expected_output->len,
  2644. output, output_length );
  2645. exit:
  2646. mbedtls_free( input );
  2647. mbedtls_free( output );
  2648. psa_destroy_key( key );
  2649. PSA_DONE( );
  2650. }
  2651. /* END_CASE */
  2652. /* BEGIN_CASE */
  2653. void cipher_verify_output( int alg_arg,
  2654. int key_type_arg,
  2655. data_t *key_data,
  2656. data_t *input )
  2657. {
  2658. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2659. psa_key_type_t key_type = key_type_arg;
  2660. psa_algorithm_t alg = alg_arg;
  2661. unsigned char *output1 = NULL;
  2662. size_t output1_size = 0;
  2663. size_t output1_length = 0;
  2664. unsigned char *output2 = NULL;
  2665. size_t output2_size = 0;
  2666. size_t output2_length = 0;
  2667. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2668. PSA_ASSERT( psa_crypto_init( ) );
  2669. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
  2670. psa_set_key_algorithm( &attributes, alg );
  2671. psa_set_key_type( &attributes, key_type );
  2672. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2673. &key ) );
  2674. output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
  2675. ASSERT_ALLOC( output1, output1_size );
  2676. PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
  2677. output1, output1_size,
  2678. &output1_length ) );
  2679. TEST_ASSERT( output1_length <=
  2680. PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
  2681. TEST_ASSERT( output1_length <=
  2682. PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
  2683. output2_size = output1_length;
  2684. ASSERT_ALLOC( output2, output2_size );
  2685. PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
  2686. output2, output2_size,
  2687. &output2_length ) );
  2688. TEST_ASSERT( output2_length <=
  2689. PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
  2690. TEST_ASSERT( output2_length <=
  2691. PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
  2692. ASSERT_COMPARE( input->x, input->len, output2, output2_length );
  2693. exit:
  2694. mbedtls_free( output1 );
  2695. mbedtls_free( output2 );
  2696. psa_destroy_key( key );
  2697. PSA_DONE( );
  2698. }
  2699. /* END_CASE */
  2700. /* BEGIN_CASE */
  2701. void cipher_verify_output_multipart( int alg_arg,
  2702. int key_type_arg,
  2703. data_t *key_data,
  2704. data_t *input,
  2705. int first_part_size_arg )
  2706. {
  2707. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2708. psa_key_type_t key_type = key_type_arg;
  2709. psa_algorithm_t alg = alg_arg;
  2710. size_t first_part_size = first_part_size_arg;
  2711. unsigned char iv[16] = {0};
  2712. size_t iv_size = 16;
  2713. size_t iv_length = 0;
  2714. unsigned char *output1 = NULL;
  2715. size_t output1_buffer_size = 0;
  2716. size_t output1_length = 0;
  2717. unsigned char *output2 = NULL;
  2718. size_t output2_buffer_size = 0;
  2719. size_t output2_length = 0;
  2720. size_t function_output_length;
  2721. psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
  2722. psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
  2723. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2724. PSA_ASSERT( psa_crypto_init( ) );
  2725. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
  2726. psa_set_key_algorithm( &attributes, alg );
  2727. psa_set_key_type( &attributes, key_type );
  2728. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2729. &key ) );
  2730. PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
  2731. PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
  2732. if( alg != PSA_ALG_ECB_NO_PADDING )
  2733. {
  2734. PSA_ASSERT( psa_cipher_generate_iv( &operation1,
  2735. iv, iv_size,
  2736. &iv_length ) );
  2737. }
  2738. output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
  2739. TEST_ASSERT( output1_buffer_size <=
  2740. PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
  2741. ASSERT_ALLOC( output1, output1_buffer_size );
  2742. TEST_ASSERT( first_part_size <= input->len );
  2743. PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
  2744. output1, output1_buffer_size,
  2745. &function_output_length ) );
  2746. TEST_ASSERT( function_output_length <=
  2747. PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
  2748. TEST_ASSERT( function_output_length <=
  2749. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
  2750. output1_length += function_output_length;
  2751. PSA_ASSERT( psa_cipher_update( &operation1,
  2752. input->x + first_part_size,
  2753. input->len - first_part_size,
  2754. output1, output1_buffer_size,
  2755. &function_output_length ) );
  2756. TEST_ASSERT( function_output_length <=
  2757. PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
  2758. alg,
  2759. input->len - first_part_size ) );
  2760. TEST_ASSERT( function_output_length <=
  2761. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
  2762. output1_length += function_output_length;
  2763. PSA_ASSERT( psa_cipher_finish( &operation1,
  2764. output1 + output1_length,
  2765. output1_buffer_size - output1_length,
  2766. &function_output_length ) );
  2767. TEST_ASSERT( function_output_length <=
  2768. PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
  2769. TEST_ASSERT( function_output_length <=
  2770. PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
  2771. output1_length += function_output_length;
  2772. PSA_ASSERT( psa_cipher_abort( &operation1 ) );
  2773. output2_buffer_size = output1_length;
  2774. TEST_ASSERT( output2_buffer_size <=
  2775. PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
  2776. TEST_ASSERT( output2_buffer_size <=
  2777. PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
  2778. ASSERT_ALLOC( output2, output2_buffer_size );
  2779. if( iv_length > 0 )
  2780. {
  2781. PSA_ASSERT( psa_cipher_set_iv( &operation2,
  2782. iv, iv_length ) );
  2783. }
  2784. PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
  2785. output2, output2_buffer_size,
  2786. &function_output_length ) );
  2787. TEST_ASSERT( function_output_length <=
  2788. PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
  2789. TEST_ASSERT( function_output_length <=
  2790. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
  2791. output2_length += function_output_length;
  2792. PSA_ASSERT( psa_cipher_update( &operation2,
  2793. output1 + first_part_size,
  2794. output1_length - first_part_size,
  2795. output2, output2_buffer_size,
  2796. &function_output_length ) );
  2797. TEST_ASSERT( function_output_length <=
  2798. PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
  2799. alg,
  2800. output1_length - first_part_size ) );
  2801. TEST_ASSERT( function_output_length <=
  2802. PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
  2803. output2_length += function_output_length;
  2804. PSA_ASSERT( psa_cipher_finish( &operation2,
  2805. output2 + output2_length,
  2806. output2_buffer_size - output2_length,
  2807. &function_output_length ) );
  2808. TEST_ASSERT( function_output_length <=
  2809. PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
  2810. TEST_ASSERT( function_output_length <=
  2811. PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
  2812. output2_length += function_output_length;
  2813. PSA_ASSERT( psa_cipher_abort( &operation2 ) );
  2814. ASSERT_COMPARE( input->x, input->len, output2, output2_length );
  2815. exit:
  2816. psa_cipher_abort( &operation1 );
  2817. psa_cipher_abort( &operation2 );
  2818. mbedtls_free( output1 );
  2819. mbedtls_free( output2 );
  2820. psa_destroy_key( key );
  2821. PSA_DONE( );
  2822. }
  2823. /* END_CASE */
  2824. /* BEGIN_CASE */
  2825. void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
  2826. int alg_arg,
  2827. data_t *nonce,
  2828. data_t *additional_data,
  2829. data_t *input_data,
  2830. int expected_result_arg )
  2831. {
  2832. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2833. psa_key_type_t key_type = key_type_arg;
  2834. psa_algorithm_t alg = alg_arg;
  2835. size_t key_bits;
  2836. unsigned char *output_data = NULL;
  2837. size_t output_size = 0;
  2838. size_t output_length = 0;
  2839. unsigned char *output_data2 = NULL;
  2840. size_t output_length2 = 0;
  2841. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  2842. psa_status_t expected_result = expected_result_arg;
  2843. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2844. PSA_ASSERT( psa_crypto_init( ) );
  2845. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
  2846. psa_set_key_algorithm( &attributes, alg );
  2847. psa_set_key_type( &attributes, key_type );
  2848. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2849. &key ) );
  2850. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  2851. key_bits = psa_get_key_bits( &attributes );
  2852. output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
  2853. alg );
  2854. /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
  2855. * should be exact. */
  2856. if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
  2857. expected_result != PSA_ERROR_NOT_SUPPORTED )
  2858. {
  2859. TEST_EQUAL( output_size,
  2860. PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
  2861. TEST_ASSERT( output_size <=
  2862. PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
  2863. }
  2864. ASSERT_ALLOC( output_data, output_size );
  2865. status = psa_aead_encrypt( key, alg,
  2866. nonce->x, nonce->len,
  2867. additional_data->x,
  2868. additional_data->len,
  2869. input_data->x, input_data->len,
  2870. output_data, output_size,
  2871. &output_length );
  2872. /* If the operation is not supported, just skip and not fail in case the
  2873. * encryption involves a common limitation of cryptography hardwares and
  2874. * an alternative implementation. */
  2875. if( status == PSA_ERROR_NOT_SUPPORTED )
  2876. {
  2877. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
  2878. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
  2879. }
  2880. TEST_EQUAL( status, expected_result );
  2881. if( PSA_SUCCESS == expected_result )
  2882. {
  2883. ASSERT_ALLOC( output_data2, output_length );
  2884. /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
  2885. * should be exact. */
  2886. TEST_EQUAL( input_data->len,
  2887. PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
  2888. TEST_ASSERT( input_data->len <=
  2889. PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
  2890. TEST_EQUAL( psa_aead_decrypt( key, alg,
  2891. nonce->x, nonce->len,
  2892. additional_data->x,
  2893. additional_data->len,
  2894. output_data, output_length,
  2895. output_data2, output_length,
  2896. &output_length2 ),
  2897. expected_result );
  2898. ASSERT_COMPARE( input_data->x, input_data->len,
  2899. output_data2, output_length2 );
  2900. }
  2901. exit:
  2902. psa_destroy_key( key );
  2903. mbedtls_free( output_data );
  2904. mbedtls_free( output_data2 );
  2905. PSA_DONE( );
  2906. }
  2907. /* END_CASE */
  2908. /* BEGIN_CASE */
  2909. void aead_encrypt( int key_type_arg, data_t *key_data,
  2910. int alg_arg,
  2911. data_t *nonce,
  2912. data_t *additional_data,
  2913. data_t *input_data,
  2914. data_t *expected_result )
  2915. {
  2916. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2917. psa_key_type_t key_type = key_type_arg;
  2918. psa_algorithm_t alg = alg_arg;
  2919. size_t key_bits;
  2920. unsigned char *output_data = NULL;
  2921. size_t output_size = 0;
  2922. size_t output_length = 0;
  2923. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2924. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  2925. PSA_ASSERT( psa_crypto_init( ) );
  2926. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
  2927. psa_set_key_algorithm( &attributes, alg );
  2928. psa_set_key_type( &attributes, key_type );
  2929. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2930. &key ) );
  2931. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  2932. key_bits = psa_get_key_bits( &attributes );
  2933. output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
  2934. alg );
  2935. /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
  2936. * should be exact. */
  2937. TEST_EQUAL( output_size,
  2938. PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
  2939. TEST_ASSERT( output_size <=
  2940. PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
  2941. ASSERT_ALLOC( output_data, output_size );
  2942. status = psa_aead_encrypt( key, alg,
  2943. nonce->x, nonce->len,
  2944. additional_data->x, additional_data->len,
  2945. input_data->x, input_data->len,
  2946. output_data, output_size,
  2947. &output_length );
  2948. /* If the operation is not supported, just skip and not fail in case the
  2949. * encryption involves a common limitation of cryptography hardwares and
  2950. * an alternative implementation. */
  2951. if( status == PSA_ERROR_NOT_SUPPORTED )
  2952. {
  2953. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
  2954. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
  2955. }
  2956. PSA_ASSERT( status );
  2957. ASSERT_COMPARE( expected_result->x, expected_result->len,
  2958. output_data, output_length );
  2959. exit:
  2960. psa_destroy_key( key );
  2961. mbedtls_free( output_data );
  2962. PSA_DONE( );
  2963. }
  2964. /* END_CASE */
  2965. /* BEGIN_CASE */
  2966. void aead_decrypt( int key_type_arg, data_t *key_data,
  2967. int alg_arg,
  2968. data_t *nonce,
  2969. data_t *additional_data,
  2970. data_t *input_data,
  2971. data_t *expected_data,
  2972. int expected_result_arg )
  2973. {
  2974. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  2975. psa_key_type_t key_type = key_type_arg;
  2976. psa_algorithm_t alg = alg_arg;
  2977. size_t key_bits;
  2978. unsigned char *output_data = NULL;
  2979. size_t output_size = 0;
  2980. size_t output_length = 0;
  2981. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  2982. psa_status_t expected_result = expected_result_arg;
  2983. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  2984. PSA_ASSERT( psa_crypto_init( ) );
  2985. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
  2986. psa_set_key_algorithm( &attributes, alg );
  2987. psa_set_key_type( &attributes, key_type );
  2988. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  2989. &key ) );
  2990. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  2991. key_bits = psa_get_key_bits( &attributes );
  2992. output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
  2993. alg );
  2994. if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
  2995. expected_result != PSA_ERROR_NOT_SUPPORTED )
  2996. {
  2997. /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
  2998. * should be exact. */
  2999. TEST_EQUAL( output_size,
  3000. PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
  3001. TEST_ASSERT( output_size <=
  3002. PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
  3003. }
  3004. ASSERT_ALLOC( output_data, output_size );
  3005. status = psa_aead_decrypt( key, alg,
  3006. nonce->x, nonce->len,
  3007. additional_data->x,
  3008. additional_data->len,
  3009. input_data->x, input_data->len,
  3010. output_data, output_size,
  3011. &output_length );
  3012. /* If the operation is not supported, just skip and not fail in case the
  3013. * decryption involves a common limitation of cryptography hardwares and
  3014. * an alternative implementation. */
  3015. if( status == PSA_ERROR_NOT_SUPPORTED )
  3016. {
  3017. MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
  3018. MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
  3019. }
  3020. TEST_EQUAL( status, expected_result );
  3021. if( expected_result == PSA_SUCCESS )
  3022. ASSERT_COMPARE( expected_data->x, expected_data->len,
  3023. output_data, output_length );
  3024. exit:
  3025. psa_destroy_key( key );
  3026. mbedtls_free( output_data );
  3027. PSA_DONE( );
  3028. }
  3029. /* END_CASE */
  3030. /* BEGIN_CASE */
  3031. void signature_size( int type_arg,
  3032. int bits,
  3033. int alg_arg,
  3034. int expected_size_arg )
  3035. {
  3036. psa_key_type_t type = type_arg;
  3037. psa_algorithm_t alg = alg_arg;
  3038. size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
  3039. TEST_EQUAL( actual_size, (size_t) expected_size_arg );
  3040. #if defined(MBEDTLS_TEST_DEPRECATED)
  3041. TEST_EQUAL( actual_size,
  3042. PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
  3043. #endif /* MBEDTLS_TEST_DEPRECATED */
  3044. exit:
  3045. ;
  3046. }
  3047. /* END_CASE */
  3048. /* BEGIN_CASE */
  3049. void sign_hash_deterministic( int key_type_arg, data_t *key_data,
  3050. int alg_arg, data_t *input_data,
  3051. data_t *output_data )
  3052. {
  3053. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3054. psa_key_type_t key_type = key_type_arg;
  3055. psa_algorithm_t alg = alg_arg;
  3056. size_t key_bits;
  3057. unsigned char *signature = NULL;
  3058. size_t signature_size;
  3059. size_t signature_length = 0xdeadbeef;
  3060. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3061. PSA_ASSERT( psa_crypto_init( ) );
  3062. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
  3063. psa_set_key_algorithm( &attributes, alg );
  3064. psa_set_key_type( &attributes, key_type );
  3065. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3066. &key ) );
  3067. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  3068. key_bits = psa_get_key_bits( &attributes );
  3069. /* Allocate a buffer which has the size advertized by the
  3070. * library. */
  3071. signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
  3072. key_bits, alg );
  3073. TEST_ASSERT( signature_size != 0 );
  3074. TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
  3075. ASSERT_ALLOC( signature, signature_size );
  3076. /* Perform the signature. */
  3077. PSA_ASSERT( psa_sign_hash( key, alg,
  3078. input_data->x, input_data->len,
  3079. signature, signature_size,
  3080. &signature_length ) );
  3081. /* Verify that the signature is what is expected. */
  3082. ASSERT_COMPARE( output_data->x, output_data->len,
  3083. signature, signature_length );
  3084. #if defined(MBEDTLS_TEST_DEPRECATED)
  3085. memset( signature, 0, signature_size );
  3086. signature_length = INVALID_EXPORT_LENGTH;
  3087. PSA_ASSERT( psa_asymmetric_sign( key, alg,
  3088. input_data->x, input_data->len,
  3089. signature, signature_size,
  3090. &signature_length ) );
  3091. ASSERT_COMPARE( output_data->x, output_data->len,
  3092. signature, signature_length );
  3093. #endif /* MBEDTLS_TEST_DEPRECATED */
  3094. exit:
  3095. /*
  3096. * Key attributes may have been returned by psa_get_key_attributes()
  3097. * thus reset them as required.
  3098. */
  3099. psa_reset_key_attributes( &attributes );
  3100. psa_destroy_key( key );
  3101. mbedtls_free( signature );
  3102. PSA_DONE( );
  3103. }
  3104. /* END_CASE */
  3105. /* BEGIN_CASE */
  3106. void sign_hash_fail( int key_type_arg, data_t *key_data,
  3107. int alg_arg, data_t *input_data,
  3108. int signature_size_arg, int expected_status_arg )
  3109. {
  3110. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3111. psa_key_type_t key_type = key_type_arg;
  3112. psa_algorithm_t alg = alg_arg;
  3113. size_t signature_size = signature_size_arg;
  3114. psa_status_t actual_status;
  3115. psa_status_t expected_status = expected_status_arg;
  3116. unsigned char *signature = NULL;
  3117. size_t signature_length = 0xdeadbeef;
  3118. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3119. ASSERT_ALLOC( signature, signature_size );
  3120. PSA_ASSERT( psa_crypto_init( ) );
  3121. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
  3122. psa_set_key_algorithm( &attributes, alg );
  3123. psa_set_key_type( &attributes, key_type );
  3124. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3125. &key ) );
  3126. actual_status = psa_sign_hash( key, alg,
  3127. input_data->x, input_data->len,
  3128. signature, signature_size,
  3129. &signature_length );
  3130. TEST_EQUAL( actual_status, expected_status );
  3131. /* The value of *signature_length is unspecified on error, but
  3132. * whatever it is, it should be less than signature_size, so that
  3133. * if the caller tries to read *signature_length bytes without
  3134. * checking the error code then they don't overflow a buffer. */
  3135. TEST_ASSERT( signature_length <= signature_size );
  3136. #if defined(MBEDTLS_TEST_DEPRECATED)
  3137. signature_length = INVALID_EXPORT_LENGTH;
  3138. TEST_EQUAL( psa_asymmetric_sign( key, alg,
  3139. input_data->x, input_data->len,
  3140. signature, signature_size,
  3141. &signature_length ),
  3142. expected_status );
  3143. TEST_ASSERT( signature_length <= signature_size );
  3144. #endif /* MBEDTLS_TEST_DEPRECATED */
  3145. exit:
  3146. psa_reset_key_attributes( &attributes );
  3147. psa_destroy_key( key );
  3148. mbedtls_free( signature );
  3149. PSA_DONE( );
  3150. }
  3151. /* END_CASE */
  3152. /* BEGIN_CASE */
  3153. void sign_verify_hash( int key_type_arg, data_t *key_data,
  3154. int alg_arg, data_t *input_data )
  3155. {
  3156. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3157. psa_key_type_t key_type = key_type_arg;
  3158. psa_algorithm_t alg = alg_arg;
  3159. size_t key_bits;
  3160. unsigned char *signature = NULL;
  3161. size_t signature_size;
  3162. size_t signature_length = 0xdeadbeef;
  3163. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3164. PSA_ASSERT( psa_crypto_init( ) );
  3165. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
  3166. psa_set_key_algorithm( &attributes, alg );
  3167. psa_set_key_type( &attributes, key_type );
  3168. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3169. &key ) );
  3170. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  3171. key_bits = psa_get_key_bits( &attributes );
  3172. /* Allocate a buffer which has the size advertized by the
  3173. * library. */
  3174. signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
  3175. key_bits, alg );
  3176. TEST_ASSERT( signature_size != 0 );
  3177. TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
  3178. ASSERT_ALLOC( signature, signature_size );
  3179. /* Perform the signature. */
  3180. PSA_ASSERT( psa_sign_hash( key, alg,
  3181. input_data->x, input_data->len,
  3182. signature, signature_size,
  3183. &signature_length ) );
  3184. /* Check that the signature length looks sensible. */
  3185. TEST_ASSERT( signature_length <= signature_size );
  3186. TEST_ASSERT( signature_length > 0 );
  3187. /* Use the library to verify that the signature is correct. */
  3188. PSA_ASSERT( psa_verify_hash( key, alg,
  3189. input_data->x, input_data->len,
  3190. signature, signature_length ) );
  3191. if( input_data->len != 0 )
  3192. {
  3193. /* Flip a bit in the input and verify that the signature is now
  3194. * detected as invalid. Flip a bit at the beginning, not at the end,
  3195. * because ECDSA may ignore the last few bits of the input. */
  3196. input_data->x[0] ^= 1;
  3197. TEST_EQUAL( psa_verify_hash( key, alg,
  3198. input_data->x, input_data->len,
  3199. signature, signature_length ),
  3200. PSA_ERROR_INVALID_SIGNATURE );
  3201. }
  3202. exit:
  3203. /*
  3204. * Key attributes may have been returned by psa_get_key_attributes()
  3205. * thus reset them as required.
  3206. */
  3207. psa_reset_key_attributes( &attributes );
  3208. psa_destroy_key( key );
  3209. mbedtls_free( signature );
  3210. PSA_DONE( );
  3211. }
  3212. /* END_CASE */
  3213. /* BEGIN_CASE */
  3214. void verify_hash( int key_type_arg, data_t *key_data,
  3215. int alg_arg, data_t *hash_data,
  3216. data_t *signature_data )
  3217. {
  3218. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3219. psa_key_type_t key_type = key_type_arg;
  3220. psa_algorithm_t alg = alg_arg;
  3221. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3222. TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
  3223. PSA_ASSERT( psa_crypto_init( ) );
  3224. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
  3225. psa_set_key_algorithm( &attributes, alg );
  3226. psa_set_key_type( &attributes, key_type );
  3227. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3228. &key ) );
  3229. PSA_ASSERT( psa_verify_hash( key, alg,
  3230. hash_data->x, hash_data->len,
  3231. signature_data->x, signature_data->len ) );
  3232. #if defined(MBEDTLS_TEST_DEPRECATED)
  3233. PSA_ASSERT( psa_asymmetric_verify( key, alg,
  3234. hash_data->x, hash_data->len,
  3235. signature_data->x,
  3236. signature_data->len ) );
  3237. #endif /* MBEDTLS_TEST_DEPRECATED */
  3238. exit:
  3239. psa_reset_key_attributes( &attributes );
  3240. psa_destroy_key( key );
  3241. PSA_DONE( );
  3242. }
  3243. /* END_CASE */
  3244. /* BEGIN_CASE */
  3245. void verify_hash_fail( int key_type_arg, data_t *key_data,
  3246. int alg_arg, data_t *hash_data,
  3247. data_t *signature_data,
  3248. int expected_status_arg )
  3249. {
  3250. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3251. psa_key_type_t key_type = key_type_arg;
  3252. psa_algorithm_t alg = alg_arg;
  3253. psa_status_t actual_status;
  3254. psa_status_t expected_status = expected_status_arg;
  3255. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3256. PSA_ASSERT( psa_crypto_init( ) );
  3257. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
  3258. psa_set_key_algorithm( &attributes, alg );
  3259. psa_set_key_type( &attributes, key_type );
  3260. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3261. &key ) );
  3262. actual_status = psa_verify_hash( key, alg,
  3263. hash_data->x, hash_data->len,
  3264. signature_data->x, signature_data->len );
  3265. TEST_EQUAL( actual_status, expected_status );
  3266. #if defined(MBEDTLS_TEST_DEPRECATED)
  3267. TEST_EQUAL( psa_asymmetric_verify( key, alg,
  3268. hash_data->x, hash_data->len,
  3269. signature_data->x, signature_data->len ),
  3270. expected_status );
  3271. #endif /* MBEDTLS_TEST_DEPRECATED */
  3272. exit:
  3273. psa_reset_key_attributes( &attributes );
  3274. psa_destroy_key( key );
  3275. PSA_DONE( );
  3276. }
  3277. /* END_CASE */
  3278. /* BEGIN_CASE */
  3279. void sign_message_deterministic( int key_type_arg,
  3280. data_t *key_data,
  3281. int alg_arg,
  3282. data_t *input_data,
  3283. data_t *output_data )
  3284. {
  3285. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3286. psa_key_type_t key_type = key_type_arg;
  3287. psa_algorithm_t alg = alg_arg;
  3288. size_t key_bits;
  3289. unsigned char *signature = NULL;
  3290. size_t signature_size;
  3291. size_t signature_length = 0xdeadbeef;
  3292. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3293. PSA_ASSERT( psa_crypto_init( ) );
  3294. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
  3295. psa_set_key_algorithm( &attributes, alg );
  3296. psa_set_key_type( &attributes, key_type );
  3297. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3298. &key ) );
  3299. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  3300. key_bits = psa_get_key_bits( &attributes );
  3301. signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
  3302. TEST_ASSERT( signature_size != 0 );
  3303. TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
  3304. ASSERT_ALLOC( signature, signature_size );
  3305. PSA_ASSERT( psa_sign_message( key, alg,
  3306. input_data->x, input_data->len,
  3307. signature, signature_size,
  3308. &signature_length ) );
  3309. ASSERT_COMPARE( output_data->x, output_data->len,
  3310. signature, signature_length );
  3311. exit:
  3312. psa_reset_key_attributes( &attributes );
  3313. psa_destroy_key( key );
  3314. mbedtls_free( signature );
  3315. PSA_DONE( );
  3316. }
  3317. /* END_CASE */
  3318. /* BEGIN_CASE */
  3319. void sign_message_fail( int key_type_arg,
  3320. data_t *key_data,
  3321. int alg_arg,
  3322. data_t *input_data,
  3323. int signature_size_arg,
  3324. int expected_status_arg )
  3325. {
  3326. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3327. psa_key_type_t key_type = key_type_arg;
  3328. psa_algorithm_t alg = alg_arg;
  3329. size_t signature_size = signature_size_arg;
  3330. psa_status_t actual_status;
  3331. psa_status_t expected_status = expected_status_arg;
  3332. unsigned char *signature = NULL;
  3333. size_t signature_length = 0xdeadbeef;
  3334. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3335. ASSERT_ALLOC( signature, signature_size );
  3336. PSA_ASSERT( psa_crypto_init( ) );
  3337. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
  3338. psa_set_key_algorithm( &attributes, alg );
  3339. psa_set_key_type( &attributes, key_type );
  3340. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3341. &key ) );
  3342. actual_status = psa_sign_message( key, alg,
  3343. input_data->x, input_data->len,
  3344. signature, signature_size,
  3345. &signature_length );
  3346. TEST_EQUAL( actual_status, expected_status );
  3347. /* The value of *signature_length is unspecified on error, but
  3348. * whatever it is, it should be less than signature_size, so that
  3349. * if the caller tries to read *signature_length bytes without
  3350. * checking the error code then they don't overflow a buffer. */
  3351. TEST_ASSERT( signature_length <= signature_size );
  3352. exit:
  3353. psa_reset_key_attributes( &attributes );
  3354. psa_destroy_key( key );
  3355. mbedtls_free( signature );
  3356. PSA_DONE( );
  3357. }
  3358. /* END_CASE */
  3359. /* BEGIN_CASE */
  3360. void sign_verify_message( int key_type_arg,
  3361. data_t *key_data,
  3362. int alg_arg,
  3363. data_t *input_data )
  3364. {
  3365. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3366. psa_key_type_t key_type = key_type_arg;
  3367. psa_algorithm_t alg = alg_arg;
  3368. size_t key_bits;
  3369. unsigned char *signature = NULL;
  3370. size_t signature_size;
  3371. size_t signature_length = 0xdeadbeef;
  3372. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3373. PSA_ASSERT( psa_crypto_init( ) );
  3374. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
  3375. PSA_KEY_USAGE_VERIFY_MESSAGE );
  3376. psa_set_key_algorithm( &attributes, alg );
  3377. psa_set_key_type( &attributes, key_type );
  3378. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3379. &key ) );
  3380. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  3381. key_bits = psa_get_key_bits( &attributes );
  3382. signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
  3383. TEST_ASSERT( signature_size != 0 );
  3384. TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
  3385. ASSERT_ALLOC( signature, signature_size );
  3386. PSA_ASSERT( psa_sign_message( key, alg,
  3387. input_data->x, input_data->len,
  3388. signature, signature_size,
  3389. &signature_length ) );
  3390. TEST_ASSERT( signature_length <= signature_size );
  3391. TEST_ASSERT( signature_length > 0 );
  3392. PSA_ASSERT( psa_verify_message( key, alg,
  3393. input_data->x, input_data->len,
  3394. signature, signature_length ) );
  3395. if( input_data->len != 0 )
  3396. {
  3397. /* Flip a bit in the input and verify that the signature is now
  3398. * detected as invalid. Flip a bit at the beginning, not at the end,
  3399. * because ECDSA may ignore the last few bits of the input. */
  3400. input_data->x[0] ^= 1;
  3401. TEST_EQUAL( psa_verify_message( key, alg,
  3402. input_data->x, input_data->len,
  3403. signature, signature_length ),
  3404. PSA_ERROR_INVALID_SIGNATURE );
  3405. }
  3406. exit:
  3407. psa_reset_key_attributes( &attributes );
  3408. psa_destroy_key( key );
  3409. mbedtls_free( signature );
  3410. PSA_DONE( );
  3411. }
  3412. /* END_CASE */
  3413. /* BEGIN_CASE */
  3414. void verify_message( int key_type_arg,
  3415. data_t *key_data,
  3416. int alg_arg,
  3417. data_t *input_data,
  3418. data_t *signature_data )
  3419. {
  3420. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3421. psa_key_type_t key_type = key_type_arg;
  3422. psa_algorithm_t alg = alg_arg;
  3423. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3424. TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
  3425. PSA_ASSERT( psa_crypto_init( ) );
  3426. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
  3427. psa_set_key_algorithm( &attributes, alg );
  3428. psa_set_key_type( &attributes, key_type );
  3429. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3430. &key ) );
  3431. PSA_ASSERT( psa_verify_message( key, alg,
  3432. input_data->x, input_data->len,
  3433. signature_data->x, signature_data->len ) );
  3434. exit:
  3435. psa_reset_key_attributes( &attributes );
  3436. psa_destroy_key( key );
  3437. PSA_DONE( );
  3438. }
  3439. /* END_CASE */
  3440. /* BEGIN_CASE */
  3441. void verify_message_fail( int key_type_arg,
  3442. data_t *key_data,
  3443. int alg_arg,
  3444. data_t *hash_data,
  3445. data_t *signature_data,
  3446. int expected_status_arg )
  3447. {
  3448. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3449. psa_key_type_t key_type = key_type_arg;
  3450. psa_algorithm_t alg = alg_arg;
  3451. psa_status_t actual_status;
  3452. psa_status_t expected_status = expected_status_arg;
  3453. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3454. PSA_ASSERT( psa_crypto_init( ) );
  3455. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
  3456. psa_set_key_algorithm( &attributes, alg );
  3457. psa_set_key_type( &attributes, key_type );
  3458. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3459. &key ) );
  3460. actual_status = psa_verify_message( key, alg,
  3461. hash_data->x, hash_data->len,
  3462. signature_data->x,
  3463. signature_data->len );
  3464. TEST_EQUAL( actual_status, expected_status );
  3465. exit:
  3466. psa_reset_key_attributes( &attributes );
  3467. psa_destroy_key( key );
  3468. PSA_DONE( );
  3469. }
  3470. /* END_CASE */
  3471. /* BEGIN_CASE */
  3472. void asymmetric_encrypt( int key_type_arg,
  3473. data_t *key_data,
  3474. int alg_arg,
  3475. data_t *input_data,
  3476. data_t *label,
  3477. int expected_output_length_arg,
  3478. int expected_status_arg )
  3479. {
  3480. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3481. psa_key_type_t key_type = key_type_arg;
  3482. psa_algorithm_t alg = alg_arg;
  3483. size_t expected_output_length = expected_output_length_arg;
  3484. size_t key_bits;
  3485. unsigned char *output = NULL;
  3486. size_t output_size;
  3487. size_t output_length = ~0;
  3488. psa_status_t actual_status;
  3489. psa_status_t expected_status = expected_status_arg;
  3490. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3491. PSA_ASSERT( psa_crypto_init( ) );
  3492. /* Import the key */
  3493. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
  3494. psa_set_key_algorithm( &attributes, alg );
  3495. psa_set_key_type( &attributes, key_type );
  3496. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3497. &key ) );
  3498. /* Determine the maximum output length */
  3499. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  3500. key_bits = psa_get_key_bits( &attributes );
  3501. output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
  3502. TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
  3503. ASSERT_ALLOC( output, output_size );
  3504. /* Encrypt the input */
  3505. actual_status = psa_asymmetric_encrypt( key, alg,
  3506. input_data->x, input_data->len,
  3507. label->x, label->len,
  3508. output, output_size,
  3509. &output_length );
  3510. TEST_EQUAL( actual_status, expected_status );
  3511. TEST_EQUAL( output_length, expected_output_length );
  3512. /* If the label is empty, the test framework puts a non-null pointer
  3513. * in label->x. Test that a null pointer works as well. */
  3514. if( label->len == 0 )
  3515. {
  3516. output_length = ~0;
  3517. if( output_size != 0 )
  3518. memset( output, 0, output_size );
  3519. actual_status = psa_asymmetric_encrypt( key, alg,
  3520. input_data->x, input_data->len,
  3521. NULL, label->len,
  3522. output, output_size,
  3523. &output_length );
  3524. TEST_EQUAL( actual_status, expected_status );
  3525. TEST_EQUAL( output_length, expected_output_length );
  3526. }
  3527. exit:
  3528. /*
  3529. * Key attributes may have been returned by psa_get_key_attributes()
  3530. * thus reset them as required.
  3531. */
  3532. psa_reset_key_attributes( &attributes );
  3533. psa_destroy_key( key );
  3534. mbedtls_free( output );
  3535. PSA_DONE( );
  3536. }
  3537. /* END_CASE */
  3538. /* BEGIN_CASE */
  3539. void asymmetric_encrypt_decrypt( int key_type_arg,
  3540. data_t *key_data,
  3541. int alg_arg,
  3542. data_t *input_data,
  3543. data_t *label )
  3544. {
  3545. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3546. psa_key_type_t key_type = key_type_arg;
  3547. psa_algorithm_t alg = alg_arg;
  3548. size_t key_bits;
  3549. unsigned char *output = NULL;
  3550. size_t output_size;
  3551. size_t output_length = ~0;
  3552. unsigned char *output2 = NULL;
  3553. size_t output2_size;
  3554. size_t output2_length = ~0;
  3555. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3556. PSA_ASSERT( psa_crypto_init( ) );
  3557. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
  3558. psa_set_key_algorithm( &attributes, alg );
  3559. psa_set_key_type( &attributes, key_type );
  3560. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3561. &key ) );
  3562. /* Determine the maximum ciphertext length */
  3563. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  3564. key_bits = psa_get_key_bits( &attributes );
  3565. output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
  3566. TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
  3567. ASSERT_ALLOC( output, output_size );
  3568. output2_size = input_data->len;
  3569. TEST_ASSERT( output2_size <=
  3570. PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
  3571. TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
  3572. ASSERT_ALLOC( output2, output2_size );
  3573. /* We test encryption by checking that encrypt-then-decrypt gives back
  3574. * the original plaintext because of the non-optional random
  3575. * part of encryption process which prevents using fixed vectors. */
  3576. PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
  3577. input_data->x, input_data->len,
  3578. label->x, label->len,
  3579. output, output_size,
  3580. &output_length ) );
  3581. /* We don't know what ciphertext length to expect, but check that
  3582. * it looks sensible. */
  3583. TEST_ASSERT( output_length <= output_size );
  3584. PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
  3585. output, output_length,
  3586. label->x, label->len,
  3587. output2, output2_size,
  3588. &output2_length ) );
  3589. ASSERT_COMPARE( input_data->x, input_data->len,
  3590. output2, output2_length );
  3591. exit:
  3592. /*
  3593. * Key attributes may have been returned by psa_get_key_attributes()
  3594. * thus reset them as required.
  3595. */
  3596. psa_reset_key_attributes( &attributes );
  3597. psa_destroy_key( key );
  3598. mbedtls_free( output );
  3599. mbedtls_free( output2 );
  3600. PSA_DONE( );
  3601. }
  3602. /* END_CASE */
  3603. /* BEGIN_CASE */
  3604. void asymmetric_decrypt( int key_type_arg,
  3605. data_t *key_data,
  3606. int alg_arg,
  3607. data_t *input_data,
  3608. data_t *label,
  3609. data_t *expected_data )
  3610. {
  3611. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3612. psa_key_type_t key_type = key_type_arg;
  3613. psa_algorithm_t alg = alg_arg;
  3614. size_t key_bits;
  3615. unsigned char *output = NULL;
  3616. size_t output_size = 0;
  3617. size_t output_length = ~0;
  3618. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3619. PSA_ASSERT( psa_crypto_init( ) );
  3620. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
  3621. psa_set_key_algorithm( &attributes, alg );
  3622. psa_set_key_type( &attributes, key_type );
  3623. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3624. &key ) );
  3625. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  3626. key_bits = psa_get_key_bits( &attributes );
  3627. /* Determine the maximum ciphertext length */
  3628. output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
  3629. TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
  3630. ASSERT_ALLOC( output, output_size );
  3631. PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
  3632. input_data->x, input_data->len,
  3633. label->x, label->len,
  3634. output,
  3635. output_size,
  3636. &output_length ) );
  3637. ASSERT_COMPARE( expected_data->x, expected_data->len,
  3638. output, output_length );
  3639. /* If the label is empty, the test framework puts a non-null pointer
  3640. * in label->x. Test that a null pointer works as well. */
  3641. if( label->len == 0 )
  3642. {
  3643. output_length = ~0;
  3644. if( output_size != 0 )
  3645. memset( output, 0, output_size );
  3646. PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
  3647. input_data->x, input_data->len,
  3648. NULL, label->len,
  3649. output,
  3650. output_size,
  3651. &output_length ) );
  3652. ASSERT_COMPARE( expected_data->x, expected_data->len,
  3653. output, output_length );
  3654. }
  3655. exit:
  3656. psa_reset_key_attributes( &attributes );
  3657. psa_destroy_key( key );
  3658. mbedtls_free( output );
  3659. PSA_DONE( );
  3660. }
  3661. /* END_CASE */
  3662. /* BEGIN_CASE */
  3663. void asymmetric_decrypt_fail( int key_type_arg,
  3664. data_t *key_data,
  3665. int alg_arg,
  3666. data_t *input_data,
  3667. data_t *label,
  3668. int output_size_arg,
  3669. int expected_status_arg )
  3670. {
  3671. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3672. psa_key_type_t key_type = key_type_arg;
  3673. psa_algorithm_t alg = alg_arg;
  3674. unsigned char *output = NULL;
  3675. size_t output_size = output_size_arg;
  3676. size_t output_length = ~0;
  3677. psa_status_t actual_status;
  3678. psa_status_t expected_status = expected_status_arg;
  3679. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3680. ASSERT_ALLOC( output, output_size );
  3681. PSA_ASSERT( psa_crypto_init( ) );
  3682. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
  3683. psa_set_key_algorithm( &attributes, alg );
  3684. psa_set_key_type( &attributes, key_type );
  3685. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  3686. &key ) );
  3687. actual_status = psa_asymmetric_decrypt( key, alg,
  3688. input_data->x, input_data->len,
  3689. label->x, label->len,
  3690. output, output_size,
  3691. &output_length );
  3692. TEST_EQUAL( actual_status, expected_status );
  3693. TEST_ASSERT( output_length <= output_size );
  3694. /* If the label is empty, the test framework puts a non-null pointer
  3695. * in label->x. Test that a null pointer works as well. */
  3696. if( label->len == 0 )
  3697. {
  3698. output_length = ~0;
  3699. if( output_size != 0 )
  3700. memset( output, 0, output_size );
  3701. actual_status = psa_asymmetric_decrypt( key, alg,
  3702. input_data->x, input_data->len,
  3703. NULL, label->len,
  3704. output, output_size,
  3705. &output_length );
  3706. TEST_EQUAL( actual_status, expected_status );
  3707. TEST_ASSERT( output_length <= output_size );
  3708. }
  3709. exit:
  3710. psa_reset_key_attributes( &attributes );
  3711. psa_destroy_key( key );
  3712. mbedtls_free( output );
  3713. PSA_DONE( );
  3714. }
  3715. /* END_CASE */
  3716. /* BEGIN_CASE */
  3717. void key_derivation_init( )
  3718. {
  3719. /* Test each valid way of initializing the object, except for `= {0}`, as
  3720. * Clang 5 complains when `-Wmissing-field-initializers` is used, even
  3721. * though it's OK by the C standard. We could test for this, but we'd need
  3722. * to supress the Clang warning for the test. */
  3723. size_t capacity;
  3724. psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
  3725. psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
  3726. psa_key_derivation_operation_t zero;
  3727. memset( &zero, 0, sizeof( zero ) );
  3728. /* A default operation should not be able to report its capacity. */
  3729. TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
  3730. PSA_ERROR_BAD_STATE );
  3731. TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
  3732. PSA_ERROR_BAD_STATE );
  3733. TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
  3734. PSA_ERROR_BAD_STATE );
  3735. /* A default operation should be abortable without error. */
  3736. PSA_ASSERT( psa_key_derivation_abort(&func) );
  3737. PSA_ASSERT( psa_key_derivation_abort(&init) );
  3738. PSA_ASSERT( psa_key_derivation_abort(&zero) );
  3739. }
  3740. /* END_CASE */
  3741. /* BEGIN_CASE */
  3742. void derive_setup( int alg_arg, int expected_status_arg )
  3743. {
  3744. psa_algorithm_t alg = alg_arg;
  3745. psa_status_t expected_status = expected_status_arg;
  3746. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  3747. PSA_ASSERT( psa_crypto_init( ) );
  3748. TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
  3749. expected_status );
  3750. exit:
  3751. psa_key_derivation_abort( &operation );
  3752. PSA_DONE( );
  3753. }
  3754. /* END_CASE */
  3755. /* BEGIN_CASE */
  3756. void derive_set_capacity( int alg_arg, int capacity_arg,
  3757. int expected_status_arg )
  3758. {
  3759. psa_algorithm_t alg = alg_arg;
  3760. size_t capacity = capacity_arg;
  3761. psa_status_t expected_status = expected_status_arg;
  3762. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  3763. PSA_ASSERT( psa_crypto_init( ) );
  3764. PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
  3765. TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
  3766. expected_status );
  3767. exit:
  3768. psa_key_derivation_abort( &operation );
  3769. PSA_DONE( );
  3770. }
  3771. /* END_CASE */
  3772. /* BEGIN_CASE */
  3773. void derive_input( int alg_arg,
  3774. int step_arg1, int key_type_arg1, data_t *input1,
  3775. int expected_status_arg1,
  3776. int step_arg2, int key_type_arg2, data_t *input2,
  3777. int expected_status_arg2,
  3778. int step_arg3, int key_type_arg3, data_t *input3,
  3779. int expected_status_arg3,
  3780. int output_key_type_arg, int expected_output_status_arg )
  3781. {
  3782. psa_algorithm_t alg = alg_arg;
  3783. psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
  3784. psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
  3785. psa_status_t expected_statuses[] = {expected_status_arg1,
  3786. expected_status_arg2,
  3787. expected_status_arg3};
  3788. data_t *inputs[] = {input1, input2, input3};
  3789. mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
  3790. MBEDTLS_SVC_KEY_ID_INIT,
  3791. MBEDTLS_SVC_KEY_ID_INIT };
  3792. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  3793. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3794. size_t i;
  3795. psa_key_type_t output_key_type = output_key_type_arg;
  3796. mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
  3797. psa_status_t expected_output_status = expected_output_status_arg;
  3798. psa_status_t actual_output_status;
  3799. PSA_ASSERT( psa_crypto_init( ) );
  3800. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
  3801. psa_set_key_algorithm( &attributes, alg );
  3802. PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
  3803. for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
  3804. {
  3805. mbedtls_test_set_step( i );
  3806. if( steps[i] == 0 )
  3807. {
  3808. /* Skip this step */
  3809. }
  3810. else if( key_types[i] != PSA_KEY_TYPE_NONE )
  3811. {
  3812. psa_set_key_type( &attributes, key_types[i] );
  3813. PSA_ASSERT( psa_import_key( &attributes,
  3814. inputs[i]->x, inputs[i]->len,
  3815. &keys[i] ) );
  3816. if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
  3817. steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
  3818. {
  3819. // When taking a private key as secret input, use key agreement
  3820. // to add the shared secret to the derivation
  3821. TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
  3822. &operation, keys[i] ),
  3823. expected_statuses[i] );
  3824. }
  3825. else
  3826. {
  3827. TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
  3828. keys[i] ),
  3829. expected_statuses[i] );
  3830. }
  3831. }
  3832. else
  3833. {
  3834. TEST_EQUAL( psa_key_derivation_input_bytes(
  3835. &operation, steps[i],
  3836. inputs[i]->x, inputs[i]->len ),
  3837. expected_statuses[i] );
  3838. }
  3839. }
  3840. if( output_key_type != PSA_KEY_TYPE_NONE )
  3841. {
  3842. psa_reset_key_attributes( &attributes );
  3843. psa_set_key_type( &attributes, output_key_type );
  3844. psa_set_key_bits( &attributes, 8 );
  3845. actual_output_status =
  3846. psa_key_derivation_output_key( &attributes, &operation,
  3847. &output_key );
  3848. }
  3849. else
  3850. {
  3851. uint8_t buffer[1];
  3852. actual_output_status =
  3853. psa_key_derivation_output_bytes( &operation,
  3854. buffer, sizeof( buffer ) );
  3855. }
  3856. TEST_EQUAL( actual_output_status, expected_output_status );
  3857. exit:
  3858. psa_key_derivation_abort( &operation );
  3859. for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
  3860. psa_destroy_key( keys[i] );
  3861. psa_destroy_key( output_key );
  3862. PSA_DONE( );
  3863. }
  3864. /* END_CASE */
  3865. /* BEGIN_CASE */
  3866. void derive_over_capacity( int alg_arg )
  3867. {
  3868. psa_algorithm_t alg = alg_arg;
  3869. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  3870. size_t key_type = PSA_KEY_TYPE_DERIVE;
  3871. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  3872. unsigned char input1[] = "Input 1";
  3873. size_t input1_length = sizeof( input1 );
  3874. unsigned char input2[] = "Input 2";
  3875. size_t input2_length = sizeof( input2 );
  3876. uint8_t buffer[42];
  3877. size_t capacity = sizeof( buffer );
  3878. const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3879. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3880. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
  3881. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3882. PSA_ASSERT( psa_crypto_init( ) );
  3883. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
  3884. psa_set_key_algorithm( &attributes, alg );
  3885. psa_set_key_type( &attributes, key_type );
  3886. PSA_ASSERT( psa_import_key( &attributes,
  3887. key_data, sizeof( key_data ),
  3888. &key ) );
  3889. /* valid key derivation */
  3890. if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
  3891. input1, input1_length,
  3892. input2, input2_length,
  3893. capacity ) )
  3894. goto exit;
  3895. /* state of operation shouldn't allow additional generation */
  3896. TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
  3897. PSA_ERROR_BAD_STATE );
  3898. PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
  3899. TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
  3900. PSA_ERROR_INSUFFICIENT_DATA );
  3901. exit:
  3902. psa_key_derivation_abort( &operation );
  3903. psa_destroy_key( key );
  3904. PSA_DONE( );
  3905. }
  3906. /* END_CASE */
  3907. /* BEGIN_CASE */
  3908. void derive_actions_without_setup( )
  3909. {
  3910. uint8_t output_buffer[16];
  3911. size_t buffer_size = 16;
  3912. size_t capacity = 0;
  3913. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  3914. TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
  3915. output_buffer, buffer_size )
  3916. == PSA_ERROR_BAD_STATE );
  3917. TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
  3918. == PSA_ERROR_BAD_STATE );
  3919. PSA_ASSERT( psa_key_derivation_abort( &operation ) );
  3920. TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
  3921. output_buffer, buffer_size )
  3922. == PSA_ERROR_BAD_STATE );
  3923. TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
  3924. == PSA_ERROR_BAD_STATE );
  3925. exit:
  3926. psa_key_derivation_abort( &operation );
  3927. }
  3928. /* END_CASE */
  3929. /* BEGIN_CASE */
  3930. void derive_output( int alg_arg,
  3931. int step1_arg, data_t *input1,
  3932. int step2_arg, data_t *input2,
  3933. int step3_arg, data_t *input3,
  3934. int requested_capacity_arg,
  3935. data_t *expected_output1,
  3936. data_t *expected_output2 )
  3937. {
  3938. psa_algorithm_t alg = alg_arg;
  3939. psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
  3940. data_t *inputs[] = {input1, input2, input3};
  3941. mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
  3942. MBEDTLS_SVC_KEY_ID_INIT,
  3943. MBEDTLS_SVC_KEY_ID_INIT };
  3944. size_t requested_capacity = requested_capacity_arg;
  3945. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  3946. uint8_t *expected_outputs[2] =
  3947. {expected_output1->x, expected_output2->x};
  3948. size_t output_sizes[2] =
  3949. {expected_output1->len, expected_output2->len};
  3950. size_t output_buffer_size = 0;
  3951. uint8_t *output_buffer = NULL;
  3952. size_t expected_capacity;
  3953. size_t current_capacity;
  3954. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3955. psa_status_t status;
  3956. size_t i;
  3957. for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
  3958. {
  3959. if( output_sizes[i] > output_buffer_size )
  3960. output_buffer_size = output_sizes[i];
  3961. if( output_sizes[i] == 0 )
  3962. expected_outputs[i] = NULL;
  3963. }
  3964. ASSERT_ALLOC( output_buffer, output_buffer_size );
  3965. PSA_ASSERT( psa_crypto_init( ) );
  3966. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
  3967. psa_set_key_algorithm( &attributes, alg );
  3968. psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
  3969. /* Extraction phase. */
  3970. PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
  3971. PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
  3972. requested_capacity ) );
  3973. for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
  3974. {
  3975. switch( steps[i] )
  3976. {
  3977. case 0:
  3978. break;
  3979. case PSA_KEY_DERIVATION_INPUT_SECRET:
  3980. PSA_ASSERT( psa_import_key( &attributes,
  3981. inputs[i]->x, inputs[i]->len,
  3982. &keys[i] ) );
  3983. if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
  3984. {
  3985. PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
  3986. TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
  3987. PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
  3988. }
  3989. PSA_ASSERT( psa_key_derivation_input_key(
  3990. &operation, steps[i], keys[i] ) );
  3991. break;
  3992. default:
  3993. PSA_ASSERT( psa_key_derivation_input_bytes(
  3994. &operation, steps[i],
  3995. inputs[i]->x, inputs[i]->len ) );
  3996. break;
  3997. }
  3998. }
  3999. PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
  4000. &current_capacity ) );
  4001. TEST_EQUAL( current_capacity, requested_capacity );
  4002. expected_capacity = requested_capacity;
  4003. /* Expansion phase. */
  4004. for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
  4005. {
  4006. /* Read some bytes. */
  4007. status = psa_key_derivation_output_bytes( &operation,
  4008. output_buffer, output_sizes[i] );
  4009. if( expected_capacity == 0 && output_sizes[i] == 0 )
  4010. {
  4011. /* Reading 0 bytes when 0 bytes are available can go either way. */
  4012. TEST_ASSERT( status == PSA_SUCCESS ||
  4013. status == PSA_ERROR_INSUFFICIENT_DATA );
  4014. continue;
  4015. }
  4016. else if( expected_capacity == 0 ||
  4017. output_sizes[i] > expected_capacity )
  4018. {
  4019. /* Capacity exceeded. */
  4020. TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
  4021. expected_capacity = 0;
  4022. continue;
  4023. }
  4024. /* Success. Check the read data. */
  4025. PSA_ASSERT( status );
  4026. if( output_sizes[i] != 0 )
  4027. ASSERT_COMPARE( output_buffer, output_sizes[i],
  4028. expected_outputs[i], output_sizes[i] );
  4029. /* Check the operation status. */
  4030. expected_capacity -= output_sizes[i];
  4031. PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
  4032. &current_capacity ) );
  4033. TEST_EQUAL( expected_capacity, current_capacity );
  4034. }
  4035. PSA_ASSERT( psa_key_derivation_abort( &operation ) );
  4036. exit:
  4037. mbedtls_free( output_buffer );
  4038. psa_key_derivation_abort( &operation );
  4039. for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
  4040. psa_destroy_key( keys[i] );
  4041. PSA_DONE( );
  4042. }
  4043. /* END_CASE */
  4044. /* BEGIN_CASE */
  4045. void derive_full( int alg_arg,
  4046. data_t *key_data,
  4047. data_t *input1,
  4048. data_t *input2,
  4049. int requested_capacity_arg )
  4050. {
  4051. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4052. psa_algorithm_t alg = alg_arg;
  4053. size_t requested_capacity = requested_capacity_arg;
  4054. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  4055. unsigned char output_buffer[16];
  4056. size_t expected_capacity = requested_capacity;
  4057. size_t current_capacity;
  4058. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4059. PSA_ASSERT( psa_crypto_init( ) );
  4060. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
  4061. psa_set_key_algorithm( &attributes, alg );
  4062. psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
  4063. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  4064. &key ) );
  4065. if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
  4066. input1->x, input1->len,
  4067. input2->x, input2->len,
  4068. requested_capacity ) )
  4069. goto exit;
  4070. PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
  4071. &current_capacity ) );
  4072. TEST_EQUAL( current_capacity, expected_capacity );
  4073. /* Expansion phase. */
  4074. while( current_capacity > 0 )
  4075. {
  4076. size_t read_size = sizeof( output_buffer );
  4077. if( read_size > current_capacity )
  4078. read_size = current_capacity;
  4079. PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
  4080. output_buffer,
  4081. read_size ) );
  4082. expected_capacity -= read_size;
  4083. PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
  4084. &current_capacity ) );
  4085. TEST_EQUAL( current_capacity, expected_capacity );
  4086. }
  4087. /* Check that the operation refuses to go over capacity. */
  4088. TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
  4089. PSA_ERROR_INSUFFICIENT_DATA );
  4090. PSA_ASSERT( psa_key_derivation_abort( &operation ) );
  4091. exit:
  4092. psa_key_derivation_abort( &operation );
  4093. psa_destroy_key( key );
  4094. PSA_DONE( );
  4095. }
  4096. /* END_CASE */
  4097. /* BEGIN_CASE */
  4098. void derive_key_exercise( int alg_arg,
  4099. data_t *key_data,
  4100. data_t *input1,
  4101. data_t *input2,
  4102. int derived_type_arg,
  4103. int derived_bits_arg,
  4104. int derived_usage_arg,
  4105. int derived_alg_arg )
  4106. {
  4107. mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
  4108. mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
  4109. psa_algorithm_t alg = alg_arg;
  4110. psa_key_type_t derived_type = derived_type_arg;
  4111. size_t derived_bits = derived_bits_arg;
  4112. psa_key_usage_t derived_usage = derived_usage_arg;
  4113. psa_algorithm_t derived_alg = derived_alg_arg;
  4114. size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
  4115. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  4116. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4117. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  4118. PSA_ASSERT( psa_crypto_init( ) );
  4119. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
  4120. psa_set_key_algorithm( &attributes, alg );
  4121. psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
  4122. PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
  4123. &base_key ) );
  4124. /* Derive a key. */
  4125. if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
  4126. input1->x, input1->len,
  4127. input2->x, input2->len,
  4128. capacity ) )
  4129. goto exit;
  4130. psa_set_key_usage_flags( &attributes, derived_usage );
  4131. psa_set_key_algorithm( &attributes, derived_alg );
  4132. psa_set_key_type( &attributes, derived_type );
  4133. psa_set_key_bits( &attributes, derived_bits );
  4134. PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
  4135. &derived_key ) );
  4136. /* Test the key information */
  4137. PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
  4138. TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
  4139. TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
  4140. /* Exercise the derived key. */
  4141. if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
  4142. goto exit;
  4143. exit:
  4144. /*
  4145. * Key attributes may have been returned by psa_get_key_attributes()
  4146. * thus reset them as required.
  4147. */
  4148. psa_reset_key_attributes( &got_attributes );
  4149. psa_key_derivation_abort( &operation );
  4150. psa_destroy_key( base_key );
  4151. psa_destroy_key( derived_key );
  4152. PSA_DONE( );
  4153. }
  4154. /* END_CASE */
  4155. /* BEGIN_CASE */
  4156. void derive_key_export( int alg_arg,
  4157. data_t *key_data,
  4158. data_t *input1,
  4159. data_t *input2,
  4160. int bytes1_arg,
  4161. int bytes2_arg )
  4162. {
  4163. mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
  4164. mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
  4165. psa_algorithm_t alg = alg_arg;
  4166. size_t bytes1 = bytes1_arg;
  4167. size_t bytes2 = bytes2_arg;
  4168. size_t capacity = bytes1 + bytes2;
  4169. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  4170. uint8_t *output_buffer = NULL;
  4171. uint8_t *export_buffer = NULL;
  4172. psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
  4173. psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
  4174. size_t length;
  4175. ASSERT_ALLOC( output_buffer, capacity );
  4176. ASSERT_ALLOC( export_buffer, capacity );
  4177. PSA_ASSERT( psa_crypto_init( ) );
  4178. psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
  4179. psa_set_key_algorithm( &base_attributes, alg );
  4180. psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
  4181. PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
  4182. &base_key ) );
  4183. /* Derive some material and output it. */
  4184. if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
  4185. input1->x, input1->len,
  4186. input2->x, input2->len,
  4187. capacity ) )
  4188. goto exit;
  4189. PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
  4190. output_buffer,
  4191. capacity ) );
  4192. PSA_ASSERT( psa_key_derivation_abort( &operation ) );
  4193. /* Derive the same output again, but this time store it in key objects. */
  4194. if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
  4195. input1->x, input1->len,
  4196. input2->x, input2->len,
  4197. capacity ) )
  4198. goto exit;
  4199. psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
  4200. psa_set_key_algorithm( &derived_attributes, 0 );
  4201. psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
  4202. psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
  4203. PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
  4204. &derived_key ) );
  4205. PSA_ASSERT( psa_export_key( derived_key,
  4206. export_buffer, bytes1,
  4207. &length ) );
  4208. TEST_EQUAL( length, bytes1 );
  4209. PSA_ASSERT( psa_destroy_key( derived_key ) );
  4210. psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
  4211. PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
  4212. &derived_key ) );
  4213. PSA_ASSERT( psa_export_key( derived_key,
  4214. export_buffer + bytes1, bytes2,
  4215. &length ) );
  4216. TEST_EQUAL( length, bytes2 );
  4217. /* Compare the outputs from the two runs. */
  4218. ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
  4219. export_buffer, capacity );
  4220. exit:
  4221. mbedtls_free( output_buffer );
  4222. mbedtls_free( export_buffer );
  4223. psa_key_derivation_abort( &operation );
  4224. psa_destroy_key( base_key );
  4225. psa_destroy_key( derived_key );
  4226. PSA_DONE( );
  4227. }
  4228. /* END_CASE */
  4229. /* BEGIN_CASE */
  4230. void derive_key( int alg_arg,
  4231. data_t *key_data, data_t *input1, data_t *input2,
  4232. int type_arg, int bits_arg,
  4233. int expected_status_arg,
  4234. int is_large_output )
  4235. {
  4236. mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
  4237. mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
  4238. psa_algorithm_t alg = alg_arg;
  4239. psa_key_type_t type = type_arg;
  4240. size_t bits = bits_arg;
  4241. psa_status_t expected_status = expected_status_arg;
  4242. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  4243. psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
  4244. psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
  4245. PSA_ASSERT( psa_crypto_init( ) );
  4246. psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
  4247. psa_set_key_algorithm( &base_attributes, alg );
  4248. psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
  4249. PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
  4250. &base_key ) );
  4251. if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
  4252. input1->x, input1->len,
  4253. input2->x, input2->len,
  4254. SIZE_MAX ) )
  4255. goto exit;
  4256. psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
  4257. psa_set_key_algorithm( &derived_attributes, 0 );
  4258. psa_set_key_type( &derived_attributes, type );
  4259. psa_set_key_bits( &derived_attributes, bits );
  4260. psa_status_t status =
  4261. psa_key_derivation_output_key( &derived_attributes,
  4262. &operation,
  4263. &derived_key );
  4264. if( is_large_output > 0 )
  4265. TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
  4266. TEST_EQUAL( status, expected_status );
  4267. exit:
  4268. psa_key_derivation_abort( &operation );
  4269. psa_destroy_key( base_key );
  4270. psa_destroy_key( derived_key );
  4271. PSA_DONE( );
  4272. }
  4273. /* END_CASE */
  4274. /* BEGIN_CASE */
  4275. void key_agreement_setup( int alg_arg,
  4276. int our_key_type_arg, int our_key_alg_arg,
  4277. data_t *our_key_data, data_t *peer_key_data,
  4278. int expected_status_arg )
  4279. {
  4280. mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
  4281. psa_algorithm_t alg = alg_arg;
  4282. psa_algorithm_t our_key_alg = our_key_alg_arg;
  4283. psa_key_type_t our_key_type = our_key_type_arg;
  4284. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  4285. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4286. psa_status_t expected_status = expected_status_arg;
  4287. psa_status_t status;
  4288. PSA_ASSERT( psa_crypto_init( ) );
  4289. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
  4290. psa_set_key_algorithm( &attributes, our_key_alg );
  4291. psa_set_key_type( &attributes, our_key_type );
  4292. PSA_ASSERT( psa_import_key( &attributes,
  4293. our_key_data->x, our_key_data->len,
  4294. &our_key ) );
  4295. /* The tests currently include inputs that should fail at either step.
  4296. * Test cases that fail at the setup step should be changed to call
  4297. * key_derivation_setup instead, and this function should be renamed
  4298. * to key_agreement_fail. */
  4299. status = psa_key_derivation_setup( &operation, alg );
  4300. if( status == PSA_SUCCESS )
  4301. {
  4302. TEST_EQUAL( psa_key_derivation_key_agreement(
  4303. &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
  4304. our_key,
  4305. peer_key_data->x, peer_key_data->len ),
  4306. expected_status );
  4307. }
  4308. else
  4309. {
  4310. TEST_ASSERT( status == expected_status );
  4311. }
  4312. exit:
  4313. psa_key_derivation_abort( &operation );
  4314. psa_destroy_key( our_key );
  4315. PSA_DONE( );
  4316. }
  4317. /* END_CASE */
  4318. /* BEGIN_CASE */
  4319. void raw_key_agreement( int alg_arg,
  4320. int our_key_type_arg, data_t *our_key_data,
  4321. data_t *peer_key_data,
  4322. data_t *expected_output )
  4323. {
  4324. mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
  4325. psa_algorithm_t alg = alg_arg;
  4326. psa_key_type_t our_key_type = our_key_type_arg;
  4327. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4328. unsigned char *output = NULL;
  4329. size_t output_length = ~0;
  4330. size_t key_bits;
  4331. ASSERT_ALLOC( output, expected_output->len );
  4332. PSA_ASSERT( psa_crypto_init( ) );
  4333. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
  4334. psa_set_key_algorithm( &attributes, alg );
  4335. psa_set_key_type( &attributes, our_key_type );
  4336. PSA_ASSERT( psa_import_key( &attributes,
  4337. our_key_data->x, our_key_data->len,
  4338. &our_key ) );
  4339. PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
  4340. key_bits = psa_get_key_bits( &attributes );
  4341. PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
  4342. peer_key_data->x, peer_key_data->len,
  4343. output, expected_output->len,
  4344. &output_length ) );
  4345. ASSERT_COMPARE( output, output_length,
  4346. expected_output->x, expected_output->len );
  4347. TEST_ASSERT( output_length <=
  4348. PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
  4349. TEST_ASSERT( output_length <=
  4350. PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
  4351. exit:
  4352. mbedtls_free( output );
  4353. psa_destroy_key( our_key );
  4354. PSA_DONE( );
  4355. }
  4356. /* END_CASE */
  4357. /* BEGIN_CASE */
  4358. void key_agreement_capacity( int alg_arg,
  4359. int our_key_type_arg, data_t *our_key_data,
  4360. data_t *peer_key_data,
  4361. int expected_capacity_arg )
  4362. {
  4363. mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
  4364. psa_algorithm_t alg = alg_arg;
  4365. psa_key_type_t our_key_type = our_key_type_arg;
  4366. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  4367. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4368. size_t actual_capacity;
  4369. unsigned char output[16];
  4370. PSA_ASSERT( psa_crypto_init( ) );
  4371. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
  4372. psa_set_key_algorithm( &attributes, alg );
  4373. psa_set_key_type( &attributes, our_key_type );
  4374. PSA_ASSERT( psa_import_key( &attributes,
  4375. our_key_data->x, our_key_data->len,
  4376. &our_key ) );
  4377. PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
  4378. PSA_ASSERT( psa_key_derivation_key_agreement(
  4379. &operation,
  4380. PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
  4381. peer_key_data->x, peer_key_data->len ) );
  4382. if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
  4383. {
  4384. /* The test data is for info="" */
  4385. PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
  4386. PSA_KEY_DERIVATION_INPUT_INFO,
  4387. NULL, 0 ) );
  4388. }
  4389. /* Test the advertized capacity. */
  4390. PSA_ASSERT( psa_key_derivation_get_capacity(
  4391. &operation, &actual_capacity ) );
  4392. TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
  4393. /* Test the actual capacity by reading the output. */
  4394. while( actual_capacity > sizeof( output ) )
  4395. {
  4396. PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
  4397. output, sizeof( output ) ) );
  4398. actual_capacity -= sizeof( output );
  4399. }
  4400. PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
  4401. output, actual_capacity ) );
  4402. TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
  4403. PSA_ERROR_INSUFFICIENT_DATA );
  4404. exit:
  4405. psa_key_derivation_abort( &operation );
  4406. psa_destroy_key( our_key );
  4407. PSA_DONE( );
  4408. }
  4409. /* END_CASE */
  4410. /* BEGIN_CASE */
  4411. void key_agreement_output( int alg_arg,
  4412. int our_key_type_arg, data_t *our_key_data,
  4413. data_t *peer_key_data,
  4414. data_t *expected_output1, data_t *expected_output2 )
  4415. {
  4416. mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
  4417. psa_algorithm_t alg = alg_arg;
  4418. psa_key_type_t our_key_type = our_key_type_arg;
  4419. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  4420. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4421. uint8_t *actual_output = NULL;
  4422. ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
  4423. expected_output2->len ) );
  4424. PSA_ASSERT( psa_crypto_init( ) );
  4425. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
  4426. psa_set_key_algorithm( &attributes, alg );
  4427. psa_set_key_type( &attributes, our_key_type );
  4428. PSA_ASSERT( psa_import_key( &attributes,
  4429. our_key_data->x, our_key_data->len,
  4430. &our_key ) );
  4431. PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
  4432. PSA_ASSERT( psa_key_derivation_key_agreement(
  4433. &operation,
  4434. PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
  4435. peer_key_data->x, peer_key_data->len ) );
  4436. if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
  4437. {
  4438. /* The test data is for info="" */
  4439. PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
  4440. PSA_KEY_DERIVATION_INPUT_INFO,
  4441. NULL, 0 ) );
  4442. }
  4443. PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
  4444. actual_output,
  4445. expected_output1->len ) );
  4446. ASSERT_COMPARE( actual_output, expected_output1->len,
  4447. expected_output1->x, expected_output1->len );
  4448. if( expected_output2->len != 0 )
  4449. {
  4450. PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
  4451. actual_output,
  4452. expected_output2->len ) );
  4453. ASSERT_COMPARE( actual_output, expected_output2->len,
  4454. expected_output2->x, expected_output2->len );
  4455. }
  4456. exit:
  4457. psa_key_derivation_abort( &operation );
  4458. psa_destroy_key( our_key );
  4459. PSA_DONE( );
  4460. mbedtls_free( actual_output );
  4461. }
  4462. /* END_CASE */
  4463. /* BEGIN_CASE */
  4464. void generate_random( int bytes_arg )
  4465. {
  4466. size_t bytes = bytes_arg;
  4467. unsigned char *output = NULL;
  4468. unsigned char *changed = NULL;
  4469. size_t i;
  4470. unsigned run;
  4471. TEST_ASSERT( bytes_arg >= 0 );
  4472. ASSERT_ALLOC( output, bytes );
  4473. ASSERT_ALLOC( changed, bytes );
  4474. PSA_ASSERT( psa_crypto_init( ) );
  4475. /* Run several times, to ensure that every output byte will be
  4476. * nonzero at least once with overwhelming probability
  4477. * (2^(-8*number_of_runs)). */
  4478. for( run = 0; run < 10; run++ )
  4479. {
  4480. if( bytes != 0 )
  4481. memset( output, 0, bytes );
  4482. PSA_ASSERT( psa_generate_random( output, bytes ) );
  4483. for( i = 0; i < bytes; i++ )
  4484. {
  4485. if( output[i] != 0 )
  4486. ++changed[i];
  4487. }
  4488. }
  4489. /* Check that every byte was changed to nonzero at least once. This
  4490. * validates that psa_generate_random is overwriting every byte of
  4491. * the output buffer. */
  4492. for( i = 0; i < bytes; i++ )
  4493. {
  4494. TEST_ASSERT( changed[i] != 0 );
  4495. }
  4496. exit:
  4497. PSA_DONE( );
  4498. mbedtls_free( output );
  4499. mbedtls_free( changed );
  4500. }
  4501. /* END_CASE */
  4502. /* BEGIN_CASE */
  4503. void generate_key( int type_arg,
  4504. int bits_arg,
  4505. int usage_arg,
  4506. int alg_arg,
  4507. int expected_status_arg,
  4508. int is_large_key )
  4509. {
  4510. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4511. psa_key_type_t type = type_arg;
  4512. psa_key_usage_t usage = usage_arg;
  4513. size_t bits = bits_arg;
  4514. psa_algorithm_t alg = alg_arg;
  4515. psa_status_t expected_status = expected_status_arg;
  4516. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4517. psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
  4518. PSA_ASSERT( psa_crypto_init( ) );
  4519. psa_set_key_usage_flags( &attributes, usage );
  4520. psa_set_key_algorithm( &attributes, alg );
  4521. psa_set_key_type( &attributes, type );
  4522. psa_set_key_bits( &attributes, bits );
  4523. /* Generate a key */
  4524. psa_status_t status = psa_generate_key( &attributes, &key );
  4525. if( is_large_key > 0 )
  4526. TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
  4527. TEST_EQUAL( status , expected_status );
  4528. if( expected_status != PSA_SUCCESS )
  4529. goto exit;
  4530. /* Test the key information */
  4531. PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
  4532. TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
  4533. TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
  4534. /* Do something with the key according to its type and permitted usage. */
  4535. if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
  4536. goto exit;
  4537. exit:
  4538. /*
  4539. * Key attributes may have been returned by psa_get_key_attributes()
  4540. * thus reset them as required.
  4541. */
  4542. psa_reset_key_attributes( &got_attributes );
  4543. psa_destroy_key( key );
  4544. PSA_DONE( );
  4545. }
  4546. /* END_CASE */
  4547. /* 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 */
  4548. void generate_key_rsa( int bits_arg,
  4549. data_t *e_arg,
  4550. int expected_status_arg )
  4551. {
  4552. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4553. psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
  4554. size_t bits = bits_arg;
  4555. psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
  4556. psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
  4557. psa_status_t expected_status = expected_status_arg;
  4558. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4559. uint8_t *exported = NULL;
  4560. size_t exported_size =
  4561. PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
  4562. size_t exported_length = SIZE_MAX;
  4563. uint8_t *e_read_buffer = NULL;
  4564. int is_default_public_exponent = 0;
  4565. size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
  4566. size_t e_read_length = SIZE_MAX;
  4567. if( e_arg->len == 0 ||
  4568. ( e_arg->len == 3 &&
  4569. e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
  4570. {
  4571. is_default_public_exponent = 1;
  4572. e_read_size = 0;
  4573. }
  4574. ASSERT_ALLOC( e_read_buffer, e_read_size );
  4575. ASSERT_ALLOC( exported, exported_size );
  4576. PSA_ASSERT( psa_crypto_init( ) );
  4577. psa_set_key_usage_flags( &attributes, usage );
  4578. psa_set_key_algorithm( &attributes, alg );
  4579. PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
  4580. e_arg->x, e_arg->len ) );
  4581. psa_set_key_bits( &attributes, bits );
  4582. /* Generate a key */
  4583. TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
  4584. if( expected_status != PSA_SUCCESS )
  4585. goto exit;
  4586. /* Test the key information */
  4587. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  4588. TEST_EQUAL( psa_get_key_type( &attributes ), type );
  4589. TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
  4590. PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
  4591. e_read_buffer, e_read_size,
  4592. &e_read_length ) );
  4593. if( is_default_public_exponent )
  4594. TEST_EQUAL( e_read_length, 0 );
  4595. else
  4596. ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
  4597. /* Do something with the key according to its type and permitted usage. */
  4598. if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
  4599. goto exit;
  4600. /* Export the key and check the public exponent. */
  4601. PSA_ASSERT( psa_export_public_key( key,
  4602. exported, exported_size,
  4603. &exported_length ) );
  4604. {
  4605. uint8_t *p = exported;
  4606. uint8_t *end = exported + exported_length;
  4607. size_t len;
  4608. /* RSAPublicKey ::= SEQUENCE {
  4609. * modulus INTEGER, -- n
  4610. * publicExponent INTEGER } -- e
  4611. */
  4612. TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
  4613. MBEDTLS_ASN1_SEQUENCE |
  4614. MBEDTLS_ASN1_CONSTRUCTED ) );
  4615. TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
  4616. TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
  4617. MBEDTLS_ASN1_INTEGER ) );
  4618. if( len >= 1 && p[0] == 0 )
  4619. {
  4620. ++p;
  4621. --len;
  4622. }
  4623. if( e_arg->len == 0 )
  4624. {
  4625. TEST_EQUAL( len, 3 );
  4626. TEST_EQUAL( p[0], 1 );
  4627. TEST_EQUAL( p[1], 0 );
  4628. TEST_EQUAL( p[2], 1 );
  4629. }
  4630. else
  4631. ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
  4632. }
  4633. exit:
  4634. /*
  4635. * Key attributes may have been returned by psa_get_key_attributes() or
  4636. * set by psa_set_key_domain_parameters() thus reset them as required.
  4637. */
  4638. psa_reset_key_attributes( &attributes );
  4639. psa_destroy_key( key );
  4640. PSA_DONE( );
  4641. mbedtls_free( e_read_buffer );
  4642. mbedtls_free( exported );
  4643. }
  4644. /* END_CASE */
  4645. /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
  4646. void persistent_key_load_key_from_storage( data_t *data,
  4647. int type_arg, int bits_arg,
  4648. int usage_flags_arg, int alg_arg,
  4649. int generation_method )
  4650. {
  4651. mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
  4652. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  4653. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  4654. mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
  4655. psa_key_type_t type = type_arg;
  4656. size_t bits = bits_arg;
  4657. psa_key_usage_t usage_flags = usage_flags_arg;
  4658. psa_algorithm_t alg = alg_arg;
  4659. psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  4660. unsigned char *first_export = NULL;
  4661. unsigned char *second_export = NULL;
  4662. size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
  4663. size_t first_exported_length;
  4664. size_t second_exported_length;
  4665. if( usage_flags & PSA_KEY_USAGE_EXPORT )
  4666. {
  4667. ASSERT_ALLOC( first_export, export_size );
  4668. ASSERT_ALLOC( second_export, export_size );
  4669. }
  4670. PSA_ASSERT( psa_crypto_init() );
  4671. psa_set_key_id( &attributes, key_id );
  4672. psa_set_key_usage_flags( &attributes, usage_flags );
  4673. psa_set_key_algorithm( &attributes, alg );
  4674. psa_set_key_type( &attributes, type );
  4675. psa_set_key_bits( &attributes, bits );
  4676. switch( generation_method )
  4677. {
  4678. case IMPORT_KEY:
  4679. /* Import the key */
  4680. PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
  4681. &key ) );
  4682. break;
  4683. case GENERATE_KEY:
  4684. /* Generate a key */
  4685. PSA_ASSERT( psa_generate_key( &attributes, &key ) );
  4686. break;
  4687. case DERIVE_KEY:
  4688. #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
  4689. {
  4690. /* Create base key */
  4691. psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
  4692. psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
  4693. psa_set_key_usage_flags( &base_attributes,
  4694. PSA_KEY_USAGE_DERIVE );
  4695. psa_set_key_algorithm( &base_attributes, derive_alg );
  4696. psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
  4697. PSA_ASSERT( psa_import_key( &base_attributes,
  4698. data->x, data->len,
  4699. &base_key ) );
  4700. /* Derive a key. */
  4701. PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
  4702. PSA_ASSERT( psa_key_derivation_input_key(
  4703. &operation,
  4704. PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
  4705. PSA_ASSERT( psa_key_derivation_input_bytes(
  4706. &operation, PSA_KEY_DERIVATION_INPUT_INFO,
  4707. NULL, 0 ) );
  4708. PSA_ASSERT( psa_key_derivation_output_key( &attributes,
  4709. &operation,
  4710. &key ) );
  4711. PSA_ASSERT( psa_key_derivation_abort( &operation ) );
  4712. PSA_ASSERT( psa_destroy_key( base_key ) );
  4713. base_key = MBEDTLS_SVC_KEY_ID_INIT;
  4714. }
  4715. #else
  4716. TEST_ASSUME( ! "KDF not supported in this configuration" );
  4717. #endif
  4718. break;
  4719. default:
  4720. TEST_ASSERT( ! "generation_method not implemented in test" );
  4721. break;
  4722. }
  4723. psa_reset_key_attributes( &attributes );
  4724. /* Export the key if permitted by the key policy. */
  4725. if( usage_flags & PSA_KEY_USAGE_EXPORT )
  4726. {
  4727. PSA_ASSERT( psa_export_key( key,
  4728. first_export, export_size,
  4729. &first_exported_length ) );
  4730. if( generation_method == IMPORT_KEY )
  4731. ASSERT_COMPARE( data->x, data->len,
  4732. first_export, first_exported_length );
  4733. }
  4734. /* Shutdown and restart */
  4735. PSA_ASSERT( psa_purge_key( key ) );
  4736. PSA_DONE();
  4737. PSA_ASSERT( psa_crypto_init() );
  4738. /* Check key slot still contains key data */
  4739. PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
  4740. TEST_ASSERT( mbedtls_svc_key_id_equal(
  4741. psa_get_key_id( &attributes ), key_id ) );
  4742. TEST_EQUAL( psa_get_key_lifetime( &attributes ),
  4743. PSA_KEY_LIFETIME_PERSISTENT );
  4744. TEST_EQUAL( psa_get_key_type( &attributes ), type );
  4745. TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
  4746. TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
  4747. mbedtls_test_update_key_usage_flags( usage_flags ) );
  4748. TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
  4749. /* Export the key again if permitted by the key policy. */
  4750. if( usage_flags & PSA_KEY_USAGE_EXPORT )
  4751. {
  4752. PSA_ASSERT( psa_export_key( key,
  4753. second_export, export_size,
  4754. &second_exported_length ) );
  4755. ASSERT_COMPARE( first_export, first_exported_length,
  4756. second_export, second_exported_length );
  4757. }
  4758. /* Do something with the key according to its type and permitted usage. */
  4759. if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
  4760. goto exit;
  4761. exit:
  4762. /*
  4763. * Key attributes may have been returned by psa_get_key_attributes()
  4764. * thus reset them as required.
  4765. */
  4766. psa_reset_key_attributes( &attributes );
  4767. mbedtls_free( first_export );
  4768. mbedtls_free( second_export );
  4769. psa_key_derivation_abort( &operation );
  4770. psa_destroy_key( base_key );
  4771. psa_destroy_key( key );
  4772. PSA_DONE();
  4773. }
  4774. /* END_CASE */